Skip to contentSkip to conversation

Allows the usage of xen_hypervisor and xen_module on x86_64

RFC PATCH In review Frediano Ziglio via Grub-devel 2 participants 18 messages Jul 8, 2025 – Jul 10, 2025

lore
Share & tools
lore
#0 lore raw

cover Allows the usage of xen_hypervisor and xen_module on x86_64

Currently xen_hypervisor and xen_module commands are available only for ARM64 EFI.
For x86_64 if the Xen format is portable executable there's no way to pass multiple modules (the feature is available using multiboot2 but only for ELF files).
So instead on adding new GRUB commands reuse the already available ones.
The modules are passed using LoadFile2 already used to pass initrd to Linux kernel.
An extension allows to pass also kernel file.
To pass command lines uses LoadOptions, EFI code already handles passing Xen and kernel command lines using LoadOptions.

Frediano Ziglio (5):
kern/list: Add a grub_list_length function
kern/list: Add a grub_list_append function
arm64/xen: Allows to use xen_hypervisor and xen_module on x86_64-efi arm64/xen: Use LoadFile2 protocol for Xen boot
arm64/xen: Allows to use kernel command line from first module

 grub-core/Makefile.core.def       |   2 +
 grub-core/kern/list.c             |  22 ++++
 grub-core/loader/arm64/xen_boot.c | 208 +++++++++++++++++++++++++++++-
 include/grub/efi/api.h            |   5 +
 include/grub/list.h               |   3 +
 5 files changed, 239 insertions(+), 1 deletion(-)
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

RFC PATCH 1/5 kern/list: Add a grub_list_length function

Counts elements in a list.

---
 grub-core/kern/list.c | 12 ++++++++++++
 include/grub/list.h   |  2 ++
 2 files changed, 14 insertions(+)
grub-core/kern/list.c +12 -0
index a256bb3f8..09761b0e5 100644
@@ -53,3 +53,15 @@ grub_list_remove (grub_list_t item)
   item->next = 0;
   item->prev = 0;
 }
+
+grub_size_t
+grub_list_length (grub_list_t head)
+{
+  grub_list_t item;
+  grub_size_t length = 0;
+
+  FOR_LIST_ELEMENTS (item, head)
+    length++;
+
+  return length;
+}
include/grub/list.h +2 -0
index 21f4b4b44..094eaf9cd 100644
@@ -23,6 +23,7 @@
 #include <grub/symbol.h>
 #include <grub/err.h>
 #include <grub/compiler.h>
+#include <grub/types.h>
 
 struct grub_list
 {
@@ -33,6 +34,7 @@ typedef struct grub_list *grub_list_t;
 
 void EXPORT_FUNC(grub_list_push) (grub_list_t *head, grub_list_t item);
 void EXPORT_FUNC(grub_list_remove) (grub_list_t item);
+grub_size_t EXPORT_FUNC(grub_list_length) (grub_list_t head);
 
 #define FOR_LIST_ELEMENTS(var, list) for ((var) = (list); (var); (var) = (var)->next)
 #define FOR_LIST_ELEMENTS_NEXT(var, list) for ((var) = (var)->next; (var); (var) = (var)->next)
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

RFC PATCH 2/5 kern/list: Add a grub_list_append function

In reply to Frediano Ziglio via Grub-devel

Add an element at the end of a list.

---
 grub-core/kern/list.c | 10 ++++++++++
 include/grub/list.h   |  1 +
 2 files changed, 11 insertions(+)
grub-core/kern/list.c +10 -0
index 09761b0e5..c5bdaf94b 100644
@@ -43,6 +43,16 @@ grub_list_push (grub_list_t *head, grub_list_t item)
   *head = item;
 }
 
+void
+grub_list_append (grub_list_t *head, grub_list_t item)
+{
+  while (*head)
+    head = &(*head)->next;
+  item->prev = head;
+  item->next = NULL;
+  *head = item;
+}
+
 void
 grub_list_remove (grub_list_t item)
 {
include/grub/list.h +1 -0
index 094eaf9cd..c11c180a7 100644
@@ -33,6 +33,7 @@ struct grub_list
 typedef struct grub_list *grub_list_t;
 
 void EXPORT_FUNC(grub_list_push) (grub_list_t *head, grub_list_t item);
+void EXPORT_FUNC(grub_list_append) (grub_list_t *head, grub_list_t item);
 void EXPORT_FUNC(grub_list_remove) (grub_list_t item);
 grub_size_t EXPORT_FUNC(grub_list_length) (grub_list_t head);
 
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

RFC PATCH 3/5 arm64/xen: Allows to use xen_hypervisor and xen_module on x86_64-efi

In reply to Frediano Ziglio via Grub-devel

Compile for x86_64 EFI architecture.
Do not fill device tree, not present for this architecture.

---
 grub-core/Makefile.core.def       |  2 ++
 grub-core/loader/arm64/xen_boot.c | 13 +++++++++++++
 2 files changed, 15 insertions(+)
grub-core/Makefile.core.def +2 -0
index 24e8c8437..bb091e549 100644
@@ -1848,7 +1848,9 @@ module = {
 module = {
   name = xen_boot;
   arm64 = loader/arm64/xen_boot.c;
+  x86_64_efi = loader/arm64/xen_boot.c;
   enable = arm64;
+  enable = x86_64_efi;
 };
 
 module = {
grub-core/loader/arm64/xen_boot.c +13 -0
index 0d53bc7fe..2975a546e 100644
@@ -46,6 +46,11 @@ GRUB_MOD_LICENSE ("GPLv3+");
  */
 #define FDT_NODE_NAME_MAX_SIZE  (49)
 
+#undef USE_DEVICE_TREE
+#if defined (__aarch64__) || defined (__arm__)
+#define USE_DEVICE_TREE 1
+#endif
+
 struct compat_string_struct
 {
   grub_size_t size;
@@ -90,6 +95,7 @@ xen_boot_address_align (grub_addr_t start, grub_size_t align)
   return (align ? (ALIGN_UP (start, align)) : start);
 }
 
+#ifdef USE_DEVICE_TREE
 static grub_err_t
 prepare_xen_hypervisor_params (void *xen_boot_fdt)
 {
@@ -185,10 +191,12 @@ prepare_xen_module_params (struct xen_boot_binary *module, void *xen_boot_fdt)
 
   return GRUB_ERR_NONE;
 }
+#endif
 
 static grub_err_t
 finalize_params_xen_boot (void)
 {
+#ifdef USE_DEVICE_TREE
   struct xen_boot_binary *module;
   void *xen_boot_fdt;
   grub_size_t additional_size = 0x1000;
@@ -241,6 +249,9 @@ fail:
   grub_fdt_unload ();
 
   return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
+#else
+  return GRUB_ERR_NONE;
+#endif
 }
 
 
@@ -310,7 +321,9 @@ xen_unload (void)
 {
   loaded = false;
   all_binaries_unload ();
+#ifdef USE_DEVICE_TREE
   grub_fdt_unload ();
+#endif
   grub_dl_unref (my_mod);
 
   return GRUB_ERR_NONE;
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

Re: [RFC PATCH 3/5] arm64/xen: Allows to use xen_hypervisor and xen_module on x86_64-efi

Quoted 15 lines · Frediano Ziglio

Compile for x86_64 EFI architecture.
Do not fill device tree, not present for this architecture.

---
 grub-core/Makefile.core.def       |  2 ++
 grub-core/loader/arm64/xen_boot.c | 13 +++++++++++++
 2 files changed, 15 insertions(+)
grub-core/Makefile.core.def +0 -0
index 24e8c8437..bb091e549 100644

grub-core/Makefile.core.def module = { · line 1851

Quoted 4 lines
grub-core/Makefile.core.def +1 -0
@@ -1848,7 +1848,9 @@ module = {
 module = {
   name = xen_boot;
   arm64 = loader/arm64/xen_boot.c;
+  x86_64_efi = loader/arm64/xen_boot.c;

Note that in case this series is accepted the file would be better moved out of "arm64" directory.

Quoted 53 lines
grub-core/Makefile.core.def +1 -0
   enable = arm64;
+  enable = x86_64_efi;
 };
 
 module = {
grub-core/loader/arm64/xen_boot.c +11 -0
index 0d53bc7fe..2975a546e 100644
@@ -46,6 +46,11 @@ GRUB_MOD_LICENSE ("GPLv3+");
  */
 #define FDT_NODE_NAME_MAX_SIZE  (49)
 
+#undef USE_DEVICE_TREE
+#if defined (__aarch64__) || defined (__arm__)
+#define USE_DEVICE_TREE 1
+#endif
+
 struct compat_string_struct
 {
   grub_size_t size;
@@ -90,6 +95,7 @@ xen_boot_address_align (grub_addr_t start, grub_size_t align)
   return (align ? (ALIGN_UP (start, align)) : start);
 }
 
+#ifdef USE_DEVICE_TREE
 static grub_err_t
 prepare_xen_hypervisor_params (void *xen_boot_fdt)
 {
@@ -185,10 +191,12 @@ prepare_xen_module_params (struct xen_boot_binary *module, void *xen_boot_fdt)
 
   return GRUB_ERR_NONE;
 }
+#endif
 
 static grub_err_t
 finalize_params_xen_boot (void)
 {
+#ifdef USE_DEVICE_TREE
   struct xen_boot_binary *module;
   void *xen_boot_fdt;
   grub_size_t additional_size = 0x1000;
@@ -241,6 +249,9 @@ fail:
   grub_fdt_unload ();
 
   return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
+#else
+  return GRUB_ERR_NONE;
+#endif
 }
 
 

grub-core/loader/arm64/xen_boot.c xen_unload (void) · line 329

Quoted 9 lines
grub-core/loader/arm64/xen_boot.c +2 -0
@@ -310,7 +321,9 @@ xen_unload (void)
 {
   loaded = false;
   all_binaries_unload ();
+#ifdef USE_DEVICE_TREE
   grub_fdt_unload ();
+#endif
   grub_dl_unref (my_mod);
 
   return GRUB_ERR_NONE;

Frediano

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

Re: [RFC PATCH 3/5] arm64/xen: Allows to use xen_hypervisor and xen_module on x86_64-efi

Quoted 13 lines · Frediano Ziglio via Grub-devel
Quoted 10 lines · Frediano Ziglio

Compile for x86_64 EFI architecture.
Do not fill device tree, not present for this architecture.

---
 grub-core/Makefile.core.def       |  2 ++
 grub-core/loader/arm64/xen_boot.c | 13 +++++++++++++
 2 files changed, 15 insertions(+)
Quoted 13 lines

grub-core/Makefile.core.def module = { · line 1851

Quoted 9 lines
grub-core/Makefile.core.def +1 -0
index 24e8c8437..bb091e549 100644
@@ -1848,7 +1848,9 @@ module = {
 module = {
   name = xen_boot;
   arm64 = loader/arm64/xen_boot.c;
+  x86_64_efi = loader/arm64/xen_boot.c;

Note that in case this series is accepted the file would be better moved out of "arm64" directory.

It feels like there is not that much code shared between the ARM64 and x86_64 implementations other than the high level command names (xen_hypervisor / xen_module). I wonder if it would be cleaner to instead have a separate implementation for x86_64 while still using the same command names?

Ross

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw

Re: [RFC PATCH 3/5] arm64/xen: Allows to use xen_hypervisor and xen_module on x86_64-efi

Quoted 30 lines · Ross Lagerwall
Quoted 13 lines · Frediano Ziglio via Grub-devel
Quoted 10 lines · Frediano Ziglio

Compile for x86_64 EFI architecture.
Do not fill device tree, not present for this architecture.

---
 grub-core/Makefile.core.def       |  2 ++
 grub-core/loader/arm64/xen_boot.c | 13 +++++++++++++
 2 files changed, 15 insertions(+)
Quoted 13 lines

grub-core/Makefile.core.def module = { · line 1851

Quoted 9 lines
grub-core/Makefile.core.def +1 -0
index 24e8c8437..bb091e549 100644
@@ -1848,7 +1848,9 @@ module = {
 module = {
   name = xen_boot;
   arm64 = loader/arm64/xen_boot.c;
+  x86_64_efi = loader/arm64/xen_boot.c;

Note that in case this series is accepted the file would be better moved out of "arm64" directory.

Quoted 6 lines

It feels like there is not that much code shared between the ARM64 and x86_64 implementations other than the high level command names (xen_hypervisor / xen_module). I wonder if it would be cleaner to instead have a separate implementation for x86_64 while still using the same command names?

Looking at the end of this series it looks like 70-80% of the code would be duplicated.
IMO it would be better to have common code calling specific architecture parts.

Quoted 1 line

Ross

Frediano

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw

Re: [RFC PATCH 3/5] arm64/xen: Allows to use xen_hypervisor and xen_module on x86_64-efi

Quoted 40 lines · Frediano Ziglio
Quoted 30 lines · Ross Lagerwall
Quoted 13 lines · Frediano Ziglio via Grub-devel
Quoted 10 lines · Frediano Ziglio

Compile for x86_64 EFI architecture.
Do not fill device tree, not present for this architecture.

---
 grub-core/Makefile.core.def       |  2 ++
 grub-core/loader/arm64/xen_boot.c | 13 +++++++++++++
 2 files changed, 15 insertions(+)
Quoted 13 lines

grub-core/Makefile.core.def module = { · line 1851

Quoted 9 lines
grub-core/Makefile.core.def +1 -0
index 24e8c8437..bb091e549 100644
@@ -1848,7 +1848,9 @@ module = {
 module = {
   name = xen_boot;
   arm64 = loader/arm64/xen_boot.c;
+  x86_64_efi = loader/arm64/xen_boot.c;

Note that in case this series is accepted the file would be better moved out of "arm64" directory.

Quoted 6 lines

It feels like there is not that much code shared between the ARM64 and x86_64 implementations other than the high level command names (xen_hypervisor / xen_module). I wonder if it would be cleaner to instead have a separate implementation for x86_64 while still using the same command names?

Quoted 4 lines

Looking at the end of this series it looks like 70-80% of the code would be duplicated.
IMO it would be better to have common code calling specific architecture parts.

In that case, I agree with common code calling arch-specific bits.

Ross

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#8 lore raw

RFC PATCH 4/5 arm64/xen: Use LoadFile2 protocol for Xen boot

In reply to Frediano Ziglio via Grub-devel

Allows to load modules using LoadFile2 protocol. Add and use a new GUID for kernel media device. This will allow Xen to pick up additional modules using EFI interface instead of using multiboot2 interface (not available on x86_64).

---
 grub-core/loader/arm64/xen_boot.c | 172 +++++++++++++++++++++++++++++-
 include/grub/efi/api.h            |   5 +
 2 files changed, 176 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +171 -1
index 2975a546e..64f685de6 100644
@@ -68,6 +68,56 @@ enum module_type
 };
 typedef enum module_type module_type_t;
 
+static grub_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+static initrd_media_device_path_t initrd_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    LINUX_EFI_INITRD_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+static initrd_media_device_path_t kernel_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    XEN_EFI_KERNEL_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+typedef struct lf2
+{
+  grub_efi_load_file2_t lf2;
+  grub_efi_handle_t handle;
+  initrd_media_device_path_t *media;
+} lf2_t;
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer);
+static void lf2_init(lf2_t *lf2, initrd_media_device_path_t *media);
+static grub_err_t lf2_start(lf2_t *lf2);
+static void lf2_done(lf2_t *lf2);
+
 struct xen_boot_binary
 {
   struct xen_boot_binary *next;
@@ -80,6 +130,8 @@ struct xen_boot_binary
 
   char *cmdline;
   int cmdline_size;
+
+  lf2_t lf2;
 };
 
 static grub_dl_t my_mod;
@@ -289,6 +341,8 @@ single_binary_unload (struct xen_boot_binary *binary)
 		    binary->cmdline, binary->cmdline_size);
     }
 
+  lf2_done (&binary->lf2);
+
   if (!binary->is_hypervisor)
     grub_list_remove (GRUB_AS_LIST (binary));
 
@@ -393,6 +447,7 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
   struct xen_boot_binary *module = NULL;
   grub_file_t file = 0;
   int nounzip = 0;
+  initrd_media_device_path_t *media;
 
   if (!argc)
     {
@@ -427,6 +482,19 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   module->is_hypervisor = false;
   module->align = 4096;
+  switch (grub_list_length (GRUB_AS_LIST (module_head)))
+    {
+    case MODULE_IMAGE:
+      media = &kernel_lf2_device_path;
+      break;
+    case MODULE_INITRD:
+      media = &initrd_lf2_device_path;
+      break;
+    default:
+      media = NULL;
+      break;
+    }
+  lf2_init(&module->lf2, media);
 
   grub_dprintf ("xen_loader", "Init module and node info\n");
 
@@ -438,7 +506,10 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   xen_boot_binary_load (module, file, argc, argv);
   if (grub_errno == GRUB_ERR_NONE)
-    grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+    {
+      grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+      lf2_start (&module->lf2);
+    }
 
  fail:
   if (file)
@@ -505,6 +576,105 @@ fail:
   return grub_errno;
 }
 
+static void
+lf2_init(lf2_t *lf2, initrd_media_device_path_t *media)
+{
+  lf2->lf2.load_file = grub_efi_load_file2;
+  lf2->handle = NULL;
+  lf2->media = media;
+}
+
+static grub_err_t
+lf2_start(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+  grub_efi_status_t status;
+
+  if (lf2->handle)
+    return GRUB_ERR_NONE;
+
+  status = b->install_multiple_protocol_interfaces (&lf2->handle,
+						    &load_file2_guid,
+						    &lf2->lf2,
+						    &device_path_guid,
+						    lf2->media,
+						    NULL);
+  if (status == GRUB_EFI_OUT_OF_RESOURCES)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+      return grub_errno;
+    }
+  else if (status != GRUB_EFI_SUCCESS)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("failed to install protocols"));
+      return grub_errno;
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static void
+lf2_done(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+
+  if (!lf2->handle)
+    return;
+
+  b->uninstall_multiple_protocol_interfaces (lf2->handle,
+					     &load_file2_guid,
+					     &lf2->lf2,
+					     &device_path_guid,
+					     lf2->media,
+					     NULL);
+  lf2->handle = NULL;
+}
+
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer)
+{
+  grub_efi_uintn_t size;
+  struct xen_boot_binary *binary, *module;
+  const void *src;
+
+  binary = NULL;
+  FOR_LIST_ELEMENTS (module, module_head)
+    if (this == &module->lf2.lf2)
+      {
+        binary = module;
+        break;
+      }
+
+  if (!binary || buffer_size == NULL)
+    return GRUB_EFI_INVALID_PARAMETER;
+
+  if (device_path->type != GRUB_EFI_END_DEVICE_PATH_TYPE ||
+      device_path->subtype != GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE)
+    return GRUB_EFI_NOT_FOUND;
+
+  if (boot_policy)
+    return GRUB_EFI_UNSUPPORTED;
+
+  size = binary->size;
+  if (buffer == NULL || *buffer_size < size)
+    {
+      *buffer_size = size;
+      return GRUB_EFI_BUFFER_TOO_SMALL;
+    }
+
+  grub_dprintf ("xen_loader", "Providing Xen module via EFI_LOAD_FILE2_PROTOCOL\n");
+
+  src = (void *) xen_boot_address_align (binary->start, binary->align);
+  grub_memcpy(buffer, src, size);
+
+  return GRUB_EFI_SUCCESS;
+}
+
 static grub_command_t cmd_xen_hypervisor;
 static grub_command_t cmd_xen_module;
 
include/grub/efi/api.h +5 -0
index b686e8afe..7d0641ade 100644
@@ -374,6 +374,11 @@
     { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } \
   }
 
+#define XEN_EFI_KERNEL_MEDIA_GUID \
+  { 0x2a4847d3, 0x990b, 0x4cf2, \
+    { 0x6b, 0xb5, 0x55, 0x71, 0xb4, 0x72, 0x17, 0x21 } \
+  }
+
 #define GRUB_EFI_VENDOR_BOOT_LOADER_INTERFACE_GUID \
   { 0x4a67b082, 0x0a4c, 0x41cf, \
     {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f } \
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#9 lore raw

Re: [RFC PATCH 4/5] arm64/xen: Use LoadFile2 protocol for Xen boot

Quoted 105 lines · Frediano Ziglio via Grub-devel

Allows to load modules using LoadFile2 protocol. Add and use a new GUID for kernel media device. This will allow Xen to pick up additional modules using EFI interface instead of using multiboot2 interface (not available on x86_64).

---
 grub-core/loader/arm64/xen_boot.c | 172 +++++++++++++++++++++++++++++-
 include/grub/efi/api.h            |   5 +
 2 files changed, 176 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +56 -0
index 2975a546e..64f685de6 100644
@@ -68,6 +68,56 @@ enum module_type
 };
 typedef enum module_type module_type_t;
 
+static grub_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+static initrd_media_device_path_t initrd_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    LINUX_EFI_INITRD_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+static initrd_media_device_path_t kernel_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    XEN_EFI_KERNEL_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+typedef struct lf2
+{
+  grub_efi_load_file2_t lf2;
+  grub_efi_handle_t handle;
+  initrd_media_device_path_t *media;
+} lf2_t;
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer);
+static void lf2_init(lf2_t *lf2, initrd_media_device_path_t *media);
+static grub_err_t lf2_start(lf2_t *lf2);
+static void lf2_done(lf2_t *lf2);
+
 struct xen_boot_binary
 {
   struct xen_boot_binary *next;
@@ -80,6 +130,8 @@ struct xen_boot_binary
 
   char *cmdline;
   int cmdline_size;
+
+  lf2_t lf2;
 };
 
 static grub_dl_t my_mod;
@@ -289,6 +341,8 @@ single_binary_unload (struct xen_boot_binary *binary)
                    binary->cmdline, binary->cmdline_size);
     }
 
+  lf2_done (&binary->lf2);
+
   if (!binary->is_hypervisor)
     grub_list_remove (GRUB_AS_LIST (binary));
 
@@ -393,6 +447,7 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
   struct xen_boot_binary *module = NULL;
   grub_file_t file = 0;
   int nounzip = 0;
+  initrd_media_device_path_t *media;
 
   if (!argc)
     {
@@ -427,6 +482,19 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   module->is_hypervisor = false;
   module->align = 4096;
+  switch (grub_list_length (GRUB_AS_LIST (module_head)))

grub-core/loader/arm64/xen_boot.c grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)), · line 495

Quoted 10 lines
grub-core/loader/arm64/xen_boot.c +10 -0
+    {
+    case MODULE_IMAGE:
+      media = &kernel_lf2_device_path;
+      break;
+    case MODULE_INITRD:
+      media = &initrd_lf2_device_path;
+      break;
+    default:
+      media = NULL;
+      break;

Shouldn't this be a hard error, at least on x86_64, rather than silently ignoring the extra modules?

In general, I'm not sure I like this interface since it is neither scalable or flexible. i.e. adding a new module would require both Xen and GRUB changes and it prevents certain combinations, e.g. booting a combined Xen + kernel image while _only_ passing the initrd as a module.

Perhaps it is the best we can do without overcomplicating the code.

Quoted 6 lines
grub-core/loader/arm64/xen_boot.c +2 -0
+    }
+  lf2_init(&module->lf2, media);
 
   grub_dprintf ("xen_loader", "Init module and node info\n");
 

grub-core/loader/arm64/xen_boot.c grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)), · line 512

Quoted 8 lines
grub-core/loader/arm64/xen_boot.c +4 -1
@@ -438,7 +506,10 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   xen_boot_binary_load (module, file, argc, argv);
   if (grub_errno == GRUB_ERR_NONE)
-    grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+    {
+      grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+      lf2_start (&module->lf2);
+    }

It's not directly related to this patch but isn't there a bug here where xen_boot_binary_load() calls single_binary_unload() on error and then it gets called again below causing a double free?

Ross

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#10 lore raw

Re: [RFC PATCH 4/5] arm64/xen: Use LoadFile2 protocol for Xen boot

Quoted 105 lines · Ross Lagerwall
Quoted 102 lines · Frediano Ziglio via Grub-devel

Allows to load modules using LoadFile2 protocol. Add and use a new GUID for kernel media device. This will allow Xen to pick up additional modules using EFI interface instead of using multiboot2 interface (not available on x86_64).

---
 grub-core/loader/arm64/xen_boot.c | 172 +++++++++++++++++++++++++++++-
 include/grub/efi/api.h            |   5 +
 2 files changed, 176 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +55 -0
index 2975a546e..64f685de6 100644
@@ -68,6 +68,56 @@ enum module_type
 };
 typedef enum module_type module_type_t;
 
+static grub_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+static initrd_media_device_path_t initrd_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    LINUX_EFI_INITRD_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+static initrd_media_device_path_t kernel_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    XEN_EFI_KERNEL_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+typedef struct lf2
+{
+  grub_efi_load_file2_t lf2;
+  grub_efi_handle_t handle;
+  initrd_media_device_path_t *media;
+} lf2_t;
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer);
+static void lf2_init(lf2_t *lf2, initrd_media_device_path_t *media);
+static grub_err_t lf2_start(lf2_t *lf2);
+static void lf2_done(lf2_t *lf2);
+
 struct xen_boot_binary
 {
   struct xen_boot_binary *next;
@@ -80,6 +130,8 @@ struct xen_boot_binary
 
   char *cmdline;
   int cmdline_size;
+
+  lf2_t lf2;
 };
 
 static grub_dl_t my_mod;
@@ -289,6 +341,8 @@ single_binary_unload (struct xen_boot_binary *binary)
                    binary->cmdline, binary->cmdline_size);
     }
 
+  lf2_done (&binary->lf2);
+
   if (!binary->is_hypervisor)
     grub_list_remove (GRUB_AS_LIST (binary));
 
@@ -393,6 +447,7 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
   struct xen_boot_binary *module = NULL;
   grub_file_t file = 0;
   int nounzip = 0;
+  initrd_media_device_path_t *media;
 
   if (!argc)
     {
@@ -427,6 +482,19 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
Quoted 17 lines

grub-core/loader/arm64/xen_boot.c grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)), · line 495

Quoted 13 lines
grub-core/loader/arm64/xen_boot.c +11 -0
   module->is_hypervisor = false;
   module->align = 4096;
+  switch (grub_list_length (GRUB_AS_LIST (module_head)))
+    {
+    case MODULE_IMAGE:
+      media = &kernel_lf2_device_path;
+      break;
+    case MODULE_INITRD:
+      media = &initrd_lf2_device_path;
+      break;
+    default:
+      media = NULL;
+      break;

Shouldn't this be a hard error, at least on x86_64, rather than silently ignoring the extra modules?

Yes, probably better. In theory the third module should be the Xen policy which is supported for x64 too, so it would be worth supporting it.

Quoted 5 lines

In general, I'm not sure I like this interface since it is neither scalable or flexible. i.e. adding a new module would require both Xen and GRUB changes and it prevents certain combinations, e.g. booting a combined Xen + kernel image while _only_ passing the initrd as a module.

The issue of skipping a module because it's embedded into xe.efi is present for ARM too, so it would be nice if the solution would be working on ARM too.
Maybe allowing to specify the module type with an option like --initrd, --kernel, --policy ?
On a related subject looking at "initrd" command for "linux" it's possible to specify multiple files to initrd allowing to put them all together generating a final initrd. It would be nice to have, although here it seems a bit out of scope.

Quoted 9 lines

Perhaps it is the best we can do without overcomplicating the code.

Quoted 7 lines
grub-core/loader/arm64/xen_boot.c +2 -0
+    }
+  lf2_init(&module->lf2, media);
 
   grub_dprintf ("xen_loader", "Init module and node info\n");
 
@@ -438,7 +506,10 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
Quoted 12 lines

grub-core/loader/arm64/xen_boot.c grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)), · line 512

Quoted 7 lines
grub-core/loader/arm64/xen_boot.c +4 -1
   xen_boot_binary_load (module, file, argc, argv);
   if (grub_errno == GRUB_ERR_NONE)
-    grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+    {
+      grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+      lf2_start (&module->lf2);
+    }

It's not directly related to this patch but isn't there a bug here where xen_boot_binary_load() calls single_binary_unload() on error and then it gets called again below causing a double free?

Yes, it looks so, not related to this change but it looks doable. I think xen_boot_binary_load calling single_binary_unload is wrong.

Quoted 1 line

Ross

Frediano

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#11 lore raw

Re: [RFC PATCH 4/5] arm64/xen: Use LoadFile2 protocol for Xen boot

In reply to Frediano Ziglio via Grub-devel
Quoted 217 lines · Frediano Ziglio via Grub-devel

Allows to load modules using LoadFile2 protocol. Add and use a new GUID for kernel media device. This will allow Xen to pick up additional modules using EFI interface instead of using multiboot2 interface (not available on x86_64).

---
 grub-core/loader/arm64/xen_boot.c | 172 +++++++++++++++++++++++++++++-
 include/grub/efi/api.h            |   5 +
 2 files changed, 176 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +153 -1
index 2975a546e..64f685de6 100644
@@ -68,6 +68,56 @@ enum module_type
 };
 typedef enum module_type module_type_t;
 
+static grub_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+static initrd_media_device_path_t initrd_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    LINUX_EFI_INITRD_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+static initrd_media_device_path_t kernel_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    XEN_EFI_KERNEL_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+typedef struct lf2
+{
+  grub_efi_load_file2_t lf2;
+  grub_efi_handle_t handle;
+  initrd_media_device_path_t *media;
+} lf2_t;
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer);
+static void lf2_init(lf2_t *lf2, initrd_media_device_path_t *media);
+static grub_err_t lf2_start(lf2_t *lf2);
+static void lf2_done(lf2_t *lf2);
+
 struct xen_boot_binary
 {
   struct xen_boot_binary *next;
@@ -80,6 +130,8 @@ struct xen_boot_binary
 
   char *cmdline;
   int cmdline_size;
+
+  lf2_t lf2;
 };
 
 static grub_dl_t my_mod;
@@ -289,6 +341,8 @@ single_binary_unload (struct xen_boot_binary *binary)
                    binary->cmdline, binary->cmdline_size);
     }
 
+  lf2_done (&binary->lf2);
+
   if (!binary->is_hypervisor)
     grub_list_remove (GRUB_AS_LIST (binary));
 
@@ -393,6 +447,7 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
   struct xen_boot_binary *module = NULL;
   grub_file_t file = 0;
   int nounzip = 0;
+  initrd_media_device_path_t *media;
 
   if (!argc)
     {
@@ -427,6 +482,19 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   module->is_hypervisor = false;
   module->align = 4096;
+  switch (grub_list_length (GRUB_AS_LIST (module_head)))
+    {
+    case MODULE_IMAGE:
+      media = &kernel_lf2_device_path;
+      break;
+    case MODULE_INITRD:
+      media = &initrd_lf2_device_path;
+      break;
+    default:
+      media = NULL;
+      break;
+    }
+  lf2_init(&module->lf2, media);
 
   grub_dprintf ("xen_loader", "Init module and node info\n");
 
@@ -438,7 +506,10 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   xen_boot_binary_load (module, file, argc, argv);
   if (grub_errno == GRUB_ERR_NONE)
-    grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+    {
+      grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+      lf2_start (&module->lf2);
+    }
 
  fail:
   if (file)
@@ -505,6 +576,105 @@ fail:
   return grub_errno;
 }
 
+static void
+lf2_init(lf2_t *lf2, initrd_media_device_path_t *media)
+{
+  lf2->lf2.load_file = grub_efi_load_file2;
+  lf2->handle = NULL;
+  lf2->media = media;
+}
+
+static grub_err_t
+lf2_start(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+  grub_efi_status_t status;
+
+  if (lf2->handle)
+    return GRUB_ERR_NONE;
+
+  status = b->install_multiple_protocol_interfaces (&lf2->handle,
+                                                   &load_file2_guid,
+                                                   &lf2->lf2,
+                                                   &device_path_guid,
+                                                   lf2->media,
+                                                   NULL);
+  if (status == GRUB_EFI_OUT_OF_RESOURCES)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+      return grub_errno;
+    }
+  else if (status != GRUB_EFI_SUCCESS)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("failed to install protocols"));
+      return grub_errno;
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static void
+lf2_done(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+
+  if (!lf2->handle)
+    return;
+
+  b->uninstall_multiple_protocol_interfaces (lf2->handle,
+                                            &load_file2_guid,
+                                            &lf2->lf2,
+                                            &device_path_guid,
+                                            lf2->media,
+                                            NULL);
+  lf2->handle = NULL;
+}
+
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer)
+{
+  grub_efi_uintn_t size;
+  struct xen_boot_binary *binary, *module;
+  const void *src;
+
+  binary = NULL;
+  FOR_LIST_ELEMENTS (module, module_head)
+    if (this == &module->lf2.lf2)
+      {
+        binary = module;
+        break;
+      }
+
+  if (!binary || buffer_size == NULL)
+    return GRUB_EFI_INVALID_PARAMETER;
+
+  if (device_path->type != GRUB_EFI_END_DEVICE_PATH_TYPE ||
+      device_path->subtype != GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE)
+    return GRUB_EFI_NOT_FOUND;
+

grub-core/loader/arm64/xen_boot.c fail: · line 669

Quoted 10 lines
grub-core/loader/arm64/xen_boot.c +10 -0
+  if (boot_policy)
+    return GRUB_EFI_UNSUPPORTED;
+
+  size = binary->size;
+  if (buffer == NULL || *buffer_size < size)
+    {
+      *buffer_size = size;
+      return GRUB_EFI_BUFFER_TOO_SMALL;
+    }
+

This is not compliant with the spec:

Quoted 2 lines

BufferSize ... On output with a return code of EFI_SUCCESS, the amount of data transferred to Buffer.

It looks like you copied this from the EFI Linux loader which has the same issue.

Ross

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw

Re: [RFC PATCH 4/5] arm64/xen: Use LoadFile2 protocol for Xen boot

Quoted 224 lines · Ross Lagerwall
Quoted 221 lines · Frediano Ziglio via Grub-devel

Allows to load modules using LoadFile2 protocol. Add and use a new GUID for kernel media device. This will allow Xen to pick up additional modules using EFI interface instead of using multiboot2 interface (not available on x86_64).

---
 grub-core/loader/arm64/xen_boot.c | 172 +++++++++++++++++++++++++++++-
 include/grub/efi/api.h            |   5 +
 2 files changed, 176 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +157 -1
index 2975a546e..64f685de6 100644
@@ -68,6 +68,56 @@ enum module_type
 };
 typedef enum module_type module_type_t;
 
+static grub_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+static initrd_media_device_path_t initrd_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    LINUX_EFI_INITRD_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+static initrd_media_device_path_t kernel_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    XEN_EFI_KERNEL_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+typedef struct lf2
+{
+  grub_efi_load_file2_t lf2;
+  grub_efi_handle_t handle;
+  initrd_media_device_path_t *media;
+} lf2_t;
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer);
+static void lf2_init(lf2_t *lf2, initrd_media_device_path_t *media);
+static grub_err_t lf2_start(lf2_t *lf2);
+static void lf2_done(lf2_t *lf2);
+
 struct xen_boot_binary
 {
   struct xen_boot_binary *next;
@@ -80,6 +130,8 @@ struct xen_boot_binary
 
   char *cmdline;
   int cmdline_size;
+
+  lf2_t lf2;
 };
 
 static grub_dl_t my_mod;
@@ -289,6 +341,8 @@ single_binary_unload (struct xen_boot_binary *binary)
                    binary->cmdline, binary->cmdline_size);
     }
 
+  lf2_done (&binary->lf2);
+
   if (!binary->is_hypervisor)
     grub_list_remove (GRUB_AS_LIST (binary));
 
@@ -393,6 +447,7 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
   struct xen_boot_binary *module = NULL;
   grub_file_t file = 0;
   int nounzip = 0;
+  initrd_media_device_path_t *media;
 
   if (!argc)
     {
@@ -427,6 +482,19 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   module->is_hypervisor = false;
   module->align = 4096;
+  switch (grub_list_length (GRUB_AS_LIST (module_head)))
+    {
+    case MODULE_IMAGE:
+      media = &kernel_lf2_device_path;
+      break;
+    case MODULE_INITRD:
+      media = &initrd_lf2_device_path;
+      break;
+    default:
+      media = NULL;
+      break;
+    }
+  lf2_init(&module->lf2, media);
 
   grub_dprintf ("xen_loader", "Init module and node info\n");
 
@@ -438,7 +506,10 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   xen_boot_binary_load (module, file, argc, argv);
   if (grub_errno == GRUB_ERR_NONE)
-    grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+    {
+      grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+      lf2_start (&module->lf2);
+    }
 
  fail:
   if (file)
@@ -505,6 +576,105 @@ fail:
   return grub_errno;
 }
 
+static void
+lf2_init(lf2_t *lf2, initrd_media_device_path_t *media)
+{
+  lf2->lf2.load_file = grub_efi_load_file2;
+  lf2->handle = NULL;
+  lf2->media = media;
+}
+
+static grub_err_t
+lf2_start(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+  grub_efi_status_t status;
+
+  if (lf2->handle)
+    return GRUB_ERR_NONE;
+
+  status = b->install_multiple_protocol_interfaces (&lf2->handle,
+                                                   &load_file2_guid,
+                                                   &lf2->lf2,
+                                                   &device_path_guid,
+                                                   lf2->media,
+                                                   NULL);
+  if (status == GRUB_EFI_OUT_OF_RESOURCES)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+      return grub_errno;
+    }
+  else if (status != GRUB_EFI_SUCCESS)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("failed to install protocols"));
+      return grub_errno;
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static void
+lf2_done(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+
+  if (!lf2->handle)
+    return;
+
+  b->uninstall_multiple_protocol_interfaces (lf2->handle,
+                                            &load_file2_guid,
+                                            &lf2->lf2,
+                                            &device_path_guid,
+                                            lf2->media,
+                                            NULL);
+  lf2->handle = NULL;
+}
+
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer)
+{
+  grub_efi_uintn_t size;
+  struct xen_boot_binary *binary, *module;
+  const void *src;
+
+  binary = NULL;
+  FOR_LIST_ELEMENTS (module, module_head)
+    if (this == &module->lf2.lf2)
+      {
+        binary = module;
+        break;
+      }
+
+  if (!binary || buffer_size == NULL)
+    return GRUB_EFI_INVALID_PARAMETER;
+
+  if (device_path->type != GRUB_EFI_END_DEVICE_PATH_TYPE ||
+      device_path->subtype != GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE)
+    return GRUB_EFI_NOT_FOUND;
+
+  if (boot_policy)
+    return GRUB_EFI_UNSUPPORTED;
+
+  size = binary->size;
Quoted 12 lines

grub-core/loader/arm64/xen_boot.c fail: · line 669

Quoted 6 lines
grub-core/loader/arm64/xen_boot.c +6 -0
+  if (buffer == NULL || *buffer_size < size)
+    {
+      *buffer_size = size;
+      return GRUB_EFI_BUFFER_TOO_SMALL;
+    }
+

This is not compliant with the spec:

Quoted 2 lines

BufferSize ... On output with a return code of EFI_SUCCESS, the amount of data transferred to Buffer.

From https://uefi.org/specs/UEFI/2.10/13_Protocols_Media_Access.html#efi-load-file-protocol-loadfile

BufferSize On input the size of Buffer in bytes. On output with a return code of EFI_SUCCESS, the amount of data transferred to Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, the size of Buffer required to retrieve the requested file.

It's coherent with the second sentence. Otherwise how does Linux get the size of the file?

Quoted 4 lines

It looks like you copied this from the EFI Linux loader which has the same issue.

Ross

Frediano

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw

Re: [RFC PATCH 4/5] arm64/xen: Use LoadFile2 protocol for Xen boot

Quoted 241 lines · Frediano Ziglio
Quoted 224 lines · Ross Lagerwall
Quoted 221 lines · Frediano Ziglio via Grub-devel

Allows to load modules using LoadFile2 protocol. Add and use a new GUID for kernel media device. This will allow Xen to pick up additional modules using EFI interface instead of using multiboot2 interface (not available on x86_64).

---
 grub-core/loader/arm64/xen_boot.c | 172 +++++++++++++++++++++++++++++-
 include/grub/efi/api.h            |   5 +
 2 files changed, 176 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +157 -1
index 2975a546e..64f685de6 100644
@@ -68,6 +68,56 @@ enum module_type
 };
 typedef enum module_type module_type_t;
 
+static grub_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID;
+static grub_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+static initrd_media_device_path_t initrd_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    LINUX_EFI_INITRD_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+static initrd_media_device_path_t kernel_lf2_device_path = {
+  {
+    {
+      GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
+      GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE,
+      sizeof(grub_efi_vendor_media_device_path_t),
+    },
+    XEN_EFI_KERNEL_MEDIA_GUID
+  }, {
+    GRUB_EFI_END_DEVICE_PATH_TYPE,
+    GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    sizeof(grub_efi_device_path_t)
+  }
+};
+
+typedef struct lf2
+{
+  grub_efi_load_file2_t lf2;
+  grub_efi_handle_t handle;
+  initrd_media_device_path_t *media;
+} lf2_t;
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer);
+static void lf2_init(lf2_t *lf2, initrd_media_device_path_t *media);
+static grub_err_t lf2_start(lf2_t *lf2);
+static void lf2_done(lf2_t *lf2);
+
 struct xen_boot_binary
 {
   struct xen_boot_binary *next;
@@ -80,6 +130,8 @@ struct xen_boot_binary
 
   char *cmdline;
   int cmdline_size;
+
+  lf2_t lf2;
 };
 
 static grub_dl_t my_mod;
@@ -289,6 +341,8 @@ single_binary_unload (struct xen_boot_binary *binary)
                    binary->cmdline, binary->cmdline_size);
     }
 
+  lf2_done (&binary->lf2);
+
   if (!binary->is_hypervisor)
     grub_list_remove (GRUB_AS_LIST (binary));
 
@@ -393,6 +447,7 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
   struct xen_boot_binary *module = NULL;
   grub_file_t file = 0;
   int nounzip = 0;
+  initrd_media_device_path_t *media;
 
   if (!argc)
     {
@@ -427,6 +482,19 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   module->is_hypervisor = false;
   module->align = 4096;
+  switch (grub_list_length (GRUB_AS_LIST (module_head)))
+    {
+    case MODULE_IMAGE:
+      media = &kernel_lf2_device_path;
+      break;
+    case MODULE_INITRD:
+      media = &initrd_lf2_device_path;
+      break;
+    default:
+      media = NULL;
+      break;
+    }
+  lf2_init(&module->lf2, media);
 
   grub_dprintf ("xen_loader", "Init module and node info\n");
 
@@ -438,7 +506,10 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
 
   xen_boot_binary_load (module, file, argc, argv);
   if (grub_errno == GRUB_ERR_NONE)
-    grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+    {
+      grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+      lf2_start (&module->lf2);
+    }
 
  fail:
   if (file)
@@ -505,6 +576,105 @@ fail:
   return grub_errno;
 }
 
+static void
+lf2_init(lf2_t *lf2, initrd_media_device_path_t *media)
+{
+  lf2->lf2.load_file = grub_efi_load_file2;
+  lf2->handle = NULL;
+  lf2->media = media;
+}
+
+static grub_err_t
+lf2_start(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+  grub_efi_status_t status;
+
+  if (lf2->handle)
+    return GRUB_ERR_NONE;
+
+  status = b->install_multiple_protocol_interfaces (&lf2->handle,
+                                                   &load_file2_guid,
+                                                   &lf2->lf2,
+                                                   &device_path_guid,
+                                                   lf2->media,
+                                                   NULL);
+  if (status == GRUB_EFI_OUT_OF_RESOURCES)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+      return grub_errno;
+    }
+  else if (status != GRUB_EFI_SUCCESS)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("failed to install protocols"));
+      return grub_errno;
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static void
+lf2_done(lf2_t *lf2)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+
+  if (!lf2->handle)
+    return;
+
+  b->uninstall_multiple_protocol_interfaces (lf2->handle,
+                                            &load_file2_guid,
+                                            &lf2->lf2,
+                                            &device_path_guid,
+                                            lf2->media,
+                                            NULL);
+  lf2->handle = NULL;
+}
+
+
+static grub_efi_status_t __grub_efi_api
+grub_efi_load_file2 (grub_efi_load_file2_t *this,
+                     grub_efi_device_path_t *device_path,
+                     grub_efi_boolean_t boot_policy,
+                     grub_efi_uintn_t *buffer_size,
+                     void *buffer)
+{
+  grub_efi_uintn_t size;
+  struct xen_boot_binary *binary, *module;
+  const void *src;
+
+  binary = NULL;
+  FOR_LIST_ELEMENTS (module, module_head)
+    if (this == &module->lf2.lf2)
+      {
+        binary = module;
+        break;
+      }
+
+  if (!binary || buffer_size == NULL)
+    return GRUB_EFI_INVALID_PARAMETER;
+
+  if (device_path->type != GRUB_EFI_END_DEVICE_PATH_TYPE ||
+      device_path->subtype != GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE)
+    return GRUB_EFI_NOT_FOUND;
+
+  if (boot_policy)
+    return GRUB_EFI_UNSUPPORTED;
+
+  size = binary->size;
Quoted 12 lines

grub-core/loader/arm64/xen_boot.c fail: · line 669

Quoted 6 lines
grub-core/loader/arm64/xen_boot.c +6 -0
+  if (buffer == NULL || *buffer_size < size)
+    {
+      *buffer_size = size;
+      return GRUB_EFI_BUFFER_TOO_SMALL;
+    }
+

This is not compliant with the spec:

Quoted 2 lines

BufferSize ... On output with a return code of EFI_SUCCESS, the amount of data transferred to Buffer.

From https://uefi.org/specs/UEFI/2.10/13_Protocols_Media_Access.html#efi-load-file-protocol-loadfile

Quoted 8 lines

BufferSize On input the size of Buffer in bytes. On output with a return code of EFI_SUCCESS, the amount of data transferred to Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL, the size of Buffer required to retrieve the requested file.

It's coherent with the second sentence. Otherwise how does Linux get the size of the file?

Well it is half compliant. It sets the size when returning EFI_BUFFER_TOO_SMALL which is enough to make Linux function, but doesn't set the size when returning EFI_SUCCESS which is not compliant with the spec.

Ross

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#14 lore raw

RFC PATCH 5/5 arm64/xen: Allows to use kernel command line from first module

In reply to Frediano Ziglio via Grub-devel

For ARM64 command lines are passed using device tree, for x86_64 we need to use LoadOptions putting both Xen and kernel arguments.
On x86_64 the LoadOptions is composed by 2 parts, EFI options and Xen options. The Xen options is composed by Xen options and, optionally, kernel options.
In order to have options for both Xen (xen_hypervisor) and kernel (first xen_module) compose the command lines.

---
 grub-core/loader/arm64/xen_boot.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +24 -1
index 64f685de6..bf66db3eb 100644
@@ -302,6 +302,29 @@ fail:
 
   return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
 #else
+  const char *xen_cmdline, *start = "placeholder -- ";
+  char *new_cmdline;
+
+  /* Concatenate Xen command line with kernel command line */
+  if (!module_head || !module_head->cmdline || !module_head->cmdline_size)
+    return GRUB_ERR_NONE;
+
+  xen_cmdline = xen_hypervisor->cmdline ? xen_hypervisor->cmdline : "";
+  if (grub_strncmp(xen_cmdline, "-- ", 3) == 0 ||
+      grub_strstr(xen_cmdline, " -- ") != NULL)
+    start = "";
+  new_cmdline =
+    grub_xasprintf ("%s%s -- %s", start, xen_cmdline, module_head->cmdline);
+  if (!new_cmdline)
+    return grub_errno;
+
+  grub_free (xen_hypervisor->cmdline);
+  grub_free (module_head->cmdline);
+  xen_hypervisor->cmdline = new_cmdline;
+  xen_hypervisor->cmdline_size = grub_strlen (new_cmdline);
+  module_head->cmdline = NULL;
+  module_head->cmdline_size = 0;
+
   return GRUB_ERR_NONE;
 #endif
 }
@@ -507,7 +530,7 @@ grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
   xen_boot_binary_load (module, file, argc, argv);
   if (grub_errno == GRUB_ERR_NONE)
     {
-      grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
+      grub_list_append (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
       lf2_start (&module->lf2);
     }
 
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#15 lore raw

Re: [RFC PATCH 5/5] arm64/xen: Allows to use kernel command line from first module

Quoted 33 lines · Frediano Ziglio via Grub-devel

For ARM64 command lines are passed using device tree, for x86_64 we need to use LoadOptions putting both Xen and kernel arguments.
On x86_64 the LoadOptions is composed by 2 parts, EFI options and Xen options. The Xen options is composed by Xen options and, optionally, kernel options.
In order to have options for both Xen (xen_hypervisor) and kernel (first xen_module) compose the command lines.

---
 grub-core/loader/arm64/xen_boot.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +10 -0
index 64f685de6..bf66db3eb 100644
@@ -302,6 +302,29 @@ fail:
 
   return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
 #else
+  const char *xen_cmdline, *start = "placeholder -- ";
+  char *new_cmdline;
+
+  /* Concatenate Xen command line with kernel command line */
+  if (!module_head || !module_head->cmdline || !module_head->cmdline_size)
+    return GRUB_ERR_NONE;
+
+  xen_cmdline = xen_hypervisor->cmdline ? xen_hypervisor->cmdline : "";
+  if (grub_strncmp(xen_cmdline, "-- ", 3) == 0 ||
+      grub_strstr(xen_cmdline, " -- ") != NULL)

grub-core/loader/arm64/xen_boot.c fail: · line 324

Quoted 10 lines
grub-core/loader/arm64/xen_boot.c +10 -0
+    start = "";
+  new_cmdline =
+    grub_xasprintf ("%s%s -- %s", start, xen_cmdline, module_head->cmdline);
+  if (!new_cmdline)
+    return grub_errno;
+
+  grub_free (xen_hypervisor->cmdline);
+  grub_free (module_head->cmdline);
+  xen_hypervisor->cmdline = new_cmdline;
+  xen_hypervisor->cmdline_size = grub_strlen (new_cmdline);

I think this is supposed to include the NUL terminator.

Ross

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#16 lore raw

Re: [RFC PATCH 5/5] arm64/xen: Allows to use kernel command line from first module

Quoted 40 lines · Ross Lagerwall
Quoted 37 lines · Frediano Ziglio via Grub-devel

For ARM64 command lines are passed using device tree, for x86_64 we need to use LoadOptions putting both Xen and kernel arguments.
On x86_64 the LoadOptions is composed by 2 parts, EFI options and Xen options. The Xen options is composed by Xen options and, optionally, kernel options.
In order to have options for both Xen (xen_hypervisor) and kernel (first xen_module) compose the command lines.

---
 grub-core/loader/arm64/xen_boot.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
grub-core/loader/arm64/xen_boot.c +14 -0
index 64f685de6..bf66db3eb 100644
@@ -302,6 +302,29 @@ fail:
 
   return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
 #else
+  const char *xen_cmdline, *start = "placeholder -- ";
+  char *new_cmdline;
+
+  /* Concatenate Xen command line with kernel command line */
+  if (!module_head || !module_head->cmdline || !module_head->cmdline_size)
+    return GRUB_ERR_NONE;
+
+  xen_cmdline = xen_hypervisor->cmdline ? xen_hypervisor->cmdline : "";
+  if (grub_strncmp(xen_cmdline, "-- ", 3) == 0 ||
+      grub_strstr(xen_cmdline, " -- ") != NULL)
+    start = "";
+  new_cmdline =
+    grub_xasprintf ("%s%s -- %s", start, xen_cmdline, module_head->cmdline);
+  if (!new_cmdline)
Quoted 9 lines

grub-core/loader/arm64/xen_boot.c fail: · line 324

Quoted 6 lines
grub-core/loader/arm64/xen_boot.c +6 -0
+    return grub_errno;
+
+  grub_free (xen_hypervisor->cmdline);
+  grub_free (module_head->cmdline);
+  xen_hypervisor->cmdline = new_cmdline;
+  xen_hypervisor->cmdline_size = grub_strlen (new_cmdline);

I think this is supposed to include the NUL terminator.

Yes, changing.

Quoted 1 line

Ross

Thanks,
Frediano

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#17 lore raw
In reply to Frediano Ziglio via Grub-devel
Quoted 13 lines · Frediano Ziglio via Grub-devel

Currently xen_hypervisor and xen_module commands are available only for ARM64 EFI.
For x86_64 if the Xen format is portable executable there's no way to pass multiple modules (the feature is available using multiboot2 but only for ELF files).
So instead on adding new GRUB commands reuse the already available ones.
The modules are passed using LoadFile2 already used to pass initrd to Linux kernel.
An extension allows to pass also kernel file.
To pass command lines uses LoadOptions, EFI code already handles passing Xen and kernel command lines using LoadOptions.

Quoted 6 lines

Frediano Ziglio (5):
kern/list: Add a grub_list_length function
kern/list: Add a grub_list_append function
arm64/xen: Allows to use xen_hypervisor and xen_module on x86_64-efi arm64/xen: Use LoadFile2 protocol for Xen boot
arm64/xen: Allows to use kernel command line from first module

When you send Xen patches please CC Xen-devel mailing list as well...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel