Skip to contentSkip to conversation

hw/loongarch/boot: Support Linux raw boot image

PATCH Superseded Jiaxun Yang 2 participants 8 messages Dec 23, 2024 – Dec 24, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/loongarch/boot: Support Linux raw boot image

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
 hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loongarch/boot.h | 17 +++++++++++++++++
 2 files changed, 62 insertions(+)
hw/loongarch/boot.c +45 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
 static int64_t load_kernel_info(struct loongarch_boot_info *info)
 {
     uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }
+    }
 
     if (kernel_size < 0) {
         error_report("could not load kernel '%s': %s",
include/hw/loongarch/boot.h +17 -0
index 96ec15016a314499acf65c6c47e0c4932aa99d01..5e8bd4dd73bbb27abccfa1fa577df52aed15b6a2 100644
@@ -8,6 +8,23 @@
 #ifndef HW_LOONGARCH_BOOT_H
 #define HW_LOONGARCH_BOOT_H
 
+/* Linux Image Format */
+#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;
+
 /* UEFI 2.10 */
 #define EFI_SYSTEM_TABLE_SIGNATURE       0x5453595320494249
 #define EFI_2_100_SYSTEM_TABLE_REVISION  ((2<<16) | (100))

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

Best regards,

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

Sorry, I do not know the background.
Now kernel image with EFI format can boot if uefi bios is provided.

What is this patch to do? is it to direct boot kernel with EFI format without UEFI bios parameter?

Regards
Bibo Mao

Quoted 113 lines · Jiaxun Yang

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
  hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
  include/hw/loongarch/boot.h | 17 +++++++++++++++++
  2 files changed, 62 insertions(+)
hw/loongarch/boot.c +45 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
  static int64_t load_kernel_info(struct loongarch_boot_info *info)
  {
      uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }
+    }
 
      if (kernel_size < 0) {
          error_report("could not load kernel '%s': %s",
include/hw/loongarch/boot.h +17 -1
index 96ec15016a314499acf65c6c47e0c4932aa99d01..5e8bd4dd73bbb27abccfa1fa577df52aed15b6a2 100644
@@ -8,6 +8,23 @@
  #ifndef HW_LOONGARCH_BOOT_H
  #define HW_LOONGARCH_BOOT_H
 
+/* Linux Image Format */
+#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;
+
  /* UEFI 2.10 */
  #define EFI_SYSTEM_TABLE_SIGNATURE       0x5453595320494249
  #define EFI_2_100_SYSTEM_TABLE_REVISION  ((2<<16) | (100))
 
---

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

Best regards,

#2 lore raw
Quoted 5 lines · 在2024年12月24日十二月 上午12:56,bibo mao写道:

Sorry, I do not know the background.
Now kernel image with EFI format can boot if uefi bios is provided.

What is this patch to do? is it to direct boot kernel with EFI format without UEFI bios parameter?

Yes, it’s now capable for booting vmlinux.efi without BIOS, as well as raw kernel built without EFI STUB.

Thanks

Quoted 118 lines

Regards
Bibo Mao

Quoted 113 lines · Jiaxun Yang

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
  hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
  include/hw/loongarch/boot.h | 17 +++++++++++++++++
  2 files changed, 62 insertions(+)
hw/loongarch/boot.c +45 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
  static int64_t load_kernel_info(struct loongarch_boot_info *info)
  {
      uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }
+    }
 
      if (kernel_size < 0) {
          error_report("could not load kernel '%s': %s",
include/hw/loongarch/boot.h +17 -1
index 96ec15016a314499acf65c6c47e0c4932aa99d01..5e8bd4dd73bbb27abccfa1fa577df52aed15b6a2 100644
@@ -8,6 +8,23 @@
  #ifndef HW_LOONGARCH_BOOT_H
  #define HW_LOONGARCH_BOOT_H
 
+/* Linux Image Format */
+#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;
+
  /* UEFI 2.10 */
  #define EFI_SYSTEM_TABLE_SIGNATURE       0x5453595320494249
  #define EFI_2_100_SYSTEM_TABLE_REVISION  ((2<<16) | (100))
 
---

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

Best regards,

Signature · 1 line
- Jiaxun
#3 lore raw
Quoted 10 lines · Jiaxun Yang
Quoted 5 lines · 在2024年12月24日十二月 上午12:56,bibo mao写道:

Sorry, I do not know the background.
Now kernel image with EFI format can boot if uefi bios is provided.

What is this patch to do? is it to direct boot kernel with EFI format without UEFI bios parameter?

Yes, it’s now capable for booting vmlinux.efi without BIOS, as well as raw kernel built without EFI STUB.

Is efi boottime service used by vmlinux.efi? such as
handle_protocol/allocate_pages etc.

Regards
Bibo Mao

Quoted 122 lines

Thanks

Quoted 118 lines

Regards
Bibo Mao

Quoted 113 lines · Jiaxun Yang

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
   hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
   include/hw/loongarch/boot.h | 17 +++++++++++++++++
   2 files changed, 62 insertions(+)
hw/loongarch/boot.c +45 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
   static int64_t load_kernel_info(struct loongarch_boot_info *info)
   {
       uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }
+    }
 
       if (kernel_size < 0) {
           error_report("could not load kernel '%s': %s",
include/hw/loongarch/boot.h +17 -1
index 96ec15016a314499acf65c6c47e0c4932aa99d01..5e8bd4dd73bbb27abccfa1fa577df52aed15b6a2 100644
@@ -8,6 +8,23 @@
   #ifndef HW_LOONGARCH_BOOT_H
   #define HW_LOONGARCH_BOOT_H
 
+/* Linux Image Format */
+#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;
+
   /* UEFI 2.10 */
   #define EFI_SYSTEM_TABLE_SIGNATURE       0x5453595320494249
   #define EFI_2_100_SYSTEM_TABLE_REVISION  ((2<<16) | (100))
 
---

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

Best regards,

#4 lore raw
Quoted 7 lines · 在2024年12月24日十二月 上午1:25,bibo mao写道:
Quoted 6 lines · Jiaxun Yang
Quoted 3 lines · 在2024年12月24日十二月 上午12:56,bibo mao写道:

Sorry, I do not know the background.
Now kernel image with EFI format can boot if uefi bios is provided.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

What is this patch to do? is it to direct boot kernel with EFI format without UEFI bios parameter?

Yes, it’s now capable for booting vmlinux.efi without BIOS, as well as raw kernel built without EFI STUB.

Is efi boottime service used by vmlinux.efi? such as
handle_protocol/allocate_pages etc.

Short answer: no.

To explain, we are jumping directly to kernel’s
entry instead of EFISTUB entry.

So it basically it works in the same manner as ELF booting, kernel won’t call EFI service if non-EFI booting environment is detected via a0 argument.

Thanks

Quoted 125 lines

Regards
Bibo Mao

Quoted 122 lines

Thanks

Quoted 118 lines

Regards
Bibo Mao

Quoted 113 lines · Jiaxun Yang

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
   hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
   include/hw/loongarch/boot.h | 17 +++++++++++++++++
   2 files changed, 62 insertions(+)
hw/loongarch/boot.c +45 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
   static int64_t load_kernel_info(struct loongarch_boot_info *info)
   {
       uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }
+    }
 
       if (kernel_size < 0) {
           error_report("could not load kernel '%s': %s",
include/hw/loongarch/boot.h +17 -1
index 96ec15016a314499acf65c6c47e0c4932aa99d01..5e8bd4dd73bbb27abccfa1fa577df52aed15b6a2 100644
@@ -8,6 +8,23 @@
   #ifndef HW_LOONGARCH_BOOT_H
   #define HW_LOONGARCH_BOOT_H
 
+/* Linux Image Format */
+#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;
+
   /* UEFI 2.10 */
   #define EFI_SYSTEM_TABLE_SIGNATURE       0x5453595320494249
   #define EFI_2_100_SYSTEM_TABLE_REVISION  ((2<<16) | (100))
 
---

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

Best regards,

Signature · 1 line
- Jiaxun
#5 lore raw
Quoted 17 lines · Jiaxun Yang
Quoted 7 lines · 在2024年12月24日十二月 上午1:25,bibo mao写道:
Quoted 6 lines · Jiaxun Yang
Quoted 3 lines · 在2024年12月24日十二月 上午12:56,bibo mao写道:

Sorry, I do not know the background.
Now kernel image with EFI format can boot if uefi bios is provided.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

What is this patch to do? is it to direct boot kernel with EFI format without UEFI bios parameter?

Yes, it’s now capable for booting vmlinux.efi without BIOS, as well as raw kernel built without EFI STUB.

Is efi boottime service used by vmlinux.efi? such as
handle_protocol/allocate_pages etc.

Quoted 6 lines

Short answer: no.

To explain, we are jumping directly to kernel’s
entry instead of EFISTUB entry.

So it basically it works in the same manner as ELF booting, kernel won’t call EFI service if non-EFI booting environment is detected via a0 argument.

That is wonderful, please add this in the note.

Regards
Bibo Mao

Quoted 128 lines

Thanks

Quoted 125 lines

Regards
Bibo Mao

Quoted 122 lines

Thanks

Quoted 118 lines

Regards
Bibo Mao

Quoted 113 lines · Jiaxun Yang

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
    hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
    include/hw/loongarch/boot.h | 17 +++++++++++++++++
    2 files changed, 62 insertions(+)
hw/loongarch/boot.c +45 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
    static int64_t load_kernel_info(struct loongarch_boot_info *info)
    {
        uint64_t kernel_entry, kernel_low, kernel_high;
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }
+    }
 
        if (kernel_size < 0) {
            error_report("could not load kernel '%s': %s",
include/hw/loongarch/boot.h +17 -1
index 96ec15016a314499acf65c6c47e0c4932aa99d01..5e8bd4dd73bbb27abccfa1fa577df52aed15b6a2 100644
@@ -8,6 +8,23 @@
    #ifndef HW_LOONGARCH_BOOT_H
    #define HW_LOONGARCH_BOOT_H
 
+/* Linux Image Format */
+#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;
+
    /* UEFI 2.10 */
    #define EFI_SYSTEM_TABLE_SIGNATURE       0x5453595320494249
    #define EFI_2_100_SYSTEM_TABLE_REVISION  ((2<<16) | (100))
 
---

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

Best regards,

#6 lore raw
In reply to Jiaxun Yang
Quoted 64 lines · Jiaxun Yang

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
  hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
  include/hw/loongarch/boot.h | 17 +++++++++++++++++
  2 files changed, 62 insertions(+)
hw/loongarch/boot.c +37 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
  static int64_t load_kernel_info(struct loongarch_boot_info *info)
  {
      uint64_t kernel_entry, kernel_low, kernel_high;

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

Quoted 10 lines
hw/loongarch/boot.c +7 -0
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }

Can we read file header file firstly?
If it is ELFMAG, load_elf is called.

If it is EFI_PE_MSDOS_MAGIC, check zimg magic in linux_efi_zboot_header, else loongarch_linux_hdr for plain image if zimg does not match.

Regards
Bibo Mao

Quoted 39 lines
hw/loongarch/boot.c +1 -0
+    }
 
      if (kernel_size < 0) {
          error_report("could not load kernel '%s': %s",
include/hw/loongarch/boot.h +17 -1
index 96ec15016a314499acf65c6c47e0c4932aa99d01..5e8bd4dd73bbb27abccfa1fa577df52aed15b6a2 100644
@@ -8,6 +8,23 @@
  #ifndef HW_LOONGARCH_BOOT_H
  #define HW_LOONGARCH_BOOT_H
 
+/* Linux Image Format */
+#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;
+
  /* UEFI 2.10 */
  #define EFI_SYSTEM_TABLE_SIGNATURE       0x5453595320494249
  #define EFI_2_100_SYSTEM_TABLE_REVISION  ((2<<16) | (100))
 
---

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

Best regards,

#7 lore raw
Quoted 78 lines · 在2024年12月24日十二月 上午4:03,bibo mao写道:
Quoted 64 lines · Jiaxun Yang

Many distros are shipping raw kernel images (i.e. vmlinux.efi).

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

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

---
It is based on my previous booting protocol patch

---
  hw/loongarch/boot.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
  include/hw/loongarch/boot.h | 17 +++++++++++++++++
  2 files changed, 62 insertions(+)
hw/loongarch/boot.c +37 -0
index 93847b0eaf8e50ce1a990b91267780e6785e1c2f..5bc889c51fafa9c6d37426b9bee9709c12183927 100644
@@ -260,6 +260,43 @@ 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 get_linux_image_info(struct loongarch_boot_info *info,
+                                    uint64_t *kernel_entry,
+                                    uint64_t *kernel_low,
+                                    uint64_t *kernel_high)
+{
+    int fd;
+    struct loongarch_linux_hdr hdr;
+    int64_t kernel_size = -1;
+
+    fd = open(info->kernel_filename, O_RDONLY | O_BINARY);
+    if (fd < 0) {
+        return -1;
+    }
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        close(fd);
+        return -1;
+    }
+
+    if ((le32_to_cpu(hdr.mz_magic) & 0xffff) != MZ_MAGIC ||
+        le32_to_cpu(hdr.linux_pe_magic) != LINUX_PE_MAGIC) {
+        close(fd);
+        return -1;
+    }
+
+    *kernel_entry = le64_to_cpu(hdr.kernel_entry);
+    /* Early kernel versions may have those fields in virtual address */
+    *kernel_entry &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    *kernel_low = le64_to_cpu(hdr.load_offset);
+    *kernel_low &= MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
+    kernel_size = le64_to_cpu(hdr.kernel_size);
+    *kernel_high = *kernel_low + kernel_size;
+
+    close(fd);
+    return kernel_size;
+}
+
  static int64_t load_kernel_info(struct loongarch_boot_info *info)
  {
      uint64_t kernel_entry, kernel_low, kernel_high;

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

Quoted 10 lines
hw/loongarch/boot.c +7 -0
@@ -270,6 +307,14 @@ 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 = get_linux_image_info(info, &kernel_entry,
+                                           &kernel_low, &kernel_high);
+        if (kernel_size >= 0) {
+            kernel_size = load_image_targphys(info->kernel_filename,
+                                              kernel_low, kernel_size);
+        }

Can we read file header file firstly?
If it is ELFMAG, load_elf is called.

Quoted 2 lines

If it is EFI_PE_MSDOS_MAGIC, check zimg magic in linux_efi_zboot_header, else loongarch_linux_hdr for plain image if zimg does not match.

I'd say as checking will be done by load_elf, the fallback path makes more sense here.

See similar design on Arm's arm_setup_direct_kernel_boot.

But you are right, I should handle vmlinuz.efi with linux_efi_zboot_header as well.

Quoted 5 lines

Regards
Bibo Mao

Quoted 1 line
hw/loongarch/boot.c +1 -0
+    }

[...]

Signature · 1 line
- Jiaxun