f328058a1665b211a3d0d7ab74bcf8d7bf9322c2
[openwrt/staging/lynxis.git] / target / linux / adm8668 / image / lzma-loader / src / decompress.c
1 /*
2 * LZMA compressed kernel decompressor for bcm947xx boards
3 *
4 * Copyright (C) 2005 by Oleg I. Vdovikin <oleg@cs.msu.su>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 *
21 * Please note, this was code based on the bunzip2 decompressor code
22 * by Manuel Novoa III (mjn3@codepoet.org), although the only thing left
23 * is an idea and part of original vendor code
24 *
25 *
26 * 12-Mar-2005 Mineharu Takahara <mtakahar@yahoo.com>
27 * pass actual output size to decoder (stream mode
28 * compressed input is not a requirement anymore)
29 *
30 * 24-Apr-2005 Oleg I. Vdovikin
31 * reordered functions using lds script, removed forward decl
32 *
33 * ??-Nov-2005 Mike Baker
34 * reorder the script as an lzma wrapper; do not depend on flash access
35 */
36
37 #include "LzmaDecode.h"
38 #include <exports.h>
39
40 #define KSEG0ADDR(addr) (0x80000000|addr)
41
42 register volatile gd_t *gd asm ("k0");
43 unsigned char *data;
44
45 static __inline__ unsigned char get_byte()
46 {
47 unsigned char *buffer;
48
49 buffer = data;
50 data++;
51
52 return *buffer;
53 }
54
55 /* This puts lzma workspace 128k below RAM end.
56 * That should be enough for both lzma and stack
57 */
58 static char *buffer = (char *)(RAMSTART + RAMSIZE - 0x00020000);
59 extern char _binary_vmlinux_lzma_start[];
60 extern char _binary_vmlinux_lzma_end[];
61 extern char lzma_start[];
62 extern char lzma_end[];
63
64 /* should be the first function */
65 void entry(unsigned int arg0, unsigned int arg1,
66 unsigned int arg2, unsigned int arg3)
67 {
68 unsigned int i; /* temp value */
69 unsigned int isize; /* compressed size */
70 unsigned int osize; /* uncompressed size */
71 int argc = arg0;
72 char **argv = (char **)arg1;
73 char **envp = (char **)arg2;
74
75 CLzmaDecoderState vs;
76
77 data = (unsigned char *)_binary_vmlinux_lzma_start;
78 isize = _binary_vmlinux_lzma_end - _binary_vmlinux_lzma_start + 1;
79
80 puts("\nLZMA kernel loader\n");
81
82 printf("lzma data @ %#x - %#x\n", _binary_vmlinux_lzma_start, _binary_vmlinux_lzma_end);
83 printf("load addr @ %#x\n\n", KERNEL_ENTRY);
84 printf("jump table @ %#x\n", gd->jt[3]);
85
86 /* lzma args */
87 i = get_byte();
88 vs.Properties.lc = i % 9, i = i / 9;
89 vs.Properties.lp = i % 5, vs.Properties.pb = i / 5;
90
91 vs.Probs = (CProb *)buffer;
92
93 /* skip rest of the LZMA coder property */
94 data += 4;
95
96 /* read the lower half of uncompressed size in the header */
97 osize = ((unsigned int)get_byte()) +
98 ((unsigned int)get_byte() << 8) +
99 ((unsigned int)get_byte() << 16) +
100 ((unsigned int)get_byte() << 24);
101
102 /* skip rest of the header (upper half of uncompressed size) */
103 data += 4;
104
105 /* decompress kernel */
106 puts("\nDecompressing kernel...");
107 if ((i = LzmaDecode(&vs,
108 (unsigned char*)data, isize, &isize,
109 (unsigned char*)KERNEL_ENTRY, osize, &osize)) == LZMA_RESULT_OK)
110 {
111 puts("success!\n");
112
113 /* Jump to load address */
114 // ((void (*)(int a0, int a1, int a2, int a3))KERNEL_ENTRY)(0,0,0,0);
115 ((void (*)(int a0, int a1, int a2, int a3))KERNEL_ENTRY)(arg0, arg1, arg2, arg3);
116 }
117 puts("failure!\n");
118 }