Skip to contentSkip to conversation

hw/arm/virt: boot with -kernel -initrd -append on top of UEFI

PATCH In review Laszlo Ersek 2 participants 6 messages Dec 5, 2014 – Dec 9, 2014

lore
Share & tools
lore
#0 lore raw

cover hw/arm/virt: boot with -kernel -initrd -append on top of UEFI

A number of tools depend on passing the kernel image, the initial ramdisk, and the kernel command line to the guest on the QEMU command line (options -kernel, -initrd, -append, respectively). At the moment, these QEMU options work, but the guest kernel loaded this way is launched by a minimal binary firmware that is dynamically composed by QEMU. As a consequence, such a kernel has no UEFI environment.

In this patchset the kernel, initrd and cmdline blobs are passed to the guest firmware to process & load. This mode is activated when both -kernel and -pflash unit#0 (or -bios) are specified.

Here's an inter-series dependency map:

  kernel  qemu  edk2  topic
  ------  ----  ----  --------------------------------------------------
  [0]                 device tree binding spec for the fw_cfg device
   ^
   |
   +------[1]<--[2]   fw_cfg device implementation
           ^     ^    and bootorder handling
           |     |
           |     |
          [3]<--[4]   -kernel / -initrd / -append booting on top of UEFI

[0] http://thread.gmane.org/gmane.linux.drivers.devicetree/101276
[kernel PATCH v2 0/2] devicetree: document ARM bindings for QEMU's
Firmware Config interface

ACKed by Peter, queued by Rob Herring

[1] http://thread.gmane.org/gmane.comp.emulators.qemu/308650
[qemu-devel] [PATCH v2] arm: add fw_cfg to "virt" board

pending review

[2] http://thread.gmane.org/gmane.comp.bios.tianocore.devel/11591
[edk2] [PATCH v2 00/12] consume fw_cfg boot order in
ArmVirtualizationQemu

pending review

[3] this patchset

[qemu-devel] [PATCH 0/3] hw/arm/virt: boot with -kernel -initrd
-append on top of UEFI

[4] to be posted

Thanks
Laszlo

Laszlo Ersek (3):
hw/loader: split out load_image_gzipped_buffer() hw/arm: pass pristine kernel image to guest firmware over fw_cfg hw/arm/virt: enable passing of EFI-stubbed kernel to guest UEFI
firmware

 include/hw/arm/arm.h |  5 +++
 include/hw/loader.h  |  9 ++++++
 hw/arm/boot.c        | 91 +++++++++++++++++++++++++++++++++++++++++++++++++---
 hw/arm/virt.c        |  1 +
 hw/core/loader.c     | 30 +++++++++++------
 5 files changed, 122 insertions(+), 14 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 hw/loader: split out load_image_gzipped_buffer()

In the next patch we'd like to reuse the image decompression facility without installing the output as a ROM at a specific guest-phys address.

In addition, expose LOAD_IMAGE_MAX_GUNZIP_BYTES, because that's a straightforward "max_sz" argument for the new load_image_gzipped_buffer().

---
 include/hw/loader.h |  9 +++++++++
 hw/core/loader.c    | 30 +++++++++++++++++++++---------
 2 files changed, 30 insertions(+), 9 deletions(-)
include/hw/loader.h +9 -0
index 6481639..8997620 100644
@@ -16,6 +16,15 @@ int load_image(const char *filename, uint8_t *addr); /* deprecated */
 ssize_t load_image_size(const char *filename, void *addr, size_t size);
 int load_image_targphys(const char *filename, hwaddr,
                         uint64_t max_sz);
+
+/* This is the limit on the maximum uncompressed image size that
+ * load_image_gzipped_buffer() and load_image_gzipped() will read. It prevents
+ * g_malloc() in those functions from allocating a huge amount of memory.
+ */
+#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
+
+int load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
+                              uint8_t **buffer);
 int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz);
 
 #define ELF_LOAD_FAILED       -1
hw/core/loader.c +21 -9
index 7527fd3..933ab64 100644
@@ -614,14 +614,9 @@ int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
                             NULL, NULL);
 }
 
-/* This simply prevents g_malloc in the function below from allocating
- * a huge amount of memory, by placing a limit on the maximum
- * uncompressed image size that load_image_gzipped will read.
- */
-#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
-
-/* Load a gzip-compressed kernel. */
-int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
+/* Load a gzip-compressed kernel to a dynamically allocated buffer. */
+int load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
+                              uint8_t **buffer)
 {
     uint8_t *compressed_data = NULL;
     uint8_t *data = NULL;
@@ -653,8 +648,11 @@ int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
         goto out;
     }
 
-    rom_add_blob_fixed(filename, data, bytes, addr);
+    /* trim to actual size and return to caller */
+    *buffer = g_realloc(data, bytes);
     ret = bytes;
+    /* ownership has been transferred to caller */
+    data = NULL;
 
  out:
     g_free(compressed_data);
@@ -662,6 +660,20 @@ int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
     return ret;
 }
 
+/* Load a gzip-compressed kernel. */
+int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
+{
+    int bytes;
+    uint8_t *data;
+
+    bytes = load_image_gzipped_buffer (filename, max_sz, &data);
+    if (bytes != -1) {
+        rom_add_blob_fixed(filename, data, bytes, addr);
+        g_free(data);
+    }
+    return bytes;
+}
+
 /*
  * Functions for reboot-persistent memory regions.
  *  - used for vga bios and option roms.
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 hw/arm: pass pristine kernel image to guest firmware over fw_cfg

In reply to Laszlo Ersek

Introduce the new boolean field "arm_boot_info.firmware_loaded". When this field is set, it means that the portion of guest DRAM that the VCPU normally starts to execute, or the pflash chip that the VCPU normally starts to execute, has been populated by board-specific code with full-fledged guest firmware code, before the board calls arm_load_kernel().

Simultaneously, "arm_boot_info.firmware_loaded" guarantees that the board code has set up the global firmware config instance, for arm_load_kernel() to find with fw_cfg_find().

Guest kernel (-kernel) and guest firmware (-bios, -pflash) has always been possible to specify independently on the command line. The following cases should be considered:

nr -bios -pflash -kernel description

             unit#0
--  -------  -------  -------  -------------------------------------------
1   present  present  absent   Board code rejects this case, -bios and
    present  present  present  -pflash unit#0 are exclusive. Left intact

by this patch.

2 absent absent present Traditional kernel loading, with qemu's
minimal board firmware. Left intact by this patch.

3   absent   present  absent   Preexistent case for booting guest firmware
    present  absent   absent   loaded with -bios or -pflash. Left intact

by this patch.

4 absent absent absent Preexistent case for not loading any
firmware or kernel up-front. Left intact by this patch.

5   present  absent   present  New case introduced by this patch: kernel
    absent   present  present  image is passed to externally loaded

firmware in unmodified form, using fw_cfg.

An easy way to see that this patch doesn't interfere with existing cases is to realize that "info->firmware_loaded" is constant zero at this point. Which makes the "outer" condition unchanged, and the "inner" condition (with the fw_cfg-related code) dead.

---
 include/hw/arm/arm.h |  5 +++
 hw/arm/boot.c        | 91 +++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 91 insertions(+), 5 deletions(-)
include/hw/arm/arm.h +5 -0
index cefc9e6..dd69d66 100644
@@ -66,6 +66,11 @@ struct arm_boot_info {
     hwaddr initrd_start;
     hwaddr initrd_size;
     hwaddr entry;
+
+    /* Boot firmware has been loaded, typically at address 0, with -bios or
+     * -pflash. It also implies that fw_cfg_find() will succeed.
+     */
+    bool firmware_loaded;
 };
 void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info);
 
hw/arm/boot.c +86 -5

111 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/3 hw/arm/virt: enable passing of EFI-stubbed kernel to guest UEFI firmware

In reply to Laszlo Ersek

The virt board already ensures mutual exclusion between -bios and -pflash unit#0; we only need to set "bootinfo.firmware_loaded", introduced in the previous patch, if either of those options was used to load the guest firmware.

---
 hw/arm/virt.c | 1 +
 1 file changed, 1 insertion(+)
hw/arm/virt.c +1 -0
index af794ea..584c40d 100644
@@ -633,6 +633,7 @@ static void machvirt_init(MachineState *machine)
     vbi->bootinfo.board_id = -1;
     vbi->bootinfo.loader_start = vbi->memmap[VIRT_MEM].base;
     vbi->bootinfo.get_dtb = machvirt_dtb;
+    vbi->bootinfo.firmware_loaded = bios_name || drive_get(IF_PFLASH, 0, 0);
     arm_load_kernel(ARM_CPU(first_cpu), &vbi->bootinfo);
 }
 
Signature · 1 line
1.8.3.1
#4 lore raw
In reply to Laszlo Ersek
Quoted 10 lines · Laszlo Ersek

A number of tools depend on passing the kernel image, the initial ramdisk, and the kernel command line to the guest on the QEMU command line (options -kernel, -initrd, -append, respectively). At the moment, these QEMU options work, but the guest kernel loaded this way is launched by a minimal binary firmware that is dynamically composed by QEMU. As a consequence, such a kernel has no UEFI environment.

In this patchset the kernel, initrd and cmdline blobs are passed to the guest firmware to process & load. This mode is activated when both -kernel and -pflash unit#0 (or -bios) are specified.

...so we avoid back-compat problems on the assumption that nobody was previously trying to provide both -bios and -kernel at once. I *think* that's probably a reasonable assumption...

-- PMM

#5 lore raw
Quoted 8 lines · Peter Maydell
Quoted 7 lines · Laszlo Ersek

A number of tools depend on passing the kernel image, the initial ramdisk, and the kernel command line to the guest on the QEMU command line (options -kernel, -initrd, -append, respectively). At the moment, these QEMU options work, but the guest kernel loaded this way is launched by a minimal binary firmware that is dynamically composed by QEMU. As a consequence, such a kernel has no UEFI environment.

Quoted 8 lines
Quoted 3 lines

In this patchset the kernel, initrd and cmdline blobs are passed to the guest firmware to process & load. This mode is activated when both -kernel and -pflash unit#0 (or -bios) are specified.

...so we avoid back-compat problems on the assumption that nobody was previously trying to provide both -bios and -kernel at once. I *think* that's probably a reasonable assumption...

I'm including these three too in the new patchset that I'll post soon, so no need to keep these queued.

Thanks
Laszlo