From patchwork Wed Nov 28 06:00:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 1004234 X-Patchwork-Delegate: xypron.glpk@gmx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="dHDFKbZi"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 434VNs6lPQz9s2P for ; Wed, 28 Nov 2018 16:59:57 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 7F8F1C22184; Wed, 28 Nov 2018 05:59:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 8FF8AC221AB; Wed, 28 Nov 2018 05:58:56 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id B527BC22167; Wed, 28 Nov 2018 05:58:52 +0000 (UTC) Received: from mail-yb1-f193.google.com (mail-yb1-f193.google.com [209.85.219.193]) by lists.denx.de (Postfix) with ESMTPS id 8415FC221BC for ; Wed, 28 Nov 2018 05:58:48 +0000 (UTC) Received: by mail-yb1-f193.google.com with SMTP id t13-v6so10149803ybb.8 for ; Tue, 27 Nov 2018 21:58:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=V6qwGHSpURnUDRCWFLWp10PXCsXIA/7PAHrzM9YMy7Y=; b=dHDFKbZivxqYkZxe8Oz42hjdlOrQYNdae/PRKy7xr8RYlR426ap33eFDqESiFScF84 SV0n+qu2ck523Ov4vhzmkBU1GEYW3t5sYL8ZvsIhVJ91457vYCF3OtIF2VOJYTz34Y11 T1oGgcBWSFDTw7oRsonUYGIQYn05GLO9m9e4o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=V6qwGHSpURnUDRCWFLWp10PXCsXIA/7PAHrzM9YMy7Y=; b=AOawLfUntyu8v+R6/Q0+1tFCRMf55JcFckrkqNaoTZpLaBk0XNgPUe2GmUpLCHO5Ok SMe4q98+afvF2B0iwzPkx0N3YY3uS6NunOGIEdvfRrb2S+ILKuky15MLaXWQjnMcc1ok 7P1BgDFd0PPvxd0uZf1DZajBmixYad6hMqsRjf2qIrs0epZqE8Vtka98M/EYzCtIMbRP Zx91AodtUT8fWQ0BKONlfAmStX42oA0E+ArYa0wXs9/QZvjEsbt0+0gNMQ5i1JbSE5d8 9SDSRtRD5CgHvEeZePfcFvrfBjOGv1qY6JaGfyaMsLkQQuJgvASik3sE2sQJcpI+EL6x oQYA== X-Gm-Message-State: AA+aEWb2x9tdLK5zrpxcPuE5gQ9BTOoEtir7+nEaXKVzuMX95Df2vN5p ExbOmxQz+N+lDdLbWUhmu+lS/A== X-Google-Smtp-Source: AFSGD/VA5bc0VQJnmYztyNCbADfiQNkqhhUKtlgiThQaBzrArJvloAxFht/qkkhixd1x2RILl9ULvQ== X-Received: by 2002:a25:764d:: with SMTP id r74-v6mr35922658ybc.461.1543384727275; Tue, 27 Nov 2018 21:58:47 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id j134sm4657152ywb.91.2018.11.27.21.58.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 21:58:46 -0800 (PST) From: AKASHI Takahiro To: trini@konsulko.com, agraf@suse.de, xypron.glpk@gmx.de Date: Wed, 28 Nov 2018 15:00:56 +0900 Message-Id: <20181128060059.5508-2-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181128060059.5508-1-takahiro.akashi@linaro.org> References: <20181128060059.5508-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 Cc: u-boot@lists.denx.de Subject: [U-Boot] [PATCH 1/4] efi_loader: support non-volatile variable behavior X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" An EFI variable is nothing but a wrapper of a corresponding u-boot environment variable (See efi_variable.c), but under the current implementation, NON_VOLATILE attribute is not honored while u-boot environment variables can be saved/restored in storage. With this patch, the expected semantics will be mimicked by deleting all the EFI variables *without* NON_VOLATILE attribute when loading them from storage at boot time. Signed-off-by: AKASHI Takahiro --- env/env.c | 4 +++ include/efi_loader.h | 1 + lib/efi_loader/efi_variable.c | 64 +++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/env/env.c b/env/env.c index afed0f3c95c3..c507a4ac5f78 100644 --- a/env/env.c +++ b/env/env.c @@ -5,6 +5,7 @@ */ #include +#include #include DECLARE_GLOBAL_DATA_PTR; @@ -195,6 +196,9 @@ int env_load(void) if (ret) { debug("Failed (%d)\n", ret); } else { +#ifdef CONFIG_EFI_LOADER + efi_purge_volatile_variables(); +#endif printf("OK\n"); return 0; } diff --git a/include/efi_loader.h b/include/efi_loader.h index 9f7a4068efa6..9cad1dcd62bb 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -533,6 +533,7 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, efi_status_t EFIAPI efi_set_variable(u16 *variable_name, efi_guid_t *vendor, u32 attributes, efi_uintn_t data_size, void *data); +int efi_purge_volatile_variables(void); /* * See section 3.1.3 in the v2.7 UEFI spec for more details on diff --git a/lib/efi_loader/efi_variable.c b/lib/efi_loader/efi_variable.c index 19d9cb865f25..ad8cd36fa1e1 100644 --- a/lib/efi_loader/efi_variable.c +++ b/lib/efi_loader/efi_variable.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #define READ_ONLY BIT(31) @@ -142,6 +144,8 @@ static const char *parse_attr(const char *str, u32 *attrp) if ((s = prefix(str, "ro"))) { attr |= READ_ONLY; + } else if ((s = prefix(str, "nv"))) { + attr |= EFI_VARIABLE_NON_VOLATILE; } else if ((s = prefix(str, "boot"))) { attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; } else if ((s = prefix(str, "run"))) { @@ -293,7 +297,7 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name, efi_guid_t *vendor, } } - val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1); + val = malloc(2 * data_size + strlen("{ro,run,boot,nv}(blob)") + 1); if (!val) { ret = EFI_OUT_OF_RESOURCES; goto out; @@ -302,12 +306,16 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name, efi_guid_t *vendor, s = val; /* store attributes: */ - attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS); + attributes &= (EFI_VARIABLE_NON_VOLATILE | + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS); s += sprintf(s, "{"); while (attributes) { u32 attr = 1 << (ffs(attributes) - 1); - if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) + if (attr == EFI_VARIABLE_NON_VOLATILE) + s += sprintf(s, "nv"); + else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) s += sprintf(s, "boot"); else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) s += sprintf(s, "run"); @@ -334,3 +342,53 @@ out: return EFI_EXIT(ret); } + +/* + * Purge all the variables which are not marked non volatile. + * This function is assumed to be called only once at boot time. + */ +int efi_purge_volatile_variables(void) +{ + char regex[256]; + char * const regexlist[] = {regex}; + char *list = NULL, *name, *value; + int len, ret = 0; + u32 attr; + + snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*"); + + len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY, + &list, 0, 1, regexlist); + + if (len < 0) + return -1; + else if (!len) + return 0; + + name = list; + while (*name) { + /* variable name */ + value = strchr(name, '='); + if (!value) + break; + *value = '\0'; + value++; + + parse_attr(value, &attr); + if (!(attr & EFI_VARIABLE_NON_VOLATILE)) { + if (env_set(name, NULL)) { + printf("cannot purge efi variable: %s\n", name); + ret = -1; + } + } + + name = strchr(value, '\n'); + if (!name) + break; + name++; + } + + free(list); + + return ret; +}