remove linux 2.4 specific build system code
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / 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
39 #define KSEG0 0x80000000
40 #define KSEG1 0xa0000000
41
42 #define KSEG1ADDR(a) ((((unsigned)(a)) & 0x1fffffffU) | KSEG1)
43
44 #define Index_Invalidate_I 0x00
45 #define Index_Writeback_Inv_D 0x01
46
47 #define cache_unroll(base,op) \
48 __asm__ __volatile__( \
49 ".set noreorder;\n" \
50 ".set mips3;\n" \
51 "cache %1, (%0);\n" \
52 ".set mips0;\n" \
53 ".set reorder\n" \
54 : \
55 : "r" (base), \
56 "i" (op));
57
58
59 static __inline__ void blast_icache(unsigned long size, unsigned long lsize)
60 {
61 unsigned long start = KSEG0;
62 unsigned long end = (start + size);
63
64 while(start < end) {
65 cache_unroll(start,Index_Invalidate_I);
66 start += lsize;
67 }
68 }
69
70 static __inline__ void blast_dcache(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_Writeback_Inv_D);
77 start += lsize;
78 }
79 }
80
81 unsigned char *data;
82
83 static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
84 {
85 *bufferSize = 1;
86 *buffer = data;
87 ++data;
88 return LZMA_RESULT_OK;
89 }
90
91 static __inline__ unsigned char get_byte(void)
92 {
93 unsigned char *buffer;
94 UInt32 fake;
95
96 return read_byte(0, &buffer, &fake), *buffer;
97 }
98
99 /* This puts lzma workspace 128k below RAM end.
100 * That should be enough for both lzma and stack
101 */
102 static char *buffer = (char *)(RAMSTART + RAMSIZE - 0x00020000);
103 extern char lzma_start[];
104 extern char lzma_end[];
105
106 /* should be the first function */
107 void entry(unsigned long icache_size, unsigned long icache_lsize,
108 unsigned long dcache_size, unsigned long dcache_lsize)
109 {
110 unsigned int i; /* temp value */
111 unsigned int osize; /* uncompressed size */
112 volatile unsigned int arg0, arg1, arg2, arg3;
113
114 /* restore argument registers */
115 __asm__ __volatile__ ("ori %0, $12, 0":"=r"(arg0));
116 __asm__ __volatile__ ("ori %0, $13, 0":"=r"(arg1));
117 __asm__ __volatile__ ("ori %0, $14, 0":"=r"(arg2));
118 __asm__ __volatile__ ("ori %0, $15, 0":"=r"(arg3));
119
120 ILzmaInCallback callback;
121 CLzmaDecoderState vs;
122 callback.Read = read_byte;
123
124 data = lzma_start;
125
126 /* lzma args */
127 i = get_byte();
128 vs.Properties.lc = i % 9, i = i / 9;
129 vs.Properties.lp = i % 5, vs.Properties.pb = i / 5;
130
131 vs.Probs = (CProb *)buffer;
132
133 /* skip rest of the LZMA coder property */
134 for (i = 0; i < 4; i++)
135 get_byte();
136
137 /* read the lower half of uncompressed size in the header */
138 osize = ((unsigned int)get_byte()) +
139 ((unsigned int)get_byte() << 8) +
140 ((unsigned int)get_byte() << 16) +
141 ((unsigned int)get_byte() << 24);
142
143 /* skip rest of the header (upper half of uncompressed size) */
144 for (i = 0; i < 4; i++)
145 get_byte();
146
147 /* decompress kernel */
148 if ((i = LzmaDecode(&vs, &callback,
149 (unsigned char*)KERNEL_ENTRY, osize, &osize)) == LZMA_RESULT_OK)
150 {
151 blast_dcache(dcache_size, dcache_lsize);
152 blast_icache(icache_size, icache_lsize);
153
154 /* Jump to load address */
155 ((void (*)(int a0, int a1, int a2, int a3)) KERNEL_ENTRY)(arg0, arg1, arg2, arg3);
156 }
157 }