1 /* udf.c - Universal Disk Format filesystem. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/file.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
26 #include <grub/types.h>
27 #include <grub/fshelp.h>
28 #include <grub/charset.h>
29 #include <grub/datetime.h>
32 GRUB_MOD_LICENSE ("GPLv3+");
34 #define GRUB_UDF_MAX_PDS 2
35 #define GRUB_UDF_MAX_PMS 6
37 #define U16 grub_le_to_cpu16
38 #define U32 grub_le_to_cpu32
39 #define U64 grub_le_to_cpu64
41 #define GRUB_UDF_TAG_IDENT_PVD 0x0001
42 #define GRUB_UDF_TAG_IDENT_AVDP 0x0002
43 #define GRUB_UDF_TAG_IDENT_VDP 0x0003
44 #define GRUB_UDF_TAG_IDENT_IUVD 0x0004
45 #define GRUB_UDF_TAG_IDENT_PD 0x0005
46 #define GRUB_UDF_TAG_IDENT_LVD 0x0006
47 #define GRUB_UDF_TAG_IDENT_USD 0x0007
48 #define GRUB_UDF_TAG_IDENT_TD 0x0008
49 #define GRUB_UDF_TAG_IDENT_LVID 0x0009
51 #define GRUB_UDF_TAG_IDENT_FSD 0x0100
52 #define GRUB_UDF_TAG_IDENT_FID 0x0101
53 #define GRUB_UDF_TAG_IDENT_AED 0x0102
54 #define GRUB_UDF_TAG_IDENT_IE 0x0103
55 #define GRUB_UDF_TAG_IDENT_TE 0x0104
56 #define GRUB_UDF_TAG_IDENT_FE 0x0105
57 #define GRUB_UDF_TAG_IDENT_EAHD 0x0106
58 #define GRUB_UDF_TAG_IDENT_USE 0x0107
59 #define GRUB_UDF_TAG_IDENT_SBD 0x0108
60 #define GRUB_UDF_TAG_IDENT_PIE 0x0109
61 #define GRUB_UDF_TAG_IDENT_EFE 0x010A
63 #define GRUB_UDF_ICBTAG_TYPE_UNDEF 0x00
64 #define GRUB_UDF_ICBTAG_TYPE_USE 0x01
65 #define GRUB_UDF_ICBTAG_TYPE_PIE 0x02
66 #define GRUB_UDF_ICBTAG_TYPE_IE 0x03
67 #define GRUB_UDF_ICBTAG_TYPE_DIRECTORY 0x04
68 #define GRUB_UDF_ICBTAG_TYPE_REGULAR 0x05
69 #define GRUB_UDF_ICBTAG_TYPE_BLOCK 0x06
70 #define GRUB_UDF_ICBTAG_TYPE_CHAR 0x07
71 #define GRUB_UDF_ICBTAG_TYPE_EA 0x08
72 #define GRUB_UDF_ICBTAG_TYPE_FIFO 0x09
73 #define GRUB_UDF_ICBTAG_TYPE_SOCKET 0x0A
74 #define GRUB_UDF_ICBTAG_TYPE_TE 0x0B
75 #define GRUB_UDF_ICBTAG_TYPE_SYMLINK 0x0C
76 #define GRUB_UDF_ICBTAG_TYPE_STREAMDIR 0x0D
78 #define GRUB_UDF_ICBTAG_FLAG_AD_MASK 0x0007
79 #define GRUB_UDF_ICBTAG_FLAG_AD_SHORT 0x0000
80 #define GRUB_UDF_ICBTAG_FLAG_AD_LONG 0x0001
81 #define GRUB_UDF_ICBTAG_FLAG_AD_EXT 0x0002
82 #define GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003
84 #define GRUB_UDF_EXT_NORMAL 0x00000000
85 #define GRUB_UDF_EXT_NREC_ALLOC 0x40000000
86 #define GRUB_UDF_EXT_NREC_NALLOC 0x80000000
87 #define GRUB_UDF_EXT_MASK 0xC0000000
89 #define GRUB_UDF_FID_CHAR_HIDDEN 0x01
90 #define GRUB_UDF_FID_CHAR_DIRECTORY 0x02
91 #define GRUB_UDF_FID_CHAR_DELETED 0x04
92 #define GRUB_UDF_FID_CHAR_PARENT 0x08
93 #define GRUB_UDF_FID_CHAR_METADATA 0x10
95 #define GRUB_UDF_STD_IDENT_BEA01 "BEA01"
96 #define GRUB_UDF_STD_IDENT_BOOT2 "BOOT2"
97 #define GRUB_UDF_STD_IDENT_CD001 "CD001"
98 #define GRUB_UDF_STD_IDENT_CDW02 "CDW02"
99 #define GRUB_UDF_STD_IDENT_NSR02 "NSR02"
100 #define GRUB_UDF_STD_IDENT_NSR03 "NSR03"
101 #define GRUB_UDF_STD_IDENT_TEA01 "TEA01"
103 #define GRUB_UDF_CHARSPEC_TYPE_CS0 0x00
104 #define GRUB_UDF_CHARSPEC_TYPE_CS1 0x01
105 #define GRUB_UDF_CHARSPEC_TYPE_CS2 0x02
106 #define GRUB_UDF_CHARSPEC_TYPE_CS3 0x03
107 #define GRUB_UDF_CHARSPEC_TYPE_CS4 0x04
108 #define GRUB_UDF_CHARSPEC_TYPE_CS5 0x05
109 #define GRUB_UDF_CHARSPEC_TYPE_CS6 0x06
110 #define GRUB_UDF_CHARSPEC_TYPE_CS7 0x07
111 #define GRUB_UDF_CHARSPEC_TYPE_CS8 0x08
113 #define GRUB_UDF_PARTMAP_TYPE_1 1
114 #define GRUB_UDF_PARTMAP_TYPE_2 2
116 struct grub_udf_lb_addr
118 grub_uint32_t block_num;
119 grub_uint16_t part_ref;
122 struct grub_udf_short_ad
124 grub_uint32_t length;
125 grub_uint32_t position;
128 struct grub_udf_long_ad
130 grub_uint32_t length;
131 struct grub_udf_lb_addr block;
132 grub_uint8_t imp_use[6];
135 struct grub_udf_extent_ad
137 grub_uint32_t length;
141 struct grub_udf_charspec
143 grub_uint8_t charset_type;
144 grub_uint8_t charset_info[63];
147 struct grub_udf_timestamp
149 grub_uint16_t type_and_timezone;
156 grub_uint8_t centi_seconds;
157 grub_uint8_t hundreds_of_micro_seconds;
158 grub_uint8_t micro_seconds;
161 struct grub_udf_regid
164 grub_uint8_t ident[23];
165 grub_uint8_t ident_suffix[8];
170 grub_uint16_t tag_ident;
171 grub_uint16_t desc_version;
172 grub_uint8_t tag_checksum;
173 grub_uint8_t reserved;
174 grub_uint16_t tag_serial_number;
175 grub_uint16_t desc_crc;
176 grub_uint16_t desc_crc_length;
177 grub_uint32_t tag_location;
180 struct grub_udf_fileset
182 struct grub_udf_tag tag;
183 struct grub_udf_timestamp datetime;
184 grub_uint16_t interchange_level;
185 grub_uint16_t max_interchange_level;
186 grub_uint32_t charset_list;
187 grub_uint32_t max_charset_list;
188 grub_uint32_t fileset_num;
189 grub_uint32_t fileset_desc_num;
190 struct grub_udf_charspec vol_charset;
191 grub_uint8_t vol_ident[128];
192 struct grub_udf_charspec fileset_charset;
193 grub_uint8_t fileset_ident[32];
194 grub_uint8_t copyright_file_ident[32];
195 grub_uint8_t abstract_file_ident[32];
196 struct grub_udf_long_ad root_icb;
197 struct grub_udf_regid domain_ident;
198 struct grub_udf_long_ad next_ext;
199 struct grub_udf_long_ad streamdir_icb;
202 struct grub_udf_icbtag
204 grub_uint32_t prior_recorded_num_direct_entries;
205 grub_uint16_t strategy_type;
206 grub_uint16_t strategy_parameter;
207 grub_uint16_t num_entries;
208 grub_uint8_t reserved;
209 grub_uint8_t file_type;
210 struct grub_udf_lb_addr parent_idb;
214 struct grub_udf_file_ident
216 struct grub_udf_tag tag;
217 grub_uint16_t version_num;
218 grub_uint8_t characteristics;
219 #define MAX_FILE_IDENT_LENGTH 256
220 grub_uint8_t file_ident_length;
221 struct grub_udf_long_ad icb;
222 grub_uint16_t imp_use_length;
225 struct grub_udf_file_entry
227 struct grub_udf_tag tag;
228 struct grub_udf_icbtag icbtag;
231 grub_uint32_t permissions;
232 grub_uint16_t link_count;
233 grub_uint8_t record_format;
234 grub_uint8_t record_display_attr;
235 grub_uint32_t record_length;
236 grub_uint64_t file_size;
237 grub_uint64_t blocks_recorded;
238 struct grub_udf_timestamp access_time;
239 struct grub_udf_timestamp modification_time;
240 struct grub_udf_timestamp attr_time;
241 grub_uint32_t checkpoint;
242 struct grub_udf_long_ad extended_attr_idb;
243 struct grub_udf_regid imp_ident;
244 grub_uint64_t unique_id;
245 grub_uint32_t ext_attr_length;
246 grub_uint32_t alloc_descs_length;
247 grub_uint8_t ext_attr[0];
250 struct grub_udf_extended_file_entry
252 struct grub_udf_tag tag;
253 struct grub_udf_icbtag icbtag;
256 grub_uint32_t permissions;
257 grub_uint16_t link_count;
258 grub_uint8_t record_format;
259 grub_uint8_t record_display_attr;
260 grub_uint32_t record_length;
261 grub_uint64_t file_size;
262 grub_uint64_t object_size;
263 grub_uint64_t blocks_recorded;
264 struct grub_udf_timestamp access_time;
265 struct grub_udf_timestamp modification_time;
266 struct grub_udf_timestamp create_time;
267 struct grub_udf_timestamp attr_time;
268 grub_uint32_t checkpoint;
269 grub_uint32_t reserved;
270 struct grub_udf_long_ad extended_attr_icb;
271 struct grub_udf_long_ad streamdir_icb;
272 struct grub_udf_regid imp_ident;
273 grub_uint64_t unique_id;
274 grub_uint32_t ext_attr_length;
275 grub_uint32_t alloc_descs_length;
276 grub_uint8_t ext_attr[0];
282 grub_uint8_t magic[5];
283 grub_uint8_t version;
288 struct grub_udf_tag tag;
289 struct grub_udf_extent_ad vds;
294 struct grub_udf_tag tag;
295 grub_uint32_t seq_num;
297 grub_uint16_t part_num;
298 struct grub_udf_regid contents;
299 grub_uint8_t contents_use[128];
300 grub_uint32_t access_type;
302 grub_uint32_t length;
305 struct grub_udf_partmap
313 grub_uint16_t seq_num;
314 grub_uint16_t part_num;
319 grub_uint8_t ident[62];
326 struct grub_udf_tag tag;
327 grub_uint32_t seq_num;
328 grub_uint32_t pvd_num;
329 grub_uint8_t ident[32];
330 grub_uint16_t vol_seq_num;
331 grub_uint16_t max_vol_seq_num;
332 grub_uint16_t interchange_level;
333 grub_uint16_t max_interchange_level;
334 grub_uint32_t charset_list;
335 grub_uint32_t max_charset_list;
336 grub_uint8_t volset_ident[128];
337 struct grub_udf_charspec desc_charset;
338 struct grub_udf_charspec expl_charset;
339 struct grub_udf_extent_ad vol_abstract;
340 struct grub_udf_extent_ad vol_copyright;
341 struct grub_udf_regid app_ident;
342 struct grub_udf_timestamp recording_time;
343 struct grub_udf_regid imp_ident;
344 grub_uint8_t imp_use[64];
345 grub_uint32_t pred_vds_loc;
347 grub_uint8_t reserved[22];
352 struct grub_udf_tag tag;
353 grub_uint32_t seq_num;
354 struct grub_udf_charspec charset;
355 grub_uint8_t ident[128];
357 struct grub_udf_regid domain_ident;
358 struct grub_udf_long_ad root_fileset;
359 grub_uint32_t map_table_length;
360 grub_uint32_t num_part_maps;
361 struct grub_udf_regid imp_ident;
362 grub_uint8_t imp_use[128];
363 struct grub_udf_extent_ad integrity_seq_ext;
364 grub_uint8_t part_maps[1608];
369 struct grub_udf_tag tag;
370 grub_uint32_t prev_ae;
371 grub_uint32_t ae_len;
377 struct grub_udf_pvd pvd;
378 struct grub_udf_lvd lvd;
379 struct grub_udf_pd pds[GRUB_UDF_MAX_PDS];
380 struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS];
381 struct grub_udf_long_ad root_icb;
382 int npd, npm, lbshift;
385 struct grub_fshelp_node
387 struct grub_udf_data *data;
391 struct grub_udf_file_entry fe;
392 struct grub_udf_extended_file_entry efe;
397 static inline grub_size_t
398 get_fshelp_size (struct grub_udf_data *data)
400 struct grub_fshelp_node *x = NULL;
402 + (1 << (GRUB_DISK_SECTOR_BITS
403 + data->lbshift)) - sizeof (x->block);
406 static grub_dl_t my_mod;
409 grub_udf_get_block (struct grub_udf_data *data,
410 grub_uint16_t part_ref, grub_uint32_t block)
412 part_ref = U16 (part_ref);
414 if (part_ref >= data->npm)
416 grub_error (GRUB_ERR_BAD_FS, "invalid part ref");
420 return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start)
425 grub_udf_read_icb (struct grub_udf_data *data,
426 struct grub_udf_long_ad *icb,
427 struct grub_fshelp_node *node)
431 block = grub_udf_get_block (data,
433 icb->block.block_num);
438 if (grub_disk_read (data->disk, block << data->lbshift, 0,
439 1 << (GRUB_DISK_SECTOR_BITS
444 if ((U16 (node->block.fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FE) &&
445 (U16 (node->block.fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_EFE))
446 return grub_error (GRUB_ERR_BAD_FS, "invalid fe/efe descriptor");
448 node->part_ref = icb->block.part_ref;
453 static grub_disk_addr_t
454 grub_udf_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
459 grub_disk_addr_t filebytes;
461 switch (U16 (node->block.fe.tag.tag_ident))
463 case GRUB_UDF_TAG_IDENT_FE:
464 ptr = (char *) &node->block.fe.ext_attr[0] + U32 (node->block.fe.ext_attr_length);
465 len = U32 (node->block.fe.alloc_descs_length);
468 case GRUB_UDF_TAG_IDENT_EFE:
469 ptr = (char *) &node->block.efe.ext_attr[0] + U32 (node->block.efe.ext_attr_length);
470 len = U32 (node->block.efe.alloc_descs_length);
474 grub_error (GRUB_ERR_BAD_FS, "invalid file entry");
478 if ((U16 (node->block.fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
479 == GRUB_UDF_ICBTAG_FLAG_AD_SHORT)
481 struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr;
483 filebytes = fileblock * U32 (node->data->lvd.bsize);
484 while (len >= (grub_ssize_t) sizeof (struct grub_udf_short_ad))
486 grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
487 grub_uint32_t adtype = U32 (ad->length) >> 30;
490 struct grub_udf_aed *extension;
491 grub_disk_addr_t sec = grub_udf_get_block(node->data,
496 buf = grub_malloc (U32 (node->data->lvd.bsize));
500 if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
504 extension = (struct grub_udf_aed *) buf;
505 if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
507 grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
511 len = U32 (extension->ae_len);
512 ad = (struct grub_udf_short_ad *)
513 (buf + sizeof (struct grub_udf_aed));
517 if (filebytes < adlen)
519 grub_uint32_t ad_pos = ad->position;
521 return ((U32 (ad_pos) & GRUB_UDF_EXT_MASK) ? 0 :
522 (grub_udf_get_block (node->data, node->part_ref, ad_pos)
523 + (filebytes >> (GRUB_DISK_SECTOR_BITS
524 + node->data->lbshift))));
529 len -= sizeof (struct grub_udf_short_ad);
534 struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr;
536 filebytes = fileblock * U32 (node->data->lvd.bsize);
537 while (len >= (grub_ssize_t) sizeof (struct grub_udf_long_ad))
539 grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
540 grub_uint32_t adtype = U32 (ad->length) >> 30;
543 struct grub_udf_aed *extension;
544 grub_disk_addr_t sec = grub_udf_get_block(node->data,
546 ad->block.block_num);
549 buf = grub_malloc (U32 (node->data->lvd.bsize));
553 if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
557 extension = (struct grub_udf_aed *) buf;
558 if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
560 grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
564 len = U32 (extension->ae_len);
565 ad = (struct grub_udf_long_ad *)
566 (buf + sizeof (struct grub_udf_aed));
570 if (filebytes < adlen)
572 grub_uint32_t ad_block_num = ad->block.block_num;
573 grub_uint32_t ad_part_ref = ad->block.part_ref;
575 return ((U32 (ad_block_num) & GRUB_UDF_EXT_MASK) ? 0 :
576 (grub_udf_get_block (node->data, ad_part_ref,
578 + (filebytes >> (GRUB_DISK_SECTOR_BITS
579 + node->data->lbshift))));
584 len -= sizeof (struct grub_udf_long_ad);
595 grub_udf_read_file (grub_fshelp_node_t node,
596 grub_disk_read_hook_t read_hook, void *read_hook_data,
597 grub_off_t pos, grub_size_t len, char *buf)
599 switch (U16 (node->block.fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
601 case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB:
605 ptr = ((U16 (node->block.fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ?
606 ((char *) &node->block.fe.ext_attr[0]
607 + U32 (node->block.fe.ext_attr_length)) :
608 ((char *) &node->block.efe.ext_attr[0]
609 + U32 (node->block.efe.ext_attr_length)));
611 grub_memcpy (buf, ptr + pos, len);
616 case GRUB_UDF_ICBTAG_FLAG_AD_EXT:
617 grub_error (GRUB_ERR_BAD_FS, "invalid extent type");
621 return grub_fshelp_read_file (node->data->disk, node,
622 read_hook, read_hook_data,
623 pos, len, buf, grub_udf_read_block,
624 U64 (node->block.fe.file_size),
625 node->data->lbshift, 0);
628 static unsigned sblocklist[] = { 256, 512, 0 };
630 static struct grub_udf_data *
631 grub_udf_mount (grub_disk_t disk)
633 struct grub_udf_data *data = 0;
634 struct grub_udf_fileset root_fs;
636 grub_uint32_t block, vblock;
639 data = grub_malloc (sizeof (struct grub_udf_data));
645 /* Search for Anchor Volume Descriptor Pointer (AVDP)
646 * and determine logical block size. */
648 for (lbshift = 0; lbshift < 4; lbshift++)
650 for (sblklist = sblocklist; *sblklist; sblklist++)
652 struct grub_udf_avdp avdp;
654 if (grub_disk_read (disk, *sblklist << lbshift, 0,
655 sizeof (struct grub_udf_avdp), &avdp))
657 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
661 if (U16 (avdp.tag.tag_ident) == GRUB_UDF_TAG_IDENT_AVDP &&
662 U32 (avdp.tag.tag_location) == *sblklist)
664 block = U32 (avdp.vds.start);
675 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
678 data->lbshift = lbshift;
680 /* Search for Volume Recognition Sequence (VRS). */
681 for (vblock = (32767 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1;;
682 vblock += (2047 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1)
684 struct grub_udf_vrs vrs;
686 if (grub_disk_read (disk, vblock << lbshift, 0,
687 sizeof (struct grub_udf_vrs), &vrs))
689 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
693 if ((!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR03, 5)) ||
694 (!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR02, 5)))
697 if ((grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BEA01, 5)) &&
698 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BOOT2, 5)) &&
699 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CD001, 5)) &&
700 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CDW02, 5)) &&
701 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_TEA01, 5)))
703 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
708 data->npd = data->npm = 0;
709 /* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */
712 struct grub_udf_tag tag;
714 if (grub_disk_read (disk, block << lbshift, 0,
715 sizeof (struct grub_udf_tag), &tag))
717 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
721 tag.tag_ident = U16 (tag.tag_ident);
722 if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PVD)
724 if (grub_disk_read (disk, block << lbshift, 0,
725 sizeof (struct grub_udf_pvd),
728 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
732 else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
734 if (data->npd >= GRUB_UDF_MAX_PDS)
736 grub_error (GRUB_ERR_BAD_FS, "too many PDs");
740 if (grub_disk_read (disk, block << lbshift, 0,
741 sizeof (struct grub_udf_pd),
742 &data->pds[data->npd]))
744 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
750 else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_LVD)
754 struct grub_udf_partmap *ppm;
756 if (grub_disk_read (disk, block << lbshift, 0,
757 sizeof (struct grub_udf_lvd),
760 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
764 if (data->npm + U32 (data->lvd.num_part_maps) > GRUB_UDF_MAX_PMS)
766 grub_error (GRUB_ERR_BAD_FS, "too many partition maps");
770 ppm = (struct grub_udf_partmap *) &data->lvd.part_maps;
771 for (k = U32 (data->lvd.num_part_maps); k > 0; k--)
773 if (ppm->type != GRUB_UDF_PARTMAP_TYPE_1)
775 grub_error (GRUB_ERR_BAD_FS, "partmap type not supported");
779 data->pms[data->npm++] = ppm;
780 ppm = (struct grub_udf_partmap *) ((char *) ppm +
784 else if (tag.tag_ident > GRUB_UDF_TAG_IDENT_TD)
786 grub_error (GRUB_ERR_BAD_FS, "invalid tag ident");
789 else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_TD)
795 for (i = 0; i < data->npm; i++)
799 for (j = 0; j < data->npd; j++)
800 if (data->pms[i]->type1.part_num == data->pds[j].part_num)
802 data->pms[i]->type1.part_num = j;
808 grub_error (GRUB_ERR_BAD_FS, "can\'t find PD");
813 block = grub_udf_get_block (data,
814 data->lvd.root_fileset.block.part_ref,
815 data->lvd.root_fileset.block.block_num);
820 if (grub_disk_read (disk, block << lbshift, 0,
821 sizeof (struct grub_udf_fileset), &root_fs))
823 grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
827 if (U16 (root_fs.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FSD)
829 grub_error (GRUB_ERR_BAD_FS, "invalid fileset descriptor");
833 data->root_icb = root_fs.root_icb;
844 grub_udf_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
846 grub_disk_addr_t ret;
847 static struct grub_udf_data *data;
849 data = grub_udf_mount (disk);
853 ret = U32 (data->pds[data->pms[0]->type1.part_num].start);
854 *sec_per_lcn = 1ULL << data->lbshift;
861 read_string (const grub_uint8_t *raw, grub_size_t sz, char *outbuf)
863 grub_uint16_t *utf16 = NULL;
864 grub_size_t utf16len = 0;
869 if (raw[0] != 8 && raw[0] != 16)
876 utf16 = grub_malloc (utf16len * sizeof (utf16[0]));
879 for (i = 0; i < utf16len; i++)
880 utf16[i] = raw[i + 1];
885 utf16len = (sz - 1) / 2;
886 utf16 = grub_malloc (utf16len * sizeof (utf16[0]));
889 for (i = 0; i < utf16len; i++)
890 utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2];
893 outbuf = grub_malloc (utf16len * GRUB_MAX_UTF8_PER_UTF16 + 1);
895 *grub_utf16_to_utf8 ((grub_uint8_t *) outbuf, utf16, utf16len) = '\0';
901 read_dstring (const grub_uint8_t *raw, grub_size_t sz)
906 char *outbuf = grub_malloc (1);
916 return read_string (raw, len, NULL);
920 grub_udf_iterate_dir (grub_fshelp_node_t dir,
921 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
923 grub_fshelp_node_t child;
924 struct grub_udf_file_ident dirent;
925 grub_off_t offset = 0;
927 child = grub_malloc (get_fshelp_size (dir->data));
931 /* The current directory is not stored. */
932 grub_memcpy (child, dir, get_fshelp_size (dir->data));
934 if (hook (".", GRUB_FSHELP_DIR, child, hook_data))
937 while (offset < U64 (dir->block.fe.file_size))
939 if (grub_udf_read_file (dir, 0, 0, offset, sizeof (dirent),
940 (char *) &dirent) != sizeof (dirent))
943 if (U16 (dirent.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FID)
945 grub_error (GRUB_ERR_BAD_FS, "invalid fid tag");
949 offset += sizeof (dirent) + U16 (dirent.imp_use_length);
950 if (!(dirent.characteristics & GRUB_UDF_FID_CHAR_DELETED))
952 child = grub_malloc (get_fshelp_size (dir->data));
956 if (grub_udf_read_icb (dir->data, &dirent.icb, child))
959 if (dirent.characteristics & GRUB_UDF_FID_CHAR_PARENT)
961 /* This is the parent directory. */
962 if (hook ("..", GRUB_FSHELP_DIR, child, hook_data))
967 enum grub_fshelp_filetype type;
969 grub_uint8_t raw[MAX_FILE_IDENT_LENGTH];
971 type = ((dirent.characteristics & GRUB_UDF_FID_CHAR_DIRECTORY) ?
972 (GRUB_FSHELP_DIR) : (GRUB_FSHELP_REG));
973 if (child->block.fe.icbtag.file_type == GRUB_UDF_ICBTAG_TYPE_SYMLINK)
974 type = GRUB_FSHELP_SYMLINK;
976 if ((grub_udf_read_file (dir, 0, 0, offset,
977 dirent.file_ident_length,
979 != dirent.file_ident_length)
982 filename = read_string (raw, dirent.file_ident_length, 0);
986 if (filename && hook (filename, type, child, hook_data))
988 grub_free (filename);
991 grub_free (filename);
995 /* Align to dword boundary. */
996 offset = (offset + dirent.file_ident_length + 3) & (~3);
1003 grub_udf_read_symlink (grub_fshelp_node_t node)
1005 grub_size_t sz = U64 (node->block.fe.file_size);
1007 const grub_uint8_t *ptr;
1012 raw = grub_malloc (sz);
1015 if (grub_udf_read_file (node, NULL, NULL, 0, sz, (char *) raw) < 0)
1021 out = grub_malloc (sz * 2 + 1);
1030 for (ptr = raw; ptr < raw + sz; )
1033 if ((grub_size_t) (ptr - raw + 4) > sz)
1035 if (!(ptr[2] == 0 && ptr[3] == 0))
1038 if ((grub_size_t) (ptr - raw + s) > sz)
1047 /* in 4 bytes. out: 1 byte. */
1052 /* in 4 bytes. out: 3 bytes. */
1059 /* in 4 bytes. out: 2 bytes. */
1065 /* in 4 + n bytes. out, at most: 1 + 2 * n bytes. */
1068 if (!read_string (ptr + 4, s - 4, optr))
1070 optr += grub_strlen (optr);
1084 grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
1088 /* Context for grub_udf_dir. */
1089 struct grub_udf_dir_ctx
1091 grub_fs_dir_hook_t hook;
1095 /* Helper for grub_udf_dir. */
1097 grub_udf_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
1098 grub_fshelp_node_t node, void *data)
1100 struct grub_udf_dir_ctx *ctx = data;
1101 struct grub_dirhook_info info;
1102 const struct grub_udf_timestamp *tstamp = NULL;
1104 grub_memset (&info, 0, sizeof (info));
1105 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
1106 if (U16 (node->block.fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE)
1107 tstamp = &node->block.fe.modification_time;
1108 else if (U16 (node->block.fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_EFE)
1109 tstamp = &node->block.efe.modification_time;
1111 if (tstamp && (U16 (tstamp->type_and_timezone) & 0xf000) == 0x1000)
1114 struct grub_datetime datetime;
1116 datetime.year = U16 (tstamp->year);
1117 datetime.month = tstamp->month;
1118 datetime.day = tstamp->day;
1119 datetime.hour = tstamp->hour;
1120 datetime.minute = tstamp->minute;
1121 datetime.second = tstamp->second;
1123 tz = U16 (tstamp->type_and_timezone) & 0xfff;
1129 info.mtimeset = !!grub_datetime2unixtime (&datetime, &info.mtime);
1131 info.mtime -= 60 * tz;
1134 return ctx->hook (filename, &info, ctx->hook_data);
1138 grub_udf_dir (grub_device_t device, const char *path,
1139 grub_fs_dir_hook_t hook, void *hook_data)
1141 struct grub_udf_dir_ctx ctx = { hook, hook_data };
1142 struct grub_udf_data *data = 0;
1143 struct grub_fshelp_node *rootnode = 0;
1144 struct grub_fshelp_node *foundnode = 0;
1146 grub_dl_ref (my_mod);
1148 data = grub_udf_mount (device->disk);
1152 rootnode = grub_malloc (get_fshelp_size (data));
1156 if (grub_udf_read_icb (data, &data->root_icb, rootnode))
1159 if (grub_fshelp_find_file (path, rootnode,
1161 grub_udf_iterate_dir, grub_udf_read_symlink,
1165 grub_udf_iterate_dir (foundnode, grub_udf_dir_iter, &ctx);
1167 if (foundnode != rootnode)
1168 grub_free (foundnode);
1171 grub_free (rootnode);
1175 grub_dl_unref (my_mod);
1181 grub_udf_open (struct grub_file *file, const char *name)
1183 struct grub_udf_data *data;
1184 struct grub_fshelp_node *rootnode = 0;
1185 struct grub_fshelp_node *foundnode;
1187 grub_dl_ref (my_mod);
1189 data = grub_udf_mount (file->device->disk);
1193 rootnode = grub_malloc (get_fshelp_size (data));
1197 if (grub_udf_read_icb (data, &data->root_icb, rootnode))
1200 if (grub_fshelp_find_file (name, rootnode,
1202 grub_udf_iterate_dir, grub_udf_read_symlink,
1206 file->data = foundnode;
1208 file->size = U64 (foundnode->block.fe.file_size);
1210 grub_free (rootnode);
1215 grub_dl_unref (my_mod);
1218 grub_free (rootnode);
1224 grub_udf_read (grub_file_t file, char *buf, grub_size_t len)
1226 struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
1228 return grub_udf_read_file (node, file->read_hook, file->read_hook_data,
1229 file->offset, len, buf);
1233 grub_udf_close (grub_file_t file)
1237 struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
1239 grub_free (node->data);
1243 grub_dl_unref (my_mod);
1245 return GRUB_ERR_NONE;
1249 grub_udf_label (grub_device_t device, char **label)
1251 struct grub_udf_data *data;
1252 data = grub_udf_mount (device->disk);
1256 *label = read_dstring (data->lvd.ident, sizeof (data->lvd.ident));
1266 gen_uuid_from_volset (char *volset_ident)
1270 grub_size_t nonhexpos;
1271 grub_uint8_t buf[17];
1274 len = grub_strlen (volset_ident);
1278 uuid = grub_malloc (17);
1285 grub_memset (buf, 0, sizeof (buf));
1286 grub_memcpy (buf, volset_ident, len);
1289 for (i = 0; i < 16; ++i)
1291 if (!grub_isxdigit (buf[i]))
1300 grub_snprintf (uuid, 17, "%02x%02x%02x%02x%02x%02x%02x%02x",
1301 buf[0], buf[1], buf[2], buf[3],
1302 buf[4], buf[5], buf[6], buf[7]);
1304 else if (nonhexpos < 16)
1306 for (i = 0; i < 8; ++i)
1307 uuid[i] = grub_tolower (buf[i]);
1308 grub_snprintf (uuid+8, 9, "%02x%02x%02x%02x",
1309 buf[8], buf[9], buf[10], buf[11]);
1313 for (i = 0; i < 16; ++i)
1314 uuid[i] = grub_tolower (buf[i]);
1322 grub_udf_uuid (grub_device_t device, char **uuid)
1325 struct grub_udf_data *data;
1326 data = grub_udf_mount (device->disk);
1330 volset_ident = read_dstring (data->pvd.volset_ident, sizeof (data->pvd.volset_ident));
1333 *uuid = gen_uuid_from_volset (volset_ident);
1334 grub_free (volset_ident);
1346 static struct grub_fs grub_udf_fs = {
1348 .dir = grub_udf_dir,
1349 .open = grub_udf_open,
1350 .read = grub_udf_read,
1351 .close = grub_udf_close,
1352 .label = grub_udf_label,
1353 .uuid = grub_udf_uuid,
1355 .reserved_first_sector = 1,
1356 .blocklist_install = 1,
1363 grub_fs_register (&grub_udf_fs);
1369 grub_fs_unregister (&grub_udf_fs);