[adm5120] from now on, the lzma loader is able to pass parameters to the kernel
[openwrt.git] / target / linux / adm5120-2.6 / image / lzma-loader / src / decompress.c
1 /*
2  * LZMA compressed kernel decompressor for ADM5120 boards
3  *
4  * Copyright (C) 2005 by Oleg I. Vdovikin <oleg@cs.msu.su>
5  * Copyright (C) 2007 OpenWrt.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  *
22  * Please note, this was code based on the bunzip2 decompressor code
23  * by Manuel Novoa III  (mjn3@codepoet.org), although the only thing left
24  * is an idea and part of original vendor code
25  *
26  *
27  * 12-Mar-2005  Mineharu Takahara <mtakahar@yahoo.com>
28  *   pass actual output size to decoder (stream mode
29  *   compressed input is not a requirement anymore)
30  *
31  * 24-Apr-2005 Oleg I. Vdovikin
32  *   reordered functions using lds script, removed forward decl
33  *
34  * 24-Mar-2007 Gabor Juhos
35  *   pass original values of the a0,a1,a2,a3 registers to the kernel
36  *
37  * 19-May-2007 Gabor Juhos
38  *   endiannes related cleanups
39  *   add support for decompressing an embedded kernel
40  *
41  */
42
43 #include <stddef.h>
44
45 #include "config.h"
46 #include "LzmaDecode.h"
47
48 #define ADM5120_FLASH_START     0x1fc00000      /* Flash start */
49 #define ADM5120_FLASH_END       0x1fe00000      /* Flash end */
50
51 #define KSEG0                   0x80000000
52 #define KSEG1                   0xa0000000
53
54 #define KSEG1ADDR(a)            ((((unsigned)(a)) & 0x1fffffffU) | KSEG1)
55
56 #define Index_Invalidate_I      0x00
57 #define Index_Writeback_Inv_D   0x01
58
59 #define cache_unroll(base,op)   \
60         __asm__ __volatile__(           \
61                 ".set noreorder;\n"             \
62                 ".set mips3;\n"                 \
63                 "cache %1, (%0);\n"             \
64                 ".set mips0;\n"                 \
65                 ".set reorder\n"                \
66                 :                               \
67                 : "r" (base),                   \
68                   "i" (op));
69
70 static __inline__ void blast_icache(unsigned long size, unsigned long lsize)
71 {
72         unsigned long start = KSEG0;
73         unsigned long end = (start + size);
74
75         while(start < end) {
76                 cache_unroll(start,Index_Invalidate_I);
77                 start += lsize;
78         }
79 }
80
81 static __inline__ void blast_dcache(unsigned long size, unsigned long lsize)
82 {
83         unsigned long start = KSEG0;
84         unsigned long end = (start + size);
85
86         while(start < end) {
87                 cache_unroll(start,Index_Writeback_Inv_D);
88                 start += lsize;
89         }
90 }
91
92 #define TRX_MAGIC       0x30524448      /* "HDR0" */
93 #define TRX_ALIGN       0x1000
94
95 struct trx_header {
96         unsigned int magic;             /* "HDR0" */
97         unsigned int len;               /* Length of file including header */
98         unsigned int crc32;             /* 32-bit CRC from flag_version to end of file */
99         unsigned int flag_version;      /* 0:15 flags, 16:31 version */
100         unsigned int offsets[3];        /* Offsets of partitions from start of header */
101 };
102
103 /* beyound the image end, size not known in advance */
104 extern unsigned char workspace[];
105 #if LZMA_WRAPPER
106 extern unsigned char _lzma_data_start[];
107 extern unsigned char _lzma_data_end[];
108 #endif
109
110 extern void board_init(void);
111 extern void board_putc(int ch);
112
113 struct env_var {
114         char    *name;
115         char    *value;
116 };
117
118 #ifdef CONFIG_PASS_KARGS
119 #define ENVV(n,v)       {.name = (n), .value = (v)}
120 struct env_var env_vars[] = {
121         ENVV("board_name",      CONFIG_BOARD_NAME),
122         ENVV(NULL, NULL)
123 };
124 #endif
125
126 unsigned char *data;
127 unsigned long datalen;
128
129 typedef void (*kernel_entry)(unsigned long reg_a0, unsigned long reg_a1,
130         unsigned long reg_a2, unsigned long reg_a3);
131
132 static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
133 {
134         *bufferSize = 1;
135         *buffer = data++;
136
137         return LZMA_RESULT_OK;
138 }
139
140 static __inline__ unsigned char get_byte(void)
141 {
142         unsigned char *buffer;
143         UInt32 fake;
144
145         read_byte(0, &buffer, &fake);
146         return *buffer;
147 }
148
149 static __inline__ unsigned int read_le32(void *buf)
150 {
151         unsigned char *p;
152
153         p = buf;
154         return ((unsigned int)p[0] + ((unsigned int)p[1] << 8) +
155                 ((unsigned int)p[2] << 16) +((unsigned int)p[3] << 24));
156 }
157
158 static void print_char(char ch)
159 {
160         if (ch == '\n')
161                 board_putc('\r');
162         board_putc(ch);
163 }
164
165 static void print_str(char * str)
166 {
167         while ( *str != 0 )
168                 print_char(*str++);
169 }
170
171 static void print_hex(int val)
172 {
173         int i;
174         int tmp;
175
176         print_str("0x");
177         for ( i=0 ; i<8 ; i++ ) {
178                 tmp = (val >> ((7-i) * 4 )) & 0xf;
179                 tmp = tmp < 10 ? (tmp + '0') : (tmp + 'A' - 10);
180                 board_putc(tmp);
181         }
182 }
183
184 #if !(LZMA_WRAPPER)
185 static unsigned char *find_kernel(void)
186 {
187         struct trx_header *hdr;
188         unsigned char *ret;
189
190         print_str("Looking for TRX header... ");
191         /* look for trx header, 32-bit data access */
192         hdr = NULL;
193         for (ret = ((unsigned char *) KSEG1ADDR(ADM5120_FLASH_START));
194                 ret < ((unsigned char *)KSEG1ADDR(ADM5120_FLASH_END));
195                 ret += TRX_ALIGN) {
196
197                 if (read_le32(ret) == TRX_MAGIC) {
198                         hdr = (struct trx_header *)ret;
199                         break;
200                 }
201         }
202
203         if (hdr == NULL) {
204                 print_str("not found!\n");
205                 return NULL;
206         }
207
208         print_str("found at ");
209         print_hex((unsigned int)ret);
210         print_str(", kernel in partition ");
211
212         /* compressed kernel is in the partition 0 or 1 */
213         if ((read_le32(&hdr->offsets[1]) == 0) ||
214                 (read_le32(&hdr->offsets[1]) > 65536)) {
215                 ret += read_le32(&hdr->offsets[0]);
216                 print_str("0\n");
217         } else {
218                 ret += read_le32(&hdr->offsets[1]);
219                 print_str("1\n");
220         }
221
222         return ret;
223 }
224 #endif /* !(LZMA_WRAPPER) */
225
226 static void halt(void)
227 {
228         print_str("\nSystem halted!\n");
229         for(;;);
230 }
231
232 /* should be the first function */
233 void decompress_entry(unsigned long reg_a0, unsigned long reg_a1,
234         unsigned long reg_a2, unsigned long reg_a3,
235         unsigned long icache_size, unsigned long icache_lsize,
236         unsigned long dcache_size, unsigned long dcache_lsize)
237 {
238         unsigned int i;  /* temp value */
239         unsigned int lc; /* literal context bits */
240         unsigned int lp; /* literal pos state bits */
241         unsigned int pb; /* pos state bits */
242         unsigned int osize; /* uncompressed size */
243         int res;
244 #if !(LZMA_WRAPPER)
245         ILzmaInCallback callback;
246 #endif
247
248         board_init();
249
250         print_str("\n\nLZMA loader for " CONFIG_BOARD_NAME
251                         ", Copyright (C) 2007 OpenWrt.org\n\n");
252
253 #if LZMA_WRAPPER
254         data = _lzma_data_start;
255         datalen = _lzma_data_end - _lzma_data_start;
256 #else
257         data = find_kernel();
258         if (data == NULL) {
259                 /* no compressed kernel found, halting */
260                 halt();
261         }
262
263         datalen = ((unsigned char *) KSEG1ADDR(ADM5120_FLASH_END))-data;
264 #endif
265
266         /* lzma args */
267         i = get_byte();
268         lc = i % 9, i = i / 9;
269         lp = i % 5, pb = i / 5;
270
271         /* skip rest of the LZMA coder property */
272         for (i = 0; i < 4; i++)
273                 get_byte();
274
275         /* read the lower half of uncompressed size in the header */
276         osize = ((unsigned int)get_byte()) +
277                 ((unsigned int)get_byte() << 8) +
278                 ((unsigned int)get_byte() << 16) +
279                 ((unsigned int)get_byte() << 24);
280
281         /* skip rest of the header (upper half of uncompressed size) */
282         for (i = 0; i < 4; i++)
283                 get_byte();
284
285         print_str("decompressing kernel... ");
286
287         /* decompress kernel */
288 #if LZMA_WRAPPER
289         res = LzmaDecode(workspace, ~0, lc, lp, pb, data, datalen,
290                 (unsigned char*)LOADADDR, osize, &i);
291 #else
292         callback.Read = read_byte;
293         res = LzmaDecode(workspace, ~0, lc, lp, pb, &callback,
294                 (unsigned char*)LOADADDR, osize, &i);
295 #endif
296         if (res != LZMA_RESULT_OK) {
297                 print_str("failed!\n");
298                 print_str("LzmaDecode: ");
299                 switch (res) {
300                 case LZMA_RESULT_DATA_ERROR:
301                         print_str("data error\n");
302                         break;
303                 case LZMA_RESULT_NOT_ENOUGH_MEM:
304                         print_str("not enough memory\n");
305                         break;
306                 default:
307                         print_str("unknown error, err=0x");
308                         print_hex(res);
309                         print_str("\n");
310                 }
311                 halt();
312         }
313
314         print_str("done!\n");
315
316         blast_dcache(dcache_size, dcache_lsize);
317         blast_icache(icache_size, icache_lsize);
318
319         print_str("launching kernel...\n\n");
320
321 #ifdef CONFIG_PASS_KARGS
322         reg_a0 = 0;
323         reg_a1 = 0;
324         reg_a2 = (unsigned long)env_vars;
325         reg_a3 = 0;
326 #endif
327         /* Jump to load address */
328         ((kernel_entry) LOADADDR)(reg_a0, reg_a1, reg_a2, reg_a3);
329 }