[PATCH 6/7] console: implement console_printf

Ahmad Fatoum
2026-04-13T10:11:55Z

printf formats to a buffer and then prints it out with puts().

Allow printing to arbitrary consoles while reusing this logic by adding
a new console_printf().

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
common/console_common.c | 29 ++++++++++++++++++++++++-----
include/console.h | 3 +++
2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/common/console_common.c b/common/console_common.c
index 86d9f523b1c5..310534affa58 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -266,7 +266,7 @@ int log_print(unsigned flags, unsigned levels)
return 0;
}

-int printf(const char *fmt, ...)
+int console_printf(struct console_device *con, const char *fmt, ...)
{
va_list args;
int i;
@@ -282,13 +282,13 @@ int printf(const char *fmt, ...)
va_end(args);

/* Print the string */
- puts(printbuffer);
+ console_puts(con, printbuffer);

return i;
}
-EXPORT_SYMBOL(printf);
+EXPORT_SYMBOL(console_printf);

-int vprintf(const char *fmt, va_list args)
+int console_vprintf(struct console_device *con, const char *fmt, va_list args)
{
int i;
char printbuffer[CFG_PBSIZE];
@@ -300,10 +300,29 @@ int vprintf(const char *fmt, va_list args)
i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);

/* Print the string */
- puts(printbuffer);
+ console_puts(con, printbuffer);

return i;
}
+EXPORT_SYMBOL(console_vprintf);
+
+int printf(const char *fmt, ...)
+{
+ va_list args;
+ int i;
+
+ va_start(args, fmt);
+ i = console_vprintf(CONSOLE_DEV_STDOUT, fmt, args);
+ va_end(args);
+
+ return i;
+}
+EXPORT_SYMBOL(printf);
+
+int vprintf(const char *fmt, va_list args)
+{
+ return console_vprintf(CONSOLE_DEV_STDOUT, fmt, args);
+}
EXPORT_SYMBOL(vprintf);

struct console_device *console_get_by_dev(struct device *dev)
diff --git a/include/console.h b/include/console.h
index 5a4dbcdf1327..ed5b501b65a4 100644
--- a/include/console.h
+++ b/include/console.h
@@ -248,6 +248,9 @@ int console_puts(struct console_device *con, const char *s);
void console_putbin(struct console_device *con, const u8 *str, size_t len);
void console_flush(void);

+int console_printf(struct console_device *con, const char *fmt, ...) __printf(2, 3);
+int console_vprintf(struct console_device *con, const char *fmt, va_list args);
+
int ctrlc(void);
int ctrlc_non_interruptible(void);
void ctrlc_handled(void);
--
2.47.3