Skip to contentSkip to conversation

Add support for zboot images compressed with zstd

PATCH Superseded Daan De Meyer 3 participants 5 messages Oct 8, 2025 – Oct 10, 2025

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for zboot images compressed with zstd

---
 hw/arm/boot.c       |  2 +-
 hw/core/loader.c    | 36 ++++++++++++++++++++++++------------
 hw/nvram/fw_cfg.c   |  2 +-
 include/hw/loader.h |  2 +-
 4 files changed, 27 insertions(+), 15 deletions(-)
hw/arm/boot.c +1 -1
index e77d8679d8..c0dec0343a 100644
@@ -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 +24 -12
index 477661a025..a2647b0c9a 100644
@@ -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 */
@@ -796,8 +801,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);
@@ -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);
 
@@ -898,8 +895,23 @@ 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);
+
+    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");
         g_free(data);
hw/nvram/fw_cfg.c +1 -1
index aa24050493..af3b112524 100644
@@ -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
index c96b5e141c..24b91ba02b 100644
@@ -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
#1 lore raw
Quoted 14 lines · Daan De Meyer
---
 hw/arm/boot.c       |  2 +-
 hw/core/loader.c    | 36 ++++++++++++++++++++++++------------
 hw/nvram/fw_cfg.c   |  2 +-
 include/hw/loader.h |  2 +-
 4 files changed, 27 insertions(+), 15 deletions(-)
hw/arm/boot.c +0 -0
index e77d8679d8..c0dec0343a 100644

hw/arm/boot.c static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, · line 830

Quoted 6 lines
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);

I would either not bother renaming this constant, or else do it in a preliminary patch of its own in the series. (That makes the part of the patch which is making the functional change easier to read and review.)

Quoted 26 lines
hw/arm/boot.c +5 -10
@@ -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);
 
@@ -898,8 +895,23 @@ 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);
+
+    if (strcmp(header->compression_type, "gzip") == 0) {
+        bytes = gunzip(data, LOAD_IMAGE_MAX_DECOMPRESSED_BYTES, *buffer + ploff, plsize);
+#ifdef CONFIG_ZSTD

hw/arm/boot.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 912

Quoted 10 lines
hw/arm/boot.c +10 -0
+    } 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;

Moving the "unrecognized compression type" error path down to here means that we have moved it below the g_malloc() of the data buffer, so we now need to g_free() to avoid a leak.

hw/arm/boot.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 917

Quoted 5 lines
hw/arm/boot.c +2 -0
+    }
+
     if (bytes < 0) {
         fprintf(stderr, "failed to decompress EFI zboot image\n");
         g_free(data);

Otherwise I think this looks OK.

thanks
-- PMM

#2 lore raw
Quoted 59 lines · Peter Maydell
Quoted 14 lines · Daan De Meyer
---
 hw/arm/boot.c       |  2 +-
 hw/core/loader.c    | 36 ++++++++++++++++++++++++------------
 hw/nvram/fw_cfg.c   |  2 +-
 include/hw/loader.h |  2 +-
 4 files changed, 27 insertions(+), 15 deletions(-)
hw/arm/boot.c +0 -0
index e77d8679d8..c0dec0343a 100644

hw/arm/boot.c static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, · line 830

Quoted 6 lines
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);

I would either not bother renaming this constant, or else do it in a preliminary patch of its own in the series. (That makes the part of the patch which is making the functional change easier to read and review.)

Quoted 32 lines
hw/arm/boot.c +11 -10
@@ -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);
 
@@ -898,8 +895,23 @@ 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);
+
+    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,
Quoted 8 lines

hw/arm/boot.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 912

Quoted 4 lines
hw/arm/boot.c +4 -0
+                "unable to handle EFI zboot image with \"%.*s\" compression\n",
+                (int)sizeof(header->compression_type) - 1,
+                header->compression_type);
+        return -1;

Moving the "unrecognized compression type" error path down to here means that we have moved it below the g_malloc() of the data buffer, so we now need to g_free() to avoid a leak.

Could we not declare:

      g_autofree uint8_t *data = NULL;

and drop the cleanup?

Quoted 11 lines

hw/arm/boot.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 917

Quoted 6 lines
hw/arm/boot.c +2 -0
+    }
+
     if (bytes < 0) {
         fprintf(stderr, "failed to decompress EFI zboot image\n");
         g_free(data);

Otherwise I think this looks OK.

thanks
-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#3 lore raw
Quoted 15 lines · Alex Bennée
Quoted 11 lines · Peter Maydell

hw/core/loader.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 912

Quoted 6 lines · Daan De Meyer
hw/core/loader.c +6 -0
+    } else {
+        fprintf(stderr,
+                "unable to handle EFI zboot image with \"%.*s\" compression\n",
+                (int)sizeof(header->compression_type) - 1,
+                header->compression_type);
+        return -1;

Moving the "unrecognized compression type" error path down to here means that we have moved it below the g_malloc() of the data buffer, so we now need to g_free() to avoid a leak.

Quoted 5 lines

Could we not declare:

      g_autofree uint8_t *data = NULL;

and drop the cleanup?

You could, if you wanted to, convert the file to g_autofree, yes. You would need to also adjust the tail end of the function in the success path to do
*buffer = g_realloc(g_steal_pointer(&data), bytes);
so we don't double-free (and drop the cleanup in other error exit paths).

I don't mind either way, but if we want to convert to g_autofree we should do it as a preliminary patch.

-- PMM

#4 lore raw
Quoted 17 lines · Peter Maydell
Quoted 15 lines · Alex Bennée
Quoted 11 lines · Peter Maydell

hw/core/loader.c ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size) · line 912

Quoted 6 lines · Daan De Meyer
hw/core/loader.c +6 -0
+    } else {
+        fprintf(stderr,
+                "unable to handle EFI zboot image with \"%.*s\" compression\n",
+                (int)sizeof(header->compression_type) - 1,
+                header->compression_type);
+        return -1;

Moving the "unrecognized compression type" error path down to here means that we have moved it below the g_malloc() of the data buffer, so we now need to g_free() to avoid a leak.

Quoted 7 lines
Quoted 5 lines

Could we not declare:

      g_autofree uint8_t *data = NULL;

and drop the cleanup?

You could, if you wanted to, convert the file to g_autofree, yes.

Whoops -- I meant "convert the function" here; I wasn't trying to suggest doing the whole file (which is probably a bad idea).

-- PMM