Martin Roth | fb8876d | 2022-08-07 15:12:12 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 3 | /* |
Stefan Reinauer | d6b4f1c | 2010-09-23 18:29:40 +0000 | [diff] [blame] | 4 | * coreboot interface to memory-saving variant of LZMA decoder |
| 5 | * |
| 6 | * Copyright (C) 2006 Carl-Daniel Hailfinger |
Stefan Reinauer | d6b4f1c | 2010-09-23 18:29:40 +0000 | [diff] [blame] | 7 | * |
Lee Leahy | 7340217 | 2017-03-10 15:23:24 -0800 | [diff] [blame] | 8 | * Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the |
| 9 | * LZMA SDK 4.42, which is written and distributed to public domain by Igor |
| 10 | * Pavlov. |
Stefan Reinauer | d6b4f1c | 2010-09-23 18:29:40 +0000 | [diff] [blame] | 11 | * |
| 12 | */ |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 13 | |
Patrick Georgi | 7994164 | 2009-04-24 16:44:34 +0000 | [diff] [blame] | 14 | #include <console/console.h> |
| 15 | #include <string.h> |
Myles Watson | 5817078 | 2009-10-28 16:13:28 +0000 | [diff] [blame] | 16 | #include <lib.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; |
Kyösti Mälkki | fcbbb91 | 2020-04-20 10:21:39 +0300 | [diff] [blame] | 30 | 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 | |
Alex Rebert | 41de2a0 | 2020-02-22 18:13:39 -0500 | [diff] [blame] | 33 | if (srcn < data_offset) { |
| 34 | printk(BIOS_WARNING, "lzma: Input too small.\n"); |
| 35 | return 0; |
| 36 | } |
| 37 | |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 38 | memcpy(properties, src, LZMA_PROPERTIES_SIZE); |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 39 | /* The outSize in LZMA stream is a 64bit integer stored in little-endian |
| 40 | * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 41 | * unaligned memory address and to load in correct endianness, read each |
| 42 | * byte and re-construct. */ |
Hung-Te Lin | d51557a | 2013-01-31 12:14:46 +0800 | [diff] [blame] | 43 | cp = src + LZMA_PROPERTIES_SIZE; |
| 44 | outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0]; |
Patrick Rudolph | f595ba2 | 2018-03-16 12:25:28 +0100 | [diff] [blame] | 45 | if (outSize > dstn) |
| 46 | outSize = dstn; |
Julius Werner | a25b5d2 | 2016-02-08 11:46:22 -0800 | [diff] [blame] | 47 | if (LzmaDecodeProperties(&state.Properties, properties, |
| 48 | LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 49 | printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n"); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 50 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 51 | } |
| 52 | mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); |
| 53 | if (mallocneeds > 15980) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 54 | printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n"); |
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 | } |
| 57 | state.Probs = (CProb *)scratchpad; |
Julius Werner | a25b5d2 | 2016-02-08 11:46:22 -0800 | [diff] [blame] | 58 | res = LzmaDecode(&state, src + data_offset, srcn - data_offset, |
| 59 | &inProcessed, dst, outSize, &outProcessed); |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 60 | if (res != 0) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 61 | printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res); |
Ward Vandewege | ec2bd53 | 2008-08-27 21:53:11 +0000 | [diff] [blame] | 62 | return 0; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 63 | } |
Julius Werner | d808687 | 2015-06-25 23:22:36 -0700 | [diff] [blame] | 64 | return outProcessed; |
Carl-Daniel Hailfinger | cba07dd | 2006-09-14 15:12:36 +0000 | [diff] [blame] | 65 | } |