blob: 932e3e8036d162217a752ae3d7f5535a55ff46b5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marian Balakowicz321359f2008-01-08 18:11:43 +01002/*
3 * (C) Copyright 2000-2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Marian Balakowicz321359f2008-01-08 18:11:43 +01005 */
6
7#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <blk.h>
Marian Balakowicz321359f2008-01-08 18:11:43 +01009#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070010#include <console.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060011#include <div64.h>
12#include <gzip.h>
Marian Balakowicz321359f2008-01-08 18:11:43 +010013#include <image.h>
14#include <malloc.h>
Clemens Gruberd0250212016-08-29 17:10:36 +020015#include <memalign.h>
Simon Glass3db71102019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060017#include <watchdog.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020018#include <u-boot/zlib.h>
Marian Balakowicz321359f2008-01-08 18:11:43 +010019
Eric Nelson918e9eb2015-02-17 11:30:30 -070020#define HEADER0 '\x1f'
21#define HEADER1 '\x8b'
Marian Balakowicz321359f2008-01-08 18:11:43 +010022#define ZALLOC_ALIGNMENT 16
23#define HEAD_CRC 2
24#define EXTRA_FIELD 4
25#define ORIG_NAME 8
26#define COMMENT 0x10
27#define RESERVED 0xe0
28#define DEFLATED 8
29
Mike Frysingere3ed0572012-04-09 13:39:53 +000030void *gzalloc(void *x, unsigned items, unsigned size)
Marian Balakowicz321359f2008-01-08 18:11:43 +010031{
32 void *p;
33
34 size *= items;
35 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
36
37 p = malloc (size);
38
39 return (p);
40}
41
Mike Frysingere3ed0572012-04-09 13:39:53 +000042void gzfree(void *x, void *addr, unsigned nb)
Marian Balakowicz321359f2008-01-08 18:11:43 +010043{
44 free (addr);
45}
46
Jean-Jacques Hiblot376ddf92017-09-15 12:57:29 +020047int gzip_parse_header(const unsigned char *src, unsigned long len)
Marian Balakowicz321359f2008-01-08 18:11:43 +010048{
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +020049 int i, flags;
Marian Balakowicz321359f2008-01-08 18:11:43 +010050
51 /* skip header */
52 i = 10;
53 flags = src[3];
54 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
55 puts ("Error: Bad gzipped data\n");
56 return (-1);
57 }
58 if ((flags & EXTRA_FIELD) != 0)
59 i = 12 + src[10] + (src[11] << 8);
60 if ((flags & ORIG_NAME) != 0)
61 while (src[i++] != 0)
62 ;
63 if ((flags & COMMENT) != 0)
64 while (src[i++] != 0)
65 ;
66 if ((flags & HEAD_CRC) != 0)
67 i += 2;
Jean-Jacques Hiblot376ddf92017-09-15 12:57:29 +020068 if (i >= len) {
Marian Balakowicz321359f2008-01-08 18:11:43 +010069 puts ("Error: gunzip out of data in header\n");
70 return (-1);
71 }
Jean-Jacques Hiblot376ddf92017-09-15 12:57:29 +020072 return i;
73}
Marian Balakowicz321359f2008-01-08 18:11:43 +010074
Jean-Jacques Hiblot376ddf92017-09-15 12:57:29 +020075int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
76{
77 int offset = gzip_parse_header(src, *lenp);
78
79 if (offset < 0)
80 return offset;
81
82 return zunzip(dst, dstlen, src, lenp, 1, offset);
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +020083}
84
Tom Rini1811b7d2016-01-14 13:02:05 -050085#ifdef CONFIG_CMD_UNZIP
Eric Nelson918e9eb2015-02-17 11:30:30 -070086__weak
Simon Glass5a4f10d2021-09-25 07:03:13 -060087void gzwrite_progress_init(ulong expectedsize)
Eric Nelson918e9eb2015-02-17 11:30:30 -070088{
89 putc('\n');
90}
91
92__weak
93void gzwrite_progress(int iteration,
Simon Glass5a4f10d2021-09-25 07:03:13 -060094 ulong bytes_written,
95 ulong total_bytes)
Eric Nelson918e9eb2015-02-17 11:30:30 -070096{
97 if (0 == (iteration & 3))
Simon Glass5a4f10d2021-09-25 07:03:13 -060098 printf("%lu/%lu\r", bytes_written, total_bytes);
Eric Nelson918e9eb2015-02-17 11:30:30 -070099}
100
101__weak
102void gzwrite_progress_finish(int returnval,
Simon Glass5a4f10d2021-09-25 07:03:13 -0600103 ulong bytes_written,
104 ulong total_bytes,
Eric Nelson918e9eb2015-02-17 11:30:30 -0700105 u32 expected_crc,
106 u32 calculated_crc)
107{
108 if (0 == returnval) {
Simon Glass5a4f10d2021-09-25 07:03:13 -0600109 printf("\n\t%lu bytes, crc 0x%08x\n",
Eric Nelson918e9eb2015-02-17 11:30:30 -0700110 total_bytes, calculated_crc);
111 } else {
Simon Glass5a4f10d2021-09-25 07:03:13 -0600112 printf("\n\tuncompressed %lu of %lu\n"
Eric Nelson918e9eb2015-02-17 11:30:30 -0700113 "\tcrcs == 0x%08x/0x%08x\n",
114 bytes_written, total_bytes,
115 expected_crc, calculated_crc);
116 }
117}
118
119int gzwrite(unsigned char *src, int len,
Simon Glass4101f682016-02-29 15:25:34 -0700120 struct blk_desc *dev,
Eric Nelson918e9eb2015-02-17 11:30:30 -0700121 unsigned long szwritebuf,
Simon Glass5a4f10d2021-09-25 07:03:13 -0600122 ulong startoffs,
123 ulong szexpected)
Eric Nelson918e9eb2015-02-17 11:30:30 -0700124{
125 int i, flags;
126 z_stream s;
127 int r = 0;
128 unsigned char *writebuf;
129 unsigned crc = 0;
Simon Glass5a4f10d2021-09-25 07:03:13 -0600130 ulong totalfilled = 0;
Eric Nelson918e9eb2015-02-17 11:30:30 -0700131 lbaint_t blksperbuf, outblock;
132 u32 expected_crc;
133 u32 payload_size;
134 int iteration = 0;
135
136 if (!szwritebuf ||
137 (szwritebuf % dev->blksz) ||
138 (szwritebuf < dev->blksz)) {
139 printf("%s: size %lu not a multiple of %lu\n",
140 __func__, szwritebuf, dev->blksz);
141 return -1;
142 }
143
144 if (startoffs & (dev->blksz-1)) {
Simon Glass5a4f10d2021-09-25 07:03:13 -0600145 printf("%s: start offset %lu not a multiple of %lu\n",
Eric Nelson918e9eb2015-02-17 11:30:30 -0700146 __func__, startoffs, dev->blksz);
147 return -1;
148 }
149
150 blksperbuf = szwritebuf / dev->blksz;
151 outblock = lldiv(startoffs, dev->blksz);
152
153 /* skip header */
154 i = 10;
155 flags = src[3];
156 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
157 puts("Error: Bad gzipped data\n");
158 return -1;
159 }
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)
164 ;
165 if ((flags & COMMENT) != 0)
166 while (src[i++] != 0)
167 ;
168 if ((flags & HEAD_CRC) != 0)
169 i += 2;
170
171 if (i >= len-8) {
172 puts("Error: gunzip out of data in header");
173 return -1;
174 }
175
176 payload_size = len - i - 8;
177
178 memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
179 expected_crc = le32_to_cpu(expected_crc);
180 u32 szuncompressed;
181 memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
182 if (szexpected == 0) {
183 szexpected = le32_to_cpu(szuncompressed);
184 } else if (szuncompressed != (u32)szexpected) {
Simon Glass5a4f10d2021-09-25 07:03:13 -0600185 printf("size of %lx doesn't match trailer low bits %x\n",
Eric Nelson918e9eb2015-02-17 11:30:30 -0700186 szexpected, szuncompressed);
187 return -1;
188 }
189 if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
Simon Glass5a4f10d2021-09-25 07:03:13 -0600190 printf("%s: uncompressed size %lu exceeds device size\n",
Eric Nelson918e9eb2015-02-17 11:30:30 -0700191 __func__, szexpected);
192 return -1;
193 }
194
195 gzwrite_progress_init(szexpected);
196
197 s.zalloc = gzalloc;
198 s.zfree = gzfree;
199
200 r = inflateInit2(&s, -MAX_WBITS);
201 if (r != Z_OK) {
202 printf("Error: inflateInit2() returned %d\n", r);
203 return -1;
204 }
205
206 s.next_in = src + i;
207 s.avail_in = payload_size+8;
Clemens Gruberd0250212016-08-29 17:10:36 +0200208 writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf);
Eric Nelson918e9eb2015-02-17 11:30:30 -0700209
210 /* decompress until deflate stream ends or end of file */
211 do {
212 if (s.avail_in == 0) {
213 printf("%s: weird termination with result %d\n",
214 __func__, r);
215 break;
216 }
217
218 /* run inflate() on input until output buffer not full */
219 do {
220 unsigned long blocks_written;
221 int numfilled;
222 lbaint_t writeblocks;
223
224 s.avail_out = szwritebuf;
225 s.next_out = writebuf;
226 r = inflate(&s, Z_SYNC_FLUSH);
227 if ((r != Z_OK) &&
228 (r != Z_STREAM_END)) {
229 printf("Error: inflate() returned %d\n", r);
230 goto out;
231 }
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)
237 / dev->blksz;
238 memset(writebuf+numfilled, 0,
239 dev->blksz-(numfilled%dev->blksz));
240 } else {
241 writeblocks = blksperbuf;
242 }
243
244 gzwrite_progress(iteration++,
245 totalfilled,
246 szexpected);
Eric Nelson6a3bf3e2016-04-11 15:21:37 -0700247 blocks_written = blk_dwrite(dev, outblock,
248 writeblocks, writebuf);
Eric Nelson918e9eb2015-02-17 11:30:30 -0700249 outblock += blocks_written;
250 if (ctrlc()) {
251 puts("abort\n");
252 goto out;
253 }
Stefan Roese29caf932022-09-02 14:10:46 +0200254 schedule();
Eric Nelson918e9eb2015-02-17 11:30:30 -0700255 } while (s.avail_out == 0);
256 /* done when inflate() says it's done */
257 } while (r != Z_STREAM_END);
258
259 if ((szexpected != totalfilled) ||
260 (crc != expected_crc))
261 r = -1;
262 else
263 r = 0;
264
265out:
266 gzwrite_progress_finish(r, totalfilled, szexpected,
267 expected_crc, crc);
268 free(writebuf);
269 inflateEnd(&s);
270
271 return r;
272}
Tom Rini1811b7d2016-01-14 13:02:05 -0500273#endif
Eric Nelson918e9eb2015-02-17 11:30:30 -0700274
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200275/*
276 * Uncompress blocks compressed with zlib without headers
277 */
278int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
279 int stoponerr, int offset)
280{
281 z_stream s;
Simon Glass9c55c542014-12-02 13:17:39 -0700282 int err = 0;
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200283 int r;
284
Mike Frysingere3ed0572012-04-09 13:39:53 +0000285 s.zalloc = gzalloc;
286 s.zfree = gzfree;
Marian Balakowicz321359f2008-01-08 18:11:43 +0100287
288 r = inflateInit2(&s, -MAX_WBITS);
289 if (r != Z_OK) {
Eric Nelson918e9eb2015-02-17 11:30:30 -0700290 printf("Error: inflateInit2() returned %d\n", r);
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200291 return -1;
Marian Balakowicz321359f2008-01-08 18:11:43 +0100292 }
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200293 s.next_in = src + offset;
294 s.avail_in = *lenp - offset;
Marian Balakowicz321359f2008-01-08 18:11:43 +0100295 s.next_out = dst;
296 s.avail_out = dstlen;
Catalin Raduf039ada2011-02-04 14:35:47 +0200297 do {
298 r = inflate(&s, Z_FINISH);
Kees Cookb75650d2013-08-16 07:59:13 -0700299 if (stoponerr == 1 && r != Z_STREAM_END &&
Stephen Warren19346652016-01-29 13:26:58 -0700300 (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
Catalin Raduf039ada2011-02-04 14:35:47 +0200301 printf("Error: inflate() returned %d\n", r);
Simon Glass9c55c542014-12-02 13:17:39 -0700302 err = -1;
303 break;
Catalin Raduf039ada2011-02-04 14:35:47 +0200304 }
Catalin Raduf039ada2011-02-04 14:35:47 +0200305 } while (r == Z_BUF_ERROR);
Marian Balakowicz321359f2008-01-08 18:11:43 +0100306 *lenp = s.next_out - (unsigned char *) dst;
307 inflateEnd(&s);
308
Simon Glass9c55c542014-12-02 13:17:39 -0700309 return err;
Marian Balakowicz321359f2008-01-08 18:11:43 +0100310}