Merge branch 'master' of git://git.denx.de/u-boot-video
[oweals/u-boot.git] / lib / gunzip.c
1 /*
2  * (C) Copyright 2000-2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <watchdog.h>
10 #include <command.h>
11 #include <console.h>
12 #include <image.h>
13 #include <malloc.h>
14 #include <u-boot/zlib.h>
15 #include <div64.h>
16
17 #define HEADER0                 '\x1f'
18 #define HEADER1                 '\x8b'
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
27 void *gzalloc(void *x, unsigned items, unsigned size)
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
39 void gzfree(void *x, void *addr, unsigned nb)
40 {
41         free (addr);
42 }
43
44 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
45 {
46         int i, flags;
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
70         return zunzip(dst, dstlen, src, lenp, 1, i);
71 }
72
73 __weak
74 void gzwrite_progress_init(u64 expectedsize)
75 {
76         putc('\n');
77 }
78
79 __weak
80 void gzwrite_progress(int iteration,
81                      u64 bytes_written,
82                      u64 total_bytes)
83 {
84         if (0 == (iteration & 3))
85                 printf("%llu/%llu\r", bytes_written, total_bytes);
86 }
87
88 __weak
89 void gzwrite_progress_finish(int returnval,
90                              u64 bytes_written,
91                              u64 total_bytes,
92                              u32 expected_crc,
93                              u32 calculated_crc)
94 {
95         if (0 == returnval) {
96                 printf("\n\t%llu bytes, crc 0x%08x\n",
97                        total_bytes, calculated_crc);
98         } else {
99                 printf("\n\tuncompressed %llu of %llu\n"
100                        "\tcrcs == 0x%08x/0x%08x\n",
101                        bytes_written, total_bytes,
102                        expected_crc, calculated_crc);
103         }
104 }
105
106 int gzwrite(unsigned char *src, int len,
107             struct block_dev_desc *dev,
108             unsigned long szwritebuf,
109             u64 startoffs,
110             u64 szexpected)
111 {
112         int i, flags;
113         z_stream s;
114         int r = 0;
115         unsigned char *writebuf;
116         unsigned crc = 0;
117         u64 totalfilled = 0;
118         lbaint_t blksperbuf, outblock;
119         u32 expected_crc;
120         u32 payload_size;
121         int iteration = 0;
122
123         if (!szwritebuf ||
124             (szwritebuf % dev->blksz) ||
125             (szwritebuf < dev->blksz)) {
126                 printf("%s: size %lu not a multiple of %lu\n",
127                        __func__, szwritebuf, dev->blksz);
128                 return -1;
129         }
130
131         if (startoffs & (dev->blksz-1)) {
132                 printf("%s: start offset %llu not a multiple of %lu\n",
133                        __func__, startoffs, dev->blksz);
134                 return -1;
135         }
136
137         blksperbuf = szwritebuf / dev->blksz;
138         outblock = lldiv(startoffs, dev->blksz);
139
140         /* skip header */
141         i = 10;
142         flags = src[3];
143         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
144                 puts("Error: Bad gzipped data\n");
145                 return -1;
146         }
147         if ((flags & EXTRA_FIELD) != 0)
148                 i = 12 + src[10] + (src[11] << 8);
149         if ((flags & ORIG_NAME) != 0)
150                 while (src[i++] != 0)
151                         ;
152         if ((flags & COMMENT) != 0)
153                 while (src[i++] != 0)
154                         ;
155         if ((flags & HEAD_CRC) != 0)
156                 i += 2;
157
158         if (i >= len-8) {
159                 puts("Error: gunzip out of data in header");
160                 return -1;
161         }
162
163         payload_size = len - i - 8;
164
165         memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
166         expected_crc = le32_to_cpu(expected_crc);
167         u32 szuncompressed;
168         memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
169         if (szexpected == 0) {
170                 szexpected = le32_to_cpu(szuncompressed);
171         } else if (szuncompressed != (u32)szexpected) {
172                 printf("size of %llx doesn't match trailer low bits %x\n",
173                        szexpected, szuncompressed);
174                 return -1;
175         }
176         if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
177                 printf("%s: uncompressed size %llu exceeds device size\n",
178                        __func__, szexpected);
179                 return -1;
180         }
181
182         gzwrite_progress_init(szexpected);
183
184         s.zalloc = gzalloc;
185         s.zfree = gzfree;
186
187         r = inflateInit2(&s, -MAX_WBITS);
188         if (r != Z_OK) {
189                 printf("Error: inflateInit2() returned %d\n", r);
190                 return -1;
191         }
192
193         s.next_in = src + i;
194         s.avail_in = payload_size+8;
195         writebuf = (unsigned char *)malloc(szwritebuf);
196
197         /* decompress until deflate stream ends or end of file */
198         do {
199                 if (s.avail_in == 0) {
200                         printf("%s: weird termination with result %d\n",
201                                __func__, r);
202                         break;
203                 }
204
205                 /* run inflate() on input until output buffer not full */
206                 do {
207                         unsigned long blocks_written;
208                         int numfilled;
209                         lbaint_t writeblocks;
210
211                         s.avail_out = szwritebuf;
212                         s.next_out = writebuf;
213                         r = inflate(&s, Z_SYNC_FLUSH);
214                         if ((r != Z_OK) &&
215                             (r != Z_STREAM_END)) {
216                                 printf("Error: inflate() returned %d\n", r);
217                                 goto out;
218                         }
219                         numfilled = szwritebuf - s.avail_out;
220                         crc = crc32(crc, writebuf, numfilled);
221                         totalfilled += numfilled;
222                         if (numfilled < szwritebuf) {
223                                 writeblocks = (numfilled+dev->blksz-1)
224                                                 / dev->blksz;
225                                 memset(writebuf+numfilled, 0,
226                                        dev->blksz-(numfilled%dev->blksz));
227                         } else {
228                                 writeblocks = blksperbuf;
229                         }
230
231                         gzwrite_progress(iteration++,
232                                          totalfilled,
233                                          szexpected);
234                         blocks_written = dev->block_write(dev->dev,
235                                                           outblock,
236                                                           writeblocks,
237                                                           writebuf);
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 }
262
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;
270         int err = 0;
271         int r;
272
273         s.zalloc = gzalloc;
274         s.zfree = gzfree;
275
276         r = inflateInit2(&s, -MAX_WBITS);
277         if (r != Z_OK) {
278                 printf("Error: inflateInit2() returned %d\n", r);
279                 return -1;
280         }
281         s.next_in = src + offset;
282         s.avail_in = *lenp - offset;
283         s.next_out = dst;
284         s.avail_out = dstlen;
285         do {
286                 r = inflate(&s, Z_FINISH);
287                 if (stoponerr == 1 && r != Z_STREAM_END &&
288                     (s.avail_out == 0 || r != Z_BUF_ERROR)) {
289                         printf("Error: inflate() returned %d\n", r);
290                         err = -1;
291                         break;
292                 }
293                 s.avail_in = *lenp - offset - (int)(s.next_out - (unsigned char*)dst);
294         } while (r == Z_BUF_ERROR);
295         *lenp = s.next_out - (unsigned char *) dst;
296         inflateEnd(&s);
297
298         return err;
299 }