mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Subject: [PATCH 9/9] efi: reimplement query_console_serial using term_getsize
Date: Fri,  1 May 2026 09:19:21 +0200	[thread overview]
Message-ID: <20260501071927.961370-1-a.fatoum@barebox.org> (raw)
In-Reply-To: <20260501070625.952091-1-a.fatoum@barebox.org>

query_console_serial() used printf() to send terminal size query
escape sequences, which go to _all_ active consoles. This corrupted
the fbconsole cursor position because fbconsole receives the
ESC [999;999H sequence and moves its cursor to the bottom-right
corner.

Replace the custom implementation with term_getsize() from lib/term.c,
which calculates the minimum terminal size across all consoles,
including the framebuffer console.

Link: https://fosstodon.org/@otte_homan@theblower.au/116229733790452489
Reported-by: @otte_homan@theblower.au
Fixes: 65ac28276e5d ("efi: loader: protocol: add console support")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 efi/loader/protocols/console.c | 119 +--------------------------------
 1 file changed, 2 insertions(+), 117 deletions(-)

diff --git a/efi/loader/protocols/console.c b/efi/loader/protocols/console.c
index 211e725cfd7b..113443db09d6 100644
--- a/efi/loader/protocols/console.c
+++ b/efi/loader/protocols/console.c
@@ -20,6 +20,7 @@
 #include <efi/protocol/text.h>
 #include <efi/error.h>
 #include <efi/guid.h>
+#include <term.h>
 
 #ifdef DEBUG
 #include <efi/loader/trace.h>
@@ -83,76 +84,6 @@ static struct simple_text_output_mode efi_con_mode = {
 	.cursor_visible = 1,
 };
 
-/**
- * term_get_char() - read a character from the console
- *
- * Wait for up to 100 ms to read a character from the console.
- *
- * @c:		pointer to the buffer to receive the character
- * Return:	0 on success, 1 otherwise
- */
-static int term_get_char(s32 *c)
-{
-	u64 timeout;
-
-	/* Wait up to 100 ms for a character */
-	timeout = get_time_ns();
-
-	while (!tstc())
-		if (is_timeout(timeout, 100 * MSECOND))
-			return 1;
-
-	*c = getchar();
-	return 0;
-}
-
-/**
- * term_read_reply() - receive and parse a reply from the terminal
- *
- * @n:		array of return values
- * @num:	number of return values expected
- * @end_char:	character indicating end of terminal message
- * Return:	non-zero indicates error
- */
-static int term_read_reply(int *n, int num, char end_char)
-{
-	s32 c;
-	int i = 0;
-
-	if (term_get_char(&c) || c != cESC)
-		return -1;
-
-	if (term_get_char(&c) || c != '[')
-		return -1;
-
-	n[0] = 0;
-	while (1) {
-		if (!term_get_char(&c)) {
-			if (c == ';') {
-				i++;
-				if (i >= num)
-					return -1;
-				n[i] = 0;
-				continue;
-			} else if (c == end_char) {
-				break;
-			} else if (c > '9' || c < '0') {
-				return -1;
-			}
-
-			/* Read one more decimal position */
-			n[i] *= 10;
-			n[i] += c - '0';
-		} else {
-			return -1;
-		}
-	}
-	if (i != num - 1)
-		return -1;
-
-	return 0;
-}
-
 /**
  * efi_cout_output_string() - write Unicode string to console
  *
@@ -270,58 +201,12 @@ static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
 	return (mode->rows == rows) && (mode->columns == cols);
 }
 
-/**
- * query_console_serial() - query serial console size
- *
- * When using a serial console or the net console we can only devise the
- * terminal size by querying the terminal using ECMA-48 control sequences.
- *
- * @rows:	pointer to return number of rows
- * @cols:	pointer to return number of columns
- * Returns:	0 on success
- */
-static int query_console_serial(int *rows, int *cols)
-{
-	int ret = 0;
-	int n[2];
-
-	/* Empty input buffer */
-	while (tstc())
-		getchar();
-
-	/*
-	 * Not all terminals understand CSI [18t for querying the console size.
-	 * We should adhere to escape sequences documented in the console_codes
-	 * man page and the ECMA-48 standard.
-	 *
-	 * So here we follow a different approach. We position the cursor to the
-	 * bottom right and query its position. Before leaving the function we
-	 * restore the original cursor position.
-	 */
-	printf(ESC "7"		/* Save cursor position */
-	       ESC "[r"		/* Set scrolling region to full window */
-	       ESC "[999;999H"	/* Move to bottom right corner */
-	       ESC "[6n");	/* Query cursor position */
-
-	/* Read {rows,cols} */
-	if (term_read_reply(n, 2, 'R')) {
-		ret = 1;
-		goto out;
-	}
-
-	*cols = n[1];
-	*rows = n[0];
-out:
-	printf(ESC "8");	/* Restore cursor position */
-	return ret;
-}
-
 static void efi_setup_console_size(void)
 {
 	int rows = 25, cols = 80;
 	int ret = -ENODEV;
 
-	ret = query_console_serial(&rows, &cols);
+	ret = term_getsize(&cols, &rows);
 	if (ret)
 		return;
 
-- 
2.47.3




  parent reply	other threads:[~2026-05-01  7:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-01  6:53 [PATCH 0/9] lib: term: fix fbconsole cursor desynchronization Ahmad Fatoum
2026-05-01  6:53 ` [PATCH 1/9] lib: term: avoid printing NUL with new new console_puts API Ahmad Fatoum
2026-05-01  6:53 ` [PATCH 2/9] lib: term: return error code from term_getsize() Ahmad Fatoum
2026-05-01  6:53 ` [PATCH 3/9] lib: term: add per-console terminal response parser Ahmad Fatoum
2026-05-01  6:53 ` [PATCH 4/9] lib: term: factor out single cdev handling from term_getsize Ahmad Fatoum
2026-05-01  6:53 ` [PATCH 5/9] lib: term: fix term_getsize cursor restore Ahmad Fatoum
2026-05-01  6:54 ` [PATCH 6/9] console: add get_size callback for direct size reporting Ahmad Fatoum
2026-05-01  6:54 ` [PATCH 7/9] video: fbconsole: implement get_size Ahmad Fatoum
2026-05-01  6:54 ` [PATCH 8/9] console: add per-console terminal.size parameter Ahmad Fatoum
2026-05-01  7:19 ` Ahmad Fatoum [this message]
2026-05-07 10:38 ` [PATCH 0/9] lib: term: fix fbconsole cursor desynchronization Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260501071927.961370-1-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox