Skip to contentSkip to conversation

font: Try opening fonts from the bundled memdisk

PATCH In review Chris Coulson 4 participants 4 messages Apr 26, 2023 – Apr 28, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 font: Try opening fonts from the bundled memdisk

Grub since 93a786a00163e50c29f0394df198518617e1c9a5 has enforced verification of font files in secure boot mode. In order to continue to be able to load some default fonts, vendors may bundle them with their signed EFI image by adding them to the built-in memdisk.

This change makes the font loader try loading fonts from the memdisk before the prefix path when attempting to load a font file by specifying its filename, which avoids having to make changes to grub configurations in order to accommodate memdisk bundled fonts. It expects the directory structure to be the same as fonts stored in the prefix path (ie, /fonts/<name>.pf2).

---
 grub-core/font/font.c | 48 ++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 17 deletions(-)
grub-core/font/font.c +31 -17
index 24adcb35a..7c83467a3 100644
@@ -415,6 +415,27 @@ read_section_as_short (struct font_file_section *section,
   return 0;
 }
 
+static grub_file_t
+try_open_from_prefix (const char *prefix, const char *filename)
+{
+  grub_file_t file;
+  char *fullname, *ptr;
+
+  fullname = grub_malloc (grub_strlen (prefix) + grub_strlen (filename) + 1
+			  + sizeof ("/fonts/") + sizeof (".pf2"));
+  if (!fullname)
+    return 0;
+  ptr = grub_stpcpy (fullname, prefix);
+  ptr = grub_stpcpy (ptr, "/fonts/");
+  ptr = grub_stpcpy (ptr, filename);
+  ptr = grub_stpcpy (ptr, ".pf2");
+  *ptr = 0;
+
+  file = grub_buffile_open (fullname, GRUB_FILE_TYPE_FONT, 1024);
+  grub_free (fullname);
+  return file;
+}
+
 /* Load a font and add it to the beginning of the global font list.
    Returns 0 upon success, nonzero upon failure.  */
 grub_font_t
@@ -433,25 +454,18 @@ grub_font_load (const char *filename)
     file = grub_buffile_open (filename, GRUB_FILE_TYPE_FONT, 1024);
   else
     {
-      const char *prefix = grub_env_get ("prefix");
-      char *fullname, *ptr;
-      if (!prefix)
+      file = try_open_from_prefix ("(memdisk)", filename);
+      if (!file)
 	{
-	  grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"),
-		      "prefix");
-	  goto fail;
+	  const char *prefix = grub_env_get ("prefix");
+	  if (!prefix)
+	    {
+	      grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"),
+			  "prefix");
+	      goto fail;
+	    }
+	  file = try_open_from_prefix (prefix, filename);
 	}
-      fullname = grub_malloc (grub_strlen (prefix) + grub_strlen (filename) + 1
-			      + sizeof ("/fonts/") + sizeof (".pf2"));
-      if (!fullname)
-	goto fail;
-      ptr = grub_stpcpy (fullname, prefix);
-      ptr = grub_stpcpy (ptr, "/fonts/");
-      ptr = grub_stpcpy (ptr, filename);
-      ptr = grub_stpcpy (ptr, ".pf2");
-      *ptr = 0;
-      file = grub_buffile_open (fullname, GRUB_FILE_TYPE_FONT, 1024);
-      grub_free (fullname);
     }
   if (!file)
     goto fail;
Signature · 1 line
2.39.2
Reviewed-by Tested-by on v1 1/1
#1 lore raw

We're running with this patch in Debian builds right now and it's working well...

(whichever you prefer!)

Quoted 93 lines · Chris Coulson

Grub since 93a786a00163e50c29f0394df198518617e1c9a5 has enforced verification of font files in secure boot mode. In order to continue to be able to load some default fonts, vendors may bundle them with their signed EFI image by adding them to the built-in memdisk.

This change makes the font loader try loading fonts from the memdisk before the prefix path when attempting to load a font file by specifying its filename, which avoids having to make changes to grub configurations in order to accommodate memdisk bundled fonts. It expects the directory structure to be the same as fonts stored in the prefix path (ie, /fonts/<name>.pf2).

---
grub-core/font/font.c | 48 ++++++++++++++++++++++++++++---------------

1 file changed, 31 insertions(+), 17 deletions(-)
grub-core/font/font.c +21 -0
index 24adcb35a..7c83467a3 100644
@@ -415,6 +415,27 @@ read_section_as_short (struct font_file_section *section,
  return 0;
 }
 
+static grub_file_t
+try_open_from_prefix (const char *prefix, const char *filename)
+{
+  grub_file_t file;
+  char *fullname, *ptr;
+
+  fullname = grub_malloc (grub_strlen (prefix) + grub_strlen (filename) + 1
+			  + sizeof ("/fonts/") + sizeof (".pf2"));
+  if (!fullname)
+    return 0;
+  ptr = grub_stpcpy (fullname, prefix);
+  ptr = grub_stpcpy (ptr, "/fonts/");
+  ptr = grub_stpcpy (ptr, filename);
+  ptr = grub_stpcpy (ptr, ".pf2");
+  *ptr = 0;
+
+  file = grub_buffile_open (fullname, GRUB_FILE_TYPE_FONT, 1024);
+  grub_free (fullname);
+  return file;
+}
+
 /* Load a font and add it to the beginning of the global font list.
   Returns 0 upon success, nonzero upon failure.  */

grub_font_t

grub-core/font/font.c +10 -17
@@ -433,25 +454,18 @@ grub_font_load (const char *filename)
    file = grub_buffile_open (filename, GRUB_FILE_TYPE_FONT, 1024);
  else
    {
-      const char *prefix = grub_env_get ("prefix");
-      char *fullname, *ptr;
-      if (!prefix)
+      file = try_open_from_prefix ("(memdisk)", filename);
+      if (!file)
 	{
-	  grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"),
-		      "prefix");
-	  goto fail;
+	  const char *prefix = grub_env_get ("prefix");
+	  if (!prefix)
+	    {
+	      grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"),
+			  "prefix");
+	      goto fail;
+	    }
+	  file = try_open_from_prefix (prefix, filename);
 	}
-      fullname = grub_malloc (grub_strlen (prefix) + grub_strlen (filename) + 1
-			      + sizeof ("/fonts/") + sizeof (".pf2"));
-      if (!fullname)
-	goto fail;
-      ptr = grub_stpcpy (fullname, prefix);
-      ptr = grub_stpcpy (ptr, "/fonts/");
-      ptr = grub_stpcpy (ptr, filename);
-      ptr = grub_stpcpy (ptr, ".pf2");
-      *ptr = 0;
-      file = grub_buffile_open (fullname, GRUB_FILE_TYPE_FONT, 1024);
-      grub_free (fullname);
    }
  if (!file)
    goto fail;
Signature · 7 lines
2.39.2


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 5 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
'There is some grim amusement in watching Pence try to run the typical
 "politician in the middle of a natural disaster" playbook, however
 incompetently, while Trump scribbles all over it in crayon and eats some
 of the pages.'   -- Russ Allbery
Reviewed-by on v1 1/1
#2 lore raw
In reply to Chris Coulson
Quoted 5 lines · Chris Coulson

Grub since 93a786a00163e50c29f0394df198518617e1c9a5 has enforced verification of font files in secure boot mode. In order to continue to be able to load some default fonts, vendors may bundle them with their signed EFI image by adding them to the built-in memdisk.

Quoted 8 lines

This change makes the font loader try loading fonts from the memdisk before the prefix path when attempting to load a font file by specifying its filename, which avoids having to make changes to grub configurations in order to accommodate memdisk bundled fonts. It expects the directory structure to be the same as fonts stored in the prefix path (ie, /fonts/<name>.pf2).

Thanks for posting this.

Be well,
--Robbie

Reviewed-by on v1 1/1
#3 lore raw
In reply to Chris Coulson
Quoted 5 lines · Chris Coulson

Grub since 93a786a00163e50c29f0394df198518617e1c9a5 has enforced verification of font files in secure boot mode. In order to continue to be able to load some default fonts, vendors may bundle them with their signed EFI image by adding them to the built-in memdisk.

Quoted 8 lines

This change makes the font loader try loading fonts from the memdisk before the prefix path when attempting to load a font file by specifying its filename, which avoids having to make changes to grub configurations in order to accommodate memdisk bundled fonts. It expects the directory structure to be the same as fonts stored in the prefix path (ie, /fonts/<name>.pf2).

Daniel