Skip to contentSkip to conversation

hw/loongarch/boot: Support Linux raw boot image

PATCH In review Jiaxun Yang 2 participants 5 messages Jan 2, 2025 – Jan 8, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover hw/loongarch/boot: Support Linux raw boot image

---
Changes in v3:
- Added PATCH 1 (Richard)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Use extract API for getting bit fields (philmd)
- Mimic arm's load_aarch64_image to handle vmlinuz.efi
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jiaxun Yang (2):
hw/core/loader: Use ssize_t for efi zboot unpacker hw/loongarch/boot: Support Linux raw boot image

 hw/arm/boot.c       |  2 +-
 hw/core/loader.c    |  4 ++--
 hw/loongarch/boot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h |  2 +-
 4 files changed, 73 insertions(+), 4 deletions(-)

---
base-commit: c69612063e1844b76ac01e3a781b979548c3585c
change-id: 20241222-la-direct-kernel-boot-c598264710e7

Best regards,

Signature · 1 line
Jiaxun Yang <[email protected]>
#1 lore raw

PATCH v3 1/2 hw/core/loader: Use ssize_t for efi zboot unpacker

Convert to use sszie_t to represent size internally to avoid large image overflowing the size.

---
 hw/arm/boot.c       | 2 +-
 hw/core/loader.c    | 4 ++--
 include/hw/loader.h | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
hw/arm/boot.c +1 -1
index 5301d8d318cabae63be5f58c3179b49b187f2512..d780c36c47378aebf4e7484c895085e25dd640e6 100644
@@ -857,7 +857,7 @@ static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
     hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR;
     uint64_t kernel_size = 0;
     uint8_t *buffer;
-    int size;
+    ssize_t size;
 
     /* On aarch64, it's the bootloader's job to uncompress the kernel. */
     size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES,
hw/core/loader.c +2 -2
index 31593a117171a30dec68c2b8141ba99834e5363d..11b5813f4664f0a825147bb637a0b9fef21625e4 100644
@@ -886,11 +886,11 @@ struct linux_efi_zboot_header {
  *
  * If the image is not a Linux EFI zboot image, do nothing and return success.
  */
-ssize_t unpack_efi_zboot_image(uint8_t **buffer, int *size)
+ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size)
 {
     const struct linux_efi_zboot_header *header;
     uint8_t *data = NULL;
-    int ploff, plsize;
+    ssize_t ploff, plsize;
     ssize_t bytes;
 
     /* ignore if this is too small to be a EFI zboot image */
include/hw/loader.h +1 -1
index 7f6d06b956fa9291e89a1d8b4e0ac44355dc12eb..8985046be40fc43741369a8431cd8e3590d1d72a 100644
@@ -101,7 +101,7 @@ ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
  * Returns the size of the decompressed payload if decompression was performed
  * successfully.
  */
-ssize_t unpack_efi_zboot_image(uint8_t **buffer, int *size);
+ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size);
 
 #define ELF_LOAD_FAILED       -1
 #define ELF_LOAD_NOT_ELF      -2
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/2 hw/loongarch/boot: Support Linux raw boot image

In reply to Jiaxun Yang

Support booting such image by parsing header as per Linux's specification [1].

This enabled booting vmlinux.efi/vmlinuz.efi shipped by distros without supplying BIOS.

[1]: https://docs.kernel.org/arch/loongarch/booting.html

---
 hw/loongarch/boot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
hw/loongarch/boot.c +69 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..38c19bffa3475d61ffab27a26af9c4c821769dca 100644
@@ -16,6 +16,26 @@
 #include "sysemu/reset.h"
 #include "sysemu/qtest.h"
 
+/*
+ * Linux Image Format
+ * https://docs.kernel.org/arch/loongarch/booting.html
+ */
+#define LINUX_PE_MAGIC  0x818223cd
+#define MZ_MAGIC        0x5a4d /* "MZ" */
+
+struct loongarch_linux_hdr {
+    uint32_t mz_magic;
+    uint32_t res0;
+    uint64_t kernel_entry;
+    uint64_t kernel_size;
+    uint64_t load_offset;
+    uint64_t res1;
+    uint64_t res2;
+    uint64_t res3;
+    uint32_t linux_pe_magic;
+    uint32_t pe_header_offset;
+} QEMU_PACKED;
+
 struct memmap_entry *memmap_table;
 unsigned memmap_entries;
 
@@ -260,6 +280,50 @@ static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
     return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
 }
 
+static int64_t load_loongarch_linux_image(const char *filename,
+                                          uint64_t *kernel_entry,
+                                          uint64_t *kernel_low,
+                                          uint64_t *kernel_high)
+{
+    gsize len;
+    ssize_t size;
+    uint8_t *buffer;
+    struct loongarch_linux_hdr *hdr;
+
+    /* Load as raw file otherwise */
+    if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
+        return -1;
+    }
+    size = len;
+
+    /* Unpack the image if it is a EFI zboot image */
+    if (unpack_efi_zboot_image(&buffer, &size) < 0) {
+        g_free(buffer);
+        return -1;
+    }
+
+    hdr = (struct loongarch_linux_hdr *)buffer;
+
+    if (extract32(le32_to_cpu(hdr->mz_magic), 0, 16) != MZ_MAGIC ||
+        le32_to_cpu(hdr->linux_pe_magic) != LINUX_PE_MAGIC) {
+        g_free(buffer);
+        return -1;
+    }
+
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry = extract64(le64_to_cpu(hdr->kernel_entry),
+                              0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = extract64(le64_to_cpu(hdr->load_offset),
+                            0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_high = *kernel_low + size;
+
+    rom_add_blob_fixed(filename, buffer, size, *kernel_low);
+
+    g_free(buffer);
+
+    return size;
+}
+
 static int64_t load_kernel_info(struct loongarch_boot_info *info)
 {
     uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +334,11 @@ static int64_t load_kernel_info(struct loongarch_boot_info *info)
                            &kernel_entry, &kernel_low,
                            &kernel_high, NULL, 0,
                            EM_LOONGARCH, 1, 0);
+    if (kernel_size < 0) {
+        kernel_size = load_loongarch_linux_image(info->kernel_filename,
+                                                 &kernel_entry, &kernel_low,
+                                                 &kernel_high);
+    }
 
     if (kernel_size < 0) {
         error_report("could not load kernel '%s': %s",
Signature · 1 line
2.43.0
Reviewed-by on v3 2/2
#3 lore raw
Quoted 98 lines · Jiaxun Yang

Support booting such image by parsing header as per Linux's specification [1].

This enabled booting vmlinux.efi/vmlinuz.efi shipped by distros without supplying BIOS.

[1]: https://docs.kernel.org/arch/loongarch/booting.html

---
  hw/loongarch/boot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 69 insertions(+)
hw/loongarch/boot.c +64 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..38c19bffa3475d61ffab27a26af9c4c821769dca 100644
@@ -16,6 +16,26 @@
  #include "sysemu/reset.h"
  #include "sysemu/qtest.h"
 
+/*
+ * Linux Image Format
+ * https://docs.kernel.org/arch/loongarch/booting.html
+ */
+#define LINUX_PE_MAGIC  0x818223cd
+#define MZ_MAGIC        0x5a4d /* "MZ" */
+
+struct loongarch_linux_hdr {
+    uint32_t mz_magic;
+    uint32_t res0;
+    uint64_t kernel_entry;
+    uint64_t kernel_size;
+    uint64_t load_offset;
+    uint64_t res1;
+    uint64_t res2;
+    uint64_t res3;
+    uint32_t linux_pe_magic;
+    uint32_t pe_header_offset;
+} QEMU_PACKED;
+
  struct memmap_entry *memmap_table;
  unsigned memmap_entries;
 
@@ -260,6 +280,50 @@ static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
      return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
  }
 
+static int64_t load_loongarch_linux_image(const char *filename,
+                                          uint64_t *kernel_entry,
+                                          uint64_t *kernel_low,
+                                          uint64_t *kernel_high)
+{
+    gsize len;
+    ssize_t size;
+    uint8_t *buffer;
+    struct loongarch_linux_hdr *hdr;
+
+    /* Load as raw file otherwise */
+    if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
+        return -1;
+    }
+    size = len;
+
+    /* Unpack the image if it is a EFI zboot image */
+    if (unpack_efi_zboot_image(&buffer, &size) < 0) {
+        g_free(buffer);
+        return -1;
+    }
+
+    hdr = (struct loongarch_linux_hdr *)buffer;
+
+    if (extract32(le32_to_cpu(hdr->mz_magic), 0, 16) != MZ_MAGIC ||
+        le32_to_cpu(hdr->linux_pe_magic) != LINUX_PE_MAGIC) {
+        g_free(buffer);
+        return -1;
+    }
+
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry = extract64(le64_to_cpu(hdr->kernel_entry),
+                              0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = extract64(le64_to_cpu(hdr->load_offset),
+                            0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_high = *kernel_low + size;
+
+    rom_add_blob_fixed(filename, buffer, size, *kernel_low);
+
+    g_free(buffer);
+
+    return size;
+}
+
  static int64_t load_kernel_info(struct loongarch_boot_info *info)
  {
      uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +334,11 @@ static int64_t load_kernel_info(struct loongarch_boot_info *info)
                             &kernel_entry, &kernel_low,

hw/loongarch/boot.c static int64_t load_kernel_info(struct loongarch_boot_info *info) · line 344

Quoted 10 lines
hw/loongarch/boot.c +5 -0
                             &kernel_high, NULL, 0,
                             EM_LOONGARCH, 1, 0);
+    if (kernel_size < 0) {
+        kernel_size = load_loongarch_linux_image(info->kernel_filename,
+                                                 &kernel_entry, &kernel_low,
+                                                 &kernel_high);
+    }
 
      if (kernel_size < 0) {
          error_report("could not load kernel '%s': %s",

Good job, and thanks for doing this.

#4 lore raw
In reply to Jiaxun Yang
Quoted 21 lines · Jiaxun Yang

---
Changes in v3:
- Added PATCH 1 (Richard)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Use extract API for getting bit fields (philmd)
- Mimic arm's load_aarch64_image to handle vmlinuz.efi
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jiaxun Yang (2):
hw/core/loader: Use ssize_t for efi zboot unpacker hw/loongarch/boot: Support Linux raw boot image

  hw/arm/boot.c       |  2 +-
  hw/core/loader.c    |  4 ++--
  hw/loongarch/boot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  include/hw/loader.h |  2 +-
  4 files changed, 73 insertions(+), 4 deletions(-)
Quoted 6 lines

---
base-commit: c69612063e1844b76ac01e3a781b979548c3585c
change-id: 20241222-la-direct-kernel-boot-c598264710e7

Best regards,

For this series, applied to loongarch-next

Regards
Bibo Mao