blob: d59a4482b73c4ce3fdc968ed335dec3ab745212a [file] [log] [blame]
Marian Balakowicz321359f2008-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-VILLARDa31e0912009-04-04 12:49:11 +020029#include <u-boot/zlib.h>
Marian Balakowicz321359f2008-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
39int gunzip(void *, int, unsigned char *, unsigned long *);
40void *zalloc(void *, unsigned, unsigned);
41void zfree(void *, void *, unsigned);
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +020042int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
43 int stoponerr, int offset);
Marian Balakowicz321359f2008-01-08 18:11:43 +010044
45void *zalloc(void *x, unsigned items, unsigned size)
46{
47 void *p;
48
49 size *= items;
50 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
51
52 p = malloc (size);
53
54 return (p);
55}
56
57void zfree(void *x, void *addr, unsigned nb)
58{
59 free (addr);
60}
61
62int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
63{
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +020064 int i, flags;
Marian Balakowicz321359f2008-01-08 18:11:43 +010065
66 /* skip header */
67 i = 10;
68 flags = src[3];
69 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
70 puts ("Error: Bad gzipped data\n");
71 return (-1);
72 }
73 if ((flags & EXTRA_FIELD) != 0)
74 i = 12 + src[10] + (src[11] << 8);
75 if ((flags & ORIG_NAME) != 0)
76 while (src[i++] != 0)
77 ;
78 if ((flags & COMMENT) != 0)
79 while (src[i++] != 0)
80 ;
81 if ((flags & HEAD_CRC) != 0)
82 i += 2;
83 if (i >= *lenp) {
84 puts ("Error: gunzip out of data in header\n");
85 return (-1);
86 }
87
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +020088 return zunzip(dst, dstlen, src, lenp, 1, i);
89}
90
91/*
92 * Uncompress blocks compressed with zlib without headers
93 */
94int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
95 int stoponerr, int offset)
96{
97 z_stream s;
98 int r;
99
Marian Balakowicz321359f2008-01-08 18:11:43 +0100100 s.zalloc = zalloc;
101 s.zfree = zfree;
102#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
103 s.outcb = (cb_func)WATCHDOG_RESET;
104#else
105 s.outcb = Z_NULL;
106#endif /* CONFIG_HW_WATCHDOG */
107
108 r = inflateInit2(&s, -MAX_WBITS);
109 if (r != Z_OK) {
110 printf ("Error: inflateInit2() returned %d\n", r);
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200111 return -1;
Marian Balakowicz321359f2008-01-08 18:11:43 +0100112 }
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200113 s.next_in = src + offset;
114 s.avail_in = *lenp - offset;
Marian Balakowicz321359f2008-01-08 18:11:43 +0100115 s.next_out = dst;
116 s.avail_out = dstlen;
117 r = inflate(&s, Z_FINISH);
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200118 if ((r != Z_STREAM_END) && (stoponerr==1)) {
Marian Balakowicz321359f2008-01-08 18:11:43 +0100119 printf ("Error: inflate() returned %d\n", r);
Matthias Fuchs107b8012009-01-02 15:11:41 +0100120 inflateEnd(&s);
Marian Balakowicz321359f2008-01-08 18:11:43 +0100121 return (-1);
122 }
123 *lenp = s.next_out - (unsigned char *) dst;
124 inflateEnd(&s);
125
Ricardo Ribalda Delgado35f6a942009-04-27 18:33:32 +0200126 return 0;
Marian Balakowicz321359f2008-01-08 18:11:43 +0100127}