Skip to contentSkip to conversation

Add support for running a 64-bit Linux kernel on a 32-bit EFI

PATCH In review Steve McIntyre 2 participants 5 messages Jan 28, 2015 – Feb 2, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for running a 64-bit Linux kernel on a 32-bit EFI

Hi folks,

I've been working in Debian on adding support for amd64 platforms which are shipped with 32-bit UEFI firmware, such as the Asus X205TA and other Bay Trail machines. As part of that, I've had a patch accepted by the Linux EFI maintainers [1] to expose the size of the underlying firmware to userland via a new sysfs file. It should make mainline Linux very soon.

Using that patch as a basis, I've patched grub to use the new sysfs interface to work out the correct default x86 platform to match the firmware. Colin's happy with the patch and has just accepted it for Debian's grub package for now; I promised to submit it upstream for review, so here it is. :-)

Feedback welcome please!

[1] http://article.gmane.org/gmane.linux.kernel.efi/5229

=====================================================================
Some platforms might be capable of running a 64-bit Linux kernel but only use a 32-bit EFI. To support such systems, it is necessary to work out the size of the firmware rather than just the size of the kernel. To enable that, there is now an extra EFI sysfs file to describe the underlying firmware. Read that if possible, otherwise fall back to the kernel type as before.

---
 grub-core/osdep/linux/platform.c |   38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
grub-core/osdep/linux/platform.c +37 -1
index 4b9f6ef..5668ae5 100644
@@ -60,6 +60,42 @@ is_64_kernel (void)
   return strcmp (un.machine, "x86_64") == 0;
 }
 
+static int
+read_platform_size (void)
+{
+  FILE *fp;
+  char *buf = NULL;
+  size_t len = 0;
+  int ret = 0;
+
+  /* Newer kernels can tell us directly about the size of the
+   * underlying firmware - let's see if that interface is there. */
+  fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r");
+  if (fp != NULL)
+  {
+    if (getline (&buf, &len, fp) > 0)
+      {
+	if (strncmp (buf, "32", 2) == 0)
+	  ret = 32;
+	else if (strncmp (buf, "64", 2) == 0)
+	  ret = 64;
+      }
+    free (buf);
+    fclose (fp);
+  }
+
+  if (ret == 0)
+    /* Unrecognised - fall back to matching the kernel size instead */
+    {
+      if (is_64_kernel ())
+	ret = 64;
+      else
+	ret = 32;
+    }
+
+  return ret;
+}
+
 const char *
 grub_install_get_default_x86_platform (void)
 { 
@@ -77,7 +113,7 @@ grub_install_get_default_x86_platform (void)
   if (is_not_empty_directory ("/sys/firmware/efi"))
     {
       grub_util_info ("...found");
-      if (is_64_kernel ())
+      if (read_platform_size() == 64)
 	return "x86_64-efi";
       else
 	return "i386-efi";
Signature · 5 lines
1.7.10.4

-- 
Steve McIntyre, Cambridge, UK.                                [email protected]
"You can't barbecue lettuce!" -- Ellie Crane
#1 lore raw

В Wed, 28 Jan 2015 00:56:30 +0000
Steve McIntyre <[email protected]> пишет:

Quoted 44 lines

Hi folks,

I've been working in Debian on adding support for amd64 platforms which are shipped with 32-bit UEFI firmware, such as the Asus X205TA and other Bay Trail machines. As part of that, I've had a patch accepted by the Linux EFI maintainers [1] to expose the size of the underlying firmware to userland via a new sysfs file. It should make mainline Linux very soon.

Using that patch as a basis, I've patched grub to use the new sysfs interface to work out the correct default x86 platform to match the firmware. Colin's happy with the patch and has just accepted it for Debian's grub package for now; I promised to submit it upstream for review, so here it is. :-)

Feedback welcome please!

[1] http://article.gmane.org/gmane.linux.kernel.efi/5229

=====================================================================
Some platforms might be capable of running a 64-bit Linux kernel but only use a 32-bit EFI. To support such systems, it is necessary to work out the size of the firmware rather than just the size of the kernel. To enable that, there is now an extra EFI sysfs file to describe the underlying firmware. Read that if possible, otherwise fall back to the kernel type as before.

---
 grub-core/osdep/linux/platform.c |   38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
grub-core/osdep/linux/platform.c +4 -0
index 4b9f6ef..5668ae5 100644
@@ -60,6 +60,42 @@ is_64_kernel (void)
   return strcmp (un.machine, "x86_64") == 0;
 }
 
+static int
+read_platform_size (void)
+{
+  FILE *fp;

grub-core/osdep/linux/platform.c is_64_kernel (void) · line 76

Quoted 10 lines
grub-core/osdep/linux/platform.c +10 -0
+  char *buf = NULL;
+  size_t len = 0;
+  int ret = 0;
+
+  /* Newer kernels can tell us directly about the size of the
+   * underlying firmware - let's see if that interface is there. */
+  fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r");
+  if (fp != NULL)
+  {
+    if (getline (&buf, &len, fp) > 0)

size >= 2

grub-core/osdep/linux/platform.c is_64_kernel (void) · line 89

Quoted 13 lines
grub-core/osdep/linux/platform.c +13 -0
+      {
+	if (strncmp (buf, "32", 2) == 0)
+	  ret = 32;
+	else if (strncmp (buf, "64", 2) == 0)
+	  ret = 64;
+      }
+    free (buf);
+    fclose (fp);
+  }
+
+  if (ret == 0)
+    /* Unrecognised - fall back to matching the kernel size instead */
+    {

I usually prefer comments inside braces and indented accordingly.

Could you send it suitable for git am? Mentioning platforms in commit message would be useful for reference. BTW, re subject - this is about installing 32 bit EFI grub on 64 bit Linux, not running, right? It already should be able to run kernel if installed appropriately manually.

Quoted 21 lines
grub-core/osdep/linux/platform.c +10 -1
+      if (is_64_kernel ())
+	ret = 64;
+      else
+	ret = 32;
+    }
+
+  return ret;
+}
+
 const char *
 grub_install_get_default_x86_platform (void)
 { 
@@ -77,7 +113,7 @@ grub_install_get_default_x86_platform (void)
   if (is_not_empty_directory ("/sys/firmware/efi"))
     {
       grub_util_info ("...found");
-      if (is_64_kernel ())
+      if (read_platform_size() == 64)
 	return "x86_64-efi";
       else
 	return "i386-efi";
#2 lore raw
Quoted 25 lines · Andrei Borzenkov

В Wed, 28 Jan 2015 00:56:30 +0000
Steve McIntyre <[email protected]> пишет:

Quoted 22 lines

=====================================================================
Some platforms might be capable of running a 64-bit Linux kernel but only use a 32-bit EFI. To support such systems, it is necessary to work out the size of the firmware rather than just the size of the kernel. To enable that, there is now an extra EFI sysfs file to describe the underlying firmware. Read that if possible, otherwise fall back to the kernel type as before.

---
 grub-core/osdep/linux/platform.c |   38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
grub-core/osdep/linux/platform.c +0 -0
index 4b9f6ef..5668ae5 100644
@@ -60,6 +60,42 @@ is_64_kernel (void)
   return strcmp (un.machine, "x86_64") == 0;
 }
 
Quoted 16 lines
Quoted 4 lines
grub-core/osdep/linux/platform.c +4 -0
+static int
+read_platform_size (void)
+{
+  FILE *fp;

grub-core/osdep/linux/platform.c is_64_kernel (void) · line 76

Quoted 10 lines
grub-core/osdep/linux/platform.c +10 -0
+  char *buf = NULL;
+  size_t len = 0;
+  int ret = 0;
+
+  /* Newer kernels can tell us directly about the size of the
+   * underlying firmware - let's see if that interface is there. */
+  fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r");
+  if (fp != NULL)
+  {
+    if (getline (&buf, &len, fp) > 0)

size >= 2

Yup.

Quoted 7 lines
Quoted 7 lines
grub-core/osdep/linux/platform.c +7 -0
+      {
+	if (strncmp (buf, "32", 2) == 0)
+	  ret = 32;
+	else if (strncmp (buf, "64", 2) == 0)
+	  ret = 64;
+      }
+    free (buf);
Quoted 8 lines

grub-core/osdep/linux/platform.c is_64_kernel (void) · line 89

Quoted 6 lines
grub-core/osdep/linux/platform.c +6 -0
+    fclose (fp);
+  }
+
+  if (ret == 0)
+    /* Unrecognised - fall back to matching the kernel size instead */
+    {

I usually prefer comments inside braces and indented accordingly.

OK, cool.

Quoted 5 lines

Could you send it suitable for git am? Mentioning platforms in commit message would be useful for reference. BTW, re subject - this is about installing 32 bit EFI grub on 64 bit Linux, not running, right? It already should be able to run kernel if installed appropriately manually.

Correct, yes.

v2 coming shortly.

Signature · 2 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
Welcome my son, welcome to the machine.
#3 lore raw
Quoted 2 lines · Steve McIntyre

v2 coming shortly.

Hi! Any comments on v2?

Signature · 4 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
Google-bait:       http://www.debian.org/CD/free-linux-cd
  Debian does NOT ship free CDs. Please do NOT contact the mailing
  lists asking us to send them to you.
#4 lore raw

PATCH 1/1 Recognise a 64-bit Linux kernel on a 32-bit EFI firmware

In reply to Andrei Borzenkov

Some x86 systems might be capable of running a 64-bit Linux kernel but only use a 32-bit EFI (e.g. Intel Bay Trail systems). It's useful for grub-install to be able to recognise such systems, to set the default x86 platform correctly.

To allow grub-install to know the size of the firmware rather than just the size of the kernel, there is now an extra EFI sysfs file to describe the underlying firmware. Read that if possible, otherwise fall back to the kernel type as before.

---
 grub-core/osdep/linux/platform.c |   39 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
grub-core/osdep/linux/platform.c +38 -1
index 4b9f6ef..775b6c0 100644
@@ -60,6 +60,43 @@ is_64_kernel (void)
   return strcmp (un.machine, "x86_64") == 0;
 }
 
+static int
+read_platform_size (void)
+{
+  FILE *fp;
+  char *buf = NULL;
+  size_t len = 0;
+  int ret = 0;
+
+  /* Newer kernels can tell us directly about the size of the
+   * underlying firmware - let's see if that interface is there. */
+  fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r");
+  if (fp != NULL)
+  {
+    if (getline (&buf, &len, fp) >= 3) /* 2 digits plus newline */
+      {
+	if (strncmp (buf, "32", 2) == 0)
+	  ret = 32;
+	else if (strncmp (buf, "64", 2) == 0)
+	  ret = 64;
+      }
+    free (buf);
+    fclose (fp);
+  }
+
+  if (ret == 0)
+    {
+      /* Unrecognised - fall back to matching the kernel size
+       * instead */
+      if (is_64_kernel ())
+	ret = 64;
+      else
+	ret = 32;
+    }
+
+  return ret;
+}
+
 const char *
 grub_install_get_default_x86_platform (void)
 { 
@@ -77,7 +114,7 @@ grub_install_get_default_x86_platform (void)
   if (is_not_empty_directory ("/sys/firmware/efi"))
     {
       grub_util_info ("...found");
-      if (is_64_kernel ())
+      if (read_platform_size() == 64)
 	return "x86_64-efi";
       else
 	return "i386-efi";
Signature · 1 line
1.7.10.4