2N/A/* hfsplus.c - HFS+ Filesystem. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2005,2006,2007,2008,2009 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/* HFS+ is documented at http://developer.apple.com/technotes/tn/tn1150.html */
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/hfs.h>
2N/A#include <grub/charset.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_HFSPLUS_MAGIC 0x482B
2N/A#define GRUB_HFSPLUSX_MAGIC 0x4858
2N/A#define GRUB_HFSPLUS_SBLOCK 2
2N/A
2N/A/* A HFS+ extent. */
2N/Astruct grub_hfsplus_extent
2N/A{
2N/A /* The first block of a file on disk. */
2N/A grub_uint32_t start;
2N/A /* The amount of blocks described by this extent. */
2N/A grub_uint32_t count;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The descriptor of a fork. */
2N/Astruct grub_hfsplus_forkdata
2N/A{
2N/A grub_uint64_t size;
2N/A grub_uint32_t clumpsize;
2N/A grub_uint32_t blocks;
2N/A struct grub_hfsplus_extent extents[8];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The HFS+ Volume Header. */
2N/Astruct grub_hfsplus_volheader
2N/A{
2N/A grub_uint16_t magic;
2N/A grub_uint16_t version;
2N/A grub_uint32_t attributes;
2N/A grub_uint8_t unused1[12];
2N/A grub_uint32_t utime;
2N/A grub_uint8_t unused2[16];
2N/A grub_uint32_t blksize;
2N/A grub_uint8_t unused3[60];
2N/A grub_uint64_t num_serial;
2N/A struct grub_hfsplus_forkdata allocations_file;
2N/A struct grub_hfsplus_forkdata extents_file;
2N/A struct grub_hfsplus_forkdata catalog_file;
2N/A struct grub_hfsplus_forkdata attrib_file;
2N/A struct grub_hfsplus_forkdata startup_file;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The type of node. */
2N/Aenum grub_hfsplus_btnode_type
2N/A {
2N/A GRUB_HFSPLUS_BTNODE_TYPE_LEAF = -1,
2N/A GRUB_HFSPLUS_BTNODE_TYPE_INDEX = 0,
2N/A GRUB_HFSPLUS_BTNODE_TYPE_HEADER = 1,
2N/A GRUB_HFSPLUS_BTNODE_TYPE_MAP = 2,
2N/A };
2N/A
2N/Astruct grub_hfsplus_btnode
2N/A{
2N/A grub_uint32_t next;
2N/A grub_uint32_t prev;
2N/A grub_int8_t type;
2N/A grub_uint8_t height;
2N/A grub_uint16_t count;
2N/A grub_uint16_t unused;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The header of a HFS+ B+ Tree. */
2N/Astruct grub_hfsplus_btheader
2N/A{
2N/A grub_uint16_t depth;
2N/A grub_uint32_t root;
2N/A grub_uint32_t leaf_records;
2N/A grub_uint32_t first_leaf_node;
2N/A grub_uint32_t last_leaf_node;
2N/A grub_uint16_t nodesize;
2N/A grub_uint16_t keysize;
2N/A grub_uint32_t total_nodes;
2N/A grub_uint32_t free_nodes;
2N/A grub_uint16_t reserved1;
2N/A grub_uint32_t clump_size; /* ignored */
2N/A grub_uint8_t btree_type;
2N/A grub_uint8_t key_compare;
2N/A grub_uint32_t attributes;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The on disk layout of a catalog key. */
2N/Astruct grub_hfsplus_catkey
2N/A{
2N/A grub_uint16_t keylen;
2N/A grub_uint32_t parent;
2N/A grub_uint16_t namelen;
2N/A grub_uint16_t name[30];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The on disk layout of an extent overflow file key. */
2N/Astruct grub_hfsplus_extkey
2N/A{
2N/A grub_uint16_t keylen;
2N/A grub_uint8_t type;
2N/A grub_uint8_t unused;
2N/A grub_uint32_t fileid;
2N/A grub_uint32_t start;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_hfsplus_key
2N/A{
2N/A union
2N/A {
2N/A struct grub_hfsplus_extkey extkey;
2N/A struct grub_hfsplus_catkey catkey;
2N/A grub_uint16_t keylen;
2N/A };
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_hfsplus_catfile
2N/A{
2N/A grub_uint16_t type;
2N/A grub_uint16_t flags;
2N/A grub_uint32_t reserved;
2N/A grub_uint32_t fileid;
2N/A grub_uint8_t unused1[4];
2N/A grub_uint32_t mtime;
2N/A grub_uint8_t unused2[22];
2N/A grub_uint16_t mode;
2N/A grub_uint8_t unused3[44];
2N/A struct grub_hfsplus_forkdata data;
2N/A struct grub_hfsplus_forkdata resource;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* Filetype information as used in inodes. */
2N/A#define GRUB_HFSPLUS_FILEMODE_MASK 0170000
2N/A#define GRUB_HFSPLUS_FILEMODE_REG 0100000
2N/A#define GRUB_HFSPLUS_FILEMODE_DIRECTORY 0040000
2N/A#define GRUB_HFSPLUS_FILEMODE_SYMLINK 0120000
2N/A
2N/A/* Some pre-defined file IDs. */
2N/A#define GRUB_HFSPLUS_FILEID_ROOTDIR 2
2N/A#define GRUB_HFSPLUS_FILEID_OVERFLOW 3
2N/A#define GRUB_HFSPLUS_FILEID_CATALOG 4
2N/A
2N/Aenum grub_hfsplus_filetype
2N/A {
2N/A GRUB_HFSPLUS_FILETYPE_DIR = 1,
2N/A GRUB_HFSPLUS_FILETYPE_REG = 2,
2N/A GRUB_HFSPLUS_FILETYPE_DIR_THREAD = 3,
2N/A GRUB_HFSPLUS_FILETYPE_REG_THREAD = 4
2N/A };
2N/A
2N/A#define GRUB_HFSPLUSX_BINARYCOMPARE 0xBC
2N/A#define GRUB_HFSPLUSX_CASEFOLDING 0xCF
2N/A
2N/A/* Internal representation of a catalog key. */
2N/Astruct grub_hfsplus_catkey_internal
2N/A{
2N/A grub_uint32_t parent;
2N/A const grub_uint16_t *name;
2N/A grub_size_t namelen;
2N/A};
2N/A
2N/A/* Internal representation of an extent overflow key. */
2N/Astruct grub_hfsplus_extkey_internal
2N/A{
2N/A grub_uint32_t fileid;
2N/A grub_uint32_t start;
2N/A};
2N/A
2N/Astruct grub_hfsplus_key_internal
2N/A{
2N/A union
2N/A {
2N/A struct grub_hfsplus_extkey_internal extkey;
2N/A struct grub_hfsplus_catkey_internal catkey;
2N/A };
2N/A};
2N/A
2N/A
2N/A
2N/Astruct grub_fshelp_node
2N/A{
2N/A struct grub_hfsplus_data *data;
2N/A struct grub_hfsplus_extent extents[8];
2N/A grub_uint64_t size;
2N/A grub_uint32_t fileid;
2N/A grub_int32_t mtime;
2N/A};
2N/A
2N/Astruct grub_hfsplus_btree
2N/A{
2N/A grub_uint32_t root;
2N/A grub_size_t nodesize;
2N/A
2N/A /* Catalog file node. */
2N/A struct grub_fshelp_node file;
2N/A};
2N/A
2N/A/* Information about a "mounted" HFS+ filesystem. */
2N/Astruct grub_hfsplus_data
2N/A{
2N/A struct grub_hfsplus_volheader volheader;
2N/A grub_disk_t disk;
2N/A
2N/A unsigned int log2blksize;
2N/A
2N/A struct grub_hfsplus_btree catalog_tree;
2N/A struct grub_hfsplus_btree extoverflow_tree;
2N/A
2N/A struct grub_fshelp_node dirroot;
2N/A struct grub_fshelp_node opened_file;
2N/A
2N/A /* This is the offset into the physical disk for an embedded HFS+
2N/A filesystem (one inside a plain HFS wrapper). */
2N/A grub_disk_addr_t embedded_offset;
2N/A int case_sensitive;
2N/A};
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/A
2N/A/* Return the offset of the record with the index INDEX, in the node
2N/A NODE which is part of the B+ tree BTREE. */
2N/Astatic inline grub_off_t
2N/Agrub_hfsplus_btree_recoffset (struct grub_hfsplus_btree *btree,
2N/A struct grub_hfsplus_btnode *node, int index)
2N/A{
2N/A char *cnode = (char *) node;
2N/A void *recptr;
2N/A recptr = (&cnode[btree->nodesize - index * sizeof (grub_uint16_t) - 2]);
2N/A return grub_be_to_cpu16 (grub_get_unaligned16 (recptr));
2N/A}
2N/A
2N/A/* Return a pointer to the record with the index INDEX, in the node
2N/A NODE which is part of the B+ tree BTREE. */
2N/Astatic inline struct grub_hfsplus_key *
2N/Agrub_hfsplus_btree_recptr (struct grub_hfsplus_btree *btree,
2N/A struct grub_hfsplus_btnode *node, int index)
2N/A{
2N/A char *cnode = (char *) node;
2N/A grub_off_t offset;
2N/A offset = grub_hfsplus_btree_recoffset (btree, node, index);
2N/A return (struct grub_hfsplus_key *) &cnode[offset];
2N/A}
2N/A
2N/A
2N/A/* Find the extent that points to FILEBLOCK. If it is not in one of
2N/A the 8 extents described by EXTENT, return -1. In that case set
2N/A FILEBLOCK to the next block. */
2N/Astatic grub_disk_addr_t
2N/Agrub_hfsplus_find_block (struct grub_hfsplus_extent *extent,
2N/A grub_disk_addr_t *fileblock)
2N/A{
2N/A int i;
2N/A grub_disk_addr_t blksleft = *fileblock;
2N/A
2N/A /* First lookup the file in the given extents. */
2N/A for (i = 0; i < 8; i++)
2N/A {
2N/A if (blksleft < grub_be_to_cpu32 (extent[i].count))
2N/A return grub_be_to_cpu32 (extent[i].start) + blksleft;
2N/A blksleft -= grub_be_to_cpu32 (extent[i].count);
2N/A }
2N/A
2N/A *fileblock = blksleft;
2N/A return 0xffffffffffffffffULL;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
2N/A struct grub_hfsplus_key_internal *key,
2N/A int (*compare_keys) (struct grub_hfsplus_key *keya,
2N/A struct grub_hfsplus_key_internal *keyb),
2N/A struct grub_hfsplus_btnode **matchnode,
2N/A grub_off_t *keyoffset);
2N/A
2N/Astatic int grub_hfsplus_cmp_extkey (struct grub_hfsplus_key *keya,
2N/A struct grub_hfsplus_key_internal *keyb);
2N/A
2N/A/* Search for the block FILEBLOCK inside the file NODE. Return the
2N/A blocknumber of this block on disk. */
2N/Astatic grub_disk_addr_t
2N/Agrub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
2N/A{
2N/A struct grub_hfsplus_btnode *nnode = 0;
2N/A grub_disk_addr_t blksleft = fileblock;
2N/A struct grub_hfsplus_extent *extents = &node->extents[0];
2N/A
2N/A while (1)
2N/A {
2N/A struct grub_hfsplus_extkey *key;
2N/A struct grub_hfsplus_key_internal extoverflow;
2N/A grub_disk_addr_t blk;
2N/A grub_off_t ptr;
2N/A
2N/A /* Try to find this block in the current set of extents. */
2N/A blk = grub_hfsplus_find_block (extents, &blksleft);
2N/A
2N/A /* The previous iteration of this loop allocated memory. The
2N/A code above used this memory, it can be freed now. */
2N/A grub_free (nnode);
2N/A nnode = 0;
2N/A
2N/A if (blk != 0xffffffffffffffffULL)
2N/A return (blk
2N/A + (node->data->embedded_offset >> (node->data->log2blksize
2N/A - GRUB_DISK_SECTOR_BITS)));
2N/A
2N/A /* For the extent overflow file, extra extents can't be found in
2N/A the extent overflow file. If this happens, you found a
2N/A bug... */
2N/A if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW)
2N/A {
2N/A grub_error (GRUB_ERR_READ_ERROR,
2N/A "extra extents found in an extend overflow file");
2N/A break;
2N/A }
2N/A
2N/A /* Set up the key to look for in the extent overflow file. */
2N/A extoverflow.extkey.fileid = node->fileid;
2N/A extoverflow.extkey.start = fileblock - blksleft;
2N/A
2N/A if (grub_hfsplus_btree_search (&node->data->extoverflow_tree,
2N/A &extoverflow,
2N/A grub_hfsplus_cmp_extkey, &nnode, &ptr))
2N/A {
2N/A grub_error (GRUB_ERR_READ_ERROR,
2N/A "no block found for the file id 0x%x and the block offset 0x%x",
2N/A node->fileid, fileblock);
2N/A break;
2N/A }
2N/A
2N/A /* The extent overflow file has 8 extents right after the key. */
2N/A key = (struct grub_hfsplus_extkey *)
2N/A grub_hfsplus_btree_recptr (&node->data->extoverflow_tree, nnode, ptr);
2N/A extents = (struct grub_hfsplus_extent *) (key + 1);
2N/A
2N/A /* The block wasn't found. Perhaps the next iteration will find
2N/A it. The last block we found is stored in BLKSLEFT now. */
2N/A }
2N/A
2N/A grub_free (nnode);
2N/A
2N/A /* Too bad, you lose. */
2N/A return -1;
2N/A}
2N/A
2N/A
2N/A/* Read LEN bytes from the file described by DATA starting with byte
2N/A POS. Return the amount of read bytes in READ. */
2N/Astatic grub_ssize_t
2N/Agrub_hfsplus_read_file (grub_fshelp_node_t node,
2N/A void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
2N/A unsigned offset, unsigned length),
2N/A grub_off_t pos, grub_size_t len, char *buf)
2N/A{
2N/A return grub_fshelp_read_file (node->data->disk, node, read_hook,
2N/A pos, len, buf, grub_hfsplus_read_block,
2N/A node->size,
2N/A node->data->log2blksize - GRUB_DISK_SECTOR_BITS);
2N/A}
2N/A
2N/Astatic struct grub_hfsplus_data *
2N/Agrub_hfsplus_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_hfsplus_data *data;
2N/A struct grub_hfsplus_btheader header;
2N/A struct grub_hfsplus_btnode node;
2N/A grub_uint16_t magic;
2N/A union {
2N/A struct grub_hfs_sblock hfs;
2N/A struct grub_hfsplus_volheader hfsplus;
2N/A } volheader;
2N/A
2N/A data = grub_malloc (sizeof (*data));
2N/A if (!data)
2N/A return 0;
2N/A
2N/A data->disk = disk;
2N/A
2N/A /* Read the bootblock. */
2N/A grub_disk_read (disk, GRUB_HFSPLUS_SBLOCK, 0, sizeof (volheader),
2N/A &volheader);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A data->embedded_offset = 0;
2N/A if (grub_be_to_cpu16 (volheader.hfs.magic) == GRUB_HFS_MAGIC)
2N/A {
2N/A grub_disk_addr_t extent_start;
2N/A grub_disk_addr_t ablk_size;
2N/A grub_disk_addr_t ablk_start;
2N/A
2N/A /* See if there's an embedded HFS+ filesystem. */
2N/A if (grub_be_to_cpu16 (volheader.hfs.embed_sig) != GRUB_HFSPLUS_MAGIC)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A /* Calculate the offset needed to translate HFS+ sector numbers. */
2N/A extent_start = grub_be_to_cpu16 (volheader.hfs.embed_extent.first_block);
2N/A ablk_size = grub_be_to_cpu32 (volheader.hfs.blksz);
2N/A ablk_start = grub_be_to_cpu16 (volheader.hfs.first_block);
2N/A data->embedded_offset = (ablk_start
2N/A + extent_start
2N/A * (ablk_size >> GRUB_DISK_SECTOR_BITS));
2N/A
2N/A grub_disk_read (disk, data->embedded_offset + GRUB_HFSPLUS_SBLOCK, 0,
2N/A sizeof (volheader), &volheader);
2N/A if (grub_errno)
2N/A goto fail;
2N/A }
2N/A
2N/A /* Make sure this is an HFS+ filesystem. XXX: Do we really support
2N/A HFX? */
2N/A magic = grub_be_to_cpu16 (volheader.hfsplus.magic);
2N/A if ((magic != GRUB_HFSPLUS_MAGIC) && (magic != GRUB_HFSPLUSX_MAGIC))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A grub_memcpy (&data->volheader, &volheader.hfsplus,
2N/A sizeof (volheader.hfsplus));
2N/A
2N/A if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data->volheader.blksize),
2N/A &data->log2blksize))
2N/A goto fail;
2N/A
2N/A /* Make a new node for the catalog tree. */
2N/A data->catalog_tree.file.data = data;
2N/A data->catalog_tree.file.fileid = GRUB_HFSPLUS_FILEID_CATALOG;
2N/A grub_memcpy (&data->catalog_tree.file.extents,
2N/A data->volheader.catalog_file.extents,
2N/A sizeof data->volheader.catalog_file.extents);
2N/A data->catalog_tree.file.size =
2N/A grub_be_to_cpu64 (data->volheader.catalog_file.size);
2N/A
2N/A /* Make a new node for the extent overflow file. */
2N/A data->extoverflow_tree.file.data = data;
2N/A data->extoverflow_tree.file.fileid = GRUB_HFSPLUS_FILEID_OVERFLOW;
2N/A grub_memcpy (&data->extoverflow_tree.file.extents,
2N/A data->volheader.extents_file.extents,
2N/A sizeof data->volheader.catalog_file.extents);
2N/A
2N/A data->extoverflow_tree.file.size =
2N/A grub_be_to_cpu64 (data->volheader.extents_file.size);
2N/A
2N/A /* Read the essential information about the trees. */
2N/A if (grub_hfsplus_read_file (&data->catalog_tree.file, 0,
2N/A sizeof (struct grub_hfsplus_btnode),
2N/A sizeof (header), (char *) &header) <= 0)
2N/A goto fail;
2N/A
2N/A data->catalog_tree.root = grub_be_to_cpu32 (header.root);
2N/A data->catalog_tree.nodesize = grub_be_to_cpu16 (header.nodesize);
2N/A data->case_sensitive = ((magic == GRUB_HFSPLUSX_MAGIC) &&
2N/A (header.key_compare == GRUB_HFSPLUSX_BINARYCOMPARE));
2N/A
2N/A if (grub_hfsplus_read_file (&data->extoverflow_tree.file, 0,
2N/A sizeof (struct grub_hfsplus_btnode),
2N/A sizeof (header), (char *) &header) <= 0)
2N/A goto fail;
2N/A
2N/A data->extoverflow_tree.root = grub_be_to_cpu32 (header.root);
2N/A
2N/A if (grub_hfsplus_read_file (&data->extoverflow_tree.file, 0, 0,
2N/A sizeof (node), (char *) &node) <= 0)
2N/A goto fail;
2N/A
2N/A data->extoverflow_tree.root = grub_be_to_cpu32 (header.root);
2N/A data->extoverflow_tree.nodesize = grub_be_to_cpu16 (header.nodesize);
2N/A
2N/A data->dirroot.data = data;
2N/A data->dirroot.fileid = GRUB_HFSPLUS_FILEID_ROOTDIR;
2N/A
2N/A return data;
2N/A
2N/A fail:
2N/A
2N/A if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
2N/A grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem");
2N/A
2N/A grub_free (data);
2N/A return 0;
2N/A}
2N/A
2N/A/* Compare the on disk catalog key KEYA with the catalog key we are
2N/A looking for (KEYB). */
2N/Astatic int
2N/Agrub_hfsplus_cmp_catkey (struct grub_hfsplus_key *keya,
2N/A struct grub_hfsplus_key_internal *keyb)
2N/A{
2N/A struct grub_hfsplus_catkey *catkey_a = &keya->catkey;
2N/A struct grub_hfsplus_catkey_internal *catkey_b = &keyb->catkey;
2N/A int diff;
2N/A grub_size_t len;
2N/A
2N/A /* Safe unsigned comparison */
2N/A grub_uint32_t aparent = grub_be_to_cpu32 (catkey_a->parent);
2N/A if (aparent > catkey_b->parent)
2N/A return 1;
2N/A if (aparent < catkey_b->parent)
2N/A return -1;
2N/A
2N/A len = grub_be_to_cpu16 (catkey_a->namelen);
2N/A if (len > catkey_b->namelen)
2N/A len = catkey_b->namelen;
2N/A diff = grub_memcmp (catkey_a->name, catkey_b->name,
2N/A len * sizeof (catkey_a->name[0]));
2N/A if (diff == 0)
2N/A diff = grub_be_to_cpu16 (catkey_a->namelen) - catkey_b->namelen;
2N/A
2N/A return diff;
2N/A}
2N/A
2N/A/* Compare the on disk catalog key KEYA with the catalog key we are
2N/A looking for (KEYB). */
2N/Astatic int
2N/Agrub_hfsplus_cmp_catkey_id (struct grub_hfsplus_key *keya,
2N/A struct grub_hfsplus_key_internal *keyb)
2N/A{
2N/A struct grub_hfsplus_catkey *catkey_a = &keya->catkey;
2N/A struct grub_hfsplus_catkey_internal *catkey_b = &keyb->catkey;
2N/A
2N/A /* Safe unsigned comparison */
2N/A grub_uint32_t aparent = grub_be_to_cpu32 (catkey_a->parent);
2N/A if (aparent > catkey_b->parent)
2N/A return 1;
2N/A if (aparent < catkey_b->parent)
2N/A return -1;
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A/* Compare the on disk extent overflow key KEYA with the extent
2N/A overflow key we are looking for (KEYB). */
2N/Astatic int
2N/Agrub_hfsplus_cmp_extkey (struct grub_hfsplus_key *keya,
2N/A struct grub_hfsplus_key_internal *keyb)
2N/A{
2N/A struct grub_hfsplus_extkey *extkey_a = &keya->extkey;
2N/A struct grub_hfsplus_extkey_internal *extkey_b = &keyb->extkey;
2N/A grub_uint32_t akey;
2N/A
2N/A /* Safe unsigned comparison */
2N/A akey = grub_be_to_cpu32 (extkey_a->fileid);
2N/A if (akey > extkey_b->fileid)
2N/A return 1;
2N/A if (akey < extkey_b->fileid)
2N/A return -1;
2N/A
2N/A akey = grub_be_to_cpu32 (extkey_a->start);
2N/A if (akey > extkey_b->start)
2N/A return 1;
2N/A if (akey < extkey_b->start)
2N/A return -1;
2N/A return 0;
2N/A}
2N/A
2N/Astatic char *
2N/Agrub_hfsplus_read_symlink (grub_fshelp_node_t node)
2N/A{
2N/A char *symlink;
2N/A grub_ssize_t numread;
2N/A
2N/A symlink = grub_malloc (node->size + 1);
2N/A if (!symlink)
2N/A return 0;
2N/A
2N/A numread = grub_hfsplus_read_file (node, 0, 0, node->size, symlink);
2N/A if (numread != (grub_ssize_t) node->size)
2N/A {
2N/A grub_free (symlink);
2N/A return 0;
2N/A }
2N/A symlink[node->size] = '\0';
2N/A
2N/A return symlink;
2N/A}
2N/A
2N/Astatic int
2N/Agrub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
2N/A struct grub_hfsplus_btnode *first_node,
2N/A grub_disk_addr_t first_rec,
2N/A int (*hook) (void *record))
2N/A{
2N/A grub_disk_addr_t rec;
2N/A
2N/A for (;;)
2N/A {
2N/A char *cnode = (char *) first_node;
2N/A
2N/A /* Iterate over all records in this node. */
2N/A for (rec = first_rec; rec < grub_be_to_cpu16 (first_node->count); rec++)
2N/A {
2N/A if (hook (grub_hfsplus_btree_recptr (btree, first_node, rec)))
2N/A return 1;
2N/A }
2N/A
2N/A if (! first_node->next)
2N/A break;
2N/A
2N/A if (grub_hfsplus_read_file (&btree->file, 0,
2N/A (grub_be_to_cpu32 (first_node->next)
2N/A * btree->nodesize),
2N/A btree->nodesize, cnode) <= 0)
2N/A return 1;
2N/A
2N/A /* Don't skip any record in the next iteration. */
2N/A first_rec = 0;
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A/* Lookup the node described by KEY in the B+ Tree BTREE. Compare
2N/A keys using the function COMPARE_KEYS. When a match is found,
2N/A return the node in MATCHNODE and a pointer to the data in this node
2N/A in KEYOFFSET. MATCHNODE should be freed by the caller. */
2N/Astatic grub_err_t
2N/Agrub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
2N/A struct grub_hfsplus_key_internal *key,
2N/A int (*compare_keys) (struct grub_hfsplus_key *keya,
2N/A struct grub_hfsplus_key_internal *keyb),
2N/A struct grub_hfsplus_btnode **matchnode,
2N/A grub_off_t *keyoffset)
2N/A{
2N/A grub_uint64_t currnode;
2N/A char *node;
2N/A struct grub_hfsplus_btnode *nodedesc;
2N/A grub_disk_addr_t rec;
2N/A
2N/A node = grub_malloc (btree->nodesize);
2N/A if (! node)
2N/A return grub_errno;
2N/A
2N/A currnode = btree->root;
2N/A while (1)
2N/A {
2N/A int match = 0;
2N/A
2N/A /* Read a node. */
2N/A if (grub_hfsplus_read_file (&btree->file, 0,
2N/A (grub_disk_addr_t) currnode
2N/A * (grub_disk_addr_t) btree->nodesize,
2N/A btree->nodesize, (char *) node) <= 0)
2N/A {
2N/A grub_free (node);
2N/A return grub_error (GRUB_ERR_BAD_FS, "couldn't read i-node");
2N/A }
2N/A
2N/A nodedesc = (struct grub_hfsplus_btnode *) node;
2N/A
2N/A /* Find the record in this tree. */
2N/A for (rec = 0; rec < grub_be_to_cpu16 (nodedesc->count); rec++)
2N/A {
2N/A struct grub_hfsplus_key *currkey;
2N/A currkey = grub_hfsplus_btree_recptr (btree, nodedesc, rec);
2N/A
2N/A /* The action that has to be taken depend on the type of
2N/A record. */
2N/A if (nodedesc->type == GRUB_HFSPLUS_BTNODE_TYPE_LEAF
2N/A && compare_keys (currkey, key) == 0)
2N/A {
2N/A /* An exact match was found! */
2N/A
2N/A *matchnode = nodedesc;
2N/A *keyoffset = rec;
2N/A
2N/A return 0;
2N/A }
2N/A else if (nodedesc->type == GRUB_HFSPLUS_BTNODE_TYPE_INDEX)
2N/A {
2N/A void *pointer;
2N/A
2N/A /* The place where the key could have been found didn't
2N/A contain the key. This means that the previous match
2N/A is the one that should be followed. */
2N/A if (compare_keys (currkey, key) > 0)
2N/A break;
2N/A
2N/A /* Mark the last key which is lower or equal to the key
2N/A that we are looking for. The last match that is
2N/A found will be used to locate the child which can
2N/A contain the record. */
2N/A pointer = ((char *) currkey
2N/A + grub_be_to_cpu16 (currkey->keylen)
2N/A + 2);
2N/A currnode = grub_be_to_cpu32 (grub_get_unaligned32 (pointer));
2N/A match = 1;
2N/A }
2N/A }
2N/A
2N/A /* No match is found, no record with this key exists in the
2N/A tree. */
2N/A if (! match)
2N/A {
2N/A *matchnode = 0;
2N/A grub_free (node);
2N/A return 1;
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/Agrub_hfsplus_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 int ret = 0;
2N/A
2N/A auto int list_nodes (void *record);
2N/A int list_nodes (void *record)
2N/A {
2N/A struct grub_hfsplus_catkey *catkey;
2N/A char *filename;
2N/A int i;
2N/A struct grub_fshelp_node *node;
2N/A struct grub_hfsplus_catfile *fileinfo;
2N/A enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
2N/A
2N/A catkey = (struct grub_hfsplus_catkey *) record;
2N/A
2N/A fileinfo =
2N/A (struct grub_hfsplus_catfile *) ((char *) record
2N/A + grub_be_to_cpu16 (catkey->keylen)
2N/A + 2 + (grub_be_to_cpu16(catkey->keylen)
2N/A % 2));
2N/A
2N/A /* Stop iterating when the last directory entry is found. */
2N/A if (grub_be_to_cpu32 (catkey->parent) != dir->fileid)
2N/A return 1;
2N/A
2N/A /* Determine the type of the node that is found. */
2N/A if (grub_be_to_cpu16 (fileinfo->type) == GRUB_HFSPLUS_FILETYPE_REG)
2N/A {
2N/A int mode = (grub_be_to_cpu16 (fileinfo->mode)
2N/A & GRUB_HFSPLUS_FILEMODE_MASK);
2N/A
2N/A if (mode == GRUB_HFSPLUS_FILEMODE_REG)
2N/A type = GRUB_FSHELP_REG;
2N/A else if (mode == GRUB_HFSPLUS_FILEMODE_SYMLINK)
2N/A type = GRUB_FSHELP_SYMLINK;
2N/A else
2N/A type = GRUB_FSHELP_UNKNOWN;
2N/A }
2N/A else if (grub_be_to_cpu16 (fileinfo->type) == GRUB_HFSPLUS_FILETYPE_DIR)
2N/A type = GRUB_FSHELP_DIR;
2N/A
2N/A if (type == GRUB_FSHELP_UNKNOWN)
2N/A return 0;
2N/A
2N/A filename = grub_malloc (grub_be_to_cpu16 (catkey->namelen)
2N/A * GRUB_MAX_UTF8_PER_UTF16 + 1);
2N/A if (! filename)
2N/A return 0;
2N/A
2N/A /* Make sure the byte order of the UTF16 string is correct. */
2N/A for (i = 0; i < grub_be_to_cpu16 (catkey->namelen); i++)
2N/A {
2N/A catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]);
2N/A
2N/A if (catkey->name[i] == '/')
2N/A catkey->name[i] = ':';
2N/A
2N/A /* If the name is obviously invalid, skip this node. */
2N/A if (catkey->name[i] == 0)
2N/A return 0;
2N/A }
2N/A
2N/A *grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey->name,
2N/A grub_be_to_cpu16 (catkey->namelen)) = '\0';
2N/A
2N/A /* Restore the byte order to what it was previously. */
2N/A for (i = 0; i < grub_be_to_cpu16 (catkey->namelen); i++)
2N/A {
2N/A if (catkey->name[i] == ':')
2N/A catkey->name[i] = '/';
2N/A catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]);
2N/A }
2N/A
2N/A /* hfs+ is case insensitive. */
2N/A if (! dir->data->case_sensitive)
2N/A type |= GRUB_FSHELP_CASE_INSENSITIVE;
2N/A
2N/A /* A valid node is found; setup the node and call the
2N/A callback function. */
2N/A node = grub_malloc (sizeof (*node));
2N/A node->data = dir->data;
2N/A
2N/A grub_memcpy (node->extents, fileinfo->data.extents,
2N/A sizeof (node->extents));
2N/A node->mtime = grub_be_to_cpu32 (fileinfo->mtime) - 2082844800;
2N/A node->size = grub_be_to_cpu64 (fileinfo->data.size);
2N/A node->fileid = grub_be_to_cpu32 (fileinfo->fileid);
2N/A
2N/A ret = hook (filename, type, node);
2N/A
2N/A grub_free (filename);
2N/A
2N/A return ret;
2N/A }
2N/A
2N/A struct grub_hfsplus_key_internal intern;
2N/A struct grub_hfsplus_btnode *node;
2N/A grub_disk_addr_t ptr;
2N/A
2N/A /* Create a key that points to the first entry in the directory. */
2N/A intern.catkey.parent = dir->fileid;
2N/A intern.catkey.name = 0;
2N/A intern.catkey.namelen = 0;
2N/A
2N/A /* First lookup the first entry. */
2N/A if (grub_hfsplus_btree_search (&dir->data->catalog_tree, &intern,
2N/A grub_hfsplus_cmp_catkey, &node, &ptr))
2N/A return 0;
2N/A
2N/A /* Iterate over all entries in this directory. */
2N/A grub_hfsplus_btree_iterate_node (&dir->data->catalog_tree, node, ptr,
2N/A list_nodes);
2N/A
2N/A grub_free (node);
2N/A
2N/A return ret;
2N/A}
2N/A
2N/A/* Open a file named NAME and initialize FILE. */
2N/Astatic grub_err_t
2N/Agrub_hfsplus_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_hfsplus_data *data;
2N/A struct grub_fshelp_node *fdiro = 0;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_hfsplus_mount (file->device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A grub_fshelp_find_file (name, &data->dirroot, &fdiro,
2N/A grub_hfsplus_iterate_dir,
2N/A grub_hfsplus_read_symlink, GRUB_FSHELP_REG);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A file->size = fdiro->size;
2N/A data->opened_file = *fdiro;
2N/A grub_free (fdiro);
2N/A
2N/A file->data = data;
2N/A file->offset = 0;
2N/A
2N/A return 0;
2N/A
2N/A fail:
2N/A if (data && fdiro != &data->dirroot)
2N/A grub_free (fdiro);
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/Astatic grub_err_t
2N/Agrub_hfsplus_close (grub_file_t file)
2N/A{
2N/A grub_free (file->data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Read LEN bytes data from FILE into BUF. */
2N/Astatic grub_ssize_t
2N/Agrub_hfsplus_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_hfsplus_data *data =
2N/A (struct grub_hfsplus_data *) file->data;
2N/A
2N/A return grub_hfsplus_read_file (&data->opened_file, file->read_hook,
2N/A file->offset, len, buf);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_hfsplus_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_hfsplus_data *data = 0;
2N/A struct grub_fshelp_node *fdiro = 0;
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 = 1;
2N/A info.mtime = node->mtime;
2N/A info.case_insensitive = !! (filetype & GRUB_FSHELP_CASE_INSENSITIVE);
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_hfsplus_mount (device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A /* Find the directory that should be opened. */
2N/A grub_fshelp_find_file (path, &data->dirroot, &fdiro,
2N/A grub_hfsplus_iterate_dir,
2N/A grub_hfsplus_read_symlink, GRUB_FSHELP_DIR);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A /* Iterate over all entries in this directory. */
2N/A grub_hfsplus_iterate_dir (fdiro, iterate);
2N/A
2N/A fail:
2N/A if (data && fdiro != &data->dirroot)
2N/A grub_free (fdiro);
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/Astatic grub_err_t
2N/Agrub_hfsplus_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_hfsplus_data *data;
2N/A grub_disk_t disk = device->disk;
2N/A struct grub_hfsplus_catkey *catkey;
2N/A int i, label_len;
2N/A struct grub_hfsplus_key_internal intern;
2N/A struct grub_hfsplus_btnode *node;
2N/A grub_disk_addr_t ptr;
2N/A
2N/A *label = 0;
2N/A
2N/A data = grub_hfsplus_mount (disk);
2N/A if (!data)
2N/A return grub_errno;
2N/A
2N/A /* Create a key that points to the label. */
2N/A intern.catkey.parent = 1;
2N/A intern.catkey.name = 0;
2N/A intern.catkey.namelen = 0;
2N/A
2N/A /* First lookup the first entry. */
2N/A if (grub_hfsplus_btree_search (&data->catalog_tree, &intern,
2N/A grub_hfsplus_cmp_catkey_id, &node, &ptr))
2N/A {
2N/A grub_free (data);
2N/A return 0;
2N/A }
2N/A
2N/A catkey = (struct grub_hfsplus_catkey *)
2N/A grub_hfsplus_btree_recptr (&data->catalog_tree, node, 0);
2N/A
2N/A label_len = grub_be_to_cpu16 (catkey->namelen);
2N/A for (i = 0; i < label_len; i++)
2N/A {
2N/A catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]);
2N/A
2N/A /* If the name is obviously invalid, skip this node. */
2N/A if (catkey->name[i] == 0)
2N/A return 0;
2N/A }
2N/A
2N/A *label = grub_malloc (label_len * GRUB_MAX_UTF8_PER_UTF16 + 1);
2N/A if (! *label)
2N/A return grub_errno;
2N/A
2N/A *grub_utf16_to_utf8 ((grub_uint8_t *) (*label), catkey->name,
2N/A label_len) = '\0';
2N/A
2N/A grub_free (node);
2N/A grub_free (data);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Get mtime. */
2N/Astatic grub_err_t
2N/Agrub_hfsplus_mtime (grub_device_t device, grub_int32_t *tm)
2N/A{
2N/A struct grub_hfsplus_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_hfsplus_mount (disk);
2N/A if (!data)
2N/A *tm = 0;
2N/A else
2N/A *tm = grub_be_to_cpu32 (data->volheader.utime) - 2082844800;
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
2N/Astatic grub_err_t
2N/Agrub_hfsplus_uuid (grub_device_t device, char **uuid)
2N/A{
2N/A struct grub_hfsplus_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_hfsplus_mount (disk);
2N/A if (data)
2N/A {
2N/A *uuid = grub_xasprintf ("%016llx",
2N/A (unsigned long long)
2N/A grub_be_to_cpu64 (data->volheader.num_serial));
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
2N/A
2N/Astatic struct grub_fs grub_hfsplus_fs =
2N/A {
2N/A .name = "hfsplus",
2N/A .dir = grub_hfsplus_dir,
2N/A .open = grub_hfsplus_open,
2N/A .read = grub_hfsplus_read,
2N/A .close = grub_hfsplus_close,
2N/A .label = grub_hfsplus_label,
2N/A .mtime = grub_hfsplus_mtime,
2N/A .uuid = grub_hfsplus_uuid,
2N/A#ifdef GRUB_UTIL
2N/A .reserved_first_sector = 1,
2N/A#endif
2N/A .next = 0
2N/A };
2N/A
2N/AGRUB_MOD_INIT(hfsplus)
2N/A{
2N/A grub_fs_register (&grub_hfsplus_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(hfsplus)
2N/A{
2N/A grub_fs_unregister (&grub_hfsplus_fs);
2N/A}