From patchwork Wed Mar 17 19:54:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 403219 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp777492jai; Wed, 17 Mar 2021 12:55:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwCc5QFfWFKfhRaD+XNQih6dhc7gynl9bt9pBBNwodYIjNdIC1Yni4WCSkgRal6hDuKwRHq X-Received: by 2002:a17:906:2c44:: with SMTP id f4mr35997268ejh.508.1616010938177; Wed, 17 Mar 2021 12:55:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616010938; cv=none; d=google.com; s=arc-20160816; b=caL73gjE9nNvouXfN7eLnyitF3PD/jIvOsXPsW6/vrCi+CDVlc7PQ0agPCnMR9NNU2 xuPGEVZQvDUrFleW+EPU3yNIh8geu36toLFMtgPyzb1jDDCzvRgWCinZtO3mML30ozMO NYf3iqGmaylhzqIi1v3+xtbgxekrD4JyStIMtdeyX6/1KJHVm6oDFFk4fLHjqXAHkSum Obvje8Tj1hMZzCNuOrCsoJej8JcQ5fyXaSqLrbb6QOz7oPxKxlLIwDomEPt8ehLpWSrL j4o8jpO0xR0hEVJXKoO9tbpSJJ+xCskOMVlRgFRL+VRR5EJEwyKhlnZaWK5OOa5xnzQv dtZg== 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=VmtQUQMKXOL62uUuonj/OqOi3MylaZv7lYHULieqI3Y=; b=G3Fr6i0xzWt6nWovvW+wKrfeNolX2CPmqSO+UUzAf4RKQLghGeie8ADLIX3o5dkr/5 mbzAtqHv8kK5ari/4aAKOVEyxAWIuP2Fgxf3qiGY4yToYy/06lGep1TCCvl/rP3N6Y79 6Wxeje4NFZ1qG3v8VD6KMnHxIVD49iV53lsJ3c7YEPzCMYBVMHaKcoR4BVy49B5VZUql JWe7GDhkLWGPsq4sHPxWJfM/ojpMYKDPIn8wC/gvc4To0kgCUwbGhfAk9guTZ9O3w0wT R7rQXFd7cIWDJFh/C+DTJRQ4u2TWwijtPfsZyT+zEzDVHwk+ztnXK6bdcowNQxx6Mbdd 7gOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OxCdOsYC; 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 m22si16517223ejr.463.2021.03.17.12.55.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 12:55:38 -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=OxCdOsYC; 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 7589E827AB; Wed, 17 Mar 2021 20:55:24 +0100 (CET) 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="OxCdOsYC"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1FF39827A8; Wed, 17 Mar 2021 20:55:21 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) (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 16E6C80040 for ; Wed, 17 Mar 2021 20:55:17 +0100 (CET) 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-x42b.google.com with SMTP id v11so3079248wro.7 for ; Wed, 17 Mar 2021 12:55:17 -0700 (PDT) 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=VmtQUQMKXOL62uUuonj/OqOi3MylaZv7lYHULieqI3Y=; b=OxCdOsYCNQlWAHw44Wkphg+K++BZHmB/xfocJyPIYsfFvxyLnKBHlepYVOzCOcVAOP N1cHBqNi4aaBSLnWrHdJ9b30ZV6EDFUs5aSJz45idLZdP/uwyYb6KdKhCjy0JmmlRSrY s7pTM3D8AQXeisWpBQunE8fqSaewcwVe6GgNEtePhX62DVHKf5dpvlPToCuXqDvAd9kf EwCNkw/Rc3yMsbeAVQfFREaLUTbu+ujc2BM84Ie80Kfve5mJW05Fq1MtBvm6+X1ZYTko 7F5lICOA46UeD2bWOZxUpYfO73PvOkreTJBl4avMWtbYI5Dl4drxiX4ExuK/CQNesh+D YzVA== 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=VmtQUQMKXOL62uUuonj/OqOi3MylaZv7lYHULieqI3Y=; b=j8Gl6Adih0mTqUooJUIMPZecxO55m/JmJPj/XABaYAhZNfYcqC8un1YL7ajQtSnOqh WmhLg+rjM7nLy5Owip1FH0YqB4wxKMHe6qA9b7Jdj736nKrJjs9O5QpwvRAmnsrV7IN9 PEEf0mfHPJZU1NalwzHYma/csfgtLT/FuKXmvLJduzB+nTOVzf5tJf8A42Vl0vCsKDKc mNuah2SHBA6d4ee0GsyZV9gLzz288ebGJ1CUeyvrJJcwmC8KGi7k0WVhHdyx4BReIB1J NWgUIMfN+HK0nPSqZfVdEStJA3A7MlfHqJA2hQZFAKyxLZrvaIm8BXKNAHFWeda0K1bD Yi4g== X-Gm-Message-State: AOAM531x9Xc7TWR7NzSqoXnsGo0y9KsbvJoW41Y+XwzQ547kxHXDW6z6 EN5fk7RYtar52IsPFD11PMxvog== X-Received: by 2002:a5d:6945:: with SMTP id r5mr5936804wrw.367.1616010916693; Wed, 17 Mar 2021 12:55:16 -0700 (PDT) Received: from localhost.localdomain (ppp-94-64-113-158.home.otenet.gr. [94.64.113.158]) by smtp.gmail.com with ESMTPSA id z82sm3343853wmg.19.2021.03.17.12.55.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 12:55:16 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de Cc: takahiro.akashi@linaro.org, Ilias Apalodimas , Alexander Graf , Sughosh Ganu , u-boot@lists.denx.de Subject: [PATCH 2/5 v4] efi_loader: Add helper functions for EFI Date: Wed, 17 Mar 2021 21:54:59 +0200 Message-Id: <20210317195505.337105-3-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210317195505.337105-1-ilias.apalodimas@linaro.org> References: <20210317195505.337105-1-ilias.apalodimas@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.4 at phobos.denx.de X-Virus-Status: Clean A following patch introduces a different logic for loading initrd's based on the EFI_LOAD_FILE2_PROTOCOL. Since similar logic can be applied in the future for other system files (i.e DTBs), let's add some helper functions which will retrieve and parse file paths stored in EFI variables. Signed-off-by: Ilias Apalodimas --- include/efi_loader.h | 7 +++ lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_file.c | 39 +++++++++++++ lib/efi_loader/efi_helper.c | 98 +++++++++++++++++++++++++++++++++ lib/efi_loader/efi_var_common.c | 33 +++++++++++ 5 files changed, 178 insertions(+) create mode 100644 lib/efi_loader/efi_helper.c -- 2.31.0 diff --git a/include/efi_loader.h b/include/efi_loader.h index 5d534e69bb59..9c227005d13e 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -558,6 +558,11 @@ struct efi_simple_file_system_protocol *efi_simple_file_system( /* open file from device-path: */ struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp); +efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size); + +/* get a device path from a Boot#### option */ +struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid); + /** * efi_size_in_pages() - convert size in bytes to size in pages * @@ -723,6 +728,8 @@ efi_status_t EFIAPI efi_query_variable_info( u64 *remaining_variable_storage_size, u64 *maximum_variable_size); +void *efi_get_var(u16 *name, const efi_guid_t *vendor, efi_uintn_t *size); + /* * See section 3.1.3 in the v2.7 UEFI spec for more details on * the layout of EFI_LOAD_OPTION. In short it is: diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile index 10b42e8847bf..da2741adecfa 100644 --- a/lib/efi_loader/Makefile +++ b/lib/efi_loader/Makefile @@ -23,6 +23,7 @@ endif obj-$(CONFIG_CMD_BOOTEFI_HELLO) += helloworld_efi.o obj-$(CONFIG_CMD_BOOTEFI_BOOTMGR) += efi_bootmgr.o obj-y += efi_boottime.o +obj-y += efi_helper.o obj-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += efi_capsule.o obj-$(CONFIG_EFI_CAPSULE_FIRMWARE) += efi_firmware.o obj-y += efi_console.o diff --git a/lib/efi_loader/efi_file.c b/lib/efi_loader/efi_file.c index 8ece8e71ee1c..204105e25afd 100644 --- a/lib/efi_loader/efi_file.c +++ b/lib/efi_loader/efi_file.c @@ -409,6 +409,45 @@ static efi_status_t efi_get_file_size(struct file_handle *fh, return EFI_SUCCESS; } +/** + * efi_file_size() - Get the size of a file using an EFI file handle + * + * @fh: EFI file handle + * @size: buffer to fill in the discovered size + * + * Return: size of the file + */ +efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size) +{ + struct efi_file_info *info = NULL; + efi_uintn_t bs = 0; + efi_status_t ret; + + *size = 0; + ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs, + info)); + if (ret != EFI_BUFFER_TOO_SMALL) { + ret = EFI_DEVICE_ERROR; + goto out; + } + + info = malloc(bs); + if (!info) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs, + info)); + if (ret != EFI_SUCCESS) + goto out; + + *size = info->file_size; + +out: + free(info); + return ret; +} + static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size, void *buffer) { diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c new file mode 100644 index 000000000000..d03a7364615e --- /dev/null +++ b/lib/efi_loader/efi_helper.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2020, Linaro Limited + */ + +#define LOG_CATEGORY LOGC_EFI +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * efi_create_current_boot_var() - Return Boot#### name were #### is replaced by + * the value of BootCurrent + * + * @var_name: variable name + * @var_name_size: size of var_name + * + * Return: Status code + */ +static efi_status_t efi_create_current_boot_var(u16 var_name[], + size_t var_name_size) +{ + efi_uintn_t boot_current_size; + efi_status_t ret; + u16 boot_current; + u16 *pos; + + boot_current_size = sizeof(boot_current); + ret = efi_get_variable_int(L"BootCurrent", + &efi_global_variable_guid, NULL, + &boot_current_size, &boot_current, NULL); + if (ret != EFI_SUCCESS) + goto out; + + pos = efi_create_indexed_name(var_name, var_name_size, "Boot", + boot_current); + if (!pos) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + +out: + return ret; +} + +/** + * efi_get_dp_from_boot() - Retrieve and return a device path from an EFI + * Boot### variable. + * A boot option may contain an array of device paths. + * We use a VenMedia() with a specific GUID to identify + * the usage of the array members. This function is + * used to extract a specific device path + * + * @guid: vendor GUID of the VenMedia() device path node identifying the + * device path + * + * Return: device path or NULL. Caller must free the returned value + */ +struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid) +{ + struct efi_device_path *file_path = NULL; + struct efi_device_path *tmp = NULL; + struct efi_load_option lo; + void *var_value = NULL; + efi_uintn_t size; + efi_status_t ret; + u16 var_name[16]; + + ret = efi_create_current_boot_var(var_name, sizeof(var_name)); + if (ret != EFI_SUCCESS) + return NULL; + + var_value = efi_get_var(var_name, &efi_global_variable_guid, &size); + if (!var_value) + return NULL; + + ret = efi_deserialize_load_option(&lo, var_value, &size); + if (ret != EFI_SUCCESS) + goto out; + + tmp = efi_dp_from_lo(&lo, &size, guid); + if (!tmp) + goto out; + + /* efi_dp_dup will just return NULL if efi_dp_next is NULL */ + file_path = efi_dp_dup(efi_dp_next(tmp)); + +out: + efi_free_pool(tmp); + free(var_value); + + return file_path; +} diff --git a/lib/efi_loader/efi_var_common.c b/lib/efi_loader/efi_var_common.c index 1c7459266a38..b11ed91a74a4 100644 --- a/lib/efi_loader/efi_var_common.c +++ b/lib/efi_loader/efi_var_common.c @@ -9,6 +9,7 @@ #include #include #include +#include enum efi_secure_mode { EFI_MODE_SETUP, @@ -343,3 +344,35 @@ enum efi_auth_var_type efi_auth_var_get_type(u16 *name, const efi_guid_t *guid) } return EFI_AUTH_VAR_NONE; } + +/** + * efi_get_var() - read value of an EFI variable + * + * @name: variable name + * @start: vendor GUID + * @size: size of allocated buffer + * + * Return: buffer with variable data or NULL + */ +void *efi_get_var(u16 *name, const efi_guid_t *vendor, efi_uintn_t *size) +{ + efi_status_t ret; + void *buf = NULL; + + *size = 0; + ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL); + if (ret == EFI_BUFFER_TOO_SMALL) { + buf = malloc(*size); + if (!buf) + return NULL; + ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL); + } + + if (ret != EFI_SUCCESS) { + free(buf); + *size = 0; + return NULL; + } + + return buf; +}