qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] hw/loongarch/boot: Support Linux raw boot image
@ 2025-01-02 22:47 Jiaxun Yang
  2025-01-02 22:47 ` [PATCH v3 1/2] hw/core/loader: Use ssize_t for efi zboot unpacker Jiaxun Yang
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jiaxun Yang @ 2025-01-02 22:47 UTC (permalink / raw)
  To: qemu-devel
  Cc: Song Gao, Bibo Mao, Richard Henderson, Peter Maydell, qemu-arm,
	Jiaxun Yang

Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
---
Changes in v3:
- Added PATCH 1 (Richard)
- Link to v2: https://lore.kernel.org/r/20241224-la-direct-kernel-boot-v2-1-3e8336c54c60@flygoat.com

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/20241223-la-direct-kernel-boot-v1-1-a79995d8b15e@flygoat.com

---
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,
-- 
Jiaxun Yang <jiaxun.yang@flygoat.com>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/2] hw/core/loader: Use ssize_t for efi zboot unpacker
  2025-01-02 22:47 [PATCH v3 0/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
@ 2025-01-02 22:47 ` Jiaxun Yang
  2025-01-02 22:47 ` [PATCH v3 2/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
  2025-01-08  2:08 ` [PATCH v3 0/2] " bibo mao
  2 siblings, 0 replies; 5+ messages in thread
From: Jiaxun Yang @ 2025-01-02 22:47 UTC (permalink / raw)
  To: qemu-devel
  Cc: Song Gao, Bibo Mao, Richard Henderson, Peter Maydell, qemu-arm,
	Jiaxun Yang

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

Suggested-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
---
 hw/arm/boot.c       | 2 +-
 hw/core/loader.c    | 4 ++--
 include/hw/loader.h | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index 5301d8d318cabae63be5f58c3179b49b187f2512..d780c36c47378aebf4e7484c895085e25dd640e6 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -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,
diff --git a/hw/core/loader.c b/hw/core/loader.c
index 31593a117171a30dec68c2b8141ba99834e5363d..11b5813f4664f0a825147bb637a0b9fef21625e4 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -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 */
diff --git a/include/hw/loader.h b/include/hw/loader.h
index 7f6d06b956fa9291e89a1d8b4e0ac44355dc12eb..8985046be40fc43741369a8431cd8e3590d1d72a 100644
--- a/include/hw/loader.h
+++ b/include/hw/loader.h
@@ -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

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/2] hw/loongarch/boot: Support Linux raw boot image
  2025-01-02 22:47 [PATCH v3 0/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
  2025-01-02 22:47 ` [PATCH v3 1/2] hw/core/loader: Use ssize_t for efi zboot unpacker Jiaxun Yang
@ 2025-01-02 22:47 ` Jiaxun Yang
  2025-01-03  1:32   ` bibo mao
  2025-01-08  2:08 ` [PATCH v3 0/2] " bibo mao
  2 siblings, 1 reply; 5+ messages in thread
From: Jiaxun Yang @ 2025-01-02 22:47 UTC (permalink / raw)
  To: qemu-devel
  Cc: Song Gao, Bibo Mao, Richard Henderson, Peter Maydell, qemu-arm,
	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

Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
---
 hw/loongarch/boot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/hw/loongarch/boot.c b/hw/loongarch/boot.c
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..38c19bffa3475d61ffab27a26af9c4c821769dca 100644
--- a/hw/loongarch/boot.c
+++ b/hw/loongarch/boot.c
@@ -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",

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 2/2] hw/loongarch/boot: Support Linux raw boot image
  2025-01-02 22:47 ` [PATCH v3 2/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
@ 2025-01-03  1:32   ` bibo mao
  0 siblings, 0 replies; 5+ messages in thread
From: bibo mao @ 2025-01-03  1:32 UTC (permalink / raw)
  To: Jiaxun Yang, qemu-devel
  Cc: Song Gao, Richard Henderson, Peter Maydell, qemu-arm



On 2025/1/3 上午6:47, Jiaxun Yang wrote:
> 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
> 
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
>   hw/loongarch/boot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 69 insertions(+)
> 
> diff --git a/hw/loongarch/boot.c b/hw/loongarch/boot.c
> index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..38c19bffa3475d61ffab27a26af9c4c821769dca 100644
> --- a/hw/loongarch/boot.c
> +++ b/hw/loongarch/boot.c
> @@ -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",
> 
Good job, and thanks for doing this.

Reviewed-by: Bibo Mao <maobibo@loongson.cn>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 0/2] hw/loongarch/boot: Support Linux raw boot image
  2025-01-02 22:47 [PATCH v3 0/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
  2025-01-02 22:47 ` [PATCH v3 1/2] hw/core/loader: Use ssize_t for efi zboot unpacker Jiaxun Yang
  2025-01-02 22:47 ` [PATCH v3 2/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
@ 2025-01-08  2:08 ` bibo mao
  2 siblings, 0 replies; 5+ messages in thread
From: bibo mao @ 2025-01-08  2:08 UTC (permalink / raw)
  To: Jiaxun Yang, qemu-devel
  Cc: Song Gao, Richard Henderson, Peter Maydell, qemu-arm



On 2025/1/3 上午6:47, Jiaxun Yang wrote:
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
> Changes in v3:
> - Added PATCH 1 (Richard)
> - Link to v2: https://lore.kernel.org/r/20241224-la-direct-kernel-boot-v2-1-3e8336c54c60@flygoat.com
> 
> 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/20241223-la-direct-kernel-boot-v1-1-a79995d8b15e@flygoat.com
> 
> ---
> 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,
> 
For this series, applied to loongarch-next

Regards
Bibo Mao



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-01-08  2:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-02 22:47 [PATCH v3 0/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
2025-01-02 22:47 ` [PATCH v3 1/2] hw/core/loader: Use ssize_t for efi zboot unpacker Jiaxun Yang
2025-01-02 22:47 ` [PATCH v3 2/2] hw/loongarch/boot: Support Linux raw boot image Jiaxun Yang
2025-01-03  1:32   ` bibo mao
2025-01-08  2:08 ` [PATCH v3 0/2] " bibo mao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).