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.5 2001/04/15 04:19:57 sam 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 *****************************************************************************/
37 #define MODULE_NAME dvd
39 #define MODULE_NAME dvdnocss
41 #include "modules_inner.h"
52 #include "input_dvd.h"
58 typedef struct partition_s
61 u8 pi_volume_desc[128];
79 /* for direct data access, LSB first */
80 #define GETN1(p) ((u8)pi_data[p])
81 #define GETN2(p) ((u16)pi_data[p]|((u16)pi_data[(p)+1]<<8))
82 #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))
83 #define GETN(p,n,target) memcpy(target,&pi_data[p],n)
86 /*****************************************************************************
87 * UDFReadLB: reads absolute Logical Block of the disc
89 * returns number of read bytes on success, 0 on error
90 *****************************************************************************/
91 static int UDFReadLB( int i_fd, off_t i_lba, size_t i_block_count, u8 *pi_data )
98 if( lseek( i_fd, i_lba * (off_t) DVD_LB_SIZE, SEEK_SET ) < 0 )
100 intf_ErrMsg( "UDF: Postion not found" );
104 return read( i_fd, pi_data, i_block_count *DVD_LB_SIZE);
108 /*****************************************************************************
109 * UDFDecode: decode unicode encoded udf data
110 *****************************************************************************/
111 static int UDFDecode( u8 * pi_data, int i_len, char * psz_target )
116 if( !( pi_data[0] & 0x18 ) )
118 psz_target[0] = '\0';
122 if( pi_data[0] & 0x10 )
124 /* ignore MSB of unicode16 */
129 psz_target[i++] = pi_data[p+=2];
136 psz_target[i++] = pi_data[p++];
150 int UDFEntity (u8 *data, u8 *Flags, char *Identifier)
153 strncpy (Identifier, &data[1], 5);
160 /*****************************************************************************
161 * UDFDescriptor: gives a tag ID from your data to find out what it refers to
162 *****************************************************************************/
163 static int UDFDescriptor( u8 * pi_data, u16 * pi_tag_id )
165 pi_tag_id[0] = GETN2( 0 );
166 /* TODO: check CRC 'n stuff */
172 /*****************************************************************************
173 * UDFExtendAD: main volume information
174 *****************************************************************************/
175 static int UDFExtentAD (u8 * pi_data, u32 * pi_length, u32 * pi_location)
177 pi_length[0] = GETN4( 0 );
178 pi_location[0] = GETN4( 4 );
184 /*****************************************************************************
185 * UDFAD: file set information
186 *****************************************************************************/
187 static int UDFAD( u8 * pi_data, struct ad_s * p_ad, u8 i_type,
188 struct partition_s partition )
190 p_ad->i_length = GETN4( 0 );
191 p_ad->i_flags = p_ad->i_length >> 30;
192 p_ad->i_length &= 0x3FFFFFFF;
197 p_ad->i_location = GETN4( 4 );
198 /* use number of current partition */
199 p_ad->i_partition = partition.i_number;
203 p_ad->i_location = GETN4( 4 );
204 p_ad->i_partition = GETN2( 8 );
208 p_ad->i_location = GETN4( 12 );
209 p_ad->i_partition = GETN2( 16 );
217 /*****************************************************************************
218 * UDFICB: takes Information Control Block from pi_data
219 *****************************************************************************/
220 static int UDFICB( u8 * pi_data, u8 * pi_file_type, u16 * pi_flags)
222 pi_file_type[0] = GETN1( 11 );
223 pi_flags[0] = GETN2( 18 );
229 /*****************************************************************************
230 * UDFPartition: gets partition descriptor
231 *****************************************************************************/
232 static int UDFPartition( u8 * pi_data, u16 * pi_flags, u16 * pi_nb,
233 char * ps_contents, u32 * pi_start, u32 * pi_length )
235 pi_flags[0] = GETN2( 20 );
236 pi_nb[0] = GETN2( 22 );
237 GETN( 24, 32, ps_contents );
238 pi_start[0] = GETN4( 188 );
239 pi_length[0] = GETN4( 192 );
245 /*****************************************************************************
246 * UDFLogVolume: reads the volume descriptor and checks the parameters
248 * returns 0 on OK, 1 on error
249 *****************************************************************************/
250 static int UDFLogVolume(u8 * pi_data, char * p_volume_descriptor )
256 UDFDecode( &pi_data[84], 128, p_volume_descriptor );
258 i_lb_size = GETN4( 212 ); // should be 2048
259 i_MT_L = GETN4( 264 ); // should be 6
260 i_N_PM = GETN4( 268 ); // should be 1
262 if( i_lb_size != DVD_LB_SIZE )
264 intf_ErrMsg( "UDF: Non valid sector size (%d)", i_lb_size );
272 /*****************************************************************************
273 * UDFFileEntry: fills a ad_t struct with information at pi_data
274 *****************************************************************************/
275 static int UDFFileEntry( u8 * pi_data, u8 * pi_file_type, struct ad_s * p_ad,
276 struct partition_s partition )
284 UDFICB( &pi_data[16], &i_file_type, &i_flags );
286 pi_file_type[0] = i_file_type;
287 i_L_EA = GETN4( 168 );
288 i_L_AD = GETN4( 172 );
291 while( p < 176 + i_L_EA + i_L_AD )
293 switch( i_flags & 0x07 )
296 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
300 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
304 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
311 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
314 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
317 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
330 /*****************************************************************************
331 * UDFFileIdentifier: gives filename and characteristics of pi_data
332 *****************************************************************************/
333 static int UDFFileIdentifier( u8 * pi_data, u8 * pi_file_characteristics,
334 char * psz_filename, struct ad_s * p_file_icb,
335 struct partition_s partition )
340 pi_file_characteristics[0] = GETN1( 18 );
341 i_L_FI = GETN1( 19 );
342 UDFAD( &pi_data[20], p_file_icb, UDFADlong, partition );
343 i_L_IU = GETN2( 36 );
347 UDFDecode( &pi_data[38+i_L_IU], i_L_FI, psz_filename );
351 psz_filename[0]='\0';
354 return 4 * ( ( 38 + i_L_FI + i_L_IU + 3 ) / 4 );
358 /*****************************************************************************
359 * UDFMapICB: Maps ICB to FileAD
361 * ICB: Location of ICB of directory to scan
362 * FileType: Type of the file
363 * File: Location of file the ICB is pointing to
364 * return 1 on success, 0 on error;
365 *****************************************************************************/
366 static int UDFMapICB( struct ad_s icb, u8 * pi_file_type, struct ad_s * p_file,
367 struct partition_s partition )
369 u8 pi_lb[DVD_LB_SIZE];
373 i_lba = partition.i_start + icb.i_location;
377 if( !UDFReadLB( partition.i_fd, i_lba++, 1, pi_lb ) )
383 UDFDescriptor( pi_lb , &i_tag_id );
386 if( i_tag_id == 261 )
388 UDFFileEntry( pi_lb, pi_file_type, p_file, partition );
392 } while( ( i_lba <= partition.i_start + icb.i_location +
393 ( icb.i_length - 1 ) / DVD_LB_SIZE ) && ( i_tag_id != 261 ) );
398 /*****************************************************************************
399 * UDFScanDir: serach filename in dir
401 * Dir: Location of directory to scan
402 * FileName: Name of file to look for
403 * FileICB: Location of ICB of the found file
404 * return 1 on success, 0 on error;
405 *****************************************************************************/
406 static int UDFScanDir( struct ad_s dir, char * psz_filename,
407 struct ad_s * p_file_icb, struct partition_s partition )
409 u8 pi_lb[2*DVD_LB_SIZE];
413 char psz_temp[DVD_LB_SIZE];
416 /* Scan dir for ICB of file */
417 i_lba = partition.i_start + dir.i_location;
421 if( !UDFReadLB( partition.i_fd, i_lba++, 1, pi_lb ) )
428 while( p < DVD_LB_SIZE )
430 UDFDescriptor( &pi_lb[p], &i_tag_id );
432 if( i_tag_id == 257 )
434 p += UDFFileIdentifier( &pi_lb[p], &i_file_char,
435 psz_temp, p_file_icb, partition );
436 if( !strcasecmp( psz_filename, psz_temp ) )
449 partition.i_start + dir.i_location + ( dir.i_length - 1 ) / DVD_LB_SIZE );
453 if( UDFReadLB( partition.i_fd, i_lba, 2, pi_lb ) <= 0 ) {
458 while( p < dir.i_length )
460 if( p > DVD_LB_SIZE )
464 dir.i_length -= DVD_LB_SIZE;
465 if( UDFReadLB( partition.i_fd, i_lba, 2, pi_lb ) <= 0 )
471 UDFDescriptor( &pi_lb[p], &i_tag_id );
473 if( i_tag_id == 257 )
475 p += UDFFileIdentifier( &pi_lb[p], &i_file_char,
476 psz_temp, p_file_icb, partition );
477 if( !strcasecmp( psz_filename, psz_temp ) )
493 /******************************************************************************
494 * UDFFindPartition: looks for a partition on the disc
496 * partnum: number of the partition, starting at 0
497 * part: structure to fill with the partition information
498 * return 1 if partition found, 0 on error;
499 ******************************************************************************/
500 static int UDFFindPartition( int i_part_nb, struct partition_s *p_partition )
502 u8 pi_lb[DVD_LB_SIZE];
503 u8 pi_anchor[DVD_LB_SIZE];
510 boolean_t b_vol_valid;
516 /* try #1, prime anchor */
520 /* Search anchor loop */
523 if( UDFReadLB( p_partition->i_fd, i_lba, 1, pi_anchor ) )
525 UDFDescriptor( pi_anchor, &i_tag_id );
537 /* final try failed */
543 /* we already found the last sector
544 * try #3, alternative backup anchor */
545 i_lba = i_last_sector;
547 /* but that's just about enough, then! */
552 /* TODO: find last sector of the disc (this is optional) */
555 /* try #2, backup anchor */
556 i_lba = i_last_sector - 256;
560 /* unable to find last sector */
567 /* it is an anchor! continue... */
572 /* main volume descriptor */
573 UDFExtentAD( &pi_anchor[16], &i_MVDS_length, &i_MVDS_location );
575 p_partition->b_valid = 0;
577 p_partition->pi_volume_desc[0] = '\0';
580 /* Find Volume Descriptor */
583 i_lba = i_MVDS_location;
587 if( !UDFReadLB( p_partition->i_fd, i_lba++, 1, pi_lb ) )
593 UDFDescriptor( pi_lb, &i_tag_id );
596 if( ( i_tag_id == 5 ) && ( !p_partition->b_valid ) )
598 /* Partition Descriptor */
600 &p_partition->i_flags,
601 &p_partition->i_number,
602 p_partition->pi_contents,
603 &p_partition->i_start,
604 &p_partition->i_length );
605 p_partition->b_valid = ( i_part_nb == p_partition->i_number );
607 else if( ( i_tag_id == 6 ) && ( !b_vol_valid) )
609 /* Logical Volume Descriptor */
610 if( UDFLogVolume( pi_lb , p_partition->pi_volume_desc ) )
612 /* TODO: sector size wrong! */
620 } while( ( i_lba <= i_MVDS_location +
621 ( i_MVDS_length - 1 ) / DVD_LB_SIZE )
623 && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
625 if( ( !p_partition->b_valid ) || ( !b_vol_valid ) )
627 /* backup volume descriptor */
628 UDFExtentAD( &pi_anchor[24], &i_MVDS_length, &i_MVDS_location );
630 } while( i-- && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
632 /* we only care for the partition, not the volume */
633 return( p_partition->b_valid);
637 /******************************************************************************
638 * UDFFindFile: looks for a file on the UDF disc/imagefile
640 * Path has to be the absolute pathname on the UDF filesystem,
642 * returns absolute LB number, or 0 on error
643 ******************************************************************************/
644 u32 UDFFindFile( int i_fd, char * psz_path )
646 struct partition_s partition;
647 struct ad_s root_icb;
652 u8 pi_lb[DVD_LB_SIZE];
654 char psz_tokenline[DVD_LB_SIZE] = "";
658 strcat( psz_tokenline, psz_path );
660 /* Init file descriptor of UDF filesystem (== DVD) */
661 partition.i_fd = i_fd;
663 /* Find partition 0, standard partition for DVD-Video */
665 if( !UDFFindPartition( i_partition, &partition ) )
667 intf_ErrMsg( "UDF: Partition 0 not found" );
671 /* Find root dir ICB */
672 i_lba = partition.i_start;
676 if( !UDFReadLB( i_fd, i_lba++, 1, pi_lb ) )
682 UDFDescriptor( pi_lb, &i_tag_id );
685 if( i_tag_id == 256 )
687 /* File Set Descriptor */
688 UDFAD( &pi_lb[400], &root_icb, UDFADlong, partition );
691 } while( ( i_lba < partition.i_start + partition.i_length )
692 && ( i_tag_id != 8) && ( i_tag_id != 256 ) );
694 if( i_tag_id != 256 )
696 intf_ErrMsg( "UDF: Bad descriptor" );
699 if( root_icb.i_partition != i_partition )
701 intf_ErrMsg( "UDF: Bad partition" );
706 if( !UDFMapICB( root_icb, &i_file_type, &file, partition ) )
708 intf_ErrMsg( "UDF: Can't find root dir" );
712 /* root dir should be dir */
713 if( i_file_type != 4 )
715 intf_ErrMsg( "UDF: Root dir error" );
719 /* Tokenize filepath */
720 psz_token = strtok( psz_tokenline, "/" );
723 if( !UDFScanDir( file, psz_token, &icb, partition ) )
725 intf_ErrMsg( "UDF: Scan dir error" );
729 if( !UDFMapICB ( icb, &i_file_type, &file, partition ) )
731 intf_ErrMsg( "UDF: ICB error" );
735 psz_token = strtok( NULL, "/" );
738 return partition.i_start + file.i_location;