From patchwork Mon Mar 17 08:33:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 874148 Delivered-To: patch@linaro.org Received: by 2002:a5d:4308:0:b0:38f:210b:807b with SMTP id h8csp1869786wrq; Mon, 17 Mar 2025 01:34:50 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVYjV5Y0Giz6KQkIMk17WHkOL67to2r5TPpV5UY7366rZWdkRgX+TCfbWJR0LarGjcSoen3Kg==@linaro.org X-Google-Smtp-Source: AGHT+IH/++/ZowzXegqSC47yCHVHLohjfUcDkrP8IwnnowEL55OuODptL+llZMEMy2vcuTeLe11Q X-Received: by 2002:a50:99d1:0:b0:5e6:de5:312f with SMTP id 4fb4d7f45d1cf-5e8a0c14582mr8466638a12.23.1742200490288; Mon, 17 Mar 2025 01:34:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742200490; cv=none; d=google.com; s=arc-20240605; b=izUtg0zbfzXKWkPGxtMzYGUMnrMI+93yJ8vWree1FZC+mLamN21b9hMjklsepZqm4o iIbx/5Ts3mXD8IfEKCg5Lb/iVSdHOTgexsvpKkWvU2Hr+mmpCXVpFT0FZgGGu/jO1+FV nZlrP5CbEEcefKi/1pZmNTohcUtpfTP59vjFyBm6zn50F6LcAVa45PO0bN4NUoB9gCEz xXVVjrPbLIEpw5ySFKfIHa6ri0yCDg3ym025crJbyXnddteMAOg2jP2GpqVjc3f9Oind WnZ3OexDRwWw9QoZny90cgAGlDuJThYW4J89ZSwvxL1VA2LRo5r4Fnc6Dc9oAIJgf4tY zk4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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; bh=9+FMpwQbUGKCYyUsJ/3CUsECYu9IJjLLDQ1Rq+mptZw=; fh=Evn5eBx2ytD/hj+tB8Xv1T61prWYrP0E59LRKWAPORI=; b=e1ON0sL2j3BR+XEei8U6yFNDv07B5ovJuRnSpC9wa6eefCMWz/o0mTqRJZT8z3PDTw UXeI8hhSKsDdjjvqp8+I8aPLIKAbTbadMs40hmPH97y4RfI3QFal755HoQC3FvmMhy74 2sk+v3zA/PSWWEc3fQ9wIwFa96I7/JtB7JKXElG9E5sn3ULVj7FSrdnxJDd9FkjZ+K1V ZJ/dDBoqtNV/OcZblT5ieigHw/XYpLAU97XWgmj9LOcIlhCz+my5MIBN/1iu6wur/BfY TqAQgBj1gG6MNyNoTP6AQ9gtAU/t4J1xQYLubkj5xDLCh7ScAHoXjogO2+J6nIkJiXs8 LKrg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; 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=fail (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 4fb4d7f45d1cf-5e816935748si6982835a12.21.2025.03.17.01.34.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Mar 2025 01:34: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; 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=fail (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 BA03D8192A; Mon, 17 Mar 2025 09:34:36 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 86E9881877; Mon, 17 Mar 2025 09:34:35 +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=-1.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 4936F80107 for ; Mon, 17 Mar 2025 09:34:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A16B5169E; Mon, 17 Mar 2025 01:34:41 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DD0003F673; Mon, 17 Mar 2025 01:34:29 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Simon Glass , Tom Rini , Heinrich Schuchardt , Anton Antonov , Tobias Waldekranz , Sughosh Ganu Subject: [PATCH v9 2/8] efi_loader: install device-tree on configuration table on every invocation Date: Mon, 17 Mar 2025 14:03:56 +0530 Message-Id: <20250317083402.412310-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250317083402.412310-1-sughosh.ganu@linaro.org> References: <20250317083402.412310-1-sughosh.ganu@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 The efi_install_fdt() function is called before booting an EFI binary, either directly, or through a bootmanager. This function installs a copy of the device-tree(DT) on the EFI configuration table, which is passed on to the OS. The current logic in this function does not install a DT if a device-tree is already installed as an EFI configuration table. However, this existing copy of the DT might not be up-to-date, or it could be a wrong DT for the image that is being booted. Always install a DT afresh to the configuration table before booting the EFI binary. Installing a new DT also involves some additional checks that are needed to clean up memory associated with the existing DT copy. Check for an existing copy, and free up that memory. Signed-off-by: Sughosh Ganu --- Changes since V8: * Re-word the commit message to indicate DT being installed as EFI configuration table * Remove the existing EFI config table in copy_fdt() * Move assignment of new_fdt_addr and fdt_pages variables to the block freeing up the existing config table memory lib/efi_loader/efi_helper.c | 39 +++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c index 15ad042bc61..f6fbcdffe82 100644 --- a/lib/efi_loader/efi_helper.c +++ b/lib/efi_loader/efi_helper.c @@ -454,11 +454,30 @@ efi_status_t efi_env_set_load_options(efi_handle_t handle, */ static efi_status_t copy_fdt(void **fdtp) { - unsigned long fdt_pages; efi_status_t ret = 0; void *fdt, *new_fdt; - u64 new_fdt_addr; - uint fdt_size; + static u64 new_fdt_addr; + static efi_uintn_t fdt_pages; + ulong fdt_size; + + /* + * Remove the configuration table that might already be + * installed, ignoring EFI_NOT_FOUND if no device-tree + * is installed + */ + efi_install_configuration_table(&efi_guid_fdt, NULL); + + if (new_fdt_addr) { + log_debug("%s: Found allocated memory at %#llx, with %#zx pages\n", + __func__, new_fdt_addr, fdt_pages); + + ret = efi_free_pages(new_fdt_addr, fdt_pages); + if (ret != EFI_SUCCESS) + log_err("Unable to free up existing FDT memory region\n"); + + new_fdt_addr = 0; + fdt_pages = 0; + } /* * Give us at least 12 KiB of breathing room in case the device tree @@ -473,15 +492,18 @@ static efi_status_t copy_fdt(void **fdtp) &new_fdt_addr); if (ret != EFI_SUCCESS) { log_err("Failed to reserve space for FDT\n"); - goto done; + return ret; } + log_debug("%s: Allocated memory at %#llx, with %#zx pages\n", + __func__, new_fdt_addr, fdt_pages); + new_fdt = (void *)(uintptr_t)new_fdt_addr; memcpy(new_fdt, fdt, fdt_totalsize(fdt)); fdt_set_totalsize(new_fdt, fdt_size); - *fdtp = (void *)(uintptr_t)new_fdt_addr; -done: - return ret; + *fdtp = new_fdt; + + return EFI_SUCCESS; } /** @@ -534,9 +556,6 @@ efi_status_t efi_install_fdt(void *fdt) const char *fdt_opt; uintptr_t fdt_addr; - /* Look for device tree that is already installed */ - if (efi_get_configuration_table(&efi_guid_fdt)) - return EFI_SUCCESS; /* Check if there is a hardware device tree */ fdt_opt = env_get("fdt_addr"); /* Use our own device tree as fallback */