2007-03-19 19:34:37 +02:00
|
|
|
/*
|
2007-07-15 18:02:38 +03:00
|
|
|
*
|
2007-05-30 13:35:27 +03:00
|
|
|
* LZMA compressed kernel decompressor for ADM5120 boards
|
2007-03-19 19:34:37 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Oleg I. Vdovikin <oleg@cs.msu.su>
|
2008-09-23 17:56:29 +03:00
|
|
|
* Copyright (C) 2007-2008 OpenWrt.org
|
|
|
|
* Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
|
2007-03-19 19:34:37 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Please note, this was code based on the bunzip2 decompressor code
|
|
|
|
* by Manuel Novoa III (mjn3@codepoet.org), although the only thing left
|
|
|
|
* is an idea and part of original vendor code
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 12-Mar-2005 Mineharu Takahara <mtakahar@yahoo.com>
|
|
|
|
* pass actual output size to decoder (stream mode
|
|
|
|
* compressed input is not a requirement anymore)
|
|
|
|
*
|
|
|
|
* 24-Apr-2005 Oleg I. Vdovikin
|
|
|
|
* reordered functions using lds script, removed forward decl
|
|
|
|
*
|
2007-03-26 10:32:10 +03:00
|
|
|
* 24-Mar-2007 Gabor Juhos
|
|
|
|
* pass original values of the a0,a1,a2,a3 registers to the kernel
|
|
|
|
*
|
2007-05-30 13:35:27 +03:00
|
|
|
* 19-May-2007 Gabor Juhos
|
|
|
|
* endiannes related cleanups
|
2007-06-25 11:47:10 +03:00
|
|
|
* add support for decompressing an embedded kernel
|
2007-05-30 13:35:27 +03:00
|
|
|
*
|
2007-03-19 19:34:37 +02:00
|
|
|
*/
|
|
|
|
|
2007-05-30 13:35:27 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2007-06-25 11:47:10 +03:00
|
|
|
#include "config.h"
|
2007-07-15 18:02:38 +03:00
|
|
|
#include "printf.h"
|
2007-03-19 19:34:37 +02:00
|
|
|
#include "LzmaDecode.h"
|
|
|
|
|
2007-05-30 13:35:27 +03:00
|
|
|
#define ADM5120_FLASH_START 0x1fc00000 /* Flash start */
|
|
|
|
#define ADM5120_FLASH_END 0x1fe00000 /* Flash end */
|
2007-03-19 19:34:37 +02:00
|
|
|
|
|
|
|
#define KSEG0 0x80000000
|
|
|
|
#define KSEG1 0xa0000000
|
|
|
|
|
|
|
|
#define KSEG1ADDR(a) ((((unsigned)(a)) & 0x1fffffffU) | KSEG1)
|
|
|
|
|
|
|
|
#define Index_Invalidate_I 0x00
|
|
|
|
#define Index_Writeback_Inv_D 0x01
|
|
|
|
|
|
|
|
#define cache_unroll(base,op) \
|
|
|
|
__asm__ __volatile__( \
|
|
|
|
".set noreorder;\n" \
|
|
|
|
".set mips3;\n" \
|
|
|
|
"cache %1, (%0);\n" \
|
|
|
|
".set mips0;\n" \
|
|
|
|
".set reorder\n" \
|
2007-05-30 13:35:27 +03:00
|
|
|
: \
|
2007-03-19 19:34:37 +02:00
|
|
|
: "r" (base), \
|
|
|
|
"i" (op));
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
#ifdef LZMA_DEBUG
|
|
|
|
# define DBG(f, a...) printf(f, ## a)
|
|
|
|
#else
|
|
|
|
# define DBG(f, a...) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
2007-03-19 19:34:37 +02:00
|
|
|
static __inline__ void blast_icache(unsigned long size, unsigned long lsize)
|
|
|
|
{
|
|
|
|
unsigned long start = KSEG0;
|
|
|
|
unsigned long end = (start + size);
|
|
|
|
|
|
|
|
while(start < end) {
|
|
|
|
cache_unroll(start,Index_Invalidate_I);
|
|
|
|
start += lsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void blast_dcache(unsigned long size, unsigned long lsize)
|
|
|
|
{
|
|
|
|
unsigned long start = KSEG0;
|
|
|
|
unsigned long end = (start + size);
|
|
|
|
|
|
|
|
while(start < end) {
|
|
|
|
cache_unroll(start,Index_Writeback_Inv_D);
|
|
|
|
start += lsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRX_MAGIC 0x30524448 /* "HDR0" */
|
2007-05-30 13:35:27 +03:00
|
|
|
#define TRX_ALIGN 0x1000
|
2007-03-19 19:34:37 +02:00
|
|
|
|
|
|
|
struct trx_header {
|
|
|
|
unsigned int magic; /* "HDR0" */
|
|
|
|
unsigned int len; /* Length of file including header */
|
|
|
|
unsigned int crc32; /* 32-bit CRC from flag_version to end of file */
|
|
|
|
unsigned int flag_version; /* 0:15 flags, 16:31 version */
|
|
|
|
unsigned int offsets[3]; /* Offsets of partitions from start of header */
|
|
|
|
};
|
|
|
|
|
2007-06-25 11:47:10 +03:00
|
|
|
struct env_var {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
};
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
/* beyound the image end, size not known in advance */
|
|
|
|
extern unsigned char workspace[];
|
|
|
|
extern void board_init(void);
|
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
static CLzmaDecoderState lzma_state;
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
typedef void (*kernel_entry)(unsigned long reg_a0, unsigned long reg_a1,
|
|
|
|
unsigned long reg_a2, unsigned long reg_a3);
|
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream,
|
|
|
|
UInt32 outSize);
|
2007-07-15 18:02:38 +03:00
|
|
|
|
2007-06-25 11:47:10 +03:00
|
|
|
#ifdef CONFIG_PASS_KARGS
|
|
|
|
#define ENVV(n,v) {.name = (n), .value = (v)}
|
|
|
|
struct env_var env_vars[] = {
|
|
|
|
ENVV("board_name", CONFIG_BOARD_NAME),
|
|
|
|
ENVV(NULL, NULL)
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
static void halt(void)
|
|
|
|
{
|
|
|
|
printf("\nSystem halted!\n");
|
|
|
|
for(;;);
|
|
|
|
}
|
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
#if (LZMA_WRAPPER)
|
2007-07-15 18:02:38 +03:00
|
|
|
extern unsigned char _lzma_data_start[];
|
|
|
|
extern unsigned char _lzma_data_end[];
|
|
|
|
|
2007-03-19 19:34:37 +02:00
|
|
|
unsigned char *data;
|
2007-05-30 13:35:27 +03:00
|
|
|
unsigned long datalen;
|
2007-03-19 19:34:37 +02:00
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
static __inline__ unsigned char get_byte(void)
|
2007-03-19 19:34:37 +02:00
|
|
|
{
|
2007-07-15 18:02:38 +03:00
|
|
|
datalen--;
|
|
|
|
return *data++;
|
2007-03-19 19:34:37 +02:00
|
|
|
}
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
static void decompress_init(void)
|
2007-03-19 19:34:37 +02:00
|
|
|
{
|
2007-07-15 18:02:38 +03:00
|
|
|
data = _lzma_data_start;
|
|
|
|
datalen = _lzma_data_end - _lzma_data_start;
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream,
|
|
|
|
SizeT outSize)
|
2007-05-30 13:35:27 +03:00
|
|
|
{
|
2007-12-14 22:35:22 +02:00
|
|
|
SizeT ip, op;
|
|
|
|
|
|
|
|
return LzmaDecode(vs, data, datalen, &ip, outStream, outSize, &op);
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
2007-07-15 18:02:38 +03:00
|
|
|
#endif /* LZMA_WRAPPER */
|
|
|
|
|
|
|
|
#if !(LZMA_WRAPPER)
|
|
|
|
|
|
|
|
#define FLASH_BANK_SIZE (2<<20)
|
2007-05-30 13:35:27 +03:00
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
static unsigned char *flash_base = (unsigned char *) KSEG1ADDR(ADM5120_FLASH_START);
|
|
|
|
static unsigned long flash_ofs = 0;
|
|
|
|
static unsigned long flash_max = 0;
|
|
|
|
static unsigned long flash_ofs_mask = (FLASH_BANK_SIZE-1);
|
|
|
|
|
|
|
|
static __inline__ unsigned char get_byte(void)
|
2007-05-30 13:35:27 +03:00
|
|
|
{
|
2007-07-15 18:02:38 +03:00
|
|
|
return *(flash_base+flash_ofs++);
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
static int lzma_read_byte(void *object, const unsigned char **buffer,
|
|
|
|
SizeT *bufferSize)
|
2007-05-30 13:35:27 +03:00
|
|
|
{
|
2007-07-15 18:02:38 +03:00
|
|
|
unsigned long len;
|
|
|
|
|
|
|
|
if (flash_ofs >= flash_max)
|
|
|
|
return LZMA_RESULT_DATA_ERROR;
|
|
|
|
|
|
|
|
len = flash_max-flash_ofs;
|
|
|
|
|
|
|
|
#if (CONFIG_FLASH_SIZE > FLASH_BANK_SIZE)
|
|
|
|
if (flash_ofs < FLASH_BANK_SIZE) {
|
|
|
|
/* switch to bank 0 */
|
|
|
|
DBG("lzma_read_byte: switch to bank 0\n");
|
|
|
|
|
|
|
|
if (len > FLASH_BANK_SIZE-flash_ofs)
|
|
|
|
len = FLASH_BANK_SIZE-flash_ofs;
|
|
|
|
} else {
|
|
|
|
/* switch to bank 1 */
|
|
|
|
DBG("lzma_read_byte: switch to bank 1\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
DBG("lzma_read_byte: ofs=%08X, len=%08X\n", flash_ofs, len);
|
|
|
|
|
|
|
|
*buffer = flash_base+(flash_ofs & flash_ofs_mask);
|
|
|
|
*bufferSize = len;
|
|
|
|
flash_ofs += len;
|
|
|
|
|
|
|
|
return LZMA_RESULT_OK;
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
static ILzmaInCallback lzma_callback = {
|
|
|
|
.Read = lzma_read_byte,
|
|
|
|
};
|
|
|
|
|
|
|
|
static __inline__ unsigned int read_le32(void *buf)
|
2007-05-30 13:35:27 +03:00
|
|
|
{
|
2007-07-15 18:02:38 +03:00
|
|
|
unsigned char *p = buf;
|
|
|
|
|
|
|
|
return ((unsigned int)p[0] + ((unsigned int)p[1] << 8) +
|
|
|
|
((unsigned int)p[2] << 16) +((unsigned int)p[3] << 24));
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
static void decompress_init(void)
|
2007-05-30 13:35:27 +03:00
|
|
|
{
|
2007-07-15 18:02:38 +03:00
|
|
|
struct trx_header *hdr = NULL;
|
|
|
|
unsigned long kofs,klen;
|
2007-05-30 13:35:27 +03:00
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("Looking for TRX header... ");
|
2007-05-30 13:35:27 +03:00
|
|
|
/* look for trx header, 32-bit data access */
|
2007-07-15 18:02:38 +03:00
|
|
|
for (flash_ofs = 0; flash_ofs < FLASH_BANK_SIZE; flash_ofs += TRX_ALIGN) {
|
|
|
|
if (read_le32(&flash_base[flash_ofs]) == TRX_MAGIC) {
|
|
|
|
hdr = (struct trx_header *)&flash_base[flash_ofs];
|
2007-05-30 13:35:27 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr == NULL) {
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("not found!\n");
|
|
|
|
/* no compressed kernel found, halting */
|
|
|
|
halt();
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compressed kernel is in the partition 0 or 1 */
|
2007-07-15 18:02:38 +03:00
|
|
|
kofs = read_le32(&hdr->offsets[1]);
|
|
|
|
if (kofs == 0 || kofs > 65536) {
|
|
|
|
klen = kofs-read_le32(&hdr->offsets[0]);
|
|
|
|
kofs = read_le32(&hdr->offsets[0]);
|
2007-05-30 13:35:27 +03:00
|
|
|
} else {
|
2007-07-15 18:02:38 +03:00
|
|
|
klen = read_le32(&hdr->offsets[2]);
|
|
|
|
if (klen > kofs)
|
|
|
|
klen -= kofs;
|
|
|
|
else
|
|
|
|
klen = read_le32(&hdr->len)-kofs;
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
2007-06-25 11:47:10 +03:00
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("found at %08X, kernel:%08X len:%08X\n", flash_ofs,
|
|
|
|
kofs, klen);
|
|
|
|
|
|
|
|
flash_ofs += kofs;
|
|
|
|
flash_max = flash_ofs+klen;
|
2007-03-19 19:34:37 +02:00
|
|
|
}
|
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream,
|
|
|
|
SizeT outSize)
|
2007-05-30 13:35:27 +03:00
|
|
|
{
|
2007-12-14 22:35:22 +02:00
|
|
|
SizeT op;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
vs->Buffer = data;
|
|
|
|
vs->BufferLim = datalen;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return LzmaDecode(vs, &lzma_callback, outStream, outSize, &op);
|
2007-05-30 13:35:27 +03:00
|
|
|
}
|
2007-07-15 18:02:38 +03:00
|
|
|
#endif /* !(LZMA_WRAPPER) */
|
2007-04-02 20:14:23 +03:00
|
|
|
|
2007-03-19 19:34:37 +02:00
|
|
|
/* should be the first function */
|
2007-04-02 20:14:23 +03:00
|
|
|
void decompress_entry(unsigned long reg_a0, unsigned long reg_a1,
|
2007-03-26 10:32:10 +03:00
|
|
|
unsigned long reg_a2, unsigned long reg_a3,
|
2007-04-02 20:14:23 +03:00
|
|
|
unsigned long icache_size, unsigned long icache_lsize,
|
2007-03-19 19:34:37 +02:00
|
|
|
unsigned long dcache_size, unsigned long dcache_lsize)
|
|
|
|
{
|
2007-12-14 22:35:22 +02:00
|
|
|
unsigned char props[LZMA_PROPERTIES_SIZE];
|
2007-03-19 19:34:37 +02:00
|
|
|
unsigned int i; /* temp value */
|
2007-12-14 22:35:22 +02:00
|
|
|
SizeT osize; /* uncompressed size */
|
2007-05-30 13:35:27 +03:00
|
|
|
int res;
|
2007-03-19 19:34:37 +02:00
|
|
|
|
2007-05-30 13:35:27 +03:00
|
|
|
board_init();
|
2007-04-02 20:14:23 +03:00
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("\n\nLZMA loader for " CONFIG_BOARD_NAME
|
2008-09-23 17:56:29 +03:00
|
|
|
", Copyright (C) 2007-2008 OpenWrt.org\n\n");
|
2007-03-19 19:34:37 +02:00
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
decompress_init();
|
2007-03-19 19:34:37 +02:00
|
|
|
|
|
|
|
/* lzma args */
|
2007-12-14 22:35:22 +02:00
|
|
|
for (i = 0; i < LZMA_PROPERTIES_SIZE; i++)
|
|
|
|
props[i] = get_byte();
|
2007-03-19 19:34:37 +02:00
|
|
|
|
|
|
|
/* skip rest of the LZMA coder property */
|
|
|
|
/* read the lower half of uncompressed size in the header */
|
2007-12-14 22:35:22 +02:00
|
|
|
osize = ((SizeT)get_byte()) +
|
|
|
|
((SizeT)get_byte() << 8) +
|
|
|
|
((SizeT)get_byte() << 16) +
|
|
|
|
((SizeT)get_byte() << 24);
|
2007-03-19 19:34:37 +02:00
|
|
|
|
|
|
|
/* skip rest of the header (upper half of uncompressed size) */
|
2007-04-02 20:14:23 +03:00
|
|
|
for (i = 0; i < 4; i++)
|
2007-03-19 19:34:37 +02:00
|
|
|
get_byte();
|
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
res = LzmaDecodeProperties(&lzma_state.Properties, props,
|
|
|
|
LZMA_PROPERTIES_SIZE);
|
|
|
|
if (res != LZMA_RESULT_OK) {
|
|
|
|
printf("Incorrect LZMA stream properties!\n");
|
|
|
|
halt();
|
|
|
|
}
|
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("decompressing kernel... ");
|
2007-05-30 13:35:27 +03:00
|
|
|
|
2007-12-14 22:35:22 +02:00
|
|
|
lzma_state.Probs = (CProb *)workspace;
|
|
|
|
res = decompress_data(&lzma_state, (unsigned char *)LOADADDR, osize);
|
2007-07-15 18:02:38 +03:00
|
|
|
|
2007-05-30 13:35:27 +03:00
|
|
|
if (res != LZMA_RESULT_OK) {
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("failed, ");
|
2007-06-08 15:58:55 +03:00
|
|
|
switch (res) {
|
|
|
|
case LZMA_RESULT_DATA_ERROR:
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("data error!\n");
|
2007-06-08 15:58:55 +03:00
|
|
|
break;
|
|
|
|
default:
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("unknown error %d!\n", res);
|
2007-06-08 15:58:55 +03:00
|
|
|
}
|
2007-05-30 13:35:27 +03:00
|
|
|
halt();
|
2007-07-15 18:02:38 +03:00
|
|
|
} else
|
|
|
|
printf("done!\n");
|
2007-03-19 19:34:37 +02:00
|
|
|
|
2007-05-30 13:35:27 +03:00
|
|
|
blast_dcache(dcache_size, dcache_lsize);
|
|
|
|
blast_icache(icache_size, icache_lsize);
|
2007-03-19 19:34:37 +02:00
|
|
|
|
2007-07-15 18:02:38 +03:00
|
|
|
printf("launching kernel...\n\n");
|
2007-05-30 13:35:27 +03:00
|
|
|
|
2007-06-25 11:47:10 +03:00
|
|
|
#ifdef CONFIG_PASS_KARGS
|
|
|
|
reg_a0 = 0;
|
|
|
|
reg_a1 = 0;
|
|
|
|
reg_a2 = (unsigned long)env_vars;
|
|
|
|
reg_a3 = 0;
|
|
|
|
#endif
|
2007-05-30 13:35:27 +03:00
|
|
|
/* Jump to load address */
|
|
|
|
((kernel_entry) LOADADDR)(reg_a0, reg_a1, reg_a2, reg_a3);
|
2007-03-19 19:34:37 +02:00
|
|
|
}
|