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: 152196 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp700398ljp; Tue, 27 Nov 2018 22:00:16 -0800 (PST) X-Google-Smtp-Source: AFSGD/W+ga9qS3+n+OW2MOrTPENIHfkhy1wSN8IKKgqpNjR51bGnO0yk/yyq3EqcLf5lT2B1z8Bk X-Received: by 2002:a50:9ee3:: with SMTP id a90mr29193601edf.283.1543384816845; Tue, 27 Nov 2018 22:00:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543384816; cv=none; d=google.com; s=arc-20160816; b=cEVBJ9skYXhBuTsiHuL5UO6KPF6h5QC3mRN2kbuxbI5H0+anixtwiXwyySa5FJJCBm KilGowuTc5zsfp1aW26+dlFLT4ZNWUw8QkMqdxzmziuYlVlcHqQrtiD5kNHfLt+8EMyg mU8D5y7/fWJfM3bAoqLcUTxOpeHoiP86mcq6OufMg3DxZ+W77p+2Gj7B7zmgYpzB+1pD Xr5D8YFufKrNQFV9VeIV4zy4lrFtLXkB8nZAGXnz2ypW/q6OXSxaLbpAdj6Ys+sMVVdw BVcFie6id5ZwM8lAxcs5zBwUn6nJfy33IS1y/YJJYEyIIFduujIw4udYaJ0Sn+kQHF5h b7Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :cc:mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature; bh=4uQXBvYD0afUoYtjYwmB88s98zkedhz9W/godjCxqDc=; b=ExpQAQnsbFWqVSxdXVhqFwZzq6eMWe7KAB/zSF/UFB+b2UTwgb8i8GL6+VZNrZ+tMR rV+hdNJAyTBV5kQcaY+5YVjztSKs2BEaY1gKTN1cG5UlOYLRTeo/2VFh+2DqiXwmhH5w govRsN+RyD009ZPjAnPRncXP7h1SAFExA4Yd4RpyiFaV0cOfV7ZLEhr0zcsROfKKoUB+ yAgVPGV9STnRkO6Iuyu51BDTztNRoJZqmwGceeK3J5OJ2jQ3DWRwd3fO53Jjhdx/vLVq 5PIiT/4AexvxR1mqEyhTuZSnl+K3I2SakAA9DKICdPSgkUgPsIVdE06a4YT62YvOEwAG grGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=dHDFKbZi; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id z49si1003300edb.342.2018.11.27.22.00.16; Tue, 27 Nov 2018 22:00:16 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=dHDFKbZi; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 6BE35C2218E; 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-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; +}