1
/* iso9660.c - iso9660 implementation with extensions:
4
* GRUB -- GRand Unified Bootloader
5
* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
7
* This program 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 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
#include <grub/file.h>
25
#include <grub/misc.h>
26
#include <grub/disk.h>
28
#include <grub/types.h>
29
#include <grub/fshelp.h>
31
#define GRUB_ISO9660_FSTYPE_DIR 0040000
32
#define GRUB_ISO9660_FSTYPE_REG 0100000
33
#define GRUB_ISO9660_FSTYPE_SYMLINK 0120000
34
#define GRUB_ISO9660_FSTYPE_MASK 0170000
36
#define GRUB_ISO9660_LOG2_BLKSZ 2
37
#define GRUB_ISO9660_BLKSZ 2048
39
#define GRUB_ISO9660_RR_DOT 2
40
#define GRUB_ISO9660_RR_DOTDOT 4
42
/* The head of a volume descriptor. */
43
struct grub_iso9660_voldesc
46
grub_uint8_t magic[5];
48
} __attribute__ ((packed));
50
/* A directory entry. */
51
struct grub_iso9660_dir
54
grub_uint8_t ext_sectors;
55
grub_uint32_t first_sector;
56
grub_uint32_t first_sector_be;
58
grub_uint32_t size_be;
59
grub_uint8_t unused1[7];
61
grub_uint8_t unused2[6];
63
} __attribute__ ((packed));
65
/* The primary volume descriptor. Only little endian is used. */
66
struct grub_iso9660_primary_voldesc
68
struct grub_iso9660_voldesc voldesc;
69
grub_uint8_t unused1[33];
70
grub_uint8_t volname[32];
71
grub_uint8_t unused2[60];
72
grub_uint32_t path_table_size;
73
grub_uint8_t unused3[4];
74
grub_uint32_t path_table;
75
grub_uint8_t unused4[12];
76
struct grub_iso9660_dir rootdir;
77
} __attribute__ ((packed));
79
/* A single entry in the path table. */
80
struct grub_iso9660_path
84
grub_uint32_t first_sector;
85
grub_uint16_t parentdir;
87
} __attribute__ ((packed));
89
/* An entry in the System Usage area of the directory entry. */
90
struct grub_iso9660_susp_entry
96
} __attribute__ ((packed));
98
/* The CE entry. This is used to describe the next block where data
100
struct grub_iso9660_susp_ce
102
struct grub_iso9660_susp_entry entry;
104
grub_uint32_t blk_be;
106
grub_uint32_t off_be;
108
grub_uint32_t len_be;
109
} __attribute__ ((packed));
111
struct grub_iso9660_data
113
struct grub_iso9660_primary_voldesc voldesc;
115
unsigned int first_sector;
121
struct grub_fshelp_node
123
struct grub_iso9660_data *data;
126
unsigned int dir_blk;
127
unsigned int dir_off;
131
static grub_dl_t my_mod;
135
/* Iterate over the susp entries, starting with block SUA_BLOCK on the
136
offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
139
grub_iso9660_susp_iterate (struct grub_iso9660_data *data,
140
int sua_block, int sua_pos, int sua_size,
142
(struct grub_iso9660_susp_entry *entry))
145
struct grub_iso9660_susp_entry *entry;
147
auto grub_err_t load_sua (void);
149
/* Load a part of the System Usage Area. */
150
grub_err_t load_sua (void)
152
sua = grub_malloc (sua_size);
156
if (grub_disk_read (data->disk, sua_block, sua_pos,
160
entry = (struct grub_iso9660_susp_entry *) sua;
167
for (; (char *) entry < (char *) sua + sua_size - 1;
168
entry = (struct grub_iso9660_susp_entry *)
169
((char *) entry + entry->len))
171
/* The last entry. */
172
if (!grub_strncmp (entry->sig, "ST", 2))
175
/* Additional entries are stored elsewhere. */
176
if (!grub_strncmp (entry->sig, "CE", 2))
178
struct grub_iso9660_susp_ce *ce;
180
ce = (struct grub_iso9660_susp_ce *) entry;
181
sua_size = grub_le_to_cpu32 (ce->len);
182
sua_pos = grub_le_to_cpu32 (ce->off);
183
sua_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
202
static struct grub_iso9660_data *
203
grub_iso9660_mount (grub_disk_t disk)
205
struct grub_iso9660_data *data = 0;
206
struct grub_iso9660_dir rootdir;
210
struct grub_iso9660_susp_entry *entry;
212
auto grub_err_t susp_iterate (struct grub_iso9660_susp_entry *);
214
grub_err_t susp_iterate (struct grub_iso9660_susp_entry *susp_entry)
216
/* The "ER" entry is used to detect extensions. The
217
`IEEE_P1285' extension means Rock ridge. */
218
if (!grub_strncmp (susp_entry->sig, "ER", 2))
226
data = grub_malloc (sizeof (struct grub_iso9660_data));
230
/* Read the superblock. */
231
if (grub_disk_read (disk, 16 << GRUB_ISO9660_LOG2_BLKSZ, 0,
232
sizeof (struct grub_iso9660_primary_voldesc),
233
(char *) &data->voldesc))
235
grub_error (GRUB_ERR_BAD_FS, "not a iso9660 filesystem");
239
if (grub_strncmp (data->voldesc.voldesc.magic, "CD001", 5))
241
grub_error (GRUB_ERR_BAD_FS, "not a iso9660 filesystem");
248
/* Read the system use area and test it to see if SUSP is
250
if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
251
<< GRUB_ISO9660_LOG2_BLKSZ), 0,
252
sizeof (rootdir), (char *) &rootdir))
254
grub_error (GRUB_ERR_BAD_FS, "not a iso9660 filesystem");
258
if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
259
<< GRUB_ISO9660_LOG2_BLKSZ), 0,
260
sizeof (rootdir), (char *) &rootdir))
262
grub_error (GRUB_ERR_BAD_FS, "not a iso9660 filesystem");
266
sua_pos = (sizeof (rootdir) + rootdir.namelen
267
+ (rootdir.namelen % 2) - 1);
268
sua_size = rootdir.len - sua_pos;
270
sua = grub_malloc (sua_size);
274
if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
275
<< GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
278
grub_error (GRUB_ERR_BAD_FS, "not a iso9660 filesystem");
282
entry = (struct grub_iso9660_susp_entry *) sua;
284
/* Test if the SUSP protocol is used on this filesystem. */
285
if (!grub_strncmp (entry->sig, "SP", 2))
287
/* The 2nd data byte stored how many bytes are skipped everytime
288
to get to the SUA (System Usage Area). */
289
data->susp_skip = entry->data[2];
290
entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
292
/* Iterate over the entries in the SUA area to detect
294
if (grub_iso9660_susp_iterate (data,
295
(grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
296
<< GRUB_ISO9660_LOG2_BLKSZ),
297
sua_pos, sua_size, susp_iterate))
310
grub_iso9660_read_symlink (grub_fshelp_node_t node)
312
struct grub_iso9660_dir dirent;
318
auto void add_part (const char *part, int len);
319
auto grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *);
321
/* Extend the symlink. */
322
void add_part (const char *part, int len)
324
int size = grub_strlen (symlink);
326
symlink = grub_realloc (symlink, size + len + 1);
329
grub_strncat (symlink, part, len);
334
/* Read in a symlink. */
335
grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *entry)
337
if (!grub_strncmp ("SL", entry->sig, 2))
339
unsigned int pos = 1;
341
/* The symlink is not stored as a POSIX symlink, translate it. */
342
while (pos < grub_le_to_cpu32 (entry->len))
350
/* The current position is the `Component Flag'. */
351
switch (entry->data[pos] & 30)
355
/* The data on pos + 2 is the actual data, pos + 1
356
is the length. Both are part of the `Component
358
add_part (&entry->data[pos + 2],
359
entry->data[pos + 1]);
360
if ((entry->data[pos] & 1))
378
/* In pos + 1 the length of the `Component Record' is
380
pos += entry->data[pos + 1] + 2;
383
/* Check if `grub_realloc' failed. */
391
if (grub_disk_read (node->data->disk, node->dir_blk, node->dir_off,
392
sizeof (dirent), (char *) &dirent))
395
sua_off = (sizeof (dirent) + dirent.namelen + 1 - (dirent.namelen % 2)
396
+ node->data->susp_skip);
397
sua_size = dirent.len - sua_off;
399
symlink = grub_malloc (1);
405
if (grub_iso9660_susp_iterate (node->data, node->dir_blk,
406
node->dir_off + sua_off,
407
sua_size, susp_iterate_sl))
418
grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
420
(*hook) (const char *filename,
421
enum grub_fshelp_filetype filetype,
422
grub_fshelp_node_t node))
424
struct grub_iso9660_dir dirent;
425
unsigned int offset = 0;
427
int filename_alloc = 0;
428
enum grub_fshelp_filetype type;
430
auto grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *);
432
grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *entry)
434
/* The filename in the rock ridge entry. */
435
if (!grub_strncmp ("NM", entry->sig, 2))
437
/* The flags are stored at the data position 0, here the
438
filename type is stored. */
439
if (entry->data[0] & GRUB_ISO9660_RR_DOT)
441
else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
448
size += grub_strlen (filename);
449
grub_realloc (filename,
450
grub_strlen (filename)
455
size = entry->len - 5;
456
filename = grub_malloc (size + 1);
460
grub_strncpy (filename, &entry->data[1], size);
461
filename [size] = '\0';
464
/* The mode information (st_mode). */
465
else if (!grub_strncmp (entry->sig, "PX", 2))
467
/* At position 0 of the PX record the st_mode information is
469
grub_uint32_t mode = ((*(grub_uint32_t *) &entry->data[0])
470
& GRUB_ISO9660_FSTYPE_MASK);
474
case GRUB_ISO9660_FSTYPE_DIR:
475
type = GRUB_FSHELP_DIR;
477
case GRUB_ISO9660_FSTYPE_REG:
478
type = GRUB_FSHELP_REG;
480
case GRUB_ISO9660_FSTYPE_SYMLINK:
481
type = GRUB_FSHELP_SYMLINK;
484
type = GRUB_FSHELP_UNKNOWN;
491
while (offset < dir->size)
493
if (grub_disk_read (dir->data->disk,
494
(dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
495
+ offset / GRUB_DISK_SECTOR_SIZE,
496
offset % GRUB_DISK_SECTOR_SIZE,
497
sizeof (dirent), (char *) &dirent))
500
/* The end of the block, skip to the next one. */
503
offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
508
char name[dirent.namelen + 1];
509
int nameoffset = offset + sizeof (dirent);
510
struct grub_fshelp_node *node;
511
int sua_off = (sizeof (dirent) + dirent.namelen + 1
512
- (dirent.namelen % 2));;
513
int sua_size = dirent.len - sua_off;
515
sua_off += offset + dir->data->susp_skip;
519
type = GRUB_FSHELP_UNKNOWN;
521
if (dir->data->rockridge
522
&& grub_iso9660_susp_iterate (dir->data,
523
(dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
525
/ GRUB_DISK_SECTOR_SIZE),
526
sua_off % GRUB_DISK_SECTOR_SIZE,
527
sua_size, susp_iterate_dir))
531
if (grub_disk_read (dir->data->disk,
532
(dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
533
+ nameoffset / GRUB_DISK_SECTOR_SIZE,
534
nameoffset % GRUB_DISK_SECTOR_SIZE,
535
dirent.namelen, (char *) name))
538
node = grub_malloc (sizeof (struct grub_fshelp_node));
542
/* Setup a new node. */
543
node->data = dir->data;
544
node->size = grub_le_to_cpu32 (dirent.size);
545
node->blk = grub_le_to_cpu32 (dirent.first_sector);
546
node->dir_blk = ((dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
547
+ offset / GRUB_DISK_SECTOR_SIZE);
548
node->dir_off = offset % GRUB_DISK_SECTOR_SIZE;
550
/* If the filetype was not stored using rockridge, use
551
whatever is stored in the iso9660 filesystem. */
552
if (type == GRUB_FSHELP_UNKNOWN)
554
if ((dirent.flags & 3) == 2)
555
type = GRUB_FSHELP_DIR;
557
type = GRUB_FSHELP_REG;
560
/* The filename was not stored in a rock ridge entry. Read it
561
from the iso9660 filesystem. */
564
name[dirent.namelen] = '\0';
565
filename = grub_strrchr (name, ';');
569
if (dirent.namelen == 1 && name[0] == 0)
571
else if (dirent.namelen == 1 && name[0] == 1)
577
if (hook (filename, type, node))
580
grub_free (filename);
584
grub_free (filename);
587
offset += dirent.len;
596
grub_iso9660_dir (grub_device_t device, const char *path,
597
int (*hook) (const char *filename, int dir))
599
struct grub_iso9660_data *data = 0;
600
struct grub_fshelp_node rootnode;
601
struct grub_fshelp_node *foundnode;
603
auto int NESTED_FUNC_ATTR iterate (const char *filename,
604
enum grub_fshelp_filetype filetype,
605
grub_fshelp_node_t node);
607
int NESTED_FUNC_ATTR iterate (const char *filename,
608
enum grub_fshelp_filetype filetype,
609
grub_fshelp_node_t node)
613
if (filetype == GRUB_FSHELP_DIR)
614
return hook (filename, 1);
616
return hook (filename, 0);
622
grub_dl_ref (my_mod);
625
data = grub_iso9660_mount (device->disk);
629
rootnode.data = data;
630
rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
631
rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
633
/* Use the fshelp function to traverse the path. */
634
if (grub_fshelp_find_file (path, &rootnode,
636
grub_iso9660_iterate_dir,
637
grub_iso9660_read_symlink,
641
/* List the files in the directory. */
642
grub_iso9660_iterate_dir (foundnode, iterate);
644
if (foundnode != &rootnode)
645
grub_free (foundnode);
651
grub_dl_unref (my_mod);
658
/* Open a file named NAME and initialize FILE. */
660
grub_iso9660_open (struct grub_file *file, const char *name)
662
struct grub_iso9660_data *data;
663
struct grub_fshelp_node rootnode;
664
struct grub_fshelp_node *foundnode;
667
grub_dl_ref (my_mod);
670
data = grub_iso9660_mount (file->device->disk);
674
rootnode.data = data;
675
rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
676
rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
678
/* Use the fshelp function to traverse the path. */
679
if (grub_fshelp_find_file (name, &rootnode,
681
grub_iso9660_iterate_dir,
682
grub_iso9660_read_symlink,
686
data->first_sector = foundnode->blk;
687
data->length = foundnode->size;
690
file->size = foundnode->size;
697
grub_dl_unref (my_mod);
707
grub_iso9660_read (grub_file_t file, char *buf, grub_ssize_t len)
709
struct grub_iso9660_data *data =
710
(struct grub_iso9660_data *) file->data;
712
/* XXX: The file is stored in as a single extent. */
713
data->disk->read_hook = file->read_hook;
714
grub_disk_read (data->disk,
715
data->first_sector << GRUB_ISO9660_LOG2_BLKSZ,
718
data->disk->read_hook = 0;
725
grub_iso9660_close (grub_file_t file)
727
grub_free (file->data);
730
grub_dl_unref (my_mod);
733
return GRUB_ERR_NONE;
738
grub_iso9660_label (grub_device_t device, char **label)
740
struct grub_iso9660_data *data;
741
data = grub_iso9660_mount (device->disk);
745
*label = grub_strndup (data->voldesc.volname, 32);
756
static struct grub_fs grub_iso9660_fs =
759
.dir = grub_iso9660_dir,
760
.open = grub_iso9660_open,
761
.read = grub_iso9660_read,
762
.close = grub_iso9660_close,
763
.label = grub_iso9660_label,
767
GRUB_MOD_INIT(iso9660)
769
grub_fs_register (&grub_iso9660_fs);
775
GRUB_MOD_FINI(iso9660)
777
grub_fs_unregister (&grub_iso9660_fs);