2N/A/* iso9660.c - iso9660 implementation with extensions:
2N/A SUSP, Rock Ridge. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/file.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/types.h>
2N/A#include <grub/fshelp.h>
2N/A#include <grub/charset.h>
2N/A#include <grub/datetime.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_ISO9660_FSTYPE_DIR 0040000
2N/A#define GRUB_ISO9660_FSTYPE_REG 0100000
2N/A#define GRUB_ISO9660_FSTYPE_SYMLINK 0120000
2N/A#define GRUB_ISO9660_FSTYPE_MASK 0170000
2N/A
2N/A#define GRUB_ISO9660_LOG2_BLKSZ 2
2N/A#define GRUB_ISO9660_BLKSZ 2048
2N/A
2N/A#define GRUB_ISO9660_RR_DOT 2
2N/A#define GRUB_ISO9660_RR_DOTDOT 4
2N/A
2N/A#define GRUB_ISO9660_VOLDESC_BOOT 0
2N/A#define GRUB_ISO9660_VOLDESC_PRIMARY 1
2N/A#define GRUB_ISO9660_VOLDESC_SUPP 2
2N/A#define GRUB_ISO9660_VOLDESC_PART 3
2N/A#define GRUB_ISO9660_VOLDESC_END 255
2N/A
2N/A/* The head of a volume descriptor. */
2N/Astruct grub_iso9660_voldesc
2N/A{
2N/A grub_uint8_t type;
2N/A grub_uint8_t magic[5];
2N/A grub_uint8_t version;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_iso9660_date2
2N/A{
2N/A grub_uint8_t year;
2N/A grub_uint8_t month;
2N/A grub_uint8_t day;
2N/A grub_uint8_t hour;
2N/A grub_uint8_t minute;
2N/A grub_uint8_t second;
2N/A grub_uint8_t offset;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A directory entry. */
2N/Astruct grub_iso9660_dir
2N/A{
2N/A grub_uint8_t len;
2N/A grub_uint8_t ext_sectors;
2N/A grub_uint32_t first_sector;
2N/A grub_uint32_t first_sector_be;
2N/A grub_uint32_t size;
2N/A grub_uint32_t size_be;
2N/A struct grub_iso9660_date2 mtime;
2N/A grub_uint8_t flags;
2N/A grub_uint8_t unused2[6];
2N/A grub_uint8_t namelen;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_iso9660_date
2N/A{
2N/A grub_uint8_t year[4];
2N/A grub_uint8_t month[2];
2N/A grub_uint8_t day[2];
2N/A grub_uint8_t hour[2];
2N/A grub_uint8_t minute[2];
2N/A grub_uint8_t second[2];
2N/A grub_uint8_t hundredth[2];
2N/A grub_uint8_t offset;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The primary volume descriptor. Only little endian is used. */
2N/Astruct grub_iso9660_primary_voldesc
2N/A{
2N/A struct grub_iso9660_voldesc voldesc;
2N/A grub_uint8_t unused1[33];
2N/A grub_uint8_t volname[32];
2N/A grub_uint8_t unused2[16];
2N/A grub_uint8_t escape[32];
2N/A grub_uint8_t unused3[12];
2N/A grub_uint32_t path_table_size;
2N/A grub_uint8_t unused4[4];
2N/A grub_uint32_t path_table;
2N/A grub_uint8_t unused5[12];
2N/A struct grub_iso9660_dir rootdir;
2N/A grub_uint8_t unused6[624];
2N/A struct grub_iso9660_date created;
2N/A struct grub_iso9660_date modified;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A single entry in the path table. */
2N/Astruct grub_iso9660_path
2N/A{
2N/A grub_uint8_t len;
2N/A grub_uint8_t sectors;
2N/A grub_uint32_t first_sector;
2N/A grub_uint16_t parentdir;
2N/A grub_uint8_t name[0];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* An entry in the System Usage area of the directory entry. */
2N/Astruct grub_iso9660_susp_entry
2N/A{
2N/A grub_uint8_t sig[2];
2N/A grub_uint8_t len;
2N/A grub_uint8_t version;
2N/A grub_uint8_t data[0];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The CE entry. This is used to describe the next block where data
2N/A can be found. */
2N/Astruct grub_iso9660_susp_ce
2N/A{
2N/A struct grub_iso9660_susp_entry entry;
2N/A grub_uint32_t blk;
2N/A grub_uint32_t blk_be;
2N/A grub_uint32_t off;
2N/A grub_uint32_t off_be;
2N/A grub_uint32_t len;
2N/A grub_uint32_t len_be;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_iso9660_data
2N/A{
2N/A struct grub_iso9660_primary_voldesc voldesc;
2N/A grub_disk_t disk;
2N/A int rockridge;
2N/A int susp_skip;
2N/A int joliet;
2N/A struct grub_fshelp_node *node;
2N/A};
2N/A
2N/Astruct grub_fshelp_node
2N/A{
2N/A struct grub_iso9660_data *data;
2N/A grub_size_t have_dirents, alloc_dirents;
2N/A int have_symlink;
2N/A struct grub_iso9660_dir dirents[8];
2N/A char symlink[0];
2N/A};
2N/A
2N/Aenum
2N/A {
2N/A FLAG_TYPE_PLAIN = 0,
2N/A FLAG_TYPE_DIR = 2,
2N/A FLAG_TYPE = 3,
2N/A FLAG_MORE_EXTENTS = 0x80
2N/A };
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/A
2N/Astatic grub_err_t
2N/Aiso9660_to_unixtime (const struct grub_iso9660_date *i, grub_int32_t *nix)
2N/A{
2N/A struct grub_datetime datetime;
2N/A
2N/A if (! i->year[0] && ! i->year[1]
2N/A && ! i->year[2] && ! i->year[3]
2N/A && ! i->month[0] && ! i->month[1]
2N/A && ! i->day[0] && ! i->day[1]
2N/A && ! i->hour[0] && ! i->hour[1]
2N/A && ! i->minute[0] && ! i->minute[1]
2N/A && ! i->second[0] && ! i->second[1]
2N/A && ! i->hundredth[0] && ! i->hundredth[1])
2N/A return grub_error (GRUB_ERR_BAD_NUMBER, "empty date");
2N/A datetime.year = (i->year[0] - '0') * 1000 + (i->year[1] - '0') * 100
2N/A + (i->year[2] - '0') * 10 + (i->year[3] - '0');
2N/A datetime.month = (i->month[0] - '0') * 10 + (i->month[1] - '0');
2N/A datetime.day = (i->day[0] - '0') * 10 + (i->day[1] - '0');
2N/A datetime.hour = (i->hour[0] - '0') * 10 + (i->hour[1] - '0');
2N/A datetime.minute = (i->minute[0] - '0') * 10 + (i->minute[1] - '0');
2N/A datetime.second = (i->second[0] - '0') * 10 + (i->second[1] - '0');
2N/A
2N/A if (!grub_datetime2unixtime (&datetime, nix))
2N/A return grub_error (GRUB_ERR_BAD_NUMBER, "incorrect date");
2N/A *nix -= i->offset * 60 * 15;
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic int
2N/Aiso9660_to_unixtime2 (const struct grub_iso9660_date2 *i, grub_int32_t *nix)
2N/A{
2N/A struct grub_datetime datetime;
2N/A
2N/A datetime.year = i->year + 1900;
2N/A datetime.month = i->month;
2N/A datetime.day = i->day;
2N/A datetime.hour = i->hour;
2N/A datetime.minute = i->minute;
2N/A datetime.second = i->second;
2N/A
2N/A if (!grub_datetime2unixtime (&datetime, nix))
2N/A return 0;
2N/A *nix -= i->offset * 60 * 15;
2N/A return 1;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Aread_node (grub_fshelp_node_t node, grub_off_t off, grub_size_t len, char *buf)
2N/A{
2N/A grub_size_t i = 0;
2N/A
2N/A while (len > 0)
2N/A {
2N/A grub_size_t toread;
2N/A grub_err_t err;
2N/A while (i < node->have_dirents
2N/A && off >= grub_le_to_cpu32 (node->dirents[i].size))
2N/A {
2N/A off -= grub_le_to_cpu32 (node->dirents[i].size);
2N/A i++;
2N/A }
2N/A if (i == node->have_dirents)
2N/A return grub_error (GRUB_ERR_OUT_OF_RANGE, "read out of range");
2N/A toread = grub_le_to_cpu32 (node->dirents[i].size);
2N/A if (toread > len)
2N/A toread = len;
2N/A err = grub_disk_read (node->data->disk,
2N/A ((grub_disk_addr_t) grub_le_to_cpu32 (node->dirents[i].first_sector)) << GRUB_ISO9660_LOG2_BLKSZ,
2N/A off, toread, buf);
2N/A if (err)
2N/A return err;
2N/A len -= toread;
2N/A off += toread;
2N/A buf += toread;
2N/A }
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Iterate over the susp entries, starting with block SUA_BLOCK on the
2N/A offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for
2N/A every entry. */
2N/Astatic grub_err_t
2N/Agrub_iso9660_susp_iterate (grub_fshelp_node_t node, grub_off_t off,
2N/A grub_ssize_t sua_size,
2N/A grub_err_t (*hook)
2N/A (struct grub_iso9660_susp_entry *entry))
2N/A{
2N/A char *sua;
2N/A struct grub_iso9660_susp_entry *entry;
2N/A grub_disk_addr_t ce_block;
2N/A int is_ce = 0;
2N/A
2N/A auto grub_err_t load_sua (void);
2N/A
2N/A /* Load a part of the System Usage Area. */
2N/A grub_err_t load_sua (void)
2N/A {
2N/A grub_err_t err;
2N/A sua = grub_malloc (sua_size);
2N/A if (!sua)
2N/A return grub_errno;
2N/A
2N/A if (is_ce)
2N/A err = grub_disk_read (node->data->disk, ce_block, off,
2N/A sua_size, sua);
2N/A else
2N/A err = read_node (node, off, sua_size, sua);
2N/A if (err)
2N/A return err;
2N/A
2N/A entry = (struct grub_iso9660_susp_entry *) sua;
2N/A return 0;
2N/A }
2N/A
2N/A if (sua_size <= 0)
2N/A return GRUB_ERR_NONE;
2N/A
2N/A if (load_sua ())
2N/A return grub_errno;
2N/A
2N/A for (; (char *) entry < (char *) sua + sua_size - 1;
2N/A entry = (struct grub_iso9660_susp_entry *)
2N/A ((char *) entry + entry->len))
2N/A {
2N/A /* The last entry. */
2N/A if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0)
2N/A break;
2N/A
2N/A /* Additional entries are stored elsewhere. */
2N/A if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0)
2N/A {
2N/A struct grub_iso9660_susp_ce *ce;
2N/A
2N/A is_ce = 1;
2N/A ce = (struct grub_iso9660_susp_ce *) entry;
2N/A sua_size = grub_le_to_cpu32 (ce->len);
2N/A off = grub_le_to_cpu32 (ce->off);
2N/A ce_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ;
2N/A
2N/A grub_free (sua);
2N/A if (load_sua ())
2N/A return grub_errno;
2N/A }
2N/A
2N/A if (hook (entry))
2N/A {
2N/A grub_free (sua);
2N/A return 0;
2N/A }
2N/A }
2N/A
2N/A grub_free (sua);
2N/A return 0;
2N/A}
2N/A
2N/Astatic char *
2N/Agrub_iso9660_convert_string (grub_uint8_t *us, int len)
2N/A{
2N/A char *p;
2N/A int i;
2N/A grub_uint16_t t[len];
2N/A
2N/A p = grub_malloc (len * GRUB_MAX_UTF8_PER_UTF16 + 1);
2N/A if (! p)
2N/A return p;
2N/A
2N/A for (i=0; i<len; i++)
2N/A t[i] = grub_be_to_cpu16 (grub_get_unaligned16 (us + 2 * i));
2N/A
2N/A *grub_utf16_to_utf8 ((grub_uint8_t *) p, t, len) = '\0';
2N/A
2N/A return p;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Aset_rockridge (struct grub_iso9660_data *data)
2N/A{
2N/A int sua_pos;
2N/A int sua_size;
2N/A char *sua;
2N/A struct grub_iso9660_dir rootdir;
2N/A struct grub_iso9660_susp_entry *entry;
2N/A
2N/A auto grub_err_t susp_iterate (struct grub_iso9660_susp_entry *);
2N/A
2N/A grub_err_t susp_iterate (struct grub_iso9660_susp_entry *susp_entry)
2N/A {
2N/A /* The "ER" entry is used to detect extensions. The
2N/A `IEEE_P1285' extension means Rock ridge. */
2N/A if (grub_strncmp ((char *) susp_entry->sig, "ER", 2) == 0)
2N/A {
2N/A data->rockridge = 1;
2N/A return 1;
2N/A }
2N/A return 0;
2N/A }
2N/A
2N/A data->rockridge = 0;
2N/A
2N/A /* Read the system use area and test it to see if SUSP is
2N/A supported. */
2N/A if (grub_disk_read (data->disk,
2N/A (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
2N/A << GRUB_ISO9660_LOG2_BLKSZ), 0,
2N/A sizeof (rootdir), (char *) &rootdir))
2N/A return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
2N/A
2N/A sua_pos = (sizeof (rootdir) + rootdir.namelen
2N/A + (rootdir.namelen % 2) - 1);
2N/A sua_size = rootdir.len - sua_pos;
2N/A
2N/A if (!sua_size)
2N/A return GRUB_ERR_NONE;
2N/A
2N/A sua = grub_malloc (sua_size);
2N/A if (! sua)
2N/A return grub_errno;
2N/A
2N/A if (grub_disk_read (data->disk,
2N/A (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector)
2N/A << GRUB_ISO9660_LOG2_BLKSZ), sua_pos,
2N/A sua_size, sua))
2N/A {
2N/A grub_free (sua);
2N/A return grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
2N/A }
2N/A
2N/A entry = (struct grub_iso9660_susp_entry *) sua;
2N/A
2N/A /* Test if the SUSP protocol is used on this filesystem. */
2N/A if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0)
2N/A {
2N/A struct grub_fshelp_node rootnode;
2N/A
2N/A rootnode.data = data;
2N/A rootnode.alloc_dirents = ARRAY_SIZE (rootnode.dirents);
2N/A rootnode.have_dirents = 1;
2N/A rootnode.have_symlink = 0;
2N/A rootnode.dirents[0] = data->voldesc.rootdir;
2N/A
2N/A /* The 2nd data byte stored how many bytes are skipped every time
2N/A to get to the SUA (System Usage Area). */
2N/A data->susp_skip = entry->data[2];
2N/A entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len);
2N/A
2N/A /* Iterate over the entries in the SUA area to detect
2N/A extensions. */
2N/A if (grub_iso9660_susp_iterate (&rootnode,
2N/A sua_pos, sua_size, susp_iterate))
2N/A {
2N/A grub_free (sua);
2N/A return grub_errno;
2N/A }
2N/A }
2N/A grub_free (sua);
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic struct grub_iso9660_data *
2N/Agrub_iso9660_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_iso9660_data *data = 0;
2N/A struct grub_iso9660_primary_voldesc voldesc;
2N/A int block;
2N/A
2N/A data = grub_zalloc (sizeof (struct grub_iso9660_data));
2N/A if (! data)
2N/A return 0;
2N/A
2N/A data->disk = disk;
2N/A
2N/A block = 16;
2N/A do
2N/A {
2N/A int copy_voldesc = 0;
2N/A
2N/A /* Read the superblock. */
2N/A if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0,
2N/A sizeof (struct grub_iso9660_primary_voldesc),
2N/A (char *) &voldesc))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY)
2N/A copy_voldesc = 1;
2N/A else if (!data->rockridge
2N/A && (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP)
2N/A && (voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f)
2N/A &&
2N/A ((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */
2N/A (voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */
2N/A (voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */
2N/A {
2N/A copy_voldesc = 1;
2N/A data->joliet = 1;
2N/A }
2N/A
2N/A if (copy_voldesc)
2N/A {
2N/A grub_memcpy((char *) &data->voldesc, (char *) &voldesc,
2N/A sizeof (struct grub_iso9660_primary_voldesc));
2N/A if (set_rockridge (data))
2N/A goto fail;
2N/A }
2N/A
2N/A block++;
2N/A } while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END);
2N/A
2N/A return data;
2N/A
2N/A fail:
2N/A grub_free (data);
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic char *
2N/Agrub_iso9660_read_symlink (grub_fshelp_node_t node)
2N/A{
2N/A return node->have_symlink
2N/A ? grub_strdup (node->symlink
2N/A + (node->have_dirents) * sizeof (node->dirents[0])
2N/A - sizeof (node->dirents)) : grub_strdup ("");
2N/A}
2N/A
2N/Astatic grub_off_t
2N/Aget_node_size (grub_fshelp_node_t node)
2N/A{
2N/A grub_off_t ret = 0;
2N/A grub_size_t i;
2N/A
2N/A for (i = 0; i < node->have_dirents; i++)
2N/A ret += grub_le_to_cpu32 (node->dirents[i].size);
2N/A return ret;
2N/A}
2N/A
2N/Astatic int
2N/Agrub_iso9660_iterate_dir (grub_fshelp_node_t dir,
2N/A int NESTED_FUNC_ATTR
2N/A (*hook) (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node))
2N/A{
2N/A struct grub_iso9660_dir dirent;
2N/A grub_off_t offset = 0;
2N/A char *filename = 0;
2N/A int filename_alloc = 0;
2N/A enum grub_fshelp_filetype type;
2N/A grub_off_t len;
2N/A char *symlink = 0;
2N/A
2N/A /* Extend the symlink. */
2N/A auto inline void __attribute__ ((always_inline)) add_part (const char *part,
2N/A int len2);
2N/A
2N/A auto inline void __attribute__ ((always_inline)) add_part (const char *part,
2N/A int len2)
2N/A {
2N/A int size = symlink ? grub_strlen (symlink) : 0;
2N/A
2N/A symlink = grub_realloc (symlink, size + len2 + 1);
2N/A if (! symlink)
2N/A return;
2N/A
2N/A symlink[size] = 0;
2N/A grub_strncat (symlink, part, len2);
2N/A }
2N/A
2N/A auto grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *);
2N/A
2N/A grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *entry)
2N/A {
2N/A /* The filename in the rock ridge entry. */
2N/A if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0)
2N/A {
2N/A /* The flags are stored at the data position 0, here the
2N/A filename type is stored. */
2N/A if (entry->data[0] & GRUB_ISO9660_RR_DOT)
2N/A filename = ".";
2N/A else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT)
2N/A filename = "..";
2N/A else if (entry->len >= 5)
2N/A {
2N/A grub_size_t size = 1, csize = 1;
2N/A char *old;
2N/A csize = size = entry->len - 5;
2N/A old = filename;
2N/A if (filename_alloc)
2N/A {
2N/A size += grub_strlen (filename);
2N/A filename = grub_realloc (filename, size + 1);
2N/A }
2N/A else
2N/A {
2N/A filename_alloc = 1;
2N/A filename = grub_zalloc (size + 1);
2N/A filename[0] = 0;
2N/A }
2N/A if (!filename)
2N/A {
2N/A filename = old;
2N/A return grub_errno;
2N/A }
2N/A filename_alloc = 1;
2N/A grub_strncat (filename, (char *) &entry->data[1], csize);
2N/A filename[size] = '\0';
2N/A }
2N/A }
2N/A /* The mode information (st_mode). */
2N/A else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0)
2N/A {
2N/A /* At position 0 of the PX record the st_mode information is
2N/A stored (little-endian). */
2N/A grub_uint32_t mode = ((entry->data[0] + (entry->data[1] << 8))
2N/A & GRUB_ISO9660_FSTYPE_MASK);
2N/A
2N/A switch (mode)
2N/A {
2N/A case GRUB_ISO9660_FSTYPE_DIR:
2N/A type = GRUB_FSHELP_DIR;
2N/A break;
2N/A case GRUB_ISO9660_FSTYPE_REG:
2N/A type = GRUB_FSHELP_REG;
2N/A break;
2N/A case GRUB_ISO9660_FSTYPE_SYMLINK:
2N/A type = GRUB_FSHELP_SYMLINK;
2N/A break;
2N/A default:
2N/A type = GRUB_FSHELP_UNKNOWN;
2N/A }
2N/A }
2N/A else if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0)
2N/A {
2N/A unsigned int pos = 1;
2N/A
2N/A /* The symlink is not stored as a POSIX symlink, translate it. */
2N/A while (pos + sizeof (*entry) < grub_le_to_cpu32 (entry->len))
2N/A {
2N/A /* The current position is the `Component Flag'. */
2N/A switch (entry->data[pos] & 30)
2N/A {
2N/A case 0:
2N/A {
2N/A /* The data on pos + 2 is the actual data, pos + 1
2N/A is the length. Both are part of the `Component
2N/A Record'. */
2N/A if (symlink && (entry->data[pos] & 1))
2N/A add_part ("/", 1);
2N/A add_part ((char *) &entry->data[pos + 2],
2N/A entry->data[pos + 1]);
2N/A
2N/A break;
2N/A }
2N/A
2N/A case 2:
2N/A add_part ("./", 2);
2N/A break;
2N/A
2N/A case 4:
2N/A add_part ("../", 3);
2N/A break;
2N/A
2N/A case 8:
2N/A add_part ("/", 1);
2N/A break;
2N/A }
2N/A /* In pos + 1 the length of the `Component Record' is
2N/A stored. */
2N/A pos += entry->data[pos + 1] + 2;
2N/A }
2N/A
2N/A /* Check if `grub_realloc' failed. */
2N/A if (grub_errno)
2N/A return grub_errno;
2N/A }
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A len = get_node_size (dir);
2N/A
2N/A for (; offset < len; offset += dirent.len)
2N/A {
2N/A symlink = 0;
2N/A
2N/A if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
2N/A return 0;
2N/A
2N/A /* The end of the block, skip to the next one. */
2N/A if (!dirent.len)
2N/A {
2N/A offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ;
2N/A continue;
2N/A }
2N/A
2N/A {
2N/A char name[dirent.namelen + 1];
2N/A int nameoffset = offset + sizeof (dirent);
2N/A struct grub_fshelp_node *node;
2N/A int sua_off = (sizeof (dirent) + dirent.namelen + 1
2N/A - (dirent.namelen % 2));
2N/A int sua_size = dirent.len - sua_off;
2N/A
2N/A sua_off += offset + dir->data->susp_skip;
2N/A
2N/A filename = 0;
2N/A filename_alloc = 0;
2N/A type = GRUB_FSHELP_UNKNOWN;
2N/A
2N/A if (dir->data->rockridge
2N/A && grub_iso9660_susp_iterate (dir, sua_off, sua_size,
2N/A susp_iterate_dir))
2N/A return 0;
2N/A
2N/A /* Read the name. */
2N/A if (read_node (dir, nameoffset, dirent.namelen, (char *) name))
2N/A return 0;
2N/A
2N/A node = grub_malloc (sizeof (struct grub_fshelp_node));
2N/A if (!node)
2N/A return 0;
2N/A
2N/A node->alloc_dirents = ARRAY_SIZE (node->dirents);
2N/A node->have_dirents = 1;
2N/A
2N/A /* Setup a new node. */
2N/A node->data = dir->data;
2N/A node->have_symlink = 0;
2N/A
2N/A /* If the filetype was not stored using rockridge, use
2N/A whatever is stored in the iso9660 filesystem. */
2N/A if (type == GRUB_FSHELP_UNKNOWN)
2N/A {
2N/A if ((dirent.flags & FLAG_TYPE) == FLAG_TYPE_DIR)
2N/A type = GRUB_FSHELP_DIR;
2N/A else
2N/A type = GRUB_FSHELP_REG;
2N/A }
2N/A
2N/A /* The filename was not stored in a rock ridge entry. Read it
2N/A from the iso9660 filesystem. */
2N/A if (!filename)
2N/A {
2N/A name[dirent.namelen] = '\0';
2N/A filename = grub_strrchr (name, ';');
2N/A if (filename)
2N/A *filename = '\0';
2N/A
2N/A /* . and .. */
2N/A if (dirent.namelen == 1 && (name[0] == 0 || name[0] == 1))
2N/A {
2N/A grub_free (node);
2N/A continue;
2N/A }
2N/A else
2N/A filename = name;
2N/A }
2N/A
2N/A if (dir->data->joliet)
2N/A {
2N/A char *oldname, *semicolon;
2N/A
2N/A oldname = filename;
2N/A filename = grub_iso9660_convert_string
2N/A ((grub_uint8_t *) oldname, dirent.namelen >> 1);
2N/A
2N/A semicolon = grub_strrchr (filename, ';');
2N/A if (semicolon)
2N/A *semicolon = '\0';
2N/A
2N/A if (filename_alloc)
2N/A grub_free (oldname);
2N/A
2N/A filename_alloc = 1;
2N/A }
2N/A
2N/A node->dirents[0] = dirent;
2N/A while (dirent.flags & FLAG_MORE_EXTENTS)
2N/A {
2N/A offset += dirent.len;
2N/A if (read_node (dir, offset, sizeof (dirent), (char *) &dirent))
2N/A {
2N/A if (filename_alloc)
2N/A grub_free (filename);
2N/A grub_free (node);
2N/A return 0;
2N/A }
2N/A if (node->have_dirents >= node->alloc_dirents)
2N/A {
2N/A struct grub_fshelp_node *new_node;
2N/A node->alloc_dirents *= 2;
2N/A new_node = grub_realloc (node,
2N/A sizeof (struct grub_fshelp_node)
2N/A + ((node->alloc_dirents
2N/A - ARRAY_SIZE (node->dirents))
2N/A * sizeof (node->dirents[0])));
2N/A if (!new_node)
2N/A {
2N/A if (filename_alloc)
2N/A grub_free (filename);
2N/A grub_free (node);
2N/A return 0;
2N/A }
2N/A node = new_node;
2N/A }
2N/A node->dirents[node->have_dirents++] = dirent;
2N/A }
2N/A if (symlink)
2N/A {
2N/A if ((node->alloc_dirents - node->have_dirents)
2N/A * sizeof (node->dirents[0]) < grub_strlen (symlink) + 1)
2N/A {
2N/A struct grub_fshelp_node *new_node;
2N/A new_node = grub_realloc (node,
2N/A sizeof (struct grub_fshelp_node)
2N/A + ((node->alloc_dirents
2N/A - ARRAY_SIZE (node->dirents))
2N/A * sizeof (node->dirents[0]))
2N/A + grub_strlen (symlink) + 1);
2N/A if (!new_node)
2N/A {
2N/A if (filename_alloc)
2N/A grub_free (filename);
2N/A grub_free (node);
2N/A return 0;
2N/A }
2N/A node = new_node;
2N/A }
2N/A node->have_symlink = 1;
2N/A grub_strcpy (node->symlink
2N/A + node->have_dirents * sizeof (node->dirents[0])
2N/A - sizeof (node->dirents), symlink);
2N/A grub_free (symlink);
2N/A }
2N/A if (hook (filename, type, node))
2N/A {
2N/A if (filename_alloc)
2N/A grub_free (filename);
2N/A return 1;
2N/A }
2N/A if (filename_alloc)
2N/A grub_free (filename);
2N/A }
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_iso9660_dir (grub_device_t device, const char *path,
2N/A int (*hook) (const char *filename,
2N/A const struct grub_dirhook_info *info))
2N/A{
2N/A struct grub_iso9660_data *data = 0;
2N/A struct grub_fshelp_node rootnode;
2N/A struct grub_fshelp_node *foundnode;
2N/A
2N/A auto int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node);
2N/A
2N/A int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node)
2N/A {
2N/A struct grub_dirhook_info info;
2N/A grub_memset (&info, 0, sizeof (info));
2N/A info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
2N/A info.mtimeset = !!iso9660_to_unixtime2 (&node->dirents[0].mtime, &info.mtime);
2N/A
2N/A grub_free (node);
2N/A return hook (filename, &info);
2N/A }
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_iso9660_mount (device->disk);
2N/A if (! data)
2N/A goto fail;
2N/A
2N/A rootnode.data = data;
2N/A rootnode.alloc_dirents = 0;
2N/A rootnode.have_dirents = 1;
2N/A rootnode.have_symlink = 0;
2N/A rootnode.dirents[0] = data->voldesc.rootdir;
2N/A
2N/A /* Use the fshelp function to traverse the path. */
2N/A if (grub_fshelp_find_file (path, &rootnode,
2N/A &foundnode,
2N/A grub_iso9660_iterate_dir,
2N/A grub_iso9660_read_symlink,
2N/A GRUB_FSHELP_DIR))
2N/A goto fail;
2N/A
2N/A /* List the files in the directory. */
2N/A grub_iso9660_iterate_dir (foundnode, iterate);
2N/A
2N/A if (foundnode != &rootnode)
2N/A grub_free (foundnode);
2N/A
2N/A fail:
2N/A grub_free (data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/A/* Open a file named NAME and initialize FILE. */
2N/Astatic grub_err_t
2N/Agrub_iso9660_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_iso9660_data *data;
2N/A struct grub_fshelp_node rootnode;
2N/A struct grub_fshelp_node *foundnode;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_iso9660_mount (file->device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A rootnode.data = data;
2N/A rootnode.alloc_dirents = 0;
2N/A rootnode.have_dirents = 1;
2N/A rootnode.have_symlink = 0;
2N/A rootnode.dirents[0] = data->voldesc.rootdir;
2N/A
2N/A /* Use the fshelp function to traverse the path. */
2N/A if (grub_fshelp_find_file (name, &rootnode,
2N/A &foundnode,
2N/A grub_iso9660_iterate_dir,
2N/A grub_iso9660_read_symlink,
2N/A GRUB_FSHELP_REG))
2N/A goto fail;
2N/A
2N/A data->node = foundnode;
2N/A file->data = data;
2N/A file->size = get_node_size (foundnode);
2N/A file->offset = 0;
2N/A
2N/A return 0;
2N/A
2N/A fail:
2N/A grub_dl_unref (my_mod);
2N/A
2N/A grub_free (data);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_iso9660_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_iso9660_data *data =
2N/A (struct grub_iso9660_data *) file->data;
2N/A
2N/A /* XXX: The file is stored in as a single extent. */
2N/A data->disk->read_hook = file->read_hook;
2N/A read_node (data->node, file->offset, len, buf);
2N/A data->disk->read_hook = NULL;
2N/A
2N/A if (grub_errno)
2N/A return -1;
2N/A
2N/A return len;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_iso9660_close (grub_file_t file)
2N/A{
2N/A struct grub_iso9660_data *data =
2N/A (struct grub_iso9660_data *) file->data;
2N/A grub_free (data->node);
2N/A grub_free (data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_iso9660_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_iso9660_data *data;
2N/A data = grub_iso9660_mount (device->disk);
2N/A
2N/A if (data)
2N/A {
2N/A if (data->joliet)
2N/A *label = grub_iso9660_convert_string (data->voldesc.volname, 16);
2N/A else
2N/A *label = grub_strndup ((char *) data->voldesc.volname, 32);
2N/A if (*label)
2N/A {
2N/A char *ptr;
2N/A for (ptr = *label; *ptr;ptr++);
2N/A ptr--;
2N/A while (ptr >= *label && *ptr == ' ')
2N/A *ptr-- = 0;
2N/A }
2N/A
2N/A grub_free (data);
2N/A }
2N/A else
2N/A *label = 0;
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_iso9660_uuid (grub_device_t device, char **uuid)
2N/A{
2N/A struct grub_iso9660_data *data;
2N/A grub_disk_t disk = device->disk;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_iso9660_mount (disk);
2N/A if (data)
2N/A {
2N/A if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1]
2N/A && ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3]
2N/A && ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1]
2N/A && ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1]
2N/A && ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1]
2N/A && ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1]
2N/A && ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1]
2N/A && ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1])
2N/A {
2N/A grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID");
2N/A *uuid = NULL;
2N/A }
2N/A else
2N/A {
2N/A *uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c",
2N/A data->voldesc.modified.year[0],
2N/A data->voldesc.modified.year[1],
2N/A data->voldesc.modified.year[2],
2N/A data->voldesc.modified.year[3],
2N/A data->voldesc.modified.month[0],
2N/A data->voldesc.modified.month[1],
2N/A data->voldesc.modified.day[0],
2N/A data->voldesc.modified.day[1],
2N/A data->voldesc.modified.hour[0],
2N/A data->voldesc.modified.hour[1],
2N/A data->voldesc.modified.minute[0],
2N/A data->voldesc.modified.minute[1],
2N/A data->voldesc.modified.second[0],
2N/A data->voldesc.modified.second[1],
2N/A data->voldesc.modified.hundredth[0],
2N/A data->voldesc.modified.hundredth[1]);
2N/A }
2N/A }
2N/A else
2N/A *uuid = NULL;
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A grub_free (data);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A/* Get writing time of filesystem. */
2N/Astatic grub_err_t
2N/Agrub_iso9660_mtime (grub_device_t device, grub_int32_t *timebuf)
2N/A{
2N/A struct grub_iso9660_data *data;
2N/A grub_disk_t disk = device->disk;
2N/A grub_err_t err;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_iso9660_mount (disk);
2N/A if (!data)
2N/A {
2N/A grub_dl_unref (my_mod);
2N/A return grub_errno;
2N/A }
2N/A err = iso9660_to_unixtime (&data->voldesc.modified, timebuf);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A grub_free (data);
2N/A
2N/A return err;
2N/A}
2N/A
2N/A
2N/A
2N/A
2N/Astatic struct grub_fs grub_iso9660_fs =
2N/A {
2N/A .name = "iso9660",
2N/A .dir = grub_iso9660_dir,
2N/A .open = grub_iso9660_open,
2N/A .read = grub_iso9660_read,
2N/A .close = grub_iso9660_close,
2N/A .label = grub_iso9660_label,
2N/A .uuid = grub_iso9660_uuid,
2N/A .mtime = grub_iso9660_mtime,
2N/A .next = 0
2N/A };
2N/A
2N/AGRUB_MOD_INIT(iso9660)
2N/A{
2N/A grub_fs_register (&grub_iso9660_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(iso9660)
2N/A{
2N/A grub_fs_unregister (&grub_iso9660_fs);
2N/A}