summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2019-02-26 08:45:08 -0500
committerTom Rini <trini@konsulko.com>2019-02-26 08:45:08 -0500
commitb3820ba997f004a376efc5446683101ff42b05af (patch)
tree384943346ded7a82aa22747fbe2010dc9f69ca33 /cmd
parentafd46c5f13d0c93c44008bd7040227d0b84e31b9 (diff)
parent00358bb804e9e8aa754f946954d299ba9e658a84 (diff)
Merge tag 'efi-2019-04-rc3' of https://github.com/xypron2/u-boot
Pull request for the UEFI sub-system for v2019.04-rc3 A new option -e is added to the env command which allows to display and change UEFI variables in a user friendly way. A new command efidebug is introduced to edit the UEFI boot sequence and to display different aspects of the state of the UEFI sub-system: memory map, loaded images, handles, drivers and devices. A bug in the UEFI boot manager is fixed.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/Kconfig20
-rw-r--r--cmd/Makefile2
-rw-r--r--cmd/efidebug.c1046
-rw-r--r--cmd/nvedit.c28
-rw-r--r--cmd/nvedit_efi.c399
5 files changed, 1494 insertions, 1 deletions
diff --git a/cmd/Kconfig b/cmd/Kconfig
index ed8d85bfa6..4bcc5c4557 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -420,6 +420,16 @@ config CMD_ENV_FLAGS
be deleted. This command shows the variables that have special
flags.
+config CMD_NVEDIT_EFI
+ bool "env [set|print] -e - set/print UEFI variables"
+ depends on EFI_LOADER
+ default y
+ imply HEXDUMP
+ help
+ UEFI variables are encoded as some form of U-Boot variables.
+ If enabled, we are allowed to set/print UEFI variables using
+ "env" command with "-e" option without knowing details.
+
endmenu
menu "Memory commands"
@@ -1397,6 +1407,16 @@ config CMD_DISPLAY
displayed on a simple board-specific display. Implement
display_putc() to use it.
+config CMD_EFIDEBUG
+ bool "efidebug - display/configure UEFI environment"
+ depends on EFI_LOADER
+ default n
+ help
+ Enable the 'efidebug' command which provides a subset of UEFI
+ shell utility with simplified functionality. It will be useful
+ particularly for managing boot parameters as well as examining
+ various EFI status for debugging.
+
config CMD_LED
bool "led"
depends on LED
diff --git a/cmd/Makefile b/cmd/Makefile
index a127a99539..acb85f49fb 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_CMD_ECHO) += echo.o
obj-$(CONFIG_ENV_IS_IN_EEPROM) += eeprom.o
obj-$(CONFIG_CMD_EEPROM) += eeprom.o
obj-$(CONFIG_EFI_STUB) += efi.o
+obj-$(CONFIG_CMD_EFIDEBUG) += efidebug.o
obj-$(CONFIG_CMD_ELF) += elf.o
obj-$(CONFIG_HUSH_PARSER) += exit.o
obj-$(CONFIG_CMD_EXT4) += ext4.o
@@ -98,6 +99,7 @@ obj-$(CONFIG_CMD_MTD) += mtd.o
obj-$(CONFIG_CMD_MTDPARTS) += mtdparts.o
obj-$(CONFIG_CMD_NAND) += nand.o
obj-$(CONFIG_CMD_NET) += net.o
+obj-$(CONFIG_CMD_NVEDIT_EFI) += nvedit_efi.o
obj-$(CONFIG_CMD_ONENAND) += onenand.o
obj-$(CONFIG_CMD_OSD) += osd.o
obj-$(CONFIG_CMD_PART) += part.o
diff --git a/cmd/efidebug.c b/cmd/efidebug.c
new file mode 100644
index 0000000000..5072a7b39b
--- /dev/null
+++ b/cmd/efidebug.c
@@ -0,0 +1,1046 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * UEFI Shell-like command
+ *
+ * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
+ */
+
+#include <charset.h>
+#include <common.h>
+#include <command.h>
+#include <efi_loader.h>
+#include <environment.h>
+#include <exports.h>
+#include <malloc.h>
+#include <search.h>
+#include <linux/ctype.h>
+
+#define BS systab.boottime
+#define RT systab.runtime
+
+/**
+ * efi_get_device_handle_info() - get information of UEFI device
+ *
+ * @handle: Handle of UEFI device
+ * @dev_path_text: Pointer to text of device path
+ * Return: 0 on success, -1 on failure
+ *
+ * Currently return a formatted text of device path.
+ */
+static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
+{
+ struct efi_device_path *dp;
+ efi_status_t ret;
+
+ ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
+ (void **)&dp, NULL /* FIXME */, NULL,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL));
+ if (ret == EFI_SUCCESS) {
+ *dev_path_text = efi_dp_str(dp);
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+#define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
+
+static const char spc[] = " ";
+static const char sep[] = "================";
+
+/**
+ * do_efi_show_devices() - show UEFI devices
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "devices" sub-command.
+ * Show all UEFI devices and their information.
+ */
+static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ efi_handle_t *handles;
+ efi_uintn_t num, i;
+ u16 *dev_path_text;
+ efi_status_t ret;
+
+ ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
+ &num, &handles));
+ if (ret != EFI_SUCCESS)
+ return CMD_RET_FAILURE;
+
+ if (!num)
+ return CMD_RET_SUCCESS;
+
+ printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
+ printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
+ for (i = 0; i < num; i++) {
+ if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
+ printf("%p %ls\n", handles[i], dev_path_text);
+ efi_free_pool(dev_path_text);
+ }
+ }
+
+ EFI_CALL(BS->free_pool(handles));
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * efi_get_driver_handle_info() - get information of UEFI driver
+ *
+ * @handle: Handle of UEFI device
+ * @driver_name: Driver name
+ * @image_path: Pointer to text of device path
+ * Return: 0 on success, -1 on failure
+ *
+ * Currently return no useful information as all UEFI drivers are
+ * built-in..
+ */
+static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
+ u16 **image_path)
+{
+ struct efi_handler *handler;
+ struct efi_loaded_image *image;
+ efi_status_t ret;
+
+ /*
+ * driver name
+ * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
+ */
+ *driver_name = NULL;
+
+ /* image name */
+ ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
+ if (ret != EFI_SUCCESS) {
+ *image_path = NULL;
+ return 0;
+ }
+
+ image = handler->protocol_interface;
+ *image_path = efi_dp_str(image->file_path);
+
+ return 0;
+}
+
+/**
+ * do_efi_show_drivers() - show UEFI drivers
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "drivers" sub-command.
+ * Show all UEFI drivers and their information.
+ */
+static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ efi_handle_t *handles;
+ efi_uintn_t num, i;
+ u16 *driver_name, *image_path_text;
+ efi_status_t ret;
+
+ ret = EFI_CALL(BS->locate_handle_buffer(
+ BY_PROTOCOL, &efi_guid_driver_binding_protocol,
+ NULL, &num, &handles));
+ if (ret != EFI_SUCCESS)
+ return CMD_RET_FAILURE;
+
+ if (!num)
+ return CMD_RET_SUCCESS;
+
+ printf("Driver%.*s Name Image Path\n",
+ EFI_HANDLE_WIDTH - 6, spc);
+ printf("%.*s ==================== ====================\n",
+ EFI_HANDLE_WIDTH, sep);
+ for (i = 0; i < num; i++) {
+ if (!efi_get_driver_handle_info(handles[i], &driver_name,
+ &image_path_text)) {
+ if (image_path_text)
+ printf("%p %-20ls %ls\n", handles[i],
+ driver_name, image_path_text);
+ else
+ printf("%p %-20ls <built-in>\n",
+ handles[i], driver_name);
+ EFI_CALL(BS->free_pool(driver_name));
+ EFI_CALL(BS->free_pool(image_path_text));
+ }
+ }
+
+ EFI_CALL(BS->free_pool(handles));
+
+ return CMD_RET_SUCCESS;
+}
+
+static const struct {
+ const char *text;
+ const efi_guid_t guid;
+} guid_list[] = {
+ {
+ "Device Path",
+ DEVICE_PATH_GUID,
+ },
+ {
+ "Device Path To Text",
+ EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
+ },
+ {
+ "Device Path Utilities",
+ EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
+ },
+ {
+ "Unicode Collation 2",
+ EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
+ },
+ {
+ "Driver Binding",
+ EFI_DRIVER_BINDING_PROTOCOL_GUID,
+ },
+ {
+ "Simple Text Input",
+ EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
+ },
+ {
+ "Simple Text Input Ex",
+ EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
+ },
+ {
+ "Simple Text Output",
+ EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
+ },
+ {
+ "Block IO",
+ BLOCK_IO_GUID,
+ },
+ {
+ "Simple File System",
+ EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
+ },
+ {
+ "Loaded Image",
+ LOADED_IMAGE_PROTOCOL_GUID,
+ },
+ {
+ "GOP",
+ EFI_GOP_GUID,
+ },
+};
+
+/**
+ * get_guid_text - get string of protocol guid
+ * @guid: Protocol guid
+ * Return: String
+ *
+ * Return string for display to represent the protocol.
+ */
+static const char *get_guid_text(const efi_guid_t *guid)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(guid_list); i++)
+ if (!guidcmp(&guid_list[i].guid, guid))
+ break;
+
+ if (i != ARRAY_SIZE(guid_list))
+ return guid_list[i].text;
+ else
+ return NULL;
+}
+
+/**
+ * do_efi_show_handles() - show UEFI handles
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "dh" sub-command.
+ * Show all UEFI handles and their information, currently all protocols
+ * added to handle.
+ */
+static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ efi_handle_t *handles;
+ efi_guid_t **guid;
+ efi_uintn_t num, count, i, j;
+ const char *guid_text;
+ efi_status_t ret;
+
+ ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
+ &num, &handles));
+ if (ret != EFI_SUCCESS)
+ return CMD_RET_FAILURE;
+
+ if (!num)
+ return CMD_RET_SUCCESS;
+
+ printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
+ printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
+ for (i = 0; i < num; i++) {
+ printf("%p", handles[i]);
+ ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
+ &count));
+ if (ret || !count) {
+ putc('\n');
+ continue;
+ }
+
+ for (j = 0; j < count; j++) {
+ if (j)
+ printf(", ");
+ else
+ putc(' ');
+
+ guid_text = get_guid_text(guid[j]);
+ if (guid_text)
+ puts(guid_text);
+ else
+ printf("%pUl", guid[j]);
+ }
+ putc('\n');
+ }
+
+ EFI_CALL(BS->free_pool(handles));
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * do_efi_show_images() - show UEFI images
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "images" sub-command.
+ * Show all UEFI loaded images and their information.
+ */
+static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ efi_print_image_infos(NULL);
+
+ return CMD_RET_SUCCESS;
+}
+
+static const char * const efi_mem_type_string[] = {
+ [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
+ [EFI_LOADER_CODE] = "LOADER CODE",
+ [EFI_LOADER_DATA] = "LOADER DATA",
+ [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
+ [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
+ [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
+ [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
+ [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
+ [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
+ [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
+ [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
+ [EFI_MMAP_IO] = "IO",
+ [EFI_MMAP_IO_PORT] = "IO PORT",
+ [EFI_PAL_CODE] = "PAL",
+};
+
+static const struct efi_mem_attrs {
+ const u64 bit;
+ const char *text;
+} efi_mem_attrs[] = {
+ {EFI_MEMORY_UC, "UC"},
+ {EFI_MEMORY_UC, "UC"},
+ {EFI_MEMORY_WC, "WC"},
+ {EFI_MEMORY_WT, "WT"},
+ {EFI_MEMORY_WB, "WB"},
+ {EFI_MEMORY_UCE, "UCE"},
+ {EFI_MEMORY_WP, "WP"},
+ {EFI_MEMORY_RP, "RP"},
+ {EFI_MEMORY_XP, "WP"},
+ {EFI_MEMORY_NV, "NV"},
+ {EFI_MEMORY_MORE_RELIABLE, "REL"},
+ {EFI_MEMORY_RO, "RO"},
+ {EFI_MEMORY_RUNTIME, "RT"},
+};
+
+/**
+ * print_memory_attributes() - print memory map attributes
+ * @attributes: Attribute value
+ *
+ * Print memory map attributes
+ */
+static void print_memory_attributes(u64 attributes)
+{
+ int sep, i;
+
+ for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
+ if (attributes & efi_mem_attrs[i].bit) {
+ if (sep) {
+ putc('|');
+ } else {
+ putc(' ');
+ sep = 1;
+ }
+ puts(efi_mem_attrs[i].text);
+ }
+}
+
+#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
+
+/**
+ * do_efi_show_memmap() - show UEFI memory map
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "memmap" sub-command.
+ * Show UEFI memory map.
+ */
+static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ struct efi_mem_desc *memmap = NULL, *map;
+ efi_uintn_t map_size = 0;
+ const char *type;
+ int i;
+ efi_status_t ret;
+
+ ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
+ if (ret == EFI_BUFFER_TOO_SMALL) {
+ map_size += sizeof(struct efi_mem_desc); /* for my own */
+ ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
+ map_size, (void *)&memmap));
+ if (ret != EFI_SUCCESS)
+ return CMD_RET_FAILURE;
+ ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
+ NULL, NULL, NULL));
+ }
+ if (ret != EFI_SUCCESS) {
+ EFI_CALL(BS->free_pool(memmap));
+ return CMD_RET_FAILURE;
+ }
+
+ printf("Type Start%.*s End%.*s Attributes\n",
+ EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
+ printf("================ %.*s %.*s ==========\n",
+ EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
+ for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
+ if (map->type < EFI_MAX_MEMORY_TYPE)
+ type = efi_mem_type_string[map->type];
+ else
+ type = "(unknown)";
+
+ printf("%-16s %.*llx-%.*llx", type,
+ EFI_PHYS_ADDR_WIDTH,
+ map->physical_start,
+ EFI_PHYS_ADDR_WIDTH,
+ map->physical_start + map->num_pages * EFI_PAGE_SIZE);
+
+ print_memory_attributes(map->attribute);
+ putc('\n');
+ }
+
+ EFI_CALL(BS->free_pool(memmap));
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * do_efi_boot_add() - set UEFI load option
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success,
+ * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "boot add" sub-command.
+ * Create or change UEFI load option.
+ * - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
+ */
+static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ int id;
+ char *endp;
+ char var_name[9];
+ u16 var_name16[9], *p;
+ efi_guid_t guid;
+ size_t label_len, label_len16;
+ u16 *label;
+ struct efi_device_path *device_path = NULL, *file_path = NULL;
+ struct efi_load_option lo;
+ void *data = NULL;
+ efi_uintn_t size;
+ int ret;
+
+ if (argc < 6 || argc > 7)
+ return CMD_RET_USAGE;
+
+ id = (int)simple_strtoul(argv[1], &endp, 16);
+ if (*endp != '\0' || id > 0xffff)
+ return CMD_RET_FAILURE;
+
+ sprintf(var_name, "Boot%04X", id);
+ p = var_name16;
+ utf8_utf16_strncpy(&p, var_name, 9);
+
+ guid = efi_global_variable_guid;
+
+ /* attributes */
+ lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
+
+ /* label */
+ label_len = strlen(argv[2]);
+ label_len16 = utf8_utf16_strnlen(argv[2], label_len);
+ label = malloc((label_len16 + 1) * sizeof(u16));
+ if (!label)
+ return CMD_RET_FAILURE;
+ lo.label = label; /* label will be changed below */
+ utf8_utf16_strncpy(&label, argv[2], label_len);
+
+ /* file path */
+ ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
+ &file_path);
+ if (ret != EFI_SUCCESS) {
+ printf("Cannot create device path for \"%s %s\"\n",
+ argv[3], argv[4]);
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+ lo.file_path = file_path;
+ lo.file_path_length = efi_dp_size(file_path)
+ + sizeof(struct efi_device_path); /* for END */
+
+ /* optional data */
+ lo.optional_data = (u8 *)(argc == 6 ? "" : argv[6]);
+
+ size = efi_serialize_load_option(&lo, (u8 **)&data);
+ if (!size) {
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+
+ ret = EFI_CALL(RT->set_variable(var_name16, &guid,
+ EFI_VARIABLE_BOOTSERVICE_ACCESS |
+ EFI_VARIABLE_RUNTIME_ACCESS,
+ size, data));
+ ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+out:
+ free(data);
+ efi_free_pool(device_path);
+ efi_free_pool(file_path);
+ free(lo.label);
+
+ return ret;
+}
+
+/**
+ * do_efi_boot_rm() - delete UEFI load options
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "boot rm" sub-command.
+ * Delete UEFI load options.
+ * - boot rm <id> ...
+ */
+static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ efi_guid_t guid;
+ int id, i;
+ char *endp;
+ char var_name[9];
+ u16 var_name16[9];
+ efi_status_t ret;
+
+ if (argc == 1)
+ return CMD_RET_USAGE;
+
+ guid = efi_global_variable_guid;
+ for (i = 1; i < argc; i++, argv++) {
+ id = (int)simple_strtoul(argv[1], &endp, 16);
+ if (*endp != '\0' || id > 0xffff)
+ return CMD_RET_FAILURE;
+
+ sprintf(var_name, "Boot%04X", id);
+ utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
+
+ ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
+ if (ret) {
+ printf("cannot remove Boot%04X", id);
+ return CMD_RET_FAILURE;
+ }
+ }
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * show_efi_boot_opt_data() - dump UEFI load option
+ *
+ * @id: Load option number
+ * @data: Value of UEFI load option variable
+ *
+ * Decode the value of UEFI load option variable and print information.
+ */
+static void show_efi_boot_opt_data(int id, void *data)
+{
+ struct efi_load_option lo;
+ char *label, *p;
+ size_t label_len16, label_len;
+ u16 *dp_str;
+
+ efi_deserialize_load_option(&lo, data);
+
+ label_len16 = u16_strlen(lo.label);
+ label_len = utf16_utf8_strnlen(lo.label, label_len16);
+ label = malloc(label_len + 1);
+ if (!label)
+ return;
+ p = label;
+ utf16_utf8_strncpy(&p, lo.label, label_len16);
+
+ printf("Boot%04X:\n", id);
+ printf("\tattributes: %c%c%c (0x%08x)\n",
+ /* ACTIVE */
+ lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
+ /* FORCE RECONNECT */
+ lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
+ /* HIDDEN */
+ lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
+ lo.attributes);
+ printf("\tlabel: %s\n", label);
+
+ dp_str = efi_dp_str(lo.file_path);
+ printf("\tfile_path: %ls\n", dp_str);
+ efi_free_pool(dp_str);
+
+ printf("\tdata: %s\n", lo.optional_data);
+
+ free(label);
+}
+
+/**
+ * show_efi_boot_opt() - dump UEFI load option
+ *
+ * @id: Load option number
+ *
+ * Dump information defined by UEFI load option.
+ */
+static void show_efi_boot_opt(int id)
+{
+ char var_name[9];
+ u16 var_name16[9], *p;
+ efi_guid_t guid;
+ void *data = NULL;
+ efi_uintn_t size;
+ int ret;
+
+ sprintf(var_name, "Boot%04X", id);
+ p = var_name16;
+ utf8_utf16_strncpy(&p, var_name, 9);
+ guid = efi_global_variable_guid;
+
+ size = 0;
+ ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
+ if (ret == (int)EFI_BUFFER_TOO_SMALL) {
+ data = malloc(size);
+ ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
+ data));
+ }
+ if (ret == EFI_SUCCESS)
+ show_efi_boot_opt_data(id, data);
+ else if (ret == EFI_NOT_FOUND)
+ printf("Boot%04X: not found\n", id);
+
+ free(data);
+}
+
+/**
+ * show_efi_boot_dump() - dump all UEFI load options
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "boot dump" sub-command.
+ * Dump information of all UEFI load options defined.
+ * - boot dump
+ */
+static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ char regex[256];
+ char * const regexlist[] = {regex};
+ char *variables = NULL, *boot, *value;
+ int len;
+ int id;
+
+ if (argc > 1)
+ return CMD_RET_USAGE;
+
+ snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_Boot[0-9A-F]+");
+
+ /* TODO: use GetNextVariableName? */
+ len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY,
+ &variables, 0, 1, regexlist);
+
+ if (!len)
+ return CMD_RET_SUCCESS;
+
+ if (len < 0)
+ return CMD_RET_FAILURE;
+
+ boot = variables;
+ while (*boot) {
+ value = strstr(boot, "Boot") + 4;
+ id = (int)simple_strtoul(value, NULL, 16);
+ show_efi_boot_opt(id);
+ boot = strchr(boot, '\n');
+ if (!*boot)
+ break;
+ boot++;
+ }
+ free(variables);
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * show_efi_boot_order() - show order of UEFI load options
+ *
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Show order of UEFI load options defined by BootOrder variable.
+ */
+static int show_efi_boot_order(void)
+{
+ efi_guid_t guid;
+ u16 *bootorder = NULL;
+ efi_uintn_t size;
+ int num, i;
+ char var_name[9];
+ u16 var_name16[9], *p16;
+ void *data;
+ struct efi_load_option lo;
+ char *label, *p;
+ size_t label_len16, label_len;
+ efi_status_t ret;
+
+ guid = efi_global_variable_guid;
+ size = 0;
+ ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
+ NULL));
+ if (ret == EFI_BUFFER_TOO_SMALL) {
+ bootorder = malloc(size);
+ ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
+ &size, bootorder));
+ }
+ if (ret == EFI_NOT_FOUND) {
+ printf("BootOrder not defined\n");
+ ret = CMD_RET_SUCCESS;
+ goto out;
+ } else if (ret != EFI_SUCCESS) {
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+
+ num = size / sizeof(u16);
+ for (i = 0; i < num; i++) {
+ sprintf(var_name, "Boot%04X", bootorder[i]);
+ p16 = var_name16;
+ utf8_utf16_strncpy(&p16, var_name, 9);
+
+ size = 0;
+ ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
+ NULL));
+ if (ret != EFI_BUFFER_TOO_SMALL) {
+ printf("%2d: Boot%04X: (not defined)\n",
+ i + 1, bootorder[i]);
+ continue;
+ }
+
+ data = malloc(size);
+ if (!data) {
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+ ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
+ data));
+ if (ret != EFI_SUCCESS) {
+ free(data);
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+
+ efi_deserialize_load_option(&lo, data);
+
+ label_len16 = u16_strlen(lo.label);
+ label_len = utf16_utf8_strnlen(lo.label, label_len16);
+ label = malloc(label_len + 1);
+ if (!label) {
+ free(data);
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+ p = label;
+ utf16_utf8_strncpy(&p, lo.label, label_len16);
+ printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
+ free(label);
+
+ free(data);
+ }
+out:
+ free(bootorder);
+
+ return ret;
+}
+
+/**
+ * do_efi_boot_next() - manage UEFI BootNext variable
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success,
+ * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "boot next" sub-command.
+ * Set BootNext variable.
+ * - boot next <id>
+ */
+static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ u16 bootnext;
+ efi_uintn_t size;
+ char *endp;
+ efi_guid_t guid;
+ efi_status_t ret;
+
+ if (argc != 2)
+ return CMD_RET_USAGE;
+
+ bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
+ if (*endp != '\0' || bootnext > 0xffff) {
+ printf("invalid value: %s\n", argv[1]);
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+
+ guid = efi_global_variable_guid;
+ size = sizeof(u16);
+ ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
+ EFI_VARIABLE_BOOTSERVICE_ACCESS |
+ EFI_VARIABLE_RUNTIME_ACCESS,
+ size, &bootnext));
+ ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+out:
+ return ret;
+}
+
+/**
+ * do_efi_boot_order() - manage UEFI BootOrder variable
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "boot order" sub-command.
+ * Show order of UEFI load options, or change it in BootOrder variable.
+ * - boot order [<id> ...]
+ */
+static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ u16 *bootorder = NULL;
+ efi_uintn_t size;
+ int id, i;
+ char *endp;
+ efi_guid_t guid;
+ efi_status_t ret;
+
+ if (argc == 1)
+ return show_efi_boot_order();
+
+ argc--;
+ argv++;
+
+ size = argc * sizeof(u16);
+ bootorder = malloc(size);
+ if (!bootorder)
+ return CMD_RET_FAILURE;
+
+ for (i = 0; i < argc; i++) {
+ id = (int)simple_strtoul(argv[i], &endp, 16);
+ if (*endp != '\0' || id > 0xffff) {
+ printf("invalid value: %s\n", argv[i]);
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+
+ bootorder[i] = (u16)id;
+ }
+
+ guid = efi_global_variable_guid;
+ ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
+ EFI_VARIABLE_BOOTSERVICE_ACCESS |
+ EFI_VARIABLE_RUNTIME_ACCESS,
+ size, bootorder));
+ ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+out:
+ free(bootorder);
+
+ return ret;
+}
+
+static cmd_tbl_t cmd_efidebug_boot_sub[] = {
+ U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
+ U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
+ U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
+ U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
+ U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
+ "", ""),
+};
+
+/**
+ * do_efi_boot_opt() - manage UEFI load options
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success,
+ * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "boot" sub-command.
+ * See above for details of sub-commands.
+ */
+static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ cmd_tbl_t *cp;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+ argc--; argv++;
+
+ cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
+ ARRAY_SIZE(cmd_efidebug_boot_sub));
+ if (!cp)
+ return CMD_RET_USAGE;
+
+ return cp->cmd(cmdtp, flag, argc, argv);
+}
+
+static cmd_tbl_t cmd_efidebug_sub[] = {
+ U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
+ U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
+ "", ""),
+ U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
+ "", ""),
+ U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
+ "", ""),
+ U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
+ "", ""),
+ U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
+ "", ""),
+};
+
+/**
+ * do_efidebug() - display and configure UEFI environment
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success,
+ * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug command which allows us to display and
+ * configure UEFI environment.
+ * See above for details of sub-commands.
+ */
+static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ cmd_tbl_t *cp;
+ efi_status_t r;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+ argc--; argv++;
+
+ /* Initialize UEFI drivers */
+ r = efi_init_obj_list();
+ if (r != EFI_SUCCESS) {
+ printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
+ r & ~EFI_ERROR_MASK);
+ return CMD_RET_FAILURE;
+ }
+
+ cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
+ ARRAY_SIZE(cmd_efidebug_sub));
+ if (!cp)
+ return CMD_RET_USAGE;
+
+ return cp->cmd(cmdtp, flag, argc, argv);
+}
+
+#ifdef CONFIG_SYS_LONGHELP
+static char efidebug_help_text[] =
+ " - UEFI Shell-like interface to configure UEFI environment\n"
+ "\n"
+ "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
+ " - set UEFI BootXXXX variable\n"
+ " <load options> will be passed to UEFI application\n"
+ "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
+ " - delete UEFI BootXXXX variables\n"
+ "efidebug boot dump\n"
+ " - dump all UEFI BootXXXX variables\n"
+ "efidebug boot next <bootid>\n"
+ " - set UEFI BootNext variable\n"
+ "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
+ " - set/show UEFI boot order\n"
+ "\n"
+ "efidebug devices\n"
+ " - show uefi devices\n"
+ "efidebug drivers\n"
+ " - show uefi drivers\n"
+ "efidebug dh\n"
+ " - show uefi handles\n"
+ "efidebug images\n"
+ " - show loaded images\n"
+ "efidebug memmap\n"
+ " - show uefi memory map\n";
+#endif
+
+U_BOOT_CMD(
+ efidebug, 10, 0, do_efidebug,
+ "Configure UEFI environment",
+ efidebug_help_text
+);
diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index ebaa16b754..24a6cf7824 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -119,6 +119,11 @@ static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
int rcode = 0;
int env_flag = H_HIDE_DOT;
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+ if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
+ return do_env_print_efi(cmdtp, flag, --argc, ++argv);
+#endif
+
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
argc--;
argv++;
@@ -216,6 +221,12 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
ENTRY e, *ep;
debug("Initial value for argc=%d\n", argc);
+
+#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
+ if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
+ return do_env_set_efi(NULL, flag, --argc, ++argv);
+#endif
+
while (argc > 1 && **(argv + 1) == '-') {
char *arg = *++argv;
@@ -1263,12 +1274,18 @@ static char env_help_text[] =
"env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
#endif
"env print [-a | name ...] - print environment\n"
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+ "env print -e [name ...] - print UEFI environment\n"
+#endif
#if defined(CONFIG_CMD_RUN)
"env run var [...] - run commands in an environment variable\n"
#endif
#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
"env save - save environment\n"
#endif
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+ "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
+#endif
"env set [-f] name [arg ...]\n";
#endif
@@ -1295,6 +1312,10 @@ U_BOOT_CMD_COMPLETE(
printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
"print environment variables",
"[-a]\n - print [all] values of all environment variables\n"
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+ "printenv -e [name ...]\n"
+ " - print UEFI variable 'name' or all the variables\n"
+#endif
"printenv name ...\n"
" - print value of environment variable 'name'",
var_complete
@@ -1322,7 +1343,12 @@ U_BOOT_CMD_COMPLETE(
U_BOOT_CMD_COMPLETE(
setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
"set environment variables",
- "[-f] name value ...\n"
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+ "-e name [value ...]\n"
+ " - set UEFI variable 'name' to 'value' ...'\n"
+ " - delete UEFI variable 'name' if 'value' not specified\n"
+#endif
+ "setenv [-f] name value ...\n"
" - [forcibly] set environment variable 'name' to 'value ...'\n"
"setenv [-f] name\n"
" - [forcibly] delete environment variable 'name'",
diff --git a/cmd/nvedit_efi.c b/cmd/nvedit_efi.c
new file mode 100644
index 0000000000..ca32566a61
--- /dev/null
+++ b/cmd/nvedit_efi.c
@@ -0,0 +1,399 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Integrate UEFI variables to u-boot env interface
+ *
+ * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
+ */
+
+#include <charset.h>
+#include <common.h>
+#include <command.h>
+#include <efi_loader.h>
+#include <exports.h>
+#include <hexdump.h>
+#include <malloc.h>
+#include <linux/kernel.h>
+
+/*
+ * From efi_variable.c,
+ *
+ * Mapping between UEFI variables and u-boot variables:
+ *
+ * efi_$guid_$varname = {attributes}(type)value
+ */
+
+static const struct {
+ u32 mask;
+ char *text;
+} efi_var_attrs[] = {
+ {EFI_VARIABLE_NON_VOLATILE, "NV"},
+ {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
+ {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
+ {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
+ {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
+};
+
+/**
+ * efi_dump_single_var() - show information about a UEFI variable
+ *
+ * @name: Name of the variable
+ * @guid: Vendor GUID
+ *
+ * Show information encoded in one UEFI variable
+ */
+static void efi_dump_single_var(u16 *name, efi_guid_t *guid)
+{
+ u32 attributes;
+ u8 *data;
+ efi_uintn_t size;
+ int count, i;
+ efi_status_t ret;
+
+ data = NULL;
+ size = 0;
+ ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
+ if (ret == EFI_BUFFER_TOO_SMALL) {
+ data = malloc(size);
+ if (!data)
+ goto out;
+
+ ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
+ data));
+ }
+ if (ret == EFI_NOT_FOUND) {
+ printf("Error: \"%ls\" not defined\n", name);
+ goto out;
+ }
+ if (ret != EFI_SUCCESS)
+ goto out;
+
+ printf("%ls:", name);
+ for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
+ if (attributes & efi_var_attrs[i].mask) {
+ if (count)
+ putc('|');
+ else
+ putc(' ');
+ count++;
+ puts(efi_var_attrs[i].text);
+ }
+ printf(", DataSize = 0x%zx\n", size);
+ print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
+
+ return;
+out:
+ free(data);
+}
+
+/**
+ * efi_dump_vars() - show information about named UEFI variables
+ *
+ * @argc: Number of arguments (variables)
+ * @argv: Argument (variable name) array
+ * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
+ *
+ * Show information encoded in named UEFI variables
+ */
+static int efi_dump_vars(int argc, char * const argv[])
+{
+ u16 *var_name16, *p;
+ efi_uintn_t buf_size, size;
+
+ buf_size = 128;
+ var_name16 = malloc(buf_size);
+ if (!var_name16)
+ return CMD_RET_FAILURE;
+
+ for (; argc > 0; argc--, argv++) {
+ size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
+ if (buf_size < size) {
+ buf_size = size;
+ p = realloc(var_name16, buf_size);
+ if (!p) {
+ free(var_name16);
+ return CMD_RET_FAILURE;
+ }
+ var_name16 = p;
+ }
+
+ p = var_name16;
+ utf8_utf16_strcpy(&p, argv[0]);
+
+ efi_dump_single_var(var_name16,
+ (efi_guid_t *)&efi_global_variable_guid);
+ }
+
+ free(var_name16);
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * efi_dump_vars() - show information about all the UEFI variables
+ *
+ * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
+ *
+ * Show information encoded in all the UEFI variables
+ */
+static int efi_dump_var_all(void)
+{
+ u16 *var_name16, *p;
+ efi_uintn_t buf_size, size;
+ efi_guid_t guid;
+ efi_status_t ret;
+
+ buf_size = 128;
+ var_name16 = malloc(buf_size);
+ if (!var_name16)
+ return CMD_RET_FAILURE;
+
+ var_name16[0] = 0;
+ for (;;) {
+ size = buf_size;
+ ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
+ &guid));
+ if (ret == EFI_NOT_FOUND)
+ break;
+ if (ret == EFI_BUFFER_TOO_SMALL) {
+ buf_size = size;
+ p = realloc(var_name16, buf_size);
+ if (!p) {
+ free(var_name16);
+ return CMD_RET_FAILURE;
+ }
+ var_name16 = p;
+ ret = EFI_CALL(efi_get_next_variable_name(&size,
+ var_name16,
+ &guid));
+ }
+ if (ret != EFI_SUCCESS) {
+ free(var_name16);
+ return CMD_RET_FAILURE;
+ }
+
+ efi_dump_single_var(var_name16, &guid);
+ }
+
+ free(var_name16);
+
+ return CMD_RET_SUCCESS;
+}
+
+/**
+ * do_env_print_efi() - show information about UEFI variables
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
+ *
+ * This function is for "env print -e" or "printenv -e" command:
+ * => env print -e [var [...]]
+ * If one or more variable names are specified, show information
+ * named UEFI variables, otherwise show all the UEFI variables.
+ */
+int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ efi_status_t ret;
+
+ /* Initialize EFI drivers */
+ ret = efi_init_obj_list();
+ if (ret != EFI_SUCCESS) {
+ printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
+ ret & ~EFI_ERROR_MASK);
+ return CMD_RET_FAILURE;
+ }
+
+ if (argc > 1)
+ /* show specified UEFI variables */
+ return efi_dump_vars(--argc, ++argv);
+
+ /* enumerate and show all UEFI variables */
+ return efi_dump_var_all();
+}
+
+/**
+ * append_value() - encode UEFI variable's value
+ * @bufp: Buffer of encoded UEFI variable's value
+ * @sizep: Size of buffer
+ * @data: data to be encoded into the value
+ * Return: 0 on success, -1 otherwise
+ *
+ * Interpret a given data string and append it to buffer.
+ * Buffer will be realloc'ed if necessary.
+ *
+ * Currently supported formats are:
+ * =0x0123...: Hexadecimal number
+ * =H0123...: Hexadecimal-byte array
+ * ="...", =S"..." or <string>:
+ * String
+ */
+static int append_value(char **bufp, size_t *sizep, char *data)
+{
+ char *tmp_buf = NULL, *new_buf = NULL, *value;
+ unsigned long len = 0;
+
+ if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
+ union {
+ u8 u8;
+ u16 u16;
+ u32 u32;
+ u64 u64;
+ } tmp_data;
+ unsigned long hex_value;
+ void *hex_ptr;
+
+ data += 3;
+ len = strlen(data);
+ if ((len & 0x1)) /* not multiple of two */
+ return -1;
+
+ len /= 2;
+ if (len > 8)
+ return -1;
+ else if (len > 4)
+ len = 8;
+ else if (len > 2)
+ len = 4;
+
+ /* convert hex hexadecimal number */
+ if (strict_strtoul(data, 16, &hex_value) < 0)
+ return -1;
+
+ tmp_buf = malloc(len);
+ if (!tmp_buf)
+ return -1;
+
+ if (len == 1) {
+ tmp_data.u8 = hex_value;
+ hex_ptr = &tmp_data.u8;
+ } else if (len == 2) {
+ tmp_data.u16 = hex_value;
+ hex_ptr = &tmp_data.u16;
+ } else if (len == 4) {
+ tmp_data.u32 = hex_value;
+ hex_ptr = &tmp_data.u32;
+ } else {
+ tmp_data.u64 = hex_value;
+ hex_ptr = &tmp_data.u64;
+ }
+ memcpy(tmp_buf, hex_ptr, len);
+ value = tmp_buf;
+
+ } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
+ data += 2;
+ len = strlen(data);
+ if (len & 0x1) /* not multiple of two */
+ return -1;
+
+ len /= 2;
+ tmp_buf = malloc(len);
+ if (!tmp_buf)
+ return -1;
+
+ if (hex2bin((u8 *)tmp_buf, data, len) < 0)
+ return -1;
+
+ value = tmp_buf;
+ } else { /* string */
+ if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
+ if (data[1] == '"')
+ data += 2;
+ else
+ data += 3;
+ value = data;
+ len = strlen(data) - 1;
+ if (data[len] != '"')
+ return -1;
+ } else {
+ value = data;
+ len = strlen(data);
+ }
+ }
+
+ new_buf = realloc(*bufp, *sizep + len);
+ if (!new_buf)
+ goto out;
+
+ memcpy(new_buf + *sizep, value, len);
+ *bufp = new_buf;
+ *sizep += len;
+
+out:
+ free(tmp_buf);
+
+ return 0;
+}
+
+/**
+ * do_env_print_efi() - set UEFI variable
+ *
+ * @cmdtp: Command table
+ * @flag: Command flag
+ * @argc: Number of arguments
+ * @argv: Argument array
+ * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
+ *
+ * This function is for "env set -e" or "setenv -e" command:
+ * => env set -e var [value ...]]
+ * Encode values specified and set given UEFI variable.
+ * If no value is specified, delete the variable.
+ */
+int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ char *var_name, *value = NULL;
+ efi_uintn_t size = 0;
+ u16 *var_name16 = NULL, *p;
+ size_t len;
+ efi_guid_t guid;
+ efi_status_t ret;
+
+ if (argc == 1)
+ return CMD_RET_USAGE;
+
+ /* Initialize EFI drivers */
+ ret = efi_init_obj_list();
+ if (ret != EFI_SUCCESS) {
+ printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
+ ret & ~EFI_ERROR_MASK);
+ return CMD_RET_FAILURE;
+ }
+
+ var_name = argv[1];
+ if (argc == 2) {
+ /* delete */
+ value = NULL;
+ size = 0;
+ } else { /* set */
+ argc -= 2;
+ argv += 2;
+
+ for ( ; argc > 0; argc--, argv++)
+ if (append_value(&value, &size, argv[0]) < 0) {
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+ }
+
+ len = utf8_utf16_strnlen(var_name, strlen(var_name));
+ var_name16 = malloc((len + 1) * 2);
+ if (!var_name16) {
+ ret = CMD_RET_FAILURE;
+ goto out;
+ }
+ p = var_name16;
+ utf8_utf16_strncpy(&p, var_name, len + 1);
+
+ guid = efi_global_variable_guid;
+ ret = EFI_CALL(efi_set_variable(var_name16, &guid,
+ EFI_VARIABLE_BOOTSERVICE_ACCESS |
+ EFI_VARIABLE_RUNTIME_ACCESS,
+ size, value));
+ ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+out:
+ free(value);
+ free(var_name16);
+
+ return ret;
+}