libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / lzma-loader / decompress.c
blob23ad9d178bce70e9d22356068a1a95c9c1ae1162
1 /*
2 * LZMA compressed kernel decompressor for bcm947xx boards
4 * Copyright (C) 2005 by Oleg I. Vdovikin <oleg@cs.msu.su>
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.
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.
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
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
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)
30 * 24-Apr-2005 Oleg I. Vdovikin
31 * reordered functions using lds script, removed forward decl
35 #include "LzmaDecode.h"
36 #include "trxhdr.h"
38 #define BCM4710_FLASH 0x1fc00000 /* Flash */
40 #define KSEG0 0x80000000
41 #define KSEG1 0xa0000000
43 #define KSEG1ADDR(a) ((((unsigned)(a)) & 0x1fffffffU) | KSEG1)
45 #define Index_Invalidate_I 0x00
46 #define Index_Writeback_Inv_D 0x01
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));
59 static __inline__ void blast_icache(unsigned long size, unsigned long lsize)
61 unsigned long start = KSEG0;
62 unsigned long end = (start + size);
64 while(start < end) {
65 cache_unroll(start,Index_Invalidate_I);
66 start += lsize;
70 static __inline__ void blast_dcache(unsigned long size, unsigned long lsize)
72 unsigned long start = KSEG0;
73 unsigned long end = (start + size);
75 while(start < end) {
76 cache_unroll(start,Index_Writeback_Inv_D);
77 start += lsize;
81 /* beyound the image end, size not known in advance */
82 extern unsigned char workspace[];
84 static unsigned int offset;
85 static unsigned char *data;
87 /* flash access should be aligned, so wrapper is used */
88 /* read byte from the flash, all accesses are 32-bit aligned */
89 static int read_byte(void *object, const unsigned char **buffer, UInt32 *bufferSize)
91 static unsigned int val;
93 if (((unsigned int)offset % 4) == 0) {
94 val = *(unsigned int *)data;
95 data += 4;
98 *bufferSize = 1;
99 *buffer = ((unsigned char *)&val) + (offset++ & 3);
101 return LZMA_RESULT_OK;
104 static __inline__ unsigned char get_byte(void)
106 const unsigned char *buffer;
107 UInt32 fake;
109 return read_byte(NULL, &buffer, &fake), *buffer;
112 /* should be the first function */
113 void entry(unsigned long icache_size, unsigned long icache_lsize,
114 unsigned long dcache_size, unsigned long dcache_lsize,
115 unsigned long fw_arg0, unsigned long fw_arg1,
116 unsigned long fw_arg2, unsigned long fw_arg3)
118 unsigned int i; /* temp value */
119 unsigned int osize; /* uncompressed size */
121 CLzmaDecoderState vs;
122 ILzmaInCallback callback;
123 callback.Read = read_byte;
125 /* look for trx header, 32-bit data access */
126 data = ((unsigned char *) KSEG1ADDR(BCM4710_FLASH));
127 while (1) {
128 if ( ((struct trx_header *)data)->magic == TRX_MAGIC
129 #ifdef TRX_MAGIC_F7D3301
130 || ((struct trx_header *)data)->magic == TRX_MAGIC_F7D3301
131 #endif
132 #ifdef TRX_MAGIC_F7D3302
133 || ((struct trx_header *)data)->magic == TRX_MAGIC_F7D3302
134 #endif
135 #ifdef TRX_MAGIC_F7D4302
136 || ((struct trx_header *)data)->magic == TRX_MAGIC_F7D4302
137 #endif
138 #ifdef TRX_MAGIC_F5D8235V3
139 || ((struct trx_header *)data)->magic == TRX_MAGIC_F5D8235V3
140 #endif
142 break;
143 data += 65536;
146 /* compressed kernel is in the partition 1 */
147 data += ((struct trx_header *)data)->offsets[1];
148 offset = 0;
150 /* lzma args */
151 i = get_byte();
152 vs.Properties.lc = i % 9, i = i / 9;
153 vs.Properties.lp = i % 5, vs.Properties.pb = i / 5;
154 vs.Probs = (CProb *)workspace;
156 /* skip rest of the LZMA coder property */
157 for (i = 0; i < 4; i++)
158 get_byte();
160 /* read the lower half of uncompressed size in the header */
161 osize = ((unsigned int)get_byte()) +
162 ((unsigned int)get_byte() << 8) +
163 ((unsigned int)get_byte() << 16) +
164 ((unsigned int)get_byte() << 24);
166 /* skip rest of the header (upper half of uncompressed size) */
167 for (i = 0; i < 4; i++)
168 get_byte();
170 /* decompress kernel */
171 if (LzmaDecode(&vs, &callback,
172 (unsigned char*)LOADADDR, osize, &i) == LZMA_RESULT_OK)
174 blast_dcache(dcache_size, dcache_lsize);
175 blast_icache(icache_size, icache_lsize);
177 /* Jump to load address */
178 ((void (*)(unsigned long, unsigned long, unsigned long,
179 unsigned long)) LOADADDR)(fw_arg0, fw_arg1, fw_arg2,
180 fw_arg3);