2006-04-23 13:37:36 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2009-01-10 13:07:44 +00:00
|
|
|
* Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
|
2006-04-23 13:37:36 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2006-04-23 13:37:36 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2006-04-23 13:37:36 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2006-04-23 13:37:36 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2006-04-23 13:37:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/disk.h>
|
2006-04-25 20:08:31 +00:00
|
|
|
#include <grub/partition.h>
|
2006-04-23 13:37:36 +00:00
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/term.h>
|
|
|
|
#include <grub/efi/api.h>
|
|
|
|
#include <grub/efi/efi.h>
|
|
|
|
#include <grub/efi/disk.h>
|
|
|
|
|
|
|
|
struct grub_efidisk_data
|
|
|
|
{
|
|
|
|
grub_efi_handle_t handle;
|
|
|
|
grub_efi_device_path_t *device_path;
|
|
|
|
grub_efi_device_path_t *last_device_path;
|
|
|
|
grub_efi_block_io_t *block_io;
|
|
|
|
struct grub_efidisk_data *next;
|
|
|
|
};
|
|
|
|
|
2011-04-15 16:15:06 +00:00
|
|
|
/* GUID. */
|
2006-04-23 13:37:36 +00:00
|
|
|
static grub_efi_guid_t block_io_guid = GRUB_EFI_BLOCK_IO_GUID;
|
|
|
|
|
|
|
|
static struct grub_efidisk_data *fd_devices;
|
|
|
|
static struct grub_efidisk_data *hd_devices;
|
|
|
|
static struct grub_efidisk_data *cd_devices;
|
|
|
|
|
|
|
|
/* Duplicate a device path. */
|
|
|
|
static grub_efi_device_path_t *
|
|
|
|
duplicate_device_path (const grub_efi_device_path_t *dp)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *p;
|
|
|
|
grub_size_t total_size = 0;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
for (p = (grub_efi_device_path_t *) dp;
|
|
|
|
;
|
|
|
|
p = GRUB_EFI_NEXT_DEVICE_PATH (p))
|
|
|
|
{
|
|
|
|
total_size += GRUB_EFI_DEVICE_PATH_LENGTH (p);
|
|
|
|
if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (p))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = grub_malloc (total_size);
|
|
|
|
if (! p)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
grub_memcpy (p, dp, total_size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the device path node right before the end node. */
|
|
|
|
static grub_efi_device_path_t *
|
|
|
|
find_last_device_path (const grub_efi_device_path_t *dp)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *next, *p;
|
|
|
|
|
|
|
|
if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (p = (grub_efi_device_path_t *) dp, next = GRUB_EFI_NEXT_DEVICE_PATH (p);
|
|
|
|
! GRUB_EFI_END_ENTIRE_DEVICE_PATH (next);
|
|
|
|
p = next, next = GRUB_EFI_NEXT_DEVICE_PATH (next))
|
|
|
|
;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grub_efidisk_data *
|
|
|
|
make_devices (void)
|
|
|
|
{
|
|
|
|
grub_efi_uintn_t num_handles;
|
|
|
|
grub_efi_handle_t *handles;
|
|
|
|
grub_efi_handle_t *handle;
|
|
|
|
struct grub_efidisk_data *devices = 0;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
/* Find handles which support the disk io interface. */
|
2011-04-15 16:15:06 +00:00
|
|
|
handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &block_io_guid,
|
2006-04-23 13:37:36 +00:00
|
|
|
0, &num_handles);
|
|
|
|
if (! handles)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Make a linked list of devices. */
|
|
|
|
for (handle = handles; num_handles--; handle++)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *dp;
|
|
|
|
grub_efi_device_path_t *ldp;
|
|
|
|
struct grub_efidisk_data *d;
|
|
|
|
grub_efi_block_io_t *bio;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-30 21:09:37 +00:00
|
|
|
dp = grub_efi_get_device_path (*handle);
|
2006-04-23 13:37:36 +00:00
|
|
|
if (! dp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ldp = find_last_device_path (dp);
|
|
|
|
if (! ldp)
|
|
|
|
/* This is empty. Why? */
|
|
|
|
continue;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
bio = grub_efi_open_protocol (*handle, &block_io_guid,
|
|
|
|
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
2011-04-15 16:15:06 +00:00
|
|
|
if (! bio)
|
2006-04-23 13:37:36 +00:00
|
|
|
/* This should not happen... Why? */
|
|
|
|
continue;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
d = grub_malloc (sizeof (*d));
|
|
|
|
if (! d)
|
|
|
|
{
|
|
|
|
/* Uggh. */
|
|
|
|
grub_free (handles);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->handle = *handle;
|
|
|
|
d->device_path = dp;
|
|
|
|
d->last_device_path = ldp;
|
|
|
|
d->block_io = bio;
|
|
|
|
d->next = devices;
|
|
|
|
devices = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_free (handles);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the parent device. */
|
|
|
|
static struct grub_efidisk_data *
|
|
|
|
find_parent_device (struct grub_efidisk_data *devices,
|
|
|
|
struct grub_efidisk_data *d)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *dp, *ldp;
|
|
|
|
struct grub_efidisk_data *parent;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
dp = duplicate_device_path (d->device_path);
|
|
|
|
if (! dp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ldp = find_last_device_path (dp);
|
|
|
|
ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
|
|
|
|
ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
|
|
|
|
ldp->length[0] = sizeof (*ldp);
|
|
|
|
ldp->length[1] = 0;
|
|
|
|
|
|
|
|
for (parent = devices; parent; parent = parent->next)
|
|
|
|
{
|
|
|
|
/* Ignore itself. */
|
|
|
|
if (parent == d)
|
|
|
|
continue;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-07-02 14:56:35 +00:00
|
|
|
if (grub_efi_compare_device_paths (parent->device_path, dp) == 0)
|
2012-06-09 08:43:24 +00:00
|
|
|
break;
|
2006-04-23 13:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grub_free (dp);
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
static int
|
2013-03-01 09:37:11 +00:00
|
|
|
is_child (struct grub_efidisk_data *child,
|
|
|
|
struct grub_efidisk_data *parent)
|
2006-04-25 20:08:31 +00:00
|
|
|
{
|
2013-03-01 09:37:11 +00:00
|
|
|
grub_efi_device_path_t *dp, *ldp;
|
|
|
|
int ret;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-03-01 09:37:11 +00:00
|
|
|
dp = duplicate_device_path (child->device_path);
|
|
|
|
if (! dp)
|
|
|
|
return 0;
|
2006-04-25 20:08:31 +00:00
|
|
|
|
2013-03-01 09:37:11 +00:00
|
|
|
ldp = find_last_device_path (dp);
|
|
|
|
ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
|
|
|
|
ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
|
|
|
|
ldp->length[0] = sizeof (*ldp);
|
|
|
|
ldp->length[1] = 0;
|
2006-04-25 20:08:31 +00:00
|
|
|
|
2013-03-01 09:37:11 +00:00
|
|
|
ret = (grub_efi_compare_device_paths (dp, parent->device_path) == 0);
|
|
|
|
grub_free (dp);
|
|
|
|
return ret;
|
2006-04-25 20:08:31 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 09:37:11 +00:00
|
|
|
#define FOR_CHILDREN(p, dev) for (p = dev; p; p = p->next) if (is_child (p, d))
|
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
/* Add a device into a list of devices in an ascending order. */
|
|
|
|
static void
|
|
|
|
add_device (struct grub_efidisk_data **devices, struct grub_efidisk_data *d)
|
|
|
|
{
|
|
|
|
struct grub_efidisk_data **p;
|
|
|
|
struct grub_efidisk_data *n;
|
|
|
|
|
|
|
|
for (p = devices; *p; p = &((*p)->next))
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-07-02 14:56:35 +00:00
|
|
|
ret = grub_efi_compare_device_paths (find_last_device_path ((*p)->device_path),
|
|
|
|
find_last_device_path (d->device_path));
|
2006-04-25 20:08:31 +00:00
|
|
|
if (ret == 0)
|
2011-07-02 14:56:35 +00:00
|
|
|
ret = grub_efi_compare_device_paths ((*p)->device_path,
|
|
|
|
d->device_path);
|
2006-04-23 13:37:36 +00:00
|
|
|
if (ret == 0)
|
|
|
|
return;
|
|
|
|
else if (ret > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = grub_malloc (sizeof (*n));
|
|
|
|
if (! n)
|
|
|
|
return;
|
|
|
|
|
|
|
|
grub_memcpy (n, d, sizeof (*n));
|
|
|
|
n->next = (*p);
|
|
|
|
(*p) = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Name the devices. */
|
|
|
|
static void
|
|
|
|
name_devices (struct grub_efidisk_data *devices)
|
|
|
|
{
|
|
|
|
struct grub_efidisk_data *d;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
/* First, identify devices by media device paths. */
|
|
|
|
for (d = devices; d; d = d->next)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *dp;
|
|
|
|
|
|
|
|
dp = d->last_device_path;
|
|
|
|
if (! dp)
|
|
|
|
continue;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
if (GRUB_EFI_DEVICE_PATH_TYPE (dp) == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE)
|
|
|
|
{
|
|
|
|
int is_hard_drive = 0;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
switch (GRUB_EFI_DEVICE_PATH_SUBTYPE (dp))
|
|
|
|
{
|
|
|
|
case GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE:
|
|
|
|
is_hard_drive = 1;
|
|
|
|
/* Fall through by intention. */
|
|
|
|
case GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE:
|
|
|
|
{
|
2012-06-07 19:55:55 +00:00
|
|
|
struct grub_efidisk_data *parent, *parent2;
|
2006-04-23 13:37:36 +00:00
|
|
|
|
|
|
|
parent = find_parent_device (devices, d);
|
2012-06-07 19:55:55 +00:00
|
|
|
if (!parent)
|
2006-04-23 13:37:36 +00:00
|
|
|
{
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
2012-06-07 19:55:55 +00:00
|
|
|
grub_printf ("skipping orphaned partition: ");
|
2012-06-09 08:43:24 +00:00
|
|
|
grub_efi_print_device_path (d->device_path);
|
2006-04-23 13:37:36 +00:00
|
|
|
#endif
|
2012-06-07 19:55:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
parent2 = find_parent_device (devices, parent);
|
|
|
|
if (parent2)
|
|
|
|
{
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
2012-06-07 19:55:55 +00:00
|
|
|
grub_printf ("skipping subpartition: ");
|
2012-06-09 08:43:24 +00:00
|
|
|
grub_efi_print_device_path (d->device_path);
|
2006-04-23 13:37:36 +00:00
|
|
|
#endif
|
2012-06-07 19:55:55 +00:00
|
|
|
/* Mark itself as used. */
|
|
|
|
d->last_device_path = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-06-09 08:43:24 +00:00
|
|
|
if (!parent->last_device_path)
|
|
|
|
{
|
|
|
|
d->last_device_path = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-06-07 19:55:55 +00:00
|
|
|
if (is_hard_drive)
|
|
|
|
{
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
2012-06-07 19:55:55 +00:00
|
|
|
grub_printf ("adding a hard drive by a partition: ");
|
|
|
|
grub_efi_print_device_path (parent->device_path);
|
|
|
|
#endif
|
|
|
|
add_device (&hd_devices, parent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
2012-06-07 19:55:55 +00:00
|
|
|
grub_printf ("adding a cdrom by a partition: ");
|
|
|
|
grub_efi_print_device_path (parent->device_path);
|
|
|
|
#endif
|
|
|
|
add_device (&cd_devices, parent);
|
2006-04-23 13:37:36 +00:00
|
|
|
}
|
2012-06-07 19:55:55 +00:00
|
|
|
|
|
|
|
/* Mark the parent as used. */
|
|
|
|
parent->last_device_path = 0;
|
2006-04-23 13:37:36 +00:00
|
|
|
}
|
|
|
|
/* Mark itself as used. */
|
|
|
|
d->last_device_path = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
|
|
|
grub_printf ("skipping other type: ");
|
|
|
|
grub_efi_print_device_path (d->device_path);
|
|
|
|
#endif
|
2006-04-23 13:37:36 +00:00
|
|
|
/* For now, ignore the others. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-06-09 08:43:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_NAMES
|
|
|
|
grub_printf ("skipping non-media: ");
|
|
|
|
grub_efi_print_device_path (d->device_path);
|
|
|
|
#endif
|
|
|
|
}
|
2006-04-23 13:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Let's see what can be added more. */
|
|
|
|
for (d = devices; d; d = d->next)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *dp;
|
|
|
|
grub_efi_block_io_media_t *m;
|
2013-04-17 17:05:57 +00:00
|
|
|
int is_floppy = 0;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
dp = d->last_device_path;
|
|
|
|
if (! dp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
m = d->block_io->media;
|
2013-04-17 17:05:57 +00:00
|
|
|
if (GRUB_EFI_DEVICE_PATH_TYPE (dp) == GRUB_EFI_ACPI_DEVICE_PATH_TYPE
|
|
|
|
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (dp)
|
|
|
|
== GRUB_EFI_ACPI_DEVICE_PATH_SUBTYPE)
|
|
|
|
{
|
|
|
|
grub_efi_acpi_device_path_t *acpi
|
|
|
|
= (grub_efi_acpi_device_path_t *) dp;
|
|
|
|
/* Floppy EISA ID. */
|
|
|
|
if (acpi->hid == 0x60441d0 || acpi->hid == 0x70041d0
|
|
|
|
|| acpi->hid == 0x70141d1)
|
|
|
|
is_floppy = 1;
|
|
|
|
}
|
|
|
|
if (is_floppy)
|
2006-04-23 13:37:36 +00:00
|
|
|
{
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
2013-04-17 17:05:57 +00:00
|
|
|
grub_printf ("adding a floppy: ");
|
2012-06-07 19:55:55 +00:00
|
|
|
grub_efi_print_device_path (d->device_path);
|
2006-04-23 13:37:36 +00:00
|
|
|
#endif
|
|
|
|
add_device (&fd_devices, d);
|
|
|
|
}
|
|
|
|
else if (m->read_only && m->block_size > GRUB_DISK_SECTOR_SIZE)
|
|
|
|
{
|
|
|
|
/* This check is too heuristic, but assume that this is a
|
|
|
|
CDROM drive. */
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
2006-04-23 13:37:36 +00:00
|
|
|
grub_printf ("adding a cdrom by guessing: ");
|
2012-06-07 19:55:55 +00:00
|
|
|
grub_efi_print_device_path (d->device_path);
|
2006-04-23 13:37:36 +00:00
|
|
|
#endif
|
|
|
|
add_device (&cd_devices, d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The default is a hard drive. */
|
2012-06-09 08:43:24 +00:00
|
|
|
#ifdef DEBUG_NAMES
|
2006-04-23 13:37:36 +00:00
|
|
|
grub_printf ("adding a hard drive by guessing: ");
|
2012-06-07 19:55:55 +00:00
|
|
|
grub_efi_print_device_path (d->device_path);
|
2006-04-23 13:37:36 +00:00
|
|
|
#endif
|
|
|
|
add_device (&hd_devices, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_devices (struct grub_efidisk_data *devices)
|
|
|
|
{
|
|
|
|
struct grub_efidisk_data *p, *q;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
for (p = devices; p; p = q)
|
|
|
|
{
|
|
|
|
q = p->next;
|
|
|
|
grub_free (p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enumerate all disks to name devices. */
|
|
|
|
static void
|
|
|
|
enumerate_disks (void)
|
|
|
|
{
|
|
|
|
struct grub_efidisk_data *devices;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
devices = make_devices ();
|
|
|
|
if (! devices)
|
|
|
|
return;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
name_devices (devices);
|
|
|
|
free_devices (devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-01-20 15:52:15 +00:00
|
|
|
grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
|
2011-07-07 10:21:53 +00:00
|
|
|
grub_disk_pull_t pull)
|
2006-04-23 13:37:36 +00:00
|
|
|
{
|
|
|
|
struct grub_efidisk_data *d;
|
|
|
|
char buf[16];
|
|
|
|
int count;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-07-07 10:21:53 +00:00
|
|
|
switch (pull)
|
2006-04-23 13:37:36 +00:00
|
|
|
{
|
2011-07-07 10:21:53 +00:00
|
|
|
case GRUB_DISK_PULL_NONE:
|
|
|
|
for (d = hd_devices, count = 0; d; d = d->next, count++)
|
|
|
|
{
|
|
|
|
grub_snprintf (buf, sizeof (buf), "hd%d", count);
|
|
|
|
grub_dprintf ("efidisk", "iterating %s\n", buf);
|
2013-01-20 15:52:15 +00:00
|
|
|
if (hook (buf, hook_data))
|
2011-07-07 10:21:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GRUB_DISK_PULL_REMOVABLE:
|
|
|
|
for (d = fd_devices, count = 0; d; d = d->next, count++)
|
|
|
|
{
|
|
|
|
grub_snprintf (buf, sizeof (buf), "fd%d", count);
|
|
|
|
grub_dprintf ("efidisk", "iterating %s\n", buf);
|
2013-01-20 15:52:15 +00:00
|
|
|
if (hook (buf, hook_data))
|
2011-07-07 10:21:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-07-07 10:21:53 +00:00
|
|
|
for (d = cd_devices, count = 0; d; d = d->next, count++)
|
|
|
|
{
|
|
|
|
grub_snprintf (buf, sizeof (buf), "cd%d", count);
|
|
|
|
grub_dprintf ("efidisk", "iterating %s\n", buf);
|
2013-01-20 15:52:15 +00:00
|
|
|
if (hook (buf, hook_data))
|
2011-07-07 10:21:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
2006-04-23 13:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_drive_number (const char *name)
|
|
|
|
{
|
|
|
|
unsigned long drive;
|
|
|
|
|
|
|
|
if ((name[0] != 'f' && name[0] != 'h' && name[0] != 'c') || name[1] != 'd')
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
drive = grub_strtoul (name + 2, 0, 10);
|
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return (int) drive ;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a efidisk");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grub_efidisk_data *
|
|
|
|
get_device (struct grub_efidisk_data *devices, int num)
|
|
|
|
{
|
|
|
|
struct grub_efidisk_data *d;
|
|
|
|
|
|
|
|
for (d = devices; d && num; d = d->next, num--)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (num == 0)
|
|
|
|
return d;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
2011-07-07 19:46:25 +00:00
|
|
|
grub_efidisk_open (const char *name, struct grub_disk *disk)
|
2006-04-23 13:37:36 +00:00
|
|
|
{
|
|
|
|
int num;
|
|
|
|
struct grub_efidisk_data *d = 0;
|
|
|
|
grub_efi_block_io_media_t *m;
|
|
|
|
|
|
|
|
grub_dprintf ("efidisk", "opening %s\n", name);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
num = get_drive_number (name);
|
|
|
|
if (num < 0)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
switch (name[0])
|
|
|
|
{
|
|
|
|
case 'f':
|
|
|
|
d = get_device (fd_devices, num);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
d = get_device (cd_devices, num);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
d = get_device (hd_devices, num);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Never reach here. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! d)
|
|
|
|
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such device");
|
|
|
|
|
2013-04-05 08:59:26 +00:00
|
|
|
disk->id = ((num << GRUB_CHAR_BIT) | name[0]);
|
2006-04-23 13:37:36 +00:00
|
|
|
m = d->block_io->media;
|
|
|
|
/* FIXME: Probably it is better to store the block size in the disk,
|
|
|
|
and total sectors should be replaced with total blocks. */
|
|
|
|
grub_dprintf ("efidisk", "m = %p, last block = %llx, block size = %x\n",
|
2008-07-18 01:11:33 +00:00
|
|
|
m, (unsigned long long) m->last_block, m->block_size);
|
2011-07-02 12:09:36 +00:00
|
|
|
disk->total_sectors = m->last_block + 1;
|
2011-04-05 07:34:58 +00:00
|
|
|
if (m->block_size & (m->block_size - 1) || !m->block_size)
|
2011-03-29 00:02:55 +00:00
|
|
|
return grub_error (GRUB_ERR_IO, "invalid sector size %d",
|
2011-04-05 07:34:58 +00:00
|
|
|
m->block_size);
|
2011-03-29 00:02:55 +00:00
|
|
|
for (disk->log_sector_size = 0;
|
2011-04-05 07:34:58 +00:00
|
|
|
(1U << disk->log_sector_size) < m->block_size;
|
2011-03-29 00:02:55 +00:00
|
|
|
disk->log_sector_size++);
|
2006-04-23 13:37:36 +00:00
|
|
|
disk->data = d;
|
|
|
|
|
|
|
|
grub_dprintf ("efidisk", "opening %s succeeded\n", name);
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
grub_efidisk_close (struct grub_disk *disk __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
/* EFI disks do not allocate extra memory, so nothing to do here. */
|
2006-04-30 21:09:37 +00:00
|
|
|
grub_dprintf ("efidisk", "closing %s\n", disk->name);
|
2006-04-23 13:37:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 07:12:14 +00:00
|
|
|
static grub_efi_status_t
|
|
|
|
grub_efidisk_readwrite (struct grub_disk *disk, grub_disk_addr_t sector,
|
|
|
|
grub_size_t size, char *buf, int wr)
|
2006-04-23 13:37:36 +00:00
|
|
|
{
|
|
|
|
/* For now, use the disk io interface rather than the block io's. */
|
|
|
|
struct grub_efidisk_data *d;
|
|
|
|
grub_efi_block_io_t *bio;
|
|
|
|
grub_efi_status_t status;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
d = disk->data;
|
|
|
|
bio = d->block_io;
|
|
|
|
|
2013-04-15 07:12:14 +00:00
|
|
|
while (size > 0)
|
|
|
|
{
|
|
|
|
grub_size_t len;
|
|
|
|
len = 0x500;
|
|
|
|
if (len > size)
|
|
|
|
len = size;
|
|
|
|
status = efi_call_5 ((wr ? bio->write_blocks : bio->read_blocks), bio,
|
|
|
|
bio->media->media_id,
|
|
|
|
(grub_efi_uint64_t) sector,
|
2013-04-16 20:10:59 +00:00
|
|
|
(grub_efi_uintn_t) len << disk->log_sector_size,
|
2013-04-15 07:12:14 +00:00
|
|
|
buf);
|
|
|
|
size -= len;
|
|
|
|
buf += len << disk->log_sector_size;
|
|
|
|
sector += len;
|
|
|
|
if (status != GRUB_EFI_SUCCESS)
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return GRUB_EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_efidisk_read (struct grub_disk *disk, grub_disk_addr_t sector,
|
|
|
|
grub_size_t size, char *buf)
|
|
|
|
{
|
|
|
|
grub_efi_status_t status;
|
|
|
|
|
2006-06-04 Yoshinori K. Okuji <okuji@enbug.org>
Clean up the code to support 64-bit addressing in disks and
files. This change is not enough for filesystems yet.
* util/i386/pc/grub-setup.c (struct boot_blocklist): Change the
type of "start" to grub_uint64_t.
(setup): Change the types of KERNEL_SECTOR and FIRST_SECTOR to
grub_disk_addr_t * and grub_disk_addr_t. Fix the format string in
save_first_sector and save_blocklists. Use grub_le_to_cpu64 to
convert addresses.
* util/i386/pc/biosdisk.c (open_device): Change the type of SECTOR
to grub_disk_addr_t.
* partmap/gpt.c (gpt_partition_map_iterate): Fix the format
string.
* partmap/pc.c (pc_partition_map_iterate): Likewise.
* partmap/amiga.c (amiga_partition_map_iterate): Cast RDSK.MAGIC
to char *.
* normal/script.c (grub_script_parse): Remove unused MEMFREE.
* normal/parser.y (YYLTYPE_IS_TRIVIAL): New macro.
* normal/lexer.c (grub_script_yyerror): Specify unused to LEX.
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf64): Cast -1
to grub_off_t, to detect an error from grub_file_seek.
(grub_multiboot_load_elf32): Likewise.
* kern/misc.c (grub_strtoul): Use grub_strtoull. Return the
maximum unsigned long value when an overflow is detected.
(grub_strtoull): New function.
(grub_divmod64): Likewise.
(grub_lltoa): use grub_divmod64.
* kern/fs.c (struct grub_fs_block): Change the type of "offset" to
grub_disk_addr_t.
(grub_fs_blocklist_open): Increase P if P is not NULL to advance
the pointer to next character. Use grub_strtoull instead of
grub_strtoul.
(grub_fs_blocklist_read): Change the types of SECTOR, OFFSET and
SIZE to grub_disk_addr_t, grub_off_t and grub_size_t,
respectively.
* kern/file.c (grub_file_read): Prevent an oveflow of LEN, as the
return value is signed.
(grub_file_seek): Change the type of OLD to grub_off_t. Do not
test if OFFSET is less than zero, as OFFSET is unsigned now.
* kern/disk.c (struct grub_disk_cache): Change the type of
"sector" to grub_disk_addr_t.
(grub_disk_cache_get_index): Change the type of SECTOR to
grub_disk_addr_t. Calculate the hash with SECTOR casted to
unsigned after shifting.
(grub_disk_cache_invalidate): Change the type of SECTOR to
grub_disk_addr_t.
(grub_disk_cache_unlock): Likewise.
(grub_disk_cache_store): Likewise.
(grub_disk_check_range): Change the types of SECTOR, OFFSET, SIZE,
START and LEN to grub_disk_addr_t *, grub_off_t *, grub_size_t,
grub_disk_addr_t and grub_uint64_t, respectively.
(grub_disk_read): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of START_SECTOR, LEN and
POS to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
(grub_disk_write): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of LEN and N to
grub_size_t.
* io/gzio.c (struct grub_gzio): Change the types of "data_offset"
and "saved_offset" to grub_off_t.
(test_header): Cast BUF to char *.
(get_byte): Cast GZIO->DATA_OFFSET to grub_off_t. Cast GZIO->INBUF
to char *.
(grub_gzio_read): Change the types of OFFSET and SIZE to
grub_off_t and grub_size_t, respectively.
* include/grub/i386/pc/boot.h (GRUB_BOOT_MACHINE_FORCE_LBA):
Removed.
(GRUB_BOOT_MACHINE_BOOT_DRIVE): Changed to 0x4c.
(GRUB_BOOT_MACHINE_KERNEL_ADDRESS): Changed to 0x40.
(GRUB_BOOT_MACHINE_KERNEL_SEGMENT): Changed to 0x42.
(GRUB_BOOT_MACHINE_DRIVE_CHECK): Changed to 0x4e.
(GRUB_BOOT_MACHINE_LIST_SIZE): Increased to 12.
* include/grub/types.h (grub_off_t): Unconditionally set to
grub_uint64_t.
(grub_disk_addr_t): Changed to grub_uint64_t.
* include/grub/partition.h (struct grub_partition): Change the
types of "start", "len" and "offset" to grub_disk_addr_t,
grub_uint64_t and grub_disk_addr_t, respectively.
(grub_partition_get_start): Return grub_disk_addr_t.
(grub_partition_get_len): Return grub_uint64_t.
* include/grub/misc.h (grub_strtoull): New prototype.
(grub_divmod64): Likewise.
* include/grub/fshelp.h (grub_fshelp_read_file): Change the types
of SECTOR, LEN and FILESIZE to grub_disk_addr_t, grub_size_t and
grub_off_t, respectively.
All callers and references changed.
* include/grub/fs.h (struct grub_fs): Change the type of LEN to
grub_size_t in "read".
All callers and references changed.
* include/grub/file.h (struct grub_file): Change the types of
"offset" and "size" to grub_off_t and grub_off_t,
respectively. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_file_read): Change the type of LEN to grub_size_t.
(grub_file_seek): Return grub_off_t. Change the type of OFFSET to
grub_off_t.
(grub_file_size): Return grub_off_t.
(grub_file_tell): Likewise.
All callers and references changed.
* include/grub/disk.h (struct grub_disk_dev): Change the types of
SECTOR and SIZE to grub_disk_addr_t and grub_size_t in "read" and
"write".
(struct grub_disk): Change the type of "total_sectors" to
grub_uint64_t. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_disk_read): Change the types of SECTOR, OFFSET and SIZE to
grub_disk_addr_t, grub_off_t and grub_size_t, respectively.
(grub_disk_write): Likewise.
All callers and references changed.
* fs/iso9660.c (grub_iso9660_susp_iterate): Cast parameters to
char * for grub_strncmp to silence gcc.
(grub_iso9660_mount): Likewise.
(grub_iso9660_mount): Likewise.
(grub_iso9660_read_symlink): Likewise. Also, remove the nonsense
return statement.
(grub_iso9660_iterate_dir): Likewise.
(grub_iso9660_label): Cast DATA->VOLDESC.VOLNAME to char *.
* fs/hfs.c (grub_hfs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.
* fs/jfs.c (grub_jfs_read_file): Likewise.
* fs/minix.c (grub_jfs_read_file): Likewise.
* fs/sfs.c (grub_jfs_read_file): Likewise.
* fs/ufs.c (grub_jfs_read_file): Likewise.
* fs/xfs.c (grub_jfs_read_file): Likewise.
* fs/fat.c (grub_fat_read_data): Change the types of SECTOR, LEN
and SIZE to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
* fs/ext2.c (grub_ext2_read_block): When an error happens, set
BLKNR to -1 instead of returning GRUB_ERRNO.
(grub_ext2_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/affs.c (grub_affs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* font/manager.c (grub_font_get_glyph): Cast BITMAP to char * for
grub_file_read.
* disk/ieee1275/ofdisk.c (grub_ofdisk_read): Fix the format
string. Do not cast SECTOR explicitly.
* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Change the type of
TOTAL_SECTORS to grub_uint64_t. Do not mask DRP->TOTAL_SECTORS.
(grub_biosdisk_rw): Change the types of SECTOR and SIZE to
grub_disk_addr_t and grub_size_t, respectively. If the sector is
over 2TB and LBA mode is not supported, raise an error.
(get_safe_sectors): New function.
(grub_biosdisk_read): Use get_safe_sectors.
(grub_biosdisk_write): Likewise.
* disk/efi/efidisk.c (grub_efidisk_read): Fix the format string.
(grub_efidisk_write): Likewise.
* disk/loopback.c (delete_loopback): Cosmetic changes.
(grub_cmd_loopback): Likewise. Also, test NEWDEV->FILENAME
correctly.
(grub_loopback_open): Likewise.
(grub_loopback_read): Likewise. Also, change the type of POS to
grub_off_t, and fix the usage of grub_memset.
* commands/i386/pc/play.c: Include grub/machine/time.h.
* commands/ls.c (grub_ls_list_files): Use "llu" instead of "d" to
print FILE->SIZE.
* commands/configfile.c: Include grub/env.h.
* commands/cmp.c (grub_cmd_cmp): Do not use ERR, but use
GRUB_ERRNO directly instead. Change the type of POS to
grub_off_t. Follow the coding standard.
* commands/blocklist.c: Include grub/partition.h.
(grub_cmd_blocklist): Return an error if the underlying device is
not a disk. Take the starting sector of a partition into account,
if a partition is used.
* boot/i386/pc/diskboot.S (bootloop): Adapted to the new offset of
a length field.
(lba_mode): Support 64-bit addresses.
(chs_mode): Likewise.
(copy_buffer): Adapted to the new offsets of a length field and a
segment field.
(blocklist_default_start): Allocate 64-bit space.
* boot/i386/pc/boot.S (force_lba): Removed.
(boot_drive): Moved to under KERNEL_SECTOR.
(kernel_sector): Moved to under KENREL_SEGMENT. Allocate 64-bit
space.
(real_start): Set %si earlier. Remove code for FORCE_LBA, since it
is useless.
(lba_mode): Refactored to support a 64-bit address. More size
optimization.
(setup_sectors): Likewise.
2006-06-04 15:56:55 +00:00
|
|
|
grub_dprintf ("efidisk",
|
2008-07-18 01:11:33 +00:00
|
|
|
"reading 0x%lx sectors at the sector 0x%llx from %s\n",
|
|
|
|
(unsigned long) size, (unsigned long long) sector, disk->name);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-04-15 07:12:14 +00:00
|
|
|
status = grub_efidisk_readwrite (disk, sector, size, buf, 0);
|
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
if (status != GRUB_EFI_SUCCESS)
|
2012-02-12 14:25:25 +00:00
|
|
|
return grub_error (GRUB_ERR_READ_ERROR,
|
|
|
|
N_("failure reading sector 0x%llx from `%s'"),
|
|
|
|
(unsigned long long) sector,
|
|
|
|
disk->name);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
2006-06-04 Yoshinori K. Okuji <okuji@enbug.org>
Clean up the code to support 64-bit addressing in disks and
files. This change is not enough for filesystems yet.
* util/i386/pc/grub-setup.c (struct boot_blocklist): Change the
type of "start" to grub_uint64_t.
(setup): Change the types of KERNEL_SECTOR and FIRST_SECTOR to
grub_disk_addr_t * and grub_disk_addr_t. Fix the format string in
save_first_sector and save_blocklists. Use grub_le_to_cpu64 to
convert addresses.
* util/i386/pc/biosdisk.c (open_device): Change the type of SECTOR
to grub_disk_addr_t.
* partmap/gpt.c (gpt_partition_map_iterate): Fix the format
string.
* partmap/pc.c (pc_partition_map_iterate): Likewise.
* partmap/amiga.c (amiga_partition_map_iterate): Cast RDSK.MAGIC
to char *.
* normal/script.c (grub_script_parse): Remove unused MEMFREE.
* normal/parser.y (YYLTYPE_IS_TRIVIAL): New macro.
* normal/lexer.c (grub_script_yyerror): Specify unused to LEX.
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf64): Cast -1
to grub_off_t, to detect an error from grub_file_seek.
(grub_multiboot_load_elf32): Likewise.
* kern/misc.c (grub_strtoul): Use grub_strtoull. Return the
maximum unsigned long value when an overflow is detected.
(grub_strtoull): New function.
(grub_divmod64): Likewise.
(grub_lltoa): use grub_divmod64.
* kern/fs.c (struct grub_fs_block): Change the type of "offset" to
grub_disk_addr_t.
(grub_fs_blocklist_open): Increase P if P is not NULL to advance
the pointer to next character. Use grub_strtoull instead of
grub_strtoul.
(grub_fs_blocklist_read): Change the types of SECTOR, OFFSET and
SIZE to grub_disk_addr_t, grub_off_t and grub_size_t,
respectively.
* kern/file.c (grub_file_read): Prevent an oveflow of LEN, as the
return value is signed.
(grub_file_seek): Change the type of OLD to grub_off_t. Do not
test if OFFSET is less than zero, as OFFSET is unsigned now.
* kern/disk.c (struct grub_disk_cache): Change the type of
"sector" to grub_disk_addr_t.
(grub_disk_cache_get_index): Change the type of SECTOR to
grub_disk_addr_t. Calculate the hash with SECTOR casted to
unsigned after shifting.
(grub_disk_cache_invalidate): Change the type of SECTOR to
grub_disk_addr_t.
(grub_disk_cache_unlock): Likewise.
(grub_disk_cache_store): Likewise.
(grub_disk_check_range): Change the types of SECTOR, OFFSET, SIZE,
START and LEN to grub_disk_addr_t *, grub_off_t *, grub_size_t,
grub_disk_addr_t and grub_uint64_t, respectively.
(grub_disk_read): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of START_SECTOR, LEN and
POS to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
(grub_disk_write): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of LEN and N to
grub_size_t.
* io/gzio.c (struct grub_gzio): Change the types of "data_offset"
and "saved_offset" to grub_off_t.
(test_header): Cast BUF to char *.
(get_byte): Cast GZIO->DATA_OFFSET to grub_off_t. Cast GZIO->INBUF
to char *.
(grub_gzio_read): Change the types of OFFSET and SIZE to
grub_off_t and grub_size_t, respectively.
* include/grub/i386/pc/boot.h (GRUB_BOOT_MACHINE_FORCE_LBA):
Removed.
(GRUB_BOOT_MACHINE_BOOT_DRIVE): Changed to 0x4c.
(GRUB_BOOT_MACHINE_KERNEL_ADDRESS): Changed to 0x40.
(GRUB_BOOT_MACHINE_KERNEL_SEGMENT): Changed to 0x42.
(GRUB_BOOT_MACHINE_DRIVE_CHECK): Changed to 0x4e.
(GRUB_BOOT_MACHINE_LIST_SIZE): Increased to 12.
* include/grub/types.h (grub_off_t): Unconditionally set to
grub_uint64_t.
(grub_disk_addr_t): Changed to grub_uint64_t.
* include/grub/partition.h (struct grub_partition): Change the
types of "start", "len" and "offset" to grub_disk_addr_t,
grub_uint64_t and grub_disk_addr_t, respectively.
(grub_partition_get_start): Return grub_disk_addr_t.
(grub_partition_get_len): Return grub_uint64_t.
* include/grub/misc.h (grub_strtoull): New prototype.
(grub_divmod64): Likewise.
* include/grub/fshelp.h (grub_fshelp_read_file): Change the types
of SECTOR, LEN and FILESIZE to grub_disk_addr_t, grub_size_t and
grub_off_t, respectively.
All callers and references changed.
* include/grub/fs.h (struct grub_fs): Change the type of LEN to
grub_size_t in "read".
All callers and references changed.
* include/grub/file.h (struct grub_file): Change the types of
"offset" and "size" to grub_off_t and grub_off_t,
respectively. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_file_read): Change the type of LEN to grub_size_t.
(grub_file_seek): Return grub_off_t. Change the type of OFFSET to
grub_off_t.
(grub_file_size): Return grub_off_t.
(grub_file_tell): Likewise.
All callers and references changed.
* include/grub/disk.h (struct grub_disk_dev): Change the types of
SECTOR and SIZE to grub_disk_addr_t and grub_size_t in "read" and
"write".
(struct grub_disk): Change the type of "total_sectors" to
grub_uint64_t. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_disk_read): Change the types of SECTOR, OFFSET and SIZE to
grub_disk_addr_t, grub_off_t and grub_size_t, respectively.
(grub_disk_write): Likewise.
All callers and references changed.
* fs/iso9660.c (grub_iso9660_susp_iterate): Cast parameters to
char * for grub_strncmp to silence gcc.
(grub_iso9660_mount): Likewise.
(grub_iso9660_mount): Likewise.
(grub_iso9660_read_symlink): Likewise. Also, remove the nonsense
return statement.
(grub_iso9660_iterate_dir): Likewise.
(grub_iso9660_label): Cast DATA->VOLDESC.VOLNAME to char *.
* fs/hfs.c (grub_hfs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.
* fs/jfs.c (grub_jfs_read_file): Likewise.
* fs/minix.c (grub_jfs_read_file): Likewise.
* fs/sfs.c (grub_jfs_read_file): Likewise.
* fs/ufs.c (grub_jfs_read_file): Likewise.
* fs/xfs.c (grub_jfs_read_file): Likewise.
* fs/fat.c (grub_fat_read_data): Change the types of SECTOR, LEN
and SIZE to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
* fs/ext2.c (grub_ext2_read_block): When an error happens, set
BLKNR to -1 instead of returning GRUB_ERRNO.
(grub_ext2_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/affs.c (grub_affs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* font/manager.c (grub_font_get_glyph): Cast BITMAP to char * for
grub_file_read.
* disk/ieee1275/ofdisk.c (grub_ofdisk_read): Fix the format
string. Do not cast SECTOR explicitly.
* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Change the type of
TOTAL_SECTORS to grub_uint64_t. Do not mask DRP->TOTAL_SECTORS.
(grub_biosdisk_rw): Change the types of SECTOR and SIZE to
grub_disk_addr_t and grub_size_t, respectively. If the sector is
over 2TB and LBA mode is not supported, raise an error.
(get_safe_sectors): New function.
(grub_biosdisk_read): Use get_safe_sectors.
(grub_biosdisk_write): Likewise.
* disk/efi/efidisk.c (grub_efidisk_read): Fix the format string.
(grub_efidisk_write): Likewise.
* disk/loopback.c (delete_loopback): Cosmetic changes.
(grub_cmd_loopback): Likewise. Also, test NEWDEV->FILENAME
correctly.
(grub_loopback_open): Likewise.
(grub_loopback_read): Likewise. Also, change the type of POS to
grub_off_t, and fix the usage of grub_memset.
* commands/i386/pc/play.c: Include grub/machine/time.h.
* commands/ls.c (grub_ls_list_files): Use "llu" instead of "d" to
print FILE->SIZE.
* commands/configfile.c: Include grub/env.h.
* commands/cmp.c (grub_cmd_cmp): Do not use ERR, but use
GRUB_ERRNO directly instead. Change the type of POS to
grub_off_t. Follow the coding standard.
* commands/blocklist.c: Include grub/partition.h.
(grub_cmd_blocklist): Return an error if the underlying device is
not a disk. Take the starting sector of a partition into account,
if a partition is used.
* boot/i386/pc/diskboot.S (bootloop): Adapted to the new offset of
a length field.
(lba_mode): Support 64-bit addresses.
(chs_mode): Likewise.
(copy_buffer): Adapted to the new offsets of a length field and a
segment field.
(blocklist_default_start): Allocate 64-bit space.
* boot/i386/pc/boot.S (force_lba): Removed.
(boot_drive): Moved to under KERNEL_SECTOR.
(kernel_sector): Moved to under KENREL_SEGMENT. Allocate 64-bit
space.
(real_start): Set %si earlier. Remove code for FORCE_LBA, since it
is useless.
(lba_mode): Refactored to support a 64-bit address. More size
optimization.
(setup_sectors): Likewise.
2006-06-04 15:56:55 +00:00
|
|
|
grub_efidisk_write (struct grub_disk *disk, grub_disk_addr_t sector,
|
|
|
|
grub_size_t size, const char *buf)
|
2006-04-23 13:37:36 +00:00
|
|
|
{
|
|
|
|
grub_efi_status_t status;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-06-04 Yoshinori K. Okuji <okuji@enbug.org>
Clean up the code to support 64-bit addressing in disks and
files. This change is not enough for filesystems yet.
* util/i386/pc/grub-setup.c (struct boot_blocklist): Change the
type of "start" to grub_uint64_t.
(setup): Change the types of KERNEL_SECTOR and FIRST_SECTOR to
grub_disk_addr_t * and grub_disk_addr_t. Fix the format string in
save_first_sector and save_blocklists. Use grub_le_to_cpu64 to
convert addresses.
* util/i386/pc/biosdisk.c (open_device): Change the type of SECTOR
to grub_disk_addr_t.
* partmap/gpt.c (gpt_partition_map_iterate): Fix the format
string.
* partmap/pc.c (pc_partition_map_iterate): Likewise.
* partmap/amiga.c (amiga_partition_map_iterate): Cast RDSK.MAGIC
to char *.
* normal/script.c (grub_script_parse): Remove unused MEMFREE.
* normal/parser.y (YYLTYPE_IS_TRIVIAL): New macro.
* normal/lexer.c (grub_script_yyerror): Specify unused to LEX.
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf64): Cast -1
to grub_off_t, to detect an error from grub_file_seek.
(grub_multiboot_load_elf32): Likewise.
* kern/misc.c (grub_strtoul): Use grub_strtoull. Return the
maximum unsigned long value when an overflow is detected.
(grub_strtoull): New function.
(grub_divmod64): Likewise.
(grub_lltoa): use grub_divmod64.
* kern/fs.c (struct grub_fs_block): Change the type of "offset" to
grub_disk_addr_t.
(grub_fs_blocklist_open): Increase P if P is not NULL to advance
the pointer to next character. Use grub_strtoull instead of
grub_strtoul.
(grub_fs_blocklist_read): Change the types of SECTOR, OFFSET and
SIZE to grub_disk_addr_t, grub_off_t and grub_size_t,
respectively.
* kern/file.c (grub_file_read): Prevent an oveflow of LEN, as the
return value is signed.
(grub_file_seek): Change the type of OLD to grub_off_t. Do not
test if OFFSET is less than zero, as OFFSET is unsigned now.
* kern/disk.c (struct grub_disk_cache): Change the type of
"sector" to grub_disk_addr_t.
(grub_disk_cache_get_index): Change the type of SECTOR to
grub_disk_addr_t. Calculate the hash with SECTOR casted to
unsigned after shifting.
(grub_disk_cache_invalidate): Change the type of SECTOR to
grub_disk_addr_t.
(grub_disk_cache_unlock): Likewise.
(grub_disk_cache_store): Likewise.
(grub_disk_check_range): Change the types of SECTOR, OFFSET, SIZE,
START and LEN to grub_disk_addr_t *, grub_off_t *, grub_size_t,
grub_disk_addr_t and grub_uint64_t, respectively.
(grub_disk_read): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of START_SECTOR, LEN and
POS to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
(grub_disk_write): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of LEN and N to
grub_size_t.
* io/gzio.c (struct grub_gzio): Change the types of "data_offset"
and "saved_offset" to grub_off_t.
(test_header): Cast BUF to char *.
(get_byte): Cast GZIO->DATA_OFFSET to grub_off_t. Cast GZIO->INBUF
to char *.
(grub_gzio_read): Change the types of OFFSET and SIZE to
grub_off_t and grub_size_t, respectively.
* include/grub/i386/pc/boot.h (GRUB_BOOT_MACHINE_FORCE_LBA):
Removed.
(GRUB_BOOT_MACHINE_BOOT_DRIVE): Changed to 0x4c.
(GRUB_BOOT_MACHINE_KERNEL_ADDRESS): Changed to 0x40.
(GRUB_BOOT_MACHINE_KERNEL_SEGMENT): Changed to 0x42.
(GRUB_BOOT_MACHINE_DRIVE_CHECK): Changed to 0x4e.
(GRUB_BOOT_MACHINE_LIST_SIZE): Increased to 12.
* include/grub/types.h (grub_off_t): Unconditionally set to
grub_uint64_t.
(grub_disk_addr_t): Changed to grub_uint64_t.
* include/grub/partition.h (struct grub_partition): Change the
types of "start", "len" and "offset" to grub_disk_addr_t,
grub_uint64_t and grub_disk_addr_t, respectively.
(grub_partition_get_start): Return grub_disk_addr_t.
(grub_partition_get_len): Return grub_uint64_t.
* include/grub/misc.h (grub_strtoull): New prototype.
(grub_divmod64): Likewise.
* include/grub/fshelp.h (grub_fshelp_read_file): Change the types
of SECTOR, LEN and FILESIZE to grub_disk_addr_t, grub_size_t and
grub_off_t, respectively.
All callers and references changed.
* include/grub/fs.h (struct grub_fs): Change the type of LEN to
grub_size_t in "read".
All callers and references changed.
* include/grub/file.h (struct grub_file): Change the types of
"offset" and "size" to grub_off_t and grub_off_t,
respectively. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_file_read): Change the type of LEN to grub_size_t.
(grub_file_seek): Return grub_off_t. Change the type of OFFSET to
grub_off_t.
(grub_file_size): Return grub_off_t.
(grub_file_tell): Likewise.
All callers and references changed.
* include/grub/disk.h (struct grub_disk_dev): Change the types of
SECTOR and SIZE to grub_disk_addr_t and grub_size_t in "read" and
"write".
(struct grub_disk): Change the type of "total_sectors" to
grub_uint64_t. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_disk_read): Change the types of SECTOR, OFFSET and SIZE to
grub_disk_addr_t, grub_off_t and grub_size_t, respectively.
(grub_disk_write): Likewise.
All callers and references changed.
* fs/iso9660.c (grub_iso9660_susp_iterate): Cast parameters to
char * for grub_strncmp to silence gcc.
(grub_iso9660_mount): Likewise.
(grub_iso9660_mount): Likewise.
(grub_iso9660_read_symlink): Likewise. Also, remove the nonsense
return statement.
(grub_iso9660_iterate_dir): Likewise.
(grub_iso9660_label): Cast DATA->VOLDESC.VOLNAME to char *.
* fs/hfs.c (grub_hfs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.
* fs/jfs.c (grub_jfs_read_file): Likewise.
* fs/minix.c (grub_jfs_read_file): Likewise.
* fs/sfs.c (grub_jfs_read_file): Likewise.
* fs/ufs.c (grub_jfs_read_file): Likewise.
* fs/xfs.c (grub_jfs_read_file): Likewise.
* fs/fat.c (grub_fat_read_data): Change the types of SECTOR, LEN
and SIZE to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
* fs/ext2.c (grub_ext2_read_block): When an error happens, set
BLKNR to -1 instead of returning GRUB_ERRNO.
(grub_ext2_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/affs.c (grub_affs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* font/manager.c (grub_font_get_glyph): Cast BITMAP to char * for
grub_file_read.
* disk/ieee1275/ofdisk.c (grub_ofdisk_read): Fix the format
string. Do not cast SECTOR explicitly.
* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Change the type of
TOTAL_SECTORS to grub_uint64_t. Do not mask DRP->TOTAL_SECTORS.
(grub_biosdisk_rw): Change the types of SECTOR and SIZE to
grub_disk_addr_t and grub_size_t, respectively. If the sector is
over 2TB and LBA mode is not supported, raise an error.
(get_safe_sectors): New function.
(grub_biosdisk_read): Use get_safe_sectors.
(grub_biosdisk_write): Likewise.
* disk/efi/efidisk.c (grub_efidisk_read): Fix the format string.
(grub_efidisk_write): Likewise.
* disk/loopback.c (delete_loopback): Cosmetic changes.
(grub_cmd_loopback): Likewise. Also, test NEWDEV->FILENAME
correctly.
(grub_loopback_open): Likewise.
(grub_loopback_read): Likewise. Also, change the type of POS to
grub_off_t, and fix the usage of grub_memset.
* commands/i386/pc/play.c: Include grub/machine/time.h.
* commands/ls.c (grub_ls_list_files): Use "llu" instead of "d" to
print FILE->SIZE.
* commands/configfile.c: Include grub/env.h.
* commands/cmp.c (grub_cmd_cmp): Do not use ERR, but use
GRUB_ERRNO directly instead. Change the type of POS to
grub_off_t. Follow the coding standard.
* commands/blocklist.c: Include grub/partition.h.
(grub_cmd_blocklist): Return an error if the underlying device is
not a disk. Take the starting sector of a partition into account,
if a partition is used.
* boot/i386/pc/diskboot.S (bootloop): Adapted to the new offset of
a length field.
(lba_mode): Support 64-bit addresses.
(chs_mode): Likewise.
(copy_buffer): Adapted to the new offsets of a length field and a
segment field.
(blocklist_default_start): Allocate 64-bit space.
* boot/i386/pc/boot.S (force_lba): Removed.
(boot_drive): Moved to under KERNEL_SECTOR.
(kernel_sector): Moved to under KENREL_SEGMENT. Allocate 64-bit
space.
(real_start): Set %si earlier. Remove code for FORCE_LBA, since it
is useless.
(lba_mode): Refactored to support a 64-bit address. More size
optimization.
(setup_sectors): Likewise.
2006-06-04 15:56:55 +00:00
|
|
|
grub_dprintf ("efidisk",
|
2008-07-18 01:11:33 +00:00
|
|
|
"writing 0x%lx sectors at the sector 0x%llx to %s\n",
|
|
|
|
(unsigned long) size, (unsigned long long) sector, disk->name);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-04-15 07:12:14 +00:00
|
|
|
status = grub_efidisk_readwrite (disk, sector, size, (char *) buf, 1);
|
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
if (status != GRUB_EFI_SUCCESS)
|
2012-02-12 14:25:25 +00:00
|
|
|
return grub_error (GRUB_ERR_WRITE_ERROR,
|
|
|
|
N_("failure writing sector 0x%llx to `%s'"),
|
|
|
|
(unsigned long long) sector, disk->name);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grub_disk_dev grub_efidisk_dev =
|
|
|
|
{
|
|
|
|
.name = "efidisk",
|
|
|
|
.id = GRUB_DISK_DEVICE_EFIDISK_ID,
|
|
|
|
.iterate = grub_efidisk_iterate,
|
|
|
|
.open = grub_efidisk_open,
|
|
|
|
.close = grub_efidisk_close,
|
|
|
|
.read = grub_efidisk_read,
|
|
|
|
.write = grub_efidisk_write,
|
|
|
|
.next = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_efidisk_fini (void)
|
|
|
|
{
|
|
|
|
free_devices (fd_devices);
|
|
|
|
free_devices (hd_devices);
|
|
|
|
free_devices (cd_devices);
|
2013-04-29 10:16:46 +00:00
|
|
|
fd_devices = 0;
|
|
|
|
hd_devices = 0;
|
|
|
|
cd_devices = 0;
|
2006-04-23 13:37:36 +00:00
|
|
|
grub_disk_dev_unregister (&grub_efidisk_dev);
|
|
|
|
}
|
2006-04-25 20:08:31 +00:00
|
|
|
|
2013-04-29 10:16:46 +00:00
|
|
|
void
|
|
|
|
grub_efidisk_init (void)
|
|
|
|
{
|
|
|
|
grub_disk_firmware_fini = grub_efidisk_fini;
|
|
|
|
|
|
|
|
enumerate_disks ();
|
|
|
|
grub_disk_dev_register (&grub_efidisk_dev);
|
|
|
|
}
|
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
/* Some utility functions to map GRUB devices with EFI devices. */
|
|
|
|
grub_efi_handle_t
|
|
|
|
grub_efidisk_get_device_handle (grub_disk_t disk)
|
|
|
|
{
|
|
|
|
struct grub_efidisk_data *d;
|
|
|
|
char type;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
if (disk->dev->id != GRUB_DISK_DEVICE_EFIDISK_ID)
|
|
|
|
return 0;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
d = disk->data;
|
|
|
|
type = disk->name[0];
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 'f':
|
|
|
|
/* This is the simplest case. */
|
|
|
|
return d->handle;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
/* FIXME: probably this is not correct. */
|
|
|
|
return d->handle;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
/* If this is the whole disk, just return its own data. */
|
|
|
|
if (! disk->partition)
|
|
|
|
return d->handle;
|
|
|
|
|
|
|
|
/* Otherwise, we must query the corresponding device to the firmware. */
|
|
|
|
{
|
|
|
|
struct grub_efidisk_data *devices;
|
|
|
|
grub_efi_handle_t handle = 0;
|
2013-03-01 09:37:11 +00:00
|
|
|
struct grub_efidisk_data *c;
|
2006-04-25 20:08:31 +00:00
|
|
|
|
2013-03-01 09:37:11 +00:00
|
|
|
devices = make_devices ();
|
|
|
|
FOR_CHILDREN (c, devices)
|
2006-04-25 20:08:31 +00:00
|
|
|
{
|
|
|
|
grub_efi_hard_drive_device_path_t hd;
|
|
|
|
|
|
|
|
grub_memcpy (&hd, c->last_device_path, sizeof (hd));
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
if ((GRUB_EFI_DEVICE_PATH_TYPE (c->last_device_path)
|
|
|
|
== GRUB_EFI_MEDIA_DEVICE_PATH_TYPE)
|
2006-04-30 21:09:37 +00:00
|
|
|
&& (GRUB_EFI_DEVICE_PATH_SUBTYPE (c->last_device_path)
|
2006-04-25 20:08:31 +00:00
|
|
|
== GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE)
|
2013-04-04 08:35:50 +00:00
|
|
|
&& (grub_partition_get_start (disk->partition)
|
|
|
|
== (hd.partition_start << (disk->log_sector_size
|
|
|
|
- GRUB_DISK_SECTOR_BITS)))
|
2006-04-25 20:08:31 +00:00
|
|
|
&& (grub_partition_get_len (disk->partition)
|
2013-04-04 08:35:50 +00:00
|
|
|
== (hd.partition_size << (disk->log_sector_size
|
|
|
|
- GRUB_DISK_SECTOR_BITS))))
|
2006-04-25 20:08:31 +00:00
|
|
|
{
|
|
|
|
handle = c->handle;
|
2013-03-01 09:37:11 +00:00
|
|
|
break;
|
2006-04-25 20:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
free_devices (devices);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
if (handle != 0)
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2006-04-25 20:08:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-28 12:26:20 +00:00
|
|
|
#define NEEDED_BUFLEN sizeof ("XdXXXXXXXXXX")
|
|
|
|
static inline int
|
|
|
|
get_diskname_from_path_real (const grub_efi_device_path_t *path,
|
|
|
|
struct grub_efidisk_data *head,
|
|
|
|
char *buf)
|
2006-04-25 20:08:31 +00:00
|
|
|
{
|
2012-01-28 12:26:20 +00:00
|
|
|
int count = 0;
|
|
|
|
struct grub_efidisk_data *d;
|
|
|
|
for (d = head, count = 0; d; d = d->next, count++)
|
|
|
|
if (grub_efi_compare_device_paths (d->device_path, path) == 0)
|
|
|
|
{
|
|
|
|
grub_snprintf (buf, NEEDED_BUFLEN - 1, "d%d", count);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-02 14:56:35 +00:00
|
|
|
|
2012-01-28 12:26:20 +00:00
|
|
|
static inline int
|
|
|
|
get_diskname_from_path (const grub_efi_device_path_t *path,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
if (get_diskname_from_path_real (path, hd_devices, buf + 1))
|
|
|
|
{
|
|
|
|
buf[0] = 'h';
|
2011-07-02 14:56:35 +00:00
|
|
|
return 1;
|
2012-01-28 12:26:20 +00:00
|
|
|
}
|
2011-07-02 14:56:35 +00:00
|
|
|
|
2012-01-28 12:26:20 +00:00
|
|
|
if (get_diskname_from_path_real (path, fd_devices, buf + 1))
|
|
|
|
{
|
|
|
|
buf[0] = 'f';
|
|
|
|
return 1;
|
|
|
|
}
|
2011-07-02 14:56:35 +00:00
|
|
|
|
2012-01-28 12:26:20 +00:00
|
|
|
if (get_diskname_from_path_real (path, cd_devices, buf + 1))
|
|
|
|
{
|
|
|
|
buf[0] = 'c';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-02 14:56:35 +00:00
|
|
|
|
2013-01-20 15:52:15 +00:00
|
|
|
/* Context for grub_efidisk_get_device_name. */
|
|
|
|
struct grub_efidisk_get_device_name_ctx
|
|
|
|
{
|
|
|
|
char *partition_name;
|
|
|
|
grub_efi_hard_drive_device_path_t hd;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper for grub_efidisk_get_device_name.
|
|
|
|
Find the identical partition. */
|
|
|
|
static int
|
2013-09-18 14:43:58 +00:00
|
|
|
grub_efidisk_get_device_name_iter (grub_disk_t disk,
|
2013-01-20 15:52:15 +00:00
|
|
|
const grub_partition_t part, void *data)
|
|
|
|
{
|
|
|
|
struct grub_efidisk_get_device_name_ctx *ctx = data;
|
|
|
|
|
2013-04-04 08:35:50 +00:00
|
|
|
if (grub_partition_get_start (part)
|
|
|
|
== (ctx->hd.partition_start << (disk->log_sector_size
|
|
|
|
- GRUB_DISK_SECTOR_BITS))
|
|
|
|
&& grub_partition_get_len (part)
|
|
|
|
== (ctx->hd.partition_size << (disk->log_sector_size
|
|
|
|
- GRUB_DISK_SECTOR_BITS)))
|
2013-01-20 15:52:15 +00:00
|
|
|
{
|
|
|
|
ctx->partition_name = grub_partition_get_name (part);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-28 12:26:20 +00:00
|
|
|
char *
|
|
|
|
grub_efidisk_get_device_name (grub_efi_handle_t *handle)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *dp, *ldp;
|
|
|
|
char device_name[NEEDED_BUFLEN];
|
2006-04-25 20:08:31 +00:00
|
|
|
|
2006-04-30 21:09:37 +00:00
|
|
|
dp = grub_efi_get_device_path (handle);
|
2006-04-25 20:08:31 +00:00
|
|
|
if (! dp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ldp = find_last_device_path (dp);
|
|
|
|
if (! ldp)
|
|
|
|
return 0;
|
|
|
|
|
2013-10-19 00:23:08 +00:00
|
|
|
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
|
|
|
|
&& (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE
|
|
|
|
|| GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE))
|
2006-04-25 20:08:31 +00:00
|
|
|
{
|
2013-10-14 15:06:39 +00:00
|
|
|
int is_cdrom = 0;
|
2013-10-19 00:23:08 +00:00
|
|
|
struct grub_efidisk_get_device_name_ctx ctx;
|
|
|
|
char *dev_name;
|
|
|
|
grub_efi_device_path_t *dup_dp;
|
|
|
|
grub_disk_t parent = 0;
|
|
|
|
|
|
|
|
/* It is necessary to duplicate the device path so that GRUB
|
|
|
|
can overwrite it. */
|
|
|
|
dup_dp = duplicate_device_path (dp);
|
|
|
|
if (! dup_dp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
grub_efi_device_path_t *dup_ldp;
|
|
|
|
dup_ldp = find_last_device_path (dup_dp);
|
|
|
|
if (!(GRUB_EFI_DEVICE_PATH_TYPE (dup_ldp) == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
|
|
|
|
&& (GRUB_EFI_DEVICE_PATH_SUBTYPE (dup_ldp) == GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE
|
|
|
|
|| GRUB_EFI_DEVICE_PATH_SUBTYPE (dup_ldp) == GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (GRUB_EFI_DEVICE_PATH_SUBTYPE (dup_ldp) == GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE)
|
|
|
|
is_cdrom = 1;
|
|
|
|
|
|
|
|
dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
|
|
|
|
dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
|
|
|
|
dup_ldp->length[0] = sizeof (*dup_ldp);
|
|
|
|
dup_ldp->length[1] = 0;
|
|
|
|
}
|
2011-07-02 12:09:36 +00:00
|
|
|
|
2013-10-19 00:23:08 +00:00
|
|
|
if (!get_diskname_from_path (dup_dp, device_name))
|
|
|
|
{
|
|
|
|
grub_free (dup_dp);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-14 15:06:39 +00:00
|
|
|
|
2013-10-19 00:23:08 +00:00
|
|
|
parent = grub_disk_open (device_name);
|
|
|
|
grub_free (dup_dp);
|
2013-10-14 15:06:39 +00:00
|
|
|
|
2013-10-19 00:23:08 +00:00
|
|
|
if (! parent)
|
|
|
|
return 0;
|
2011-07-02 12:09:36 +00:00
|
|
|
|
2013-10-19 00:23:08 +00:00
|
|
|
/* Find a partition which matches the hard drive device path. */
|
|
|
|
ctx.partition_name = NULL;
|
|
|
|
grub_memcpy (&ctx.hd, ldp, sizeof (ctx.hd));
|
|
|
|
if (ctx.hd.partition_start == 0
|
|
|
|
&& (ctx.hd.partition_size << (parent->log_sector_size
|
|
|
|
- GRUB_DISK_SECTOR_BITS))
|
|
|
|
== grub_disk_get_size (parent))
|
|
|
|
{
|
|
|
|
dev_name = grub_strdup (parent->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
grub_partition_iterate (parent, grub_efidisk_get_device_name_iter,
|
|
|
|
&ctx);
|
2009-12-29 09:04:06 +00:00
|
|
|
|
2013-10-19 00:23:08 +00:00
|
|
|
if (! ctx.partition_name)
|
|
|
|
{
|
|
|
|
grub_disk_close (parent);
|
|
|
|
if (is_cdrom)
|
|
|
|
return grub_strdup (device_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-14 15:06:39 +00:00
|
|
|
|
2013-10-19 00:23:08 +00:00
|
|
|
dev_name = grub_xasprintf ("%s,%s", parent->name,
|
|
|
|
ctx.partition_name);
|
|
|
|
grub_free (ctx.partition_name);
|
|
|
|
}
|
|
|
|
grub_disk_close (parent);
|
|
|
|
|
|
|
|
return dev_name;
|
2006-04-25 20:08:31 +00:00
|
|
|
}
|
2013-10-14 15:06:39 +00:00
|
|
|
/* This may be guessed device - floppy, cdrom or entire disk. */
|
|
|
|
if (!get_diskname_from_path (dp, device_name))
|
|
|
|
return 0;
|
|
|
|
return grub_strdup (device_name);
|
2006-04-25 20:08:31 +00:00
|
|
|
}
|