From patchwork Thu Aug 28 02:07:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 155 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1756346891; bh=zz+nP41NTcwfVTCY/K3IigGuObtnpTmRfxAzsxQxC2E=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Bsz3H0L6CZec/ekXsFP3MNiqLIfKbz7kcR4azkrVIQxcFHsKJO5abpgfCrTPuVebB qa+ZPF9eRScqMry3y8/IWtfAsdJHngtlBLh4N5wEvPfra/F+kYe0e52CSP+ALf5o8e yUVfL17XONu2Agep3LppEe7nehO+ZKigDaIepcUkLUY6CcKikQpORgmmtg/KgzRQRk rZH5LuXcQju2yJNGgmFOlpDpHE5RqDZ10unElK1ztbqcRBpX0I893Tlld6DJewKDn1 gb/yxQISgyfXYXjeVUcspyxrcWgndPIhJbnGuy2s5CSBK5cP64KcxKnr3DqXDfd+6u pH4KJBkloelDQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3A8DE6783B for ; Wed, 27 Aug 2025 20:08:11 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id 0wFKRq10_0xj for ; Wed, 27 Aug 2025 20:08:11 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1756346889; bh=zz+nP41NTcwfVTCY/K3IigGuObtnpTmRfxAzsxQxC2E=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=NeNTEz5rbVlN56zJPNg7loun96/0aXuwMyJjfSMETxDj3p0b8mvJWzFRt8Goc/4ki vVpjmhIttdYsTmU2vNj703BSZmNoXq4f+yOeuQ4kGgMk8HQMaNp7C4kSuYgna1osY6 6hP7vMpgg4ffwcv5MRZlk0y3903AN2sZEgOAhjtsFjUMzL1/KpLYzMVSkILnwNbx+H rtJQGqh7T7ZvEpajhmORzi7H2t8zUs9gryjHzgs4zC9pgXZ8ToWG/GDI3vThmyd6vG B1SIin9v5Z/NXvi+eDs6vSCZtLnNgv+PZvwIbiYARJIIPxtUS0y9zyCL+As2n2Hnx9 cSEHoK8YyNhYw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3654367829 for ; Wed, 27 Aug 2025 20:08:09 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1756346885; bh=h6XaJqxEsfsFvsgl695GPlZGzLoord17ShoESiWe21Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FeZ9KMbPaw/yxEdKxdE2JmFQ8oVPIcMO8SrtFi8wecKeAONEj4RSJ5LUefQV/2gzB wYcXu4WqdRza1qLZ/zNSchM0m9vcsTK4MeQo98xLfrOENbiXQsQ2MBFJRHZ1y9ehvI P+SKKCuldSX/Hq6qY6jO4nRwnAJhlAKnZOz8CadUazcP1MEqewy2sxgDX/V3J80DE7 WIKNmqcvAy0i8WDSQViRl7cNhYZQceEUnq7F3nbVYJJSSECvlGkphiwqkiZbINM2wh 3aqNh9WKAO6oqIdCpH979aWLrwL7Hqf4xh9UtmRcw4IQzS6qBOxRWxM7dLFYSh3le0 B8TWchJLHvnHA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id BF1A06743E; Wed, 27 Aug 2025 20:08:05 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id rkW6O4U0SN2B; Wed, 27 Aug 2025 20:08:05 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1756346884; bh=zMZU542tShLwYlEnq0QIcJsWakJezkZob2oCC7WSrEM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jShBV68LDA9T1O1eBbt+ab/UTrJvbHRhb6G5+/Iq80IA207n5Fzdj2sHQe4+0DKJh PlsVHdUPNBk0EjXeBjXZhE9VnYnUud+ABSWt/w0x8J+/Cc4YwSuQjf2Ojo36qbtRqu B9SHQY23UQDMNBNMgqk7LtDdaW7GT1SDVtrRTTDGq4Uc/qDK8BQiV/9dwfOVEpFS4R w7E9HB+k9vqaRQzOkbvzugnmG2lRM9Sc268P4RlcIMJZM7VvY8q1BmHmgAqF9iVtAO 06sN5krV3zi//uy+njHVETdI6z5hth9CA0HCIUAEssP+WQ6VDPtHt3VzVHc/Wjp9If 0nH1D72qLUWtA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id DC8D86777E; Wed, 27 Aug 2025 20:08:03 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Wed, 27 Aug 2025 20:07:15 -0600 Message-ID: <20250828020732.981415-13-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828020732.981415-1-sjg@u-boot.org> References: <20250828020732.981415-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: IDDEVB2IFSEF4FN7APFERYSEFBHGJKFA X-Message-ID-Hash: IDDEVB2IFSEF4FN7APFERYSEFBHGJKFA X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 12/20] efi: Use abuf when reading EFI variables List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass The abuf interface provides a nicer abstraction of the data and size of EFI variables. Create a new efi_read_var() function and export it so it can be used elsewhere. Adjust the existing efi_dump_single_var() to use it. Signed-off-by: Simon Glass --- cmd/nvedit_efi.c | 62 ++++++++++++++++++++++++++++++++---------------- include/efi.h | 15 ++++++++++++ 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/cmd/nvedit_efi.c b/cmd/nvedit_efi.c index 5616e73bcfd..45d6a8ef67a 100644 --- a/cmd/nvedit_efi.c +++ b/cmd/nvedit_efi.c @@ -5,6 +5,7 @@ * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited */ +#include #include #include #include @@ -50,6 +51,36 @@ struct var_info { efi_guid_t guid; }; +int efi_read_var(const u16 *name, const efi_guid_t *guid, u32 *attrp, + struct abuf *buf, u64 *timep) +{ + efi_uintn_t size; + efi_status_t eret; + u32 attr; + u64 time; + + abuf_init(buf); + size = 0; + eret = efi_get_variable_int(name, guid, &attr, &size, NULL, &time); + if (eret == EFI_BUFFER_TOO_SMALL) { + if (!abuf_realloc(buf, size)) + return -ENOMEM; + + eret = efi_get_variable_int(name, guid, &attr, &size, buf->data, + &time); + } + if (eret == EFI_NOT_FOUND) + return -ENOENT; + if (eret != EFI_SUCCESS) + return -EBADF; + if (attrp) + *attrp = attr; + if (timep) + *timep = time; + + return 0; +} + /** * efi_dump_single_var() - show information about a UEFI variable * @@ -63,30 +94,19 @@ struct var_info { static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose, bool nodump) { - u32 attributes; - u8 *data; - u64 time; struct rtc_time tm; - efi_uintn_t size; + u32 attributes; + struct abuf buf; int count, i; - efi_status_t ret; - - data = NULL; - size = 0; - ret = efi_get_variable_int(name, guid, &attributes, &size, data, &time); - if (ret == EFI_BUFFER_TOO_SMALL) { - data = malloc(size); - if (!data) - goto out; + u64 time; + int ret; - ret = efi_get_variable_int(name, guid, &attributes, &size, - data, &time); - } - if (ret == EFI_NOT_FOUND) { + ret = efi_read_var(name, guid, &attributes, &buf, &time); + if (ret == -ENOENT) { printf("Error: \"%ls\" not defined\n", name); goto out; } - if (ret != EFI_SUCCESS) + if (ret) goto out; if (verbose) { @@ -103,16 +123,16 @@ static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, count++; puts(efi_var_attrs[i].text); } - printf(", DataSize = 0x%zx\n", size); + printf(", DataSize = 0x%zx\n", buf.size); if (!nodump) print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, - data, size, true); + buf.data, buf.size, true); } else { printf("%ls\n", name); } out: - free(data); + abuf_uninit(&buf); } static bool match_name(int argc, char *const argv[], u16 *var_name16) diff --git a/include/efi.h b/include/efi.h index c07717811da..07d07050c01 100644 --- a/include/efi.h +++ b/include/efi.h @@ -23,6 +23,7 @@ #include #endif +struct abuf; struct udevice; /* Type INTN in UEFI specification */ @@ -863,4 +864,18 @@ efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size, int efi_dp_from_bootdev(const struct udevice *dev, const struct efi_device_path **dpp); +/** + * efi_read_var() - Read an EFI variable + * + * @name: Name of variable to read + * @guid: GUID for the variable + * @attrp: Returns variable attributes if non-NULL, on success + * @buf: Returns allocated buffer containing the value + * @timep: Returns the timestamp for the variable if non_NULL + * Return: 0 if OK, -ENOENT if the variable was not found, -EBADF if something + * went wrong when reading + */ +int efi_read_var(const u16 *name, const efi_guid_t *guid, u32 *attrp, + struct abuf *buf, u64 *timep); + #endif /* _LINUX_EFI_H */