1
/* iso9660.c - iso9660 implementation with extensions:
4
* GRUB -- GRand Unified Bootloader
5
* Copyright (C) 2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
7
* GRUB is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* GRUB is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
22
#include <grub/file.h>
24
#include <grub/misc.h>
25
#include <grub/disk.h>
27
#include <grub/types.h>
28
#include <grub/fshelp.h>
29
#include <grub/charset.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
#define GRUB_ISO9660_VOLDESC_BOOT 0
43
#define GRUB_ISO9660_VOLDESC_PRIMARY 1
44
#define GRUB_ISO9660_VOLDESC_SUPP 2
45
#define GRUB_ISO9660_VOLDESC_PART 3
46
#define GRUB_ISO9660_VOLDESC_END 255
48
/* The head of a volume descriptor. */
49
struct grub_iso9660_voldesc
52
grub_uint8_t magic[5];
54
} __attribute__ ((packed));
56
/* A directory entry. */
57
struct grub_iso9660_dir
60
grub_uint8_t ext_sectors;
61
grub_uint32_t first_sector;
62
grub_uint32_t first_sector_be;
64
grub_uint32_t size_be;
65
grub_uint8_t unused1[7];
67
grub_uint8_t unused2[6];
69
} __attribute__ ((packed));
71
struct grub_iso9660_date
74
grub_uint8_t month[2];
77
grub_uint8_t minute[2];
78
grub_uint8_t second[2];
79
grub_uint8_t hundredth[2];
81
} __attribute__ ((packed));
83
/* The primary volume descriptor. Only little endian is used. */
84
struct grub_iso9660_primary_voldesc
86
struct grub_iso9660_voldesc voldesc;
87
grub_uint8_t unused1[33];
88
grub_uint8_t volname[32];
89
grub_uint8_t unused2[16];
90
grub_uint8_t escape[32];
91
grub_uint8_t unused3[12];
92
grub_uint32_t path_table_size;
93
grub_uint8_t unused4[4];
94
grub_uint32_t path_table;
95
grub_uint8_t unused5[12];
96
struct grub_iso9660_dir rootdir;
97
grub_uint8_t unused6[624];
98
struct grub_iso9660_date created;
99
struct grub_iso9660_date modified;
100
} __attribute__ ((packed));
102
/* A single entry in the path table. */
103
struct grub_iso9660_path
106
grub_uint8_t sectors;
107
grub_uint32_t first_sector;
108
grub_uint16_t parentdir;
109
grub_uint8_t name[0];
110
} __attribute__ ((packed));
112
/* An entry in the System Usage area of the directory entry. */
113
struct grub_iso9660_susp_entry
117
grub_uint8_t version;
118
grub_uint8_t data[0];
119
} __attribute__ ((packed));
121
/* The CE entry. This is used to describe the next block where data
123
struct grub_iso9660_susp_ce
125
struct grub_iso9660_susp_entry entry;
127
grub_uint32_t blk_be;
129
grub_uint32_t off_be;
131
grub_uint32_t len_be;
132
} __attribute__ ((packed));
134
struct grub_iso9660_data
136
struct grub_iso9660_primary_voldesc voldesc;
138
unsigned int first_sector;
145
struct grub_fshelp_node
147
struct grub_iso9660_data *data;
150
unsigned int dir_blk;
151
unsigned int dir_off;
154
static grub_dl_t my_mod;
157
/* Iterate over the susp entries, starting with block SUA_BLOCK on the
158
offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
161
grub_iso9660_susp_iterate (struct grub_iso9660_data *data,
162
int sua_block, int sua_pos, int sua_size,
164
(struct grub_iso9660_susp_entry *entry))
167
struct grub_iso9660_susp_entry *entry;
169
auto grub_err_t load_sua (void);
171
/* Load a part of the System Usage Area. */
172
grub_err_t load_sua (void)
174
sua = grub_malloc (sua_size);
178
if (grub_disk_read (data->disk, sua_block, sua_pos,
182
entry = (struct grub_iso9660_susp_entry *) sua;
189
for (; (char *) entry < (char *) sua + sua_size - 1;
190
entry = (struct grub_iso9660_susp_entry *)
191
((char *) entry + entry->len))
193
/* The last entry. */
194
if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0)
197
/* Additional entries are stored elsewhere. */
198
if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0)
200
struct grub_iso9660_susp_ce *ce;
202
ce = (struct grub_iso9660_susp_ce *) entry;
203
sua_size = grub_le_to_cpu32 (ce->len);
204
sua_pos = grub_le_to_cpu32 (ce->off);
205
sua_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
224
grub_iso9660_convert_string (grub_uint16_t *us, int len)
229
p = grub_malloc (len * 4 + 1);
233
for (i=0; i<len; i++)
234
us[i] = grub_be_to_cpu16 (us[i]);
236
*grub_utf16_to_utf8 ((grub_uint8_t *) p, us, len) = '\0';
241
static struct grub_iso9660_data *
242
grub_iso9660_mount (grub_disk_t disk)
244
struct grub_iso9660_data *data = 0;
245
struct grub_iso9660_dir rootdir;
249
struct grub_iso9660_susp_entry *entry;
250
struct grub_iso9660_primary_voldesc voldesc;
253
auto grub_err_t susp_iterate (struct grub_iso9660_susp_entry *);
255
grub_err_t susp_iterate (struct grub_iso9660_susp_entry *susp_entry)
257
/* The "ER" entry is used to detect extensions. The
258
`IEEE_P1285' extension means Rock ridge. */
259
if (grub_strncmp ((char *) susp_entry->sig, "ER", 2) == 0)
267
data = grub_zalloc (sizeof (struct grub_iso9660_data));
276
int copy_voldesc = 0;
278
/* Read the superblock. */
279
if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0,
280
sizeof (struct grub_iso9660_primary_voldesc),
283
grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
287
if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0)
289
grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
293
if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY)
295
else if ((voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP) &&
296
(voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f) &&
297
((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */
298
(voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */
299
(voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */
306
grub_memcpy((char *) &data->voldesc, (char *) &voldesc,
307
sizeof (struct grub_iso9660_primary_voldesc));
310
} while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END);
312
/* Read the system use area and test it to see if SUSP is
314
if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
315
<< GRUB_ISO9660_LOG2_BLKSZ), 0,
316
sizeof (rootdir), (char *) &rootdir))
318
grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
322
sua_pos = (sizeof (rootdir) + rootdir.namelen
323
+ (rootdir.namelen % 2) - 1);
324
sua_size = rootdir.len - sua_pos;
326
sua = grub_malloc (sua_size);
330
if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
331
<< GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
334
grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
338
entry = (struct grub_iso9660_susp_entry *) sua;
340
/* Test if the SUSP protocol is used on this filesystem. */
341
if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0)
343
/* The 2nd data byte stored how many bytes are skipped every time
344
to get to the SUA (System Usage Area). */
345
data->susp_skip = entry->data[2];
346
entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
348
/* Iterate over the entries in the SUA area to detect
350
if (grub_iso9660_susp_iterate (data,
351
(grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
352
<< GRUB_ISO9660_LOG2_BLKSZ),
353
sua_pos, sua_size, susp_iterate))
366
grub_iso9660_read_symlink (grub_fshelp_node_t node)
368
struct grub_iso9660_dir dirent;
374
auto void add_part (const char *part, int len);
375
auto grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *);
377
/* Extend the symlink. */
378
void add_part (const char *part, int len)
380
int size = grub_strlen (symlink);
382
symlink = grub_realloc (symlink, size + len + 1);
386
grub_strncat (symlink, part, len);
389
/* Read in a symlink. */
390
grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *entry)
392
if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0)
394
unsigned int pos = 1;
396
/* The symlink is not stored as a POSIX symlink, translate it. */
397
while (pos < grub_le_to_cpu32 (entry->len))
405
/* The current position is the `Component Flag'. */
406
switch (entry->data[pos] & 30)
410
/* The data on pos + 2 is the actual data, pos + 1
411
is the length. Both are part of the `Component
413
add_part ((char *) &entry->data[pos + 2],
414
entry->data[pos + 1]);
415
if ((entry->data[pos] & 1))
433
/* In pos + 1 the length of the `Component Record' is
435
pos += entry->data[pos + 1] + 2;
438
/* Check if `grub_realloc' failed. */
446
if (grub_disk_read (node->data->disk, node->dir_blk, node->dir_off,
447
sizeof (dirent), (char *) &dirent))
450
sua_off = (sizeof (dirent) + dirent.namelen + 1 - (dirent.namelen % 2)
451
+ node->data->susp_skip);
452
sua_size = dirent.len - sua_off;
454
symlink = grub_malloc (1);
460
if (grub_iso9660_susp_iterate (node->data, node->dir_blk,
461
node->dir_off + sua_off,
462
sua_size, susp_iterate_sl))
473
grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
475
(*hook) (const char *filename,
476
enum grub_fshelp_filetype filetype,
477
grub_fshelp_node_t node))
479
struct grub_iso9660_dir dirent;
480
unsigned int offset = 0;
482
int filename_alloc = 0;
483
enum grub_fshelp_filetype type;
485
auto grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *);
487
grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *entry)
489
/* The filename in the rock ridge entry. */
490
if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0)
492
/* The flags are stored at the data position 0, here the
493
filename type is stored. */
494
if (entry->data[0] & GRUB_ISO9660_RR_DOT)
496
else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
503
size += grub_strlen (filename);
504
grub_realloc (filename,
505
grub_strlen (filename)
510
size = entry->len - 5;
511
filename = grub_zalloc (size + 1);
514
grub_strncpy (filename, (char *) &entry->data[1], size);
515
filename[size] = '\0';
518
/* The mode information (st_mode). */
519
else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0)
521
/* At position 0 of the PX record the st_mode information is
522
stored (little-endian). */
523
grub_uint32_t mode = ((entry->data[0] + (entry->data[1] << 8))
524
& GRUB_ISO9660_FSTYPE_MASK);
528
case GRUB_ISO9660_FSTYPE_DIR:
529
type = GRUB_FSHELP_DIR;
531
case GRUB_ISO9660_FSTYPE_REG:
532
type = GRUB_FSHELP_REG;
534
case GRUB_ISO9660_FSTYPE_SYMLINK:
535
type = GRUB_FSHELP_SYMLINK;
538
type = GRUB_FSHELP_UNKNOWN;
545
while (offset < dir->size)
547
if (grub_disk_read (dir->data->disk,
548
(dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
549
+ offset / GRUB_DISK_SECTOR_SIZE,
550
offset % GRUB_DISK_SECTOR_SIZE,
551
sizeof (dirent), (char *) &dirent))
554
/* The end of the block, skip to the next one. */
557
offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
562
char name[dirent.namelen + 1];
563
int nameoffset = offset + sizeof (dirent);
564
struct grub_fshelp_node *node;
565
int sua_off = (sizeof (dirent) + dirent.namelen + 1
566
- (dirent.namelen % 2));
567
int sua_size = dirent.len - sua_off;
569
sua_off += offset + dir->data->susp_skip;
573
type = GRUB_FSHELP_UNKNOWN;
575
if (dir->data->rockridge
576
&& grub_iso9660_susp_iterate (dir->data,
577
(dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
579
/ GRUB_DISK_SECTOR_SIZE),
580
sua_off % GRUB_DISK_SECTOR_SIZE,
581
sua_size, susp_iterate_dir))
585
if (grub_disk_read (dir->data->disk,
586
(dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
587
+ nameoffset / GRUB_DISK_SECTOR_SIZE,
588
nameoffset % GRUB_DISK_SECTOR_SIZE,
589
dirent.namelen, (char *) name))
592
node = grub_malloc (sizeof (struct grub_fshelp_node));
596
/* Setup a new node. */
597
node->data = dir->data;
598
node->size = grub_le_to_cpu32 (dirent.size);
599
node->blk = grub_le_to_cpu32 (dirent.first_sector);
600
node->dir_blk = ((dir->blk << GRUB_ISO9660_LOG2_BLKSZ)
601
+ offset / GRUB_DISK_SECTOR_SIZE);
602
node->dir_off = offset % GRUB_DISK_SECTOR_SIZE;
604
/* If the filetype was not stored using rockridge, use
605
whatever is stored in the iso9660 filesystem. */
606
if (type == GRUB_FSHELP_UNKNOWN)
608
if ((dirent.flags & 3) == 2)
609
type = GRUB_FSHELP_DIR;
611
type = GRUB_FSHELP_REG;
614
/* The filename was not stored in a rock ridge entry. Read it
615
from the iso9660 filesystem. */
618
name[dirent.namelen] = '\0';
619
filename = grub_strrchr (name, ';');
623
if (dirent.namelen == 1 && name[0] == 0)
625
else if (dirent.namelen == 1 && name[0] == 1)
631
if (dir->data->joliet)
636
filename = grub_iso9660_convert_string
637
((grub_uint16_t *) oldname, dirent.namelen >> 1);
645
if (hook (filename, type, node))
648
grub_free (filename);
652
grub_free (filename);
655
offset += dirent.len;
664
grub_iso9660_dir (grub_device_t device, const char *path,
665
int (*hook) (const char *filename,
666
const struct grub_dirhook_info *info))
668
struct grub_iso9660_data *data = 0;
669
struct grub_fshelp_node rootnode;
670
struct grub_fshelp_node *foundnode;
672
auto int NESTED_FUNC_ATTR iterate (const char *filename,
673
enum grub_fshelp_filetype filetype,
674
grub_fshelp_node_t node);
676
int NESTED_FUNC_ATTR iterate (const char *filename,
677
enum grub_fshelp_filetype filetype,
678
grub_fshelp_node_t node)
680
struct grub_dirhook_info info;
681
grub_memset (&info, 0, sizeof (info));
682
info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
684
return hook (filename, &info);
687
grub_dl_ref (my_mod);
689
data = grub_iso9660_mount (device->disk);
693
rootnode.data = data;
694
rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
695
rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
697
/* Use the fshelp function to traverse the path. */
698
if (grub_fshelp_find_file (path, &rootnode,
700
grub_iso9660_iterate_dir,
701
grub_iso9660_read_symlink,
705
/* List the files in the directory. */
706
grub_iso9660_iterate_dir (foundnode, iterate);
708
if (foundnode != &rootnode)
709
grub_free (foundnode);
714
grub_dl_unref (my_mod);
720
/* Open a file named NAME and initialize FILE. */
722
grub_iso9660_open (struct grub_file *file, const char *name)
724
struct grub_iso9660_data *data;
725
struct grub_fshelp_node rootnode;
726
struct grub_fshelp_node *foundnode;
728
grub_dl_ref (my_mod);
730
data = grub_iso9660_mount (file->device->disk);
734
rootnode.data = data;
735
rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector);
736
rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size);
738
/* Use the fshelp function to traverse the path. */
739
if (grub_fshelp_find_file (name, &rootnode,
741
grub_iso9660_iterate_dir,
742
grub_iso9660_read_symlink,
746
data->first_sector = foundnode->blk;
747
data->length = foundnode->size;
750
file->size = foundnode->size;
756
grub_dl_unref (my_mod);
765
grub_iso9660_read (grub_file_t file, char *buf, grub_size_t len)
767
struct grub_iso9660_data *data =
768
(struct grub_iso9660_data *) file->data;
770
/* XXX: The file is stored in as a single extent. */
771
data->disk->read_hook = file->read_hook;
772
grub_disk_read (data->disk,
773
data->first_sector << GRUB_ISO9660_LOG2_BLKSZ,
776
data->disk->read_hook = NULL;
786
grub_iso9660_close (grub_file_t file)
788
grub_free (file->data);
790
grub_dl_unref (my_mod);
792
return GRUB_ERR_NONE;
797
grub_iso9660_label (grub_device_t device, char **label)
799
struct grub_iso9660_data *data;
800
data = grub_iso9660_mount (device->disk);
805
*label = grub_iso9660_convert_string
806
((grub_uint16_t *) &data->voldesc.volname, 16);
808
*label = grub_strndup ((char *) data->voldesc.volname, 32);
819
grub_iso9660_uuid (grub_device_t device, char **uuid)
821
struct grub_iso9660_data *data;
822
grub_disk_t disk = device->disk;
824
grub_dl_ref (my_mod);
826
data = grub_iso9660_mount (disk);
829
if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1]
830
&& ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3]
831
&& ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1]
832
&& ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1]
833
&& ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1]
834
&& ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1]
835
&& ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1]
836
&& ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1])
838
grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID");
843
*uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c",
844
data->voldesc.modified.year[0],
845
data->voldesc.modified.year[1],
846
data->voldesc.modified.year[2],
847
data->voldesc.modified.year[3],
848
data->voldesc.modified.month[0],
849
data->voldesc.modified.month[1],
850
data->voldesc.modified.day[0],
851
data->voldesc.modified.day[1],
852
data->voldesc.modified.hour[0],
853
data->voldesc.modified.hour[1],
854
data->voldesc.modified.minute[0],
855
data->voldesc.modified.minute[1],
856
data->voldesc.modified.second[0],
857
data->voldesc.modified.second[1],
858
data->voldesc.modified.hundredth[0],
859
data->voldesc.modified.hundredth[1]);
865
grub_dl_unref (my_mod);
874
static struct grub_fs grub_iso9660_fs =
877
.dir = grub_iso9660_dir,
878
.open = grub_iso9660_open,
879
.read = grub_iso9660_read,
880
.close = grub_iso9660_close,
881
.label = grub_iso9660_label,
882
.uuid = grub_iso9660_uuid,
886
GRUB_MOD_INIT(iso9660)
888
grub_fs_register (&grub_iso9660_fs);
892
GRUB_MOD_FINI(iso9660)
894
grub_fs_unregister (&grub_iso9660_fs);