Add support for zboot images compressed with zstd
v3 cover Add support for zboot images compressed with zstd
Fedora arm64 has an EFI_ZBOOT kernel image compressed with zstd. Let's make sure we can use it for direct kernel boot with qemu.
Daan De Meyer (3):
Rename LOAD_IMAGE_MAX_GUNZIP_BYTES to
LOAD_IMAGE_MAX_DECOMPRESSED_BYTES
Use g_autofree in unpack_efi_zboot_image() Add support for zboot images compressed with zstd
hw/arm/boot.c | 2 +- hw/core/loader.c | 41 ++++++++++++++++++++++++++--------------- hw/nvram/fw_cfg.c | 2 +- include/hw/loader.h | 2 +- 4 files changed, 29 insertions(+), 18 deletions(-)
Signature · 1 line
2.51.0
PATCH v3 1/3 Rename LOAD_IMAGE_MAX_GUNZIP_BYTES to LOAD_IMAGE_MAX_DECOMPRESSED_BYTES
Preparation for adding support for zstd compressed efi zboot kernel images.
--- hw/arm/boot.c | 2 +- hw/core/loader.c | 8 ++++---- hw/nvram/fw_cfg.c | 2 +- include/hw/loader.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-)
hw/arm/boot.c +1 -1
@@ -826,7 +826,7 @@ static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,ssize_t size;/* On aarch64, it's the bootloader's job to uncompress the kernel. */ - size =load_image_gzipped_buffer (filename,LOAD_IMAGE_MAX_GUNZIP_BYTES , + size =load_image_gzipped_buffer (filename,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , &buffer);if (size <0 ) {
hw/core/loader.c +4 -4
@@ -796,8 +796,8 @@ ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz, goto out; } -if (max_sz >LOAD_IMAGE_MAX_GUNZIP_BYTES ) { - max_sz =LOAD_IMAGE_MAX_GUNZIP_BYTES ; +if (max_sz >LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ) { + max_sz =LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ; } data =g_malloc (max_sz);
@@ -898,8 +898,8 @@ ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size)return -1 ; } - data =g_malloc (LOAD_IMAGE_MAX_GUNZIP_BYTES ); - bytes =gunzip (data,LOAD_IMAGE_MAX_GUNZIP_BYTES , *buffer + ploff, plsize); + data =g_malloc (LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ); + bytes =gunzip (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize);if (bytes <0 ) {fprintf (stderr,"failed to decompress EFI zboot image\n" );g_free (data);
hw/nvram/fw_cfg.c +1 -1
@@ -1115,7 +1115,7 @@ void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,if (try_decompress) { size =load_image_gzipped_buffer (image_name, -LOAD_IMAGE_MAX_GUNZIP_BYTES , &data); +LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , &data); }if (size == (size_t )-1 ) {
include/hw/loader.h +1 -1
@@ -80,7 +80,7 @@ ssize_t load_image_mr(const char *filename, MemoryRegion *mr);* load_image_gzipped_buffer() will read. It prevents * g_malloc() in those functions from allocating a huge amount of memory. */ -#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20) +#define LOAD_IMAGE_MAX_DECOMPRESSED_BYTES (256 << 20)ssize_t load_image_gzipped_buffer (const char *filename,uint64_t max_sz,uint8_t **buffer);
Signature · 1 line
2.51.0
Re: [PATCH v3 1/3] Rename LOAD_IMAGE_MAX_GUNZIP_BYTES to LOAD_IMAGE_MAX_DECOMPRESSED_BYTES
Quoted 10 lines · Daan De Meyer
Preparation for adding support for zstd compressed efi zboot kernel images.
Signed-off-by Daan De Meyer <[email protected]>--- hw/arm/boot.c | 2 +- hw/core/loader.c | 8 ++++---- hw/nvram/fw_cfg.c | 2 +- include/hw/loader.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 1/3] Rename LOAD_IMAGE_MAX_GUNZIP_BYTES to LOAD_IMAGE_MAX_DECOMPRESSED_BYTES
In reply to Daan De MeyerQuoted 40 lines · Daan De Meyer
Preparation for adding support for zstd compressed efi zboot kernel images.
Signed-off-by Daan De Meyer <[email protected]>--- hw/arm/boot.c | 2 +- hw/core/loader.c | 8 ++++---- hw/nvram/fw_cfg.c | 2 +- include/hw/loader.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-)hw/arm/boot.c +1
-1@@ -826,7 +826,7 @@ static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,ssize_t size;/* On aarch64, it's the bootloader's job to uncompress the kernel. */ - size =load_image_gzipped_buffer (filename,LOAD_IMAGE_MAX_GUNZIP_BYTES , + size =load_image_gzipped_buffer (filename,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , &buffer);if (size <0 ) {hw/core/loader.c +2
-2@@ -796,8 +796,8 @@ ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz, goto out; } -if (max_sz >LOAD_IMAGE_MAX_GUNZIP_BYTES ) { - max_sz =LOAD_IMAGE_MAX_GUNZIP_BYTES ; +if (max_sz >LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ) { + max_sz =LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ; } data =g_malloc (max_sz);
hw/core/loader.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 902
Quoted 7 lines
hw/core/loader.c +2
-2@@ -898,8 +898,8 @@ ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size)return -1 ; } - data =g_malloc (LOAD_IMAGE_MAX_GUNZIP_BYTES ); - bytes =gunzip (data,LOAD_IMAGE_MAX_GUNZIP_BYTES , *buffer + ploff, plsize); + data =g_malloc (LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ); + bytes =gunzip (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize);
I'd take the opportunity to introduce a size_t variable, since patch #3 re-use this definition.
Otherwise,
Re: [PATCH v3 1/3] Rename LOAD_IMAGE_MAX_GUNZIP_BYTES to LOAD_IMAGE_MAX_DECOMPRESSED_BYTES
In reply to Daan De MeyerQuoted 17 lines · Daan De Meyer
Preparation for adding support for zstd compressed efi zboot kernel images.
Signed-off-by Daan De Meyer <[email protected]>--- hw/arm/boot.c | 2 +- hw/core/loader.c | 8 ++++---- hw/nvram/fw_cfg.c | 2 +- include/hw/loader.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-)include/hw/loader.h +0
-0
include/hw/loader.h ssize_t load_image_mr(const char *filename, MemoryRegion *mr); · line 83
Quoted 5 lines
include/hw/loader.h +1
-1@@ -80,7 +80,7 @@ ssize_t load_image_mr(const char *filename, MemoryRegion *mr);* load_image_gzipped_buffer() will read. It prevents * g_malloc() in those functions from allocating a huge amount of memory. */ -#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20) +#define LOAD_IMAGE_MAX_DECOMPRESSED_BYTES (256 << 20)
Maybe also rename UBOOT_MAX_GUNZIP_BYTES for coherency.
Re: [PATCH v3 1/3] Rename LOAD_IMAGE_MAX_GUNZIP_BYTES to LOAD_IMAGE_MAX_DECOMPRESSED_BYTES
In reply to Daan De MeyerQuoted 10 lines · Daan De Meyer
Preparation for adding support for zstd compressed efi zboot kernel images.
Signed-off-by Daan De Meyer <[email protected]>--- hw/arm/boot.c | 2 +- hw/core/loader.c | 8 ++++---- hw/nvram/fw_cfg.c | 2 +- include/hw/loader.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-)
PATCH v3 2/3 Use g_autofree in unpack_efi_zboot_image()
In reply to Daan De Meyer--- hw/core/loader.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
hw/core/loader.c +2 -3
@@ -864,7 +864,7 @@ struct linux_efi_zboot_header {ssize_t unpack_efi_zboot_image (uint8_t **buffer,ssize_t *size) {const struct linux_efi_zboot_header *header; -uint8_t *data =NULL ; + g_autofree uint8_t *data = NULL;ssize_t ploff, plsize;ssize_t bytes;
@@ -902,12 +902,11 @@ ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) bytes =gunzip (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize);if (bytes <0 ) {fprintf (stderr,"failed to decompress EFI zboot image\n" ); -g_free (data);return -1 ; }g_free (*buffer); - *buffer =g_realloc (data, bytes); + *buffer =g_realloc (g_steal_pointer (&data), bytes); *size = bytes;return bytes; }
Signature · 1 line
2.51.0
Re: [PATCH v3 2/3] Use g_autofree in unpack_efi_zboot_image()
Quoted 4 lines · Daan De Meyer
Signed-off-by Daan De Meyer <[email protected]>--- hw/core/loader.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/3] Use g_autofree in unpack_efi_zboot_image()
In reply to Daan De MeyerQuoted 4 lines · Daan De Meyer
Signed-off-by Daan De Meyer <[email protected]>--- hw/core/loader.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
Re: [PATCH v3 2/3] Use g_autofree in unpack_efi_zboot_image()
In reply to Daan De MeyerQuoted 4 lines · Daan De Meyer
Signed-off-by Daan De Meyer <[email protected]>--- hw/core/loader.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
PATCH v3 3/3 Add support for zboot images compressed with zstd
In reply to Daan De Meyer--- hw/core/loader.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
hw/core/loader.c +21 -9
@@ -67,6 +67,11 @@#include <zlib.h> +#ifdef CONFIG_ZSTD +#include <zstd.h> +#include <zstd_errors.h> +#endif +static int roms_loaded;/* return the size or -1 if error */
@@ -882,14 +887,6 @@ ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size)return 0 ; } -if (strcmp (header->compression_type,"gzip" ) !=0 ) { -fprintf (stderr, -"unable to handle EFI zboot image with \"%.*s\" compression\n" , - (int )sizeof (header->compression_type) -1 , - header->compression_type); -return -1 ; - } - ploff =ldl_le_p (&header->payload_offset); plsize =ldl_le_p (&header->payload_size);
@@ -899,7 +896,22 @@ ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) } data =g_malloc (LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ); - bytes =gunzip (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize); + +if (strcmp (header->compression_type,"gzip" ) ==0 ) { + bytes =gunzip (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize); +#ifdef CONFIG_ZSTD + }else if (strcmp (header->compression_type,"zstd" ) ==0 ) { +size_t ret =ZSTD_decompress (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize); + bytes =ZSTD_isError (ret) ?-1 : (ssize_t ) ret; +#endif + }else { +fprintf (stderr, +"unable to handle EFI zboot image with \"%.*s\" compression\n" , + (int )sizeof (header->compression_type) -1 , + header->compression_type); +return -1 ; + } +if (bytes <0 ) {fprintf (stderr,"failed to decompress EFI zboot image\n" );return -1 ;
Signature · 1 line
2.51.0
Quoted 4 lines · Daan De Meyer
Signed-off-by Daan De Meyer <[email protected]>--- hw/core/loader.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 7 lines · Daan De Meyer
Signed-off-by Daan De Meyer <[email protected]>--- hw/core/loader.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
hw/core/loader.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 904
Quoted 10 lines
hw/core/loader.c +6
-1@@ -899,7 +896,22 @@ ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) } data =g_malloc (LOAD_IMAGE_MAX_DECOMPRESSED_BYTES ); - bytes =gunzip (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize); + +if (strcmp (header->compression_type,"gzip" ) ==0 ) { + bytes =gunzip (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize); +#ifdef CONFIG_ZSTD + }else if (strcmp (header->compression_type,"zstd" ) ==0 ) { +size_t ret =ZSTD_decompress (data,LOAD_IMAGE_MAX_DECOMPRESSED_BYTES , *buffer + ploff, plsize);
(here I'd re-use a shorter local variable instead of this def)