From patchwork Fri Mar 5 22:22: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: 393592 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp811542jai; Fri, 5 Mar 2021 14:23:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRxTQFEb7oEeWlf1a662Z7g9v5y/7Pn4sb9jmqpGA7ME2/YakKobInUnvvFnYJuIUkfpSC X-Received: by 2002:a17:906:85b:: with SMTP id f27mr4559980ejd.414.1614983003214; Fri, 05 Mar 2021 14:23:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614983003; cv=none; d=google.com; s=arc-20160816; b=rKUUkwznbGcVpIui3vbIiOd6n00ssJNRXnPazQVWCeSmN0s+FYVAQtO83TKBfRVyzr rRU+6PN23SFlMLioRjz7Ym2xznOrIaopO68aDqt9EZSavP8CsA6NAFhm5m6e3DvM/LCf Xet2XcbMlWAqNrvZ7yEAbCdPKJAKeT3vCVDI8gwRM7xOEGRxY+hxWoDBoMjidTSD46QE FDYr2WQQ4PFtmtF/gQK0MDs1MaGRNyCdFvPZFsTQZ5VZs5n6jEHkzCPHgluQ73rtFFG9 V7uJBYTYdIR2bs78B6cPfNBsem0DVJ5rUduYfMc6RYFWFlGMto2tqPGngp/GOfbRtvQ4 RgqQ== 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=RFQf9WM86z8M4nZY3lXy/h4OYj0BhG3f7ftR+C6YDzI=; b=eZkqlIZmTjNEvUdUs5jrUZJP6paHeWx+pQUkJJDswoZRKUxwDd4aZSUNHr9vbz958k FRwNujlqgKgdgfNR0L3w5krKxa/l51j944Oms5T6zu+84bMQCioLWDMmdyB23MYc8oaX kBSI5Nle+35u7bdQ2cp7G79Wd+KWF8u3PMz8pJD/BDT4NOkloMOtC+gwfEHJ6UZ6VP0k Y72a2YCvgPNdYYkztfFwWgVDWkmcAuep5yRflMePzbAdrVA8Xk6cDUQ+9kULe2JYs+38 AWox7aAJmzdRf2fsu8au5I4JfOZ/0RIFYEKHdZKnkxfcVaKqGr9s/52nWLXIuPek3WW3 E2Uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Vy+f6/iO"; 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 k25si2284834ejq.186.2021.03.05.14.23.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 14:23:23 -0800 (PST) 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="Vy+f6/iO"; 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 68A88826A5; Fri, 5 Mar 2021 23:23:16 +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="Vy+f6/iO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A8D26826AC; Fri, 5 Mar 2021 23:23:14 +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-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) (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 8E5F082462 for ; Fri, 5 Mar 2021 23:23:11 +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-x42e.google.com with SMTP id v15so3735754wrx.4 for ; Fri, 05 Mar 2021 14:23:11 -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=RFQf9WM86z8M4nZY3lXy/h4OYj0BhG3f7ftR+C6YDzI=; b=Vy+f6/iOZ8VjkEoMcOhoMZ5zivPT+rYG4WCN65OvhgdvQaaQ5+nNrkp2BUa+XmWRi3 vgYddTZ27PZL471eXiFzkK/hjWm89+MV4XXS8vvcXY//iz/CwU5YwIfZsloqng/H4Ia7 vjuzAOBMXNbjZm5VSxjPmSVNPRyg/yE92G8zWM0HQbOHvXHRz/Bro42flfh1CBOYjfJc +tdmtf2dTEoQSMlIUBGIomaqggxP8ZyLKG/ynOkhiKDCf0YDYbxwaV241W9xbupr/lle CEhjlzqJipG3QpWPHp5qo93OfEIGbUV5VD8YKlXMu4gFRK78zKjoAb/nLiTpKK+X1/iA ZUjA== 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=RFQf9WM86z8M4nZY3lXy/h4OYj0BhG3f7ftR+C6YDzI=; b=F1kzS5Uopr3nW0lEFHnOIrxdPQ4WY4QEvd/DdJugXVd0Kh5JLdIUlt3tP+lFUeNOfy 8+ooFqNuXyathiV8EBehk7GV/W16dW5jzp1GcB+LP86vlVdrugR/l9OITevDTF3P3ZcY RBc0Hdv0o3ik7d5vPeqL7Or0jqdHA8IG9cjye6DR4MaDbRwytW+UN5pacMCxivQbc73A lcaDJ1nZDawBr4PMKyOkx237xjfBp277I7byjW5pMEsWE+nQvnatDqaNznk1C/101oFZ hfC91f7pWDHs1ej3BUQMpKsI7wj3RL33rxnCVwB7ryu7sbkWlTnwhO59vY+Bff7Cf2+J W3/g== X-Gm-Message-State: AOAM531SQ5iq4lWRWl6+i7Co/iwmj4bArdoVECDH4aRIbqSH2QpxO5c5 Cd/gTr4CyyOQJvy+Mec1cU/QYeqbcgSLLS4k X-Received: by 2002:a5d:67c8:: with SMTP id n8mr11356629wrw.351.1614982991089; Fri, 05 Mar 2021 14:23:11 -0800 (PST) Received: from apalos.home ([2a02:587:4647:e6c6:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id s11sm6493296wme.22.2021.03.05.14.23.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 14:23:10 -0800 (PST) From: Ilias Apalodimas To: xypron.glpk@gmx.de, takahiro.akashi@linaro.org Cc: Ilias Apalodimas , Alexander Graf , Sughosh Ganu , u-boot@lists.denx.de Subject: [PATCH 3/6] efi_loader: Introduce helper functions for EFI Date: Sat, 6 Mar 2021 00:22:59 +0200 Message-Id: <20210305222303.2866284-3-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305222303.2866284-1-ilias.apalodimas@linaro.org> References: <20210305222303.2866284-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_helper.h | 15 +++++ lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_helper.c | 118 ++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 include/efi_helper.h create mode 100644 lib/efi_loader/efi_helper.c -- 2.30.1 diff --git a/include/efi_helper.h b/include/efi_helper.h new file mode 100644 index 000000000000..4980a1bb35d7 --- /dev/null +++ b/include/efi_helper.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (c) 2020, Linaro Limited + */ + +#if !defined _EFI_HELPER_H_ +#define _EFI_HELPER_H + +#include +#include + +void *efi_get_var(u16 *name, const efi_guid_t *vendor, efi_uintn_t *size); +struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid); + +#endif 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_helper.c b/lib/efi_loader/efi_helper.c new file mode 100644 index 000000000000..5437faa3ec49 --- /dev/null +++ b/lib/efi_loader/efi_helper.c @@ -0,0 +1,118 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2020, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * 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; +} + +/** + * create_boot_var_indexed() - 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 create_boot_var_indexed(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 + * + * @guid: Vendor guid of the VenMedia DP + * + * 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; + struct efi_load_option lo; + void *var_value = NULL; + efi_uintn_t size; + efi_status_t ret; + u16 var_name[16]; + + ret = create_boot_var_indexed(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) + return NULL; + + file_path = efi_dp_from_lo(&lo, &size, guid); + if (!file_path) { + log_debug("Missing file path in %ls", var_name); + return NULL; + } + + return file_path; +}