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