2N/A/* udf.c - Universal Disk Format filesystem. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/file.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/types.h>
2N/A#include <grub/fshelp.h>
2N/A#include <grub/charset.h>
2N/A#include <grub/datetime.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_UDF_MAX_PDS 2
2N/A#define GRUB_UDF_MAX_PMS 6
2N/A
2N/A#define U16 grub_le_to_cpu16
2N/A#define U32 grub_le_to_cpu32
2N/A#define U64 grub_le_to_cpu64
2N/A
2N/A#define GRUB_UDF_TAG_IDENT_PVD 0x0001
2N/A#define GRUB_UDF_TAG_IDENT_AVDP 0x0002
2N/A#define GRUB_UDF_TAG_IDENT_VDP 0x0003
2N/A#define GRUB_UDF_TAG_IDENT_IUVD 0x0004
2N/A#define GRUB_UDF_TAG_IDENT_PD 0x0005
2N/A#define GRUB_UDF_TAG_IDENT_LVD 0x0006
2N/A#define GRUB_UDF_TAG_IDENT_USD 0x0007
2N/A#define GRUB_UDF_TAG_IDENT_TD 0x0008
2N/A#define GRUB_UDF_TAG_IDENT_LVID 0x0009
2N/A
2N/A#define GRUB_UDF_TAG_IDENT_FSD 0x0100
2N/A#define GRUB_UDF_TAG_IDENT_FID 0x0101
2N/A#define GRUB_UDF_TAG_IDENT_AED 0x0102
2N/A#define GRUB_UDF_TAG_IDENT_IE 0x0103
2N/A#define GRUB_UDF_TAG_IDENT_TE 0x0104
2N/A#define GRUB_UDF_TAG_IDENT_FE 0x0105
2N/A#define GRUB_UDF_TAG_IDENT_EAHD 0x0106
2N/A#define GRUB_UDF_TAG_IDENT_USE 0x0107
2N/A#define GRUB_UDF_TAG_IDENT_SBD 0x0108
2N/A#define GRUB_UDF_TAG_IDENT_PIE 0x0109
2N/A#define GRUB_UDF_TAG_IDENT_EFE 0x010A
2N/A
2N/A#define GRUB_UDF_ICBTAG_TYPE_UNDEF 0x00
2N/A#define GRUB_UDF_ICBTAG_TYPE_USE 0x01
2N/A#define GRUB_UDF_ICBTAG_TYPE_PIE 0x02
2N/A#define GRUB_UDF_ICBTAG_TYPE_IE 0x03
2N/A#define GRUB_UDF_ICBTAG_TYPE_DIRECTORY 0x04
2N/A#define GRUB_UDF_ICBTAG_TYPE_REGULAR 0x05
2N/A#define GRUB_UDF_ICBTAG_TYPE_BLOCK 0x06
2N/A#define GRUB_UDF_ICBTAG_TYPE_CHAR 0x07
2N/A#define GRUB_UDF_ICBTAG_TYPE_EA 0x08
2N/A#define GRUB_UDF_ICBTAG_TYPE_FIFO 0x09
2N/A#define GRUB_UDF_ICBTAG_TYPE_SOCKET 0x0A
2N/A#define GRUB_UDF_ICBTAG_TYPE_TE 0x0B
2N/A#define GRUB_UDF_ICBTAG_TYPE_SYMLINK 0x0C
2N/A#define GRUB_UDF_ICBTAG_TYPE_STREAMDIR 0x0D
2N/A
2N/A#define GRUB_UDF_ICBTAG_FLAG_AD_MASK 0x0007
2N/A#define GRUB_UDF_ICBTAG_FLAG_AD_SHORT 0x0000
2N/A#define GRUB_UDF_ICBTAG_FLAG_AD_LONG 0x0001
2N/A#define GRUB_UDF_ICBTAG_FLAG_AD_EXT 0x0002
2N/A#define GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003
2N/A
2N/A#define GRUB_UDF_EXT_NORMAL 0x00000000
2N/A#define GRUB_UDF_EXT_NREC_ALLOC 0x40000000
2N/A#define GRUB_UDF_EXT_NREC_NALLOC 0x80000000
2N/A#define GRUB_UDF_EXT_MASK 0xC0000000
2N/A
2N/A#define GRUB_UDF_FID_CHAR_HIDDEN 0x01
2N/A#define GRUB_UDF_FID_CHAR_DIRECTORY 0x02
2N/A#define GRUB_UDF_FID_CHAR_DELETED 0x04
2N/A#define GRUB_UDF_FID_CHAR_PARENT 0x08
2N/A#define GRUB_UDF_FID_CHAR_METADATA 0x10
2N/A
2N/A#define GRUB_UDF_STD_IDENT_BEA01 "BEA01"
2N/A#define GRUB_UDF_STD_IDENT_BOOT2 "BOOT2"
2N/A#define GRUB_UDF_STD_IDENT_CD001 "CD001"
2N/A#define GRUB_UDF_STD_IDENT_CDW02 "CDW02"
2N/A#define GRUB_UDF_STD_IDENT_NSR02 "NSR02"
2N/A#define GRUB_UDF_STD_IDENT_NSR03 "NSR03"
2N/A#define GRUB_UDF_STD_IDENT_TEA01 "TEA01"
2N/A
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS0 0x00
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS1 0x01
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS2 0x02
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS3 0x03
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS4 0x04
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS5 0x05
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS6 0x06
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS7 0x07
2N/A#define GRUB_UDF_CHARSPEC_TYPE_CS8 0x08
2N/A
2N/A#define GRUB_UDF_PARTMAP_TYPE_1 1
2N/A#define GRUB_UDF_PARTMAP_TYPE_2 2
2N/A
2N/Astruct grub_udf_lb_addr
2N/A{
2N/A grub_uint32_t block_num;
2N/A grub_uint16_t part_ref;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_short_ad
2N/A{
2N/A grub_uint32_t length;
2N/A grub_uint32_t position;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_long_ad
2N/A{
2N/A grub_uint32_t length;
2N/A struct grub_udf_lb_addr block;
2N/A grub_uint8_t imp_use[6];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_extent_ad
2N/A{
2N/A grub_uint32_t length;
2N/A grub_uint32_t start;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_charspec
2N/A{
2N/A grub_uint8_t charset_type;
2N/A grub_uint8_t charset_info[63];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_timestamp
2N/A{
2N/A grub_uint16_t type_and_timezone;
2N/A grub_uint16_t year;
2N/A grub_uint8_t month;
2N/A grub_uint8_t day;
2N/A grub_uint8_t hour;
2N/A grub_uint8_t minute;
2N/A grub_uint8_t second;
2N/A grub_uint8_t centi_seconds;
2N/A grub_uint8_t hundreds_of_micro_seconds;
2N/A grub_uint8_t micro_seconds;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_regid
2N/A{
2N/A grub_uint8_t flags;
2N/A grub_uint8_t ident[23];
2N/A grub_uint8_t ident_suffix[8];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_tag
2N/A{
2N/A grub_uint16_t tag_ident;
2N/A grub_uint16_t desc_version;
2N/A grub_uint8_t tag_checksum;
2N/A grub_uint8_t reserved;
2N/A grub_uint16_t tag_serial_number;
2N/A grub_uint16_t desc_crc;
2N/A grub_uint16_t desc_crc_length;
2N/A grub_uint32_t tag_location;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_fileset
2N/A{
2N/A struct grub_udf_tag tag;
2N/A struct grub_udf_timestamp datetime;
2N/A grub_uint16_t interchange_level;
2N/A grub_uint16_t max_interchange_level;
2N/A grub_uint32_t charset_list;
2N/A grub_uint32_t max_charset_list;
2N/A grub_uint32_t fileset_num;
2N/A grub_uint32_t fileset_desc_num;
2N/A struct grub_udf_charspec vol_charset;
2N/A grub_uint8_t vol_ident[128];
2N/A struct grub_udf_charspec fileset_charset;
2N/A grub_uint8_t fileset_ident[32];
2N/A grub_uint8_t copyright_file_ident[32];
2N/A grub_uint8_t abstract_file_ident[32];
2N/A struct grub_udf_long_ad root_icb;
2N/A struct grub_udf_regid domain_ident;
2N/A struct grub_udf_long_ad next_ext;
2N/A struct grub_udf_long_ad streamdir_icb;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_icbtag
2N/A{
2N/A grub_uint32_t prior_recorded_num_direct_entries;
2N/A grub_uint16_t strategy_type;
2N/A grub_uint16_t strategy_parameter;
2N/A grub_uint16_t num_entries;
2N/A grub_uint8_t reserved;
2N/A grub_uint8_t file_type;
2N/A struct grub_udf_lb_addr parent_idb;
2N/A grub_uint16_t flags;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_file_ident
2N/A{
2N/A struct grub_udf_tag tag;
2N/A grub_uint16_t version_num;
2N/A grub_uint8_t characteristics;
2N/A grub_uint8_t file_ident_length;
2N/A struct grub_udf_long_ad icb;
2N/A grub_uint16_t imp_use_length;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_file_entry
2N/A{
2N/A struct grub_udf_tag tag;
2N/A struct grub_udf_icbtag icbtag;
2N/A grub_uint32_t uid;
2N/A grub_uint32_t gid;
2N/A grub_uint32_t permissions;
2N/A grub_uint16_t link_count;
2N/A grub_uint8_t record_format;
2N/A grub_uint8_t record_display_attr;
2N/A grub_uint32_t record_length;
2N/A grub_uint64_t file_size;
2N/A grub_uint64_t blocks_recorded;
2N/A struct grub_udf_timestamp access_time;
2N/A struct grub_udf_timestamp modification_time;
2N/A struct grub_udf_timestamp attr_time;
2N/A grub_uint32_t checkpoint;
2N/A struct grub_udf_long_ad extended_attr_idb;
2N/A struct grub_udf_regid imp_ident;
2N/A grub_uint64_t unique_id;
2N/A grub_uint32_t ext_attr_length;
2N/A grub_uint32_t alloc_descs_length;
2N/A grub_uint8_t ext_attr[1872];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_extended_file_entry
2N/A{
2N/A struct grub_udf_tag tag;
2N/A struct grub_udf_icbtag icbtag;
2N/A grub_uint32_t uid;
2N/A grub_uint32_t gid;
2N/A grub_uint32_t permissions;
2N/A grub_uint16_t link_count;
2N/A grub_uint8_t record_format;
2N/A grub_uint8_t record_display_attr;
2N/A grub_uint32_t record_length;
2N/A grub_uint64_t file_size;
2N/A grub_uint64_t object_size;
2N/A grub_uint64_t blocks_recorded;
2N/A struct grub_udf_timestamp access_time;
2N/A struct grub_udf_timestamp modification_time;
2N/A struct grub_udf_timestamp create_time;
2N/A struct grub_udf_timestamp attr_time;
2N/A grub_uint32_t checkpoint;
2N/A grub_uint32_t reserved;
2N/A struct grub_udf_long_ad extended_attr_icb;
2N/A struct grub_udf_long_ad streamdir_icb;
2N/A struct grub_udf_regid imp_ident;
2N/A grub_uint64_t unique_id;
2N/A grub_uint32_t ext_attr_length;
2N/A grub_uint32_t alloc_descs_length;
2N/A grub_uint8_t ext_attr[1832];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_vrs
2N/A{
2N/A grub_uint8_t type;
2N/A grub_uint8_t magic[5];
2N/A grub_uint8_t version;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_avdp
2N/A{
2N/A struct grub_udf_tag tag;
2N/A struct grub_udf_extent_ad vds;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_pd
2N/A{
2N/A struct grub_udf_tag tag;
2N/A grub_uint32_t seq_num;
2N/A grub_uint16_t flags;
2N/A grub_uint16_t part_num;
2N/A struct grub_udf_regid contents;
2N/A grub_uint8_t contents_use[128];
2N/A grub_uint32_t access_type;
2N/A grub_uint32_t start;
2N/A grub_uint32_t length;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_partmap
2N/A{
2N/A grub_uint8_t type;
2N/A grub_uint8_t length;
2N/A union
2N/A {
2N/A struct
2N/A {
2N/A grub_uint16_t seq_num;
2N/A grub_uint16_t part_num;
2N/A } type1;
2N/A
2N/A struct
2N/A {
2N/A grub_uint8_t ident[62];
2N/A } type2;
2N/A };
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_lvd
2N/A{
2N/A struct grub_udf_tag tag;
2N/A grub_uint32_t seq_num;
2N/A struct grub_udf_charspec charset;
2N/A grub_uint8_t ident[128];
2N/A grub_uint32_t bsize;
2N/A struct grub_udf_regid domain_ident;
2N/A struct grub_udf_long_ad root_fileset;
2N/A grub_uint32_t map_table_length;
2N/A grub_uint32_t num_part_maps;
2N/A struct grub_udf_regid imp_ident;
2N/A grub_uint8_t imp_use[128];
2N/A struct grub_udf_extent_ad integrity_seq_ext;
2N/A grub_uint8_t part_maps[1608];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_aed
2N/A{
2N/A struct grub_udf_tag tag;
2N/A grub_uint32_t prev_ae;
2N/A grub_uint32_t ae_len;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_udf_data
2N/A{
2N/A grub_disk_t disk;
2N/A struct grub_udf_lvd lvd;
2N/A struct grub_udf_pd pds[GRUB_UDF_MAX_PDS];
2N/A struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS];
2N/A struct grub_udf_long_ad root_icb;
2N/A int npd, npm, lbshift;
2N/A};
2N/A
2N/Astruct grub_fshelp_node
2N/A{
2N/A struct grub_udf_data *data;
2N/A union
2N/A {
2N/A struct grub_udf_file_entry fe;
2N/A struct grub_udf_extended_file_entry efe;
2N/A };
2N/A int part_ref;
2N/A};
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/Astatic grub_uint32_t
2N/Agrub_udf_get_block (struct grub_udf_data *data,
2N/A grub_uint16_t part_ref, grub_uint32_t block)
2N/A{
2N/A part_ref = U16 (part_ref);
2N/A
2N/A if (part_ref >= data->npm)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid part ref");
2N/A return 0;
2N/A }
2N/A
2N/A return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start)
2N/A + U32 (block));
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_udf_read_icb (struct grub_udf_data *data,
2N/A struct grub_udf_long_ad *icb,
2N/A struct grub_fshelp_node *node)
2N/A{
2N/A grub_uint32_t block;
2N/A
2N/A block = grub_udf_get_block (data,
2N/A icb->block.part_ref,
2N/A icb->block.block_num);
2N/A
2N/A if (grub_errno)
2N/A return grub_errno;
2N/A
2N/A if (grub_disk_read (data->disk, block << data->lbshift, 0,
2N/A sizeof (struct grub_udf_file_entry),
2N/A &node->fe))
2N/A return grub_errno;
2N/A
2N/A if ((U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FE) &&
2N/A (U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_EFE))
2N/A return grub_error (GRUB_ERR_BAD_FS, "invalid fe/efe descriptor");
2N/A
2N/A node->part_ref = icb->block.part_ref;
2N/A node->data = data;
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_disk_addr_t
2N/Agrub_udf_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
2N/A{
2N/A char *buf = NULL;
2N/A char *ptr;
2N/A grub_ssize_t len;
2N/A grub_disk_addr_t filebytes;
2N/A
2N/A switch (U16 (node->fe.tag.tag_ident))
2N/A {
2N/A case GRUB_UDF_TAG_IDENT_FE:
2N/A ptr = (char *) &node->fe.ext_attr[0] + U32 (node->fe.ext_attr_length);
2N/A len = U32 (node->fe.alloc_descs_length);
2N/A break;
2N/A
2N/A case GRUB_UDF_TAG_IDENT_EFE:
2N/A ptr = (char *) &node->efe.ext_attr[0] + U32 (node->efe.ext_attr_length);
2N/A len = U32 (node->efe.alloc_descs_length);
2N/A break;
2N/A
2N/A default:
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid file entry");
2N/A return 0;
2N/A }
2N/A
2N/A if ((U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
2N/A == GRUB_UDF_ICBTAG_FLAG_AD_SHORT)
2N/A {
2N/A struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr;
2N/A
2N/A filebytes = fileblock * U32 (node->data->lvd.bsize);
2N/A while (len >= (grub_ssize_t) sizeof (struct grub_udf_short_ad))
2N/A {
2N/A grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
2N/A grub_uint32_t adtype = U32 (ad->length) >> 30;
2N/A if (adtype == 3)
2N/A {
2N/A struct grub_udf_aed *extension;
2N/A grub_disk_addr_t sec = grub_udf_get_block(node->data,
2N/A node->part_ref,
2N/A ad->position);
2N/A if (!buf)
2N/A {
2N/A buf = grub_malloc (U32 (node->data->lvd.bsize));
2N/A if (!buf)
2N/A return 0;
2N/A }
2N/A if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
2N/A 0, adlen, buf))
2N/A goto fail;
2N/A
2N/A extension = (struct grub_udf_aed *) buf;
2N/A if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
2N/A goto fail;
2N/A }
2N/A
2N/A len = U32 (extension->ae_len);
2N/A ad = (struct grub_udf_short_ad *)
2N/A (buf + sizeof (struct grub_udf_aed));
2N/A continue;
2N/A }
2N/A
2N/A if (filebytes < adlen)
2N/A {
2N/A grub_uint32_t ad_pos = ad->position;
2N/A grub_free (buf);
2N/A return ((U32 (ad_pos) & GRUB_UDF_EXT_MASK) ? 0 :
2N/A (grub_udf_get_block (node->data, node->part_ref, ad_pos)
2N/A + (filebytes >> (GRUB_DISK_SECTOR_BITS
2N/A + node->data->lbshift))));
2N/A }
2N/A
2N/A filebytes -= adlen;
2N/A ad++;
2N/A len -= sizeof (struct grub_udf_short_ad);
2N/A }
2N/A }
2N/A else
2N/A {
2N/A struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr;
2N/A
2N/A filebytes = fileblock * U32 (node->data->lvd.bsize);
2N/A while (len >= (grub_ssize_t) sizeof (struct grub_udf_long_ad))
2N/A {
2N/A grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
2N/A grub_uint32_t adtype = U32 (ad->length) >> 30;
2N/A if (adtype == 3)
2N/A {
2N/A struct grub_udf_aed *extension;
2N/A grub_disk_addr_t sec = grub_udf_get_block(node->data,
2N/A ad->block.part_ref,
2N/A ad->block.block_num);
2N/A if (!buf)
2N/A {
2N/A buf = grub_malloc (U32 (node->data->lvd.bsize));
2N/A if (!buf)
2N/A return 0;
2N/A }
2N/A if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
2N/A 0, adlen, buf))
2N/A goto fail;
2N/A
2N/A extension = (struct grub_udf_aed *) buf;
2N/A if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
2N/A goto fail;
2N/A }
2N/A
2N/A len = U32 (extension->ae_len);
2N/A ad = (struct grub_udf_long_ad *)
2N/A (buf + sizeof (struct grub_udf_aed));
2N/A continue;
2N/A }
2N/A
2N/A if (filebytes < adlen)
2N/A {
2N/A grub_uint32_t ad_block_num = ad->block.block_num;
2N/A grub_uint32_t ad_part_ref = ad->block.part_ref;
2N/A grub_free (buf);
2N/A return ((U32 (ad_block_num) & GRUB_UDF_EXT_MASK) ? 0 :
2N/A (grub_udf_get_block (node->data, ad_part_ref,
2N/A ad_block_num)
2N/A + (filebytes >> (GRUB_DISK_SECTOR_BITS
2N/A + node->data->lbshift))));
2N/A }
2N/A
2N/A filebytes -= adlen;
2N/A ad++;
2N/A len -= sizeof (struct grub_udf_long_ad);
2N/A }
2N/A }
2N/A
2N/Afail:
2N/A grub_free (buf);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_udf_read_file (grub_fshelp_node_t node,
2N/A void NESTED_FUNC_ATTR
2N/A (*read_hook) (grub_disk_addr_t sector,
2N/A unsigned offset, unsigned length),
2N/A grub_off_t pos, grub_size_t len, char *buf)
2N/A{
2N/A switch (U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
2N/A {
2N/A case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB:
2N/A {
2N/A char *ptr;
2N/A
2N/A ptr = ((U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ?
2N/A ((char *) &node->fe.ext_attr[0]
2N/A + U32 (node->fe.ext_attr_length)) :
2N/A ((char *) &node->efe.ext_attr[0]
2N/A + U32 (node->efe.ext_attr_length)));
2N/A
2N/A grub_memcpy (buf, ptr + pos, len);
2N/A
2N/A return len;
2N/A }
2N/A
2N/A case GRUB_UDF_ICBTAG_FLAG_AD_EXT:
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid extent type");
2N/A return 0;
2N/A }
2N/A
2N/A return grub_fshelp_read_file (node->data->disk, node, read_hook,
2N/A pos, len, buf, grub_udf_read_block,
2N/A U64 (node->fe.file_size),
2N/A node->data->lbshift);
2N/A}
2N/A
2N/Astatic unsigned sblocklist[] = { 256, 512, 0 };
2N/A
2N/Astatic struct grub_udf_data *
2N/Agrub_udf_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_udf_data *data = 0;
2N/A struct grub_udf_fileset root_fs;
2N/A unsigned *sblklist;
2N/A grub_uint32_t block, vblock;
2N/A int i, lbshift;
2N/A
2N/A data = grub_malloc (sizeof (struct grub_udf_data));
2N/A if (!data)
2N/A return 0;
2N/A
2N/A data->disk = disk;
2N/A
2N/A /* Search for Anchor Volume Descriptor Pointer (AVDP)
2N/A * and determine logical block size. */
2N/A block = 0;
2N/A for (lbshift = 0; lbshift < 4; lbshift++)
2N/A {
2N/A for (sblklist = sblocklist; *sblklist; sblklist++)
2N/A {
2N/A struct grub_udf_avdp avdp;
2N/A
2N/A if (grub_disk_read (disk, *sblklist << lbshift, 0,
2N/A sizeof (struct grub_udf_avdp), &avdp))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A if (U16 (avdp.tag.tag_ident) == GRUB_UDF_TAG_IDENT_AVDP &&
2N/A U32 (avdp.tag.tag_location) == *sblklist)
2N/A {
2N/A block = U32 (avdp.vds.start);
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (block)
2N/A break;
2N/A }
2N/A
2N/A if (!block)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A data->lbshift = lbshift;
2N/A
2N/A /* Search for Volume Recognition Sequence (VRS). */
2N/A for (vblock = (32767 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1;;
2N/A vblock += (2047 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1)
2N/A {
2N/A struct grub_udf_vrs vrs;
2N/A
2N/A if (grub_disk_read (disk, vblock << lbshift, 0,
2N/A sizeof (struct grub_udf_vrs), &vrs))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A if ((!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR03, 5)) ||
2N/A (!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR02, 5)))
2N/A break;
2N/A
2N/A if ((grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BEA01, 5)) &&
2N/A (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BOOT2, 5)) &&
2N/A (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CD001, 5)) &&
2N/A (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CDW02, 5)) &&
2N/A (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_TEA01, 5)))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A }
2N/A
2N/A data->npd = data->npm = 0;
2N/A /* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */
2N/A while (1)
2N/A {
2N/A struct grub_udf_tag tag;
2N/A
2N/A if (grub_disk_read (disk, block << lbshift, 0,
2N/A sizeof (struct grub_udf_tag), &tag))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A tag.tag_ident = U16 (tag.tag_ident);
2N/A if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
2N/A {
2N/A if (data->npd >= GRUB_UDF_MAX_PDS)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "too many PDs");
2N/A goto fail;
2N/A }
2N/A
2N/A if (grub_disk_read (disk, block << lbshift, 0,
2N/A sizeof (struct grub_udf_pd),
2N/A &data->pds[data->npd]))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A data->npd++;
2N/A }
2N/A else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_LVD)
2N/A {
2N/A int k;
2N/A
2N/A struct grub_udf_partmap *ppm;
2N/A
2N/A if (grub_disk_read (disk, block << lbshift, 0,
2N/A sizeof (struct grub_udf_lvd),
2N/A &data->lvd))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A if (data->npm + U32 (data->lvd.num_part_maps) > GRUB_UDF_MAX_PMS)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "too many partition maps");
2N/A goto fail;
2N/A }
2N/A
2N/A ppm = (struct grub_udf_partmap *) &data->lvd.part_maps;
2N/A for (k = U32 (data->lvd.num_part_maps); k > 0; k--)
2N/A {
2N/A if (ppm->type != GRUB_UDF_PARTMAP_TYPE_1)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "partmap type not supported");
2N/A goto fail;
2N/A }
2N/A
2N/A data->pms[data->npm++] = ppm;
2N/A ppm = (struct grub_udf_partmap *) ((char *) ppm +
2N/A U32 (ppm->length));
2N/A }
2N/A }
2N/A else if (tag.tag_ident > GRUB_UDF_TAG_IDENT_TD)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid tag ident");
2N/A goto fail;
2N/A }
2N/A else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_TD)
2N/A break;
2N/A
2N/A block++;
2N/A }
2N/A
2N/A for (i = 0; i < data->npm; i++)
2N/A {
2N/A int j;
2N/A
2N/A for (j = 0; j < data->npd; j++)
2N/A if (data->pms[i]->type1.part_num == data->pds[j].part_num)
2N/A {
2N/A data->pms[i]->type1.part_num = j;
2N/A break;
2N/A }
2N/A
2N/A if (j == data->npd)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "can\'t find PD");
2N/A goto fail;
2N/A }
2N/A }
2N/A
2N/A block = grub_udf_get_block (data,
2N/A data->lvd.root_fileset.block.part_ref,
2N/A data->lvd.root_fileset.block.block_num);
2N/A
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A if (grub_disk_read (disk, block << lbshift, 0,
2N/A sizeof (struct grub_udf_fileset), &root_fs))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A if (U16 (root_fs.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FSD)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid fileset descriptor");
2N/A goto fail;
2N/A }
2N/A
2N/A data->root_icb = root_fs.root_icb;
2N/A
2N/A return data;
2N/A
2N/Afail:
2N/A grub_free (data);
2N/A return 0;
2N/A}
2N/A
2N/Astatic char *
2N/Aread_string (grub_uint8_t *raw, grub_size_t sz)
2N/A{
2N/A grub_uint16_t *utf16 = NULL;
2N/A char *ret;
2N/A grub_size_t utf16len = 0;
2N/A
2N/A if (raw[0] != 8 && raw[0] != 16)
2N/A return NULL;
2N/A
2N/A if (raw[0] == 8)
2N/A {
2N/A unsigned i;
2N/A utf16len = sz - 1;
2N/A utf16 = grub_malloc (utf16len * sizeof (utf16[0]));
2N/A if (!utf16)
2N/A return NULL;
2N/A for (i = 0; i < utf16len; i++)
2N/A utf16[i] = raw[i + 1];
2N/A }
2N/A if (raw[0] == 16)
2N/A {
2N/A unsigned i;
2N/A utf16len = (sz - 1) / 2;
2N/A utf16 = grub_malloc (utf16len * sizeof (utf16[0]));
2N/A if (!utf16)
2N/A return NULL;
2N/A for (i = 0; i < utf16len; i++)
2N/A utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2];
2N/A }
2N/A ret = grub_malloc (utf16len * GRUB_MAX_UTF8_PER_UTF16 + 1);
2N/A if (ret)
2N/A *grub_utf16_to_utf8 ((grub_uint8_t *) ret, utf16, utf16len) = '\0';
2N/A grub_free (utf16);
2N/A return ret;
2N/A}
2N/A
2N/Astatic int
2N/Agrub_udf_iterate_dir (grub_fshelp_node_t dir,
2N/A int NESTED_FUNC_ATTR
2N/A (*hook) (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node))
2N/A{
2N/A grub_fshelp_node_t child;
2N/A struct grub_udf_file_ident dirent;
2N/A grub_off_t offset = 0;
2N/A
2N/A child = grub_malloc (sizeof (struct grub_fshelp_node));
2N/A if (!child)
2N/A return 0;
2N/A
2N/A /* The current directory is not stored. */
2N/A grub_memcpy ((char *) child, (char *) dir,
2N/A sizeof (struct grub_fshelp_node));
2N/A
2N/A if (hook (".", GRUB_FSHELP_DIR, child))
2N/A return 1;
2N/A
2N/A while (offset < U64 (dir->fe.file_size))
2N/A {
2N/A if (grub_udf_read_file (dir, 0, offset, sizeof (dirent),
2N/A (char *) &dirent) != sizeof (dirent))
2N/A return 0;
2N/A
2N/A if (U16 (dirent.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FID)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "invalid fid tag");
2N/A return 0;
2N/A }
2N/A
2N/A offset += sizeof (dirent) + U16 (dirent.imp_use_length);
2N/A if (!(dirent.characteristics & GRUB_UDF_FID_CHAR_DELETED))
2N/A {
2N/A child = grub_malloc (sizeof (struct grub_fshelp_node));
2N/A if (!child)
2N/A return 0;
2N/A
2N/A if (grub_udf_read_icb (dir->data, &dirent.icb, child))
2N/A return 0;
2N/A
2N/A if (dirent.characteristics & GRUB_UDF_FID_CHAR_PARENT)
2N/A {
2N/A /* This is the parent directory. */
2N/A if (hook ("..", GRUB_FSHELP_DIR, child))
2N/A return 1;
2N/A }
2N/A else
2N/A {
2N/A enum grub_fshelp_filetype type;
2N/A char *filename;
2N/A grub_uint8_t raw[dirent.file_ident_length];
2N/A
2N/A type = ((dirent.characteristics & GRUB_UDF_FID_CHAR_DIRECTORY) ?
2N/A (GRUB_FSHELP_DIR) : (GRUB_FSHELP_REG));
2N/A if (child->fe.icbtag.file_type == GRUB_UDF_ICBTAG_TYPE_SYMLINK)
2N/A type = GRUB_FSHELP_SYMLINK;
2N/A
2N/A if ((grub_udf_read_file (dir, 0, offset,
2N/A dirent.file_ident_length,
2N/A (char *) raw))
2N/A != dirent.file_ident_length)
2N/A return 0;
2N/A
2N/A filename = read_string (raw, dirent.file_ident_length);
2N/A if (!filename)
2N/A grub_print_error ();
2N/A
2N/A if (filename && hook (filename, type, child))
2N/A {
2N/A grub_free (filename);
2N/A return 1;
2N/A }
2N/A grub_free (filename);
2N/A }
2N/A }
2N/A
2N/A /* Align to dword boundary. */
2N/A offset = (offset + dirent.file_ident_length + 3) & (~3);
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic char *
2N/Agrub_ufs_read_symlink (grub_fshelp_node_t node)
2N/A{
2N/A grub_size_t sz = U64 (node->fe.file_size);
2N/A grub_uint8_t *raw;
2N/A char *ret;
2N/A
2N/A if (sz < 4)
2N/A return NULL;
2N/A raw = grub_malloc (sz - 4);
2N/A if (!raw)
2N/A return NULL;
2N/A if (grub_udf_read_file (node, NULL, 4, sz - 4, (char *) raw) < 0)
2N/A return NULL;
2N/A ret = read_string (raw, sz - 4);
2N/A grub_free (raw);
2N/A return ret;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_udf_dir (grub_device_t device, const char *path,
2N/A int (*hook) (const char *filename,
2N/A const struct grub_dirhook_info *info))
2N/A{
2N/A struct grub_udf_data *data = 0;
2N/A struct grub_fshelp_node rootnode;
2N/A struct grub_fshelp_node *foundnode;
2N/A
2N/A auto int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node);
2N/A
2N/A int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node)
2N/A {
2N/A struct grub_dirhook_info info;
2N/A const struct grub_udf_timestamp *tstamp = NULL;
2N/A grub_memset (&info, 0, sizeof (info));
2N/A info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
2N/A if (U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE)
2N/A tstamp = &node->fe.modification_time;
2N/A else if (U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_EFE)
2N/A tstamp = &node->efe.modification_time;
2N/A
2N/A if (tstamp && (U16 (tstamp->type_and_timezone) & 0xf000) == 0x1000)
2N/A {
2N/A grub_int16_t tz;
2N/A struct grub_datetime datetime;
2N/A
2N/A datetime.year = U16 (tstamp->year);
2N/A datetime.month = tstamp->month;
2N/A datetime.day = tstamp->day;
2N/A datetime.hour = tstamp->hour;
2N/A datetime.minute = tstamp->minute;
2N/A datetime.second = tstamp->second;
2N/A
2N/A tz = U16 (tstamp->type_and_timezone) & 0xfff;
2N/A if (tz & 0x800)
2N/A tz |= 0xf000;
2N/A if (tz == -2047)
2N/A tz = 0;
2N/A
2N/A info.mtimeset = !!grub_datetime2unixtime (&datetime, &info.mtime);
2N/A
2N/A info.mtime -= 60 * tz;
2N/A }
2N/A grub_free (node);
2N/A return hook (filename, &info);
2N/A }
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_udf_mount (device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
2N/A goto fail;
2N/A
2N/A if (grub_fshelp_find_file (path, &rootnode,
2N/A &foundnode,
2N/A grub_udf_iterate_dir, grub_ufs_read_symlink,
2N/A GRUB_FSHELP_DIR))
2N/A goto fail;
2N/A
2N/A grub_udf_iterate_dir (foundnode, iterate);
2N/A
2N/A if (foundnode != &rootnode)
2N/A grub_free (foundnode);
2N/A
2N/Afail:
2N/A grub_free (data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_udf_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_udf_data *data;
2N/A struct grub_fshelp_node rootnode;
2N/A struct grub_fshelp_node *foundnode;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_udf_mount (file->device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
2N/A goto fail;
2N/A
2N/A if (grub_fshelp_find_file (name, &rootnode,
2N/A &foundnode,
2N/A grub_udf_iterate_dir, grub_ufs_read_symlink,
2N/A GRUB_FSHELP_REG))
2N/A goto fail;
2N/A
2N/A file->data = foundnode;
2N/A file->offset = 0;
2N/A file->size = U64 (foundnode->fe.file_size);
2N/A
2N/A return 0;
2N/A
2N/Afail:
2N/A grub_dl_unref (my_mod);
2N/A
2N/A grub_free (data);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_udf_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
2N/A
2N/A return grub_udf_read_file (node, file->read_hook, file->offset, len, buf);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_udf_close (grub_file_t file)
2N/A{
2N/A if (file->data)
2N/A {
2N/A struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
2N/A
2N/A grub_free (node->data);
2N/A grub_free (node);
2N/A }
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_udf_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_udf_data *data;
2N/A data = grub_udf_mount (device->disk);
2N/A
2N/A if (data)
2N/A {
2N/A *label = read_string (data->lvd.ident, sizeof (data->lvd.ident));
2N/A grub_free (data);
2N/A }
2N/A else
2N/A *label = 0;
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic struct grub_fs grub_udf_fs = {
2N/A .name = "udf",
2N/A .dir = grub_udf_dir,
2N/A .open = grub_udf_open,
2N/A .read = grub_udf_read,
2N/A .close = grub_udf_close,
2N/A .label = grub_udf_label,
2N/A .next = 0
2N/A};
2N/A
2N/AGRUB_MOD_INIT (udf)
2N/A{
2N/A grub_fs_register (&grub_udf_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI (udf)
2N/A{
2N/A grub_fs_unregister (&grub_udf_fs);
2N/A}