2 * Wrapper for decompressing LZ4-compressed kernel, initramfs, and initrd
4 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 #include "lz4/lz4_decompress.c"
15 #include <linux/decompress/unlz4.h>
17 #include <linux/types.h>
18 #include <linux/lz4.h>
19 #include <linux/decompress/mm.h>
20 #include <linux/compiler.h>
22 #include <asm/unaligned.h>
25 * Note: Uncompressed chunk size is used in the compressor side
26 * (userspace side for compression).
27 * It is hardcoded because there is not proper way to extract it
28 * from the binary stream which is generated by the preliminary
29 * version of LZ4 tool so far.
31 #define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
32 #define ARCHIVE_MAGICNUMBER 0x184C2102
34 STATIC
inline int INIT
unlz4(u8
*input
, long in_len
,
35 long (*fill
)(void *, unsigned long),
36 long (*flush
)(void *, unsigned long),
37 u8
*output
, long *posp
,
38 void (*error
) (char *x
))
42 size_t uncomp_chunksize
= LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE
;
48 size_t out_len
= get_unaligned_le32(input
+ in_len
);
56 error("NULL output pointer and no flush function provided");
59 outp
= large_malloc(uncomp_chunksize
);
61 error("Could not allocate output buffer");
67 error("Both input pointer and fill function provided,");
72 error("NULL input pointer and missing fill function");
75 inp
= large_malloc(lz4_compressbound(uncomp_chunksize
));
77 error("Could not allocate input buffer");
89 error("data corrupted");
94 chunksize
= get_unaligned_le32(inp
);
95 if (chunksize
== ARCHIVE_MAGICNUMBER
) {
101 error("invalid header");
115 error("data corrupted");
120 chunksize
= get_unaligned_le32(inp
);
121 if (chunksize
== ARCHIVE_MAGICNUMBER
) {
139 if (chunksize
> lz4_compressbound(uncomp_chunksize
)) {
140 error("chunk length is longer than allocated");
143 size
= fill(inp
, chunksize
);
144 if (size
< chunksize
) {
145 error("data corrupted");
150 if (out_len
>= uncomp_chunksize
) {
151 dest_len
= uncomp_chunksize
;
155 ret
= lz4_decompress(inp
, &chunksize
, outp
, dest_len
);
157 dest_len
= uncomp_chunksize
;
158 ret
= lz4_decompress_unknownoutputsize(inp
, chunksize
, outp
,
162 error("Decoding failed");
167 if (flush
&& flush(outp
, dest_len
) != dest_len
)
180 error("data corrupted");
190 large_free(inp_start
);
199 STATIC
int INIT
__decompress(unsigned char *buf
, long in_len
,
200 long (*fill
)(void*, unsigned long),
201 long (*flush
)(void*, unsigned long),
202 unsigned char *output
, long out_len
,
204 void (*error
)(char *x
)
207 return unlz4(buf
, in_len
- 4, fill
, flush
, output
, posp
, error
);