mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH] fbconsole: make UTF-8 the default encoding instead of CP437
Date: Sat,  2 May 2026 13:29:40 +0200	[thread overview]
Message-ID: <20260502112941.1829273-1-a.fatoum@barebox.org> (raw)

The Linux kernel fonts that were ported to barebox were all (and still
are in Linux) CP437 fonts.

This doesn't matter when the framebuffer console is only displaying
barebox' own 7-bit ASCII output, but in combination with barebox running
as EFI loader, the framebuffer console may end up being EFI stdout:

- EFI applications like GRUB output UTF-16
- barebox' efi/loader/protocols/console.c duly converts it into UTF-8
- barebox' Framebuffer console processes that thinking it's CP437

To be able to use e.g. the box drawing characters that we already have
in the CP437, let's change the framebuffer console default to expect
UTF-8. This will increase code size a bit, but is the appropriate thing
to do nowadays. Still gate it behind an option that allows restoring the
old behavior.

Reported-by: Lucie L. Hartmann
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/video/Kconfig     | 14 ++++++++++++++
 drivers/video/fbconsole.c | 24 ++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
index d9f49724c819..ce1023722142 100644
--- a/drivers/video/Kconfig
+++ b/drivers/video/Kconfig
@@ -13,6 +13,20 @@ config FRAMEBUFFER_CONSOLE
 	select FONTS
 	prompt "framebuffer console support"
 
+config FRAMEBUFFER_CONSOLE_UTF8
+	bool "UTF-8 output for framebuffer console"
+	depends on FRAMEBUFFER_CONSOLE
+	select CHARSET
+	default y
+	help
+	  Maps UTF-8 console output to CP437 font positions, enabling
+	  the framebuffer console to render accented Latin characters,
+	  mathematical symbols, and additional box-drawing characters
+	  using the existing bitmap fonts.
+
+	  Say n here if your own output is intentionally CP437 or you want
+	  to save the few hundred bytes needed to do UTF-8 decoding.
+
 config DRIVER_VIDEO_FB_SSD1307
 	bool "Solomon SSD1307 framebuffer support"
 	depends on (I2C || SPI) && GPIOLIB
diff --git a/drivers/video/fbconsole.c b/drivers/video/fbconsole.c
index ab1c4dfed0a4..f44a3e0636ab 100644
--- a/drivers/video/fbconsole.c
+++ b/drivers/video/fbconsole.c
@@ -10,6 +10,8 @@
 #include <gui/image_renderer.h>
 #include <gui/graphic_utils.h>
 #include <linux/font.h>
+#include <linux/ctype.h>
+#include <charset.h>
 
 enum state_t {
 	LIT,				/* Literal input */
@@ -76,6 +78,8 @@ struct fbc_priv {
 
 	int active;
 	int in_console;
+
+	char utf8_buf[5];	/* UTF-8 stream decoder buffer */
 };
 
 static int fbc_getc(struct console_device *cdev)
@@ -630,10 +634,26 @@ static void fbc_putc(struct console_device *cdev, char c)
 	case LIT:
 		switch (c) {
 		case '\033':
+			priv->utf8_buf[0] = '\0';
 			priv->state = ESC;
 			break;
-		default:
-			printchar(priv, c);
+		case '\0':
+			break;
+		default: {
+			int cp = c;
+
+			if (IS_ENABLED(CONFIG_FRAMEBUFFER_CONSOLE_UTF8)) {
+				/* All our fonts are CP437, so convert it
+				 * directly to that code page.
+				 */
+				cp = utf8_to_cp437_stream(c, priv->utf8_buf);
+				if (!cp)
+					break;
+			}
+
+			printchar(priv, cp);
+			break;
+		}
 		}
 		break;
 	case ESC:
-- 
2.47.3




             reply	other threads:[~2026-05-02 11:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-02 11:29 Ahmad Fatoum [this message]
2026-05-07 10:38 ` 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=20260502112941.1829273-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