Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | d6b4f1c | 2010-09-23 18:29:40 +0000 | [diff] [blame] | 2 | * 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 Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 11 | |
Patrick Georgi | 7994164 | 2009-04-24 16:44:34 +0000 | [diff] [blame] | 12 | #include <console/console.h> |
| 13 | #include <string.h> |
Myles Watson | 5817078 | 2009-10-28 16:13:28 +0000 | [diff] [blame] | 14 | #include <lib.h> |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 15 | |
Edward O'Callaghan | cd31afd | 2014-11-11 12:27:06 +1100 | [diff] [blame] | 16 | #include "lzmadecode.h" |
| 17 | |
Patrick Georgi | 7994164 | 2009-04-24 16:44:34 +0000 | [diff] [blame] | 18 | unsigned long ulzma(unsigned char * src, unsigned char * dst) |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 19 | { |
| 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 Werner | dbe0df1 | 2014-06-06 16:10:56 -0700 | [diff] [blame^] | 27 | MAYBE_STATIC unsigned char scratchpad[15980]; |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 28 | unsigned char *cp; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 29 | |
| 30 | memcpy(properties, src, LZMA_PROPERTIES_SIZE); |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 31 | /* 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 Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 33 | * unaligned memory address and to load in correct endianness, read each |
| 34 | * byte and re-construct. */ |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 35 | cp = src + LZMA_PROPERTIES_SIZE; |
| 36 | outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0]; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 37 | if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 38 | printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n"); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 39 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 40 | } |
| 41 | mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); |
| 42 | if (mallocneeds > 15980) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 43 | printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n"); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 44 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 45 | } |
| 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 Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 50 | printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 51 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 52 | } |
| 53 | return outSize; |
| 54 | } |