summaryrefslogtreecommitdiffstats
path: root/grub-core/commands/efi
diff options
context:
space:
mode:
Diffstat (limited to 'grub-core/commands/efi')
-rw-r--r--grub-core/commands/efi/efifwsetup.c90
-rw-r--r--grub-core/commands/efi/fixvideo.c114
-rw-r--r--grub-core/commands/efi/loadbios.c222
-rw-r--r--grub-core/commands/efi/lsefi.c155
-rw-r--r--grub-core/commands/efi/lsefimmap.c160
-rw-r--r--grub-core/commands/efi/lsefisystab.c125
-rw-r--r--grub-core/commands/efi/lssal.c169
-rw-r--r--grub-core/commands/efi/smbios.c61
-rw-r--r--grub-core/commands/efi/tpm.c241
9 files changed, 1337 insertions, 0 deletions
diff --git a/grub-core/commands/efi/efifwsetup.c b/grub-core/commands/efi/efifwsetup.c
new file mode 100644
index 0000000..eaca032
--- /dev/null
+++ b/grub-core/commands/efi/efifwsetup.c
@@ -0,0 +1,90 @@
+/* fwsetup.c - Reboot into firmware setup menu. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ grub_efi_uint64_t *old_os_indications;
+ grub_efi_uint64_t os_indications = GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
+ grub_err_t status;
+ grub_size_t oi_size;
+ grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+
+ grub_efi_get_variable ("OsIndications", &global, &oi_size,
+ (void **) &old_os_indications);
+
+ if (old_os_indications != NULL && oi_size == sizeof (os_indications))
+ os_indications |= *old_os_indications;
+
+ status = grub_efi_set_variable ("OsIndications", &global, &os_indications,
+ sizeof (os_indications));
+ if (status != GRUB_ERR_NONE)
+ return status;
+
+ grub_reboot ();
+
+ return GRUB_ERR_BUG;
+}
+
+static grub_command_t cmd = NULL;
+
+static grub_efi_boolean_t
+efifwsetup_is_supported (void)
+{
+ grub_efi_uint64_t *os_indications_supported = NULL;
+ grub_size_t oi_size = 0;
+ grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+
+ grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
+ (void **) &os_indications_supported);
+
+ if (!os_indications_supported)
+ return 0;
+
+ if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
+ return 1;
+
+ return 0;
+}
+
+GRUB_MOD_INIT (efifwsetup)
+{
+ if (efifwsetup_is_supported ())
+ cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
+ N_("Reboot into firmware setup menu."));
+
+}
+
+GRUB_MOD_FINI (efifwsetup)
+{
+ if (cmd)
+ grub_unregister_command (cmd);
+}
diff --git a/grub-core/commands/efi/fixvideo.c b/grub-core/commands/efi/fixvideo.c
new file mode 100644
index 0000000..d9d54a2
--- /dev/null
+++ b/grub-core/commands/efi/fixvideo.c
@@ -0,0 +1,114 @@
+/* fixvideo.c - fix video problem in efi */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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/dl.h>
+#include <grub/misc.h>
+#include <grub/file.h>
+#include <grub/pci.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/mm.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static struct grub_video_patch
+{
+ const char *name;
+ grub_uint32_t pci_id;
+ grub_uint32_t mmio_bar;
+ grub_uint32_t mmio_reg;
+ grub_uint32_t mmio_old;
+} video_patches[] =
+ {
+ {"Intel 945GM", 0x27a28086, 0, 0x71184, 0x1000000}, /* DSPBBASE */
+ {"Intel 965GM", 0x2a028086, 0, 0x7119C, 0x1000000}, /* DSPBSURF */
+ {0, 0, 0, 0, 0}
+ };
+
+static int
+scan_card (grub_pci_device_t dev, grub_pci_id_t pciid,
+ void *data __attribute__ ((unused)))
+{
+ grub_pci_address_t addr;
+
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
+ if (grub_pci_read_byte (addr + 3) == 0x3)
+ {
+ struct grub_video_patch *p = video_patches;
+
+ while (p->name)
+ {
+ if (p->pci_id == pciid)
+ {
+ grub_addr_t base;
+
+ grub_dprintf ("fixvideo", "Found graphic card: %s\n", p->name);
+ addr += 8 + p->mmio_bar * 4;
+ base = grub_pci_read (addr);
+ if ((! base) || (base & GRUB_PCI_ADDR_SPACE_IO) ||
+ (base & GRUB_PCI_ADDR_MEM_PREFETCH))
+ grub_dprintf ("fixvideo", "Invalid MMIO bar %d\n", p->mmio_bar);
+ else
+ {
+ base &= GRUB_PCI_ADDR_MEM_MASK;
+ base += p->mmio_reg;
+
+ if (*((volatile grub_uint32_t *) base) != p->mmio_old)
+ grub_dprintf ("fixvideo", "Old value doesn't match\n");
+ else
+ {
+ *((volatile grub_uint32_t *) base) = 0;
+ if (*((volatile grub_uint32_t *) base))
+ grub_dprintf ("fixvideo", "Setting MMIO fails\n");
+ }
+ }
+
+ return 1;
+ }
+ p++;
+ }
+
+ grub_dprintf ("fixvideo", "Unknown graphic card: %x\n", pciid);
+ }
+
+ return 0;
+}
+
+static grub_err_t
+grub_cmd_fixvideo (grub_command_t cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char *argv[] __attribute__ ((unused)))
+{
+ grub_pci_iterate (scan_card, NULL);
+ return 0;
+}
+
+static grub_command_t cmd_fixvideo;
+
+GRUB_MOD_INIT(fixvideo)
+{
+ cmd_fixvideo = grub_register_command ("fix_video", grub_cmd_fixvideo,
+ 0, N_("Fix video problem."));
+
+}
+
+GRUB_MOD_FINI(fixvideo)
+{
+ grub_unregister_command (cmd_fixvideo);
+}
diff --git a/grub-core/commands/efi/loadbios.c b/grub-core/commands/efi/loadbios.c
new file mode 100644
index 0000000..5c7725f
--- /dev/null
+++ b/grub-core/commands/efi/loadbios.c
@@ -0,0 +1,222 @@
+/* loadbios.c - command to load a bios dump */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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/dl.h>
+#include <grub/misc.h>
+#include <grub/file.h>
+#include <grub/efi/efi.h>
+#include <grub/pci.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_efi_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
+static grub_efi_guid_t acpi2_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
+static grub_efi_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
+
+#define EBDA_SEG_ADDR 0x40e
+#define LOW_MEM_ADDR 0x413
+#define FAKE_EBDA_SEG 0x9fc0
+
+#define BLANK_MEM 0xffffffff
+#define VBIOS_ADDR 0xc0000
+#define SBIOS_ADDR 0xf0000
+
+static int
+enable_rom_area (void)
+{
+ grub_pci_address_t addr;
+ grub_uint32_t *rom_ptr;
+ grub_pci_device_t dev = { .bus = 0, .device = 0, .function = 0};
+
+ rom_ptr = (grub_uint32_t *) VBIOS_ADDR;
+ if (*rom_ptr != BLANK_MEM)
+ {
+ grub_puts_ (N_("ROM image is present."));
+ return 0;
+ }
+
+ /* FIXME: should be macroified. */
+ addr = grub_pci_make_address (dev, 144);
+ grub_pci_write_byte (addr++, 0x30);
+ grub_pci_write_byte (addr++, 0x33);
+ grub_pci_write_byte (addr++, 0x33);
+ grub_pci_write_byte (addr++, 0x33);
+ grub_pci_write_byte (addr++, 0x33);
+ grub_pci_write_byte (addr++, 0x33);
+ grub_pci_write_byte (addr++, 0x33);
+ grub_pci_write_byte (addr, 0);
+
+ *rom_ptr = 0;
+ if (*rom_ptr != 0)
+ {
+ grub_puts_ (N_("Can\'t enable ROM area."));
+ return 0;
+ }
+
+ return 1;
+}
+
+static void
+lock_rom_area (void)
+{
+ grub_pci_address_t addr;
+ grub_pci_device_t dev = { .bus = 0, .device = 0, .function = 0};
+
+ /* FIXME: should be macroified. */
+ addr = grub_pci_make_address (dev, 144);
+ grub_pci_write_byte (addr++, 0x10);
+ grub_pci_write_byte (addr++, 0x11);
+ grub_pci_write_byte (addr++, 0x11);
+ grub_pci_write_byte (addr++, 0x11);
+ grub_pci_write_byte (addr, 0x11);
+}
+
+static void
+fake_bios_data (int use_rom)
+{
+ unsigned i;
+ void *acpi, *smbios;
+ grub_uint16_t *ebda_seg_ptr, *low_mem_ptr;
+
+ ebda_seg_ptr = (grub_uint16_t *) EBDA_SEG_ADDR;
+ low_mem_ptr = (grub_uint16_t *) LOW_MEM_ADDR;
+ if ((*ebda_seg_ptr) || (*low_mem_ptr))
+ return;
+
+ acpi = 0;
+ smbios = 0;
+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
+ {
+ grub_efi_packed_guid_t *guid =
+ &grub_efi_system_table->configuration_table[i].vendor_guid;
+
+ if (! grub_memcmp (guid, &acpi2_guid, sizeof (grub_efi_guid_t)))
+ {
+ acpi = grub_efi_system_table->configuration_table[i].vendor_table;
+ grub_dprintf ("efi", "ACPI2: %p\n", acpi);
+ }
+ else if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_efi_guid_t)))
+ {
+ void *t;
+
+ t = grub_efi_system_table->configuration_table[i].vendor_table;
+ if (! acpi)
+ acpi = t;
+ grub_dprintf ("efi", "ACPI: %p\n", t);
+ }
+ else if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_efi_guid_t)))
+ {
+ smbios = grub_efi_system_table->configuration_table[i].vendor_table;
+ grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
+ }
+ }
+
+ *ebda_seg_ptr = FAKE_EBDA_SEG;
+ *low_mem_ptr = (FAKE_EBDA_SEG >> 6);
+
+ *((grub_uint16_t *) (FAKE_EBDA_SEG << 4)) = 640 - *low_mem_ptr;
+
+ if (acpi)
+ grub_memcpy ((char *) ((FAKE_EBDA_SEG << 4) + 16), acpi, 1024 - 16);
+
+ if ((use_rom) && (smbios))
+ grub_memcpy ((char *) SBIOS_ADDR, (char *) smbios + 16, 16);
+}
+
+static grub_err_t
+grub_cmd_fakebios (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char *argv[] __attribute__ ((unused)))
+{
+ if (enable_rom_area ())
+ {
+ fake_bios_data (1);
+ lock_rom_area ();
+ }
+ else
+ fake_bios_data (0);
+
+ return 0;
+}
+
+static grub_err_t
+grub_cmd_loadbios (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ grub_file_t file;
+ int size;
+
+ if (argc == 0)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+
+ if (argc > 1)
+ {
+ file = grub_file_open (argv[1], GRUB_FILE_TYPE_VBE_DUMP);
+ if (! file)
+ return grub_errno;
+
+ if (file->size != 4)
+ grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid int10 dump size");
+ else
+ grub_file_read (file, (void *) 0x40, 4);
+
+ grub_file_close (file);
+ if (grub_errno)
+ return grub_errno;
+ }
+
+ file = grub_file_open (argv[0], GRUB_FILE_TYPE_VBE_DUMP);
+ if (! file)
+ return grub_errno;
+
+ size = file->size;
+ if ((size < 0x10000) || (size > 0x40000))
+ grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid bios dump size");
+ else if (enable_rom_area ())
+ {
+ grub_file_read (file, (void *) VBIOS_ADDR, size);
+ fake_bios_data (size <= 0x40000);
+ lock_rom_area ();
+ }
+
+ grub_file_close (file);
+ return grub_errno;
+}
+
+static grub_command_t cmd_fakebios, cmd_loadbios;
+
+GRUB_MOD_INIT(loadbios)
+{
+ cmd_fakebios = grub_register_command_lockdown ("fakebios", grub_cmd_fakebios,
+ 0, N_("Create BIOS-like structures for"
+ " backward compatibility with"
+ " existing OS."));
+
+ cmd_loadbios = grub_register_command_lockdown ("loadbios", grub_cmd_loadbios,
+ N_("BIOS_DUMP [INT10_DUMP]"),
+ N_("Load BIOS dump."));
+}
+
+GRUB_MOD_FINI(loadbios)
+{
+ grub_unregister_command (cmd_fakebios);
+ grub_unregister_command (cmd_loadbios);
+}
diff --git a/grub-core/commands/efi/lsefi.c b/grub-core/commands/efi/lsefi.c
new file mode 100644
index 0000000..d1ce99a
--- /dev/null
+++ b/grub-core/commands/efi/lsefi.c
@@ -0,0 +1,155 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/edid.h>
+#include <grub/efi/pci.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/uga_draw.h>
+#include <grub/efi/graphics_output.h>
+#include <grub/efi/console_control.h>
+#include <grub/command.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct known_protocol
+{
+ grub_efi_guid_t guid;
+ const char *name;
+} known_protocols[] =
+ {
+ { GRUB_EFI_DISK_IO_GUID, "disk" },
+ { GRUB_EFI_BLOCK_IO_GUID, "block" },
+ { GRUB_EFI_SERIAL_IO_GUID, "serial" },
+ { GRUB_EFI_SIMPLE_NETWORK_GUID, "network" },
+ { GRUB_EFI_PXE_GUID, "pxe" },
+ { GRUB_EFI_DEVICE_PATH_GUID, "device path" },
+ { GRUB_EFI_PCI_IO_GUID, "PCI" },
+ { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root" },
+ { GRUB_EFI_EDID_ACTIVE_GUID, "active EDID" },
+ { GRUB_EFI_EDID_DISCOVERED_GUID, "discovered EDID" },
+ { GRUB_EFI_EDID_OVERRIDE_GUID, "override EDID" },
+ { GRUB_EFI_GOP_GUID, "GOP" },
+ { GRUB_EFI_UGA_DRAW_GUID, "UGA draw" },
+ { GRUB_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, "simple text output" },
+ { GRUB_EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, "simple text input" },
+ { GRUB_EFI_SIMPLE_POINTER_PROTOCOL_GUID, "simple pointer" },
+ { GRUB_EFI_CONSOLE_CONTROL_GUID, "console control" },
+ { GRUB_EFI_ABSOLUTE_POINTER_PROTOCOL_GUID, "absolute pointer" },
+ { GRUB_EFI_DRIVER_BINDING_PROTOCOL_GUID, "EFI driver binding" },
+ { GRUB_EFI_LOAD_FILE_PROTOCOL_GUID, "load file" },
+ { GRUB_EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, "simple FS" },
+ { GRUB_EFI_TAPE_IO_PROTOCOL_GUID, "tape I/O" },
+ { GRUB_EFI_UNICODE_COLLATION_PROTOCOL_GUID, "unicode collation" },
+ { GRUB_EFI_SCSI_IO_PROTOCOL_GUID, "SCSI I/O" },
+ { GRUB_EFI_USB2_HC_PROTOCOL_GUID, "USB host" },
+ { GRUB_EFI_DEBUG_SUPPORT_PROTOCOL_GUID, "debug support" },
+ { GRUB_EFI_DEBUGPORT_PROTOCOL_GUID, "debug port" },
+ { GRUB_EFI_DECOMPRESS_PROTOCOL_GUID, "decompress" },
+ { GRUB_EFI_LOADED_IMAGE_PROTOCOL_GUID, "loaded image" },
+ { GRUB_EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, "device path to text" },
+ { GRUB_EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID, "device path utilities" },
+ { GRUB_EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID, "device path from text" },
+ { GRUB_EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID, "HII config routing" },
+ { GRUB_EFI_HII_DATABASE_PROTOCOL_GUID, "HII database" },
+ { GRUB_EFI_HII_STRING_PROTOCOL_GUID, "HII string" },
+ { GRUB_EFI_HII_IMAGE_PROTOCOL_GUID, "HII image" },
+ { GRUB_EFI_HII_FONT_PROTOCOL_GUID, "HII font" },
+ { GRUB_EFI_COMPONENT_NAME2_PROTOCOL_GUID, "component name 2" },
+ { GRUB_EFI_HII_CONFIGURATION_ACCESS_PROTOCOL_GUID,
+ "HII configuration access" },
+ { GRUB_EFI_USB_IO_PROTOCOL_GUID, "USB I/O" },
+ };
+
+static grub_err_t
+grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ grub_efi_handle_t *handles;
+ grub_efi_uintn_t num_handles;
+ unsigned i, j, k;
+
+ handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES,
+ NULL, NULL, &num_handles);
+
+ for (i = 0; i < num_handles; i++)
+ {
+ grub_efi_handle_t handle = handles[i];
+ grub_efi_status_t status;
+ grub_efi_uintn_t num_protocols;
+ grub_efi_packed_guid_t **protocols;
+ grub_efi_device_path_t *dp;
+
+ grub_printf ("Handle %p\n", handle);
+
+ dp = grub_efi_get_device_path (handle);
+ if (dp)
+ {
+ grub_printf (" ");
+ grub_efi_print_device_path (dp);
+ }
+
+ status = efi_call_3 (grub_efi_system_table->boot_services->protocols_per_handle,
+ handle, &protocols, &num_protocols);
+ if (status != GRUB_EFI_SUCCESS) {
+ grub_printf ("Unable to retrieve protocols\n");
+ continue;
+ }
+ for (j = 0; j < num_protocols; j++)
+ {
+ for (k = 0; k < ARRAY_SIZE (known_protocols); k++)
+ if (grub_memcmp (protocols[j], &known_protocols[k].guid,
+ sizeof (known_protocols[k].guid)) == 0)
+ break;
+ if (k < ARRAY_SIZE (known_protocols))
+ grub_printf (" %s\n", known_protocols[k].name);
+ else
+ grub_printf (" %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
+ protocols[j]->data1,
+ protocols[j]->data2,
+ protocols[j]->data3,
+ (unsigned) protocols[j]->data4[0],
+ (unsigned) protocols[j]->data4[1],
+ (unsigned) protocols[j]->data4[2],
+ (unsigned) protocols[j]->data4[3],
+ (unsigned) protocols[j]->data4[4],
+ (unsigned) protocols[j]->data4[5],
+ (unsigned) protocols[j]->data4[6],
+ (unsigned) protocols[j]->data4[7]);
+ }
+
+ }
+
+ return 0;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(lsefi)
+{
+ cmd = grub_register_command ("lsefi", grub_cmd_lsefi,
+ NULL, "Display EFI handles.");
+}
+
+GRUB_MOD_FINI(lsefi)
+{
+ grub_unregister_command (cmd);
+}
diff --git a/grub-core/commands/efi/lsefimmap.c b/grub-core/commands/efi/lsefimmap.c
new file mode 100644
index 0000000..c85ff7f
--- /dev/null
+++ b/grub-core/commands/efi/lsefimmap.c
@@ -0,0 +1,160 @@
+/* lsefimemmap.c - Display memory map. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2008 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/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/command.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define ADD_MEMORY_DESCRIPTOR(desc, size) \
+ ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
+
+static grub_err_t
+grub_cmd_lsefimmap (grub_command_t cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ grub_efi_uintn_t map_size;
+ grub_efi_memory_descriptor_t *memory_map;
+ grub_efi_memory_descriptor_t *memory_map_end;
+ grub_efi_memory_descriptor_t *desc;
+ grub_efi_uintn_t desc_size;
+
+ map_size = 0;
+ if (grub_efi_get_memory_map (&map_size, NULL, NULL, &desc_size, 0) < 0)
+ return 0;
+
+ memory_map = grub_malloc (map_size);
+ if (memory_map == NULL)
+ return grub_errno;
+ if (grub_efi_get_memory_map (&map_size, memory_map, NULL, &desc_size, 0) <= 0)
+ goto fail;
+
+ grub_printf
+ ("Type Physical start - end #Pages "
+ " Size Attributes\n");
+ memory_map_end = ADD_MEMORY_DESCRIPTOR (memory_map, map_size);
+ for (desc = memory_map;
+ desc < memory_map_end;
+ desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size))
+ {
+ grub_efi_uint64_t size;
+ grub_efi_uint64_t attr;
+ static const char types_str[][9] =
+ {
+ "reserved",
+ "ldr-code",
+ "ldr-data",
+ "BS-code ",
+ "BS-data ",
+ "RT-code ",
+ "RT-data ",
+ "conv-mem",
+ "unusable",
+ "ACPI-rec",
+ "ACPI-nvs",
+ "MMIO ",
+ "IO-ports",
+ "PAL-code",
+ "persist ",
+ };
+ if (desc->type < ARRAY_SIZE (types_str))
+ grub_printf ("%s ", types_str[desc->type]);
+ else
+ grub_printf ("Unk %02x ", desc->type);
+
+ grub_printf (" %016" PRIxGRUB_UINT64_T "-%016" PRIxGRUB_UINT64_T
+ " %08" PRIxGRUB_UINT64_T,
+ desc->physical_start,
+ desc->physical_start + (desc->num_pages << 12) - 1,
+ desc->num_pages);
+
+ size = desc->num_pages << 12; /* 4 KiB page size */
+ /*
+ * Since size is a multiple of 4 KiB, no need to handle units
+ * of just Bytes (which would use a mask of 0x3ff).
+ *
+ * 14 characters would support the largest possible number of 4 KiB
+ * pages that are not a multiple of larger units (e.g., MiB):
+ * 17592186044415 (0xffffff_fffff000), but that uses a lot of
+ * whitespace for a rare case. 6 characters usually suffices;
+ * columns will be off if not, but this is preferable to rounding.
+ */
+ if (size & 0xfffff)
+ grub_printf (" %6" PRIuGRUB_UINT64_T "KiB", size >> 10);
+ else if (size & 0x3fffffff)
+ grub_printf (" %6" PRIuGRUB_UINT64_T "MiB", size >> 20);
+ else if (size & 0xffffffffff)
+ grub_printf (" %6" PRIuGRUB_UINT64_T "GiB", size >> 30);
+ else if (size & 0x3ffffffffffff)
+ grub_printf (" %6" PRIuGRUB_UINT64_T "TiB", size >> 40);
+ else if (size & 0xfffffffffffffff)
+ grub_printf (" %6" PRIuGRUB_UINT64_T "PiB", size >> 50);
+ else
+ grub_printf (" %6" PRIuGRUB_UINT64_T "EiB", size >> 60);
+
+ attr = desc->attribute;
+ if (attr & GRUB_EFI_MEMORY_RUNTIME)
+ grub_printf (" RT");
+ if (attr & GRUB_EFI_MEMORY_UC)
+ grub_printf (" UC");
+ if (attr & GRUB_EFI_MEMORY_WC)
+ grub_printf (" WC");
+ if (attr & GRUB_EFI_MEMORY_WT)
+ grub_printf (" WT");
+ if (attr & GRUB_EFI_MEMORY_WB)
+ grub_printf (" WB");
+ if (attr & GRUB_EFI_MEMORY_UCE)
+ grub_printf (" UCE");
+ if (attr & GRUB_EFI_MEMORY_WP)
+ grub_printf (" WP");
+ if (attr & GRUB_EFI_MEMORY_RP)
+ grub_printf (" RP");
+ if (attr & GRUB_EFI_MEMORY_XP)
+ grub_printf (" XP");
+ if (attr & GRUB_EFI_MEMORY_NV)
+ grub_printf (" NV");
+ if (attr & GRUB_EFI_MEMORY_MORE_RELIABLE)
+ grub_printf (" MR");
+ if (attr & GRUB_EFI_MEMORY_RO)
+ grub_printf (" RO");
+
+ grub_printf ("\n");
+ }
+
+ fail:
+ grub_free (memory_map);
+ return 0;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(lsefimmap)
+{
+ cmd = grub_register_command ("lsefimmap", grub_cmd_lsefimmap,
+ "", "Display EFI memory map.");
+}
+
+GRUB_MOD_FINI(lsefimmap)
+{
+ grub_unregister_command (cmd);
+}
diff --git a/grub-core/commands/efi/lsefisystab.c b/grub-core/commands/efi/lsefisystab.c
new file mode 100644
index 0000000..456198e
--- /dev/null
+++ b/grub-core/commands/efi/lsefisystab.c
@@ -0,0 +1,125 @@
+/* lsefisystab.c - Display EFI systab. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2008 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/mm.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/normal.h>
+#include <grub/charset.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct guid_mapping
+{
+ grub_efi_guid_t guid;
+ const char *name;
+};
+
+static const struct guid_mapping guid_mappings[] =
+ {
+ { GRUB_EFI_ACPI_20_TABLE_GUID, "ACPI-2.0"},
+ { GRUB_EFI_ACPI_TABLE_GUID, "ACPI-1.0"},
+ { GRUB_EFI_CRC32_GUIDED_SECTION_EXTRACTION_GUID,
+ "CRC32 GUIDED SECTION EXTRACTION"},
+ { GRUB_EFI_DEBUG_IMAGE_INFO_TABLE_GUID, "DEBUG IMAGE INFO"},
+ { GRUB_EFI_DEVICE_TREE_GUID, "DEVICE TREE"},
+ { GRUB_EFI_DXE_SERVICES_TABLE_GUID, "DXE SERVICES"},
+ { GRUB_EFI_HCDP_TABLE_GUID, "HCDP"},
+ { GRUB_EFI_HOB_LIST_GUID, "HOB LIST"},
+ { GRUB_EFI_LZMA_CUSTOM_DECOMPRESS_GUID, "LZMA CUSTOM DECOMPRESS"},
+ { GRUB_EFI_MEMORY_TYPE_INFORMATION_GUID, "MEMORY TYPE INFO"},
+ { GRUB_EFI_MPS_TABLE_GUID, "MPS"},
+ { GRUB_EFI_RT_PROPERTIES_TABLE_GUID, "RT PROPERTIES"},
+ { GRUB_EFI_SAL_TABLE_GUID, "SAL"},
+ { GRUB_EFI_SMBIOS_TABLE_GUID, "SMBIOS"},
+ { GRUB_EFI_SMBIOS3_TABLE_GUID, "SMBIOS3"},
+ { GRUB_EFI_SYSTEM_RESOURCE_TABLE_GUID, "SYSTEM RESOURCE TABLE"},
+ { GRUB_EFI_TIANO_CUSTOM_DECOMPRESS_GUID, "TIANO CUSTOM DECOMPRESS"},
+ { GRUB_EFI_TSC_FREQUENCY_GUID, "TSC FREQUENCY"},
+ };
+
+static grub_err_t
+grub_cmd_lsefisystab (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ const grub_efi_system_table_t *st = grub_efi_system_table;
+ grub_efi_configuration_table_t *t;
+ unsigned int i;
+
+ grub_printf ("Address: %p\n", st);
+ grub_printf ("Signature: %016" PRIxGRUB_UINT64_T " revision: %08x\n",
+ st->hdr.signature, st->hdr.revision);
+ {
+ char *vendor;
+ grub_uint16_t *vendor_utf16;
+ grub_printf ("Vendor: ");
+
+ for (vendor_utf16 = st->firmware_vendor; *vendor_utf16; vendor_utf16++);
+ /* Allocate extra 3 bytes to simplify math. */
+ vendor = grub_calloc (4, vendor_utf16 - st->firmware_vendor + 1);
+ if (!vendor)
+ return grub_errno;
+ *grub_utf16_to_utf8 ((grub_uint8_t *) vendor, st->firmware_vendor,
+ vendor_utf16 - st->firmware_vendor) = 0;
+ grub_printf ("%s", vendor);
+ grub_free (vendor);
+ }
+
+ grub_printf (", Version=%x\n", st->firmware_revision);
+
+ grub_printf ("%lld tables:\n", (long long) st->num_table_entries);
+ t = st->configuration_table;
+ for (i = 0; i < st->num_table_entries; i++)
+ {
+ unsigned int j;
+
+ grub_printf ("%p ", t->vendor_table);
+
+ grub_printf ("%08x-%04x-%04x-",
+ t->vendor_guid.data1, t->vendor_guid.data2,
+ t->vendor_guid.data3);
+ for (j = 0; j < 8; j++)
+ grub_printf ("%02x", t->vendor_guid.data4[j]);
+
+ for (j = 0; j < ARRAY_SIZE (guid_mappings); j++)
+ if (grub_memcmp (&guid_mappings[j].guid, &t->vendor_guid,
+ sizeof (grub_efi_guid_t)) == 0)
+ grub_printf (" %s", guid_mappings[j].name);
+
+ grub_printf ("\n");
+ t++;
+ }
+ return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(lsefisystab)
+{
+ cmd = grub_register_command ("lsefisystab", grub_cmd_lsefisystab,
+ "", "Display EFI system tables.");
+}
+
+GRUB_MOD_FINI(lsefisystab)
+{
+ grub_unregister_command (cmd);
+}
diff --git a/grub-core/commands/efi/lssal.c b/grub-core/commands/efi/lssal.c
new file mode 100644
index 0000000..5084ddd
--- /dev/null
+++ b/grub-core/commands/efi/lssal.c
@@ -0,0 +1,169 @@
+/* lssal.c - Display EFI SAL systab. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2008 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/mm.h>
+#include <grub/misc.h>
+#include <grub/normal.h>
+#include <grub/charset.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/dl.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static void
+disp_sal (void *table)
+{
+ struct grub_efi_sal_system_table *t = table;
+ void *desc;
+ grub_uint32_t len, l, i;
+
+ grub_printf ("SAL rev: %02x, signature: %x, len:%x\n",
+ t->sal_rev, t->signature, t->total_table_len);
+ grub_printf ("nbr entry: %d, chksum: %02x, SAL version A: %02x B: %02x\n",
+ t->entry_count, t->checksum,
+ t->sal_a_version, t->sal_b_version);
+ grub_printf ("OEM-ID: %-32s\n", t->oem_id);
+ grub_printf ("Product-ID: %-32s\n", t->product_id);
+
+ desc = t->entries;
+ len = t->total_table_len - sizeof (struct grub_efi_sal_system_table);
+ if (t->total_table_len <= sizeof (struct grub_efi_sal_system_table))
+ return;
+ for (i = 0; i < t->entry_count; i++)
+ {
+ switch (*(grub_uint8_t *) desc)
+ {
+ case GRUB_EFI_SAL_SYSTEM_TABLE_TYPE_ENTRYPOINT_DESCRIPTOR:
+ {
+ struct grub_efi_sal_system_table_entrypoint_descriptor *c = desc;
+ l = sizeof (*c);
+ grub_printf (" Entry point: PAL=%016" PRIxGRUB_UINT64_T
+ " SAL=%016" PRIxGRUB_UINT64_T " GP=%016"
+ PRIxGRUB_UINT64_T "\n",
+ c->pal_proc_addr, c->sal_proc_addr,
+ c->global_data_ptr);
+ }
+ break;
+ case GRUB_EFI_SAL_SYSTEM_TABLE_TYPE_MEMORY_DESCRIPTOR:
+ {
+ struct grub_efi_sal_system_table_memory_descriptor *c = desc;
+ l = sizeof (*c);
+ grub_printf (" Memory descriptor entry addr=%016" PRIxGRUB_UINT64_T
+ " len=%" PRIuGRUB_UINT64_T "KB\n",
+ c->addr, c->len * 4);
+ grub_printf (" sal_used=%d attr=%x AR=%x attr_mask=%x "
+ "type=%x usage=%x\n",
+ c->sal_used, c->attr, c->ar, c->attr_mask, c->mem_type,
+ c->usage);
+ }
+ break;
+ case GRUB_EFI_SAL_SYSTEM_TABLE_TYPE_PLATFORM_FEATURES:
+ {
+ struct grub_efi_sal_system_table_platform_features *c = desc;
+ l = sizeof (*c);
+ grub_printf (" Platform features: %02x", c->flags);
+ if (c->flags & GRUB_EFI_SAL_SYSTEM_TABLE_PLATFORM_FEATURE_BUSLOCK)
+ grub_printf (" BusLock");
+ if (c->flags & GRUB_EFI_SAL_SYSTEM_TABLE_PLATFORM_FEATURE_IRQREDIRECT)
+ grub_printf (" IrqRedirect");
+ if (c->flags & GRUB_EFI_SAL_SYSTEM_TABLE_PLATFORM_FEATURE_IPIREDIRECT)
+
+ grub_printf (" IPIRedirect");
+ if (c->flags & GRUB_EFI_SAL_SYSTEM_TABLE_PLATFORM_FEATURE_ITCDRIFT)
+
+ grub_printf (" ITCDrift");
+ grub_printf ("\n");
+ }
+ break;
+ case GRUB_EFI_SAL_SYSTEM_TABLE_TYPE_TRANSLATION_REGISTER_DESCRIPTOR:
+ {
+ struct grub_efi_sal_system_table_translation_register_descriptor *c
+ = desc;
+ l = sizeof (*c);
+ grub_printf (" TR type=%d num=%d va=%016" PRIxGRUB_UINT64_T
+ " pte=%016" PRIxGRUB_UINT64_T "\n",
+ c->register_type, c->register_number,
+ c->addr, c->page_size);
+ }
+ break;
+ case GRUB_EFI_SAL_SYSTEM_TABLE_TYPE_PURGE_TRANSLATION_COHERENCE:
+ {
+ struct grub_efi_sal_system_table_purge_translation_coherence *c
+ = desc;
+ l = sizeof (*c);
+ grub_printf (" PTC coherence nbr=%d addr=%016" PRIxGRUB_UINT64_T "\n",
+ c->ndomains, c->coherence);
+ }
+ break;
+ case GRUB_EFI_SAL_SYSTEM_TABLE_TYPE_AP_WAKEUP:
+ {
+ struct grub_efi_sal_system_table_ap_wakeup *c = desc;
+ l = sizeof (*c);
+ grub_printf (" AP wake-up: mec=%d vect=%" PRIxGRUB_UINT64_T "\n",
+ c->mechanism, c->vector);
+ }
+ break;
+ default:
+ grub_printf (" unknown entry 0x%x\n", *(grub_uint8_t *)desc);
+ return;
+ }
+ desc = (grub_uint8_t *)desc + l;
+ if (len <= l)
+ return;
+ len -= l;
+ }
+}
+
+static grub_err_t
+grub_cmd_lssal (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ const grub_efi_system_table_t *st = grub_efi_system_table;
+ grub_efi_configuration_table_t *t = st->configuration_table;
+ unsigned int i;
+ grub_efi_packed_guid_t guid = GRUB_EFI_SAL_TABLE_GUID;
+
+ for (i = 0; i < st->num_table_entries; i++)
+ {
+ if (grub_memcmp (&guid, &t->vendor_guid,
+ sizeof (grub_efi_packed_guid_t)) == 0)
+ {
+ disp_sal (t->vendor_table);
+ return GRUB_ERR_NONE;
+ }
+ t++;
+ }
+ grub_printf ("SAL not found\n");
+ return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(lssal)
+{
+ cmd = grub_register_command ("lssal", grub_cmd_lssal, "",
+ "Display SAL system table.");
+}
+
+GRUB_MOD_FINI(lssal)
+{
+ grub_unregister_command (cmd);
+}
diff --git a/grub-core/commands/efi/smbios.c b/grub-core/commands/efi/smbios.c
new file mode 100644
index 0000000..75202d5
--- /dev/null
+++ b/grub-core/commands/efi/smbios.c
@@ -0,0 +1,61 @@
+/* smbios.c - get smbios tables. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2019 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/smbios.h>
+#include <grub/misc.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/api.h>
+
+struct grub_smbios_eps *
+grub_machine_smbios_get_eps (void)
+{
+ unsigned i;
+ static grub_efi_packed_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
+
+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
+ {
+ grub_efi_packed_guid_t *guid =
+ &grub_efi_system_table->configuration_table[i].vendor_guid;
+
+ if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_efi_packed_guid_t)))
+ return (struct grub_smbios_eps *)
+ grub_efi_system_table->configuration_table[i].vendor_table;
+ }
+
+ return 0;
+}
+
+struct grub_smbios_eps3 *
+grub_machine_smbios_get_eps3 (void)
+{
+ unsigned i;
+ static grub_efi_packed_guid_t smbios3_guid = GRUB_EFI_SMBIOS3_TABLE_GUID;
+
+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
+ {
+ grub_efi_packed_guid_t *guid =
+ &grub_efi_system_table->configuration_table[i].vendor_guid;
+
+ if (! grub_memcmp (guid, &smbios3_guid, sizeof (grub_efi_packed_guid_t)))
+ return (struct grub_smbios_eps3 *)
+ grub_efi_system_table->configuration_table[i].vendor_table;
+ }
+
+ return 0;
+}
diff --git a/grub-core/commands/efi/tpm.c b/grub-core/commands/efi/tpm.c
new file mode 100644
index 0000000..a97d853
--- /dev/null
+++ b/grub-core/commands/efi/tpm.c
@@ -0,0 +1,241 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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/>.
+ *
+ * EFI TPM support code.
+ */
+
+#include <grub/err.h>
+#include <grub/i18n.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/tpm.h>
+#include <grub/mm.h>
+#include <grub/tpm.h>
+#include <grub/term.h>
+
+typedef TCG_PCR_EVENT grub_tpm_event_t;
+
+static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
+static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
+
+static grub_efi_handle_t *grub_tpm_handle;
+static grub_uint8_t grub_tpm_version;
+
+static grub_int8_t tpm1_present = -1;
+static grub_int8_t tpm2_present = -1;
+
+static grub_efi_boolean_t
+grub_tpm1_present (grub_efi_tpm_protocol_t *tpm)
+{
+ grub_efi_status_t status;
+ TCG_EFI_BOOT_SERVICE_CAPABILITY caps;
+ grub_uint32_t flags;
+ grub_efi_physical_address_t eventlog, lastevent;
+
+ if (tpm1_present != -1)
+ return (grub_efi_boolean_t) tpm1_present;
+
+ caps.Size = (grub_uint8_t) sizeof (caps);
+
+ status = efi_call_5 (tpm->status_check, tpm, &caps, &flags, &eventlog,
+ &lastevent);
+
+ if (status != GRUB_EFI_SUCCESS || caps.TPMDeactivatedFlag
+ || !caps.TPMPresentFlag)
+ tpm1_present = 0;
+ else
+ tpm1_present = 1;
+
+ grub_dprintf ("tpm", "tpm1%s present\n", tpm1_present ? "" : " NOT");
+
+ return (grub_efi_boolean_t) tpm1_present;
+}
+
+static grub_efi_boolean_t
+grub_tpm2_present (grub_efi_tpm2_protocol_t *tpm)
+{
+ grub_efi_status_t status;
+ EFI_TCG2_BOOT_SERVICE_CAPABILITY caps;
+
+ caps.Size = (grub_uint8_t) sizeof (caps);
+
+ if (tpm2_present != -1)
+ return (grub_efi_boolean_t) tpm2_present;
+
+ status = efi_call_2 (tpm->get_capability, tpm, &caps);
+
+ if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
+ tpm2_present = 0;
+ else
+ tpm2_present = 1;
+
+ grub_dprintf ("tpm", "tpm2%s present\n", tpm2_present ? "" : " NOT");
+
+ return (grub_efi_boolean_t) tpm2_present;
+}
+
+static grub_efi_boolean_t
+grub_tpm_handle_find (grub_efi_handle_t *tpm_handle,
+ grub_efi_uint8_t *protocol_version)
+{
+ grub_efi_handle_t *handles;
+ grub_efi_uintn_t num_handles;
+
+ if (grub_tpm_handle != NULL)
+ {
+ *tpm_handle = grub_tpm_handle;
+ *protocol_version = grub_tpm_version;
+ return 1;
+ }
+
+ handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm_guid, NULL,
+ &num_handles);
+ if (handles && num_handles > 0)
+ {
+ grub_tpm_handle = handles[0];
+ *tpm_handle = handles[0];
+ grub_tpm_version = 1;
+ *protocol_version = 1;
+ grub_dprintf ("tpm", "TPM handle Found, version: 1\n");
+ return 1;
+ }
+
+ handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm2_guid, NULL,
+ &num_handles);
+ if (handles && num_handles > 0)
+ {
+ grub_tpm_handle = handles[0];
+ *tpm_handle = handles[0];
+ grub_tpm_version = 2;
+ *protocol_version = 2;
+ grub_dprintf ("tpm", "TPM handle Found, version: 2\n");
+ return 1;
+ }
+
+ return 0;
+}
+
+static grub_err_t
+grub_efi_log_event_status (grub_efi_status_t status)
+{
+ switch (status)
+ {
+ case GRUB_EFI_SUCCESS:
+ return 0;
+ case GRUB_EFI_DEVICE_ERROR:
+ return grub_error (GRUB_ERR_IO, N_("Command failed"));
+ case GRUB_EFI_INVALID_PARAMETER:
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
+ case GRUB_EFI_BUFFER_TOO_SMALL:
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
+ case GRUB_EFI_NOT_FOUND:
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
+ default:
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
+ }
+}
+
+static grub_err_t
+grub_tpm1_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
+ grub_size_t size, grub_uint8_t pcr,
+ const char *description)
+{
+ grub_tpm_event_t *event;
+ grub_efi_status_t status;
+ grub_efi_tpm_protocol_t *tpm;
+ grub_efi_physical_address_t lastevent;
+ grub_uint32_t algorithm;
+ grub_uint32_t eventnum = 0;
+
+ tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+ if (!grub_tpm1_present (tpm))
+ return 0;
+
+ event = grub_zalloc (sizeof (*event) + grub_strlen (description) + 1);
+ if (!event)
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+ N_("cannot allocate TPM event buffer"));
+
+ event->PCRIndex = pcr;
+ event->EventType = EV_IPL;
+ event->EventSize = grub_strlen (description) + 1;
+ grub_memcpy (event->Event, description, event->EventSize);
+
+ algorithm = TCG_ALG_SHA;
+ status = efi_call_7 (tpm->log_extend_event, tpm, (grub_addr_t) buf, (grub_uint64_t) size,
+ algorithm, event, &eventnum, &lastevent);
+ grub_free (event);
+
+ return grub_efi_log_event_status (status);
+}
+
+static grub_err_t
+grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
+ grub_size_t size, grub_uint8_t pcr,
+ const char *description)
+{
+ EFI_TCG2_EVENT *event;
+ grub_efi_status_t status;
+ grub_efi_tpm2_protocol_t *tpm;
+
+ tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+ if (!grub_tpm2_present (tpm))
+ return 0;
+
+ event =
+ grub_zalloc (sizeof (EFI_TCG2_EVENT) + grub_strlen (description) + 1);
+ if (!event)
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+ N_("cannot allocate TPM event buffer"));
+
+ event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);
+ event->Header.HeaderVersion = 1;
+ event->Header.PCRIndex = pcr;
+ event->Header.EventType = EV_IPL;
+ event->Size =
+ sizeof (*event) - sizeof (event->Event) + grub_strlen (description) + 1;
+ grub_memcpy (event->Event, description, grub_strlen (description) + 1);
+
+ status = efi_call_5 (tpm->hash_log_extend_event, tpm, 0, (grub_addr_t) buf,
+ (grub_uint64_t) size, event);
+ grub_free (event);
+
+ return grub_efi_log_event_status (status);
+}
+
+grub_err_t
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
+ const char *description)
+{
+ grub_efi_handle_t tpm_handle;
+ grub_efi_uint8_t protocol_version;
+
+ if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
+ return 0;
+
+ grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
+ pcr, size, description);
+
+ if (protocol_version == 1)
+ return grub_tpm1_log_event (tpm_handle, buf, size, pcr, description);
+ else
+ return grub_tpm2_log_event (tpm_handle, buf, size, pcr, description);
+}