2N/A/* hfs.c - HFS. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2004,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
2N/A http://developer.apple.com/documentation/mac/Files/Files-2.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/hfs.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_HFS_SBLOCK 2
2N/A#define GRUB_HFS_EMBED_HFSPLUS_SIG 0x482B
2N/A
2N/A#define GRUB_HFS_BLKS (data->blksz >> 9)
2N/A
2N/A#define GRUB_HFS_NODE_LEAF 0xFF
2N/A
2N/A/* The two supported filesystems a record can have. */
2N/Aenum
2N/A {
2N/A GRUB_HFS_FILETYPE_DIR = 1,
2N/A GRUB_HFS_FILETYPE_FILE = 2
2N/A };
2N/A
2N/A/* Catalog node ID (CNID). */
2N/Aenum grub_hfs_cnid_type
2N/A {
2N/A GRUB_HFS_CNID_ROOT_PARENT = 1,
2N/A GRUB_HFS_CNID_ROOT = 2,
2N/A GRUB_HFS_CNID_EXT = 3,
2N/A GRUB_HFS_CNID_CAT = 4,
2N/A GRUB_HFS_CNID_BAD = 5
2N/A };
2N/A
2N/A/* A node descriptor. This is the header of every node. */
2N/Astruct grub_hfs_node
2N/A{
2N/A grub_uint32_t next;
2N/A grub_uint32_t prev;
2N/A grub_uint8_t type;
2N/A grub_uint8_t level;
2N/A grub_uint16_t reccnt;
2N/A grub_uint16_t unused;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The head of the B*-Tree. */
2N/Astruct grub_hfs_treeheader
2N/A{
2N/A grub_uint16_t tree_depth;
2N/A /* The number of the first node. */
2N/A grub_uint32_t root_node;
2N/A grub_uint32_t leaves;
2N/A grub_uint32_t first_leaf;
2N/A grub_uint32_t last_leaf;
2N/A grub_uint16_t node_size;
2N/A grub_uint16_t key_size;
2N/A grub_uint32_t nodes;
2N/A grub_uint32_t free_nodes;
2N/A grub_uint8_t unused[76];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The state of a mounted HFS filesystem. */
2N/Astruct grub_hfs_data
2N/A{
2N/A struct grub_hfs_sblock sblock;
2N/A grub_disk_t disk;
2N/A grub_hfs_datarecord_t extents;
2N/A int fileid;
2N/A int size;
2N/A int ext_root;
2N/A int ext_size;
2N/A int cat_root;
2N/A int cat_size;
2N/A int blksz;
2N/A int log2_blksz;
2N/A int rootdir;
2N/A};
2N/A
2N/A/* The key as used on disk in a catalog tree. This is used to lookup
2N/A file/directory nodes by parent directory ID and filename. */
2N/Astruct grub_hfs_catalog_key
2N/A{
2N/A grub_uint8_t unused;
2N/A grub_uint32_t parent_dir;
2N/A
2N/A /* Filename length. */
2N/A grub_uint8_t strlen;
2N/A
2N/A /* Filename. */
2N/A grub_uint8_t str[31];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The key as used on disk in a extent overflow tree. Using this key
2N/A the extents can be looked up using a fileid and logical start block
2N/A as index. */
2N/Astruct grub_hfs_extent_key
2N/A{
2N/A /* The kind of fork. This is used to store meta information like
2N/A icons, attributes, etc. We will only use the datafork, which is
2N/A 0. */
2N/A grub_uint8_t forktype;
2N/A grub_uint32_t fileid;
2N/A grub_uint16_t first_block;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A directory record. This is used to find out the directory ID. */
2N/Astruct grub_hfs_dirrec
2N/A{
2N/A /* For a directory, type == 1. */
2N/A grub_uint8_t type;
2N/A grub_uint8_t unused[5];
2N/A grub_uint32_t dirid;
2N/A grub_uint32_t ctime;
2N/A grub_uint32_t mtime;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* Information about a file. */
2N/Astruct grub_hfs_filerec
2N/A{
2N/A /* For a file, type == 2. */
2N/A grub_uint8_t type;
2N/A grub_uint8_t unused[19];
2N/A grub_uint32_t fileid;
2N/A grub_uint8_t unused2[2];
2N/A grub_uint32_t size;
2N/A grub_uint8_t unused3[18];
2N/A grub_uint32_t mtime;
2N/A grub_uint8_t unused4[22];
2N/A
2N/A /* The first 3 extents of the file. The other extents can be found
2N/A in the extent overflow file. */
2N/A grub_hfs_datarecord_t extents;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A record descriptor, both key and data, used to pass to call back
2N/A functions. */
2N/Astruct grub_hfs_record
2N/A{
2N/A void *key;
2N/A int keylen;
2N/A void *data;
2N/A int datalen;
2N/A};
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/Astatic int grub_hfs_find_node (struct grub_hfs_data *, char *,
2N/A grub_uint32_t, int, char *, int);
2N/A
2N/A/* Find block BLOCK of the file FILE in the mounted UFS filesystem
2N/A DATA. The first 3 extents are described by DAT. If cache is set,
2N/A using caching to improve non-random reads. */
2N/Astatic unsigned int
2N/Agrub_hfs_block (struct grub_hfs_data *data, grub_hfs_datarecord_t dat,
2N/A int file, int block, int cache)
2N/A{
2N/A grub_hfs_datarecord_t dr;
2N/A int pos = 0;
2N/A struct grub_hfs_extent_key key;
2N/A
2N/A int tree = 0;
2N/A static int cache_file = 0;
2N/A static int cache_pos = 0;
2N/A static grub_hfs_datarecord_t cache_dr;
2N/A
2N/A grub_memcpy (dr, dat, sizeof (dr));
2N/A
2N/A key.forktype = 0;
2N/A key.fileid = grub_cpu_to_be32 (file);
2N/A
2N/A if (cache && cache_file == file && block > cache_pos)
2N/A {
2N/A pos = cache_pos;
2N/A key.first_block = grub_cpu_to_be16 (pos);
2N/A grub_memcpy (dr, cache_dr, sizeof (cache_dr));
2N/A }
2N/A
2N/A for (;;)
2N/A {
2N/A int i;
2N/A
2N/A /* Try all 3 extents. */
2N/A for (i = 0; i < 3; i++)
2N/A {
2N/A /* Check if the block is stored in this extent. */
2N/A if (grub_be_to_cpu16 (dr[i].count) + pos > block)
2N/A {
2N/A int first = grub_be_to_cpu16 (dr[i].first_block);
2N/A
2N/A /* If the cache is enabled, store the current position
2N/A in the tree. */
2N/A if (tree && cache)
2N/A {
2N/A cache_file = file;
2N/A cache_pos = pos;
2N/A grub_memcpy (cache_dr, dr, sizeof (cache_dr));
2N/A }
2N/A
2N/A return (grub_be_to_cpu16 (data->sblock.first_block)
2N/A + (first + block - pos) * GRUB_HFS_BLKS);
2N/A }
2N/A
2N/A /* Try the next extent. */
2N/A pos += grub_be_to_cpu16 (dr[i].count);
2N/A }
2N/A
2N/A /* Lookup the block in the extent overflow file. */
2N/A key.first_block = grub_cpu_to_be16 (pos);
2N/A tree = 1;
2N/A grub_hfs_find_node (data, (char *) &key, data->ext_root,
2N/A 1, (char *) &dr, sizeof (dr));
2N/A if (grub_errno)
2N/A return 0;
2N/A }
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_hfs_read_file (struct grub_hfs_data *data,
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 grub_off_t i;
2N/A grub_off_t blockcnt;
2N/A
2N/A blockcnt = grub_divmod64 (((len + pos)
2N/A + data->blksz - 1), data->blksz, 0);
2N/A
2N/A for (i = grub_divmod64 (pos, data->blksz, 0); i < blockcnt; i++)
2N/A {
2N/A grub_disk_addr_t blknr;
2N/A grub_off_t blockoff;
2N/A grub_off_t blockend = data->blksz;
2N/A
2N/A int skipfirst = 0;
2N/A
2N/A grub_divmod64 (pos, data->blksz, &blockoff);
2N/A
2N/A blknr = grub_hfs_block (data, data->extents, data->fileid, i, 1);
2N/A if (grub_errno)
2N/A return -1;
2N/A
2N/A /* Last block. */
2N/A if (i == blockcnt - 1)
2N/A {
2N/A grub_divmod64 ((len + pos), data->blksz, &blockend);
2N/A
2N/A /* The last portion is exactly EXT2_BLOCK_SIZE (data). */
2N/A if (! blockend)
2N/A blockend = data->blksz;
2N/A }
2N/A
2N/A /* First block. */
2N/A if (i == grub_divmod64 (pos, data->blksz, 0))
2N/A {
2N/A skipfirst = blockoff;
2N/A blockend -= skipfirst;
2N/A }
2N/A
2N/A /* If the block number is 0 this block is not stored on disk but
2N/A is zero filled instead. */
2N/A if (blknr)
2N/A {
2N/A data->disk->read_hook = read_hook;
2N/A grub_disk_read (data->disk, blknr, skipfirst,
2N/A blockend, buf);
2N/A data->disk->read_hook = 0;
2N/A if (grub_errno)
2N/A return -1;
2N/A }
2N/A
2N/A buf += data->blksz - skipfirst;
2N/A }
2N/A
2N/A return len;
2N/A}
2N/A
2N/A
2N/A/* Mount the filesystem on the disk DISK. */
2N/Astatic struct grub_hfs_data *
2N/Agrub_hfs_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_hfs_data *data;
2N/A struct grub_hfs_catalog_key key;
2N/A struct grub_hfs_dirrec dir;
2N/A int first_block;
2N/A
2N/A struct
2N/A {
2N/A struct grub_hfs_node node;
2N/A struct grub_hfs_treeheader head;
2N/A } treehead;
2N/A
2N/A data = grub_malloc (sizeof (struct grub_hfs_data));
2N/A if (!data)
2N/A return 0;
2N/A
2N/A /* Read the superblock. */
2N/A if (grub_disk_read (disk, GRUB_HFS_SBLOCK, 0,
2N/A sizeof (struct grub_hfs_sblock), &data->sblock))
2N/A goto fail;
2N/A
2N/A /* Check if this is a HFS filesystem. */
2N/A if (grub_be_to_cpu16 (data->sblock.magic) != GRUB_HFS_MAGIC)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an HFS filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A /* Check if this is an embedded HFS+ filesystem. */
2N/A if (grub_be_to_cpu16 (data->sblock.embed_sig) == GRUB_HFS_EMBED_HFSPLUS_SIG)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "embedded HFS+ filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A data->blksz = grub_be_to_cpu32 (data->sblock.blksz);
2N/A data->disk = disk;
2N/A
2N/A /* Lookup the root node of the extent overflow tree. */
2N/A first_block = ((grub_be_to_cpu16 (data->sblock.extent_recs[0].first_block)
2N/A * GRUB_HFS_BLKS)
2N/A + grub_be_to_cpu16 (data->sblock.first_block));
2N/A
2N/A if (grub_disk_read (data->disk, first_block, 0,
2N/A sizeof (treehead), &treehead))
2N/A goto fail;
2N/A data->ext_root = grub_be_to_cpu32 (treehead.head.root_node);
2N/A data->ext_size = grub_be_to_cpu16 (treehead.head.node_size);
2N/A
2N/A /* Lookup the root node of the catalog tree. */
2N/A first_block = ((grub_be_to_cpu16 (data->sblock.catalog_recs[0].first_block)
2N/A * GRUB_HFS_BLKS)
2N/A + grub_be_to_cpu16 (data->sblock.first_block));
2N/A if (grub_disk_read (data->disk, first_block, 0,
2N/A sizeof (treehead), &treehead))
2N/A goto fail;
2N/A data->cat_root = grub_be_to_cpu32 (treehead.head.root_node);
2N/A data->cat_size = grub_be_to_cpu16 (treehead.head.node_size);
2N/A
2N/A /* Lookup the root directory node in the catalog tree using the
2N/A volume name. */
2N/A key.parent_dir = grub_cpu_to_be32 (1);
2N/A key.strlen = data->sblock.volname[0];
2N/A grub_strcpy ((char *) key.str, (char *) (data->sblock.volname + 1));
2N/A
2N/A if (grub_hfs_find_node (data, (char *) &key, data->cat_root,
2N/A 0, (char *) &dir, sizeof (dir)) == 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "cannot find the HFS root directory");
2N/A goto fail;
2N/A }
2N/A
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A data->rootdir = grub_be_to_cpu32 (dir.dirid);
2N/A
2N/A return data;
2N/A fail:
2N/A grub_free (data);
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 return 0;
2N/A}
2N/A
2N/A/* Compare the K1 and K2 catalog file keys using HFS character ordering. */
2N/Astatic int
2N/Agrub_hfs_cmp_catkeys (struct grub_hfs_catalog_key *k1,
2N/A struct grub_hfs_catalog_key *k2)
2N/A{
2N/A /* Taken from hfsutils 3.2.6 and converted to a readable form */
2N/A static const unsigned char hfs_charorder[256] = {
2N/A [0x00] = 0,
2N/A [0x01] = 1,
2N/A [0x02] = 2,
2N/A [0x03] = 3,
2N/A [0x04] = 4,
2N/A [0x05] = 5,
2N/A [0x06] = 6,
2N/A [0x07] = 7,
2N/A [0x08] = 8,
2N/A [0x09] = 9,
2N/A [0x0A] = 10,
2N/A [0x0B] = 11,
2N/A [0x0C] = 12,
2N/A [0x0D] = 13,
2N/A [0x0E] = 14,
2N/A [0x0F] = 15,
2N/A [0x10] = 16,
2N/A [0x11] = 17,
2N/A [0x12] = 18,
2N/A [0x13] = 19,
2N/A [0x14] = 20,
2N/A [0x15] = 21,
2N/A [0x16] = 22,
2N/A [0x17] = 23,
2N/A [0x18] = 24,
2N/A [0x19] = 25,
2N/A [0x1A] = 26,
2N/A [0x1B] = 27,
2N/A [0x1C] = 28,
2N/A [0x1D] = 29,
2N/A [0x1E] = 30,
2N/A [0x1F] = 31,
2N/A [' '] = 32, [0xCA] = 32,
2N/A ['!'] = 33,
2N/A ['"'] = 34,
2N/A [0xD2] = 35,
2N/A [0xD3] = 36,
2N/A [0xC7] = 37,
2N/A [0xC8] = 38,
2N/A ['#'] = 39,
2N/A ['$'] = 40,
2N/A ['%'] = 41,
2N/A ['&'] = 42,
2N/A ['\''] = 43,
2N/A [0xD4] = 44,
2N/A [0xD5] = 45,
2N/A ['('] = 46,
2N/A [')'] = 47,
2N/A ['*'] = 48,
2N/A ['+'] = 49,
2N/A [','] = 50,
2N/A ['-'] = 51,
2N/A ['.'] = 52,
2N/A ['/'] = 53,
2N/A ['0'] = 54,
2N/A ['1'] = 55,
2N/A ['2'] = 56,
2N/A ['3'] = 57,
2N/A ['4'] = 58,
2N/A ['5'] = 59,
2N/A ['6'] = 60,
2N/A ['7'] = 61,
2N/A ['8'] = 62,
2N/A ['9'] = 63,
2N/A [':'] = 64,
2N/A [';'] = 65,
2N/A ['<'] = 66,
2N/A ['='] = 67,
2N/A ['>'] = 68,
2N/A ['?'] = 69,
2N/A ['@'] = 70,
2N/A ['A'] = 71, ['a'] = 71,
2N/A [0x88] = 72, [0xCB] = 72,
2N/A [0x80] = 73, [0x8A] = 73,
2N/A [0x8B] = 74, [0xCC] = 74,
2N/A [0x81] = 75, [0x8C] = 75,
2N/A [0xAE] = 76, [0xBE] = 76,
2N/A ['`'] = 77,
2N/A [0x87] = 78,
2N/A [0x89] = 79,
2N/A [0xBB] = 80,
2N/A ['B'] = 81, ['b'] = 81,
2N/A ['C'] = 82, ['c'] = 82,
2N/A [0x82] = 83, [0x8D] = 83,
2N/A ['D'] = 84, ['d'] = 84,
2N/A ['E'] = 85, ['e'] = 85,
2N/A [0x83] = 86, [0x8E] = 86,
2N/A [0x8F] = 87,
2N/A [0x90] = 88,
2N/A [0x91] = 89,
2N/A ['F'] = 90, ['f'] = 90,
2N/A ['G'] = 91, ['g'] = 91,
2N/A ['H'] = 92, ['h'] = 92,
2N/A ['I'] = 93, ['i'] = 93,
2N/A [0x92] = 94,
2N/A [0x93] = 95,
2N/A [0x94] = 96,
2N/A [0x95] = 97,
2N/A ['J'] = 98, ['j'] = 98,
2N/A ['K'] = 99, ['k'] = 99,
2N/A ['L'] = 100, ['l'] = 100,
2N/A ['M'] = 101, ['m'] = 101,
2N/A ['N'] = 102, ['n'] = 102,
2N/A [0x84] = 103, [0x96] = 103,
2N/A ['O'] = 104, ['o'] = 104,
2N/A [0x85] = 105, [0x9A] = 105,
2N/A [0x9B] = 106, [0xCD] = 106,
2N/A [0xAF] = 107, [0xBF] = 107,
2N/A [0xCE] = 108, [0xCF] = 108,
2N/A [0x97] = 109,
2N/A [0x98] = 110,
2N/A [0x99] = 111,
2N/A [0xBC] = 112,
2N/A ['P'] = 113, ['p'] = 113,
2N/A ['Q'] = 114, ['q'] = 114,
2N/A ['R'] = 115, ['r'] = 115,
2N/A ['S'] = 116, ['s'] = 116,
2N/A [0xA7] = 117,
2N/A ['T'] = 118, ['t'] = 118,
2N/A ['U'] = 119, ['u'] = 119,
2N/A [0x86] = 120, [0x9F] = 120,
2N/A [0x9C] = 121,
2N/A [0x9D] = 122,
2N/A [0x9E] = 123,
2N/A ['V'] = 124, ['v'] = 124,
2N/A ['W'] = 125, ['w'] = 125,
2N/A ['X'] = 126, ['x'] = 126,
2N/A ['Y'] = 127, ['y'] = 127,
2N/A [0xD8] = 128,
2N/A ['Z'] = 129, ['z'] = 129,
2N/A ['['] = 130,
2N/A ['\\'] = 131,
2N/A [']'] = 132,
2N/A ['^'] = 133,
2N/A ['_'] = 134,
2N/A ['{'] = 135,
2N/A ['|'] = 136,
2N/A ['}'] = 137,
2N/A ['~'] = 138,
2N/A [0x7F] = 139,
2N/A [0xA0] = 140,
2N/A [0xA1] = 141,
2N/A [0xA2] = 142,
2N/A [0xA3] = 143,
2N/A [0xA4] = 144,
2N/A [0xA5] = 145,
2N/A [0xA6] = 146,
2N/A [0xA8] = 147,
2N/A [0xA9] = 148,
2N/A [0xAA] = 149,
2N/A [0xAB] = 150,
2N/A [0xAC] = 151,
2N/A [0xAD] = 152,
2N/A [0xB0] = 153,
2N/A [0xB1] = 154,
2N/A [0xB2] = 155,
2N/A [0xB3] = 156,
2N/A [0xB4] = 157,
2N/A [0xB5] = 158,
2N/A [0xB6] = 159,
2N/A [0xB7] = 160,
2N/A [0xB8] = 161,
2N/A [0xB9] = 162,
2N/A [0xBA] = 163,
2N/A [0xBD] = 164,
2N/A [0xC0] = 165,
2N/A [0xC1] = 166,
2N/A [0xC2] = 167,
2N/A [0xC3] = 168,
2N/A [0xC4] = 169,
2N/A [0xC5] = 170,
2N/A [0xC6] = 171,
2N/A [0xC9] = 172,
2N/A [0xD0] = 173,
2N/A [0xD1] = 174,
2N/A [0xD6] = 175,
2N/A [0xD7] = 176,
2N/A [0xD9] = 177,
2N/A [0xDA] = 178,
2N/A [0xDB] = 179,
2N/A [0xDC] = 180,
2N/A [0xDD] = 181,
2N/A [0xDE] = 182,
2N/A [0xDF] = 183,
2N/A [0xE0] = 184,
2N/A [0xE1] = 185,
2N/A [0xE2] = 186,
2N/A [0xE3] = 187,
2N/A [0xE4] = 188,
2N/A [0xE5] = 189,
2N/A [0xE6] = 190,
2N/A [0xE7] = 191,
2N/A [0xE8] = 192,
2N/A [0xE9] = 193,
2N/A [0xEA] = 194,
2N/A [0xEB] = 195,
2N/A [0xEC] = 196,
2N/A [0xED] = 197,
2N/A [0xEE] = 198,
2N/A [0xEF] = 199,
2N/A [0xF0] = 200,
2N/A [0xF1] = 201,
2N/A [0xF2] = 202,
2N/A [0xF3] = 203,
2N/A [0xF4] = 204,
2N/A [0xF5] = 205,
2N/A [0xF6] = 206,
2N/A [0xF7] = 207,
2N/A [0xF8] = 208,
2N/A [0xF9] = 209,
2N/A [0xFA] = 210,
2N/A [0xFB] = 211,
2N/A [0xFC] = 212,
2N/A [0xFD] = 213,
2N/A [0xFE] = 214,
2N/A [0xFF] = 215,
2N/A };
2N/A int i;
2N/A int cmp;
2N/A int minlen = (k1->strlen < k2->strlen) ? k1->strlen : k2->strlen;
2N/A
2N/A cmp = (grub_be_to_cpu32 (k1->parent_dir) - grub_be_to_cpu32 (k2->parent_dir));
2N/A if (cmp != 0)
2N/A return cmp;
2N/A
2N/A for (i = 0; i < minlen; i++)
2N/A {
2N/A cmp = (hfs_charorder[k1->str[i]] - hfs_charorder[k2->str[i]]);
2N/A if (cmp != 0)
2N/A return cmp;
2N/A }
2N/A
2N/A /* Shorter strings precede long ones. */
2N/A return (k1->strlen - k2->strlen);
2N/A}
2N/A
2N/A
2N/A/* Compare the K1 and K2 extent overflow file keys. */
2N/Astatic int
2N/Agrub_hfs_cmp_extkeys (struct grub_hfs_extent_key *k1,
2N/A struct grub_hfs_extent_key *k2)
2N/A{
2N/A int cmp = k1->forktype - k2->forktype;
2N/A if (cmp == 0)
2N/A cmp = grub_be_to_cpu32 (k1->fileid) - grub_be_to_cpu32 (k2->fileid);
2N/A if (cmp == 0)
2N/A cmp = (grub_be_to_cpu16 (k1->first_block)
2N/A - grub_be_to_cpu16 (k2->first_block));
2N/A return cmp;
2N/A}
2N/A
2N/A
2N/A/* Iterate the records in the node with index IDX in the mounted HFS
2N/A filesystem DATA. This node holds data of the type TYPE (0 =
2N/A catalog node, 1 = extent overflow node). If this is set, continue
2N/A iterating to the next node. For every records, call NODE_HOOK. */
2N/Astatic grub_err_t
2N/Agrub_hfs_iterate_records (struct grub_hfs_data *data, int type, int idx,
2N/A int this, int (*node_hook) (struct grub_hfs_node *hnd,
2N/A struct grub_hfs_record *))
2N/A{
2N/A int nodesize = type == 0 ? data->cat_size : data->ext_size;
2N/A
2N/A union
2N/A {
2N/A struct grub_hfs_node node;
2N/A char rawnode[nodesize];
2N/A grub_uint16_t offsets[nodesize / 2];
2N/A } node;
2N/A
2N/A do
2N/A {
2N/A int i;
2N/A struct grub_hfs_extent *dat;
2N/A int blk;
2N/A
2N/A dat = (struct grub_hfs_extent *) (type == 0
2N/A ? (&data->sblock.catalog_recs)
2N/A : (&data->sblock.extent_recs));
2N/A
2N/A /* Read the node into memory. */
2N/A blk = grub_hfs_block (data, dat,
2N/A (type == 0) ? GRUB_HFS_CNID_CAT : GRUB_HFS_CNID_EXT,
2N/A idx / (data->blksz / nodesize), 0);
2N/A blk += (idx % (data->blksz / nodesize));
2N/A if (grub_errno)
2N/A return grub_errno;
2N/A
2N/A if (grub_disk_read (data->disk, blk, 0,
2N/A sizeof (node), &node))
2N/A return grub_errno;
2N/A
2N/A /* Iterate over all records in this node. */
2N/A for (i = 0; i < grub_be_to_cpu16 (node.node.reccnt); i++)
2N/A {
2N/A int pos = (nodesize >> 1) - 1 - i;
2N/A struct pointer
2N/A {
2N/A grub_uint8_t keylen;
2N/A grub_uint8_t key;
2N/A } __attribute__ ((packed)) *pnt;
2N/A pnt = (struct pointer *) (grub_be_to_cpu16 (node.offsets[pos])
2N/A + node.rawnode);
2N/A
2N/A struct grub_hfs_record rec =
2N/A {
2N/A &pnt->key,
2N/A pnt->keylen,
2N/A &pnt->key + pnt->keylen +(pnt->keylen + 1) % 2,
2N/A nodesize - grub_be_to_cpu16 (node.offsets[pos])
2N/A - pnt->keylen - 1
2N/A };
2N/A
2N/A if (node_hook (&node.node, &rec))
2N/A return 0;
2N/A }
2N/A
2N/A idx = grub_be_to_cpu32 (node.node.next);
2N/A } while (idx && this);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/A/* Lookup a record in the mounted filesystem DATA using the key KEY.
2N/A The index of the node on top of the tree is IDX. The tree is of
2N/A the type TYPE (0 = catalog node, 1 = extent overflow node). Return
2N/A the data in DATAR with a maximum length of DATALEN. */
2N/Astatic int
2N/Agrub_hfs_find_node (struct grub_hfs_data *data, char *key,
2N/A grub_uint32_t idx, int type, char *datar, int datalen)
2N/A{
2N/A int found = -1;
2N/A int isleaf = 0;
2N/A int done = 0;
2N/A
2N/A auto int node_found (struct grub_hfs_node *, struct grub_hfs_record *);
2N/A
2N/A int node_found (struct grub_hfs_node *hnd, struct grub_hfs_record *rec)
2N/A {
2N/A int cmp = 1;
2N/A
2N/A if (type == 0)
2N/A cmp = grub_hfs_cmp_catkeys (rec->key, (void *) key);
2N/A else
2N/A cmp = grub_hfs_cmp_extkeys (rec->key, (void *) key);
2N/A
2N/A /* If the key is smaller or equal to the current node, mark the
2N/A entry. In case of a non-leaf mode it will be used to lookup
2N/A the rest of the tree. */
2N/A if (cmp <= 0)
2N/A found = grub_be_to_cpu32 (grub_get_unaligned32 (rec->data));
2N/A else /* The key can not be found in the tree. */
2N/A return 1;
2N/A
2N/A /* Check if this node is a leaf node. */
2N/A if (hnd->type == GRUB_HFS_NODE_LEAF)
2N/A {
2N/A isleaf = 1;
2N/A
2N/A /* Found it!!!! */
2N/A if (cmp == 0)
2N/A {
2N/A done = 1;
2N/A
2N/A grub_memcpy (datar, rec->data,
2N/A rec->datalen < datalen ? rec->datalen : datalen);
2N/A return 1;
2N/A }
2N/A }
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A do
2N/A {
2N/A found = -1;
2N/A
2N/A if (grub_hfs_iterate_records (data, type, idx, 0, node_found))
2N/A return 0;
2N/A
2N/A if (found == -1)
2N/A return 0;
2N/A
2N/A idx = found;
2N/A } while (! isleaf);
2N/A
2N/A return done;
2N/A}
2N/A
2N/A
2N/A/* Iterate over the directory with the id DIR. The tree is searched
2N/A starting with the node ROOT_IDX. For every entry in this directory
2N/A call HOOK. */
2N/Astatic grub_err_t
2N/Agrub_hfs_iterate_dir (struct grub_hfs_data *data, grub_uint32_t root_idx,
2N/A unsigned int dir, int (*hook) (struct grub_hfs_record *))
2N/A{
2N/A int found = -1;
2N/A int isleaf = 0;
2N/A int next = 0;
2N/A
2N/A /* The lowest key possible with DIR as root directory. */
2N/A struct grub_hfs_catalog_key key = {0, grub_cpu_to_be32 (dir), 0, ""};
2N/A
2N/A auto int node_found (struct grub_hfs_node *, struct grub_hfs_record *);
2N/A auto int it_dir (struct grub_hfs_node * __attribute ((unused)),
2N/A struct grub_hfs_record *);
2N/A
2N/A
2N/A int node_found (struct grub_hfs_node *hnd, struct grub_hfs_record *rec)
2N/A {
2N/A struct grub_hfs_catalog_key *ckey = rec->key;
2N/A
2N/A if (grub_hfs_cmp_catkeys (rec->key, (void *) &key) <= 0)
2N/A found = grub_be_to_cpu32 (grub_get_unaligned32 (rec->data));
2N/A
2N/A if (hnd->type == 0xFF && ckey->strlen > 0)
2N/A {
2N/A isleaf = 1;
2N/A next = grub_be_to_cpu32 (hnd->next);
2N/A
2N/A /* An entry was found. */
2N/A if (grub_be_to_cpu32 (ckey->parent_dir) == dir)
2N/A return hook (rec);
2N/A }
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A int it_dir (struct grub_hfs_node *hnd __attribute ((unused)),
2N/A struct grub_hfs_record *rec)
2N/A {
2N/A struct grub_hfs_catalog_key *ckey = rec->key;
2N/A struct grub_hfs_catalog_key *origkey = &key;
2N/A
2N/A /* Stop when the entries do not match anymore. */
2N/A if (grub_be_to_cpu32 (ckey->parent_dir)
2N/A != grub_be_to_cpu32 ((origkey)->parent_dir))
2N/A return 1;
2N/A
2N/A return hook (rec);
2N/A }
2N/A
2N/A do
2N/A {
2N/A found = -1;
2N/A
2N/A if (grub_hfs_iterate_records (data, 0, root_idx, 0, node_found))
2N/A return grub_errno;
2N/A
2N/A if (found == -1)
2N/A return 0;
2N/A
2N/A root_idx = found;
2N/A } while (! isleaf);
2N/A
2N/A /* If there was a matching record in this leaf node, continue the
2N/A iteration until the last record was found. */
2N/A grub_hfs_iterate_records (data, 0, next, 1, it_dir);
2N/A return grub_errno;
2N/A}
2N/A
2N/A#define MAX_UTF8_PER_MAC_ROMAN 3
2N/A
2N/Astatic const char macroman[0x80][MAX_UTF8_PER_MAC_ROMAN + 1] =
2N/A {
2N/A /* 80 */ "\xc3\x84",
2N/A /* 81 */ "\xc3\x85",
2N/A /* 82 */ "\xc3\x87",
2N/A /* 83 */ "\xc3\x89",
2N/A /* 84 */ "\xc3\x91",
2N/A /* 85 */ "\xc3\x96",
2N/A /* 86 */ "\xc3\x9c",
2N/A /* 87 */ "\xc3\xa1",
2N/A /* 88 */ "\xc3\xa0",
2N/A /* 89 */ "\xc3\xa2",
2N/A /* 8A */ "\xc3\xa4",
2N/A /* 8B */ "\xc3\xa3",
2N/A /* 8C */ "\xc3\xa5",
2N/A /* 8D */ "\xc3\xa7",
2N/A /* 8E */ "\xc3\xa9",
2N/A /* 8F */ "\xc3\xa8",
2N/A /* 90 */ "\xc3\xaa",
2N/A /* 91 */ "\xc3\xab",
2N/A /* 92 */ "\xc3\xad",
2N/A /* 93 */ "\xc3\xac",
2N/A /* 94 */ "\xc3\xae",
2N/A /* 95 */ "\xc3\xaf",
2N/A /* 96 */ "\xc3\xb1",
2N/A /* 97 */ "\xc3\xb3",
2N/A /* 98 */ "\xc3\xb2",
2N/A /* 99 */ "\xc3\xb4",
2N/A /* 9A */ "\xc3\xb6",
2N/A /* 9B */ "\xc3\xb5",
2N/A /* 9C */ "\xc3\xba",
2N/A /* 9D */ "\xc3\xb9",
2N/A /* 9E */ "\xc3\xbb",
2N/A /* 9F */ "\xc3\xbc",
2N/A /* A0 */ "\xe2\x80\xa0",
2N/A /* A1 */ "\xc2\xb0",
2N/A /* A2 */ "\xc2\xa2",
2N/A /* A3 */ "\xc2\xa3",
2N/A /* A4 */ "\xc2\xa7",
2N/A /* A5 */ "\xe2\x80\xa2",
2N/A /* A6 */ "\xc2\xb6",
2N/A /* A7 */ "\xc3\x9f",
2N/A /* A8 */ "\xc2\xae",
2N/A /* A9 */ "\xc2\xa9",
2N/A /* AA */ "\xe2\x84\xa2",
2N/A /* AB */ "\xc2\xb4",
2N/A /* AC */ "\xc2\xa8",
2N/A /* AD */ "\xe2\x89\xa0",
2N/A /* AE */ "\xc3\x86",
2N/A /* AF */ "\xc3\x98",
2N/A /* B0 */ "\xe2\x88\x9e",
2N/A /* B1 */ "\xc2\xb1",
2N/A /* B2 */ "\xe2\x89\xa4",
2N/A /* B3 */ "\xe2\x89\xa5",
2N/A /* B4 */ "\xc2\xa5",
2N/A /* B5 */ "\xc2\xb5",
2N/A /* B6 */ "\xe2\x88\x82",
2N/A /* B7 */ "\xe2\x88\x91",
2N/A /* B8 */ "\xe2\x88\x8f",
2N/A /* B9 */ "\xcf\x80",
2N/A /* BA */ "\xe2\x88\xab",
2N/A /* BB */ "\xc2\xaa",
2N/A /* BC */ "\xc2\xba",
2N/A /* BD */ "\xce\xa9",
2N/A /* BE */ "\xc3\xa6",
2N/A /* BF */ "\xc3\xb8",
2N/A /* C0 */ "\xc2\xbf",
2N/A /* C1 */ "\xc2\xa1",
2N/A /* C2 */ "\xc2\xac",
2N/A /* C3 */ "\xe2\x88\x9a",
2N/A /* C4 */ "\xc6\x92",
2N/A /* C5 */ "\xe2\x89\x88",
2N/A /* C6 */ "\xe2\x88\x86",
2N/A /* C7 */ "\xc2\xab",
2N/A /* C8 */ "\xc2\xbb",
2N/A /* C9 */ "\xe2\x80\xa6",
2N/A /* CA */ "\xc2\xa0",
2N/A /* CB */ "\xc3\x80",
2N/A /* CC */ "\xc3\x83",
2N/A /* CD */ "\xc3\x95",
2N/A /* CE */ "\xc5\x92",
2N/A /* CF */ "\xc5\x93",
2N/A /* D0 */ "\xe2\x80\x93",
2N/A /* D1 */ "\xe2\x80\x94",
2N/A /* D2 */ "\xe2\x80\x9c",
2N/A /* D3 */ "\xe2\x80\x9d",
2N/A /* D4 */ "\xe2\x80\x98",
2N/A /* D5 */ "\xe2\x80\x99",
2N/A /* D6 */ "\xc3\xb7",
2N/A /* D7 */ "\xe2\x97\x8a",
2N/A /* D8 */ "\xc3\xbf",
2N/A /* D9 */ "\xc5\xb8",
2N/A /* DA */ "\xe2\x81\x84",
2N/A /* DB */ "\xe2\x82\xac",
2N/A /* DC */ "\xe2\x80\xb9",
2N/A /* DD */ "\xe2\x80\xba",
2N/A /* DE */ "\xef\xac\x81",
2N/A /* DF */ "\xef\xac\x82",
2N/A /* E0 */ "\xe2\x80\xa1",
2N/A /* E1 */ "\xc2\xb7",
2N/A /* E2 */ "\xe2\x80\x9a",
2N/A /* E3 */ "\xe2\x80\x9e",
2N/A /* E4 */ "\xe2\x80\xb0",
2N/A /* E5 */ "\xc3\x82",
2N/A /* E6 */ "\xc3\x8a",
2N/A /* E7 */ "\xc3\x81",
2N/A /* E8 */ "\xc3\x8b",
2N/A /* E9 */ "\xc3\x88",
2N/A /* EA */ "\xc3\x8d",
2N/A /* EB */ "\xc3\x8e",
2N/A /* EC */ "\xc3\x8f",
2N/A /* ED */ "\xc3\x8c",
2N/A /* EE */ "\xc3\x93",
2N/A /* EF */ "\xc3\x94",
2N/A /* F0 */ "\xef\xa3\xbf",
2N/A /* F1 */ "\xc3\x92",
2N/A /* F2 */ "\xc3\x9a",
2N/A /* F3 */ "\xc3\x9b",
2N/A /* F4 */ "\xc3\x99",
2N/A /* F5 */ "\xc4\xb1",
2N/A /* F6 */ "\xcb\x86",
2N/A /* F7 */ "\xcb\x9c",
2N/A /* F8 */ "\xc2\xaf",
2N/A /* F9 */ "\xcb\x98",
2N/A /* FA */ "\xcb\x99",
2N/A /* FB */ "\xcb\x9a",
2N/A /* FC */ "\xc2\xb8",
2N/A /* FD */ "\xcb\x9d",
2N/A /* FE */ "\xcb\x9b",
2N/A /* FF */ "\xcb\x87",
2N/A };
2N/A
2N/Astatic void
2N/Amacroman_to_utf8 (char *to, const grub_uint8_t *from, grub_size_t len)
2N/A{
2N/A char *optr = to;
2N/A const grub_uint8_t *iptr;
2N/A
2N/A for (iptr = from; iptr < from + len && *iptr; iptr++)
2N/A {
2N/A /* Translate '/' to ':' as per HFS spec. */
2N/A if (*iptr == '/')
2N/A {
2N/A *optr++ = ':';
2N/A continue;
2N/A }
2N/A if (!(*iptr & 0x80))
2N/A {
2N/A *optr++ = *iptr;
2N/A continue;
2N/A }
2N/A optr = grub_stpcpy (optr, macroman[*iptr & 0x7f]);
2N/A }
2N/A *optr = 0;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Autf8_to_macroman (grub_uint8_t *to, const char *from)
2N/A{
2N/A grub_uint8_t *end = to + 31;
2N/A grub_uint8_t *optr = to;
2N/A const char *iptr = from;
2N/A
2N/A while (*iptr && optr < end)
2N/A {
2N/A int i, clen;
2N/A /* Translate ':' to '/' as per HFS spec. */
2N/A if (*iptr == ':')
2N/A {
2N/A *optr++ = '/';
2N/A iptr++;
2N/A continue;
2N/A }
2N/A if (!(*iptr & 0x80))
2N/A {
2N/A *optr++ = *iptr++;
2N/A continue;
2N/A }
2N/A clen = 2;
2N/A if ((*iptr & 0xf0) == 0xe0)
2N/A clen++;
2N/A for (i = 0; i < 0x80; i++)
2N/A if (grub_memcmp (macroman[i], iptr, clen) == 0)
2N/A break;
2N/A if (i == 0x80)
2N/A break;
2N/A *optr++ = i | 0x80;
2N/A iptr += clen;
2N/A }
2N/A /* Too long or not encodable. */
2N/A if (*iptr)
2N/A return -1;
2N/A return optr - to;
2N/A}
2N/A
2N/A
2N/A/* Find a file or directory with the pathname PATH in the filesystem
2N/A DATA. Return the file record in RETDATA when it is non-zero.
2N/A Return the directory number in RETINODE when it is non-zero. */
2N/Astatic grub_err_t
2N/Agrub_hfs_find_dir (struct grub_hfs_data *data, const char *path,
2N/A struct grub_hfs_filerec *retdata, int *retinode)
2N/A{
2N/A int inode = data->rootdir;
2N/A char *next;
2N/A char *origpath;
2N/A union {
2N/A struct grub_hfs_filerec frec;
2N/A struct grub_hfs_dirrec dir;
2N/A } fdrec;
2N/A
2N/A fdrec.frec.type = GRUB_HFS_FILETYPE_DIR;
2N/A
2N/A if (path[0] != '/')
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
2N/A return 0;
2N/A }
2N/A
2N/A origpath = grub_strdup (path);
2N/A if (!origpath)
2N/A return grub_errno;
2N/A
2N/A path = origpath;
2N/A while (*path == '/')
2N/A path++;
2N/A
2N/A while (path && grub_strlen (path))
2N/A {
2N/A grub_ssize_t slen;
2N/A if (fdrec.frec.type != GRUB_HFS_FILETYPE_DIR)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
2N/A goto fail;
2N/A }
2N/A
2N/A /* Isolate a part of the path. */
2N/A next = grub_strchr (path, '/');
2N/A if (next)
2N/A {
2N/A while (*next == '/')
2N/A *(next++) = '\0';
2N/A }
2N/A
2N/A struct grub_hfs_catalog_key key;
2N/A
2N/A key.parent_dir = grub_cpu_to_be32 (inode);
2N/A slen = utf8_to_macroman (key.str, path);
2N/A if (slen < 0)
2N/A {
2N/A grub_error (GRUB_ERR_FILE_NOT_FOUND, "file `%s' not found", path);
2N/A goto fail;
2N/A }
2N/A key.strlen = slen;
2N/A
2N/A /* Lookup this node. */
2N/A if (! grub_hfs_find_node (data, (char *) &key, data->cat_root,
2N/A 0, (char *) &fdrec.frec, sizeof (fdrec.frec)))
2N/A {
2N/A grub_error (GRUB_ERR_FILE_NOT_FOUND, "file `%s' not found", origpath);
2N/A goto fail;
2N/A }
2N/A
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A inode = grub_be_to_cpu32 (fdrec.dir.dirid);
2N/A path = next;
2N/A }
2N/A
2N/A if (retdata)
2N/A grub_memcpy (retdata, &fdrec.frec, sizeof (fdrec.frec));
2N/A
2N/A if (retinode)
2N/A *retinode = inode;
2N/A
2N/A fail:
2N/A grub_free (origpath);
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_hfs_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 int inode;
2N/A
2N/A auto int dir_hook (struct grub_hfs_record *rec);
2N/A
2N/A int dir_hook (struct grub_hfs_record *rec)
2N/A {
2N/A struct grub_hfs_dirrec *drec = rec->data;
2N/A struct grub_hfs_filerec *frec = rec->data;
2N/A struct grub_hfs_catalog_key *ckey = rec->key;
2N/A char fname[sizeof (ckey->str) * MAX_UTF8_PER_MAC_ROMAN + 1] = { 0 };
2N/A struct grub_dirhook_info info;
2N/A grub_size_t len;
2N/A
2N/A grub_memset (&info, 0, sizeof (info));
2N/A
2N/A len = ckey->strlen;
2N/A if (len > sizeof (ckey->str))
2N/A len = sizeof (ckey->str);
2N/A macroman_to_utf8 (fname, ckey->str, len);
2N/A
2N/A info.case_insensitive = 1;
2N/A
2N/A if (drec->type == GRUB_HFS_FILETYPE_DIR)
2N/A {
2N/A info.dir = 1;
2N/A info.mtimeset = 1;
2N/A info.mtime = grub_be_to_cpu32 (drec->mtime) - 2082844800;
2N/A return hook (fname, &info);
2N/A }
2N/A if (frec->type == GRUB_HFS_FILETYPE_FILE)
2N/A {
2N/A info.dir = 0;
2N/A info.mtimeset = 1;
2N/A info.mtime = grub_be_to_cpu32 (frec->mtime) - 2082844800;
2N/A return hook (fname, &info);
2N/A }
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A struct grub_hfs_data *data;
2N/A struct grub_hfs_filerec frec;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_hfs_mount (device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A /* First the directory ID for the directory. */
2N/A if (grub_hfs_find_dir (data, path, &frec, &inode))
2N/A goto fail;
2N/A
2N/A if (frec.type != GRUB_HFS_FILETYPE_DIR)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
2N/A goto fail;
2N/A }
2N/A
2N/A grub_hfs_iterate_dir (data, data->cat_root, inode, dir_hook);
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_hfs_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_hfs_data *data;
2N/A struct grub_hfs_filerec frec;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_hfs_mount (file->device->disk);
2N/A
2N/A if (data && grub_hfs_find_dir (data, name, &frec, 0))
2N/A {
2N/A grub_free (data);
2N/A grub_dl_unref (my_mod);
2N/A return grub_errno;
2N/A }
2N/A
2N/A if (frec.type != GRUB_HFS_FILETYPE_FILE)
2N/A {
2N/A grub_free (data);
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a file");
2N/A grub_dl_unref (my_mod);
2N/A return grub_errno;
2N/A }
2N/A
2N/A grub_memcpy (data->extents, frec.extents, sizeof (grub_hfs_datarecord_t));
2N/A file->size = grub_be_to_cpu32 (frec.size);
2N/A data->size = grub_be_to_cpu32 (frec.size);
2N/A data->fileid = grub_be_to_cpu32 (frec.fileid);
2N/A file->offset = 0;
2N/A
2N/A file->data = data;
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_hfs_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_hfs_data *data =
2N/A (struct grub_hfs_data *) file->data;
2N/A
2N/A return grub_hfs_read_file (data, file->read_hook, file->offset, len, buf);
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_hfs_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 0;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_hfs_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_hfs_data *data;
2N/A
2N/A data = grub_hfs_mount (device->disk);
2N/A
2N/A if (data)
2N/A {
2N/A grub_size_t len = data->sblock.volname[0];
2N/A if (len > sizeof (data->sblock.volname) - 1)
2N/A len = sizeof (data->sblock.volname) - 1;
2N/A *label = grub_malloc (len * MAX_UTF8_PER_MAC_ROMAN + 1);
2N/A if (*label)
2N/A macroman_to_utf8 (*label, data->sblock.volname + 1,
2N/A len + 1);
2N/A }
2N/A else
2N/A *label = 0;
2N/A
2N/A grub_free (data);
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_hfs_mtime (grub_device_t device, grub_int32_t *tm)
2N/A{
2N/A struct grub_hfs_data *data;
2N/A
2N/A data = grub_hfs_mount (device->disk);
2N/A
2N/A if (data)
2N/A *tm = grub_be_to_cpu32 (data->sblock.mtime) - 2082844800;
2N/A else
2N/A *tm = 0;
2N/A
2N/A grub_free (data);
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_hfs_uuid (grub_device_t device, char **uuid)
2N/A{
2N/A struct grub_hfs_data *data;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_hfs_mount (device->disk);
2N/A if (data && data->sblock.num_serial != 0)
2N/A {
2N/A *uuid = grub_xasprintf ("%016llx",
2N/A (unsigned long long)
2N/A grub_be_to_cpu64 (data->sblock.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_hfs_fs =
2N/A {
2N/A .name = "hfs",
2N/A .dir = grub_hfs_dir,
2N/A .open = grub_hfs_open,
2N/A .read = grub_hfs_read,
2N/A .close = grub_hfs_close,
2N/A .label = grub_hfs_label,
2N/A .uuid = grub_hfs_uuid,
2N/A .mtime = grub_hfs_mtime,
2N/A .next = 0
2N/A };
2N/A
2N/AGRUB_MOD_INIT(hfs)
2N/A{
2N/A grub_fs_register (&grub_hfs_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(hfs)
2N/A{
2N/A grub_fs_unregister (&grub_hfs_fs);
2N/A}