blob: d2b7ad477916b4a03fbe6b6b439cc76456d6a9cb [file] [log] [blame]
Marian Balakowiczbe946ba2008-01-08 18:11:43 +01001/*
2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <watchdog.h>
26#include <command.h>
27#include <image.h>
28#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD6bb94492009-04-04 12:49:11 +020029#include <u-boot/zlib.h>
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010030
31#define ZALLOC_ALIGNMENT 16
32#define HEAD_CRC 2
33#define EXTRA_FIELD 4
34#define ORIG_NAME 8
35#define COMMENT 0x10
36#define RESERVED 0xe0
37#define DEFLATED 8
38
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010039void *zalloc(void *, unsigned, unsigned);
40void zfree(void *, void *, unsigned);
41
42void *zalloc(void *x, unsigned items, unsigned size)
43{
44 void *p;
45
46 size *= items;
47 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
48
49 p = malloc (size);
50
51 return (p);
52}
53
54void zfree(void *x, void *addr, unsigned nb)
55{
56 free (addr);
57}
58
59int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
60{
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +020061 int i, flags;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010062
63 /* skip header */
64 i = 10;
65 flags = src[3];
66 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
67 puts ("Error: Bad gzipped data\n");
68 return (-1);
69 }
70 if ((flags & EXTRA_FIELD) != 0)
71 i = 12 + src[10] + (src[11] << 8);
72 if ((flags & ORIG_NAME) != 0)
73 while (src[i++] != 0)
74 ;
75 if ((flags & COMMENT) != 0)
76 while (src[i++] != 0)
77 ;
78 if ((flags & HEAD_CRC) != 0)
79 i += 2;
80 if (i >= *lenp) {
81 puts ("Error: gunzip out of data in header\n");
82 return (-1);
83 }
84
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +020085 return zunzip(dst, dstlen, src, lenp, 1, i);
86}
87
88/*
89 * Uncompress blocks compressed with zlib without headers
90 */
91int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
92 int stoponerr, int offset)
93{
94 z_stream s;
95 int r;
96
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010097 s.zalloc = zalloc;
98 s.zfree = zfree;
99#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
100 s.outcb = (cb_func)WATCHDOG_RESET;
101#else
102 s.outcb = Z_NULL;
103#endif /* CONFIG_HW_WATCHDOG */
104
105 r = inflateInit2(&s, -MAX_WBITS);
106 if (r != Z_OK) {
107 printf ("Error: inflateInit2() returned %d\n", r);
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200108 return -1;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100109 }
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200110 s.next_in = src + offset;
111 s.avail_in = *lenp - offset;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100112 s.next_out = dst;
113 s.avail_out = dstlen;
114 r = inflate(&s, Z_FINISH);
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200115 if ((r != Z_STREAM_END) && (stoponerr==1)) {
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100116 printf ("Error: inflate() returned %d\n", r);
Matthias Fuchs1f2b05a2009-01-02 15:11:41 +0100117 inflateEnd(&s);
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100118 return (-1);
119 }
120 *lenp = s.next_out - (unsigned char *) dst;
121 inflateEnd(&s);
122
Ricardo Ribalda Delgado4dc9fa62009-04-27 18:33:32 +0200123 return 0;
Marian Balakowiczbe946ba2008-01-08 18:11:43 +0100124}