[U-Boot] [PATCH v6 1/7] cmd: env: add "-e" option for handling UEFI variables

Alexander Graf agraf at suse.de
Fri Jan 25 12:42:17 UTC 2019



On 24.01.19 12:04, AKASHI Takahiro wrote:
> "env [print|set] -e" allows for handling uefi variables without
> knowing details about mapping to corresponding u-boot variables.
> 
> Signed-off-by: AKASHI Takahiro <takahiro.akashi at linaro.org>
> ---
>  MAINTAINERS       |   1 +
>  cmd/Kconfig       |   6 +
>  cmd/Makefile      |   1 +
>  cmd/nvedit.c      |  28 +++-
>  cmd/nvedit_efi.c  | 319 ++++++++++++++++++++++++++++++++++++++++++++++
>  include/command.h |   4 +
>  6 files changed, 358 insertions(+), 1 deletion(-)
>  create mode 100644 cmd/nvedit_efi.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index ae825014bda9..22ac686ab2d6 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -438,6 +438,7 @@ F:	lib/efi*/
>  F:	test/py/tests/test_efi*
>  F:	test/unicode_ut.c
>  F:	cmd/bootefi.c
> +F:	cmd/nvedit_efi.c
>  F:	tools/file2include.c
>  
>  FPGA
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index ea1a325eb301..812a7eb9b74b 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -53,6 +53,12 @@ config SYS_PROMPT
>  	  This string is displayed in the command line to the left of the
>  	  cursor.
>  
> +config CMD_NVEDIT_EFI
> +	bool
> +	depends on EFI_LOADER
> +	default y if EFI_LOADER

No need for the "if EFI_LOADER" here. You only ever get to run the
default path when EFI_LOADER is set due to the depends on line before.

> +	imply HEXDUMP
> +
>  menu "Autoboot options"
>  
>  config AUTOBOOT
> diff --git a/cmd/Makefile b/cmd/Makefile
> index 15ae4d250f50..142e0ee222ca 100644
> --- a/cmd/Makefile
> +++ b/cmd/Makefile
> @@ -98,6 +98,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/nvedit.c b/cmd/nvedit.c
> index ce746bbf1b3e..7973d7add5fc 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 defined(CONFIG_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,14 +1274,21 @@ 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 | -f] name [arg ...]\n";
> +#else
>  	"env set [-f] name [arg ...]\n";
>  #endif
> +#endif
>  
>  U_BOOT_CMD(
>  	env, CONFIG_SYS_MAXARGS, 1, do_env,
> @@ -1295,6 +1313,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 +1344,11 @@ 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"
> +#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 000000000000..58c92009e8db
> --- /dev/null
> +++ b/cmd/nvedit_efi.c
> @@ -0,0 +1,319 @@
> +// 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>
> +
> +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"},
> +};
> +
> +static int print_variable_info(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_get_variable(name, guid, &attributes, &size, data);
> +	if (ret == EFI_BUFFER_TOO_SMALL) {
> +		data = malloc(size);
> +		if (!data)
> +			return -1;

Just goto out.

> +
> +		ret = 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%lx\n", size);
> +	print_hex_dump("    ", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
> +
> +	return 0;
> +out:
> +	free(data);
> +	return -1;
> +}
> +
> +/*
> + * From efi_variable.c,
> + *
> + * Mapping between UEFI variables and u-boot variables:
> + *
> + *   efi_$guid_$varname = {attributes}(type)value
> + */
> +static int efi_dump_var(cmd_tbl_t *cmdtp, int flag,
> +			int argc, char * const argv[])
> +{
> +	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;
> +
> +	if (argc > 1) {

Missing a comment on what this does. Also, I think it makes sense to
extract the 2 cases (dump 1 var, dump all vars) into separate functions.
They share almost no code and just make indentation hard.

> +		argc--;
> +		argv++;
> +
> +		for (; argc > 0; argc--, argv++) {
> +			size = (strlen(argv[0]) + 1) * 2;

(utf8_utf16_strlen() + 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]);
> +			guid = efi_global_variable_guid;

Why do you need to copy the guid into a stack variable? Can't you just
pass the pointer?

> +
> +			print_variable_info(var_name16, &guid);

Not checking return value?

> +		}
> +
> +		free(var_name16);
> +
> +		return CMD_RET_SUCCESS;
> +	}
> +
> +	var_name16[0] = 0;
> +	for (;;) {
> +		size = buf_size;
> +		ret = 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_get_next_variable_name(&size, var_name16,
> +							 &guid);
> +		}
> +		if (ret != EFI_SUCCESS) {
> +			free(var_name16);
> +			return CMD_RET_FAILURE;
> +		}
> +
> +		print_variable_info(var_name16, &guid);

Not checking return value?


Alex


More information about the U-Boot mailing list