[PATCH v2] efistub/x86: Add fallback for SMBIOS record lookup

Francesco Pompo posted 1 patch 3 months ago
drivers/firmware/efi/libstub/x86-stub.c | 107 +++++++++++++++++++++++-
1 file changed, 106 insertions(+), 1 deletion(-)
[PATCH v2] efistub/x86: Add fallback for SMBIOS record lookup
Posted by Francesco Pompo 3 months ago
Some Apple EFI firmwares do not provide the SMBIOS Protocol,
causing efi_get_smbios_record() to fail. This prevents retrieval of
system information such as product name, which is needed by
apple_set_os() to enable the integrated GPU on dual-graphics Intel
MacBooks.

Add a fallback that directly parses the SMBIOS entry point table when
the protocol is unavailable.

Signed-off-by: Francesco Pompo <francescopompo2@gmail.com>
---
 drivers/firmware/efi/libstub/x86-stub.c | 107 +++++++++++++++++++++++-
 1 file changed, 106 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c
index f8e465da344d..13059412fdb9 100644
--- a/drivers/firmware/efi/libstub/x86-stub.c
+++ b/drivers/firmware/efi/libstub/x86-stub.c
@@ -225,6 +225,110 @@ static void retrieve_apple_device_properties(struct boot_params *boot_params)
 	}
 }
 
+struct smbios_entry_point {
+	char anchor[4];
+	u8 ep_checksum;
+	u8 ep_length;
+	u8 major_version;
+	u8 minor_version;
+	u16 max_size_entry;
+	u8 ep_rev;
+	u8 reserved[5];
+	char int_anchor[5];
+	u8 int_checksum;
+	u16 st_length;
+	u32 st_address;
+	u16 number_of_entries;
+	u8 bcd_rev;
+};
+
+static bool verify_ep_checksum(const struct smbios_entry_point *ep)
+{
+	const u8 *ptr = (u8 *)ep;
+	u8 sum = 0;
+	int i;
+
+	for (i = 0; i < ep->ep_length; i++)
+		sum += ptr[i];
+
+	return sum == 0;
+}
+
+static bool verify_ep_int_checksum(const struct smbios_entry_point *ep)
+{
+	const u8 *ptr = (u8 *)&ep->int_anchor;
+	u8 sum = 0;
+	int i;
+
+	for (i = 0; i < 15; i++)
+		sum += ptr[i];
+
+	return sum == 0;
+}
+
+static bool verify_ep_integrity(const struct smbios_entry_point *ep)
+{
+	if (memcmp(ep->anchor, "_SM_", sizeof(ep->anchor)) != 0)
+		return false;
+
+	if (memcmp(ep->int_anchor, "_DMI_", sizeof(ep->int_anchor)) != 0)
+		return false;
+
+	if (!verify_ep_checksum(ep) || !verify_ep_int_checksum(ep))
+		return false;
+
+	return true;
+}
+
+static const struct efi_smbios_record *search_record(void *table, u32 length,
+						     u8 type)
+{
+	const u8 *p, *end;
+
+	p = (u8 *)table;
+	end = p + length;
+
+	while (p + sizeof(struct efi_smbios_record) < end) {
+		const struct efi_smbios_record *hdr =
+			(struct efi_smbios_record *)p;
+		const u8 *next;
+
+		if (hdr->type == type)
+			return hdr;
+
+		/* Type 127 = End-of-Table */
+		if (hdr->type == 0x7F)
+			return NULL;
+
+		/* Jumping to the unformed section */
+		next = p + hdr->length;
+
+		/* Unformed section ends with 0000h */
+		while ((next[0] != 0 || next[1] != 0) && next + 1 < end)
+			next++;
+
+		next += 2;
+		p = next;
+	}
+
+	return NULL;
+}
+
+static const struct efi_smbios_record *get_table_record(u8 type)
+{
+	const struct smbios_entry_point *ep;
+
+	ep = get_efi_config_table(SMBIOS_TABLE_GUID);
+	if (!ep)
+		return NULL;
+
+	if (!verify_ep_integrity(ep))
+		return NULL;
+
+	return search_record((void *)(unsigned long)ep->st_address,
+		ep->st_length, type);
+}
+
 static bool apple_match_product_name(void)
 {
 	static const char type1_product_matches[][15] = {
@@ -240,7 +344,8 @@ static bool apple_match_product_name(void)
 	const struct efi_smbios_type1_record *record;
 	const u8 *product;
 
-	record = (struct efi_smbios_type1_record *)efi_get_smbios_record(1);
+	record = (struct efi_smbios_type1_record *)(efi_get_smbios_record(1) ?:
+		get_table_record(1));
 	if (!record)
 		return false;
 
-- 
2.50.1
Re: [PATCH v2] efistub/x86: Add fallback for SMBIOS record lookup
Posted by Ard Biesheuvel 3 months ago
On Mon, 3 Nov 2025 at 13:33, Francesco Pompo <francescopompo2@gmail.com> wrote:
>
> Some Apple EFI firmwares do not provide the SMBIOS Protocol,
> causing efi_get_smbios_record() to fail. This prevents retrieval of
> system information such as product name, which is needed by
> apple_set_os() to enable the integrated GPU on dual-graphics Intel
> MacBooks.
>
> Add a fallback that directly parses the SMBIOS entry point table when
> the protocol is unavailable.
>
> Signed-off-by: Francesco Pompo <francescopompo2@gmail.com>
> ---
>  drivers/firmware/efi/libstub/x86-stub.c | 107 +++++++++++++++++++++++-
>  1 file changed, 106 insertions(+), 1 deletion(-)
>

OK, I've pushed this to the efi/next branch now. I did apply some
cosmetic tweaks, though, so please double check that I did not break
anything.


> diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c
> index f8e465da344d..13059412fdb9 100644
> --- a/drivers/firmware/efi/libstub/x86-stub.c
> +++ b/drivers/firmware/efi/libstub/x86-stub.c
> @@ -225,6 +225,110 @@ static void retrieve_apple_device_properties(struct boot_params *boot_params)
>         }
>  }
>
> +struct smbios_entry_point {
> +       char anchor[4];
> +       u8 ep_checksum;
> +       u8 ep_length;
> +       u8 major_version;
> +       u8 minor_version;
> +       u16 max_size_entry;
> +       u8 ep_rev;
> +       u8 reserved[5];
> +       char int_anchor[5];
> +       u8 int_checksum;
> +       u16 st_length;
> +       u32 st_address;
> +       u16 number_of_entries;
> +       u8 bcd_rev;
> +};
> +
> +static bool verify_ep_checksum(const struct smbios_entry_point *ep)
> +{
> +       const u8 *ptr = (u8 *)ep;
> +       u8 sum = 0;
> +       int i;
> +
> +       for (i = 0; i < ep->ep_length; i++)
> +               sum += ptr[i];
> +
> +       return sum == 0;
> +}
> +
> +static bool verify_ep_int_checksum(const struct smbios_entry_point *ep)
> +{
> +       const u8 *ptr = (u8 *)&ep->int_anchor;
> +       u8 sum = 0;
> +       int i;
> +
> +       for (i = 0; i < 15; i++)
> +               sum += ptr[i];
> +
> +       return sum == 0;
> +}
> +
> +static bool verify_ep_integrity(const struct smbios_entry_point *ep)
> +{
> +       if (memcmp(ep->anchor, "_SM_", sizeof(ep->anchor)) != 0)
> +               return false;
> +
> +       if (memcmp(ep->int_anchor, "_DMI_", sizeof(ep->int_anchor)) != 0)
> +               return false;
> +
> +       if (!verify_ep_checksum(ep) || !verify_ep_int_checksum(ep))
> +               return false;
> +
> +       return true;
> +}
> +
> +static const struct efi_smbios_record *search_record(void *table, u32 length,
> +                                                    u8 type)
> +{
> +       const u8 *p, *end;
> +
> +       p = (u8 *)table;
> +       end = p + length;
> +
> +       while (p + sizeof(struct efi_smbios_record) < end) {
> +               const struct efi_smbios_record *hdr =
> +                       (struct efi_smbios_record *)p;
> +               const u8 *next;
> +
> +               if (hdr->type == type)
> +                       return hdr;
> +
> +               /* Type 127 = End-of-Table */
> +               if (hdr->type == 0x7F)
> +                       return NULL;
> +
> +               /* Jumping to the unformed section */
> +               next = p + hdr->length;
> +
> +               /* Unformed section ends with 0000h */
> +               while ((next[0] != 0 || next[1] != 0) && next + 1 < end)
> +                       next++;
> +
> +               next += 2;
> +               p = next;
> +       }
> +
> +       return NULL;
> +}
> +
> +static const struct efi_smbios_record *get_table_record(u8 type)
> +{
> +       const struct smbios_entry_point *ep;
> +
> +       ep = get_efi_config_table(SMBIOS_TABLE_GUID);
> +       if (!ep)
> +               return NULL;
> +
> +       if (!verify_ep_integrity(ep))
> +               return NULL;
> +
> +       return search_record((void *)(unsigned long)ep->st_address,
> +               ep->st_length, type);
> +}
> +
>  static bool apple_match_product_name(void)
>  {
>         static const char type1_product_matches[][15] = {
> @@ -240,7 +344,8 @@ static bool apple_match_product_name(void)
>         const struct efi_smbios_type1_record *record;
>         const u8 *product;
>
> -       record = (struct efi_smbios_type1_record *)efi_get_smbios_record(1);
> +       record = (struct efi_smbios_type1_record *)(efi_get_smbios_record(1) ?:
> +               get_table_record(1));
>         if (!record)
>                 return false;
>
> --
> 2.50.1
>
Re: [PATCH v2] efistub/x86: Add fallback for SMBIOS record lookup
Posted by Francesco Pompo' 3 months ago
On Mon, Nov 03, 2025 at 02:47:22PM +0100, Ard Biesheuvel wrote:
> On Mon, 3 Nov 2025 at 13:33, Francesco Pompo <francescopompo2@gmail.com> wrote:
> >
> > Some Apple EFI firmwares do not provide the SMBIOS Protocol,
> > causing efi_get_smbios_record() to fail. This prevents retrieval of
> > system information such as product name, which is needed by
> > apple_set_os() to enable the integrated GPU on dual-graphics Intel
> > MacBooks.
> >
> > Add a fallback that directly parses the SMBIOS entry point table when
> > the protocol is unavailable.
> >
> > Signed-off-by: Francesco Pompo <francescopompo2@gmail.com>
> > ---
> >  drivers/firmware/efi/libstub/x86-stub.c | 107 +++++++++++++++++++++++-
> >  1 file changed, 106 insertions(+), 1 deletion(-)
> >
> 
> OK, I've pushed this to the efi/next branch now. I did apply some
> cosmetic tweaks, though, so please double check that I did not break
> anything.

Seems fine to me. I compiled it and it's working correctly on my
hardware.

Thank you! :)