blob: 8efa1e6e1abed7ec7712d15de0f2b18d18f651f1 [file] [log] [blame]
Stefan Reinauer14e22772010-04-27 06:56:47 +00001/*
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +00002 * coreboot interface to memory-saving variant of LZMA decoder
3 *
4 * Copyright (C) 2006 Carl-Daniel Hailfinger
5 * Released under the GNU GPL v2 or later
6 *
7 * Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the LZMA
8 * SDK 4.42, which is written and distributed to public domain by Igor Pavlov.
9 *
10 */
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000011
Patrick Georgi79941642009-04-24 16:44:34 +000012#include <console/console.h>
13#include <string.h>
Myles Watson58170782009-10-28 16:13:28 +000014#include <lib.h>
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000015
Edward O'Callaghancd31afd2014-11-11 12:27:06 +110016#include "lzmadecode.h"
17
Patrick Georgi79941642009-04-24 16:44:34 +000018unsigned long ulzma(unsigned char * src, unsigned char * dst)
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000019{
20 unsigned char properties[LZMA_PROPERTIES_SIZE];
21 UInt32 outSize;
22 SizeT inProcessed;
23 SizeT outProcessed;
24 int res;
25 CLzmaDecoderState state;
26 SizeT mallocneeds;
Julius Wernerdbe0df12014-06-06 16:10:56 -070027 MAYBE_STATIC unsigned char scratchpad[15980];
Hung-Te Lind51557a2013-01-31 12:14:46 +080028 unsigned char *cp;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000029
30 memcpy(properties, src, LZMA_PROPERTIES_SIZE);
Hung-Te Lind51557a2013-01-31 12:14:46 +080031 /* The outSize in LZMA stream is a 64bit integer stored in little-endian
32 * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
Martin Rothcbf2bd72013-07-09 21:51:14 -060033 * unaligned memory address and to load in correct endianness, read each
34 * byte and re-construct. */
Hung-Te Lind51557a2013-01-31 12:14:46 +080035 cp = src + LZMA_PROPERTIES_SIZE;
36 outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000037 if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000038 printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
Ward Vandewegeec2bd532008-08-27 21:53:11 +000039 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000040 }
41 mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
42 if (mallocneeds > 15980) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000043 printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
Ward Vandewegeec2bd532008-08-27 21:53:11 +000044 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000045 }
46 state.Probs = (CProb *)scratchpad;
47 res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed,
48 dst, outSize, &outProcessed);
49 if (res != 0) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000050 printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
Ward Vandewegeec2bd532008-08-27 21:53:11 +000051 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000052 }
53 return outSize;
54}