Linux Framebuffer Layer development
 help / color / mirror / Atom feed
* [PATCH 0/4] drm: Safe font-data access in log/panic drawing
@ 2026-05-29 14:01 Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 1/4] lib/fonts: Look up glyph data with font_data_glyph_buf() Thomas Zimmermann
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Thomas Zimmermann @ 2026-05-29 14:01 UTC (permalink / raw)
  To: jfalempe, javierm, deller, maarten.lankhorst, mripard, airlied,
	simona
  Cc: dri-devel, linux-fbdev, Thomas Zimmermann

Looking up glyph shapes with a signed char in drm_draw_get_char_bitmap()
is unsafe. It also does not support extended ASCII codes with values
larger than 127.

Add the new function font_data_glyph_buf() to the font-data helpers. It
looks up the correct glyph from font data or returns NULL if no such
glyph exists. Convert DRM's log and panic code to the new function. Also
cast the character code to support all 256 ASCII characters.

Tested with drm_log on bochs.

Thomas Zimmermann (4):
  lib/fonts: Look up glyph data with font_data_glyph_buf()
  drm/client: log: Look up glyph shape with font helper
  drm/panic: Look up glyph shape with font helper
  drm/draw: Remove unused helper drm_draw_get_char_bitmap()

 drivers/gpu/drm/clients/drm_log.c   | 10 ++++++----
 drivers/gpu/drm/drm_draw_internal.h |  7 -------
 drivers/gpu/drm/drm_panic.c         |  6 ++++--
 include/linux/font.h                |  3 +++
 lib/fonts/fonts.c                   | 31 +++++++++++++++++++++++++++++
 5 files changed, 44 insertions(+), 13 deletions(-)

-- 
2.54.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] lib/fonts: Look up glyph data with font_data_glyph_buf()
  2026-05-29 14:01 [PATCH 0/4] drm: Safe font-data access in log/panic drawing Thomas Zimmermann
@ 2026-05-29 14:01 ` Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 2/4] drm/client: log: Look up glyph shape with font helper Thomas Zimmermann
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Zimmermann @ 2026-05-29 14:01 UTC (permalink / raw)
  To: jfalempe, javierm, deller, maarten.lankhorst, mripard, airlied,
	simona
  Cc: dri-devel, linux-fbdev, Thomas Zimmermann

Add font_data_glyph_buf() to retrieve a character's glyph data or NULL
otherwise. Console fonts can currently contain 256 or 512 glyphs. The
kernel-internal characters are of type char, unsigned short or unsigned
int. Catch all of them by accepting unsigned int. Callers possibly have
to cast from signed to unsigned types to reach all glyphs in a font.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
---
 include/linux/font.h |  3 +++
 lib/fonts/fonts.c    | 31 +++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/linux/font.h b/include/linux/font.h
index 6845f02d739a..ea23b727388b 100644
--- a/include/linux/font.h
+++ b/include/linux/font.h
@@ -101,6 +101,9 @@ font_data_t *font_data_import(const struct console_font *font, unsigned int vpit
 void font_data_get(font_data_t *fd);
 bool font_data_put(font_data_t *fd);
 unsigned int font_data_size(font_data_t *fd);
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+					 unsigned int width, unsigned int vpitch,
+					 unsigned int c);
 bool font_data_is_equal(font_data_t *lhs, font_data_t *rhs);
 int font_data_export(font_data_t *fd, struct console_font *font, unsigned int vpitch);
 
diff --git a/lib/fonts/fonts.c b/lib/fonts/fonts.c
index f5d5333450a0..4fc66722d00d 100644
--- a/lib/fonts/fonts.c
+++ b/lib/fonts/fonts.c
@@ -178,6 +178,37 @@ unsigned int font_data_size(font_data_t *fd)
 }
 EXPORT_SYMBOL_GPL(font_data_size);
 
+static unsigned int font_data_num_glyphs(font_data_t *fd, unsigned int width, unsigned int height)
+{
+	return font_data_size(fd) / font_glyph_size(width, height);
+}
+
+/**
+ * font_data_glyph_buf() - Returns the glyph for a specific character as raw bytes
+ * @fd: The font data
+ * @width: The glyph width in bits per scanline
+ * @vpitch: The number of scanlines per glyph
+ * @c: The character
+ *
+ * Glyphs start at fixed intervals within the font data. font_data_glyph_buf()
+ * returns the glyph shape of the specified character. If no such glyph
+ * exists in the font, it returns NULL.
+ *
+ * Returns:
+ * The character's raw glyph shape, or NULL if no glyph exists for the character. The
+ * provided buffer is read-only.
+ */
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+					 unsigned int width, unsigned int vpitch,
+					 unsigned int c)
+{
+	if (c >= font_data_num_glyphs(fd, width, vpitch))
+		return NULL;
+
+	return font_data_buf(fd) + font_glyph_size(width, vpitch) * c;
+}
+EXPORT_SYMBOL_GPL(font_data_glyph_buf);
+
 /**
  * font_data_is_equal - Compares font data for equality
  * @lhs: Left-hand side font data
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] drm/client: log: Look up glyph shape with font helper
  2026-05-29 14:01 [PATCH 0/4] drm: Safe font-data access in log/panic drawing Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 1/4] lib/fonts: Look up glyph data with font_data_glyph_buf() Thomas Zimmermann
@ 2026-05-29 14:01 ` Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 3/4] drm/panic: " Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 4/4] drm/draw: Remove unused helper drm_draw_get_char_bitmap() Thomas Zimmermann
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Zimmermann @ 2026-05-29 14:01 UTC (permalink / raw)
  To: jfalempe, javierm, deller, maarten.lankhorst, mripard, airlied,
	simona
  Cc: dri-devel, linux-fbdev, Thomas Zimmermann

Look up glyph shapes with font_data_glyph_buf(). Handle non-existing
glyphs gracefully. Enable extended ASCII by casting to unsigned char.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
---
 drivers/gpu/drm/clients/drm_log.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/clients/drm_log.c b/drivers/gpu/drm/clients/drm_log.c
index 8d21b785bead..e3e02c84a4cf 100644
--- a/drivers/gpu/drm/clients/drm_log.c
+++ b/drivers/gpu/drm/clients/drm_log.c
@@ -122,10 +122,12 @@ static void drm_log_draw_line(struct drm_log_scanout *scanout, const char *s,
 	iosys_map_incr(&map, r.y1 * fb->pitches[0]);
 	for (i = 0; i < len && i < scanout->columns; i++) {
 		u32 color = (i < prefix_len) ? scanout->prefix_color : scanout->front_color;
-		src = drm_draw_get_char_bitmap(font, s[i], font_pitch);
-		drm_log_blit(&map, fb->pitches[0], src, font_pitch,
-			     scanout->scaled_font_h, scanout->scaled_font_w,
-			     px_width, color);
+		src = font_data_glyph_buf(font->data, font->width, font->height,
+					  (unsigned char)s[i]);
+		if (src)
+			drm_log_blit(&map, fb->pitches[0], src, font_pitch,
+				     scanout->scaled_font_h, scanout->scaled_font_w,
+				     px_width, color);
 		iosys_map_incr(&map, scanout->scaled_font_w * px_width);
 	}
 
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] drm/panic: Look up glyph shape with font helper
  2026-05-29 14:01 [PATCH 0/4] drm: Safe font-data access in log/panic drawing Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 1/4] lib/fonts: Look up glyph data with font_data_glyph_buf() Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 2/4] drm/client: log: Look up glyph shape with font helper Thomas Zimmermann
@ 2026-05-29 14:01 ` Thomas Zimmermann
  2026-05-29 14:01 ` [PATCH 4/4] drm/draw: Remove unused helper drm_draw_get_char_bitmap() Thomas Zimmermann
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Zimmermann @ 2026-05-29 14:01 UTC (permalink / raw)
  To: jfalempe, javierm, deller, maarten.lankhorst, mripard, airlied,
	simona
  Cc: dri-devel, linux-fbdev, Thomas Zimmermann

Look up glyph shapes with font_data_glyph_buf(). Handle non-existing
glyphs gracefully. Enable extended ASCII by casting to unsigned char.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
---
 drivers/gpu/drm/drm_panic.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/drm_panic.c b/drivers/gpu/drm/drm_panic.c
index d6d3b8d85dea..e576c4791861 100644
--- a/drivers/gpu/drm/drm_panic.c
+++ b/drivers/gpu/drm/drm_panic.c
@@ -443,9 +443,11 @@ static void draw_txt_rectangle(struct drm_scanout_buffer *sb,
 			rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2;
 
 		for (j = 0; j < line_len; j++) {
-			src = drm_draw_get_char_bitmap(font, msg[i].txt[j], font_pitch);
+			src = font_data_glyph_buf(font->data, font->width, font->height,
+						  (unsigned char)msg[i].txt[j]);
 			rec.x2 = rec.x1 + font->width;
-			drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
+			if (src)
+				drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
 			rec.x1 += font->width;
 		}
 	}
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] drm/draw: Remove unused helper drm_draw_get_char_bitmap()
  2026-05-29 14:01 [PATCH 0/4] drm: Safe font-data access in log/panic drawing Thomas Zimmermann
                   ` (2 preceding siblings ...)
  2026-05-29 14:01 ` [PATCH 3/4] drm/panic: " Thomas Zimmermann
@ 2026-05-29 14:01 ` Thomas Zimmermann
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Zimmermann @ 2026-05-29 14:01 UTC (permalink / raw)
  To: jfalempe, javierm, deller, maarten.lankhorst, mripard, airlied,
	simona
  Cc: dri-devel, linux-fbdev, Thomas Zimmermann

Glyph-shape lookup has been integrated into the font-data interface
and all callers have been updated. Remove the old helper.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
---
 drivers/gpu/drm/drm_draw_internal.h | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/drivers/gpu/drm/drm_draw_internal.h b/drivers/gpu/drm/drm_draw_internal.h
index 261967145635..44ddcee4744c 100644
--- a/drivers/gpu/drm/drm_draw_internal.h
+++ b/drivers/gpu/drm/drm_draw_internal.h
@@ -7,7 +7,6 @@
 #ifndef __DRM_DRAW_INTERNAL_H__
 #define __DRM_DRAW_INTERNAL_H__
 
-#include <linux/font.h>
 #include <linux/types.h>
 
 struct iosys_map;
@@ -18,12 +17,6 @@ static inline bool drm_draw_is_pixel_fg(const u8 *sbuf8, unsigned int spitch, in
 	return (sbuf8[(y * spitch) + x / 8] & (0x80 >> (x % 8))) != 0;
 }
 
-static inline const u8 *drm_draw_get_char_bitmap(const struct font_desc *font,
-						 char c, size_t font_pitch)
-{
-	return font->data + (c * font->height) * font_pitch;
-}
-
 bool drm_draw_can_convert_from_xrgb8888(u32 format);
 
 u32 drm_draw_color_from_xrgb8888(u32 color, u32 format);
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-05-29 14:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-29 14:01 [PATCH 0/4] drm: Safe font-data access in log/panic drawing Thomas Zimmermann
2026-05-29 14:01 ` [PATCH 1/4] lib/fonts: Look up glyph data with font_data_glyph_buf() Thomas Zimmermann
2026-05-29 14:01 ` [PATCH 2/4] drm/client: log: Look up glyph shape with font helper Thomas Zimmermann
2026-05-29 14:01 ` [PATCH 3/4] drm/panic: " Thomas Zimmermann
2026-05-29 14:01 ` [PATCH 4/4] drm/draw: Remove unused helper drm_draw_get_char_bitmap() Thomas Zimmermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox