All of lore.kernel.org
 help / color / mirror / Atom feed
From: AKASHI Takahiro <takahiro.akashi@linaro.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v5 19/19] efi_loader: variable: rework with new env interfaces
Date: Thu,  5 Sep 2019 17:21:33 +0900	[thread overview]
Message-ID: <20190905082133.18996-20-takahiro.akashi@linaro.org> (raw)
In-Reply-To: <20190905082133.18996-1-takahiro.akashi@linaro.org>

In the previous commit, two efi-related contexts were introduced.

With this patch, EFI variables implementation will be modified to
support volatile/non-volatile attribute properly in such away as UEFI
specification expects; If a variable is non-volatile and modified,
its value will be updated in backing storage immediately and preserved
across reboot. If volatile, it will be lost after reboot.

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
 lib/efi_loader/efi_variable.c | 91 ++++++++++++++++++++++++++++++++---
 1 file changed, 83 insertions(+), 8 deletions(-)

diff --git a/lib/efi_loader/efi_variable.c b/lib/efi_loader/efi_variable.c
index 6687b69a400d..18b9e9e5d9f5 100644
--- a/lib/efi_loader/efi_variable.c
+++ b/lib/efi_loader/efi_variable.c
@@ -185,7 +185,9 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
 
 	EFI_PRINT("get '%s'\n", native_name);
 
-	val = env_get(native_name);
+	val = env_get(ctx_efi, native_name);
+	if (!val)
+		val = env_get(ctx_efi_volatile, native_name);
 	free(native_name);
 	if (!val)
 		return EFI_EXIT(EFI_NOT_FOUND);
@@ -388,12 +390,48 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
 		efi_cur_variable = NULL;
 
 		snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
-		list_len = hexport_r(&env_htab, '\n',
+		list_len = hexport_r(ctx_efi->htab, '\n',
 				     H_MATCH_REGEX | H_MATCH_KEY,
 				     &efi_variables_list, 0, 1, regexlist);
 		/* 1 indicates that no match was found */
-		if (list_len <= 1)
-			return EFI_EXIT(EFI_NOT_FOUND);
+		if (list_len <= 1) {
+			list_len = hexport_r(ctx_efi_volatile->htab, '\n',
+					     H_MATCH_REGEX | H_MATCH_KEY,
+					     &efi_variables_list, 0, 1,
+					     regexlist);
+
+			if (list_len <= 1)
+				return EFI_EXIT(EFI_NOT_FOUND);
+		} else {
+			char *list_volatile, *list_tmp;
+			size_t total_len;
+
+			list_volatile = NULL;
+			total_len = list_len;
+			list_len = hexport_r(ctx_efi_volatile->htab, '\n',
+					     H_MATCH_REGEX | H_MATCH_KEY,
+					     &list_volatile, 0, 1,
+					     regexlist);
+
+			/* concatenate two lists */
+			if (list_len > 1) {
+				total_len += list_len - 1;
+				list_tmp = efi_variables_list;
+
+				efi_variables_list = malloc(total_len);
+				if (efi_variables_list) {
+					strcpy(efi_variables_list, list_tmp);
+					strcat(efi_variables_list,
+					       list_volatile);
+				}
+
+				free(list_tmp);
+				free(list_volatile);
+
+				if (!efi_variables_list)
+					return EFI_EXIT(EFI_OUT_OF_RESOURCES);
+			}
+		}
 
 		variable = efi_variables_list;
 	}
@@ -424,8 +462,9 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
 				     efi_uintn_t data_size, const void *data)
 {
 	char *native_name = NULL, *val = NULL, *s;
+	struct env_context *ctx;
+	u32 attr, non_volatile;
 	efi_status_t ret = EFI_SUCCESS;
-	u32 attr;
 
 	EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
 		  data_size, data);
@@ -447,12 +486,17 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
 
 	if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
 		/* delete the variable: */
-		env_set(native_name, NULL);
+		env_set(ctx_efi, native_name, NULL);
 		ret = EFI_SUCCESS;
 		goto out;
 	}
 
-	val = env_get(native_name);
+	if (attributes & EFI_VARIABLE_NON_VOLATILE)
+		ctx = ctx_efi;
+	else
+		ctx = ctx_efi_volatile;
+
+	val = env_get(ctx, native_name);
 	if (val) {
 		parse_attr(val, &attr);
 
@@ -485,6 +529,7 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
 	 * store attributes
 	 * TODO: several attributes are not supported
 	 */
+	non_volatile = (attributes & EFI_VARIABLE_NON_VOLATILE);
 	attributes &= (EFI_VARIABLE_NON_VOLATILE |
 		       EFI_VARIABLE_BOOTSERVICE_ACCESS |
 		       EFI_VARIABLE_RUNTIME_ACCESS);
@@ -512,7 +557,9 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
 
 	EFI_PRINT("setting: %s=%s\n", native_name, val);
 
-	if (env_set(native_name, val))
+	if (env_set(ctx, native_name, val))
+		ret = EFI_DEVICE_ERROR;
+	else if (non_volatile && env_save(ctx))
 		ret = EFI_DEVICE_ERROR;
 
 out:
@@ -619,5 +666,33 @@ void efi_variables_boot_exit_notify(void)
  */
 efi_status_t efi_init_variables(void)
 {
+	int ret;
+
+	/*
+	 * Volatile variables:
+	 * Context for volatile variables has no backing storage
+	 */
+	ret = env_ctx_init(ctx_efi_volatile);
+	if (ret) {
+		printf("Initializing efi variables(volatile) failed\n");
+
+		return EFI_DEVICE_ERROR;
+	}
+
+	/* Non-Volatile variables */
+	ret = env_ctx_init(ctx_efi);
+	if (ret) {
+		printf("Initializing efi variables failed\n");
+
+		return EFI_DEVICE_ERROR;
+	}
+
+	ret = env_load(ctx_efi);
+	if (ret && ret != -ENODEV) {
+		printf("Loading efi variables failed\n");
+
+		return EFI_DEVICE_ERROR;
+	}
+
 	return EFI_SUCCESS;
 }
-- 
2.21.0

  parent reply	other threads:[~2019-09-05  8:21 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-05  8:21 [U-Boot] [PATCH v5 00/19] efi_loader: non-volatile variables support AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 01/19] env: extend interfaces allowing for env contexts AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 02/19] env: define env context for U-Boot environment AKASHI Takahiro
2019-09-05 19:43   ` Heinrich Schuchardt
2019-09-06  0:41     ` AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 03/19] env: nowhere: rework with new env interfaces AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 04/19] env: flash: support multiple env contexts AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 05/19] env: fat: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 06/19] hashtable: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 07/19] api: converted with new env interfaces AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 08/19] arch: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 09/19] board: " AKASHI Takahiro
2019-09-05 12:02   ` Lukasz Majewski
2019-09-06  0:34     ` AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 10/19] cmd: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 11/19] common: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 12/19] disk: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 13/19] drivers: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 14/19] fs: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 15/19] lib: converted with new env interfaces (except efi_loader) AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 16/19] net: converted with new env interfaces AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 17/19] post: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 18/19] env, efi_loader: define env context for UEFI variables AKASHI Takahiro
2019-09-05 19:37   ` Heinrich Schuchardt
2019-09-06  0:54     ` AKASHI Takahiro
2019-09-05  8:21 ` AKASHI Takahiro [this message]
2019-09-05  8:31 ` [U-Boot] [PATCH v5 00/19] efi_loader: non-volatile variables support AKASHI Takahiro
2019-10-01  6:28 ` AKASHI Takahiro
2019-10-23  6:53   ` AKASHI Takahiro
2019-10-25  7:06     ` Wolfgang Denk
2019-10-25  7:56       ` AKASHI Takahiro
2019-10-25 13:25         ` Wolfgang Denk
2019-10-28  1:14           ` AKASHI Takahiro
2019-10-29 13:28             ` Wolfgang Denk
2019-11-01  6:04               ` AKASHI Takahiro
2019-11-04 16:00                 ` Wolfgang Denk
2019-11-04 16:16                   ` Tom Rini
2019-11-05  5:18                   ` AKASHI Takahiro

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=20190905082133.18996-20-takahiro.akashi@linaro.org \
    --to=takahiro.akashi@linaro.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.