kexec: Add support for UKI format kernel
Subsystems (2)
v2 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
---
v1 -> v2:
Add include/pe.h in makefile to fix 'make dist'
Rename default_initrd_fd to implicit_initrd_fd
Pingfan Liu (3):
kexec: Introduce implicit_initrd_fd to pass internal initrd
information
kexec: Introduce UKI image parser
arm64: Support UKI image format
include/Makefile | 1 + 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 ++ 9 files changed, 240 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 v2 1/3 kexec: Introduce implicit_initrd_fd to pass internal initrd information
At present, initrd information is passed through --initrd args, and handled until image load. But for the UKI case, the initrd may be stored implicitly 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 implicit_initrd_fd.
--- 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 (implicit_initrd_fd !=-1 ) + info->initrd_fd = implicit_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 (implicit_initrd_fd !=-1 ) + info->initrd_fd = implicit_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 implicit_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 implicit_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 v2 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 continue the procession of the kernel image.
--- include/Makefile | 1 + include/pe.h | 104 +++++++++++++++++++++++++++++++++++++++ kexec/Makefile | 1 + kexec/kexec-uki.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++ kexec/kexec.h | 4 ++ 5 files changed, 232 insertions(+) create mode 100644 include/pe.h create mode 100644 kexec/kexec-uki.c
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 \
include/pe.h new file +104 -0
104 lines not on this page · raw message
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: [PATCHv2 2/3] kexec: Introduce UKI image parser
Quoted 9 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 continue the procession of the kernel image.
Signed-off-by Pingfan Liu <[email protected]>
...
Quoted 1 line
kexec/kexec-uki.c +0
-0
...
Quoted 33 lines
kexec/kexec-uki.c +33
-0+int uki_image_probe (const char *file_buf,off_t buf_sz) +{ +struct pe_hdr *pe_hdr; +struct pe32plus_opt_hdr *opt_hdr; +struct section_header *sect_hdr; +int pe_hdr_offset, section_nr, linux_sz =-1 ; +char *pe_part_buf, *linux_src; +char *initrd_fname =NULL ; +int initrd_fd =-1 ; + + pe_hdr_offset =get_pehdr_offset (file_buf); + pe_part_buf = (char *)file_buf + pe_hdr_offset; + pe_hdr = (struct pe_hdr *)pe_part_buf; +if (pe_hdr->opt_hdr_size ==0 ) { +printf ("ERR: optional header is missing\n" ); +return -1 ; + } + section_nr = pe_hdr->sections; + opt_hdr = (struct pe32plus_opt_hdr *)(pe_part_buf +sizeof (struct pe_hdr )); + sect_hdr = (struct section_header *)((char *)opt_hdr + pe_hdr->opt_hdr_size); + +for (int i =0 ; i < section_nr; i++) { +if (!strcmp (sect_hdr->name,UKI_LINUX_SECTION )) { +/* data_addr is relative to the whole file */ + linux_src = (char *)file_buf + sect_hdr->data_addr; + linux_sz = sect_hdr->raw_data_size; + + }else if (!strcmp (sect_hdr->name,UKI_INITRD_SECTION )) { +if (!(initrd_fname =strdup (FILENAME_UKI_INITRD ))) { +dbgprintf ("%s: Can't duplicate strings\n" , __func__); + goto next; + } +
kexec/kexec-uki.c line 80
Quoted 10 lines
kexec/kexec-uki.c +10
-0+if ((initrd_fd =mkstemp (initrd_fname)) <0 ) { +dbgprintf ("%s: Can't open file %s\n" , __func__, initrd_fname); + goto next; + } + +if (write (initrd_fd, (char *)file_buf + sect_hdr->data_addr, + sect_hdr->raw_data_size) != sect_hdr->raw_data_size) { +dbgprintf ("%s: Can't write the compressed file %s\n" , + __func__, initrd_fname); + goto next;
I plan to apply this series, but initrd_fd appears to be leaked here. Could you send a follow-up patch to address that? Perhaps like this:
err = write(...);
close(initrd_fd)
if (err != sect_hdr->raw_data_size) {
...
} else {
...
}Thanks!
kexec/kexec-uki.c line 88
Quoted 8 lines
kexec/kexec-uki.c +8
-0+ }else { + implicit_initrd_fd =open (initrd_fname,O_RDONLY ); +close (initrd_fd); + } + } +next: + sect_hdr++; + }
...
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCHv2 2/3] kexec: Introduce UKI image parser
Hi Simon,
Thanks for your review.
Quoted 56 lines · Simon Horman
Quoted 9 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 continue the procession of the kernel image.
Signed-off-by Pingfan Liu <[email protected]>...
Quoted 1 line
kexec/kexec-uki.c +0
-0...
Quoted 38 lines
kexec/kexec-uki.c +38
-0+int uki_image_probe (const char *file_buf,off_t buf_sz) +{ +struct pe_hdr *pe_hdr; +struct pe32plus_opt_hdr *opt_hdr; +struct section_header *sect_hdr; +int pe_hdr_offset, section_nr, linux_sz =-1 ; +char *pe_part_buf, *linux_src; +char *initrd_fname =NULL ; +int initrd_fd =-1 ; + + pe_hdr_offset =get_pehdr_offset (file_buf); + pe_part_buf = (char *)file_buf + pe_hdr_offset; + pe_hdr = (struct pe_hdr *)pe_part_buf; +if (pe_hdr->opt_hdr_size ==0 ) { +printf ("ERR: optional header is missing\n" ); +return -1 ; + } + section_nr = pe_hdr->sections; + opt_hdr = (struct pe32plus_opt_hdr *)(pe_part_buf +sizeof (struct pe_hdr )); + sect_hdr = (struct section_header *)((char *)opt_hdr + pe_hdr->opt_hdr_size); + +for (int i =0 ; i < section_nr; i++) { +if (!strcmp (sect_hdr->name,UKI_LINUX_SECTION )) { +/* data_addr is relative to the whole file */ + linux_src = (char *)file_buf + sect_hdr->data_addr; + linux_sz = sect_hdr->raw_data_size; + + }else if (!strcmp (sect_hdr->name,UKI_INITRD_SECTION )) { +if (!(initrd_fname =strdup (FILENAME_UKI_INITRD ))) { +dbgprintf ("%s: Can't duplicate strings\n" , __func__); + goto next; + } + +if ((initrd_fd =mkstemp (initrd_fname)) <0 ) { +dbgprintf ("%s: Can't open file %s\n" , __func__, initrd_fname); + goto next; + } +
Quoted 9 lines
kexec/kexec-uki.c line 80
Quoted 5 lines
kexec/kexec-uki.c +5
-0+if (write (initrd_fd, (char *)file_buf + sect_hdr->data_addr, + sect_hdr->raw_data_size) != sect_hdr->raw_data_size) { +dbgprintf ("%s: Can't write the compressed file %s\n" , + __func__, initrd_fname); + goto next;I plan to apply this series, but
initrd_fdappears to be leaked here. Could you send a follow-up patch to address that? Perhaps like this:
I have sent out a dedicated v3 "[PATCHv3 2/3] kexec: Introduce UKI image parser" to fix this leak issue.
Quoted 2 lines
err = write(...);
close(initrd_fd)My fix duplicates the close(initrd_fd) in each branch. Since initrd_fd is the only reference to mkstemp(initrd_fname), I think if closing it, the temp file may disappear before implicit_initrd_fd= open(initrd_fname, O_RDONLY).
Best Regards,
Pingfan
Quoted 17 lines
if (err != sect_hdr->raw_data_size) { ... } else { ... }Thanks!
kexec/kexec-uki.c line 88
Quoted 8 lines
kexec/kexec-uki.c +8
-0+ }else { + implicit_initrd_fd =open (initrd_fname,O_RDONLY ); +close (initrd_fd); + } + } +next: + sect_hdr++; + }
Quoted 2 lines
...
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
PATCH v3 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 continue the procession of the kernel image.
--- include/Makefile | 1 + include/pe.h | 104 +++++++++++++++++++++++++++++++++++++++ kexec/Makefile | 1 + kexec/kexec-uki.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++ kexec/kexec.h | 4 ++ 5 files changed, 233 insertions(+) create mode 100644 include/pe.h create mode 100644 kexec/kexec-uki.c
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 \
include/pe.h new file +104 -0
104 lines not on this page · raw message
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 +123 -0
123 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
PATCH v2 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
Quoted 9 lines · Pingfan Liu
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.
Quoted 4 lines
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
Thanks,
Series applied.
Please see my request for a follow-up for patch 2/3 [1].
[1] https://lore.kernel.org/kexec/[email protected]/
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Hi Pingfan,
Hi Simon,
I know I'm a little bit too late, but I finally had time to take a look. All in all the series looks fine to me. But I have two questions.
1) Is there a reason why you don't enable the UKI for x86 as well? The way I see it patch 1 and 2 cover x86 as well. So you only should need a patch similar to patch 3. Or am I missing something?
2) Would it make sense to "reuse" the mechanism you introduced for the initrd for the command line as well. I.e. if it's a UKI with a .cmdline section and the user didn't provide any of the --command-line, --reuse-cmdline, etc. then use the command line from the UKI.
Thanks
Philipp
Quoted 35 lines · On Wed, 16 Oct 2024 19:34:12 +0800 Pingfan Liu <[email protected]> wrote:
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]>---
v1 -> v2:
Addinclude/pe.hin makefile to fix 'make dist'
Renamedefault_initrd_fdtoimplicit_initrd_fdPingfan Liu (3):
kexec: Introduceimplicit_initrd_fdto pass internal initrd
information
kexec: Introduce UKI image parser
arm64: Support UKI image format
Quoted 13 lines
include/Makefile | 1 + 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 ++ 9 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 include/pe.h create mode 100644 kexec/kexec-uki.c
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Hi Philipp,
Thanks for sharing your thoughts.
Quoted 11 lines · Philipp Rudo
Hi Pingfan,
Hi Simon,I know I'm a little bit too late, but I finally had time to take a look. All in all the series looks fine to me. But I have two questions.
1) Is there a reason why you don't enable the UKI for x86 as well? The way I see it patch 1 and 2 cover x86 as well. So you only should need a patch similar to patch 3. Or am I missing something?
Yes, there should be one for x86. I will do it after this series is merged.
Quoted 5 lines
2) Would it make sense to "reuse" the mechanism you introduced for the initrd for the command line as well. I.e. if it's a UKI with a .cmdline section and the user didn't provide any of the --command-line, --reuse-cmdline, etc. then use the command line from the UKI.
I had thought about it, and the scenario should look like this: a system with a UKI image so there is no direct access to get initrd, which obstructs the use of --initrd option. On the other hand, --reuse-cmdline can always get the cmdline from /proc/cmdline without trouble.
Thanks,
Pingfan
Quoted 55 lines
Thanks
PhilippQuoted 48 lines · On Wed, 16 Oct 2024 19:34:12 +0800 Pingfan Liu <[email protected]> wrote:
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]>---
v1 -> v2:
Addinclude/pe.hin makefile to fix 'make dist'
Renamedefault_initrd_fdtoimplicit_initrd_fdPingfan Liu (3):
kexec: Introduceimplicit_initrd_fdto pass internal initrd
information
kexec: Introduce UKI image parser
arm64: Support UKI image formatinclude/Makefile | 1 + 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 ++ 9 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 include/pe.h create mode 100644 kexec/kexec-uki.c
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec