Blame SOURCES/kexec-tools-2.0.19-kexec-arm64-Add-support-for-handling-zlib-compressed.patch

603de6
From f4c1caaa97a2e019ccc00ab63e390c60d1c9454c Mon Sep 17 00:00:00 2001
603de6
From: Bhupesh Sharma <bhsharma@redhat.com>
603de6
Date: Mon, 15 Jul 2019 11:32:56 +0530
603de6
Subject: [PATCH 4/5] kexec/arm64: Add support for handling zlib compressed
603de6
 (Image.gz) image
603de6
603de6
Currently the kexec_file_load() support for arm64 doesn't allow
603de6
handling zlib compressed (i.e. Image.gz) image.
603de6
603de6
Since most distributions use 'make zinstall' rule inside
603de6
'arch/arm64/boot/Makefile' to install the arm64
603de6
Image.gz compressed file inside the boot destination directory (for e.g.
603de6
/boot), currently we cannot use kexec_file_load() to load vmlinuz (or
603de6
Image.gz):
603de6
603de6
 # file /boot/vmlinuz
603de6
   /boot/vmlinuz: gzip compressed data, was "Image", <..snip..>, max
603de6
   compression, from Unix, original size 21945120
603de6
603de6
 Now, since via kexec_file_load() we pass the 'fd' of Image.gz
603de6
 (compressed file) via the following command line ...
603de6
603de6
 # kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname
603de6
   -r`.img --reuse-cmdline
603de6
603de6
... kernel returns -EINVAL error value, as it is not able to locate
603de6
the magic number =0x644d5241, which is expected in the 64-byte header
603de6
of the decompressed kernel image.
603de6
603de6
We can fix this in user-space kexec-tools, which handles an
603de6
'Image.gz' being passed via kexec_file_load(), using an approach
603de6
as follows:
603de6
603de6
a). Copy the contents of Image.gz to a temporary file.
603de6
b). Decompress (gunzip-decompress) the contents inside the
603de6
    temporary file.
603de6
c). Pass the 'fd' of the temporary file to the kernel space. So
603de6
    basically the kernel space still gets a decompressed kernel
603de6
    image to load via kexec-tools
603de6
603de6
I tested this patch for the following three use-cases:
603de6
603de6
1. Uncompressed Image file:
603de6
 #kexec -s -l Image --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline
603de6
603de6
2. Signed Image file:
603de6
 #kexec -s -l Image.signed --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline
603de6
603de6
3. zlib compressed Image.gz file:
603de6
 #kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline
603de6
603de6
Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com>
603de6
Signed-off-by: Simon Horman <horms@verge.net.au>
603de6
---
603de6
 kexec/arch/arm64/Makefile             |   3 +-
603de6
 kexec/arch/arm64/kexec-arm64.c        |   1 +
603de6
 kexec/arch/arm64/kexec-arm64.h        |   7 ++
603de6
 kexec/arch/arm64/kexec-image-arm64.c  |   4 +-
603de6
 kexec/arch/arm64/kexec-zImage-arm64.c | 226 ++++++++++++++++++++++++++++++++++
603de6
 kexec/kexec.c                         |  12 ++
603de6
 6 files changed, 250 insertions(+), 3 deletions(-)
603de6
 create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c
603de6
603de6
diff --git a/kexec/arch/arm64/Makefile b/kexec/arch/arm64/Makefile
603de6
index 9d9111caa8ed..d27c8ee1b5e7 100644
603de6
--- a/kexec/arch/arm64/Makefile
603de6
+++ b/kexec/arch/arm64/Makefile
603de6
@@ -15,7 +15,8 @@ arm64_KEXEC_SRCS += \
603de6
 	kexec/arch/arm64/kexec-arm64.c \
603de6
 	kexec/arch/arm64/kexec-elf-arm64.c \
603de6
 	kexec/arch/arm64/kexec-uImage-arm64.c \
603de6
-	kexec/arch/arm64/kexec-image-arm64.c
603de6
+	kexec/arch/arm64/kexec-image-arm64.c \
603de6
+	kexec/arch/arm64/kexec-zImage-arm64.c
603de6
 
603de6
 arm64_UIMAGE = kexec/kexec-uImage.c
603de6
 
603de6
diff --git a/kexec/arch/arm64/kexec-arm64.c b/kexec/arch/arm64/kexec-arm64.c
603de6
index 2992bce8139f..eb3a3a37307c 100644
603de6
--- a/kexec/arch/arm64/kexec-arm64.c
603de6
+++ b/kexec/arch/arm64/kexec-arm64.c
603de6
@@ -71,6 +71,7 @@ struct file_type file_type[] = {
603de6
 	{"vmlinux", elf_arm64_probe, elf_arm64_load, elf_arm64_usage},
603de6
 	{"Image", image_arm64_probe, image_arm64_load, image_arm64_usage},
603de6
 	{"uImage", uImage_arm64_probe, uImage_arm64_load, uImage_arm64_usage},
603de6
+	{"zImage", zImage_arm64_probe, zImage_arm64_load, zImage_arm64_usage},
603de6
 };
603de6
 
603de6
 int file_types = sizeof(file_type) / sizeof(file_type[0]);
603de6
diff --git a/kexec/arch/arm64/kexec-arm64.h b/kexec/arch/arm64/kexec-arm64.h
603de6
index cc3419f4c10f..628de79b7f70 100644
603de6
--- a/kexec/arch/arm64/kexec-arm64.h
603de6
+++ b/kexec/arch/arm64/kexec-arm64.h
603de6
@@ -38,11 +38,18 @@ int image_arm64_probe(const char *kernel_buf, off_t kernel_size);
603de6
 int image_arm64_load(int argc, char **argv, const char *kernel_buf,
603de6
 	off_t kernel_size, struct kexec_info *info);
603de6
 void image_arm64_usage(void);
603de6
+
603de6
 int uImage_arm64_probe(const char *buf, off_t len);
603de6
 int uImage_arm64_load(int argc, char **argv, const char *buf, off_t len,
603de6
 		      struct kexec_info *info);
603de6
 void uImage_arm64_usage(void);
603de6
 
603de6
+int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size);
603de6
+int zImage_arm64_load(int argc, char **argv, const char *kernel_buf,
603de6
+	off_t kernel_size, struct kexec_info *info);
603de6
+void zImage_arm64_usage(void);
603de6
+
603de6
+
603de6
 off_t initrd_base;
603de6
 off_t initrd_size;
603de6
 
603de6
diff --git a/kexec/arch/arm64/kexec-image-arm64.c b/kexec/arch/arm64/kexec-image-arm64.c
603de6
index 685a99352e39..aa8f2e22d72b 100644
603de6
--- a/kexec/arch/arm64/kexec-image-arm64.c
603de6
+++ b/kexec/arch/arm64/kexec-image-arm64.c
603de6
@@ -114,6 +114,6 @@ exit:
603de6
 void image_arm64_usage(void)
603de6
 {
603de6
 	printf(
603de6
-"     An ARM64 binary image, compressed or not, big or little endian.\n"
603de6
-"     Typically an Image, Image.gz or Image.lzma file.\n\n");
603de6
+"     An ARM64 binary image, uncompressed, big or little endian.\n"
603de6
+"     Typically an Image file.\n\n");
603de6
 }
603de6
diff --git a/kexec/arch/arm64/kexec-zImage-arm64.c b/kexec/arch/arm64/kexec-zImage-arm64.c
603de6
new file mode 100644
603de6
index 000000000000..6ee82ff6c79e
603de6
--- /dev/null
603de6
+++ b/kexec/arch/arm64/kexec-zImage-arm64.c
603de6
@@ -0,0 +1,226 @@
603de6
+/*
603de6
+ * ARM64 kexec zImage (Image.gz) support.
603de6
+ *
603de6
+ * Several distros use 'make zinstall' rule inside
603de6
+ * 'arch/arm64/boot/Makefile' to install the arm64
603de6
+ * Image.gz compressed file inside the boot destination
603de6
+ * directory (for e.g. /boot).
603de6
+ *
603de6
+ * Currently we cannot use kexec_file_load() to load vmlinuz
603de6
+ * (or Image.gz).
603de6
+ *
603de6
+ * To support Image.gz, we should:
603de6
+ * a). Copy the contents of Image.gz to a temporary file.
603de6
+ * b). Decompress (gunzip-decompress) the contents inside the
603de6
+ *     temporary file.
603de6
+ * c). Pass the 'fd' of the temporary file to the kernel space.
603de6
+ *
603de6
+ * So basically the kernel space still gets a decompressed
603de6
+ * kernel image to load via kexec-tools.
603de6
+ */
603de6
+
603de6
+#define _GNU_SOURCE
603de6
+
603de6
+#include <errno.h>
603de6
+#include <fcntl.h>
603de6
+#include <limits.h>
603de6
+#include <stdlib.h>
603de6
+#include "crashdump-arm64.h"
603de6
+#include "image-header.h"
603de6
+#include "kexec.h"
603de6
+#include "kexec-arm64.h"
603de6
+#include "kexec-syscall.h"
603de6
+#include "kexec-zlib.h"
603de6
+#include "arch/options.h"
603de6
+
603de6
+#define FILENAME_IMAGE		"/tmp/ImageXXXXXX"
603de6
+
603de6
+/* Returns:
603de6
+ * -1 : in case of error/invalid format (not a valid Image.gz format.
603de6
+ * fd : File descriptor of the temp file containing the decompressed
603de6
+ *      Image.
603de6
+ */
603de6
+int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size)
603de6
+{
603de6
+	int ret = -1;
603de6
+	int fd = 0;
603de6
+	int kernel_fd = 0;
603de6
+	char *fname = NULL;
603de6
+	char *kernel_uncompressed_buf = NULL;
603de6
+	const struct arm64_image_header *h;
603de6
+
603de6
+	if (!is_zlib_file(kernel_buf, &kernel_size)) {
603de6
+		dbgprintf("%s: Not an zImage file (Image.gz).\n", __func__);
603de6
+		return -1;
603de6
+	}
603de6
+
603de6
+	if (!(fname = strdup(FILENAME_IMAGE))) {
603de6
+		dbgprintf("%s: Can't duplicate strings %s\n", __func__,
603de6
+				fname);
603de6
+		return -1;
603de6
+	}
603de6
+
603de6
+	if ((fd = mkstemp(fname)) < 0) {
603de6
+		dbgprintf("%s: Can't open file %s\n", __func__,
603de6
+				fname);
603de6
+		ret = -1;
603de6
+		goto fail_mkstemp;
603de6
+	}
603de6
+
603de6
+	kernel_uncompressed_buf =
603de6
+		(char *) calloc(kernel_size, sizeof(off_t));
603de6
+	if (!kernel_uncompressed_buf) {
603de6
+		dbgprintf("%s: Can't calloc %ld bytes\n",
603de6
+				__func__, kernel_size);
603de6
+		ret= -ENOMEM;
603de6
+		goto fail_calloc;
603de6
+	}
603de6
+
603de6
+	/* slurp in the input kernel */
603de6
+	dbgprintf("%s: ", __func__);
603de6
+	kernel_uncompressed_buf = slurp_decompress_file(kernel_buf,
603de6
+							&kernel_size);
603de6
+
603de6
+	/* check for correct header magic */
603de6
+	if (kernel_size < sizeof(struct arm64_image_header)) {
603de6
+		dbgprintf("%s: No arm64 image header.\n", __func__);
603de6
+		ret = -1;
603de6
+		goto fail_bad_header;
603de6
+	}
603de6
+
603de6
+	h = (const struct arm64_image_header *)(kernel_uncompressed_buf);
603de6
+
603de6
+	if (!arm64_header_check_magic(h)) {
603de6
+		dbgprintf("%s: Bad arm64 image header.\n", __func__);
603de6
+		ret = -1;
603de6
+		goto fail_bad_header;
603de6
+	}
603de6
+
603de6
+	if (write(fd, kernel_uncompressed_buf,
603de6
+				kernel_size) != kernel_size) {
603de6
+		dbgprintf("%s: Can't write the uncompressed file %s\n",
603de6
+				__func__, fname);
603de6
+		ret = -1;
603de6
+		goto fail_bad_header;
603de6
+	}
603de6
+
603de6
+	close(fd);
603de6
+
603de6
+	/* Open the tmp file again, this time in O_RDONLY mode, as
603de6
+	 * opening the file in O_RDWR and calling kexec_file_load()
603de6
+	 * causes the kernel to return -ETXTBSY
603de6
+	 */
603de6
+	kernel_fd = open(fname, O_RDONLY);
603de6
+	if (kernel_fd == -1) {
603de6
+		dbgprintf("%s: Failed to open file %s\n",
603de6
+				__func__, fname);
603de6
+		ret = -1;
603de6
+		goto fail_bad_header;
603de6
+	}
603de6
+
603de6
+	unlink(fname);
603de6
+
603de6
+	free(kernel_uncompressed_buf);
603de6
+	free(fname);
603de6
+
603de6
+	return kernel_fd;
603de6
+
603de6
+fail_bad_header:
603de6
+	free(kernel_uncompressed_buf);
603de6
+
603de6
+fail_calloc:
603de6
+	if (fd >= 0)
603de6
+		close(fd);
603de6
+
603de6
+	unlink(fname);
603de6
+
603de6
+fail_mkstemp:
603de6
+	free(fname);
603de6
+
603de6
+	return ret;
603de6
+}
603de6
+
603de6
+int zImage_arm64_load(int argc, char **argv, const char *kernel_buf,
603de6
+	off_t kernel_size, struct kexec_info *info)
603de6
+{
603de6
+	const struct arm64_image_header *header;
603de6
+	unsigned long kernel_segment;
603de6
+	int result;
603de6
+
603de6
+	if (info->file_mode) {
603de6
+		if (arm64_opts.initrd) {
603de6
+			info->initrd_fd = open(arm64_opts.initrd, O_RDONLY);
603de6
+			if (info->initrd_fd == -1) {
603de6
+				fprintf(stderr,
603de6
+					"Could not open initrd file %s:%s\n",
603de6
+					arm64_opts.initrd, strerror(errno));
603de6
+				result = EFAILED;
603de6
+				goto exit;
603de6
+			}
603de6
+		}
603de6
+
603de6
+		if (arm64_opts.command_line) {
603de6
+			info->command_line = (char *)arm64_opts.command_line;
603de6
+			info->command_line_len =
603de6
+					strlen(arm64_opts.command_line) + 1;
603de6
+		}
603de6
+
603de6
+		return 0;
603de6
+	}
603de6
+
603de6
+	header = (const struct arm64_image_header *)(kernel_buf);
603de6
+
603de6
+	if (arm64_process_image_header(header))
603de6
+		return EFAILED;
603de6
+
603de6
+	kernel_segment = arm64_locate_kernel_segment(info);
603de6
+
603de6
+	if (kernel_segment == ULONG_MAX) {
603de6
+		dbgprintf("%s: Kernel segment is not allocated\n", __func__);
603de6
+		result = EFAILED;
603de6
+		goto exit;
603de6
+	}
603de6
+
603de6
+	dbgprintf("%s: kernel_segment: %016lx\n", __func__, kernel_segment);
603de6
+	dbgprintf("%s: text_offset:    %016lx\n", __func__,
603de6
+		arm64_mem.text_offset);
603de6
+	dbgprintf("%s: image_size:     %016lx\n", __func__,
603de6
+		arm64_mem.image_size);
603de6
+	dbgprintf("%s: phys_offset:    %016lx\n", __func__,
603de6
+		arm64_mem.phys_offset);
603de6
+	dbgprintf("%s: vp_offset:      %016lx\n", __func__,
603de6
+		arm64_mem.vp_offset);
603de6
+	dbgprintf("%s: PE format:      %s\n", __func__,
603de6
+		(arm64_header_check_pe_sig(header) ? "yes" : "no"));
603de6
+
603de6
+	/* create and initialize elf core header segment */
603de6
+	if (info->kexec_flags & KEXEC_ON_CRASH) {
603de6
+		result = load_crashdump_segments(info);
603de6
+		if (result) {
603de6
+			dbgprintf("%s: Creating eflcorehdr failed.\n",
603de6
+								__func__);
603de6
+			goto exit;
603de6
+		}
603de6
+	}
603de6
+
603de6
+	/* load the kernel */
603de6
+	add_segment_phys_virt(info, kernel_buf, kernel_size,
603de6
+			kernel_segment + arm64_mem.text_offset,
603de6
+			arm64_mem.image_size, 0);
603de6
+
603de6
+	/* load additional data */
603de6
+	result = arm64_load_other_segments(info, kernel_segment
603de6
+		+ arm64_mem.text_offset);
603de6
+
603de6
+exit:
603de6
+	if (result)
603de6
+		fprintf(stderr, "kexec: load failed.\n");
603de6
+	return result;
603de6
+}
603de6
+
603de6
+void zImage_arm64_usage(void)
603de6
+{
603de6
+	printf(
603de6
+"     An ARM64 zImage, compressed, big or little endian.\n"
603de6
+"     Typically an Image.gz or Image.lzma file.\n\n");
603de6
+}
603de6
diff --git a/kexec/kexec.c b/kexec/kexec.c
603de6
index 8ca3b457cac8..bc6ab3dbd10b 100644
603de6
--- a/kexec/kexec.c
603de6
+++ b/kexec/kexec.c
603de6
@@ -1206,8 +1206,20 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
603de6
 	kernel_buf = slurp_decompress_file(kernel, &kernel_size);
603de6
 
603de6
 	for (i = 0; i < file_types; i++) {
603de6
+#ifdef __aarch64__
603de6
+		/* handle Image.gz like cases */
603de6
+		if (is_zlib_file(kernel, &kernel_size)) {
603de6
+			if ((ret = file_type[i].probe(kernel, kernel_size)) >= 0) {
603de6
+				kernel_fd = ret;
603de6
+				break;
603de6
+			}
603de6
+		} else
603de6
+			if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
603de6
+				break;
603de6
+#else
603de6
 		if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
603de6
 			break;
603de6
+#endif
603de6
 	}
603de6
 
603de6
 	if (i == file_types) {
603de6
-- 
603de6
2.7.4
603de6