Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / fs / iso9660.c
blobcd4acc8ec1ce3537d22fc16fb8ae5c904cc3ec85
1 /* iso9660.c - iso9660 implementation with extensions:
2 SUSP, Rock Ridge. */
3 /*
4 * GRUB -- GRand Unified Bootloader
5 * Copyright (C) 2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
7 * GRUB is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GRUB is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/err.h>
22 #include <grub/file.h>
23 #include <grub/mm.h>
24 #include <grub/misc.h>
25 #include <grub/disk.h>
26 #include <grub/dl.h>
27 #include <grub/types.h>
28 #include <grub/fshelp.h>
29 #include <grub/charset.h>
30 #include <grub/datetime.h>
32 GRUB_MOD_LICENSE ("GPLv3+");
34 #define GRUB_ISO9660_FSTYPE_DIR 0040000
35 #define GRUB_ISO9660_FSTYPE_REG 0100000
36 #define GRUB_ISO9660_FSTYPE_SYMLINK 0120000
37 #define GRUB_ISO9660_FSTYPE_MASK 0170000
39 #define GRUB_ISO9660_LOG2_BLKSZ 2
40 #define GRUB_ISO9660_BLKSZ 2048
42 #define GRUB_ISO9660_RR_DOT 2
43 #define GRUB_ISO9660_RR_DOTDOT 4
45 #define GRUB_ISO9660_VOLDESC_BOOT 0
46 #define GRUB_ISO9660_VOLDESC_PRIMARY 1
47 #define GRUB_ISO9660_VOLDESC_SUPP 2
48 #define GRUB_ISO9660_VOLDESC_PART 3
49 #define GRUB_ISO9660_VOLDESC_END 255
51 /* The head of a volume descriptor. */
52 struct grub_iso9660_voldesc
54 grub_uint8_t type;
55 grub_uint8_t magic[5];
56 grub_uint8_t version;
57 } __attribute__ ((packed));
59 struct grub_iso9660_date2
61 grub_uint8_t year;
62 grub_uint8_t month;
63 grub_uint8_t day;
64 grub_uint8_t hour;
65 grub_uint8_t minute;
66 grub_uint8_t second;
67 grub_uint8_t offset;
68 } __attribute__ ((packed));
70 /* A directory entry. */
71 struct grub_iso9660_dir
73 grub_uint8_t len;
74 grub_uint8_t ext_sectors;
75 grub_uint32_t first_sector;
76 grub_uint32_t first_sector_be;
77 grub_uint32_t size;
78 grub_uint32_t size_be;
79 struct grub_iso9660_date2 mtime;
80 grub_uint8_t flags;
81 grub_uint8_t unused2[6];
82 grub_uint8_t namelen;
83 } __attribute__ ((packed));
85 struct grub_iso9660_date
87 grub_uint8_t year[4];
88 grub_uint8_t month[2];
89 grub_uint8_t day[2];
90 grub_uint8_t hour[2];
91 grub_uint8_t minute[2];
92 grub_uint8_t second[2];
93 grub_uint8_t hundredth[2];
94 grub_uint8_t offset;
95 } __attribute__ ((packed));
97 /* The primary volume descriptor. Only little endian is used. */
98 struct grub_iso9660_primary_voldesc
100 struct grub_iso9660_voldesc voldesc;
101 grub_uint8_t unused1[33];
102 grub_uint8_t volname[32];
103 grub_uint8_t unused2[16];
104 grub_uint8_t escape[32];
105 grub_uint8_t unused3[12];
106 grub_uint32_t path_table_size;
107 grub_uint8_t unused4[4];
108 grub_uint32_t path_table;
109 grub_uint8_t unused5[12];
110 struct grub_iso9660_dir rootdir;
111 grub_uint8_t unused6[624];
112 struct grub_iso9660_date created;
113 struct grub_iso9660_date modified;
114 } __attribute__ ((packed));
116 /* A single entry in the path table. */
117 struct grub_iso9660_path
119 grub_uint8_t len;
120 grub_uint8_t sectors;
121 grub_uint32_t first_sector;
122 grub_uint16_t parentdir;
123 grub_uint8_t name[0];
124 } __attribute__ ((packed));
126 /* An entry in the System Usage area of the directory entry. */
127 struct grub_iso9660_susp_entry
129 grub_uint8_t sig[2];
130 grub_uint8_t len;
131 grub_uint8_t version;
132 grub_uint8_t data[0];
133 } __attribute__ ((packed));
135 /* The CE entry. This is used to describe the next block where data
136 can be found. */
137 struct grub_iso9660_susp_ce
139 struct grub_iso9660_susp_entry entry;
140 grub_uint32_t blk;
141 grub_uint32_t blk_be;
142 grub_uint32_t off;
143 grub_uint32_t off_be;
144 grub_uint32_t len;
145 grub_uint32_t len_be;
146 } __attribute__ ((packed));
148 struct grub_iso9660_data
150 struct grub_iso9660_primary_voldesc voldesc;
151 grub_disk_t disk;
152 int rockridge;
153 int susp_skip;
154 int joliet;
155 struct grub_fshelp_node *node;
158 struct grub_fshelp_node
160 struct grub_iso9660_data *data;
161 grub_size_t have_dirents, alloc_dirents;
162 int have_symlink;
163 struct grub_iso9660_dir dirents[8];
164 char symlink[0];
167 enum
169 FLAG_TYPE_PLAIN = 0,
170 FLAG_TYPE_DIR = 2,
171 FLAG_TYPE = 3,
172 FLAG_MORE_EXTENTS = 0x80
175 static grub_dl_t my_mod;
178 static grub_err_t
179 iso9660_to_unixtime (const struct grub_iso9660_date *i, grub_int32_t *nix)
181 struct grub_datetime datetime;
183 if (! i->year[0] && ! i->year[1]
184 && ! i->year[2] && ! i->year[3]
185 && ! i->month[0] && ! i->month[1]
186 && ! i->day[0] && ! i->day[1]
187 && ! i->hour[0] && ! i->hour[1]
188 && ! i->minute[0] && ! i->minute[1]
189 && ! i->second[0] && ! i->second[1]
190 && ! i->hundredth[0] && ! i->hundredth[1])
191 return grub_error (GRUB_ERR_BAD_NUMBER, "empty date");
192 datetime.year = (i->year[0] - '0') * 1000 + (i->year[1] - '0') * 100
193 + (i->year[2] - '0') * 10 + (i->year[3] - '0');
194 datetime.month = (i->month[0] - '0') * 10 + (i->month[1] - '0');
195 datetime.day = (i->day[0] - '0') * 10 + (i->day[1] - '0');
196 datetime.hour = (i->hour[0] - '0') * 10 + (i->hour[1] - '0');
197 datetime.minute = (i->minute[0] - '0') * 10 + (i->minute[1] - '0');
198 datetime.second = (i->second[0] - '0') * 10 + (i->second[1] - '0');
200 if (!grub_datetime2unixtime (&datetime, nix))
201 return grub_error (GRUB_ERR_BAD_NUMBER, "incorrect date");
202 *nix -= i->offset * 60 * 15;
203 return GRUB_ERR_NONE;
206 static int
207 iso9660_to_unixtime2 (const struct grub_iso9660_date2 *i, grub_int32_t *nix)
209 struct grub_datetime datetime;
211 datetime.year = i->year + 1900;
212 datetime.month = i->month;
213 datetime.day = i->day;
214 datetime.hour = i->hour;
215 datetime.minute = i->minute;
216 datetime.second = i->second;
218 if (!grub_datetime2unixtime (&datetime, nix))
219 return 0;
220 *nix -= i->offset * 60 * 15;
221 return 1;
224 static grub_err_t
225 read_node (grub_fshelp_node_t node, grub_off_t off, grub_size_t len, char *buf)
227 grub_size_t i = 0;
229 while (len > 0)
231 grub_size_t toread;
232 grub_err_t err;
233 while (i < node->have_dirents
234 && off >= grub_le_to_cpu32 (node->dirents[i].size))
236 off -= grub_le_to_cpu32 (node->dirents[i].size);
237 i++;
239 if (i == node->have_dirents)
240 return grub_error (GRUB_ERR_OUT_OF_RANGE, "read out of range");
241 toread = grub_le_to_cpu32 (node->dirents[i].size);
242 if (toread > len)
243 toread = len;
244 err = grub_disk_read (node->data->disk,
245 ((grub_disk_addr_t) grub_le_to_cpu32 (node->dirents[i].first_sector)) << GRUB_ISO9660_LOG2_BLKSZ,
246 off, toread, buf);
247 if (err)
248 return err;
249 len -= toread;
250 off += toread;
251 buf += toread;
253 return GRUB_ERR_NONE;
256 /* Iterate over the susp entries, starting with block SUA_BLOCK on the
257 offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
258 every entry. */
259 static grub_err_t
260 grub_iso9660_susp_iterate (grub_fshelp_node_t node, grub_off_t off,
261 grub_ssize_t sua_size,
262 grub_err_t (*hook)
263 (struct grub_iso9660_susp_entry *entry))
265 char *sua;
266 struct grub_iso9660_susp_entry *entry;
267 grub_disk_addr_t ce_block;
268 int is_ce = 0;
270 auto grub_err_t load_sua (void);
272 /* Load a part of the System Usage Area. */
273 grub_err_t load_sua (void)
275 grub_err_t err;
276 sua = grub_malloc (sua_size);
277 if (!sua)
278 return grub_errno;
280 if (is_ce)
281 err = grub_disk_read (node->data->disk, ce_block, off,
282 sua_size, sua);
283 else
284 err = read_node (node, off, sua_size, sua);
285 if (err)
286 return err;
288 entry = (struct grub_iso9660_susp_entry *) sua;
289 return 0;
292 if (sua_size <= 0)
293 return GRUB_ERR_NONE;
295 if (load_sua ())
296 return grub_errno;
298 for (; (char *) entry < (char *) sua + sua_size - 1;
299 entry = (struct grub_iso9660_susp_entry *)
300 ((char *) entry + entry->len))
302 /* The last entry. */
303 if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0)
304 break;
306 /* Additional entries are stored elsewhere. */
307 if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0)
309 struct grub_iso9660_susp_ce *ce;
311 is_ce = 1;
312 ce = (struct grub_iso9660_susp_ce *) entry;
313 sua_size = grub_le_to_cpu32 (ce->len);
314 off = grub_le_to_cpu32 (ce->off);
315 ce_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
317 grub_free (sua);
318 if (load_sua ())
319 return grub_errno;
322 if (hook (entry))
324 grub_free (sua);
325 return 0;
329 grub_free (sua);
330 return 0;
333 static char *
334 grub_iso9660_convert_string (grub_uint8_t *us, int len)
336 char *p;
337 int i;
338 grub_uint16_t t[len];
340 p = grub_malloc (len * GRUB_MAX_UTF8_PER_UTF16 + 1);
341 if (! p)
342 return p;
344 for (i=0; i<len; i++)
345 t[i] = grub_be_to_cpu16 (grub_get_unaligned16 (us + 2 * i));
347 *grub_utf16_to_utf8 ((grub_uint8_t *) p, t, len) = '\0';
349 return p;
352 static grub_err_t
353 set_rockridge (struct grub_iso9660_data *data)
355 int sua_pos;
356 int sua_size;
357 char *sua;
358 struct grub_iso9660_dir rootdir;
359 struct grub_iso9660_susp_entry *entry;
361 auto grub_err_t susp_iterate (struct grub_iso9660_susp_entry *);
363 grub_err_t susp_iterate (struct grub_iso9660_susp_entry *susp_entry)
365 /* The "ER" entry is used to detect extensions. The
366 `IEEE_P1285' extension means Rock ridge. */
367 if (grub_strncmp ((char *) susp_entry->sig, "ER", 2) == 0)
369 data->rockridge = 1;
370 return 1;
372 return 0;
375 data->rockridge = 0;
377 /* Read the system use area and test it to see if SUSP is
378 supported. */
379 if (grub_disk_read (data->disk,
380 (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
381 << GRUB_ISO9660_LOG2_BLKSZ), 0,
382 sizeof (rootdir), (char *) &rootdir))
383 return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
385 sua_pos = (sizeof (rootdir) + rootdir.namelen
386 + (rootdir.namelen % 2) - 1);
387 sua_size = rootdir.len - sua_pos;
389 if (!sua_size)
390 return GRUB_ERR_NONE;
392 sua = grub_malloc (sua_size);
393 if (! sua)
394 return grub_errno;
396 if (grub_disk_read (data->disk,
397 (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
398 << GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
399 sua_size, sua))
401 grub_free (sua);
402 return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
405 entry = (struct grub_iso9660_susp_entry *) sua;
407 /* Test if the SUSP protocol is used on this filesystem. */
408 if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0)
410 struct grub_fshelp_node rootnode;
412 rootnode.data = data;
413 rootnode.alloc_dirents = ARRAY_SIZE (rootnode.dirents);
414 rootnode.have_dirents = 1;
415 rootnode.have_symlink = 0;
416 rootnode.dirents[0] = data->voldesc.rootdir;
418 /* The 2nd data byte stored how many bytes are skipped every time
419 to get to the SUA (System Usage Area). */
420 data->susp_skip = entry->data[2];
421 entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
423 /* Iterate over the entries in the SUA area to detect
424 extensions. */
425 if (grub_iso9660_susp_iterate (&rootnode,
426 sua_pos, sua_size, susp_iterate))
428 grub_free (sua);
429 return grub_errno;
432 grub_free (sua);
433 return GRUB_ERR_NONE;
436 static struct grub_iso9660_data *
437 grub_iso9660_mount (grub_disk_t disk)
439 struct grub_iso9660_data *data = 0;
440 struct grub_iso9660_primary_voldesc voldesc;
441 int block;
443 data = grub_zalloc (sizeof (struct grub_iso9660_data));
444 if (! data)
445 return 0;
447 data->disk = disk;
449 block = 16;
452 int copy_voldesc = 0;
454 /* Read the superblock. */
455 if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0,
456 sizeof (struct grub_iso9660_primary_voldesc),
457 (char *) &voldesc))
459 grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
460 goto fail;
463 if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0)
465 grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
466 goto fail;
469 if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY)
470 copy_voldesc = 1;
471 else if (!data->rockridge
472 && (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP)
473 && (voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f)
475 ((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */
476 (voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */
477 (voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */
479 copy_voldesc = 1;
480 data->joliet = 1;
483 if (copy_voldesc)
485 grub_memcpy((char *) &data->voldesc, (char *) &voldesc,
486 sizeof (struct grub_iso9660_primary_voldesc));
487 if (set_rockridge (data))
488 goto fail;
491 block++;
492 } while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END);
494 return data;
496 fail:
497 grub_free (data);
498 return 0;
502 static char *
503 grub_iso9660_read_symlink (grub_fshelp_node_t node)
505 return node->have_symlink
506 ? grub_strdup (node->symlink
507 + (node->have_dirents) * sizeof (node->dirents[0])
508 - sizeof (node->dirents)) : grub_strdup ("");
511 static grub_off_t
512 get_node_size (grub_fshelp_node_t node)
514 grub_off_t ret = 0;
515 grub_size_t i;
517 for (i = 0; i < node->have_dirents; i++)
518 ret += grub_le_to_cpu32 (node->dirents[i].size);
519 return ret;
522 static int
523 grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
524 int NESTED_FUNC_ATTR
525 (*hook) (const char *filename,
526 enum grub_fshelp_filetype filetype,
527 grub_fshelp_node_t node))
529 struct grub_iso9660_dir dirent;
530 grub_off_t offset = 0;
531 char *filename = 0;
532 int filename_alloc = 0;
533 enum grub_fshelp_filetype type;
534 grub_off_t len;
535 char *symlink = 0;
536 int was_continue = 0;
538 /* Extend the symlink. */
539 auto inline void __attribute__ ((always_inline)) add_part (const char *part,
540 int len2);
542 auto inline void __attribute__ ((always_inline)) add_part (const char *part,
543 int len2)
545 int size = symlink ? grub_strlen (symlink) : 0;
547 symlink = grub_realloc (symlink, size + len2 + 1);
548 if (! symlink)
549 return;
551 symlink[size] = 0;
552 grub_strncat (symlink, part, len2);
555 auto grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *);
557 grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *entry)
559 /* The filename in the rock ridge entry. */
560 if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0)
562 /* The flags are stored at the data position 0, here the
563 filename type is stored. */
564 /* FIXME: Fix this slightly improper cast. */
565 if (entry->data[0] & GRUB_ISO9660_RR_DOT)
566 filename = (char *) ".";
567 else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
568 filename = (char *) "..";
569 else if (entry->len >= 5)
571 grub_size_t size = 1, csize = 1;
572 char *old;
573 csize = size = entry->len - 5;
574 old = filename;
575 if (filename_alloc)
577 size += grub_strlen (filename);
578 filename = grub_realloc (filename, size + 1);
580 else
582 filename_alloc = 1;
583 filename = grub_zalloc (size + 1);
584 filename[0] = 0;
586 if (!filename)
588 filename = old;
589 return grub_errno;
591 filename_alloc = 1;
592 grub_strncat (filename, (char *) &entry->data[1], csize);
593 filename[size] = '\0';
596 /* The mode information (st_mode). */
597 else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0)
599 /* At position 0 of the PX record the st_mode information is
600 stored (little-endian). */
601 grub_uint32_t mode = ((entry->data[0] + (entry->data[1] << 8))
602 & GRUB_ISO9660_FSTYPE_MASK);
604 switch (mode)
606 case GRUB_ISO9660_FSTYPE_DIR:
607 type = GRUB_FSHELP_DIR;
608 break;
609 case GRUB_ISO9660_FSTYPE_REG:
610 type = GRUB_FSHELP_REG;
611 break;
612 case GRUB_ISO9660_FSTYPE_SYMLINK:
613 type = GRUB_FSHELP_SYMLINK;
614 break;
615 default:
616 type = GRUB_FSHELP_UNKNOWN;
619 else if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0)
621 unsigned int pos = 1;
623 /* The symlink is not stored as a POSIX symlink, translate it. */
624 while (pos + sizeof (*entry) < entry->len)
626 /* The current position is the `Component Flag'. */
627 switch (entry->data[pos] & 30)
629 case 0:
631 /* The data on pos + 2 is the actual data, pos + 1
632 is the length. Both are part of the `Component
633 Record'. */
634 if (symlink && !was_continue)
635 add_part ("/", 1);
636 add_part ((char *) &entry->data[pos + 2],
637 entry->data[pos + 1]);
638 was_continue = (entry->data[pos] & 1);
639 break;
642 case 2:
643 add_part ("./", 2);
644 break;
646 case 4:
647 add_part ("../", 3);
648 break;
650 case 8:
651 add_part ("/", 1);
652 break;
654 /* In pos + 1 the length of the `Component Record' is
655 stored. */
656 pos += entry->data[pos + 1] + 2;
659 /* Check if `grub_realloc' failed. */
660 if (grub_errno)
661 return grub_errno;
664 return 0;
667 len = get_node_size (dir);
669 for (; offset < len; offset += dirent.len)
671 symlink = 0;
672 was_continue = 0;
674 if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
675 return 0;
677 /* The end of the block, skip to the next one. */
678 if (!dirent.len)
680 offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
681 continue;
685 char name[dirent.namelen + 1];
686 int nameoffset = offset + sizeof (dirent);
687 struct grub_fshelp_node *node;
688 int sua_off = (sizeof (dirent) + dirent.namelen + 1
689 - (dirent.namelen % 2));
690 int sua_size = dirent.len - sua_off;
692 sua_off += offset + dir->data->susp_skip;
694 filename = 0;
695 filename_alloc = 0;
696 type = GRUB_FSHELP_UNKNOWN;
698 if (dir->data->rockridge
699 && grub_iso9660_susp_iterate (dir, sua_off, sua_size,
700 susp_iterate_dir))
701 return 0;
703 /* Read the name. */
704 if (read_node (dir, nameoffset, dirent.namelen, (char *) name))
705 return 0;
707 node = grub_malloc (sizeof (struct grub_fshelp_node));
708 if (!node)
709 return 0;
711 node->alloc_dirents = ARRAY_SIZE (node->dirents);
712 node->have_dirents = 1;
714 /* Setup a new node. */
715 node->data = dir->data;
716 node->have_symlink = 0;
718 /* If the filetype was not stored using rockridge, use
719 whatever is stored in the iso9660 filesystem. */
720 if (type == GRUB_FSHELP_UNKNOWN)
722 if ((dirent.flags & FLAG_TYPE) == FLAG_TYPE_DIR)
723 type = GRUB_FSHELP_DIR;
724 else
725 type = GRUB_FSHELP_REG;
728 /* . and .. */
729 if (!filename && dirent.namelen == 1 && name[0] == 0)
730 filename = (char *) ".";
732 if (!filename && dirent.namelen == 1 && name[0] == 1)
733 filename = (char *) "..";
735 /* The filename was not stored in a rock ridge entry. Read it
736 from the iso9660 filesystem. */
737 if (!dir->data->joliet && !filename)
739 char *ptr;
740 name[dirent.namelen] = '\0';
741 filename = grub_strrchr (name, ';');
742 if (filename)
743 *filename = '\0';
744 /* ISO9660 names are not case-preserving. */
745 type |= GRUB_FSHELP_CASE_INSENSITIVE;
746 for (ptr = name; *ptr; ptr++)
747 *ptr = grub_tolower (*ptr);
748 if (ptr != name && *(ptr - 1) == '.')
749 *(ptr - 1) = 0;
750 filename = name;
753 if (dir->data->joliet && !filename)
755 char *oldname, *semicolon;
757 oldname = name;
758 filename = grub_iso9660_convert_string
759 ((grub_uint8_t *) oldname, dirent.namelen >> 1);
761 semicolon = grub_strrchr (filename, ';');
762 if (semicolon)
763 *semicolon = '\0';
765 if (filename_alloc)
766 grub_free (oldname);
768 filename_alloc = 1;
771 node->dirents[0] = dirent;
772 while (dirent.flags & FLAG_MORE_EXTENTS)
774 offset += dirent.len;
775 if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
777 if (filename_alloc)
778 grub_free (filename);
779 grub_free (node);
780 return 0;
782 if (node->have_dirents >= node->alloc_dirents)
784 struct grub_fshelp_node *new_node;
785 node->alloc_dirents *= 2;
786 new_node = grub_realloc (node,
787 sizeof (struct grub_fshelp_node)
788 + ((node->alloc_dirents
789 - ARRAY_SIZE (node->dirents))
790 * sizeof (node->dirents[0])));
791 if (!new_node)
793 if (filename_alloc)
794 grub_free (filename);
795 grub_free (node);
796 return 0;
798 node = new_node;
800 node->dirents[node->have_dirents++] = dirent;
802 if (symlink)
804 if ((node->alloc_dirents - node->have_dirents)
805 * sizeof (node->dirents[0]) < grub_strlen (symlink) + 1)
807 struct grub_fshelp_node *new_node;
808 new_node = grub_realloc (node,
809 sizeof (struct grub_fshelp_node)
810 + ((node->alloc_dirents
811 - ARRAY_SIZE (node->dirents))
812 * sizeof (node->dirents[0]))
813 + grub_strlen (symlink) + 1);
814 if (!new_node)
816 if (filename_alloc)
817 grub_free (filename);
818 grub_free (node);
819 return 0;
821 node = new_node;
823 node->have_symlink = 1;
824 grub_strcpy (node->symlink
825 + node->have_dirents * sizeof (node->dirents[0])
826 - sizeof (node->dirents), symlink);
827 grub_free (symlink);
828 symlink = 0;
829 was_continue = 0;
831 if (hook (filename, type, node))
833 if (filename_alloc)
834 grub_free (filename);
835 return 1;
837 if (filename_alloc)
838 grub_free (filename);
842 return 0;
847 static grub_err_t
848 grub_iso9660_dir (grub_device_t device, const char *path,
849 int (*hook) (const char *filename,
850 const struct grub_dirhook_info *info))
852 struct grub_iso9660_data *data = 0;
853 struct grub_fshelp_node rootnode;
854 struct grub_fshelp_node *foundnode;
856 auto int NESTED_FUNC_ATTR iterate (const char *filename,
857 enum grub_fshelp_filetype filetype,
858 grub_fshelp_node_t node);
860 int NESTED_FUNC_ATTR iterate (const char *filename,
861 enum grub_fshelp_filetype filetype,
862 grub_fshelp_node_t node)
864 struct grub_dirhook_info info;
865 grub_memset (&info, 0, sizeof (info));
866 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
867 info.mtimeset = !!iso9660_to_unixtime2 (&node->dirents[0].mtime, &info.mtime);
869 grub_free (node);
870 return hook (filename, &info);
873 grub_dl_ref (my_mod);
875 data = grub_iso9660_mount (device->disk);
876 if (! data)
877 goto fail;
879 rootnode.data = data;
880 rootnode.alloc_dirents = 0;
881 rootnode.have_dirents = 1;
882 rootnode.have_symlink = 0;
883 rootnode.dirents[0] = data->voldesc.rootdir;
885 /* Use the fshelp function to traverse the path. */
886 if (grub_fshelp_find_file (path, &rootnode,
887 &foundnode,
888 grub_iso9660_iterate_dir,
889 grub_iso9660_read_symlink,
890 GRUB_FSHELP_DIR))
891 goto fail;
893 /* List the files in the directory. */
894 grub_iso9660_iterate_dir (foundnode, iterate);
896 if (foundnode != &rootnode)
897 grub_free (foundnode);
899 fail:
900 grub_free (data);
902 grub_dl_unref (my_mod);
904 return grub_errno;
908 /* Open a file named NAME and initialize FILE. */
909 static grub_err_t
910 grub_iso9660_open (struct grub_file *file, const char *name)
912 struct grub_iso9660_data *data;
913 struct grub_fshelp_node rootnode;
914 struct grub_fshelp_node *foundnode;
916 grub_dl_ref (my_mod);
918 data = grub_iso9660_mount (file->device->disk);
919 if (!data)
920 goto fail;
922 rootnode.data = data;
923 rootnode.alloc_dirents = 0;
924 rootnode.have_dirents = 1;
925 rootnode.have_symlink = 0;
926 rootnode.dirents[0] = data->voldesc.rootdir;
928 /* Use the fshelp function to traverse the path. */
929 if (grub_fshelp_find_file (name, &rootnode,
930 &foundnode,
931 grub_iso9660_iterate_dir,
932 grub_iso9660_read_symlink,
933 GRUB_FSHELP_REG))
934 goto fail;
936 data->node = foundnode;
937 file->data = data;
938 file->size = get_node_size (foundnode);
939 file->offset = 0;
941 return 0;
943 fail:
944 grub_dl_unref (my_mod);
946 grub_free (data);
948 return grub_errno;
952 static grub_ssize_t
953 grub_iso9660_read (grub_file_t file, char *buf, grub_size_t len)
955 struct grub_iso9660_data *data =
956 (struct grub_iso9660_data *) file->data;
958 /* XXX: The file is stored in as a single extent. */
959 data->disk->read_hook = file->read_hook;
960 read_node (data->node, file->offset, len, buf);
961 data->disk->read_hook = NULL;
963 if (grub_errno)
964 return -1;
966 return len;
970 static grub_err_t
971 grub_iso9660_close (grub_file_t file)
973 struct grub_iso9660_data *data =
974 (struct grub_iso9660_data *) file->data;
975 grub_free (data->node);
976 grub_free (data);
978 grub_dl_unref (my_mod);
980 return GRUB_ERR_NONE;
984 static grub_err_t
985 grub_iso9660_label (grub_device_t device, char **label)
987 struct grub_iso9660_data *data;
988 data = grub_iso9660_mount (device->disk);
990 if (data)
992 if (data->joliet)
993 *label = grub_iso9660_convert_string (data->voldesc.volname, 16);
994 else
995 *label = grub_strndup ((char *) data->voldesc.volname, 32);
996 if (*label)
998 char *ptr;
999 for (ptr = *label; *ptr;ptr++);
1000 ptr--;
1001 while (ptr >= *label && *ptr == ' ')
1002 *ptr-- = 0;
1005 grub_free (data);
1007 else
1008 *label = 0;
1010 return grub_errno;
1014 static grub_err_t
1015 grub_iso9660_uuid (grub_device_t device, char **uuid)
1017 struct grub_iso9660_data *data;
1018 grub_disk_t disk = device->disk;
1020 grub_dl_ref (my_mod);
1022 data = grub_iso9660_mount (disk);
1023 if (data)
1025 if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1]
1026 && ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3]
1027 && ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1]
1028 && ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1]
1029 && ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1]
1030 && ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1]
1031 && ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1]
1032 && ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1])
1034 grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID");
1035 *uuid = NULL;
1037 else
1039 *uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c",
1040 data->voldesc.modified.year[0],
1041 data->voldesc.modified.year[1],
1042 data->voldesc.modified.year[2],
1043 data->voldesc.modified.year[3],
1044 data->voldesc.modified.month[0],
1045 data->voldesc.modified.month[1],
1046 data->voldesc.modified.day[0],
1047 data->voldesc.modified.day[1],
1048 data->voldesc.modified.hour[0],
1049 data->voldesc.modified.hour[1],
1050 data->voldesc.modified.minute[0],
1051 data->voldesc.modified.minute[1],
1052 data->voldesc.modified.second[0],
1053 data->voldesc.modified.second[1],
1054 data->voldesc.modified.hundredth[0],
1055 data->voldesc.modified.hundredth[1]);
1058 else
1059 *uuid = NULL;
1061 grub_dl_unref (my_mod);
1063 grub_free (data);
1065 return grub_errno;
1068 /* Get writing time of filesystem. */
1069 static grub_err_t
1070 grub_iso9660_mtime (grub_device_t device, grub_int32_t *timebuf)
1072 struct grub_iso9660_data *data;
1073 grub_disk_t disk = device->disk;
1074 grub_err_t err;
1076 grub_dl_ref (my_mod);
1078 data = grub_iso9660_mount (disk);
1079 if (!data)
1081 grub_dl_unref (my_mod);
1082 return grub_errno;
1084 err = iso9660_to_unixtime (&data->voldesc.modified, timebuf);
1086 grub_dl_unref (my_mod);
1088 grub_free (data);
1090 return err;
1096 static struct grub_fs grub_iso9660_fs =
1098 .name = "iso9660",
1099 .dir = grub_iso9660_dir,
1100 .open = grub_iso9660_open,
1101 .read = grub_iso9660_read,
1102 .close = grub_iso9660_close,
1103 .label = grub_iso9660_label,
1104 .uuid = grub_iso9660_uuid,
1105 .mtime = grub_iso9660_mtime,
1106 #ifdef GRUB_UTIL
1107 .reserved_first_sector = 1,
1108 .blocklist_install = 1,
1109 #endif
1110 .next = 0
1113 GRUB_MOD_INIT(iso9660)
1115 grub_fs_register (&grub_iso9660_fs);
1116 my_mod = mod;
1119 GRUB_MOD_FINI(iso9660)
1121 grub_fs_unregister (&grub_iso9660_fs);