Skip to contentSkip to conversation

kexec: Add support for UKI format kernel

PATCH Superseded Pingfan Liu 2 participants 9 messages Oct 8, 2024 – Oct 16, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover kexec: Add support for UKI format kernel

As a UEFI PE format kernel image becomes more popular, there is a need for kexec to reboot those kinds of images.

After the introduction of the UKI (another PE), at present, there are three competitive methods to support that goal, but all of them have pros and cons.[1] It seems that none of them can be accepted in the near future. Therefore, we are resorting to the user space kexec-tools to parse the UKI format for the time being.

By parsing the UKI, systemd-stub is stepped around and PCM will not affect the boot up of the second system.

[1]: https://github.com/rhkdump/kexec_uefi/blob/main/overview.md#the-competitive-solutions

Cc Simon Horman <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Baoquan He <[email protected]>
Cc Dave Young <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Jan Hendrik Farr <[email protected]>
Cc Philipp Rudo <[email protected]>
Cc Lennart Poettering <[email protected]>

Pingfan Liu (3):
kexec: Introduce default_initrd_fd to pass internal initrd information
kexec: Introduce UKI image parser
arm64: Support UKI image format

 include/pe.h                         | 104 +++++++++++++++++++++++
 kexec/Makefile                       |   1 +
 kexec/arch/arm64/kexec-arm64.c       |   1 +
 kexec/arch/arm64/kexec-image-arm64.c |   3 +-
 kexec/arch/x86_64/kexec-bzImage64.c  |   3 +-
 kexec/kexec-uki.c                    | 122 +++++++++++++++++++++++++++
 kexec/kexec.c                        |   2 +
 kexec/kexec.h                        |   5 ++
 8 files changed, 239 insertions(+), 2 deletions(-)
 create mode 100644 include/pe.h
 create mode 100644 kexec/kexec-uki.c
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

PATCH 1/3 kexec: Introduce default_initrd_fd to pass internal initrd information

At present, initrd information is passed through --initrd args, but for the UKI case, the initrd may be stored in the .initrd section.

It means that initrd is perceived at the probe stage. And there should be a way to carry this information forward to the load stage. In oder to implement that, introducing a global default_initrd_fd to do it.

Cc Simon Horman <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Baoquan He <[email protected]>
Cc Dave Young <[email protected]>
---
 kexec/arch/arm64/kexec-image-arm64.c | 3 ++-
 kexec/arch/x86_64/kexec-bzImage64.c  | 3 ++-
 kexec/kexec.c                        | 2 ++
 kexec/kexec.h                        | 1 +
 4 files changed, 7 insertions(+), 2 deletions(-)
kexec/arch/arm64/kexec-image-arm64.c +2 -1
index a196747..188e7f6 100644
@@ -50,7 +50,8 @@ int image_arm64_load(int argc, char **argv, const char *kernel_buf,
 				result = EFAILED;
 				goto exit;
 			}
-		}
+		} else if (default_initrd_fd != -1)
+			info->initrd_fd = default_initrd_fd;
 
 		if (arm64_opts.command_line) {
 			info->command_line = (char *)arm64_opts.command_line;
kexec/arch/x86_64/kexec-bzImage64.c +2 -1
index aba4e3b..e337dc0 100644
@@ -307,7 +307,8 @@ int bzImage64_load_file(int argc, char **argv, struct kexec_info *info)
 			ret = -1;
 			goto out;
 		}
-	}
+	} else if (default_initrd_fd != -1)
+		info->initrd_fd = default_initrd_fd;
 
 	info->command_line = command_line;
 	info->command_line_len = command_line_len;
kexec/kexec.c +2 -0
index 7c614b0..91edb56 100644
@@ -67,6 +67,8 @@ int do_hotplug = 0;
 static unsigned long kexec_flags = 0;
 /* Flags for kexec file (fd) based syscall */
 static unsigned long kexec_file_flags = 0;
+/* initrd detected in probe phase */
+int default_initrd_fd = -1;
 int kexec_debug = 0;
 
 void dbgprint_mem_range(const char *prefix, struct memory_range *mr, int nr_mr)
kexec/kexec.h +1 -0
index 31c323f..396687d 100644
@@ -114,6 +114,7 @@ do { \
 #define _ALIGN(addr, size)     _ALIGN_UP(addr, size)
 
 extern unsigned long long mem_min, mem_max;
+extern int default_initrd_fd;
 extern int kexec_debug;
 
 #define dbgprintf(...) \
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw

PATCH 2/3 kexec: Introduce UKI image parser

In reply to Pingfan Liu

A UKI image is a PE file that consists of several sections, typically
including: .text, .data, .linux, .initrd, .cmdline, and others.

The kernel image is stored in the .linux section, which is one of the formats currently recognized by kexec-tools. Therefore, the UKI parser can be used to strip away the UKI layer, allowing the other parser to process the kernel image.

Cc Simon Horman <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Baoquan He <[email protected]>
Cc Dave Young <[email protected]>
---
 include/pe.h      | 104 +++++++++++++++++++++++++++++++++++++++
 kexec/Makefile    |   1 +
 kexec/kexec-uki.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++
 kexec/kexec.h     |   4 ++
 4 files changed, 231 insertions(+)
 create mode 100644 include/pe.h
 create mode 100644 kexec/kexec-uki.c
include/pe.h new file +104 -0
new file mode 100644
index 0000000..2617074
@@ -0,0 +1,104 @@
+/*
+ * Extract from linux kernel include/linux/pe.h
+ */
+
+#ifndef __PE_H__
+#define __PE_H__
+
+struct pe_hdr {
+	uint32_t magic;		/* PE magic */
+	uint16_t machine;	/* machine type */
+	uint16_t sections;	/* number of sections */
+	uint32_t timestamp;	/* time_t */
+	uint32_t symbol_table;	/* symbol table offset */
+	uint32_t symbols;	/* number of symbols */
+	uint16_t opt_hdr_size;	/* size of optional header */
+	uint16_t flags;		/* flags */
+};
+
+/* the fact that pe32 isn't padded where pe32+ is 64-bit means union won't
+ * work right.  vomit. */
+struct pe32_opt_hdr {
+	/* "standard" header */
+	uint16_t magic;		/* file type */
+	uint8_t  ld_major;	/* linker major version */
+	uint8_t  ld_minor;	/* linker minor version */
+	uint32_t text_size;	/* size of text section(s) */
+	uint32_t data_size;	/* size of data section(s) */
+	uint32_t bss_size;	/* size of bss section(s) */
+	uint32_t entry_point;	/* file offset of entry point */
+	uint32_t code_base;	/* relative code addr in ram */
+	uint32_t data_base;	/* relative data addr in ram */
+	/* "windows" header */
+	uint32_t image_base;	/* preferred load address */
+	uint32_t section_align;	/* alignment in bytes */
+	uint32_t file_align;	/* file alignment in bytes */
+	uint16_t os_major;	/* major OS version */
+	uint16_t os_minor;	/* minor OS version */
+	uint16_t image_major;	/* major image version */
+	uint16_t image_minor;	/* minor image version */
+	uint16_t subsys_major;	/* major subsystem version */
+	uint16_t subsys_minor;	/* minor subsystem version */
+	uint32_t win32_version;	/* reserved, must be 0 */
+	uint32_t image_size;	/* image size */
+	uint32_t header_size;	/* header size rounded up to
+				   file_align */
+	uint32_t csum;		/* checksum */
+	uint16_t subsys;	/* subsystem */
+	uint16_t dll_flags;	/* more flags! */
+	uint32_t stack_size_req;/* amt of stack requested */
+	uint32_t stack_size;	/* amt of stack required */
+	uint32_t heap_size_req;	/* amt of heap requested */
+	uint32_t heap_size;	/* amt of heap required */
+	uint32_t loader_flags;	/* reserved, must be 0 */
+	uint32_t data_dirs;	/* number of data dir entries */
+};
+
+struct pe32plus_opt_hdr {
+	uint16_t magic;		/* file type */
+	uint8_t  ld_major;	/* linker major version */
+	uint8_t  ld_minor;	/* linker minor version */
+	uint32_t text_size;	/* size of text section(s) */
+	uint32_t data_size;	/* size of data section(s) */
+	uint32_t bss_size;	/* size of bss section(s) */
+	uint32_t entry_point;	/* file offset of entry point */
+	uint32_t code_base;	/* relative code addr in ram */
+	/* "windows" header */
+	uint64_t image_base;	/* preferred load address */
+	uint32_t section_align;	/* alignment in bytes */
+	uint32_t file_align;	/* file alignment in bytes */
+	uint16_t os_major;	/* major OS version */
+	uint16_t os_minor;	/* minor OS version */
+	uint16_t image_major;	/* major image version */
+	uint16_t image_minor;	/* minor image version */
+	uint16_t subsys_major;	/* major subsystem version */
+	uint16_t subsys_minor;	/* minor subsystem version */
+	uint32_t win32_version;	/* reserved, must be 0 */
+	uint32_t image_size;	/* image size */
+	uint32_t header_size;	/* header size rounded up to
+				   file_align */
+	uint32_t csum;		/* checksum */
+	uint16_t subsys;	/* subsystem */
+	uint16_t dll_flags;	/* more flags! */
+	uint64_t stack_size_req;/* amt of stack requested */
+	uint64_t stack_size;	/* amt of stack required */
+	uint64_t heap_size_req;	/* amt of heap requested */
+	uint64_t heap_size;	/* amt of heap required */
+	uint32_t loader_flags;	/* reserved, must be 0 */
+	uint32_t data_dirs;	/* number of data dir entries */
+};
+
+struct section_header {
+	char name[8];			/* name or "/12\0" string tbl offset */
+	uint32_t virtual_size;		/* size of loaded section in ram */
+	uint32_t virtual_address;	/* relative virtual address */
+	uint32_t raw_data_size;		/* size of the section */
+	uint32_t data_addr;		/* file pointer to first page of sec */
+	uint32_t relocs;		/* file pointer to relocation entries */
+	uint32_t line_numbers;		/* line numbers! */
+	uint16_t num_relocs;		/* number of relocations */
+	uint16_t num_lin_numbers;	/* srsly. */
+	uint32_t flags;
+};
+
+#endif
kexec/Makefile +1 -0
index 11682bf..d4f26d7 100644
@@ -18,6 +18,7 @@ KEXEC_SRCS_base += kexec/kexec-elf-core.c
 KEXEC_SRCS_base += kexec/kexec-elf-rel.c
 KEXEC_SRCS_base += kexec/kexec-elf-boot.c
 KEXEC_SRCS_base += kexec/kexec-pe-zboot.c
+KEXEC_SRCS_base += kexec/kexec-uki.c
 KEXEC_SRCS_base += kexec/kexec-iomem.c
 KEXEC_SRCS_base += kexec/firmware_memmap.c
 KEXEC_SRCS_base += kexec/crashdump.c
kexec/kexec-uki.c new file +122 -0

122 lines not on this page · raw message

kexec/kexec.h +4 -0
index 396687d..c47408b 100644
@@ -357,4 +357,8 @@ static inline void ultoa(unsigned long val, char *str)
 	str[pos] = 0;
 }
 
+extern int uki_image_probe(const char *file_buf, off_t buf_sz);
+extern int uki_image_load(int argc, char **argv, const char *buf, off_t len,
+		struct kexec_info *info);
+extern void uki_image_usage(void);
 #endif /* KEXEC_H */
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#3 lore raw

Re: [PATCH 2/3] kexec: Introduce UKI image parser

Quoted 14 lines · Pingfan Liu

A UKI image is a PE file that consists of several sections, typically
including: .text, .data, .linux, .initrd, .cmdline, and others.

The kernel image is stored in the .linux section, which is one of the formats currently recognized by kexec-tools. Therefore, the UKI parser can be used to strip away the UKI layer, allowing the other parser to process the kernel image.

Cc Simon Horman <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Baoquan He <[email protected]>
Cc Dave Young <[email protected]>
Quoted 8 lines
---
 include/pe.h      | 104 +++++++++++++++++++++++++++++++++++++++
 kexec/Makefile    |   1 +
 kexec/kexec-uki.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++
 kexec/kexec.h     |   4 ++
 4 files changed, 231 insertions(+)
 create mode 100644 include/pe.h
 create mode 100644 kexec/kexec-uki.c

Hi Pingfan Liu,

I think you need to squash the following into this patch so that pe.h is included in distribution tarballs.

You should be able to see the problem by running: make distcheck.

include/Makefile +1 -0
index cd88a26b1476..6a3e8541c36d 100644
@@ -8,6 +8,7 @@ dist += include/Makefile		\
 	include/x86/mb_header.h		\
 	include/x86/multiboot2.h	\
 	include/elf.h			\
+	include/pe.h			\
 	include/image.h			\
 	include/unused.h		\
 	include/boot/linuxbios_tables.h	\

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#4 lore raw

Re: [PATCH 2/3] kexec: Introduce UKI image parser

Quoted 25 lines · Simon Horman
Quoted 14 lines · Pingfan Liu

A UKI image is a PE file that consists of several sections, typically
including: .text, .data, .linux, .initrd, .cmdline, and others.

The kernel image is stored in the .linux section, which is one of the formats currently recognized by kexec-tools. Therefore, the UKI parser can be used to strip away the UKI layer, allowing the other parser to process the kernel image.

Cc Simon Horman <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Baoquan He <[email protected]>
Cc Dave Young <[email protected]>
Quoted 8 lines
---
 include/pe.h      | 104 +++++++++++++++++++++++++++++++++++++++
 kexec/Makefile    |   1 +
 kexec/kexec-uki.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++
 kexec/kexec.h     |   4 ++
 4 files changed, 231 insertions(+)
 create mode 100644 include/pe.h
 create mode 100644 kexec/kexec-uki.c
Quoted 7 lines

Hi Pingfan Liu,

I think you need to squash the following into this patch so that pe.h is included in distribution tarballs.

You should be able to see the problem by running: make distcheck.

Oh, thanks. I will fix it in V2 and remember to do such tests in future.

Best regards,

Pingfan

include/Makefile dist += include/Makefile \ · line 14

Quoted 13 lines
include/Makefile +1 -0
index cd88a26b1476..6a3e8541c36d 100644
@@ -8,6 +8,7 @@ dist += include/Makefile                \
        include/x86/mb_header.h         \
        include/x86/multiboot2.h        \
        include/elf.h                   \
+       include/pe.h                    \
        include/image.h                 \
        include/unused.h                \
        include/boot/linuxbios_tables.h \

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#5 lore raw

Re: [PATCH 2/3] kexec: Introduce UKI image parser

Hello everyone,

I have created V2 and make a PR: https://github.com/horms/kexec-tools/pull/9

Due to utilizing the workflow in github to have some basic checks automatically , I migrated it to github instead of mailing-list.

Please share your opinions.

Thanks

Quoted 41 lines · Pingfan Liu
Quoted 25 lines · Simon Horman
Quoted 14 lines · Pingfan Liu

A UKI image is a PE file that consists of several sections, typically
including: .text, .data, .linux, .initrd, .cmdline, and others.

The kernel image is stored in the .linux section, which is one of the formats currently recognized by kexec-tools. Therefore, the UKI parser can be used to strip away the UKI layer, allowing the other parser to process the kernel image.

Cc Simon Horman <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Baoquan He <[email protected]>
Cc Dave Young <[email protected]>
Quoted 8 lines
---
 include/pe.h      | 104 +++++++++++++++++++++++++++++++++++++++
 kexec/Makefile    |   1 +
 kexec/kexec-uki.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++
 kexec/kexec.h     |   4 ++
 4 files changed, 231 insertions(+)
 create mode 100644 include/pe.h
 create mode 100644 kexec/kexec-uki.c
Quoted 7 lines

Hi Pingfan Liu,

I think you need to squash the following into this patch so that pe.h is included in distribution tarballs.

You should be able to see the problem by running: make distcheck.

Oh, thanks. I will fix it in V2 and remember to do such tests in future.

Best regards,

Pingfan

Quoted 13 lines

include/Makefile dist += include/Makefile \ · line 14

Quoted 13 lines
include/Makefile +1 -0
index cd88a26b1476..6a3e8541c36d 100644
@@ -8,6 +8,7 @@ dist += include/Makefile                \
        include/x86/mb_header.h         \
        include/x86/multiboot2.h        \
        include/elf.h                   \
+       include/pe.h                    \
        include/image.h                 \
        include/unused.h                \
        include/boot/linuxbios_tables.h \

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#6 lore raw

Re: [PATCH 2/3] kexec: Introduce UKI image parser

Quoted 8 lines · Pingfan Liu

Hello everyone,

I have created V2 and make a PR: https://github.com/horms/kexec-tools/pull/9

Due to utilizing the workflow in github to have some basic checks automatically , I migrated it to github instead of mailing-list.

Please share your opinions.

I appreciate you making use of GitHub,
but please post patch submissions using the email workflow.

Thanks!

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#7 lore raw

Re: [PATCH 2/3] kexec: Introduce UKI image parser

Quoted 6 lines · Simon Horman
Quoted 4 lines · Pingfan Liu

Hello everyone,

I have created V2 and make a PR: https://github.com/horms/kexec-tools/pull/9

Quoted 8 lines
Quoted 4 lines

Due to utilizing the workflow in github to have some basic checks automatically , I migrated it to github instead of mailing-list.

Please share your opinions.

I appreciate you making use of GitHub,
but please post patch submissions using the email workflow.

See, I will use .github/workflows to check any build issue and go through the email workflow.

I have posted V2 using the mailing list.

Thanks,

Pingfan

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#8 lore raw

PATCH 3/3 arm64: Support UKI image format

In reply to Pingfan Liu
Cc Simon Horman <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Baoquan He <[email protected]>
Cc Dave Young <[email protected]>
---
 kexec/arch/arm64/kexec-arm64.c | 1 +
 1 file changed, 1 insertion(+)
kexec/arch/arm64/kexec-arm64.c +1 -0
index 55e822b..9091f40 100644
@@ -75,6 +75,7 @@ struct file_type file_type[] = {
 	{"Image", image_arm64_probe, image_arm64_load, image_arm64_usage},
 	{"uImage", uImage_arm64_probe, uImage_arm64_load, uImage_arm64_usage},
 	{"vmlinuz", pez_arm64_probe, pez_arm64_load, pez_arm64_usage},
+	{"uki", uki_image_probe, uki_image_load, uki_image_usage},
 };
 
 int file_types = sizeof(file_type) / sizeof(file_type[0]);
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec