blob: cf52002166690113ccb02f1d881c81f25aa5c0ce [file] [log] [blame]
Martin Rothfb8876d2022-08-07 15:12:12 -06001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
Stefan Reinauer14e22772010-04-27 06:56:47 +00003/*
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +00004 * coreboot interface to memory-saving variant of LZMA decoder
5 *
6 * Copyright (C) 2006 Carl-Daniel Hailfinger
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +00007 *
Lee Leahy73402172017-03-10 15:23:24 -08008 * 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 Reinauerd6b4f1c2010-09-23 18:29:40 +000011 *
12 */
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000013
Patrick Georgi79941642009-04-24 16:44:34 +000014#include <console/console.h>
15#include <string.h>
Myles Watson58170782009-10-28 16:13:28 +000016#include <lib.h>
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000017
Edward O'Callaghancd31afd2014-11-11 12:27:06 +110018#include "lzmadecode.h"
19
Julius Wernera25b5d22016-02-08 11:46:22 -080020size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000021{
22 unsigned char properties[LZMA_PROPERTIES_SIZE];
Julius Wernera25b5d22016-02-08 11:46:22 -080023 const int data_offset = LZMA_PROPERTIES_SIZE + 8;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000024 UInt32 outSize;
25 SizeT inProcessed;
26 SizeT outProcessed;
27 int res;
28 CLzmaDecoderState state;
29 SizeT mallocneeds;
Kyösti Mälkkifcbbb912020-04-20 10:21:39 +030030 static unsigned char scratchpad[15980];
Julius Wernera25b5d22016-02-08 11:46:22 -080031 const unsigned char *cp;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000032
Alex Rebert41de2a02020-02-22 18:13:39 -050033 if (srcn < data_offset) {
34 printk(BIOS_WARNING, "lzma: Input too small.\n");
35 return 0;
36 }
37
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000038 memcpy(properties, src, LZMA_PROPERTIES_SIZE);
Hung-Te Lind51557a2013-01-31 12:14:46 +080039 /* 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 Rothcbf2bd72013-07-09 21:51:14 -060041 * unaligned memory address and to load in correct endianness, read each
42 * byte and re-construct. */
Hung-Te Lind51557a2013-01-31 12:14:46 +080043 cp = src + LZMA_PROPERTIES_SIZE;
44 outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
Patrick Rudolphf595ba22018-03-16 12:25:28 +010045 if (outSize > dstn)
46 outSize = dstn;
Julius Wernera25b5d22016-02-08 11:46:22 -080047 if (LzmaDecodeProperties(&state.Properties, properties,
48 LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000049 printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
Ward Vandewegeec2bd532008-08-27 21:53:11 +000050 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000051 }
52 mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
53 if (mallocneeds > 15980) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000054 printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
Ward Vandewegeec2bd532008-08-27 21:53:11 +000055 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000056 }
57 state.Probs = (CProb *)scratchpad;
Julius Wernera25b5d22016-02-08 11:46:22 -080058 res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
59 &inProcessed, dst, outSize, &outProcessed);
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000060 if (res != 0) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000061 printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
Ward Vandewegeec2bd532008-08-27 21:53:11 +000062 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000063 }
Julius Wernerd8086872015-06-25 23:22:36 -070064 return outProcessed;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000065}