Skip to contentSkip to conversation

EFI: Fix boot from a device without a file system

PATCH Superseded Szymon Acedański 5 participants 10 messages May 19 – May 26

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 EFI: Fix boot from a device without a file system

When netbooting a unified Xen kernel image (via GRUB chainloader), the resulting loaded_image->DeviceHandle does not support SIMPLE_FILE_SYSTEM_PROTOCOL.

Instead of crashing via noreturn PrintErrMesg(), print a message via PrintStr() and return NULL from get_parent_handle().

---
Tested on Intel Lunar Lake laptop via PXE-loaded GRUB chainloading the unified image including Xen built from upstream staging. Without the patch the boot aborts with:

Couldn't obtain the File System Protocol Interface: ErrCode: 0x8000000000000003

(EFI_UNSUPPORTED, per UEFI spec Appendix D). With the patch, dom0 reaches login.

 xen/common/efi/boot.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/common/efi/boot.c +9 -0
index 9ea2183c0b..a6c3c371c6 100644
@@ -471,6 +471,15 @@ static EFI_FILE_HANDLE __init get_parent_handle(const EFI_LOADED_IMAGE *loaded_i
         /* Get the file system interface. */
         ret = efi_bs->HandleProtocol(loaded_image->DeviceHandle,
                                      &fs_protocol, (void **)&fio);
+        if ( ret == EFI_UNSUPPORTED )
+        {
+            /*
+             * The image could come from a device without a file system,
+             * for example it could be a netbooted unified Xen kernel image.
+             */
+            PrintStr(L"File System Protocol not supported on boot device\r\n");
+            return NULL;
+        }
         if ( EFI_ERROR(ret) )
             PrintErrMesg(L"Couldn't obtain the File System Protocol Interface",
                          ret);
Signature · 1 line
2.53.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Szymon Acedański

When netbooting a unified Xen kernel image (via GRUB chainloader), the resulting loaded_image->DeviceHandle does not support SIMPLE_FILE_SYSTEM_PROTOCOL.

Instead of crashing via noreturn PrintErrMesg(), print a message via PrintStr() and return NULL from get_parent_handle().

It's worth noting this isn't the first instance of returning NULL from get_parent_handle(). The return value is used only as an argument to read_file() (sometimes indirectly), and if it gets to be called with NULL, read_file() will terminate execution via PrintErrMesg(). But with unified Xen image, the intention is to not call read_file() at all, only read_section(), so tolerating get_parent_handle() failure in this case is desired. Keeping the message in place will ease debugging if read_file() will actually be called later.

As a side note, a slightly better approach would be to call get_parent_handle() lazily (on the first call to read_file()?). But it's a bigger change that I feel may be too late for in Xen 4.22.

Also, adding cc: Oleksii for release ack.

Quoted 37 lines

---
Tested on Intel Lunar Lake laptop via PXE-loaded GRUB chainloading the unified image including Xen built from upstream staging. Without the patch the boot aborts with:

Couldn't obtain the File System Protocol Interface: ErrCode: 0x8000000000000003

(EFI_UNSUPPORTED, per UEFI spec Appendix D). With the patch, dom0 reaches login.

 xen/common/efi/boot.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/common/efi/boot.c +9 -0
index 9ea2183c0b..a6c3c371c6 100644
@@ -471,6 +471,15 @@ static EFI_FILE_HANDLE __init get_parent_handle(const EFI_LOADED_IMAGE *loaded_i
         /* Get the file system interface. */
         ret = efi_bs->HandleProtocol(loaded_image->DeviceHandle,
                                      &fs_protocol, (void **)&fio);
+        if ( ret == EFI_UNSUPPORTED )
+        {
+            /*
+             * The image could come from a device without a file system,
+             * for example it could be a netbooted unified Xen kernel image.
+             */
+            PrintStr(L"File System Protocol not supported on boot device\r\n");
+            return NULL;
+        }
         if ( EFI_ERROR(ret) )
             PrintErrMesg(L"Couldn't obtain the File System Protocol Interface",
                          ret);
Signature · 1 line
2.53.0
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#2 lore raw
Quoted 16 lines · Marek Marczykowski-Górecki
Quoted 6 lines · Szymon Acedański

When netbooting a unified Xen kernel image (via GRUB chainloader), the resulting loaded_image->DeviceHandle does not support SIMPLE_FILE_SYSTEM_PROTOCOL.

Instead of crashing via noreturn PrintErrMesg(), print a message via PrintStr() and return NULL from get_parent_handle().

It's worth noting this isn't the first instance of returning NULL from get_parent_handle(). The return value is used only as an argument to read_file() (sometimes indirectly), and if it gets to be called with NULL, read_file() will terminate execution via PrintErrMesg(). But with unified Xen image, the intention is to not call read_file() at all, only read_section(), so tolerating get_parent_handle() failure in this case is desired. Keeping the message in place will ease debugging if read_file() will actually be called later.

Quoted 7 lines

As a side note, a slightly better approach would be to call get_parent_handle() lazily (on the first call to read_file()?). But it's a bigger change that I feel may be too late for in Xen 4.22.

Also, adding cc: Oleksii for release ack.

Bugfixes are still fine to go in.

Personally, I think moving the call to get_parent_handle() is a better fix, and fine for 4.22 even at this juncture.  ARM already does something along these lines in allocate_module_file().

~Andrew

#3 lore raw
Quoted 23 lines · Andrew Cooper
Quoted 22 lines · Marek Marczykowski-Górecki
Quoted 6 lines · Szymon Acedański

When netbooting a unified Xen kernel image (via GRUB chainloader), the resulting loaded_image->DeviceHandle does not support SIMPLE_FILE_SYSTEM_PROTOCOL.

Instead of crashing via noreturn PrintErrMesg(), print a message via PrintStr() and return NULL from get_parent_handle().

It's worth noting this isn't the first instance of returning NULL from get_parent_handle(). The return value is used only as an argument to read_file() (sometimes indirectly), and if it gets to be called with NULL, read_file() will terminate execution via PrintErrMesg(). But with unified Xen image, the intention is to not call read_file() at all, only read_section(), so tolerating get_parent_handle() failure in this case is desired. Keeping the message in place will ease debugging if read_file() will actually be called later.

As a side note, a slightly better approach would be to call get_parent_handle() lazily (on the first call to read_file()?). But it's a bigger change that I feel may be too late for in Xen 4.22.

Quoted 7 lines
Quoted 1 line

Also, adding cc: Oleksii for release ack.

Bugfixes are still fine to go in.

Personally, I think moving the call to get_parent_handle() is a better fix, and fine for 4.22 even at this juncture.  ARM already does something along these lines in allocate_module_file().

I agree that we could consider suggested better approach and I am okay with having this patch (or its new version) as part of 4.22.

Thanks.

~ Oleksii

#4 lore raw

PATCH v2 1/1 EFI: Fix boot from a device without a file system

In reply to Szymon Acedański

When netbooting a unified Xen kernel image (via GRUB chainloader), the resulting loaded_image->DeviceHandle does not support SIMPLE_FILE_SYSTEM_PROTOCOL.

Instead of crashing via noreturn PrintErrMesg() in get_parent_handle(), we defer calling this function until filesystem access is needed. This way when booting UKI, get_parent_handle() is not called at all.

---
Changes in v2:
- Restructured along the lines Andrew and Marek both suggested on v1:
defer get_parent_handle() until the first call site that actually needs a file.

This mirrors the existing lazy pattern in ARM's allocate_module_file() in xen/arch/arm/efi/efi-boot.h, which was also changed to use the new ensure_dir_handle() helper.

Tests performed:
- Same as v1: PXE-loaded GRUB chainloading UKI - failure without patch, success with patch
- QEMU boot from EFI partition, with config, kernel and initrd on EFI partition too - success with and without patch (halts later in both cases due to unrelated issue)
- Cross-compiling ARM64 - success

 xen/arch/arm/efi/efi-boot.h | 12 ++++----
 xen/arch/x86/efi/efi-boot.h |  9 ++++--
 xen/common/efi/boot.c       | 57 ++++++++++++++++++++++++-------------
 3 files changed, 50 insertions(+), 28 deletions(-)
xen/arch/arm/efi/efi-boot.h +7 -5
index ea59de47e7..069cc68b0a 100644
@@ -403,7 +403,7 @@ static void __init noreturn efi_arch_post_exit_boot(void)
 }
 
 static void __init efi_arch_cfg_file_early(const EFI_LOADED_IMAGE *image,
-                                           EFI_FILE_HANDLE dir_handle,
+                                           EFI_FILE_HANDLE *dir_handle,
                                            const char *section)
 {
     union string name;
@@ -419,8 +419,11 @@ static void __init efi_arch_cfg_file_early(const EFI_LOADED_IMAGE *image,
         name.s = get_value(&cfg, section, "dtb");
         if ( name.s )
         {
+            CHAR16 *fname;
+
             split_string(name.s);
-            read_file(dir_handle, s2w(&name), &dtbfile, NULL);
+            ensure_dir_handle(image, dir_handle, &fname);
+            read_file(*dir_handle, s2w(&name), &dtbfile, NULL);
             efi_bs->FreePool(name.w);
         }
     }
@@ -430,7 +433,7 @@ static void __init efi_arch_cfg_file_early(const EFI_LOADED_IMAGE *image,
 }
 
 static void __init efi_arch_cfg_file_late(const EFI_LOADED_IMAGE *image,
-                                          EFI_FILE_HANDLE dir_handle,
+                                          EFI_FILE_HANDLE *dir_handle,
                                           const char *section)
 {
 }
@@ -665,8 +668,7 @@ static int __init allocate_module_file(const EFI_LOADED_IMAGE *loaded_image,
     file_info->name_len = name_len;
 
     /* Get the file system interface. */
-    if ( !*dir_handle )
-        *dir_handle = get_parent_handle(loaded_image, &fname);
+    ensure_dir_handle(loaded_image, dir_handle, &fname);
 
     /* Load the binary in memory */
     read_file(*dir_handle, s2w(&module_name), &module_binary, NULL);
xen/arch/x86/efi/efi-boot.h +6 -3
index 42a2c46b5e..d738b839ee 100644
@@ -284,13 +284,13 @@ static void __init noreturn efi_arch_post_exit_boot(void)
 }
 
 static void __init efi_arch_cfg_file_early(const EFI_LOADED_IMAGE *image,
-                                           EFI_FILE_HANDLE dir_handle,
+                                           EFI_FILE_HANDLE *dir_handle,
                                            const char *section)
 {
 }
 
 static void __init efi_arch_cfg_file_late(const EFI_LOADED_IMAGE *image,
-                                          EFI_FILE_HANDLE dir_handle,
+                                          EFI_FILE_HANDLE *dir_handle,
                                           const char *section)
 {
     union string name;
@@ -304,9 +304,12 @@ static void __init efi_arch_cfg_file_late(const EFI_LOADED_IMAGE *image,
         name.s = get_value(&cfg, "global", "ucode");
     if ( name.s )
     {
+        CHAR16 *fname;
+
         microcode_set_module(mbi.mods_count);
         split_string(name.s);
-        read_file(dir_handle, s2w(&name), &ucode, NULL);
+        ensure_dir_handle(image, dir_handle, &fname);
+        read_file(*dir_handle, s2w(&name), &ucode, NULL);
         efi_bs->FreePool(name.w);
     }
 }
xen/common/efi/boot.c +37 -20

119 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw
Quoted 41 lines · Szymon Acedański
xen/common/efi/boot.c +17 -13
@@ -1526,31 +1537,33 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
 
         gop = efi_get_gop(&gop_handle);
 
-        /* Get the file system interface. */
-        dir_handle = get_parent_handle(loaded_image, &file_name);
-
         /* Read and parse the config file. */
         if ( read_section(loaded_image, L"config", &cfg, NULL) )
             PrintStr(L"Using builtin config file\r\n");
-        else if ( !cfg_file_name && file_name )
+        else
         {
-            CHAR16 *tail;
+            ensure_dir_handle(loaded_image, &dir_handle, &file_name);
 
-            while ( (tail = point_tail(file_name)) != NULL )
+            if ( !cfg_file_name )
             {
-                wstrcpy(tail, L".cfg");
-                if ( read_file(dir_handle, file_name, &cfg, NULL) )
-                    break;
-                *tail = 0;
+                CHAR16 *tail;
+
+                while ( (tail = point_tail(file_name)) != NULL )
+                {
+                    wstrcpy(tail, L".cfg");
+                    if ( read_file(dir_handle, file_name, &cfg, NULL) )
+                        break;
+                    *tail = 0;
+                }
+                if ( !tail )
+                    blexit(L"No configuration file found.");
+                PrintStr(L"Using configuration file '");
+                PrintStr(file_name);
+                PrintStr(L"'\r\n");
             }
-            if ( !tail )
-                blexit(L"No configuration file found.");
-            PrintStr(L"Using configuration file '");

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle, · line 1569

Quoted 10 lines
xen/common/efi/boot.c +2 -4
-            PrintStr(file_name);
-            PrintStr(L"'\r\n");
+            else if ( !read_file(dir_handle, cfg_file_name, &cfg, NULL) )
+                blexit(L"Configuration file not found.");
         }
-        else if ( !read_file(dir_handle, cfg_file_name, &cfg, NULL) )
-            blexit(L"Configuration file not found.");
         pre_parse(&cfg);
 
         if ( section.w )

Seeing in particular this hunk - why not have read_file() call the new function? Most of the churn here would then go away.

Jan

#6 lore raw
Quoted 51 lines · Jan Beulich
Quoted 50 lines · Szymon Acedański
xen/common/efi/boot.c +19 -17
@@ -1526,31 +1537,33 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
 
         gop = efi_get_gop(&gop_handle);
 
-        /* Get the file system interface. */
-        dir_handle = get_parent_handle(loaded_image, &file_name);
-
         /* Read and parse the config file. */
         if ( read_section(loaded_image, L"config", &cfg, NULL) )
             PrintStr(L"Using builtin config file\r\n");
-        else if ( !cfg_file_name && file_name )
+        else
         {
-            CHAR16 *tail;
+            ensure_dir_handle(loaded_image, &dir_handle, &file_name);
 
-            while ( (tail = point_tail(file_name)) != NULL )
+            if ( !cfg_file_name )
             {
-                wstrcpy(tail, L".cfg");
-                if ( read_file(dir_handle, file_name, &cfg, NULL) )
-                    break;
-                *tail = 0;
+                CHAR16 *tail;
+
+                while ( (tail = point_tail(file_name)) != NULL )
+                {
+                    wstrcpy(tail, L".cfg");
+                    if ( read_file(dir_handle, file_name, &cfg, NULL) )
+                        break;
+                    *tail = 0;
+                }
+                if ( !tail )
+                    blexit(L"No configuration file found.");
+                PrintStr(L"Using configuration file '");
+                PrintStr(file_name);
+                PrintStr(L"'\r\n");
             }
-            if ( !tail )
-                blexit(L"No configuration file found.");
-            PrintStr(L"Using configuration file '");
-            PrintStr(file_name);
-            PrintStr(L"'\r\n");
+            else if ( !read_file(dir_handle, cfg_file_name, &cfg, NULL) )
+                blexit(L"Configuration file not found.");
         }
-        else if ( !read_file(dir_handle, cfg_file_name, &cfg, NULL) )
-            blexit(L"Configuration file not found.");
         pre_parse(&cfg);
 
Quoted 3 lines

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle, · line 1569

Quoted 1 line
xen/common/efi/boot.c +0 -0
         if ( section.w )

Seeing in particular this hunk - why not have read_file() call the new function?

This is because get_parent_handle not only sets dir_handle, but also sets file_name to something like xen.efi or BOOTX64.EFI. The quoted code then replaces .efi with .cfg to get the path to the config file to load:

Quoted 4 lines

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle, · line 1554

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+                while ( (tail = point_tail(file_name)) != NULL )
+                {
+                    wstrcpy(tail, L".cfg");
+                    if ( read_file(dir_handle, file_name, &cfg, NULL) )

I considered calling ensure_dir_handle() from read_file() for the other call sites, but this would:
- still leave the explicit call in the quoted hunk, so it's a bit inconsistent (most calls implicit, one explicit)
- requires passing loaded_image to read_file + changing dir_handle argument to a pointer

Happy to do it in v3 if you think the call-site savings outweigh the inconsistency and the extra argument.

Quoted 1 line

Most of the churn here would then go away.

The hunk above is the restructure of two else-if branches into a single else block with ensure_dir_handle() on top. Most of the churn is indentation.

Szymon

(ACK on sending new patch versions as new threads)

#7 lore raw
Quoted 52 lines · Szymon Acedański
Quoted 51 lines · Jan Beulich
Quoted 50 lines · Szymon Acedański
xen/common/efi/boot.c +19 -17
@@ -1526,31 +1537,33 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
 
         gop = efi_get_gop(&gop_handle);
 
-        /* Get the file system interface. */
-        dir_handle = get_parent_handle(loaded_image, &file_name);
-
         /* Read and parse the config file. */
         if ( read_section(loaded_image, L"config", &cfg, NULL) )
             PrintStr(L"Using builtin config file\r\n");
-        else if ( !cfg_file_name && file_name )
+        else
         {
-            CHAR16 *tail;
+            ensure_dir_handle(loaded_image, &dir_handle, &file_name);
 
-            while ( (tail = point_tail(file_name)) != NULL )
+            if ( !cfg_file_name )
             {
-                wstrcpy(tail, L".cfg");
-                if ( read_file(dir_handle, file_name, &cfg, NULL) )
-                    break;
-                *tail = 0;
+                CHAR16 *tail;
+
+                while ( (tail = point_tail(file_name)) != NULL )
+                {
+                    wstrcpy(tail, L".cfg");
+                    if ( read_file(dir_handle, file_name, &cfg, NULL) )
+                        break;
+                    *tail = 0;
+                }
+                if ( !tail )
+                    blexit(L"No configuration file found.");
+                PrintStr(L"Using configuration file '");
+                PrintStr(file_name);
+                PrintStr(L"'\r\n");
             }
-            if ( !tail )
-                blexit(L"No configuration file found.");
-            PrintStr(L"Using configuration file '");
-            PrintStr(file_name);
-            PrintStr(L"'\r\n");
+            else if ( !read_file(dir_handle, cfg_file_name, &cfg, NULL) )
+                blexit(L"Configuration file not found.");
         }
-        else if ( !read_file(dir_handle, cfg_file_name, &cfg, NULL) )
-            blexit(L"Configuration file not found.");
         pre_parse(&cfg);
 
Quoted 7 lines
Quoted 3 lines

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle, · line 1569

Quoted 1 line
xen/common/efi/boot.c +0 -0
         if ( section.w )

Seeing in particular this hunk - why not have read_file() call the new function?

This is because get_parent_handle not only sets dir_handle, but also sets file_name to something like xen.efi or BOOTX64.EFI. The quoted code then replaces .efi with .cfg to get the path to the config file to load:

Yes, especially for this case, get_parent_handle() needs to be called before read_file(). Other calls don't need that, but I'm not sure if having two ways of calling read_file() would be better.

Speaking of, the dir_handle==NULL case in read_file() is unreachable now, right? Maybe can be replaced with an assert?

Quoted 24 lines
Quoted 4 lines

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle, · line 1554

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+                while ( (tail = point_tail(file_name)) != NULL )
+                {
+                    wstrcpy(tail, L".cfg");
+                    if ( read_file(dir_handle, file_name, &cfg, NULL) )

I considered calling ensure_dir_handle() from read_file() for the other call sites, but this would:
- still leave the explicit call in the quoted hunk, so it's a bit inconsistent (most calls implicit, one explicit)
- requires passing loaded_image to read_file + changing dir_handle argument to a pointer

Happy to do it in v3 if you think the call-site savings outweigh the inconsistency and the extra argument.

Quoted 1 line

Most of the churn here would then go away.

The hunk above is the restructure of two else-if branches into a single else block with ensure_dir_handle() on top. Most of the churn is indentation.

Szymon

(ACK on sending new patch versions as new threads)

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#8 lore raw
Quoted 2 lines · Marek Marczykowski

Speaking of, the dir_handle==NULL case in read_file() is unreachable
now, right? Maybe can be replaced with an assert?Oh yes, just submitted v3 with BUG_ON(!dir_handle).

Szymon

#9 lore raw
In reply to Szymon Acedański
Quoted 11 lines · Szymon Acedański

When netbooting a unified Xen kernel image (via GRUB chainloader), the resulting loaded_image->DeviceHandle does not support SIMPLE_FILE_SYSTEM_PROTOCOL.

Instead of crashing via noreturn PrintErrMesg() in get_parent_handle(), we defer calling this function until filesystem access is needed. This way when booting UKI, get_parent_handle() is not called at all.

Oh, one other, formal thing (for the future): Please don't send new patch versions as reply to a prior one. They should root entirely new threads.

Jan