blob: 1d65616c13d4ded7dd84b302fbe8f9e12cae9d01 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marian Balakowiczbe946ba2008-01-08 18:11:43 +01002/*
3 * (C) Copyright 2000-2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Marian Balakowiczbe946ba2008-01-08 18:11:43 +01005 */
6
7#include <common.h>
Marian Balakowiczbe946ba2008-01-08 18:11:43 +01008#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Simon Glass1a974af2019-08-01 09:46:36 -060010#include <div64.h>
11#include <gzip.h>
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010012#include <image.h>
13#include <malloc.h>
Clemens Gruber8a80c332016-08-29 17:10:36 +020014#include <memalign.h>
Simon Glass1a974af2019-08-01 09:46:36 -060015#include <watchdog.h>
Jean-Christophe PLAGNIOL-VILLARD6bb94492009-04-04 12:49:11 +020016#include <u-boot/zlib.h>
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010017
Eric Nelson5441b042015-02-17 11:30:30 -070018#define HEADER0 '\x1f'
19#define HEADER1 '\x8b'
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010020#define ZALLOC_ALIGNMENT 16
21#define HEAD_CRC 2
22#define EXTRA_FIELD 4
23#define ORIG_NAME 8
24#define COMMENT 0x10
25#define RESERVED 0xe0
26#define DEFLATED 8
27
Mike Frysinger802383b2012-04-09 13:39:53 +000028void *gzalloc(void *x, unsigned items, unsigned size)
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010029{
30 void *p;
31
32 size *= items;
33 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
34
35 p = malloc (size);
36
37 return (p);
38}
39
Mike Frysinger802383b2012-04-09 13:39:53 +000040void gzfree(void *x, void *addr, unsigned nb)
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010041{
42 free (addr);
43}
44
Jean-Jacques Hiblot0d3aa342017-09-15 12:57:29 +020045int gzip_parse_header(const unsigned char *src, unsigned long len)
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010046{
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +020047 int i, flags;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010048
49 /* skip header */
50 i = 10;
51 flags = src[3];
52 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
53 puts ("Error: Bad gzipped data\n");
54 return (-1);
55 }
56 if ((flags & EXTRA_FIELD) != 0)
57 i = 12 + src[10] + (src[11] << 8);
58 if ((flags & ORIG_NAME) != 0)
59 while (src[i++] != 0)
60 ;
61 if ((flags & COMMENT) != 0)
62 while (src[i++] != 0)
63 ;
64 if ((flags & HEAD_CRC) != 0)
65 i += 2;
Jean-Jacques Hiblot0d3aa342017-09-15 12:57:29 +020066 if (i >= len) {
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010067 puts ("Error: gunzip out of data in header\n");
68 return (-1);
69 }
Jean-Jacques Hiblot0d3aa342017-09-15 12:57:29 +020070 return i;
71}
72
73int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
74{
75 int offset = gzip_parse_header(src, *lenp);
76
77 if (offset < 0)
78 return offset;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010079
Jean-Jacques Hiblot0d3aa342017-09-15 12:57:29 +020080 return zunzip(dst, dstlen, src, lenp, 1, offset);
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +020081}
82
Tom Rinib5118862016-01-14 13:02:05 -050083#ifdef CONFIG_CMD_UNZIP
Eric Nelson5441b042015-02-17 11:30:30 -070084__weak
85void gzwrite_progress_init(u64 expectedsize)
86{
87 putc('\n');
88}
89
90__weak
91void gzwrite_progress(int iteration,
92 u64 bytes_written,
93 u64 total_bytes)
94{
95 if (0 == (iteration & 3))
96 printf("%llu/%llu\r", bytes_written, total_bytes);
97}
98
99__weak
100void gzwrite_progress_finish(int returnval,
101 u64 bytes_written,
102 u64 total_bytes,
103 u32 expected_crc,
104 u32 calculated_crc)
105{
106 if (0 == returnval) {
107 printf("\n\t%llu bytes, crc 0x%08x\n",
108 total_bytes, calculated_crc);
109 } else {
110 printf("\n\tuncompressed %llu of %llu\n"
111 "\tcrcs == 0x%08x/0x%08x\n",
112 bytes_written, total_bytes,
113 expected_crc, calculated_crc);
114 }
115}
116
117int gzwrite(unsigned char *src, int len,
Simon Glasse3394752016-02-29 15:25:34 -0700118 struct blk_desc *dev,
Eric Nelson5441b042015-02-17 11:30:30 -0700119 unsigned long szwritebuf,
120 u64 startoffs,
121 u64 szexpected)
122{
123 int i, flags;
124 z_stream s;
125 int r = 0;
126 unsigned char *writebuf;
127 unsigned crc = 0;
128 u64 totalfilled = 0;
129 lbaint_t blksperbuf, outblock;
130 u32 expected_crc;
131 u32 payload_size;
132 int iteration = 0;
133
134 if (!szwritebuf ||
135 (szwritebuf % dev->blksz) ||
136 (szwritebuf < dev->blksz)) {
137 printf("%s: size %lu not a multiple of %lu\n",
138 __func__, szwritebuf, dev->blksz);
139 return -1;
140 }
141
142 if (startoffs & (dev->blksz-1)) {
143 printf("%s: start offset %llu not a multiple of %lu\n",
144 __func__, startoffs, dev->blksz);
145 return -1;
146 }
147
148 blksperbuf = szwritebuf / dev->blksz;
149 outblock = lldiv(startoffs, dev->blksz);
150
151 /* skip header */
152 i = 10;
153 flags = src[3];
154 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
155 puts("Error: Bad gzipped data\n");
156 return -1;
157 }
158 if ((flags & EXTRA_FIELD) != 0)
159 i = 12 + src[10] + (src[11] << 8);
160 if ((flags & ORIG_NAME) != 0)
161 while (src[i++] != 0)
162 ;
163 if ((flags & COMMENT) != 0)
164 while (src[i++] != 0)
165 ;
166 if ((flags & HEAD_CRC) != 0)
167 i += 2;
168
169 if (i >= len-8) {
170 puts("Error: gunzip out of data in header");
171 return -1;
172 }
173
174 payload_size = len - i - 8;
175
176 memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
177 expected_crc = le32_to_cpu(expected_crc);
178 u32 szuncompressed;
179 memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
180 if (szexpected == 0) {
181 szexpected = le32_to_cpu(szuncompressed);
182 } else if (szuncompressed != (u32)szexpected) {
183 printf("size of %llx doesn't match trailer low bits %x\n",
184 szexpected, szuncompressed);
185 return -1;
186 }
187 if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
188 printf("%s: uncompressed size %llu exceeds device size\n",
189 __func__, szexpected);
190 return -1;
191 }
192
193 gzwrite_progress_init(szexpected);
194
195 s.zalloc = gzalloc;
196 s.zfree = gzfree;
197
198 r = inflateInit2(&s, -MAX_WBITS);
199 if (r != Z_OK) {
200 printf("Error: inflateInit2() returned %d\n", r);
201 return -1;
202 }
203
204 s.next_in = src + i;
205 s.avail_in = payload_size+8;
Clemens Gruber8a80c332016-08-29 17:10:36 +0200206 writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf);
Eric Nelson5441b042015-02-17 11:30:30 -0700207
208 /* decompress until deflate stream ends or end of file */
209 do {
210 if (s.avail_in == 0) {
211 printf("%s: weird termination with result %d\n",
212 __func__, r);
213 break;
214 }
215
216 /* run inflate() on input until output buffer not full */
217 do {
218 unsigned long blocks_written;
219 int numfilled;
220 lbaint_t writeblocks;
221
222 s.avail_out = szwritebuf;
223 s.next_out = writebuf;
224 r = inflate(&s, Z_SYNC_FLUSH);
225 if ((r != Z_OK) &&
226 (r != Z_STREAM_END)) {
227 printf("Error: inflate() returned %d\n", r);
228 goto out;
229 }
230 numfilled = szwritebuf - s.avail_out;
231 crc = crc32(crc, writebuf, numfilled);
232 totalfilled += numfilled;
233 if (numfilled < szwritebuf) {
234 writeblocks = (numfilled+dev->blksz-1)
235 / dev->blksz;
236 memset(writebuf+numfilled, 0,
237 dev->blksz-(numfilled%dev->blksz));
238 } else {
239 writeblocks = blksperbuf;
240 }
241
242 gzwrite_progress(iteration++,
243 totalfilled,
244 szexpected);
Eric Nelson91634992016-04-11 15:21:37 -0700245 blocks_written = blk_dwrite(dev, outblock,
246 writeblocks, writebuf);
Eric Nelson5441b042015-02-17 11:30:30 -0700247 outblock += blocks_written;
248 if (ctrlc()) {
249 puts("abort\n");
250 goto out;
251 }
252 WATCHDOG_RESET();
253 } while (s.avail_out == 0);
254 /* done when inflate() says it's done */
255 } while (r != Z_STREAM_END);
256
257 if ((szexpected != totalfilled) ||
258 (crc != expected_crc))
259 r = -1;
260 else
261 r = 0;
262
263out:
264 gzwrite_progress_finish(r, totalfilled, szexpected,
265 expected_crc, crc);
266 free(writebuf);
267 inflateEnd(&s);
268
269 return r;
270}
Tom Rinib5118862016-01-14 13:02:05 -0500271#endif
Eric Nelson5441b042015-02-17 11:30:30 -0700272
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200273/*
274 * Uncompress blocks compressed with zlib without headers
275 */
276int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
277 int stoponerr, int offset)
278{
279 z_stream s;
Simon Glass49d42c02014-12-02 13:17:39 -0700280 int err = 0;
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200281 int r;
282
Mike Frysinger802383b2012-04-09 13:39:53 +0000283 s.zalloc = gzalloc;
284 s.zfree = gzfree;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100285
286 r = inflateInit2(&s, -MAX_WBITS);
287 if (r != Z_OK) {
Eric Nelson5441b042015-02-17 11:30:30 -0700288 printf("Error: inflateInit2() returned %d\n", r);
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200289 return -1;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100290 }
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200291 s.next_in = src + offset;
292 s.avail_in = *lenp - offset;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100293 s.next_out = dst;
294 s.avail_out = dstlen;
Catalin Radu456874f2011-02-04 14:35:47 +0200295 do {
296 r = inflate(&s, Z_FINISH);
Kees Cooked1469a2013-08-16 07:59:13 -0700297 if (stoponerr == 1 && r != Z_STREAM_END &&
Stephen Warren0d1c43a2016-01-29 13:26:58 -0700298 (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
Catalin Radu456874f2011-02-04 14:35:47 +0200299 printf("Error: inflate() returned %d\n", r);
Simon Glass49d42c02014-12-02 13:17:39 -0700300 err = -1;
301 break;
Catalin Radu456874f2011-02-04 14:35:47 +0200302 }
Catalin Radu456874f2011-02-04 14:35:47 +0200303 } while (r == Z_BUF_ERROR);
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100304 *lenp = s.next_out - (unsigned char *) dst;
305 inflateEnd(&s);
306
Simon Glass49d42c02014-12-02 13:17:39 -0700307 return err;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100308}