Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2006 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <watchdog.h> |
| 10 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 12 | #include <image.h> |
| 13 | #include <malloc.h> |
Clemens Gruber | 8a80c33 | 2016-08-29 17:10:36 +0200 | [diff] [blame^] | 14 | #include <memalign.h> |
Jean-Christophe PLAGNIOL-VILLARD | 6bb9449 | 2009-04-04 12:49:11 +0200 | [diff] [blame] | 15 | #include <u-boot/zlib.h> |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 16 | #include <div64.h> |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 17 | |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 18 | #define HEADER0 '\x1f' |
| 19 | #define HEADER1 '\x8b' |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 20 | #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 Frysinger | 802383b | 2012-04-09 13:39:53 +0000 | [diff] [blame] | 28 | void *gzalloc(void *x, unsigned items, unsigned size) |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 29 | { |
| 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 Frysinger | 802383b | 2012-04-09 13:39:53 +0000 | [diff] [blame] | 40 | void gzfree(void *x, void *addr, unsigned nb) |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 41 | { |
| 42 | free (addr); |
| 43 | } |
| 44 | |
| 45 | int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp) |
| 46 | { |
Ricardo Ribalda Delgado | 4dc9fa6 | 2009-04-27 18:33:32 +0200 | [diff] [blame] | 47 | int i, flags; |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 48 | |
| 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; |
| 66 | if (i >= *lenp) { |
| 67 | puts ("Error: gunzip out of data in header\n"); |
| 68 | return (-1); |
| 69 | } |
| 70 | |
Ricardo Ribalda Delgado | 4dc9fa6 | 2009-04-27 18:33:32 +0200 | [diff] [blame] | 71 | return zunzip(dst, dstlen, src, lenp, 1, i); |
| 72 | } |
| 73 | |
Tom Rini | b511886 | 2016-01-14 13:02:05 -0500 | [diff] [blame] | 74 | #ifdef CONFIG_CMD_UNZIP |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 75 | __weak |
| 76 | void gzwrite_progress_init(u64 expectedsize) |
| 77 | { |
| 78 | putc('\n'); |
| 79 | } |
| 80 | |
| 81 | __weak |
| 82 | void gzwrite_progress(int iteration, |
| 83 | u64 bytes_written, |
| 84 | u64 total_bytes) |
| 85 | { |
| 86 | if (0 == (iteration & 3)) |
| 87 | printf("%llu/%llu\r", bytes_written, total_bytes); |
| 88 | } |
| 89 | |
| 90 | __weak |
| 91 | void gzwrite_progress_finish(int returnval, |
| 92 | u64 bytes_written, |
| 93 | u64 total_bytes, |
| 94 | u32 expected_crc, |
| 95 | u32 calculated_crc) |
| 96 | { |
| 97 | if (0 == returnval) { |
| 98 | printf("\n\t%llu bytes, crc 0x%08x\n", |
| 99 | total_bytes, calculated_crc); |
| 100 | } else { |
| 101 | printf("\n\tuncompressed %llu of %llu\n" |
| 102 | "\tcrcs == 0x%08x/0x%08x\n", |
| 103 | bytes_written, total_bytes, |
| 104 | expected_crc, calculated_crc); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | int gzwrite(unsigned char *src, int len, |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 109 | struct blk_desc *dev, |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 110 | unsigned long szwritebuf, |
| 111 | u64 startoffs, |
| 112 | u64 szexpected) |
| 113 | { |
| 114 | int i, flags; |
| 115 | z_stream s; |
| 116 | int r = 0; |
| 117 | unsigned char *writebuf; |
| 118 | unsigned crc = 0; |
| 119 | u64 totalfilled = 0; |
| 120 | lbaint_t blksperbuf, outblock; |
| 121 | u32 expected_crc; |
| 122 | u32 payload_size; |
| 123 | int iteration = 0; |
| 124 | |
| 125 | if (!szwritebuf || |
| 126 | (szwritebuf % dev->blksz) || |
| 127 | (szwritebuf < dev->blksz)) { |
| 128 | printf("%s: size %lu not a multiple of %lu\n", |
| 129 | __func__, szwritebuf, dev->blksz); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | if (startoffs & (dev->blksz-1)) { |
| 134 | printf("%s: start offset %llu not a multiple of %lu\n", |
| 135 | __func__, startoffs, dev->blksz); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | blksperbuf = szwritebuf / dev->blksz; |
| 140 | outblock = lldiv(startoffs, dev->blksz); |
| 141 | |
| 142 | /* skip header */ |
| 143 | i = 10; |
| 144 | flags = src[3]; |
| 145 | if (src[2] != DEFLATED || (flags & RESERVED) != 0) { |
| 146 | puts("Error: Bad gzipped data\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | if ((flags & EXTRA_FIELD) != 0) |
| 150 | i = 12 + src[10] + (src[11] << 8); |
| 151 | if ((flags & ORIG_NAME) != 0) |
| 152 | while (src[i++] != 0) |
| 153 | ; |
| 154 | if ((flags & COMMENT) != 0) |
| 155 | while (src[i++] != 0) |
| 156 | ; |
| 157 | if ((flags & HEAD_CRC) != 0) |
| 158 | i += 2; |
| 159 | |
| 160 | if (i >= len-8) { |
| 161 | puts("Error: gunzip out of data in header"); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | payload_size = len - i - 8; |
| 166 | |
| 167 | memcpy(&expected_crc, src + len - 8, sizeof(expected_crc)); |
| 168 | expected_crc = le32_to_cpu(expected_crc); |
| 169 | u32 szuncompressed; |
| 170 | memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed)); |
| 171 | if (szexpected == 0) { |
| 172 | szexpected = le32_to_cpu(szuncompressed); |
| 173 | } else if (szuncompressed != (u32)szexpected) { |
| 174 | printf("size of %llx doesn't match trailer low bits %x\n", |
| 175 | szexpected, szuncompressed); |
| 176 | return -1; |
| 177 | } |
| 178 | if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) { |
| 179 | printf("%s: uncompressed size %llu exceeds device size\n", |
| 180 | __func__, szexpected); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | gzwrite_progress_init(szexpected); |
| 185 | |
| 186 | s.zalloc = gzalloc; |
| 187 | s.zfree = gzfree; |
| 188 | |
| 189 | r = inflateInit2(&s, -MAX_WBITS); |
| 190 | if (r != Z_OK) { |
| 191 | printf("Error: inflateInit2() returned %d\n", r); |
| 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | s.next_in = src + i; |
| 196 | s.avail_in = payload_size+8; |
Clemens Gruber | 8a80c33 | 2016-08-29 17:10:36 +0200 | [diff] [blame^] | 197 | writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf); |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 198 | |
| 199 | /* decompress until deflate stream ends or end of file */ |
| 200 | do { |
| 201 | if (s.avail_in == 0) { |
| 202 | printf("%s: weird termination with result %d\n", |
| 203 | __func__, r); |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | /* run inflate() on input until output buffer not full */ |
| 208 | do { |
| 209 | unsigned long blocks_written; |
| 210 | int numfilled; |
| 211 | lbaint_t writeblocks; |
| 212 | |
| 213 | s.avail_out = szwritebuf; |
| 214 | s.next_out = writebuf; |
| 215 | r = inflate(&s, Z_SYNC_FLUSH); |
| 216 | if ((r != Z_OK) && |
| 217 | (r != Z_STREAM_END)) { |
| 218 | printf("Error: inflate() returned %d\n", r); |
| 219 | goto out; |
| 220 | } |
| 221 | numfilled = szwritebuf - s.avail_out; |
| 222 | crc = crc32(crc, writebuf, numfilled); |
| 223 | totalfilled += numfilled; |
| 224 | if (numfilled < szwritebuf) { |
| 225 | writeblocks = (numfilled+dev->blksz-1) |
| 226 | / dev->blksz; |
| 227 | memset(writebuf+numfilled, 0, |
| 228 | dev->blksz-(numfilled%dev->blksz)); |
| 229 | } else { |
| 230 | writeblocks = blksperbuf; |
| 231 | } |
| 232 | |
| 233 | gzwrite_progress(iteration++, |
| 234 | totalfilled, |
| 235 | szexpected); |
Eric Nelson | 9163499 | 2016-04-11 15:21:37 -0700 | [diff] [blame] | 236 | blocks_written = blk_dwrite(dev, outblock, |
| 237 | writeblocks, writebuf); |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 238 | outblock += blocks_written; |
| 239 | if (ctrlc()) { |
| 240 | puts("abort\n"); |
| 241 | goto out; |
| 242 | } |
| 243 | WATCHDOG_RESET(); |
| 244 | } while (s.avail_out == 0); |
| 245 | /* done when inflate() says it's done */ |
| 246 | } while (r != Z_STREAM_END); |
| 247 | |
| 248 | if ((szexpected != totalfilled) || |
| 249 | (crc != expected_crc)) |
| 250 | r = -1; |
| 251 | else |
| 252 | r = 0; |
| 253 | |
| 254 | out: |
| 255 | gzwrite_progress_finish(r, totalfilled, szexpected, |
| 256 | expected_crc, crc); |
| 257 | free(writebuf); |
| 258 | inflateEnd(&s); |
| 259 | |
| 260 | return r; |
| 261 | } |
Tom Rini | b511886 | 2016-01-14 13:02:05 -0500 | [diff] [blame] | 262 | #endif |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 263 | |
Ricardo Ribalda Delgado | 4dc9fa6 | 2009-04-27 18:33:32 +0200 | [diff] [blame] | 264 | /* |
| 265 | * Uncompress blocks compressed with zlib without headers |
| 266 | */ |
| 267 | int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp, |
| 268 | int stoponerr, int offset) |
| 269 | { |
| 270 | z_stream s; |
Simon Glass | 49d42c0 | 2014-12-02 13:17:39 -0700 | [diff] [blame] | 271 | int err = 0; |
Ricardo Ribalda Delgado | 4dc9fa6 | 2009-04-27 18:33:32 +0200 | [diff] [blame] | 272 | int r; |
| 273 | |
Mike Frysinger | 802383b | 2012-04-09 13:39:53 +0000 | [diff] [blame] | 274 | s.zalloc = gzalloc; |
| 275 | s.zfree = gzfree; |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 276 | |
| 277 | r = inflateInit2(&s, -MAX_WBITS); |
| 278 | if (r != Z_OK) { |
Eric Nelson | 5441b04 | 2015-02-17 11:30:30 -0700 | [diff] [blame] | 279 | printf("Error: inflateInit2() returned %d\n", r); |
Ricardo Ribalda Delgado | 4dc9fa6 | 2009-04-27 18:33:32 +0200 | [diff] [blame] | 280 | return -1; |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 281 | } |
Ricardo Ribalda Delgado | 4dc9fa6 | 2009-04-27 18:33:32 +0200 | [diff] [blame] | 282 | s.next_in = src + offset; |
| 283 | s.avail_in = *lenp - offset; |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 284 | s.next_out = dst; |
| 285 | s.avail_out = dstlen; |
Catalin Radu | 456874f | 2011-02-04 14:35:47 +0200 | [diff] [blame] | 286 | do { |
| 287 | r = inflate(&s, Z_FINISH); |
Kees Cook | ed1469a | 2013-08-16 07:59:13 -0700 | [diff] [blame] | 288 | if (stoponerr == 1 && r != Z_STREAM_END && |
Stephen Warren | 0d1c43a | 2016-01-29 13:26:58 -0700 | [diff] [blame] | 289 | (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) { |
Catalin Radu | 456874f | 2011-02-04 14:35:47 +0200 | [diff] [blame] | 290 | printf("Error: inflate() returned %d\n", r); |
Simon Glass | 49d42c0 | 2014-12-02 13:17:39 -0700 | [diff] [blame] | 291 | err = -1; |
| 292 | break; |
Catalin Radu | 456874f | 2011-02-04 14:35:47 +0200 | [diff] [blame] | 293 | } |
Catalin Radu | 456874f | 2011-02-04 14:35:47 +0200 | [diff] [blame] | 294 | } while (r == Z_BUF_ERROR); |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 295 | *lenp = s.next_out - (unsigned char *) dst; |
| 296 | inflateEnd(&s); |
| 297 | |
Simon Glass | 49d42c0 | 2014-12-02 13:17:39 -0700 | [diff] [blame] | 298 | return err; |
Marian Balakowicz | be946ba | 2008-01-08 18:11:43 +0100 | [diff] [blame] | 299 | } |