From patchwork Thu May 22 01:17:47 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: 891700 Delivered-To: patch@linaro.org Received: by 2002:adf:e88a:0:b0:3a3:61c9:c5d4 with SMTP id d10csp2404514wrm; Wed, 21 May 2025 18:18:32 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXzkK1LE6vI/rTcw4Sevrjc3mf+n8EpM3x4Sn7dEpgZkF5RQF/Jt4N+MzzMxuYQV0+YfQl9jw==@linaro.org X-Google-Smtp-Source: AGHT+IFPEjbR2+XhqZ7FP6gE1baCYij+p80U+gM7UAh7wf9Vniyfvdt+rpZFD8x68eGcV/fbjCwJ X-Received: by 2002:a5d:588c:0:b0:3a3:7152:c665 with SMTP id ffacd0b85a97d-3a37152c6b0mr11806334f8f.54.1747876712551; Wed, 21 May 2025 18:18:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1747876712; cv=none; d=google.com; s=arc-20240605; b=ax+2tzF3SWzZRQ8yuTYWfN6c06UxQiNTShIBT15PPddCi2608oKa4pbGte/fwWSp7i FD2e/azIiQuU5J8pLMAVCO6NewC2/7bteL3pkBMxkAkDr/veAX9YP36JUUpGvKTB62lu EJKdayBt/Zpgp69nPi1ff3JheRCCUHiKrZXCQtO1vRc5BeKvx6U6N6FoWV1hT7ds/v7w cU85+zO+m+vCrTLfjUvsd+0GVFW3PMLn3ADgqO5MfYhzXXToTTa/Oez5fzoqrMFrTJ0C 4sR4nwkgZMMxPTgfUiyNaxuqPJ4dpEIdZFIJOop/6NkOoi+JTd4346szmYj2WBfy6P+c Ot3w== 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=bkimbc0G1eX9h5DfGKuPBORAMalWiav523ma7qZ4mLI=; fh=qX5ZZPHEMXWmb4A5/dTtVKkSjWWl13aZe4Ej/wmUqYQ=; b=Y4LgMiLis/+EFJ4fA0/9KGIvFa+thUCAg/VSDDnAIDr8MEV27xzhVhWS9Riq4iGi1Z rNoIrptfAlFeQAE6kDqIO8bXZw8g9WiR7y9Sce0C6JGI8TMXgNAioi7jQDL0+m9DzqQt aXB2O2RrTgaf1dllVELD3Kp29TqDAX+CLg4zrXz/f4uIUUowfsjM05sKq4NTs9Iw7A5J 0YySRKL1itUSeirGG/BFTUjDjSsO3zydmXRVQCeuRFe87/OQR13gfPeKZH/wWgd7izbQ h6s3ApLZ5AaRp0AqljWLeAGTzTWM7BmYnzrfqQAmy5g59wJd07dbOnm8Hq0ZKCQqxLcc +FVg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20230601 header.b="htvCrlO/"; 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 ffacd0b85a97d-3a3626f21b7si10660008f8f.801.2025.05.21.18.18.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 May 2025 18:18:32 -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="htvCrlO/"; 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 3C24082F63; Thu, 22 May 2025 03:18:11 +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="htvCrlO/"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 71D75829F5; Thu, 22 May 2025 03:18:10 +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-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) (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 06E0E82E7D for ; Thu, 22 May 2025 03:18:08 +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-pg1-x529.google.com with SMTP id 41be03b00d2f7-b2c02e79d31so241792a12.2 for ; Wed, 21 May 2025 18:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747876686; x=1748481486; 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=bkimbc0G1eX9h5DfGKuPBORAMalWiav523ma7qZ4mLI=; b=htvCrlO/XVZtaZm6xNdwBjcMFP4EeJVHuhZ1/baKMvdSlTtFec0Ouz/lFkEmlTQ1n8 0VeOOakd0LuDlNeg1zMPyTbupBcEfB+T85HOA49VAB9XudDPpQUxaZ0hKthRrblnUzNl s6aFo5juMfKPVjTR2S/DLsbrcie462Qztkk/uoQ0Z/n2olgrDnHe5tN00mo4uTfpw3yv DQS0OeEkihT6rNC9tD1asRkr980toYaESMbKtSX7sHTDYwGBFBthz/VAqNj95YwZRajw 47GPPKX8JapmerOKrKXcdTWlYSva5LbGdj6wu5OXaeh5RyZLjgRBkAkhY9j1JOn8whle yVTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747876686; x=1748481486; 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=bkimbc0G1eX9h5DfGKuPBORAMalWiav523ma7qZ4mLI=; b=EcxXOfkIgEG9OEvxHx0+4854fvlOnieOhewKUer5LU5bIVQe0q30mezpSMUmEELL4G 7BMXNPUL+uzkbIwCsP79KrCWGu/smbZmC8RbC12LmfmXjtr4n5+d8CdQpAXFuAmPsB1m wXm/qagcvN7gyrlSD+J8Bj4bQxXjS3T47sS0vYxfoHojOGVX6eSxtpWUF8Z7Z94T4Kfv EqnuWvkpVuBZOMZb30R/hwkruvqXrCOH3ZWaD8ZDfi6rEusf5khqNtvDFuJbe6oGeHm3 ScdsqV2+KpkmpawiO68Y8bXdY1fb3DSwcMrqTDqo+P79tyhtMmCT6otJSqZU+q4VBA0N 7cig== X-Gm-Message-State: AOJu0Yzps5a77T2/H0DtFy+SLpaT0tOUOA4F1DGgzI0wuzgbUPOQCJ2o TppxCzAd6k58vpSuD9kbemJTZHQkgErvNjV42JKBddB6yGwMhphLoAtGAtiq8A== X-Gm-Gg: ASbGncuChRslLNja1DCcBIbSb9U4HIB6gRWvDPFhxpb+zdjjvOy/Eg/xHhwntwZ6xfo qjK+6/S2lxuMZRvIIJjyAHeFMR+93blE0AEN+tAtpRnW0ctvDnoL8KXcTANqCM5wDIxoQ/mQ2Pr 4H5AABLWiQ7NL+JLEgfrBklujy6refVzqXbX9+Z/2yQGuJ3juTcNvwmZ2Ohr9nByi9mF1kRqSrE cCtNV7a7pUxmSY3psfPnmSUKfWAGOJiGWF1Q7DlDYfx4zTwKtnx/rqdMhsHyvz/YZXcI4SktIY0 +Y5zsT48LLE8CLMo7fozWE+CDjyVK1wByBG1Ev0c3oafLL0= X-Received: by 2002:a17:903:4b4b:b0:224:a74:28d2 with SMTP id d9443c01a7336-231d452c4bbmr335429875ad.26.1747876685609; Wed, 21 May 2025 18:18:05 -0700 (PDT) Received: from localhost ([61.71.79.124]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-231d4eb9fb6sm98663025ad.161.2025.05.21.18.18.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 May 2025 18:18:05 -0700 (PDT) From: "Ying-Chun Liu (PaulLiu)" To: u-boot@lists.denx.de Cc: "Ying-Chun Liu (PaulLiu)" , Heinrich Schuchardt , Ilias Apalodimas , Peter Robinson Subject: [PATCH v3 3/4] lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory Date: Thu, 22 May 2025 09:17:47 +0800 Message-ID: <20250522011749.3498970-4-paulliu@debian.org> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250522011749.3498970-1-paulliu@debian.org> References: <20250522011749.3498970-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 --- include/efi_loader.h | 2 ++ lib/efi_loader/efi_memory.c | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/efi_loader.h b/include/efi_loader.h index d0c72d0bc58..ff7ca5a0480 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -878,6 +878,8 @@ 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); +/* Reallocate boot service data pool memory */ +void *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..4c76df642dc 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -666,6 +666,58 @@ 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: pointer to allocated memory or NULL + */ +void *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) + return efi_alloc(size); + + ret = efi_check_allocated((uintptr_t)ptr, true); + if (ret != EFI_SUCCESS) + return ptr; + + 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 ptr; + } + + /* Don't realloc. The actual size in pages is the same. */ + if (alloc->num_pages == num_pages) + return ptr; + + 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); + + return new_ptr; +} + /** * efi_free_pool() - free memory from pool *