From patchwork Mon Mar 10 11:57:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 872196 Delivered-To: patch@linaro.org Received: by 2002:a5d:64ce:0:b0:38f:210b:807b with SMTP id f14csp942042wri; Mon, 10 Mar 2025 04:58:27 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXrw6gH4bLZgji5m2iOCQZXWuM9VYWw5Pr08xcxlcv27EotXdHpcpJEl7JfJu24g5YWqzLVvw==@linaro.org X-Google-Smtp-Source: AGHT+IG0GlqIFxtcUxK4tKvdjia9/GyQ83m33wvbFQ/Vc74Az4y0O+eQZRPSb2kEHq4SbIWQ4NSf X-Received: by 2002:a05:6902:218b:b0:e5b:18ff:8cf4 with SMTP id 3f1490d57ef6-e635c0f7f8fmr17684317276.5.1741607906992; Mon, 10 Mar 2025 04:58:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1741607906; cv=none; d=google.com; s=arc-20240605; b=QxUogFcvVocPqgkZVThX1BBtF/hqIj2R8HshYH15oNAxAUu6vs0h5V+rLVuL0L5Rug jBcuxW0F5AI7EbltkRYRw2/qRHEBgPZ6UX/sB+zeyuKL5VwZVLZTLc0kVZVW9v4+ZDWS r7/Kbn6S3GCwaOYd48ck+uTfcV4NdGX9R3WbczHY92EZnklIdpPd4xp3iL7cAWJmkZ0T HJmJz7APYKXWy7NMIFoRh2rDm6mdoqlJWoxjXiDHb2bR//f2Mc/KPIgUpySCGDQW0Gb9 UYSxA3Q26xGn5co97loXiQbaNNi+xoIdcBRl9MdLsvK8Vp3GshFm0d2D+fZRhDje3gme dsCQ== 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=42EsITw+E5qh1V7tOlARE4cCmLe0KJ8MhZjwZsGC1xQ=; fh=WIErNPtEyr4LSk0kn+FyUcnvc47vQRNxykSeJQzwMAA=; b=ADAVeaTNZVQg9mpdyQEYNIIyNnewh4txf9L0fRf5kqHuIMC0W1bFv8qCaXarqlLBkS CpEDftOo8IGOQml4DwfAbJG/3AaH1jS/xCYrNY7lHgzYL/jwNiwnhpFMzBHEhUq+Yv7c hjVy9B08f2VqqpO34yAgvPdKf4ou4jostGXlE4PCCFdzYEx5Iqcjk4xgFgPPtxFLC8a+ b4oZxywc+7SvRruj8wChvttwWSE9asA+qmHmqbfMBEXQfyvYosIokOATgdxlNYN6AbL1 prdIxXhHiza4FyhHgifbF90FnqzWXGtH1VFpzvN3GC1/5p4fe3cyuLT6KbOA4sQR8/lZ wEBA==; 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 3f1490d57ef6-e634b90d817si7671463276.605.2025.03.10.04.58.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 04:58:26 -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 3273581668; Mon, 10 Mar 2025 12:58:17 +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 028CE811DF; Mon, 10 Mar 2025 12:58:16 +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_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_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 B8CE28142D for ; Mon, 10 Mar 2025 12:58:13 +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 86CB01516; Mon, 10 Mar 2025 04:58:24 -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 350543F5A1; Mon, 10 Mar 2025 04:58:09 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Simon Glass , Tom Rini , Heinrich Schuchardt , Anton Antonov , Tobias Waldekranz , Bin Meng , Sughosh Ganu Subject: [PATCH v7 2/8] efi_loader: install device-tree on configuration table on every invocation Date: Mon, 10 Mar 2025 17:27:44 +0530 Message-Id: <20250310115750.1019051-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250310115750.1019051-1-sughosh.ganu@linaro.org> References: <20250310115750.1019051-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 V6: New patch lib/efi_loader/efi_helper.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c index 15ad042bc61..fe25eabeaac 100644 --- a/lib/efi_loader/efi_helper.c +++ b/lib/efi_loader/efi_helper.c @@ -454,11 +454,27 @@ 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 ulong fdt_pages; + ulong fdt_size; + + if ((new_fdt_addr && !fdt_pages) || (fdt_pages && !new_fdt_addr)) { + log_err("%s: Fdt address/size not in the right state\n", + __func__); + return EFI_INVALID_PARAMETER; + } + + if (new_fdt_addr) { + log_debug("%s: Found allocated memory at %#llx, with %#lx 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 +488,20 @@ static efi_status_t copy_fdt(void **fdtp) EFI_ACPI_RECLAIM_MEMORY, fdt_pages, &new_fdt_addr); if (ret != EFI_SUCCESS) { + new_fdt_addr = fdt_pages = 0; log_err("Failed to reserve space for FDT\n"); - goto done; + return ret; } + log_debug("%s: Allocated memory at %#llx, with %#lx 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 +554,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 */