From patchwork Wed Apr 17 10:19:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 789309 Delivered-To: patch@linaro.org Received: by 2002:a5d:4dc5:0:b0:346:15ad:a2a with SMTP id f5csp204509wru; Wed, 17 Apr 2024 03:19:50 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX/Zbl146hyRd9PqJEiUf40fSDLfIU/0xkDjhEY0nk3UWpu8XpHrwL1Y9Z8i9D+e0SnWF6NanQiqKmNaGhwLFV1 X-Google-Smtp-Source: AGHT+IEEwH5n/Mp+aoaPI+uTxSo2HablToItGjWiaCevSpMlqkoZN2PP4XRcOQ3kUSl8GK39h2Ta X-Received: by 2002:a17:907:982:b0:a52:64a4:5e82 with SMTP id bf2-20020a170907098200b00a5264a45e82mr8543962ejc.22.1713349190541; Wed, 17 Apr 2024 03:19:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713349190; cv=none; d=google.com; s=arc-20160816; b=vWSs8zEhFE5fIW4QIF8acjXZevXwq/PoOeU3dWzjBLYh4/1ft3j9UdqpGbwpn1SFUD oYxVOEYVYGnlD/Y/V7fFvqgbtGgKdE7L2QNs1K96Y5x1DrPCd51zGAImz49KaNWSSM8b BnBkMxhcrk+pFHp6mXLi2iXdKoAVQ2ecr7bXPorxTy8rOV+Xn3uMhCLPMyyJuTogYSCH hApV/oVXphd1awtyD4patK4bwQ65q3J0C8MEOgdRyl4q5VfA8NRwiPeTNLedo8Lvusux Q33bMwWqpBJWDm8xyYIw+yqEaxCl6y0upA7lYQy4L2OXPDuGgkJmxJ5wZpBOwT/0s/x2 wXMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gPghf8TXYvYdo7Ht0e/0AUynb8vjZ4cHYaJHPSUWEZs=; fh=FKBNAw45E7YkA0jN3xAvc1gl6pMMP3/nWr68fZcbsL0=; b=gCrbhWNmqBkzAo0SDpFbFizRCk7rj3THo17tjjqyJjzAukTO+rQ32e4kKnaytLXSm+ VnLBUkY7LByyXlnId3y/t32hDIOB/vQSIv+CP9ONTYUzAoIY8lNV3LFimj8GOtHpagJl zsoGEXt42sDm1XFGwRjrAiSJM+RIVH4PxdAeLj7Yd7Wjqv06kJm+XYQ2LITt9MPZcnAO xpR2tu1rr/S+CDWLJ8iXkXSZDeXyzBp0KeCzVt0h/YcbpjmNf2RQHcZvaVD5eP7pC+2M RRwrVqxkLPgS5KKmfpO9nxY9NrfA3pDxFGXFhIpu07Ted+LUjgfImpb3viV7BDGqWfiK Vsuw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rQ9jnGDv; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id i18-20020a170906a29200b00a555bdbdc9csi445276ejz.890.2024.04.17.03.19.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:19:50 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rQ9jnGDv; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CF0248845C; Wed, 17 Apr 2024 12:19:43 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="rQ9jnGDv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4879A88344; Wed, 17 Apr 2024 12:19:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D6BC48845C for ; Wed, 17 Apr 2024 12:19:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-518b9527c60so4422583e87.0 for ; Wed, 17 Apr 2024 03:19:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713349180; x=1713953980; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gPghf8TXYvYdo7Ht0e/0AUynb8vjZ4cHYaJHPSUWEZs=; b=rQ9jnGDvKfLecBWBK79WukUwCGwWQQFqhPs17YPTNLAf1mMf5UEppo9DQz5+1ABDXa dGblrBLmiRK5tcPw4gayEh0itmoqFlBW55d7SXhQOVTjkx0qotRYmVAr0J54SZszyeMD cZnhBinvwwZPExPpJMfzHKVH/w9EFuOWjTmyava4veQE2yGjSyirsVN8lhpWQiIIuP5M /FNTF/4fyAv/enZ9P99n7UTn/P22FbWazHvJaxB8rVzYSbQ20CrFMXvC2CiX/Gz+F0C1 9SjOL0Z/WFdOj+24ScLb+q7RAl6NenumnJQOyd89YB97AC9qfzcW0ytbAWqAwmdGkABz ApCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713349180; x=1713953980; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gPghf8TXYvYdo7Ht0e/0AUynb8vjZ4cHYaJHPSUWEZs=; b=Px/LiRypLxo5H9ngcKJ9eY+zqjzI5mdQDuwNBxtyx6/Fji+ZzRAEKsT2b3b2lp6ZaJ RFdHKuqd37LSudybsUFbVe7t5Ywg2FTBRVuakf38Lkcw6i2lpf5xo4u/ftLKXgQE/dXu B29q/NGe9tXV0xWD1lxz0Goeuz8TX6+SgIZg80tXECuiVC4Sb7zPHZX+mumjKI7i/xiU 7w7/ryqqKGeTT9z5wLC/f0yhtStB6AqFYGltwAbUCajShX/lEIZTXZbl3akfiODJi3Dq pFeA7QCv5EJdtT+PqEwdJn/grez4VBs6Ie9YpShABeM10KY5Ijq5WxwXMmt9ppnVH/ah XQlg== X-Forwarded-Encrypted: i=1; AJvYcCUrQ0ofFtburx3VN8gaXMTEpCYpOH6JM54XdwkYbcIVsmBGsZuPzSrpmVOzo33+n002nEU2a0BVwGJXlhf6s+eL+wqO7A== X-Gm-Message-State: AOJu0YzdvJEWsOex0hHFkDcmj2CHuAld7l1++pOxxJefhLvaGQqDLdgR 3ErTNxyK4tYoLxOvxHEySXTq4rPSK3h4F6M9CdevoSNUQ35D613Q1vqt5DRDTGY= X-Received: by 2002:a05:6512:46a:b0:515:9aba:743a with SMTP id x10-20020a056512046a00b005159aba743amr12277530lfd.59.1713349180024; Wed, 17 Apr 2024 03:19:40 -0700 (PDT) Received: from hades.. (ppp089210071137.access.hol.gr. [89.210.71.137]) by smtp.gmail.com with ESMTPSA id g12-20020a5d488c000000b003472489d26fsm13567780wrq.19.2024.04.17.03.19.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:19:39 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de, kettenis@openbsd.org Cc: caleb.connolly@linaro.org, sumit.garg@linaro.org, quic_llindhol@quicinc.com, ardb@kernel.org, pbrobinson@gmail.com, pjones@redhat.com, Ilias Apalodimas , Tom Rini , Masahisa Kojima , AKASHI Takahiro , Raymond Mao , Janne Grunau , Simon Glass , Matthias Schiffer , Abdellatif El Khlifi , Sughosh Ganu , Sam Edwards , Richard Henderson , Alper Nebi Yasak , Weizhao Ouyang , u-boot@lists.denx.de Subject: [PATCH v2 1/4] efi_loader: conditionally enable SetvariableRT Date: Wed, 17 Apr 2024 13:19:22 +0300 Message-Id: <20240417101928.119115-2-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240417101928.119115-1-ilias.apalodimas@linaro.org> References: <20240417101928.119115-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean When we store EFI variables on file we don't allow SetVariable at runtime, since the OS doesn't know how to access or write that file. At the same time keeping the U-Boot drivers alive in runtime sections and performing writes from the firmware is dangerous -- if at all possible. For GetVariable at runtime we copy runtime variables in RAM and expose them to the OS. Add a Kconfig option and provide SetVariable at runtime using the same memory backend. The OS will be responsible for syncing the RAM contents to the file, otherwise any changes made during runtime won't persist reboots. It's worth noting that the variable store format is defined in EBBR [0] and authenticated variables are explicitly prohibited, since they have to be stored on a medium that's tamper and rollback protected. - pre-patch $~ mount | grep efiva efivarfs on /sys/firmware/efi/efivars type efivarfs (ro,nosuid,nodev,noexec,relatime) $~ efibootmgr -n 0001 Could not set BootNext: Read-only file system - post-patch $~ mount | grep efiva efivarfs on /sys/firmware/efi/efivars type efivarfs (rw,nosuid,nodev,noexec,relatime) $~ efibootmgr -n 0001 BootNext: 0001 BootCurrent: 0000 BootOrder: 0000,0001 Boot0000* debian HD(1,GPT,bdae5610-3331-4e4d-9466-acb5caf0b4a6,0x800,0x100000)/File(EFI\debian\grubaa64.efi) Boot0001* virtio 0 VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b,0000000000000000)/VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b,850000001f000000)/VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b,1600850000000000){auto_created_boot_option} $~ efivar -p -n 8be4df61-93ca-11d2-aa0d-00e098032b8c-BootNext GUID: 8be4df61-93ca-11d2-aa0d-00e098032b8c Name: "BootNext" Attributes: Non-Volatile Boot Service Access Runtime Service Access Value: 00000000 01 00 [0] https://arm-software.github.io/ebbr/index.html#document-chapter5-variable-storage Signed-off-by: Ilias Apalodimas Reviewed-by: Heinrich Schuchardt --- lib/efi_loader/Kconfig | 16 +++ lib/efi_loader/efi_runtime.c | 4 + lib/efi_loader/efi_variable.c | 115 ++++++++++++++++-- .../efi_selftest_variables_runtime.c | 13 +- 4 files changed, 134 insertions(+), 14 deletions(-) diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig index e13a6f9f4c3a..cc8371a3bb4c 100644 --- a/lib/efi_loader/Kconfig +++ b/lib/efi_loader/Kconfig @@ -62,6 +62,22 @@ config EFI_VARIABLE_FILE_STORE Select this option if you want non-volatile UEFI variables to be stored as file /ubootefi.var on the EFI system partition. +config EFI_RT_VOLATILE_STORE + bool "Allow variable runtime services in volatile storage (e.g RAM)" + depends on EFI_VARIABLE_FILE_STORE + help + When EFI variables are stored on file we don't allow SetVariableRT, + since the OS doesn't know how to write that file. At he same time + we copy runtime variables in DRAM and support GetVariableRT + + Enable this option to allow SetVariableRT on the RAM backend of + the EFI variable storage. The OS will be responsible for syncing + the RAM contents to the file, otherwise any changes made during + runtime won't persist reboots. + Authenticated variables are not supported. Note that this will + violate the EFI spec since writing auth variables will return + EFI_INVALID_PARAMETER + config EFI_MM_COMM_TEE bool "UEFI variables storage service via the trusted world" depends on OPTEE diff --git a/lib/efi_loader/efi_runtime.c b/lib/efi_loader/efi_runtime.c index a61c9a77b13f..dde083b09665 100644 --- a/lib/efi_loader/efi_runtime.c +++ b/lib/efi_loader/efi_runtime.c @@ -127,6 +127,10 @@ efi_status_t efi_init_runtime_supported(void) EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP | EFI_RT_SUPPORTED_CONVERT_POINTER; + if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) + rt_table->runtime_services_supported |= + EFI_RT_SUPPORTED_SET_VARIABLE; + /* * This value must be synced with efi_runtime_detach_list * as well as efi_runtime_services. diff --git a/lib/efi_loader/efi_variable.c b/lib/efi_loader/efi_variable.c index e6c1219a11c8..abc2a3402f42 100644 --- a/lib/efi_loader/efi_variable.c +++ b/lib/efi_loader/efi_variable.c @@ -219,17 +219,20 @@ efi_get_next_variable_name_int(efi_uintn_t *variable_name_size, return efi_get_next_variable_name_mem(variable_name_size, variable_name, vendor); } -efi_status_t efi_set_variable_int(const u16 *variable_name, - const efi_guid_t *vendor, - u32 attributes, efi_uintn_t data_size, - const void *data, bool ro_check) +/** + * setvariable_allowed() - checks defined by the UEFI spec for setvariable + * + * @variable_name: name of the variable + * @vendor: vendor GUID + * @attributes: attributes of the variable + * @data_size: size of the buffer with the variable value + * @data: buffer with the variable value + * Return: status code + */ +static efi_status_t __efi_runtime +setvariable_allowed(const u16 *variable_name, const efi_guid_t *vendor, + u32 attributes, efi_uintn_t data_size, const void *data) { - struct efi_var_entry *var; - efi_uintn_t ret; - bool append, delete; - u64 time = 0; - enum efi_auth_var_type var_type; - if (!variable_name || !*variable_name || !vendor) return EFI_INVALID_PARAMETER; @@ -261,6 +264,25 @@ efi_status_t efi_set_variable_int(const u16 *variable_name, !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) return EFI_INVALID_PARAMETER; + return EFI_SUCCESS; +} + +efi_status_t efi_set_variable_int(const u16 *variable_name, + const efi_guid_t *vendor, + u32 attributes, efi_uintn_t data_size, + const void *data, bool ro_check) +{ + struct efi_var_entry *var; + efi_uintn_t ret; + bool append, delete; + u64 time = 0; + enum efi_auth_var_type var_type; + + ret = setvariable_allowed(variable_name, vendor, attributes, data_size, + data); + if (ret != EFI_SUCCESS) + return ret; + /* check if a variable exists */ var = efi_var_mem_find(vendor, variable_name, NULL); append = !!(attributes & EFI_VARIABLE_APPEND_WRITE); @@ -454,7 +476,78 @@ efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, u32 attributes, efi_uintn_t data_size, const void *data) { - return EFI_UNSUPPORTED; + struct efi_var_entry *var; + efi_uintn_t ret; + bool append, delete; + u64 time = 0; + + if (!IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) + return EFI_UNSUPPORTED; + + /* + * Authenticated variables are not supported. The EFI spec + * in ยง32.3.6 requires keys to be stored in non-volatile storage which + * is tamper and delete resistant. + * The rest of the checks are in setvariable_allowed() + */ + if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) + return EFI_INVALID_PARAMETER; + /* BS only variables are hidden deny writing them */ + if (!(attributes & EFI_VARIABLE_RUNTIME_ACCESS)) + return EFI_INVALID_PARAMETER; + + ret = setvariable_allowed(variable_name, vendor, attributes, data_size, + data); + if (ret != EFI_SUCCESS) + return ret; + + /* check if a variable exists */ + var = efi_var_mem_find(vendor, variable_name, NULL); + append = !!(attributes & EFI_VARIABLE_APPEND_WRITE); + attributes &= ~EFI_VARIABLE_APPEND_WRITE; + delete = !append && (!data_size || !attributes); + + if (var) { + if (var->attr & EFI_VARIABLE_READ_ONLY || + !(var->attr & EFI_VARIABLE_NON_VOLATILE)) + return EFI_WRITE_PROTECTED; + + /* attributes won't be changed */ + if (!delete && (((var->attr & ~EFI_VARIABLE_READ_ONLY) != + (attributes & ~EFI_VARIABLE_READ_ONLY)))) + return EFI_INVALID_PARAMETER; + time = var->time; + } else { + if (!(attributes & EFI_VARIABLE_NON_VOLATILE)) + return EFI_INVALID_PARAMETER; + if (append && !data_size) + return EFI_SUCCESS; + if (delete) + return EFI_NOT_FOUND; + } + + if (delete) { + /* EFI_NOT_FOUND has been handled before */ + attributes = var->attr; + ret = EFI_SUCCESS; + } else if (append && var) { + u16 *old_data = (void *)((uintptr_t)var->name + + sizeof(u16) * (u16_strlen(var->name) + 1)); + + ret = efi_var_mem_ins(variable_name, vendor, attributes, + var->length, old_data, data_size, data, + time); + } else { + ret = efi_var_mem_ins(variable_name, vendor, attributes, + data_size, data, 0, NULL, time); + } + + if (ret != EFI_SUCCESS) + return ret; + /* We are always inserting new variables, get rid of the old copy */ + efi_var_mem_del(var); + + return EFI_SUCCESS; } /** diff --git a/lib/efi_selftest/efi_selftest_variables_runtime.c b/lib/efi_selftest/efi_selftest_variables_runtime.c index 4700d9424105..4c9405c0a7c7 100644 --- a/lib/efi_selftest/efi_selftest_variables_runtime.c +++ b/lib/efi_selftest/efi_selftest_variables_runtime.c @@ -62,9 +62,16 @@ static int execute(void) EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, 3, v + 4); - if (ret != EFI_UNSUPPORTED) { - efi_st_error("SetVariable failed\n"); - return EFI_ST_FAILURE; + if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) { + if (ret != EFI_INVALID_PARAMETER) { + efi_st_error("SetVariable failed\n"); + return EFI_ST_FAILURE; + } + } else { + if (ret != EFI_UNSUPPORTED) { + efi_st_error("SetVariable failed\n"); + return EFI_ST_FAILURE; + } } len = EFI_ST_MAX_DATA_SIZE; ret = runtime->get_variable(u"PlatformLangCodes", &guid_vendor0, From patchwork Wed Apr 17 10:19:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 789310 Delivered-To: patch@linaro.org Received: by 2002:a5d:4dc5:0:b0:346:15ad:a2a with SMTP id f5csp204575wru; Wed, 17 Apr 2024 03:20:01 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWEohNN2Vrq/fOufrazv4nQyD2AZ5th32dUqBw8v4VGA3iOCUiV1NPohY65TOAP0LjtJlxnxQ3xdEgaXG/rrApS X-Google-Smtp-Source: AGHT+IGro7ezhLPNzKXD1vl4sudYYovctna++a0VvWRO9XoR+b1ua37IzRzYsFRpnpq2AdVS53se X-Received: by 2002:a17:907:724b:b0:a52:5a04:fb0d with SMTP id ds11-20020a170907724b00b00a525a04fb0dmr6960418ejc.3.1713349201252; Wed, 17 Apr 2024 03:20:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713349201; cv=none; d=google.com; s=arc-20160816; b=um2K9817llSknnxrsYorhwrv6dMYRk0/AskmAomA5PoE+qEsxsnoDVBHZ22eiWtCvw iCdyzZF6w2Bdo2nCpzmAqE+Ob47k+fo1wiwDORpA1jBNgYENXXwwezhBlAYkEs/IN7U+ mjl0mNE9ahrym4leQg9nV9zgcUmiL8wH69acrdv/Lb/GTQhVuJrVWkGfHt9ZEnGm7Q76 4KnbtmCJJ4s3qkazuYuaftS8GiqGzwKsZap5+Wb5FF4PY6g3ii++h52JrDe6SVSah85+ xObwC3zgZWHZKrjnVPTXY4SS3FaegjjyaF/QoUTD1ob1mTd6rekLLOT5x6xZetWpjkxg 5gZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=chYdKJSIqCPZ5ji7MNGV72oV6gQgjYtrYtQPdRJyYPA=; fh=ur5iMzZiWL/zwG/0DDxl8+DIdxvyRZrdgA7yGJ2rS8Q=; b=sgDnHJklwZjsvvbXZV/5MlZQ6otW95syb3Rsz0TjFL1awAqAGIR9mbuMJHPRkwHpNW gyi3bB/XPEp8C6I3qhSsOUHDi1zEFqC+GXrBW6Ix2ifTH/0gRtE0fnehCq/INqwiymDS Ghp+b8cVAY2cK5pwTxAaL67MZmxWvHwDw31gEJEoo6QLSgcVX3BcNLGtueZw0a+Pi9XP h1/89QRXON+zUVpGWT0sKlMe8FjEORaQUouoot1F1qUhYqnwKCcL5sLGtrfh6Nvi+Kar idLY5Ijr2PTHtYbGoSF3HDmmpznfnae/NJBoNvPAqEO9q7jsX+e12uEvY5fPu0Huv3Wm kroQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oJ3qbwHK; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ka8-20020a170907990800b00a525ea63c7dsi3600967ejc.779.2024.04.17.03.20.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:20:01 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oJ3qbwHK; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2F0E788464; Wed, 17 Apr 2024 12:19:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="oJ3qbwHK"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7EC6D88461; Wed, 17 Apr 2024 12:19:49 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9866E8842A for ; Wed, 17 Apr 2024 12:19:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-418ce99e7fcso134095e9.1 for ; Wed, 17 Apr 2024 03:19:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713349185; x=1713953985; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=chYdKJSIqCPZ5ji7MNGV72oV6gQgjYtrYtQPdRJyYPA=; b=oJ3qbwHKMyrpHFlnadXV+mO+7vQDx+vsNiuIIJ2GOMHobjklN1YK4aHIdyKJSq+xXy kUjahyANZebAe+wbJ6YI5li5RTGXM3PMvUFieVh9YHT4rW/7iPOmFmPnNVzBMS5nDLJo nKivdBvXU8E9j4etBJU9vnzSL4/RTjB7UitOT1DNfxEW8WAR2s/TsKhYsTkURxeYgA/8 8TvaJtdOje19YDNN4VdVE/nXfgm7L2sww/OKWys5HBDdeyzxf4Eg6jOSWfv1FXJVQglT 7p6uRN/UYLpTugpTVwrJeHJB592bQEWgxiiffrPNKI5To/AJksN0LzU3YmFOX6HlswAt 5PSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713349185; x=1713953985; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=chYdKJSIqCPZ5ji7MNGV72oV6gQgjYtrYtQPdRJyYPA=; b=d+VgpUAdZMlVBQdqcOy9K826OYGgNC8h/GZ9x6HNtGtnajRanzm1P9CgCTYKo39keH CaIGcly4Lc/YyHj6PKz65PoOX/XKhYNwmML7pRaS2BHk9GzN5MOLYFp5NWCz7+8HerbK 7Hgb7N1Xn24yc83wcIhPbIKnBysnz4eX/JSovfgBaOBnXpKYJ/PNYmpfuFuu22y5gOm2 p5vPN3WwO0uJa8iLw0uCxB9yAMB/6uWZIms/u8aI9Bvzgy9ZfrKH47eJw+LtiAk13jIL lx54mGE6AIumFKe3azaVGNyXp1mDY/ucIAW0A4cUgCDXkj7WGgldYG9VlF+RVL3w+mSt 1z2g== X-Forwarded-Encrypted: i=1; AJvYcCWZ0wk+xxQFvx7l2SWWzew9gj2Xc0JLopYzPam30AYB45wuv0n4zwygXFYth6hP6TmGBQHfguNB8aRZixKlUZVeQRSvrw== X-Gm-Message-State: AOJu0YwtZQY2f+YA5i0b1X0EgBlSbZ4Dx4jJEzh6hWZkmAlwyF4XWxBF +uiGzL81AyBsbJan5CkAI5cJKQkDULFmpOOBwUGKwjVrhQ7HB42PYNHn+dMdgfY= X-Received: by 2002:a05:6000:1ace:b0:346:251a:396d with SMTP id i14-20020a0560001ace00b00346251a396dmr13149525wry.51.1713349184914; Wed, 17 Apr 2024 03:19:44 -0700 (PDT) Received: from hades.. (ppp089210071137.access.hol.gr. [89.210.71.137]) by smtp.gmail.com with ESMTPSA id g12-20020a5d488c000000b003472489d26fsm13567780wrq.19.2024.04.17.03.19.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:19:44 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de, kettenis@openbsd.org Cc: caleb.connolly@linaro.org, sumit.garg@linaro.org, quic_llindhol@quicinc.com, ardb@kernel.org, pbrobinson@gmail.com, pjones@redhat.com, Ilias Apalodimas , Tom Rini , Masahisa Kojima , AKASHI Takahiro , Raymond Mao , Simon Glass , Janne Grunau , Matthias Schiffer , Abdellatif El Khlifi , Alper Nebi Yasak , Sughosh Ganu , Richard Henderson , Sam Edwards , Weizhao Ouyang , u-boot@lists.denx.de Subject: [PATCH v2 2/4] efi_loader: Add OS notifications for SetVariable at runtime Date: Wed, 17 Apr 2024 13:19:23 +0300 Message-Id: <20240417101928.119115-3-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240417101928.119115-1-ilias.apalodimas@linaro.org> References: <20240417101928.119115-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Previous patches enable SetVariable at runtime using a volatile storage backend using EFI_RUNTIME_SERVICES_DATA allocared memory. Since there's no recommendation from the spec on how to notify the OS, add a volatile EFI variable that contains the filename relative to the ESP. OS'es can use that file and update it at runtime $~ efivar -p -n b2ac5fc9-92b7-4acd-aeac-11e818c3130c-RTStorageVolatile GUID: b2ac5fc9-92b7-4acd-aeac-11e818c3130c Name: "RTStorageVolatile" Attributes: Boot Service Access Runtime Service Access Value: 00000000 75 62 6f 6f 74 65 66 69 2e 76 61 72 00 |ubootefi.var. | Signed-off-by: Ilias Apalodimas Reviewed-by: Heinrich Schuchardt --- include/efi_loader.h | 4 ++++ lib/efi_loader/efi_runtime.c | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/efi_loader.h b/include/efi_loader.h index bb51c0281774..69442f4e58de 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -159,6 +159,10 @@ static inline void efi_set_bootdev(const char *dev, const char *devnr, #define EFICONFIG_AUTO_GENERATED_ENTRY_GUID \ EFI_GUID(0x8108ac4e, 0x9f11, 0x4d59, \ 0x85, 0x0e, 0xe2, 0x1a, 0x52, 0x2c, 0x59, 0xb2) +#define U_BOOT_EFI_RT_VAR_FILE_GUID \ + EFI_GUID(0xb2ac5fc9, 0x92b7, 0x4acd, \ + 0xae, 0xac, 0x11, 0xe8, 0x18, 0xc3, 0x13, 0x0c) + /* Use internal device tree when starting UEFI application */ #define EFI_FDT_USE_INTERNAL NULL diff --git a/lib/efi_loader/efi_runtime.c b/lib/efi_loader/efi_runtime.c index dde083b09665..c8f7a88ba8db 100644 --- a/lib/efi_loader/efi_runtime.c +++ b/lib/efi_loader/efi_runtime.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -110,6 +111,7 @@ static __efi_runtime_data efi_uintn_t efi_descriptor_size; */ efi_status_t efi_init_runtime_supported(void) { + const efi_guid_t efi_guid_efi_rt_var_file = U_BOOT_EFI_RT_VAR_FILE_GUID; efi_status_t ret; struct efi_rt_properties_table *rt_table; @@ -127,9 +129,20 @@ efi_status_t efi_init_runtime_supported(void) EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP | EFI_RT_SUPPORTED_CONVERT_POINTER; - if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) - rt_table->runtime_services_supported |= - EFI_RT_SUPPORTED_SET_VARIABLE; + if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) { + ret = efi_set_variable_int(u"RTStorageVolatile", + &efi_guid_efi_rt_var_file, + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_READ_ONLY, + sizeof(EFI_VAR_FILE_NAME), + EFI_VAR_FILE_NAME, false); + if (ret != EFI_SUCCESS) { + log_err("Failed to set RTStorageVolatile\n"); + return ret; + } + rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_SET_VARIABLE; + } /* * This value must be synced with efi_runtime_detach_list From patchwork Wed Apr 17 10:19:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 789311 Delivered-To: patch@linaro.org Received: by 2002:a5d:4dc5:0:b0:346:15ad:a2a with SMTP id f5csp204628wru; Wed, 17 Apr 2024 03:20:11 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXGHENx2hf3igxFwwVNSezDHHY66DQEXY9BVBw8AVBmXpoxcErYAJ/Fpx6Ce8LRNd7MkYbDtyNzDXTcRPFrBFi6 X-Google-Smtp-Source: AGHT+IGiRV9TqzKEehlYY6C4L7HYSXdmvIY2UiyFWkq8pwoxnvcurHN8O7KnQpWHfiyDiYX1LvxO X-Received: by 2002:a05:6512:312b:b0:516:cd83:71ce with SMTP id p11-20020a056512312b00b00516cd8371cemr9521795lfd.31.1713349211532; Wed, 17 Apr 2024 03:20:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713349211; cv=none; d=google.com; s=arc-20160816; b=yJJnox4/QfFK0snU8gaPp1DFnvNvD7P2yeQM3CCUlBA8p1HzspVQQn/S3bx30/pnDj e80t2t4b9r5DcJJwy8+84YYA8Y13/SROuaK/UxYlW4YsXMb8YphNutVRCtDo3Aih44nR gA7SP4D7CveQB+fRs+V4D+D6W2rVn/vBMHlfbntTotdfwuQyhqzETDq8Jlc8TOjtmM4E YZPUBZZAwTsJAbl2YU5WPPDhlSH9sp3RTX4Fx3i5ZRYfiM11+NM9jPLOKyYCcOA8JpxA Or/rwfSgO2BRteG1cI5U2qwmOlbwt9GwMXVcjGyQFY/FfCCN2RWLVZIHw0DzER6jxWnC eFCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pMd8OlxBtIIcuMk5RTxGzDKRyct5SClUuz0w1Aajvpk=; fh=Zcym5C61qBvHF+tw45XpEoCDW5Acr1d8Oa0JB3FnPzM=; b=qCaUJlndXrYD2Ls84JZ3ECiqLPFkCfozs00ojmtvSmKEVYsKTfKxkdnjQxe/B4AfGg USlji4sNtCChlWVXnWAFek4KdJRRezW9fWJ2YpvPU+SKRPinttJaVP1kiTTfEiL0yAcR M+n/2zf2zucrN0IgazoYWscHr1DsrKJotKHeotJio6obQeSnQbPs7yuB4TVZBKG6GGzY ffMLI8alaGIr8ZyRhM5yvO0IBHWzZv3hoRRCFlHgIXOHw2dZhLiv14UI6LHYDen+EsZ2 T6mmrVkjOFbyoNa1h71JJGiSsk5q0MbNuV6yo5mhiWzT5tOZR4cN+VzXWcCRNbnxfc84 nQLg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yREKFCcz; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id f28-20020a50a6dc000000b0056e0967bfa6si6696227edc.215.2024.04.17.03.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:20:11 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yREKFCcz; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8B8498846F; Wed, 17 Apr 2024 12:19:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="yREKFCcz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B935B88473; Wed, 17 Apr 2024 12:19:53 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 2E38088461 for ; Wed, 17 Apr 2024 12:19:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-349545c3eb8so1067712f8f.2 for ; Wed, 17 Apr 2024 03:19:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713349190; x=1713953990; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pMd8OlxBtIIcuMk5RTxGzDKRyct5SClUuz0w1Aajvpk=; b=yREKFCcztr/uj1SYsc6oc8NzLqnezuPB3janjAccjd7ls1lEi3qyYNregdt0bde5O1 611vU/lct56bUmI1p3S8RGbVK5HD7J9zOMQRNUPGqIXUj9xX2XSbp6gg8hXDNOgfzG6L UNPhRUSM4Pkth/BaIbJ5vf5d0/WUFC3elf843OIMXrUN4j8xIDxoVujcaeXTLTzOzZqk dRogo2pY5NLLhF31v74ZLLGJLn+6PWcbVTlA5fQdhDvdvYSdI2C/lWLReO2B2+VTQ68f 85dVJc7TsYnF/ftSHYecdKiiHcwxY4O+FMWuTQc5V+b+WwrY1ymi8gcD9oBbF7P+ZFHP ne0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713349190; x=1713953990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pMd8OlxBtIIcuMk5RTxGzDKRyct5SClUuz0w1Aajvpk=; b=V/ivWXBm4NEXZiudbxUcon2QEqrunFDxrv0GYxzQeJeEwyfgQz2a5tDYFTnl3MXePq RZk9O+fzHBwx677kAIUj4fBiAfLgyiMBZaWE9Gv1dTnH/APOixqrxT08QyZQ0Zlu8zl1 /cqxHBw4mTaCSZwVxVP9kleYrYPCijBZQhXtjYin9T3mElxs5FpYINfI1M3WHE5i+0rM 85sTVegKXoSf1hgsmKOgGxvl7fGb0iqdBlVHK+CuN4p3cV49M4tFrqj6Dww/aS0c7N7d h7NXLltfysycUjilRXDePrjkCgh0fzgg4u+ectzwvDprVCkwpFlkboE+zULyR6ZoXP83 39yw== X-Forwarded-Encrypted: i=1; AJvYcCUoR2S+rgtHoskCUm90SE+oCc5K2o073B3QHu38jgdYr5nVzTiQ1BjlhUYp4/SFTZ0qMrFuv0GX+O/PzrSRrtKTC+Sfqw== X-Gm-Message-State: AOJu0Yx23MRFElcx0fEf+KRNv5xyy6j0X4Kjttsvm9BbMianqnHwlTug yIrdjrh0Y9I8qxYXFdF21xIsGro/+dOCmPB5grlarhnNoB0Lo9kO1tOZ/2DRlxs= X-Received: by 2002:a5d:5f90:0:b0:341:a640:b516 with SMTP id dr16-20020a5d5f90000000b00341a640b516mr12198352wrb.70.1713349190508; Wed, 17 Apr 2024 03:19:50 -0700 (PDT) Received: from hades.. (ppp089210071137.access.hol.gr. [89.210.71.137]) by smtp.gmail.com with ESMTPSA id g12-20020a5d488c000000b003472489d26fsm13567780wrq.19.2024.04.17.03.19.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:19:49 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de, kettenis@openbsd.org Cc: caleb.connolly@linaro.org, sumit.garg@linaro.org, quic_llindhol@quicinc.com, ardb@kernel.org, pbrobinson@gmail.com, pjones@redhat.com, Ilias Apalodimas , Heinrich Schuchardt , Tom Rini , Masahisa Kojima , AKASHI Takahiro , Raymond Mao , Matthias Schiffer , Simon Glass , Janne Grunau , Abdellatif El Khlifi , Sughosh Ganu , Richard Henderson , Sam Edwards , Alper Nebi Yasak , Weizhao Ouyang , u-boot@lists.denx.de Subject: [PATCH v2 3/4] efi_loader: add an EFI variable with the file contents Date: Wed, 17 Apr 2024 13:19:24 +0300 Message-Id: <20240417101928.119115-4-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240417101928.119115-1-ilias.apalodimas@linaro.org> References: <20240417101928.119115-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Previous patches enabled SetVariableRT using a RAM backend. Although EBBR [0] defines a variable format we can teach userspace tools and write the altered variables, it's better if we skip the ABI requirements completely. So let's add a new variable, in its own namespace called "VarToFile" which contains a binary dump of the updated RT, BS and, NV variables and will be updated when GetVariable is called. Some adjustments are needed to do that. Currently we discard BS-only variables in EBS(). We need to preserve those on the RAM backend that exposes the variables. Since BS-only variables can't appear at runtime we need to move the memory masking checks from efi_var_collect() to efi_get_next_variable_name_mem()/ efi_get_variable_mem() and do the filtering at runtime. We also need an efi_var_collect() variant available at runtime, in order to construct the "VarToFile" buffer on the fly. All users and applications (for linux) have to do when updating a variable is dd that variable in the file described by "RTStorageVolatile". Linux efivarfs uses a first 4 bytes of the output to represent attributes in little-endian format. So, storing variables works like this: $~ efibootmgr -n 0001 $~ dd if=/sys/firmware/efi/efivars/VarToFile-b2ac5fc9-92b7-4acd-aeac-11e818c3130c of=/boot/efi/ubootefi.var skip=4 bs=1 [0] https://arm-software.github.io/ebbr/index.html#document-chapter5-variable-storage Co-developed-by: Heinrich Schuchardt Signed-off-by: Heinrich Schuchardt Signed-off-by: Ilias Apalodimas --- include/efi_variable.h | 14 ++- lib/charset.c | 2 +- lib/efi_loader/efi_runtime.c | 19 ++++ lib/efi_loader/efi_var_common.c | 6 +- lib/efi_loader/efi_var_mem.c | 146 ++++++++++++++++++------------ lib/efi_loader/efi_variable.c | 6 +- lib/efi_loader/efi_variable_tee.c | 5 - 7 files changed, 130 insertions(+), 68 deletions(-) diff --git a/include/efi_variable.h b/include/efi_variable.h index 42a2b7c52bef..b545a36aac50 100644 --- a/include/efi_variable.h +++ b/include/efi_variable.h @@ -271,13 +271,16 @@ const efi_guid_t *efi_auth_var_get_guid(const u16 *name); * * @variable_name_size: size of variable_name buffer in bytes * @variable_name: name of uefi variable's name in u16 + * @mask: bitmask with required attributes of variables to be collected. + * variables are only collected if all of the required + * attributes match. Use 0 to skip matching * @vendor: vendor's guid * * Return: status code */ efi_status_t __efi_runtime efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, u16 *variable_name, - efi_guid_t *vendor); + efi_guid_t *vendor, u32 mask); /** * efi_get_variable_mem() - Runtime common code across efi variable * implementations for GetVariable() from @@ -289,12 +292,15 @@ efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, u16 *variable_na * @data_size: size of the buffer to which the variable value is copied * @data: buffer to which the variable value is copied * @timep: authentication time (seconds since start of epoch) + * @mask: bitmask with required attributes of variables to be collected. + * variables are only collected if all of the required + * attributes match. Use 0 to skip matching * Return: status code */ efi_status_t __efi_runtime efi_get_variable_mem(const u16 *variable_name, const efi_guid_t *vendor, u32 *attributes, efi_uintn_t *data_size, void *data, - u64 *timep); + u64 *timep, u32 mask); /** * efi_get_variable_runtime() - runtime implementation of GetVariable() @@ -334,4 +340,8 @@ efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size, */ void efi_var_buf_update(struct efi_var_file *var_buf); +efi_status_t __efi_runtime efi_var_collect_mem(struct efi_var_file *buf, + efi_uintn_t *lenp, + u32 check_attr_mask); + #endif diff --git a/lib/charset.c b/lib/charset.c index df4f04074852..182c92a50c48 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -387,7 +387,7 @@ int u16_strcasecmp(const u16 *s1, const u16 *s2) * > 0 if the first different u16 in s1 is greater than the * corresponding u16 in s2 */ -int u16_strncmp(const u16 *s1, const u16 *s2, size_t n) +int __efi_runtime u16_strncmp(const u16 *s1, const u16 *s2, size_t n) { int ret = 0; diff --git a/lib/efi_loader/efi_runtime.c b/lib/efi_loader/efi_runtime.c index c8f7a88ba8db..99ad1f35d8f1 100644 --- a/lib/efi_loader/efi_runtime.c +++ b/lib/efi_loader/efi_runtime.c @@ -130,6 +130,8 @@ efi_status_t efi_init_runtime_supported(void) EFI_RT_SUPPORTED_CONVERT_POINTER; if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) { + int s = 0; + ret = efi_set_variable_int(u"RTStorageVolatile", &efi_guid_efi_rt_var_file, EFI_VARIABLE_BOOTSERVICE_ACCESS | @@ -141,6 +143,23 @@ efi_status_t efi_init_runtime_supported(void) log_err("Failed to set RTStorageVolatile\n"); return ret; } + ret = efi_set_variable_int(u"VarToFile", + &efi_guid_efi_rt_var_file, + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS, + sizeof(s), + &s, false); + if (ret != EFI_SUCCESS) { + log_err("Failed to set VarToFile\n"); + efi_set_variable_int(u"RTStorageVolatile", + &efi_guid_efi_rt_var_file, + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_READ_ONLY, + 0, NULL, false); + + return ret; + } rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_SET_VARIABLE; } diff --git a/lib/efi_loader/efi_var_common.c b/lib/efi_loader/efi_var_common.c index aa8feffd3ec1..7862f2d6ce8a 100644 --- a/lib/efi_loader/efi_var_common.c +++ b/lib/efi_loader/efi_var_common.c @@ -182,7 +182,8 @@ efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid, { efi_status_t ret; - ret = efi_get_variable_mem(variable_name, guid, attributes, data_size, data, NULL); + ret = efi_get_variable_mem(variable_name, guid, attributes, data_size, + data, NULL, EFI_VARIABLE_RUNTIME_ACCESS); /* Remove EFI_VARIABLE_READ_ONLY flag */ if (attributes) @@ -195,7 +196,8 @@ efi_status_t __efi_runtime EFIAPI efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size, u16 *variable_name, efi_guid_t *guid) { - return efi_get_next_variable_name_mem(variable_name_size, variable_name, guid); + return efi_get_next_variable_name_mem(variable_name_size, variable_name, + guid, EFI_VARIABLE_RUNTIME_ACCESS); } /** diff --git a/lib/efi_loader/efi_var_mem.c b/lib/efi_loader/efi_var_mem.c index 6c21cec5d457..65ab858c926e 100644 --- a/lib/efi_loader/efi_var_mem.c +++ b/lib/efi_loader/efi_var_mem.c @@ -36,9 +36,11 @@ efi_var_mem_compare(struct efi_var_entry *var, const efi_guid_t *guid, const u16 *data, *var_name; bool match = true; - for (guid1 = (u8 *)&var->guid, guid2 = (u8 *)guid, i = 0; - i < sizeof(efi_guid_t) && match; ++i) - match = (guid1[i] == guid2[i]); + if (guid) { + for (guid1 = (u8 *)&var->guid, guid2 = (u8 *)guid, i = 0; + i < sizeof(efi_guid_t) && match; ++i) + match = (guid1[i] == guid2[i]); + } for (data = var->name, var_name = name;; ++data) { if (match) @@ -184,53 +186,6 @@ u64 __efi_runtime efi_var_mem_free(void) sizeof(struct efi_var_entry); } -/** - * efi_var_mem_bs_del() - delete boot service only variables - */ -static void efi_var_mem_bs_del(void) -{ - struct efi_var_entry *var = efi_var_buf->var; - - for (;;) { - struct efi_var_entry *last; - - last = (struct efi_var_entry *) - ((uintptr_t)efi_var_buf + efi_var_buf->length); - if (var >= last) - break; - if (var->attr & EFI_VARIABLE_RUNTIME_ACCESS) { - u16 *data; - - /* skip variable */ - for (data = var->name; *data; ++data) - ; - ++data; - var = (struct efi_var_entry *) - ALIGN((uintptr_t)data + var->length, 8); - } else { - /* delete variable */ - efi_var_mem_del(var); - } - } -} - -/** - * efi_var_mem_notify_exit_boot_services() - ExitBootService callback - * - * @event: callback event - * @context: callback context - */ -static void EFIAPI -efi_var_mem_notify_exit_boot_services(struct efi_event *event, void *context) -{ - EFI_ENTRY("%p, %p", event, context); - - /* Delete boot service only variables */ - efi_var_mem_bs_del(); - - EFI_EXIT(EFI_SUCCESS); -} - /** * efi_var_mem_notify_exit_boot_services() - SetVirtualMemoryMap callback * @@ -261,11 +216,7 @@ efi_status_t efi_var_mem_init(void) efi_var_buf->magic = EFI_VAR_FILE_MAGIC; efi_var_buf->length = (uintptr_t)efi_var_buf->var - (uintptr_t)efi_var_buf; - /* crc32 for 0 bytes = 0 */ - ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, - efi_var_mem_notify_exit_boot_services, NULL, - NULL, &event); if (ret != EFI_SUCCESS) return ret; ret = efi_create_event(EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, TPL_CALLBACK, @@ -276,10 +227,75 @@ efi_status_t efi_var_mem_init(void) return ret; } +/** + * efi_var_collect_mem() - Copy EFI variables matching attributes mask from + * efi_var_buf + * + * @buf: buffer containing variable collection + * @lenp: buffer length + * @mask: mask of matched attributes + * + * Return: Status code + */ +efi_status_t __efi_runtime +efi_var_collect_mem(struct efi_var_file *buf, efi_uintn_t *lenp, u32 mask) +{ + static struct efi_var_file __efi_runtime_data hdr = { + .magic = EFI_VAR_FILE_MAGIC, + }; + struct efi_var_entry *last, *var, *var_to; + + hdr.length = sizeof(struct efi_var_file); + + var = efi_var_buf->var; + last = (struct efi_var_entry *) + ((uintptr_t)efi_var_buf + efi_var_buf->length); + if (buf) + var_to = buf->var; + + while (var < last) { + u32 len; + struct efi_var_entry *var_next; + + efi_var_mem_compare(var, NULL, u"", &var_next); + len = (uintptr_t)var_next - (uintptr_t)var; + + if ((var->attr & mask) != mask) { + var = (void *)var + len; + continue; + } + + hdr.length += len; + + if (buf && hdr.length <= *lenp) { + efi_memcpy_runtime(var_to, var, len); + var_to = (void *)var_to + len; + } + var = (void *)var + len; + } + + if (!buf && hdr.length <= *lenp) { + *lenp = hdr.length; + return EFI_INVALID_PARAMETER; + } + + if (!buf || hdr.length > *lenp) { + *lenp = hdr.length; + return EFI_BUFFER_TOO_SMALL; + } + hdr.crc32 = crc32(0, (u8 *)buf->var, + hdr.length - sizeof(struct efi_var_file)); + + efi_memcpy_runtime(buf, &hdr, sizeof(hdr)); + *lenp = hdr.length; + + return EFI_SUCCESS; +} + efi_status_t __efi_runtime efi_get_variable_mem(const u16 *variable_name, const efi_guid_t *vendor, u32 *attributes, efi_uintn_t *data_size, void *data, - u64 *timep) + u64 *timep, u32 mask) { efi_uintn_t old_size; struct efi_var_entry *var; @@ -291,11 +307,22 @@ efi_get_variable_mem(const u16 *variable_name, const efi_guid_t *vendor, if (!var) return EFI_NOT_FOUND; + /* + * This function is used at runtime to dump EFI variables. + * The memory backend we keep around has BS-only variables as + * well. At runtime we filter them here + */ + if (mask && !((var->attr & mask) == mask)) + return EFI_NOT_FOUND; + if (attributes) *attributes = var->attr; if (timep) *timep = var->time; + if (!u16_strcmp(variable_name, u"VarToFile")) + return efi_var_collect_mem(data, data_size, EFI_VARIABLE_NON_VOLATILE); + old_size = *data_size; *data_size = var->length; if (old_size < var->length) @@ -315,7 +342,8 @@ efi_get_variable_mem(const u16 *variable_name, const efi_guid_t *vendor, efi_status_t __efi_runtime efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, - u16 *variable_name, efi_guid_t *vendor) + u16 *variable_name, efi_guid_t *vendor, + u32 mask) { struct efi_var_entry *var; efi_uintn_t len, old_size; @@ -324,6 +352,7 @@ efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, if (!variable_name_size || !variable_name || !vendor) return EFI_INVALID_PARAMETER; +skip: len = *variable_name_size >> 1; if (u16_strnlen(variable_name, len) == len) return EFI_INVALID_PARAMETER; @@ -347,6 +376,11 @@ efi_get_next_variable_name_mem(efi_uintn_t *variable_name_size, efi_memcpy_runtime(variable_name, var->name, *variable_name_size); efi_memcpy_runtime(vendor, &var->guid, sizeof(efi_guid_t)); + if (mask && !((var->attr & mask) == mask)) { + *variable_name_size = old_size; + goto skip; + } + return EFI_SUCCESS; } diff --git a/lib/efi_loader/efi_variable.c b/lib/efi_loader/efi_variable.c index abc2a3402f42..4aaa05a617d7 100644 --- a/lib/efi_loader/efi_variable.c +++ b/lib/efi_loader/efi_variable.c @@ -209,14 +209,16 @@ efi_get_variable_int(const u16 *variable_name, const efi_guid_t *vendor, u32 *attributes, efi_uintn_t *data_size, void *data, u64 *timep) { - return efi_get_variable_mem(variable_name, vendor, attributes, data_size, data, timep); + return efi_get_variable_mem(variable_name, vendor, attributes, data_size, + data, timep, 0); } efi_status_t __efi_runtime efi_get_next_variable_name_int(efi_uintn_t *variable_name_size, u16 *variable_name, efi_guid_t *vendor) { - return efi_get_next_variable_name_mem(variable_name_size, variable_name, vendor); + return efi_get_next_variable_name_mem(variable_name_size, variable_name, + vendor, 0); } /** diff --git a/lib/efi_loader/efi_variable_tee.c b/lib/efi_loader/efi_variable_tee.c index dde135fd9f81..4f1aa298da13 100644 --- a/lib/efi_loader/efi_variable_tee.c +++ b/lib/efi_loader/efi_variable_tee.c @@ -959,11 +959,6 @@ void efi_variables_boot_exit_notify(void) log_err("Unable to notify the MM partition for ExitBootServices\n"); free(comm_buf); - /* - * Populate the list for runtime variables. - * asking EFI_VARIABLE_RUNTIME_ACCESS is redundant, since - * efi_var_mem_notify_exit_boot_services will clean those, but that's fine - */ ret = efi_var_collect(&var_buf, &len, EFI_VARIABLE_RUNTIME_ACCESS); if (ret != EFI_SUCCESS) log_err("Can't populate EFI variables. No runtime variables will be available\n"); From patchwork Wed Apr 17 10:19:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 789312 Delivered-To: patch@linaro.org Received: by 2002:a5d:4dc5:0:b0:346:15ad:a2a with SMTP id f5csp204725wru; Wed, 17 Apr 2024 03:20:22 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUcbPYd2lLOy6/RIF058Frm2HRt4+Ko6IjSFllLHoLdgVwrz8swMpo4CJTjwRlGxH4+0zaPxQ1DTMhHDkaVv6JT X-Google-Smtp-Source: AGHT+IGfn4ZLkFDf5/iUVXdi/xkWyqj2NVYm6NGxWt2Ft7sAMcu4+6kJXRwjtDKyTDcPmTFEwdef X-Received: by 2002:a50:cd83:0:b0:56e:2273:6548 with SMTP id p3-20020a50cd83000000b0056e22736548mr10191758edi.8.1713349222437; Wed, 17 Apr 2024 03:20:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713349222; cv=none; d=google.com; s=arc-20160816; b=uWJtZSaLga2UFndENCYZQt42nlEQwRLraAB0sEskayEDJim1ZwGIp6sM1F5QvRRLDb uDeL6Glloz4mqnINRjn80PpGfOn66WbPGh19jT+YZrksyqmdKnFivzBnde4/Mv1fCLoU IVk/K0fyRW65iUDTrb2qn+eqjqzzu286h8fs0aiYBLbmgWBvzcdo2PATRJXekaHr9RXt k9T5/KceavvYmX8nOMfKVcwkVN+6s6PbgMUO4qsT+UVThmTSa7zGujg2VU51vyXLuZQ3 PyBc36ktl0M8yPLUsmS9At1bixc5s1SsXCPvgrST1A7w6yV6p3bqBxLHg5FMuuBbrj2z mvpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5mbi9CNku5RtmM+MIL6L/pSl5j85cc0WaO0GSnvMbtk=; fh=JBBHjr/RopCk7HeUJN2nEgM+qKO5PM8EJI/WLLoTcKg=; b=ltHZQrl93w3HmmM9CyYX8gkko9/cxWcx3W3mB4eDffJq69IISAxqfKuZ2D1568Q5dw vh0Eyl6i4t0sTFr29sfK/WIepvEuhDmkGgZ4/l0R0nbeCXRuFcqjPgseC7w5i3TcAe0u Y5Mun4cChmZsaQvcrUzJZzGtUfGq8hwEmGZtNLjKTvuRIyBvxFsK1rZlqBYOT74hBKCI iW96meZofUHea9EcH2y0rPlddD11y+rANsdJtCJOO7OtXmBFH470MUBBgyRTAZL3lFnY GtX5LI2GhOhr9NcgoS2TPhQzLEgotUOsD0iqaZBwzMzMgHC6GGICI/Tw/G9XokgozPvI ypng==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ybad0dnA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id j29-20020a508a9d000000b0056fe540f559si6571762edj.95.2024.04.17.03.20.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:20:22 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ybad0dnA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3747888476; Wed, 17 Apr 2024 12:20:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="ybad0dnA"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6249088467; Wed, 17 Apr 2024 12:19:59 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 19BA18847B for ; Wed, 17 Apr 2024 12:19:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-516d487659bso6533137e87.2 for ; Wed, 17 Apr 2024 03:19:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713349195; x=1713953995; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5mbi9CNku5RtmM+MIL6L/pSl5j85cc0WaO0GSnvMbtk=; b=ybad0dnAKmCCm6AVHWu24AxKcxp/3R/8+jEIwhzGMrg4qBk/xXZMxmGK+eo0GGbVYW 5Gc46tK0M9UboMTa1OmqzmYD6o1cly7z3CJ7elGhY4SdJVQxj2IFNEVRAS5+OO+9LGvZ XtJs3TCufC+ml85hyeiRjJDzLFMbCedoJT1x29G8tWEw7vrse+J0CBvQVQA8FW6u6g7F sT6tlN3esVHna+B7DMvj8tk6caFXVwQEMsaYxpaZg11m3gGD5uypbKP4L/HQHpy77Esi tP0y4GZrca8Ibvh35rk85P8hXB78LB/SKpLZazeGdRc+yz/rgvftB/jyaJpEZReP2ne+ G60Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713349195; x=1713953995; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5mbi9CNku5RtmM+MIL6L/pSl5j85cc0WaO0GSnvMbtk=; b=s7BFDdHfiJJmeMQvqcds5E5OsoAbrWpGfVSDeDD14X9ZJz58ZCm7NreANWHQPIgP2s 3ZwKxC8Uhe4wD6AzyddmXbWtDNCcq3K+2usrnL3J0N3ZFsC+d/BLeyQG3xJWTaIdGifT iQD4n2F/F2/YHfDeRTIBenXtuD47HuWCRv4rPZWykHVK9DKbbppFxa3BVNAFs9sUxPed w7ZuBoId6F8arwNY7K/142MAvU8GWhQ+2D4J9gSJbzDNUvNXPXV+VKbGWWgULiycBEyQ 6dLxCKtybtfVRK+xJRyL6PdctXURD99M7Xcc0md4ngj49o4IQSJnIYt2HjMlQ2ytpcXb YdbA== X-Forwarded-Encrypted: i=1; AJvYcCWO2V4bfrJ4mF/D40jz2c8cqn0mV6WKo4PLGoP6q7k3dFlTg55QPMxedDQMB09pX5puj2FHHWVrqnVS8MODUQV0DMMI2Q== X-Gm-Message-State: AOJu0Ywjgt3vyQFuP6rKxE4QYQxUoNgNgHilCDj69yU4qQDgzgp49oC5 38cZVfKyK97I/a1CtzcRCjSHCG2HPeP3LSNiL33LKcHvE+uKW7z4ae+iMiOGpPg= X-Received: by 2002:ac2:4e15:0:b0:519:1eba:6381 with SMTP id e21-20020ac24e15000000b005191eba6381mr3256954lfr.49.1713349195404; Wed, 17 Apr 2024 03:19:55 -0700 (PDT) Received: from hades.. (ppp089210071137.access.hol.gr. [89.210.71.137]) by smtp.gmail.com with ESMTPSA id g12-20020a5d488c000000b003472489d26fsm13567780wrq.19.2024.04.17.03.19.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 03:19:54 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de, kettenis@openbsd.org Cc: caleb.connolly@linaro.org, sumit.garg@linaro.org, quic_llindhol@quicinc.com, ardb@kernel.org, pbrobinson@gmail.com, pjones@redhat.com, Ilias Apalodimas , Tom Rini , Masahisa Kojima , AKASHI Takahiro , Raymond Mao , Matthias Schiffer , Janne Grunau , Simon Glass , Abdellatif El Khlifi , Alper Nebi Yasak , Sughosh Ganu , Sam Edwards , Richard Henderson , Weizhao Ouyang , u-boot@lists.denx.de Subject: [PATCH v2 4/4] efi_selftest: add tests for setvariableRT Date: Wed, 17 Apr 2024 13:19:25 +0300 Message-Id: <20240417101928.119115-5-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240417101928.119115-1-ilias.apalodimas@linaro.org> References: <20240417101928.119115-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Since we support SetVariableRT now add the relevant tests - Search for the RTStorageVolatile and VarToFile variables after EBS - Try to update with invalid variales (BS, RT only) - Try to write a variable bigger than our backend storage - Write a variable that fits and check VarToFile has been updated correclty - Append to the variable and check VarToFile changes - Try to delete VarToFile which is write protected Signed-off-by: Ilias Apalodimas --- .../efi_selftest_variables_runtime.c | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/lib/efi_selftest/efi_selftest_variables_runtime.c b/lib/efi_selftest/efi_selftest_variables_runtime.c index 4c9405c0a7c7..e492b50a43c2 100644 --- a/lib/efi_selftest/efi_selftest_variables_runtime.c +++ b/lib/efi_selftest/efi_selftest_variables_runtime.c @@ -10,6 +10,7 @@ */ #include +#include #define EFI_ST_MAX_DATA_SIZE 16 #define EFI_ST_MAX_VARNAME_SIZE 40 @@ -17,6 +18,8 @@ static struct efi_boot_services *boottime; static struct efi_runtime_services *runtime; static const efi_guid_t guid_vendor0 = EFI_GLOBAL_VARIABLE_GUID; +static const efi_guid_t __efi_runtime_data efi_rt_var_guid = + U_BOOT_EFI_RT_VAR_FILE_GUID; /* * Setup unit test. @@ -45,11 +48,14 @@ static int execute(void) u32 attr; u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c, 0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,}; + u8 v2[CONFIG_EFI_VAR_BUF_SIZE]; u8 data[EFI_ST_MAX_DATA_SIZE]; + u8 data2[CONFIG_EFI_VAR_BUF_SIZE]; u16 varname[EFI_ST_MAX_VARNAME_SIZE]; efi_guid_t guid; u64 max_storage, rem_storage, max_size; + memset(v2, 0x1, sizeof(v2)); ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS, &max_storage, &rem_storage, &max_size); @@ -63,10 +69,107 @@ static int execute(void) EFI_VARIABLE_RUNTIME_ACCESS, 3, v + 4); if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) { + efi_uintn_t prev_len, delta; + if (ret != EFI_INVALID_PARAMETER) { efi_st_error("SetVariable failed\n"); return EFI_ST_FAILURE; } + + len = sizeof(data); + ret = runtime->get_variable(u"RTStorageVolatile", + &efi_rt_var_guid, + &attr, &len, data); + if (ret != EFI_SUCCESS) { + efi_st_error("GetVariable failed\n"); + return EFI_ST_FAILURE; + } + + len = sizeof(data2); + ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid, + &attr, &len, data2); + if (ret != EFI_SUCCESS) { + efi_st_error("GetVariable failed\n"); + return EFI_ST_FAILURE; + } + /* + * VarToFile will size must change once a variable is inserted + * Store it now, we'll use it later + */ + prev_len = len; + ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0, + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_NON_VOLATILE, + sizeof(v2), + v2); + /* + * This will try to update VarToFile as well and must fail, + * without changing or deleting VarToFile + */ + if (ret != EFI_OUT_OF_RESOURCES) { + efi_st_error("SetVariable failed\n"); + return EFI_ST_FAILURE; + } + len = sizeof(data2); + ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid, + &attr, &len, data2); + if (ret != EFI_SUCCESS || prev_len != len) { + efi_st_error("Get/SetVariable failed\n"); + return EFI_ST_FAILURE; + } + + /* SetVariableRT updates VarToFile with efi_st_var0 */ + ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0, + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_NON_VOLATILE, + sizeof(v), v); + if (ret != EFI_SUCCESS) { + efi_st_error("SetVariable failed\n"); + return EFI_ST_FAILURE; + } + len = sizeof(data2); + delta = 2 * (u16_strlen(u"efi_st_var0") + 1) + sizeof(v) + + sizeof(struct efi_var_entry); + ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid, + &attr, &len, data2); + if (ret != EFI_SUCCESS || prev_len + delta != len) { + efi_st_error("Get/SetVariable failed\n"); + return EFI_ST_FAILURE; + } + + /* append on an existing variable will updateVarToFile */ + ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0, + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_APPEND_WRITE | + EFI_VARIABLE_NON_VOLATILE, + sizeof(v), v); + if (ret != EFI_SUCCESS) { + efi_st_error("SetVariable failed\n"); + return EFI_ST_FAILURE; + } + prev_len = len; + delta = sizeof(v); + len = sizeof(data2); + ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid, + &attr, &len, data2); + if (ret != EFI_SUCCESS || prev_len + delta != len) { + efi_st_error("Get/SetVariable failed\n"); + return EFI_ST_FAILURE; + } + + /* Variables that are BS, RT and volatile are RO after EBS */ + ret = runtime->set_variable(u"VarToFile", &efi_rt_var_guid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_NON_VOLATILE, + sizeof(v), v); + if (ret != EFI_WRITE_PROTECTED) { + efi_st_error("Get/SetVariable failed\n"); + return EFI_ST_FAILURE; + } } else { if (ret != EFI_UNSUPPORTED) { efi_st_error("SetVariable failed\n");