Skip to contentSkip to conversation

cmd: env: add "-e" option for handling UEFI variables

PATCH New AKASHI Takahiro 2 participants 3 messages Feb 1, 2019 – Feb 20, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: env: add "-e" option for handling UEFI variables

# This is a replacement of my patch#1[1] with minor updates to fix

# travis build errors. The other patches are the exact same as in v6.
# [1] https://lists.denx.de/pipermail/u-boot/2019-January/356035.html

"env [print|set] -e" allows for handling uefi variables without knowing details about mapping to corresponding u-boot variables.

---
 MAINTAINERS       |   1 +
 cmd/Kconfig       |   6 +
 cmd/Makefile      |   1 +
 cmd/nvedit.c      |  28 +++-
 cmd/nvedit_efi.c  | 340 ++++++++++++++++++++++++++++++++++++++++++++++
 include/command.h |   4 +
 6 files changed, 379 insertions(+), 1 deletion(-)
 create mode 100644 cmd/nvedit_efi.c
MAINTAINERS +1 -0
index ae825014bda9..22ac686ab2d6 100644
@@ -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
cmd/Kconfig +6 -0
index ea1a325eb301..c66333598d31 100644
@@ -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
+	imply HEXDUMP
+
 menu "Autoboot options"
 
 config AUTOBOOT
cmd/Makefile +1 -0
index 15ae4d250f50..142e0ee222ca 100644
@@ -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
cmd/nvedit.c +27 -1
index ce746bbf1b3e..07285b877f40 100644
@@ -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_SPL_BUILD) && 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'",
cmd/nvedit_efi.c new file +340 -0

340 lines not on this page · raw message

include/command.h +4 -0
index feddef300ccc..efc4dfc457c1 100644
@@ -51,6 +51,10 @@ extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 #if defined(CONFIG_CMD_BOOTEFI)
 extern int do_bootefi_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 #endif
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+#endif
 
 /* common/command.c */
 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
Signature · 1 line
2.19.1
#1 lore raw

PATCH 1/1 cmd: env: add "-e" option for handling UEFI variables

Quoted 8 lines · AKASHI Takahiro

# This is a replacement of my patch#1[1] with minor updates to fix

# travis build errors. The other patches are the exact same as in v6.
# [1] https://lists.denx.de/pipermail/u-boot/2019-January/356035.html

"env [print|set] -e" allows for handling uefi variables without knowing details about mapping to corresponding u-boot variables.

Thanks for the patch. As long as we use U-Boot variables as backend for EFI variables this additional functionality is very helpful.

How about adding a Python test (in a separate patch)?

When resending, please, send the complete series marked as v7 or whatever is next.

Please, provide a change log separated via
---
from the commit message (so that it is not copied into the repository).

Quoted 27 lines
---
 MAINTAINERS       |   1 +
 cmd/Kconfig       |   6 +
 cmd/Makefile      |   1 +
 cmd/nvedit.c      |  28 +++-
 cmd/nvedit_efi.c  | 340 ++++++++++++++++++++++++++++++++++++++++++++++
 include/command.h |   4 +
 6 files changed, 379 insertions(+), 1 deletion(-)
 create mode 100644 cmd/nvedit_efi.c
MAINTAINERS +1 -0
index ae825014bda9..22ac686ab2d6 100644
@@ -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
cmd/Kconfig +0 -0
index ea1a325eb301..c66333598d31 100644

cmd/Kconfig config SYS_PROMPT · line 61

Quoted 9 lines
cmd/Kconfig +6 -0
@@ -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
+	imply HEXDUMP
+

Please, add a descriptive text so that this configuration item become editable with 'make menuconfig'.

Quoted 45 lines
cmd/Kconfig +0 -0
 menu "Autoboot options"
 
 config AUTOBOOT
cmd/Makefile +1 -0
index 15ae4d250f50..142e0ee222ca 100644
@@ -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
cmd/nvedit.c +11 -0
index ce746bbf1b3e..07285b877f40 100644
@@ -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_SPL_BUILD) && 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;
 

cmd/nvedit.c static char env_help_text[] = · line 1278

Quoted 5 lines
cmd/nvedit.c +2 -0
@@ -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"

There is a duplicate space before '-e'.

cmd/nvedit.c static char env_help_text[] = · line 1287

Quoted 9 lines
cmd/nvedit.c +3 -0
+#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";

=> help env set
env - environment handling commands

Usage:
<snip>
env set [-e | -f] name [arg ...]

This leaves the user without a clue what env set actually does and what -e is meant for.

Please, add a bit more text.

Wouldn't separate lines for 'set' with and without '-e' be more consistent with what you did for 'env print'?

cmd/nvedit.c static char env_help_text[] = · line 1289

Quoted 2 lines
cmd/nvedit.c +1 -0
+#else
 	"env set [-f] name [arg ...]\n";

Here too.

Quoted 61 lines
cmd/nvedit.c +10 -1
 #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'",
cmd/nvedit_efi.c new file +26 -0
new file mode 100644
index 000000000000..08a7889139e6
@@ -0,0 +1,340 @@
+// 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;

cmd/nvedit_efi.c line 36

Quoted 10 lines
cmd/nvedit_efi.c new file +10 -0
+	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"},
+};
+
+/* show information of named variable */

The comments for a function should look like shown below. This allows to use Sphinx to generate a html documentation page. I prefer to add descriptions to all functions.

/**
* print_variable_info() - show information about a variable *
* @name: name of the variable
* @guid: vendor GUID
*/

Quoted 34 lines
cmd/nvedit_efi.c new file +34 -0
+static void 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)
+			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);

cmd/nvedit_efi.c line 80

Quoted 10 lines
cmd/nvedit_efi.c new file +10 -0
+		}
+	printf(", DataSize = 0x%zx\n", size);
+	print_hex_dump("    ", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
+
+	return;
+out:
+	free(data);
+}
+
+static int efi_dump_var_ones(int argc,  char * const argv[])

The naming of this function sounds strange. How about efi_dump_single_var() or just efi_dump_var()?

Quoted 95 lines
cmd/nvedit_efi.c new file +95 -0
+{
+	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]);
+
+		print_variable_info(var_name16,
+				    (efi_guid_t *)&efi_global_variable_guid);
+	}
+
+	free(var_name16);
+
+	return CMD_RET_SUCCESS;
+}
+
+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_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);
+	}
+
+	free(var_name16);
+
+	return CMD_RET_SUCCESS;
+}
+
+static int efi_dump_var(cmd_tbl_t *cmdtp, int flag,
+			int argc, char * const argv[])
+{
+	if (argc > 1)
+		/* show specified UEFI variables */
+		return efi_dump_var_ones(--argc, ++argv);
+
+	/* enumerate and show all UEFI variables */
+	return efi_dump_var_all();
+}
+
+int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	efi_status_t r;
+
+	/* Initialize EFI drivers */
+	r = efi_init_obj_list();
+	if (r != EFI_SUCCESS) {
+		printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
+		       r & ~EFI_ERROR_MASK);

cmd/nvedit_efi.c line 185

Quoted 10 lines
cmd/nvedit_efi.c new file +10 -0
+		return CMD_RET_FAILURE;
+	}
+
+	return efi_dump_var(cmdtp, flag, argc, argv);
+}
+
+/*
+ * Interpret a given data string and append it to buffer.
+ * Buffer will be realloc'ed if necessary.
+ */

Please, use Sphinx style comment:

/**
* append_value() - append data string to buffer *
* Interpret a given data string and append it to buffer.
* Buffer will be realloc'ed if necessary.
*
* @bufp: pointer to pointer to buffer
* @sizep: pointer to size of buffer
* (updated if buffer is reallocated)
* @data: data to be interpreted and appended
* Return: 0 on success
*/

Quoted 116 lines
cmd/nvedit_efi.c new file +116 -0
+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;
+}
+
+static int efi_set_var(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;
+	efi_guid_t guid;
+	efi_status_t ret;
+
+	if (argc == 1)
+		return CMD_RET_SUCCESS;
+
+	var_name = argv[1];
+	if (argc == 2) {
+		/* remove */
+		value = NULL;
+		size = 0;
+	} else { /* set */
+		argc -= 2;

cmd/nvedit_efi.c line 311

Quoted 10 lines
cmd/nvedit_efi.c new file +10 -0
+		argv += 2;
+
+		for ( ; argc > 0; argc--, argv++)
+			if (append_value(&value, &size, argv[0]) < 0) {
+				ret = CMD_RET_FAILURE;
+				goto out;
+			}
+	}
+
+	var_name16 = malloc((strlen(var_name) + 1) * 2);

Please, always check the return value of malloc().

strlen() is the wrong function here. Use utf8_utf16_strlen() to match the copy function below.

cmd/nvedit_efi.c line 313

Quoted 2 lines
cmd/nvedit_efi.c new file +2 -0
+	p = var_name16;
+	utf8_utf16_strncpy(&p, var_name, strlen(var_name) + 1);

Don't you trust your malloc() call?
Wouldn't you use utf8_utf16_strcpy()?

Best regards

Heinrich

Quoted 43 lines
cmd/nvedit_efi.c new file +27 -0
+
+	guid = efi_global_variable_guid;
+	ret = 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;
+}
+
+int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	efi_status_t r;
+
+	/* Initialize EFI 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;
+	}
+
+	return efi_set_var(cmdtp, flag, argc, argv);
+}
include/command.h +4 -0
index feddef300ccc..efc4dfc457c1 100644
@@ -51,6 +51,10 @@ extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 #if defined(CONFIG_CMD_BOOTEFI)
 extern int do_bootefi_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 #endif
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+#endif
 
 /* common/command.c */
 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
#2 lore raw

PATCH 1/1 cmd: env: add "-e" option for handling UEFI variables

Quoted 8 lines · Heinrich Schuchardt
Quoted 7 lines · AKASHI Takahiro

# This is a replacement of my patch#1[1] with minor updates to fix

# travis build errors. The other patches are the exact same as in v6.
# [1] https://lists.denx.de/pipermail/u-boot/2019-January/356035.html

"env [print|set] -e" allows for handling uefi variables without knowing details about mapping to corresponding u-boot variables.

Quoted 6 lines
Quoted 1 line

Thanks for the patch. As long as we use U-Boot variables as backend for EFI variables this additional functionality is very helpful.

How about adding a Python test (in a separate patch)?

Yes, I will when I have time.

Quoted 2 lines

When resending, please, send the complete series marked as v7 or whatever is next.

The reason why I didn't do so is that I haven't had any reviews on the rest of the patches. In addition, patch#1 can be applied on its own since v6.

Quoted 3 lines

Please, provide a change log separated via
---
from the commit message (so that it is not copied into the repository).

Normally if I want to add a change log, I will create a cover letter even there is only one patch file.

Quoted 31 lines
Quoted 31 lines
---
 MAINTAINERS       |   1 +
 cmd/Kconfig       |   6 +
 cmd/Makefile      |   1 +
 cmd/nvedit.c      |  28 +++-
 cmd/nvedit_efi.c  | 340 ++++++++++++++++++++++++++++++++++++++++++++++
 include/command.h |   4 +
 6 files changed, 379 insertions(+), 1 deletion(-)
 create mode 100644 cmd/nvedit_efi.c
MAINTAINERS +1 -0
index ae825014bda9..22ac686ab2d6 100644
@@ -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
cmd/Kconfig +0 -0
index ea1a325eb301..c66333598d31 100644
@@ -53,6 +53,12 @@ config SYS_PROMPT
 	  This string is displayed in the command line to the left of the
 	  cursor.
 
Quoted 9 lines

cmd/Kconfig config SYS_PROMPT · line 61

Quoted 6 lines
cmd/Kconfig +6 -0
+config CMD_NVEDIT_EFI
+	bool
+	depends on EFI_LOADER
+	default y
+	imply HEXDUMP
+

Please, add a descriptive text so that this configuration item become editable with 'make menuconfig'.

I don't really think it is necessary, but I will do so.

Quoted 44 lines
Quoted 44 lines
cmd/Kconfig +0 -0
 menu "Autoboot options"
 
 config AUTOBOOT
cmd/Makefile +1 -0
index 15ae4d250f50..142e0ee222ca 100644
@@ -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
cmd/nvedit.c +11 -0
index ce746bbf1b3e..07285b877f40 100644
@@ -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_SPL_BUILD) && 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;
 
Quoted 8 lines

cmd/nvedit.c static char env_help_text[] = · line 1278

Quoted 6 lines
cmd/nvedit.c +2 -0
@@ -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"

There is a duplicate space before '-e'.

Fill fix.

Quoted 13 lines

cmd/nvedit.c static char env_help_text[] = · line 1287

Quoted 9 lines
cmd/nvedit.c +3 -0
+#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";

=> help env set
env - environment handling commands

Quoted 6 lines

Usage:
<snip>
env set [-e | -f] name [arg ...]

This leaves the user without a clue what env set actually does and what -e is meant for.

Well, it is also the case for [-f].

Quoted 7 lines

Please, add a bit more text.

Wouldn't separate lines for 'set' with and without '-e' be more consistent with what you did for 'env print'?

Quoted 2 lines
cmd/nvedit.c +1 -0
+#else
 	"env set [-f] name [arg ...]\n";

So I will add a separate help line for clarification:
"env set -e name [arg ...] - set UEFI variable\n"

Quoted 69 lines

Here too.

Quoted 67 lines
cmd/nvedit.c +10 -1
 #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'",
cmd/nvedit_efi.c new file +32 -0
new file mode 100644
index 000000000000..08a7889139e6
@@ -0,0 +1,340 @@
+// 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"},
Quoted 8 lines

cmd/nvedit_efi.c line 36

Quoted 4 lines
cmd/nvedit_efi.c new file +4 -0
+	{EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
+};
+
+/* show information of named variable */

The comments for a function should look like shown below. This allows to use Sphinx to generate a html documentation page. I prefer to add descriptions to all functions.

Okay, but really "all?"

Quoted 6 lines

/**
* print_variable_info() - show information about a variable *
* @name: name of the variable
* @guid: vendor GUID
*/

I'm really confused with the format of description. There are several different styles of syntax seen across the tree. See my comment below.

Quoted 39 lines
Quoted 39 lines
cmd/nvedit_efi.c new file +39 -0
+static void 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)
+			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%zx\n", size);
+	print_hex_dump("    ", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
+
+	return;
Quoted 8 lines

cmd/nvedit_efi.c line 80

Quoted 5 lines
cmd/nvedit_efi.c new file +5 -0
+out:
+	free(data);
+}
+
+static int efi_dump_var_ones(int argc,  char * const argv[])

The naming of this function sounds strange. How about efi_dump_single_var() or just efi_dump_var()?

Okay.

Quoted 108 lines
Quoted 95 lines
cmd/nvedit_efi.c new file +95 -0
+{
+	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]);
+
+		print_variable_info(var_name16,
+				    (efi_guid_t *)&efi_global_variable_guid);
+	}
+
+	free(var_name16);
+
+	return CMD_RET_SUCCESS;
+}
+
+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_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);
+	}
+
+	free(var_name16);
+
+	return CMD_RET_SUCCESS;
+}
+
+static int efi_dump_var(cmd_tbl_t *cmdtp, int flag,
+			int argc, char * const argv[])
+{
+	if (argc > 1)
+		/* show specified UEFI variables */
+		return efi_dump_var_ones(--argc, ++argv);
+
+	/* enumerate and show all UEFI variables */
+	return efi_dump_var_all();
+}
+
+int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	efi_status_t r;
+
+	/* Initialize EFI drivers */
+	r = efi_init_obj_list();
+	if (r != EFI_SUCCESS) {
+		printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
+		       r & ~EFI_ERROR_MASK);

cmd/nvedit_efi.c line 185

Quoted 10 lines
cmd/nvedit_efi.c new file +10 -0
+		return CMD_RET_FAILURE;
+	}
+
+	return efi_dump_var(cmdtp, flag, argc, argv);
+}
+
+/*
+ * Interpret a given data string and append it to buffer.
+ * Buffer will be realloc'ed if necessary.
+ */

Please, use Sphinx style comment:

Quoted 5 lines

/**
* append_value() - append data string to buffer *
* Interpret a given data string and append it to buffer.
* Buffer will be realloc'ed if necessary.

For example, the function description should be placed between "@data" and "Return:"?

Quoted 5 lines

* @bufp: pointer to pointer to buffer
* @sizep: pointer to size of buffer
* (updated if buffer is reallocated)
* @data: data to be interpreted and appended
* Return: 0 on success

Some people writes "@return(:)". Some people have nothing about return value.

Quoted 121 lines

*/

Quoted 120 lines
cmd/nvedit_efi.c new file +120 -0
+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;
+}
+
+static int efi_set_var(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;
+	efi_guid_t guid;
+	efi_status_t ret;
+
+	if (argc == 1)
+		return CMD_RET_SUCCESS;
+
+	var_name = argv[1];
+	if (argc == 2) {
+		/* remove */
+		value = NULL;
+		size = 0;
+	} else { /* set */
+		argc -= 2;
+		argv += 2;
+
+		for ( ; argc > 0; argc--, argv++)
+			if (append_value(&value, &size, argv[0]) < 0) {
Quoted 8 lines

cmd/nvedit_efi.c line 311

Quoted 6 lines
cmd/nvedit_efi.c new file +6 -0
+				ret = CMD_RET_FAILURE;
+				goto out;
+			}
+	}
+
+	var_name16 = malloc((strlen(var_name) + 1) * 2);

Please, always check the return value of malloc().

Okay.

Quoted 2 lines

strlen() is the wrong function here. Use utf8_utf16_strlen() to match the copy function below.

Okay.

Quoted 5 lines

cmd/nvedit_efi.c line 313

Quoted 2 lines
cmd/nvedit_efi.c new file +2 -0
+	p = var_name16;
+	utf8_utf16_strncpy(&p, var_name, strlen(var_name) + 1);

Don't you trust your malloc() call?
Wouldn't you use utf8_utf16_strcpy()?

utf8_utf16_strcpy() doesn't exist now.
utf8_utf16_strncpy() does.

Thanks,
-Takahiro Akashi

Quoted 48 lines

Best regards

Heinrich

Quoted 43 lines
cmd/nvedit_efi.c new file +27 -0
+
+	guid = efi_global_variable_guid;
+	ret = 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;
+}
+
+int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	efi_status_t r;
+
+	/* Initialize EFI 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;
+	}
+
+	return efi_set_var(cmdtp, flag, argc, argv);
+}
include/command.h +4 -0
index feddef300ccc..efc4dfc457c1 100644
@@ -51,6 +51,10 @@ extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 #if defined(CONFIG_CMD_BOOTEFI)
 extern int do_bootefi_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 #endif
+#if defined(CONFIG_CMD_NVEDIT_EFI)
+int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+#endif
 
 /* common/command.c */
 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int