2013-04-28 13:31:33 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2013 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB 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.
|
|
|
|
*
|
|
|
|
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/command.h>
|
|
|
|
#include <grub/i18n.h>
|
|
|
|
#include <grub/device.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/fs.h>
|
|
|
|
#include <grub/env.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
|
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
static const char *modnames_def[] = {
|
|
|
|
/* FIXME: autogenerate this. */
|
|
|
|
#if defined (__i386__) || defined (__x86_64__) || defined (GRUB_MACHINE_MIPS_LOONGSON)
|
|
|
|
"pata", "ahci", "usbms", "ohci", "uhci", "ehci"
|
|
|
|
#elif defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
|
|
|
|
"pata"
|
|
|
|
#else
|
|
|
|
#error "Fill this"
|
|
|
|
#endif
|
|
|
|
};
|
2013-04-28 13:31:33 +00:00
|
|
|
|
|
|
|
static grub_err_t
|
2013-04-29 10:14:57 +00:00
|
|
|
get_uuid (const char *name, char **uuid, int getnative)
|
2013-04-28 13:31:33 +00:00
|
|
|
{
|
|
|
|
grub_device_t dev;
|
|
|
|
grub_fs_t fs = 0;
|
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
*uuid = 0;
|
|
|
|
|
2013-04-28 13:31:33 +00:00
|
|
|
dev = grub_device_open (name);
|
|
|
|
if (!dev)
|
|
|
|
return grub_errno;
|
2013-04-29 10:14:57 +00:00
|
|
|
|
|
|
|
if (!dev->disk)
|
|
|
|
{
|
|
|
|
grub_dprintf ("nativedisk", "Skipping non-disk\n");
|
2015-01-24 20:18:36 +00:00
|
|
|
grub_device_close (dev);
|
2013-04-29 10:14:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dev->disk->dev->id)
|
|
|
|
{
|
|
|
|
/* Firmware disks. */
|
|
|
|
case GRUB_DISK_DEVICE_BIOSDISK_ID:
|
|
|
|
case GRUB_DISK_DEVICE_OFDISK_ID:
|
ieee1275: obdisk driver
Add a new disk driver called obdisk for IEEE1275 platforms. Currently
the only platform using this disk driver is SPARC, however other IEEE1275
platforms could start using it if they so choose. While the functionality
within the current IEEE1275 ofdisk driver may be suitable for PPC and x86, it
presented too many problems on SPARC hardware.
Within the old ofdisk, there is not a way to determine the true canonical
name for the disk. Within Open Boot, the same disk can have multiple names
but all reference the same disk. For example the same disk can be referenced
by its SAS WWN, using this form:
/pci@302/pci@2/pci@0/pci@17/LSI,sas@0/disk@w5000cca02f037d6d,0
It can also be referenced by its PHY identifier using this form:
/pci@302/pci@2/pci@0/pci@17/LSI,sas@0/disk@p0
It can also be referenced by its Target identifier using this form:
/pci@302/pci@2/pci@0/pci@17/LSI,sas@0/disk@0
Also, when the LUN=0, it is legal to omit the ,0 from the device name. So with
the disk above, before taking into account the device aliases, there are 6 ways
to reference the same disk.
Then it is possible to have 0 .. n device aliases all representing the same disk.
Within this new driver the true canonical name is determined using the the
IEEE1275 encode-unit and decode-unit commands when address_cells == 4. This
will determine the true single canonical name for the device so multiple ihandles
are not opened for the same device. This is what frequently happens with the old
ofdisk driver. With some devices when they are opened multiple times it causes
the entire system to hang.
Another problem solved with this driver is devices that do not have a device
alias can be booted and used within GRUB. Within the old ofdisk, this was not
possible, unless it was the original boot device. All devices behind a SAS
or SCSI parent can be found. Within the old ofdisk, finding these disks
relied on there being an alias defined. The alias requirement is not
necessary with this new driver. It can also find devices behind a parent
after they have been hot-plugged. This is something that is not possible
with the old ofdisk driver.
The old ofdisk driver also incorrectly assumes that the device pointing to by a
device alias is in its true canonical form. This assumption is never made with
this new driver.
Another issue solved with this driver is that it properly caches the ihandle
for all open devices. The old ofdisk tries to do this by caching the last
opened ihandle. However this does not work properly because the layer above
does not use a consistent device name for the same disk when calling into the
driver. This is because the upper layer uses the bootpath value returned within
/chosen, other times it uses the device alias, and other times it uses the
value within grub.cfg. It does not have a way to figure out that these devices
are the same disk. This is not a problem with this new driver.
Due to the way GRUB repeatedly opens and closes the same disk. Caching the
ihandle is important on SPARC. Without caching, some SAS devices can take
15 - 20 minutes to get to the GRUB menu. This ihandle caching is not possible
without correctly having the canonical disk name.
When available, this driver also tries to use the deblocker #blocks and
a way of determining the disk size.
Finally and probably most importantly, this new driver is also capable of
seeing all partitions on a GPT disk. With the old driver, the GPT
partition table can not be read and only the first partition on the disk
can be seen.
Signed-off-by: Eric Snowberg <eric.snowberg@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2019-03-05 01:27:39 +00:00
|
|
|
case GRUB_DISK_DEVICE_OBDISK_ID:
|
2013-04-29 10:14:57 +00:00
|
|
|
case GRUB_DISK_DEVICE_EFIDISK_ID:
|
|
|
|
case GRUB_DISK_DEVICE_NAND_ID:
|
|
|
|
case GRUB_DISK_DEVICE_ARCDISK_ID:
|
|
|
|
case GRUB_DISK_DEVICE_HOSTDISK_ID:
|
2013-07-25 14:31:18 +00:00
|
|
|
case GRUB_DISK_DEVICE_UBOOTDISK_ID:
|
2013-04-29 10:14:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Native disks. */
|
|
|
|
case GRUB_DISK_DEVICE_ATA_ID:
|
|
|
|
case GRUB_DISK_DEVICE_SCSI_ID:
|
2013-11-09 20:29:11 +00:00
|
|
|
case GRUB_DISK_DEVICE_XEN:
|
2013-04-29 10:14:57 +00:00
|
|
|
if (getnative)
|
|
|
|
break;
|
2017-04-04 16:23:55 +00:00
|
|
|
/* FALLTHROUGH */
|
2013-04-29 10:14:57 +00:00
|
|
|
|
|
|
|
/* Virtual disks. */
|
2013-06-15 22:06:13 +00:00
|
|
|
/* GRUB dynamically generated files. */
|
2013-04-29 10:14:57 +00:00
|
|
|
case GRUB_DISK_DEVICE_PROCFS_ID:
|
2013-06-15 22:06:13 +00:00
|
|
|
/* To access through host OS routines (grub-emu only). */
|
2013-04-29 10:14:57 +00:00
|
|
|
case GRUB_DISK_DEVICE_HOST_ID:
|
2013-06-15 22:06:13 +00:00
|
|
|
/* To access coreboot roms. */
|
|
|
|
case GRUB_DISK_DEVICE_CBFSDISK_ID:
|
2013-04-29 10:14:57 +00:00
|
|
|
/* GRUB-only memdisk. Can't match any of firmware devices. */
|
|
|
|
case GRUB_DISK_DEVICE_MEMDISK_ID:
|
|
|
|
grub_dprintf ("nativedisk", "Skipping native disk %s\n",
|
|
|
|
dev->disk->name);
|
2015-01-24 20:18:36 +00:00
|
|
|
grub_device_close (dev);
|
2013-04-29 10:14:57 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* FIXME: those probably need special handling. */
|
|
|
|
case GRUB_DISK_DEVICE_LOOPBACK_ID:
|
|
|
|
case GRUB_DISK_DEVICE_DISKFILTER_ID:
|
|
|
|
case GRUB_DISK_DEVICE_CRYPTODISK_ID:
|
|
|
|
break;
|
|
|
|
}
|
2013-04-28 13:31:33 +00:00
|
|
|
if (dev)
|
|
|
|
fs = grub_fs_probe (dev);
|
|
|
|
if (!fs)
|
|
|
|
{
|
|
|
|
grub_device_close (dev);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
2013-04-29 10:14:57 +00:00
|
|
|
if (!fs->uuid || fs->uuid (dev, uuid) || !*uuid)
|
2013-04-28 13:31:33 +00:00
|
|
|
{
|
|
|
|
grub_device_close (dev);
|
|
|
|
|
|
|
|
if (!grub_errno)
|
|
|
|
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
|
|
|
|
N_("%s does not support UUIDs"), fs->name);
|
|
|
|
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
grub_device_close (dev);
|
|
|
|
return GRUB_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;
|
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
if (get_uuid (name, &cur_uuid, 1))
|
2013-04-28 13:31:33 +00:00
|
|
|
{
|
2013-04-28 16:52:53 +00:00
|
|
|
if (grub_errno == GRUB_ERR_UNKNOWN_FS)
|
|
|
|
grub_errno = 0;
|
2013-04-28 13:31:33 +00:00
|
|
|
grub_print_error ();
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-29 10:14:57 +00:00
|
|
|
|
|
|
|
grub_dprintf ("nativedisk", "checking %s: %s\n", name,
|
|
|
|
cur_uuid);
|
|
|
|
if (ctx->prefix_uuid && grub_strcasecmp (cur_uuid, ctx->prefix_uuid) == 0)
|
2013-04-28 13:31:33 +00:00
|
|
|
{
|
|
|
|
char *prefix;
|
|
|
|
prefix = grub_xasprintf ("(%s)/%s", name, ctx->prefix_path);
|
|
|
|
grub_env_set ("prefix", prefix);
|
|
|
|
grub_free (prefix);
|
|
|
|
ctx->prefix_found = 1;
|
|
|
|
}
|
2013-04-29 10:14:57 +00:00
|
|
|
if (ctx->root_uuid && grub_strcasecmp (cur_uuid, ctx->root_uuid) == 0)
|
2013-04-28 13:31:33 +00:00
|
|
|
{
|
|
|
|
grub_env_set ("root", name);
|
|
|
|
ctx->root_found = 1;
|
|
|
|
}
|
|
|
|
return ctx->prefix_found && ctx->root_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_nativedisk (grub_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;
|
|
|
|
grub_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 = grub_env_get ("prefix");
|
|
|
|
|
|
|
|
if (! prefix)
|
|
|
|
return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
|
|
|
|
|
|
|
|
if (prefix)
|
|
|
|
path_prefix = (prefix[0] == '(') ? grub_strchr (prefix, ')') : NULL;
|
|
|
|
if (path_prefix)
|
|
|
|
path_prefix++;
|
|
|
|
else
|
|
|
|
path_prefix = prefix;
|
|
|
|
|
|
|
|
mods = grub_malloc (argc * sizeof (mods[0]));
|
|
|
|
if (!mods)
|
|
|
|
return grub_errno;
|
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
if (get_uuid (NULL, &uuid_root, 0))
|
2016-01-12 17:37:43 +00:00
|
|
|
{
|
|
|
|
grub_free (mods);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
2013-04-28 13:31:33 +00:00
|
|
|
|
|
|
|
prefdev = grub_file_get_device_name (prefix);
|
|
|
|
if (grub_errno)
|
|
|
|
{
|
|
|
|
grub_print_error ();
|
|
|
|
prefdev = 0;
|
|
|
|
}
|
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
if (get_uuid (prefdev, &uuid_prefix, 0))
|
2013-04-28 13:31:33 +00:00
|
|
|
{
|
|
|
|
grub_free (uuid_root);
|
2016-01-12 17:37:43 +00:00
|
|
|
grub_free (prefdev);
|
|
|
|
grub_free (mods);
|
2013-04-28 13:31:33 +00:00
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
grub_dprintf ("nativedisk", "uuid_prefix = %s, uuid_root = %s\n",
|
|
|
|
uuid_prefix, uuid_root);
|
|
|
|
|
2013-04-28 13:31:33 +00:00
|
|
|
for (mods_loaded = 0; mods_loaded < argc; mods_loaded++)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
grub_dl_t mod;
|
|
|
|
grub_file_t file = NULL;
|
|
|
|
grub_ssize_t size;
|
|
|
|
void *core = 0;
|
|
|
|
|
|
|
|
mod = grub_dl_get (args[mods_loaded]);
|
|
|
|
if (mod)
|
|
|
|
{
|
|
|
|
mods[mods_loaded] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
|
|
|
|
prefix, args[mods_loaded]);
|
|
|
|
if (! filename)
|
|
|
|
goto fail;
|
|
|
|
|
2013-11-20 01:28:29 +00:00
|
|
|
file = grub_file_open (filename,
|
|
|
|
GRUB_FILE_TYPE_GRUB_MODULE);
|
2013-04-28 13:31:33 +00:00
|
|
|
grub_free (filename);
|
|
|
|
if (! file)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
size = grub_file_size (file);
|
|
|
|
core = grub_malloc (size);
|
|
|
|
if (! core)
|
|
|
|
{
|
|
|
|
grub_file_close (file);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grub_file_read (file, core, size) != (grub_ssize_t) size)
|
|
|
|
{
|
|
|
|
grub_file_close (file);
|
|
|
|
grub_free (core);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_file_close (file);
|
|
|
|
|
|
|
|
mods[mods_loaded] = grub_dl_load_core_noinit (core, size);
|
|
|
|
if (! mods[mods_loaded])
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
if (mods[i])
|
|
|
|
grub_dl_init (mods[i]);
|
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
if (uuid_prefix || uuid_root)
|
|
|
|
{
|
|
|
|
struct search_ctx ctx;
|
|
|
|
grub_fs_autoload_hook_t saved_autoload;
|
2013-04-28 13:31:33 +00:00
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
/* No need to autoload FS since obviously we already have the necessary fs modules. */
|
|
|
|
saved_autoload = grub_fs_autoload_hook;
|
|
|
|
grub_fs_autoload_hook = 0;
|
2013-04-28 13:31:33 +00:00
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
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;
|
2013-04-28 13:31:33 +00:00
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
/* FIXME: try to guess the correct values. */
|
|
|
|
grub_device_iterate (iterate_device, &ctx);
|
2013-04-28 13:31:33 +00:00
|
|
|
|
2013-04-29 10:14:57 +00:00
|
|
|
grub_fs_autoload_hook = saved_autoload;
|
|
|
|
}
|
2013-04-28 13:31:33 +00:00
|
|
|
grub_free (uuid_root);
|
|
|
|
grub_free (uuid_prefix);
|
2016-01-12 17:37:43 +00:00
|
|
|
grub_free (prefdev);
|
|
|
|
grub_free (mods);
|
2013-04-28 13:31:33 +00:00
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
grub_free (uuid_root);
|
|
|
|
grub_free (uuid_prefix);
|
2016-01-12 17:37:43 +00:00
|
|
|
grub_free (prefdev);
|
2013-04-28 13:31:33 +00:00
|
|
|
|
|
|
|
for (i = 0; i < mods_loaded; i++)
|
|
|
|
if (mods[i])
|
|
|
|
{
|
|
|
|
mods[i]->fini = 0;
|
|
|
|
grub_dl_unload (mods[i]);
|
|
|
|
}
|
2016-01-12 17:37:43 +00:00
|
|
|
grub_free (mods);
|
|
|
|
|
2013-04-28 13:31:33 +00:00
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_command_t cmd;
|
|
|
|
|
|
|
|
GRUB_MOD_INIT(nativedisk)
|
|
|
|
{
|
2013-05-07 09:41:47 +00:00
|
|
|
cmd = grub_register_command ("nativedisk", grub_cmd_nativedisk, N_("[MODULE1 MODULE2 ...]"),
|
2013-04-28 13:31:33 +00:00
|
|
|
N_("Switch to native disk drivers. If no modules are specified default set (pata,ahci,usbms,ohci,uhci,ehci) is used"));
|
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(nativedisk)
|
|
|
|
{
|
|
|
|
grub_unregister_command (cmd);
|
|
|
|
}
|