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> |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 15 | #include <timestamp.h> |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 16 | |
Edward O'Callaghan | cd31afd | 2014-11-11 12:27:06 +1100 | [diff] [blame] | 17 | #include "lzmadecode.h" |
| 18 | |
Patrick Georgi | 7994164 | 2009-04-24 16:44:34 +0000 | [diff] [blame] | 19 | unsigned long ulzma(unsigned char * src, unsigned char * dst) |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 20 | { |
| 21 | unsigned char properties[LZMA_PROPERTIES_SIZE]; |
| 22 | UInt32 outSize; |
| 23 | SizeT inProcessed; |
| 24 | SizeT outProcessed; |
| 25 | int res; |
| 26 | CLzmaDecoderState state; |
| 27 | SizeT mallocneeds; |
Julius Werner | dbe0df1 | 2014-06-06 16:10:56 -0700 | [diff] [blame] | 28 | MAYBE_STATIC unsigned char scratchpad[15980]; |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 29 | unsigned char *cp; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 30 | |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 31 | /* Note: these timestamps aren't useful for memory-mapped media (x86) */ |
| 32 | timestamp_add_now(TS_START_ULZMA); |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 33 | memcpy(properties, src, LZMA_PROPERTIES_SIZE); |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 34 | /* The outSize in LZMA stream is a 64bit integer stored in little-endian |
| 35 | * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 36 | * unaligned memory address and to load in correct endianness, read each |
| 37 | * byte and re-construct. */ |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 38 | cp = src + LZMA_PROPERTIES_SIZE; |
| 39 | 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] | 40 | if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 41 | printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n"); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 42 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 43 | } |
| 44 | mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); |
| 45 | if (mallocneeds > 15980) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 46 | printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n"); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 47 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 48 | } |
| 49 | state.Probs = (CProb *)scratchpad; |
| 50 | res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed, |
| 51 | dst, outSize, &outProcessed); |
| 52 | if (res != 0) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 53 | printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 54 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 55 | } |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 56 | timestamp_add_now(TS_END_ULZMA); |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 57 | return outSize; |
| 58 | } |