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