1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
16 #include <u-boot/crc.h>
18 #include <u-boot/zlib.h>
20 #define HEADER0 '\x1f'
21 #define HEADER1 '\x8b'
22 #define ZALLOC_ALIGNMENT 16
30 void *gzalloc(void *x, unsigned items, unsigned size)
35 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
42 void gzfree(void *x, void *addr, unsigned nb)
47 int gzip_parse_header(const unsigned char *src, unsigned long len)
54 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
55 puts ("Error: Bad gzipped data\n");
58 if ((flags & EXTRA_FIELD) != 0)
59 i = 12 + src[10] + (src[11] << 8);
60 if ((flags & ORIG_NAME) != 0)
63 if ((flags & COMMENT) != 0)
66 if ((flags & HEAD_CRC) != 0)
69 puts ("Error: gunzip out of data in header\n");
75 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
77 int offset = gzip_parse_header(src, *lenp);
82 return zunzip(dst, dstlen, src, lenp, 1, offset);
85 #ifdef CONFIG_CMD_UNZIP
87 void gzwrite_progress_init(u64 expectedsize)
93 void gzwrite_progress(int iteration,
97 if (0 == (iteration & 3))
98 printf("%llu/%llu\r", bytes_written, total_bytes);
102 void gzwrite_progress_finish(int returnval,
108 if (0 == returnval) {
109 printf("\n\t%llu bytes, crc 0x%08x\n",
110 total_bytes, calculated_crc);
112 printf("\n\tuncompressed %llu of %llu\n"
113 "\tcrcs == 0x%08x/0x%08x\n",
114 bytes_written, total_bytes,
115 expected_crc, calculated_crc);
119 int gzwrite(unsigned char *src, int len,
120 struct blk_desc *dev,
121 unsigned long szwritebuf,
128 unsigned char *writebuf;
131 lbaint_t blksperbuf, outblock;
137 (szwritebuf % dev->blksz) ||
138 (szwritebuf < dev->blksz)) {
139 printf("%s: size %lu not a multiple of %lu\n",
140 __func__, szwritebuf, dev->blksz);
144 if (startoffs & (dev->blksz-1)) {
145 printf("%s: start offset %llu not a multiple of %lu\n",
146 __func__, startoffs, dev->blksz);
150 blksperbuf = szwritebuf / dev->blksz;
151 outblock = lldiv(startoffs, dev->blksz);
156 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
157 puts("Error: Bad gzipped data\n");
160 if ((flags & EXTRA_FIELD) != 0)
161 i = 12 + src[10] + (src[11] << 8);
162 if ((flags & ORIG_NAME) != 0)
163 while (src[i++] != 0)
165 if ((flags & COMMENT) != 0)
166 while (src[i++] != 0)
168 if ((flags & HEAD_CRC) != 0)
172 puts("Error: gunzip out of data in header");
176 payload_size = len - i - 8;
178 memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
179 expected_crc = le32_to_cpu(expected_crc);
181 memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
182 if (szexpected == 0) {
183 szexpected = le32_to_cpu(szuncompressed);
184 } else if (szuncompressed != (u32)szexpected) {
185 printf("size of %llx doesn't match trailer low bits %x\n",
186 szexpected, szuncompressed);
189 if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
190 printf("%s: uncompressed size %llu exceeds device size\n",
191 __func__, szexpected);
195 gzwrite_progress_init(szexpected);
200 r = inflateInit2(&s, -MAX_WBITS);
202 printf("Error: inflateInit2() returned %d\n", r);
207 s.avail_in = payload_size+8;
208 writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf);
210 /* decompress until deflate stream ends or end of file */
212 if (s.avail_in == 0) {
213 printf("%s: weird termination with result %d\n",
218 /* run inflate() on input until output buffer not full */
220 unsigned long blocks_written;
222 lbaint_t writeblocks;
224 s.avail_out = szwritebuf;
225 s.next_out = writebuf;
226 r = inflate(&s, Z_SYNC_FLUSH);
228 (r != Z_STREAM_END)) {
229 printf("Error: inflate() returned %d\n", r);
232 numfilled = szwritebuf - s.avail_out;
233 crc = crc32(crc, writebuf, numfilled);
234 totalfilled += numfilled;
235 if (numfilled < szwritebuf) {
236 writeblocks = (numfilled+dev->blksz-1)
238 memset(writebuf+numfilled, 0,
239 dev->blksz-(numfilled%dev->blksz));
241 writeblocks = blksperbuf;
244 gzwrite_progress(iteration++,
247 blocks_written = blk_dwrite(dev, outblock,
248 writeblocks, writebuf);
249 outblock += blocks_written;
255 } while (s.avail_out == 0);
256 /* done when inflate() says it's done */
257 } while (r != Z_STREAM_END);
259 if ((szexpected != totalfilled) ||
260 (crc != expected_crc))
266 gzwrite_progress_finish(r, totalfilled, szexpected,
276 * Uncompress blocks compressed with zlib without headers
278 int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
279 int stoponerr, int offset)
288 r = inflateInit2(&s, -MAX_WBITS);
290 printf("Error: inflateInit2() returned %d\n", r);
293 s.next_in = src + offset;
294 s.avail_in = *lenp - offset;
296 s.avail_out = dstlen;
298 r = inflate(&s, Z_FINISH);
299 if (stoponerr == 1 && r != Z_STREAM_END &&
300 (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
301 printf("Error: inflate() returned %d\n", r);
305 } while (r == Z_BUF_ERROR);
306 *lenp = s.next_out - (unsigned char *) dst;