MErge mainline into intwrap

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-08-25 23:39:42 +02:00
commit afba9f98ec
719 changed files with 55744 additions and 25714 deletions

776
grub-core/kern/efi/efi.c Normal file
View file

@ -0,0 +1,776 @@
/* efi.c - generic EFI support */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2006,2007,2008,2009,2010 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/misc.h>
#include <grub/charset.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#include <grub/efi/console_control.h>
#include <grub/efi/pe32.h>
#include <grub/machine/time.h>
#include <grub/term.h>
#include <grub/kernel.h>
#include <grub/mm.h>
/* The handle of GRUB itself. Filled in by the startup code. */
grub_efi_handle_t grub_efi_image_handle;
/* The pointer to a system table. Filled in by the startup code. */
grub_efi_system_table_t *grub_efi_system_table;
static grub_efi_guid_t console_control_guid = GRUB_EFI_CONSOLE_CONTROL_GUID;
static grub_efi_guid_t loaded_image_guid = GRUB_EFI_LOADED_IMAGE_GUID;
static grub_efi_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
void *
grub_efi_locate_protocol (grub_efi_guid_t *protocol, void *registration)
{
void *interface;
grub_efi_status_t status;
status = efi_call_3 (grub_efi_system_table->boot_services->locate_protocol,
protocol, registration, &interface);
if (status != GRUB_EFI_SUCCESS)
return 0;
return interface;
}
/* Return the array of handles which meet the requirement. If successful,
the number of handles is stored in NUM_HANDLES. The array is allocated
from the heap. */
grub_efi_handle_t *
grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
grub_efi_guid_t *protocol,
void *search_key,
grub_efi_uintn_t *num_handles)
{
grub_efi_boot_services_t *b;
grub_efi_status_t status;
grub_efi_handle_t *buffer;
grub_efi_uintn_t buffer_size = 8 * sizeof (grub_efi_handle_t);
buffer = grub_malloc (buffer_size);
if (! buffer)
return 0;
b = grub_efi_system_table->boot_services;
status = efi_call_5 (b->locate_handle, search_type, protocol, search_key,
&buffer_size, buffer);
if (status == GRUB_EFI_BUFFER_TOO_SMALL)
{
grub_free (buffer);
buffer = grub_malloc (buffer_size);
if (! buffer)
return 0;
status = efi_call_5 (b->locate_handle, search_type, protocol, search_key,
&buffer_size, buffer);
}
if (status != GRUB_EFI_SUCCESS)
{
grub_free (buffer);
return 0;
}
*num_handles = buffer_size / sizeof (grub_efi_handle_t);
return buffer;
}
void *
grub_efi_open_protocol (grub_efi_handle_t handle,
grub_efi_guid_t *protocol,
grub_efi_uint32_t attributes)
{
grub_efi_boot_services_t *b;
grub_efi_status_t status;
void *interface;
b = grub_efi_system_table->boot_services;
status = efi_call_6 (b->open_protocol, handle,
protocol,
&interface,
grub_efi_image_handle,
0,
attributes);
if (status != GRUB_EFI_SUCCESS)
return 0;
return interface;
}
int
grub_efi_set_text_mode (int on)
{
grub_efi_console_control_protocol_t *c;
grub_efi_screen_mode_t mode, new_mode;
c = grub_efi_locate_protocol (&console_control_guid, 0);
if (! c)
/* No console control protocol instance available, assume it is
already in text mode. */
return 1;
if (efi_call_4 (c->get_mode, c, &mode, 0, 0) != GRUB_EFI_SUCCESS)
return 0;
new_mode = on ? GRUB_EFI_SCREEN_TEXT : GRUB_EFI_SCREEN_GRAPHICS;
if (mode != new_mode)
if (efi_call_2 (c->set_mode, c, new_mode) != GRUB_EFI_SUCCESS)
return 0;
return 1;
}
void
grub_efi_stall (grub_efi_uintn_t microseconds)
{
efi_call_1 (grub_efi_system_table->boot_services->stall, microseconds);
}
grub_efi_loaded_image_t *
grub_efi_get_loaded_image (grub_efi_handle_t image_handle)
{
return grub_efi_open_protocol (image_handle,
&loaded_image_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
}
void
grub_exit (void)
{
grub_efi_fini ();
efi_call_4 (grub_efi_system_table->boot_services->exit,
grub_efi_image_handle, GRUB_EFI_SUCCESS, 0, 0);
for (;;) ;
}
/* On i386, a firmware-independant grub_reboot() is provided by realmode.S. */
#ifndef __i386__
void
grub_reboot (void)
{
grub_efi_fini ();
efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL);
for (;;) ;
}
#endif
int
grub_efi_exit_boot_services (grub_efi_uintn_t map_key)
{
grub_efi_boot_services_t *b;
grub_efi_status_t status;
b = grub_efi_system_table->boot_services;
status = efi_call_2 (b->exit_boot_services, grub_efi_image_handle, map_key);
return status == GRUB_EFI_SUCCESS;
}
grub_err_t
grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
grub_efi_uintn_t descriptor_size,
grub_efi_uint32_t descriptor_version,
grub_efi_memory_descriptor_t *virtual_map)
{
grub_efi_runtime_services_t *r;
grub_efi_status_t status;
r = grub_efi_system_table->runtime_services;
status = efi_call_4 (r->set_virtual_address_map, memory_map_size,
descriptor_size, descriptor_version, virtual_map);
if (status == GRUB_EFI_SUCCESS)
return GRUB_ERR_NONE;
return grub_error (GRUB_ERR_IO, "set_virtual_address_map failed");
}
grub_uint32_t
grub_get_rtc (void)
{
grub_efi_time_t time;
grub_efi_runtime_services_t *r;
r = grub_efi_system_table->runtime_services;
if (efi_call_2 (r->get_time, &time, 0) != GRUB_EFI_SUCCESS)
/* What is possible in this case? */
return 0;
return (((time.minute * 60 + time.second) * 1000
+ time.nanosecond / 1000000)
* GRUB_TICKS_PER_SECOND / 1000);
}
/* Search the mods section from the PE32/PE32+ image. This code uses
a PE32 header, but should work with PE32+ as well. */
grub_addr_t
grub_arch_modules_addr (void)
{
grub_efi_loaded_image_t *image;
struct grub_pe32_header *header;
struct grub_pe32_coff_header *coff_header;
struct grub_pe32_section_table *sections;
struct grub_pe32_section_table *section;
struct grub_module_info *info;
grub_uint16_t i;
image = grub_efi_get_loaded_image (grub_efi_image_handle);
if (! image)
return 0;
header = image->image_base;
coff_header = &(header->coff_header);
sections
= (struct grub_pe32_section_table *) ((char *) coff_header
+ sizeof (*coff_header)
+ coff_header->optional_header_size);
for (i = 0, section = sections;
i < coff_header->num_sections;
i++, section++)
{
if (grub_strcmp (section->name, "mods") == 0)
break;
}
if (i == coff_header->num_sections)
return 0;
info = (struct grub_module_info *) ((char *) image->image_base
+ section->virtual_address);
if (info->magic != GRUB_MODULE_MAGIC)
return 0;
return (grub_addr_t) info;
}
char *
grub_efi_get_filename (grub_efi_device_path_t *dp)
{
char *name = 0;
while (1)
{
grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
break;
else if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
&& subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
{
grub_efi_file_path_device_path_t *fp;
grub_efi_uint16_t len;
char *p;
grub_size_t size;
if (name)
{
size = grub_strlen (name);
name[size] = '/';
size++;
}
else
size = 0;
len = ((GRUB_EFI_DEVICE_PATH_LENGTH (dp) - 4)
/ sizeof (grub_efi_char16_t));
p = grub_realloc (name, size + len * 4 + 1);
if (! p)
{
grub_free (name);
return 0;
}
name = p;
fp = (grub_efi_file_path_device_path_t *) dp;
*grub_utf16_to_utf8 ((grub_uint8_t *) name + size,
fp->path_name, len) = '\0';
}
dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
}
if (name)
{
/* EFI breaks paths with backslashes. */
char *p;
for (p = name; *p; p++)
if (*p == '\\')
*p = '/';
}
return name;
}
grub_efi_device_path_t *
grub_efi_get_device_path (grub_efi_handle_t handle)
{
return grub_efi_open_protocol (handle, &device_path_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
}
/* Print the chain of Device Path nodes. This is mainly for debugging. */
void
grub_efi_print_device_path (grub_efi_device_path_t *dp)
{
while (1)
{
grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
switch (type)
{
case GRUB_EFI_END_DEVICE_PATH_TYPE:
switch (subtype)
{
case GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE:
grub_printf ("/EndEntire\n");
//grub_putchar ('\n');
break;
case GRUB_EFI_END_THIS_DEVICE_PATH_SUBTYPE:
grub_printf ("/EndThis\n");
//grub_putchar ('\n');
break;
default:
grub_printf ("/EndUnknown(%x)\n", (unsigned) subtype);
break;
}
break;
case GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE:
switch (subtype)
{
case GRUB_EFI_PCI_DEVICE_PATH_SUBTYPE:
{
grub_efi_pci_device_path_t pci;
grub_memcpy (&pci, dp, len);
grub_printf ("/PCI(%x,%x)",
(unsigned) pci.function, (unsigned) pci.device);
}
break;
case GRUB_EFI_PCCARD_DEVICE_PATH_SUBTYPE:
{
grub_efi_pccard_device_path_t pccard;
grub_memcpy (&pccard, dp, len);
grub_printf ("/PCCARD(%x)",
(unsigned) pccard.function);
}
break;
case GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE:
{
grub_efi_memory_mapped_device_path_t mmapped;
grub_memcpy (&mmapped, dp, len);
grub_printf ("/MMap(%x,%llx,%llx)",
(unsigned) mmapped.memory_type,
(unsigned long long) mmapped.start_address,
(unsigned long long) mmapped.end_address);
}
break;
case GRUB_EFI_VENDOR_DEVICE_PATH_SUBTYPE:
{
grub_efi_vendor_device_path_t vendor;
grub_memcpy (&vendor, dp, sizeof (vendor));
grub_printf ("/Vendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
(unsigned) vendor.vendor_guid.data1,
(unsigned) vendor.vendor_guid.data2,
(unsigned) vendor.vendor_guid.data3,
(unsigned) vendor.vendor_guid.data4[0],
(unsigned) vendor.vendor_guid.data4[1],
(unsigned) vendor.vendor_guid.data4[2],
(unsigned) vendor.vendor_guid.data4[3],
(unsigned) vendor.vendor_guid.data4[4],
(unsigned) vendor.vendor_guid.data4[5],
(unsigned) vendor.vendor_guid.data4[6],
(unsigned) vendor.vendor_guid.data4[7]);
}
break;
case GRUB_EFI_CONTROLLER_DEVICE_PATH_SUBTYPE:
{
grub_efi_controller_device_path_t controller;
grub_memcpy (&controller, dp, len);
grub_printf ("/Ctrl(%x)",
(unsigned) controller.controller_number);
}
break;
default:
grub_printf ("/UnknownHW(%x)", (unsigned) subtype);
break;
}
break;
case GRUB_EFI_ACPI_DEVICE_PATH_TYPE:
switch (subtype)
{
case GRUB_EFI_ACPI_DEVICE_PATH_SUBTYPE:
{
grub_efi_acpi_device_path_t acpi;
grub_memcpy (&acpi, dp, len);
grub_printf ("/ACPI(%x,%x)",
(unsigned) acpi.hid,
(unsigned) acpi.uid);
}
break;
case GRUB_EFI_EXPANDED_ACPI_DEVICE_PATH_SUBTYPE:
{
grub_efi_expanded_acpi_device_path_t eacpi;
grub_memcpy (&eacpi, dp, sizeof (eacpi));
grub_printf ("/ACPI(");
if (GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp)[0] == '\0')
grub_printf ("%x,", (unsigned) eacpi.hid);
else
grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp));
if (GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp)[0] == '\0')
grub_printf ("%x,", (unsigned) eacpi.uid);
else
grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp));
if (GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp)[0] == '\0')
grub_printf ("%x)", (unsigned) eacpi.cid);
else
grub_printf ("%s)", GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp));
}
break;
default:
grub_printf ("/UnknownACPI(%x)", (unsigned) subtype);
break;
}
break;
case GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE:
switch (subtype)
{
case GRUB_EFI_ATAPI_DEVICE_PATH_SUBTYPE:
{
grub_efi_atapi_device_path_t atapi;
grub_memcpy (&atapi, dp, len);
grub_printf ("/ATAPI(%x,%x,%x)",
(unsigned) atapi.primary_secondary,
(unsigned) atapi.slave_master,
(unsigned) atapi.lun);
}
break;
case GRUB_EFI_SCSI_DEVICE_PATH_SUBTYPE:
{
grub_efi_scsi_device_path_t scsi;
grub_memcpy (&scsi, dp, len);
grub_printf ("/SCSI(%x,%x)",
(unsigned) scsi.pun,
(unsigned) scsi.lun);
}
break;
case GRUB_EFI_FIBRE_CHANNEL_DEVICE_PATH_SUBTYPE:
{
grub_efi_fibre_channel_device_path_t fc;
grub_memcpy (&fc, dp, len);
grub_printf ("/FibreChannel(%llx,%llx)",
(unsigned long long) fc.wwn,
(unsigned long long) fc.lun);
}
break;
case GRUB_EFI_1394_DEVICE_PATH_SUBTYPE:
{
grub_efi_1394_device_path_t firewire;
grub_memcpy (&firewire, dp, len);
grub_printf ("/1394(%llx)", (unsigned long long) firewire.guid);
}
break;
case GRUB_EFI_USB_DEVICE_PATH_SUBTYPE:
{
grub_efi_usb_device_path_t usb;
grub_memcpy (&usb, dp, len);
grub_printf ("/USB(%x,%x)",
(unsigned) usb.parent_port_number,
(unsigned) usb.interface);
}
break;
case GRUB_EFI_USB_CLASS_DEVICE_PATH_SUBTYPE:
{
grub_efi_usb_class_device_path_t usb_class;
grub_memcpy (&usb_class, dp, len);
grub_printf ("/USBClass(%x,%x,%x,%x,%x)",
(unsigned) usb_class.vendor_id,
(unsigned) usb_class.product_id,
(unsigned) usb_class.device_class,
(unsigned) usb_class.device_subclass,
(unsigned) usb_class.device_protocol);
}
break;
case GRUB_EFI_I2O_DEVICE_PATH_SUBTYPE:
{
grub_efi_i2o_device_path_t i2o;
grub_memcpy (&i2o, dp, len);
grub_printf ("/I2O(%x)", (unsigned) i2o.tid);
}
break;
case GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE:
{
grub_efi_mac_address_device_path_t mac;
grub_memcpy (&mac, dp, len);
grub_printf ("/MacAddr(%02x:%02x:%02x:%02x:%02x:%02x,%x)",
(unsigned) mac.mac_address[0],
(unsigned) mac.mac_address[1],
(unsigned) mac.mac_address[2],
(unsigned) mac.mac_address[3],
(unsigned) mac.mac_address[4],
(unsigned) mac.mac_address[5],
(unsigned) mac.if_type);
}
break;
case GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE:
{
grub_efi_ipv4_device_path_t ipv4;
grub_memcpy (&ipv4, dp, len);
grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
(unsigned) ipv4.local_ip_address[0],
(unsigned) ipv4.local_ip_address[1],
(unsigned) ipv4.local_ip_address[2],
(unsigned) ipv4.local_ip_address[3],
(unsigned) ipv4.remote_ip_address[0],
(unsigned) ipv4.remote_ip_address[1],
(unsigned) ipv4.remote_ip_address[2],
(unsigned) ipv4.remote_ip_address[3],
(unsigned) ipv4.local_port,
(unsigned) ipv4.remote_port,
(unsigned) ipv4.protocol,
(unsigned) ipv4.static_ip_address);
}
break;
case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
{
grub_efi_ipv6_device_path_t ipv6;
grub_memcpy (&ipv6, dp, len);
grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
(unsigned) ipv6.local_ip_address[0],
(unsigned) ipv6.local_ip_address[1],
(unsigned) ipv6.local_ip_address[2],
(unsigned) ipv6.local_ip_address[3],
(unsigned) ipv6.local_ip_address[4],
(unsigned) ipv6.local_ip_address[5],
(unsigned) ipv6.local_ip_address[6],
(unsigned) ipv6.local_ip_address[7],
(unsigned) ipv6.remote_ip_address[0],
(unsigned) ipv6.remote_ip_address[1],
(unsigned) ipv6.remote_ip_address[2],
(unsigned) ipv6.remote_ip_address[3],
(unsigned) ipv6.remote_ip_address[4],
(unsigned) ipv6.remote_ip_address[5],
(unsigned) ipv6.remote_ip_address[6],
(unsigned) ipv6.remote_ip_address[7],
(unsigned) ipv6.local_port,
(unsigned) ipv6.remote_port,
(unsigned) ipv6.protocol,
(unsigned) ipv6.static_ip_address);
}
break;
case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
{
grub_efi_infiniband_device_path_t ib;
grub_memcpy (&ib, dp, len);
grub_printf ("/InfiniBand(%x,%llx,%llx,%llx)",
(unsigned) ib.port_gid[0], /* XXX */
(unsigned long long) ib.remote_id,
(unsigned long long) ib.target_port_id,
(unsigned long long) ib.device_id);
}
break;
case GRUB_EFI_UART_DEVICE_PATH_SUBTYPE:
{
grub_efi_uart_device_path_t uart;
grub_memcpy (&uart, dp, len);
grub_printf ("/UART(%llu,%u,%x,%x)",
(unsigned long long) uart.baud_rate,
uart.data_bits,
uart.parity,
uart.stop_bits);
}
break;
case GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE:
{
grub_efi_vendor_messaging_device_path_t vendor;
grub_memcpy (&vendor, dp, sizeof (vendor));
grub_printf ("/Vendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
(unsigned) vendor.vendor_guid.data1,
(unsigned) vendor.vendor_guid.data2,
(unsigned) vendor.vendor_guid.data3,
(unsigned) vendor.vendor_guid.data4[0],
(unsigned) vendor.vendor_guid.data4[1],
(unsigned) vendor.vendor_guid.data4[2],
(unsigned) vendor.vendor_guid.data4[3],
(unsigned) vendor.vendor_guid.data4[4],
(unsigned) vendor.vendor_guid.data4[5],
(unsigned) vendor.vendor_guid.data4[6],
(unsigned) vendor.vendor_guid.data4[7]);
}
break;
default:
grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
break;
}
break;
case GRUB_EFI_MEDIA_DEVICE_PATH_TYPE:
switch (subtype)
{
case GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE:
{
grub_efi_hard_drive_device_path_t hd;
grub_memcpy (&hd, dp, len);
grub_printf ("/HD(%u,%llx,%llx,%02x%02x%02x%02x%02x%02x%02x%02x,%x,%x)",
hd.partition_number,
(unsigned long long) hd.partition_start,
(unsigned long long) hd.partition_size,
(unsigned) hd.partition_signature[0],
(unsigned) hd.partition_signature[1],
(unsigned) hd.partition_signature[2],
(unsigned) hd.partition_signature[3],
(unsigned) hd.partition_signature[4],
(unsigned) hd.partition_signature[5],
(unsigned) hd.partition_signature[6],
(unsigned) hd.partition_signature[7],
(unsigned) hd.mbr_type,
(unsigned) hd.signature_type);
}
break;
case GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE:
{
grub_efi_cdrom_device_path_t cd;
grub_memcpy (&cd, dp, len);
grub_printf ("/CD(%u,%llx,%llx)",
cd.boot_entry,
(unsigned long long) cd.partition_start,
(unsigned long long) cd.partition_size);
}
break;
case GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE:
{
grub_efi_vendor_media_device_path_t vendor;
grub_memcpy (&vendor, dp, sizeof (vendor));
grub_printf ("/Vendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
(unsigned) vendor.vendor_guid.data1,
(unsigned) vendor.vendor_guid.data2,
(unsigned) vendor.vendor_guid.data3,
(unsigned) vendor.vendor_guid.data4[0],
(unsigned) vendor.vendor_guid.data4[1],
(unsigned) vendor.vendor_guid.data4[2],
(unsigned) vendor.vendor_guid.data4[3],
(unsigned) vendor.vendor_guid.data4[4],
(unsigned) vendor.vendor_guid.data4[5],
(unsigned) vendor.vendor_guid.data4[6],
(unsigned) vendor.vendor_guid.data4[7]);
}
break;
case GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE:
{
grub_efi_file_path_device_path_t *fp;
grub_uint8_t buf[(len - 4) * 2 + 1];
fp = (grub_efi_file_path_device_path_t *) dp;
*grub_utf16_to_utf8 (buf, fp->path_name,
(len - 4) / sizeof (grub_efi_char16_t))
= '\0';
grub_printf ("/File(%s)", buf);
}
break;
case GRUB_EFI_PROTOCOL_DEVICE_PATH_SUBTYPE:
{
grub_efi_protocol_device_path_t proto;
grub_memcpy (&proto, dp, sizeof (proto));
grub_printf ("/Protocol(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
(unsigned) proto.guid.data1,
(unsigned) proto.guid.data2,
(unsigned) proto.guid.data3,
(unsigned) proto.guid.data4[0],
(unsigned) proto.guid.data4[1],
(unsigned) proto.guid.data4[2],
(unsigned) proto.guid.data4[3],
(unsigned) proto.guid.data4[4],
(unsigned) proto.guid.data4[5],
(unsigned) proto.guid.data4[6],
(unsigned) proto.guid.data4[7]);
}
break;
default:
grub_printf ("/UnknownMedia(%x)", (unsigned) subtype);
break;
}
break;
case GRUB_EFI_BIOS_DEVICE_PATH_TYPE:
switch (subtype)
{
case GRUB_EFI_BIOS_DEVICE_PATH_SUBTYPE:
{
grub_efi_bios_device_path_t bios;
grub_memcpy (&bios, dp, sizeof (bios));
grub_printf ("/BIOS(%x,%x,%s)",
(unsigned) bios.device_type,
(unsigned) bios.status_flags,
(char *) (dp + 1));
}
break;
default:
grub_printf ("/UnknownBIOS(%x)", (unsigned) subtype);
break;
}
break;
default:
grub_printf ("/UnknownType(%x,%x)\n",
(unsigned) type,
(unsigned) subtype);
return;
break;
}
if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
break;
dp = (grub_efi_device_path_t *) ((char *) dp + len);
}
}
int
grub_efi_finish_boot_services (void)
{
grub_efi_uintn_t mmap_size = 0;
grub_efi_uintn_t map_key;
grub_efi_uintn_t desc_size;
grub_efi_uint32_t desc_version;
void *mmap_buf = 0;
if (grub_efi_get_memory_map (&mmap_size, mmap_buf, &map_key,
&desc_size, &desc_version) < 0)
return 0;
mmap_buf = grub_malloc (mmap_size);
if (grub_efi_get_memory_map (&mmap_size, mmap_buf, &map_key,
&desc_size, &desc_version) <= 0)
return 0;
return grub_efi_exit_boot_services (map_key);
}

107
grub-core/kern/efi/init.c Normal file
View file

@ -0,0 +1,107 @@
/* init.c - generic EFI initialization and finalization */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2006,2007 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/efi/efi.h>
#include <grub/efi/console.h>
#include <grub/efi/disk.h>
#include <grub/term.h>
#include <grub/misc.h>
#include <grub/env.h>
#include <grub/mm.h>
#include <grub/kernel.h>
void
grub_efi_init (void)
{
/* First of all, initialize the console so that GRUB can display
messages. */
grub_console_init ();
/* Initialize the memory management system. */
grub_efi_mm_init ();
efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer,
0, 0, 0, NULL);
grub_efidisk_init ();
}
void
grub_efi_set_prefix (void)
{
grub_efi_loaded_image_t *image = NULL;
char *device = NULL;
char *path = NULL;
{
char *pptr = NULL;
if (grub_prefix[0] == '(')
{
pptr = grub_strrchr (grub_prefix, ')');
if (pptr)
{
device = grub_strndup (grub_prefix + 1, pptr - grub_prefix - 1);
pptr++;
}
}
if (!pptr)
pptr = grub_prefix;
if (pptr[0])
path = grub_strdup (pptr);
}
if (!device || !path)
image = grub_efi_get_loaded_image (grub_efi_image_handle);
if (image && !device)
device = grub_efidisk_get_device_name (image->device_handle);
if (image && !path)
{
char *p;
path = grub_efi_get_filename (image->file_path);
/* Get the directory. */
p = grub_strrchr (path, '/');
if (p)
*p = '\0';
}
if (device && path)
{
char *prefix;
prefix = grub_xasprintf ("(%s)%s", device, path);
if (prefix)
{
grub_env_set ("prefix", prefix);
grub_free (prefix);
}
}
grub_free (device);
grub_free (path);
}
void
grub_efi_fini (void)
{
grub_efidisk_fini ();
grub_console_fini ();
}

448
grub-core/kern/efi/mm.c Normal file
View file

@ -0,0 +1,448 @@
/* mm.c - generic EFI memory management */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2006,2007,2008,2009 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/misc.h>
#include <grub/mm.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
#define BYTES_TO_PAGES(bytes) ((bytes) >> 12)
#define PAGES_TO_BYTES(pages) ((pages) << 12)
/* The size of a memory map obtained from the firmware. This must be
a multiplier of 4KB. */
#define MEMORY_MAP_SIZE 0x3000
/* Maintain the list of allocated pages. */
struct allocated_page
{
grub_efi_physical_address_t addr;
grub_efi_uint64_t num_pages;
};
#define ALLOCATED_PAGES_SIZE 0x1000
#define MAX_ALLOCATED_PAGES \
(ALLOCATED_PAGES_SIZE / sizeof (struct allocated_page))
static struct allocated_page *allocated_pages = 0;
/* The minimum and maximum heap size for GRUB itself. */
#define MIN_HEAP_SIZE 0x100000
#define MAX_HEAP_SIZE (1600 * 0x100000)
/* Allocate pages. Return the pointer to the first of allocated pages. */
void *
grub_efi_allocate_pages (grub_efi_physical_address_t address,
grub_efi_uintn_t pages)
{
grub_efi_allocate_type_t type;
grub_efi_status_t status;
grub_efi_boot_services_t *b;
#if GRUB_TARGET_SIZEOF_VOID_P < 8
/* Limit the memory access to less than 4GB for 32-bit platforms. */
if (address > 0xffffffff)
return 0;
#endif
#if GRUB_TARGET_SIZEOF_VOID_P < 8 || defined (MCMODEL_SMALL)
if (address == 0)
{
type = GRUB_EFI_ALLOCATE_MAX_ADDRESS;
address = 0xffffffff;
}
else
type = GRUB_EFI_ALLOCATE_ADDRESS;
#else
if (address == 0)
type = GRUB_EFI_ALLOCATE_ANY_PAGES;
else
type = GRUB_EFI_ALLOCATE_ADDRESS;
#endif
b = grub_efi_system_table->boot_services;
status = efi_call_4 (b->allocate_pages, type, GRUB_EFI_LOADER_DATA, pages, &address);
if (status != GRUB_EFI_SUCCESS)
return 0;
if (address == 0)
{
/* Uggh, the address 0 was allocated... This is too annoying,
so reallocate another one. */
address = 0xffffffff;
status = efi_call_4 (b->allocate_pages, type, GRUB_EFI_LOADER_DATA, pages, &address);
grub_efi_free_pages (0, pages);
if (status != GRUB_EFI_SUCCESS)
return 0;
}
if (allocated_pages)
{
unsigned i;
for (i = 0; i < MAX_ALLOCATED_PAGES; i++)
if (allocated_pages[i].addr == 0)
{
allocated_pages[i].addr = address;
allocated_pages[i].num_pages = pages;
break;
}
if (i == MAX_ALLOCATED_PAGES)
grub_fatal ("too many page allocations");
}
return (void *) ((grub_addr_t) address);
}
/* Free pages starting from ADDRESS. */
void
grub_efi_free_pages (grub_efi_physical_address_t address,
grub_efi_uintn_t pages)
{
grub_efi_boot_services_t *b;
if (allocated_pages
&& ((grub_efi_physical_address_t) ((grub_addr_t) allocated_pages)
!= address))
{
unsigned i;
for (i = 0; i < MAX_ALLOCATED_PAGES; i++)
if (allocated_pages[i].addr == address)
{
allocated_pages[i].addr = 0;
break;
}
}
b = grub_efi_system_table->boot_services;
efi_call_2 (b->free_pages, address, pages);
}
/* Get the memory map as defined in the EFI spec. Return 1 if successful,
return 0 if partial, or return -1 if an error occurs. */
int
grub_efi_get_memory_map (grub_efi_uintn_t *memory_map_size,
grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t *map_key,
grub_efi_uintn_t *descriptor_size,
grub_efi_uint32_t *descriptor_version)
{
grub_efi_status_t status;
grub_efi_boot_services_t *b;
grub_efi_uintn_t key;
grub_efi_uint32_t version;
/* Allow some parameters to be missing. */
if (! map_key)
map_key = &key;
if (! descriptor_version)
descriptor_version = &version;
b = grub_efi_system_table->boot_services;
status = efi_call_5 (b->get_memory_map, memory_map_size, memory_map, map_key,
descriptor_size, descriptor_version);
if (status == GRUB_EFI_SUCCESS)
return 1;
else if (status == GRUB_EFI_BUFFER_TOO_SMALL)
return 0;
else
return -1;
}
/* Sort the memory map in place. */
static void
sort_memory_map (grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t desc_size,
grub_efi_memory_descriptor_t *memory_map_end)
{
grub_efi_memory_descriptor_t *d1;
grub_efi_memory_descriptor_t *d2;
for (d1 = memory_map;
d1 < memory_map_end;
d1 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size))
{
grub_efi_memory_descriptor_t *max_desc = d1;
for (d2 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size);
d2 < memory_map_end;
d2 = NEXT_MEMORY_DESCRIPTOR (d2, desc_size))
{
if (max_desc->num_pages < d2->num_pages)
max_desc = d2;
}
if (max_desc != d1)
{
grub_efi_memory_descriptor_t tmp;
tmp = *d1;
*d1 = *max_desc;
*max_desc = tmp;
}
}
}
/* Filter the descriptors. GRUB needs only available memory. */
static grub_efi_memory_descriptor_t *
filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
grub_efi_memory_descriptor_t *filtered_memory_map,
grub_efi_uintn_t desc_size,
grub_efi_memory_descriptor_t *memory_map_end)
{
grub_efi_memory_descriptor_t *desc;
grub_efi_memory_descriptor_t *filtered_desc;
for (desc = memory_map, filtered_desc = filtered_memory_map;
desc < memory_map_end;
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
{
if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
#if GRUB_TARGET_SIZEOF_VOID_P < 8 || defined (MCMODEL_SMALL)
&& desc->physical_start <= 0xffffffff
#endif
&& desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000
&& desc->num_pages != 0)
{
grub_memcpy (filtered_desc, desc, desc_size);
/* Avoid less than 1MB, because some loaders seem to be confused. */
if (desc->physical_start < 0x100000)
{
desc->num_pages -= BYTES_TO_PAGES (0x100000
- desc->physical_start);
desc->physical_start = 0x100000;
}
#if GRUB_TARGET_SIZEOF_VOID_P < 8 || defined (MCMODEL_SMALL)
if (BYTES_TO_PAGES (filtered_desc->physical_start)
+ filtered_desc->num_pages
> BYTES_TO_PAGES (0x100000000LL))
filtered_desc->num_pages
= (BYTES_TO_PAGES (0x100000000LL)
- BYTES_TO_PAGES (filtered_desc->physical_start));
#endif
if (filtered_desc->num_pages == 0)
continue;
filtered_desc = NEXT_MEMORY_DESCRIPTOR (filtered_desc, desc_size);
}
}
return filtered_desc;
}
/* Return the total number of pages. */
static grub_efi_uint64_t
get_total_pages (grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t desc_size,
grub_efi_memory_descriptor_t *memory_map_end)
{
grub_efi_memory_descriptor_t *desc;
grub_efi_uint64_t total = 0;
for (desc = memory_map;
desc < memory_map_end;
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
total += desc->num_pages;
return total;
}
/* Add memory regions. */
static void
add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t desc_size,
grub_efi_memory_descriptor_t *memory_map_end,
grub_efi_uint64_t required_pages)
{
grub_efi_memory_descriptor_t *desc;
for (desc = memory_map;
desc < memory_map_end;
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
{
grub_efi_uint64_t pages;
grub_efi_physical_address_t start;
void *addr;
start = desc->physical_start;
pages = desc->num_pages;
if (pages > required_pages)
{
start += PAGES_TO_BYTES (pages - required_pages);
pages = required_pages;
}
addr = grub_efi_allocate_pages (start, pages);
if (! addr)
grub_fatal ("cannot allocate conventional memory %p with %u pages",
(void *) ((grub_addr_t) start),
(unsigned) pages);
grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
required_pages -= pages;
if (required_pages == 0)
break;
}
if (required_pages > 0)
grub_fatal ("too little memory");
}
#if 0
/* Print the memory map. */
static void
print_memory_map (grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t desc_size,
grub_efi_memory_descriptor_t *memory_map_end)
{
grub_efi_memory_descriptor_t *desc;
int i;
for (desc = memory_map, i = 0;
desc < memory_map_end;
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size), i++)
{
grub_printf ("MD: t=%x, p=%llx, v=%llx, n=%llx, a=%llx\n",
desc->type, desc->physical_start, desc->virtual_start,
desc->num_pages, desc->attribute);
}
}
#endif
void
grub_efi_mm_init (void)
{
grub_efi_memory_descriptor_t *memory_map;
grub_efi_memory_descriptor_t *memory_map_end;
grub_efi_memory_descriptor_t *filtered_memory_map;
grub_efi_memory_descriptor_t *filtered_memory_map_end;
grub_efi_uintn_t map_size;
grub_efi_uintn_t desc_size;
grub_efi_uint64_t total_pages;
grub_efi_uint64_t required_pages;
int mm_status;
/* First of all, allocate pages to maintain allocations. */
allocated_pages
= grub_efi_allocate_pages (0, BYTES_TO_PAGES (ALLOCATED_PAGES_SIZE));
if (! allocated_pages)
grub_fatal ("cannot allocate memory");
grub_memset (allocated_pages, 0, ALLOCATED_PAGES_SIZE);
/* Prepare a memory region to store two memory maps. */
memory_map = grub_efi_allocate_pages (0,
2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
if (! memory_map)
grub_fatal ("cannot allocate memory");
/* Obtain descriptors for available memory. */
map_size = MEMORY_MAP_SIZE;
mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0);
if (mm_status == 0)
{
grub_efi_free_pages
((grub_efi_physical_address_t) ((grub_addr_t) memory_map),
2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
memory_map = grub_efi_allocate_pages (0, 2 * BYTES_TO_PAGES (map_size));
if (! memory_map)
grub_fatal ("cannot allocate memory");
mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0,
&desc_size, 0);
}
if (mm_status < 0)
grub_fatal ("cannot get memory map");
memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
filtered_memory_map = memory_map_end;
filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
desc_size, memory_map_end);
/* By default, request a quarter of the available memory. */
total_pages = get_total_pages (filtered_memory_map, desc_size,
filtered_memory_map_end);
required_pages = (total_pages >> 2);
if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
required_pages = BYTES_TO_PAGES (MAX_HEAP_SIZE);
/* Sort the filtered descriptors, so that GRUB can allocate pages
from smaller regions. */
sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end);
/* Allocate memory regions for GRUB's memory management. */
add_memory_regions (filtered_memory_map, desc_size,
filtered_memory_map_end, required_pages);
#if 0
/* For debug. */
map_size = MEMORY_MAP_SIZE;
if (grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0) < 0)
grub_fatal ("cannot get memory map");
grub_printf ("printing memory map\n");
print_memory_map (memory_map, desc_size,
NEXT_MEMORY_DESCRIPTOR (memory_map, map_size));
grub_abort ();
#endif
/* Release the memory maps. */
grub_efi_free_pages ((grub_addr_t) memory_map,
2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
}
void
grub_efi_mm_fini (void)
{
if (allocated_pages)
{
unsigned i;
for (i = 0; i < MAX_ALLOCATED_PAGES; i++)
{
struct allocated_page *p;
p = allocated_pages + i;
if (p->addr != 0)
grub_efi_free_pages ((grub_addr_t) p->addr, p->num_pages);
}
grub_efi_free_pages ((grub_addr_t) allocated_pages,
BYTES_TO_PAGES (ALLOCATED_PAGES_SIZE));
}
}