1 /*****************************************************************************
2 * dvd_udf.c: udf filesystem tools.
4 * Mainly used to find asolute logical block adress of *.ifo files. It only
5 * contains the basic udf handling functions
6 *****************************************************************************
7 * Copyright (C) 1998-2001 VideoLAN
8 * $Id: dvd_udf.c,v 1.4 2001/04/13 05:36:12 stef Exp $
10 * Author: Stéphane Borel <stef@via.ecp.fr>
13 * - dvdudf by Christian Wolff <scarabaeus@convergence.de>
14 * - fixes by Billy Biggs <vektor@dumbterm.net>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 *****************************************************************************/
31 #define MODULE_NAME dvd
32 #include "modules_inner.h"
48 #include "input_dvd.h"
54 typedef struct partition_s
57 u8 pi_volume_desc[128];
75 /* for direct data access, LSB first */
76 #define GETN1(p) ((u8)pi_data[p])
77 #define GETN2(p) ((u16)pi_data[p]|((u16)pi_data[(p)+1]<<8))
78 #define GETN4(p) ((u32)pi_data[p]|((u32)pi_data[(p)+1]<<8)|((u32)pi_data[(p)+2]<<16)|((u32)pi_data[(p)+3]<<24))
79 #define GETN(p,n,target) memcpy(target,&pi_data[p],n)
82 /*****************************************************************************
83 * UDFReadLB: reads absolute Logical Block of the disc
85 * returns number of read bytes on success, 0 on error
86 *****************************************************************************/
87 static int UDFReadLB( int i_fd, off_t i_lba, size_t i_block_count, u8 *pi_data )
94 if( lseek( i_fd, i_lba * (off_t) DVD_LB_SIZE, SEEK_SET ) < 0 )
96 intf_ErrMsg( "UDF: Postion not found" );
100 return read( i_fd, pi_data, i_block_count *DVD_LB_SIZE);
104 /*****************************************************************************
105 * UDFDecode: decode unicode encoded udf data
106 *****************************************************************************/
107 static int UDFDecode( u8 * pi_data, int i_len, char * psz_target )
112 if( !( pi_data[0] & 0x18 ) )
114 psz_target[0] = '\0';
118 if( pi_data[0] & 0x10 )
120 /* ignore MSB of unicode16 */
125 psz_target[i++] = pi_data[p+=2];
132 psz_target[i++] = pi_data[p++];
146 int UDFEntity (u8 *data, u8 *Flags, char *Identifier)
149 strncpy (Identifier, &data[1], 5);
156 /*****************************************************************************
157 * UDFDescriptor: gives a tag ID from your data to find out what it refers to
158 *****************************************************************************/
159 static int UDFDescriptor( u8 * pi_data, u16 * pi_tag_id )
161 pi_tag_id[0] = GETN2( 0 );
162 /* TODO: check CRC 'n stuff */
168 /*****************************************************************************
169 * UDFExtendAD: main volume information
170 *****************************************************************************/
171 static int UDFExtentAD (u8 * pi_data, u32 * pi_length, u32 * pi_location)
173 pi_length[0] = GETN4( 0 );
174 pi_location[0] = GETN4( 4 );
180 /*****************************************************************************
181 * UDFAD: file set information
182 *****************************************************************************/
183 static int UDFAD( u8 * pi_data, struct ad_s * p_ad, u8 i_type,
184 struct partition_s partition )
186 p_ad->i_length = GETN4( 0 );
187 p_ad->i_flags = p_ad->i_length >> 30;
188 p_ad->i_length &= 0x3FFFFFFF;
193 p_ad->i_location = GETN4( 4 );
194 /* use number of current partition */
195 p_ad->i_partition = partition.i_number;
199 p_ad->i_location = GETN4( 4 );
200 p_ad->i_partition = GETN2( 8 );
204 p_ad->i_location = GETN4( 12 );
205 p_ad->i_partition = GETN2( 16 );
213 /*****************************************************************************
214 * UDFICB: takes Information Control Block from pi_data
215 *****************************************************************************/
216 static int UDFICB( u8 * pi_data, u8 * pi_file_type, u16 * pi_flags)
218 pi_file_type[0] = GETN1( 11 );
219 pi_flags[0] = GETN2( 18 );
225 /*****************************************************************************
226 * UDFPartition: gets partition descriptor
227 *****************************************************************************/
228 static int UDFPartition( u8 * pi_data, u16 * pi_flags, u16 * pi_nb,
229 char * ps_contents, u32 * pi_start, u32 * pi_length )
231 pi_flags[0] = GETN2( 20 );
232 pi_nb[0] = GETN2( 22 );
233 GETN( 24, 32, ps_contents );
234 pi_start[0] = GETN4( 188 );
235 pi_length[0] = GETN4( 192 );
241 /*****************************************************************************
242 * UDFLogVolume: reads the volume descriptor and checks the parameters
244 * returns 0 on OK, 1 on error
245 *****************************************************************************/
246 static int UDFLogVolume(u8 * pi_data, char * p_volume_descriptor )
252 UDFDecode( &pi_data[84], 128, p_volume_descriptor );
254 i_lb_size = GETN4( 212 ); // should be 2048
255 i_MT_L = GETN4( 264 ); // should be 6
256 i_N_PM = GETN4( 268 ); // should be 1
258 if( i_lb_size != DVD_LB_SIZE )
260 intf_ErrMsg( "UDF: Non valid sector size (%d)", i_lb_size );
268 /*****************************************************************************
269 * UDFFileEntry: fills a ad_t struct with information at pi_data
270 *****************************************************************************/
271 static int UDFFileEntry( u8 * pi_data, u8 * pi_file_type, struct ad_s * p_ad,
272 struct partition_s partition )
280 UDFICB( &pi_data[16], &i_file_type, &i_flags );
282 pi_file_type[0] = i_file_type;
283 i_L_EA = GETN4( 168 );
284 i_L_AD = GETN4( 172 );
287 while( p < 176 + i_L_EA + i_L_AD )
289 switch( i_flags & 0x07 )
292 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
296 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
300 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
307 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
310 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
313 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
326 /*****************************************************************************
327 * UDFFileIdentifier: gives filename and characteristics of pi_data
328 *****************************************************************************/
329 static int UDFFileIdentifier( u8 * pi_data, u8 * pi_file_characteristics,
330 char * psz_filename, struct ad_s * p_file_icb,
331 struct partition_s partition )
336 pi_file_characteristics[0] = GETN1( 18 );
337 i_L_FI = GETN1( 19 );
338 UDFAD( &pi_data[20], p_file_icb, UDFADlong, partition );
339 i_L_IU = GETN2( 36 );
343 UDFDecode( &pi_data[38+i_L_IU], i_L_FI, psz_filename );
347 psz_filename[0]='\0';
350 return 4 * ( ( 38 + i_L_FI + i_L_IU + 3 ) / 4 );
354 /*****************************************************************************
355 * UDFMapICB: Maps ICB to FileAD
357 * ICB: Location of ICB of directory to scan
358 * FileType: Type of the file
359 * File: Location of file the ICB is pointing to
360 * return 1 on success, 0 on error;
361 *****************************************************************************/
362 static int UDFMapICB( struct ad_s icb, u8 * pi_file_type, struct ad_s * p_file,
363 struct partition_s partition )
365 u8 pi_lb[DVD_LB_SIZE];
369 i_lba = partition.i_start + icb.i_location;
373 if( !UDFReadLB( partition.i_fd, i_lba++, 1, pi_lb ) )
379 UDFDescriptor( pi_lb , &i_tag_id );
382 if( i_tag_id == 261 )
384 UDFFileEntry( pi_lb, pi_file_type, p_file, partition );
388 } while( ( i_lba <= partition.i_start + icb.i_location +
389 ( icb.i_length - 1 ) / DVD_LB_SIZE ) && ( i_tag_id != 261 ) );
394 /*****************************************************************************
395 * UDFScanDir: serach filename in dir
397 * Dir: Location of directory to scan
398 * FileName: Name of file to look for
399 * FileICB: Location of ICB of the found file
400 * return 1 on success, 0 on error;
401 *****************************************************************************/
402 static int UDFScanDir( struct ad_s dir, char * psz_filename,
403 struct ad_s * p_file_icb, struct partition_s partition )
405 u8 pi_lb[2*DVD_LB_SIZE];
409 char psz_temp[DVD_LB_SIZE];
412 /* Scan dir for ICB of file */
413 i_lba = partition.i_start + dir.i_location;
417 if( !UDFReadLB( partition.i_fd, i_lba++, 1, pi_lb ) )
424 while( p < DVD_LB_SIZE )
426 UDFDescriptor( &pi_lb[p], &i_tag_id );
428 if( i_tag_id == 257 )
430 p += UDFFileIdentifier( &pi_lb[p], &i_file_char,
431 psz_temp, p_file_icb, partition );
432 if( !strcasecmp( psz_filename, psz_temp ) )
445 partition.i_start + dir.i_location + ( dir.i_length - 1 ) / DVD_LB_SIZE );
449 if( UDFReadLB( partition.i_fd, i_lba, 2, pi_lb ) <= 0 ) {
454 while( p < dir.i_length )
456 if( p > DVD_LB_SIZE )
460 dir.i_length -= DVD_LB_SIZE;
461 if( UDFReadLB( partition.i_fd, i_lba, 2, pi_lb ) <= 0 )
467 UDFDescriptor( &pi_lb[p], &i_tag_id );
469 if( i_tag_id == 257 )
471 p += UDFFileIdentifier( &pi_lb[p], &i_file_char,
472 psz_temp, p_file_icb, partition );
473 if( !strcasecmp( psz_filename, psz_temp ) )
489 /******************************************************************************
490 * UDFFindPartition: looks for a partition on the disc
492 * partnum: number of the partition, starting at 0
493 * part: structure to fill with the partition information
494 * return 1 if partition found, 0 on error;
495 ******************************************************************************/
496 static int UDFFindPartition( int i_part_nb, struct partition_s *p_partition )
498 u8 pi_lb[DVD_LB_SIZE];
499 u8 pi_anchor[DVD_LB_SIZE];
506 boolean_t b_vol_valid;
512 /* try #1, prime anchor */
516 /* Search anchor loop */
519 if( UDFReadLB( p_partition->i_fd, i_lba, 1, pi_anchor ) )
521 UDFDescriptor( pi_anchor, &i_tag_id );
533 /* final try failed */
539 /* we already found the last sector
540 * try #3, alternative backup anchor */
541 i_lba = i_last_sector;
543 /* but that's just about enough, then! */
548 /* TODO: find last sector of the disc (this is optional) */
551 /* try #2, backup anchor */
552 i_lba = i_last_sector - 256;
556 /* unable to find last sector */
563 /* it is an anchor! continue... */
568 /* main volume descriptor */
569 UDFExtentAD( &pi_anchor[16], &i_MVDS_length, &i_MVDS_location );
571 p_partition->b_valid = 0;
573 p_partition->pi_volume_desc[0] = '\0';
576 /* Find Volume Descriptor */
579 i_lba = i_MVDS_location;
583 if( !UDFReadLB( p_partition->i_fd, i_lba++, 1, pi_lb ) )
589 UDFDescriptor( pi_lb, &i_tag_id );
592 if( ( i_tag_id == 5 ) && ( !p_partition->b_valid ) )
594 /* Partition Descriptor */
596 &p_partition->i_flags,
597 &p_partition->i_number,
598 p_partition->pi_contents,
599 &p_partition->i_start,
600 &p_partition->i_length );
601 p_partition->b_valid = ( i_part_nb == p_partition->i_number );
603 else if( ( i_tag_id == 6 ) && ( !b_vol_valid) )
605 /* Logical Volume Descriptor */
606 if( UDFLogVolume( pi_lb , p_partition->pi_volume_desc ) )
608 /* TODO: sector size wrong! */
616 } while( ( i_lba <= i_MVDS_location +
617 ( i_MVDS_length - 1 ) / DVD_LB_SIZE )
619 && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
621 if( ( !p_partition->b_valid ) || ( !b_vol_valid ) )
623 /* backup volume descriptor */
624 UDFExtentAD( &pi_anchor[24], &i_MVDS_length, &i_MVDS_location );
626 } while( i-- && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
628 /* we only care for the partition, not the volume */
629 return( p_partition->b_valid);
633 /******************************************************************************
634 * UDFFindFile: looks for a file on the UDF disc/imagefile
636 * Path has to be the absolute pathname on the UDF filesystem,
638 * returns absolute LB number, or 0 on error
639 ******************************************************************************/
640 u32 UDFFindFile( int i_fd, char * psz_path )
642 struct partition_s partition;
643 struct ad_s root_icb;
648 u8 pi_lb[DVD_LB_SIZE];
650 char psz_tokenline[DVD_LB_SIZE] = "";
654 strcat( psz_tokenline, psz_path );
656 /* Init file descriptor of UDF filesystem (== DVD) */
657 partition.i_fd = i_fd;
659 /* Find partition 0, standard partition for DVD-Video */
661 if( !UDFFindPartition( i_partition, &partition ) )
663 intf_ErrMsg( "UDF: Partition 0 not found" );
667 /* Find root dir ICB */
668 i_lba = partition.i_start;
672 if( !UDFReadLB( i_fd, i_lba++, 1, pi_lb ) )
678 UDFDescriptor( pi_lb, &i_tag_id );
681 if( i_tag_id == 256 )
683 /* File Set Descriptor */
684 UDFAD( &pi_lb[400], &root_icb, UDFADlong, partition );
687 } while( ( i_lba < partition.i_start + partition.i_length )
688 && ( i_tag_id != 8) && ( i_tag_id != 256 ) );
690 if( i_tag_id != 256 )
692 intf_ErrMsg( "UDF: Bad descriptor" );
695 if( root_icb.i_partition != i_partition )
697 intf_ErrMsg( "UDF: Bad partition" );
702 if( !UDFMapICB( root_icb, &i_file_type, &file, partition ) )
704 intf_ErrMsg( "UDF: Can't find root dir" );
708 /* root dir should be dir */
709 if( i_file_type != 4 )
711 intf_ErrMsg( "UDF: Root dir error" );
715 /* Tokenize filepath */
716 psz_token = strtok( psz_tokenline, "/" );
719 if( !UDFScanDir( file, psz_token, &icb, partition ) )
721 intf_ErrMsg( "UDF: Scan dir error" );
725 if( !UDFMapICB ( icb, &i_file_type, &file, partition ) )
727 intf_ErrMsg( "UDF: ICB error" );
731 psz_token = strtok( NULL, "/" );
734 return partition.i_start + file.i_location;