2 * coreboot interface to memory-saving variant of LZMA decoder
4 * Copyright (C) 2006 Carl-Daniel Hailfinger
5 * Released under the GNU GPL v2 or later
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.
12 #include <console/console.h>
15 #include <timestamp.h>
17 #include "lzmadecode.h"
19 size_t ulzman(const void *src
, size_t srcn
, void *dst
, size_t dstn
)
21 unsigned char properties
[LZMA_PROPERTIES_SIZE
];
22 const int data_offset
= LZMA_PROPERTIES_SIZE
+ 8;
27 CLzmaDecoderState state
;
29 MAYBE_STATIC
unsigned char scratchpad
[15980];
30 const unsigned char *cp
;
32 memcpy(properties
, src
, LZMA_PROPERTIES_SIZE
);
33 /* The outSize in LZMA stream is a 64bit integer stored in little-endian
34 * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
35 * unaligned memory address and to load in correct endianness, read each
36 * byte and re-construct. */
37 cp
= src
+ LZMA_PROPERTIES_SIZE
;
38 outSize
= cp
[3] << 24 | cp
[2] << 16 | cp
[1] << 8 | cp
[0];
39 if (LzmaDecodeProperties(&state
.Properties
, properties
,
40 LZMA_PROPERTIES_SIZE
) != LZMA_RESULT_OK
) {
41 printk(BIOS_WARNING
, "lzma: Incorrect stream properties.\n");
44 mallocneeds
= (LzmaGetNumProbs(&state
.Properties
) * sizeof(CProb
));
45 if (mallocneeds
> 15980) {
46 printk(BIOS_WARNING
, "lzma: Decoder scratchpad too small!\n");
49 state
.Probs
= (CProb
*)scratchpad
;
50 res
= LzmaDecode(&state
, src
+ data_offset
, srcn
- data_offset
,
51 &inProcessed
, dst
, outSize
, &outProcessed
);
53 printk(BIOS_WARNING
, "lzma: Decoding error = %d\n", res
);