vaseboot/VasEBoot-core/commands/macbless.c

236 lines
6.5 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* hfspbless.c - set the hfs+ boot directory. */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2003,2005,2007,2008,2009,2012,2013 Free Software Foundation, Inc.
*
* VasEBoot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VasEBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/command.h>
#include <VasEBoot/fs.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/device.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/hfsplus.h>
#include <VasEBoot/hfs.h>
#include <VasEBoot/partition.h>
#include <VasEBoot/file.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/err.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
struct find_node_context
{
VasEBoot_uint64_t inode_found;
char *dirname;
enum
{ FOUND_NONE, FOUND_FILE, FOUND_DIR } found;
};
static int
find_inode (const char *filename,
const struct VasEBoot_dirhook_info *info, void *data)
{
struct find_node_context *ctx = data;
if (!info->inodeset)
return 0;
if ((VasEBoot_strcmp (ctx->dirname, filename) == 0
|| (info->case_insensitive
&& VasEBoot_strcasecmp (ctx->dirname, filename) == 0)))
{
ctx->inode_found = info->inode;
ctx->found = info->dir ? FOUND_DIR : FOUND_FILE;
}
return 0;
}
VasEBoot_err_t
VasEBoot_mac_bless_inode (VasEBoot_device_t dev, VasEBoot_uint32_t inode, int is_dir,
int intel)
{
VasEBoot_err_t err;
union
{
struct VasEBoot_hfs_sblock hfs;
struct VasEBoot_hfsplus_volheader hfsplus;
} volheader;
VasEBoot_disk_addr_t embedded_offset;
if (intel && is_dir)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT,
"can't bless a directory for mactel");
if (!intel && !is_dir)
return VasEBoot_error (VasEBoot_ERR_NOT_IMPLEMENTED_YET,
"can't bless a file for mac PPC");
/* Read the bootblock. */
err = VasEBoot_disk_read (dev->disk, VasEBoot_HFSPLUS_SBLOCK, 0, sizeof (volheader),
(char *) &volheader);
if (err)
return err;
embedded_offset = 0;
if (VasEBoot_be_to_cpu16 (volheader.hfs.magic) == VasEBoot_HFS_MAGIC)
{
int extent_start;
int ablk_size;
int ablk_start;
/* See if there's an embedded HFS+ filesystem. */
if (VasEBoot_be_to_cpu16 (volheader.hfs.embed_sig) != VasEBoot_HFSPLUS_MAGIC)
{
if (intel)
volheader.hfs.intel_bootfile = VasEBoot_be_to_cpu32 (inode);
else
volheader.hfs.ppc_bootdir = VasEBoot_be_to_cpu32 (inode);
return VasEBoot_ERR_NONE;
}
/* Calculate the offset needed to translate HFS+ sector numbers. */
extent_start =
VasEBoot_be_to_cpu16 (volheader.hfs.embed_extent.first_block);
ablk_size = VasEBoot_be_to_cpu32 (volheader.hfs.blksz);
ablk_start = VasEBoot_be_to_cpu16 (volheader.hfs.first_block);
embedded_offset = (ablk_start
+ ((VasEBoot_uint64_t) extent_start)
* (ablk_size >> VasEBoot_DISK_SECTOR_BITS));
err =
VasEBoot_disk_read (dev->disk, embedded_offset + VasEBoot_HFSPLUS_SBLOCK, 0,
sizeof (volheader), (char *) &volheader);
if (err)
return err;
}
if ((VasEBoot_be_to_cpu16 (volheader.hfsplus.magic) != VasEBoot_HFSPLUS_MAGIC)
&& (VasEBoot_be_to_cpu16 (volheader.hfsplus.magic) != VasEBoot_HFSPLUSX_MAGIC))
return VasEBoot_error (VasEBoot_ERR_BAD_FS, "not a HFS+ filesystem");
if (intel)
volheader.hfsplus.intel_bootfile = VasEBoot_be_to_cpu32 (inode);
else
volheader.hfsplus.ppc_bootdir = VasEBoot_be_to_cpu32 (inode);
return VasEBoot_disk_write (dev->disk, embedded_offset + VasEBoot_HFSPLUS_SBLOCK, 0,
sizeof (volheader), (char *) &volheader);
}
VasEBoot_err_t
VasEBoot_mac_bless_file (VasEBoot_device_t dev, const char *path_in, int intel)
{
VasEBoot_fs_t fs;
char *path, *tail;
struct find_node_context ctx;
fs = VasEBoot_fs_probe (dev);
if (!fs || (VasEBoot_strcmp (fs->name, "hfsplus") != 0
&& VasEBoot_strcmp (fs->name, "hfs") != 0))
return VasEBoot_error (VasEBoot_ERR_BAD_FS, "no suitable FS found");
path = VasEBoot_strdup (path_in);
if (!path)
return VasEBoot_errno;
tail = path + VasEBoot_strlen (path) - 1;
/* Remove trailing '/'. */
while (tail != path && *tail == '/')
*(tail--) = 0;
tail = VasEBoot_strrchr (path, '/');
ctx.found = 0;
if (tail)
{
*tail = 0;
ctx.dirname = tail + 1;
(fs->dir) (dev, *path == 0 ? "/" : path, find_inode, &ctx);
}
else
{
ctx.dirname = path + 1;
(fs->dir) (dev, "/", find_inode, &ctx);
}
if (!ctx.found)
{
VasEBoot_free (path);
return VasEBoot_error (VasEBoot_ERR_FILE_NOT_FOUND, N_("file `%s' not found"),
path_in);
}
VasEBoot_free (path);
return VasEBoot_mac_bless_inode (dev, (VasEBoot_uint32_t) ctx.inode_found,
(ctx.found == FOUND_DIR), intel);
}
static VasEBoot_err_t
VasEBoot_cmd_macbless (VasEBoot_command_t cmd, int argc, char **args)
{
char *device_name;
char *path = 0;
VasEBoot_device_t dev = 0;
VasEBoot_err_t err;
if (argc != 1)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("one argument expected"));
device_name = VasEBoot_file_get_device_name (args[0]);
dev = VasEBoot_device_open (device_name);
path = VasEBoot_strchr (args[0], ')');
if (!path)
path = args[0];
else
path = path + 1;
if (!path || *path == 0 || !dev)
{
if (dev)
VasEBoot_device_close (dev);
VasEBoot_free (device_name);
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "invalid argument");
}
err = VasEBoot_mac_bless_file (dev, path, cmd->name[3] == 't');
VasEBoot_device_close (dev);
VasEBoot_free (device_name);
return err;
}
static VasEBoot_command_t cmd, cmd_ppc;
VasEBoot_MOD_INIT(macbless)
{
cmd = VasEBoot_register_command ("mactelbless", VasEBoot_cmd_macbless,
N_("FILE"),
N_
("Bless FILE of HFS or HFS+ partition for intel macs."));
cmd_ppc =
VasEBoot_register_command ("macppcbless", VasEBoot_cmd_macbless, N_("DIR"),
N_
("Bless DIR of HFS or HFS+ partition for PPC macs."));
}
VasEBoot_MOD_FINI(macbless)
{
VasEBoot_unregister_command (cmd);
VasEBoot_unregister_command (cmd_ppc);
}