[Concept,12/13] console: Add putsn() to ulib

Message ID 20260204001002.2638622-13-sjg@u-boot.org
State New
Headers
Series Add putsn() for length-based console output |

Commit Message

Simon Glass Feb. 4, 2026, 12:09 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add the ulib_putsn() function to the U-Boot library interface for
standalone applications. This provides length-based string output
without requiring nul-termination.

The implementation outputs characters one at a time using the exported
putc() function, making it available to standalone applications without
requiring changes to the jump table.

Co-developed-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 include/u-boot-lib.h | 15 +++++++++++++++
 lib/ulib/ulib.c      | 10 ++++++++++
 2 files changed, 25 insertions(+)
  

Patch

diff --git a/include/u-boot-lib.h b/include/u-boot-lib.h
index 934cc33eff5..034ff12a344 100644
--- a/include/u-boot-lib.h
+++ b/include/u-boot-lib.h
@@ -38,4 +38,19 @@  void ulib_uninit(void);
  */
 const char *ulib_get_version(void);
 
+/**
+ * ulib_putsn() - Write a string with specified length
+ *
+ * This outputs exactly @len characters from @s, regardless of any nul
+ * characters that may be present. This is useful for printing substrings
+ * or binary data with embedded nuls.
+ *
+ * If CONFIG_CONSOLE_PUTSN is enabled, this calls putsn() directly.
+ * Otherwise, it outputs characters one at a time using putc().
+ *
+ * @s: String to output (need not be nul-terminated)
+ * @len: Number of characters to output
+ */
+void ulib_putsn(const char *s, int len);
+
 #endif
diff --git a/lib/ulib/ulib.c b/lib/ulib/ulib.c
index c957ae6ba67..bb2cbc7cbac 100644
--- a/lib/ulib/ulib.c
+++ b/lib/ulib/ulib.c
@@ -6,6 +6,7 @@ 
  * Written by Simon Glass <simon.glass@canonical.com>
  */
 
+#include <stdio.h>
 #include <string.h>
 #include <version.h>
 #include <asm/global_data.h>
@@ -36,3 +37,12 @@  const char *ulib_get_version(void)
 {
 	return version_string;
 }
+
+void ulib_putsn(const char *s, int len)
+{
+	if (CONFIG_IS_ENABLED(CONSOLE_PUTSN))
+		putsn(s, len);
+	else
+		while (len--)
+			putc(*s++);
+}