U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: Matthew Garrett <mjg59@srcf.ucam.org>, u-boot@lists.denx.de
Cc: Matthew Garrett <mgarrett@aurora.tech>,
	AKASHI Takahiro <akashi.tkhro@gmail.com>,
	Dmitry Rokosov <ddrokosov@salutedevices.com>,
	Francis Laniel <francis.laniel@amarulasolutions.com>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Jerome Forissier <jerome.forissier@linaro.org>,
	Mattijs Korpershoek <mkorpershoek@baylibre.com>,
	Simon Glass <sjg@chromium.org>, Tom Rini <trini@konsulko.com>
Subject: Re: [PATCH 10/10] Add command to set an environment variable to an EFI variable
Date: Sun, 24 Nov 2024 15:58:18 +0100	[thread overview]
Message-ID: <b79cd88a-4346-444d-979c-8eb2ed913d5e@gmx.de> (raw)
In-Reply-To: <20241123195616.305687-11-mjg59@srcf.ucam.org>

On 11/23/24 20:55, Matthew Garrett wrote:
> From: Matthew Garrett <mgarrett@aurora.tech>
>
> We may want to make things conditional on EFI variable state
>
> Signed-off-by: Matthew Garrett <mgarrett@aurora.tech>
> ---
>
>   cmd/Kconfig     |   4 ++
>   cmd/Makefile    |   1 +
>   cmd/efigetenv.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++
>   3 files changed, 138 insertions(+)
>   create mode 100644 cmd/efigetenv.c
>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 33bf3d1ad39..118fb721081 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -752,6 +752,10 @@ config CMD_NVEDIT_SELECT
>   	help
>   	  Select the compiled-in persistent storage of environment variables.
>
> +config CMD_EFI_GET_ENV
> +	bool "efi get env"
> +	help
> +	  Set an environment variable to the contents of an EFI variable
>   endmenu
>
>   menu "Memory commands"
> diff --git a/cmd/Makefile b/cmd/Makefile
> index 38cb7a4aea5..0507c204c0e 100644
> --- a/cmd/Makefile
> +++ b/cmd/Makefile
> @@ -69,6 +69,7 @@ obj-$(CONFIG_CMD_EEPROM) += eeprom.o
>   obj-$(CONFIG_EFI) += efi.o efi_common.o
>   obj-$(CONFIG_CMD_EFIDEBUG) += efidebug.o efi_common.o
>   obj-$(CONFIG_CMD_EFICONFIG) += eficonfig.o
> +obj-$(CONFIG_CMD_EFI_GET_ENV) += efigetenv.o
>   ifdef CONFIG_CMD_EFICONFIG
>   ifdef CONFIG_EFI_MM_COMM_TEE
>   obj-$(CONFIG_EFI_SECURE_BOOT) += eficonfig_sbkey.o
> diff --git a/cmd/efigetenv.c b/cmd/efigetenv.c
> new file mode 100644
> index 00000000000..5284ee92d6c
> --- /dev/null
> +++ b/cmd/efigetenv.c
> @@ -0,0 +1,133 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +#include <charset.h>
> +#include <command.h>
> +#include <efi_loader.h>
> +#include <efi_variable.h>
> +#include <env.h>
> +#include <hexdump.h>
> +#include <malloc.h>
> +#include <uuid.h>
> +
> +/* Set a U-Boot environment variable to the contents of a UEFI variable */
> +int do_efi_get_env(struct cmd_tbl *cmdtb, int flat, int argc, char *const argv[])
> +{
> +	u16 *var_name = NULL;
> +	char *strdata = NULL;
> +	efi_uintn_t size = 0;
> +	bool var_content_is_utf16_string = false;
> +	efi_status_t ret;
> +	efi_guid_t guid;
> +	u8 *data = NULL;
> +	u32 attributes;
> +	size_t len;
> +	u64 time;
> +	u16 *p;
> +
> +	ret = efi_init_obj_list();
> +	if (ret != EFI_SUCCESS) {
> +		printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
> +		       ret & ~EFI_ERROR_MASK);
> +		return CMD_RET_FAILURE;
> +	}
> +
> +	argv++;
> +	argc--;
> +
> +	if (argc != 3 && argc != 4)
> +		return CMD_RET_USAGE;
> +
> +	if (argc == 4) {
> +		if (strcmp(argv[0], "-s"))
> +			return CMD_RET_USAGE;
> +		var_content_is_utf16_string = true;
> +		argv++;
> +		argc--;
> +	}
> +
> +	len = utf8_utf16_strnlen(argv[0], strlen(argv[0]));
> +	var_name = malloc((len + 1) * 2);
> +	if (!var_name) {
> +		printf("## Out of memory\n");
> +		return CMD_RET_FAILURE;
> +	}
> +	p = var_name;
> +	utf8_utf16_strncpy(&p, argv[0], len + 1);
> +
> +	if (uuid_str_to_bin(argv[1], guid.b, UUID_STR_FORMAT_GUID)) {
> +		ret = CMD_RET_USAGE;
> +		goto out;
> +	}
> +
> +	ret = efi_get_variable_int(var_name, &guid, &attributes, &size, data,
> +				   &time);
> +	if (ret == EFI_BUFFER_TOO_SMALL) {
> +		data = malloc(size);
> +		if (!data) {
> +			printf("## Out of memory\n");
> +			ret = CMD_RET_FAILURE;
> +			goto out;
> +		}
> +		ret = efi_get_variable_int(var_name, &guid, &attributes,
> +					   &size, data, &time);

This duplicates code in efi_dump_single_var(), do_efi_capsule_res(),
get_dp_device() and others.

We should carve out a function.

Best regards

Heinrich

> +	}
> +
> +	if (ret == EFI_NOT_FOUND) {
> +		printf("Error: \"%ls\" not defined\n", var_name);
> +		ret = CMD_RET_FAILURE;
> +		goto out;
> +	}
> +
> +	if (ret != EFI_SUCCESS) {
> +		printf("Error: Cannot read variable, r = %lu\n",
> +		       ret & ~EFI_ERROR_MASK);
> +		ret = CMD_RET_FAILURE;
> +		goto out;
> +	}
> +
> +	if (var_content_is_utf16_string) {
> +		char *p;
> +
> +		len = utf16_utf8_strnlen((u16 *)data, size / 2);
> +		strdata = malloc(len + 1);
> +		if (!strdata) {
> +			printf("## Out of memory\n");
> +			ret = CMD_RET_FAILURE;
> +			goto out;
> +		}
> +		p = strdata;
> +		utf16_utf8_strncpy(&p, (u16 *)data, size / 2);
> +	} else {
> +		len = size * 2;
> +		strdata = malloc(len + 1);
> +		if (!strdata) {
> +			printf("## Out of memory\n");
> +			ret = CMD_RET_FAILURE;
> +			goto out;
> +		}
> +		bin2hex(strdata, data, size);
> +	}
> +
> +	strdata[len] = '\0';
> +
> +	ret = env_set(argv[2], strdata);
> +	if (ret) {
> +		ret = CMD_RET_FAILURE;
> +		goto out;
> +	}
> +
> +	ret = CMD_RET_SUCCESS;
> +out:
> +	free(strdata);
> +	free(data);
> +	free(var_name);
> +
> +	return ret;
> +}
> +
> +U_BOOT_CMD(
> +	efigetenv, 5, 4, do_efi_get_env,
> +	"set environment variable to content of EFI variable",
> +	"[-s] name guid envvar\n"
> +	"    - set environment variable 'envvar' to the EFI variable 'name'-'guid'\n"
> +	"      \"-s\": Interpret the EFI variable value as a UTF-16 string\n"
> +);


  reply	other threads:[~2024-11-24 14:58 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-23 19:54 [PATCH 00/10] Improve UEFI app support Matthew Garrett
2024-11-23 19:55 ` [PATCH 01/10] Add EFI handover support to bootm Matthew Garrett
2024-11-24 14:43   ` Heinrich Schuchardt
2024-11-24 19:29     ` Matthew Garrett
2024-11-24 19:51       ` Heinrich Schuchardt
2024-12-08 23:06         ` Simon Glass
2024-11-25 13:46       ` Ilias Apalodimas
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 02/10] Add part_find command Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-12-10  8:21   ` Heinrich Schuchardt
2024-12-10 16:16     ` Simon Glass
2024-11-23 19:55 ` [PATCH 03/10] Add a command to find a load address Matthew Garrett
2024-11-24 15:56   ` Tom Rini
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 04/10] Hook up EFI env variable support in the EFI app Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 05/10] Add EFI network driver Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 06/10] Add UEFI TPM2 driver Matthew Garrett
2024-12-01 16:12   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 07/10] Support separate DTB files with the UEFI app Matthew Garrett
2024-11-25 13:55   ` Ilias Apalodimas
2024-12-01 16:14   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 08/10] Use the correct ramdisk address Matthew Garrett
2024-12-01 16:14   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-11-23 19:55 ` [PATCH 09/10] Fix efi_bind_block Matthew Garrett
2024-11-25 13:40   ` Ilias Apalodimas
2024-12-09  2:28     ` Simon Glass
2024-12-01 16:14   ` Simon Glass
2024-12-08 15:29     ` Simon Glass
2024-12-10  8:45   ` Heinrich Schuchardt
2024-12-10 16:17     ` Simon Glass
2024-12-10 17:11       ` Tom Rini
2024-12-11 17:50         ` Janis Danisevskis
2024-12-11 17:59           ` Ilias Apalodimas
2024-12-11 20:23           ` Simon Glass
2024-12-11 22:28             ` Janis Danisevskis
2024-11-23 19:55 ` [PATCH 10/10] Add command to set an environment variable to an EFI variable Matthew Garrett
2024-11-24 14:58   ` Heinrich Schuchardt [this message]
2024-12-01 16:14     ` Simon Glass
2024-12-08 15:29       ` Simon Glass
2024-12-09  2:28     ` Simon Glass
2024-11-24 14:40 ` [PATCH 00/10] Improve UEFI app support Heinrich Schuchardt
2024-12-01 16:15 ` Simon Glass
2024-12-08 15:28   ` Simon Glass
2024-12-08 15:51     ` Tom Rini
2024-12-08 18:50       ` Tom Rini
2024-12-08 23:07       ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b79cd88a-4346-444d-979c-8eb2ed913d5e@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=akashi.tkhro@gmail.com \
    --cc=ddrokosov@salutedevices.com \
    --cc=francis.laniel@amarulasolutions.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jerome.forissier@linaro.org \
    --cc=mgarrett@aurora.tech \
    --cc=mjg59@srcf.ucam.org \
    --cc=mkorpershoek@baylibre.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox