Merge aruba support
[openwrt/svn-archive/archive.git] / openwrt / target / linux / image / aruba / 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 #define LOADADDR 0x80100000
39
40 #define KSEG0 0x80000000
41 #define KSEG1 0xa0000000
42
43 #define KSEG1ADDR(a) ((((unsigned)(a)) & 0x1fffffffU) | KSEG1)
44
45 #define Index_Invalidate_I 0x00
46 #define Index_Writeback_Inv_D 0x01
47
48 #define cache_unroll(base,op) \
49 __asm__ __volatile__( \
50 ".set noreorder;\n" \
51 ".set mips3;\n" \
52 "cache %1, (%0);\n" \
53 ".set mips0;\n" \
54 ".set reorder\n" \
55 : \
56 : "r" (base), \
57 "i" (op));
58
59
60 static __inline__ void blast_icache(unsigned long size, unsigned long lsize)
61 {
62 unsigned long start = KSEG0;
63 unsigned long end = (start + size);
64
65 while(start < end) {
66 cache_unroll(start,Index_Invalidate_I);
67 start += lsize;
68 }
69 }
70
71 static __inline__ void blast_dcache(unsigned long size, unsigned long lsize)
72 {
73 unsigned long start = KSEG0;
74 unsigned long end = (start + size);
75
76 while(start < end) {
77 cache_unroll(start,Index_Writeback_Inv_D);
78 start += lsize;
79 }
80 }
81
82 unsigned char *data;
83
84 static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
85 {
86 *bufferSize = 1;
87 *buffer = data;
88 ++data;
89 return LZMA_RESULT_OK;
90 }
91
92 static __inline__ unsigned char get_byte(void)
93 {
94 unsigned char *buffer;
95 UInt32 fake;
96
97 return read_byte(0, &buffer, &fake), *buffer;
98 }
99
100 static char *buffer = (char *)0x80C00000;
101 extern char lzma_start[];
102 extern char lzma_end[];
103
104 /* should be the first function */
105 void entry(unsigned long icache_size, unsigned long icache_lsize,
106 unsigned long dcache_size, unsigned long dcache_lsize)
107 {
108 unsigned int i; /* temp value */
109 unsigned int osize; /* uncompressed size */
110
111 ILzmaInCallback callback;
112 CLzmaDecoderState vs;
113 callback.Read = read_byte;
114
115 data = lzma_start;
116
117 /* lzma args */
118 i = get_byte();
119 vs.Properties.lc = i % 9, i = i / 9;
120 vs.Properties.lp = i % 5, vs.Properties.pb = i / 5;
121
122 vs.Probs = (CProb *)buffer;
123
124 /* skip rest of the LZMA coder property */
125 for (i = 0; i < 4; i++)
126 get_byte();
127
128 /* read the lower half of uncompressed size in the header */
129 osize = ((unsigned int)get_byte()) +
130 ((unsigned int)get_byte() << 8) +
131 ((unsigned int)get_byte() << 16) +
132 ((unsigned int)get_byte() << 24);
133
134 /* skip rest of the header (upper half of uncompressed size) */
135 for (i = 0; i < 4; i++)
136 get_byte();
137
138 /* decompress kernel */
139 if ((i = LzmaDecode(&vs, &callback,
140 (unsigned char*)LOADADDR, osize, &osize)) == LZMA_RESULT_OK)
141 {
142 blast_dcache(dcache_size, dcache_lsize);
143 blast_icache(icache_size, icache_lsize);
144
145 /* Jump to load address */
146 ((void (*)(void)) LOADADDR)();
147 }
148 }