Skip to contentSkip to conversation

zboot: enable kexec_load for zboot kernel images

PATCH Applied Dave Young 4 participants 6 messages Sep 14, 2023 – Sep 20, 2023

Subsystems (2)
lore
Share & tools
lore

Dave Young

author
#0 lore raw

cover zboot: enable kexec_load for zboot kernel images

The current kexec-tools only support kexec_file_load for zboot kernel images on arm64.

This series tweak a bit of the code so kexec_load can also load zboot images on both arm64 and loongarch.

V1 changes:
- dup the kernel_fd so that kexec_file_load can work since slurp_fd will close it.
- code clean up.

Dave Young (2):
zboot: enable arm64 kexec_load for zboot image
zboot: add loongarch kexec_load support

 include/kexec-pe-zboot.h                   |  3 +-
 kexec/arch/arm64/kexec-vmlinuz-arm64.c     | 26 ++++++-
 kexec/arch/loongarch/Makefile              |  1 +
 kexec/arch/loongarch/image-header.h        |  1 +
 kexec/arch/loongarch/kexec-loongarch.c     |  1 +
 kexec/arch/loongarch/kexec-loongarch.h     |  4 +
 kexec/arch/loongarch/kexec-pez-loongarch.c | 90 ++++++++++++++++++++++
 kexec/kexec-pe-zboot.c                     |  4 +-
 kexec/kexec.c                              |  2 +-
 kexec/kexec.h                              |  1 +
 10 files changed, 127 insertions(+), 6 deletions(-)
 create mode 100644 kexec/arch/loongarch/kexec-pez-loongarch.c
Signature · 7 lines
2.37.2


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

PATCH 1/2 zboot: enable arm64 kexec_load for zboot image

kexec_file_load support of zboot kernel image decompressed the vmlinuz, so in kexec_load code just load the kernel with reading the decompressed kernel fd into a new buffer and use it directly.

---
 include/kexec-pe-zboot.h               |  3 ++-
 kexec/arch/arm64/kexec-vmlinuz-arm64.c | 26 +++++++++++++++++++++++---
 kexec/kexec-pe-zboot.c                 |  4 +++-
 kexec/kexec.c                          |  2 +-
 kexec/kexec.h                          |  1 +
 5 files changed, 30 insertions(+), 6 deletions(-)
include/kexec-pe-zboot.h +2 -1
index e2e0448a81f2..374916cbe883 100644
@@ -11,5 +11,6 @@ struct linux_pe_zboot_header {
 	uint32_t compress_type;
 };
 
-int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd);
+int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,
+		off_t *kernel_size);
 #endif
kexec/arch/arm64/kexec-vmlinuz-arm64.c +23 -3
index c0ee47c8f50a..e291a34c97ad 100644
@@ -34,6 +34,7 @@
 #include "arch/options.h"
 
 static int kernel_fd = -1;
+static off_t decompressed_size;
 
 /* Returns:
  * -1 : in case of error/invalid format (not a valid PE+compressed ZBOOT format.
@@ -72,7 +73,7 @@ int pez_arm64_probe(const char *kernel_buf, off_t kernel_size)
 		return -1;
 	}
 
-	ret = pez_prepare(buf, buf_sz, &kernel_fd);
+	ret = pez_prepare(buf, buf_sz, &kernel_fd, &decompressed_size);
 
 	if (!ret) {
 	    /* validate the arm64 specific header */
@@ -98,8 +99,27 @@ bad_header:
 int pez_arm64_load(int argc, char **argv, const char *buf, off_t len,
 			struct kexec_info *info)
 {
-	info->kernel_fd = kernel_fd;
-	return image_arm64_load(argc, argv, buf, len, info);
+	if (kernel_fd > 0 && decompressed_size > 0) {
+		char *kbuf;
+		off_t nread;
+		int fd;
+
+		info->kernel_fd = kernel_fd;
+		fd = dup(kernel_fd);
+		if (fd < 0) {
+			dbgprintf("%s: dup fd failed.\n", __func__);
+			return -1;
+		}
+		kbuf = slurp_fd(fd, NULL, decompressed_size, &nread);
+		if (!kbuf || nread != decompressed_size) {
+			dbgprintf("%s: slurp_fd failed.\n", __func__);
+			return -1;
+		}
+		return image_arm64_load(argc, argv, kbuf, decompressed_size, info);
+	}
+
+	dbgprintf("%s: wrong kernel file descriptor.\n", __func__);
+	return -1;
 }
 
 void pez_arm64_usage(void)
kexec/kexec-pe-zboot.c +3 -1
index 2f2e052b76c5..3abd17d9fe59 100644
@@ -37,7 +37,8 @@
  *
  * crude_buf: the content, which is read from the kernel file without any processing
  */
-int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd)
+int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,
+		off_t *kernel_size)
 {
 	int ret = -1;
 	int fd = 0;
@@ -110,6 +111,7 @@ int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd)
 		goto fail_bad_header;
 	}
 
+	*kernel_size = decompressed_size;
 	dbgprintf("%s: done\n", __func__);
 
 	ret = 0;
kexec/kexec.c +1 -1
index c3b182e254e0..1edbd349c86d 100644
@@ -489,7 +489,7 @@ static int add_backup_segments(struct kexec_info *info,
 	return 0;
 }
 
-static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
+char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
 {
 	char *buf;
 	off_t progress;
kexec/kexec.h +1 -0
index ed3b499a80f2..093338969c57 100644
@@ -267,6 +267,7 @@ extern void die(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
 extern void *xmalloc(size_t size);
 extern void *xrealloc(void *ptr, size_t size);
+extern char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread);
 extern char *slurp_file(const char *filename, off_t *r_size);
 extern char *slurp_file_mmap(const char *filename, off_t *r_size);
 extern char *slurp_file_len(const char *filename, off_t size, off_t *nread);
Signature · 7 lines
2.37.2


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

PATCH 2/2 zboot: add loongarch kexec_load support

In reply to Dave Young

Copy arm64 code and change for loongarch so that the kexec -c can load a zboot image.
Note: probe zboot image first otherwise the pei-loongarch file type will be used.

---
 kexec/arch/loongarch/Makefile              |  1 +
 kexec/arch/loongarch/image-header.h        |  1 +
 kexec/arch/loongarch/kexec-loongarch.c     |  1 +
 kexec/arch/loongarch/kexec-loongarch.h     |  4 +
 kexec/arch/loongarch/kexec-pez-loongarch.c | 90 ++++++++++++++++++++++
 5 files changed, 97 insertions(+)
 create mode 100644 kexec/arch/loongarch/kexec-pez-loongarch.c
kexec/arch/loongarch/Makefile +1 -0
index 3b33b9693287..cee7e569a2a2 100644
@@ -6,6 +6,7 @@ loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-elf-loongarch.c
 loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-pei-loongarch.c
 loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-elf-rel-loongarch.c
 loongarch_KEXEC_SRCS += kexec/arch/loongarch/crashdump-loongarch.c
+loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-pez-loongarch.c
 
 loongarch_MEM_REGIONS = kexec/mem_regions.c
 
kexec/arch/loongarch/image-header.h +1 -0
index 3b7576552685..223d81f77d9f 100644
@@ -33,6 +33,7 @@ struct loongarch_image_header {
 };
 
 static const uint8_t loongarch_image_pe_sig[2] = {'M', 'Z'};
+static const uint8_t loongarch_pe_machtype[6] = {'P','E', 0x0, 0x0, 0x64, 0x62};
 
 /**
  * loongarch_header_check_pe_sig - Helper to check the loongarch image header.
kexec/arch/loongarch/kexec-loongarch.c +1 -0
index f47c99861674..62ff8fd1aeb7 100644
@@ -165,6 +165,7 @@ int get_memory_ranges(struct memory_range **range, int *ranges,
 
 struct file_type file_type[] = {
 	{"elf-loongarch", elf_loongarch_probe, elf_loongarch_load, elf_loongarch_usage},
+	{"pez-loongarch", pez_loongarch_probe, pez_loongarch_load, pez_loongarch_usage},
 	{"pei-loongarch", pei_loongarch_probe, pei_loongarch_load, pei_loongarch_usage},
 };
 int file_types = sizeof(file_type) / sizeof(file_type[0]);
kexec/arch/loongarch/kexec-loongarch.h +4 -0
index 5120a26fd513..2c7624f2fd3a 100644
@@ -27,6 +27,10 @@ int pei_loongarch_probe(const char *buf, off_t len);
 int pei_loongarch_load(int argc, char **argv, const char *buf, off_t len,
 	struct kexec_info *info);
 void pei_loongarch_usage(void);
+int pez_loongarch_probe(const char *kernel_buf, off_t kernel_size);
+int pez_loongarch_load(int argc, char **argv, const char *buf, off_t len,
+		       struct kexec_info *info);
+void pez_loongarch_usage(void);
 
 int loongarch_process_image_header(const struct loongarch_image_header *h);
 
kexec/arch/loongarch/kexec-pez-loongarch.c new file +90 -0

90 lines not on this page · raw message

Signature · 7 lines
2.37.2


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Tested-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] zboot: enable arm64 kexec_load for zboot image

In reply to Dave Young
Quoted 4 lines · Dave Young

kexec_file_load support of zboot kernel image decompressed the vmlinuz, so in kexec_load code just load the kernel with reading the decompressed kernel fd into a new buffer and use it directly.

Quoted 8 lines
---
 include/kexec-pe-zboot.h               |  3 ++-
 kexec/arch/arm64/kexec-vmlinuz-arm64.c | 26 +++++++++++++++++++++++---
 kexec/kexec-pe-zboot.c                 |  4 +++-
 kexec/kexec.c                          |  2 +-
 kexec/kexec.h                          |  1 +
 5 files changed, 30 insertions(+), 6 deletions(-)

I got this problem too on arm64 machine, applied this patch to test, can confirm it works to fix the load failure when kexec_load is taken.

Quoted 120 lines
include/kexec-pe-zboot.h +2 -1
index e2e0448a81f2..374916cbe883 100644
@@ -11,5 +11,6 @@ struct linux_pe_zboot_header {
 	uint32_t compress_type;
 };
 
-int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd);
+int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,
+		off_t *kernel_size);
 #endif
kexec/arch/arm64/kexec-vmlinuz-arm64.c +23 -3
index c0ee47c8f50a..e291a34c97ad 100644
@@ -34,6 +34,7 @@
 #include "arch/options.h"
 
 static int kernel_fd = -1;
+static off_t decompressed_size;
 
 /* Returns:
  * -1 : in case of error/invalid format (not a valid PE+compressed ZBOOT format.
@@ -72,7 +73,7 @@ int pez_arm64_probe(const char *kernel_buf, off_t kernel_size)
 		return -1;
 	}
 
-	ret = pez_prepare(buf, buf_sz, &kernel_fd);
+	ret = pez_prepare(buf, buf_sz, &kernel_fd, &decompressed_size);
 
 	if (!ret) {
 	    /* validate the arm64 specific header */
@@ -98,8 +99,27 @@ bad_header:
 int pez_arm64_load(int argc, char **argv, const char *buf, off_t len,
 			struct kexec_info *info)
 {
-	info->kernel_fd = kernel_fd;
-	return image_arm64_load(argc, argv, buf, len, info);
+	if (kernel_fd > 0 && decompressed_size > 0) {
+		char *kbuf;
+		off_t nread;
+		int fd;
+
+		info->kernel_fd = kernel_fd;
+		fd = dup(kernel_fd);
+		if (fd < 0) {
+			dbgprintf("%s: dup fd failed.\n", __func__);
+			return -1;
+		}
+		kbuf = slurp_fd(fd, NULL, decompressed_size, &nread);
+		if (!kbuf || nread != decompressed_size) {
+			dbgprintf("%s: slurp_fd failed.\n", __func__);
+			return -1;
+		}
+		return image_arm64_load(argc, argv, kbuf, decompressed_size, info);
+	}
+
+	dbgprintf("%s: wrong kernel file descriptor.\n", __func__);
+	return -1;
 }
 
 void pez_arm64_usage(void)
kexec/kexec-pe-zboot.c +3 -1
index 2f2e052b76c5..3abd17d9fe59 100644
@@ -37,7 +37,8 @@
  *
  * crude_buf: the content, which is read from the kernel file without any processing
  */
-int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd)
+int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,
+		off_t *kernel_size)
 {
 	int ret = -1;
 	int fd = 0;
@@ -110,6 +111,7 @@ int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd)
 		goto fail_bad_header;
 	}
 
+	*kernel_size = decompressed_size;
 	dbgprintf("%s: done\n", __func__);
 
 	ret = 0;
kexec/kexec.c +1 -1
index c3b182e254e0..1edbd349c86d 100644
@@ -489,7 +489,7 @@ static int add_backup_segments(struct kexec_info *info,
 	return 0;
 }
 
-static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
+char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
 {
 	char *buf;
 	off_t progress;
kexec/kexec.h +1 -0
index ed3b499a80f2..093338969c57 100644
@@ -267,6 +267,7 @@ extern void die(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
 extern void *xmalloc(size_t size);
 extern void *xrealloc(void *ptr, size_t size);
+extern char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread);
 extern char *slurp_file(const char *filename, off_t *r_size);
 extern char *slurp_file_mmap(const char *filename, off_t *r_size);
 extern char *slurp_file_len(const char *filename, off_t size, off_t *nread);
Signature · 7 lines
2.37.2


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

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

Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH v1 1/2] zboot: enable arm64 kexec_load for zboot image

In reply to Dave Young
Quoted 125 lines · Dave Young

kexec_file_load support of zboot kernel image decompressed the vmlinuz, so in kexec_load code just load the kernel with reading the decompressed kernel fd into a new buffer and use it directly.

---
 include/kexec-pe-zboot.h               |  3 ++-
 kexec/arch/arm64/kexec-vmlinuz-arm64.c | 26 +++++++++++++++++++++++---
 kexec/kexec-pe-zboot.c                 |  4 +++-
 kexec/kexec.c                          |  2 +-
 kexec/kexec.h                          |  1 +
 5 files changed, 30 insertions(+), 6 deletions(-)
include/kexec-pe-zboot.h +2 -1
index e2e0448a81f2..374916cbe883 100644
@@ -11,5 +11,6 @@ struct linux_pe_zboot_header {
        uint32_t compress_type;
 };
 
-int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd);
+int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,
+               off_t *kernel_size);
 #endif
kexec/arch/arm64/kexec-vmlinuz-arm64.c +23 -3
index c0ee47c8f50a..e291a34c97ad 100644
@@ -34,6 +34,7 @@
 #include "arch/options.h"
 
 static int kernel_fd = -1;
+static off_t decompressed_size;
 
 /* Returns:
  * -1 : in case of error/invalid format (not a valid PE+compressed ZBOOT format.
@@ -72,7 +73,7 @@ int pez_arm64_probe(const char *kernel_buf, off_t kernel_size)
                return -1;
        }
 
-       ret = pez_prepare(buf, buf_sz, &kernel_fd);
+       ret = pez_prepare(buf, buf_sz, &kernel_fd, &decompressed_size);
 
        if (!ret) {
            /* validate the arm64 specific header */
@@ -98,8 +99,27 @@ bad_header:
 int pez_arm64_load(int argc, char **argv, const char *buf, off_t len,
                        struct kexec_info *info)
 {
-       info->kernel_fd = kernel_fd;
-       return image_arm64_load(argc, argv, buf, len, info);
+       if (kernel_fd > 0 && decompressed_size > 0) {
+               char *kbuf;
+               off_t nread;
+               int fd;
+
+               info->kernel_fd = kernel_fd;
+               fd = dup(kernel_fd);
+               if (fd < 0) {
+                       dbgprintf("%s: dup fd failed.\n", __func__);
+                       return -1;
+               }
+               kbuf = slurp_fd(fd, NULL, decompressed_size, &nread);
+               if (!kbuf || nread != decompressed_size) {
+                       dbgprintf("%s: slurp_fd failed.\n", __func__);
+                       return -1;
+               }
+               return image_arm64_load(argc, argv, kbuf, decompressed_size, info);
+       }
+
+       dbgprintf("%s: wrong kernel file descriptor.\n", __func__);
+       return -1;
 }
 
 void pez_arm64_usage(void)
kexec/kexec-pe-zboot.c +3 -1
index 2f2e052b76c5..3abd17d9fe59 100644
@@ -37,7 +37,8 @@
  *
  * crude_buf: the content, which is read from the kernel file without any processing
  */
-int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd)
+int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd,
+               off_t *kernel_size)
 {
        int ret = -1;
        int fd = 0;
@@ -110,6 +111,7 @@ int pez_prepare(const char *crude_buf, off_t buf_sz, int *kernel_fd)
                goto fail_bad_header;
        }
 
+       *kernel_size = decompressed_size;
        dbgprintf("%s: done\n", __func__);
 
        ret = 0;
kexec/kexec.c +1 -1
index c3b182e254e0..1edbd349c86d 100644
@@ -489,7 +489,7 @@ static int add_backup_segments(struct kexec_info *info,
        return 0;
 }
 
-static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
+char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
 {
        char *buf;
        off_t progress;
kexec/kexec.h +1 -1
index ed3b499a80f2..093338969c57 100644
@@ -267,6 +267,7 @@ extern void die(const char *fmt, ...)
        __attribute__ ((format (printf, 1, 2)));
 extern void *xmalloc(size_t size);
 extern void *xrealloc(void *ptr, size_t size);
+extern char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread);
 extern char *slurp_file(const char *filename, off_t *r_size);
 extern char *slurp_file_mmap(const char *filename, off_t *r_size);
 extern char *slurp_file_len(const char *filename, off_t size, off_t *nread);
--
Quoted 2 lines

2.37.2

Have tested both kexec_load and kexec_file_load, both of them work.

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