From patchwork Wed Feb 4 00:09:55 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1805 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=1770163868; bh=xBCNADqWb//JLjgEznKazz6FyRU0JSCrh+XImg49WS8=; 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=QoYP3jvIfiUsiO5SAzQOAb/F752/9BfZRPMt7oUjEIX37CqIqejMCzubjYStcVP8k hOSLL5a76fyZHvJ+hc+M3cEUnm3X0I33jNXt08Xmwbe+JKFzcGkFeM/SHx1W4ebEAp VVmcUvoJ7bpU3m9PdzIDSmLirzYtu72ig4crJCHoKsWmS9XulD2m0zKnSRbA8S3mQR oCFY9USi9Dw+YBUFlNDH1uyaCD7rmkIptmHT1zZzgzoDg1eWUTIu25w3+DKLqUrBmd CcSsSTgQ0Ljc5cWX98+k78+VzUQqFi6uh6Z1aP4lpcyWkSwhBhLQVgvW+zYnmFjTm6 w/0lboEjRgnuw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 58503698FD for ; Tue, 3 Feb 2026 17:11:08 -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 a591Fa7dAuOi for ; Tue, 3 Feb 2026 17:11:08 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1770163868; bh=xBCNADqWb//JLjgEznKazz6FyRU0JSCrh+XImg49WS8=; 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=QoYP3jvIfiUsiO5SAzQOAb/F752/9BfZRPMt7oUjEIX37CqIqejMCzubjYStcVP8k hOSLL5a76fyZHvJ+hc+M3cEUnm3X0I33jNXt08Xmwbe+JKFzcGkFeM/SHx1W4ebEAp VVmcUvoJ7bpU3m9PdzIDSmLirzYtu72ig4crJCHoKsWmS9XulD2m0zKnSRbA8S3mQR oCFY9USi9Dw+YBUFlNDH1uyaCD7rmkIptmHT1zZzgzoDg1eWUTIu25w3+DKLqUrBmd CcSsSTgQ0Ljc5cWX98+k78+VzUQqFi6uh6Z1aP4lpcyWkSwhBhLQVgvW+zYnmFjTm6 w/0lboEjRgnuw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4591B69911 for ; Tue, 3 Feb 2026 17:11:08 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1770163866; bh=H3tf9jSaESQ2b2pSOBCRLYCdoquouBheJNTSamUjEnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EyYjXaKCUH/x1WvKYwS0wzYcalWWTshSdWA0PBBrs1ULUhCOY4UUAnjky2g5yL74u tBUJQaHrbUhX3w+WZF/XwGhpYEIR9jj+vGTgzUEnc4JC2DMikGM6RUESbfb2N4LfPt 64bmL0W+b4wdz6amshPmSC8Y31DHMl0J8EreL1c81VkFrEQglxDtxcahTBoZEzJWqU DOQ3u0DxFMgfpEf4g/JazFOu6T3JqDqwjsi9xD/f4evruOQ2e1MVoNFtwdGoEgKgYE 9RflWXKrIaq+ggEMULrrLYvi7YxoSyTlDMugyaVsmrp4QtxU7WNob50ARjtHrnmHfz 4RCktdK/9eS4w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 71AE2698FD; Tue, 3 Feb 2026 17:11:06 -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 mB7l8i6Sdg9s; Tue, 3 Feb 2026 17:11:06 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1770163862; bh=FdAvMB64OpeSs+oh+g60SrCiUhbjFXoJqhqgBpsG2wc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lq7FjPjvyFektI6KgWnrjtrewsWkOV6VxhkR4+mF/yCExZLH6nO1r3mY2TKlAPb1B 8TSVDvS+BQ+mVUWcDlHlQO2vr8TWAhRkiu1h8nr1INU/UMQeIlBjgxASrtKpXQK4hI dDZEwrUrC3tGH57MOoPmYrTHkbr/sMewl2IAk/3pIyDwBW9Ag2fyA4zlohj7Jc2JJB SVY+om1rnksN3bfbRji801Hw4BQHYVr6D+YFVODx3jetzCMGUqSI3akI8dGmeE61WP 1G0OVEiqkRzcn7XU6nW2sDEm3J9KqkAoHYpBoqBkkLQgYjilxlHR9slb+WJi5bwckz ISHgSCcLYv4Hw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 2094A69909; Tue, 3 Feb 2026 17:11:02 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 3 Feb 2026 17:09:55 -0700 Message-ID: <20260204001002.2638622-12-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260204001002.2638622-1-sjg@u-boot.org> References: <20260204001002.2638622-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: 3A5337S2K6DEWG7POTQO53COJQPSXQ24 X-Message-ID-Hash: 3A5337S2K6DEWG7POTQO53COJQPSXQ24 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: Simon Glass , "Claude Sonnet 4 . 5" X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 11/13] console: Add err_putsn() for length-based error output 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 Add err_putsn() to output errors with a specified length, which is more efficient when the length is already known (e.g., from snprintf()). Create console_putsn_select() which uses device putsn() methods when CONFIG_CONSOLE_PUTSN is enabled, falling back to puts() otherwise. Refactor err_puts() to call err_putsn() and update err_printf() to use the length returned by vscnprintf(). Co-developed-by: Claude Sonnet 4.5 Signed-off-by: Simon Glass --- common/console.c | 59 +++++++++++++++++++++++++++++++++++++++-------- include/console.h | 15 ++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/common/console.c b/common/console.c index f9669a0e028..96e8977ce06 100644 --- a/common/console.c +++ b/common/console.c @@ -291,30 +291,56 @@ static void console_putc_pager(int file, const char c) } /** - * console_puts_select() - Output a string to all console devices + * console_putsn_select() - Output a string with length to console devices * * @file: File number to output to (e,g, stdout, see stdio.h) * @serial_only: true to output only to serial, false to output to everything * else * @s: String to output + * @len: Length of string to output */ -static void console_puts_select(int file, bool serial_only, const char *s) +static void console_putsn_select(int file, bool serial_only, const char *s, + int len) { + struct stdio_dev *sdev; int i; struct stdio_dev *dev; for_each_console_dev(i, file, dev) { bool is_serial = console_dev_is_serial(dev); - if (dev->puts && serial_only == is_serial) - dev->puts(dev, s); + if (serial_only == is_serial) { + sdev = dev; + if (CONFIG_IS_ENABLED(CONSOLE_PUTSN) && sdev->putsn) + sdev->putsn(sdev, s, len); + else if (sdev->puts) + sdev->puts(sdev, s); + } } } -void err_puts(bool serial_only, const char *s) +/** + * console_puts_select() - Output a string to all console devices + * + * @file: File number to output to (e,g, stdout, see stdio.h) + * @serial_only: true to output only to serial, false to output to everything + * else + * @s: String to output + */ +static void console_puts_select(int file, bool serial_only, const char *s) +{ + console_putsn_select(file, serial_only, s, strlen(s)); +} + +void err_putsn(bool serial_only, const char *s, int len) { if (gd->flags & GD_FLG_DEVINIT) - console_puts_select(stderr, serial_only, s); + console_putsn_select(stderr, serial_only, s, len); +} + +void err_puts(bool serial_only, const char *s) +{ + err_putsn(serial_only, s, strlen(s)); } int err_printf(bool serial_only, const char *fmt, ...) @@ -330,7 +356,7 @@ int err_printf(bool serial_only, const char *fmt, ...) */ ret = vscnprintf(buf, sizeof(buf), fmt, args); va_end(args); - err_puts(serial_only, buf); + err_putsn(serial_only, buf, ret); return ret; } @@ -472,11 +498,24 @@ static inline void console_putc_pager(int file, const char c) stdio_devices[file]->putc(stdio_devices[file], c); } -void console_puts_select(int file, bool serial_only, const char *s) +static inline void console_putsn_select(int file, bool serial_only, + const char *s, int len) { + struct stdio_dev *sdev; + if ((gd->flags & GD_FLG_DEVINIT) && - serial_only == console_dev_is_serial(stdio_devices[file])) - stdio_devices[file]->puts(stdio_devices[file], s); + serial_only == console_dev_is_serial(stdio_devices[file])) { + sdev = stdio_devices[file]; + if (CONFIG_IS_ENABLED(CONSOLE_PUTSN) && sdev->putsn) + sdev->putsn(sdev, s, len); + else + sdev->puts(sdev, s); + } +} + +void console_puts_select(int file, bool serial_only, const char *s) +{ + console_putsn_select(file, serial_only, s, strlen(s)); } static inline void console_puts_pager(int file, const char *s) diff --git a/include/console.h b/include/console.h index 83b8d4f642c..486131f8bf7 100644 --- a/include/console.h +++ b/include/console.h @@ -158,6 +158,21 @@ static inline bool console_record_isempty(void) */ int console_announce_r(void); +/** + * err_putsn() - Output a string with length to selected console devices + * + * This writes to stderr only. It is useful for outputting errors when the + * length is already known. + * + * Outputs exactly @len characters from @s, regardless of any nul characters. + * + * @serial_only: true to output only to serial, false to output to everything + * else + * @s: String to output (need not be nul-terminated) + * @len: Number of characters to output + */ +void err_putsn(bool serial_only, const char *s, int len); + /** * err_puts() - Output a string to selected console devices *