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