Skip to contentSkip to conversation

arm64: zboot support

PATCH Superseded Pingfan Liu 3 participants 13 messages May 5, 2023 – May 12, 2023

Subsystems (2)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover arm64: zboot support

As more complicated capsule kernel format occurs like zboot, where the compressed kernel is stored as a payload. The straight forward decompression can not meet the demand.

As the first step, on aarch64, reading in the kernel file in a probe method and decide how to unfold the content by the method itself.

The new designed probe interface returns two factors:
1. the parsed kernel_buf should be returned so that it can be used by the image load method later.
2. the final fd passed to sys_kexec_file_load, since aarch64 kernel can only work with Image format, the outer payload should be stripped and a temporary file of Image should be created.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Pingfan Liu (5):
kexec: Adding missing free for kernel_buf arm64/zImage: Remove unnecessary allocation for
kernel_uncompressed_buf
arm64: change the prototype of image probe function
arm64: Scatter the reading of kernel file into each probe
arm64: add support for zboot image

 kexec/arch/arm/kexec-arm.h            |   4 +-
 kexec/arch/arm/kexec-uImage-arm.c     |   2 +-
 kexec/arch/arm64/Makefile             |   3 +-
 kexec/arch/arm64/kexec-arm64.c        |   1 +
 kexec/arch/arm64/kexec-arm64.h        |  13 +-
 kexec/arch/arm64/kexec-elf-arm64.c    |   7 +-
 kexec/arch/arm64/kexec-image-arm64.c  |   6 +-
 kexec/arch/arm64/kexec-uImage-arm64.c |  17 +-
 kexec/arch/arm64/kexec-zImage-arm64.c |  23 +--
 kexec/arch/arm64/kexec-zboot-arm64.c  | 261 ++++++++++++++++++++++++++
 kexec/arch/arm64/zboot.h              |  26 +++
 kexec/kexec.c                         |  48 +++--
 kexec/kexec.h                         |   8 +
 13 files changed, 377 insertions(+), 42 deletions(-)
 create mode 100644 kexec/arch/arm64/kexec-zboot-arm64.c
 create mode 100644 kexec/arch/arm64/zboot.h
Signature · 7 lines
2.31.1


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

PATCH 1/5 kexec: Adding missing free for kernel_buf

slurp_decompress_file() allocates memory but nowhere to free it. Adding that missing free.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 kexec/kexec.c | 1 +
 1 file changed, 1 insertion(+)
kexec/kexec.c +1 -0
index 36bb2ad..614cd1d 100644
@@ -1379,6 +1379,7 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
 		}
 	}
 
+	free(kernel_buf);
 	close(kernel_fd);
 	return ret;
 }
Signature · 7 lines
2.31.1


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

Re: [PATCH 1/5] kexec: Adding missing free for kernel_buf

Quoted 2 lines · Pingfan Liu

slurp_decompress_file() allocates memory but nowhere to free it. Adding that missing free.

Hi Pingfan,

There seem to be:

a) other places where slurp_decompress_file() is called and;
b) other places where do_kexec_file_load() returns.

I wonder if a more comprehensive fix appropriate.

Quoted 25 lines

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 kexec/kexec.c | 1 +
 1 file changed, 1 insertion(+)
kexec/kexec.c +1 -0
index 36bb2ad..614cd1d 100644
@@ -1379,6 +1379,7 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
 		}
 	}
 
+	free(kernel_buf);
 	close(kernel_fd);
 	return ret;
 }
Signature · 1 line
2.31.1

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

#3 lore raw

PATCH 2/5 arm64/zImage: Remove unnecessary allocation for kernel_uncompressed_buf

In reply to Pingfan Liu

Since slurp_decompress_file() allocates the buffer to hold the content read from the file, no need to allocate the buffer again.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 kexec/arch/arm64/kexec-zImage-arm64.c | 10 ----------
 1 file changed, 10 deletions(-)
kexec/arch/arm64/kexec-zImage-arm64.c +0 -10
index 6ee82ff..166d7ef 100644
@@ -67,15 +67,6 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size)
 		goto fail_mkstemp;
 	}
 
-	kernel_uncompressed_buf =
-		(char *) calloc(kernel_size, sizeof(off_t));
-	if (!kernel_uncompressed_buf) {
-		dbgprintf("%s: Can't calloc %ld bytes\n",
-				__func__, kernel_size);
-		ret= -ENOMEM;
-		goto fail_calloc;
-	}
-
 	/* slurp in the input kernel */
 	dbgprintf("%s: ", __func__);
 	kernel_uncompressed_buf = slurp_decompress_file(kernel_buf,
@@ -128,7 +119,6 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size)
 fail_bad_header:
 	free(kernel_uncompressed_buf);
 
-fail_calloc:
 	if (fd >= 0)
 		close(fd);
 
Signature · 7 lines
2.31.1


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

PATCH 3/5 arm64: change the prototype of image probe function

In reply to Pingfan Liu

Changing the aarch64 probe's prototype from

	typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
to
	typedef int (probe_t)(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);

Later, info can be used to return both the file descriptor and parsed kernel buffer. The fd is passed to sys_kexec_file_load, and the parsed kernel buffer is used by image's load function.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 kexec/arch/arm/kexec-arm.h            |  4 ++--
 kexec/arch/arm/kexec-uImage-arm.c     |  2 +-
 kexec/arch/arm64/kexec-arm64.h        |  8 ++++----
 kexec/arch/arm64/kexec-elf-arm64.c    |  2 +-
 kexec/arch/arm64/kexec-image-arm64.c  |  2 +-
 kexec/arch/arm64/kexec-uImage-arm64.c |  2 +-
 kexec/arch/arm64/kexec-zImage-arm64.c |  2 +-
 kexec/kexec.c                         | 15 +++++++++++++--
 kexec/kexec.h                         |  6 ++++++
 9 files changed, 30 insertions(+), 13 deletions(-)
kexec/arch/arm/kexec-arm.h +2 -2
index a74cce2..18069f3 100644
@@ -9,12 +9,12 @@
 
 extern off_t initrd_base, initrd_size;
 
-int zImage_arm_probe(const char *buf, off_t len);
+int zImage_arm_probe(const char *buf, off_t len, struct kexec_info *info);
 int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 		        struct kexec_info *info);
 void zImage_arm_usage(void);
 
-int uImage_arm_probe(const char *buf, off_t len);
+int uImage_arm_probe(const char *buf, off_t len, struct kexec_info *info);
 int uImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 		        struct kexec_info *info);
 extern int have_sysfs_fdt(void);
kexec/arch/arm/kexec-uImage-arm.c +1 -1
index 03c2f4d..d955eb3 100644
@@ -9,7 +9,7 @@
 #include "../../kexec.h"
 #include "kexec-arm.h"
 
-int uImage_arm_probe(const char *buf, off_t len)
+int uImage_arm_probe(const char *buf, off_t len, struct kexec_info *info)
 {
 	return uImage_probe_kernel(buf, len, IH_ARCH_ARM);
 }
kexec/arch/arm64/kexec-arm64.h +4 -4
index 5eb9fc0..88bb508 100644
@@ -29,22 +29,22 @@
 #define NOT_KV_ADDR	(0x0)
 #define NOT_PADDR	(ULONGLONG_MAX)
 
-int elf_arm64_probe(const char *kernel_buf, off_t kernel_size);
+int elf_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);
 int elf_arm64_load(int argc, char **argv, const char *kernel_buf,
 	off_t kernel_size, struct kexec_info *info);
 void elf_arm64_usage(void);
 
-int image_arm64_probe(const char *kernel_buf, off_t kernel_size);
+int image_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);
 int image_arm64_load(int argc, char **argv, const char *kernel_buf,
 	off_t kernel_size, struct kexec_info *info);
 void image_arm64_usage(void);
 
-int uImage_arm64_probe(const char *buf, off_t len);
+int uImage_arm64_probe(const char *buf, off_t len, struct kexec_info *info);
 int uImage_arm64_load(int argc, char **argv, const char *buf, off_t len,
 		      struct kexec_info *info);
 void uImage_arm64_usage(void);
 
-int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size);
+int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);
 int zImage_arm64_load(int argc, char **argv, const char *kernel_buf,
 	off_t kernel_size, struct kexec_info *info);
 void zImage_arm64_usage(void);
kexec/arch/arm64/kexec-elf-arm64.c +1 -1
index e14f8e9..9238dd5 100644
@@ -16,7 +16,7 @@
 #include "kexec-elf.h"
 #include "kexec-syscall.h"
 
-int elf_arm64_probe(const char *kernel_buf, off_t kernel_size)
+int elf_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
 {
 	struct mem_ehdr ehdr;
 	int result;
kexec/arch/arm64/kexec-image-arm64.c +1 -1
index aa8f2e2..84aca72 100644
@@ -14,7 +14,7 @@
 #include "kexec-syscall.h"
 #include "arch/options.h"
 
-int image_arm64_probe(const char *kernel_buf, off_t kernel_size)
+int image_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
 {
 	const struct arm64_image_header *h;
 
kexec/arch/arm64/kexec-uImage-arm64.c +1 -1
index c466913..f5b94c8 100644
@@ -9,7 +9,7 @@
 #include "../../kexec.h"
 #include "kexec-arm64.h"
 
-int uImage_arm64_probe(const char *buf, off_t len)
+int uImage_arm64_probe(const char *buf, off_t len, struct kexec_info *info)
 {
 	int ret;
 
kexec/arch/arm64/kexec-zImage-arm64.c +1 -1
index 166d7ef..7877741 100644
@@ -40,7 +40,7 @@
  * fd : File descriptor of the temp file containing the decompressed
  *      Image.
  */
-int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size)
+int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
 {
 	int ret = -1;
 	int fd = 0;
kexec/kexec.c +13 -2
index 614cd1d..116fb4a 100644
@@ -742,14 +742,25 @@ static int my_load(const char *type, int fileind, int argc, char **argv,
 			return -1;
 		} else {
 			/* make sure our file is really of that type */
+#ifndef __aarch64__
 			if (file_type[i].probe(kernel_buf, kernel_size) < 0)
 				guess_only = 1;
+#else
+			if (file_type[i].probe(kernel_buf, kernel_size, &info) < 0)
+				guess_only = 1;
+
+#endif
 		}
 	}
 	if (!type || guess_only) {
 		for (i = 0; i < file_types; i++) {
+#ifndef __aarch64__
 			if (file_type[i].probe(kernel_buf, kernel_size) == 0)
 				break;
+#else
+			if (file_type[i].probe(kernel_buf, kernel_size, &info) == 0)
+				break;
+#endif
 		}
 		if (i == file_types) {
 			fprintf(stderr, "Cannot determine the file type "
@@ -1304,12 +1315,12 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
 #ifdef __aarch64__
 		/* handle Image.gz like cases */
 		if (is_zlib_file(kernel, &kernel_size)) {
-			if ((ret = file_type[i].probe(kernel, kernel_size)) >= 0) {
+			if ((ret = file_type[i].probe(kernel, kernel_size, &info)) >= 0) {
 				kernel_fd = ret;
 				break;
 			}
 		} else
-			if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
+			if (file_type[i].probe(kernel_buf, kernel_size, &info) >= 0)
 				break;
 #else
 		if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
kexec/kexec.h +6 -0
index 0d820ad..6e8430e 100644
@@ -191,7 +191,13 @@ unsigned long locate_hole(struct kexec_info *info,
 	unsigned long hole_min, unsigned long hole_max,
 	int hole_end);
 
+#ifndef __aarch64__
 typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
+#else
+typedef int (probe_t)(const char *kern_fname, off_t kernel_size,
+	struct kexec_info *info);
+#endif
+
 typedef int (load_t )(int argc, char **argv,
 	const char *kernel_buf, off_t kernel_size, 
 	struct kexec_info *info);
Signature · 7 lines
2.31.1


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

Re: [PATCH 3/5] arm64: change the prototype of image probe function

Quoted 10 lines · Pingfan Liu

Changing the aarch64 probe's prototype from

	typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
to
	typedef int (probe_t)(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);

Later, info can be used to return both the file descriptor and parsed kernel buffer. The fd is passed to sys_kexec_file_load, and the parsed kernel buffer is used by image's load function.

Hi Pingfan,

I am seeing a build failure on ARM (32bit).

138 | int zImage_arm_probe(const char *UNUSED(buf), off_t UNUSED(len))

      |     ^~~~~~~~~~~~~~~~
In file included from ../../kexec/arch/arm/kexec-zImage-arm.c:21:
../../kexec/arch/arm/kexec-arm.h:12:5: note: previous declaration of ‘zImage_arm_probe’ was here
   12 | int zImage_arm_probe(const char *buf, off_t len, struct kexec_info *info);
      |     ^~~~~~~~~~~~~~~~

make[1]: *** [Makefile:124: kexec/arch/arm/kexec-zImage-arm.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/home/runner/work/kexec-tools/kexec-tools/kexec-tools-2.0.26.git/_build/sub'
make: *** [Makefile:276: distcheck] Error 2
Error: Process completed with exit code 2.

...

Quoted 5 lines
kexec/kexec.h +0 -0
index 0d820ad..6e8430e 100644

kexec/kexec.h unsigned long locate_hole(struct kexec_info *info, · line 200

Quoted 10 lines
kexec/kexec.h +6 -0
@@ -191,7 +191,13 @@ unsigned long locate_hole(struct kexec_info *info,
 	unsigned long hole_min, unsigned long hole_max,
 	int hole_end);
 
+#ifndef __aarch64__
 typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
+#else
+typedef int (probe_t)(const char *kern_fname, off_t kernel_size,
+	struct kexec_info *info);
+#endif
+

This seems kind of unfortunate.
Could we update the prototype for all architectures?

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

#6 lore raw

Re: [PATCH 3/5] arm64: change the prototype of image probe function

Quoted 23 lines · Simon Horman
Quoted 10 lines · Pingfan Liu

Changing the aarch64 probe's prototype from

      typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
to
      typedef int (probe_t)(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);

Later, info can be used to return both the file descriptor and parsed kernel buffer. The fd is passed to sys_kexec_file_load, and the parsed kernel buffer is used by image's load function.

Hi Pingfan,

I am seeing a build failure on ARM (32bit).

138 | int zImage_arm_probe(const char *UNUSED(buf), off_t UNUSED(len))

      |     ^~~~~~~~~~~~~~~~
In file included from ../../kexec/arch/arm/kexec-zImage-arm.c:21:
../../kexec/arch/arm/kexec-arm.h:12:5: note: previous declaration of ‘zImage_arm_probe’ was here
   12 | int zImage_arm_probe(const char *buf, off_t len, struct kexec_info *info);
      |     ^~~~~~~~~~~~~~~~
Quoted 6 lines

make[1]: *** [Makefile:124: kexec/arch/arm/kexec-zImage-arm.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/home/runner/work/kexec-tools/kexec-tools/kexec-tools-2.0.26.git/_build/sub'
make: *** [Makefile:276: distcheck] Error 2
Error: Process completed with exit code 2.

Oops. I just tested on x86_64 and aarch64, and did not detect this bug.

Quoted 12 lines

...

Quoted 8 lines
kexec/kexec.h +0 -0
index 0d820ad..6e8430e 100644
@@ -191,7 +191,13 @@ unsigned long locate_hole(struct kexec_info *info,
      unsigned long hole_min, unsigned long hole_max,
      int hole_end);
 
Quoted 11 lines

kexec/kexec.h unsigned long locate_hole(struct kexec_info *info, · line 200

Quoted 7 lines
kexec/kexec.h +6 -0
+#ifndef __aarch64__
 typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
+#else
+typedef int (probe_t)(const char *kern_fname, off_t kernel_size,
+     struct kexec_info *info);
+#endif
+

This seems kind of unfortunate.
Could we update the prototype for all architectures?

I will have a try with coccinelle.

Thanks for your help.

Regards,

Pingfan

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

#7 lore raw

PATCH 4/5 arm64: Scatter the reading of kernel file into each probe

In reply to Pingfan Liu

As more complicated capsule kernel format occurs like zboot, where the compressed kernel is stored as a payload. The straight forward decompression can not meet the demand.

So reading in the kernel file in a probe file and decide how to unfold the content by the method itself.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 kexec/arch/arm64/kexec-elf-arm64.c    |  7 ++++-
 kexec/arch/arm64/kexec-image-arm64.c  |  6 +++-
 kexec/arch/arm64/kexec-uImage-arm64.c | 17 ++++++++---
 kexec/arch/arm64/kexec-zImage-arm64.c | 13 ++++-----
 kexec/kexec.c                         | 42 ++++++++++++++++++---------
 kexec/kexec.h                         |  2 ++
 6 files changed, 60 insertions(+), 27 deletions(-)
kexec/arch/arm64/kexec-elf-arm64.c +6 -1
index 9238dd5..cedf3c6 100644
@@ -16,11 +16,13 @@
 #include "kexec-elf.h"
 #include "kexec-syscall.h"
 
-int elf_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
+int elf_arm64_probe(const char *kern_fname, off_t kernel_size, struct kexec_info *info)
 {
 	struct mem_ehdr ehdr;
+	char *kernel_buf;
 	int result;
 
+	kernel_buf = slurp_file(kern_fname, &kernel_size);
 	result = build_elf_exec_info(kernel_buf, kernel_size, &ehdr, 0);
 
 	if (result < 0) {
@@ -34,8 +36,11 @@ int elf_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info
 		goto on_exit;
 	}
 
+	info->kernel_fd = open(kern_fname, O_RDONLY);
+	info->kernel_buf = kernel_buf;
 	result = 0;
 on_exit:
+	free(kernel_buf);
 	free_elf_info(&ehdr);
 	return result;
 }
kexec/arch/arm64/kexec-image-arm64.c +5 -1
index 84aca72..90cd40a 100644
@@ -14,10 +14,12 @@
 #include "kexec-syscall.h"
 #include "arch/options.h"
 
-int image_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
+int image_arm64_probe(const char *kern_fname, off_t kernel_size, struct kexec_info *info)
 {
 	const struct arm64_image_header *h;
+	char *kernel_buf;
 
+	kernel_buf = slurp_file(kern_fname, &kernel_size);
 	if (kernel_size < sizeof(struct arm64_image_header)) {
 		dbgprintf("%s: No arm64 image header.\n", __func__);
 		return -1;
@@ -29,6 +31,8 @@ int image_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_in
 		dbgprintf("%s: Bad arm64 image header.\n", __func__);
 		return -1;
 	}
+	info->kernel_fd = open(kern_fname, O_RDONLY);
+	info->kernel_buf = kernel_buf;
 
 	return 0;
 }
kexec/arch/arm64/kexec-uImage-arm64.c +13 -4
index f5b94c8..cce1c76 100644
@@ -3,26 +3,35 @@
  */
 #include <stdint.h>
 #include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
 #include <sys/types.h>
 #include <image.h>
 #include <kexec-uImage.h>
 #include "../../kexec.h"
 #include "kexec-arm64.h"
 
-int uImage_arm64_probe(const char *buf, off_t len, struct kexec_info *info)
+int uImage_arm64_probe(const char *kern_fname, off_t len, struct kexec_info *info)
 {
 	int ret;
+	char *kernel_buf;
 
-	ret = uImage_probe_kernel(buf, len, IH_ARCH_ARM64);
+	kernel_buf = slurp_file(kern_fname, &len);
+	ret = uImage_probe_kernel(kernel_buf, len, IH_ARCH_ARM64);
 
 	/*  0 - valid uImage.
 	 * -1 - uImage is corrupted.
 	 *  1 - image is not a uImage.
 	 */
-	if (!ret)
+	if (!ret) {
+		info->kernel_fd = open(kern_fname, O_RDONLY);
+		info->kernel_buf = kernel_buf;
 		return 0;
-	else
+	}
+	else {
+		free(kernel_buf);
 		return -1;
+	}
 }
 
 int uImage_arm64_load(int argc, char **argv, const char *buf, off_t len,
kexec/arch/arm64/kexec-zImage-arm64.c +6 -7
index 7877741..450a915 100644
@@ -40,7 +40,7 @@
  * fd : File descriptor of the temp file containing the decompressed
  *      Image.
  */
-int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
+int zImage_arm64_probe(const char *kernel, off_t kernel_size, struct kexec_info *info)
 {
 	int ret = -1;
 	int fd = 0;
@@ -49,7 +49,7 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_i
 	char *kernel_uncompressed_buf = NULL;
 	const struct arm64_image_header *h;
 
-	if (!is_zlib_file(kernel_buf, &kernel_size)) {
+	if (!is_zlib_file(kernel, &kernel_size)) {
 		dbgprintf("%s: Not an zImage file (Image.gz).\n", __func__);
 		return -1;
 	}
@@ -69,7 +69,7 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_i
 
 	/* slurp in the input kernel */
 	dbgprintf("%s: ", __func__);
-	kernel_uncompressed_buf = slurp_decompress_file(kernel_buf,
+	kernel_uncompressed_buf = slurp_decompress_file(kernel,
 							&kernel_size);
 
 	/* check for correct header magic */
@@ -108,13 +108,12 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_i
 		ret = -1;
 		goto fail_bad_header;
 	}
-
+	info->kernel_fd = kernel_fd;
+	info->kernel_buf = kernel_uncompressed_buf;
 	unlink(fname);
-
-	free(kernel_uncompressed_buf);
 	free(fname);
 
-	return kernel_fd;
+	return 0;
 
 fail_bad_header:
 	free(kernel_uncompressed_buf);
kexec/kexec.c +28 -14

94 lines not on this page · raw message

kexec/kexec.h +2 -0
index 6e8430e..3349d16 100644
@@ -164,7 +164,9 @@ struct kexec_info {
 	unsigned long file_mode :1;
 
 	/* Filled by kernel image processing code */
+	int kernel_fd;
 	int initrd_fd;
+	char *kernel_buf;
 	char *command_line;
 	int command_line_len;
 
Signature · 7 lines
2.31.1


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

PATCH 4/5 arm64: Scatter the logic of reading of kernel file into each probe

In reply to Pingfan Liu

As more complicated capsule kernel format occurs like zboot, where the compressed kernel is stored as a payload. The straight forward decompression can not meet the demand.

So reading in the kernel file in a probe method and decide how to unfold the content by the method itself.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 kexec/arch/arm64/kexec-elf-arm64.c    |  7 ++++-
 kexec/arch/arm64/kexec-image-arm64.c  |  6 +++-
 kexec/arch/arm64/kexec-uImage-arm64.c | 17 ++++++++---
 kexec/arch/arm64/kexec-zImage-arm64.c | 13 ++++-----
 kexec/kexec.c                         | 42 ++++++++++++++++++---------
 kexec/kexec.h                         |  2 ++
 6 files changed, 60 insertions(+), 27 deletions(-)
kexec/arch/arm64/kexec-elf-arm64.c +6 -1
index 9238dd5..cedf3c6 100644
@@ -16,11 +16,13 @@
 #include "kexec-elf.h"
 #include "kexec-syscall.h"
 
-int elf_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
+int elf_arm64_probe(const char *kern_fname, off_t kernel_size, struct kexec_info *info)
 {
 	struct mem_ehdr ehdr;
+	char *kernel_buf;
 	int result;
 
+	kernel_buf = slurp_file(kern_fname, &kernel_size);
 	result = build_elf_exec_info(kernel_buf, kernel_size, &ehdr, 0);
 
 	if (result < 0) {
@@ -34,8 +36,11 @@ int elf_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info
 		goto on_exit;
 	}
 
+	info->kernel_fd = open(kern_fname, O_RDONLY);
+	info->kernel_buf = kernel_buf;
 	result = 0;
 on_exit:
+	free(kernel_buf);
 	free_elf_info(&ehdr);
 	return result;
 }
kexec/arch/arm64/kexec-image-arm64.c +5 -1
index 84aca72..90cd40a 100644
@@ -14,10 +14,12 @@
 #include "kexec-syscall.h"
 #include "arch/options.h"
 
-int image_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
+int image_arm64_probe(const char *kern_fname, off_t kernel_size, struct kexec_info *info)
 {
 	const struct arm64_image_header *h;
+	char *kernel_buf;
 
+	kernel_buf = slurp_file(kern_fname, &kernel_size);
 	if (kernel_size < sizeof(struct arm64_image_header)) {
 		dbgprintf("%s: No arm64 image header.\n", __func__);
 		return -1;
@@ -29,6 +31,8 @@ int image_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_in
 		dbgprintf("%s: Bad arm64 image header.\n", __func__);
 		return -1;
 	}
+	info->kernel_fd = open(kern_fname, O_RDONLY);
+	info->kernel_buf = kernel_buf;
 
 	return 0;
 }
kexec/arch/arm64/kexec-uImage-arm64.c +13 -4
index f5b94c8..cce1c76 100644
@@ -3,26 +3,35 @@
  */
 #include <stdint.h>
 #include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
 #include <sys/types.h>
 #include <image.h>
 #include <kexec-uImage.h>
 #include "../../kexec.h"
 #include "kexec-arm64.h"
 
-int uImage_arm64_probe(const char *buf, off_t len, struct kexec_info *info)
+int uImage_arm64_probe(const char *kern_fname, off_t len, struct kexec_info *info)
 {
 	int ret;
+	char *kernel_buf;
 
-	ret = uImage_probe_kernel(buf, len, IH_ARCH_ARM64);
+	kernel_buf = slurp_file(kern_fname, &len);
+	ret = uImage_probe_kernel(kernel_buf, len, IH_ARCH_ARM64);
 
 	/*  0 - valid uImage.
 	 * -1 - uImage is corrupted.
 	 *  1 - image is not a uImage.
 	 */
-	if (!ret)
+	if (!ret) {
+		info->kernel_fd = open(kern_fname, O_RDONLY);
+		info->kernel_buf = kernel_buf;
 		return 0;
-	else
+	}
+	else {
+		free(kernel_buf);
 		return -1;
+	}
 }
 
 int uImage_arm64_load(int argc, char **argv, const char *buf, off_t len,
kexec/arch/arm64/kexec-zImage-arm64.c +6 -7
index 7877741..450a915 100644
@@ -40,7 +40,7 @@
  * fd : File descriptor of the temp file containing the decompressed
  *      Image.
  */
-int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info)
+int zImage_arm64_probe(const char *kernel, off_t kernel_size, struct kexec_info *info)
 {
 	int ret = -1;
 	int fd = 0;
@@ -49,7 +49,7 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_i
 	char *kernel_uncompressed_buf = NULL;
 	const struct arm64_image_header *h;
 
-	if (!is_zlib_file(kernel_buf, &kernel_size)) {
+	if (!is_zlib_file(kernel, &kernel_size)) {
 		dbgprintf("%s: Not an zImage file (Image.gz).\n", __func__);
 		return -1;
 	}
@@ -69,7 +69,7 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_i
 
 	/* slurp in the input kernel */
 	dbgprintf("%s: ", __func__);
-	kernel_uncompressed_buf = slurp_decompress_file(kernel_buf,
+	kernel_uncompressed_buf = slurp_decompress_file(kernel,
 							&kernel_size);
 
 	/* check for correct header magic */
@@ -108,13 +108,12 @@ int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_i
 		ret = -1;
 		goto fail_bad_header;
 	}
-
+	info->kernel_fd = kernel_fd;
+	info->kernel_buf = kernel_uncompressed_buf;
 	unlink(fname);
-
-	free(kernel_uncompressed_buf);
 	free(fname);
 
-	return kernel_fd;
+	return 0;
 
 fail_bad_header:
 	free(kernel_uncompressed_buf);
kexec/kexec.c +28 -14

94 lines not on this page · raw message

kexec/kexec.h +2 -0
index 6e8430e..3349d16 100644
@@ -164,7 +164,9 @@ struct kexec_info {
 	unsigned long file_mode :1;
 
 	/* Filled by kernel image processing code */
+	int kernel_fd;
 	int initrd_fd;
+	char *kernel_buf;
 	char *command_line;
 	int command_line_len;
 
Signature · 7 lines
2.31.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#9 lore raw
In reply to Pingfan Liu
Quoted 7 lines · Pingfan Liu

As more complicated capsule kernel format occurs like zboot, where the compressed kernel is stored as a payload. The straight forward decompression can not meet the demand.

As the first step, on aarch64, reading in the kernel file in a probe method and decide how to unfold the content by the method itself.

Quoted 7 lines

The new designed probe interface returns two factors:
1. the parsed kernel_buf should be returned so that it can be used by the image load method later.
2. the final fd passed to sys_kexec_file_load, since aarch64 kernel can only work with Image format, the outer payload should be stripped and a temporary file of Image should be created.

Well this is unfortunate. I pinged you a few days back but I guess it must have gotten lost in the noise.

I'm ok with dropping my set, but I don't see your 5/5, and it doesn't show up in the archive either. Which presumably is the core ZBOOT functionality. From what I can tell without that, this set then is all just cleanup. So did you do that on purpose, if so maybe its best then to just rebase on top of my set?

Given I'm not a regular contributor here I'm not sure my opinion matters much, but one of the things that bothered me when I was yak shaving my set, were the #ifdef aarch64 bits in the common code. I considered attempting to remove it but that was a bit more involved than I wanted to get. But, as Ard correctly points out, the zboot is being used by more than aarch64 at this point. So that is also a argument for assuring whatever the probe()/etc interface is, that its not #ifdefed.

Quoted 28 lines

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Pingfan Liu (5):
kexec: Adding missing free for kernel_buf arm64/zImage: Remove unnecessary allocation for
kernel_uncompressed_buf
arm64: change the prototype of image probe function
arm64: Scatter the reading of kernel file into each probe
arm64: add support for zboot image

  kexec/arch/arm/kexec-arm.h            |   4 +-
  kexec/arch/arm/kexec-uImage-arm.c     |   2 +-
  kexec/arch/arm64/Makefile             |   3 +-
  kexec/arch/arm64/kexec-arm64.c        |   1 +
  kexec/arch/arm64/kexec-arm64.h        |  13 +-
  kexec/arch/arm64/kexec-elf-arm64.c    |   7 +-
  kexec/arch/arm64/kexec-image-arm64.c  |   6 +-
  kexec/arch/arm64/kexec-uImage-arm64.c |  17 +-
  kexec/arch/arm64/kexec-zImage-arm64.c |  23 +--
  kexec/arch/arm64/kexec-zboot-arm64.c  | 261 ++++++++++++++++++++++++++
  kexec/arch/arm64/zboot.h              |  26 +++
  kexec/kexec.c                         |  48 +++--
  kexec/kexec.h                         |   8 +
  13 files changed, 377 insertions(+), 42 deletions(-)
Quoted 3 lines

create mode 100644 kexec/arch/arm64/kexec-zboot-arm64.c create mode 100644 kexec/arch/arm64/zboot.h

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

#10 lore raw
Quoted 17 lines · Jeremy Linton
Quoted 7 lines · Pingfan Liu

As more complicated capsule kernel format occurs like zboot, where the compressed kernel is stored as a payload. The straight forward decompression can not meet the demand.

As the first step, on aarch64, reading in the kernel file in a probe method and decide how to unfold the content by the method itself.

Quoted 7 lines

The new designed probe interface returns two factors:
1. the parsed kernel_buf should be returned so that it can be used by the image load method later.
2. the final fd passed to sys_kexec_file_load, since aarch64 kernel can only work with Image format, the outer payload should be stripped and a temporary file of Image should be created.

Quoted 3 lines

Well this is unfortunate. I pinged you a few days back but I guess it must have gotten lost in the noise.

Sorry for that.

Quoted 6 lines

I'm ok with dropping my set, but I don't see your 5/5, and it doesn't show up in the archive either. Which presumably is the core ZBOOT functionality. From what I can tell without that, this set then is all just cleanup. So did you do that on purpose, if so maybe its best then to just rebase on top of my set?

I forgot to add the CC in that patch, and it turns out to be sent to myself only. I have re-sent it.

I am happy to rebase on your set with some adaptation to the new probe interface.

Quoted 9 lines

Given I'm not a regular contributor here I'm not sure my opinion matters much, but one of the things that bothered me when I was yak shaving my set, were the #ifdef aarch64 bits in the common code. I considered attempting to remove it but that was a bit more involved than I wanted to get. But, as Ard correctly points out, the zboot is being used by more than aarch64 at this point. So that is also a argument for assuring whatever the probe()/etc interface is, that its not #ifdefed.

Yes, it makes code look not neat. I have tried to tackle it with sed, but failed. I will try to achieve it with coccinelle.

Thanks,

Pingfan

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

#11 lore raw

PATCH 5/5 arm64: add support for zboot image

In reply to Pingfan Liu

zboot image consists of zboot_header and Image.gz. And the compressed payload should be located and parsed with extra effort. Most of important, the kernel can only work with Image, so the final fd should point to a temporary file, which contains Image.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 kexec/arch/arm64/Makefile            |   3 +-
 kexec/arch/arm64/kexec-arm64.c       |   1 +
 kexec/arch/arm64/kexec-arm64.h       |   5 +
 kexec/arch/arm64/kexec-zboot-arm64.c | 261 +++++++++++++++++++++++++++
 kexec/arch/arm64/zboot.h             |  26 +++
 5 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 kexec/arch/arm64/kexec-zboot-arm64.c
 create mode 100644 kexec/arch/arm64/zboot.h
kexec/arch/arm64/Makefile +2 -1
index d27c8ee..7826a36 100644
@@ -16,7 +16,8 @@ arm64_KEXEC_SRCS += \
 	kexec/arch/arm64/kexec-elf-arm64.c \
 	kexec/arch/arm64/kexec-uImage-arm64.c \
 	kexec/arch/arm64/kexec-image-arm64.c \
-	kexec/arch/arm64/kexec-zImage-arm64.c
+	kexec/arch/arm64/kexec-zImage-arm64.c \
+	kexec/arch/arm64/kexec-zboot-arm64.c
 
 arm64_UIMAGE = kexec/kexec-uImage.c
 
kexec/arch/arm64/kexec-arm64.c +1 -0
index ec6df4b..88b5f57 100644
@@ -72,6 +72,7 @@ const struct arch_map_entry arches[] = {
 
 struct file_type file_type[] = {
 	{"vmlinux", elf_arm64_probe, elf_arm64_load, elf_arm64_usage},
+	{"zboot", zboot_arm64_probe, zboot_arm64_load, zboot_arm64_usage},
 	{"Image", image_arm64_probe, image_arm64_load, image_arm64_usage},
 	{"uImage", uImage_arm64_probe, uImage_arm64_load, uImage_arm64_usage},
 	{"zImage", zImage_arm64_probe, zImage_arm64_load, zImage_arm64_usage},
kexec/arch/arm64/kexec-arm64.h +5 -0
index 88bb508..98e1be9 100644
@@ -50,6 +50,11 @@ int zImage_arm64_load(int argc, char **argv, const char *kernel_buf,
 void zImage_arm64_usage(void);
 
 
+int zboot_arm64_probe(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);
+int zboot_arm64_load(int argc, char **argv, const char *kernel_buf,
+	off_t kernel_size, struct kexec_info *info);
+void zboot_arm64_usage(void);
+
 extern off_t initrd_base;
 extern off_t initrd_size;
 
kexec/arch/arm64/kexec-zboot-arm64.c new file +261 -0

261 lines not on this page · raw message

kexec/arch/arm64/zboot.h new file +26 -0
new file mode 100644
index 0000000..fa97aa3
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ZBOOT_H
+#define ZBOOT_H
+
+struct zboot_image_header {
+	union {
+		struct {
+			uint32_t magic;
+			/* image type, .ascii "zimg" */
+			char zimg[4];
+			int32_t gzdata_offset;
+			int32_t gzdata_size;
+			int32_t reserved[2];
+			/* compression type, .asciz */
+			char comp_type[];
+		};
+		struct {
+			char pad[56];
+		};
+	};
+	/* 0x818223cd */
+	uint32_t linux_pe_magic;
+	int32_t pe_header_offset;
+} __packed;
+
+#endif
Signature · 7 lines
2.31.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#12 lore raw
In reply to Pingfan Liu

I have rebased Jeremy's V2 (which is off-list) onto my new code.

The whole series since commit a8de94e:
LoongArch: kdump: Set up kernel image segment
is available in the Git repository
at https://github.com/pfliu/kexec-tools/tree/zboot
up to the head

I have compiled it on x86_64 and aarch64, and successfully booted the zboot image on aarch64

@Simon, is there any instruction to do the cross-building for kexec-tools, so I can verify the pattern substitution works well on other arches. I think I can post it to the list with a cover-letter after verification.

Thanks,

Pingfan

Quoted 46 lines · Pingfan Liu

As more complicated capsule kernel format occurs like zboot, where the compressed kernel is stored as a payload. The straight forward decompression can not meet the demand.

As the first step, on aarch64, reading in the kernel file in a probe method and decide how to unfold the content by the method itself.

The new designed probe interface returns two factors:
1. the parsed kernel_buf should be returned so that it can be used by the image load method later.
2. the final fd passed to sys_kexec_file_load, since aarch64 kernel can only work with Image format, the outer payload should be stripped and a temporary file of Image should be created.

To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Pingfan Liu (5):
kexec: Adding missing free for kernel_buf arm64/zImage: Remove unnecessary allocation for
kernel_uncompressed_buf
arm64: change the prototype of image probe function
arm64: Scatter the reading of kernel file into each probe
arm64: add support for zboot image

 kexec/arch/arm/kexec-arm.h            |   4 +-
 kexec/arch/arm/kexec-uImage-arm.c     |   2 +-
 kexec/arch/arm64/Makefile             |   3 +-
 kexec/arch/arm64/kexec-arm64.c        |   1 +
 kexec/arch/arm64/kexec-arm64.h        |  13 +-
 kexec/arch/arm64/kexec-elf-arm64.c    |   7 +-
 kexec/arch/arm64/kexec-image-arm64.c  |   6 +-
 kexec/arch/arm64/kexec-uImage-arm64.c |  17 +-
 kexec/arch/arm64/kexec-zImage-arm64.c |  23 +--
 kexec/arch/arm64/kexec-zboot-arm64.c  | 261 ++++++++++++++++++++++++++
 kexec/arch/arm64/zboot.h              |  26 +++
 kexec/kexec.c                         |  48 +++--
 kexec/kexec.h                         |   8 +
 13 files changed, 377 insertions(+), 42 deletions(-)
 create mode 100644 kexec/arch/arm64/kexec-zboot-arm64.c
 create mode 100644 kexec/arch/arm64/zboot.h
Quoted 3 lines

--
2.31.1

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