malloc->zalloc
[grub2/phcoder.git] / fs / udf.c
blob9dfe431f6c0ca206d9b7011ef0bb8f00ee700fb1
1 /* udf.c - Universal Disk Format filesystem. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 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/>.
20 #include <grub/err.h>
21 #include <grub/file.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
25 #include <grub/dl.h>
26 #include <grub/types.h>
27 #include <grub/fshelp.h>
29 #define GRUB_UDF_MAX_PDS 2
30 #define GRUB_UDF_MAX_PMS 6
32 #define U16 grub_le_to_cpu16
33 #define U32 grub_le_to_cpu32
34 #define U64 grub_le_to_cpu64
36 #define GRUB_UDF_LOG2_BLKSZ 2
37 #define GRUB_UDF_BLKSZ 2048
39 #define GRUB_UDF_TAG_IDENT_PVD 0x0001
40 #define GRUB_UDF_TAG_IDENT_AVDP 0x0002
41 #define GRUB_UDF_TAG_IDENT_VDP 0x0003
42 #define GRUB_UDF_TAG_IDENT_IUVD 0x0004
43 #define GRUB_UDF_TAG_IDENT_PD 0x0005
44 #define GRUB_UDF_TAG_IDENT_LVD 0x0006
45 #define GRUB_UDF_TAG_IDENT_USD 0x0007
46 #define GRUB_UDF_TAG_IDENT_TD 0x0008
47 #define GRUB_UDF_TAG_IDENT_LVID 0x0009
49 #define GRUB_UDF_TAG_IDENT_FSD 0x0100
50 #define GRUB_UDF_TAG_IDENT_FID 0x0101
51 #define GRUB_UDF_TAG_IDENT_AED 0x0102
52 #define GRUB_UDF_TAG_IDENT_IE 0x0103
53 #define GRUB_UDF_TAG_IDENT_TE 0x0104
54 #define GRUB_UDF_TAG_IDENT_FE 0x0105
55 #define GRUB_UDF_TAG_IDENT_EAHD 0x0106
56 #define GRUB_UDF_TAG_IDENT_USE 0x0107
57 #define GRUB_UDF_TAG_IDENT_SBD 0x0108
58 #define GRUB_UDF_TAG_IDENT_PIE 0x0109
59 #define GRUB_UDF_TAG_IDENT_EFE 0x010A
61 #define GRUB_UDF_ICBTAG_TYPE_UNDEF 0x00
62 #define GRUB_UDF_ICBTAG_TYPE_USE 0x01
63 #define GRUB_UDF_ICBTAG_TYPE_PIE 0x02
64 #define GRUB_UDF_ICBTAG_TYPE_IE 0x03
65 #define GRUB_UDF_ICBTAG_TYPE_DIRECTORY 0x04
66 #define GRUB_UDF_ICBTAG_TYPE_REGULAR 0x05
67 #define GRUB_UDF_ICBTAG_TYPE_BLOCK 0x06
68 #define GRUB_UDF_ICBTAG_TYPE_CHAR 0x07
69 #define GRUB_UDF_ICBTAG_TYPE_EA 0x08
70 #define GRUB_UDF_ICBTAG_TYPE_FIFO 0x09
71 #define GRUB_UDF_ICBTAG_TYPE_SOCKET 0x0A
72 #define GRUB_UDF_ICBTAG_TYPE_TE 0x0B
73 #define GRUB_UDF_ICBTAG_TYPE_SYMLINK 0x0C
74 #define GRUB_UDF_ICBTAG_TYPE_STREAMDIR 0x0D
76 #define GRUB_UDF_ICBTAG_FLAG_AD_MASK 0x0007
77 #define GRUB_UDF_ICBTAG_FLAG_AD_SHORT 0x0000
78 #define GRUB_UDF_ICBTAG_FLAG_AD_LONG 0x0001
79 #define GRUB_UDF_ICBTAG_FLAG_AD_EXT 0x0002
80 #define GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003
82 #define GRUB_UDF_EXT_NORMAL 0x00000000
83 #define GRUB_UDF_EXT_NREC_ALLOC 0x40000000
84 #define GRUB_UDF_EXT_NREC_NALLOC 0x80000000
85 #define GRUB_UDF_EXT_MASK 0xC0000000
87 #define GRUB_UDF_FID_CHAR_HIDDEN 0x01
88 #define GRUB_UDF_FID_CHAR_DIRECTORY 0x02
89 #define GRUB_UDF_FID_CHAR_DELETED 0x04
90 #define GRUB_UDF_FID_CHAR_PARENT 0x08
91 #define GRUB_UDF_FID_CHAR_METADATA 0x10
93 #define GRUB_UDF_STD_IDENT_BEA01 "BEA01"
94 #define GRUB_UDF_STD_IDENT_BOOT2 "BOOT2"
95 #define GRUB_UDF_STD_IDENT_CD001 "CD001"
96 #define GRUB_UDF_STD_IDENT_CDW02 "CDW02"
97 #define GRUB_UDF_STD_IDENT_NSR02 "NSR02"
98 #define GRUB_UDF_STD_IDENT_NSR03 "NSR03"
99 #define GRUB_UDF_STD_IDENT_TEA01 "TEA01"
101 #define GRUB_UDF_CHARSPEC_TYPE_CS0 0x00
102 #define GRUB_UDF_CHARSPEC_TYPE_CS1 0x01
103 #define GRUB_UDF_CHARSPEC_TYPE_CS2 0x02
104 #define GRUB_UDF_CHARSPEC_TYPE_CS3 0x03
105 #define GRUB_UDF_CHARSPEC_TYPE_CS4 0x04
106 #define GRUB_UDF_CHARSPEC_TYPE_CS5 0x05
107 #define GRUB_UDF_CHARSPEC_TYPE_CS6 0x06
108 #define GRUB_UDF_CHARSPEC_TYPE_CS7 0x07
109 #define GRUB_UDF_CHARSPEC_TYPE_CS8 0x08
111 #define GRUB_UDF_PARTMAP_TYPE_1 1
112 #define GRUB_UDF_PARTMAP_TYPE_2 2
114 struct grub_udf_lb_addr
116 grub_uint32_t block_num;
117 grub_uint16_t part_ref;
118 } __attribute__ ((packed));
120 struct grub_udf_short_ad
122 grub_uint32_t length;
123 grub_uint32_t position;
124 } __attribute__ ((packed));
126 struct grub_udf_long_ad
128 grub_uint32_t length;
129 struct grub_udf_lb_addr block;
130 grub_uint8_t imp_use[6];
131 } __attribute__ ((packed));
133 struct grub_udf_extent_ad
135 grub_uint32_t length;
136 grub_uint32_t start;
137 } __attribute__ ((packed));
139 struct grub_udf_charspec
141 grub_uint8_t charset_type;
142 grub_uint8_t charset_info[63];
143 } __attribute__ ((packed));
145 struct grub_udf_timestamp
147 grub_uint16_t type_and_timezone;
148 grub_uint16_t year;
149 grub_uint8_t month;
150 grub_uint8_t day;
151 grub_uint8_t hour;
152 grub_uint8_t minute;
153 grub_uint8_t second;
154 grub_uint8_t centi_seconds;
155 grub_uint8_t hundreds_of_micro_seconds;
156 grub_uint8_t micro_seconds;
157 } __attribute__ ((packed));
159 struct grub_udf_regid
161 grub_uint8_t flags;
162 grub_uint8_t ident[23];
163 grub_uint8_t ident_suffix[8];
164 } __attribute__ ((packed));
166 struct grub_udf_tag
168 grub_uint16_t tag_ident;
169 grub_uint16_t desc_version;
170 grub_uint8_t tag_checksum;
171 grub_uint8_t reserved;
172 grub_uint16_t tag_serial_number;
173 grub_uint16_t desc_crc;
174 grub_uint16_t desc_crc_length;
175 grub_uint32_t tag_location;
176 } __attribute__ ((packed));
178 struct grub_udf_fileset
180 struct grub_udf_tag tag;
181 struct grub_udf_timestamp datetime;
182 grub_uint16_t interchange_level;
183 grub_uint16_t max_interchange_level;
184 grub_uint32_t charset_list;
185 grub_uint32_t max_charset_list;
186 grub_uint32_t fileset_num;
187 grub_uint32_t fileset_desc_num;
188 struct grub_udf_charspec vol_charset;
189 grub_uint8_t vol_ident[128];
190 struct grub_udf_charspec fileset_charset;
191 grub_uint8_t fileset_ident[32];
192 grub_uint8_t copyright_file_ident[32];
193 grub_uint8_t abstract_file_ident[32];
194 struct grub_udf_long_ad root_icb;
195 struct grub_udf_regid domain_ident;
196 struct grub_udf_long_ad next_ext;
197 struct grub_udf_long_ad streamdir_icb;
198 } __attribute__ ((packed));
200 struct grub_udf_icbtag
202 grub_uint32_t prior_recorded_num_direct_entries;
203 grub_uint16_t strategy_type;
204 grub_uint16_t strategy_parameter;
205 grub_uint16_t num_entries;
206 grub_uint8_t reserved;
207 grub_uint8_t file_type;
208 struct grub_udf_lb_addr parent_idb;
209 grub_uint16_t flags;
210 } __attribute__ ((packed));
212 struct grub_udf_file_ident
214 struct grub_udf_tag tag;
215 grub_uint16_t version_num;
216 grub_uint8_t characteristics;
217 grub_uint8_t file_ident_length;
218 struct grub_udf_long_ad icb;
219 grub_uint16_t imp_use_length;
220 } __attribute__ ((packed));
222 struct grub_udf_file_entry
224 struct grub_udf_tag tag;
225 struct grub_udf_icbtag icbtag;
226 grub_uint32_t uid;
227 grub_uint32_t gid;
228 grub_uint32_t permissions;
229 grub_uint16_t link_count;
230 grub_uint8_t record_format;
231 grub_uint8_t record_display_attr;
232 grub_uint32_t record_length;
233 grub_uint64_t file_size;
234 grub_uint64_t blocks_recorded;
235 struct grub_udf_timestamp access_time;
236 struct grub_udf_timestamp modification_time;
237 struct grub_udf_timestamp attr_time;
238 grub_uint32_t checkpoint;
239 struct grub_udf_long_ad extended_attr_idb;
240 struct grub_udf_regid imp_ident;
241 grub_uint64_t unique_id;
242 grub_uint32_t ext_attr_length;
243 grub_uint32_t alloc_descs_length;
244 grub_uint8_t ext_attr[1872];
245 } __attribute__ ((packed));
247 struct grub_udf_extended_file_entry
249 struct grub_udf_tag tag;
250 struct grub_udf_icbtag icbtag;
251 grub_uint32_t uid;
252 grub_uint32_t gid;
253 grub_uint32_t permissions;
254 grub_uint16_t link_count;
255 grub_uint8_t record_format;
256 grub_uint8_t record_display_attr;
257 grub_uint32_t record_length;
258 grub_uint64_t file_size;
259 grub_uint64_t object_size;
260 grub_uint64_t blocks_recorded;
261 struct grub_udf_timestamp access_time;
262 struct grub_udf_timestamp modification_time;
263 struct grub_udf_timestamp create_time;
264 struct grub_udf_timestamp attr_time;
265 grub_uint32_t checkpoint;
266 grub_uint32_t reserved;
267 struct grub_udf_long_ad extended_attr_icb;
268 struct grub_udf_long_ad streamdir_icb;
269 struct grub_udf_regid imp_ident;
270 grub_uint64_t unique_id;
271 grub_uint32_t ext_attr_length;
272 grub_uint32_t alloc_descs_length;
273 grub_uint8_t ext_attr[1832];
274 } __attribute__ ((packed));
276 struct grub_udf_vrs
278 grub_uint8_t type;
279 grub_uint8_t magic[5];
280 grub_uint8_t version;
281 } __attribute__ ((packed));
283 struct grub_udf_avdp
285 struct grub_udf_tag tag;
286 struct grub_udf_extent_ad vds;
287 } __attribute__ ((packed));
289 struct grub_udf_pd
291 struct grub_udf_tag tag;
292 grub_uint32_t seq_num;
293 grub_uint16_t flags;
294 grub_uint16_t part_num;
295 struct grub_udf_regid contents;
296 grub_uint8_t contents_use[128];
297 grub_uint32_t access_type;
298 grub_uint32_t start;
299 grub_uint32_t length;
300 } __attribute__ ((packed));
302 struct grub_udf_partmap
304 grub_uint8_t type;
305 grub_uint8_t length;
306 union
308 struct
310 grub_uint16_t seq_num;
311 grub_uint16_t part_num;
312 } type1;
314 struct
316 grub_uint8_t ident[62];
317 } type2;
321 struct grub_udf_lvd
323 struct grub_udf_tag tag;
324 grub_uint32_t seq_num;
325 struct grub_udf_charspec charset;
326 grub_uint8_t ident[128];
327 grub_uint32_t bsize;
328 struct grub_udf_regid domain_ident;
329 struct grub_udf_long_ad root_fileset;
330 grub_uint32_t map_table_length;
331 grub_uint32_t num_part_maps;
332 struct grub_udf_regid imp_ident;
333 grub_uint8_t imp_use[128];
334 struct grub_udf_extent_ad integrity_seq_ext;
335 grub_uint8_t part_maps[1608];
336 } __attribute__ ((packed));
338 struct grub_udf_data
340 grub_disk_t disk;
341 struct grub_udf_lvd lvd;
342 struct grub_udf_pd pds[GRUB_UDF_MAX_PDS];
343 struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS];
344 struct grub_udf_long_ad root_icb;
345 int npd, npm;
348 struct grub_fshelp_node
350 struct grub_udf_data *data;
351 union
353 struct grub_udf_file_entry fe;
354 struct grub_udf_extended_file_entry efe;
356 int part_ref;
359 static grub_dl_t my_mod;
361 static grub_uint32_t
362 grub_udf_get_block (struct grub_udf_data *data,
363 grub_uint16_t part_ref, grub_uint32_t block)
365 part_ref = U16 (part_ref);
367 if (part_ref >= data->npm)
369 grub_error (GRUB_ERR_BAD_FS, "invalid part ref");
370 return 0;
373 return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start)
374 + U32 (block));
377 static grub_err_t
378 grub_udf_read_icb (struct grub_udf_data *data,
379 struct grub_udf_long_ad *icb,
380 struct grub_fshelp_node *node)
382 grub_uint32_t block;
384 block = grub_udf_get_block (data,
385 icb->block.part_ref,
386 icb->block.block_num);
388 if (grub_errno)
389 return grub_errno;
391 if (grub_disk_read (data->disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
392 sizeof (struct grub_udf_file_entry),
393 &node->fe))
394 return grub_errno;
396 if ((U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FE) &&
397 (U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_EFE))
398 return grub_error (GRUB_ERR_BAD_FS, "invalid fe/efe descriptor");
400 node->part_ref = icb->block.part_ref;
401 node->data = data;
402 return 0;
405 static grub_disk_addr_t
406 grub_udf_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
408 char *ptr;
409 int len;
410 grub_disk_addr_t filebytes;
412 if (U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE)
414 ptr = (char *) &node->fe.ext_attr[0] + U32 (node->fe.ext_attr_length);
415 len = U32 (node->fe.alloc_descs_length);
417 else
419 ptr = (char *) &node->efe.ext_attr[0] + U32 (node->efe.ext_attr_length);
420 len = U32 (node->efe.alloc_descs_length);
423 if ((U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
424 == GRUB_UDF_ICBTAG_FLAG_AD_SHORT)
426 struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr;
428 len /= sizeof (struct grub_udf_short_ad);
429 filebytes = fileblock * GRUB_UDF_BLKSZ;
430 while (len > 0)
432 if (filebytes < U32 (ad->length))
433 return ((U32 (ad->position) & GRUB_UDF_EXT_MASK) ? 0 :
434 (grub_udf_get_block (node->data,
435 node->part_ref,
436 ad->position)
437 + (filebytes / GRUB_UDF_BLKSZ)));
439 filebytes -= U32 (ad->length);
440 ad++;
441 len--;
444 else
446 struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr;
448 len /= sizeof (struct grub_udf_long_ad);
449 filebytes = fileblock * GRUB_UDF_BLKSZ;
450 while (len > 0)
452 if (filebytes < U32 (ad->length))
453 return ((U32 (ad->block.block_num) & GRUB_UDF_EXT_MASK) ? 0 :
454 (grub_udf_get_block (node->data,
455 ad->block.part_ref,
456 ad->block.block_num)
457 + (filebytes / GRUB_UDF_BLKSZ)));
459 filebytes -= U32 (ad->length);
460 ad++;
461 len--;
465 return 0;
468 static grub_ssize_t
469 grub_udf_read_file (grub_fshelp_node_t node,
470 void NESTED_FUNC_ATTR
471 (*read_hook) (grub_disk_addr_t sector,
472 unsigned offset, unsigned length),
473 int pos, grub_size_t len, char *buf)
475 switch (U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
477 case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB:
479 char *ptr;
481 ptr = ((U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ?
482 ((char *) &node->fe.ext_attr[0]
483 + U32 (node->fe.ext_attr_length)) :
484 ((char *) &node->efe.ext_attr[0]
485 + U32 (node->efe.ext_attr_length)));
487 grub_memcpy (buf, ptr + pos, len);
489 return len;
492 case GRUB_UDF_ICBTAG_FLAG_AD_EXT:
493 grub_error (GRUB_ERR_BAD_FS, "invalid extent type");
494 return 0;
497 return grub_fshelp_read_file (node->data->disk, node, read_hook,
498 pos, len, buf, grub_udf_read_block,
499 U64 (node->fe.file_size),
500 GRUB_UDF_LOG2_BLKSZ);
503 static int sblocklist[] = { 256, 512, 0 };
505 static struct grub_udf_data *
506 grub_udf_mount (grub_disk_t disk)
508 struct grub_udf_data *data = 0;
509 struct grub_udf_fileset root_fs;
510 int *sblklist = sblocklist;
511 grub_uint32_t block;
512 int i;
514 data = grub_malloc (sizeof (struct grub_udf_data));
515 if (!data)
516 return 0;
518 data->disk = disk;
520 /* Search for Volume Recognition Sequence (VRS). */
521 for (block = 16;; block++)
523 struct grub_udf_vrs vrs;
525 if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
526 sizeof (struct grub_udf_vrs), &vrs))
528 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
529 goto fail;
532 if ((!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR03, 5)) ||
533 (!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR02, 5)))
534 break;
536 if ((grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BEA01, 5)) &&
537 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BOOT2, 5)) &&
538 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CD001, 5)) &&
539 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CDW02, 5)) &&
540 (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_TEA01, 5)))
542 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
543 goto fail;
547 /* Search for Anchor Volume Descriptor Pointer (AVDP). */
548 while (1)
550 struct grub_udf_avdp avdp;
552 if (grub_disk_read (disk, *sblklist << GRUB_UDF_LOG2_BLKSZ, 0,
553 sizeof (struct grub_udf_avdp), &avdp))
555 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
556 goto fail;
559 if (U16 (avdp.tag.tag_ident) == GRUB_UDF_TAG_IDENT_AVDP)
561 block = U32 (avdp.vds.start);
562 break;
565 sblklist++;
566 if (*sblklist == 0)
568 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
569 goto fail;
573 data->npd = data->npm = 0;
574 /* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */
575 while (1)
577 struct grub_udf_tag tag;
579 if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
580 sizeof (struct grub_udf_tag), &tag))
582 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
583 goto fail;
586 tag.tag_ident = U16 (tag.tag_ident);
587 if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
589 if (data->npd >= GRUB_UDF_MAX_PDS)
591 grub_error (GRUB_ERR_BAD_FS, "too many PDs");
592 goto fail;
595 if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
596 sizeof (struct grub_udf_pd),
597 &data->pds[data->npd]))
599 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
600 goto fail;
603 data->npd++;
605 else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_LVD)
607 int k;
609 struct grub_udf_partmap *ppm;
611 if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
612 sizeof (struct grub_udf_lvd),
613 &data->lvd))
615 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
616 goto fail;
619 if (data->npm + U32 (data->lvd.num_part_maps) > GRUB_UDF_MAX_PMS)
621 grub_error (GRUB_ERR_BAD_FS, "too many partition maps");
622 goto fail;
625 ppm = (struct grub_udf_partmap *) &data->lvd.part_maps;
626 for (k = U32 (data->lvd.num_part_maps); k > 0; k--)
628 if (ppm->type != GRUB_UDF_PARTMAP_TYPE_1)
630 grub_error (GRUB_ERR_BAD_FS, "partmap type not supported");
631 goto fail;
634 data->pms[data->npm++] = ppm;
635 ppm = (struct grub_udf_partmap *) ((char *) ppm +
636 U32 (ppm->length));
639 else if (tag.tag_ident > GRUB_UDF_TAG_IDENT_TD)
641 grub_error (GRUB_ERR_BAD_FS, "invalid tag ident");
642 goto fail;
644 else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_TD)
645 break;
647 block++;
650 for (i = 0; i < data->npm; i++)
652 int j;
654 for (j = 0; j < data->npd; j++)
655 if (data->pms[i]->type1.part_num == data->pds[j].part_num)
657 data->pms[i]->type1.part_num = j;
658 break;
661 if (j == data->npd)
663 grub_error (GRUB_ERR_BAD_FS, "can\'t find PD");
664 goto fail;
668 block = grub_udf_get_block (data,
669 data->lvd.root_fileset.block.part_ref,
670 data->lvd.root_fileset.block.block_num);
672 if (grub_errno)
673 goto fail;
675 if (grub_disk_read (disk, block << GRUB_UDF_LOG2_BLKSZ, 0,
676 sizeof (struct grub_udf_fileset), &root_fs))
678 grub_error (GRUB_ERR_BAD_FS, "not an udf filesystem");
679 goto fail;
682 if (U16 (root_fs.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FSD)
684 grub_error (GRUB_ERR_BAD_FS, "invalid fileset descriptor");
685 goto fail;
688 data->root_icb = root_fs.root_icb;
690 return data;
692 fail:
693 grub_free (data);
694 return 0;
697 static int
698 grub_udf_iterate_dir (grub_fshelp_node_t dir,
699 int NESTED_FUNC_ATTR
700 (*hook) (const char *filename,
701 enum grub_fshelp_filetype filetype,
702 grub_fshelp_node_t node))
704 grub_fshelp_node_t child;
705 struct grub_udf_file_ident dirent;
706 grub_uint32_t offset = 0;
708 child = grub_malloc (sizeof (struct grub_fshelp_node));
709 if (!child)
710 return 0;
712 /* The current directory is not stored. */
713 grub_memcpy ((char *) child, (char *) dir,
714 sizeof (struct grub_fshelp_node));
716 if (hook (".", GRUB_FSHELP_DIR, child))
717 return 1;
719 while (offset < U64 (dir->fe.file_size))
721 if (grub_udf_read_file (dir, 0, offset, sizeof (dirent),
722 (char *) &dirent) != sizeof (dirent))
723 return 0;
725 if (U16 (dirent.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FID)
727 grub_error (GRUB_ERR_BAD_FS, "invalid fid tag");
728 return 0;
731 child = grub_malloc (sizeof (struct grub_fshelp_node));
732 if (!child)
733 return 0;
735 if (grub_udf_read_icb (dir->data, &dirent.icb, child))
736 return 0;
738 offset += sizeof (dirent) + U16 (dirent.imp_use_length);
739 if (dirent.characteristics & GRUB_UDF_FID_CHAR_PARENT)
741 /* This is the parent directory. */
742 if (hook ("..", GRUB_FSHELP_DIR, child))
743 return 1;
745 else
747 enum grub_fshelp_filetype type;
748 char filename[dirent.file_ident_length + 1];
750 type = ((dirent.characteristics & GRUB_UDF_FID_CHAR_DIRECTORY) ?
751 (GRUB_FSHELP_DIR) : (GRUB_FSHELP_REG));
753 if ((grub_udf_read_file (dir, 0, offset,
754 dirent.file_ident_length, filename))
755 != dirent.file_ident_length)
756 return 0;
758 filename[dirent.file_ident_length] = 0;
759 if (hook (&filename[1], type, child))
760 return 1;
763 /* Align to dword boundary. */
764 offset = (offset + dirent.file_ident_length + 3) & (~3);
767 return 0;
770 static grub_err_t
771 grub_udf_dir (grub_device_t device, const char *path,
772 int (*hook) (const char *filename,
773 const struct grub_dirhook_info *info))
775 struct grub_udf_data *data = 0;
776 struct grub_fshelp_node rootnode;
777 struct grub_fshelp_node *foundnode;
779 auto int NESTED_FUNC_ATTR iterate (const char *filename,
780 enum grub_fshelp_filetype filetype,
781 grub_fshelp_node_t node);
783 int NESTED_FUNC_ATTR iterate (const char *filename,
784 enum grub_fshelp_filetype filetype,
785 grub_fshelp_node_t node)
787 struct grub_dirhook_info info;
788 grub_memset (&info, 0, sizeof (info));
789 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
790 grub_free (node);
791 return hook (filename, &info);
794 grub_dl_ref (my_mod);
796 data = grub_udf_mount (device->disk);
797 if (!data)
798 goto fail;
800 if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
801 goto fail;
803 if (grub_fshelp_find_file (path, &rootnode,
804 &foundnode,
805 grub_udf_iterate_dir, 0, GRUB_FSHELP_DIR))
806 goto fail;
808 grub_udf_iterate_dir (foundnode, iterate);
810 if (foundnode != &rootnode)
811 grub_free (foundnode);
813 fail:
814 grub_free (data);
816 grub_dl_unref (my_mod);
818 return grub_errno;
821 static grub_err_t
822 grub_udf_open (struct grub_file *file, const char *name)
824 struct grub_udf_data *data;
825 struct grub_fshelp_node rootnode;
826 struct grub_fshelp_node *foundnode;
828 grub_dl_ref (my_mod);
830 data = grub_udf_mount (file->device->disk);
831 if (!data)
832 goto fail;
834 if (grub_udf_read_icb (data, &data->root_icb, &rootnode))
835 goto fail;
837 if (grub_fshelp_find_file (name, &rootnode,
838 &foundnode,
839 grub_udf_iterate_dir, 0, GRUB_FSHELP_REG))
840 goto fail;
842 file->data = foundnode;
843 file->offset = 0;
844 file->size = U64 (foundnode->fe.file_size);
846 return 0;
848 fail:
849 grub_dl_unref (my_mod);
851 grub_free (data);
853 return grub_errno;
856 static grub_ssize_t
857 grub_udf_read (grub_file_t file, char *buf, grub_size_t len)
859 struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
861 return grub_udf_read_file (node, file->read_hook, file->offset, len, buf);
864 static grub_err_t
865 grub_udf_close (grub_file_t file)
867 if (file->data)
869 struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data;
871 grub_free (node->data);
872 grub_free (node);
875 grub_dl_unref (my_mod);
877 return GRUB_ERR_NONE;
880 static grub_err_t
881 grub_udf_label (grub_device_t device, char **label)
883 struct grub_udf_data *data;
884 data = grub_udf_mount (device->disk);
886 if (data)
888 *label = grub_strdup ((char *) &data->lvd.ident[1]);
889 grub_free (data);
891 else
892 *label = 0;
894 return grub_errno;
897 static struct grub_fs grub_udf_fs = {
898 .name = "udf",
899 .dir = grub_udf_dir,
900 .open = grub_udf_open,
901 .read = grub_udf_read,
902 .close = grub_udf_close,
903 .label = grub_udf_label,
904 .next = 0
907 GRUB_MOD_INIT (udf)
909 grub_fs_register (&grub_udf_fs);
910 my_mod = mod;
913 GRUB_MOD_FINI (udf)
915 grub_fs_unregister (&grub_udf_fs);