Skip to contentSkip to conversation

hw/arm: add support for shim loading

PATCH Applied Gerd Hoffmann 3 participants 21 messages May 21 – Jul 3

lore
Share & tools
lore
#0 lore raw

cover hw/arm: add support for shim loading

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

 hw/arm/boot.c             |  6 ++++++
 hw/i386/x86-common.c      | 15 ++-------------
 hw/nvram/fw_cfg.c         | 22 ++++++++++++++++++++++
 include/hw/arm/boot.h     |  1 +
 include/hw/nvram/fw_cfg.h | 15 +++++++++++++++
 5 files changed, 46 insertions(+), 13 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/3 hw/nvram: add load_image_to_fw_cfg_file()

Function is simliar to load_image_to_fw_cfg() but loads the image into a named fw_cfg file instead of fixed keys.

---
 hw/nvram/fw_cfg.c         | 22 ++++++++++++++++++++++
 include/hw/nvram/fw_cfg.h | 15 +++++++++++++++
 2 files changed, 37 insertions(+)
hw/nvram/fw_cfg.c +22 -0
index 1d7d835421..d8653d93cd 100644
@@ -1143,6 +1143,28 @@ void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
 }
 
+void load_image_to_fw_cfg_file(FWCfgState *fw_cfg,
+                               const char *fw_cfg_name,
+                               const char *image_name)
+{
+    GMappedFile *mapped_file;
+    GError *gerr = NULL;
+
+    if (image_name == NULL) {
+        return;
+    }
+
+    mapped_file = g_mapped_file_new(image_name, false, &gerr);
+    if (!mapped_file) {
+        error_report("qemu: error reading %s: %s",
+                     image_name, gerr->message);
+        exit(1);
+    }
+    fw_cfg_add_file(fw_cfg, fw_cfg_name,
+                    g_mapped_file_get_contents(mapped_file),
+                    g_mapped_file_get_length(mapped_file));
+}
+
 static void fw_cfg_class_init(ObjectClass *klass, const void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
include/hw/nvram/fw_cfg.h +15 -0
index 56f17a0bdc..7b31bd0ba6 100644
@@ -351,4 +351,19 @@ void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
                           uint16_t data_key, const char *image_name,
                           bool try_decompress);
 
+/**
+ * load_image_to_fw_cfg_file() - Load an image file into an fw_cfg entry
+ *                               identified by fw_cfg file name.
+ * @fw_cfg:         The firmware config instance to store the data in.
+ * @fw_cfg_name:    The name of the fw_cfg (pseudo) file.
+ * @image_name:     The name of the image file to load. If it is NULL, the
+ *                  function returns without doing anything.
+ *
+ * In case of failure, the function prints an error message to stderr and the
+ * process exits with status 1.
+ */
+void load_image_to_fw_cfg_file(FWCfgState *fw_cfg,
+                               const char *fw_cfg_name,
+                               const char *image_name);
+
 #endif
Signature · 1 line
2.54.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] hw/nvram: add load_image_to_fw_cfg_file()

Quoted 8 lines · Gerd Hoffmann

Function is simliar to load_image_to_fw_cfg() but loads the image into a named fw_cfg file instead of fixed keys.

---
  hw/nvram/fw_cfg.c         | 22 ++++++++++++++++++++++
  include/hw/nvram/fw_cfg.h | 15 +++++++++++++++
  2 files changed, 37 insertions(+)
#3 lore raw

PATCH 2/3 hw/i386: switch shim loading to load_image_to_fw_cfg_file

In reply to Gerd Hoffmann

Use the new helper function instead of open-coding the shim image load.

---
 hw/i386/x86-common.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
hw/i386/x86-common.c +2 -13
index fde05fa7d7..8f9419e7d3 100644
@@ -980,19 +980,8 @@ void x86_load_linux(X86MachineState *x86ms,
     fw_cfg_add_file(fw_cfg, "etc/boot/kernel", kernel, kernel_size);
 
     if (machine->shim_filename) {
-        GMappedFile *mapped_file;
-        GError *gerr = NULL;
-
-        mapped_file = g_mapped_file_new(machine->shim_filename, false, &gerr);
-        if (!mapped_file) {
-            fprintf(stderr, "qemu: error reading shim %s: %s\n",
-                    machine->shim_filename, gerr->message);
-            exit(1);
-        }
-
-        fw_cfg_add_file(fw_cfg, "etc/boot/shim",
-                        g_mapped_file_get_contents(mapped_file),
-                        g_mapped_file_get_length(mapped_file));
+        load_image_to_fw_cfg_file(fw_cfg, "etc/boot/shim",
+                                  machine->shim_filename);
     }
 
     if (sev_enabled()) {
Signature · 1 line
2.54.0
#5 lore raw

PATCH 3/3 hw/arm: add support for shim loading

In reply to Gerd Hoffmann

Add support for direct kernel boot with
shim to the arm platform.

---
 hw/arm/boot.c         | 6 ++++++
 include/hw/arm/boot.h | 1 +
 2 files changed, 7 insertions(+)
hw/arm/boot.c +6 -0
index 780b6be637..9b7553dde5 100644
@@ -1161,6 +1161,11 @@ static void arm_setup_firmware_boot(ARMCPU *cpu, struct arm_boot_info *info)
             fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
                               info->kernel_cmdline);
         }
+
+        if (info->shim_filename) {
+            load_image_to_fw_cfg_file(fw_cfg, "etc/boot/shim",
+                                      info->shim_filename);
+        }
     }
 
     /*
@@ -1195,6 +1200,7 @@ void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info)
      * doesn't support secure.
      */
     assert(!(info->secure_board_setup && kvm_enabled()));
+    info->shim_filename = ms->shim_filename;
     info->kernel_filename = ms->kernel_filename;
     info->kernel_cmdline = ms->kernel_cmdline;
     info->initrd_filename = ms->initrd_filename;
include/hw/arm/boot.h +1 -0
index a2e22bda8a..94386fdbea 100644
@@ -39,6 +39,7 @@ void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
 /* arm_boot.c */
 struct arm_boot_info {
     uint64_t ram_size;
+    const char *shim_filename;
     const char *kernel_filename;
     const char *kernel_cmdline;
     const char *initrd_filename;
Signature · 1 line
2.54.0
#6 lore raw
In reply to Gerd Hoffmann
Quoted 8 lines · Gerd Hoffmann

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

Any chance of some information on what this is?

There's no real description of it in the commit messages, and as far as I can see the only documentation we have is the rather cryptic:
-shim shim.efi
Use ‘shim.efi’ to boot the kernel
in the "Invocation" section...

thanks
-- PMM

#7 lore raw
Quoted 11 lines · Peter Maydell
Quoted 8 lines · Gerd Hoffmann

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

Any chance of some information on what this is?

It passes the shim binary to the efi firmware,
in addition to the kernel binary.

x86 has this for a while already, this brings arm on par.

docs update with some background below.

take care,
Gerd

docs/system/linuxboot.rst +9 -0
@@ -17,6 +17,15 @@ Use ``-kernel`` to provide the Linux kernel image and ``-append`` to
 give the kernel command line arguments. The ``-initrd`` option can be
 used to provide an INITRD image.
 
+The ``-shim`` option specifies the shim.efi binary.  This is needed
+when using direct kernel boot with UEFI secure boot enabled.  The
+verification chain used by linux distros requires shim.efi.  Typically
+shim.efi is signed by micsosoft and verified by the firmware.  The
+linux kernel is signed by the distro and is verified by shim.efi.  So
+without shim.efi in the loop secure boot verification will not work.
+Usually you can find shim.efi as ``EFI/BOOT/BOOT{X64,AA64}.EFI`` on
+distro install media.
+
 If you do not need graphical output, you can disable it and redirect the
 virtual serial port and the QEMU monitor to the console with the
 ``-nographic`` option. The typical command line is:
#8 lore raw
Quoted 28 lines · Gerd Hoffmann
Quoted 11 lines · Peter Maydell
Quoted 8 lines · Gerd Hoffmann

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

Any chance of some information on what this is?

It passes the shim binary to the efi firmware,
in addition to the kernel binary.

x86 has this for a while already, this brings arm on par.

docs update with some background below.

take care,
Gerd

docs/system/linuxboot.rst +0 -0
@@ -17,6 +17,15 @@ Use ``-kernel`` to provide the Linux kernel image and ``-append`` to
 give the kernel command line arguments. The ``-initrd`` option can be

docs/system/linuxboot.rst Use ``-kernel`` to provide the Linux kernel image and ``-append`` to · line 27

Quoted 10 lines
docs/system/linuxboot.rst +8 -0
 used to provide an INITRD image.
 
+The ``-shim`` option specifies the shim.efi binary.  This is needed
+when using direct kernel boot with UEFI secure boot enabled.  The
+verification chain used by linux distros requires shim.efi.  Typically
+shim.efi is signed by micsosoft and verified by the firmware.  The
+linux kernel is signed by the distro and is verified by shim.efi.  So
+without shim.efi in the loop secure boot verification will not work.
+Usually you can find shim.efi as ``EFI/BOOT/BOOT{X64,AA64}.EFI`` on
+distro install media.

Thanks. I'm not sure why you'd want to enable UEFI secure boot when you're not booting via UEFI, though.

Do we actually run the shim.efi code, or is it just there to be "verified" ?

-- PMM

#9 lore raw
Quoted 31 lines · Peter Maydell
Quoted 30 lines · Gerd Hoffmann
Quoted 11 lines · Peter Maydell
Quoted 8 lines · Gerd Hoffmann

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

Any chance of some information on what this is?

It passes the shim binary to the efi firmware,
in addition to the kernel binary.

x86 has this for a while already, this brings arm on par.

docs update with some background below.

take care,
Gerd

docs/system/linuxboot.rst +0 -0
@@ -17,6 +17,15 @@ Use ``-kernel`` to provide the Linux kernel image and ``-append`` to
 give the kernel command line arguments. The ``-initrd`` option can be
 used to provide an INITRD image.
 
Quoted 11 lines

docs/system/linuxboot.rst Use ``-kernel`` to provide the Linux kernel image and ``-append`` to · line 27

Quoted 8 lines
docs/system/linuxboot.rst +8 -0
+The ``-shim`` option specifies the shim.efi binary.  This is needed
+when using direct kernel boot with UEFI secure boot enabled.  The
+verification chain used by linux distros requires shim.efi.  Typically
+shim.efi is signed by micsosoft and verified by the firmware.  The
+linux kernel is signed by the distro and is verified by shim.efi.  So
+without shim.efi in the loop secure boot verification will not work.
+Usually you can find shim.efi as ``EFI/BOOT/BOOT{X64,AA64}.EFI`` on
+distro install media.

Thanks. I'm not sure why you'd want to enable UEFI secure boot when you're not booting via UEFI, though.

When not booting via UEFI you can ignore that.

Quoted 2 lines

Do we actually run the shim.efi code, or is it just there to be "verified" ?

edk2 firmware will load + run it (if present).

take care,
Gerd

#10 lore raw
Quoted 45 lines · Gerd Hoffmann
Quoted 31 lines · Peter Maydell
Quoted 30 lines · Gerd Hoffmann
Quoted 11 lines · Peter Maydell
Quoted 8 lines · Gerd Hoffmann

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

Any chance of some information on what this is?

It passes the shim binary to the efi firmware,
in addition to the kernel binary.

x86 has this for a while already, this brings arm on par.

docs update with some background below.

take care,
Gerd

docs/system/linuxboot.rst +0 -0
@@ -17,6 +17,15 @@ Use ``-kernel`` to provide the Linux kernel image and ``-append`` to
 give the kernel command line arguments. The ``-initrd`` option can be
 used to provide an INITRD image.
 
Quoted 11 lines

docs/system/linuxboot.rst Use ``-kernel`` to provide the Linux kernel image and ``-append`` to · line 27

Quoted 8 lines
docs/system/linuxboot.rst +8 -0
+The ``-shim`` option specifies the shim.efi binary.  This is needed
+when using direct kernel boot with UEFI secure boot enabled.  The
+verification chain used by linux distros requires shim.efi.  Typically
+shim.efi is signed by micsosoft and verified by the firmware.  The
+linux kernel is signed by the distro and is verified by shim.efi.  So
+without shim.efi in the loop secure boot verification will not work.
+Usually you can find shim.efi as ``EFI/BOOT/BOOT{X64,AA64}.EFI`` on
+distro install media.

Thanks. I'm not sure why you'd want to enable UEFI secure boot when you're not booting via UEFI, though.

Quoted 1 line

When not booting via UEFI you can ignore that.

But if I'm doing direct kernel boot then by definition I'm not booting via UEFI, I'm directly booting the kernel...

thanks
-- PMM

#11 lore raw

Hi,

Quoted 4 lines
Quoted 1 line

When not booting via UEFI you can ignore that.

But if I'm doing direct kernel boot then by definition I'm not booting via UEFI, I'm directly booting the kernel...

You can (on aarch64) do direct kernel boot both with and without UEFI firmware ...

Without firmware qemu will load the kernel binary, then start the VM with the instruction pointer register pointing to the elf entry point.

With firmware qemu exposes kernel + initrd (and with this patch shim too) via fw_cfg, and the firmware can pick it up from there and run it (instead of trying to boot via disk / cdrom / network).

take care,
Gerd

#12 lore raw
Quoted 11 lines · Gerd Hoffmann

Hi,

Quoted 4 lines
Quoted 1 line

When not booting via UEFI you can ignore that.

But if I'm doing direct kernel boot then by definition I'm not booting via UEFI, I'm directly booting the kernel...

You can (on aarch64) do direct kernel boot both with and without UEFI firmware ...

Quoted 6 lines

Without firmware qemu will load the kernel binary, then start the VM with the instruction pointer register pointing to the elf entry point.

With firmware qemu exposes kernel + initrd (and with this patch shim too) via fw_cfg, and the firmware can pick it up from there and run it (instead of trying to boot via disk / cdrom / network).

I don't really think of the latter as being direct-boot, and nor does our documentation:

https://www.qemu.org/docs/master/system/invocation.html#hxtool-8

# There are broadly 4 ways you can boot a system with QEMU. #

#  - specify a firmware and let it control finding a kernel
#  - specify a firmware and pass a hint to the kernel to boot
#  - direct kernel image boot
#  - manually load files into the guest's address space
#
# The third method is useful for quickly testing kernels but as there is
# no firmware to pass configuration information to the kernel [...]

Anyway, I'll reply to your patch updating the documentation with some suggestions for expanding it.

-- PMM

#13 lore raw

Hi,

Quoted 9 lines
Quoted 3 lines

With firmware qemu exposes kernel + initrd (and with this patch shim too) via fw_cfg, and the firmware can pick it up from there and run it (instead of trying to boot via disk / cdrom / network).

I don't really think of the latter as being direct-boot, and nor does our documentation:

# - specify a firmware and pass a hint to the kernel to boot # - direct kernel image boot

That is a technical detail which doesn't make much of a difference from the end user perspective. And often people talk about "direct kernel boot" without drawing a line between these two cases, because the user-visible behavior is pretty much identical: You pass a kernel to qemu via '-kernel', and it gets booted ...

take care,
Gerd

#14 lore raw
Quoted 10 lines · Gerd Hoffmann

Hi,

Quoted 6 lines
Quoted 3 lines

With firmware qemu exposes kernel + initrd (and with this patch shim too) via fw_cfg, and the firmware can pick it up from there and run it (instead of trying to boot via disk / cdrom / network).

I don't really think of the latter as being direct-boot, and nor does our documentation:

Quoted 8 lines
Quoted 2 lines

# - specify a firmware and pass a hint to the kernel to boot # - direct kernel image boot

That is a technical detail which doesn't make much of a difference from the end user perspective. And often people talk about "direct kernel boot" without drawing a line between these two cases, because the user-visible behavior is pretty much identical: You pass a kernel to qemu via '-kernel', and it gets booted ...

As an end user I find them majorly different -- I usually don't want the firmware, because it takes forever to boot if you do that.

This is probably partly an x86 vs Arm difference: for x86 there is always some kind of BIOS booted, but for Arm there by default is not, and direct kernel boot is much more common.

thanks
-- PMM

#15 lore raw

Hi,

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

# - specify a firmware and pass a hint to the kernel to boot # - direct kernel image boot

That is a technical detail which doesn't make much of a difference from the end user perspective. And often people talk about "direct kernel boot" without drawing a line between these two cases, because the user-visible behavior is pretty much identical: You pass a kernel to qemu via '-kernel', and it gets booted ...

Quoted 3 lines

As an end user I find them majorly different -- I usually don't want the firmware, because it takes forever to boot if you do that.

Not sure what exactly you are doing, but for me edk2 is done in less than a second (unless I turn on firmware debug logging), and that on a rpi4 host which isn't exactly fast hardware. I wouldn't call that "forever".

Quoted 4 lines

This is probably partly an x86 vs Arm difference: for x86 there is always some kind of BIOS booted, but for Arm there by default is not, and direct kernel boot is much more common.

Yes, on x86 you need something (be it ovmf or seabios or qboot) which places smbios and acpi tables properly in guest memory. On arm the linux kernel is happy with the device tree, so it is possible to skip the firmware part (unless you want run the guest in acpi=on mode).

take care,
Gerd

#16 lore raw
Quoted 12 lines · Gerd Hoffmann

Hi,

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

# - specify a firmware and pass a hint to the kernel to boot # - direct kernel image boot

That is a technical detail which doesn't make much of a difference from the end user perspective. And often people talk about "direct kernel boot" without drawing a line between these two cases, because the user-visible behavior is pretty much identical: You pass a kernel to qemu via '-kernel', and it gets booted ...

Quoted 8 lines
Quoted 3 lines

As an end user I find them majorly different -- I usually don't want the firmware, because it takes forever to boot if you do that.

Not sure what exactly you are doing, but for me edk2 is done in less than a second (unless I turn on firmware debug logging), and that on a rpi4 host which isn't exactly fast hardware. I wouldn't call that "forever".

If you're running TCG emulation then booting EDK2 is definitely not that fast, and I think a lot of use of QEMU for aarch64 targets is TCG, not KVM.

thanks
-- PMM

#17 lore raw
Quoted 17 lines · Peter Maydell
Quoted 16 lines · Gerd Hoffmann

Hi,

Quoted 12 lines
Quoted 8 lines
Quoted 2 lines

# - specify a firmware and pass a hint to the kernel to boot # - direct kernel image boot

That is a technical detail which doesn't make much of a difference from the end user perspective. And often people talk about "direct kernel boot" without drawing a line between these two cases, because the user-visible behavior is pretty much identical: You pass a kernel to qemu via '-kernel', and it gets booted ...

As an end user I find them majorly different -- I usually don't want the firmware, because it takes forever to boot if you do that.

Quoted 8 lines
Quoted 4 lines

Not sure what exactly you are doing, but for me edk2 is done in less than a second (unless I turn on firmware debug logging), and that on a rpi4 host which isn't exactly fast hardware. I wouldn't call that "forever".

If you're running TCG emulation then booting EDK2 is definitely not that fast, and I think a lot of use of QEMU for aarch64 targets is TCG, not KVM.

On my x86 workstation edk2 boot time on tcg is not very different from the rpi4 with kvm, also less than one second (until it complains that the kernel isn't arm ;).

The rpi4 with tcg takes quite some time indeed, but edk2 isn't exceptionally slow. Booting until the kernel panics because there is no root filesystem takes ~30 seconds, and those are roughly 50% / 50% splitted between firmware and kernel. A good chunk of the firmware time is probably the zboot efi stub for kernel self-decompression (would have to instrument the firmware for more details on that).

Comparing that to direct boot without firmware does not work for me:

unable to handle EFI zboot image with "zstd" compression
qemu-system-aarch64: could not load kernel '/boot/vmlinuz-7.0.11-200.fc44.aarch64'

Anyway, I'll go over the comments and try to make the docs patch more clear for people with less UEFI background. Update should come later this week.

take care,
Gerd

#18 lore raw
Quoted 4 lines · Gerd Hoffmann

The rpi4 with tcg takes quite some time indeed, but edk2 isn't exceptionally slow. Booting until the kernel panics because there is no root filesystem takes ~30 seconds, and those are roughly 50% / 50% splitted between firmware and kernel.

Put another way, booting via EDK2 is doubling the time taken :-)

Quoted 8 lines

A good chunk of the firmware time
is probably the zboot efi stub for kernel self-decompression (would have to instrument the firmware for more details on that).

Comparing that to direct boot without firmware does not work for me:

unable to handle EFI zboot image with "zstd" compression
qemu-system-aarch64: could not load kernel '/boot/vmlinuz-7.0.11-200.fc44.aarch64'

That error happens if you didn't have libzstd-dev available when you built QEMU. A QEMU with zstd support should be able to decompress a zstd EFI image.

thanks
-- PMM

#19 lore raw
In reply to Peter Maydell
Quoted 11 lines · Peter Maydell
Quoted 8 lines · Gerd Hoffmann

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

Any chance of some information on what this is?

Ping. The doc update with additional info is merged, this isn't yet.

Do you want take this through the arm queue? Alternatively I can merge this via firmware queue.

thanks,
Gerd

#20 lore raw
Quoted 12 lines · Gerd Hoffmann
Quoted 10 lines · Peter Maydell
Quoted 8 lines · Gerd Hoffmann

Also little refactoring in preparation
to avoid code duplication.

Gerd Hoffmann (3):
hw/nvram: add load_image_to_fw_cfg_file() hw/i386: switch shim loading to load_image_to_fw_cfg_file hw/arm: add support for shim loading

Quoted 7 lines
Quoted 1 line

Any chance of some information on what this is?

Ping. The doc update with additional info is merged, this isn't yet.

Do you want take this through the arm queue? Alternatively I can merge this via firmware queue.

I've applied it to target-arm.next; thanks.

-- PMM