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>
30
#define GRUB_UDF_MAX_PDS 2
31
#define GRUB_UDF_MAX_PMS 6
33
#define U16 grub_le_to_cpu16
34
#define U32 grub_le_to_cpu32
35
#define U64 grub_le_to_cpu64
37
#define GRUB_UDF_TAG_IDENT_PVD 0x0001
38
#define GRUB_UDF_TAG_IDENT_AVDP 0x0002
39
#define GRUB_UDF_TAG_IDENT_VDP 0x0003
40
#define GRUB_UDF_TAG_IDENT_IUVD 0x0004
41
#define GRUB_UDF_TAG_IDENT_PD 0x0005
42
#define GRUB_UDF_TAG_IDENT_LVD 0x0006
43
#define GRUB_UDF_TAG_IDENT_USD 0x0007
44
#define GRUB_UDF_TAG_IDENT_TD 0x0008
45
#define GRUB_UDF_TAG_IDENT_LVID 0x0009
47
#define GRUB_UDF_TAG_IDENT_FSD 0x0100
48
#define GRUB_UDF_TAG_IDENT_FID 0x0101
49
#define GRUB_UDF_TAG_IDENT_AED 0x0102
50
#define GRUB_UDF_TAG_IDENT_IE 0x0103
51
#define GRUB_UDF_TAG_IDENT_TE 0x0104
52
#define GRUB_UDF_TAG_IDENT_FE 0x0105
53
#define GRUB_UDF_TAG_IDENT_EAHD 0x0106
54
#define GRUB_UDF_TAG_IDENT_USE 0x0107
55
#define GRUB_UDF_TAG_IDENT_SBD 0x0108
56
#define GRUB_UDF_TAG_IDENT_PIE 0x0109
57
#define GRUB_UDF_TAG_IDENT_EFE 0x010A
59
#define GRUB_UDF_ICBTAG_TYPE_UNDEF 0x00
60
#define GRUB_UDF_ICBTAG_TYPE_USE 0x01
61
#define GRUB_UDF_ICBTAG_TYPE_PIE 0x02
62
#define GRUB_UDF_ICBTAG_TYPE_IE 0x03
63
#define GRUB_UDF_ICBTAG_TYPE_DIRECTORY 0x04
64
#define GRUB_UDF_ICBTAG_TYPE_REGULAR 0x05
65
#define GRUB_UDF_ICBTAG_TYPE_BLOCK 0x06
66
#define GRUB_UDF_ICBTAG_TYPE_CHAR 0x07
67
#define GRUB_UDF_ICBTAG_TYPE_EA 0x08
68
#define GRUB_UDF_ICBTAG_TYPE_FIFO 0x09
69
#define GRUB_UDF_ICBTAG_TYPE_SOCKET 0x0A
70
#define GRUB_UDF_ICBTAG_TYPE_TE 0x0B
71
#define GRUB_UDF_ICBTAG_TYPE_SYMLINK 0x0C
72
#define GRUB_UDF_ICBTAG_TYPE_STREAMDIR 0x0D
74
#define GRUB_UDF_ICBTAG_FLAG_AD_MASK 0x0007
75
#define GRUB_UDF_ICBTAG_FLAG_AD_SHORT 0x0000
76
#define GRUB_UDF_ICBTAG_FLAG_AD_LONG 0x0001
77
#define GRUB_UDF_ICBTAG_FLAG_AD_EXT 0x0002
78
#define GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003
80
#define GRUB_UDF_EXT_NORMAL 0x00000000
81
#define GRUB_UDF_EXT_NREC_ALLOC 0x40000000
82
#define GRUB_UDF_EXT_NREC_NALLOC 0x80000000
83
#define GRUB_UDF_EXT_MASK 0xC0000000
85
#define GRUB_UDF_FID_CHAR_HIDDEN 0x01
86
#define GRUB_UDF_FID_CHAR_DIRECTORY 0x02
87
#define GRUB_UDF_FID_CHAR_DELETED 0x04
88
#define GRUB_UDF_FID_CHAR_PARENT 0x08
89
#define GRUB_UDF_FID_CHAR_METADATA 0x10
91
#define GRUB_UDF_STD_IDENT_BEA01 "BEA01"
92
#define GRUB_UDF_STD_IDENT_BOOT2 "BOOT2"
93
#define GRUB_UDF_STD_IDENT_CD001 "CD001"
94
#define GRUB_UDF_STD_IDENT_CDW02 "CDW02"
95
#define GRUB_UDF_STD_IDENT_NSR02 "NSR02"
96
#define GRUB_UDF_STD_IDENT_NSR03 "NSR03"
97
#define GRUB_UDF_STD_IDENT_TEA01 "TEA01"
99
#define GRUB_UDF_CHARSPEC_TYPE_CS0 0x00
100
#define GRUB_UDF_CHARSPEC_TYPE_CS1 0x01
101
#define GRUB_UDF_CHARSPEC_TYPE_CS2 0x02
102
#define GRUB_UDF_CHARSPEC_TYPE_CS3 0x03
103
#define GRUB_UDF_CHARSPEC_TYPE_CS4 0x04
104
#define GRUB_UDF_CHARSPEC_TYPE_CS5 0x05
105
#define GRUB_UDF_CHARSPEC_TYPE_CS6 0x06
106
#define GRUB_UDF_CHARSPEC_TYPE_CS7 0x07
107
#define GRUB_UDF_CHARSPEC_TYPE_CS8 0x08
109
#define GRUB_UDF_PARTMAP_TYPE_1 1
110
#define GRUB_UDF_PARTMAP_TYPE_2 2
112
struct grub_udf_lb_addr
114
grub_uint32_t block_num;
115
grub_uint16_t part_ref;
116
} __attribute__ ((packed));
118
struct grub_udf_short_ad
120
grub_uint32_t length;
121
grub_uint32_t position;
122
} __attribute__ ((packed));
124
struct grub_udf_long_ad
126
grub_uint32_t length;
127
struct grub_udf_lb_addr block;
128
grub_uint8_t imp_use[6];
129
} __attribute__ ((packed));
131
struct grub_udf_extent_ad
133
grub_uint32_t length;
135
} __attribute__ ((packed));
137
struct grub_udf_charspec
139
grub_uint8_t charset_type;
140
grub_uint8_t charset_info[63];
141
} __attribute__ ((packed));
143
struct grub_udf_timestamp
145
grub_uint16_t type_and_timezone;
152
grub_uint8_t centi_seconds;
153
grub_uint8_t hundreds_of_micro_seconds;
154
grub_uint8_t micro_seconds;
155
} __attribute__ ((packed));
157
struct grub_udf_regid
160
grub_uint8_t ident[23];
161
grub_uint8_t ident_suffix[8];
162
} __attribute__ ((packed));
166
grub_uint16_t tag_ident;
167
grub_uint16_t desc_version;
168
grub_uint8_t tag_checksum;
169
grub_uint8_t reserved;
170
grub_uint16_t tag_serial_number;
171
grub_uint16_t desc_crc;
172
grub_uint16_t desc_crc_length;
173
grub_uint32_t tag_location;
174
} __attribute__ ((packed));
176
struct grub_udf_fileset
178
struct grub_udf_tag tag;
179
struct grub_udf_timestamp datetime;
180
grub_uint16_t interchange_level;
181
grub_uint16_t max_interchange_level;
182
grub_uint32_t charset_list;
183
grub_uint32_t max_charset_list;
184
grub_uint32_t fileset_num;
185
grub_uint32_t fileset_desc_num;
186
struct grub_udf_charspec vol_charset;
187
grub_uint8_t vol_ident[128];
188
struct grub_udf_charspec fileset_charset;
189
grub_uint8_t fileset_ident[32];
190
grub_uint8_t copyright_file_ident[32];
191
grub_uint8_t abstract_file_ident[32];
192
struct grub_udf_long_ad root_icb;
193
struct grub_udf_regid domain_ident;
194
struct grub_udf_long_ad next_ext;
195
struct grub_udf_long_ad streamdir_icb;
196
} __attribute__ ((packed));
198
struct grub_udf_icbtag
200
grub_uint32_t prior_recorded_num_direct_entries;
201
grub_uint16_t strategy_type;
202
grub_uint16_t strategy_parameter;
203
grub_uint16_t num_entries;
204
grub_uint8_t reserved;
205
grub_uint8_t file_type;
206
struct grub_udf_lb_addr parent_idb;
208
} __attribute__ ((packed));
210
struct grub_udf_file_ident
212
struct grub_udf_tag tag;
213
grub_uint16_t version_num;
214
grub_uint8_t characteristics;
215
grub_uint8_t file_ident_length;
216
struct grub_udf_long_ad icb;
217
grub_uint16_t imp_use_length;
218
} __attribute__ ((packed));
220
struct grub_udf_file_entry
222
struct grub_udf_tag tag;
223
struct grub_udf_icbtag icbtag;
226
grub_uint32_t permissions;
227
grub_uint16_t link_count;
228
grub_uint8_t record_format;
229
grub_uint8_t record_display_attr;
230
grub_uint32_t record_length;
231
grub_uint64_t file_size;
232
grub_uint64_t blocks_recorded;
233
struct grub_udf_timestamp access_time;
234
struct grub_udf_timestamp modification_time;
235
struct grub_udf_timestamp attr_time;
236
grub_uint32_t checkpoint;
237
struct grub_udf_long_ad extended_attr_idb;
238
struct grub_udf_regid imp_ident;
239
grub_uint64_t unique_id;
240
grub_uint32_t ext_attr_length;
241
grub_uint32_t alloc_descs_length;
242
grub_uint8_t ext_attr[1872];
243
} __attribute__ ((packed));
245
struct grub_udf_extended_file_entry
247
struct grub_udf_tag tag;
248
struct grub_udf_icbtag icbtag;
251
grub_uint32_t permissions;
252
grub_uint16_t link_count;
253
grub_uint8_t record_format;
254
grub_uint8_t record_display_attr;
255
grub_uint32_t record_length;
256
grub_uint64_t file_size;
257
grub_uint64_t object_size;
258
grub_uint64_t blocks_recorded;
259
struct grub_udf_timestamp access_time;
260
struct grub_udf_timestamp modification_time;
261
struct grub_udf_timestamp create_time;
262
struct grub_udf_timestamp attr_time;
263
grub_uint32_t checkpoint;
264
grub_uint32_t reserved;
265
struct grub_udf_long_ad extended_attr_icb;
266
struct grub_udf_long_ad streamdir_icb;
267
struct grub_udf_regid imp_ident;
268
grub_uint64_t unique_id;
269
grub_uint32_t ext_attr_length;
270
grub_uint32_t alloc_descs_length;
271
grub_uint8_t ext_attr[1832];
272
} __attribute__ ((packed));
277
grub_uint8_t magic[5];
278
grub_uint8_t version;
279
} __attribute__ ((packed));
283
struct grub_udf_tag tag;
284
struct grub_udf_extent_ad vds;
285
} __attribute__ ((packed));
289
struct grub_udf_tag tag;
290
grub_uint32_t seq_num;
292
grub_uint16_t part_num;
293
struct grub_udf_regid contents;
294
grub_uint8_t contents_use[128];
295
grub_uint32_t access_type;
297
grub_uint32_t length;
298
} __attribute__ ((packed));
300
struct grub_udf_partmap
308
grub_uint16_t seq_num;
309
grub_uint16_t part_num;
314
grub_uint8_t ident[62];
321
struct grub_udf_tag tag;
322
grub_uint32_t seq_num;
323
struct grub_udf_charspec charset;
324
grub_uint8_t ident[128];
326
struct grub_udf_regid domain_ident;
327
struct grub_udf_long_ad root_fileset;
328
grub_uint32_t map_table_length;
329
grub_uint32_t num_part_maps;
330
struct grub_udf_regid imp_ident;
331
grub_uint8_t imp_use[128];
332
struct grub_udf_extent_ad integrity_seq_ext;
333
grub_uint8_t part_maps[1608];
334
} __attribute__ ((packed));
338
struct grub_udf_tag tag;
339
grub_uint32_t prev_ae;
340
grub_uint32_t ae_len;
341
} __attribute__ ((packed));
346
struct grub_udf_lvd lvd;
347
struct grub_udf_pd pds[GRUB_UDF_MAX_PDS];
348
struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS];
349
struct grub_udf_long_ad root_icb;
350
int npd, npm, lbshift;
353
struct grub_fshelp_node
355
struct grub_udf_data *data;
358
struct grub_udf_file_entry fe;
359
struct grub_udf_extended_file_entry efe;
364
static grub_dl_t my_mod;
367
grub_udf_get_block (struct grub_udf_data *data,
368
grub_uint16_t part_ref, grub_uint32_t block)
370
part_ref = U16 (part_ref);
372
if (part_ref >= data->npm)
374
grub_error (GRUB_ERR_BAD_FS, "invalid part ref");
378
return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start)
383
grub_udf_read_icb (struct grub_udf_data *data,
384
struct grub_udf_long_ad *icb,
385
struct grub_fshelp_node *node)
389
block = grub_udf_get_block (data,
391
icb->block.block_num);
396
if (grub_disk_read (data->disk, block << data->lbshift, 0,
397
sizeof (struct grub_udf_file_entry),
401
if ((U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FE) &&
402
(U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_EFE))
403
return grub_error (GRUB_ERR_BAD_FS, "invalid fe/efe descriptor");
405
node->part_ref = icb->block.part_ref;
410
static grub_disk_addr_t
411
grub_udf_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
416
grub_disk_addr_t filebytes;
418
switch (U16 (node->fe.tag.tag_ident))
420
case GRUB_UDF_TAG_IDENT_FE:
421
ptr = (char *) &node->fe.ext_attr[0] + U32 (node->fe.ext_attr_length);
422
len = U32 (node->fe.alloc_descs_length);
425
case GRUB_UDF_TAG_IDENT_EFE:
426
ptr = (char *) &node->efe.ext_attr[0] + U32 (node->efe.ext_attr_length);
427
len = U32 (node->efe.alloc_descs_length);
431
grub_error (GRUB_ERR_BAD_FS, "invalid file entry");
435
if ((U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
436
== GRUB_UDF_ICBTAG_FLAG_AD_SHORT)
438
struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr;
440
filebytes = fileblock * U32 (node->data->lvd.bsize);
441
while (len >= (grub_ssize_t) sizeof (struct grub_udf_short_ad))
443
grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
444
grub_uint32_t adtype = U32 (ad->length) >> 30;
447
struct grub_udf_aed *extension;
448
grub_disk_addr_t sec = grub_udf_get_block(node->data,
453
buf = grub_malloc (U32 (node->data->lvd.bsize));
457
if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
461
extension = (struct grub_udf_aed *) buf;
462
if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
464
grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
468
len = U32 (extension->ae_len);
469
ad = (struct grub_udf_short_ad *)
470
(buf + sizeof (struct grub_udf_aed));
474
if (filebytes < adlen)
476
grub_uint32_t ad_pos = ad->position;
478
return ((U32 (ad_pos) & GRUB_UDF_EXT_MASK) ? 0 :
479
(grub_udf_get_block (node->data, node->part_ref, ad_pos)
480
+ (filebytes >> (GRUB_DISK_SECTOR_BITS
481
+ node->data->lbshift))));
486
len -= sizeof (struct grub_udf_short_ad);
491
struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr;
493
filebytes = fileblock * U32 (node->data->lvd.bsize);
494
while (len >= (grub_ssize_t) sizeof (struct grub_udf_long_ad))
496
grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff;
497
grub_uint32_t adtype = U32 (ad->length) >> 30;
500
struct grub_udf_aed *extension;
501
grub_disk_addr_t sec = grub_udf_get_block(node->data,
503
ad->block.block_num);
506
buf = grub_malloc (U32 (node->data->lvd.bsize));
510
if (grub_disk_read (node->data->disk, sec << node->data->lbshift,
514
extension = (struct grub_udf_aed *) buf;
515
if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED)
517
grub_error (GRUB_ERR_BAD_FS, "invalid aed tag");
521
len = U32 (extension->ae_len);
522
ad = (struct grub_udf_long_ad *)
523
(buf + sizeof (struct grub_udf_aed));
527
if (filebytes < adlen)
529
grub_uint32_t ad_block_num = ad->block.block_num;
530
grub_uint32_t ad_part_ref = ad->block.part_ref;
532
return ((U32 (ad_block_num) & GRUB_UDF_EXT_MASK) ? 0 :
533
(grub_udf_get_block (node->data, ad_part_ref,
535
+ (filebytes >> (GRUB_DISK_SECTOR_BITS
536
+ node->data->lbshift))));
541
len -= sizeof (struct grub_udf_long_ad);
553
grub_udf_read_file (grub_fshelp_node_t node,
554
void NESTED_FUNC_ATTR
555
(*read_hook) (grub_disk_addr_t sector,
556
unsigned offset, unsigned length),
557
grub_off_t pos, grub_size_t len, char *buf)
559
switch (U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
561
case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB:
565
ptr = ((U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ?
566
((char *) &node->fe.ext_attr[0]
567
+ U32 (node->fe.ext_attr_length)) :
568
((char *) &node->efe.ext_attr[0]
569
+ U32 (node->efe.ext_attr_length)));
571
grub_memcpy (buf, ptr + pos, len);
576
case GRUB_UDF_ICBTAG_FLAG_AD_EXT:
577
grub_error (GRUB_ERR_BAD_FS, "invalid extent type");
581
return grub_fshelp_read_file (node->data->disk, node, read_hook,
582
pos, len, buf, grub_udf_read_block,
583
U64 (node->fe.file_size),
584
node->data->lbshift);
587
static unsigned sblocklist[] = { 256, 512, 0 };
589
static struct grub_udf_data *
590
grub_udf_mount (grub_disk_t disk)
592
struct grub_udf_data *data = 0;
593
struct grub_udf_fileset root_fs;
595
grub_uint32_t block, vblock;
598
data = grub_malloc (sizeof (struct grub_udf_data));
604
/* Search for Anchor Volume Descriptor Pointer (AVDP)
605
* and determine logical block size. */
607
for (lbshift = 0; lbshift < 4; lbshift++)
609
for (sblklist = sblocklist; *sblklist; sblklist++)
611
struct grub_udf_avdp avdp;
613
if (grub_disk_read (disk, *sblklist << lbshift, 0,
614
sizeof (struct grub_udf_avdp), &avdp))
616
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
620
if (U16 (avdp.tag.tag_ident) == GRUB_UDF_TAG_IDENT_AVDP &&
621
U32 (avdp.tag.tag_location) == *sblklist)
623
block = U32 (avdp.vds.start);
634
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
637
data->lbshift = lbshift;
639
/* Search for Volume Recognition Sequence (VRS). */
640
for (vblock = (32767 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1;;
641
vblock += (2047 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1)
643
struct grub_udf_vrs vrs;
645
if (grub_disk_read (disk, vblock << lbshift, 0,
646
sizeof (struct grub_udf_vrs), &vrs))
648
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
652
if ((!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR03, 5)) ||
653
(!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR02, 5)))
656
if ((grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BEA01, 5)) &&
657
(grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BOOT2, 5)) &&
658
(grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CD001, 5)) &&
659
(grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CDW02, 5)) &&
660
(grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_TEA01, 5)))
662
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
667
data->npd = data->npm = 0;
668
/* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */
671
struct grub_udf_tag tag;
673
if (grub_disk_read (disk, block << lbshift, 0,
674
sizeof (struct grub_udf_tag), &tag))
676
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
680
tag.tag_ident = U16 (tag.tag_ident);
681
if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
683
if (data->npd >= GRUB_UDF_MAX_PDS)
685
grub_error (GRUB_ERR_BAD_FS, "too many PDs");
689
if (grub_disk_read (disk, block << lbshift, 0,
690
sizeof (struct grub_udf_pd),
691
&data->pds[data->npd]))
693
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
699
else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_LVD)
703
struct grub_udf_partmap *ppm;
705
if (grub_disk_read (disk, block << lbshift, 0,
706
sizeof (struct grub_udf_lvd),
709
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
713
if (data->npm + U32 (data->lvd.num_part_maps) > GRUB_UDF_MAX_PMS)
715
grub_error (GRUB_ERR_BAD_FS, "too many partition maps");
719
ppm = (struct grub_udf_partmap *) &data->lvd.part_maps;
720
for (k = U32 (data->lvd.num_part_maps); k > 0; k--)
722
if (ppm->type != GRUB_UDF_PARTMAP_TYPE_1)
724
grub_error (GRUB_ERR_BAD_FS, "partmap type not supported");
728
data->pms[data->npm++] = ppm;
729
ppm = (struct grub_udf_partmap *) ((char *) ppm +
733
else if (tag.tag_ident > GRUB_UDF_TAG_IDENT_TD)
735
grub_error (GRUB_ERR_BAD_FS, "invalid tag ident");
738
else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_TD)
744
for (i = 0; i < data->npm; i++)
748
for (j = 0; j < data->npd; j++)
749
if (data->pms[i]->type1.part_num == data->pds[j].part_num)
751
data->pms[i]->type1.part_num = j;
757
grub_error (GRUB_ERR_BAD_FS, "can\'t find PD");
762
block = grub_udf_get_block (data,
763
data->lvd.root_fileset.block.part_ref,
764
data->lvd.root_fileset.block.block_num);
769
if (grub_disk_read (disk, block << lbshift, 0,
770
sizeof (struct grub_udf_fileset), &root_fs))
772
grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
776
if (U16 (root_fs.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FSD)
778
grub_error (GRUB_ERR_BAD_FS, "invalid fileset descriptor");
782
data->root_icb = root_fs.root_icb;
792
grub_udf_iterate_dir (grub_fshelp_node_t dir,
794
(*hook) (const char *filename,
795
enum grub_fshelp_filetype filetype,
796
grub_fshelp_node_t node))
798
grub_fshelp_node_t child;
799
struct grub_udf_file_ident dirent;
800
grub_off_t offset = 0;
802
child = grub_malloc (sizeof (struct grub_fshelp_node));
806
/* The current directory is not stored. */
807
grub_memcpy ((char *) child, (char *) dir,
808
sizeof (struct grub_fshelp_node));
810
if (hook (".", GRUB_FSHELP_DIR, child))
813
while (offset < U64 (dir->fe.file_size))
815
if (grub_udf_read_file (dir, 0, offset, sizeof (dirent),
816
(char *) &dirent) != sizeof (dirent))
819
if (U16 (dirent.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FID)
821
grub_error (GRUB_ERR_BAD_FS, "invalid fid tag");
825
offset += sizeof (dirent) + U16 (dirent.imp_use_length);
826
if (!(dirent.characteristics & GRUB_UDF_FID_CHAR_DELETED))
828
child = grub_malloc (sizeof (struct grub_fshelp_node));
832
if (grub_udf_read_icb (dir->data, &dirent.icb, child))
835
if (dirent.characteristics & GRUB_UDF_FID_CHAR_PARENT)
837
/* This is the parent directory. */
838
if (hook ("..", GRUB_FSHELP_DIR, child))
843
enum grub_fshelp_filetype type;
844
grub_uint8_t raw[dirent.file_ident_length];
845
grub_uint16_t utf16[dirent.file_ident_length - 1];
846
grub_uint8_t filename[dirent.file_ident_length * 2];
847
grub_size_t utf16len = 0;
849
type = ((dirent.characteristics & GRUB_UDF_FID_CHAR_DIRECTORY) ?
850
(GRUB_FSHELP_DIR) : (GRUB_FSHELP_REG));
852
if ((grub_udf_read_file (dir, 0, offset,
853
dirent.file_ident_length,
855
!= dirent.file_ident_length)
861
utf16len = dirent.file_ident_length - 1;
862
for (i = 0; i < utf16len; i++)
863
utf16[i] = raw[i + 1];
868
utf16len = (dirent.file_ident_length - 1) / 2;
869
for (i = 0; i < utf16len; i++)
870
utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2];
872
if (raw[0] == 8 || raw[0] == 16)
874
*grub_utf16_to_utf8 (filename, utf16, utf16len) = '\0';
876
if (hook ((char *) filename, type, child))
882
/* Align to dword boundary. */
883
offset = (offset + dirent.file_ident_length + 3) & (~3);
890
grub_udf_dir (grub_device_t device, const char *path,
891
int (*hook) (const char *filename,
892
const struct grub_dirhook_info *info))
894
struct grub_udf_data *data = 0;
895
struct grub_fshelp_node rootnode;
896
struct grub_fshelp_node *foundnode;
898
auto int NESTED_FUNC_ATTR iterate (const char *filename,
899
enum grub_fshelp_filetype filetype,
900
grub_fshelp_node_t node);
902
int NESTED_FUNC_ATTR iterate (const char *filename,
903
enum grub_fshelp_filetype filetype,
904
grub_fshelp_node_t node)
906
struct grub_dirhook_info info;
907
grub_memset (&info, 0, sizeof (info));
908
info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
910
return hook (filename, &info);
913
grub_dl_ref (my_mod);
915
data = grub_udf_mount (device->disk);
919
if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
922
if (grub_fshelp_find_file (path, &rootnode,
924
grub_udf_iterate_dir, 0, GRUB_FSHELP_DIR))
927
grub_udf_iterate_dir (foundnode, iterate);
929
if (foundnode != &rootnode)
930
grub_free (foundnode);
935
grub_dl_unref (my_mod);
941
grub_udf_open (struct grub_file *file, const char *name)
943
struct grub_udf_data *data;
944
struct grub_fshelp_node rootnode;
945
struct grub_fshelp_node *foundnode;
947
grub_dl_ref (my_mod);
949
data = grub_udf_mount (file->device->disk);
953
if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
956
if (grub_fshelp_find_file (name, &rootnode,
958
grub_udf_iterate_dir, 0, GRUB_FSHELP_REG))
961
file->data = foundnode;
963
file->size = U64 (foundnode->fe.file_size);
968
grub_dl_unref (my_mod);
976
grub_udf_read (grub_file_t file, char *buf, grub_size_t len)
978
struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
980
return grub_udf_read_file (node, file->read_hook, file->offset, len, buf);
984
grub_udf_close (grub_file_t file)
988
struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
990
grub_free (node->data);
994
grub_dl_unref (my_mod);
996
return GRUB_ERR_NONE;
1000
grub_udf_label (grub_device_t device, char **label)
1002
struct grub_udf_data *data;
1003
data = grub_udf_mount (device->disk);
1007
*label = grub_strdup ((char *) &data->lvd.ident[1]);
1016
static struct grub_fs grub_udf_fs = {
1018
.dir = grub_udf_dir,
1019
.open = grub_udf_open,
1020
.read = grub_udf_read,
1021
.close = grub_udf_close,
1022
.label = grub_udf_label,
1028
grub_fs_register (&grub_udf_fs);
1034
grub_fs_unregister (&grub_udf_fs);