]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/efiffs/mod/grub/grub-core/fs/iso9660.c
1.1.07 release
[Ventoy.git] / EDK2 / efiffs / mod / grub / grub-core / fs / iso9660.c
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.
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
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>
31
32 GRUB_MOD_LICENSE ("GPLv3+");
33
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
38
39 #define GRUB_ISO9660_LOG2_BLKSZ 2
40 #define GRUB_ISO9660_BLKSZ 2048
41
42 #define GRUB_ISO9660_RR_DOT 2
43 #define GRUB_ISO9660_RR_DOTDOT 4
44
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
50
51 extern int g_fs_name_nocase;
52 /* The head of a volume descriptor. */
53 PRAGMA_BEGIN_PACKED
54 struct grub_iso9660_voldesc
55 {
56 grub_uint8_t type;
57 grub_uint8_t magic[5];
58 grub_uint8_t version;
59 } GRUB_PACKED;
60
61 struct grub_iso9660_date2
62 {
63 grub_uint8_t year;
64 grub_uint8_t month;
65 grub_uint8_t day;
66 grub_uint8_t hour;
67 grub_uint8_t minute;
68 grub_uint8_t second;
69 grub_uint8_t offset;
70 } GRUB_PACKED;
71
72 /* A directory entry. */
73 struct grub_iso9660_dir
74 {
75 grub_uint8_t len;
76 grub_uint8_t ext_sectors;
77 grub_uint32_t first_sector;
78 grub_uint32_t first_sector_be;
79 grub_uint32_t size;
80 grub_uint32_t size_be;
81 struct grub_iso9660_date2 mtime;
82 grub_uint8_t flags;
83 grub_uint8_t unused2[6];
84 #define MAX_NAMELEN 255
85 grub_uint8_t namelen;
86 } GRUB_PACKED;
87
88 struct grub_iso9660_date
89 {
90 grub_uint8_t year[4];
91 grub_uint8_t month[2];
92 grub_uint8_t day[2];
93 grub_uint8_t hour[2];
94 grub_uint8_t minute[2];
95 grub_uint8_t second[2];
96 grub_uint8_t hundredth[2];
97 grub_uint8_t offset;
98 } GRUB_PACKED;
99
100 /* The primary volume descriptor. Only little endian is used. */
101 struct grub_iso9660_primary_voldesc
102 {
103 struct grub_iso9660_voldesc voldesc;
104 grub_uint8_t unused1[33];
105 grub_uint8_t volname[32];
106 grub_uint8_t unused2[16];
107 grub_uint8_t escape[32];
108 grub_uint8_t unused3[12];
109 grub_uint32_t path_table_size;
110 grub_uint8_t unused4[4];
111 grub_uint32_t path_table;
112 grub_uint8_t unused5[12];
113 struct grub_iso9660_dir rootdir;
114 grub_uint8_t unused6[624];
115 struct grub_iso9660_date created;
116 struct grub_iso9660_date modified;
117 } GRUB_PACKED;
118
119 /* A single entry in the path table. */
120 struct grub_iso9660_path
121 {
122 grub_uint8_t len;
123 grub_uint8_t sectors;
124 grub_uint32_t first_sector;
125 grub_uint16_t parentdir;
126 grub_uint8_t name[0];
127 } GRUB_PACKED;
128
129 /* An entry in the System Usage area of the directory entry. */
130 struct grub_iso9660_susp_entry
131 {
132 grub_uint8_t sig[2];
133 grub_uint8_t len;
134 /*! MSVC compilers cannot handle a zero sized array in the middle
135 of a struct, and grub_iso9660_susp_entry is reused within
136 grub_iso9660_susp_ce. Therefore, instead of defining:
137 grub_uint8_t version;
138 grub_uint8_t data[];
139 we leverage the fact that these attributes are the same size
140 and use an union. The only gotcha is that the actual
141 payload of u.data[] starts at 1, not 0. */
142 union {
143 grub_uint8_t version;
144 grub_uint8_t data[1];
145 } u;
146 } GRUB_PACKED;
147
148 /* The CE entry. This is used to describe the next block where data
149 can be found. */
150 struct grub_iso9660_susp_ce
151 {
152 struct grub_iso9660_susp_entry entry;
153 grub_uint32_t blk;
154 grub_uint32_t blk_be;
155 grub_uint32_t off;
156 grub_uint32_t off_be;
157 grub_uint32_t len;
158 grub_uint32_t len_be;
159 } GRUB_PACKED;
160 PRAGMA_END_PACKED
161
162 struct grub_iso9660_data
163 {
164 struct grub_iso9660_primary_voldesc voldesc;
165 grub_disk_t disk;
166 int rockridge;
167 int susp_skip;
168 int joliet;
169 struct grub_fshelp_node *node;
170 };
171
172 struct grub_fshelp_node
173 {
174 struct grub_iso9660_data *data;
175 grub_size_t have_dirents, alloc_dirents;
176 int have_symlink;
177 struct grub_iso9660_dir dirents[8];
178 char symlink[0];
179 };
180
181 enum
182 {
183 FLAG_TYPE_PLAIN = 0,
184 FLAG_TYPE_DIR = 2,
185 FLAG_TYPE = 3,
186 FLAG_MORE_EXTENTS = 0x80
187 };
188
189 static grub_dl_t my_mod;
190 \f
191
192 static grub_err_t
193 iso9660_to_unixtime (const struct grub_iso9660_date *i, grub_int32_t *nix)
194 {
195 struct grub_datetime datetime;
196
197 if (! i->year[0] && ! i->year[1]
198 && ! i->year[2] && ! i->year[3]
199 && ! i->month[0] && ! i->month[1]
200 && ! i->day[0] && ! i->day[1]
201 && ! i->hour[0] && ! i->hour[1]
202 && ! i->minute[0] && ! i->minute[1]
203 && ! i->second[0] && ! i->second[1]
204 && ! i->hundredth[0] && ! i->hundredth[1])
205 return grub_error (GRUB_ERR_BAD_NUMBER, "empty date");
206 datetime.year = (i->year[0] - '0') * 1000 + (i->year[1] - '0') * 100
207 + (i->year[2] - '0') * 10 + (i->year[3] - '0');
208 datetime.month = (i->month[0] - '0') * 10 + (i->month[1] - '0');
209 datetime.day = (i->day[0] - '0') * 10 + (i->day[1] - '0');
210 datetime.hour = (i->hour[0] - '0') * 10 + (i->hour[1] - '0');
211 datetime.minute = (i->minute[0] - '0') * 10 + (i->minute[1] - '0');
212 datetime.second = (i->second[0] - '0') * 10 + (i->second[1] - '0');
213
214 if (!grub_datetime2unixtime (&datetime, nix))
215 return grub_error (GRUB_ERR_BAD_NUMBER, "incorrect date");
216 *nix -= i->offset * 60 * 15;
217 return GRUB_ERR_NONE;
218 }
219
220 static int
221 iso9660_to_unixtime2 (const struct grub_iso9660_date2 *i, grub_int32_t *nix)
222 {
223 struct grub_datetime datetime;
224
225 datetime.year = i->year + 1900;
226 datetime.month = i->month;
227 datetime.day = i->day;
228 datetime.hour = i->hour;
229 datetime.minute = i->minute;
230 datetime.second = i->second;
231
232 if (!grub_datetime2unixtime (&datetime, nix))
233 return 0;
234 *nix -= i->offset * 60 * 15;
235 return 1;
236 }
237
238 static grub_err_t
239 read_node (grub_fshelp_node_t node, grub_off_t off, grub_size_t len, char *buf)
240 {
241 grub_size_t i = 0;
242
243 while (len > 0)
244 {
245 grub_size_t toread;
246 grub_err_t err;
247 while (i < node->have_dirents
248 && off >= grub_le_to_cpu32 (node->dirents[i].size))
249 {
250 off -= grub_le_to_cpu32 (node->dirents[i].size);
251 i++;
252 }
253 if (i == node->have_dirents)
254 return grub_error (GRUB_ERR_OUT_OF_RANGE, "read out of range");
255 toread = grub_le_to_cpu32 (node->dirents[i].size);
256 if (toread > len)
257 toread = len;
258 err = grub_disk_read (node->data->disk,
259 ((grub_disk_addr_t) grub_le_to_cpu32 (node->dirents[i].first_sector)) << GRUB_ISO9660_LOG2_BLKSZ,
260 off, toread, buf);
261 if (err)
262 return err;
263 len -= toread;
264 off += toread;
265 buf += toread;
266 }
267 return GRUB_ERR_NONE;
268 }
269
270 /* Iterate over the susp entries, starting with block SUA_BLOCK on the
271 offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
272 every entry. */
273 static grub_err_t
274 grub_iso9660_susp_iterate (grub_fshelp_node_t node, grub_off_t off,
275 grub_ssize_t sua_size,
276 grub_err_t (*hook)
277 (struct grub_iso9660_susp_entry *entry, void *hook_arg),
278 void *hook_arg)
279 {
280 char *sua;
281 struct grub_iso9660_susp_entry *entry;
282 grub_err_t err;
283
284 if (sua_size <= 0)
285 return GRUB_ERR_NONE;
286
287 sua = grub_malloc (sua_size);
288 if (!sua)
289 return grub_errno;
290
291 /* Load a part of the System Usage Area. */
292 err = read_node (node, off, sua_size, sua);
293 if (err)
294 return err;
295
296 for (entry = (struct grub_iso9660_susp_entry *) sua; (char *) entry < (char *) sua + sua_size - 1 && entry->len > 0;
297 entry = (struct grub_iso9660_susp_entry *)
298 ((char *) entry + entry->len))
299 {
300 /* The last entry. */
301 if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0)
302 break;
303
304 /* Additional entries are stored elsewhere. */
305 if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0)
306 {
307 struct grub_iso9660_susp_ce *ce;
308 grub_disk_addr_t ce_block;
309
310 ce = (struct grub_iso9660_susp_ce *) entry;
311 sua_size = grub_le_to_cpu32 (ce->len);
312 off = grub_le_to_cpu32 (ce->off);
313 ce_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
314
315 grub_free (sua);
316 sua = grub_malloc (sua_size);
317 if (!sua)
318 return grub_errno;
319
320 /* Load a part of the System Usage Area. */
321 err = grub_disk_read (node->data->disk, ce_block, off,
322 sua_size, sua);
323 if (err)
324 return err;
325
326 entry = (struct grub_iso9660_susp_entry *) sua;
327 }
328
329 if (hook (entry, hook_arg))
330 {
331 grub_free (sua);
332 return 0;
333 }
334 }
335
336 grub_free (sua);
337 return 0;
338 }
339
340 static char *
341 grub_iso9660_convert_string (grub_uint8_t *us, int len)
342 {
343 char *p;
344 int i;
345 grub_uint16_t t[MAX_NAMELEN / 2 + 1];
346
347 p = grub_malloc (len * GRUB_MAX_UTF8_PER_UTF16 + 1);
348 if (! p)
349 return NULL;
350
351 for (i=0; i<len; i++)
352 t[i] = grub_be_to_cpu16 (grub_get_unaligned16 (us + 2 * i));
353
354 *grub_utf16_to_utf8 ((grub_uint8_t *) p, t, len) = '\0';
355
356 return p;
357 }
358
359 static grub_err_t
360 susp_iterate_set_rockridge (struct grub_iso9660_susp_entry *susp_entry,
361 void *_data)
362 {
363 struct grub_iso9660_data *data = _data;
364 /* The "ER" entry is used to detect extensions. The
365 `IEEE_P1285' extension means Rock ridge. */
366 if (grub_strncmp ((char *) susp_entry->sig, "ER", 2) == 0)
367 {
368 data->rockridge = 1;
369 return 1;
370 }
371 return 0;
372 }
373
374 static grub_err_t
375 set_rockridge (struct grub_iso9660_data *data)
376 {
377 int sua_pos;
378 int sua_size;
379 char *sua;
380 struct grub_iso9660_dir rootdir;
381 struct grub_iso9660_susp_entry *entry;
382
383 data->rockridge = 0;
384
385 /* Read the system use area and test it to see if SUSP is
386 supported. */
387 if (grub_disk_read (data->disk,
388 (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
389 << GRUB_ISO9660_LOG2_BLKSZ), 0,
390 sizeof (rootdir), (char *) &rootdir))
391 return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
392
393 sua_pos = (sizeof (rootdir) + rootdir.namelen
394 + (rootdir.namelen % 2) - 1);
395 sua_size = rootdir.len - sua_pos;
396
397 if (!sua_size)
398 return GRUB_ERR_NONE;
399
400 sua = grub_malloc (sua_size);
401 if (! sua)
402 return grub_errno;
403
404 if (grub_disk_read (data->disk,
405 (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
406 << GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
407 sua_size, sua))
408 {
409 grub_free (sua);
410 return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
411 }
412
413 entry = (struct grub_iso9660_susp_entry *) sua;
414
415 /* Test if the SUSP protocol is used on this filesystem. */
416 if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0)
417 {
418 struct grub_fshelp_node rootnode;
419
420 rootnode.data = data;
421 rootnode.alloc_dirents = ARRAY_SIZE (rootnode.dirents);
422 rootnode.have_dirents = 1;
423 rootnode.have_symlink = 0;
424 rootnode.dirents[0] = data->voldesc.rootdir;
425
426 /* The 2nd data byte stored how many bytes are skipped every time
427 to get to the SUA (System Usage Area). */
428 data->susp_skip = entry->u.data[1 + 2];
429 entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
430
431 /* Iterate over the entries in the SUA area to detect
432 extensions. */
433 if (grub_iso9660_susp_iterate (&rootnode,
434 sua_pos, sua_size, susp_iterate_set_rockridge,
435 data))
436 {
437 grub_free (sua);
438 return grub_errno;
439 }
440 }
441 grub_free (sua);
442 return GRUB_ERR_NONE;
443 }
444
445 static struct grub_iso9660_data *
446 grub_iso9660_mount (grub_disk_t disk)
447 {
448 struct grub_iso9660_data *data = 0;
449 struct grub_iso9660_primary_voldesc voldesc;
450 int block;
451
452 data = grub_zalloc (sizeof (struct grub_iso9660_data));
453 if (! data)
454 return 0;
455
456 data->disk = disk;
457
458 block = 16;
459 do
460 {
461 int copy_voldesc = 0;
462
463 /* Read the superblock. */
464 if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0,
465 sizeof (struct grub_iso9660_primary_voldesc),
466 (char *) &voldesc))
467 {
468 grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
469 goto fail;
470 }
471
472 if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0)
473 {
474 grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
475 goto fail;
476 }
477
478 if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY)
479 copy_voldesc = 1;
480 else if (!data->rockridge
481 && (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP)
482 && (voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f)
483 &&
484 ((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */
485 (voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */
486 (voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */
487 {
488 copy_voldesc = 1;
489 data->joliet = 1;
490 }
491
492 if (copy_voldesc)
493 {
494 grub_memcpy((char *) &data->voldesc, (char *) &voldesc,
495 sizeof (struct grub_iso9660_primary_voldesc));
496 if (set_rockridge (data))
497 goto fail;
498 }
499
500 block++;
501 } while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END);
502
503 return data;
504
505 fail:
506 grub_free (data);
507 return 0;
508 }
509
510
511 static char *
512 grub_iso9660_read_symlink (grub_fshelp_node_t node)
513 {
514 return node->have_symlink
515 ? grub_strdup (node->symlink
516 + (node->have_dirents) * sizeof (node->dirents[0])
517 - sizeof (node->dirents)) : grub_strdup ("");
518 }
519
520 static grub_off_t
521 get_node_size (grub_fshelp_node_t node)
522 {
523 grub_off_t ret = 0;
524 grub_size_t i;
525
526 for (i = 0; i < node->have_dirents; i++)
527 ret += grub_le_to_cpu32 (node->dirents[i].size);
528 return ret;
529 }
530
531 struct iterate_dir_ctx
532 {
533 char *filename;
534 int filename_alloc;
535 enum grub_fshelp_filetype type;
536 char *symlink;
537 int was_continue;
538 };
539
540 /* Extend the symlink. */
541 static void
542 add_part (struct iterate_dir_ctx *ctx,
543 const char *part,
544 int len2)
545 {
546 int size = ctx->symlink ? grub_strlen (ctx->symlink) : 0;
547
548 ctx->symlink = grub_realloc (ctx->symlink, size + len2 + 1);
549 if (! ctx->symlink)
550 return;
551
552 grub_memcpy (ctx->symlink + size, part, len2);
553 ctx->symlink[size + len2] = 0;
554 }
555
556 static grub_err_t
557 susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
558 void *_ctx)
559 {
560 struct iterate_dir_ctx *ctx = _ctx;
561
562 /* The filename in the rock ridge entry. */
563 if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0)
564 {
565 /* The flags are stored at the data position 0, here the
566 filename type is stored. */
567 /* FIXME: Fix this slightly improper cast. */
568 if (entry->u.data[1 + 0] & GRUB_ISO9660_RR_DOT)
569 ctx->filename = (char *) ".";
570 else if (entry->u.data[1 + 0] & GRUB_ISO9660_RR_DOTDOT)
571 ctx->filename = (char *) "..";
572 else if (entry->len >= 5)
573 {
574 grub_size_t off = 0, csize = 1;
575 char *old;
576 csize = entry->len - 5;
577 old = ctx->filename;
578 if (ctx->filename_alloc)
579 {
580 off = grub_strlen (ctx->filename);
581 ctx->filename = grub_realloc (ctx->filename, csize + off + 1);
582 }
583 else
584 {
585 off = 0;
586 ctx->filename = grub_zalloc (csize + 1);
587 }
588 if (!ctx->filename)
589 {
590 ctx->filename = old;
591 return grub_errno;
592 }
593 ctx->filename_alloc = 1;
594 grub_memcpy (ctx->filename + off, (char *) &entry->u.data[1 + 1], csize);
595 ctx->filename[off + csize] = '\0';
596 }
597 }
598 /* The mode information (st_mode). */
599 else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0)
600 {
601 /* At position 0 of the PX record the st_mode information is
602 stored (little-endian). */
603 grub_uint32_t mode = ((entry->u.data[1 + 0] + (entry->u.data[1 + 1] << 8))
604 & GRUB_ISO9660_FSTYPE_MASK);
605
606 switch (mode)
607 {
608 case GRUB_ISO9660_FSTYPE_DIR:
609 ctx->type = GRUB_FSHELP_DIR;
610 break;
611 case GRUB_ISO9660_FSTYPE_REG:
612 ctx->type = GRUB_FSHELP_REG;
613 break;
614 case GRUB_ISO9660_FSTYPE_SYMLINK:
615 ctx->type = GRUB_FSHELP_SYMLINK;
616 break;
617 default:
618 ctx->type = GRUB_FSHELP_UNKNOWN;
619 }
620 }
621 else if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0)
622 {
623 unsigned int pos = 1;
624
625 /* The symlink is not stored as a POSIX symlink, translate it. */
626 while (pos + sizeof (*entry) < entry->len)
627 {
628 /* The current position is the `Component Flag'. */
629 switch (entry->u.data[1 + pos] & 30)
630 {
631 case 0:
632 {
633 /* The data on pos + 2 is the actual data, pos + 1
634 is the length. Both are part of the `Component
635 Record'. */
636 if (ctx->symlink && !ctx->was_continue)
637 add_part (ctx, "/", 1);
638 add_part (ctx, (char *) &entry->u.data[1 + pos + 2],
639 entry->u.data[1 + pos + 1]);
640 ctx->was_continue = (entry->u.data[1 + pos] & 1);
641 break;
642 }
643
644 case 2:
645 add_part (ctx, "./", 2);
646 break;
647
648 case 4:
649 add_part (ctx, "../", 3);
650 break;
651
652 case 8:
653 add_part (ctx, "/", 1);
654 break;
655 }
656 /* In pos + 1 the length of the `Component Record' is
657 stored. */
658 pos += entry->u.data[1 + pos + 1] + 2;
659 }
660
661 /* Check if `grub_realloc' failed. */
662 if (grub_errno)
663 return grub_errno;
664 }
665
666 return 0;
667 }
668
669 static int
670 grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
671 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
672 {
673 struct grub_iso9660_dir dirent;
674 grub_off_t offset = 0;
675 grub_off_t len;
676 struct iterate_dir_ctx ctx;
677
678 len = get_node_size (dir);
679
680 for (; offset < len; offset += dirent.len)
681 {
682 ctx.symlink = 0;
683 ctx.was_continue = 0;
684
685 if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
686 return 0;
687
688 /* The end of the block, skip to the next one. */
689 if (!dirent.len)
690 {
691 offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
692 continue;
693 }
694
695 {
696 char name[MAX_NAMELEN + 1];
697 int nameoffset = offset + sizeof (dirent);
698 struct grub_fshelp_node *node;
699 int sua_off = (sizeof (dirent) + dirent.namelen + 1
700 - (dirent.namelen % 2));
701 int sua_size = dirent.len - sua_off;
702
703 sua_off += offset + dir->data->susp_skip;
704
705 ctx.filename = 0;
706 ctx.filename_alloc = 0;
707 ctx.type = GRUB_FSHELP_UNKNOWN;
708
709 if (dir->data->rockridge
710 && grub_iso9660_susp_iterate (dir, sua_off, sua_size,
711 susp_iterate_dir, &ctx))
712 return 0;
713
714 /* Read the name. */
715 if (read_node (dir, nameoffset, dirent.namelen, (char *) name))
716 return 0;
717
718 node = grub_malloc (sizeof (struct grub_fshelp_node));
719 if (!node)
720 return 0;
721
722 node->alloc_dirents = ARRAY_SIZE (node->dirents);
723 node->have_dirents = 1;
724
725 /* Setup a new node. */
726 node->data = dir->data;
727 node->have_symlink = 0;
728
729 /* If the filetype was not stored using rockridge, use
730 whatever is stored in the iso9660 filesystem. */
731 if (ctx.type == GRUB_FSHELP_UNKNOWN)
732 {
733 if ((dirent.flags & FLAG_TYPE) == FLAG_TYPE_DIR)
734 ctx.type = GRUB_FSHELP_DIR;
735 else
736 ctx.type = GRUB_FSHELP_REG;
737 }
738
739 /* . and .. */
740 if (!ctx.filename && dirent.namelen == 1 && name[0] == 0)
741 ctx.filename = (char *) ".";
742
743 if (!ctx.filename && dirent.namelen == 1 && name[0] == 1)
744 ctx.filename = (char *) "..";
745
746 if (g_fs_name_nocase)
747 ctx.type |= GRUB_FSHELP_CASE_INSENSITIVE;
748
749 /* The filename was not stored in a rock ridge entry. Read it
750 from the iso9660 filesystem. */
751 if (!dir->data->joliet && !ctx.filename)
752 {
753 char *ptr;
754 name[dirent.namelen] = '\0';
755 ctx.filename = grub_strrchr (name, ';');
756 if (ctx.filename)
757 *ctx.filename = '\0';
758 /* ISO9660 names are not case-preserving. */
759 ctx.type |= GRUB_FSHELP_CASE_INSENSITIVE;
760 for (ptr = name; *ptr; ptr++)
761 *ptr = grub_tolower (*ptr);
762 if (ptr != name && *(ptr - 1) == '.')
763 *(ptr - 1) = 0;
764 ctx.filename = name;
765 }
766
767 if (dir->data->joliet && !ctx.filename)
768 {
769 char *semicolon;
770
771 ctx.filename = grub_iso9660_convert_string
772 ((grub_uint8_t *) name, dirent.namelen >> 1);
773
774 semicolon = grub_strrchr (ctx.filename, ';');
775 if (semicolon)
776 *semicolon = '\0';
777
778 ctx.filename_alloc = 1;
779 }
780
781 node->dirents[0] = dirent;
782 while (dirent.flags & FLAG_MORE_EXTENTS)
783 {
784 offset += dirent.len;
785 if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
786 {
787 if (ctx.filename_alloc)
788 grub_free (ctx.filename);
789 grub_free (node);
790 return 0;
791 }
792 if (node->have_dirents >= node->alloc_dirents)
793 {
794 struct grub_fshelp_node *new_node;
795 node->alloc_dirents *= 2;
796 new_node = grub_realloc (node,
797 sizeof (struct grub_fshelp_node)
798 + ((node->alloc_dirents
799 - ARRAY_SIZE (node->dirents))
800 * sizeof (node->dirents[0])));
801 if (!new_node)
802 {
803 if (ctx.filename_alloc)
804 grub_free (ctx.filename);
805 grub_free (node);
806 return 0;
807 }
808 node = new_node;
809 }
810 node->dirents[node->have_dirents++] = dirent;
811 }
812 if (ctx.symlink)
813 {
814 if ((node->alloc_dirents - node->have_dirents)
815 * sizeof (node->dirents[0]) < grub_strlen (ctx.symlink) + 1)
816 {
817 struct grub_fshelp_node *new_node;
818 new_node = grub_realloc (node,
819 sizeof (struct grub_fshelp_node)
820 + ((node->alloc_dirents
821 - ARRAY_SIZE (node->dirents))
822 * sizeof (node->dirents[0]))
823 + grub_strlen (ctx.symlink) + 1);
824 if (!new_node)
825 {
826 if (ctx.filename_alloc)
827 grub_free (ctx.filename);
828 grub_free (node);
829 return 0;
830 }
831 node = new_node;
832 }
833 node->have_symlink = 1;
834 grub_strcpy (node->symlink
835 + node->have_dirents * sizeof (node->dirents[0])
836 - sizeof (node->dirents), ctx.symlink);
837 grub_free (ctx.symlink);
838 ctx.symlink = 0;
839 ctx.was_continue = 0;
840 }
841 if (hook (ctx.filename, ctx.type, node, hook_data))
842 {
843 if (ctx.filename_alloc)
844 grub_free (ctx.filename);
845 return 1;
846 }
847 if (ctx.filename_alloc)
848 grub_free (ctx.filename);
849 }
850 }
851
852 return 0;
853 }
854
855
856 \f
857 /* Context for grub_iso9660_dir. */
858 struct grub_iso9660_dir_ctx
859 {
860 grub_fs_dir_hook_t hook;
861 void *hook_data;
862 };
863
864 /* Helper for grub_iso9660_dir. */
865 static int
866 grub_iso9660_dir_iter (const char *filename,
867 enum grub_fshelp_filetype filetype,
868 grub_fshelp_node_t node, void *data)
869 {
870 struct grub_iso9660_dir_ctx *ctx = data;
871 struct grub_dirhook_info info;
872
873 grub_memset (&info, 0, sizeof (info));
874 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
875 info.mtimeset = !!iso9660_to_unixtime2 (&node->dirents[0].mtime, &info.mtime);
876
877 grub_free (node);
878 return ctx->hook (filename, &info, ctx->hook_data);
879 }
880
881 static grub_err_t
882 grub_iso9660_dir (grub_device_t device, const char *path,
883 grub_fs_dir_hook_t hook, void *hook_data)
884 {
885 struct grub_iso9660_dir_ctx ctx = { hook, hook_data };
886 struct grub_iso9660_data *data = 0;
887 struct grub_fshelp_node rootnode;
888 struct grub_fshelp_node *foundnode;
889
890 grub_dl_ref (my_mod);
891
892 data = grub_iso9660_mount (device->disk);
893 if (! data)
894 goto fail;
895
896 rootnode.data = data;
897 rootnode.alloc_dirents = 0;
898 rootnode.have_dirents = 1;
899 rootnode.have_symlink = 0;
900 rootnode.dirents[0] = data->voldesc.rootdir;
901
902 /* Use the fshelp function to traverse the path. */
903 if (grub_fshelp_find_file (path, &rootnode,
904 &foundnode,
905 grub_iso9660_iterate_dir,
906 grub_iso9660_read_symlink,
907 GRUB_FSHELP_DIR))
908 goto fail;
909
910 /* List the files in the directory. */
911 grub_iso9660_iterate_dir (foundnode, grub_iso9660_dir_iter, &ctx);
912
913 if (foundnode != &rootnode)
914 grub_free (foundnode);
915
916 fail:
917 grub_free (data);
918
919 grub_dl_unref (my_mod);
920
921 return grub_errno;
922 }
923
924
925 /* Open a file named NAME and initialize FILE. */
926 static grub_err_t
927 grub_iso9660_open (struct grub_file *file, const char *name)
928 {
929 struct grub_iso9660_data *data;
930 struct grub_fshelp_node rootnode;
931 struct grub_fshelp_node *foundnode;
932
933 grub_dl_ref (my_mod);
934
935 data = grub_iso9660_mount (file->device->disk);
936 if (!data)
937 goto fail;
938
939 rootnode.data = data;
940 rootnode.alloc_dirents = 0;
941 rootnode.have_dirents = 1;
942 rootnode.have_symlink = 0;
943 rootnode.dirents[0] = data->voldesc.rootdir;
944
945 /* Use the fshelp function to traverse the path. */
946 if (grub_fshelp_find_file (name, &rootnode,
947 &foundnode,
948 grub_iso9660_iterate_dir,
949 grub_iso9660_read_symlink,
950 GRUB_FSHELP_REG))
951 goto fail;
952
953 data->node = foundnode;
954 file->data = data;
955 file->size = get_node_size (foundnode);
956 file->offset = 0;
957
958 return 0;
959
960 fail:
961 grub_dl_unref (my_mod);
962
963 grub_free (data);
964
965 return grub_errno;
966 }
967
968
969 static grub_ssize_t
970 grub_iso9660_read (grub_file_t file, char *buf, grub_size_t len)
971 {
972 struct grub_iso9660_data *data =
973 (struct grub_iso9660_data *) file->data;
974 grub_err_t err;
975
976 /* XXX: The file is stored in as a single extent. */
977 data->disk->read_hook = file->read_hook;
978 data->disk->read_hook_data = file->read_hook_data;
979 err = read_node (data->node, file->offset, len, buf);
980 data->disk->read_hook = NULL;
981
982 if (err || grub_errno)
983 return -1;
984
985 return len;
986 }
987
988
989 static grub_err_t
990 grub_iso9660_close (grub_file_t file)
991 {
992 struct grub_iso9660_data *data =
993 (struct grub_iso9660_data *) file->data;
994 grub_free (data->node);
995 grub_free (data);
996
997 grub_dl_unref (my_mod);
998
999 return GRUB_ERR_NONE;
1000 }
1001
1002
1003 static grub_err_t
1004 grub_iso9660_label (grub_device_t device, char **label)
1005 {
1006 struct grub_iso9660_data *data;
1007 data = grub_iso9660_mount (device->disk);
1008
1009 if (data)
1010 {
1011 if (data->joliet)
1012 *label = grub_iso9660_convert_string (data->voldesc.volname, 16);
1013 else
1014 *label = grub_strndup ((char *) data->voldesc.volname, 32);
1015 if (*label)
1016 {
1017 char *ptr;
1018 for (ptr = *label; *ptr;ptr++);
1019 ptr--;
1020 while (ptr >= *label && *ptr == ' ')
1021 *ptr-- = 0;
1022 }
1023
1024 grub_free (data);
1025 }
1026 else
1027 *label = 0;
1028
1029 return grub_errno;
1030 }
1031
1032
1033 static grub_err_t
1034 grub_iso9660_uuid (grub_device_t device, char **uuid)
1035 {
1036 struct grub_iso9660_data *data;
1037 grub_disk_t disk = device->disk;
1038
1039 grub_dl_ref (my_mod);
1040
1041 data = grub_iso9660_mount (disk);
1042 if (data)
1043 {
1044 if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1]
1045 && ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3]
1046 && ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1]
1047 && ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1]
1048 && ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1]
1049 && ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1]
1050 && ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1]
1051 && ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1])
1052 {
1053 grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID");
1054 *uuid = NULL;
1055 }
1056 else
1057 {
1058 *uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c",
1059 data->voldesc.modified.year[0],
1060 data->voldesc.modified.year[1],
1061 data->voldesc.modified.year[2],
1062 data->voldesc.modified.year[3],
1063 data->voldesc.modified.month[0],
1064 data->voldesc.modified.month[1],
1065 data->voldesc.modified.day[0],
1066 data->voldesc.modified.day[1],
1067 data->voldesc.modified.hour[0],
1068 data->voldesc.modified.hour[1],
1069 data->voldesc.modified.minute[0],
1070 data->voldesc.modified.minute[1],
1071 data->voldesc.modified.second[0],
1072 data->voldesc.modified.second[1],
1073 data->voldesc.modified.hundredth[0],
1074 data->voldesc.modified.hundredth[1]);
1075 }
1076 }
1077 else
1078 *uuid = NULL;
1079
1080 grub_dl_unref (my_mod);
1081
1082 grub_free (data);
1083
1084 return grub_errno;
1085 }
1086
1087 /* Get writing time of filesystem. */
1088 static grub_err_t
1089 grub_iso9660_mtime (grub_device_t device, grub_int32_t *timebuf)
1090 {
1091 struct grub_iso9660_data *data;
1092 grub_disk_t disk = device->disk;
1093 grub_err_t err;
1094
1095 grub_dl_ref (my_mod);
1096
1097 data = grub_iso9660_mount (disk);
1098 if (!data)
1099 {
1100 grub_dl_unref (my_mod);
1101 return grub_errno;
1102 }
1103 err = iso9660_to_unixtime (&data->voldesc.modified, timebuf);
1104
1105 grub_dl_unref (my_mod);
1106
1107 grub_free (data);
1108
1109 return err;
1110 }
1111
1112
1113 \f
1114
1115 static struct grub_fs grub_iso9660_fs =
1116 {
1117 .name = "iso9660",
1118 .fs_dir = grub_iso9660_dir,
1119 .fs_open = grub_iso9660_open,
1120 .fs_read = grub_iso9660_read,
1121 .fs_close = grub_iso9660_close,
1122 .fs_label = grub_iso9660_label,
1123 .fs_uuid = grub_iso9660_uuid,
1124 .fs_mtime = grub_iso9660_mtime,
1125 #ifdef GRUB_UTIL
1126 .reserved_first_sector = 1,
1127 .blocklist_install = 1,
1128 #endif
1129 .next = 0
1130 };
1131
1132 GRUB_MOD_INIT(iso9660)
1133 {
1134 grub_fs_register (&grub_iso9660_fs);
1135 my_mod = mod;
1136 }
1137
1138 GRUB_MOD_FINI(iso9660)
1139 {
1140 grub_fs_unregister (&grub_iso9660_fs);
1141 }