[PATCH 8/9] console: add per-console terminal.size parameter

Ahmad Fatoum a.fatoum at barebox.org
Thu Apr 30 23:54:02 PDT 2026


Expose a read-only "size" device parameter on every registered console.
Reading it queries the console dimensions via term_cdev_get_size() and
returns a WxH string (e.g. "80x25").

For consoles that will implement the get_size callback like fbconsole,
the size is read directly. For consoles with both stdin and stdout
active the size is probed using ANSI escape sequences. Consoles that
are output-only or cannot be queried return an error.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 common/console.c  | 18 ++++++++++++++++++
 include/console.h |  2 ++
 2 files changed, 20 insertions(+)

diff --git a/common/console.c b/common/console.c
index 8b6824b85e43..6b484b67bc45 100644
--- a/common/console.c
+++ b/common/console.c
@@ -10,6 +10,7 @@
 #include <malloc.h>
 #include <param.h>
 #include <console.h>
+#include <term.h>
 #include <driver.h>
 #include <fs.h>
 #include <of.h>
@@ -216,6 +217,20 @@ static int console_baudrate_set(struct param_d *param, void *priv)
 	return console_set_baudrate(cdev, cdev->baudrate_param);
 }
 
+static int console_size_get(struct param_d *param, void *priv)
+{
+	struct console_device *cdev = priv;
+	int rows = 0, cols = 0;
+
+	/* Parameter will just report 0x0 on errors) */
+	(void)term_cdev_get_size(cdev, &cols, &rows);
+
+	free(cdev->term_size);
+	cdev->term_size = xasprintf("%ux%u", cols, rows);
+
+	return 0;
+}
+
 static void console_init_early(void)
 {
 	kfifo_init(console_input_fifo, console_input_buffer,
@@ -391,6 +406,9 @@ int console_register(struct console_device *newcdev)
 	dev_add_param_string(dev, "active", console_active_set, console_active_get,
 			     &newcdev->active_string, newcdev);
 
+	dev_add_param_string(dev, "terminal.size", NULL, console_size_get,
+			     &newcdev->term_size, newcdev);
+
 	if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_FIRST)) {
 		if (list_empty(&console_list))
 			activate = CONSOLE_STDIOE;
diff --git a/include/console.h b/include/console.h
index f2b8da1546ef..cfd2480f30d3 100644
--- a/include/console.h
+++ b/include/console.h
@@ -58,6 +58,8 @@ struct console_device {
 	unsigned int baudrate;
 	unsigned int baudrate_param;
 
+	char *term_size;
+
 	const char *linux_console_name;
 	const char *linux_earlycon_name;
 	void __iomem *phys_base;
-- 
2.47.3




More information about the barebox mailing list