2 * LZMA compressed kernel decompressor for ADM5120 boards
4 * Copyright (C) 2005 by Oleg I. Vdovikin <oleg@cs.msu.su>
5 * Copyright (C) 2007 OpenWrt.org
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.
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.
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
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
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)
31 * 24-Apr-2005 Oleg I. Vdovikin
32 * reordered functions using lds script, removed forward decl
34 * 24-Mar-2007 Gabor Juhos
35 * pass original values of the a0,a1,a2,a3 registers to the kernel
37 * 19-May-2007 Gabor Juhos
38 * endiannes related cleanups
39 * add support for decompressing an embedded kernel
46 #include "LzmaDecode.h"
48 #define ADM5120_FLASH_START 0x1fc00000 /* Flash start */
49 #define ADM5120_FLASH_END 0x1fe00000 /* Flash end */
51 #define KSEG0 0x80000000
52 #define KSEG1 0xa0000000
54 #define KSEG1ADDR(a) ((((unsigned)(a)) & 0x1fffffffU) | KSEG1)
56 #define Index_Invalidate_I 0x00
57 #define Index_Writeback_Inv_D 0x01
59 #define cache_unroll(base,op) \
60 __asm__ __volatile__( \
70 static __inline__
void blast_icache(unsigned long size
, unsigned long lsize
)
72 unsigned long start
= KSEG0
;
73 unsigned long end
= (start
+ size
);
76 cache_unroll(start
,Index_Invalidate_I
);
81 static __inline__
void blast_dcache(unsigned long size
, unsigned long lsize
)
83 unsigned long start
= KSEG0
;
84 unsigned long end
= (start
+ size
);
87 cache_unroll(start
,Index_Writeback_Inv_D
);
92 #define TRX_MAGIC 0x30524448 /* "HDR0" */
93 #define TRX_ALIGN 0x1000
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 */
103 /* beyound the image end, size not known in advance */
104 extern unsigned char workspace
[];
106 extern unsigned char _lzma_data_start
[];
107 extern unsigned char _lzma_data_end
[];
110 extern void board_init(void);
111 extern void board_putc(int ch
);
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
),
127 unsigned long datalen
;
129 typedef void (*kernel_entry
)(unsigned long reg_a0
, unsigned long reg_a1
,
130 unsigned long reg_a2
, unsigned long reg_a3
);
132 static int read_byte(void *object
, unsigned char **buffer
, UInt32
*bufferSize
)
137 return LZMA_RESULT_OK
;
140 static __inline__
unsigned char get_byte(void)
142 unsigned char *buffer
;
145 read_byte(0, &buffer
, &fake
);
149 static __inline__
unsigned int read_le32(void *buf
)
154 return ((unsigned int)p
[0] + ((unsigned int)p
[1] << 8) +
155 ((unsigned int)p
[2] << 16) +((unsigned int)p
[3] << 24));
158 static void print_char(char ch
)
165 static void print_str(char * str
)
171 static void print_hex(int val
)
177 for ( i
=0 ; i
<8 ; i
++ ) {
178 tmp
= (val
>> ((7-i
) * 4 )) & 0xf;
179 tmp
= tmp
< 10 ? (tmp
+ '0') : (tmp
+ 'A' - 10);
185 static unsigned char *find_kernel(void)
187 struct trx_header
*hdr
;
190 print_str("Looking for TRX header... ");
191 /* look for trx header, 32-bit data access */
193 for (ret
= ((unsigned char *) KSEG1ADDR(ADM5120_FLASH_START
));
194 ret
< ((unsigned char *)KSEG1ADDR(ADM5120_FLASH_END
));
197 if (read_le32(ret
) == TRX_MAGIC
) {
198 hdr
= (struct trx_header
*)ret
;
204 print_str("not found!\n");
208 print_str("found at ");
209 print_hex((unsigned int)ret
);
210 print_str(", kernel in partition ");
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]);
218 ret
+= read_le32(&hdr
->offsets
[1]);
224 #endif /* !(LZMA_WRAPPER) */
226 static void halt(void)
228 print_str("\nSystem halted!\n");
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
)
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 */
245 ILzmaInCallback callback
;
250 print_str("\n\nLZMA loader for " CONFIG_BOARD_NAME
251 ", Copyright (C) 2007 OpenWrt.org\n\n");
254 data
= _lzma_data_start
;
255 datalen
= _lzma_data_end
- _lzma_data_start
;
257 data
= find_kernel();
259 /* no compressed kernel found, halting */
263 datalen
= ((unsigned char *) KSEG1ADDR(ADM5120_FLASH_END
))-data
;
268 lc
= i
% 9, i
= i
/ 9;
269 lp
= i
% 5, pb
= i
/ 5;
271 /* skip rest of the LZMA coder property */
272 for (i
= 0; i
< 4; i
++)
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);
281 /* skip rest of the header (upper half of uncompressed size) */
282 for (i
= 0; i
< 4; i
++)
285 print_str("decompressing kernel... ");
287 /* decompress kernel */
289 res
= LzmaDecode(workspace
, ~0, lc
, lp
, pb
, data
, datalen
,
290 (unsigned char*)LOADADDR
, osize
, &i
);
292 callback
.Read
= read_byte
;
293 res
= LzmaDecode(workspace
, ~0, lc
, lp
, pb
, &callback
,
294 (unsigned char*)LOADADDR
, osize
, &i
);
296 if (res
!= LZMA_RESULT_OK
) {
297 print_str("failed!\n");
298 print_str("LzmaDecode: ");
300 case LZMA_RESULT_DATA_ERROR
:
301 print_str("data error\n");
303 case LZMA_RESULT_NOT_ENOUGH_MEM
:
304 print_str("not enough memory\n");
307 print_str("unknown error, err=0x");
314 print_str("done!\n");
316 blast_dcache(dcache_size
, dcache_lsize
);
317 blast_icache(icache_size
, icache_lsize
);
319 print_str("launching kernel...\n\n");
321 #ifdef CONFIG_PASS_KARGS
324 reg_a2
= (unsigned long)env_vars
;
327 /* Jump to load address */
328 ((kernel_entry
) LOADADDR
)(reg_a0
, reg_a1
, reg_a2
, reg_a3
);