From patchwork Wed Dec 10 00:06:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 864 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=1765325319; bh=wQZ7w48wH0jwq1d+Um5e28PPWa6scFt/cmUOXRDBfuw=; 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=Q8Jii9KPLw1d1Dc9GwzILhahdITvXq3QATBwYiH39UtXthUbrn+QnLtOfP+PNOTDJ 6kd8ZLCzumwxgUN0G+Qdepd9/JOZcaZinCND8oGLReIkwsABo1DpYv8A7is7ZezjhL /X2yRo5kdWTokrwwFlGqayprlu7R9VVWwMyVso2ngMyC4qlKHNwGvBm6tIvBVyn/An G/QFzWOf0Gj4798zI4OUDTgpgUK78AL2Tmok0fFyraKusWWSCoPRAZiImp3X/8+zae S622fZf89DEWBbOgUHDQGFLhdY8VhfPEX51cl7ymghkR+yRrqaKDeOqCmN6MSoC14H 0dKO58JR5cRxw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9862C68A26 for ; Tue, 9 Dec 2025 17:08:39 -0700 (MST) 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 bVgywI3v7925 for ; Tue, 9 Dec 2025 17:08:39 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325319; bh=wQZ7w48wH0jwq1d+Um5e28PPWa6scFt/cmUOXRDBfuw=; 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=Q8Jii9KPLw1d1Dc9GwzILhahdITvXq3QATBwYiH39UtXthUbrn+QnLtOfP+PNOTDJ 6kd8ZLCzumwxgUN0G+Qdepd9/JOZcaZinCND8oGLReIkwsABo1DpYv8A7is7ZezjhL /X2yRo5kdWTokrwwFlGqayprlu7R9VVWwMyVso2ngMyC4qlKHNwGvBm6tIvBVyn/An G/QFzWOf0Gj4798zI4OUDTgpgUK78AL2Tmok0fFyraKusWWSCoPRAZiImp3X/8+zae S622fZf89DEWBbOgUHDQGFLhdY8VhfPEX51cl7ymghkR+yRrqaKDeOqCmN6MSoC14H 0dKO58JR5cRxw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3C68A68974 for ; Tue, 9 Dec 2025 17:08:39 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325315; bh=kkD2ElO3qfEZDNGWoXBqLTDMvg/UOdoEJSzQ+Ip6PjE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I7DreutgLOxSzdkH1CXUI3L+cMAH28dxXtej+cldtJsTtRhMGCOi0AQcZDNHMlP/H CiGPc8Mg4i8T4yc3XTZ2Lx63ctMDFJ/VVAslgbEtT8xa3qmwZLgCLNLG3lbXKeUIYR +W3uNcRQ173xpC8SH6NdRQkJ0OtS4os5G/7BCz6moiWCRoAxnt9q144WfLCQ7Wn3IR dHGdfm+ub2G0U6PJH9mq4UEOkg2EXHtoLO/7wibtHO4ycXtrM4FON59S0nY1kZEIfa hiRt0HNT7gre2rlC5Cr0YhQjZ1ZNTzkSmU9jdlEAhNtq+zG24Sz0GhlJdnCI2k5cFn xewUAm2XJ35OQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4EC08689EC; Tue, 9 Dec 2025 17:08:35 -0700 (MST) 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 BiQ00YfaxJbL; Tue, 9 Dec 2025 17:08:35 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325309; bh=a/hij0xrZsgNHzc0SzI42Te6zxWWuoQmgdTNMZeZH60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WHA7EB/T0ON9ZYjCtIklG32M9yeupjkTxqnrBaoMAihxbEBL1uel0qyFAfxYnwOvx kgVfhMJ4zqLgTlgMd68/W+/g0S9kwuIeUO0V1lpIa85xhW6xeSkQOb5T6tlyYgEjtl nQZhlrjE7dwhu/tGgp0j+iJVVjvJ8iHe94wLir/FL2sAS3HMOJE4ebYYvqRbdYjRWp 1Oetf5V3KEx10zV5E9ROLfObWxDVCW0/6LU5DPdGF15/8zAc2OdrnvDBv7ha4ULnam +KC42dCfP6qvk04kgWkL9+b1VIMD3DzNGWRpRj9Yt2iF5YFMVe6hKAuCbjFgJCKASe AzWczn5GtNZcA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 64A4F68947; Tue, 9 Dec 2025 17:08:29 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:06:58 -0700 Message-ID: <20251210000737.180797-8-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251210000737.180797-1-sjg@u-boot.org> References: <20251210000737.180797-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: EYK5CIPKW7D2V7BXZRO4IEMRLI6YC5L3 X-Message-ID-Hash: EYK5CIPKW7D2V7BXZRO4IEMRLI6YC5L3 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 , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 07/35] lib: Add format_size() to format sizes as strings 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 Refactor print_size() to use a new format_size() helper that formats a size into a buffer. This allows callers to get the formatted string without printing it directly. The format_size() function is only exported in U-Boot proper (controlled by CONFIG_LIB_FORMAT_SIZE) to avoid code-size impact in SPL/TPL where it remains static. Co-developed-by: Claude Signed-off-by: Simon Glass --- include/display_options.h | 14 ++++++++++++++ lib/Kconfig | 7 +++++++ lib/display_options.c | 34 ++++++++++++++++++++++++---------- test/lib/test_print.c | 14 ++++++++++++++ 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/include/display_options.h b/include/display_options.h index f3bdff172eb..5941d6bd37a 100644 --- a/include/display_options.h +++ b/include/display_options.h @@ -11,6 +11,20 @@ #include +#if CONFIG_IS_ENABLED(LIB_FORMAT_SIZE) +/** + * format_size() - Format a size with a suffix + * + * Format sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB", + * xxx GiB, xxx.y GiB, etc as needed + * + * @buf: Buffer to write to (must be at least 12 bytes) + * @size: Size to format + * Return: @buf + */ +char *format_size(char *buf, uint64_t size); +#endif + /** * print_size() - Print a size with a suffix * diff --git a/lib/Kconfig b/lib/Kconfig index d7f791f77f3..79a75f98446 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -36,6 +36,13 @@ config BACKTRACE stack. This is currently only available on sandbox. The backtrace command can be used to print the backtrace. +config LIB_FORMAT_SIZE + bool + default y + help + Enables the format_size() helper which formats a size as a + human-readable string. + config BCH bool "Enable Software based BCH ECC" help diff --git a/lib/display_options.c b/lib/display_options.c index 7f9dfcc43ee..3aee08c7b00 100644 --- a/lib/display_options.c +++ b/lib/display_options.c @@ -98,7 +98,11 @@ void print_freq(uint64_t freq, const char *s) printf(" %cHz%s", c, s); } -void print_size(uint64_t size, const char *s) +#if CONFIG_IS_ENABLED(LIB_FORMAT_SIZE) +char *format_size(char *buf, uint64_t size) +#else +static char *format_size(char *buf, uint64_t size) +#endif { unsigned long m = 0, n; uint64_t f; @@ -107,6 +111,7 @@ void print_size(uint64_t size, const char *s) char c = 0; unsigned int i; + /* Find the best unit to display */ for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) { if (size >> d) { c = names[i]; @@ -116,12 +121,12 @@ void print_size(uint64_t size, const char *s) if (!c) { /* - * SPL tiny-printf is not capable for printing uint64_t. - * We have just checked that the size is small enought to fit + * SPL tiny-printf is not capable of printing uint64_t. + * We have just checked that the size is small enough to fit * unsigned int safely. */ - printf("%u Bytes%s", (unsigned int)size, s); - return; + sprintf(buf, "%u Bytes", (unsigned int)size); + return buf; } n = size >> d; @@ -143,11 +148,20 @@ void print_size(uint64_t size, const char *s) } } - printf ("%lu", n); - if (m) { - printf (".%ld", m); - } - printf (" %ciB%s", c, s); + if (m) + sprintf(buf, "%lu.%ld %ciB", n, m, c); + else + sprintf(buf, "%lu %ciB", n, c); + + return buf; +} + +void print_size(uint64_t size, const char *s) +{ + char buf[12]; + + format_size(buf, size); + printf("%s%s", buf, s); } #define MAX_LINE_LENGTH_BYTES 64 diff --git a/test/lib/test_print.c b/test/lib/test_print.c index cd7f3f85769..f9b04a3e3cb 100644 --- a/test/lib/test_print.c +++ b/test/lib/test_print.c @@ -68,3 +68,17 @@ static int lib_test_print_size(struct unit_test_state *uts) return 0; } LIB_TEST(lib_test_print_size, UTF_CONSOLE); + +static int lib_test_format_size(struct unit_test_state *uts) +{ + char buf[12]; + + ut_asserteq_str("321 Bytes", format_size(buf, 321)); + ut_asserteq_str("4.2 KiB", format_size(buf, 4321)); + ut_asserteq_str("53 KiB", format_size(buf, 54321)); + ut_asserteq_str("1 GiB", format_size(buf, 1073741824)); + ut_asserteq_str("49.4 TiB", format_size(buf, 54321987654321)); + + return 0; +} +LIB_TEST(lib_test_format_size, 0);