Currently, when decompressing a gzip-compressed image during bootm, a
generic error such as "inflate() returned -5" is shown when the buffer is
too small. However, it is not immediately clear that this is caused by
CONFIG_SYS_BOOTM_LEN being too small.

This patch improves error handling by:
- Detecting Z_BUF_ERROR (-5) returned from the inflate() call
- Suggesting the user to increase CONFIG_SYS_BOOTM_LEN when applicable
- Preserving the original return code from zunzip() instead of overwriting
  it with -1

By providing clearer hints when decompression fails due to insufficient
buffer size, this change helps users diagnose and fix boot failures more
easily.

Signed-off-by: Aristo Chen <aristo.c...@canonical.com>
---
 boot/bootm.c | 15 +++++++++++++++
 lib/gunzip.c |  2 +-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/boot/bootm.c b/boot/bootm.c
index f5cbb10f0d1..eae19232487 100644
--- a/boot/bootm.c
+++ b/boot/bootm.c
@@ -34,6 +34,7 @@
 
 #include <bootm.h>
 #include <image.h>
+#include <u-boot/zlib.h>
 
 #define MAX_CMDLINE_SIZE       SZ_4K
 
@@ -573,12 +574,26 @@ static int handle_decomp_error(int comp_type, size_t 
uncomp_size,
                               size_t buf_size, int ret)
 {
        const char *name = genimg_get_comp_name(comp_type);
+       bool likely_buf_too_small = false;
 
        /* ENOSYS means unimplemented compression type, don't reset. */
        if (ret == -ENOSYS)
                return BOOTM_ERR_UNIMPLEMENTED;
 
+       switch (comp_type) {
+       case IH_COMP_GZIP:
+               if (ret == Z_BUF_ERROR) /* -5 */
+                       likely_buf_too_small = true;
+               break;
+       // Add more if necessary
+       default:
+               break;
+       }
+
        if (uncomp_size >= buf_size)
+               likely_buf_too_small = true;
+
+       if (likely_buf_too_small)
                printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
        else
                printf("%s: uncompress error %d\n", name, ret);
diff --git a/lib/gunzip.c b/lib/gunzip.c
index 52007715bda..a05dcde9a75 100644
--- a/lib/gunzip.c
+++ b/lib/gunzip.c
@@ -299,7 +299,7 @@ __rcode int zunzip(void *dst, int dstlen, unsigned char 
*src,
                if (stoponerr == 1 && r != Z_STREAM_END &&
                    (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
                        printf("Error: inflate() returned %d\n", r);
-                       err = -1;
+                       err = r;
                        break;
                }
        } while (r == Z_BUF_ERROR);
-- 
2.34.1

Reply via email to