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 | * |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 7 | * Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the |
| 8 | * LZMA SDK 4.42, which is written and distributed to public domain by Igor |
| 9 | * Pavlov. |
Stefan Reinauer | d6b4f1c | 2010-09-23 18:29:40 +0000 | [diff] [blame] | 10 | * |
| 11 | */ |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 12 | |
Patrick Georgi | 7994164 | 2009-04-24 16:44:34 +0000 | [diff] [blame] | 13 | #include <console/console.h> |
| 14 | #include <string.h> |
Myles Watson | 5817078 | 2009-10-28 16:13:28 +0000 | [diff] [blame] | 15 | #include <lib.h> |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 16 | #include <timestamp.h> |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 17 | |
Edward O'Callaghan | cd31afd | 2014-11-11 12:27:06 +1100 | [diff] [blame] | 18 | #include "lzmadecode.h" |
| 19 | |
Julius Werner | a25b5d2 | 2016-02-08 11:46:22 -0800 | [diff] [blame] | 20 | size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn) |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 21 | { |
| 22 | unsigned char properties[LZMA_PROPERTIES_SIZE]; |
Julius Werner | a25b5d2 | 2016-02-08 11:46:22 -0800 | [diff] [blame] | 23 | const int data_offset = LZMA_PROPERTIES_SIZE + 8; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 24 | UInt32 outSize; |
| 25 | SizeT inProcessed; |
| 26 | SizeT outProcessed; |
| 27 | int res; |
| 28 | CLzmaDecoderState state; |
| 29 | SizeT mallocneeds; |
Julius Werner | dbe0df1 | 2014-06-06 16:10:56 -0700 | [diff] [blame] | 30 | MAYBE_STATIC unsigned char scratchpad[15980]; |
Julius Werner | a25b5d2 | 2016-02-08 11:46:22 -0800 | [diff] [blame] | 31 | const unsigned char *cp; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 32 | |
| 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]; |
Julius Werner | a25b5d2 | 2016-02-08 11:46:22 -0800 | [diff] [blame] | 40 | if (LzmaDecodeProperties(&state.Properties, properties, |
| 41 | LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 42 | printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n"); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 43 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 44 | } |
| 45 | mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); |
| 46 | if (mallocneeds > 15980) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 47 | printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n"); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 48 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 49 | } |
| 50 | state.Probs = (CProb *)scratchpad; |
Julius Werner | a25b5d2 | 2016-02-08 11:46:22 -0800 | [diff] [blame] | 51 | res = LzmaDecode(&state, src + data_offset, srcn - data_offset, |
| 52 | &inProcessed, dst, outSize, &outProcessed); |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 53 | if (res != 0) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 54 | printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 55 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 56 | } |
Julius Werner | d808687 | 2015-06-25 23:22:36 -0700 | [diff] [blame] | 57 | return outProcessed; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 58 | } |