* [PATCH 0/4] kexec/arm64: Add support for zlib compressed kernel images (Image.gz)
@ 2019-07-10 19:54 Bhupesh Sharma
2019-07-10 19:54 ` [PATCH 1/4] kexec/kexec.c: Add the missing close() for fd used for kexec_file_load() Bhupesh Sharma
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Bhupesh Sharma @ 2019-07-10 19:54 UTC (permalink / raw)
To: kexec; +Cc: bhupesh.linux, bhsharma, horms, takahiro.akashi
This patchset adds the support for zlib compressed kernel images
(Image.gz) for arm64 kexec-tools.
This was discussed a bit with the arm64 kernel maintainers (see [0]) and
after discussing the pros and cons of adding this support in
kernel-space v/s in user-space, we decided it makes more sense to add
this support to the user-space kexec-tools.
Note that this series adds support for unsigned arm64 Image.gz files
only as signing an Image.gz type file is not a easy implementation from
kexec-tools p-o-v. Also even standard signing tools like sbsign fail to
recongize the Image.gz format:
$ sbsign --key certs/signing_key.pem --cert certs/signing_key.pem Image.gz
Invalid DOS header magic
... because that gzip file isn't a PE32+ that can be loaded by UEFI.
So I will work on the same in coming days and try to send a RFC
patchset for the same later on.
[0]. http://lists.infradead.org/pipermail/kexec/2019-June/023156.html
Bhupesh Sharma (4):
kexec/kexec.c: Add the missing close() for fd used for
kexec_file_load()
kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe()
kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function
kexec/arm64: Add support for handling zlib compressed (Image.gz) image
kexec/arch/arm64/Makefile | 3 +-
kexec/arch/arm64/kexec-arm64.c | 1 +
kexec/arch/arm64/kexec-arm64.h | 7 ++
kexec/arch/arm64/kexec-image-arm64.c | 4 +-
kexec/arch/arm64/kexec-uImage-arm64.c | 13 ++-
kexec/arch/arm64/kexec-zImage-arm64.c | 199 ++++++++++++++++++++++++++++++++++
kexec/kexec-zlib.h | 1 +
kexec/kexec.c | 14 +++
kexec/zlib.c | 32 ++++++
9 files changed, 270 insertions(+), 4 deletions(-)
create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c
--
2.7.4
_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 1/4] kexec/kexec.c: Add the missing close() for fd used for kexec_file_load() 2019-07-10 19:54 [PATCH 0/4] kexec/arm64: Add support for zlib compressed kernel images (Image.gz) Bhupesh Sharma @ 2019-07-10 19:54 ` Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 2/4] kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe() Bhupesh Sharma ` (2 subsequent siblings) 3 siblings, 0 replies; 9+ messages in thread From: Bhupesh Sharma @ 2019-07-10 19:54 UTC (permalink / raw) To: kexec; +Cc: bhupesh.linux, bhsharma, horms, takahiro.akashi In kexec/kexec.c, we open() the kernel Image file and pass this file descriptor to the kexec_file_load() system call, but never call a corresponding close(). Fix the same via this patch. Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> --- kexec/kexec.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kexec/kexec.c b/kexec/kexec.c index 32ae56c8f1c3..8ca3b457cac8 100644 --- a/kexec/kexec.c +++ b/kexec/kexec.c @@ -1234,6 +1234,8 @@ static int do_kexec_file_load(int fileind, int argc, char **argv, if (ret != 0) fprintf(stderr, "kexec_file_load failed: %s\n", strerror(errno)); + + close(kernel_fd); return ret; } -- 2.7.4 _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/4] kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe() 2019-07-10 19:54 [PATCH 0/4] kexec/arm64: Add support for zlib compressed kernel images (Image.gz) Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 1/4] kexec/kexec.c: Add the missing close() for fd used for kexec_file_load() Bhupesh Sharma @ 2019-07-10 19:54 ` Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 3/4] kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 4/4] kexec/arm64: Add support for handling zlib compressed (Image.gz) image Bhupesh Sharma 3 siblings, 0 replies; 9+ messages in thread From: Bhupesh Sharma @ 2019-07-10 19:54 UTC (permalink / raw) To: kexec; +Cc: bhupesh.linux, bhsharma, horms, takahiro.akashi Commit bf06cf2095e1 ("kexec/uImage: probe to identify a corrupted image"), defined the 'uImage_probe_kernel()' function return values and correspondingly ;uImage_arm64_probe()' returns the same (0 -> If the image is valid 'type' image, -1 -> If the image is corrupted and 1 -> If the image is not a uImage). This causes issues because, in later patches we introduce zImage support for arm64, and since it is probed after uImage, the return values from 'uImage_arm64_probe()' needs to be fixed to make sure that kexec will not return with an invalid error code. Now, 'uImage_arm64_probe()' returns the following values instead: 0 - valid uImage. -1 - uImage is corrupted. 1 - image is not a uImage. Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> --- kexec/arch/arm64/kexec-uImage-arm64.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/kexec/arch/arm64/kexec-uImage-arm64.c b/kexec/arch/arm64/kexec-uImage-arm64.c index 126ea9c2555b..c4669131b667 100644 --- a/kexec/arch/arm64/kexec-uImage-arm64.c +++ b/kexec/arch/arm64/kexec-uImage-arm64.c @@ -11,7 +11,18 @@ int uImage_arm64_probe(const char *buf, off_t len) { - return uImage_probe_kernel(buf, len, IH_ARCH_ARM64); + int ret; + + ret = uImage_probe_kernel(buf, len, IH_ARCH_ARM64); + + /* 0 - valid uImage. + * -1 - uImage is corrupted. + * 1 - image is not a uImage. + */ + if (!ret) + return 0; + else + return -1; } int uImage_arm64_load(int argc, char **argv, const char *buf, off_t len, -- 2.7.4 _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/4] kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function 2019-07-10 19:54 [PATCH 0/4] kexec/arm64: Add support for zlib compressed kernel images (Image.gz) Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 1/4] kexec/kexec.c: Add the missing close() for fd used for kexec_file_load() Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 2/4] kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe() Bhupesh Sharma @ 2019-07-10 19:54 ` Bhupesh Sharma 2019-07-11 9:58 ` Simon Horman 2019-07-10 19:54 ` [PATCH 4/4] kexec/arm64: Add support for handling zlib compressed (Image.gz) image Bhupesh Sharma 3 siblings, 1 reply; 9+ messages in thread From: Bhupesh Sharma @ 2019-07-10 19:54 UTC (permalink / raw) To: kexec; +Cc: bhupesh.linux, bhsharma, horms, takahiro.akashi This patch adds 'is_zlib_file()' helper function which can be used to quickly determine with the passed kernel image is a zlib compressed kernel image. This is specifically useful for arm64 zImage (or Image.gz) support, which is introduced by later patches in this patchset. Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> --- kexec/kexec-zlib.h | 1 + kexec/zlib.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/kexec/kexec-zlib.h b/kexec/kexec-zlib.h index 43c107bf4a72..16300f294759 100644 --- a/kexec/kexec-zlib.h +++ b/kexec/kexec-zlib.h @@ -6,5 +6,6 @@ #include "config.h" +int is_zlib_file(const char *filename, off_t *r_size); char *zlib_decompress_file(const char *filename, off_t *r_size); #endif /* __KEXEC_ZLIB_H */ diff --git a/kexec/zlib.c b/kexec/zlib.c index 95b608059d41..34d5ca566769 100644 --- a/kexec/zlib.c +++ b/kexec/zlib.c @@ -23,6 +23,32 @@ static void _gzerror(gzFile fp, int *errnum, const char **errmsg) } } +int is_zlib_file(const char *filename, off_t *r_size) +{ + gzFile fp; + int errnum; + const char *msg; + + if (!filename) + goto out; + + fp = gzopen(filename, "rb"); + if (fp == 0) { + _gzerror(fp, &errnum, &msg); + dbgprintf("Cannot open `%s': %s\n", filename, msg); + goto out; + } + + if (gzdirect(fp)) + /* It's not in gzip format */ + goto out; + + /* It's in gzip format */ + return 1; +out: + return 0; +} + char *zlib_decompress_file(const char *filename, off_t *r_size) { gzFile fp; @@ -84,6 +110,12 @@ fail: return buf; } #else + +int is_zlib_file(const char *filename, off_t *r_size) +{ + return 0; +} + char *zlib_decompress_file(const char *UNUSED(filename), off_t *UNUSED(r_size)) { return NULL; -- 2.7.4 _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 3/4] kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function 2019-07-10 19:54 ` [PATCH 3/4] kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function Bhupesh Sharma @ 2019-07-11 9:58 ` Simon Horman 2019-07-12 8:06 ` Bhupesh Sharma 0 siblings, 1 reply; 9+ messages in thread From: Simon Horman @ 2019-07-11 9:58 UTC (permalink / raw) To: Bhupesh Sharma; +Cc: takahiro.akashi, bhupesh.linux, kexec On Thu, Jul 11, 2019 at 01:24:28AM +0530, Bhupesh Sharma wrote: > This patch adds 'is_zlib_file()' helper function which can be > used to quickly determine with the passed kernel image is a zlib > compressed kernel image. > > This is specifically useful for arm64 zImage (or Image.gz) support, > which is introduced by later patches in this patchset. > > Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> > --- > kexec/kexec-zlib.h | 1 + > kexec/zlib.c | 32 ++++++++++++++++++++++++++++++++ > 2 files changed, 33 insertions(+) > > diff --git a/kexec/kexec-zlib.h b/kexec/kexec-zlib.h > index 43c107bf4a72..16300f294759 100644 > --- a/kexec/kexec-zlib.h > +++ b/kexec/kexec-zlib.h > @@ -6,5 +6,6 @@ > > #include "config.h" > > +int is_zlib_file(const char *filename, off_t *r_size); > char *zlib_decompress_file(const char *filename, off_t *r_size); > #endif /* __KEXEC_ZLIB_H */ > diff --git a/kexec/zlib.c b/kexec/zlib.c > index 95b608059d41..34d5ca566769 100644 > --- a/kexec/zlib.c > +++ b/kexec/zlib.c > @@ -23,6 +23,32 @@ static void _gzerror(gzFile fp, int *errnum, const char **errmsg) > } > } > > +int is_zlib_file(const char *filename, off_t *r_size) > +{ > + gzFile fp; > + int errnum; > + const char *msg; > + > + if (!filename) > + goto out; > + > + fp = gzopen(filename, "rb"); Does fp need to be closed somewhere to avoid a leak? > + if (fp == 0) { > + _gzerror(fp, &errnum, &msg); > + dbgprintf("Cannot open `%s': %s\n", filename, msg); > + goto out; > + } > + > + if (gzdirect(fp)) > + /* It's not in gzip format */ > + goto out; > + > + /* It's in gzip format */ > + return 1; > +out: > + return 0; > +} > + > char *zlib_decompress_file(const char *filename, off_t *r_size) > { > gzFile fp; > @@ -84,6 +110,12 @@ fail: > return buf; > } > #else > + > +int is_zlib_file(const char *filename, off_t *r_size) > +{ > + return 0; > +} > + > char *zlib_decompress_file(const char *UNUSED(filename), off_t *UNUSED(r_size)) > { > return NULL; > -- > 2.7.4 > _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 3/4] kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function 2019-07-11 9:58 ` Simon Horman @ 2019-07-12 8:06 ` Bhupesh Sharma 0 siblings, 0 replies; 9+ messages in thread From: Bhupesh Sharma @ 2019-07-12 8:06 UTC (permalink / raw) To: Simon Horman; +Cc: AKASHI Takahiro, Bhupesh SHARMA, kexec mailing list Hi Simon, On Thu, Jul 11, 2019 at 3:28 PM Simon Horman <horms@verge.net.au> wrote: > > On Thu, Jul 11, 2019 at 01:24:28AM +0530, Bhupesh Sharma wrote: > > This patch adds 'is_zlib_file()' helper function which can be > > used to quickly determine with the passed kernel image is a zlib > > compressed kernel image. > > > > This is specifically useful for arm64 zImage (or Image.gz) support, > > which is introduced by later patches in this patchset. > > > > Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> > > --- > > kexec/kexec-zlib.h | 1 + > > kexec/zlib.c | 32 ++++++++++++++++++++++++++++++++ > > 2 files changed, 33 insertions(+) > > > > diff --git a/kexec/kexec-zlib.h b/kexec/kexec-zlib.h > > index 43c107bf4a72..16300f294759 100644 > > --- a/kexec/kexec-zlib.h > > +++ b/kexec/kexec-zlib.h > > @@ -6,5 +6,6 @@ > > > > #include "config.h" > > > > +int is_zlib_file(const char *filename, off_t *r_size); > > char *zlib_decompress_file(const char *filename, off_t *r_size); > > #endif /* __KEXEC_ZLIB_H */ > > diff --git a/kexec/zlib.c b/kexec/zlib.c > > index 95b608059d41..34d5ca566769 100644 > > --- a/kexec/zlib.c > > +++ b/kexec/zlib.c > > @@ -23,6 +23,32 @@ static void _gzerror(gzFile fp, int *errnum, const char **errmsg) > > } > > } > > > > +int is_zlib_file(const char *filename, off_t *r_size) > > +{ > > + gzFile fp; > > + int errnum; > > + const char *msg; > > + > > + if (!filename) > > + goto out; > > + > > + fp = gzopen(filename, "rb"); > > Does fp need to be closed somewhere to avoid a leak? Right. Will fix this in v2. v2 will be soon on its way. Thanks, Bhupesh > > + if (fp == 0) { > > + _gzerror(fp, &errnum, &msg); > > + dbgprintf("Cannot open `%s': %s\n", filename, msg); > > + goto out; > > + } > > + > > + if (gzdirect(fp)) > > + /* It's not in gzip format */ > > + goto out; > > + > > + /* It's in gzip format */ > > + return 1; > > +out: > > + return 0; > > +} > > + > > char *zlib_decompress_file(const char *filename, off_t *r_size) > > { > > gzFile fp; > > @@ -84,6 +110,12 @@ fail: > > return buf; > > } > > #else > > + > > +int is_zlib_file(const char *filename, off_t *r_size) > > +{ > > + return 0; > > +} > > + > > char *zlib_decompress_file(const char *UNUSED(filename), off_t *UNUSED(r_size)) > > { > > return NULL; > > -- > > 2.7.4 > > _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 4/4] kexec/arm64: Add support for handling zlib compressed (Image.gz) image 2019-07-10 19:54 [PATCH 0/4] kexec/arm64: Add support for zlib compressed kernel images (Image.gz) Bhupesh Sharma ` (2 preceding siblings ...) 2019-07-10 19:54 ` [PATCH 3/4] kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function Bhupesh Sharma @ 2019-07-10 19:54 ` Bhupesh Sharma 2019-07-11 10:08 ` Simon Horman 3 siblings, 1 reply; 9+ messages in thread From: Bhupesh Sharma @ 2019-07-10 19:54 UTC (permalink / raw) To: kexec; +Cc: bhupesh.linux, bhsharma, horms, takahiro.akashi Currently the kexec_file_load() support for arm64 doesn't allow handling zlib compressed (i.e. Image.gz) image. Since most distributions use 'make zinstall' rule inside 'arch/arm64/boot/Makefile' to install the arm64 Image.gz compressed file inside the boot destination directory (for e.g. /boot), currently we cannot use kexec_file_load() to load vmlinuz (or Image.gz): # file /boot/vmlinuz /boot/vmlinuz: gzip compressed data, was "Image", <..snip..>, max compression, from Unix, original size 21945120 Now, since via kexec_file_load() we pass the 'fd' of Image.gz (compressed file) via the following command line ... # kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline ... kernel returns -EINVAL error value, as it is not able to locate the magic number =0x644d5241, which is expected in the 64-byte header of the decompressed kernel image. We can fix this in user-space kexec-tools, which handles an 'Image.gz' being passed via kexec_file_load(), using an approach as follows: a). Copy the contents of Image.gz to a temporary file. b). Decompress (gunzip-decompress) the contents inside the temporary file. c). Pass the 'fd' of the temporary file to the kernel space. So basically the kernel space still gets a decompressed kernel image to load via kexec-tools I tested this patch for the following three use-cases: 1. Uncompressed Image file: #kexec -s -l Image --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline 2. Signed Image file: #kexec -s -l Image.signed --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline 3. zlib compressed Image.gz file: #kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> --- kexec/arch/arm64/Makefile | 3 +- kexec/arch/arm64/kexec-arm64.c | 1 + kexec/arch/arm64/kexec-arm64.h | 7 ++ kexec/arch/arm64/kexec-image-arm64.c | 4 +- kexec/arch/arm64/kexec-zImage-arm64.c | 199 ++++++++++++++++++++++++++++++++++ kexec/kexec.c | 12 ++ 6 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c diff --git a/kexec/arch/arm64/Makefile b/kexec/arch/arm64/Makefile index 9d9111caa8ed..d27c8ee1b5e7 100644 --- a/kexec/arch/arm64/Makefile +++ b/kexec/arch/arm64/Makefile @@ -15,7 +15,8 @@ arm64_KEXEC_SRCS += \ kexec/arch/arm64/kexec-arm64.c \ 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-image-arm64.c \ + kexec/arch/arm64/kexec-zImage-arm64.c arm64_UIMAGE = kexec/kexec-uImage.c diff --git a/kexec/arch/arm64/kexec-arm64.c b/kexec/arch/arm64/kexec-arm64.c index 2992bce8139f..eb3a3a37307c 100644 --- a/kexec/arch/arm64/kexec-arm64.c +++ b/kexec/arch/arm64/kexec-arm64.c @@ -71,6 +71,7 @@ struct file_type file_type[] = { {"vmlinux", elf_arm64_probe, elf_arm64_load, elf_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}, }; int file_types = sizeof(file_type) / sizeof(file_type[0]); diff --git a/kexec/arch/arm64/kexec-arm64.h b/kexec/arch/arm64/kexec-arm64.h index cc3419f4c10f..628de79b7f70 100644 --- a/kexec/arch/arm64/kexec-arm64.h +++ b/kexec/arch/arm64/kexec-arm64.h @@ -38,11 +38,18 @@ int image_arm64_probe(const char *kernel_buf, off_t kernel_size); 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_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_load(int argc, char **argv, const char *kernel_buf, + off_t kernel_size, struct kexec_info *info); +void zImage_arm64_usage(void); + + off_t initrd_base; off_t initrd_size; diff --git a/kexec/arch/arm64/kexec-image-arm64.c b/kexec/arch/arm64/kexec-image-arm64.c index 685a99352e39..aa8f2e22d72b 100644 --- a/kexec/arch/arm64/kexec-image-arm64.c +++ b/kexec/arch/arm64/kexec-image-arm64.c @@ -114,6 +114,6 @@ exit: void image_arm64_usage(void) { printf( -" An ARM64 binary image, compressed or not, big or little endian.\n" -" Typically an Image, Image.gz or Image.lzma file.\n\n"); +" An ARM64 binary image, uncompressed, big or little endian.\n" +" Typically an Image file.\n\n"); } diff --git a/kexec/arch/arm64/kexec-zImage-arm64.c b/kexec/arch/arm64/kexec-zImage-arm64.c new file mode 100644 index 000000000000..2b83ef6bdffd --- /dev/null +++ b/kexec/arch/arm64/kexec-zImage-arm64.c @@ -0,0 +1,199 @@ +/* + * ARM64 kexec zImage (Image.gz) support. + * + * Several distros use 'make zinstall' rule inside + * 'arch/arm64/boot/Makefile' to install the arm64 + * Image.gz compressed file inside the boot destination + * directory (for e.g. /boot). + * + * Currently we cannot use kexec_file_load() to load vmlinuz + * (or Image.gz). + * + * To support Image.gz, we should: + * a). Copy the contents of Image.gz to a temporary file. + * b). Decompress (gunzip-decompress) the contents inside the + * temporary file. + * c). Pass the 'fd' of the temporary file to the kernel space. + * + * So basically the kernel space still gets a decompressed + * kernel image to load via kexec-tools. + */ + +#define _GNU_SOURCE + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdlib.h> +#include "crashdump-arm64.h" +#include "image-header.h" +#include "kexec.h" +#include "kexec-arm64.h" +#include "kexec-syscall.h" +#include "kexec-zlib.h" +#include "arch/options.h" + +#define FILENAME_IMAGE "/tmp/ImageXXXXXX" + +/* Returns: + * -1 : in case of error/invalid format (not a valid Image.gz format. + * fd: File descriptor of the temp file containing the decompressed + * Image. + */ +int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size) +{ + int kernel_fd = 0; + char *fname = NULL; + char *kernel_uncompressed_buf = NULL; + const struct arm64_image_header *h; + + if (!is_zlib_file(kernel_buf, &kernel_size)) { + dbgprintf("%s: Not an zImage file (Image.gz).\n", __func__); + return -1; + } + + if (!(fname = strdup(FILENAME_IMAGE))) { + dbgprintf("%s: Can't duplicate strings %s\n", __func__, + fname); + return -1; + } + + if ((kernel_fd = mkstemp(fname)) < 0) { + dbgprintf("%s: Can't open file %s\n", __func__, + fname); + return -1; + } + + 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); + return -ENOMEM; + } + + /* slurp in the input kernel */ + dbgprintf("%s: ", __func__); + kernel_uncompressed_buf = slurp_decompress_file(kernel_buf, + &kernel_size); + + /* check for correct header magic */ + if (kernel_size < sizeof(struct arm64_image_header)) { + dbgprintf("%s: No arm64 image header.\n", __func__); + return -1; + } + + h = (const struct arm64_image_header *)(kernel_uncompressed_buf); + + if (!arm64_header_check_magic(h)) { + dbgprintf("%s: Bad arm64 image header.\n", __func__); + return -1; + } + + if (write(kernel_fd, kernel_uncompressed_buf, + kernel_size) != kernel_size) { + dbgprintf("%s: Can't write the uncompressed file %s\n", + __func__, fname); + return -1; + } + + close(kernel_fd); + + /* Open the tmp file again, this time in O_RDONLY mode, as + * opening the file in O_RDWR and calling kexec_file_load() + * causes the kernel to return -ETXTBSY + */ + kernel_fd = open(fname, O_RDONLY); + if (kernel_fd == -1) { + dbgprintf("%s: Failed to open file %s\n", + __func__, fname); + return -1; + } + + return kernel_fd; +} + +int zImage_arm64_load(int argc, char **argv, const char *kernel_buf, + off_t kernel_size, struct kexec_info *info) +{ + const struct arm64_image_header *header; + unsigned long kernel_segment; + int result; + + if (info->file_mode) { + if (arm64_opts.initrd) { + info->initrd_fd = open(arm64_opts.initrd, O_RDONLY); + if (info->initrd_fd == -1) { + fprintf(stderr, + "Could not open initrd file %s:%s\n", + arm64_opts.initrd, strerror(errno)); + result = EFAILED; + goto exit; + } + } + + if (arm64_opts.command_line) { + info->command_line = (char *)arm64_opts.command_line; + info->command_line_len = + strlen(arm64_opts.command_line) + 1; + } + + return 0; + } + + header = (const struct arm64_image_header *)(kernel_buf); + + if (arm64_process_image_header(header)) + return EFAILED; + + kernel_segment = arm64_locate_kernel_segment(info); + + if (kernel_segment == ULONG_MAX) { + dbgprintf("%s: Kernel segment is not allocated\n", __func__); + result = EFAILED; + goto exit; + } + + dbgprintf("%s: kernel_segment: %016lx\n", __func__, kernel_segment); + dbgprintf("%s: text_offset: %016lx\n", __func__, + arm64_mem.text_offset); + dbgprintf("%s: image_size: %016lx\n", __func__, + arm64_mem.image_size); + dbgprintf("%s: phys_offset: %016lx\n", __func__, + arm64_mem.phys_offset); + dbgprintf("%s: vp_offset: %016lx\n", __func__, + arm64_mem.vp_offset); + dbgprintf("%s: PE format: %s\n", __func__, + (arm64_header_check_pe_sig(header) ? "yes" : "no")); + + /* create and initialize elf core header segment */ + if (info->kexec_flags & KEXEC_ON_CRASH) { + result = load_crashdump_segments(info); + if (result) { + dbgprintf("%s: Creating eflcorehdr failed.\n", + __func__); + goto exit; + } + } + + /* load the kernel */ + add_segment_phys_virt(info, kernel_buf, kernel_size, + kernel_segment + arm64_mem.text_offset, + arm64_mem.image_size, 0); + + /* load additional data */ + result = arm64_load_other_segments(info, kernel_segment + + arm64_mem.text_offset); + +exit: + if (result) + fprintf(stderr, "kexec: load failed.\n"); + return result; +} + +void zImage_arm64_usage(void) +{ + printf( +" An ARM64 zImage, compressed, big or little endian.\n" +" Typically an Image.gz or Image.lzma file.\n\n"); +} diff --git a/kexec/kexec.c b/kexec/kexec.c index 8ca3b457cac8..bc6ab3dbd10b 100644 --- a/kexec/kexec.c +++ b/kexec/kexec.c @@ -1206,8 +1206,20 @@ static int do_kexec_file_load(int fileind, int argc, char **argv, kernel_buf = slurp_decompress_file(kernel, &kernel_size); for (i = 0; i < file_types; i++) { +#ifdef __aarch64__ + /* handle Image.gz like cases */ + if (is_zlib_file(kernel, &kernel_size)) { + if ((ret = file_type[i].probe(kernel, kernel_size)) >= 0) { + kernel_fd = ret; + break; + } + } else + if (file_type[i].probe(kernel_buf, kernel_size) >= 0) + break; +#else if (file_type[i].probe(kernel_buf, kernel_size) >= 0) break; +#endif } if (i == file_types) { -- 2.7.4 _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 4/4] kexec/arm64: Add support for handling zlib compressed (Image.gz) image 2019-07-10 19:54 ` [PATCH 4/4] kexec/arm64: Add support for handling zlib compressed (Image.gz) image Bhupesh Sharma @ 2019-07-11 10:08 ` Simon Horman 2019-07-12 8:06 ` Bhupesh Sharma 0 siblings, 1 reply; 9+ messages in thread From: Simon Horman @ 2019-07-11 10:08 UTC (permalink / raw) To: Bhupesh Sharma; +Cc: takahiro.akashi, bhupesh.linux, kexec On Thu, Jul 11, 2019 at 01:24:29AM +0530, Bhupesh Sharma wrote: > Currently the kexec_file_load() support for arm64 doesn't allow > handling zlib compressed (i.e. Image.gz) image. > > Since most distributions use 'make zinstall' rule inside > 'arch/arm64/boot/Makefile' to install the arm64 > Image.gz compressed file inside the boot destination directory (for e.g. > /boot), currently we cannot use kexec_file_load() to load vmlinuz (or > Image.gz): > > # file /boot/vmlinuz > /boot/vmlinuz: gzip compressed data, was "Image", <..snip..>, max > compression, from Unix, original size 21945120 > > Now, since via kexec_file_load() we pass the 'fd' of Image.gz > (compressed file) via the following command line ... > > # kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname > -r`.img --reuse-cmdline > > ... kernel returns -EINVAL error value, as it is not able to locate > the magic number =0x644d5241, which is expected in the 64-byte header > of the decompressed kernel image. > > We can fix this in user-space kexec-tools, which handles an > 'Image.gz' being passed via kexec_file_load(), using an approach > as follows: > > a). Copy the contents of Image.gz to a temporary file. > b). Decompress (gunzip-decompress) the contents inside the > temporary file. > c). Pass the 'fd' of the temporary file to the kernel space. So > basically the kernel space still gets a decompressed kernel > image to load via kexec-tools > > I tested this patch for the following three use-cases: > > 1. Uncompressed Image file: > #kexec -s -l Image --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline > > 2. Signed Image file: > #kexec -s -l Image.signed --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline > > 3. zlib compressed Image.gz file: > #kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline > > Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> > --- > kexec/arch/arm64/Makefile | 3 +- > kexec/arch/arm64/kexec-arm64.c | 1 + > kexec/arch/arm64/kexec-arm64.h | 7 ++ > kexec/arch/arm64/kexec-image-arm64.c | 4 +- > kexec/arch/arm64/kexec-zImage-arm64.c | 199 ++++++++++++++++++++++++++++++++++ > kexec/kexec.c | 12 ++ > 6 files changed, 223 insertions(+), 3 deletions(-) > create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c > > diff --git a/kexec/arch/arm64/Makefile b/kexec/arch/arm64/Makefile > index 9d9111caa8ed..d27c8ee1b5e7 100644 > --- a/kexec/arch/arm64/Makefile > +++ b/kexec/arch/arm64/Makefile > @@ -15,7 +15,8 @@ arm64_KEXEC_SRCS += \ > kexec/arch/arm64/kexec-arm64.c \ > 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-image-arm64.c \ > + kexec/arch/arm64/kexec-zImage-arm64.c > > arm64_UIMAGE = kexec/kexec-uImage.c > > diff --git a/kexec/arch/arm64/kexec-arm64.c b/kexec/arch/arm64/kexec-arm64.c > index 2992bce8139f..eb3a3a37307c 100644 > --- a/kexec/arch/arm64/kexec-arm64.c > +++ b/kexec/arch/arm64/kexec-arm64.c > @@ -71,6 +71,7 @@ struct file_type file_type[] = { > {"vmlinux", elf_arm64_probe, elf_arm64_load, elf_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}, > }; > > int file_types = sizeof(file_type) / sizeof(file_type[0]); > diff --git a/kexec/arch/arm64/kexec-arm64.h b/kexec/arch/arm64/kexec-arm64.h > index cc3419f4c10f..628de79b7f70 100644 > --- a/kexec/arch/arm64/kexec-arm64.h > +++ b/kexec/arch/arm64/kexec-arm64.h > @@ -38,11 +38,18 @@ int image_arm64_probe(const char *kernel_buf, off_t kernel_size); > 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_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_load(int argc, char **argv, const char *kernel_buf, > + off_t kernel_size, struct kexec_info *info); > +void zImage_arm64_usage(void); > + > + > off_t initrd_base; > off_t initrd_size; > > diff --git a/kexec/arch/arm64/kexec-image-arm64.c b/kexec/arch/arm64/kexec-image-arm64.c > index 685a99352e39..aa8f2e22d72b 100644 > --- a/kexec/arch/arm64/kexec-image-arm64.c > +++ b/kexec/arch/arm64/kexec-image-arm64.c > @@ -114,6 +114,6 @@ exit: > void image_arm64_usage(void) > { > printf( > -" An ARM64 binary image, compressed or not, big or little endian.\n" > -" Typically an Image, Image.gz or Image.lzma file.\n\n"); > +" An ARM64 binary image, uncompressed, big or little endian.\n" > +" Typically an Image file.\n\n"); > } > diff --git a/kexec/arch/arm64/kexec-zImage-arm64.c b/kexec/arch/arm64/kexec-zImage-arm64.c > new file mode 100644 > index 000000000000..2b83ef6bdffd > --- /dev/null > +++ b/kexec/arch/arm64/kexec-zImage-arm64.c > @@ -0,0 +1,199 @@ > +/* > + * ARM64 kexec zImage (Image.gz) support. > + * > + * Several distros use 'make zinstall' rule inside > + * 'arch/arm64/boot/Makefile' to install the arm64 > + * Image.gz compressed file inside the boot destination > + * directory (for e.g. /boot). > + * > + * Currently we cannot use kexec_file_load() to load vmlinuz > + * (or Image.gz). > + * > + * To support Image.gz, we should: > + * a). Copy the contents of Image.gz to a temporary file. > + * b). Decompress (gunzip-decompress) the contents inside the > + * temporary file. > + * c). Pass the 'fd' of the temporary file to the kernel space. > + * > + * So basically the kernel space still gets a decompressed > + * kernel image to load via kexec-tools. > + */ > + > +#define _GNU_SOURCE > + > +#include <errno.h> > +#include <fcntl.h> > +#include <limits.h> > +#include <stdlib.h> > +#include "crashdump-arm64.h" > +#include "image-header.h" > +#include "kexec.h" > +#include "kexec-arm64.h" > +#include "kexec-syscall.h" > +#include "kexec-zlib.h" > +#include "arch/options.h" > + > +#define FILENAME_IMAGE "/tmp/ImageXXXXXX" > + > +/* Returns: > + * -1 : in case of error/invalid format (not a valid Image.gz format. > + * fd: File descriptor of the temp file containing the decompressed > + * Image. > + */ > +int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size) > +{ > + int kernel_fd = 0; > + char *fname = NULL; > + char *kernel_uncompressed_buf = NULL; > + const struct arm64_image_header *h; > + > + if (!is_zlib_file(kernel_buf, &kernel_size)) { > + dbgprintf("%s: Not an zImage file (Image.gz).\n", __func__); > + return -1; > + } > + > + if (!(fname = strdup(FILENAME_IMAGE))) { > + dbgprintf("%s: Can't duplicate strings %s\n", __func__, > + fname); > + return -1; > + } > + > + if ((kernel_fd = mkstemp(fname)) < 0) { > + dbgprintf("%s: Can't open file %s\n", __func__, > + fname); > + return -1; > + } > + > + 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); > + return -ENOMEM; > + } > + > + /* slurp in the input kernel */ > + dbgprintf("%s: ", __func__); > + kernel_uncompressed_buf = slurp_decompress_file(kernel_buf, > + &kernel_size); > + > + /* check for correct header magic */ > + if (kernel_size < sizeof(struct arm64_image_header)) { > + dbgprintf("%s: No arm64 image header.\n", __func__); > + return -1; > + } > + > + h = (const struct arm64_image_header *)(kernel_uncompressed_buf); > + > + if (!arm64_header_check_magic(h)) { > + dbgprintf("%s: Bad arm64 image header.\n", __func__); > + return -1; > + } > + > + if (write(kernel_fd, kernel_uncompressed_buf, > + kernel_size) != kernel_size) { > + dbgprintf("%s: Can't write the uncompressed file %s\n", > + __func__, fname); > + return -1; > + } I think that kernel_fd needs to also be closed in error cases to avoid a leak. > + > + close(kernel_fd); What if the contents of the file whose name is fdname is replaced at this moment? > + > + /* Open the tmp file again, this time in O_RDONLY mode, as > + * opening the file in O_RDWR and calling kexec_file_load() > + * causes the kernel to return -ETXTBSY > + */ > + kernel_fd = open(fname, O_RDONLY); > + if (kernel_fd == -1) { > + dbgprintf("%s: Failed to open file %s\n", > + __func__, fname); > + return -1; > + } I think that fname is leaked here and in error cases. > + > + return kernel_fd; > +} > + > +int zImage_arm64_load(int argc, char **argv, const char *kernel_buf, > + off_t kernel_size, struct kexec_info *info) > +{ > + const struct arm64_image_header *header; > + unsigned long kernel_segment; > + int result; > + > + if (info->file_mode) { > + if (arm64_opts.initrd) { > + info->initrd_fd = open(arm64_opts.initrd, O_RDONLY); > + if (info->initrd_fd == -1) { > + fprintf(stderr, > + "Could not open initrd file %s:%s\n", > + arm64_opts.initrd, strerror(errno)); > + result = EFAILED; > + goto exit; > + } > + } > + > + if (arm64_opts.command_line) { > + info->command_line = (char *)arm64_opts.command_line; > + info->command_line_len = > + strlen(arm64_opts.command_line) + 1; > + } > + > + return 0; > + } > + > + header = (const struct arm64_image_header *)(kernel_buf); > + > + if (arm64_process_image_header(header)) > + return EFAILED; > + > + kernel_segment = arm64_locate_kernel_segment(info); > + > + if (kernel_segment == ULONG_MAX) { > + dbgprintf("%s: Kernel segment is not allocated\n", __func__); > + result = EFAILED; > + goto exit; > + } > + > + dbgprintf("%s: kernel_segment: %016lx\n", __func__, kernel_segment); > + dbgprintf("%s: text_offset: %016lx\n", __func__, > + arm64_mem.text_offset); > + dbgprintf("%s: image_size: %016lx\n", __func__, > + arm64_mem.image_size); > + dbgprintf("%s: phys_offset: %016lx\n", __func__, > + arm64_mem.phys_offset); > + dbgprintf("%s: vp_offset: %016lx\n", __func__, > + arm64_mem.vp_offset); > + dbgprintf("%s: PE format: %s\n", __func__, > + (arm64_header_check_pe_sig(header) ? "yes" : "no")); > + > + /* create and initialize elf core header segment */ > + if (info->kexec_flags & KEXEC_ON_CRASH) { > + result = load_crashdump_segments(info); > + if (result) { > + dbgprintf("%s: Creating eflcorehdr failed.\n", > + __func__); > + goto exit; > + } > + } > + > + /* load the kernel */ > + add_segment_phys_virt(info, kernel_buf, kernel_size, > + kernel_segment + arm64_mem.text_offset, > + arm64_mem.image_size, 0); > + > + /* load additional data */ > + result = arm64_load_other_segments(info, kernel_segment > + + arm64_mem.text_offset); > + > +exit: > + if (result) > + fprintf(stderr, "kexec: load failed.\n"); > + return result; > +} > + > +void zImage_arm64_usage(void) > +{ > + printf( > +" An ARM64 zImage, compressed, big or little endian.\n" > +" Typically an Image.gz or Image.lzma file.\n\n"); > +} > diff --git a/kexec/kexec.c b/kexec/kexec.c > index 8ca3b457cac8..bc6ab3dbd10b 100644 > --- a/kexec/kexec.c > +++ b/kexec/kexec.c > @@ -1206,8 +1206,20 @@ static int do_kexec_file_load(int fileind, int argc, char **argv, > kernel_buf = slurp_decompress_file(kernel, &kernel_size); > > for (i = 0; i < file_types; i++) { > +#ifdef __aarch64__ > + /* handle Image.gz like cases */ > + if (is_zlib_file(kernel, &kernel_size)) { > + if ((ret = file_type[i].probe(kernel, kernel_size)) >= 0) { > + kernel_fd = ret; > + break; > + } > + } else > + if (file_type[i].probe(kernel_buf, kernel_size) >= 0) > + break; > +#else > if (file_type[i].probe(kernel_buf, kernel_size) >= 0) > break; > +#endif > } > > if (i == file_types) { > -- > 2.7.4 > _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 4/4] kexec/arm64: Add support for handling zlib compressed (Image.gz) image 2019-07-11 10:08 ` Simon Horman @ 2019-07-12 8:06 ` Bhupesh Sharma 0 siblings, 0 replies; 9+ messages in thread From: Bhupesh Sharma @ 2019-07-12 8:06 UTC (permalink / raw) To: Simon Horman; +Cc: AKASHI Takahiro, Bhupesh SHARMA, kexec mailing list Hi Simon, Thanks for your review. On Thu, Jul 11, 2019 at 3:38 PM Simon Horman <horms@verge.net.au> wrote: > > On Thu, Jul 11, 2019 at 01:24:29AM +0530, Bhupesh Sharma wrote: > > Currently the kexec_file_load() support for arm64 doesn't allow > > handling zlib compressed (i.e. Image.gz) image. > > > > Since most distributions use 'make zinstall' rule inside > > 'arch/arm64/boot/Makefile' to install the arm64 > > Image.gz compressed file inside the boot destination directory (for e.g. > > /boot), currently we cannot use kexec_file_load() to load vmlinuz (or > > Image.gz): > > > > # file /boot/vmlinuz > > /boot/vmlinuz: gzip compressed data, was "Image", <..snip..>, max > > compression, from Unix, original size 21945120 > > > > Now, since via kexec_file_load() we pass the 'fd' of Image.gz > > (compressed file) via the following command line ... > > > > # kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname > > -r`.img --reuse-cmdline > > > > ... kernel returns -EINVAL error value, as it is not able to locate > > the magic number =0x644d5241, which is expected in the 64-byte header > > of the decompressed kernel image. > > > > We can fix this in user-space kexec-tools, which handles an > > 'Image.gz' being passed via kexec_file_load(), using an approach > > as follows: > > > > a). Copy the contents of Image.gz to a temporary file. > > b). Decompress (gunzip-decompress) the contents inside the > > temporary file. > > c). Pass the 'fd' of the temporary file to the kernel space. So > > basically the kernel space still gets a decompressed kernel > > image to load via kexec-tools > > > > I tested this patch for the following three use-cases: > > > > 1. Uncompressed Image file: > > #kexec -s -l Image --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline > > > > 2. Signed Image file: > > #kexec -s -l Image.signed --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline > > > > 3. zlib compressed Image.gz file: > > #kexec -s -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img --reuse-cmdline > > > > Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com> > > --- > > kexec/arch/arm64/Makefile | 3 +- > > kexec/arch/arm64/kexec-arm64.c | 1 + > > kexec/arch/arm64/kexec-arm64.h | 7 ++ > > kexec/arch/arm64/kexec-image-arm64.c | 4 +- > > kexec/arch/arm64/kexec-zImage-arm64.c | 199 ++++++++++++++++++++++++++++++++++ > > kexec/kexec.c | 12 ++ > > 6 files changed, 223 insertions(+), 3 deletions(-) > > create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c > > > > diff --git a/kexec/arch/arm64/Makefile b/kexec/arch/arm64/Makefile > > index 9d9111caa8ed..d27c8ee1b5e7 100644 > > --- a/kexec/arch/arm64/Makefile > > +++ b/kexec/arch/arm64/Makefile > > @@ -15,7 +15,8 @@ arm64_KEXEC_SRCS += \ > > kexec/arch/arm64/kexec-arm64.c \ > > 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-image-arm64.c \ > > + kexec/arch/arm64/kexec-zImage-arm64.c > > > > arm64_UIMAGE = kexec/kexec-uImage.c > > > > diff --git a/kexec/arch/arm64/kexec-arm64.c b/kexec/arch/arm64/kexec-arm64.c > > index 2992bce8139f..eb3a3a37307c 100644 > > --- a/kexec/arch/arm64/kexec-arm64.c > > +++ b/kexec/arch/arm64/kexec-arm64.c > > @@ -71,6 +71,7 @@ struct file_type file_type[] = { > > {"vmlinux", elf_arm64_probe, elf_arm64_load, elf_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}, > > }; > > > > int file_types = sizeof(file_type) / sizeof(file_type[0]); > > diff --git a/kexec/arch/arm64/kexec-arm64.h b/kexec/arch/arm64/kexec-arm64.h > > index cc3419f4c10f..628de79b7f70 100644 > > --- a/kexec/arch/arm64/kexec-arm64.h > > +++ b/kexec/arch/arm64/kexec-arm64.h > > @@ -38,11 +38,18 @@ int image_arm64_probe(const char *kernel_buf, off_t kernel_size); > > 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_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_load(int argc, char **argv, const char *kernel_buf, > > + off_t kernel_size, struct kexec_info *info); > > +void zImage_arm64_usage(void); > > + > > + > > off_t initrd_base; > > off_t initrd_size; > > > > diff --git a/kexec/arch/arm64/kexec-image-arm64.c b/kexec/arch/arm64/kexec-image-arm64.c > > index 685a99352e39..aa8f2e22d72b 100644 > > --- a/kexec/arch/arm64/kexec-image-arm64.c > > +++ b/kexec/arch/arm64/kexec-image-arm64.c > > @@ -114,6 +114,6 @@ exit: > > void image_arm64_usage(void) > > { > > printf( > > -" An ARM64 binary image, compressed or not, big or little endian.\n" > > -" Typically an Image, Image.gz or Image.lzma file.\n\n"); > > +" An ARM64 binary image, uncompressed, big or little endian.\n" > > +" Typically an Image file.\n\n"); > > } > > diff --git a/kexec/arch/arm64/kexec-zImage-arm64.c b/kexec/arch/arm64/kexec-zImage-arm64.c > > new file mode 100644 > > index 000000000000..2b83ef6bdffd > > --- /dev/null > > +++ b/kexec/arch/arm64/kexec-zImage-arm64.c > > @@ -0,0 +1,199 @@ > > +/* > > + * ARM64 kexec zImage (Image.gz) support. > > + * > > + * Several distros use 'make zinstall' rule inside > > + * 'arch/arm64/boot/Makefile' to install the arm64 > > + * Image.gz compressed file inside the boot destination > > + * directory (for e.g. /boot). > > + * > > + * Currently we cannot use kexec_file_load() to load vmlinuz > > + * (or Image.gz). > > + * > > + * To support Image.gz, we should: > > + * a). Copy the contents of Image.gz to a temporary file. > > + * b). Decompress (gunzip-decompress) the contents inside the > > + * temporary file. > > + * c). Pass the 'fd' of the temporary file to the kernel space. > > + * > > + * So basically the kernel space still gets a decompressed > > + * kernel image to load via kexec-tools. > > + */ > > + > > +#define _GNU_SOURCE > > + > > +#include <errno.h> > > +#include <fcntl.h> > > +#include <limits.h> > > +#include <stdlib.h> > > +#include "crashdump-arm64.h" > > +#include "image-header.h" > > +#include "kexec.h" > > +#include "kexec-arm64.h" > > +#include "kexec-syscall.h" > > +#include "kexec-zlib.h" > > +#include "arch/options.h" > > + > > +#define FILENAME_IMAGE "/tmp/ImageXXXXXX" > > + > > +/* Returns: > > + * -1 : in case of error/invalid format (not a valid Image.gz format. > > + * fd: File descriptor of the temp file containing the decompressed > > + * Image. > > + */ > > +int zImage_arm64_probe(const char *kernel_buf, off_t kernel_size) > > +{ > > + int kernel_fd = 0; > > + char *fname = NULL; > > + char *kernel_uncompressed_buf = NULL; > > + const struct arm64_image_header *h; > > + > > + if (!is_zlib_file(kernel_buf, &kernel_size)) { > > + dbgprintf("%s: Not an zImage file (Image.gz).\n", __func__); > > + return -1; > > + } > > + > > + if (!(fname = strdup(FILENAME_IMAGE))) { > > + dbgprintf("%s: Can't duplicate strings %s\n", __func__, > > + fname); > > + return -1; > > + } > > + > > + if ((kernel_fd = mkstemp(fname)) < 0) { > > + dbgprintf("%s: Can't open file %s\n", __func__, > > + fname); > > + return -1; > > + } > > + > > + 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); > > + return -ENOMEM; > > + } > > + > > + /* slurp in the input kernel */ > > + dbgprintf("%s: ", __func__); > > + kernel_uncompressed_buf = slurp_decompress_file(kernel_buf, > > + &kernel_size); > > + > > + /* check for correct header magic */ > > + if (kernel_size < sizeof(struct arm64_image_header)) { > > + dbgprintf("%s: No arm64 image header.\n", __func__); > > + return -1; > > + } > > + > > + h = (const struct arm64_image_header *)(kernel_uncompressed_buf); > > + > > + if (!arm64_header_check_magic(h)) { > > + dbgprintf("%s: Bad arm64 image header.\n", __func__); > > + return -1; > > + } > > + > > + if (write(kernel_fd, kernel_uncompressed_buf, > > + kernel_size) != kernel_size) { > > + dbgprintf("%s: Can't write the uncompressed file %s\n", > > + __func__, fname); > > + return -1; > > + } > > I think that kernel_fd needs to also be closed in > error cases to avoid a leak. Right. > > + > > + close(kernel_fd); > > What if the contents of the file whose name is fdname is replaced at this > moment? Normally that wouldn't happen, but I will check this more carefully and fix in v2. > > + > > + /* Open the tmp file again, this time in O_RDONLY mode, as > > + * opening the file in O_RDWR and calling kexec_file_load() > > + * causes the kernel to return -ETXTBSY > > + */ > > + kernel_fd = open(fname, O_RDONLY); > > + if (kernel_fd == -1) { > > + dbgprintf("%s: Failed to open file %s\n", > > + __func__, fname); > > + return -1; > > + } > > I think that fname is leaked here and in error cases. Right. Will fix this in v2. Thanks, Bhupesh > > + > > + return kernel_fd; > > +} > > + > > +int zImage_arm64_load(int argc, char **argv, const char *kernel_buf, > > + off_t kernel_size, struct kexec_info *info) > > +{ > > + const struct arm64_image_header *header; > > + unsigned long kernel_segment; > > + int result; > > + > > + if (info->file_mode) { > > + if (arm64_opts.initrd) { > > + info->initrd_fd = open(arm64_opts.initrd, O_RDONLY); > > + if (info->initrd_fd == -1) { > > + fprintf(stderr, > > + "Could not open initrd file %s:%s\n", > > + arm64_opts.initrd, strerror(errno)); > > + result = EFAILED; > > + goto exit; > > + } > > + } > > + > > + if (arm64_opts.command_line) { > > + info->command_line = (char *)arm64_opts.command_line; > > + info->command_line_len = > > + strlen(arm64_opts.command_line) + 1; > > + } > > + > > + return 0; > > + } > > + > > + header = (const struct arm64_image_header *)(kernel_buf); > > + > > + if (arm64_process_image_header(header)) > > + return EFAILED; > > + > > + kernel_segment = arm64_locate_kernel_segment(info); > > + > > + if (kernel_segment == ULONG_MAX) { > > + dbgprintf("%s: Kernel segment is not allocated\n", __func__); > > + result = EFAILED; > > + goto exit; > > + } > > + > > + dbgprintf("%s: kernel_segment: %016lx\n", __func__, kernel_segment); > > + dbgprintf("%s: text_offset: %016lx\n", __func__, > > + arm64_mem.text_offset); > > + dbgprintf("%s: image_size: %016lx\n", __func__, > > + arm64_mem.image_size); > > + dbgprintf("%s: phys_offset: %016lx\n", __func__, > > + arm64_mem.phys_offset); > > + dbgprintf("%s: vp_offset: %016lx\n", __func__, > > + arm64_mem.vp_offset); > > + dbgprintf("%s: PE format: %s\n", __func__, > > + (arm64_header_check_pe_sig(header) ? "yes" : "no")); > > + > > + /* create and initialize elf core header segment */ > > + if (info->kexec_flags & KEXEC_ON_CRASH) { > > + result = load_crashdump_segments(info); > > + if (result) { > > + dbgprintf("%s: Creating eflcorehdr failed.\n", > > + __func__); > > + goto exit; > > + } > > + } > > + > > + /* load the kernel */ > > + add_segment_phys_virt(info, kernel_buf, kernel_size, > > + kernel_segment + arm64_mem.text_offset, > > + arm64_mem.image_size, 0); > > + > > + /* load additional data */ > > + result = arm64_load_other_segments(info, kernel_segment > > + + arm64_mem.text_offset); > > + > > +exit: > > + if (result) > > + fprintf(stderr, "kexec: load failed.\n"); > > + return result; > > +} > > + > > +void zImage_arm64_usage(void) > > +{ > > + printf( > > +" An ARM64 zImage, compressed, big or little endian.\n" > > +" Typically an Image.gz or Image.lzma file.\n\n"); > > +} > > diff --git a/kexec/kexec.c b/kexec/kexec.c > > index 8ca3b457cac8..bc6ab3dbd10b 100644 > > --- a/kexec/kexec.c > > +++ b/kexec/kexec.c > > @@ -1206,8 +1206,20 @@ static int do_kexec_file_load(int fileind, int argc, char **argv, > > kernel_buf = slurp_decompress_file(kernel, &kernel_size); > > > > for (i = 0; i < file_types; i++) { > > +#ifdef __aarch64__ > > + /* handle Image.gz like cases */ > > + if (is_zlib_file(kernel, &kernel_size)) { > > + if ((ret = file_type[i].probe(kernel, kernel_size)) >= 0) { > > + kernel_fd = ret; > > + break; > > + } > > + } else > > + if (file_type[i].probe(kernel_buf, kernel_size) >= 0) > > + break; > > +#else > > if (file_type[i].probe(kernel_buf, kernel_size) >= 0) > > break; > > +#endif > > } > > > > if (i == file_types) { > > -- > > 2.7.4 > > _______________________________________________ kexec mailing list kexec@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kexec ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2019-07-12 8:07 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2019-07-10 19:54 [PATCH 0/4] kexec/arm64: Add support for zlib compressed kernel images (Image.gz) Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 1/4] kexec/kexec.c: Add the missing close() for fd used for kexec_file_load() Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 2/4] kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe() Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 3/4] kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function Bhupesh Sharma 2019-07-11 9:58 ` Simon Horman 2019-07-12 8:06 ` Bhupesh Sharma 2019-07-10 19:54 ` [PATCH 4/4] kexec/arm64: Add support for handling zlib compressed (Image.gz) image Bhupesh Sharma 2019-07-11 10:08 ` Simon Horman 2019-07-12 8:06 ` Bhupesh Sharma
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox