/* udf.c - Universal Disk Format filesystem. */ /* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2008,2009 Free Software Foundation, Inc. * * VasEBoot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VasEBoot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VasEBoot. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include VasEBoot_MOD_LICENSE ("GPLv3+"); #define VasEBoot_UDF_MAX_PDS 2 #define VasEBoot_UDF_MAX_PMS 6 #define U16 VasEBoot_le_to_cpu16 #define U32 VasEBoot_le_to_cpu32 #define U64 VasEBoot_le_to_cpu64 #define VasEBoot_UDF_TAG_IDENT_PVD 0x0001 #define VasEBoot_UDF_TAG_IDENT_AVDP 0x0002 #define VasEBoot_UDF_TAG_IDENT_VDP 0x0003 #define VasEBoot_UDF_TAG_IDENT_IUVD 0x0004 #define VasEBoot_UDF_TAG_IDENT_PD 0x0005 #define VasEBoot_UDF_TAG_IDENT_LVD 0x0006 #define VasEBoot_UDF_TAG_IDENT_USD 0x0007 #define VasEBoot_UDF_TAG_IDENT_TD 0x0008 #define VasEBoot_UDF_TAG_IDENT_LVID 0x0009 #define VasEBoot_UDF_TAG_IDENT_FSD 0x0100 #define VasEBoot_UDF_TAG_IDENT_FID 0x0101 #define VasEBoot_UDF_TAG_IDENT_AED 0x0102 #define VasEBoot_UDF_TAG_IDENT_IE 0x0103 #define VasEBoot_UDF_TAG_IDENT_TE 0x0104 #define VasEBoot_UDF_TAG_IDENT_FE 0x0105 #define VasEBoot_UDF_TAG_IDENT_EAHD 0x0106 #define VasEBoot_UDF_TAG_IDENT_USE 0x0107 #define VasEBoot_UDF_TAG_IDENT_SBD 0x0108 #define VasEBoot_UDF_TAG_IDENT_PIE 0x0109 #define VasEBoot_UDF_TAG_IDENT_EFE 0x010A #define VasEBoot_UDF_ICBTAG_TYPE_UNDEF 0x00 #define VasEBoot_UDF_ICBTAG_TYPE_USE 0x01 #define VasEBoot_UDF_ICBTAG_TYPE_PIE 0x02 #define VasEBoot_UDF_ICBTAG_TYPE_IE 0x03 #define VasEBoot_UDF_ICBTAG_TYPE_DIRECTORY 0x04 #define VasEBoot_UDF_ICBTAG_TYPE_REGULAR 0x05 #define VasEBoot_UDF_ICBTAG_TYPE_BLOCK 0x06 #define VasEBoot_UDF_ICBTAG_TYPE_CHAR 0x07 #define VasEBoot_UDF_ICBTAG_TYPE_EA 0x08 #define VasEBoot_UDF_ICBTAG_TYPE_FIFO 0x09 #define VasEBoot_UDF_ICBTAG_TYPE_SOCKET 0x0A #define VasEBoot_UDF_ICBTAG_TYPE_TE 0x0B #define VasEBoot_UDF_ICBTAG_TYPE_SYMLINK 0x0C #define VasEBoot_UDF_ICBTAG_TYPE_STREAMDIR 0x0D #define VasEBoot_UDF_ICBTAG_FLAG_AD_MASK 0x0007 #define VasEBoot_UDF_ICBTAG_FLAG_AD_SHORT 0x0000 #define VasEBoot_UDF_ICBTAG_FLAG_AD_LONG 0x0001 #define VasEBoot_UDF_ICBTAG_FLAG_AD_EXT 0x0002 #define VasEBoot_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003 #define VasEBoot_UDF_EXT_NORMAL 0x00000000 #define VasEBoot_UDF_EXT_NREC_ALLOC 0x40000000 #define VasEBoot_UDF_EXT_NREC_NALLOC 0x80000000 #define VasEBoot_UDF_EXT_MASK 0xC0000000 #define VasEBoot_UDF_FID_CHAR_HIDDEN 0x01 #define VasEBoot_UDF_FID_CHAR_DIRECTORY 0x02 #define VasEBoot_UDF_FID_CHAR_DELETED 0x04 #define VasEBoot_UDF_FID_CHAR_PARENT 0x08 #define VasEBoot_UDF_FID_CHAR_METADATA 0x10 #define VasEBoot_UDF_STD_IDENT_BEA01 "BEA01" #define VasEBoot_UDF_STD_IDENT_BOOT2 "BOOT2" #define VasEBoot_UDF_STD_IDENT_CD001 "CD001" #define VasEBoot_UDF_STD_IDENT_CDW02 "CDW02" #define VasEBoot_UDF_STD_IDENT_NSR02 "NSR02" #define VasEBoot_UDF_STD_IDENT_NSR03 "NSR03" #define VasEBoot_UDF_STD_IDENT_TEA01 "TEA01" #define VasEBoot_UDF_CHARSPEC_TYPE_CS0 0x00 #define VasEBoot_UDF_CHARSPEC_TYPE_CS1 0x01 #define VasEBoot_UDF_CHARSPEC_TYPE_CS2 0x02 #define VasEBoot_UDF_CHARSPEC_TYPE_CS3 0x03 #define VasEBoot_UDF_CHARSPEC_TYPE_CS4 0x04 #define VasEBoot_UDF_CHARSPEC_TYPE_CS5 0x05 #define VasEBoot_UDF_CHARSPEC_TYPE_CS6 0x06 #define VasEBoot_UDF_CHARSPEC_TYPE_CS7 0x07 #define VasEBoot_UDF_CHARSPEC_TYPE_CS8 0x08 #define VasEBoot_UDF_PARTMAP_TYPE_1 1 #define VasEBoot_UDF_PARTMAP_TYPE_2 2 struct VasEBoot_udf_lb_addr { VasEBoot_uint32_t block_num; VasEBoot_uint16_t part_ref; } VasEBoot_PACKED; struct VasEBoot_udf_short_ad { VasEBoot_uint32_t length; VasEBoot_uint32_t position; } VasEBoot_PACKED; struct VasEBoot_udf_long_ad { VasEBoot_uint32_t length; struct VasEBoot_udf_lb_addr block; VasEBoot_uint8_t imp_use[6]; } VasEBoot_PACKED; struct VasEBoot_udf_extent_ad { VasEBoot_uint32_t length; VasEBoot_uint32_t start; } VasEBoot_PACKED; struct VasEBoot_udf_charspec { VasEBoot_uint8_t charset_type; VasEBoot_uint8_t charset_info[63]; } VasEBoot_PACKED; struct VasEBoot_udf_timestamp { VasEBoot_uint16_t type_and_timezone; VasEBoot_uint16_t year; VasEBoot_uint8_t month; VasEBoot_uint8_t day; VasEBoot_uint8_t hour; VasEBoot_uint8_t minute; VasEBoot_uint8_t second; VasEBoot_uint8_t centi_seconds; VasEBoot_uint8_t hundreds_of_micro_seconds; VasEBoot_uint8_t micro_seconds; } VasEBoot_PACKED; struct VasEBoot_udf_regid { VasEBoot_uint8_t flags; VasEBoot_uint8_t ident[23]; VasEBoot_uint8_t ident_suffix[8]; } VasEBoot_PACKED; struct VasEBoot_udf_tag { VasEBoot_uint16_t tag_ident; VasEBoot_uint16_t desc_version; VasEBoot_uint8_t tag_checksum; VasEBoot_uint8_t reserved; VasEBoot_uint16_t tag_serial_number; VasEBoot_uint16_t desc_crc; VasEBoot_uint16_t desc_crc_length; VasEBoot_uint32_t tag_location; } VasEBoot_PACKED; struct VasEBoot_udf_fileset { struct VasEBoot_udf_tag tag; struct VasEBoot_udf_timestamp datetime; VasEBoot_uint16_t interchange_level; VasEBoot_uint16_t max_interchange_level; VasEBoot_uint32_t charset_list; VasEBoot_uint32_t max_charset_list; VasEBoot_uint32_t fileset_num; VasEBoot_uint32_t fileset_desc_num; struct VasEBoot_udf_charspec vol_charset; VasEBoot_uint8_t vol_ident[128]; struct VasEBoot_udf_charspec fileset_charset; VasEBoot_uint8_t fileset_ident[32]; VasEBoot_uint8_t copyright_file_ident[32]; VasEBoot_uint8_t abstract_file_ident[32]; struct VasEBoot_udf_long_ad root_icb; struct VasEBoot_udf_regid domain_ident; struct VasEBoot_udf_long_ad next_ext; struct VasEBoot_udf_long_ad streamdir_icb; } VasEBoot_PACKED; struct VasEBoot_udf_icbtag { VasEBoot_uint32_t prior_recorded_num_direct_entries; VasEBoot_uint16_t strategy_type; VasEBoot_uint16_t strategy_parameter; VasEBoot_uint16_t num_entries; VasEBoot_uint8_t reserved; VasEBoot_uint8_t file_type; struct VasEBoot_udf_lb_addr parent_idb; VasEBoot_uint16_t flags; } VasEBoot_PACKED; struct VasEBoot_udf_file_ident { struct VasEBoot_udf_tag tag; VasEBoot_uint16_t version_num; VasEBoot_uint8_t characteristics; #define MAX_FILE_IDENT_LENGTH 256 VasEBoot_uint8_t file_ident_length; struct VasEBoot_udf_long_ad icb; VasEBoot_uint16_t imp_use_length; } VasEBoot_PACKED; struct VasEBoot_udf_file_entry { struct VasEBoot_udf_tag tag; struct VasEBoot_udf_icbtag icbtag; VasEBoot_uint32_t uid; VasEBoot_uint32_t gid; VasEBoot_uint32_t permissions; VasEBoot_uint16_t link_count; VasEBoot_uint8_t record_format; VasEBoot_uint8_t record_display_attr; VasEBoot_uint32_t record_length; VasEBoot_uint64_t file_size; VasEBoot_uint64_t blocks_recorded; struct VasEBoot_udf_timestamp access_time; struct VasEBoot_udf_timestamp modification_time; struct VasEBoot_udf_timestamp attr_time; VasEBoot_uint32_t checkpoint; struct VasEBoot_udf_long_ad extended_attr_idb; struct VasEBoot_udf_regid imp_ident; VasEBoot_uint64_t unique_id; VasEBoot_uint32_t ext_attr_length; VasEBoot_uint32_t alloc_descs_length; VasEBoot_uint8_t ext_attr[0]; } VasEBoot_PACKED; struct VasEBoot_udf_extended_file_entry { struct VasEBoot_udf_tag tag; struct VasEBoot_udf_icbtag icbtag; VasEBoot_uint32_t uid; VasEBoot_uint32_t gid; VasEBoot_uint32_t permissions; VasEBoot_uint16_t link_count; VasEBoot_uint8_t record_format; VasEBoot_uint8_t record_display_attr; VasEBoot_uint32_t record_length; VasEBoot_uint64_t file_size; VasEBoot_uint64_t object_size; VasEBoot_uint64_t blocks_recorded; struct VasEBoot_udf_timestamp access_time; struct VasEBoot_udf_timestamp modification_time; struct VasEBoot_udf_timestamp create_time; struct VasEBoot_udf_timestamp attr_time; VasEBoot_uint32_t checkpoint; VasEBoot_uint32_t reserved; struct VasEBoot_udf_long_ad extended_attr_icb; struct VasEBoot_udf_long_ad streamdir_icb; struct VasEBoot_udf_regid imp_ident; VasEBoot_uint64_t unique_id; VasEBoot_uint32_t ext_attr_length; VasEBoot_uint32_t alloc_descs_length; VasEBoot_uint8_t ext_attr[0]; } VasEBoot_PACKED; struct VasEBoot_udf_vrs { VasEBoot_uint8_t type; VasEBoot_uint8_t magic[5]; VasEBoot_uint8_t version; } VasEBoot_PACKED; struct VasEBoot_udf_avdp { struct VasEBoot_udf_tag tag; struct VasEBoot_udf_extent_ad vds; } VasEBoot_PACKED; struct VasEBoot_udf_pd { struct VasEBoot_udf_tag tag; VasEBoot_uint32_t seq_num; VasEBoot_uint16_t flags; VasEBoot_uint16_t part_num; struct VasEBoot_udf_regid contents; VasEBoot_uint8_t contents_use[128]; VasEBoot_uint32_t access_type; VasEBoot_uint32_t start; VasEBoot_uint32_t length; } VasEBoot_PACKED; struct VasEBoot_udf_partmap { VasEBoot_uint8_t type; VasEBoot_uint8_t length; union { struct { VasEBoot_uint16_t seq_num; VasEBoot_uint16_t part_num; } type1; struct { VasEBoot_uint8_t ident[62]; } type2; }; } VasEBoot_PACKED; struct VasEBoot_udf_lvd { struct VasEBoot_udf_tag tag; VasEBoot_uint32_t seq_num; struct VasEBoot_udf_charspec charset; VasEBoot_uint8_t ident[128]; VasEBoot_uint32_t bsize; struct VasEBoot_udf_regid domain_ident; struct VasEBoot_udf_long_ad root_fileset; VasEBoot_uint32_t map_table_length; VasEBoot_uint32_t num_part_maps; struct VasEBoot_udf_regid imp_ident; VasEBoot_uint8_t imp_use[128]; struct VasEBoot_udf_extent_ad integrity_seq_ext; VasEBoot_uint8_t part_maps[1608]; } VasEBoot_PACKED; struct VasEBoot_udf_aed { struct VasEBoot_udf_tag tag; VasEBoot_uint32_t prev_ae; VasEBoot_uint32_t ae_len; } VasEBoot_PACKED; struct VasEBoot_udf_data { VasEBoot_disk_t disk; struct VasEBoot_udf_lvd lvd; struct VasEBoot_udf_pd pds[VasEBoot_UDF_MAX_PDS]; struct VasEBoot_udf_partmap *pms[VasEBoot_UDF_MAX_PMS]; struct VasEBoot_udf_long_ad root_icb; int npd, npm, lbshift; }; struct VasEBoot_fshelp_node { struct VasEBoot_udf_data *data; int part_ref; union { struct VasEBoot_udf_file_entry fe; struct VasEBoot_udf_extended_file_entry efe; char raw[0]; } block; }; static inline VasEBoot_size_t get_fshelp_size (struct VasEBoot_udf_data *data) { struct VasEBoot_fshelp_node *x = NULL; return sizeof (*x) + (1 << (VasEBoot_DISK_SECTOR_BITS + data->lbshift)) - sizeof (x->block); } static VasEBoot_dl_t my_mod; static VasEBoot_uint32_t VasEBoot_udf_get_block (struct VasEBoot_udf_data *data, VasEBoot_uint16_t part_ref, VasEBoot_uint32_t block) { part_ref = U16 (part_ref); if (part_ref >= data->npm) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid part ref"); return 0; } return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start) + U32 (block)); } static VasEBoot_err_t VasEBoot_udf_read_icb (struct VasEBoot_udf_data *data, struct VasEBoot_udf_long_ad *icb, struct VasEBoot_fshelp_node *node) { VasEBoot_uint32_t block; block = VasEBoot_udf_get_block (data, icb->block.part_ref, icb->block.block_num); if (VasEBoot_errno) return VasEBoot_errno; if (VasEBoot_disk_read (data->disk, block << data->lbshift, 0, 1 << (VasEBoot_DISK_SECTOR_BITS + data->lbshift), &node->block)) return VasEBoot_errno; if ((U16 (node->block.fe.tag.tag_ident) != VasEBoot_UDF_TAG_IDENT_FE) && (U16 (node->block.fe.tag.tag_ident) != VasEBoot_UDF_TAG_IDENT_EFE)) return VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid fe/efe descriptor"); node->part_ref = icb->block.part_ref; node->data = data; return 0; } static VasEBoot_disk_addr_t VasEBoot_udf_read_block (VasEBoot_fshelp_node_t node, VasEBoot_disk_addr_t fileblock) { char *buf = NULL; char *ptr; VasEBoot_ssize_t len; VasEBoot_disk_addr_t filebytes; switch (U16 (node->block.fe.tag.tag_ident)) { case VasEBoot_UDF_TAG_IDENT_FE: ptr = (char *) &node->block.fe.ext_attr[0] + U32 (node->block.fe.ext_attr_length); len = U32 (node->block.fe.alloc_descs_length); break; case VasEBoot_UDF_TAG_IDENT_EFE: ptr = (char *) &node->block.efe.ext_attr[0] + U32 (node->block.efe.ext_attr_length); len = U32 (node->block.efe.alloc_descs_length); break; default: VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid file entry"); return 0; } if ((U16 (node->block.fe.icbtag.flags) & VasEBoot_UDF_ICBTAG_FLAG_AD_MASK) == VasEBoot_UDF_ICBTAG_FLAG_AD_SHORT) { struct VasEBoot_udf_short_ad *ad = (struct VasEBoot_udf_short_ad *) ptr; filebytes = fileblock * U32 (node->data->lvd.bsize); while (len >= (VasEBoot_ssize_t) sizeof (struct VasEBoot_udf_short_ad)) { VasEBoot_uint32_t adlen = U32 (ad->length) & 0x3fffffff; VasEBoot_uint32_t adtype = U32 (ad->length) >> 30; if (adtype == 3) { struct VasEBoot_udf_aed *extension; VasEBoot_disk_addr_t sec = VasEBoot_udf_get_block(node->data, node->part_ref, ad->position); if (!buf) { buf = VasEBoot_malloc (U32 (node->data->lvd.bsize)); if (!buf) return 0; } if (VasEBoot_disk_read (node->data->disk, sec << node->data->lbshift, 0, adlen, buf)) goto fail; extension = (struct VasEBoot_udf_aed *) buf; if (U16 (extension->tag.tag_ident) != VasEBoot_UDF_TAG_IDENT_AED) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid aed tag"); goto fail; } len = U32 (extension->ae_len); ad = (struct VasEBoot_udf_short_ad *) (buf + sizeof (struct VasEBoot_udf_aed)); continue; } if (filebytes < adlen) { VasEBoot_uint32_t ad_pos = ad->position; VasEBoot_free (buf); return ((U32 (ad_pos) & VasEBoot_UDF_EXT_MASK) ? 0 : (VasEBoot_udf_get_block (node->data, node->part_ref, ad_pos) + (filebytes >> (VasEBoot_DISK_SECTOR_BITS + node->data->lbshift)))); } filebytes -= adlen; ad++; len -= sizeof (struct VasEBoot_udf_short_ad); } } else { struct VasEBoot_udf_long_ad *ad = (struct VasEBoot_udf_long_ad *) ptr; filebytes = fileblock * U32 (node->data->lvd.bsize); while (len >= (VasEBoot_ssize_t) sizeof (struct VasEBoot_udf_long_ad)) { VasEBoot_uint32_t adlen = U32 (ad->length) & 0x3fffffff; VasEBoot_uint32_t adtype = U32 (ad->length) >> 30; if (adtype == 3) { struct VasEBoot_udf_aed *extension; VasEBoot_disk_addr_t sec = VasEBoot_udf_get_block(node->data, ad->block.part_ref, ad->block.block_num); if (!buf) { buf = VasEBoot_malloc (U32 (node->data->lvd.bsize)); if (!buf) return 0; } if (VasEBoot_disk_read (node->data->disk, sec << node->data->lbshift, 0, adlen, buf)) goto fail; extension = (struct VasEBoot_udf_aed *) buf; if (U16 (extension->tag.tag_ident) != VasEBoot_UDF_TAG_IDENT_AED) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid aed tag"); goto fail; } len = U32 (extension->ae_len); ad = (struct VasEBoot_udf_long_ad *) (buf + sizeof (struct VasEBoot_udf_aed)); continue; } if (filebytes < adlen) { VasEBoot_uint32_t ad_block_num = ad->block.block_num; VasEBoot_uint32_t ad_part_ref = ad->block.part_ref; VasEBoot_free (buf); return ((U32 (ad_block_num) & VasEBoot_UDF_EXT_MASK) ? 0 : (VasEBoot_udf_get_block (node->data, ad_part_ref, ad_block_num) + (filebytes >> (VasEBoot_DISK_SECTOR_BITS + node->data->lbshift)))); } filebytes -= adlen; ad++; len -= sizeof (struct VasEBoot_udf_long_ad); } } fail: VasEBoot_free (buf); return 0; } static VasEBoot_ssize_t VasEBoot_udf_read_file (VasEBoot_fshelp_node_t node, VasEBoot_disk_read_hook_t read_hook, void *read_hook_data, VasEBoot_off_t pos, VasEBoot_size_t len, char *buf) { switch (U16 (node->block.fe.icbtag.flags) & VasEBoot_UDF_ICBTAG_FLAG_AD_MASK) { case VasEBoot_UDF_ICBTAG_FLAG_AD_IN_ICB: { char *ptr; ptr = ((U16 (node->block.fe.tag.tag_ident) == VasEBoot_UDF_TAG_IDENT_FE) ? ((char *) &node->block.fe.ext_attr[0] + U32 (node->block.fe.ext_attr_length)) : ((char *) &node->block.efe.ext_attr[0] + U32 (node->block.efe.ext_attr_length))); VasEBoot_memcpy (buf, ptr + pos, len); return len; } case VasEBoot_UDF_ICBTAG_FLAG_AD_EXT: VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid extent type"); return 0; } return VasEBoot_fshelp_read_file (node->data->disk, node, read_hook, read_hook_data, pos, len, buf, VasEBoot_udf_read_block, U64 (node->block.fe.file_size), node->data->lbshift, 0); } static unsigned sblocklist[] = { 256, 512, 0 }; static struct VasEBoot_udf_data * VasEBoot_udf_mount (VasEBoot_disk_t disk) { struct VasEBoot_udf_data *data = 0; struct VasEBoot_udf_fileset root_fs; unsigned *sblklist; VasEBoot_uint32_t block, vblock; int i, lbshift; data = VasEBoot_malloc (sizeof (struct VasEBoot_udf_data)); if (!data) return 0; data->disk = disk; /* Search for Anchor Volume Descriptor Pointer (AVDP) * and determine logical block size. */ block = 0; for (lbshift = 0; lbshift < 4; lbshift++) { for (sblklist = sblocklist; *sblklist; sblklist++) { struct VasEBoot_udf_avdp avdp; if (VasEBoot_disk_read (disk, *sblklist << lbshift, 0, sizeof (struct VasEBoot_udf_avdp), &avdp)) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } if (U16 (avdp.tag.tag_ident) == VasEBoot_UDF_TAG_IDENT_AVDP && U32 (avdp.tag.tag_location) == *sblklist) { block = U32 (avdp.vds.start); break; } } if (block) break; } if (!block) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } data->lbshift = lbshift; /* Search for Volume Recognition Sequence (VRS). */ for (vblock = (32767 >> (lbshift + VasEBoot_DISK_SECTOR_BITS)) + 1;; vblock += (2047 >> (lbshift + VasEBoot_DISK_SECTOR_BITS)) + 1) { struct VasEBoot_udf_vrs vrs; if (VasEBoot_disk_read (disk, vblock << lbshift, 0, sizeof (struct VasEBoot_udf_vrs), &vrs)) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } if ((!VasEBoot_memcmp (vrs.magic, VasEBoot_UDF_STD_IDENT_NSR03, 5)) || (!VasEBoot_memcmp (vrs.magic, VasEBoot_UDF_STD_IDENT_NSR02, 5))) break; if ((VasEBoot_memcmp (vrs.magic, VasEBoot_UDF_STD_IDENT_BEA01, 5)) && (VasEBoot_memcmp (vrs.magic, VasEBoot_UDF_STD_IDENT_BOOT2, 5)) && (VasEBoot_memcmp (vrs.magic, VasEBoot_UDF_STD_IDENT_CD001, 5)) && (VasEBoot_memcmp (vrs.magic, VasEBoot_UDF_STD_IDENT_CDW02, 5)) && (VasEBoot_memcmp (vrs.magic, VasEBoot_UDF_STD_IDENT_TEA01, 5))) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } } data->npd = data->npm = 0; /* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */ while (1) { struct VasEBoot_udf_tag tag; if (VasEBoot_disk_read (disk, block << lbshift, 0, sizeof (struct VasEBoot_udf_tag), &tag)) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } tag.tag_ident = U16 (tag.tag_ident); if (tag.tag_ident == VasEBoot_UDF_TAG_IDENT_PD) { if (data->npd >= VasEBoot_UDF_MAX_PDS) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "too many PDs"); goto fail; } if (VasEBoot_disk_read (disk, block << lbshift, 0, sizeof (struct VasEBoot_udf_pd), &data->pds[data->npd])) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } data->npd++; } else if (tag.tag_ident == VasEBoot_UDF_TAG_IDENT_LVD) { int k; struct VasEBoot_udf_partmap *ppm; if (VasEBoot_disk_read (disk, block << lbshift, 0, sizeof (struct VasEBoot_udf_lvd), &data->lvd)) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } if (data->npm + U32 (data->lvd.num_part_maps) > VasEBoot_UDF_MAX_PMS) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "too many partition maps"); goto fail; } ppm = (struct VasEBoot_udf_partmap *) &data->lvd.part_maps; for (k = U32 (data->lvd.num_part_maps); k > 0; k--) { if (ppm->type != VasEBoot_UDF_PARTMAP_TYPE_1) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "partmap type not supported"); goto fail; } data->pms[data->npm++] = ppm; ppm = (struct VasEBoot_udf_partmap *) ((char *) ppm + U32 (ppm->length)); } } else if (tag.tag_ident > VasEBoot_UDF_TAG_IDENT_TD) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid tag ident"); goto fail; } else if (tag.tag_ident == VasEBoot_UDF_TAG_IDENT_TD) break; block++; } for (i = 0; i < data->npm; i++) { int j; for (j = 0; j < data->npd; j++) if (data->pms[i]->type1.part_num == data->pds[j].part_num) { data->pms[i]->type1.part_num = j; break; } if (j == data->npd) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "can\'t find PD"); goto fail; } } block = VasEBoot_udf_get_block (data, data->lvd.root_fileset.block.part_ref, data->lvd.root_fileset.block.block_num); if (VasEBoot_errno) goto fail; if (VasEBoot_disk_read (disk, block << lbshift, 0, sizeof (struct VasEBoot_udf_fileset), &root_fs)) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "not an UDF filesystem"); goto fail; } if (U16 (root_fs.tag.tag_ident) != VasEBoot_UDF_TAG_IDENT_FSD) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid fileset descriptor"); goto fail; } data->root_icb = root_fs.root_icb; return data; fail: VasEBoot_free (data); return 0; } #ifdef VasEBoot_UTIL VasEBoot_disk_addr_t VasEBoot_udf_get_cluster_sector (VasEBoot_disk_t disk, VasEBoot_uint64_t *sec_per_lcn) { VasEBoot_disk_addr_t ret; static struct VasEBoot_udf_data *data; data = VasEBoot_udf_mount (disk); if (!data) return 0; ret = U32 (data->pds[data->pms[0]->type1.part_num].start); *sec_per_lcn = 1ULL << data->lbshift; VasEBoot_free (data); return ret; } #endif static char * read_string (const VasEBoot_uint8_t *raw, VasEBoot_size_t sz, char *outbuf) { VasEBoot_uint16_t *utf16 = NULL; VasEBoot_size_t utf16len = 0; if (sz == 0) return NULL; if (raw[0] != 8 && raw[0] != 16) return NULL; if (raw[0] == 8) { unsigned i; utf16len = sz - 1; utf16 = VasEBoot_malloc (utf16len * sizeof (utf16[0])); if (!utf16) return NULL; for (i = 0; i < utf16len; i++) utf16[i] = raw[i + 1]; } if (raw[0] == 16) { unsigned i; utf16len = (sz - 1) / 2; utf16 = VasEBoot_malloc (utf16len * sizeof (utf16[0])); if (!utf16) return NULL; for (i = 0; i < utf16len; i++) utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2]; } if (!outbuf) outbuf = VasEBoot_malloc (utf16len * VasEBoot_MAX_UTF8_PER_UTF16 + 1); if (outbuf) *VasEBoot_utf16_to_utf8 ((VasEBoot_uint8_t *) outbuf, utf16, utf16len) = '\0'; VasEBoot_free (utf16); return outbuf; } static int VasEBoot_udf_iterate_dir (VasEBoot_fshelp_node_t dir, VasEBoot_fshelp_iterate_dir_hook_t hook, void *hook_data) { VasEBoot_fshelp_node_t child; struct VasEBoot_udf_file_ident dirent; VasEBoot_off_t offset = 0; child = VasEBoot_malloc (get_fshelp_size (dir->data)); if (!child) return 0; /* The current directory is not stored. */ VasEBoot_memcpy (child, dir, get_fshelp_size (dir->data)); if (hook (".", VasEBoot_FSHELP_DIR, child, hook_data)) return 1; while (offset < U64 (dir->block.fe.file_size)) { if (VasEBoot_udf_read_file (dir, 0, 0, offset, sizeof (dirent), (char *) &dirent) != sizeof (dirent)) return 0; if (U16 (dirent.tag.tag_ident) != VasEBoot_UDF_TAG_IDENT_FID) { VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid fid tag"); return 0; } offset += sizeof (dirent) + U16 (dirent.imp_use_length); if (!(dirent.characteristics & VasEBoot_UDF_FID_CHAR_DELETED)) { child = VasEBoot_malloc (get_fshelp_size (dir->data)); if (!child) return 0; if (VasEBoot_udf_read_icb (dir->data, &dirent.icb, child)) return 0; if (dirent.characteristics & VasEBoot_UDF_FID_CHAR_PARENT) { /* This is the parent directory. */ if (hook ("..", VasEBoot_FSHELP_DIR, child, hook_data)) return 1; } else { enum VasEBoot_fshelp_filetype type; char *filename; VasEBoot_uint8_t raw[MAX_FILE_IDENT_LENGTH]; type = ((dirent.characteristics & VasEBoot_UDF_FID_CHAR_DIRECTORY) ? (VasEBoot_FSHELP_DIR) : (VasEBoot_FSHELP_REG)); if (child->block.fe.icbtag.file_type == VasEBoot_UDF_ICBTAG_TYPE_SYMLINK) type = VasEBoot_FSHELP_SYMLINK; if ((VasEBoot_udf_read_file (dir, 0, 0, offset, dirent.file_ident_length, (char *) raw)) != dirent.file_ident_length) return 0; filename = read_string (raw, dirent.file_ident_length, 0); if (!filename) VasEBoot_print_error (); if (filename && hook (filename, type, child, hook_data)) { VasEBoot_free (filename); return 1; } VasEBoot_free (filename); } } /* Align to dword boundary. */ offset = (offset + dirent.file_ident_length + 3) & (~3); } return 0; } static char * VasEBoot_udf_read_symlink (VasEBoot_fshelp_node_t node) { VasEBoot_size_t sz = U64 (node->block.fe.file_size); VasEBoot_uint8_t *raw; const VasEBoot_uint8_t *ptr; char *out, *optr; if (sz < 4) return NULL; raw = VasEBoot_malloc (sz); if (!raw) return NULL; if (VasEBoot_udf_read_file (node, NULL, NULL, 0, sz, (char *) raw) < 0) { VasEBoot_free (raw); return NULL; } out = VasEBoot_malloc (sz * 2 + 1); if (!out) { VasEBoot_free (raw); return NULL; } optr = out; for (ptr = raw; ptr < raw + sz; ) { VasEBoot_size_t s; if ((VasEBoot_size_t) (ptr - raw + 4) > sz) goto fail; if (!(ptr[2] == 0 && ptr[3] == 0)) goto fail; s = 4 + ptr[1]; if ((VasEBoot_size_t) (ptr - raw + s) > sz) goto fail; switch (*ptr) { case 1: if (ptr[1]) goto fail; /* Fallthrough. */ case 2: /* in 4 bytes. out: 1 byte. */ optr = out; *optr++ = '/'; break; case 3: /* in 4 bytes. out: 3 bytes. */ if (optr != out) *optr++ = '/'; *optr++ = '.'; *optr++ = '.'; break; case 4: /* in 4 bytes. out: 2 bytes. */ if (optr != out) *optr++ = '/'; *optr++ = '.'; break; case 5: /* in 4 + n bytes. out, at most: 1 + 2 * n bytes. */ if (optr != out) *optr++ = '/'; if (!read_string (ptr + 4, s - 4, optr)) goto fail; optr += VasEBoot_strlen (optr); break; default: goto fail; } ptr += s; } *optr = 0; VasEBoot_free (raw); return out; fail: VasEBoot_free (raw); VasEBoot_free (out); VasEBoot_error (VasEBoot_ERR_BAD_FS, "invalid symlink"); return NULL; } /* Context for VasEBoot_udf_dir. */ struct VasEBoot_udf_dir_ctx { VasEBoot_fs_dir_hook_t hook; void *hook_data; }; /* Helper for VasEBoot_udf_dir. */ static int VasEBoot_udf_dir_iter (const char *filename, enum VasEBoot_fshelp_filetype filetype, VasEBoot_fshelp_node_t node, void *data) { struct VasEBoot_udf_dir_ctx *ctx = data; struct VasEBoot_dirhook_info info; const struct VasEBoot_udf_timestamp *tstamp = NULL; VasEBoot_memset (&info, 0, sizeof (info)); info.dir = ((filetype & VasEBoot_FSHELP_TYPE_MASK) == VasEBoot_FSHELP_DIR); if (U16 (node->block.fe.tag.tag_ident) == VasEBoot_UDF_TAG_IDENT_FE) tstamp = &node->block.fe.modification_time; else if (U16 (node->block.fe.tag.tag_ident) == VasEBoot_UDF_TAG_IDENT_EFE) tstamp = &node->block.efe.modification_time; if (tstamp && (U16 (tstamp->type_and_timezone) & 0xf000) == 0x1000) { VasEBoot_int16_t tz; struct VasEBoot_datetime datetime; datetime.year = U16 (tstamp->year); datetime.month = tstamp->month; datetime.day = tstamp->day; datetime.hour = tstamp->hour; datetime.minute = tstamp->minute; datetime.second = tstamp->second; tz = U16 (tstamp->type_and_timezone) & 0xfff; if (tz & 0x800) tz |= 0xf000; if (tz == -2047) tz = 0; info.mtimeset = !!VasEBoot_datetime2unixtime (&datetime, &info.mtime); info.mtime -= 60 * tz; } VasEBoot_free (node); return ctx->hook (filename, &info, ctx->hook_data); } static VasEBoot_err_t VasEBoot_udf_dir (VasEBoot_device_t device, const char *path, VasEBoot_fs_dir_hook_t hook, void *hook_data) { struct VasEBoot_udf_dir_ctx ctx = { hook, hook_data }; struct VasEBoot_udf_data *data = 0; struct VasEBoot_fshelp_node *rootnode = 0; struct VasEBoot_fshelp_node *foundnode = 0; VasEBoot_dl_ref (my_mod); data = VasEBoot_udf_mount (device->disk); if (!data) goto fail; rootnode = VasEBoot_malloc (get_fshelp_size (data)); if (!rootnode) goto fail; if (VasEBoot_udf_read_icb (data, &data->root_icb, rootnode)) goto fail; if (VasEBoot_fshelp_find_file (path, rootnode, &foundnode, VasEBoot_udf_iterate_dir, VasEBoot_udf_read_symlink, VasEBoot_FSHELP_DIR)) goto fail; VasEBoot_udf_iterate_dir (foundnode, VasEBoot_udf_dir_iter, &ctx); if (foundnode != rootnode) VasEBoot_free (foundnode); fail: VasEBoot_free (rootnode); VasEBoot_free (data); VasEBoot_dl_unref (my_mod); return VasEBoot_errno; } static VasEBoot_err_t VasEBoot_udf_open (struct VasEBoot_file *file, const char *name) { struct VasEBoot_udf_data *data; struct VasEBoot_fshelp_node *rootnode = 0; struct VasEBoot_fshelp_node *foundnode; VasEBoot_dl_ref (my_mod); data = VasEBoot_udf_mount (file->device->disk); if (!data) goto fail; rootnode = VasEBoot_malloc (get_fshelp_size (data)); if (!rootnode) goto fail; if (VasEBoot_udf_read_icb (data, &data->root_icb, rootnode)) goto fail; if (VasEBoot_fshelp_find_file (name, rootnode, &foundnode, VasEBoot_udf_iterate_dir, VasEBoot_udf_read_symlink, VasEBoot_FSHELP_REG)) goto fail; file->data = foundnode; file->offset = 0; file->size = U64 (foundnode->block.fe.file_size); VasEBoot_free (rootnode); return 0; fail: VasEBoot_dl_unref (my_mod); VasEBoot_free (data); VasEBoot_free (rootnode); return VasEBoot_errno; } static VasEBoot_ssize_t VasEBoot_udf_read (VasEBoot_file_t file, char *buf, VasEBoot_size_t len) { struct VasEBoot_fshelp_node *node = (struct VasEBoot_fshelp_node *) file->data; return VasEBoot_udf_read_file (node, file->read_hook, file->read_hook_data, file->offset, len, buf); } static VasEBoot_err_t VasEBoot_udf_close (VasEBoot_file_t file) { if (file->data) { struct VasEBoot_fshelp_node *node = (struct VasEBoot_fshelp_node *) file->data; VasEBoot_free (node->data); VasEBoot_free (node); } VasEBoot_dl_unref (my_mod); return VasEBoot_ERR_NONE; } static VasEBoot_err_t VasEBoot_udf_label (VasEBoot_device_t device, char **label) { struct VasEBoot_udf_data *data; data = VasEBoot_udf_mount (device->disk); if (data) { *label = read_string (data->lvd.ident, sizeof (data->lvd.ident), 0); VasEBoot_free (data); } else *label = 0; return VasEBoot_errno; } static struct VasEBoot_fs VasEBoot_udf_fs = { .name = "udf", .dir = VasEBoot_udf_dir, .open = VasEBoot_udf_open, .read = VasEBoot_udf_read, .close = VasEBoot_udf_close, .label = VasEBoot_udf_label, #ifdef VasEBoot_UTIL .reserved_first_sector = 1, .blocklist_install = 1, #endif .next = 0 }; VasEBoot_MOD_INIT (udf) { VasEBoot_fs_register (&VasEBoot_udf_fs); my_mod = mod; } VasEBoot_MOD_FINI (udf) { VasEBoot_fs_unregister (&VasEBoot_udf_fs); }