diff mbox series

[U-Boot,v7,2/7] cmd: add efidebug command

Message ID 20190221062652.3641-3-takahiro.akashi@linaro.org
State Superseded
Delegated to: Andes
Headers show
Series cmd: add efidebug for efi environment | expand

Commit Message

AKASHI Takahiro Feb. 21, 2019, 6:26 a.m. UTC
Currently, there is no easy way to add or modify UEFI variables.
In particular, bootmgr supports BootOrder/BootXXXX variables, it is
quite hard to define them as u-boot variables because they are represented
in a complicated and encoded format.

The new command, efidebug, helps address these issues and give us
more friendly interfaces:
 * efidebug boot add: add BootXXXX variable
 * efidebug boot rm: remove BootXXXX variable
 * efidebug boot dump: display all BootXXXX variables
 * efidebug boot next: set BootNext variable
 * efidebug boot order: set/display a boot order (BootOrder)

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
 MAINTAINERS    |   1 +
 cmd/Kconfig    |  10 +
 cmd/Makefile   |   1 +
 cmd/efidebug.c | 578 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 590 insertions(+)
 create mode 100644 cmd/efidebug.c

Comments

Heinrich Schuchardt Feb. 21, 2019, 7:24 p.m. UTC | #1
On 2/21/19 7:26 AM, AKASHI Takahiro wrote:
> Currently, there is no easy way to add or modify UEFI variables.
> In particular, bootmgr supports BootOrder/BootXXXX variables, it is
> quite hard to define them as u-boot variables because they are represented
> in a complicated and encoded format.
> 
> The new command, efidebug, helps address these issues and give us
> more friendly interfaces:
>  * efidebug boot add: add BootXXXX variable
>  * efidebug boot rm: remove BootXXXX variable
>  * efidebug boot dump: display all BootXXXX variables
>  * efidebug boot next: set BootNext variable
>  * efidebug boot order: set/display a boot order (BootOrder)
> 
> Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> ---
>  MAINTAINERS    |   1 +
>  cmd/Kconfig    |  10 +
>  cmd/Makefile   |   1 +
>  cmd/efidebug.c | 578 +++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 590 insertions(+)
>  create mode 100644 cmd/efidebug.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 0cce9db2660e..4fabb75eda37 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -471,6 +471,7 @@ F:	lib/efi*/
>  F:	test/py/tests/test_efi*
>  F:	test/unicode_ut.c
>  F:	cmd/bootefi.c
> +F:	cmd/efidebug.c
>  F:	cmd/nvedit_efi.c
>  F:	tools/file2include.c
>  
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index ddcdee44538d..88b086b074be 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -1407,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"
>  	default y if LED
> diff --git a/cmd/Makefile b/cmd/Makefile
> index b9ee51869d48..acb85f49fba8 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
> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> new file mode 100644
> index 000000000000..641ec98a4d32
> --- /dev/null
> +++ b/cmd/efidebug.c
> @@ -0,0 +1,578 @@
> +// 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>
> +
> +/**
> + * do_efi_boot_add() - set UEFI boot 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 boot 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 = 0x1; /* always ACTIVE */

I would prefer the usage of a constant.

We should move the definitions from lib/efi_loader/efi_bootmgr.c to
efi_api.h:

#define LOAD_OPTION_ACTIVE		0x00000001
#define LOAD_OPTION_FORCE_RECONNECT	0x00000002
#define LOAD_OPTION_HIDDEN		0x00000008

And add the missing values:

#define LOAD_OPTION_CATEGORY		0x00001F00
#define LOAD_OPTION_CATEGORY_BOOT	0x00000000
#define LOAD_OPTION_CATEGORY_APP	0x00000100

> +
> +	/* 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_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 boot 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 boot 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_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 boot option
> + *
> + * @id:		Boot opton number
> + * @data:	Value of UEFI boot option variable
> + *
> + * Decode the value of UEFI boot 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 & 0x1 ? 'A' : '-',

Please, use constants here.

> +	       /* FORCE RECONNECT */
> +	       lo.attributes & 0x2 ? 'R' : '-',
> +	       /* HIDDEN */
> +	       lo.attributes & 0x8 ? 'H' : '-',
> +	       lo.attributes);

How about LOAD_OPTION_CATEGORY_APP?
It is not supported by us yet. So it is not strictly necessary now.

Otherwise

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>


> +	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 boot option
> + *
> + * @id:		Boot opton number
> + *
> + * Dump information defined by UEFI boot 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_get_variable(var_name16, &guid, NULL, &size, NULL);
> +	if (ret == (int)EFI_BUFFER_TOO_SMALL) {
> +		data = malloc(size);
> +		ret = efi_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 boot 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 boot 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 boot options
> + *
> + * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
> + *
> + * Show order of UEFI boot 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_get_variable(L"BootOrder", &guid, NULL, &size, NULL);
> +	if (ret == EFI_BUFFER_TOO_SMALL) {
> +		bootorder = malloc(size);
> +		ret = efi_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_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_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_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 boot 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_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 boot 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, "", ""),
> +};
> +
> +/**
> + * 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 dsiplay 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";
> +#endif
> +
> +U_BOOT_CMD(
> +	efidebug, 10, 0, do_efidebug,
> +	"Configure UEFI environment",
> +	efidebug_help_text
> +);
>
AKASHI Takahiro Feb. 22, 2019, 1:04 a.m. UTC | #2
On Thu, Feb 21, 2019 at 08:24:21PM +0100, Heinrich Schuchardt wrote:
> On 2/21/19 7:26 AM, AKASHI Takahiro wrote:
> > Currently, there is no easy way to add or modify UEFI variables.
> > In particular, bootmgr supports BootOrder/BootXXXX variables, it is
> > quite hard to define them as u-boot variables because they are represented
> > in a complicated and encoded format.
> > 
> > The new command, efidebug, helps address these issues and give us
> > more friendly interfaces:
> >  * efidebug boot add: add BootXXXX variable
> >  * efidebug boot rm: remove BootXXXX variable
> >  * efidebug boot dump: display all BootXXXX variables
> >  * efidebug boot next: set BootNext variable
> >  * efidebug boot order: set/display a boot order (BootOrder)
> > 
> > Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> > ---
> >  MAINTAINERS    |   1 +
> >  cmd/Kconfig    |  10 +
> >  cmd/Makefile   |   1 +
> >  cmd/efidebug.c | 578 +++++++++++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 590 insertions(+)
> >  create mode 100644 cmd/efidebug.c
> > 
> > diff --git a/MAINTAINERS b/MAINTAINERS
> > index 0cce9db2660e..4fabb75eda37 100644
> > --- a/MAINTAINERS
> > +++ b/MAINTAINERS
> > @@ -471,6 +471,7 @@ F:	lib/efi*/
> >  F:	test/py/tests/test_efi*
> >  F:	test/unicode_ut.c
> >  F:	cmd/bootefi.c
> > +F:	cmd/efidebug.c
> >  F:	cmd/nvedit_efi.c
> >  F:	tools/file2include.c
> >  
> > diff --git a/cmd/Kconfig b/cmd/Kconfig
> > index ddcdee44538d..88b086b074be 100644
> > --- a/cmd/Kconfig
> > +++ b/cmd/Kconfig
> > @@ -1407,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"
> >  	default y if LED
> > diff --git a/cmd/Makefile b/cmd/Makefile
> > index b9ee51869d48..acb85f49fba8 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
> > diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> > new file mode 100644
> > index 000000000000..641ec98a4d32
> > --- /dev/null
> > +++ b/cmd/efidebug.c
> > @@ -0,0 +1,578 @@
> > +// 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>
> > +
> > +/**
> > + * do_efi_boot_add() - set UEFI boot 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 boot 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 = 0x1; /* always ACTIVE */
> 
> I would prefer the usage of a constant.
> 
> We should move the definitions from lib/efi_loader/efi_bootmgr.c to
> efi_api.h:

Okay, but in efi_loader.h as struct efi_load_option is defined there.

> #define LOAD_OPTION_ACTIVE		0x00000001
> #define LOAD_OPTION_FORCE_RECONNECT	0x00000002
> #define LOAD_OPTION_HIDDEN		0x00000008
> 
> And add the missing values:
> 
> #define LOAD_OPTION_CATEGORY		0x00001F00
> #define LOAD_OPTION_CATEGORY_BOOT	0x00000000
> #define LOAD_OPTION_CATEGORY_APP	0x00000100
> 
> > +
> > +	/* 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_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 boot 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 boot 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_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 boot option
> > + *
> > + * @id:		Boot opton number
> > + * @data:	Value of UEFI boot option variable
> > + *
> > + * Decode the value of UEFI boot 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 & 0x1 ? 'A' : '-',
> 
> Please, use constants here.

Okay.

> > +	       /* FORCE RECONNECT */
> > +	       lo.attributes & 0x2 ? 'R' : '-',
> > +	       /* HIDDEN */
> > +	       lo.attributes & 0x8 ? 'H' : '-',
> > +	       lo.attributes);
> 
> How about LOAD_OPTION_CATEGORY_APP?
> It is not supported by us yet. So it is not strictly necessary now.

Next time when "boot menu" or "hot key" will be supported.

Thanks,
-Takahiro Akashi

> Otherwise
> 
> Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> 
> 
> > +	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 boot option
> > + *
> > + * @id:		Boot opton number
> > + *
> > + * Dump information defined by UEFI boot 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_get_variable(var_name16, &guid, NULL, &size, NULL);
> > +	if (ret == (int)EFI_BUFFER_TOO_SMALL) {
> > +		data = malloc(size);
> > +		ret = efi_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 boot 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 boot 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 boot options
> > + *
> > + * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
> > + *
> > + * Show order of UEFI boot 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_get_variable(L"BootOrder", &guid, NULL, &size, NULL);
> > +	if (ret == EFI_BUFFER_TOO_SMALL) {
> > +		bootorder = malloc(size);
> > +		ret = efi_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_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_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_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 boot 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_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 boot 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, "", ""),
> > +};
> > +
> > +/**
> > + * 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 dsiplay 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";
> > +#endif
> > +
> > +U_BOOT_CMD(
> > +	efidebug, 10, 0, do_efidebug,
> > +	"Configure UEFI environment",
> > +	efidebug_help_text
> > +);
> > 
>
Heinrich Schuchardt Feb. 22, 2019, 2:43 a.m. UTC | #3
On 2/22/19 2:04 AM, AKASHI Takahiro wrote:
> On Thu, Feb 21, 2019 at 08:24:21PM +0100, Heinrich Schuchardt wrote:
>> On 2/21/19 7:26 AM, AKASHI Takahiro wrote:
>>> Currently, there is no easy way to add or modify UEFI variables.
>>> In particular, bootmgr supports BootOrder/BootXXXX variables, it is
>>> quite hard to define them as u-boot variables because they are represented
>>> in a complicated and encoded format.
>>>
>>> The new command, efidebug, helps address these issues and give us
>>> more friendly interfaces:
>>>  * efidebug boot add: add BootXXXX variable
>>>  * efidebug boot rm: remove BootXXXX variable
>>>  * efidebug boot dump: display all BootXXXX variables
>>>  * efidebug boot next: set BootNext variable
>>>  * efidebug boot order: set/display a boot order (BootOrder)
>>>
>>> Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
>>> ---
>>>  MAINTAINERS    |   1 +
>>>  cmd/Kconfig    |  10 +
>>>  cmd/Makefile   |   1 +
>>>  cmd/efidebug.c | 578 +++++++++++++++++++++++++++++++++++++++++++++++++
>>>  4 files changed, 590 insertions(+)
>>>  create mode 100644 cmd/efidebug.c
>>>
>>> diff --git a/MAINTAINERS b/MAINTAINERS
>>> index 0cce9db2660e..4fabb75eda37 100644
>>> --- a/MAINTAINERS
>>> +++ b/MAINTAINERS
>>> @@ -471,6 +471,7 @@ F:	lib/efi*/
>>>  F:	test/py/tests/test_efi*
>>>  F:	test/unicode_ut.c
>>>  F:	cmd/bootefi.c
>>> +F:	cmd/efidebug.c
>>>  F:	cmd/nvedit_efi.c
>>>  F:	tools/file2include.c
>>>  
>>> diff --git a/cmd/Kconfig b/cmd/Kconfig
>>> index ddcdee44538d..88b086b074be 100644
>>> --- a/cmd/Kconfig
>>> +++ b/cmd/Kconfig
>>> @@ -1407,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"
>>>  	default y if LED
>>> diff --git a/cmd/Makefile b/cmd/Makefile
>>> index b9ee51869d48..acb85f49fba8 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
>>> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
>>> new file mode 100644
>>> index 000000000000..641ec98a4d32
>>> --- /dev/null
>>> +++ b/cmd/efidebug.c
>>> @@ -0,0 +1,578 @@
>>> +// 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>
>>> +
>>> +/**
>>> + * do_efi_boot_add() - set UEFI boot 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 boot 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 = 0x1; /* always ACTIVE */
>>
>> I would prefer the usage of a constant.
>>
>> We should move the definitions from lib/efi_loader/efi_bootmgr.c to
>> efi_api.h:
> 
> Okay, but in efi_loader.h as struct efi_load_option is defined there.

struct efi_load_option is nothing that is defined in the UEFI spec but a
U-Boot specific structure (it is not the same as EFI_LOAD_OPTION).

The constants are defined in the UEFI spec.

To my understanding we are using efi_loader only for U-Boot specific
stuff up to now.

Best regards

Heinrich

> 
>> #define LOAD_OPTION_ACTIVE		0x00000001
>> #define LOAD_OPTION_FORCE_RECONNECT	0x00000002
>> #define LOAD_OPTION_HIDDEN		0x00000008
>>
>> And add the missing values:
>>
>> #define LOAD_OPTION_CATEGORY		0x00001F00
>> #define LOAD_OPTION_CATEGORY_BOOT	0x00000000
>> #define LOAD_OPTION_CATEGORY_APP	0x00000100
>>
>>> +
>>> +	/* 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_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 boot 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 boot 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_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 boot option
>>> + *
>>> + * @id:		Boot opton number
>>> + * @data:	Value of UEFI boot option variable
>>> + *
>>> + * Decode the value of UEFI boot 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 & 0x1 ? 'A' : '-',
>>
>> Please, use constants here.
> 
> Okay.
> 
>>> +	       /* FORCE RECONNECT */
>>> +	       lo.attributes & 0x2 ? 'R' : '-',
>>> +	       /* HIDDEN */
>>> +	       lo.attributes & 0x8 ? 'H' : '-',
>>> +	       lo.attributes);
>>
>> How about LOAD_OPTION_CATEGORY_APP?
>> It is not supported by us yet. So it is not strictly necessary now.
> 
> Next time when "boot menu" or "hot key" will be supported.
> 
> Thanks,
> -Takahiro Akashi
> 
>> Otherwise
>>
>> Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
>>
>>
>>> +	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 boot option
>>> + *
>>> + * @id:		Boot opton number
>>> + *
>>> + * Dump information defined by UEFI boot 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_get_variable(var_name16, &guid, NULL, &size, NULL);
>>> +	if (ret == (int)EFI_BUFFER_TOO_SMALL) {
>>> +		data = malloc(size);
>>> +		ret = efi_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 boot 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 boot 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 boot options
>>> + *
>>> + * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
>>> + *
>>> + * Show order of UEFI boot 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_get_variable(L"BootOrder", &guid, NULL, &size, NULL);
>>> +	if (ret == EFI_BUFFER_TOO_SMALL) {
>>> +		bootorder = malloc(size);
>>> +		ret = efi_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_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_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_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 boot 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_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 boot 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, "", ""),
>>> +};
>>> +
>>> +/**
>>> + * 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 dsiplay 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";
>>> +#endif
>>> +
>>> +U_BOOT_CMD(
>>> +	efidebug, 10, 0, do_efidebug,
>>> +	"Configure UEFI environment",
>>> +	efidebug_help_text
>>> +);
>>>
>>
>
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 0cce9db2660e..4fabb75eda37 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -471,6 +471,7 @@  F:	lib/efi*/
 F:	test/py/tests/test_efi*
 F:	test/unicode_ut.c
 F:	cmd/bootefi.c
+F:	cmd/efidebug.c
 F:	cmd/nvedit_efi.c
 F:	tools/file2include.c
 
diff --git a/cmd/Kconfig b/cmd/Kconfig
index ddcdee44538d..88b086b074be 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1407,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"
 	default y if LED
diff --git a/cmd/Makefile b/cmd/Makefile
index b9ee51869d48..acb85f49fba8 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
diff --git a/cmd/efidebug.c b/cmd/efidebug.c
new file mode 100644
index 000000000000..641ec98a4d32
--- /dev/null
+++ b/cmd/efidebug.c
@@ -0,0 +1,578 @@ 
+// 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>
+
+/**
+ * do_efi_boot_add() - set UEFI boot 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 boot 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 = 0x1; /* 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_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 boot 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 boot 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_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 boot option
+ *
+ * @id:		Boot opton number
+ * @data:	Value of UEFI boot option variable
+ *
+ * Decode the value of UEFI boot 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 & 0x1 ? 'A' : '-',
+	       /* FORCE RECONNECT */
+	       lo.attributes & 0x2 ? 'R' : '-',
+	       /* HIDDEN */
+	       lo.attributes & 0x8 ? '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 boot option
+ *
+ * @id:		Boot opton number
+ *
+ * Dump information defined by UEFI boot 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_get_variable(var_name16, &guid, NULL, &size, NULL);
+	if (ret == (int)EFI_BUFFER_TOO_SMALL) {
+		data = malloc(size);
+		ret = efi_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 boot 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 boot 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 boot options
+ *
+ * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Show order of UEFI boot 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_get_variable(L"BootOrder", &guid, NULL, &size, NULL);
+	if (ret == EFI_BUFFER_TOO_SMALL) {
+		bootorder = malloc(size);
+		ret = efi_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_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_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_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 boot 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_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 boot 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, "", ""),
+};
+
+/**
+ * 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 dsiplay 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";
+#endif
+
+U_BOOT_CMD(
+	efidebug, 10, 0, do_efidebug,
+	"Configure UEFI environment",
+	efidebug_help_text
+);