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.2 2001/02/18 01:42:05 stef Exp $
10 * Author: Stéphane Borel <stef@via.ecp.fr>
13 * - dvdudf by Christian Wolff <scarabaeus@convergence.de>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
28 *****************************************************************************/
43 #include "input_dvd.h"
49 typedef struct partition_s
52 u8 pi_volume_desc[128];
70 /* for direct data access, LSB first */
71 #define GETN1(p) ((u8)pi_data[p])
72 #define GETN2(p) ((u16)pi_data[p]|((u16)pi_data[(p)+1]<<8))
73 #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))
74 #define GETN(p,n,target) memcpy(target,&pi_data[p],n)
77 /*****************************************************************************
78 * UDFReadLB: reads absolute Logical Block of the disc
80 * returns number of read bytes on success, 0 on error
81 *****************************************************************************/
82 static int UDFReadLB( int i_fd, off_t i_lba, size_t i_block_count, u8 *pi_data )
89 if( lseek( i_fd, i_lba * (off_t) DVD_LB_SIZE, SEEK_SET ) < 0 )
91 intf_ErrMsg( "UDF: Postion not found" );
95 return read( i_fd, pi_data, i_block_count *DVD_LB_SIZE);
99 /*****************************************************************************
100 * UDFDecode: decode unicode encoded udf data
101 *****************************************************************************/
102 static int UDFDecode( u8 * pi_data, int i_len, char * psz_target )
107 if( !( pi_data[0] & 0x18 ) )
109 psz_target[0] = '\0';
113 if( pi_data[0] & 0x10 )
115 /* ignore MSB of unicode16 */
120 psz_target[i++] = pi_data[p+=2];
127 psz_target[i++] = pi_data[p++];
141 int UDFEntity (u8 *data, u8 *Flags, char *Identifier)
144 strncpy (Identifier, &data[1], 5);
151 /*****************************************************************************
152 * UDFDescriptor: gives a tag ID from your data to find out what it refers to
153 *****************************************************************************/
154 static int UDFDescriptor( u8 * pi_data, u16 * pi_tag_id )
156 pi_tag_id[0] = GETN2( 0 );
157 /* TODO: check CRC 'n stuff */
163 /*****************************************************************************
164 * UDFExtendAD: main volume information
165 *****************************************************************************/
166 static int UDFExtentAD (u8 * pi_data, u32 * pi_length, u32 * pi_location)
168 pi_length[0] = GETN4( 0 );
169 pi_location[0] = GETN4( 4 );
175 /*****************************************************************************
176 * UDFAD: file set information
177 *****************************************************************************/
178 static int UDFAD( u8 * pi_data, struct ad_s * p_ad, u8 i_type,
179 struct partition_s partition )
181 p_ad->i_length = GETN4( 0 );
182 p_ad->i_flags = p_ad->i_length >> 30;
183 p_ad->i_length &= 0x3FFFFFFF;
188 p_ad->i_location = GETN4( 4 );
189 /* use number of current partition */
190 p_ad->i_partition = partition.i_number;
194 p_ad->i_location = GETN4( 4 );
195 p_ad->i_partition = GETN2( 8 );
199 p_ad->i_location = GETN4( 12 );
200 p_ad->i_partition = GETN2( 16 );
208 /*****************************************************************************
209 * UDFICB: takes Information Control Block from pi_data
210 *****************************************************************************/
211 static int UDFICB( u8 * pi_data, u8 * pi_file_type, u16 * pi_flags)
213 pi_file_type[0] = GETN1( 11 );
214 pi_flags[0] = GETN2( 18 );
220 /*****************************************************************************
221 * UDFPartition: gets partition descriptor
222 *****************************************************************************/
223 static int UDFPartition( u8 * pi_data, u16 * pi_flags, u16 * pi_nb,
224 char * ps_contents, u32 * pi_start, u32 * pi_length )
226 pi_flags[0] = GETN2( 20 );
227 pi_nb[0] = GETN2( 22 );
228 GETN( 24, 32, ps_contents );
229 pi_start[0] = GETN4( 188 );
230 pi_length[0] = GETN4( 192 );
236 /*****************************************************************************
237 * UDFLogVolume: reads the volume descriptor and checks the parameters
239 * returns 0 on OK, 1 on error
240 *****************************************************************************/
241 static int UDFLogVolume(u8 * pi_data, char * p_volume_descriptor )
247 UDFDecode( &pi_data[84], 128, p_volume_descriptor );
249 i_lb_size = GETN4( 212 ); // should be 2048
250 i_MT_L = GETN4( 264 ); // should be 6
251 i_N_PM = GETN4( 268 ); // should be 1
253 if( i_lb_size != DVD_LB_SIZE )
255 intf_ErrMsg( "UDF: Non valid sector size (%d)", i_lb_size );
263 /*****************************************************************************
264 * UDFFileEntry: fills a ad_t struct with information at pi_data
265 *****************************************************************************/
266 static int UDFFileEntry( u8 * pi_data, u8 * pi_file_type, struct ad_s * p_ad,
267 struct partition_s partition )
275 UDFICB( &pi_data[16], &i_file_type, &i_flags );
277 pi_file_type[0] = i_file_type;
278 i_L_EA = GETN4( 168 );
279 i_L_AD = GETN4( 172 );
282 while( p < 176 + i_L_EA + i_L_AD )
284 switch( i_flags & 0x07 )
287 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
291 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
295 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
302 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
305 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
308 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
321 /*****************************************************************************
322 * UDFFileIdentifier: gives filename and characteristics of pi_data
323 *****************************************************************************/
324 static int UDFFileIdentifier( u8 * pi_data, u8 * pi_file_characteristics,
325 char * psz_filename, struct ad_s * p_file_icb,
326 struct partition_s partition )
331 pi_file_characteristics[0] = GETN1( 18 );
332 i_L_FI = GETN1( 19 );
333 UDFAD( &pi_data[20], p_file_icb, UDFADlong, partition );
334 i_L_IU = GETN2( 36 );
338 UDFDecode( &pi_data[38+i_L_IU], i_L_FI, psz_filename );
342 psz_filename[0]='\0';
345 return 4 * ( ( 38 + i_L_FI + i_L_IU + 3 ) / 4 );
349 /*****************************************************************************
350 * UDFMapICB: Maps ICB to FileAD
352 * ICB: Location of ICB of directory to scan
353 * FileType: Type of the file
354 * File: Location of file the ICB is pointing to
355 * return 1 on success, 0 on error;
356 *****************************************************************************/
357 static int UDFMapICB( struct ad_s icb, u8 * pi_file_type, struct ad_s * p_file,
358 struct partition_s partition )
360 u8 pi_lb[DVD_LB_SIZE];
364 i_lba = partition.i_start + icb.i_location;
368 if( !UDFReadLB( partition.i_fd, i_lba++, 1, pi_lb ) )
374 UDFDescriptor( pi_lb , &i_tag_id );
377 if( i_tag_id == 261 )
379 UDFFileEntry( pi_lb, pi_file_type, p_file, partition );
383 } while( ( i_lba <= partition.i_start + icb.i_location +
384 ( icb.i_length - 1 ) / DVD_LB_SIZE ) && ( i_tag_id != 261 ) );
389 /*****************************************************************************
390 * UDFScanDir: serach filename in dir
392 * Dir: Location of directory to scan
393 * FileName: Name of file to look for
394 * FileICB: Location of ICB of the found file
395 * return 1 on success, 0 on error;
396 *****************************************************************************/
397 static int UDFScanDir( struct ad_s dir, char * psz_filename,
398 struct ad_s * p_file_icb, struct partition_s partition )
400 u8 pi_lb[DVD_LB_SIZE];
404 char psz_temp[DVD_LB_SIZE];
407 /* Scan dir for ICB of file */
408 i_lba = partition.i_start + dir.i_location;
412 if( !UDFReadLB( partition.i_fd, i_lba++, 1, pi_lb ) )
419 while( p < DVD_LB_SIZE )
421 UDFDescriptor( &pi_lb[p], &i_tag_id );
423 if( i_tag_id == 257 )
425 p += UDFFileIdentifier( &pi_lb[p], &i_file_char,
426 psz_temp, p_file_icb, partition );
427 if( !strcasecmp( psz_filename, psz_temp ) )
440 partition.i_start + dir.i_location + ( dir.i_length - 1 ) / DVD_LB_SIZE );
446 /******************************************************************************
447 * UDFFindPartition: looks for a partition on the disc
449 * partnum: number of the partition, starting at 0
450 * part: structure to fill with the partition information
451 * return 1 if partition found, 0 on error;
452 ******************************************************************************/
453 static int UDFFindPartition( int i_part_nb, struct partition_s *p_partition )
455 u8 pi_lb[DVD_LB_SIZE];
456 u8 pi_anchor[DVD_LB_SIZE];
463 boolean_t b_vol_valid;
469 /* try #1, prime anchor */
473 /* Search anchor loop */
476 if( UDFReadLB( p_partition->i_fd, i_lba, 1, pi_anchor ) )
478 UDFDescriptor( pi_anchor, &i_tag_id );
490 /* final try failed */
496 /* we already found the last sector
497 * try #3, alternative backup anchor */
498 i_lba = i_last_sector;
500 /* but that's just about enough, then! */
505 /* TODO: find last sector of the disc (this is optional) */
508 /* try #2, backup anchor */
509 i_lba = i_last_sector - 256;
513 /* unable to find last sector */
520 /* it is an anchor! continue... */
525 /* main volume descriptor */
526 UDFExtentAD( &pi_anchor[16], &i_MVDS_length, &i_MVDS_location );
528 p_partition->b_valid = 0;
530 p_partition->pi_volume_desc[0] = '\0';
533 /* Find Volume Descriptor */
536 i_lba = i_MVDS_location;
540 if( !UDFReadLB( p_partition->i_fd, i_lba++, 1, pi_lb ) )
546 UDFDescriptor( pi_lb, &i_tag_id );
549 if( ( i_tag_id == 5 ) && ( !p_partition->b_valid ) )
551 /* Partition Descriptor */
553 &p_partition->i_flags,
554 &p_partition->i_number,
555 p_partition->pi_contents,
556 &p_partition->i_start,
557 &p_partition->i_length );
558 p_partition->b_valid = ( i_part_nb == p_partition->i_number );
560 else if( ( i_tag_id == 6 ) && ( !b_vol_valid) )
562 /* Logical Volume Descriptor */
563 if( UDFLogVolume( pi_lb , p_partition->pi_volume_desc ) )
565 /* TODO: sector size wrong! */
573 } while( ( i_lba <= i_MVDS_location +
574 ( i_MVDS_length - 1 ) / DVD_LB_SIZE )
576 && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
578 if( ( !p_partition->b_valid ) || ( !b_vol_valid ) )
580 /* backup volume descriptor */
581 UDFExtentAD( &pi_anchor[24], &i_MVDS_length, &i_MVDS_location );
583 } while( i-- && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
585 /* we only care for the partition, not the volume */
586 return( p_partition->b_valid);
590 /******************************************************************************
591 * UDFFindFile: looks for a file on the UDF disc/imagefile
593 * Path has to be the absolute pathname on the UDF filesystem,
595 * returns absolute LB number, or 0 on error
596 ******************************************************************************/
597 u32 UDFFindFile( int i_fd, char * psz_path )
599 struct partition_s partition;
600 struct ad_s root_icb;
605 u8 pi_lb[DVD_LB_SIZE];
607 char psz_tokenline[DVD_LB_SIZE] = "";
611 strcat( psz_tokenline, psz_path );
613 /* Init file descriptor of UDF filesystem (== DVD) */
614 partition.i_fd = i_fd;
616 /* Find partition 0, standard partition for DVD-Video */
618 if( !UDFFindPartition( i_partition, &partition ) )
620 intf_ErrMsg( "UDF: Partition 0 not found" );
624 /* Find root dir ICB */
625 i_lba = partition.i_start;
629 if( !UDFReadLB( i_fd, i_lba++, 1, pi_lb ) )
635 UDFDescriptor( pi_lb, &i_tag_id );
638 if( i_tag_id == 256 )
640 /* File Set Descriptor */
641 UDFAD( &pi_lb[400], &root_icb, UDFADlong, partition );
644 } while( ( i_lba < partition.i_start + partition.i_length )
645 && ( i_tag_id != 8) && ( i_tag_id != 256 ) );
647 if( i_tag_id != 256 )
649 intf_ErrMsg( "UDF: Bad descriptor" );
652 if( root_icb.i_partition != i_partition )
654 intf_ErrMsg( "UDF: Bad partition" );
659 if( !UDFMapICB( root_icb, &i_file_type, &file, partition ) )
661 intf_ErrMsg( "UDF: Can't find root dir" );
665 /* root dir should be dir */
666 if( i_file_type != 4 )
668 intf_ErrMsg( "UDF: Root dir error" );
672 /* Tokenize filepath */
673 psz_token = strtok( psz_tokenline, "/" );
676 if( !UDFScanDir( file, psz_token, &icb, partition ) )
678 intf_ErrMsg( "UDF: Scan dir error" );
682 if( !UDFMapICB ( icb, &i_file_type, &file, partition ) )
684 intf_ErrMsg( "UDF: ICB error" );
688 psz_token = strtok( NULL, "/" );
691 return partition.i_start + file.i_location;