git: 5e25f7b09977 - main - loader: loader can pick too large font

From: Toomas Soome <tsoome_at_FreeBSD.org>
Date: Sun, 11 May 2025 23:09:32 UTC
The branch main has been updated by tsoome:

URL: https://cgit.FreeBSD.org/src/commit/?id=5e25f7b09977002b069946e82055f26e0f4aec8d

commit 5e25f7b09977002b069946e82055f26e0f4aec8d
Author:     Toomas Soome <tsoome@FreeBSD.org>
AuthorDate: 2025-05-11 23:07:15 +0000
Commit:     Toomas Soome <tsoome@FreeBSD.org>
CommitDate: 2025-05-11 23:07:15 +0000

    loader: loader can pick too large font
    
    While calculating font size based on EDID data, we can end up
    selecting too large font and too small terminal. Add check to
    prevent it.
    
    Tested by:      bz, ziaee
    Reviewed by:    adrian
    Differential Revision:  https://reviews.freebsd.org/D50258
---
 stand/common/gfx_fb.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/stand/common/gfx_fb.c b/stand/common/gfx_fb.c
index efb210dff210..ad38a657e233 100644
--- a/stand/common/gfx_fb.c
+++ b/stand/common/gfx_fb.c
@@ -2054,7 +2054,8 @@ gfx_get_ppi(void)
  * not smaller than calculated size value.
  */
 static vt_font_bitmap_data_t *
-gfx_get_font(void)
+gfx_get_font(teken_unit_t rows, teken_unit_t cols, teken_unit_t height,
+    teken_unit_t width)
 {
 	unsigned ppi, size;
 	vt_font_bitmap_data_t *font = NULL;
@@ -2077,6 +2078,14 @@ gfx_get_font(void)
 	size = roundup(size * 2, 10) / 10;
 
 	STAILQ_FOREACH(fl, &fonts, font_next) {
+		/*
+		 * Skip too large fonts.
+		 */
+		font = fl->font_data;
+		if (height / font->vfbd_height < rows &&
+		    width / font->vfbd_width < cols)
+			continue;
+
 		next = STAILQ_NEXT(fl, font_next);
 
 		/*
@@ -2084,7 +2093,6 @@ gfx_get_font(void)
 		 * we have our font. Make sure, it actually is loaded.
 		 */
 		if (next == NULL || next->font_data->vfbd_height < size) {
-			font = fl->font_data;
 			if (font->vfbd_font == NULL ||
 			    fl->font_flags == FONT_RELOAD) {
 				if (fl->font_load != NULL &&
@@ -2093,6 +2101,7 @@ gfx_get_font(void)
 			}
 			break;
 		}
+		font = NULL;
 	}
 
 	return (font);
@@ -2123,7 +2132,7 @@ set_font(teken_unit_t *rows, teken_unit_t *cols, teken_unit_t h, teken_unit_t w)
 	}
 
 	if (font == NULL)
-		font = gfx_get_font();
+		font = gfx_get_font(*rows, *cols, h, w);
 
 	if (font != NULL) {
 		*rows = height / font->vfbd_height;