From patchwork Tue Jun 17 12:08:54 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: 897375 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2110104wrc; Tue, 17 Jun 2025 05:09:44 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUIqN5Y+6I5+4pxTD+wpajzhzAl9r5M33nlnscEen2m6hml5OX7kpHOxUITGBSI9xMOq2Lifw==@linaro.org X-Google-Smtp-Source: AGHT+IFJe4iVAY+AnQ5QMKFG0g8ixTtoqsZfuyhGQGjJS9IjCg3jCRxKJSZsncBParfsQAaSha1r X-Received: by 2002:a05:6214:248f:b0:6f5:3cae:920f with SMTP id 6a1803df08f44-6fb4775911amr214725116d6.27.1750162183766; Tue, 17 Jun 2025 05:09:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750162183; cv=none; d=google.com; s=arc-20240605; b=EzV6tS7rjm1/PZFknAX/mydeW0XtN+iZslcAA+XNYDOsvybIF368XKLacXKfSkmtFn sCT5l78/On3UG6TIKSVeAVYyCutlunBAyLwImy6hW9aXoL8/yOZCQs7W8SG9bdgyZOQN LYAV/R2/8c4OxM6xOa1xOKFmqSjVUXr9M5U7Z3WQxm9dQhh2LTJUTN1Ikn8l3wKUFDSm ETPJ9uDBdl8J44FnqF0msfHgTUsBYurEbpyf6TFOAE7QF4ujfIt2b8KxHN4Ehhp9D7EH bWmIZ8iFD8hAc8QHWJ6F5Cznv0k4kxkVCrjvRN5QZpHFVpo5kY5jANwpINOKIbxLH1/b 5NKA== 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=ydNmL5Gk1TuCL7/qVzPwHMAwrmFgsuXUQqMEZZL11K8=; fh=0OGUXlCirN47PXkSyTSV4KNpm8J6ckvzictniQ+qXW0=; b=gTwR17SVLNyzMzFVd6F33eJfH26SXVmLNJ5gbMkOcfKYHEE6+qrT7v9iV5Xspguk3p 0tREVYTCb63bp/W7D5WlG8pHp/0pH0aSQEAtSSO+2XPCwBey+eGHmivs5K0CCP4JhhXr P/AnkQrwrkSwZfy7rVyQl3NZ67WVw0j0HRYAYIfcfDj41D28v7MYk+/KA0xaWVpAFMio bhvZ3HUmuwzfHfts9i6fOieDppDo0nacMrE/gJ9Ls+3xuO5obOHKL0ScSnhH/eNYYx3p DGXIDQ2a8gnU1mfhrPPCId8fj2+E0L/PhFrdKkH0sH1cO31SiSFnfg+cs7LkxK4S6Ruc n77g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20230601 header.b=UfTpgXjW; 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 6a1803df08f44-6fb35ad99f2si128289356d6.1.2025.06.17.05.09.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 05:09:43 -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=UfTpgXjW; 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 A095B82E17; Tue, 17 Jun 2025 14:09:14 +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="UfTpgXjW"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 42AB582D60; Tue, 17 Jun 2025 14:09:13 +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=-1.9 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-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) (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 4638D82BA1 for ; Tue, 17 Jun 2025 14:09: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=grandpaul@gmail.com Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3a35c894313so6358117f8f.2 for ; Tue, 17 Jun 2025 05:09:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750162150; x=1750766950; 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=ydNmL5Gk1TuCL7/qVzPwHMAwrmFgsuXUQqMEZZL11K8=; b=UfTpgXjWxwe3888iZdt7hk7I68mcEnu57E+NjM3STOLcxlH3Mnp3Rpc6HcVZGNFk9i RsCexvoiy4/f2L0+2AIg+wRvJQUTq1YSUvLHx3WHJglmaHUAhhGiVcqp959PcT/apcnk 3mX4OuNK84a9qK2lssIzLDsUx/5NlwQ/g+RcJ+i4oi7rxGq0qYqzATMCTrYmEpaUvT+b H3MMVXGx8CDqtmkdOqzcVOr/xXaNvKUVZKJK4ij495zc/uj1/QidwBKNEm4axY9X4n03 MRCRez5TZHiN+KrIalDhqKI0l4ejEDTSsBzGodogPHvG+DBB5nXoFWHW8w4WzVWtQB8u HIdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750162150; x=1750766950; 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=ydNmL5Gk1TuCL7/qVzPwHMAwrmFgsuXUQqMEZZL11K8=; b=Jbr2tsXO7i6LloMQgDfFUqd4vUy65En/cPdLEz+5RBTRPUE1go+5k8sGWQk9TpbfAR 6wB4wtDKA/gaYtAQDknfjSLL6RQUoBclnuTWiCSolg7VBJAz1DG+izsiFXEgAjMX2yP0 eO8C8/x3R289aGu9mk8/NEdYL92IFNQvKgIP5xNmOSEqfVtMK6FPjV+iB/4r+8xCX8de DAA4H8IcByePHsHy8lSdJzeHFKu3X/pW+6Jo5dp7QS8WMAlDlptAuU60+EdCF4towB/5 zQGvWFBMdatRXeWeF3STcu0SbjiZa6ZucTqW/MJITTs5e96hHuJ3evPs/KN2omKycDXK 1btA== X-Gm-Message-State: AOJu0YwbGEFrR2FvFE6lNbWDCqNldhXoLTK4tqdLPHfIEqVvFqtEJSfR VNsQyS6sY0S0Vhp1UiERRx52EPLuny5Jxc+ndKha1zdZiImCfodkNTynSTDVD29B73U= X-Gm-Gg: ASbGnct+97mKPNb9xTtDxBvYV0DQWmtFdR6HG+gKSgqHggoeejRY4oRJRCfREpPDL6j X/vPlJd3z4OH81mfDordX42LNsBc+kOyx5ThyfEYdZH5J4N+7vMCOSf/UOb1qwTRxk0BYaewDc7 VM9rOkUSGUZCF2sU2wJZVhwPg5o5x3qRcGNVm/alGHhWrLT6XRxtRgeUrnzbYSWyosW3m52NPBn GNWyo1PU75ccBITfBBiNW8zM6k7fgfRz+yV0YVgFYiukmnOPRJJCwJrtK33vyC0aothOw7JGaQi i0tBmm98BpCNK11t1d4hT4yRrjoEMDb5P/SgGFMp0MwkxAGIggPJr+BdxGgLKA== X-Received: by 2002:a05:6000:2184:b0:3a5:783f:5296 with SMTP id ffacd0b85a97d-3a5783f5728mr6156685f8f.56.1750162150319; Tue, 17 Jun 2025 05:09:10 -0700 (PDT) Received: from localhost ([2001:8a0:6a5a:6c00:eb29:af93:a639:c827]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a5780c5004sm10228630f8f.56.2025.06.17.05.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 05:09:09 -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 v6 3/4] lib: efi_loader: efi_memory.c: add efi_realloc() for realloc memory Date: Tue, 17 Jun 2025 13:08:54 +0100 Message-Id: <20250617120855.87492-4-paulliu@debian.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250617120855.87492-1-paulliu@debian.org> References: <20250617120855.87492-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. V6: Return error status when efi_alloc failed. --- include/efi_loader.h | 10 +++++++ lib/efi_loader/efi_memory.c | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 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..1570e300570 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -666,6 +666,64 @@ 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); + if (*ptr) + return EFI_SUCCESS; + return EFI_OUT_OF_RESOURCES; + } + + 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 *