kexec/arm64: Add support for zlib compressed kernel images (Image.gz)
Subsystems (2)
v2 cover kexec/arm64: Add support for zlib compressed kernel images (Image.gz)
Changes since v1:
----------------
(v1 can be viewed here: http://lists.infradead.org/pipermail/kexec/2019-July/023288.html)
- Fixed the potential file descriptor leakage code legs pointed out by Simon.
- Ran Valgrind to make sure no potential memory leakage issues are introduced by this patchset.
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 | 226 ++++++++++++++++++++++++++++++++++ kexec/kexec-zlib.h | 1 + kexec/kexec.c | 14 +++ kexec/zlib.c | 38 ++++++ 9 files changed, 303 insertions(+), 4 deletions(-) create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 1/4 kexec/kexec.c: Add the missing close() for fd used for kexec_file_load()
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.
--- kexec/kexec.c | 2 ++ 1 file changed, 2 insertions(+)
kexec/kexec.c +2 -0
@@ -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; }
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 2/4 kexec-uImage-arm64.c: Fix return value of uImage_arm64_probe()
In reply to Bhupesh SharmaCommit 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.
--- kexec/arch/arm64/kexec-uImage-arm64.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
kexec/arch/arm64/kexec-uImage-arm64.c +12 -1
@@ -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,
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 3/4 kexec/kexec-zlib.h: Add 'is_zlib_file()' helper function
In reply to Bhupesh SharmaThis 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.
--- kexec/kexec-zlib.h | 1 + kexec/zlib.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+)
kexec/kexec-zlib.h +1 -0
@@ -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 */
kexec/zlib.c +38 -0
@@ -23,6 +23,38 @@ 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; +int is_zlib_file =0 ;/* default: It's not in gzip format */ +const char *msg; +ssize_t result; + +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 in gzip format */ + is_zlib_file =1 ; + + result =gzclose (fp); +if (result !=Z_OK ) { +_gzerror (fp, &errnum, &msg); +dbgprintf (" Close of %s failed: %s\n" , filename, msg); + } + +out: +return is_zlib_file; +} +char *zlib_decompress_file (const char *filename,off_t *r_size) {gzFile fp;
@@ -84,6 +116,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 ;
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 4/4 kexec/arm64: Add support for handling zlib compressed (Image.gz) image
In reply to Bhupesh SharmaCurrently 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
--- 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 | 226 ++++++++++++++++++++++++++++++++++ kexec/kexec.c | 12 ++ 6 files changed, 250 insertions(+), 3 deletions(-) create mode 100644 kexec/arch/arm64/kexec-zImage-arm64.c
kexec/arch/arm64/Makefile +2 -1
@@ -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
kexec/arch/arm64/kexec-arm64.c +1 -0
@@ -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 ]);
kexec/arch/arm64/kexec-arm64.h +7 -0
@@ -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;
kexec/arch/arm64/kexec-image-arm64.c +2 -2
@@ -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" ); }
kexec/arch/arm64/kexec-zImage-arm64.c new file +226 -0
226 lines not on this page · raw message
kexec/kexec.c +12 -0
@@ -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) {
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Simon Horman
AppliedQuoted 21 lines · Bhupesh Sharma
Changes since v1:
----------------
(v1 can be viewed here: http://lists.infradead.org/pipermail/kexec/2019-July/023288.html)
- Fixed the potential file descriptor leakage code legs pointed out by Simon.
- Ran Valgrind to make sure no potential memory leakage issues are introduced by this patchset.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:
Quoted 8 lines
$ 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
Thanks, applied for inclusion in v2.0.20.
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Quoted 26 lines · Simon Horman
Quoted 24 lines · Bhupesh Sharma
Changes since v1:
----------------
(v1 can be viewed here: http://lists.infradead.org/pipermail/kexec/2019-July/023288.html)
- Fixed the potential file descriptor leakage code legs pointed out by Simon.
- Ran Valgrind to make sure no potential memory leakage issues are introduced by this patchset.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
Quoted 7 lines
Quoted 5 lines
... 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
Thanks, applied for inclusion in v2.0.20.
Thanks Simon.
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec