whoops, I just realized the kernel image being transferred control
with `StartImage()`, doesn't return a null-terminated string on
failure when kernel efistub calls `Exit()` in case of failure. And the
kernel always returns NULL in the place we expected string, in case of
failure that makes the kernel efistub to call `Exit()`. So i modified
my previous patch to address this.

NOTE: we need to avoid printing efi status code as much as possible
because the spec didn't document well and most people won't find much
useful information on online or the spec. So use error messages as
much as possible and print status code as hex if necessary. Currently
those i made string is the one i think mostly the kernel  will return
on failure.

diff --git a/grub-core/loader/efi/linux.c b/grub-core/loader/efi/linux.c
index 78ea07ca8..062a07f9e 100644
--- a/grub-core/loader/efi/linux.c
+++ b/grub-core/loader/efi/linux.c
@@ -211,9 +211,15 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr,
grub_size_t size, char *args)
  status = b->load_image (0, grub_efi_image_handle,
                         (grub_efi_device_path_t *) mempath,
                         (void *) addr, size, &image_handle);
-  if (status != GRUB_EFI_SUCCESS)
-    return grub_error (GRUB_ERR_BAD_OS, "cannot load image");
-
+
+  if (status != GRUB_EFI_SUCCESS) {
+         if (status == GRUB_EFI_INVALID_PARAMETER)
+               return  grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
+         else if (status == GRUB_EFI_OUT_OF_RESOURCES)
+               return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
+         else
+               return grub_error (GRUB_ERR_BAD_OS, "cannot load image
0x%" PRIxGRUB_EFI_UINTN_T, status);
+  }
  grub_dprintf ("linux", "linux command line: '%s'\n", args);

  /* Convert command line to UTF-16. */
@@ -238,7 +244,13 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr,
grub_size_t size, char *args)
  status = b->start_image (image_handle, 0, NULL);

  /* When successful, not reached */
-  grub_error (GRUB_ERR_BAD_OS, "start_image() returned 0x%"
PRIxGRUB_EFI_UINTN_T, status);
+  if (status == GRUB_EFI_INVALID_PARAMETER)
+         grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
+  else if (status == GRUB_EFI_OUT_OF_RESOURCES)
+         grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
+  else
+         grub_error (GRUB_ERR_BAD_OS, "cannot start image 0x%"
PRIxGRUB_EFI_UINTN_T, status);
+
  grub_efi_free_pages ((grub_addr_t) loaded_image->load_options,
                      GRUB_EFI_BYTES_TO_PAGES (len));
  loaded_image->load_options = NULL;

Best regards, khaalid

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to