]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/linux/linux-mtx-1-2.4.24/04-zboot-2.4.24.patch
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / linux / linux-mtx-1-2.4.24 / 04-zboot-2.4.24.patch
1 diff -Naru linux/arch/mips/Makefile linux-new/arch/mips/Makefile
2 --- linux/arch/mips/Makefile    2003-10-22 02:58:37.000000000 -0400
3 +++ linux-new/arch/mips/Makefile        2003-12-18 14:26:20.000000000 -0500
4 @@ -724,15 +724,21 @@
5  endif
6  
7  MAKEBOOT = $(MAKE) -C arch/$(ARCH)/boot
8 +MAKEZBOOT = $(MAKE) -C arch/$(ARCH)/zboot
9 +BOOT_TARGETS = zImage zImage.initrd zImage.flash
10  
11  vmlinux.ecoff: vmlinux
12         @$(MAKEBOOT) $@
13  
14 + $(BOOT_TARGETS): vmlinux
15 +       @$(MAKEZBOOT) $@
16 +
17  vmlinux.srec: vmlinux
18         @$(MAKEBOOT) $@
19  
20  archclean:
21         @$(MAKEBOOT) clean
22 +       @$(MAKEZBOOT) clean
23         rm -f arch/$(ARCH)/ld.script
24         $(MAKE) -C arch/$(ARCH)/tools clean
25         $(MAKE) -C arch/mips/baget clean
26 diff -Naru linux/arch/mips/zboot/common/au1k_uart.c linux-new/arch/mips/zboot/common/au1k_uart.c
27 --- linux/arch/mips/zboot/common/au1k_uart.c    1969-12-31 19:00:00.000000000 -0500
28 +++ linux-new/arch/mips/zboot/common/au1k_uart.c        2003-12-18 14:26:20.000000000 -0500
29 @@ -0,0 +1,103 @@
30 +/*
31 + * BRIEF MODULE DESCRIPTION
32 + *     Simple Au1000 uart routines.
33 + *
34 + * Copyright 2001 MontaVista Software Inc.
35 + * Author: MontaVista Software, Inc.
36 + *             ppopov@mvista.com or source@mvista.com
37 + *
38 + *  This program is free software; you can redistribute         it and/or modify it
39 + *  under  the terms of         the GNU General  Public License as published by the
40 + *  Free Software Foundation;  either version 2 of the License, or (at your
41 + *  option) any later version.
42 + *
43 + *  THIS  SOFTWARE  IS PROVIDED          ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
44 + *  WARRANTIES,          INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
45 + *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
46 + *  NO EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
47 + *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 + *  NOT LIMITED          TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
49 + *  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
50 + *  ANY THEORY OF LIABILITY, WHETHER IN         CONTRACT, STRICT LIABILITY, OR TORT
51 + *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52 + *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 + *
54 + *  You should have received a copy of the  GNU General Public License along
55 + *  with this program; if not, write  to the Free Software Foundation, Inc.,
56 + *  675 Mass Ave, Cambridge, MA 02139, USA.
57 + */
58 +#include <linux/config.h>
59 +#include <asm/io.h>
60 +#include <asm/au1000.h>
61 +#include "ns16550.h"
62 +
63 +typedef         unsigned char uint8;
64 +typedef         unsigned int  uint32;
65 +
66 +#define         UART16550_BAUD_2400             2400
67 +#define         UART16550_BAUD_4800             4800
68 +#define         UART16550_BAUD_9600             9600
69 +#define         UART16550_BAUD_19200            19200
70 +#define         UART16550_BAUD_38400            38400
71 +#define         UART16550_BAUD_57600            57600
72 +#define         UART16550_BAUD_115200           115200
73 +
74 +#define         UART16550_PARITY_NONE           0
75 +#define         UART16550_PARITY_ODD            0x08
76 +#define         UART16550_PARITY_EVEN           0x18
77 +#define         UART16550_PARITY_MARK           0x28
78 +#define         UART16550_PARITY_SPACE          0x38
79 +
80 +#define         UART16550_DATA_5BIT             0x0
81 +#define         UART16550_DATA_6BIT             0x1
82 +#define         UART16550_DATA_7BIT             0x2
83 +#define         UART16550_DATA_8BIT             0x3
84 +
85 +#define         UART16550_STOP_1BIT             0x0
86 +#define         UART16550_STOP_2BIT             0x4
87 +
88 +/* It would be nice if we had a better way to do this.
89 + * It could be a variable defined in one of the board specific files.
90 + */
91 +#undef UART_BASE
92 +#ifdef CONFIG_COGENT_CSB250
93 +#define UART_BASE UART3_ADDR
94 +#else
95 +#define UART_BASE UART0_ADDR
96 +#endif
97 +
98 +/* memory-mapped read/write of the port */
99 +#define UART16550_READ(y)    (readl(UART_BASE + y) & 0xff)
100 +#define UART16550_WRITE(y,z) (writel(z&0xff, UART_BASE + y))
101 +
102 +/*
103 + * We use uart 0, which is already initialized by
104 + * yamon. 
105 + */
106 +volatile struct NS16550 *
107 +serial_init(int chan)
108 +{
109 +       volatile struct NS16550 *com_port;
110 +       com_port = (struct NS16550 *) UART_BASE;
111 +       return (com_port);
112 +}
113 +
114 +void
115 +serial_putc(volatile struct NS16550 *com_port, unsigned char c)
116 +{
117 +       while ((UART16550_READ(UART_LSR)&0x40) == 0);
118 +       UART16550_WRITE(UART_TX, c);
119 +}
120 +
121 +unsigned char
122 +serial_getc(volatile struct NS16550 *com_port)
123 +{
124 +       while((UART16550_READ(UART_LSR) & 0x1) == 0);
125 +       return UART16550_READ(UART_RX);
126 +}
127 +
128 +int
129 +serial_tstc(volatile struct NS16550 *com_port)
130 +{
131 +       return((UART16550_READ(UART_LSR) & LSR_DR) != 0);
132 +}
133 diff -Naru linux/arch/mips/zboot/common/ctype.c linux-new/arch/mips/zboot/common/ctype.c
134 --- linux/arch/mips/zboot/common/ctype.c        1969-12-31 19:00:00.000000000 -0500
135 +++ linux-new/arch/mips/zboot/common/ctype.c    2003-12-18 14:26:20.000000000 -0500
136 @@ -0,0 +1,35 @@
137 +/*
138 + *  linux/lib/ctype.c
139 + *
140 + *  Copyright (C) 1991, 1992  Linus Torvalds
141 + */
142 +
143 +#include <linux/ctype.h>
144 +
145 +unsigned char _ctype[] = {
146 +_C,_C,_C,_C,_C,_C,_C,_C,                       /* 0-7 */
147 +_C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C,                /* 8-15 */
148 +_C,_C,_C,_C,_C,_C,_C,_C,                       /* 16-23 */
149 +_C,_C,_C,_C,_C,_C,_C,_C,                       /* 24-31 */
150 +_S|_SP,_P,_P,_P,_P,_P,_P,_P,                   /* 32-39 */
151 +_P,_P,_P,_P,_P,_P,_P,_P,                       /* 40-47 */
152 +_D,_D,_D,_D,_D,_D,_D,_D,                       /* 48-55 */
153 +_D,_D,_P,_P,_P,_P,_P,_P,                       /* 56-63 */
154 +_P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U,     /* 64-71 */
155 +_U,_U,_U,_U,_U,_U,_U,_U,                       /* 72-79 */
156 +_U,_U,_U,_U,_U,_U,_U,_U,                       /* 80-87 */
157 +_U,_U,_U,_P,_P,_P,_P,_P,                       /* 88-95 */
158 +_P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L,     /* 96-103 */
159 +_L,_L,_L,_L,_L,_L,_L,_L,                       /* 104-111 */
160 +_L,_L,_L,_L,_L,_L,_L,_L,                       /* 112-119 */
161 +_L,_L,_L,_P,_P,_P,_P,_C,                       /* 120-127 */
162 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,               /* 128-143 */
163 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,               /* 144-159 */
164 +_S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,   /* 160-175 */
165 +_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,       /* 176-191 */
166 +_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,       /* 192-207 */
167 +_U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L,       /* 208-223 */
168 +_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,       /* 224-239 */
169 +_L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L};      /* 240-255 */
170 +
171 +
172 diff -Naru linux/arch/mips/zboot/common/dummy.c linux-new/arch/mips/zboot/common/dummy.c
173 --- linux/arch/mips/zboot/common/dummy.c        1969-12-31 19:00:00.000000000 -0500
174 +++ linux-new/arch/mips/zboot/common/dummy.c    2003-12-18 14:26:20.000000000 -0500
175 @@ -0,0 +1,4 @@
176 +int main(void)
177 +{
178 +       return 0;
179 +}
180 diff -Naru linux/arch/mips/zboot/common/Makefile linux-new/arch/mips/zboot/common/Makefile
181 --- linux/arch/mips/zboot/common/Makefile       1969-12-31 19:00:00.000000000 -0500
182 +++ linux-new/arch/mips/zboot/common/Makefile   2003-12-18 14:26:20.000000000 -0500
183 @@ -0,0 +1,27 @@
184 +#
185 +# arch/mips/zboot/common/Makefile
186 +#
187 +# This file is subject to the terms and conditions of the GNU General Public
188 +# License.  See the file "COPYING" in the main directory of this archive
189 +# for more details.
190 +#
191 +# Tom Rini     January 2001
192 +#
193 +
194 +.c.s:
195 +       $(CC) $(CFLAGS) -S -o $*.s $<
196 +.s.o:
197 +       $(AS) -o $*.o $<
198 +.c.o:
199 +       $(CC) $(CFLAGS) -c -o $*.o $<
200 +.S.s:
201 +       $(CPP) $(AFLAGS) -o $*.o $<
202 +.S.o:
203 +       $(CC) $(AFLAGS) -c -o $*.o $<
204 +
205 +clean:
206 +       rm -rf *.o
207 +
208 +OBJCOPY_ARGS = -O elf32-tradlittlemips
209 +
210 +include $(TOPDIR)/Rules.make
211 diff -Naru linux/arch/mips/zboot/common/misc-common.c linux-new/arch/mips/zboot/common/misc-common.c
212 --- linux/arch/mips/zboot/common/misc-common.c  1969-12-31 19:00:00.000000000 -0500
213 +++ linux-new/arch/mips/zboot/common/misc-common.c      2003-12-18 14:26:20.000000000 -0500
214 @@ -0,0 +1,437 @@
215 +/*
216 + * arch/mips/zboot/common/misc-common.c
217 + * 
218 + * Misc. bootloader code (almost) all platforms can use
219 + *
220 + * Author: Johnnie Peters <jpeters@mvista.com>
221 + * Editor: Tom Rini <trini@mvista.com>
222 + *
223 + * Derived from arch/ppc/boot/prep/misc.c
224 + *
225 + * Ported by Pete Popov <ppopov@mvista.com> to
226 + * support mips board(s).  I also got rid of the vga console
227 + * code.
228 + *
229 + * Copyright 2000-2001 MontaVista Software Inc.
230 + *
231 + * This program is free software; you can redistribute  it and/or modify it
232 + * under  the terms of  the GNU General  Public License as published by the
233 + * Free Software Foundation;  either version 2 of the  License, or (at your
234 + * option) any later version.
235 + *
236 + * THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR   IMPLIED
237 + * WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
238 + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
239 + * NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT,  INDIRECT,
240 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
241 + * NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
242 + * USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
243 + * ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
244 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
245 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
246 + *
247 + * You should have received a copy of the  GNU General Public License along
248 + * with this program; if not, write  to the Free Software Foundation, Inc.,
249 + * 675 Mass Ave, Cambridge, MA 02139, USA.
250 + */
251 +
252 +#include <linux/config.h>
253 +#include "zlib.h"
254 +#include <stdarg.h>
255 +
256 +extern char *avail_ram;
257 +extern char *end_avail;
258 +extern char _end[];
259 +
260 +void puts(const char *);
261 +void putc(const char c);
262 +void puthex(unsigned long val);
263 +void _bcopy(char *src, char *dst, int len);
264 +void gunzip(void *, int, unsigned char *, int *);
265 +static int _cvt(unsigned long val, char *buf, long radix, char *digits);
266 +
267 +void _vprintk(void(*)(const char), const char *, va_list ap);
268 +
269 +struct NS16550 *com_port;
270 +
271 +int serial_tstc(volatile struct NS16550 *);
272 +unsigned char serial_getc(volatile struct NS16550 *);
273 +void serial_putc(volatile struct NS16550 *, unsigned char);
274 +
275 +void pause(void)
276 +{
277 +       puts("pause\n");
278 +}
279 +
280 +void exit(void)
281 +{
282 +       puts("exit\n");
283 +       while(1); 
284 +}
285 +
286 +int tstc(void)
287 +{
288 +       return (serial_tstc(com_port));
289 +}
290 +
291 +int getc(void)
292 +{
293 +       while (1) {
294 +               if (serial_tstc(com_port))
295 +                       return (serial_getc(com_port));
296 +       }
297 +}
298 +
299 +void 
300 +putc(const char c)
301 +{
302 +       int x,y;
303 +
304 +       serial_putc(com_port, c);
305 +       if ( c == '\n' )
306 +               serial_putc(com_port, '\r');
307 +}
308 +
309 +void puts(const char *s)
310 +{
311 +       char c;
312 +       while ( ( c = *s++ ) != '\0' ) {
313 +               serial_putc(com_port, c);
314 +               if ( c == '\n' ) serial_putc(com_port, '\r');
315 +       }
316 +}
317 +
318 +void error(char *x)
319 +{
320 +       puts("\n\n");
321 +       puts(x);
322 +       puts("\n\n -- System halted");
323 +
324 +       while(1);       /* Halt */
325 +}
326 +
327 +void *zalloc(void *x, unsigned items, unsigned size)
328 +{
329 +       void *p = avail_ram;
330 +       
331 +       size *= items;
332 +       size = (size + 7) & -8;
333 +       avail_ram += size;
334 +       if (avail_ram > end_avail) {
335 +               puts("oops... out of memory\n");
336 +               pause();
337 +       }
338 +       return p;
339 +}
340 +
341 +void zfree(void *x, void *addr, unsigned nb)
342 +{
343 +}
344 +
345 +#define HEAD_CRC       2
346 +#define EXTRA_FIELD    4
347 +#define ORIG_NAME      8
348 +#define COMMENT                0x10
349 +#define RESERVED       0xe0
350 +
351 +#define DEFLATED       8
352 +
353 +void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
354 +{
355 +       z_stream s;
356 +       int r, i, flags;
357 +       
358 +       /* skip header */
359 +       i = 10;
360 +       flags = src[3];
361 +       if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
362 +               puts("bad gzipped data\n");
363 +               exit();
364 +       }
365 +       if ((flags & EXTRA_FIELD) != 0)
366 +               i = 12 + src[10] + (src[11] << 8);
367 +       if ((flags & ORIG_NAME) != 0)
368 +               while (src[i++] != 0)
369 +                       ;
370 +       if ((flags & COMMENT) != 0)
371 +               while (src[i++] != 0)
372 +                       ;
373 +       if ((flags & HEAD_CRC) != 0)
374 +               i += 2;
375 +       if (i >= *lenp) {
376 +               puts("gunzip: ran out of data in header\n");
377 +               exit();
378 +       }
379 +       
380 +       s.zalloc = zalloc;
381 +       s.zfree = zfree;
382 +       r = inflateInit2(&s, -MAX_WBITS);
383 +       if (r != Z_OK) {
384 +               puts("inflateInit2 returned %d\n");
385 +               exit();
386 +       }
387 +       s.next_in = src + i;
388 +       s.avail_in = *lenp - i;
389 +       s.next_out = dst;
390 +       s.avail_out = dstlen;
391 +       r = inflate(&s, Z_FINISH);
392 +       if (r != Z_OK && r != Z_STREAM_END) {
393 +               puts("inflate returned %d\n");
394 +               exit();
395 +       }
396 +       *lenp = s.next_out - (unsigned char *) dst;
397 +       inflateEnd(&s);
398 +}
399 +
400 +void
401 +puthex(unsigned long val)
402 +{
403 +
404 +       unsigned char buf[10];
405 +       int i;
406 +       for (i = 7;  i >= 0;  i--)
407 +       {
408 +               buf[i] = "0123456789ABCDEF"[val & 0x0F];
409 +               val >>= 4;
410 +       }
411 +       buf[8] = '\0';
412 +       puts(buf);
413 +}
414 +
415 +#define FALSE 0
416 +#define TRUE  1
417 +
418 +void
419 +_printk(char const *fmt, ...)
420 +{
421 +       va_list ap;
422 +
423 +       va_start(ap, fmt);
424 +       _vprintk(putc, fmt, ap);
425 +       va_end(ap);
426 +       return;
427 +}
428 +
429 +#define is_digit(c) ((c >= '0') && (c <= '9'))
430 +
431 +void
432 +_vprintk(void(*putc)(const char), const char *fmt0, va_list ap)
433 +{
434 +       char c, sign, *cp = 0;
435 +       int left_prec, right_prec, zero_fill, length = 0, pad, pad_on_right;
436 +       char buf[32];
437 +       long val;
438 +       while ((c = *fmt0++))
439 +       {
440 +               if (c == '%')
441 +               {
442 +                       c = *fmt0++;
443 +                       left_prec = right_prec = pad_on_right = 0;
444 +                       if (c == '-')
445 +                       {
446 +                               c = *fmt0++;
447 +                               pad_on_right++;
448 +                       }
449 +                       if (c == '0')
450 +                       {
451 +                               zero_fill = TRUE;
452 +                               c = *fmt0++;
453 +                       } else
454 +                       {
455 +                               zero_fill = FALSE;
456 +                       }
457 +                       while (is_digit(c))
458 +                       {
459 +                               left_prec = (left_prec * 10) + (c - '0');
460 +                               c = *fmt0++;
461 +                       }
462 +                       if (c == '.')
463 +                       {
464 +                               c = *fmt0++;
465 +                               zero_fill++;
466 +                               while (is_digit(c))
467 +                               {
468 +                                       right_prec = (right_prec * 10) + (c - '0');
469 +                                       c = *fmt0++;
470 +                               }
471 +                       } else
472 +                       {
473 +                               right_prec = left_prec;
474 +                       }
475 +                       sign = '\0';
476 +                       switch (c)
477 +                       {
478 +                       case 'd':
479 +                       case 'x':
480 +                       case 'X':
481 +                               val = va_arg(ap, long);
482 +                               switch (c)
483 +                               {
484 +                               case 'd':
485 +                                       if (val < 0)
486 +                                       {
487 +                                               sign = '-';
488 +                                               val = -val;
489 +                                       }
490 +                                       length = _cvt(val, buf, 10, "0123456789");
491 +                                       break;
492 +                               case 'x':
493 +                                       length = _cvt(val, buf, 16, "0123456789abcdef");
494 +                                       break;
495 +                               case 'X':
496 +                                       length = _cvt(val, buf, 16, "0123456789ABCDEF");
497 +                                       break;
498 +                               }
499 +                               cp = buf;
500 +                               break;
501 +                       case 's':
502 +                               cp = va_arg(ap, char *);
503 +                               length = strlen(cp);
504 +                               break;
505 +                       case 'c':
506 +                               c = va_arg(ap, long /*char*/);
507 +                               (*putc)(c);
508 +                               continue;
509 +                       default:
510 +                               (*putc)('?');
511 +                       }
512 +                       pad = left_prec - length;
513 +                       if (sign != '\0')
514 +                       {
515 +                               pad--;
516 +                       }
517 +                       if (zero_fill)
518 +                       {
519 +                               c = '0';
520 +                               if (sign != '\0')
521 +                               {
522 +                                       (*putc)(sign);
523 +                                       sign = '\0';
524 +                               }
525 +                       } else
526 +                       {
527 +                               c = ' ';
528 +                       }
529 +                       if (!pad_on_right)
530 +                       {
531 +                               while (pad-- > 0)
532 +                               {
533 +                                       (*putc)(c);
534 +                               }
535 +                       }
536 +                       if (sign != '\0')
537 +                       {
538 +                               (*putc)(sign);
539 +                       }
540 +                       while (length-- > 0)
541 +                       {
542 +                               (*putc)(c = *cp++);
543 +                               if (c == '\n')
544 +                               {
545 +                                       (*putc)('\r');
546 +                               }
547 +                       }
548 +                       if (pad_on_right)
549 +                       {
550 +                               while (pad-- > 0)
551 +                               {
552 +                                       (*putc)(c);
553 +                               }
554 +                       }
555 +               } else
556 +               {
557 +                       (*putc)(c);
558 +                       if (c == '\n')
559 +                       {
560 +                               (*putc)('\r');
561 +                       }
562 +               }
563 +       }
564 +}
565 +
566 +int
567 +_cvt(unsigned long val, char *buf, long radix, char *digits)
568 +{
569 +       char temp[80];
570 +       char *cp = temp;
571 +       int length = 0;
572 +       if (val == 0)
573 +       { /* Special case */
574 +               *cp++ = '0';
575 +       } else
576 +               while (val)
577 +               {
578 +                       *cp++ = digits[val % radix];
579 +                       val /= radix;
580 +               }
581 +       while (cp != temp)
582 +       {
583 +               *buf++ = *--cp;
584 +               length++;
585 +       }
586 +       *buf = '\0';
587 +       return (length);
588 +}
589 +
590 +void
591 +_dump_buf_with_offset(unsigned char *p, int s, unsigned char *base)
592 +{
593 +       int i, c;
594 +       if ((unsigned int)s > (unsigned int)p)
595 +       {
596 +               s = (unsigned int)s - (unsigned int)p;
597 +       }
598 +       while (s > 0)
599 +       {
600 +               if (base)
601 +               {
602 +                       _printk("%06X: ", (int)p - (int)base);
603 +               } else
604 +               {
605 +                       _printk("%06X: ", p);
606 +               }
607 +               for (i = 0;  i < 16;  i++)
608 +               {
609 +                       if (i < s)
610 +                       {
611 +                               _printk("%02X", p[i] & 0xFF);
612 +                       } else
613 +                       {
614 +                               _printk("  ");
615 +                       }
616 +                       if ((i % 2) == 1) _printk(" ");
617 +                       if ((i % 8) == 7) _printk(" ");
618 +               }
619 +               _printk(" |");
620 +               for (i = 0;  i < 16;  i++)
621 +               {
622 +                       if (i < s)
623 +                       {
624 +                               c = p[i] & 0xFF;
625 +                               if ((c < 0x20) || (c >= 0x7F)) c = '.';
626 +                       } else
627 +                       {
628 +                               c = ' ';
629 +                       }
630 +                       _printk("%c", c);
631 +               }
632 +               _printk("|\n");
633 +               s -= 16;
634 +               p += 16;
635 +       }
636 +}
637 +
638 +void
639 +_dump_buf(unsigned char *p, int s)
640 +{
641 +       _printk("\n");
642 +       _dump_buf_with_offset(p, s, 0);
643 +}
644 +
645 +/*
646 + * Local variables:
647 + *  c-indent-level: 8
648 + *  c-basic-offset: 8
649 + *  tab-width: 8
650 + * End:
651 + */
652 diff -Naru linux/arch/mips/zboot/common/misc-simple.c linux-new/arch/mips/zboot/common/misc-simple.c
653 --- linux/arch/mips/zboot/common/misc-simple.c  1969-12-31 19:00:00.000000000 -0500
654 +++ linux-new/arch/mips/zboot/common/misc-simple.c      2003-12-18 14:26:20.000000000 -0500
655 @@ -0,0 +1,127 @@
656 +/*
657 + * arch/mips/zboot/common/misc-simple.c
658 + *
659 + * Misc. bootloader code for many machines.  This assumes you have are using
660 + * a 6xx/7xx/74xx CPU in your machine.  This assumes the chunk of memory
661 + * below 8MB is free.  Finally, it assumes you have a NS16550-style uart for 
662 + * your serial console.  If a machine meets these requirements, it can quite
663 + * likely use this code during boot.
664 + * 
665 + * Author: Matt Porter <mporter@mvista.com>
666 + * Derived from arch/ppc/boot/prep/misc.c
667 + *
668 + * Copyright 2001 MontaVista Software Inc.
669 + *
670 + * This program is free software; you can redistribute  it and/or modify it
671 + * under  the terms of  the GNU General  Public License as published by the
672 + * Free Software Foundation;  either version 2 of the  License, or (at your
673 + * option) any later version.
674 + */
675 +
676 +#include <linux/types.h>
677 +#include <linux/elf.h>
678 +#include <linux/config.h>
679 +
680 +#include <asm/page.h>
681 +#include <asm/processor.h>
682 +#include <asm/mmu.h>
683 +
684 +#include "zlib.h"
685 +
686 +extern struct NS16550 *com_port;
687 +
688 +char *avail_ram;
689 +char *end_avail;
690 +extern char _end[];
691 +char *zimage_start;
692 +
693 +#ifdef CONFIG_CMDLINE
694 +#define CMDLINE CONFIG_CMDLINE
695 +#else
696 +#define CMDLINE ""
697 +#endif
698 +char cmd_preset[] = CMDLINE;
699 +char cmd_buf[256];
700 +char *cmd_line = cmd_buf;
701 +
702 +/* The linker tells us where the image is.
703 +*/
704 +extern unsigned char __image_begin, __image_end;
705 +extern unsigned char __ramdisk_begin, __ramdisk_end;
706 +unsigned long initrd_size;
707 +
708 +extern void puts(const char *);
709 +extern void putc(const char c);
710 +extern void puthex(unsigned long val);
711 +extern void *memcpy(void * __dest, __const void * __src,
712 +                           __kernel_size_t __n);
713 +extern void gunzip(void *, int, unsigned char *, int *);
714 +extern void udelay(long delay);
715 +extern int tstc(void);
716 +extern int getc(void);
717 +extern volatile struct NS16550 *serial_init(int chan);
718 +
719 +void
720 +decompress_kernel(unsigned long load_addr, int num_words, 
721 +               unsigned long cksum, unsigned long *sp)
722 +{
723 +       int timer = 0;
724 +       extern unsigned long start;
725 +       char *cp, ch;
726 +       int i;
727 +       int     zimage_size;
728 +
729 +       com_port = (struct NS16550 *)serial_init(0);
730 +
731 +       initrd_size = (unsigned long)(&__ramdisk_end) -
732 +               (unsigned long)(&__ramdisk_begin);
733 +
734 +       /*
735 +        * Reveal where we were loaded at and where we
736 +        * were relocated to.
737 +        */
738 +       puts("loaded at:     "); puthex(load_addr);
739 +       puts(" "); puthex((unsigned long)(load_addr + (4*num_words))); puts("\n");
740 +       if ( (unsigned long)load_addr != (unsigned long)&start )
741 +       {
742 +               puts("relocated to:  "); puthex((unsigned long)&start);
743 +               puts(" ");
744 +               puthex((unsigned long)((unsigned long)&start + (4*num_words)));
745 +               puts("\n");
746 +       }
747 +
748 +       /*
749 +        * We link ourself to an arbitrary low address.  When we run, we
750 +        * relocate outself to that address.  __image_being points to
751 +        * the part of the image where the zImage is. -- Tom
752 +        */
753 +       zimage_start = (char *)(unsigned long)(&__image_begin);
754 +       zimage_size = (unsigned long)(&__image_end) -
755 +                       (unsigned long)(&__image_begin);
756 +
757 +       /*
758 +        * The zImage and initrd will be between start and _end, so they've
759 +        * already been moved once.  We're good to go now. -- Tom
760 +        */
761 +       puts("zimage at:     "); puthex((unsigned long)zimage_start);
762 +       puts(" "); puthex((unsigned long)(zimage_size+zimage_start));
763 +       puts("\n");
764 +
765 +       if ( initrd_size ) {
766 +               puts("initrd at:     ");
767 +               puthex((unsigned long)(&__ramdisk_begin));
768 +               puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
769 +       }
770 +
771 +       /* assume the chunk below 8M is free */
772 +       avail_ram = (char *)AVAIL_RAM_START;
773 +       end_avail = (char *)AVAIL_RAM_END;
774 +
775 +       /* Display standard Linux/MIPS boot prompt for kernel args */
776 +       puts("Uncompressing Linux at load address ");
777 +       puthex(LOADADDR);
778 +       puts("\n");
779 +       /* I don't like this hard coded gunzip size (fixme) */
780 +       gunzip((void *)LOADADDR, 0x400000, zimage_start, &zimage_size);
781 +       puts("Now booting the kernel\n");
782 +}
783 diff -Naru linux/arch/mips/zboot/common/no_initrd.c linux-new/arch/mips/zboot/common/no_initrd.c
784 --- linux/arch/mips/zboot/common/no_initrd.c    1969-12-31 19:00:00.000000000 -0500
785 +++ linux-new/arch/mips/zboot/common/no_initrd.c        2003-12-18 14:26:20.000000000 -0500
786 @@ -0,0 +1,2 @@
787 +char initrd_data[1];
788 +int initrd_len = 0;
789 diff -Naru linux/arch/mips/zboot/common/ns16550.c linux-new/arch/mips/zboot/common/ns16550.c
790 --- linux/arch/mips/zboot/common/ns16550.c      1969-12-31 19:00:00.000000000 -0500
791 +++ linux-new/arch/mips/zboot/common/ns16550.c  2003-12-18 14:26:20.000000000 -0500
792 @@ -0,0 +1,57 @@
793 +/*
794 + * NS16550 support
795 + */
796 +
797 +#include <linux/config.h>
798 +#include <asm/serial.h>
799 +#include "ns16550.h"
800 +
801 +typedef struct NS16550 *NS16550_t;
802 +
803 +const NS16550_t COM_PORTS[] = { (NS16550_t) COM1,
804 +    (NS16550_t) COM2,
805 +    (NS16550_t) COM3,
806 +    (NS16550_t) COM4 };
807 +
808 +volatile struct NS16550 *
809 +serial_init(int chan)
810 +{
811 +       volatile struct NS16550 *com_port;
812 +       com_port = (struct NS16550 *) COM_PORTS[chan];
813 +       /* See if port is present */
814 +       com_port->lcr = 0x00;
815 +       com_port->ier = 0xFF;
816 +#if 0
817 +       if (com_port->ier != 0x0F) return ((struct NS16550 *)0);
818 +#endif
819 +       com_port->ier = 0x00;
820 +       com_port->lcr = 0x80;  /* Access baud rate */
821 +#ifdef CONFIG_SERIAL_CONSOLE_NONSTD
822 +       com_port->dll = (BASE_BAUD / CONFIG_SERIAL_CONSOLE_BAUD);
823 +       com_port->dlm = (BASE_BAUD / CONFIG_SERIAL_CONSOLE_BAUD) >> 8;
824 +#endif
825 +       com_port->lcr = 0x03;  /* 8 data, 1 stop, no parity */
826 +       com_port->mcr = 0x03;  /* RTS/DTR */
827 +       com_port->fcr = 0x07;  /* Clear & enable FIFOs */
828 +       return (com_port);
829 +}
830 +
831 +void
832 +serial_putc(volatile struct NS16550 *com_port, unsigned char c)
833 +{
834 +       while ((com_port->lsr & LSR_THRE) == 0) ;
835 +       com_port->thr = c;
836 +}
837 +
838 +unsigned char
839 +serial_getc(volatile struct NS16550 *com_port)
840 +{
841 +       while ((com_port->lsr & LSR_DR) == 0) ;
842 +       return (com_port->rbr);
843 +}
844 +
845 +int
846 +serial_tstc(volatile struct NS16550 *com_port)
847 +{
848 +       return ((com_port->lsr & LSR_DR) != 0);
849 +}
850 --- linux/arch/mips/zboot/common/string.c       1969-12-31 19:00:00.000000000 -0500
851 +++ linux-new/arch/mips/zboot/common/string.c   2003-12-18 17:11:10.000000000 -0500
852 @@ -0,0 +1,497 @@
853 +/*
854 + *  linux/lib/string.c
855 + *
856 + *  Copyright (C) 1991, 1992  Linus Torvalds
857 + */
858 +
859 +/*
860 + * stupid library routines.. The optimized versions should generally be found
861 + * as inline code in <asm-xx/string.h>
862 + *
863 + * These are buggy as well..
864 + *
865 + * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
866 + * -  Added strsep() which will replace strtok() soon (because strsep() is
867 + *    reentrant and should be faster). Use only strsep() in new code, please.
868 + */
869
870 +#include <linux/types.h>
871 +#include <linux/string.h>
872 +#include <linux/ctype.h>
873 +
874 +/**
875 + * strnicmp - Case insensitive, length-limited string comparison
876 + * @s1: One string
877 + * @s2: The other string
878 + * @len: the maximum number of characters to compare
879 + */
880 +int strnicmp(const char *s1, const char *s2, size_t len)
881 +{
882 +       /* Yes, Virginia, it had better be unsigned */
883 +       unsigned char c1, c2;
884 +
885 +       c1 = 0; c2 = 0;
886 +       if (len) {
887 +               do {
888 +                       c1 = *s1; c2 = *s2;
889 +                       s1++; s2++;
890 +                       if (!c1)
891 +                               break;
892 +                       if (!c2)
893 +                               break;
894 +                       if (c1 == c2)
895 +                               continue;
896 +                       c1 = tolower(c1);
897 +                       c2 = tolower(c2);
898 +                       if (c1 != c2)
899 +                               break;
900 +               } while (--len);
901 +       }
902 +       return (int)c1 - (int)c2;
903 +}
904 +
905 +char * ___strtok;
906 +
907 +#ifndef __HAVE_ARCH_STRCPY
908 +/**
909 + * strcpy - Copy a %NUL terminated string
910 + * @dest: Where to copy the string to
911 + * @src: Where to copy the string from
912 + */
913 +char * strcpy(char * dest,const char *src)
914 +{
915 +       char *tmp = dest;
916 +
917 +       while ((*dest++ = *src++) != '\0')
918 +               /* nothing */;
919 +       return tmp;
920 +}
921 +#endif
922 +
923 +#ifndef __HAVE_ARCH_STRNCPY
924 +/**
925 + * strncpy - Copy a length-limited, %NUL-terminated string
926 + * @dest: Where to copy the string to
927 + * @src: Where to copy the string from
928 + * @count: The maximum number of bytes to copy
929 + *
930 + * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
931 + * However, the result is not %NUL-terminated if the source exceeds
932 + * @count bytes.
933 + */
934 +char * strncpy(char * dest,const char *src,size_t count)
935 +{
936 +       char *tmp = dest;
937 +
938 +       while (count-- && (*dest++ = *src++) != '\0')
939 +               /* nothing */;
940 +
941 +       return tmp;
942 +}
943 +#endif
944 +
945 +/**
946 + * strcat - Append one %NUL-terminated string to another
947 + * @dest: The string to be appended to
948 + * @src: The string to append to it
949 + */
950 +char * strcat(char * dest, const char * src)
951 +{
952 +       char *tmp = dest;
953 +
954 +       while (*dest)
955 +               dest++;
956 +       while ((*dest++ = *src++) != '\0')
957 +               ;
958 +
959 +       return tmp;
960 +}
961 +
962 +/**
963 + * strncat - Append a length-limited, %NUL-terminated string to another
964 + * @dest: The string to be appended to
965 + * @src: The string to append to it
966 + * @count: The maximum numbers of bytes to copy
967 + *
968 + * Note that in contrast to strncpy, strncat ensures the result is
969 + * terminated.
970 + */
971 +char * strncat(char *dest, const char *src, size_t count)
972 +{
973 +       char *tmp = dest;
974 +
975 +       if (count) {
976 +               while (*dest)
977 +                       dest++;
978 +               while ((*dest++ = *src++)) {
979 +                       if (--count == 0) {
980 +                               *dest = '\0';
981 +                               break;
982 +                       }
983 +               }
984 +       }
985 +
986 +       return tmp;
987 +}
988 +
989 +#ifndef __HAVE_ARCH_STRCMP
990 +/**
991 + * strcmp - Compare two strings
992 + * @cs: One string
993 + * @ct: Another string
994 + */
995 +int strcmp(const char * cs,const char * ct)
996 +{
997 +       register signed char __res;
998 +
999 +       while (1) {
1000 +               if ((__res = *cs - *ct++) != 0 || !*cs++)
1001 +                       break;
1002 +       }
1003 +
1004 +       return __res;
1005 +}
1006 +#endif
1007 +
1008 +#ifndef __HAVE_ARCH_STRNCMP
1009 +/**
1010 + * strncmp - Compare two length-limited strings
1011 + * @cs: One string
1012 + * @ct: Another string
1013 + * @count: The maximum number of bytes to compare
1014 + */
1015 +int strncmp(const char * cs,const char * ct,size_t count)
1016 +{
1017 +       register signed char __res = 0;
1018 +
1019 +       while (count) {
1020 +               if ((__res = *cs - *ct++) != 0 || !*cs++)
1021 +                       break;
1022 +               count--;
1023 +       }
1024 +
1025 +       return __res;
1026 +}
1027 +#endif
1028 +
1029 +/**
1030 + * strchr - Find the first occurrence of a character in a string
1031 + * @s: The string to be searched
1032 + * @c: The character to search for
1033 + */
1034 +char * strchr(const char * s, int c)
1035 +{
1036 +       for(; *s != (char) c; ++s)
1037 +               if (*s == '\0')
1038 +                       return NULL;
1039 +       return (char *) s;
1040 +}
1041 +
1042 +/**
1043 + * strrchr - Find the last occurrence of a character in a string
1044 + * @s: The string to be searched
1045 + * @c: The character to search for
1046 + */
1047 +char * strrchr(const char * s, int c)
1048 +{
1049 +       const char *p = s + strlen(s);
1050 +       do {
1051 +           if (*p == (char)c)
1052 +               return (char *)p;
1053 +       } while (--p >= s);
1054 +       return NULL;
1055 +}
1056 +
1057 +/**
1058 + * strlen - Find the length of a string
1059 + * @s: The string to be sized
1060 + */
1061 +size_t strlen(const char * s)
1062 +{
1063 +       const char *sc;
1064 +
1065 +       for (sc = s; *sc != '\0'; ++sc)
1066 +               /* nothing */;
1067 +       return sc - s;
1068 +}
1069 +
1070 +/**
1071 + * strnlen - Find the length of a length-limited string
1072 + * @s: The string to be sized
1073 + * @count: The maximum number of bytes to search
1074 + */
1075 +size_t strnlen(const char * s, size_t count)
1076 +{
1077 +       const char *sc;
1078 +
1079 +       for (sc = s; count-- && *sc != '\0'; ++sc)
1080 +               /* nothing */;
1081 +       return sc - s;
1082 +}
1083 +
1084 +/**
1085 + * strspn - Calculate the length of the initial substring of @s which only
1086 + *     contain letters in @accept
1087 + * @s: The string to be searched
1088 + * @accept: The string to search for
1089 + */
1090 +size_t strspn(const char *s, const char *accept)
1091 +{
1092 +       const char *p;
1093 +       const char *a;
1094 +       size_t count = 0;
1095 +
1096 +       for (p = s; *p != '\0'; ++p) {
1097 +               for (a = accept; *a != '\0'; ++a) {
1098 +                       if (*p == *a)
1099 +                               break;
1100 +               }
1101 +               if (*a == '\0')
1102 +                       return count;
1103 +               ++count;
1104 +       }
1105 +
1106 +       return count;
1107 +}
1108 +
1109 +/**
1110 + * strpbrk - Find the first occurrence of a set of characters
1111 + * @cs: The string to be searched
1112 + * @ct: The characters to search for
1113 + */
1114 +char * strpbrk(const char * cs,const char * ct)
1115 +{
1116 +       const char *sc1,*sc2;
1117 +
1118 +       for( sc1 = cs; *sc1 != '\0'; ++sc1) {
1119 +               for( sc2 = ct; *sc2 != '\0'; ++sc2) {
1120 +                       if (*sc1 == *sc2)
1121 +                               return (char *) sc1;
1122 +               }
1123 +       }
1124 +       return NULL;
1125 +}
1126 +
1127 +/**
1128 + * strtok - Split a string into tokens
1129 + * @s: The string to be searched
1130 + * @ct: The characters to search for
1131 + *
1132 + * WARNING: strtok is deprecated, use strsep instead.
1133 + */
1134 +char * strtok(char * s,const char * ct)
1135 +{
1136 +       char *sbegin, *send;
1137 +
1138 +       sbegin  = s ? s : ___strtok;
1139 +       if (!sbegin) {
1140 +               return NULL;
1141 +       }
1142 +       sbegin += strspn(sbegin,ct);
1143 +       if (*sbegin == '\0') {
1144 +               ___strtok = NULL;
1145 +               return( NULL );
1146 +       }
1147 +       send = strpbrk( sbegin, ct);
1148 +       if (send && *send != '\0')
1149 +               *send++ = '\0';
1150 +       ___strtok = send;
1151 +       return (sbegin);
1152 +}
1153 +
1154 +/**
1155 + * strsep - Split a string into tokens
1156 + * @s: The string to be searched
1157 + * @ct: The characters to search for
1158 + *
1159 + * strsep() updates @s to point after the token, ready for the next call.
1160 + *
1161 + * It returns empty tokens, too, behaving exactly like the libc function
1162 + * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
1163 + * Same semantics, slimmer shape. ;)
1164 + */
1165 +char * strsep(char **s, const char *ct)
1166 +{
1167 +       char *sbegin = *s, *end;
1168 +
1169 +       if (sbegin == NULL)
1170 +               return NULL;
1171 +
1172 +       end = strpbrk(sbegin, ct);
1173 +       if (end)
1174 +               *end++ = '\0';
1175 +       *s = end;
1176 +
1177 +       return sbegin;
1178 +}
1179 +
1180 +/**
1181 + * memset - Fill a region of memory with the given value
1182 + * @s: Pointer to the start of the area.
1183 + * @c: The byte to fill the area with
1184 + * @count: The size of the area.
1185 + *
1186 + * Do not use memset() to access IO space, use memset_io() instead.
1187 + */
1188 +void * memset(void * s,int c, size_t count)
1189 +{
1190 +       char *xs = (char *) s;
1191 +
1192 +       while (count--)
1193 +               *xs++ = c;
1194 +
1195 +       return s;
1196 +}
1197 +
1198 +/**
1199 + * bcopy - Copy one area of memory to another
1200 + * @src: Where to copy from
1201 + * @dest: Where to copy to
1202 + * @count: The size of the area.
1203 + *
1204 + * Note that this is the same as memcpy(), with the arguments reversed.
1205 + * memcpy() is the standard, bcopy() is a legacy BSD function.
1206 + *
1207 + * You should not use this function to access IO space, use memcpy_toio()
1208 + * or memcpy_fromio() instead.
1209 + */
1210 +char * bcopy(const char * src, char * dest, int count)
1211 +{
1212 +       char *tmp = dest;
1213 +
1214 +       while (count--)
1215 +               *tmp++ = *src++;
1216 +
1217 +       return dest;
1218 +}
1219 +
1220 +/**
1221 + * memcpy - Copy one area of memory to another
1222 + * @dest: Where to copy to
1223 + * @src: Where to copy from
1224 + * @count: The size of the area.
1225 + *
1226 + * You should not use this function to access IO space, use memcpy_toio()
1227 + * or memcpy_fromio() instead.
1228 + */
1229 +void * memcpy(void * dest,const void *src,size_t count)
1230 +{
1231 +       char *tmp = (char *) dest, *s = (char *) src;
1232 +
1233 +       while (count--)
1234 +               *tmp++ = *s++;
1235 +
1236 +       return dest;
1237 +}
1238 +
1239 +/**
1240 + * memmove - Copy one area of memory to another
1241 + * @dest: Where to copy to
1242 + * @src: Where to copy from
1243 + * @count: The size of the area.
1244 + *
1245 + * Unlike memcpy(), memmove() copes with overlapping areas.
1246 + */
1247 +void * memmove(void * dest,const void *src,size_t count)
1248 +{
1249 +       char *tmp, *s;
1250 +
1251 +       if (dest <= src) {
1252 +               tmp = (char *) dest;
1253 +               s = (char *) src;
1254 +               while (count--)
1255 +                       *tmp++ = *s++;
1256 +               }
1257 +       else {
1258 +               tmp = (char *) dest + count;
1259 +               s = (char *) src + count;
1260 +               while (count--)
1261 +                       *--tmp = *--s;
1262 +               }
1263 +
1264 +       return dest;
1265 +}
1266 +
1267 +/**
1268 + * memcmp - Compare two areas of memory
1269 + * @cs: One area of memory
1270 + * @ct: Another area of memory
1271 + * @count: The size of the area.
1272 + */
1273 +int memcmp(const void * cs,const void * ct,size_t count)
1274 +{
1275 +       const unsigned char *su1, *su2;
1276 +       signed char res = 0;
1277 +
1278 +       for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
1279 +               if ((res = *su1 - *su2) != 0)
1280 +                       break;
1281 +       return res;
1282 +}
1283 +
1284 +#ifndef __HAVE_ARCH_MEMSCAN
1285 +/**
1286 + * memscan - Find a character in an area of memory.
1287 + * @addr: The memory area
1288 + * @c: The byte to search for
1289 + * @size: The size of the area.
1290 + *
1291 + * returns the address of the first occurrence of @c, or 1 byte past
1292 + * the area if @c is not found
1293 + */
1294 +void * memscan(void * addr, int c, size_t size)
1295 +{
1296 +       unsigned char * p = (unsigned char *) addr;
1297 +       unsigned char * e = p + size;
1298 +
1299 +       while (p != e) {
1300 +               if (*p == c)
1301 +                       return (void *) p;
1302 +               p++;
1303 +       }
1304 +
1305 +       return (void *) p;
1306 +}
1307 +#endif
1308 +
1309 +/**
1310 + * strstr - Find the first substring in a %NUL terminated string
1311 + * @s1: The string to be searched
1312 + * @s2: The string to search for
1313 + */
1314 +char * strstr(const char * s1,const char * s2)
1315 +{
1316 +       int l1, l2;
1317 +
1318 +       l2 = strlen(s2);
1319 +       if (!l2)
1320 +               return (char *) s1;
1321 +       l1 = strlen(s1);
1322 +       while (l1 >= l2) {
1323 +               l1--;
1324 +               if (!memcmp(s1,s2,l2))
1325 +                       return (char *) s1;
1326 +               s1++;
1327 +       }
1328 +       return NULL;
1329 +}
1330 +
1331 +/**
1332 + * memchr - Find a character in an area of memory.
1333 + * @s: The memory area
1334 + * @c: The byte to search for
1335 + * @n: The size of the area.
1336 + *
1337 + * returns the address of the first occurrence of @c, or %NULL
1338 + * if @c is not found
1339 + */
1340 +void *memchr(const void *s, int c, size_t n)
1341 +{
1342 +       const unsigned char *p = s;
1343 +       while (n-- != 0) {
1344 +               if ((unsigned char)c == *p++) {
1345 +                       return (void *)(p-1);
1346 +               }
1347 +       }
1348 +       return NULL;
1349 +}
1350 diff -Naru linux/arch/mips/zboot/csb250/head.S linux-new/arch/mips/zboot/csb250/head.S
1351 --- linux/arch/mips/zboot/csb250/head.S 1969-12-31 19:00:00.000000000 -0500
1352 +++ linux-new/arch/mips/zboot/csb250/head.S     2003-12-18 14:26:20.000000000 -0500
1353 @@ -0,0 +1,157 @@
1354 +/*
1355 + * arch/mips/kernel/head.S
1356 + *
1357 + * This file is subject to the terms and conditions of the GNU General Public
1358 + * License.  See the file "COPYING" in the main directory of this archive
1359 + * for more details.
1360 + *
1361 + * Copyright (C) 1994, 1995 Waldorf Electronics
1362 + * Written by Ralf Baechle and Andreas Busse
1363 + * Copyright (C) 1995 - 1999 Ralf Baechle
1364 + * Copyright (C) 1996 Paul M. Antoine
1365 + * Modified for DECStation and hence R3000 support by Paul M. Antoine
1366 + * Further modifications by David S. Miller and Harald Koerfgen
1367 + * Copyright (C) 1999 Silicon Graphics, Inc.
1368 + *
1369 + * Head.S contains the MIPS exception handler and startup code.
1370 + *
1371 + **************************************************************************
1372 + *  9 Nov, 2000.
1373 + *  Added Cache Error exception handler and SBDDP EJTAG debug exception.
1374 + *
1375 + *  Kevin Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
1376 + *  Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
1377 + **************************************************************************
1378 + */
1379 +#include <linux/config.h>
1380 +#include <linux/threads.h>
1381 +
1382 +#include <asm/asm.h>
1383 +#include <asm/cacheops.h>
1384 +#include <asm/mipsregs.h>
1385 +#include <asm/offset.h>
1386 +#include <asm/cachectl.h>
1387 +#include <asm/regdef.h>
1388 +
1389 +#define IndexInvalidate_I       0x00
1390 +#define IndexWriteBack_D        0x01
1391 +
1392 +       .set noreorder
1393 +       .cprestore
1394 +       LEAF(start)
1395 +start:
1396 +       bal     locate
1397 +       nop
1398 +
1399 +       .globl  asize           /* Someday we'll put the initrd info here. */
1400 +asize: .word   0
1401 +       .word   0
1402 +       .word   0
1403 +       .word   0
1404 +
1405 +locate:
1406 +       subu    s8, ra, 8       /* Where we were loaded */
1407 +       la      sp, (.stack + 8192)
1408 +
1409 +       move    s0, a0          /* Save boot rom start args */
1410 +       move    s1, a1
1411 +       move    s2, a2
1412 +       move    s3, a3
1413 +
1414 +       la      a0, start       /* Where we were linked to run */
1415 +
1416 +       move    a1, s8
1417 +       la      a2, _edata
1418 +       subu    t1, a2, a0
1419 +       srl     t1, t1, 2
1420 +
1421 +       /* copy text section */
1422 +       li      t0, 0
1423 +1:     lw      v0, 0(a1)
1424 +       nop
1425 +       sw      v0, 0(a0)
1426 +       xor     t0, t0, v0
1427 +       addu    a0, 4
1428 +       bne     a2, a0, 1b
1429 +       addu    a1, 4
1430 +
1431 +       /* Clear BSS */
1432 +       la      a0, _edata
1433 +       la      a2, _end
1434 +2:     sw      zero, 0(a0)
1435 +       bne     a2, a0, 2b
1436 +       addu    a0, 4
1437 +
1438 +       /* push the D-Cache and invalidate I-Cache */
1439 +       li      k0, 0x80000000  # start address
1440 +       li      k1, 0x80004000  # end address (16KB I-Cache)
1441 +       subu    k1, 128
1442 +
1443 +1:
1444 +       .set mips3
1445 +       cache   IndexWriteBack_D, 0(k0)
1446 +       cache   IndexWriteBack_D, 32(k0)
1447 +       cache   IndexWriteBack_D, 64(k0)
1448 +       cache   IndexWriteBack_D, 96(k0)
1449 +       cache   IndexInvalidate_I, 0(k0)
1450 +       cache   IndexInvalidate_I, 32(k0)
1451 +       cache   IndexInvalidate_I, 64(k0)
1452 +       cache   IndexInvalidate_I, 96(k0)
1453 +       .set mips0
1454 +
1455 +       bne     k0, k1, 1b
1456 +       addu    k0, k0, 128
1457 +       /* done */
1458 +
1459 +/*     move    a0, s8               /* load address */
1460 +       subu    a0, s8, 0x1000               /* load address */
1461 +       move    a1, t1               /* length in words */
1462 +       move    a2, t0               /* checksum */
1463 +       move    a3, sp
1464 +
1465 +       la      ra, 1f
1466 +       la      k0, decompress_kernel
1467 +       jr      k0
1468 +       nop
1469 +1:
1470 +
1471 +       la      a2, __ramdisk_begin
1472 +       la      a3, initrd_size
1473 +       lw      a0, 0(a2)
1474 +       lw      a1, 0(a3)
1475 +       li      k0, KERNEL_ENTRY
1476 +       jr      k0
1477 +       nop
1478 +3:
1479 +       b 3b
1480 +       END(start)
1481 +
1482 +       LEAF(udelay)
1483 +udelay:
1484 +       END(udelay)
1485 +
1486 +
1487 +       LEAF(FlushCache)
1488 +       li      k0, 0x80000000  # start address
1489 +       li      k1, 0x80004000  # end address (16KB I-Cache)
1490 +       subu    k1, 128
1491 +
1492 +1:
1493 +       .set mips3
1494 +       cache   IndexWriteBack_D, 0(k0)
1495 +       cache   IndexWriteBack_D, 32(k0)
1496 +       cache   IndexWriteBack_D, 64(k0)
1497 +       cache   IndexWriteBack_D, 96(k0)
1498 +       cache   IndexInvalidate_I, 0(k0)
1499 +       cache   IndexInvalidate_I, 32(k0)
1500 +       cache   IndexInvalidate_I, 64(k0)
1501 +       cache   IndexInvalidate_I, 96(k0)
1502 +       .set mips0
1503 +
1504 +       bne     k0, k1, 1b
1505 +       addu    k0, k0, 128
1506 +       jr      ra
1507 +       nop
1508 +       END(FlushCache)
1509 +
1510 +       .comm .stack,4096*2,4
1511 diff -Naru linux/arch/mips/zboot/csb250/Makefile linux-new/arch/mips/zboot/csb250/Makefile
1512 --- linux/arch/mips/zboot/csb250/Makefile       1969-12-31 19:00:00.000000000 -0500
1513 +++ linux-new/arch/mips/zboot/csb250/Makefile   2003-12-18 14:26:20.000000000 -0500
1514 @@ -0,0 +1,90 @@
1515 +# arch/mips/zboot/pb1xxx/Makefile
1516 +# 
1517 +# Makefile for Cogent CSB250 Au1500 board.
1518 +# All of the boot loader code was derived from the ppc
1519 +# boot code.
1520 +#
1521 +# This program is free software; you can redistribute  it and/or modify it
1522 +# under  the terms of  the GNU General  Public License as published by the
1523 +# Free Software Foundation;  either version 2 of the  License, or (at your
1524 +# option) any later version.
1525 +
1526 +.c.s:
1527 +       $(CC) $(CFLAGS) -S -o $*.s $<
1528 +.s.o:
1529 +       $(AS) -o $*.o $<
1530 +.c.o:
1531 +       $(CC) $(CFLAGS) -D__BOOTER__ -c -o $*.o $<
1532 +.S.s:
1533 +       $(CPP) $(AFLAGS) -o $*.o $<
1534 +.S.o:
1535 +       $(CC) $(AFLAGS) -c -o $*.o $<
1536 +
1537 +#########################################################################
1538 +# START BOARD SPECIFIC VARIABLES
1539 +BNAME=csb250
1540 +
1541 +# These two variables control where the zImage is stored
1542 +# in flash and loaded in memory.  It only controls how the srec
1543 +# file is generated, the code is the same.
1544 +RAM_RUN_ADDR = 0x80a00000
1545 +FLASH_LOAD_ADDR = 0xBFD00000
1546 +
1547 +# These two variables specify the free ram region
1548 +# that can be used for temporary malloc area
1549 +AVAIL_RAM_START=0x80400000
1550 +AVAIL_RAM_END=0x80800000
1551 +
1552 +# This one must match the LOADADDR in arch/mips/Makefile!
1553 +LOADADDR=0x80100000
1554 +# END BOARD SPECIFIC VARIABLES
1555 +#########################################################################
1556 +
1557 +OBJECTS := head.o ../common/misc-common.o ../common/misc-simple.o \
1558 +       ../common/au1k_uart.o ../common/string.o ../common/ctype.o
1559 +LIBS := ../lib/zlib.a
1560 +
1561 +ENTRY := ../utils/entry
1562 +OFFSET := ../utils/offset
1563 +SIZE := ../utils/size
1564 +
1565 +LD_ARGS := -T ../ld.script -Ttext $(RAM_RUN_ADDR) -Bstatic
1566 +OBJCOPY_ARGS = -O elf32-tradbigmips
1567 +
1568 +all: zImage
1569 +
1570 +clean:
1571 +       rm -rf *.o vmlinux* zvmlinux.* ../images/*.srec
1572 +
1573 +head.o: head.S $(TOPDIR)/vmlinux
1574 +       $(CC) $(AFLAGS) \
1575 +       -DKERNEL_ENTRY=$(shell sh $(ENTRY) $(NM) $(TOPDIR)/vmlinux ) \
1576 +       -c -o $*.o $<
1577 +
1578 +../common/misc-simple.o:
1579 +       $(CC) $(CFLAGS) -DINITRD_OFFSET=0 -DINITRD_SIZE=0 -DZIMAGE_OFFSET=0 \
1580 +               -DAVAIL_RAM_START=$(AVAIL_RAM_START) \
1581 +               -DAVAIL_RAM_END=$(AVAIL_RAM_END) \
1582 +               -DLOADADDR=$(LOADADDR) \
1583 +               -DZIMAGE_SIZE=0 -c -o $@ $*.c
1584 +
1585 +zvmlinux: $(OBJECTS) $(LIBS) ../ld.script ../images/vmlinux.gz ../common/dummy.o
1586 +       $(OBJCOPY) \
1587 +               --add-section=.image=../images/vmlinux.gz \
1588 +               --set-section-flags=.image=contents,alloc,load,readonly,data \
1589 +               ../common/dummy.o image.o
1590 +       $(LD) $(LD_ARGS) -o $@ $(OBJECTS) image.o $(LIBS)
1591 +       $(OBJCOPY) $(OBJCOPY_ARGS) $@ $@ -R .comment -R .stab -R .stabstr \
1592 +               -R .initrd -R .sysmap
1593 +
1594 +# Here we manipulate the image in order to get it the necessary
1595 +# srecord file we need.
1596 +zImage: zvmlinux
1597 +       mv zvmlinux ../images/zImage.$(BNAME)
1598 +       $(OBJCOPY) -O binary ../images/zImage.$(BNAME) ../images/$(BNAME).bin
1599 +
1600 +zImage.flash: zImage
1601 +       $(OBJCOPY) -O srec --adjust-vma 0x3ed00000 \
1602 +               ../images/zImage.$(BNAME) ../images/$(BNAME).flash.srec
1603 +
1604 +include $(TOPDIR)/Rules.make
1605 diff -Naru linux/arch/mips/zboot/images/Makefile linux-new/arch/mips/zboot/images/Makefile
1606 --- linux/arch/mips/zboot/images/Makefile       1969-12-31 19:00:00.000000000 -0500
1607 +++ linux-new/arch/mips/zboot/images/Makefile   2003-12-18 14:26:20.000000000 -0500
1608 @@ -0,0 +1,10 @@
1609 +
1610 +include $(TOPDIR)/Rules.make
1611 +
1612 +vmlinux.gz: $(TOPDIR)/vmlinux
1613 +       $(OBJCOPY) -S -O binary $(TOPDIR)/vmlinux vmlinux
1614 +       gzip -vf vmlinux
1615 +
1616 +clean:
1617 +       rm -f vmlinux.*  zImage.*
1618 +
1619 diff -Naru linux/arch/mips/zboot/include/nonstdio.h linux-new/arch/mips/zboot/include/nonstdio.h
1620 --- linux/arch/mips/zboot/include/nonstdio.h    1969-12-31 19:00:00.000000000 -0500
1621 +++ linux-new/arch/mips/zboot/include/nonstdio.h        2003-12-18 14:26:20.000000000 -0500
1622 @@ -0,0 +1,18 @@
1623 +/*
1624 + * Copyright (C) Paul Mackerras 1997.
1625 + *
1626 + * This program is free software; you can redistribute it and/or
1627 + * modify it under the terms of the GNU General Public License
1628 + * as published by the Free Software Foundation; either version
1629 + * 2 of the License, or (at your option) any later version.
1630 + */
1631 +typedef int    FILE;
1632 +extern FILE *stdin, *stdout;
1633 +#define NULL   ((void *)0)
1634 +#define EOF    (-1)
1635 +#define fopen(n, m)    NULL
1636 +#define fflush(f)      0
1637 +#define fclose(f)      0
1638 +extern char *fgets();
1639 +
1640 +#define perror(s)      printf("%s: no files!\n", (s))
1641 diff -Naru linux/arch/mips/zboot/include/ns16550.h linux-new/arch/mips/zboot/include/ns16550.h
1642 --- linux/arch/mips/zboot/include/ns16550.h     1969-12-31 19:00:00.000000000 -0500
1643 +++ linux-new/arch/mips/zboot/include/ns16550.h 2003-12-18 14:26:20.000000000 -0500
1644 @@ -0,0 +1,49 @@
1645 +/*
1646 + * NS16550 Serial Port
1647 + */
1648 +
1649 +/*
1650 + * Figure out which file will have the definitons of COMx
1651 + */
1652 +#if !defined(CONFIG_AU1X00_UART)
1653 +#error no serial.h
1654 +#endif
1655 +
1656 +/* Some machines have their uart registers 16 bytes apart.  Most don't.
1657 + * TODO: Make this work like drivers/char/serial does - Tom */
1658 +#if !defined(UART_REG_PAD)
1659 +#define UART_REG_PAD(x)
1660 +#endif
1661 +
1662 +struct NS16550
1663 + {
1664 +  unsigned char rbr;  /* 0 */
1665 +  UART_REG_PAD(rbr)
1666 +  unsigned char ier;  /* 1 */
1667 +  UART_REG_PAD(ier)
1668 +  unsigned char fcr;  /* 2 */
1669 +  UART_REG_PAD(fcr)
1670 +  unsigned char lcr;  /* 3 */
1671 +  UART_REG_PAD(lcr)
1672 +  unsigned char mcr;  /* 4 */
1673 +  UART_REG_PAD(mcr)
1674 +  unsigned char lsr;  /* 5 */
1675 +  UART_REG_PAD(lsr)
1676 +  unsigned char msr;  /* 6 */
1677 +  UART_REG_PAD(msr)
1678 +  unsigned char scr;  /* 7 */
1679 + };
1680 +
1681 +#define thr rbr
1682 +#define iir fcr
1683 +#define dll rbr
1684 +#define dlm ier
1685 +
1686 +#define LSR_DR   0x01  /* Data ready */
1687 +#define LSR_OE   0x02  /* Overrun */
1688 +#define LSR_PE   0x04  /* Parity error */
1689 +#define LSR_FE   0x08  /* Framing error */
1690 +#define LSR_BI   0x10  /* Break */
1691 +#define LSR_THRE 0x20  /* Xmit holding register empty */
1692 +#define LSR_TEMT 0x40  /* Xmitter empty */
1693 +#define LSR_ERR  0x80  /* Error */
1694 diff -Naru linux/arch/mips/zboot/include/pb1000_serial.h linux-new/arch/mips/zboot/include/pb1000_serial.h
1695 --- linux/arch/mips/zboot/include/pb1000_serial.h       1969-12-31 19:00:00.000000000 -0500
1696 +++ linux-new/arch/mips/zboot/include/pb1000_serial.h   2003-12-18 14:26:20.000000000 -0500
1697 @@ -0,0 +1,20 @@
1698 +/*
1699 + * arch/ppc/boot/include/sandpoint_serial.h
1700 + * 
1701 + * Location of the COM ports on Motorola SPS Sandpoint machines
1702 + *
1703 + * Author: Mark A. Greer
1704 + *        mgreer@mvista.com
1705 + *
1706 + * Copyright 2001 MontaVista Software Inc.
1707 + *
1708 + * This program is free software; you can redistribute  it and/or modify it
1709 + * under  the terms of  the GNU General  Public License as published by the
1710 + * Free Software Foundation;  either version 2 of the  License, or (at your
1711 + * option) any later version.
1712 + */
1713 +
1714 +#define COM1 0xfe0003f8
1715 +#define COM2 0xfe0002f8
1716 +#define COM3 0x00000000                /* No COM3 */
1717 +#define COM4 0x00000000                /* No COM4 */
1718 diff -Naru linux/arch/mips/zboot/include/zlib.h linux-new/arch/mips/zboot/include/zlib.h
1719 --- linux/arch/mips/zboot/include/zlib.h        1969-12-31 19:00:00.000000000 -0500
1720 +++ linux-new/arch/mips/zboot/include/zlib.h    2003-12-18 14:26:21.000000000 -0500
1721 @@ -0,0 +1,432 @@
1722 +/*     $Id: zlib.h,v 1.2 2002/02/16 16:55:45 ppopov Exp $      */
1723 +
1724 +/*
1725 + * This file is derived from zlib.h and zconf.h from the zlib-0.95
1726 + * distribution by Jean-loup Gailly and Mark Adler, with some additions
1727 + * by Paul Mackerras to aid in implementing Deflate compression and
1728 + * decompression for PPP packets.
1729 + */
1730 +
1731 +/*
1732 + *  ==FILEVERSION 960122==
1733 + *
1734 + * This marker is used by the Linux installation script to determine
1735 + * whether an up-to-date version of this file is already installed.
1736 + */
1737 +
1738 +/* zlib.h -- interface of the 'zlib' general purpose compression library
1739 +  version 0.95, Aug 16th, 1995.
1740 +
1741 +  Copyright (C) 1995 Jean-loup Gailly and Mark Adler
1742 +
1743 +  This software is provided 'as-is', without any express or implied
1744 +  warranty.  In no event will the authors be held liable for any damages
1745 +  arising from the use of this software.
1746 +
1747 +  Permission is granted to anyone to use this software for any purpose,
1748 +  including commercial applications, and to alter it and redistribute it
1749 +  freely, subject to the following restrictions:
1750 +
1751 +  1. The origin of this software must not be misrepresented; you must not
1752 +     claim that you wrote the original software. If you use this software
1753 +     in a product, an acknowledgment in the product documentation would be
1754 +     appreciated but is not required.
1755 +  2. Altered source versions must be plainly marked as such, and must not be
1756 +     misrepresented as being the original software.
1757 +  3. This notice may not be removed or altered from any source distribution.
1758 +
1759 +  Jean-loup Gailly        Mark Adler
1760 +  gzip@prep.ai.mit.edu    madler@alumni.caltech.edu
1761 + */
1762 +
1763 +#ifndef _ZLIB_H
1764 +#define _ZLIB_H
1765 +
1766 +/* #include "zconf.h" */       /* included directly here */
1767 +
1768 +/* zconf.h -- configuration of the zlib compression library
1769 + * Copyright (C) 1995 Jean-loup Gailly.
1770 + * For conditions of distribution and use, see copyright notice in zlib.h 
1771 + */
1772 +
1773 +/* From: zconf.h,v 1.12 1995/05/03 17:27:12 jloup Exp */
1774 +
1775 +/*
1776 +     The library does not install any signal handler. It is recommended to
1777 +  add at least a handler for SIGSEGV when decompressing; the library checks
1778 +  the consistency of the input data whenever possible but may go nuts
1779 +  for some forms of corrupted input.
1780 + */
1781 +
1782 +/*
1783 + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
1784 + * than 64k bytes at a time (needed on systems with 16-bit int).
1785 + * Compile with -DUNALIGNED_OK if it is OK to access shorts or ints
1786 + * at addresses which are not a multiple of their size.
1787 + * Under DOS, -DFAR=far or -DFAR=__far may be needed.
1788 + */
1789 +
1790 +#ifndef STDC
1791 +#  if defined(MSDOS) || defined(__STDC__) || defined(__cplusplus)
1792 +#    define STDC
1793 +#  endif
1794 +#endif
1795 +
1796 +#ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */
1797 +#  include <unix.h>
1798 +#endif
1799 +
1800 +/* Maximum value for memLevel in deflateInit2 */
1801 +#ifndef MAX_MEM_LEVEL
1802 +#  ifdef MAXSEG_64K
1803 +#    define MAX_MEM_LEVEL 8
1804 +#  else
1805 +#    define MAX_MEM_LEVEL 9
1806 +#  endif
1807 +#endif
1808 +
1809 +#ifndef FAR
1810 +#  define FAR
1811 +#endif
1812 +
1813 +/* Maximum value for windowBits in deflateInit2 and inflateInit2 */
1814 +#ifndef MAX_WBITS
1815 +#  define MAX_WBITS   15 /* 32K LZ77 window */
1816 +#endif
1817 +
1818 +/* The memory requirements for deflate are (in bytes):
1819 +            1 << (windowBits+2)   +  1 << (memLevel+9)
1820 + that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
1821 + plus a few kilobytes for small objects. For example, if you want to reduce
1822 + the default memory requirements from 256K to 128K, compile with
1823 +     make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
1824 + Of course this will generally degrade compression (there's no free lunch).
1825 +
1826 +   The memory requirements for inflate are (in bytes) 1 << windowBits
1827 + that is, 32K for windowBits=15 (default value) plus a few kilobytes
1828 + for small objects.
1829 +*/
1830 +
1831 +                        /* Type declarations */
1832 +
1833 +#ifndef OF /* function prototypes */
1834 +#  ifdef STDC
1835 +#    define OF(args)  args
1836 +#  else
1837 +#    define OF(args)  ()
1838 +#  endif
1839 +#endif
1840 +
1841 +typedef unsigned char  Byte;  /* 8 bits */
1842 +typedef unsigned int   uInt;  /* 16 bits or more */
1843 +typedef unsigned long  uLong; /* 32 bits or more */
1844 +
1845 +typedef Byte FAR Bytef;
1846 +typedef char FAR charf;
1847 +typedef int FAR intf;
1848 +typedef uInt FAR uIntf;
1849 +typedef uLong FAR uLongf;
1850 +
1851 +#ifdef STDC
1852 +   typedef void FAR *voidpf;
1853 +   typedef void     *voidp;
1854 +#else
1855 +   typedef Byte FAR *voidpf;
1856 +   typedef Byte     *voidp;
1857 +#endif
1858 +
1859 +/* end of original zconf.h */
1860 +
1861 +#define ZLIB_VERSION "0.95P"
1862 +
1863 +/* 
1864 +     The 'zlib' compression library provides in-memory compression and
1865 +  decompression functions, including integrity checks of the uncompressed
1866 +  data.  This version of the library supports only one compression method
1867 +  (deflation) but other algorithms may be added later and will have the same
1868 +  stream interface.
1869 +
1870 +     For compression the application must provide the output buffer and
1871 +  may optionally provide the input buffer for optimization. For decompression,
1872 +  the application must provide the input buffer and may optionally provide
1873 +  the output buffer for optimization.
1874 +
1875 +     Compression can be done in a single step if the buffers are large
1876 +  enough (for example if an input file is mmap'ed), or can be done by
1877 +  repeated calls of the compression function.  In the latter case, the
1878 +  application must provide more input and/or consume the output
1879 +  (providing more output space) before each call.
1880 +*/
1881 +
1882 +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
1883 +typedef void   (*free_func)  OF((voidpf opaque, voidpf address, uInt nbytes));
1884 +
1885 +struct internal_state;
1886 +
1887 +typedef struct z_stream_s {
1888 +    Bytef    *next_in;  /* next input byte */
1889 +    uInt     avail_in;  /* number of bytes available at next_in */
1890 +    uLong    total_in;  /* total nb of input bytes read so far */
1891 +
1892 +    Bytef    *next_out; /* next output byte should be put there */
1893 +    uInt     avail_out; /* remaining free space at next_out */
1894 +    uLong    total_out; /* total nb of bytes output so far */
1895 +
1896 +    char     *msg;      /* last error message, NULL if no error */
1897 +    struct internal_state FAR *state; /* not visible by applications */
1898 +
1899 +    alloc_func zalloc;  /* used to allocate the internal state */
1900 +    free_func  zfree;   /* used to free the internal state */
1901 +    voidp      opaque;  /* private data object passed to zalloc and zfree */
1902 +
1903 +    Byte     data_type; /* best guess about the data type: ascii or binary */
1904 +
1905 +} z_stream;
1906 +
1907 +/*
1908 +   The application must update next_in and avail_in when avail_in has
1909 +   dropped to zero. It must update next_out and avail_out when avail_out
1910 +   has dropped to zero. The application must initialize zalloc, zfree and
1911 +   opaque before calling the init function. All other fields are set by the
1912 +   compression library and must not be updated by the application.
1913 +
1914 +   The opaque value provided by the application will be passed as the first
1915 +   parameter for calls of zalloc and zfree. This can be useful for custom
1916 +   memory management. The compression library attaches no meaning to the
1917 +   opaque value.
1918 +
1919 +   zalloc must return Z_NULL if there is not enough memory for the object.
1920 +   On 16-bit systems, the functions zalloc and zfree must be able to allocate
1921 +   exactly 65536 bytes, but will not be required to allocate more than this
1922 +   if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
1923 +   pointers returned by zalloc for objects of exactly 65536 bytes *must*
1924 +   have their offset normalized to zero. The default allocation function
1925 +   provided by this library ensures this (see zutil.c). To reduce memory
1926 +   requirements and avoid any allocation of 64K objects, at the expense of
1927 +   compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
1928 +
1929 +   The fields total_in and total_out can be used for statistics or
1930 +   progress reports. After compression, total_in holds the total size of
1931 +   the uncompressed data and may be saved for use in the decompressor
1932 +   (particularly if the decompressor wants to decompress everything in
1933 +   a single step).
1934 +*/
1935 +
1936 +                        /* constants */
1937 +
1938 +#define Z_NO_FLUSH      0
1939 +#define Z_PARTIAL_FLUSH 1
1940 +#define Z_FULL_FLUSH    2
1941 +#define Z_SYNC_FLUSH    3 /* experimental: partial_flush + byte align */
1942 +#define Z_FINISH        4
1943 +#define Z_PACKET_FLUSH 5
1944 +/* See deflate() below for the usage of these constants */
1945 +
1946 +#define Z_OK            0
1947 +#define Z_STREAM_END    1
1948 +#define Z_ERRNO        (-1)
1949 +#define Z_STREAM_ERROR (-2)
1950 +#define Z_DATA_ERROR   (-3)
1951 +#define Z_MEM_ERROR    (-4)
1952 +#define Z_BUF_ERROR    (-5)
1953 +/* error codes for the compression/decompression functions */
1954 +
1955 +#define Z_BEST_SPEED             1
1956 +#define Z_BEST_COMPRESSION       9
1957 +#define Z_DEFAULT_COMPRESSION  (-1)
1958 +/* compression levels */
1959 +
1960 +#define Z_FILTERED            1
1961 +#define Z_HUFFMAN_ONLY        2
1962 +#define Z_DEFAULT_STRATEGY    0
1963 +
1964 +#define Z_BINARY   0
1965 +#define Z_ASCII    1
1966 +#define Z_UNKNOWN  2
1967 +/* Used to set the data_type field */
1968 +
1969 +#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
1970 +
1971 +extern char *zlib_version;
1972 +/* The application can compare zlib_version and ZLIB_VERSION for consistency.
1973 +   If the first character differs, the library code actually used is
1974 +   not compatible with the zlib.h header file used by the application.
1975 + */
1976 +
1977 +                        /* basic functions */
1978 +
1979 +extern int inflateInit OF((z_stream *strm));
1980 +/* 
1981 +     Initializes the internal stream state for decompression. The fields
1982 +   zalloc and zfree must be initialized before by the caller.  If zalloc and
1983 +   zfree are set to Z_NULL, inflateInit updates them to use default allocation
1984 +   functions.
1985 +
1986 +     inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
1987 +   enough memory.  msg is set to null if there is no error message.
1988 +   inflateInit does not perform any decompression: this will be done by
1989 +   inflate().
1990 +*/
1991 +
1992 +
1993 +extern int inflate OF((z_stream *strm, int flush));
1994 +/*
1995 +  Performs one or both of the following actions:
1996 +
1997 +  - Decompress more input starting at next_in and update next_in and avail_in
1998 +    accordingly. If not all input can be processed (because there is not
1999 +    enough room in the output buffer), next_in is updated and processing
2000 +    will resume at this point for the next call of inflate().
2001 +
2002 +  - Provide more output starting at next_out and update next_out and avail_out
2003 +    accordingly.  inflate() always provides as much output as possible
2004 +    (until there is no more input data or no more space in the output buffer).
2005 +
2006 +  Before the call of inflate(), the application should ensure that at least
2007 +  one of the actions is possible, by providing more input and/or consuming
2008 +  more output, and updating the next_* and avail_* values accordingly.
2009 +  The application can consume the uncompressed output when it wants, for
2010 +  example when the output buffer is full (avail_out == 0), or after each
2011 +  call of inflate().
2012 +
2013 +    If the parameter flush is set to Z_PARTIAL_FLUSH or Z_PACKET_FLUSH,
2014 +  inflate flushes as much output as possible to the output buffer. The
2015 +  flushing behavior of inflate is not specified for values of the flush
2016 +  parameter other than Z_PARTIAL_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the
2017 +  current implementation actually flushes as much output as possible
2018 +  anyway.  For Z_PACKET_FLUSH, inflate checks that once all the input data
2019 +  has been consumed, it is expecting to see the length field of a stored
2020 +  block; if not, it returns Z_DATA_ERROR.
2021 +
2022 +    inflate() should normally be called until it returns Z_STREAM_END or an
2023 +  error. However if all decompression is to be performed in a single step
2024 +  (a single call of inflate), the parameter flush should be set to
2025 +  Z_FINISH. In this case all pending input is processed and all pending
2026 +  output is flushed; avail_out must be large enough to hold all the
2027 +  uncompressed data. (The size of the uncompressed data may have been saved
2028 +  by the compressor for this purpose.) The next operation on this stream must
2029 +  be inflateEnd to deallocate the decompression state. The use of Z_FINISH
2030 +  is never required, but can be used to inform inflate that a faster routine
2031 +  may be used for the single inflate() call.
2032 +
2033 +    inflate() returns Z_OK if some progress has been made (more input
2034 +  processed or more output produced), Z_STREAM_END if the end of the
2035 +  compressed data has been reached and all uncompressed output has been
2036 +  produced, Z_DATA_ERROR if the input data was corrupted, Z_STREAM_ERROR if
2037 +  the stream structure was inconsistent (for example if next_in or next_out
2038 +  was NULL), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if no
2039 +  progress is possible or if there was not enough room in the output buffer
2040 +  when Z_FINISH is used. In the Z_DATA_ERROR case, the application may then
2041 +  call inflateSync to look for a good compression block.  */
2042 +
2043 +
2044 +extern int inflateEnd OF((z_stream *strm));
2045 +/*
2046 +     All dynamically allocated data structures for this stream are freed.
2047 +   This function discards any unprocessed input and does not flush any
2048 +   pending output.
2049 +
2050 +     inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
2051 +   was inconsistent. In the error case, msg may be set but then points to a
2052 +   static string (which must not be deallocated).
2053 +*/
2054 +
2055 +                        /* advanced functions */
2056 +
2057 +extern int inflateInit2 OF((z_stream *strm,
2058 +                            int  windowBits));
2059 +/*   
2060 +     This is another version of inflateInit with more compression options. The
2061 +   fields next_out, zalloc and zfree must be initialized before by the caller.
2062 +
2063 +     The windowBits parameter is the base two logarithm of the maximum window
2064 +   size (the size of the history buffer).  It should be in the range 8..15 for
2065 +   this version of the library (the value 16 will be allowed soon). The
2066 +   default value is 15 if inflateInit is used instead. If a compressed stream
2067 +   with a larger window size is given as input, inflate() will return with
2068 +   the error code Z_DATA_ERROR instead of trying to allocate a larger window.
2069 +
2070 +     If next_out is not null, the library will use this buffer for the history
2071 +   buffer; the buffer must either be large enough to hold the entire output
2072 +   data, or have at least 1<<windowBits bytes.  If next_out is null, the
2073 +   library will allocate its own buffer (and leave next_out null). next_in
2074 +   need not be provided here but must be provided by the application for the
2075 +   next call of inflate().
2076 +
2077 +     If the history buffer is provided by the application, next_out must
2078 +   never be changed by the application since the decompressor maintains
2079 +   history information inside this buffer from call to call; the application
2080 +   can only reset next_out to the beginning of the history buffer when
2081 +   avail_out is zero and all output has been consumed.
2082 +
2083 +      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
2084 +   not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
2085 +   windowBits < 8). msg is set to null if there is no error message.
2086 +   inflateInit2 does not perform any decompression: this will be done by
2087 +   inflate().
2088 +*/
2089 +
2090 +extern int inflateSync OF((z_stream *strm));
2091 +/* 
2092 +    Skips invalid compressed data until the special marker (see deflate()
2093 +  above) can be found, or until all available input is skipped. No output
2094 +  is provided.
2095 +
2096 +    inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR
2097 +  if no more input was provided, Z_DATA_ERROR if no marker has been found,
2098 +  or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
2099 +  case, the application may save the current current value of total_in which
2100 +  indicates where valid compressed data was found. In the error case, the
2101 +  application may repeatedly call inflateSync, providing more input each time,
2102 +  until success or end of the input data.
2103 +*/
2104 +
2105 +extern int inflateReset OF((z_stream *strm));
2106 +/*
2107 +     This function is equivalent to inflateEnd followed by inflateInit,
2108 +   but does not free and reallocate all the internal decompression state.
2109 +   The stream will keep attributes that may have been set by inflateInit2.
2110 +
2111 +      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
2112 +   stream state was inconsistent (such as zalloc or state being NULL).
2113 +*/
2114 +
2115 +extern int inflateIncomp OF((z_stream *strm));
2116 +/*
2117 +     This function adds the data at next_in (avail_in bytes) to the output
2118 +   history without performing any output.  There must be no pending output,
2119 +   and the decompressor must be expecting to see the start of a block.
2120 +   Calling this function is equivalent to decompressing a stored block
2121 +   containing the data at next_in (except that the data is not output).
2122 +*/
2123 +
2124 +                        /* checksum functions */
2125 +
2126 +/*
2127 +     This function is not related to compression but is exported
2128 +   anyway because it might be useful in applications using the
2129 +   compression library.
2130 +*/
2131 +
2132 +extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len));
2133 +
2134 +/*
2135 +     Update a running Adler-32 checksum with the bytes buf[0..len-1] and
2136 +   return the updated checksum. If buf is NULL, this function returns
2137 +   the required initial value for the checksum.
2138 +   An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
2139 +   much faster. Usage example:
2140 +
2141 +     uLong adler = adler32(0L, Z_NULL, 0);
2142 +
2143 +     while (read_buffer(buffer, length) != EOF) {
2144 +       adler = adler32(adler, buffer, length);
2145 +     }
2146 +     if (adler != original_adler) error();
2147 +*/
2148 +
2149 +#ifndef _Z_UTIL_H
2150 +    struct internal_state {int dummy;}; /* hack for buggy compilers */
2151 +#endif
2152 +
2153 +#endif /* _ZLIB_H */
2154 diff -Naru linux/arch/mips/zboot/ld.script linux-new/arch/mips/zboot/ld.script
2155 --- linux/arch/mips/zboot/ld.script     1969-12-31 19:00:00.000000000 -0500
2156 +++ linux-new/arch/mips/zboot/ld.script 2003-12-18 14:26:21.000000000 -0500
2157 @@ -0,0 +1,151 @@
2158 +OUTPUT_ARCH(mips)
2159 +ENTRY(start)
2160 +SECTIONS
2161 +{
2162 +  /* Read-only sections, merged into text segment: */
2163 +  /* . = 0x81000000; */
2164 +  .init          : { *(.init)          } =0
2165 +  .text      :
2166 +  {
2167 +    _ftext = . ;
2168 +    *(.text)
2169 +    *(.rodata)
2170 +    *(.rodata1)
2171 +    /* .gnu.warning sections are handled specially by elf32.em.  */
2172 +    *(.gnu.warning)
2173 +  } =0
2174 +  .kstrtab : { *(.kstrtab) }
2175 +
2176 +  . = ALIGN(16);               /* Exception table */
2177 +  __start___ex_table = .;
2178 +  __ex_table : { *(__ex_table) }
2179 +  __stop___ex_table = .;
2180 +
2181 +  __start___dbe_table = .;     /* Exception table for data bus errors */
2182 +  __dbe_table : { *(__dbe_table) }
2183 +  __stop___dbe_table = .;
2184 +
2185 +  __start___ksymtab = .;       /* Kernel symbol table */
2186 +  __ksymtab : { *(__ksymtab) }
2187 +  __stop___ksymtab = .;
2188 +
2189 +  _etext = .;
2190 +
2191 +  . = ALIGN(8192);
2192 +  .data.init_task : { *(.data.init_task) }
2193 +
2194 +  /* Startup code */
2195 +  . = ALIGN(4096);
2196 +  __init_begin = .;
2197 +  .text.init : { *(.text.init) }
2198 +  .data.init : { *(.data.init) }
2199 +  . = ALIGN(16);
2200 +  __setup_start = .;
2201 +  .setup.init : { *(.setup.init) }
2202 +  __setup_end = .;
2203 +  __initcall_start = .;
2204 +  .initcall.init : { *(.initcall.init) }
2205 +  __initcall_end = .;
2206 +  . = ALIGN(4096);     /* Align double page for init_task_union */
2207 +  __init_end = .;
2208 +
2209 +  . = ALIGN(4096);
2210 +  .data.page_aligned : { *(.data.idt) }
2211 +
2212 +  . = ALIGN(32);
2213 +  .data.cacheline_aligned : { *(.data.cacheline_aligned) }
2214 +
2215 +  .fini      : { *(.fini)    } =0
2216 +  .reginfo : { *(.reginfo) }
2217 +  /* Adjust the address for the data segment.  We want to adjust up to
2218 +     the same address within the page on the next page up.  It would
2219 +     be more correct to do this:
2220 +       . = .;
2221 +     The current expression does not correctly handle the case of a
2222 +     text segment ending precisely at the end of a page; it causes the
2223 +     data segment to skip a page.  The above expression does not have
2224 +     this problem, but it will currently (2/95) cause BFD to allocate
2225 +     a single segment, combining both text and data, for this case.
2226 +     This will prevent the text segment from being shared among
2227 +     multiple executions of the program; I think that is more
2228 +     important than losing a page of the virtual address space (note
2229 +     that no actual memory is lost; the page which is skipped can not
2230 +     be referenced).  */
2231 +  . = .;
2232 +  .data    :
2233 +  {
2234 +    _fdata = . ;
2235 +    *(.data)
2236 +
2237 +   /* Put the compressed image here, so bss is on the end. */
2238 +   __image_begin = .;
2239 +   *(.image)
2240 +   __image_end = .;
2241 +   /* Align the initial ramdisk image (INITRD) on page boundaries. */
2242 +   . = ALIGN(4096);
2243 +   __ramdisk_begin = .;
2244 +   *(.initrd)
2245 +   __ramdisk_end = .;
2246 +   . = ALIGN(4096);
2247 +
2248 +    CONSTRUCTORS
2249 +  }
2250 +  .data1   : { *(.data1) }
2251 +  _gp = . + 0x8000;
2252 +  .lit8 : { *(.lit8) }
2253 +  .lit4 : { *(.lit4) }
2254 +  .ctors         : { *(.ctors)   }
2255 +  .dtors         : { *(.dtors)   }
2256 +  .got           : { *(.got.plt) *(.got) }
2257 +  .dynamic       : { *(.dynamic) }
2258 +  /* We want the small data sections together, so single-instruction offsets
2259 +     can access them all, and initialized data all before uninitialized, so
2260 +     we can shorten the on-disk segment size.  */
2261 +  .sdata     : { *(.sdata) }
2262 +  . = ALIGN(4);
2263 +  _edata  =  .;
2264 +  PROVIDE (edata = .);
2265 +
2266 +  __bss_start = .;
2267 +  _fbss = .;
2268 +  .sbss      : { *(.sbss) *(.scommon) }
2269 +  .bss       :
2270 +  {
2271 +   *(.dynbss)
2272 +   *(.bss)
2273 +   *(COMMON)
2274 +   .  = ALIGN(4);
2275 +  _end = . ;
2276 +  PROVIDE (end = .);
2277 +  }
2278 +
2279 +  /* Sections to be discarded */
2280 +  /DISCARD/ :
2281 +  {
2282 +        *(.text.exit)
2283 +        *(.data.exit)
2284 +        *(.exitcall.exit)
2285 +  }
2286 +
2287 +  /* This is the MIPS specific mdebug section.  */
2288 +  .mdebug : { *(.mdebug) }
2289 +  /* These are needed for ELF backends which have not yet been
2290 +     converted to the new style linker.  */
2291 +  .stab 0 : { *(.stab) }
2292 +  .stabstr 0 : { *(.stabstr) }
2293 +  /* DWARF debug sections.
2294 +     Symbols in the .debug DWARF section are relative to the beginning of the
2295 +     section so we begin .debug at 0.  It's not clear yet what needs to happen
2296 +     for the others.   */
2297 +  .debug          0 : { *(.debug) }
2298 +  .debug_srcinfo  0 : { *(.debug_srcinfo) }
2299 +  .debug_aranges  0 : { *(.debug_aranges) }
2300 +  .debug_pubnames 0 : { *(.debug_pubnames) }
2301 +  .debug_sfnames  0 : { *(.debug_sfnames) }
2302 +  .line           0 : { *(.line) }
2303 +  /* These must appear regardless of  .  */
2304 +  .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }
2305 +  .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }
2306 +  .comment : { *(.comment) }
2307 +  .note : { *(.note) }
2308 +}
2309 diff -Naru linux/arch/mips/zboot/lib/Makefile linux-new/arch/mips/zboot/lib/Makefile
2310 --- linux/arch/mips/zboot/lib/Makefile  1969-12-31 19:00:00.000000000 -0500
2311 +++ linux-new/arch/mips/zboot/lib/Makefile      2003-12-18 14:26:21.000000000 -0500
2312 @@ -0,0 +1,9 @@
2313 +#
2314 +# Makefile for some libs needed by zImage.
2315 +#
2316 +
2317 +L_TARGET := zlib.a
2318 +
2319 +obj-y := zlib.o
2320 +
2321 +include $(TOPDIR)/Rules.make
2322 diff -Naru linux/arch/mips/zboot/lib/zlib.c linux-new/arch/mips/zboot/lib/zlib.c
2323 --- linux/arch/mips/zboot/lib/zlib.c    1969-12-31 19:00:00.000000000 -0500
2324 +++ linux-new/arch/mips/zboot/lib/zlib.c        2003-12-18 14:26:21.000000000 -0500
2325 @@ -0,0 +1,2148 @@
2326 +/*
2327 + * This file is derived from various .h and .c files from the zlib-0.95
2328 + * distribution by Jean-loup Gailly and Mark Adler, with some additions
2329 + * by Paul Mackerras to aid in implementing Deflate compression and
2330 + * decompression for PPP packets.  See zlib.h for conditions of
2331 + * distribution and use.
2332 + *
2333 + * Changes that have been made include:
2334 + * - changed functions not used outside this file to "local"
2335 + * - added minCompression parameter to deflateInit2
2336 + * - added Z_PACKET_FLUSH (see zlib.h for details)
2337 + * - added inflateIncomp
2338 + *
2339 + * $Id: zlib.c,v 1.2 2002/02/16 16:55:45 ppopov Exp $
2340 + */
2341 +
2342 +/*+++++*/
2343 +/* zutil.h -- internal interface and configuration of the compression library
2344 + * Copyright (C) 1995 Jean-loup Gailly.
2345 + * For conditions of distribution and use, see copyright notice in zlib.h
2346 + */
2347 +
2348 +/* WARNING: this file should *not* be used by applications. It is
2349 +   part of the implementation of the compression library and is
2350 +   subject to change. Applications should only use zlib.h.
2351 + */
2352 +
2353 +/* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */
2354 +
2355 +#define _Z_UTIL_H
2356 +
2357 +#include "zlib.h"
2358 +
2359 +#ifndef local
2360 +#  define local static
2361 +#endif
2362 +/* compile with -Dlocal if your debugger can't find static symbols */
2363 +
2364 +#define FAR
2365 +
2366 +typedef unsigned char  uch;
2367 +typedef uch FAR uchf;
2368 +typedef unsigned short ush;
2369 +typedef ush FAR ushf;
2370 +typedef unsigned long  ulg;
2371 +
2372 +extern char *z_errmsg[]; /* indexed by 1-zlib_error */
2373 +
2374 +#define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
2375 +/* To be used only when the state is known to be valid */
2376 +
2377 +#ifndef NULL
2378 +#define NULL   ((void *) 0)
2379 +#endif
2380 +
2381 +        /* common constants */
2382 +
2383 +#define DEFLATED   8
2384 +
2385 +#ifndef DEF_WBITS
2386 +#  define DEF_WBITS MAX_WBITS
2387 +#endif
2388 +/* default windowBits for decompression. MAX_WBITS is for compression only */
2389 +
2390 +#if MAX_MEM_LEVEL >= 8
2391 +#  define DEF_MEM_LEVEL 8
2392 +#else
2393 +#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
2394 +#endif
2395 +/* default memLevel */
2396 +
2397 +#define STORED_BLOCK 0
2398 +#define STATIC_TREES 1
2399 +#define DYN_TREES    2
2400 +/* The three kinds of block type */
2401 +
2402 +#define MIN_MATCH  3
2403 +#define MAX_MATCH  258
2404 +/* The minimum and maximum match lengths */
2405 +
2406 +         /* functions */
2407 +
2408 +#include <linux/string.h>
2409 +#define zmemcpy memcpy
2410 +#define zmemzero(dest, len)    memset(dest, 0, len)
2411 +
2412 +/* Diagnostic functions */
2413 +#ifdef DEBUG_ZLIB
2414 +#  include <stdio.h>
2415 +#  ifndef verbose
2416 +#    define verbose 0
2417 +#  endif
2418 +#  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
2419 +#  define Trace(x) fprintf x
2420 +#  define Tracev(x) {if (verbose) fprintf x ;}
2421 +#  define Tracevv(x) {if (verbose>1) fprintf x ;}
2422 +#  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
2423 +#  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
2424 +#else
2425 +#  define Assert(cond,msg)
2426 +#  define Trace(x)
2427 +#  define Tracev(x)
2428 +#  define Tracevv(x)
2429 +#  define Tracec(c,x)
2430 +#  define Tracecv(c,x)
2431 +#endif
2432 +
2433 +
2434 +typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));
2435 +
2436 +/* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */
2437 +/* void   zcfree  OF((voidpf opaque, voidpf ptr)); */
2438 +
2439 +#define ZALLOC(strm, items, size) \
2440 +           (*((strm)->zalloc))((strm)->opaque, (items), (size))
2441 +#define ZFREE(strm, addr, size)        \
2442 +          (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
2443 +#define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);}
2444 +
2445 +/* deflate.h -- internal compression state
2446 + * Copyright (C) 1995 Jean-loup Gailly
2447 + * For conditions of distribution and use, see copyright notice in zlib.h 
2448 + */
2449 +
2450 +/* WARNING: this file should *not* be used by applications. It is
2451 +   part of the implementation of the compression library and is
2452 +   subject to change. Applications should only use zlib.h.
2453 + */
2454 +
2455 +/*+++++*/
2456 +/* infblock.h -- header to use infblock.c
2457 + * Copyright (C) 1995 Mark Adler
2458 + * For conditions of distribution and use, see copyright notice in zlib.h 
2459 + */
2460 +
2461 +/* WARNING: this file should *not* be used by applications. It is
2462 +   part of the implementation of the compression library and is
2463 +   subject to change. Applications should only use zlib.h.
2464 + */
2465 +
2466 +struct inflate_blocks_state;
2467 +typedef struct inflate_blocks_state FAR inflate_blocks_statef;
2468 +
2469 +local inflate_blocks_statef * inflate_blocks_new OF((
2470 +    z_stream *z,
2471 +    check_func c,               /* check function */
2472 +    uInt w));                   /* window size */
2473 +
2474 +local int inflate_blocks OF((
2475 +    inflate_blocks_statef *,
2476 +    z_stream *,
2477 +    int));                      /* initial return code */
2478 +
2479 +local void inflate_blocks_reset OF((
2480 +    inflate_blocks_statef *,
2481 +    z_stream *,
2482 +    uLongf *));                  /* check value on output */
2483 +
2484 +local int inflate_blocks_free OF((
2485 +    inflate_blocks_statef *,
2486 +    z_stream *,
2487 +    uLongf *));                  /* check value on output */
2488 +
2489 +local int inflate_addhistory OF((
2490 +    inflate_blocks_statef *,
2491 +    z_stream *));
2492 +
2493 +local int inflate_packet_flush OF((
2494 +    inflate_blocks_statef *));
2495 +
2496 +/*+++++*/
2497 +/* inftrees.h -- header to use inftrees.c
2498 + * Copyright (C) 1995 Mark Adler
2499 + * For conditions of distribution and use, see copyright notice in zlib.h 
2500 + */
2501 +
2502 +/* WARNING: this file should *not* be used by applications. It is
2503 +   part of the implementation of the compression library and is
2504 +   subject to change. Applications should only use zlib.h.
2505 + */
2506 +
2507 +/* Huffman code lookup table entry--this entry is four bytes for machines
2508 +   that have 16-bit pointers (e.g. PC's in the small or medium model). */
2509 +
2510 +typedef struct inflate_huft_s FAR inflate_huft;
2511 +
2512 +struct inflate_huft_s {
2513 +  union {
2514 +    struct {
2515 +      Byte Exop;        /* number of extra bits or operation */
2516 +      Byte Bits;        /* number of bits in this code or subcode */
2517 +    } what;
2518 +    uInt Nalloc;       /* number of these allocated here */
2519 +    Bytef *pad;         /* pad structure to a power of 2 (4 bytes for */
2520 +  } word;               /*  16-bit, 8 bytes for 32-bit machines) */
2521 +  union {
2522 +    uInt Base;          /* literal, length base, or distance base */
2523 +    inflate_huft *Next; /* pointer to next level of table */
2524 +  } more;
2525 +};
2526 +
2527 +#ifdef DEBUG_ZLIB
2528 +  local uInt inflate_hufts;
2529 +#endif
2530 +
2531 +local int inflate_trees_bits OF((
2532 +    uIntf *,                    /* 19 code lengths */
2533 +    uIntf *,                    /* bits tree desired/actual depth */
2534 +    inflate_huft * FAR *,       /* bits tree result */
2535 +    z_stream *));               /* for zalloc, zfree functions */
2536 +
2537 +local int inflate_trees_dynamic OF((
2538 +    uInt,                       /* number of literal/length codes */
2539 +    uInt,                       /* number of distance codes */
2540 +    uIntf *,                    /* that many (total) code lengths */
2541 +    uIntf *,                    /* literal desired/actual bit depth */
2542 +    uIntf *,                    /* distance desired/actual bit depth */
2543 +    inflate_huft * FAR *,       /* literal/length tree result */
2544 +    inflate_huft * FAR *,       /* distance tree result */
2545 +    z_stream *));               /* for zalloc, zfree functions */
2546 +
2547 +local int inflate_trees_fixed OF((
2548 +    uIntf *,                    /* literal desired/actual bit depth */
2549 +    uIntf *,                    /* distance desired/actual bit depth */
2550 +    inflate_huft * FAR *,       /* literal/length tree result */
2551 +    inflate_huft * FAR *));     /* distance tree result */
2552 +
2553 +local int inflate_trees_free OF((
2554 +    inflate_huft *,             /* tables to free */
2555 +    z_stream *));               /* for zfree function */
2556 +
2557 +
2558 +/*+++++*/
2559 +/* infcodes.h -- header to use infcodes.c
2560 + * Copyright (C) 1995 Mark Adler
2561 + * For conditions of distribution and use, see copyright notice in zlib.h 
2562 + */
2563 +
2564 +/* WARNING: this file should *not* be used by applications. It is
2565 +   part of the implementation of the compression library and is
2566 +   subject to change. Applications should only use zlib.h.
2567 + */
2568 +
2569 +struct inflate_codes_state;
2570 +typedef struct inflate_codes_state FAR inflate_codes_statef;
2571 +
2572 +local inflate_codes_statef *inflate_codes_new OF((
2573 +    uInt, uInt,
2574 +    inflate_huft *, inflate_huft *,
2575 +    z_stream *));
2576 +
2577 +local int inflate_codes OF((
2578 +    inflate_blocks_statef *,
2579 +    z_stream *,
2580 +    int));
2581 +
2582 +local void inflate_codes_free OF((
2583 +    inflate_codes_statef *,
2584 +    z_stream *));
2585 +
2586 +
2587 +/*+++++*/
2588 +/* inflate.c -- zlib interface to inflate modules
2589 + * Copyright (C) 1995 Mark Adler
2590 + * For conditions of distribution and use, see copyright notice in zlib.h 
2591 + */
2592 +
2593 +/* inflate private state */
2594 +struct internal_state {
2595 +
2596 +  /* mode */
2597 +  enum {
2598 +      METHOD,   /* waiting for method byte */
2599 +      FLAG,     /* waiting for flag byte */
2600 +      BLOCKS,   /* decompressing blocks */
2601 +      CHECK4,   /* four check bytes to go */
2602 +      CHECK3,   /* three check bytes to go */
2603 +      CHECK2,   /* two check bytes to go */
2604 +      CHECK1,   /* one check byte to go */
2605 +      DONE,     /* finished check, done */
2606 +      BAD}      /* got an error--stay here */
2607 +    mode;               /* current inflate mode */
2608 +
2609 +  /* mode dependent information */
2610 +  union {
2611 +    uInt method;        /* if FLAGS, method byte */
2612 +    struct {
2613 +      uLong was;                /* computed check value */
2614 +      uLong need;               /* stream check value */
2615 +    } check;            /* if CHECK, check values to compare */
2616 +    uInt marker;        /* if BAD, inflateSync's marker bytes count */
2617 +  } sub;        /* submode */
2618 +
2619 +  /* mode independent information */
2620 +  int  nowrap;          /* flag for no wrapper */
2621 +  uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
2622 +  inflate_blocks_statef 
2623 +    *blocks;            /* current inflate_blocks state */
2624 +
2625 +};
2626 +
2627 +
2628 +int inflateReset(z)
2629 +z_stream *z;
2630 +{
2631 +  uLong c;
2632 +
2633 +  if (z == Z_NULL || z->state == Z_NULL)
2634 +    return Z_STREAM_ERROR;
2635 +  z->total_in = z->total_out = 0;
2636 +  z->msg = Z_NULL;
2637 +  z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
2638 +  inflate_blocks_reset(z->state->blocks, z, &c);
2639 +  Trace((stderr, "inflate: reset\n"));
2640 +  return Z_OK;
2641 +}
2642 +
2643 +
2644 +int inflateEnd(z)
2645 +z_stream *z;
2646 +{
2647 +  uLong c;
2648 +
2649 +  if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
2650 +    return Z_STREAM_ERROR;
2651 +  if (z->state->blocks != Z_NULL)
2652 +    inflate_blocks_free(z->state->blocks, z, &c);
2653 +  ZFREE(z, z->state, sizeof(struct internal_state));
2654 +  z->state = Z_NULL;
2655 +  Trace((stderr, "inflate: end\n"));
2656 +  return Z_OK;
2657 +}
2658 +
2659 +
2660 +int inflateInit2(z, w)
2661 +z_stream *z;
2662 +int w;
2663 +{
2664 +  /* initialize state */
2665 +  if (z == Z_NULL)
2666 +    return Z_STREAM_ERROR;
2667 +/*  if (z->zalloc == Z_NULL) z->zalloc = zcalloc; */
2668 +/*  if (z->zfree == Z_NULL) z->zfree = zcfree; */
2669 +  if ((z->state = (struct internal_state FAR *)
2670 +       ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
2671 +    return Z_MEM_ERROR;
2672 +  z->state->blocks = Z_NULL;
2673 +
2674 +  /* handle undocumented nowrap option (no zlib header or check) */
2675 +  z->state->nowrap = 0;
2676 +  if (w < 0)
2677 +  {
2678 +    w = - w;
2679 +    z->state->nowrap = 1;
2680 +  }
2681 +
2682 +  /* set window size */
2683 +  if (w < 8 || w > 15)
2684 +  {
2685 +    inflateEnd(z);
2686 +    return Z_STREAM_ERROR;
2687 +  }
2688 +  z->state->wbits = (uInt)w;
2689 +
2690 +  /* create inflate_blocks state */
2691 +  if ((z->state->blocks =
2692 +       inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
2693 +      == Z_NULL)
2694 +  {
2695 +    inflateEnd(z);
2696 +    return Z_MEM_ERROR;
2697 +  }
2698 +  Trace((stderr, "inflate: allocated\n"));
2699 +
2700 +  /* reset state */
2701 +  inflateReset(z);
2702 +  return Z_OK;
2703 +}
2704 +
2705 +
2706 +int inflateInit(z)
2707 +z_stream *z;
2708 +{
2709 +  return inflateInit2(z, DEF_WBITS);
2710 +}
2711 +
2712 +
2713 +#define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
2714 +#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
2715 +
2716 +int inflate(z, f)
2717 +z_stream *z;
2718 +int f;
2719 +{
2720 +  int r;
2721 +  uInt b;
2722 +
2723 +  if (z == Z_NULL || z->next_in == Z_NULL)
2724 +    return Z_STREAM_ERROR;
2725 +  r = Z_BUF_ERROR;
2726 +  while (1) switch (z->state->mode)
2727 +  {
2728 +    case METHOD:
2729 +      NEEDBYTE
2730 +      if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
2731 +      {
2732 +        z->state->mode = BAD;
2733 +        z->msg = "unknown compression method";
2734 +        z->state->sub.marker = 5;       /* can't try inflateSync */
2735 +        break;
2736 +      }
2737 +      if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
2738 +      {
2739 +        z->state->mode = BAD;
2740 +        z->msg = "invalid window size";
2741 +        z->state->sub.marker = 5;       /* can't try inflateSync */
2742 +        break;
2743 +      }
2744 +      z->state->mode = FLAG;
2745 +    case FLAG:
2746 +      NEEDBYTE
2747 +      if ((b = NEXTBYTE) & 0x20)
2748 +      {
2749 +        z->state->mode = BAD;
2750 +        z->msg = "invalid reserved bit";
2751 +        z->state->sub.marker = 5;       /* can't try inflateSync */
2752 +        break;
2753 +      }
2754 +      if (((z->state->sub.method << 8) + b) % 31)
2755 +      {
2756 +        z->state->mode = BAD;
2757 +        z->msg = "incorrect header check";
2758 +        z->state->sub.marker = 5;       /* can't try inflateSync */
2759 +        break;
2760 +      }
2761 +      Trace((stderr, "inflate: zlib header ok\n"));
2762 +      z->state->mode = BLOCKS;
2763 +    case BLOCKS:
2764 +      r = inflate_blocks(z->state->blocks, z, r);
2765 +      if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
2766 +         r = inflate_packet_flush(z->state->blocks);
2767 +      if (r == Z_DATA_ERROR)
2768 +      {
2769 +        z->state->mode = BAD;
2770 +        z->state->sub.marker = 0;       /* can try inflateSync */
2771 +        break;
2772 +      }
2773 +      if (r != Z_STREAM_END)
2774 +        return r;
2775 +      r = Z_OK;
2776 +      inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
2777 +      if (z->state->nowrap)
2778 +      {
2779 +        z->state->mode = DONE;
2780 +        break;
2781 +      }
2782 +      z->state->mode = CHECK4;
2783 +    case CHECK4:
2784 +      NEEDBYTE
2785 +      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
2786 +      z->state->mode = CHECK3;
2787 +    case CHECK3:
2788 +      NEEDBYTE
2789 +      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
2790 +      z->state->mode = CHECK2;
2791 +    case CHECK2:
2792 +      NEEDBYTE
2793 +      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
2794 +      z->state->mode = CHECK1;
2795 +    case CHECK1:
2796 +      NEEDBYTE
2797 +      z->state->sub.check.need += (uLong)NEXTBYTE;
2798 +
2799 +      if (z->state->sub.check.was != z->state->sub.check.need)
2800 +      {
2801 +        z->state->mode = BAD;
2802 +        z->msg = "incorrect data check";
2803 +        z->state->sub.marker = 5;       /* can't try inflateSync */
2804 +        break;
2805 +      }
2806 +      Trace((stderr, "inflate: zlib check ok\n"));
2807 +      z->state->mode = DONE;
2808 +    case DONE:
2809 +      return Z_STREAM_END;
2810 +    case BAD:
2811 +      return Z_DATA_ERROR;
2812 +    default:
2813 +      return Z_STREAM_ERROR;
2814 +  }
2815 +
2816 + empty:
2817 +  if (f != Z_PACKET_FLUSH)
2818 +    return r;
2819 +  z->state->mode = BAD;
2820 +  z->state->sub.marker = 0;       /* can try inflateSync */
2821 +  return Z_DATA_ERROR;
2822 +}
2823 +
2824 +/*
2825 + * This subroutine adds the data at next_in/avail_in to the output history
2826 + * without performing any output.  The output buffer must be "caught up";
2827 + * i.e. no pending output (hence s->read equals s->write), and the state must
2828 + * be BLOCKS (i.e. we should be willing to see the start of a series of
2829 + * BLOCKS).  On exit, the output will also be caught up, and the checksum
2830 + * will have been updated if need be.
2831 + */
2832 +
2833 +int inflateIncomp(z)
2834 +z_stream *z;
2835 +{
2836 +    if (z->state->mode != BLOCKS)
2837 +       return Z_DATA_ERROR;
2838 +    return inflate_addhistory(z->state->blocks, z);
2839 +}
2840 +
2841 +
2842 +int inflateSync(z)
2843 +z_stream *z;
2844 +{
2845 +  uInt n;       /* number of bytes to look at */
2846 +  Bytef *p;     /* pointer to bytes */
2847 +  uInt m;       /* number of marker bytes found in a row */
2848 +  uLong r, w;   /* temporaries to save total_in and total_out */
2849 +
2850 +  /* set up */
2851 +  if (z == Z_NULL || z->state == Z_NULL)
2852 +    return Z_STREAM_ERROR;
2853 +  if (z->state->mode != BAD)
2854 +  {
2855 +    z->state->mode = BAD;
2856 +    z->state->sub.marker = 0;
2857 +  }
2858 +  if ((n = z->avail_in) == 0)
2859 +    return Z_BUF_ERROR;
2860 +  p = z->next_in;
2861 +  m = z->state->sub.marker;
2862 +
2863 +  /* search */
2864 +  while (n && m < 4)
2865 +  {
2866 +    if (*p == (Byte)(m < 2 ? 0 : 0xff))
2867 +      m++;
2868 +    else if (*p)
2869 +      m = 0;
2870 +    else
2871 +      m = 4 - m;
2872 +    p++, n--;
2873 +  }
2874 +
2875 +  /* restore */
2876 +  z->total_in += p - z->next_in;
2877 +  z->next_in = p;
2878 +  z->avail_in = n;
2879 +  z->state->sub.marker = m;
2880 +
2881 +  /* return no joy or set up to restart on a new block */
2882 +  if (m != 4)
2883 +    return Z_DATA_ERROR;
2884 +  r = z->total_in;  w = z->total_out;
2885 +  inflateReset(z);
2886 +  z->total_in = r;  z->total_out = w;
2887 +  z->state->mode = BLOCKS;
2888 +  return Z_OK;
2889 +}
2890 +
2891 +#undef NEEDBYTE
2892 +#undef NEXTBYTE
2893 +
2894 +/*+++++*/
2895 +/* infutil.h -- types and macros common to blocks and codes
2896 + * Copyright (C) 1995 Mark Adler
2897 + * For conditions of distribution and use, see copyright notice in zlib.h 
2898 + */
2899 +
2900 +/* WARNING: this file should *not* be used by applications. It is
2901 +   part of the implementation of the compression library and is
2902 +   subject to change. Applications should only use zlib.h.
2903 + */
2904 +
2905 +/* inflate blocks semi-private state */
2906 +struct inflate_blocks_state {
2907 +
2908 +  /* mode */
2909 +  enum {
2910 +      TYPE,     /* get type bits (3, including end bit) */
2911 +      LENS,     /* get lengths for stored */
2912 +      STORED,   /* processing stored block */
2913 +      TABLE,    /* get table lengths */
2914 +      BTREE,    /* get bit lengths tree for a dynamic block */
2915 +      DTREE,    /* get length, distance trees for a dynamic block */
2916 +      CODES,    /* processing fixed or dynamic block */
2917 +      DRY,      /* output remaining window bytes */
2918 +      DONEB,     /* finished last block, done */
2919 +      BADB}      /* got a data error--stuck here */
2920 +    mode;               /* current inflate_block mode */
2921 +
2922 +  /* mode dependent information */
2923 +  union {
2924 +    uInt left;          /* if STORED, bytes left to copy */
2925 +    struct {
2926 +      uInt table;               /* table lengths (14 bits) */
2927 +      uInt index;               /* index into blens (or border) */
2928 +      uIntf *blens;             /* bit lengths of codes */
2929 +      uInt bb;                  /* bit length tree depth */
2930 +      inflate_huft *tb;         /* bit length decoding tree */
2931 +      int nblens;              /* # elements allocated at blens */
2932 +    } trees;            /* if DTREE, decoding info for trees */
2933 +    struct {
2934 +      inflate_huft *tl, *td;    /* trees to free */
2935 +      inflate_codes_statef 
2936 +         *codes;
2937 +    } decode;           /* if CODES, current state */
2938 +  } sub;                /* submode */
2939 +  uInt last;            /* true if this block is the last block */
2940 +
2941 +  /* mode independent information */
2942 +  uInt bitk;            /* bits in bit buffer */
2943 +  uLong bitb;           /* bit buffer */
2944 +  Bytef *window;        /* sliding window */
2945 +  Bytef *end;           /* one byte after sliding window */
2946 +  Bytef *read;          /* window read pointer */
2947 +  Bytef *write;         /* window write pointer */
2948 +  check_func checkfn;   /* check function */
2949 +  uLong check;          /* check on output */
2950 +
2951 +};
2952 +
2953 +
2954 +/* defines for inflate input/output */
2955 +/*   update pointers and return */
2956 +#define UPDBITS {s->bitb=b;s->bitk=k;}
2957 +#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
2958 +#define UPDOUT {s->write=q;}
2959 +#define UPDATE {UPDBITS UPDIN UPDOUT}
2960 +#define LEAVE {UPDATE return inflate_flush(s,z,r);}
2961 +/*   get bytes and bits */
2962 +#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
2963 +#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
2964 +#define NEXTBYTE (n--,*p++)
2965 +#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
2966 +#define DUMPBITS(j) {b>>=(j);k-=(j);}
2967 +/*   output bytes */
2968 +#define WAVAIL (q<s->read?s->read-q-1:s->end-q)
2969 +#define LOADOUT {q=s->write;m=WAVAIL;}
2970 +#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}}
2971 +#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
2972 +#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
2973 +#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
2974 +/*   load local pointers */
2975 +#define LOAD {LOADIN LOADOUT}
2976 +
2977 +/*
2978 + * The IBM 150 firmware munges the data right after _etext[].  This
2979 + * protects it. -- Cort
2980 + */
2981 +local uInt protect_mask[] = {0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0};
2982 +/* And'ing with mask[n] masks the lower n bits */
2983 +local uInt inflate_mask[] = {
2984 +    0x0000,
2985 +    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
2986 +    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
2987 +};
2988 +
2989 +/* copy as much as possible from the sliding window to the output area */
2990 +local int inflate_flush OF((
2991 +    inflate_blocks_statef *,
2992 +    z_stream *,
2993 +    int));
2994 +
2995 +/*+++++*/
2996 +/* inffast.h -- header to use inffast.c
2997 + * Copyright (C) 1995 Mark Adler
2998 + * For conditions of distribution and use, see copyright notice in zlib.h 
2999 + */
3000 +
3001 +/* WARNING: this file should *not* be used by applications. It is
3002 +   part of the implementation of the compression library and is
3003 +   subject to change. Applications should only use zlib.h.
3004 + */
3005 +
3006 +local int inflate_fast OF((
3007 +    uInt,
3008 +    uInt,
3009 +    inflate_huft *,
3010 +    inflate_huft *,
3011 +    inflate_blocks_statef *,
3012 +    z_stream *));
3013 +
3014 +
3015 +/*+++++*/
3016 +/* infblock.c -- interpret and process block types to last block
3017 + * Copyright (C) 1995 Mark Adler
3018 + * For conditions of distribution and use, see copyright notice in zlib.h 
3019 + */
3020 +
3021 +/* Table for deflate from PKZIP's appnote.txt. */
3022 +local uInt border[] = { /* Order of the bit length code lengths */
3023 +        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
3024 +
3025 +/*
3026 +   Notes beyond the 1.93a appnote.txt:
3027 +
3028 +   1. Distance pointers never point before the beginning of the output
3029 +      stream.
3030 +   2. Distance pointers can point back across blocks, up to 32k away.
3031 +   3. There is an implied maximum of 7 bits for the bit length table and
3032 +      15 bits for the actual data.
3033 +   4. If only one code exists, then it is encoded using one bit.  (Zero
3034 +      would be more efficient, but perhaps a little confusing.)  If two
3035 +      codes exist, they are coded using one bit each (0 and 1).
3036 +   5. There is no way of sending zero distance codes--a dummy must be
3037 +      sent if there are none.  (History: a pre 2.0 version of PKZIP would
3038 +      store blocks with no distance codes, but this was discovered to be
3039 +      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
3040 +      zero distance codes, which is sent as one code of zero bits in
3041 +      length.
3042 +   6. There are up to 286 literal/length codes.  Code 256 represents the
3043 +      end-of-block.  Note however that the static length tree defines
3044 +      288 codes just to fill out the Huffman codes.  Codes 286 and 287
3045 +      cannot be used though, since there is no length base or extra bits
3046 +      defined for them.  Similarily, there are up to 30 distance codes.
3047 +      However, static trees define 32 codes (all 5 bits) to fill out the
3048 +      Huffman codes, but the last two had better not show up in the data.
3049 +   7. Unzip can check dynamic Huffman blocks for complete code sets.
3050 +      The exception is that a single code would not be complete (see #4).
3051 +   8. The five bits following the block type is really the number of
3052 +      literal codes sent minus 257.
3053 +   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
3054 +      (1+6+6).  Therefore, to output three times the length, you output
3055 +      three codes (1+1+1), whereas to output four times the same length,
3056 +      you only need two codes (1+3).  Hmm.
3057 +  10. In the tree reconstruction algorithm, Code = Code + Increment
3058 +      only if BitLength(i) is not zero.  (Pretty obvious.)
3059 +  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
3060 +  12. Note: length code 284 can represent 227-258, but length code 285
3061 +      really is 258.  The last length deserves its own, short code
3062 +      since it gets used a lot in very redundant files.  The length
3063 +      258 is special since 258 - 3 (the min match length) is 255.
3064 +  13. The literal/length and distance code bit lengths are read as a
3065 +      single stream of lengths.  It is possible (and advantageous) for
3066 +      a repeat code (16, 17, or 18) to go across the boundary between
3067 +      the two sets of lengths.
3068 + */
3069 +
3070 +
3071 +local void inflate_blocks_reset(s, z, c)
3072 +inflate_blocks_statef *s;
3073 +z_stream *z;
3074 +uLongf *c;
3075 +{
3076 +  if (s->checkfn != Z_NULL)
3077 +    *c = s->check;
3078 +  if (s->mode == BTREE || s->mode == DTREE)
3079 +    ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
3080 +  if (s->mode == CODES)
3081 +  {
3082 +    inflate_codes_free(s->sub.decode.codes, z);
3083 +    inflate_trees_free(s->sub.decode.td, z);
3084 +    inflate_trees_free(s->sub.decode.tl, z);
3085 +  }
3086 +  s->mode = TYPE;
3087 +  s->bitk = 0;
3088 +  s->bitb = 0;
3089 +  s->read = s->write = s->window;
3090 +  if (s->checkfn != Z_NULL)
3091 +    s->check = (*s->checkfn)(0L, Z_NULL, 0);
3092 +  Trace((stderr, "inflate:   blocks reset\n"));
3093 +}
3094 +
3095 +
3096 +local inflate_blocks_statef *inflate_blocks_new(z, c, w)
3097 +z_stream *z;
3098 +check_func c;
3099 +uInt w;
3100 +{
3101 +  inflate_blocks_statef *s;
3102 +
3103 +  if ((s = (inflate_blocks_statef *)ZALLOC
3104 +       (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
3105 +    return s;
3106 +  if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
3107 +  {
3108 +    ZFREE(z, s, sizeof(struct inflate_blocks_state));
3109 +    return Z_NULL;
3110 +  }
3111 +  s->end = s->window + w;
3112 +  s->checkfn = c;
3113 +  s->mode = TYPE;
3114 +  Trace((stderr, "inflate:   blocks allocated\n"));
3115 +  inflate_blocks_reset(s, z, &s->check);
3116 +  return s;
3117 +}
3118 +
3119 +
3120 +local int inflate_blocks(s, z, r)
3121 +inflate_blocks_statef *s;
3122 +z_stream *z;
3123 +int r;
3124 +{
3125 +  uInt t;               /* temporary storage */
3126 +  uLong b;              /* bit buffer */
3127 +  uInt k;               /* bits in bit buffer */
3128 +  Bytef *p;             /* input data pointer */
3129 +  uInt n;               /* bytes available there */
3130 +  Bytef *q;             /* output window write pointer */
3131 +  uInt m;               /* bytes to end of window or read pointer */
3132 +
3133 +  /* copy input/output information to locals (UPDATE macro restores) */
3134 +  LOAD
3135 +
3136 +  /* process input based on current state */
3137 +  while (1) switch (s->mode)
3138 +  {
3139 +    case TYPE:
3140 +      NEEDBITS(3)
3141 +      t = (uInt)b & 7;
3142 +      s->last = t & 1;
3143 +      switch (t >> 1)
3144 +      {
3145 +        case 0:                         /* stored */
3146 +          Trace((stderr, "inflate:     stored block%s\n",
3147 +                 s->last ? " (last)" : ""));
3148 +          DUMPBITS(3)
3149 +          t = k & 7;                    /* go to byte boundary */
3150 +          DUMPBITS(t)
3151 +          s->mode = LENS;               /* get length of stored block */
3152 +          break;
3153 +        case 1:                         /* fixed */
3154 +          Trace((stderr, "inflate:     fixed codes block%s\n",
3155 +                 s->last ? " (last)" : ""));
3156 +          {
3157 +            uInt bl, bd;
3158 +            inflate_huft *tl, *td;
3159 +
3160 +            inflate_trees_fixed(&bl, &bd, &tl, &td);
3161 +            s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
3162 +            if (s->sub.decode.codes == Z_NULL)
3163 +            {
3164 +              r = Z_MEM_ERROR;
3165 +              LEAVE
3166 +            }
3167 +            s->sub.decode.tl = Z_NULL;  /* don't try to free these */
3168 +            s->sub.decode.td = Z_NULL;
3169 +          }
3170 +          DUMPBITS(3)
3171 +          s->mode = CODES;
3172 +          break;
3173 +        case 2:                         /* dynamic */
3174 +          Trace((stderr, "inflate:     dynamic codes block%s\n",
3175 +                 s->last ? " (last)" : ""));
3176 +          DUMPBITS(3)
3177 +          s->mode = TABLE;
3178 +          break;
3179 +        case 3:                         /* illegal */
3180 +          DUMPBITS(3)
3181 +          s->mode = BADB;
3182 +          z->msg = "invalid block type";
3183 +          r = Z_DATA_ERROR;
3184 +          LEAVE
3185 +      }
3186 +      break;
3187 +    case LENS:
3188 +      NEEDBITS(32)
3189 +      if (((~b) >> 16) != (b & 0xffff))
3190 +      {
3191 +        s->mode = BADB;
3192 +        z->msg = "invalid stored block lengths";
3193 +        r = Z_DATA_ERROR;
3194 +        LEAVE
3195 +      }
3196 +      s->sub.left = (uInt)b & 0xffff;
3197 +      b = k = 0;                      /* dump bits */
3198 +      Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
3199 +      s->mode = s->sub.left ? STORED : TYPE;
3200 +      break;
3201 +    case STORED:
3202 +      if (n == 0)
3203 +        LEAVE
3204 +      NEEDOUT
3205 +      t = s->sub.left;
3206 +      if (t > n) t = n;
3207 +      if (t > m) t = m;
3208 +      zmemcpy(q, p, t);
3209 +      p += t;  n -= t;
3210 +      q += t;  m -= t;
3211 +      if ((s->sub.left -= t) != 0)
3212 +        break;
3213 +      Tracev((stderr, "inflate:       stored end, %lu total out\n",
3214 +              z->total_out + (q >= s->read ? q - s->read :
3215 +              (s->end - s->read) + (q - s->window))));
3216 +      s->mode = s->last ? DRY : TYPE;
3217 +      break;
3218 +    case TABLE:
3219 +      NEEDBITS(14)
3220 +      s->sub.trees.table = t = (uInt)b & 0x3fff;
3221 +#ifndef PKZIP_BUG_WORKAROUND
3222 +      if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
3223 +      {
3224 +        s->mode = BADB;
3225 +        z->msg = "too many length or distance symbols";
3226 +        r = Z_DATA_ERROR;
3227 +        LEAVE
3228 +      }
3229 +#endif
3230 +      t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
3231 +      if (t < 19)
3232 +        t = 19;
3233 +      if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
3234 +      {
3235 +        r = Z_MEM_ERROR;
3236 +        LEAVE
3237 +      }
3238 +      s->sub.trees.nblens = t;
3239 +      DUMPBITS(14)
3240 +      s->sub.trees.index = 0;
3241 +      Tracev((stderr, "inflate:       table sizes ok\n"));
3242 +      s->mode = BTREE;
3243 +    case BTREE:
3244 +      while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
3245 +      {
3246 +        NEEDBITS(3)
3247 +        s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
3248 +        DUMPBITS(3)
3249 +      }
3250 +      while (s->sub.trees.index < 19)
3251 +        s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
3252 +      s->sub.trees.bb = 7;
3253 +      t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
3254 +                             &s->sub.trees.tb, z);
3255 +      if (t != Z_OK)
3256 +      {
3257 +        r = t;
3258 +        if (r == Z_DATA_ERROR)
3259 +          s->mode = BADB;
3260 +        LEAVE
3261 +      }
3262 +      s->sub.trees.index = 0;
3263 +      Tracev((stderr, "inflate:       bits tree ok\n"));
3264 +      s->mode = DTREE;
3265 +    case DTREE:
3266 +      while (t = s->sub.trees.table,
3267 +             s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
3268 +      {
3269 +        inflate_huft *h;
3270 +        uInt i, j, c;
3271 +
3272 +        t = s->sub.trees.bb;
3273 +        NEEDBITS(t)
3274 +        h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
3275 +        t = h->word.what.Bits;
3276 +        c = h->more.Base;
3277 +        if (c < 16)
3278 +        {
3279 +          DUMPBITS(t)
3280 +          s->sub.trees.blens[s->sub.trees.index++] = c;
3281 +        }
3282 +        else /* c == 16..18 */
3283 +        {
3284 +          i = c == 18 ? 7 : c - 14;
3285 +          j = c == 18 ? 11 : 3;
3286 +          NEEDBITS(t + i)
3287 +          DUMPBITS(t)
3288 +          j += (uInt)b & inflate_mask[i];
3289 +          DUMPBITS(i)
3290 +          i = s->sub.trees.index;
3291 +          t = s->sub.trees.table;
3292 +          if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
3293 +              (c == 16 && i < 1))
3294 +          {
3295 +            s->mode = BADB;
3296 +            z->msg = "invalid bit length repeat";
3297 +            r = Z_DATA_ERROR;
3298 +            LEAVE
3299 +          }
3300 +          c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
3301 +          do {
3302 +            s->sub.trees.blens[i++] = c;
3303 +          } while (--j);
3304 +          s->sub.trees.index = i;
3305 +        }
3306 +      }
3307 +      inflate_trees_free(s->sub.trees.tb, z);
3308 +      s->sub.trees.tb = Z_NULL;
3309 +      {
3310 +        uInt bl, bd;
3311 +        inflate_huft *tl, *td;
3312 +        inflate_codes_statef *c;
3313 +
3314 +        bl = 9;         /* must be <= 9 for lookahead assumptions */
3315 +        bd = 6;         /* must be <= 9 for lookahead assumptions */
3316 +        t = s->sub.trees.table;
3317 +        t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
3318 +                                  s->sub.trees.blens, &bl, &bd, &tl, &td, z);
3319 +        if (t != Z_OK)
3320 +        {
3321 +          if (t == (uInt)Z_DATA_ERROR)
3322 +            s->mode = BADB;
3323 +          r = t;
3324 +          LEAVE
3325 +        }
3326 +        Tracev((stderr, "inflate:       trees ok\n"));
3327 +        if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
3328 +        {
3329 +          inflate_trees_free(td, z);
3330 +          inflate_trees_free(tl, z);
3331 +          r = Z_MEM_ERROR;
3332 +          LEAVE
3333 +        }
3334 +        ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
3335 +        s->sub.decode.codes = c;
3336 +        s->sub.decode.tl = tl;
3337 +        s->sub.decode.td = td;
3338 +      }
3339 +      s->mode = CODES;
3340 +    case CODES:
3341 +      UPDATE
3342 +      if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
3343 +        return inflate_flush(s, z, r);
3344 +      r = Z_OK;
3345 +      inflate_codes_free(s->sub.decode.codes, z);
3346 +      inflate_trees_free(s->sub.decode.td, z);
3347 +      inflate_trees_free(s->sub.decode.tl, z);
3348 +      LOAD
3349 +      Tracev((stderr, "inflate:       codes end, %lu total out\n",
3350 +              z->total_out + (q >= s->read ? q - s->read :
3351 +              (s->end - s->read) + (q - s->window))));
3352 +      if (!s->last)
3353 +      {
3354 +        s->mode = TYPE;
3355 +        break;
3356 +      }
3357 +      if (k > 7)              /* return unused byte, if any */
3358 +      {
3359 +        Assert(k < 16, "inflate_codes grabbed too many bytes")
3360 +        k -= 8;
3361 +        n++;
3362 +        p--;                    /* can always return one */
3363 +      }
3364 +      s->mode = DRY;
3365 +    case DRY:
3366 +      FLUSH
3367 +      if (s->read != s->write)
3368 +        LEAVE
3369 +      s->mode = DONEB;
3370 +    case DONEB:
3371 +      r = Z_STREAM_END;
3372 +      LEAVE
3373 +    case BADB:
3374 +      r = Z_DATA_ERROR;
3375 +      LEAVE
3376 +    default:
3377 +      r = Z_STREAM_ERROR;
3378 +      LEAVE
3379 +  }
3380 +}
3381 +
3382 +
3383 +local int inflate_blocks_free(s, z, c)
3384 +inflate_blocks_statef *s;
3385 +z_stream *z;
3386 +uLongf *c;
3387 +{
3388 +  inflate_blocks_reset(s, z, c);
3389 +  ZFREE(z, s->window, s->end - s->window);
3390 +  ZFREE(z, s, sizeof(struct inflate_blocks_state));
3391 +  Trace((stderr, "inflate:   blocks freed\n"));
3392 +  return Z_OK;
3393 +}
3394 +
3395 +/*
3396 + * This subroutine adds the data at next_in/avail_in to the output history
3397 + * without performing any output.  The output buffer must be "caught up";
3398 + * i.e. no pending output (hence s->read equals s->write), and the state must
3399 + * be BLOCKS (i.e. we should be willing to see the start of a series of
3400 + * BLOCKS).  On exit, the output will also be caught up, and the checksum
3401 + * will have been updated if need be.
3402 + */
3403 +local int inflate_addhistory(s, z)
3404 +inflate_blocks_statef *s;
3405 +z_stream *z;
3406 +{
3407 +    uLong b;              /* bit buffer */  /* NOT USED HERE */
3408 +    uInt k;               /* bits in bit buffer */ /* NOT USED HERE */
3409 +    uInt t;               /* temporary storage */
3410 +    Bytef *p;             /* input data pointer */
3411 +    uInt n;               /* bytes available there */
3412 +    Bytef *q;             /* output window write pointer */
3413 +    uInt m;               /* bytes to end of window or read pointer */
3414 +
3415 +    if (s->read != s->write)
3416 +       return Z_STREAM_ERROR;
3417 +    if (s->mode != TYPE)
3418 +       return Z_DATA_ERROR;
3419 +
3420 +    /* we're ready to rock */
3421 +    LOAD
3422 +    /* while there is input ready, copy to output buffer, moving
3423 +     * pointers as needed.
3424 +     */
3425 +    while (n) {
3426 +       t = n;  /* how many to do */
3427 +       /* is there room until end of buffer? */
3428 +       if (t > m) t = m;
3429 +       /* update check information */
3430 +       if (s->checkfn != Z_NULL)
3431 +           s->check = (*s->checkfn)(s->check, q, t);
3432 +       zmemcpy(q, p, t);
3433 +       q += t;
3434 +       p += t;
3435 +       n -= t;
3436 +       z->total_out += t;
3437 +       s->read = q;    /* drag read pointer forward */
3438 +/*      WRAP  */       /* expand WRAP macro by hand to handle s->read */
3439 +       if (q == s->end) {
3440 +           s->read = q = s->window;
3441 +           m = WAVAIL;
3442 +       }
3443 +    }
3444 +    UPDATE
3445 +    return Z_OK;
3446 +}
3447 +
3448 +
3449 +/*
3450 + * At the end of a Deflate-compressed PPP packet, we expect to have seen
3451 + * a `stored' block type value but not the (zero) length bytes.
3452 + */
3453 +local int inflate_packet_flush(s)
3454 +    inflate_blocks_statef *s;
3455 +{
3456 +    if (s->mode != LENS)
3457 +       return Z_DATA_ERROR;
3458 +    s->mode = TYPE;
3459 +    return Z_OK;
3460 +}
3461 +
3462 +
3463 +/*+++++*/
3464 +/* inftrees.c -- generate Huffman trees for efficient decoding
3465 + * Copyright (C) 1995 Mark Adler
3466 + * For conditions of distribution and use, see copyright notice in zlib.h 
3467 + */
3468 +
3469 +/* simplify the use of the inflate_huft type with some defines */
3470 +#define base more.Base
3471 +#define next more.Next
3472 +#define exop word.what.Exop
3473 +#define bits word.what.Bits
3474 +
3475 +
3476 +local int huft_build OF((
3477 +    uIntf *,            /* code lengths in bits */
3478 +    uInt,               /* number of codes */
3479 +    uInt,               /* number of "simple" codes */
3480 +    uIntf *,            /* list of base values for non-simple codes */
3481 +    uIntf *,            /* list of extra bits for non-simple codes */
3482 +    inflate_huft * FAR*,/* result: starting table */
3483 +    uIntf *,            /* maximum lookup bits (returns actual) */
3484 +    z_stream *));       /* for zalloc function */
3485 +
3486 +local voidpf falloc OF((
3487 +    voidpf,             /* opaque pointer (not used) */
3488 +    uInt,               /* number of items */
3489 +    uInt));             /* size of item */
3490 +
3491 +local void ffree OF((
3492 +    voidpf q,           /* opaque pointer (not used) */
3493 +    voidpf p,           /* what to free (not used) */
3494 +    uInt n));          /* number of bytes (not used) */
3495 +
3496 +/* Tables for deflate from PKZIP's appnote.txt. */
3497 +local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
3498 +        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
3499 +        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
3500 +        /* actually lengths - 2; also see note #13 above about 258 */
3501 +local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
3502 +        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3503 +        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
3504 +local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
3505 +        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
3506 +        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
3507 +        8193, 12289, 16385, 24577};
3508 +local uInt cpdext[] = { /* Extra bits for distance codes */
3509 +        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
3510 +        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
3511 +        12, 12, 13, 13};
3512 +
3513 +/*
3514 +   Huffman code decoding is performed using a multi-level table lookup.
3515 +   The fastest way to decode is to simply build a lookup table whose
3516 +   size is determined by the longest code.  However, the time it takes
3517 +   to build this table can also be a factor if the data being decoded
3518 +   is not very long.  The most common codes are necessarily the
3519 +   shortest codes, so those codes dominate the decoding time, and hence
3520 +   the speed.  The idea is you can have a shorter table that decodes the
3521 +   shorter, more probable codes, and then point to subsidiary tables for
3522 +   the longer codes.  The time it costs to decode the longer codes is
3523 +   then traded against the time it takes to make longer tables.
3524 +
3525 +   This results of this trade are in the variables lbits and dbits
3526 +   below.  lbits is the number of bits the first level table for literal/
3527 +   length codes can decode in one step, and dbits is the same thing for
3528 +   the distance codes.  Subsequent tables are also less than or equal to
3529 +   those sizes.  These values may be adjusted either when all of the
3530 +   codes are shorter than that, in which case the longest code length in
3531 +   bits is used, or when the shortest code is *longer* than the requested
3532 +   table size, in which case the length of the shortest code in bits is
3533 +   used.
3534 +
3535 +   There are two different values for the two tables, since they code a
3536 +   different number of possibilities each.  The literal/length table
3537 +   codes 286 possible values, or in a flat code, a little over eight
3538 +   bits.  The distance table codes 30 possible values, or a little less
3539 +   than five bits, flat.  The optimum values for speed end up being
3540 +   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
3541 +   The optimum values may differ though from machine to machine, and
3542 +   possibly even between compilers.  Your mileage may vary.
3543 + */
3544 +
3545 +
3546 +/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
3547 +#define BMAX 15         /* maximum bit length of any code */
3548 +#define N_MAX 288       /* maximum number of codes in any set */
3549 +
3550 +#ifdef DEBUG_ZLIB
3551 +  uInt inflate_hufts;
3552 +#endif
3553 +
3554 +local int huft_build(b, n, s, d, e, t, m, zs)
3555 +uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
3556 +uInt n;                 /* number of codes (assumed <= N_MAX) */
3557 +uInt s;                 /* number of simple-valued codes (0..s-1) */
3558 +uIntf *d;               /* list of base values for non-simple codes */
3559 +uIntf *e;               /* list of extra bits for non-simple codes */  
3560 +inflate_huft * FAR *t;  /* result: starting table */
3561 +uIntf *m;               /* maximum lookup bits, returns actual */
3562 +z_stream *zs;           /* for zalloc function */
3563 +/* Given a list of code lengths and a maximum table size, make a set of
3564 +   tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
3565 +   if the given code set is incomplete (the tables are still built in this
3566 +   case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
3567 +   over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
3568 +{
3569 +
3570 +  uInt a;                       /* counter for codes of length k */
3571 +  uInt c[BMAX+1];               /* bit length count table */
3572 +  uInt f;                       /* i repeats in table every f entries */
3573 +  int g;                        /* maximum code length */
3574 +  int h;                        /* table level */
3575 +  register uInt i;              /* counter, current code */
3576 +  register uInt j;              /* counter */
3577 +  register int k;               /* number of bits in current code */
3578 +  int l;                        /* bits per table (returned in m) */
3579 +  register uIntf *p;            /* pointer into c[], b[], or v[] */
3580 +  inflate_huft *q;              /* points to current table */
3581 +  struct inflate_huft_s r;      /* table entry for structure assignment */
3582 +  inflate_huft *u[BMAX];        /* table stack */
3583 +  uInt v[N_MAX];                /* values in order of bit length */
3584 +  register int w;               /* bits before this table == (l * h) */
3585 +  uInt x[BMAX+1];               /* bit offsets, then code stack */
3586 +  uIntf *xp;                    /* pointer into x */
3587 +  int y;                        /* number of dummy codes added */
3588 +  uInt z;                       /* number of entries in current table */
3589 +
3590 +
3591 +  /* Generate counts for each bit length */
3592 +  p = c;
3593 +#define C0 *p++ = 0;
3594 +#define C2 C0 C0 C0 C0
3595 +#define C4 C2 C2 C2 C2
3596 +  C4                            /* clear c[]--assume BMAX+1 is 16 */
3597 +  p = b;  i = n;
3598 +  do {
3599 +    c[*p++]++;                  /* assume all entries <= BMAX */
3600 +  } while (--i);
3601 +  if (c[0] == n)                /* null input--all zero length codes */
3602 +  {
3603 +    *t = (inflate_huft *)Z_NULL;
3604 +    *m = 0;
3605 +    return Z_OK;
3606 +  }
3607 +
3608 +
3609 +  /* Find minimum and maximum length, bound *m by those */
3610 +  l = *m;
3611 +  for (j = 1; j <= BMAX; j++)
3612 +    if (c[j])
3613 +      break;
3614 +  k = j;                        /* minimum code length */
3615 +  if ((uInt)l < j)
3616 +    l = j;
3617 +  for (i = BMAX; i; i--)
3618 +    if (c[i])
3619 +      break;
3620 +  g = i;                        /* maximum code length */
3621 +  if ((uInt)l > i)
3622 +    l = i;
3623 +  *m = l;
3624 +
3625 +
3626 +  /* Adjust last length count to fill out codes, if needed */
3627 +  for (y = 1 << j; j < i; j++, y <<= 1)
3628 +    if ((y -= c[j]) < 0)
3629 +      return Z_DATA_ERROR;
3630 +  if ((y -= c[i]) < 0)
3631 +    return Z_DATA_ERROR;
3632 +  c[i] += y;
3633 +
3634 +
3635 +  /* Generate starting offsets into the value table for each length */
3636 +  x[1] = j = 0;
3637 +  p = c + 1;  xp = x + 2;
3638 +  while (--i) {                 /* note that i == g from above */
3639 +    *xp++ = (j += *p++);
3640 +  }
3641 +
3642 +
3643 +  /* Make a table of values in order of bit lengths */
3644 +  p = b;  i = 0;
3645 +  do {
3646 +    if ((j = *p++) != 0)
3647 +      v[x[j]++] = i;
3648 +  } while (++i < n);
3649 +
3650 +
3651 +  /* Generate the Huffman codes and for each, make the table entries */
3652 +  x[0] = i = 0;                 /* first Huffman code is zero */
3653 +  p = v;                        /* grab values in bit order */
3654 +  h = -1;                       /* no tables yet--level -1 */
3655 +  w = -l;                       /* bits decoded == (l * h) */
3656 +  u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
3657 +  q = (inflate_huft *)Z_NULL;   /* ditto */
3658 +  z = 0;                        /* ditto */
3659 +
3660 +  /* go through the bit lengths (k already is bits in shortest code) */
3661 +  for (; k <= g; k++)
3662 +  {
3663 +    a = c[k];
3664 +    while (a--)
3665 +    {
3666 +      /* here i is the Huffman code of length k bits for value *p */
3667 +      /* make tables up to required level */
3668 +      while (k > w + l)
3669 +      {
3670 +        h++;
3671 +        w += l;                 /* previous table always l bits */
3672 +
3673 +        /* compute minimum size table less than or equal to l bits */
3674 +        z = (z = g - w) > (uInt)l ? l : z;      /* table size upper limit */
3675 +        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
3676 +        {                       /* too few codes for k-w bit table */
3677 +          f -= a + 1;           /* deduct codes from patterns left */
3678 +          xp = c + k;
3679 +          if (j < z)
3680 +            while (++j < z)     /* try smaller tables up to z bits */
3681 +            {
3682 +              if ((f <<= 1) <= *++xp)
3683 +                break;          /* enough codes to use up j bits */
3684 +              f -= *xp;         /* else deduct codes from patterns */
3685 +            }
3686 +        }
3687 +        z = 1 << j;             /* table entries for j-bit table */
3688 +
3689 +        /* allocate and link in new table */
3690 +        if ((q = (inflate_huft *)ZALLOC
3691 +             (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
3692 +        {
3693 +          if (h)
3694 +            inflate_trees_free(u[0], zs);
3695 +          return Z_MEM_ERROR;   /* not enough memory */
3696 +        }
3697 +       q->word.Nalloc = z + 1;
3698 +#ifdef DEBUG_ZLIB
3699 +        inflate_hufts += z + 1;
3700 +#endif
3701 +        *t = q + 1;             /* link to list for huft_free() */
3702 +        *(t = &(q->next)) = Z_NULL;
3703 +        u[h] = ++q;             /* table starts after link */
3704 +
3705 +        /* connect to last table, if there is one */
3706 +        if (h)
3707 +        {
3708 +          x[h] = i;             /* save pattern for backing up */
3709 +          r.bits = (Byte)l;     /* bits to dump before this table */
3710 +          r.exop = (Byte)j;     /* bits in this table */
3711 +          r.next = q;           /* pointer to this table */
3712 +          j = i >> (w - l);     /* (get around Turbo C bug) */
3713 +          u[h-1][j] = r;        /* connect to last table */
3714 +        }
3715 +      }
3716 +
3717 +      /* set up table entry in r */
3718 +      r.bits = (Byte)(k - w);
3719 +      if (p >= v + n)
3720 +        r.exop = 128 + 64;      /* out of values--invalid code */
3721 +      else if (*p < s)
3722 +      {
3723 +        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
3724 +        r.base = *p++;          /* simple code is just the value */
3725 +      }
3726 +      else
3727 +      {
3728 +        r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */
3729 +        r.base = d[*p++ - s];
3730 +      }
3731 +
3732 +      /* fill code-like entries with r */
3733 +      f = 1 << (k - w);
3734 +      for (j = i >> w; j < z; j += f)
3735 +        q[j] = r;
3736 +
3737 +      /* backwards increment the k-bit code i */
3738 +      for (j = 1 << (k - 1); i & j; j >>= 1)
3739 +        i ^= j;
3740 +      i ^= j;
3741 +
3742 +      /* backup over finished tables */
3743 +      while ((i & ((1 << w) - 1)) != x[h])
3744 +      {
3745 +        h--;                    /* don't need to update q */
3746 +        w -= l;
3747 +      }
3748 +    }
3749 +  }
3750 +
3751 +
3752 +  /* Return Z_BUF_ERROR if we were given an incomplete table */
3753 +  return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
3754 +}
3755 +
3756 +
3757 +local int inflate_trees_bits(c, bb, tb, z)
3758 +uIntf *c;               /* 19 code lengths */
3759 +uIntf *bb;              /* bits tree desired/actual depth */
3760 +inflate_huft * FAR *tb; /* bits tree result */
3761 +z_stream *z;            /* for zfree function */
3762 +{
3763 +  int r;
3764 +
3765 +  r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
3766 +  if (r == Z_DATA_ERROR)
3767 +    z->msg = "oversubscribed dynamic bit lengths tree";
3768 +  else if (r == Z_BUF_ERROR)
3769 +  {
3770 +    inflate_trees_free(*tb, z);
3771 +    z->msg = "incomplete dynamic bit lengths tree";
3772 +    r = Z_DATA_ERROR;
3773 +  }
3774 +  return r;
3775 +}
3776 +
3777 +
3778 +local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
3779 +uInt nl;                /* number of literal/length codes */
3780 +uInt nd;                /* number of distance codes */
3781 +uIntf *c;               /* that many (total) code lengths */
3782 +uIntf *bl;              /* literal desired/actual bit depth */
3783 +uIntf *bd;              /* distance desired/actual bit depth */
3784 +inflate_huft * FAR *tl; /* literal/length tree result */
3785 +inflate_huft * FAR *td; /* distance tree result */
3786 +z_stream *z;            /* for zfree function */
3787 +{
3788 +  int r;
3789 +
3790 +  /* build literal/length tree */
3791 +  if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
3792 +  {
3793 +    if (r == Z_DATA_ERROR)
3794 +      z->msg = "oversubscribed literal/length tree";
3795 +    else if (r == Z_BUF_ERROR)
3796 +    {
3797 +      inflate_trees_free(*tl, z);
3798 +      z->msg = "incomplete literal/length tree";
3799 +      r = Z_DATA_ERROR;
3800 +    }
3801 +    return r;
3802 +  }
3803 +
3804 +  /* build distance tree */
3805 +  if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
3806 +  {
3807 +    if (r == Z_DATA_ERROR)
3808 +      z->msg = "oversubscribed literal/length tree";
3809 +    else if (r == Z_BUF_ERROR) {
3810 +#ifdef PKZIP_BUG_WORKAROUND
3811 +      r = Z_OK;
3812 +    }
3813 +#else
3814 +      inflate_trees_free(*td, z);
3815 +      z->msg = "incomplete literal/length tree";
3816 +      r = Z_DATA_ERROR;
3817 +    }
3818 +    inflate_trees_free(*tl, z);
3819 +    return r;
3820 +#endif
3821 +  }
3822 +
3823 +  /* done */
3824 +  return Z_OK;
3825 +}
3826 +
3827 +
3828 +/* build fixed tables only once--keep them here */
3829 +local int fixed_lock = 0;
3830 +local int fixed_built = 0;
3831 +#define FIXEDH 530      /* number of hufts used by fixed tables */
3832 +local uInt fixed_left = FIXEDH;
3833 +local inflate_huft fixed_mem[FIXEDH];
3834 +local uInt fixed_bl;
3835 +local uInt fixed_bd;
3836 +local inflate_huft *fixed_tl;
3837 +local inflate_huft *fixed_td;
3838 +
3839 +
3840 +local voidpf falloc(q, n, s)
3841 +voidpf q;        /* opaque pointer (not used) */
3842 +uInt n;         /* number of items */
3843 +uInt s;         /* size of item */
3844 +{
3845 +  Assert(s == sizeof(inflate_huft) && n <= fixed_left,
3846 +         "inflate_trees falloc overflow");
3847 +  if (q) s++; /* to make some compilers happy */
3848 +  fixed_left -= n;
3849 +  return (voidpf)(fixed_mem + fixed_left);
3850 +}
3851 +
3852 +
3853 +local void ffree(q, p, n)
3854 +voidpf q;
3855 +voidpf p;
3856 +uInt n;
3857 +{
3858 +  Assert(0, "inflate_trees ffree called!");
3859 +  if (q) q = p; /* to make some compilers happy */
3860 +}
3861 +
3862 +
3863 +local int inflate_trees_fixed(bl, bd, tl, td)
3864 +uIntf *bl;               /* literal desired/actual bit depth */
3865 +uIntf *bd;               /* distance desired/actual bit depth */
3866 +inflate_huft * FAR *tl;  /* literal/length tree result */
3867 +inflate_huft * FAR *td;  /* distance tree result */
3868 +{
3869 +  /* build fixed tables if not built already--lock out other instances */
3870 +  while (++fixed_lock > 1)
3871 +    fixed_lock--;
3872 +  if (!fixed_built)
3873 +  {
3874 +    int k;              /* temporary variable */
3875 +    unsigned c[288];    /* length list for huft_build */
3876 +    z_stream z;         /* for falloc function */
3877 +
3878 +    /* set up fake z_stream for memory routines */
3879 +    z.zalloc = falloc;
3880 +    z.zfree = ffree;
3881 +    z.opaque = Z_NULL;
3882 +
3883 +    /* literal table */
3884 +    for (k = 0; k < 144; k++)
3885 +      c[k] = 8;
3886 +    for (; k < 256; k++)
3887 +      c[k] = 9;
3888 +    for (; k < 280; k++)
3889 +      c[k] = 7;
3890 +    for (; k < 288; k++)
3891 +      c[k] = 8;
3892 +    fixed_bl = 7;
3893 +    huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
3894 +
3895 +    /* distance table */
3896 +    for (k = 0; k < 30; k++)
3897 +      c[k] = 5;
3898 +    fixed_bd = 5;
3899 +    huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
3900 +
3901 +    /* done */
3902 +    fixed_built = 1;
3903 +  }
3904 +  fixed_lock--;
3905 +  *bl = fixed_bl;
3906 +  *bd = fixed_bd;
3907 +  *tl = fixed_tl;
3908 +  *td = fixed_td;
3909 +  return Z_OK;
3910 +}
3911 +
3912 +
3913 +local int inflate_trees_free(t, z)
3914 +inflate_huft *t;        /* table to free */
3915 +z_stream *z;            /* for zfree function */
3916 +/* Free the malloc'ed tables built by huft_build(), which makes a linked
3917 +   list of the tables it made, with the links in a dummy first entry of
3918 +   each table. */
3919 +{
3920 +  register inflate_huft *p, *q;
3921 +
3922 +  /* Go through linked list, freeing from the malloced (t[-1]) address. */
3923 +  p = t;
3924 +  while (p != Z_NULL)
3925 +  {
3926 +    q = (--p)->next;
3927 +    ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft));
3928 +    p = q;
3929 +  } 
3930 +  return Z_OK;
3931 +}
3932 +
3933 +/*+++++*/
3934 +/* infcodes.c -- process literals and length/distance pairs
3935 + * Copyright (C) 1995 Mark Adler
3936 + * For conditions of distribution and use, see copyright notice in zlib.h 
3937 + */
3938 +
3939 +/* simplify the use of the inflate_huft type with some defines */
3940 +#define base more.Base
3941 +#define next more.Next
3942 +#define exop word.what.Exop
3943 +#define bits word.what.Bits
3944 +
3945 +/* inflate codes private state */
3946 +struct inflate_codes_state {
3947 +
3948 +  /* mode */
3949 +  enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
3950 +      START,    /* x: set up for LEN */
3951 +      LEN,      /* i: get length/literal/eob next */
3952 +      LENEXT,   /* i: getting length extra (have base) */
3953 +      DIST,     /* i: get distance next */
3954 +      DISTEXT,  /* i: getting distance extra */
3955 +      COPY,     /* o: copying bytes in window, waiting for space */
3956 +      LIT,      /* o: got literal, waiting for output space */
3957 +      WASH,     /* o: got eob, possibly still output waiting */
3958 +      END,      /* x: got eob and all data flushed */
3959 +      BADCODE}  /* x: got error */
3960 +    mode;               /* current inflate_codes mode */
3961 +
3962 +  /* mode dependent information */
3963 +  uInt len;
3964 +  union {
3965 +    struct {
3966 +      inflate_huft *tree;       /* pointer into tree */
3967 +      uInt need;                /* bits needed */
3968 +    } code;             /* if LEN or DIST, where in tree */
3969 +    uInt lit;           /* if LIT, literal */
3970 +    struct {
3971 +      uInt get;                 /* bits to get for extra */
3972 +      uInt dist;                /* distance back to copy from */
3973 +    } copy;             /* if EXT or COPY, where and how much */
3974 +  } sub;                /* submode */
3975 +
3976 +  /* mode independent information */
3977 +  Byte lbits;           /* ltree bits decoded per branch */
3978 +  Byte dbits;           /* dtree bits decoder per branch */
3979 +  inflate_huft *ltree;          /* literal/length/eob tree */
3980 +  inflate_huft *dtree;          /* distance tree */
3981 +
3982 +};
3983 +
3984 +
3985 +local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
3986 +uInt bl, bd;
3987 +inflate_huft *tl, *td;
3988 +z_stream *z;
3989 +{
3990 +  inflate_codes_statef *c;
3991 +
3992 +  if ((c = (inflate_codes_statef *)
3993 +       ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
3994 +  {
3995 +    c->mode = START;
3996 +    c->lbits = (Byte)bl;
3997 +    c->dbits = (Byte)bd;
3998 +    c->ltree = tl;
3999 +    c->dtree = td;
4000 +    Tracev((stderr, "inflate:       codes new\n"));
4001 +  }
4002 +  return c;
4003 +}
4004 +
4005 +
4006 +local int inflate_codes(s, z, r)
4007 +inflate_blocks_statef *s;
4008 +z_stream *z;
4009 +int r;
4010 +{
4011 +  uInt j;               /* temporary storage */
4012 +  inflate_huft *t;      /* temporary pointer */
4013 +  uInt e;               /* extra bits or operation */
4014 +  uLong b;              /* bit buffer */
4015 +  uInt k;               /* bits in bit buffer */
4016 +  Bytef *p;             /* input data pointer */
4017 +  uInt n;               /* bytes available there */
4018 +  Bytef *q;             /* output window write pointer */
4019 +  uInt m;               /* bytes to end of window or read pointer */
4020 +  Bytef *f;             /* pointer to copy strings from */
4021 +  inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
4022 +
4023 +  /* copy input/output information to locals (UPDATE macro restores) */
4024 +  LOAD
4025 +
4026 +  /* process input and output based on current state */
4027 +  while (1) switch (c->mode)
4028 +  {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
4029 +    case START:         /* x: set up for LEN */
4030 +#ifndef SLOW
4031 +      if (m >= 258 && n >= 10)
4032 +      {
4033 +        UPDATE
4034 +        r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
4035 +        LOAD
4036 +        if (r != Z_OK)
4037 +        {
4038 +          c->mode = r == Z_STREAM_END ? WASH : BADCODE;
4039 +          break;
4040 +        }
4041 +      }
4042 +#endif /* !SLOW */
4043 +      c->sub.code.need = c->lbits;
4044 +      c->sub.code.tree = c->ltree;
4045 +      c->mode = LEN;
4046 +    case LEN:           /* i: get length/literal/eob next */
4047 +      j = c->sub.code.need;
4048 +      NEEDBITS(j)
4049 +      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
4050 +      DUMPBITS(t->bits)
4051 +      e = (uInt)(t->exop);
4052 +      if (e == 0)               /* literal */
4053 +      {
4054 +        c->sub.lit = t->base;
4055 +        Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
4056 +                 "inflate:         literal '%c'\n" :
4057 +                 "inflate:         literal 0x%02x\n", t->base));
4058 +        c->mode = LIT;
4059 +        break;
4060 +      }
4061 +      if (e & 16)               /* length */
4062 +      {
4063 +        c->sub.copy.get = e & 15;
4064 +        c->len = t->base;
4065 +        c->mode = LENEXT;
4066 +        break;
4067 +      }
4068 +      if ((e & 64) == 0)        /* next table */
4069 +      {
4070 +        c->sub.code.need = e;
4071 +        c->sub.code.tree = t->next;
4072 +        break;
4073 +      }
4074 +      if (e & 32)               /* end of block */
4075 +      {
4076 +        Tracevv((stderr, "inflate:         end of block\n"));
4077 +        c->mode = WASH;
4078 +        break;
4079 +      }
4080 +      c->mode = BADCODE;        /* invalid code */
4081 +      z->msg = "invalid literal/length code";
4082 +      r = Z_DATA_ERROR;
4083 +      LEAVE
4084 +    case LENEXT:        /* i: getting length extra (have base) */
4085 +      j = c->sub.copy.get;
4086 +      NEEDBITS(j)
4087 +      c->len += (uInt)b & inflate_mask[j];
4088 +      DUMPBITS(j)
4089 +      c->sub.code.need = c->dbits;
4090 +      c->sub.code.tree = c->dtree;
4091 +      Tracevv((stderr, "inflate:         length %u\n", c->len));
4092 +      c->mode = DIST;
4093 +    case DIST:          /* i: get distance next */
4094 +      j = c->sub.code.need;
4095 +      NEEDBITS(j)
4096 +      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
4097 +      DUMPBITS(t->bits)
4098 +      e = (uInt)(t->exop);
4099 +      if (e & 16)               /* distance */
4100 +      {
4101 +        c->sub.copy.get = e & 15;
4102 +        c->sub.copy.dist = t->base;
4103 +        c->mode = DISTEXT;
4104 +        break;
4105 +      }
4106 +      if ((e & 64) == 0)        /* next table */
4107 +      {
4108 +        c->sub.code.need = e;
4109 +        c->sub.code.tree = t->next;
4110 +        break;
4111 +      }
4112 +      c->mode = BADCODE;        /* invalid code */
4113 +      z->msg = "invalid distance code";
4114 +      r = Z_DATA_ERROR;
4115 +      LEAVE
4116 +    case DISTEXT:       /* i: getting distance extra */
4117 +      j = c->sub.copy.get;
4118 +      NEEDBITS(j)
4119 +      c->sub.copy.dist += (uInt)b & inflate_mask[j];
4120 +      DUMPBITS(j)
4121 +      Tracevv((stderr, "inflate:         distance %u\n", c->sub.copy.dist));
4122 +      c->mode = COPY;
4123 +    case COPY:          /* o: copying bytes in window, waiting for space */
4124 +#ifndef __TURBOC__ /* Turbo C bug for following expression */
4125 +      f = (uInt)(q - s->window) < c->sub.copy.dist ?
4126 +          s->end - (c->sub.copy.dist - (q - s->window)) :
4127 +          q - c->sub.copy.dist;
4128 +#else
4129 +      f = q - c->sub.copy.dist;
4130 +      if ((uInt)(q - s->window) < c->sub.copy.dist)
4131 +        f = s->end - (c->sub.copy.dist - (q - s->window));
4132 +#endif
4133 +      while (c->len)
4134 +      {
4135 +        NEEDOUT
4136 +        OUTBYTE(*f++)
4137 +        if (f == s->end)
4138 +          f = s->window;
4139 +        c->len--;
4140 +      }
4141 +      c->mode = START;
4142 +      break;
4143 +    case LIT:           /* o: got literal, waiting for output space */
4144 +      NEEDOUT
4145 +      OUTBYTE(c->sub.lit)
4146 +      c->mode = START;
4147 +      break;
4148 +    case WASH:          /* o: got eob, possibly more output */
4149 +      FLUSH
4150 +      if (s->read != s->write)
4151 +        LEAVE
4152 +      c->mode = END;
4153 +    case END:
4154 +      r = Z_STREAM_END;
4155 +      LEAVE
4156 +    case BADCODE:       /* x: got error */
4157 +      r = Z_DATA_ERROR;
4158 +      LEAVE
4159 +    default:
4160 +      r = Z_STREAM_ERROR;
4161 +      LEAVE
4162 +  }
4163 +}
4164 +
4165 +
4166 +local void inflate_codes_free(c, z)
4167 +inflate_codes_statef *c;
4168 +z_stream *z;
4169 +{
4170 +  ZFREE(z, c, sizeof(struct inflate_codes_state));
4171 +  Tracev((stderr, "inflate:       codes free\n"));
4172 +}
4173 +
4174 +/*+++++*/
4175 +/* inflate_util.c -- data and routines common to blocks and codes
4176 + * Copyright (C) 1995 Mark Adler
4177 + * For conditions of distribution and use, see copyright notice in zlib.h 
4178 + */
4179 +
4180 +/* copy as much as possible from the sliding window to the output area */
4181 +local int inflate_flush(s, z, r)
4182 +inflate_blocks_statef *s;
4183 +z_stream *z;
4184 +int r;
4185 +{
4186 +  uInt n;
4187 +  Bytef *p, *q;
4188 +
4189 +  /* local copies of source and destination pointers */
4190 +  p = z->next_out;
4191 +  q = s->read;
4192 +
4193 +  /* compute number of bytes to copy as far as end of window */
4194 +  n = (uInt)((q <= s->write ? s->write : s->end) - q);
4195 +  if (n > z->avail_out) n = z->avail_out;
4196 +  if (n && r == Z_BUF_ERROR) r = Z_OK;
4197 +
4198 +  /* update counters */
4199 +  z->avail_out -= n;
4200 +  z->total_out += n;
4201 +
4202 +  /* update check information */
4203 +  if (s->checkfn != Z_NULL)
4204 +    s->check = (*s->checkfn)(s->check, q, n);
4205 +
4206 +  /* copy as far as end of window */
4207 +  zmemcpy(p, q, n);
4208 +  p += n;
4209 +  q += n;
4210 +
4211 +  /* see if more to copy at beginning of window */
4212 +  if (q == s->end)
4213 +  {
4214 +    /* wrap pointers */
4215 +    q = s->window;
4216 +    if (s->write == s->end)
4217 +      s->write = s->window;
4218 +
4219 +    /* compute bytes to copy */
4220 +    n = (uInt)(s->write - q);
4221 +    if (n > z->avail_out) n = z->avail_out;
4222 +    if (n && r == Z_BUF_ERROR) r = Z_OK;
4223 +
4224 +    /* update counters */
4225 +    z->avail_out -= n;
4226 +    z->total_out += n;
4227 +
4228 +    /* update check information */
4229 +    if (s->checkfn != Z_NULL)
4230 +      s->check = (*s->checkfn)(s->check, q, n);
4231 +
4232 +    /* copy */
4233 +    zmemcpy(p, q, n);
4234 +    p += n;
4235 +    q += n;
4236 +  }
4237 +
4238 +  /* update pointers */
4239 +  z->next_out = p;
4240 +  s->read = q;
4241 +
4242 +  /* done */
4243 +  return r;
4244 +}
4245 +
4246 +
4247 +/*+++++*/
4248 +/* inffast.c -- process literals and length/distance pairs fast
4249 + * Copyright (C) 1995 Mark Adler
4250 + * For conditions of distribution and use, see copyright notice in zlib.h 
4251 + */
4252 +
4253 +/* simplify the use of the inflate_huft type with some defines */
4254 +#define base more.Base
4255 +#define next more.Next
4256 +#define exop word.what.Exop
4257 +#define bits word.what.Bits
4258 +
4259 +/* macros for bit input with no checking and for returning unused bytes */
4260 +#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
4261 +#define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
4262 +
4263 +/* Called with number of bytes left to write in window at least 258
4264 +   (the maximum string length) and number of input bytes available
4265 +   at least ten.  The ten bytes are six bytes for the longest length/
4266 +   distance pair plus four bytes for overloading the bit buffer. */
4267 +
4268 +local int inflate_fast(bl, bd, tl, td, s, z)
4269 +uInt bl, bd;
4270 +inflate_huft *tl, *td;
4271 +inflate_blocks_statef *s;
4272 +z_stream *z;
4273 +{
4274 +  inflate_huft *t;      /* temporary pointer */
4275 +  uInt e;               /* extra bits or operation */
4276 +  uLong b;              /* bit buffer */
4277 +  uInt k;               /* bits in bit buffer */
4278 +  Bytef *p;             /* input data pointer */
4279 +  uInt n;               /* bytes available there */
4280 +  Bytef *q;             /* output window write pointer */
4281 +  uInt m;               /* bytes to end of window or read pointer */
4282 +  uInt ml;              /* mask for literal/length tree */
4283 +  uInt md;              /* mask for distance tree */
4284 +  uInt c;               /* bytes to copy */
4285 +  uInt d;               /* distance back to copy from */
4286 +  Bytef *r;             /* copy source pointer */
4287 +
4288 +  /* load input, output, bit values */
4289 +  LOAD
4290 +
4291 +  /* initialize masks */
4292 +  ml = inflate_mask[bl];
4293 +  md = inflate_mask[bd];
4294 +
4295 +  /* do until not enough input or output space for fast loop */
4296 +  do {                          /* assume called with m >= 258 && n >= 10 */
4297 +    /* get literal/length code */
4298 +    GRABBITS(20)                /* max bits for literal/length code */
4299 +    if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
4300 +    {
4301 +      DUMPBITS(t->bits)
4302 +      Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
4303 +                "inflate:         * literal '%c'\n" :
4304 +                "inflate:         * literal 0x%02x\n", t->base));
4305 +      *q++ = (Byte)t->base;
4306 +      m--;
4307 +      continue;
4308 +    }
4309 +    do {
4310 +      DUMPBITS(t->bits)
4311 +      if (e & 16)
4312 +      {
4313 +        /* get extra bits for length */
4314 +        e &= 15;
4315 +        c = t->base + ((uInt)b & inflate_mask[e]);
4316 +        DUMPBITS(e)
4317 +        Tracevv((stderr, "inflate:         * length %u\n", c));
4318 +
4319 +        /* decode distance base of block to copy */
4320 +        GRABBITS(15);           /* max bits for distance code */
4321 +        e = (t = td + ((uInt)b & md))->exop;
4322 +        do {
4323 +          DUMPBITS(t->bits)
4324 +          if (e & 16)
4325 +          {
4326 +            /* get extra bits to add to distance base */
4327 +            e &= 15;
4328 +            GRABBITS(e)         /* get extra bits (up to 13) */
4329 +            d = t->base + ((uInt)b & inflate_mask[e]);
4330 +            DUMPBITS(e)
4331 +            Tracevv((stderr, "inflate:         * distance %u\n", d));
4332 +
4333 +            /* do the copy */
4334 +            m -= c;
4335 +            if ((uInt)(q - s->window) >= d)     /* offset before dest */
4336 +            {                                   /*  just copy */
4337 +              r = q - d;
4338 +              *q++ = *r++;  c--;        /* minimum count is three, */
4339 +              *q++ = *r++;  c--;        /*  so unroll loop a little */
4340 +            }
4341 +            else                        /* else offset after destination */
4342 +            {
4343 +              e = d - (q - s->window);  /* bytes from offset to end */
4344 +              r = s->end - e;           /* pointer to offset */
4345 +              if (c > e)                /* if source crosses, */
4346 +              {
4347 +                c -= e;                 /* copy to end of window */
4348 +                do {
4349 +                  *q++ = *r++;
4350 +                } while (--e);
4351 +                r = s->window;          /* copy rest from start of window */
4352 +              }
4353 +            }
4354 +            do {                        /* copy all or what's left */
4355 +              *q++ = *r++;
4356 +            } while (--c);
4357 +            break;
4358 +          }
4359 +          else if ((e & 64) == 0)
4360 +            e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
4361 +          else
4362 +          {
4363 +            z->msg = "invalid distance code";
4364 +            UNGRAB
4365 +            UPDATE
4366 +            return Z_DATA_ERROR;
4367 +          }
4368 +        } while (1);
4369 +        break;
4370 +      }
4371 +      if ((e & 64) == 0)
4372 +      {
4373 +        if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
4374 +        {
4375 +          DUMPBITS(t->bits)
4376 +          Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
4377 +                    "inflate:         * literal '%c'\n" :
4378 +                    "inflate:         * literal 0x%02x\n", t->base));
4379 +          *q++ = (Byte)t->base;
4380 +          m--;
4381 +          break;
4382 +        }
4383 +      }
4384 +      else if (e & 32)
4385 +      {
4386 +        Tracevv((stderr, "inflate:         * end of block\n"));
4387 +        UNGRAB
4388 +        UPDATE
4389 +        return Z_STREAM_END;
4390 +      }
4391 +      else
4392 +      {
4393 +        z->msg = "invalid literal/length code";
4394 +        UNGRAB
4395 +        UPDATE
4396 +        return Z_DATA_ERROR;
4397 +      }
4398 +    } while (1);
4399 +  } while (m >= 258 && n >= 10);
4400 +
4401 +  /* not enough input or output--restore pointers and return */
4402 +  UNGRAB
4403 +  UPDATE
4404 +  return Z_OK;
4405 +}
4406 +
4407 +
4408 +/*+++++*/
4409 +/* zutil.c -- target dependent utility functions for the compression library
4410 + * Copyright (C) 1995 Jean-loup Gailly.
4411 + * For conditions of distribution and use, see copyright notice in zlib.h 
4412 + */
4413 +
4414 +/* From: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp */
4415 +
4416 +char *zlib_version = ZLIB_VERSION;
4417 +
4418 +char *z_errmsg[] = {
4419 +"stream end",          /* Z_STREAM_END    1 */
4420 +"",                    /* Z_OK            0 */
4421 +"file error",          /* Z_ERRNO        (-1) */
4422 +"stream error",        /* Z_STREAM_ERROR (-2) */
4423 +"data error",          /* Z_DATA_ERROR   (-3) */
4424 +"insufficient memory", /* Z_MEM_ERROR    (-4) */
4425 +"buffer error",        /* Z_BUF_ERROR    (-5) */
4426 +""};
4427 +
4428 +
4429 +/*+++++*/
4430 +/* adler32.c -- compute the Adler-32 checksum of a data stream
4431 + * Copyright (C) 1995 Mark Adler
4432 + * For conditions of distribution and use, see copyright notice in zlib.h 
4433 + */
4434 +
4435 +/* From: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp */
4436 +
4437 +#define BASE 65521L /* largest prime smaller than 65536 */
4438 +#define NMAX 5552
4439 +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
4440 +
4441 +#define DO1(buf)  {s1 += *buf++; s2 += s1;}
4442 +#define DO2(buf)  DO1(buf); DO1(buf);
4443 +#define DO4(buf)  DO2(buf); DO2(buf);
4444 +#define DO8(buf)  DO4(buf); DO4(buf);
4445 +#define DO16(buf) DO8(buf); DO8(buf);
4446 +
4447 +/* ========================================================================= */
4448 +uLong adler32(adler, buf, len)
4449 +    uLong adler;
4450 +    Bytef *buf;
4451 +    uInt len;
4452 +{
4453 +    unsigned long s1 = adler & 0xffff;
4454 +    unsigned long s2 = (adler >> 16) & 0xffff;
4455 +    int k;
4456 +
4457 +    if (buf == Z_NULL) return 1L;
4458 +
4459 +    while (len > 0) {
4460 +        k = len < NMAX ? len : NMAX;
4461 +        len -= k;
4462 +        while (k >= 16) {
4463 +            DO16(buf);
4464 +            k -= 16;
4465 +        }
4466 +        if (k != 0) do {
4467 +            DO1(buf);
4468 +        } while (--k);
4469 +        s1 %= BASE;
4470 +        s2 %= BASE;
4471 +    }
4472 +    return (s2 << 16) | s1;
4473 +}
4474 diff -Naru linux/arch/mips/zboot/Makefile linux-new/arch/mips/zboot/Makefile
4475 --- linux/arch/mips/zboot/Makefile      1969-12-31 19:00:00.000000000 -0500
4476 +++ linux-new/arch/mips/zboot/Makefile  2003-12-18 14:26:21.000000000 -0500
4477 @@ -0,0 +1,79 @@
4478 +#
4479 +# arch/mips/zboot/Makefile
4480 +#
4481 +# This file is subject to the terms and conditions of the GNU General Public
4482 +# License.  See the file "COPYING" in the main directory of this archive
4483 +# for more details.
4484 +
4485 +# Adapted for MIPS Pete Popov, Dan Malek
4486 +#
4487 +# Copyright (C) 1994 by Linus Torvalds
4488 +# Adapted for PowerPC by Gary Thomas
4489 +# modified by Cort (cort@cs.nmt.edu)
4490 +#
4491 +
4492 +.c.s:
4493 +       $(CC) $(CFLAGS) -S -o $*.s $<
4494 +.s.o:
4495 +       $(AS) -o $*.o $<
4496 +.c.o:
4497 +       $(CC) $(CFLAGS) -c -o $*.o $<
4498 +.S.s:
4499 +       $(CPP) $(AFLAGS) -o $*.o $<
4500 +.S.o:
4501 +       $(CC) $(AFLAGS) -c -o $*.o $<
4502 +
4503 +GZIP_FLAGS = -v9f
4504 +
4505 +CFLAGS := $(CPPFLAGS) -O2 -D__BOOTER__ \
4506 +       -fomit-frame-pointer -fno-strict-aliasing -fno-common \
4507 +       -G 0 -mno-abicalls -fno-pic -mcpu=r4600 -mips2 \
4508 +               -I$(TOPDIR)/arch/$(ARCH)/zboot/include \
4509 +               -I$(TOPDIR)/include/asm
4510 +AFLAGS += -D__BOOTER__
4511 +
4512 +BOOT_TARGETS = zImage zImage.initrd zImage.flash zImage.initrd.flash
4513 +
4514 +lib/zlib.a:
4515 +       $(MAKE) -C lib
4516 +
4517 +images/vmlinux.gz: $(TOPDIR)/vmlinux
4518 +       $(MAKE) -C images vmlinux.gz
4519 +
4520 +$(BOOT_TARGETS): lib/zlib.a images/vmlinux.gz
4521 +ifdef CONFIG_MIPS_PB1000
4522 +       $(MAKE) -C pb1xxx $@
4523 +endif
4524 +ifdef CONFIG_MIPS_PB1500
4525 +       $(MAKE) -C pb1xxx $@
4526 +endif
4527 +ifdef CONFIG_MIPS_PB1100
4528 +       $(MAKE) -C pb1xxx $@
4529 +endif
4530 +ifdef CONFIG_MIPS_DB1000
4531 +       $(MAKE) -C pb1xxx $@
4532 +endif
4533 +ifdef CONFIG_MIPS_DB1100
4534 +       $(MAKE) -C pb1xxx $@
4535 +endif
4536 +ifdef CONFIG_MIPS_DB1500
4537 +       $(MAKE) -C pb1xxx $@
4538 +endif
4539 +ifdef CONFIG_MIPS_BOSPORUS
4540 +       $(MAKE) -C pb1xxx $@
4541 +endif
4542 +ifdef CONFIG_COGENT_CSB250
4543 +       $(MAKE) -C csb250 $@
4544 +endif
4545 +ifdef CONFIG_MIPS_XXS1500
4546 +BOOT_DIR = xxs1500
4547 +endif
4548 +
4549 +# Do the dirs
4550 +clean:
4551 +       $(MAKE) -C common clean
4552 +       $(MAKE) -C images clean
4553 +       $(MAKE) -C pb1xxx clean
4554 +       $(MAKE) -C xxs1500 clean
4555 +
4556 +include $(TOPDIR)/Rules.make
4557 diff -Naru linux/arch/mips/zboot/pb1xxx/head.S linux-new/arch/mips/zboot/pb1xxx/head.S
4558 --- linux/arch/mips/zboot/pb1xxx/head.S 1969-12-31 19:00:00.000000000 -0500
4559 +++ linux-new/arch/mips/zboot/pb1xxx/head.S     2003-12-18 14:26:21.000000000 -0500
4560 @@ -0,0 +1,149 @@
4561 +/*
4562 + * arch/mips/kernel/head.S
4563 + *
4564 + * This file is subject to the terms and conditions of the GNU General Public
4565 + * License.  See the file "COPYING" in the main directory of this archive
4566 + * for more details.
4567 + *
4568 + * Copyright (C) 1994, 1995 Waldorf Electronics
4569 + * Written by Ralf Baechle and Andreas Busse
4570 + * Copyright (C) 1995 - 1999 Ralf Baechle
4571 + * Copyright (C) 1996 Paul M. Antoine
4572 + * Modified for DECStation and hence R3000 support by Paul M. Antoine
4573 + * Further modifications by David S. Miller and Harald Koerfgen
4574 + * Copyright (C) 1999 Silicon Graphics, Inc.
4575 + *
4576 + * Head.S contains the MIPS exception handler and startup code.
4577 + *
4578 + **************************************************************************
4579 + *  9 Nov, 2000.
4580 + *  Added Cache Error exception handler and SBDDP EJTAG debug exception.
4581 + *
4582 + *  Kevin Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
4583 + *  Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
4584 + **************************************************************************
4585 + */
4586 +#include <linux/config.h>
4587 +#include <linux/threads.h>
4588 +
4589 +#include <asm/asm.h>
4590 +#include <asm/cacheops.h>
4591 +#include <asm/mipsregs.h>
4592 +#include <asm/offset.h>
4593 +#include <asm/cachectl.h>
4594 +#include <asm/regdef.h>
4595 +
4596 +#define IndexInvalidate_I       0x00
4597 +#define IndexWriteBack_D        0x01
4598 +
4599 +       .set noreorder
4600 +       .cprestore
4601 +       LEAF(start)
4602 +start:
4603 +       bal     locate
4604 +       nop
4605 +locate:
4606 +       subu    s8, ra, 8       /* Where we were loaded */
4607 +       la      sp, (.stack + 8192)
4608 +
4609 +       move    s0, a0          /* Save boot rom start args */
4610 +       move    s1, a1
4611 +       move    s2, a2
4612 +       move    s3, a3
4613 +
4614 +       la      a0, start       /* Where we were linked to run */
4615 +
4616 +       move    a1, s8
4617 +       la      a2, _edata
4618 +       subu    t1, a2, a0
4619 +       srl     t1, t1, 2
4620 +
4621 +       /* copy text section */
4622 +       li      t0, 0
4623 +1:     lw      v0, 0(a1)
4624 +       nop
4625 +       sw      v0, 0(a0)
4626 +       xor     t0, t0, v0
4627 +       addu    a0, 4
4628 +       bne     a2, a0, 1b
4629 +       addu    a1, 4
4630 +
4631 +       /* Clear BSS */
4632 +       la      a0, _edata
4633 +       la      a2, _end
4634 +2:     sw      zero, 0(a0)
4635 +       bne     a2, a0, 2b
4636 +       addu    a0, 4
4637 +
4638 +       /* push the D-Cache and invalidate I-Cache */
4639 +       li      k0, 0x80000000  # start address
4640 +       li      k1, 0x80004000  # end address (16KB I-Cache)
4641 +       subu    k1, 128
4642 +
4643 +1:
4644 +       .set mips3
4645 +       cache   IndexWriteBack_D, 0(k0)
4646 +       cache   IndexWriteBack_D, 32(k0)
4647 +       cache   IndexWriteBack_D, 64(k0)
4648 +       cache   IndexWriteBack_D, 96(k0)
4649 +       cache   IndexInvalidate_I, 0(k0)
4650 +       cache   IndexInvalidate_I, 32(k0)
4651 +       cache   IndexInvalidate_I, 64(k0)
4652 +       cache   IndexInvalidate_I, 96(k0)
4653 +       .set mips0
4654 +
4655 +       bne     k0, k1, 1b
4656 +       addu    k0, k0, 128
4657 +       /* done */
4658 +
4659 +       move    a0, s8               /* load address */
4660 +       move    a1, t1               /* length in words */
4661 +       move    a2, t0               /* checksum */
4662 +       move    a3, sp
4663 +
4664 +       la      ra, 1f
4665 +       la      k0, decompress_kernel
4666 +       jr      k0
4667 +       nop
4668 +1:
4669 +
4670 +       move    a0, s0
4671 +       move    a1, s1
4672 +       move    a2, s2
4673 +       move    a3, s3
4674 +       li      k0, KERNEL_ENTRY
4675 +       jr      k0
4676 +       nop
4677 +3:
4678 +       b 3b
4679 +       END(start)
4680 +
4681 +       LEAF(udelay)
4682 +udelay:
4683 +       END(udelay)
4684 +
4685 +
4686 +       LEAF(FlushCache)
4687 +       li      k0, 0x80000000  # start address
4688 +       li      k1, 0x80004000  # end address (16KB I-Cache)
4689 +       subu    k1, 128
4690 +
4691 +1:
4692 +       .set mips3
4693 +       cache   IndexWriteBack_D, 0(k0)
4694 +       cache   IndexWriteBack_D, 32(k0)
4695 +       cache   IndexWriteBack_D, 64(k0)
4696 +       cache   IndexWriteBack_D, 96(k0)
4697 +       cache   IndexInvalidate_I, 0(k0)
4698 +       cache   IndexInvalidate_I, 32(k0)
4699 +       cache   IndexInvalidate_I, 64(k0)
4700 +       cache   IndexInvalidate_I, 96(k0)
4701 +       .set mips0
4702 +
4703 +       bne     k0, k1, 1b
4704 +       addu    k0, k0, 128
4705 +       jr      ra
4706 +       nop
4707 +       END(FlushCache)
4708 +
4709 +       .comm .stack,4096*2,4
4710 diff -Naru linux/arch/mips/zboot/pb1xxx/Makefile linux-new/arch/mips/zboot/pb1xxx/Makefile
4711 --- linux/arch/mips/zboot/pb1xxx/Makefile       1969-12-31 19:00:00.000000000 -0500
4712 +++ linux-new/arch/mips/zboot/pb1xxx/Makefile   2003-12-18 14:26:21.000000000 -0500
4713 @@ -0,0 +1,123 @@
4714 +# arch/mips/zboot/pb1xxx/Makefile
4715 +# 
4716 +# Makefile for Alchemy Semiconductor Pb1[015]00 boards.
4717 +# All of the boot loader code was derived from the ppc
4718 +# boot code.
4719 +#
4720 +# Copyright 2001,2002 MontaVista Software Inc.
4721 +#
4722 +# Author: Mark A. Greer
4723 +#        mgreer@mvista.com
4724 +# Ported and modified for mips support by 
4725 +# Pete Popov <ppopov@mvista.com>
4726 +#
4727 +# This program is free software; you can redistribute  it and/or modify it
4728 +# under  the terms of  the GNU General  Public License as published by the
4729 +# Free Software Foundation;  either version 2 of the  License, or (at your
4730 +# option) any later version.
4731 +
4732 +.c.s:
4733 +       $(CC) $(CFLAGS) -S -o $*.s $<
4734 +.s.o:
4735 +       $(AS) -o $*.o $<
4736 +.c.o:
4737 +       $(CC) $(CFLAGS) -D__BOOTER__ -c -o $*.o $<
4738 +.S.s:
4739 +       $(CPP) $(AFLAGS) -o $*.o $<
4740 +.S.o:
4741 +       $(CC) $(AFLAGS) -c -o $*.o $<
4742 +
4743 +#########################################################################
4744 +# START BOARD SPECIFIC VARIABLES
4745 +ifdef CONFIG_MIPS_PB1000
4746 +BNAME=pb1000
4747 +endif
4748 +
4749 +ifdef CONFIG_MIPS_PB1100
4750 +BNAME=pb1100
4751 +endif
4752 +
4753 +ifdef CONFIG_MIPS_PB1500
4754 +BNAME=pb1500
4755 +endif
4756 +
4757 +ifdef CONFIG_MIPS_DB1000
4758 +BNAME=db1000
4759 +endif
4760 +
4761 +ifdef CONFIG_MIPS_DB1100
4762 +BNAME=db1100
4763 +endif
4764 +
4765 +ifdef CONFIG_MIPS_DB1500
4766 +BNAME=db1500
4767 +endif
4768 +
4769 +ifdef CONFIG_MIPS_BOSPORUS
4770 +BNAME=bosporus
4771 +endif
4772 +
4773 +# These two variables control where the zImage is stored
4774 +# in flash and loaded in memory.  It only controls how the srec
4775 +# file is generated, the code is the same.
4776 +RAM_RUN_ADDR = 0x81000000
4777 +FLASH_LOAD_ADDR = 0xBFD00000
4778 +
4779 +# These two variables specify the free ram region
4780 +# that can be used for temporary malloc area
4781 +AVAIL_RAM_START=0x80400000
4782 +AVAIL_RAM_END=0x80800000
4783 +
4784 +# This one must match the LOADADDR in arch/mips/Makefile!
4785 +LOADADDR=0x80100000
4786 +# END BOARD SPECIFIC VARIABLES
4787 +#########################################################################
4788 +
4789 +OBJECTS := head.o ../common/misc-common.o ../common/misc-simple.o \
4790 +       ../common/au1k_uart.o ../common/string.o ../common/ctype.o
4791 +LIBS := ../lib/zlib.a
4792 +
4793 +ENTRY := ../utils/entry
4794 +OFFSET := ../utils/offset
4795 +SIZE := ../utils/size
4796 +
4797 +LD_ARGS := -T ../ld.script -Ttext $(RAM_RUN_ADDR) -Bstatic
4798 +OBJCOPY_ARGS = -O elf32-tradlittlemips
4799 +
4800 +all: zImage
4801 +
4802 +clean:
4803 +       rm -rf *.o vmlinux* zvmlinux.* ../images/*.srec
4804 +
4805 +head.o: head.S $(TOPDIR)/vmlinux
4806 +       $(CC) $(AFLAGS) \
4807 +       -DKERNEL_ENTRY=$(shell sh $(ENTRY) $(NM) $(TOPDIR)/vmlinux ) \
4808 +       -c -o $*.o $<
4809 +
4810 +../common/misc-simple.o:
4811 +       $(CC) $(CFLAGS) -DINITRD_OFFSET=0 -DINITRD_SIZE=0 -DZIMAGE_OFFSET=0 \
4812 +               -DAVAIL_RAM_START=$(AVAIL_RAM_START) \
4813 +               -DAVAIL_RAM_END=$(AVAIL_RAM_END) \
4814 +               -DLOADADDR=$(LOADADDR) \
4815 +               -DZIMAGE_SIZE=0 -c -o $@ $*.c
4816 +
4817 +zvmlinux: $(OBJECTS) $(LIBS) ../ld.script ../images/vmlinux.gz ../common/dummy.o
4818 +       $(OBJCOPY) \
4819 +               --add-section=.image=../images/vmlinux.gz \
4820 +               --set-section-flags=.image=contents,alloc,load,readonly,data \
4821 +               ../common/dummy.o image.o
4822 +       $(LD) $(LD_ARGS) -o $@ $(OBJECTS) image.o $(LIBS)
4823 +       $(OBJCOPY) $(OBJCOPY_ARGS) $@ $@ -R .comment -R .stab -R .stabstr \
4824 +               -R .initrd -R .sysmap
4825 +
4826 +# Here we manipulate the image in order to get it the necessary
4827 +# srecord file we need.
4828 +zImage: zvmlinux
4829 +       mv zvmlinux ../images/zImage.$(BNAME)
4830 +       $(OBJCOPY) -O srec ../images/zImage.$(BNAME) ../images/$(BNAME).srec
4831 +
4832 +zImage.flash: zImage
4833 +       $(OBJCOPY) -O srec --adjust-vma 0x3ed00000 \
4834 +               ../images/zImage.$(BNAME) ../images/$(BNAME).flash.srec
4835 +
4836 +include $(TOPDIR)/Rules.make
4837 diff -Naru linux/arch/mips/zboot/utils/entry linux-new/arch/mips/zboot/utils/entry
4838 --- linux/arch/mips/zboot/utils/entry   1969-12-31 19:00:00.000000000 -0500
4839 +++ linux-new/arch/mips/zboot/utils/entry       2003-12-18 14:26:21.000000000 -0500
4840 @@ -0,0 +1,12 @@
4841 +#!/bin/sh
4842 +
4843 +# grab the kernel_entry address from the vmlinux elf image
4844 +entry=`$1 $2  | grep kernel_entry`
4845 +
4846 +fs=`echo $entry | grep ffffffff`  # check toolchain output
4847 +
4848 +if [ -n "$fs" ]; then
4849 +       echo "0x"`$1 $2  | grep kernel_entry | cut -c9- | awk '{print $1}'`
4850 +else
4851 +       echo "0x"`$1 $2  | grep kernel_entry | cut -c1- | awk '{print $1}'`
4852 +fi
4853 diff -Naru linux/arch/mips/zboot/utils/offset linux-new/arch/mips/zboot/utils/offset
4854 --- linux/arch/mips/zboot/utils/offset  1969-12-31 19:00:00.000000000 -0500
4855 +++ linux-new/arch/mips/zboot/utils/offset      2003-12-18 14:26:21.000000000 -0500
4856 @@ -0,0 +1,3 @@
4857 +#!/bin/sh
4858 +
4859 +echo "0x"`$1 -h $2  | grep $3 | grep -v zvmlinux| awk '{print $6}'`
4860 diff -Naru linux/arch/mips/zboot/utils/size linux-new/arch/mips/zboot/utils/size
4861 --- linux/arch/mips/zboot/utils/size    1969-12-31 19:00:00.000000000 -0500
4862 +++ linux-new/arch/mips/zboot/utils/size        2003-12-18 14:26:21.000000000 -0500
4863 @@ -0,0 +1,4 @@
4864 +#!/bin/sh
4865 +
4866 +OFFSET=`$1 -h $2  | grep $3 | grep -v zvmlinux | awk '{print $3}'`
4867 +echo "0x"$OFFSET
4868 diff -Naru linux/arch/mips/zboot/xxs1500/head.S linux-new/arch/mips/zboot/xxs1500/head.S
4869 --- linux/arch/mips/zboot/xxs1500/head.S        1969-12-31 19:00:00.000000000 -0500
4870 +++ linux-new/arch/mips/zboot/xxs1500/head.S    2003-12-18 14:26:21.000000000 -0500
4871 @@ -0,0 +1,137 @@
4872 +/*
4873 + * arch/mips/kernel/head.S
4874 + *
4875 + * This file is subject to the terms and conditions of the GNU General Public
4876 + * License.  See the file "COPYING" in the main directory of this archive
4877 + * for more details.
4878 + *
4879 + * Copyright (C) 1994, 1995 Waldorf Electronics
4880 + * Written by Ralf Baechle and Andreas Busse
4881 + * Copyright (C) 1995 - 1999 Ralf Baechle
4882 + * Copyright (C) 1996 Paul M. Antoine
4883 + * Modified for DECStation and hence R3000 support by Paul M. Antoine
4884 + * Further modifications by David S. Miller and Harald Koerfgen
4885 + * Copyright (C) 1999 Silicon Graphics, Inc.
4886 + *
4887 + * Head.S contains the MIPS exception handler and startup code.
4888 + *
4889 + **************************************************************************
4890 + *  9 Nov, 2000.
4891 + *  Added Cache Error exception handler and SBDDP EJTAG debug exception.
4892 + *
4893 + *  Kevin Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
4894 + *  Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
4895 + **************************************************************************
4896 + */
4897 +#include <linux/config.h>
4898 +#include <linux/threads.h>
4899 +
4900 +#include <asm/asm.h>
4901 +#include <asm/cacheops.h>
4902 +#include <asm/mipsregs.h>
4903 +#include <asm/offset.h>
4904 +#include <asm/cachectl.h>
4905 +#include <asm/regdef.h>
4906 +
4907 +#define IndexInvalidate_I       0x00
4908 +
4909 +       .set noreorder
4910 +       .cprestore
4911 +       LEAF(start)
4912 +start:
4913 +
4914 +locate:
4915 +       la      sp, .stack
4916 +       move    s0, a0
4917 +       move    s1, a1
4918 +       move    s2, a2
4919 +       move    s3, a3
4920 +
4921 +       la      a0, start
4922 +
4923 +       li      a1, FLASH_LOAD_ADDR
4924 +       la      a2, _edata
4925 +       subu    t1, a2, a0
4926 +       srl     t1, t1, 2
4927 +
4928 +       /* copy text section */
4929 +       li      t0, 0
4930 +1:     lw      v0, 0(a1)
4931 +       nop
4932 +       sw      v0, 0(a0)
4933 +       xor     t0, t0, v0
4934 +       addu    a0, 4
4935 +       bne     a2, a0, 1b
4936 +       addu    a1, 4
4937 +
4938 +       /* Clear BSS */
4939 +       la      a0, _edata
4940 +       la      a2, _end
4941 +2:     sw      zero, 0(a0)
4942 +       bne     a2, a0, 2b
4943 +       addu    a0, 4
4944 +
4945 +       /* flush the I-Cache */
4946 +       li      k0, 0x80000000  # start address
4947 +       li      k1, 0x80004000  # end address (16KB I-Cache)
4948 +       subu    k1, 128
4949 +
4950 +1:
4951 +       .set mips3
4952 +       cache   IndexInvalidate_I, 0(k0)
4953 +       cache   IndexInvalidate_I, 32(k0)
4954 +       cache   IndexInvalidate_I, 64(k0)
4955 +       cache   IndexInvalidate_I, 96(k0)
4956 +       .set mips0
4957 +
4958 +       bne     k0, k1, 1b
4959 +       addu    k0, k0, 128
4960 +       /* done */
4961 +
4962 +       li      a0, FLASH_LOAD_ADDR  /* load address */
4963 +       move    a1, t1               /* length in words */
4964 +       move    a2, t0               /* checksum */
4965 +       move    a3, sp
4966 +
4967 +       la      ra, 1f
4968 +       la      k0, decompress_kernel
4969 +       jr      k0
4970 +       nop
4971 +1:
4972 +
4973 +       move    a0, s0
4974 +       move    a1, s1
4975 +       move    a2, s2
4976 +       move    a3, s3
4977 +       li      k0, KERNEL_ENTRY
4978 +       jr      k0
4979 +       nop
4980 +3:
4981 +       b 3b
4982 +       END(start)
4983 +
4984 +       LEAF(udelay)
4985 +udelay:
4986 +       END(udelay)
4987 +
4988 +
4989 +       LEAF(FlushCache)
4990 +       li      k0, 0x80000000  # start address
4991 +       li      k1, 0x80004000  # end address (16KB I-Cache)
4992 +       subu    k1, 128
4993 +
4994 +1:
4995 +       .set mips3
4996 +       cache   IndexInvalidate_I, 0(k0)
4997 +       cache   IndexInvalidate_I, 32(k0)
4998 +       cache   IndexInvalidate_I, 64(k0)
4999 +       cache   IndexInvalidate_I, 96(k0)
5000 +       .set mips0
5001 +
5002 +       bne     k0, k1, 1b
5003 +       addu    k0, k0, 128
5004 +       jr      ra
5005 +       nop
5006 +       END(FlushCache)
5007 +
5008 +       .comm .stack,4096*2,4
5009 diff -Naru linux/arch/mips/zboot/xxs1500/ld.script linux-new/arch/mips/zboot/xxs1500/ld.script
5010 --- linux/arch/mips/zboot/xxs1500/ld.script     1969-12-31 19:00:00.000000000 -0500
5011 +++ linux-new/arch/mips/zboot/xxs1500/ld.script 2003-12-18 14:26:21.000000000 -0500
5012 @@ -0,0 +1,147 @@
5013 +OUTPUT_ARCH(mips)
5014 +ENTRY(start)
5015 +SECTIONS
5016 +{
5017 +  /* Read-only sections, merged into text segment: */
5018 +  /* . = 0x81000000; */
5019 +  .init          : { *(.init)          } =0
5020 +  .text      :
5021 +  {
5022 +    _ftext = . ;
5023 +    *(.text)
5024 +    *(.rodata)
5025 +    *(.rodata1)
5026 +    /* .gnu.warning sections are handled specially by elf32.em.  */
5027 +    *(.gnu.warning)
5028 +  } =0
5029 +  .kstrtab : { *(.kstrtab) }
5030 +
5031 +  . = ALIGN(16);               /* Exception table */
5032 +  __start___ex_table = .;
5033 +  __ex_table : { *(__ex_table) }
5034 +  __stop___ex_table = .;
5035 +
5036 +  __start___dbe_table = .;     /* Exception table for data bus errors */
5037 +  __dbe_table : { *(__dbe_table) }
5038 +  __stop___dbe_table = .;
5039 +
5040 +  __start___ksymtab = .;       /* Kernel symbol table */
5041 +  __ksymtab : { *(__ksymtab) }
5042 +  __stop___ksymtab = .;
5043 +
5044 +  _etext = .;
5045 +
5046 +  . = ALIGN(8192);
5047 +  .data.init_task : { *(.data.init_task) }
5048 +
5049 +  /* Startup code */
5050 +  . = ALIGN(4096);
5051 +  __init_begin = .;
5052 +  .text.init : { *(.text.init) }
5053 +  .data.init : { *(.data.init) }
5054 +  . = ALIGN(16);
5055 +  __setup_start = .;
5056 +  .setup.init : { *(.setup.init) }
5057 +  __setup_end = .;
5058 +  __initcall_start = .;
5059 +  .initcall.init : { *(.initcall.init) }
5060 +  __initcall_end = .;
5061 +  . = ALIGN(4096);     /* Align double page for init_task_union */
5062 +  __init_end = .;
5063 +
5064 +  . = ALIGN(4096);
5065 +  .data.page_aligned : { *(.data.idt) }
5066 +
5067 +  . = ALIGN(32);
5068 +  .data.cacheline_aligned : { *(.data.cacheline_aligned) }
5069 +
5070 +  .fini      : { *(.fini)    } =0
5071 +  .reginfo : { *(.reginfo) }
5072 +  /* Adjust the address for the data segment.  We want to adjust up to
5073 +     the same address within the page on the next page up.  It would
5074 +     be more correct to do this:
5075 +       . = .;
5076 +     The current expression does not correctly handle the case of a
5077 +     text segment ending precisely at the end of a page; it causes the
5078 +     data segment to skip a page.  The above expression does not have
5079 +     this problem, but it will currently (2/95) cause BFD to allocate
5080 +     a single segment, combining both text and data, for this case.
5081 +     This will prevent the text segment from being shared among
5082 +     multiple executions of the program; I think that is more
5083 +     important than losing a page of the virtual address space (note
5084 +     that no actual memory is lost; the page which is skipped can not
5085 +     be referenced).  */
5086 +  . = .;
5087 +  .data    :
5088 +  {
5089 +    _fdata = . ;
5090 +    *(.data)
5091 +
5092 +   /* Align the initial ramdisk image (INITRD) on page boundaries. */
5093 +   . = ALIGN(4096);
5094 +   __rd_start = .;
5095 +   *(.initrd)
5096 +   __rd_end = .;
5097 +   . = ALIGN(4096);
5098 +
5099 +    CONSTRUCTORS
5100 +  }
5101 +  .data1   : { *(.data1) }
5102 +  _gp = . + 0x8000;
5103 +  .lit8 : { *(.lit8) }
5104 +  .lit4 : { *(.lit4) }
5105 +  .ctors         : { *(.ctors)   }
5106 +  .dtors         : { *(.dtors)   }
5107 +  .got           : { *(.got.plt) *(.got) }
5108 +  .dynamic       : { *(.dynamic) }
5109 +  /* We want the small data sections together, so single-instruction offsets
5110 +     can access them all, and initialized data all before uninitialized, so
5111 +     we can shorten the on-disk segment size.  */
5112 +  .sdata     : { *(.sdata) }
5113 +  . = ALIGN(4);
5114 +  _edata  =  .;
5115 +  PROVIDE (edata = .);
5116 +
5117 +  __bss_start = .;
5118 +  _fbss = .;
5119 +  .sbss      : { *(.sbss) *(.scommon) }
5120 +  .bss       :
5121 +  {
5122 +   *(.dynbss)
5123 +   *(.bss)
5124 +   *(COMMON)
5125 +   .  = ALIGN(4);
5126 +  _end = . ;
5127 +  PROVIDE (end = .);
5128 +  }
5129 +
5130 +  /* Sections to be discarded */
5131 +  /DISCARD/ :
5132 +  {
5133 +        *(.text.exit)
5134 +        *(.data.exit)
5135 +        *(.exitcall.exit)
5136 +  }
5137 +
5138 +  /* This is the MIPS specific mdebug section.  */
5139 +  .mdebug : { *(.mdebug) }
5140 +  /* These are needed for ELF backends which have not yet been
5141 +     converted to the new style linker.  */
5142 +  .stab 0 : { *(.stab) }
5143 +  .stabstr 0 : { *(.stabstr) }
5144 +  /* DWARF debug sections.
5145 +     Symbols in the .debug DWARF section are relative to the beginning of the
5146 +     section so we begin .debug at 0.  It's not clear yet what needs to happen
5147 +     for the others.   */
5148 +  .debug          0 : { *(.debug) }
5149 +  .debug_srcinfo  0 : { *(.debug_srcinfo) }
5150 +  .debug_aranges  0 : { *(.debug_aranges) }
5151 +  .debug_pubnames 0 : { *(.debug_pubnames) }
5152 +  .debug_sfnames  0 : { *(.debug_sfnames) }
5153 +  .line           0 : { *(.line) }
5154 +  /* These must appear regardless of  .  */
5155 +  .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }
5156 +  .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }
5157 +  .comment : { *(.comment) }
5158 +  .note : { *(.note) }
5159 +}
5160 diff -Naru linux/arch/mips/zboot/xxs1500/Makefile linux-new/arch/mips/zboot/xxs1500/Makefile
5161 --- linux/arch/mips/zboot/xxs1500/Makefile      1969-12-31 19:00:00.000000000 -0500
5162 +++ linux-new/arch/mips/zboot/xxs1500/Makefile  2003-12-18 14:26:21.000000000 -0500
5163 @@ -0,0 +1,123 @@
5164 +# arch/mips/compressed/alchemy/Makefile
5165 +# 
5166 +# Makefile for Alchemy Semiconductor Pb1[015]00 boards.
5167 +# All of the boot loader code was derived from the ppc
5168 +# boot code.
5169 +#
5170 +# Copyright 2001,2002 MontaVista Software Inc.
5171 +#
5172 +# Author: Mark A. Greer
5173 +#        mgreer@mvista.com
5174 +# Ported and modified for mips support by 
5175 +# Pete Popov <ppopov@mvista.com>
5176 +#
5177 +# This program is free software; you can redistribute  it and/or modify it
5178 +# under  the terms of  the GNU General  Public License as published by the
5179 +# Free Software Foundation;  either version 2 of the  License, or (at your
5180 +# option) any later version.
5181 +
5182 +.c.s:
5183 +       $(CC) $(CFLAGS) -S -o $*.s $<
5184 +.s.o:
5185 +       $(AS) -o $*.o $<
5186 +.c.o:
5187 +       $(CC) $(CFLAGS) -D__BOOTER__ -c -o $*.o $<
5188 +.S.s:
5189 +       $(CPP) $(AFLAGS) -o $*.o $<
5190 +.S.o:
5191 +       $(CC) $(AFLAGS) -c -o $*.o $<
5192 +
5193 +#########################################################################
5194 +# START BOARD SPECIFIC VARIABLES
5195 +BNAME=xxs1500
5196 +
5197 +
5198 +# These two variables control where the zImage is stored
5199 +# in flash and loaded in memory. If you change either one,
5200 +# be sure to make the appropriate change to the zImage
5201 +# rule.
5202 +RAM_LOAD_ADDR = 0x81000000
5203 +FLASH_LOAD_ADDR = 0xBF000000
5204 +
5205 +# These two variables specify the free ram region
5206 +# that can be used for temporary malloc area
5207 +AVAIL_RAM_START=0x80400000
5208 +AVAIL_RAM_END=0x80800000
5209 +
5210 +# This one must match the LOADADDR in arch/mips/Makefile!
5211 +LOADADDR=0x80100000
5212 +# END BOARD SPECIFIC VARIABLES
5213 +#########################################################################
5214 +
5215 +ZLINKFLAGS = -T ld.script -Ttext $(RAM_LOAD_ADDR)
5216 +
5217 +OBJECTS := head.o ../common/misc-common.o ../common/misc-simple.o \
5218 +       ../common/au1k_uart.o ../common/string.o ../common/ctype.o
5219 +LIBS := ../lib/zlib.a
5220 +
5221 +ENTRY := ../utils/entry
5222 +OFFSET := ../utils/offset
5223 +SIZE := ../utils/size
5224 +
5225 +all: zImage
5226 +
5227 +clean:
5228 +       rm -rf *.o vmlinux* zvmlinux.*
5229 +
5230 +head.o: head.S $(TOPDIR)/vmlinux
5231 +       $(CC) -DFLASH_LOAD_ADDR=$(FLASH_LOAD_ADDR) $(AFLAGS) \
5232 +       -DKERNEL_ENTRY=$(shell sh $(ENTRY) $(NM) $(TOPDIR)/vmlinux ) \
5233 +       -c -o $*.o $<
5234 +
5235 +../common/misc-simple.o:
5236 +       $(CC) $(CFLAGS) -DINITRD_OFFSET=0 -DINITRD_SIZE=0 -DZIMAGE_OFFSET=0 \
5237 +               -DAVAIL_RAM_START=$(AVAIL_RAM_START) \
5238 +               -DAVAIL_RAM_END=$(AVAIL_RAM_END) \
5239 +               -DLOADADDR=$(LOADADDR) \
5240 +               -DZIMAGE_SIZE=0 -c -o $@ $*.c
5241 +
5242 +# This is the first pass at building the boot loader image,
5243 +# without knowing the file offset where the vmlinuz.gz
5244 +# kernel will end up.  We build this image, check the offset,
5245 +# and then rebuild it with the correct offset and size
5246 +# passed to mips-simple.c
5247 +zvmlinux.no: $(OBJECTS) $(LIBS) ../images/vmlinux.gz
5248 +       $(LD) $(ZLINKFLAGS) -o $@.tmp $(OBJECTS) $(LIBS)
5249 +       $(OBJCOPY) -R .comment \
5250 +               --add-section=image=../images/vmlinux.gz \
5251 +               $@.tmp $@
5252 +       # rm -f $@.tmp
5253 +
5254 +
5255 +# This is the final image we build, now that we know what
5256 +# the vmlinuz.gz offset is.
5257 +zvmlinux: $(OBJECTS) $(LIBS) ../images/vmlinux.gz zvmlinux.no
5258 +       $(CC) $(CFLAGS) -DINITRD_OFFSET=0 -DINITRD_SIZE=0 \
5259 +               -DZIMAGE_OFFSET=$(shell sh $(OFFSET) $(OBJDUMP) $@.no image) \
5260 +               -DZIMAGE_SIZE=$(shell sh $(SIZE) $(OBJDUMP) $@.no image) \
5261 +               -D__BOOTER__ \
5262 +               -DAVAIL_RAM_START=$(AVAIL_RAM_START) \
5263 +               -DAVAIL_RAM_END=$(AVAIL_RAM_END) \
5264 +               -DLOADADDR=$(LOADADDR) \
5265 +               -c -o ../common/misc-simple.o ../common/misc-simple.c
5266 +       $(LD) $(ZLINKFLAGS) -o $@.tmp $(OBJECTS) $(LIBS)
5267 +       $(OBJCOPY) -R .comment \
5268 +               --add-section=image=../images/vmlinux.gz \
5269 +               $@.tmp $@
5270 +       $(OBJCOPY) --adjust-section-vma=image+$(RAM_LOAD_ADDR) $@
5271 +       $(OBJCOPY) --adjust-section-vma=image+$(shell sh $(OFFSET) \
5272 +       $(OBJDUMP) $@.no image ) $@
5273 +       # rm -f $@.tmp
5274 +       # rm -f $@.no
5275 +
5276 +
5277 +# Here we manipulate the image in order to get it the necessary
5278 +# srecord file we need.
5279 +zImage: zvmlinux
5280 +       mv zvmlinux ../images/$@.$(BNAME)
5281 +       $(OBJCOPY) --set-section-flags=image=alloc,load,code ../images/$@.$(BNAME)
5282 +       $(OBJCOPY) -O srec --adjust-vma 0x3e000000 \
5283 +       ../images/$@.$(BNAME) ../images/$@.$(BNAME).srec
5284 +       # rm ../images/vmlinux.gz
5285 +
5286 +include $(TOPDIR)/Rules.make