From patchwork Wed Mar 12 08:54:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 872819 Delivered-To: patch@linaro.org Received: by 2002:a5d:64e7:0:b0:38f:210b:807b with SMTP id g7csp158610wri; Wed, 12 Mar 2025 01:55:12 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUJh+AsDaiMG6TQi3xqW1KCoJBJVqLfWB6senlh8IGMyG+fHO0gjkpabSsVq5pGsyTJruJeLg==@linaro.org X-Google-Smtp-Source: AGHT+IGasp7Vz466gDe2pI+iIpDONuNszn/ZWR7IwHrBAv2Hf//RQFPN6PKo2aF5xIwWIxHuaKPA X-Received: by 2002:a05:690c:6502:b0:6fd:1d9c:bd6a with SMTP id 00721157ae682-6ff091ab2fbmr85120007b3.3.1741769712253; Wed, 12 Mar 2025 01:55:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1741769712; cv=none; d=google.com; s=arc-20240605; b=WAx5uAeMrvPRymnq03kXcK0bj4qfvocsa70UuBNeuBKMOWIEXngeWUM4fR/4eLSah4 etPqMfJwqhmUakkzfbPkIv3jXM9+0tof6F2wMgLKexeo8VvctRlxdfVwY3gfPMyRi3v2 7sbKoWZIk+uSbKCIpn5A0s4ybhDjF8sMDLwpKYpbedotBO93QhGqvNcw7u0nop5XbaBN KjrGfCyzZk6wdhX9g2PFz025AaVFhIlZ8OSS2VJCOR9wo1rQGIwVMCOxY/HHxRyZkUCm CsgXOgpYY8hLYPmBo9uieebk7A5Aa9e33SYcwbKNYpVh9FMFBsVchf/pAKAgFKyW1km4 rvag== 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=rjhhGsOve/9mOhtFPuLfNvZNpBLVz4X5DOyMc+XXD/M=; fh=Evn5eBx2ytD/hj+tB8Xv1T61prWYrP0E59LRKWAPORI=; b=F6ha9ki+CuHvCTvYKJCwQu2dwvEOcrsnLn7obfVvRbCYms4Yv6voMAfdZ8IRa/Joks TU/TdYJNefF3urJbsPC2MC6L02FsYm1MiSGydANPz07RXlJZyqOPsDdzQi2sEGT3wWm7 gWvR1m9cJ+9soKYcIwal7CraT4KaOfpUzKz1SsrzjKgeqO2TQO9r6PUSlnnv0Gkj5BgO FpUmQLyYYzNwG5NZK5vBoUkYl6kqQyvMVr6vwOp3X1EexUeeiPeu7olhhGckX8IqBi2G NsGrNLvsjkYSMbFwzKZSXmFpGST4hq+EHgYeA5xQ2GBdoMi1dfM6m8/ekWffoXjfEAHv aQ/w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; 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=fail (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 00721157ae682-6febbfcf564si94778227b3.0.2025.03.12.01.55.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Mar 2025 01:55:12 -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; 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=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 0390E8197D; Wed, 12 Mar 2025 09:54:58 +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 426B0818C7; Wed, 12 Mar 2025 09:54:56 +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 327E5801FB for ; Wed, 12 Mar 2025 09:54:53 +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 6B94A152B; Wed, 12 Mar 2025 01:55:03 -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 145FA3F5A1; Wed, 12 Mar 2025 01:54:49 -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 v8 2/8] efi_loader: install device-tree on configuration table on every invocation Date: Wed, 12 Mar 2025 14:24:18 +0530 Message-Id: <20250312085424.1201148-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250312085424.1201148-1-sughosh.ganu@linaro.org> References: <20250312085424.1201148-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 the table already has a DT installed on it. 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 V7: * Change the type of fdt_pages to efi_uintn_t * Remove the check for either of fdt_pages or new_fdt_addr being 0 * Change the format specifier for fdt_pages in the debug messages for size_t * Put the assignment of new_fdt_addr and fdt_pages on separate lines to avoid checkpatch error lib/efi_loader/efi_helper.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c index 15ad042bc61..37e5859741f 100644 --- a/lib/efi_loader/efi_helper.c +++ b/lib/efi_loader/efi_helper.c @@ -454,11 +454,21 @@ 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; + + 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"); + return ret; + } + } /* * Give us at least 12 KiB of breathing room in case the device tree @@ -472,16 +482,21 @@ static efi_status_t copy_fdt(void **fdtp) EFI_ACPI_RECLAIM_MEMORY, fdt_pages, &new_fdt_addr); if (ret != EFI_SUCCESS) { + new_fdt_addr = 0; + fdt_pages = 0; 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 +549,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 */