1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #include <u-boot/crc.h>
17 #include <u-boot/zlib.h>
19 #define HEADER0 '\x1f'
20 #define HEADER1 '\x8b'
21 #define ZALLOC_ALIGNMENT 16
29 void *gzalloc(void *x, unsigned items, unsigned size)
34 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
41 void gzfree(void *x, void *addr, unsigned nb)
46 int gzip_parse_header(const unsigned char *src, unsigned long len)
53 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
54 puts ("Error: Bad gzipped data\n");
57 if ((flags & EXTRA_FIELD) != 0)
58 i = 12 + src[10] + (src[11] << 8);
59 if ((flags & ORIG_NAME) != 0)
62 if ((flags & COMMENT) != 0)
65 if ((flags & HEAD_CRC) != 0)
68 puts ("Error: gunzip out of data in header\n");
74 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
76 int offset = gzip_parse_header(src, *lenp);
81 return zunzip(dst, dstlen, src, lenp, 1, offset);
84 #ifdef CONFIG_CMD_UNZIP
86 void gzwrite_progress_init(u64 expectedsize)
92 void gzwrite_progress(int iteration,
96 if (0 == (iteration & 3))
97 printf("%llu/%llu\r", bytes_written, total_bytes);
101 void gzwrite_progress_finish(int returnval,
107 if (0 == returnval) {
108 printf("\n\t%llu bytes, crc 0x%08x\n",
109 total_bytes, calculated_crc);
111 printf("\n\tuncompressed %llu of %llu\n"
112 "\tcrcs == 0x%08x/0x%08x\n",
113 bytes_written, total_bytes,
114 expected_crc, calculated_crc);
118 int gzwrite(unsigned char *src, int len,
119 struct blk_desc *dev,
120 unsigned long szwritebuf,
127 unsigned char *writebuf;
130 lbaint_t blksperbuf, outblock;
136 (szwritebuf % dev->blksz) ||
137 (szwritebuf < dev->blksz)) {
138 printf("%s: size %lu not a multiple of %lu\n",
139 __func__, szwritebuf, dev->blksz);
143 if (startoffs & (dev->blksz-1)) {
144 printf("%s: start offset %llu not a multiple of %lu\n",
145 __func__, startoffs, dev->blksz);
149 blksperbuf = szwritebuf / dev->blksz;
150 outblock = lldiv(startoffs, dev->blksz);
155 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
156 puts("Error: Bad gzipped data\n");
159 if ((flags & EXTRA_FIELD) != 0)
160 i = 12 + src[10] + (src[11] << 8);
161 if ((flags & ORIG_NAME) != 0)
162 while (src[i++] != 0)
164 if ((flags & COMMENT) != 0)
165 while (src[i++] != 0)
167 if ((flags & HEAD_CRC) != 0)
171 puts("Error: gunzip out of data in header");
175 payload_size = len - i - 8;
177 memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
178 expected_crc = le32_to_cpu(expected_crc);
180 memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
181 if (szexpected == 0) {
182 szexpected = le32_to_cpu(szuncompressed);
183 } else if (szuncompressed != (u32)szexpected) {
184 printf("size of %llx doesn't match trailer low bits %x\n",
185 szexpected, szuncompressed);
188 if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
189 printf("%s: uncompressed size %llu exceeds device size\n",
190 __func__, szexpected);
194 gzwrite_progress_init(szexpected);
199 r = inflateInit2(&s, -MAX_WBITS);
201 printf("Error: inflateInit2() returned %d\n", r);
206 s.avail_in = payload_size+8;
207 writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf);
209 /* decompress until deflate stream ends or end of file */
211 if (s.avail_in == 0) {
212 printf("%s: weird termination with result %d\n",
217 /* run inflate() on input until output buffer not full */
219 unsigned long blocks_written;
221 lbaint_t writeblocks;
223 s.avail_out = szwritebuf;
224 s.next_out = writebuf;
225 r = inflate(&s, Z_SYNC_FLUSH);
227 (r != Z_STREAM_END)) {
228 printf("Error: inflate() returned %d\n", r);
231 numfilled = szwritebuf - s.avail_out;
232 crc = crc32(crc, writebuf, numfilled);
233 totalfilled += numfilled;
234 if (numfilled < szwritebuf) {
235 writeblocks = (numfilled+dev->blksz-1)
237 memset(writebuf+numfilled, 0,
238 dev->blksz-(numfilled%dev->blksz));
240 writeblocks = blksperbuf;
243 gzwrite_progress(iteration++,
246 blocks_written = blk_dwrite(dev, outblock,
247 writeblocks, writebuf);
248 outblock += blocks_written;
254 } while (s.avail_out == 0);
255 /* done when inflate() says it's done */
256 } while (r != Z_STREAM_END);
258 if ((szexpected != totalfilled) ||
259 (crc != expected_crc))
265 gzwrite_progress_finish(r, totalfilled, szexpected,
275 * Uncompress blocks compressed with zlib without headers
277 int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
278 int stoponerr, int offset)
287 r = inflateInit2(&s, -MAX_WBITS);
289 printf("Error: inflateInit2() returned %d\n", r);
292 s.next_in = src + offset;
293 s.avail_in = *lenp - offset;
295 s.avail_out = dstlen;
297 r = inflate(&s, Z_FINISH);
298 if (stoponerr == 1 && r != Z_STREAM_END &&
299 (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
300 printf("Error: inflate() returned %d\n", r);
304 } while (r == Z_BUF_ERROR);
305 *lenp = s.next_out - (unsigned char *) dst;