From patchwork Thu May 29 04:05:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ying-Chun Liu \(PaulLiu\)" X-Patchwork-Id: 893118 Delivered-To: patch@linaro.org Received: by 2002:a5d:4e0d:0:b0:3a4:ee3f:8f15 with SMTP id p13csp340996wrt; Wed, 28 May 2025 21:06:16 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVfbP92ZYWYNcD5hTAAzlRgRp+RzPX9hg+aGHvIHp7nN4abA2ThcF/1dx9BjZpBqK7Eaa9sXA==@linaro.org X-Google-Smtp-Source: AGHT+IFv0L3PByKjIxaPe0XD9XT0Ydz2UqyU0RykYcKCiAKDjfo0fZNaYBkIBtet12bI62fqQn1n X-Received: by 2002:a05:6a00:843:b0:736:4e0a:7e82 with SMTP id d2e1a72fcca58-745fdead9bamr25305543b3a.10.1748491576268; Wed, 28 May 2025 21:06:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1748491576; cv=none; d=google.com; s=arc-20240605; b=RCvMD6zWHdwmKrN+EMF14AQV31li0NWdBPY0PRbrb6c754mxweFKsFqAjhUq77G2aP yvzUdTgRvKuUxvzJOZZevaS+3RbaCK3nugMLZGZae+Zvddf1PW7d/zwoE2G7q7vz9A1t o03g+423p2laSoa3+4Ko9wLA73Lcg8EoieheHBwnG+aEYdZWyBt9iJ9ayCT9OeqfdU9o 4tLkKqg4KNpNVyXMaG5luvpIXPfBMDGH0CzK3cw2TIqsnA+kbyJ+T/aBnVrfUUzzXtgU qnYFeD26h5MUGt2PaThcStIrhYBRfr881IAXPvhsuTM6O8W698fwIX0IInCrjJOj1oS5 f22Q== 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:dkim-signature; bh=OAxJD/tro4tQ+LLFN6U7a59OXHxib3oVjSK6WfPOJpk=; fh=0OGUXlCirN47PXkSyTSV4KNpm8J6ckvzictniQ+qXW0=; b=lN3MX72jbyqbCVfLMFJ69d1E9UCtGFQlbT3Doou6HtOCZ0RMWlPyXZ69AZ1q73WvaG Jt5nquKzYdtsMavlqBiBp/tUeYRQ8no9LGQARngpJ95XiXHMWh+Wc9D6o44ftYllkSKL ULdbWuxmg9s4HU+zjmjTmP3t5rK6fPfcm1tvgSb2LlsGdft7A9NrDgPlBi7iK4k1MT6g KwfsFRZdQx0WuGiZjEEBnt2JKI8jgPmnMHOg5KvPgYEy6L17r8sHnuF9reqaUOzazRnD LG621i8tm7dHLLMZ6iBjytfezeiwUJtzfoTPJdBU+3YQ1Chdw28yLi2Ey03s5/cpV5Hz f0WA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20230601 header.b="fepik/v8"; 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; dara=fail header.i=@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 41be03b00d2f7-b2d98718db3si3750431a12.90.2025.05.28.21.06.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 May 2025 21:06:16 -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; dkim=fail header.i=@gmail.com header.s=20230601 header.b="fepik/v8"; 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; dara=fail header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CD9EC835A5; Thu, 29 May 2025 06:05:49 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="fepik/v8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3983E83641; Thu, 29 May 2025 06:05:44 +0200 (CEST) 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_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) (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 E3BB38354B for ; Thu, 29 May 2025 06:05:41 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=grandpaul@gmail.com Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-311ef4fb43dso359429a91.3 for ; Wed, 28 May 2025 21:05:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748491540; x=1749096340; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=OAxJD/tro4tQ+LLFN6U7a59OXHxib3oVjSK6WfPOJpk=; b=fepik/v84yibxdYIVSM8XT9/Til9YK5yYH7FV1GoQuh8DiM41FeB9O3puoxmRywstC r0pm9Fj2G8l9YYQzPoVPfvAi3f7TEffNtzSOOd/eJi2pLU18fs1Vj8WNbMJqPiblML5+ qlstCqxaQ7VINoFR1SoURnXL6gKhe2valEWmwvdPjzCt51ingFF64qrt8Sf0upCbl+u/ 4QUmIrVef+l5av7pwbbqRDU4U2suF4mF7xS+rtI+ef+ao/cAVdqwqvdlSKpiK2P4vIg8 wUoZyuQ81gDQuC8FMc7FQHvbZ049PLy3XihYASVI5HoqKoMONgHbzUvDRtDYNsm1C/Xw pibg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748491540; x=1749096340; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=OAxJD/tro4tQ+LLFN6U7a59OXHxib3oVjSK6WfPOJpk=; b=Ok+ljNZ0u0qspeYccLC2KpphusnX2bwgbrqFMNYjpgcIq8NzI132y8HF8nIWN3Foaa nPH2Q+3H/FtXipCc6+Qh7+8gYB49FdWHYvwmY+ZtAtOWE440q0nT0bIjAuAWTJIMDPGU smFhw0oVvRtKQewEQun/v7CctPxoIYyWm6waFi1xQrqUu2zVQXhodcwl4BzGdsMBMZCG QwsagtZImXidO2EIClT47CgfZM8wVKtciwM8H4blwhyvs68PPXK/f7DsEfW6cJOp9+ru KsY+65KKwabcMr6L4FasICtQscPeZds3LY7lylXFD4BVVvVXl/S+MfGhqb00qZtnlJjP LKFg== X-Gm-Message-State: AOJu0Yx8m9QZI2sFac3pYYGhUloeH9uUtQ371YDUBJ/th+Cu5QakhVPS P+ZRmfGB6z4ExTYU/Q7gwEIs2B7JteygNDLHurAG0GEg37lBlZSvmlG21pjSEzqA X-Gm-Gg: ASbGncsm5moPiIvr95cWvMVSXq5WjWtlTHJpXOoRAj2XUPryp5WKsG6P93h/h5CdqSJ rCrSQFfOrb0COM/jfLo1DphKwLF6BVi+bKqyZMQso3O5Unbb34L9tY7wwgd9XEyrAjgYeaaLWUZ CTnrN01FQ4VzwPAw/+9kdXZ9XZUkvczxiCra0lPODxdT4HQNoL9sB3+zQF4fXYKYrL5NHS+85ox L0GbdHPSxS7tgATRbdczQ+5lDRIB/9yDxw148kBSdS0OSkKRIgMrw1cCdak9Pa1GNWGh2hts4Ir uns2xtQpokud3uU3lC/QHgNcyXcefHf+7WpaRpELqvkVb3M= X-Received: by 2002:a17:90b:224e:b0:311:df4b:4b7a with SMTP id 98e67ed59e1d1-311df4b4dcemr8154307a91.29.1748491539750; Wed, 28 May 2025 21:05:39 -0700 (PDT) Received: from localhost ([61.71.79.124]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-3121b71deafsm504158a91.16.2025.05.28.21.05.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 May 2025 21:05:39 -0700 (PDT) From: "Ying-Chun Liu (PaulLiu)" To: u-boot@lists.denx.de Cc: "Ying-Chun Liu (PaulLiu)" , Heinrich Schuchardt , Ilias Apalodimas , Peter Robinson , Simon Glass Subject: [PATCH v4 3/4] lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory Date: Thu, 29 May 2025 12:05:19 +0800 Message-ID: <20250529040520.3799600-4-paulliu@debian.org> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250529040520.3799600-1-paulliu@debian.org> References: <20250529040520.3799600-1-paulliu@debian.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 From: "Ying-Chun Liu (PaulLiu)" Add efi_realloc() for realloc memory that previously alloc by efi_alloc(). Signed-off-by: Ying-Chun Liu (PaulLiu) Cc: Heinrich Schuchardt Cc: Ilias Apalodimas Cc: Peter Robinson Cc: Simon Glass --- V4: Fix efi_realloc return failure code. --- include/efi_loader.h | 10 +++++++ lib/efi_loader/efi_memory.c | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/include/efi_loader.h b/include/efi_loader.h index d0c72d0bc58..13ca2ec9a4e 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -878,6 +878,16 @@ efi_status_t efi_next_variable_name(efi_uintn_t *size, u16 **buf, #define efi_size_in_pages(size) (((size) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT) /* Allocate boot service data pool memory */ void *efi_alloc(size_t len); +/** + * efi_realloc() - reallocate boot services data pool memory + * + * Reallocate memory from pool for a new size and copy the data from old one. + * + * @ptr: pointer to the buffer + * @size: number of bytes to allocate + * Return: EFI status to indicate success or not + */ +efi_status_t efi_realloc(void **ptr, size_t len); /* Allocate pages on the specified alignment */ void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align); /* More specific EFI memory allocator, called by EFI payloads */ diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c index 0abb1f6159a..d69bbb85efb 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -666,6 +666,62 @@ void *efi_alloc(size_t size) return buf; } +/** + * efi_realloc() - reallocate boot services data pool memory + * + * Reallocate memory from pool for a new size and copy the data from old one. + * + * @ptr: pointer to old buffer + * @size: number of bytes to allocate + * Return: EFI status to indicate success or not + */ +efi_status_t efi_realloc(void **ptr, size_t size) +{ + efi_status_t ret; + void *new_ptr; + struct efi_pool_allocation *alloc; + u64 num_pages = efi_size_in_pages(size + + sizeof(struct efi_pool_allocation)); + size_t old_size; + + if (!*ptr) { + *ptr = efi_alloc(size); + return EFI_SUCCESS; + } + + ret = efi_check_allocated((uintptr_t)*ptr, true); + if (ret != EFI_SUCCESS) + return ret; + + alloc = container_of(*ptr, struct efi_pool_allocation, data); + + /* Check that this memory was allocated by efi_allocate_pool() */ + if (((uintptr_t)alloc & EFI_PAGE_MASK) || + alloc->checksum != checksum(alloc)) { + printf("%s: illegal realloc 0x%p\n", __func__, *ptr); + return EFI_INVALID_PARAMETER; + } + + /* Don't realloc. The actual size in pages is the same. */ + if (alloc->num_pages == num_pages) + return EFI_SUCCESS; + + old_size = alloc->num_pages * EFI_PAGE_SIZE - + sizeof(struct efi_pool_allocation); + + new_ptr = efi_alloc(size); + + /* copy old data to new alloced buffer */ + memcpy(new_ptr, *ptr, min(size, old_size)); + + /* free the old buffer */ + efi_free_pool(*ptr); + + *ptr = new_ptr; + + return EFI_SUCCESS; +} + /** * efi_free_pool() - free memory from pool *