2010-04-27 08:56:47 +02:00
|
|
|
/*
|
2010-09-23 20:29:40 +02:00
|
|
|
* coreboot interface to memory-saving variant of LZMA decoder
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Carl-Daniel Hailfinger
|
|
|
|
* Released under the GNU GPL v2 or later
|
|
|
|
*
|
|
|
|
* Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the LZMA
|
|
|
|
* SDK 4.42, which is written and distributed to public domain by Igor Pavlov.
|
|
|
|
*
|
|
|
|
*/
|
2006-09-14 17:12:36 +02:00
|
|
|
|
2009-04-24 18:44:34 +02:00
|
|
|
#include <console/console.h>
|
|
|
|
#include <string.h>
|
2009-10-28 17:13:28 +01:00
|
|
|
#include <lib.h>
|
2006-09-14 17:12:36 +02:00
|
|
|
|
2014-11-11 02:27:06 +01:00
|
|
|
#include "lzmadecode.h"
|
|
|
|
|
2009-04-24 18:44:34 +02:00
|
|
|
unsigned long ulzma(unsigned char * src, unsigned char * dst)
|
2006-09-14 17:12:36 +02:00
|
|
|
{
|
|
|
|
unsigned char properties[LZMA_PROPERTIES_SIZE];
|
|
|
|
UInt32 outSize;
|
|
|
|
SizeT inProcessed;
|
|
|
|
SizeT outProcessed;
|
|
|
|
int res;
|
|
|
|
CLzmaDecoderState state;
|
|
|
|
SizeT mallocneeds;
|
2014-06-07 01:10:56 +02:00
|
|
|
MAYBE_STATIC unsigned char scratchpad[15980];
|
2013-01-31 05:14:46 +01:00
|
|
|
unsigned char *cp;
|
2006-09-14 17:12:36 +02:00
|
|
|
|
|
|
|
memcpy(properties, src, LZMA_PROPERTIES_SIZE);
|
2013-01-31 05:14:46 +01:00
|
|
|
/* The outSize in LZMA stream is a 64bit integer stored in little-endian
|
|
|
|
* (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
|
2013-07-10 05:51:14 +02:00
|
|
|
* unaligned memory address and to load in correct endianness, read each
|
|
|
|
* byte and re-construct. */
|
2013-01-31 05:14:46 +01:00
|
|
|
cp = src + LZMA_PROPERTIES_SIZE;
|
|
|
|
outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
|
2006-09-14 17:12:36 +02:00
|
|
|
if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
|
2008-08-27 23:53:11 +02:00
|
|
|
return 0;
|
2006-09-14 17:12:36 +02:00
|
|
|
}
|
|
|
|
mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
|
|
|
|
if (mallocneeds > 15980) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
|
2008-08-27 23:53:11 +02:00
|
|
|
return 0;
|
2006-09-14 17:12:36 +02:00
|
|
|
}
|
|
|
|
state.Probs = (CProb *)scratchpad;
|
|
|
|
res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed,
|
|
|
|
dst, outSize, &outProcessed);
|
|
|
|
if (res != 0) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
|
2008-08-27 23:53:11 +02:00
|
|
|
return 0;
|
2006-09-14 17:12:36 +02:00
|
|
|
}
|
|
|
|
return outSize;
|
|
|
|
}
|