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