[Concept,06/13] sandbox: Add support for length-based console output

Message ID 20260204001002.2638622-7-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 os_putsn() to support length-based output in sandbox environments.
This allows putsn() to work correctly before the serial console is
initialised.

The implementation uses os_write() directly for efficiency, and os_puts()
is refactored to use os_putsn() to reduce code duplication.

Note this changes sandbox to write all characters in one call.

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

 arch/sandbox/cpu/os.c |  8 ++++++--
 include/os.h          | 13 +++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)
  

Patch

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 5278ce55766..142b685e031 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -827,10 +827,14 @@  void os_putc(int ch)
 	os_write(1, &ch, 1);
 }
 
+void os_putsn(const char *str, int len)
+{
+	os_write(1, str, len);
+}
+
 void os_puts(const char *str)
 {
-	while (*str)
-		os_putc(*str++);
+	os_putsn(str, strlen(str));
 }
 
 void os_flush(void)
diff --git a/include/os.h b/include/os.h
index 3ea88230af3..bc556f2195f 100644
--- a/include/os.h
+++ b/include/os.h
@@ -385,6 +385,19 @@  void os_putc(int ch);
  */
 void os_puts(const char *str);
 
+/**
+ * os_putsn() - write a string with length to controlling OS terminal
+ *
+ * This bypasses the U-Boot console support and writes directly to the OS
+ * stdout file descriptor.
+ *
+ * Outputs exactly @len characters from @str, regardless of any nul characters.
+ *
+ * @str:	String to write (need not be nul-terminated)
+ * @len:	Number of characters to write
+ */
+void os_putsn(const char *str, int len);
+
 /**
  * os_flush() - flush controlling OS terminal
  *