vaseboot/VasEBoot-core/commands/nativedisk.c

331 lines
8.3 KiB
C

/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 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/types.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/err.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/device.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/fs.h>
#include <VasEBoot/env.h>
#include <VasEBoot/file.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
static const char *modnames_def[] = {
/* FIXME: autogenerate this. */
#if defined (__i386__) || defined (__x86_64__) || defined (VasEBoot_MACHINE_MIPS_LOONGSON)
"pata", "ahci", "usbms", "ohci", "uhci", "ehci"
#elif defined (VasEBoot_MACHINE_MIPS_QEMU_MIPS)
"pata"
#else
#error "Fill this"
#endif
};
static VasEBoot_err_t
get_uuid (const char *name, char **uuid, int getnative)
{
VasEBoot_device_t dev;
VasEBoot_fs_t fs = 0;
*uuid = 0;
dev = VasEBoot_device_open (name);
if (!dev)
return VasEBoot_errno;
if (!dev->disk)
{
VasEBoot_dprintf ("nativedisk", "Skipping non-disk\n");
VasEBoot_device_close (dev);
return 0;
}
switch (dev->disk->dev->id)
{
/* Firmware disks. */
case VasEBoot_DISK_DEVICE_BIOSDISK_ID:
case VasEBoot_DISK_DEVICE_OFDISK_ID:
case VasEBoot_DISK_DEVICE_EFIDISK_ID:
case VasEBoot_DISK_DEVICE_NAND_ID:
case VasEBoot_DISK_DEVICE_ARCDISK_ID:
case VasEBoot_DISK_DEVICE_HOSTDISK_ID:
case VasEBoot_DISK_DEVICE_UBOOTDISK_ID:
break;
/* Native disks. */
case VasEBoot_DISK_DEVICE_ATA_ID:
case VasEBoot_DISK_DEVICE_SCSI_ID:
case VasEBoot_DISK_DEVICE_XEN:
if (getnative)
break;
/* FALLTHROUGH */
/* Virtual disks. */
/* VasEBoot dynamically generated files. */
case VasEBoot_DISK_DEVICE_PROCFS_ID:
/* To access through host OS routines (VasEBoot-emu only). */
case VasEBoot_DISK_DEVICE_HOST_ID:
/* To access coreboot roms. */
case VasEBoot_DISK_DEVICE_CBFSDISK_ID:
/* VasEBoot-only memdisk. Can't match any of firmware devices. */
case VasEBoot_DISK_DEVICE_MEMDISK_ID:
VasEBoot_dprintf ("nativedisk", "Skipping native disk %s\n",
dev->disk->name);
VasEBoot_device_close (dev);
return 0;
/* FIXME: those probably need special handling. */
case VasEBoot_DISK_DEVICE_LOOPBACK_ID:
case VasEBoot_DISK_DEVICE_DISKFILTER_ID:
case VasEBoot_DISK_DEVICE_CRYPTODISK_ID:
break;
}
if (dev)
fs = VasEBoot_fs_probe (dev);
if (!fs)
{
VasEBoot_device_close (dev);
return VasEBoot_errno;
}
if (!fs->uuid || fs->uuid (dev, uuid) || !*uuid)
{
VasEBoot_device_close (dev);
if (!VasEBoot_errno)
VasEBoot_error (VasEBoot_ERR_NOT_IMPLEMENTED_YET,
N_("%s does not support UUIDs"), fs->name);
return VasEBoot_errno;
}
VasEBoot_device_close (dev);
return VasEBoot_ERR_NONE;
}
struct search_ctx
{
char *root_uuid;
char *prefix_uuid;
const char *prefix_path;
int prefix_found, root_found;
};
static int
iterate_device (const char *name, void *data)
{
struct search_ctx *ctx = data;
char *cur_uuid;
if (get_uuid (name, &cur_uuid, 1))
{
if (VasEBoot_errno == VasEBoot_ERR_UNKNOWN_FS)
VasEBoot_errno = 0;
VasEBoot_print_error ();
return 0;
}
VasEBoot_dprintf ("nativedisk", "checking %s: %s\n", name,
cur_uuid);
if (ctx->prefix_uuid && VasEBoot_strcasecmp (cur_uuid, ctx->prefix_uuid) == 0)
{
char *prefix;
prefix = VasEBoot_xasprintf ("(%s)/%s", name, ctx->prefix_path);
VasEBoot_env_set ("prefix", prefix);
VasEBoot_free (prefix);
ctx->prefix_found = 1;
}
if (ctx->root_uuid && VasEBoot_strcasecmp (cur_uuid, ctx->root_uuid) == 0)
{
VasEBoot_env_set ("root", name);
ctx->root_found = 1;
}
return ctx->prefix_found && ctx->root_found;
}
static VasEBoot_err_t
VasEBoot_cmd_nativedisk (VasEBoot_command_t cmd __attribute__ ((unused)),
int argc, char **args_in)
{
char *uuid_root = 0, *uuid_prefix, *prefdev = 0;
const char *prefix = 0;
const char *path_prefix = 0;
int mods_loaded = 0;
VasEBoot_dl_t *mods;
const char **args;
int i;
if (argc == 0)
{
argc = ARRAY_SIZE (modnames_def);
args = modnames_def;
}
else
args = (const char **) args_in;
prefix = VasEBoot_env_get ("prefix");
if (! prefix)
return VasEBoot_error (VasEBoot_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
if (prefix)
path_prefix = (prefix[0] == '(') ? VasEBoot_strchr (prefix, ')') : NULL;
if (path_prefix)
path_prefix++;
else
path_prefix = prefix;
mods = VasEBoot_malloc (argc * sizeof (mods[0]));
if (!mods)
return VasEBoot_errno;
if (get_uuid (NULL, &uuid_root, 0))
{
VasEBoot_free (mods);
return VasEBoot_errno;
}
prefdev = VasEBoot_file_get_device_name (prefix);
if (VasEBoot_errno)
{
VasEBoot_print_error ();
prefdev = 0;
}
if (get_uuid (prefdev, &uuid_prefix, 0))
{
VasEBoot_free (uuid_root);
VasEBoot_free (prefdev);
VasEBoot_free (mods);
return VasEBoot_errno;
}
VasEBoot_dprintf ("nativedisk", "uuid_prefix = %s, uuid_root = %s\n",
uuid_prefix, uuid_root);
for (mods_loaded = 0; mods_loaded < argc; mods_loaded++)
{
char *filename;
VasEBoot_dl_t mod;
VasEBoot_file_t file = NULL;
VasEBoot_ssize_t size;
void *core = 0;
mod = VasEBoot_dl_get (args[mods_loaded]);
if (mod)
{
mods[mods_loaded] = 0;
continue;
}
filename = VasEBoot_xasprintf ("%s/" VasEBoot_TARGET_CPU "-" VasEBoot_PLATFORM "/%s.mod",
prefix, args[mods_loaded]);
if (! filename)
goto fail;
file = VasEBoot_file_open (filename);
VasEBoot_free (filename);
if (! file)
goto fail;
size = VasEBoot_file_size (file);
core = VasEBoot_malloc (size);
if (! core)
{
VasEBoot_file_close (file);
goto fail;
}
if (VasEBoot_file_read (file, core, size) != (VasEBoot_ssize_t) size)
{
VasEBoot_file_close (file);
VasEBoot_free (core);
goto fail;
}
VasEBoot_file_close (file);
mods[mods_loaded] = VasEBoot_dl_load_core_noinit (core, size);
if (! mods[mods_loaded])
goto fail;
}
for (i = 0; i < argc; i++)
if (mods[i])
VasEBoot_dl_init (mods[i]);
if (uuid_prefix || uuid_root)
{
struct search_ctx ctx;
VasEBoot_fs_autoload_hook_t saved_autoload;
/* No need to autoload FS since obviously we already have the necessary fs modules. */
saved_autoload = VasEBoot_fs_autoload_hook;
VasEBoot_fs_autoload_hook = 0;
ctx.root_uuid = uuid_root;
ctx.prefix_uuid = uuid_prefix;
ctx.prefix_path = path_prefix;
ctx.prefix_found = !uuid_prefix;
ctx.root_found = !uuid_root;
/* FIXME: try to guess the correct values. */
VasEBoot_device_iterate (iterate_device, &ctx);
VasEBoot_fs_autoload_hook = saved_autoload;
}
VasEBoot_free (uuid_root);
VasEBoot_free (uuid_prefix);
VasEBoot_free (prefdev);
VasEBoot_free (mods);
return VasEBoot_ERR_NONE;
fail:
VasEBoot_free (uuid_root);
VasEBoot_free (uuid_prefix);
VasEBoot_free (prefdev);
for (i = 0; i < mods_loaded; i++)
if (mods[i])
{
mods[i]->fini = 0;
VasEBoot_dl_unload (mods[i]);
}
VasEBoot_free (mods);
return VasEBoot_errno;
}
static VasEBoot_command_t cmd;
VasEBoot_MOD_INIT(nativedisk)
{
cmd = VasEBoot_register_command ("nativedisk", VasEBoot_cmd_nativedisk, N_("[MODULE1 MODULE2 ...]"),
N_("Switch to native disk drivers. If no modules are specified default set (pata,ahci,usbms,ohci,uhci,ehci) is used"));
}
VasEBoot_MOD_FINI(nativedisk)
{
VasEBoot_unregister_command (cmd);
}