]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lib/fonts: Create font_data_t from struct console_font with font_data_import()
authorThomas Zimmermann <tzimmermann@suse.de>
Mon, 9 Mar 2026 14:14:53 +0000 (15:14 +0100)
committerHelge Deller <deller@gmx.de>
Mon, 9 Mar 2026 14:47:21 +0000 (15:47 +0100)
Add font_data_import() and update consoles to use it.

The implementation of font_data_import() is based on code from fbcon,
which supports overflow checks and crc32 checksums. Fbcon uses the crc32
checksum.

Newport_con now implements the same overflow checks as fbcon. As before,
this console does not support checksums, which are optional. Newport_con
can now also handle input font data with a vertical pitch other than 32
bytes. (The vertical pitch is the offset between two glyphs in the font
data.)

As an internal change, remove the const qualifier from the data field
if struct font_data. This allows font_data_import() to write the data
without type casting. For all users of the font data via font_data_t,
the stored data is still read only.

v3:
- fix typos

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: Helge Deller <deller@gmx.de>
drivers/video/console/newport_con.c
drivers/video/fbdev/core/fbcon.c
include/linux/font.h
lib/fonts/fonts.c

index db0228bce00e83ed0f36a27c5612c39081d38424..e88ff3a93b779d282d7ff5df3134a76a7b8f0be7 100644 (file)
@@ -501,31 +501,17 @@ static int newport_set_font(int unit, const struct console_font *op,
 {
        int w = op->width;
        int h = op->height;
-       int size = h * op->charcount;
        int i;
        font_data_t *new_data;
-       unsigned char *data = op->data, *p;
 
        /* ladis: when I grow up, there will be a day... and more sizes will
         * be supported ;-) */
-       if ((w != 8) || (h != 16) || (vpitch != 32)
-           || (op->charcount != 256 && op->charcount != 512))
+       if (w != 8 || h != 16 || (op->charcount != 256 && op->charcount != 512))
                return -EINVAL;
 
-       if (!(new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size,
-            GFP_USER))) return -ENOMEM;
-
-       new_data += FONT_EXTRA_WORDS * sizeof(int);
-       FNTSIZE(new_data) = size;
-       REFCOUNT(new_data) = 1; /* usage counter */
-       FNTSUM(new_data) = 0;
-
-       p = (unsigned char *)font_data_buf(new_data);
-       for (i = 0; i < op->charcount; i++) {
-               memcpy(p, data, h);
-               data += 32;
-               p += h;
-       }
+       new_data = font_data_import(op, vpitch, NULL);
+       if (IS_ERR(new_data))
+               return PTR_ERR(new_data);
 
        /* check if font is already used by other console */
        for (i = 0; i < MAX_NR_CONSOLES; i++) {
index 00255ac92e42834ba51aa2605c38c2c24942dc85..53677c09a0ec77a82f71cbac966c0b0b6823fc15 100644 (file)
@@ -2039,8 +2039,6 @@ static void updatescrollmode(struct fbcon_display *p,
        updatescrollmode_accel(p, info, vc);
 }
 
-#define PITCH(w) (((w) + 7) >> 3)
-
 static int fbcon_resize(struct vc_data *vc, unsigned int width,
                        unsigned int height, bool from_user)
 {
@@ -2424,7 +2422,6 @@ static int fbcon_do_set_font(struct vc_data *vc, int w, int h, int charcount,
        resize = (w != vc->vc_font.width) || (h != vc->vc_font.height);
        p->fontdata = data;
        vc->vc_font.data = font_data_buf(p->fontdata);
-
        old_width = vc->vc_font.width;
        old_height = vc->vc_font.height;
        old_charcount = vc->vc_font.charcount;
@@ -2482,11 +2479,8 @@ static int fbcon_set_font(struct vc_data *vc, const struct console_font *font,
        unsigned charcount = font->charcount;
        int w = font->width;
        int h = font->height;
-       int size, alloc_size;
-       int i, csum, ret;
+       int i, ret;
        font_data_t *new_data;
-       const u8 *data = font->data;
-       int pitch = PITCH(font->width);
 
        /* Is there a reason why fbconsole couldn't handle any charcount >256?
         * If not this check should be changed to charcount < 256 */
@@ -2510,34 +2504,10 @@ static int fbcon_set_font(struct vc_data *vc, const struct console_font *font,
        if (fbcon_invalid_charcount(info, charcount))
                return -EINVAL;
 
-       /* Check for integer overflow in font size calculation */
-       if (check_mul_overflow(h, pitch, &size) ||
-           check_mul_overflow(size, charcount, &size))
-               return -EINVAL;
-
-       /* Check for overflow in allocation size calculation */
-       if (check_add_overflow(FONT_EXTRA_WORDS * sizeof(int), size, &alloc_size))
-               return -EINVAL;
-
-       new_data = kmalloc(alloc_size, GFP_USER);
-
-       if (!new_data)
-               return -ENOMEM;
-
-       memset((u8 *)new_data, 0, FONT_EXTRA_WORDS * sizeof(int));
-
-       new_data += FONT_EXTRA_WORDS * sizeof(int);
-       FNTSIZE(new_data) = size;
-       REFCOUNT(new_data) = 1; /* usage counter */
-       for (i=0; i< charcount; i++) {
-               memcpy((u8 *)new_data + i * h * pitch, data + i * vpitch * pitch, h * pitch);
-       }
-
-       /* Since linux has a nice crc32 function use it for counting font
-        * checksums. */
-       csum = crc32(0, new_data, size);
+       new_data = font_data_import(font, vpitch, crc32);
+       if (IS_ERR(new_data))
+               return PTR_ERR(new_data);
 
-       FNTSUM(new_data) = csum;
        /* Check if the same font is on some other console already */
        for (i = first_fb_vc; i <= last_fb_vc; i++) {
                if (fb_display[i].fontdata &&
index 58bf3c64cabbf5ccf3454536d671051f8cf0a227..3eb4818402c507d60e2bcc4c0a2ca943bc54b5f7 100644 (file)
@@ -13,6 +13,8 @@
 
 #include <linux/types.h>
 
+struct console_font;
+
 /*
  * font_data_t and helpers
  */
@@ -54,6 +56,8 @@ static inline const unsigned char *font_data_buf(font_data_t *fd)
        return (const unsigned char *)fd;
 }
 
+font_data_t *font_data_import(const struct console_font *font, unsigned int vpitch,
+                             u32 (*calc_csum)(u32, const void *, size_t));
 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);
@@ -124,7 +128,7 @@ extern const struct font_desc *get_default_font(int xres, int yres,
 
 struct font_data {
        unsigned int extra[FONT_EXTRA_WORDS];
-       const unsigned char data[];
+       unsigned char data[];
 } __packed;
 
 #endif /* _VIDEO_FONT_H */
index 3fb76d1856475e688618ef778dddcb1bbc1ad2f0..16e75c3d2a0f2aa3856a52e59c2dd7ece2200090 100644 (file)
@@ -14,7 +14,9 @@
 
 #include <linux/container_of.h>
 #include <linux/font.h>
+#include <linux/kd.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/types.h>
@@ -23,6 +25,8 @@
 #include <asm/setup.h>
 #endif
 
+#define console_font_pitch(font) DIV_ROUND_UP((font)->width, 8)
+
 /*
  * Helpers for font_data_t
  */
@@ -42,6 +46,64 @@ static void font_data_free(font_data_t *fd)
        kfree(to_font_data_struct(fd));
 }
 
+/**
+ * font_data_import - Allocates and initializes font data from user space
+ * @font: A font from user space
+ * @vpitch: The size of a single glyph in @font in bytes
+ * @calc_csum: An optional helper to calculate a chechsum
+ *
+ * Font data from user space must be translated to the kernel's format. The
+ * font's glyph geometry and data is provided in @font. The parameter @vpitch
+ * gives the number of bytes per glyph, including trailing bytes.
+ *
+ * The parameter @calc_csum is optional. Fbcon passes crc32() to calculate the
+ * font data's checksum.
+ *
+ * Returns:
+ * Newly initialized font data on success, or a pointer-encoded errno value otherwise.
+ */
+font_data_t *font_data_import(const struct console_font *font, unsigned int vpitch,
+                             u32 (*calc_csum)(u32, const void *, size_t))
+{
+       unsigned int pitch = console_font_pitch(font);
+       unsigned int h = font->height;
+       unsigned int charcount = font->charcount;
+       const unsigned char *data = font->data;
+       u32 csum = 0;
+       struct font_data *font_data;
+       int size, alloc_size;
+       unsigned int i;
+       font_data_t *fd;
+
+       /* Check for integer overflow in font-size calculation */
+       if (check_mul_overflow(h, pitch, &size) ||
+           check_mul_overflow(size, charcount, &size))
+               return ERR_PTR(-EINVAL);
+
+       /* Check for overflow in allocation size calculation */
+       if (check_add_overflow(sizeof(*font_data), size, &alloc_size))
+               return ERR_PTR(-EINVAL);
+
+       font_data = kmalloc(alloc_size, GFP_USER);
+       if (!font_data)
+               return ERR_PTR(-ENOMEM);
+       memset(font_data->extra, 0, sizeof(font_data->extra));
+
+       for (i = 0; i < charcount; ++i)
+               memcpy(font_data->data + i * h * pitch, data + i * vpitch * pitch, h * pitch);
+
+       if (calc_csum)
+               csum = calc_csum(0, font_data->data, size);
+
+       fd = font_data->data;
+       REFCOUNT(fd) = 1; /* start with reference acquired */
+       FNTSIZE(fd) = size;
+       FNTSUM(fd) = csum;
+
+       return fd;
+}
+EXPORT_SYMBOL_GPL(font_data_import);
+
 /**
  * font_data_get - Acquires a reference on font data
  * @fd: Font data