hw/loongarch/boot: Support Linux raw boot image
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]>
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
@@ -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
@@ -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
@@ -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
PATCH v3 2/2 hw/loongarch/boot: Support Linux raw boot image
In reply to Jiaxun YangSupport 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
@@ -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
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
Signed-off-by Jiaxun Yang <[email protected]>--- hw/loongarch/boot.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)hw/loongarch/boot.c +64
-0@@ -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.
Quoted 21 lines · Jiaxun Yang
Signed-off-by Jiaxun Yang <[email protected]>---
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'sload_aarch64_imageto handle vmlinuz.efi
- Link to v1: https://lore.kernel.org/r/[email protected]---
Jiaxun Yang (2):
hw/core/loader: Usessize_tfor efi zboot unpacker hw/loongarch/boot: Support Linux raw boot imagehw/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-c598264710e7Best regards,
For this series, applied to loongarch-next
Regards
Bibo Mao