kexec: Add support for UKI format kernel
Subsystems (2)
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
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
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.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 2/3 kexec: Introduce UKI image parser
In reply to Pingfan LiuA 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.
--- 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
@@ -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
@@ -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
@@ -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
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.
Signed-off-by Pingfan Liu <[email protected]>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
@@ -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
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.
Signed-off-by Pingfan Liu <[email protected]>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.his 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@@ -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
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.
Signed-off-by Pingfan Liu <[email protected]>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.cQuoted 7 lines
Hi Pingfan Liu,
I think you need to squash the following into this patch so that
pe.his 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@@ -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
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
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
PATCH 3/3 arm64: Support UKI image format
In reply to Pingfan Liu--- kexec/arch/arm64/kexec-arm64.c | 1 + 1 file changed, 1 insertion(+)
kexec/arch/arm64/kexec-arm64.c +1 -0
@@ -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