blob: c0cfcfd486be00a5887b930b967c3d61b6f0a0b6 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Kyungsik Leee76e1fd2013-07-08 16:01:46 -07002/*
3 * Wrapper for decompressing LZ4-compressed kernel, initramfs, and initrd
4 *
5 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
Kyungsik Leee76e1fd2013-07-08 16:01:46 -07006 */
7
8#ifdef STATIC
9#define PREBOOT
10#include "lz4/lz4_decompress.c"
11#else
12#include <linux/decompress/unlz4.h>
13#endif
14#include <linux/types.h>
15#include <linux/lz4.h>
16#include <linux/decompress/mm.h>
17#include <linux/compiler.h>
18
19#include <asm/unaligned.h>
20
21/*
22 * Note: Uncompressed chunk size is used in the compressor side
23 * (userspace side for compression).
24 * It is hardcoded because there is not proper way to extract it
25 * from the binary stream which is generated by the preliminary
26 * version of LZ4 tool so far.
27 */
28#define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
29#define ARCHIVE_MAGICNUMBER 0x184C2102
30
Yinghai Lud97b07c2014-08-08 14:23:14 -070031STATIC inline int INIT unlz4(u8 *input, long in_len,
32 long (*fill)(void *, unsigned long),
33 long (*flush)(void *, unsigned long),
34 u8 *output, long *posp,
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070035 void (*error) (char *x))
36{
37 int ret = -1;
38 size_t chunksize = 0;
39 size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE;
40 u8 *inp;
41 u8 *inp_start;
42 u8 *outp;
Yinghai Lud97b07c2014-08-08 14:23:14 -070043 long size = in_len;
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070044#ifdef PREBOOT
45 size_t out_len = get_unaligned_le32(input + in_len);
46#endif
47 size_t dest_len;
48
49
50 if (output) {
51 outp = output;
52 } else if (!flush) {
53 error("NULL output pointer and no flush function provided");
54 goto exit_0;
55 } else {
56 outp = large_malloc(uncomp_chunksize);
57 if (!outp) {
58 error("Could not allocate output buffer");
59 goto exit_0;
60 }
61 }
62
63 if (input && fill) {
64 error("Both input pointer and fill function provided,");
65 goto exit_1;
66 } else if (input) {
67 inp = input;
68 } else if (!fill) {
69 error("NULL input pointer and missing fill function");
70 goto exit_1;
71 } else {
Sven Schmidte23d54e2017-02-24 15:01:16 -080072 inp = large_malloc(LZ4_compressBound(uncomp_chunksize));
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070073 if (!inp) {
74 error("Could not allocate input buffer");
75 goto exit_1;
76 }
77 }
78 inp_start = inp;
79
80 if (posp)
81 *posp = 0;
82
Yinghai Lu4d4b8662014-08-08 14:23:10 -070083 if (fill) {
84 size = fill(inp, 4);
85 if (size < 4) {
86 error("data corrupted");
87 goto exit_2;
88 }
89 }
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070090
91 chunksize = get_unaligned_le32(inp);
92 if (chunksize == ARCHIVE_MAGICNUMBER) {
Yinghai Lu4d4b8662014-08-08 14:23:10 -070093 if (!fill) {
94 inp += 4;
95 size -= 4;
96 }
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070097 } else {
98 error("invalid header");
99 goto exit_2;
100 }
101
102 if (posp)
103 *posp += 4;
104
105 for (;;) {
106
Yinghai Lu4d4b8662014-08-08 14:23:10 -0700107 if (fill) {
108 size = fill(inp, 4);
109 if (size == 0)
110 break;
111 if (size < 4) {
112 error("data corrupted");
113 goto exit_2;
114 }
115 }
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700116
117 chunksize = get_unaligned_le32(inp);
118 if (chunksize == ARCHIVE_MAGICNUMBER) {
Yinghai Lu4d4b8662014-08-08 14:23:10 -0700119 if (!fill) {
120 inp += 4;
121 size -= 4;
122 }
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700123 if (posp)
124 *posp += 4;
125 continue;
126 }
Yinghai Lu4d4b8662014-08-08 14:23:10 -0700127
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700128
129 if (posp)
130 *posp += 4;
131
Yinghai Lu4d4b8662014-08-08 14:23:10 -0700132 if (!fill) {
133 inp += 4;
134 size -= 4;
135 } else {
Sven Schmidte23d54e2017-02-24 15:01:16 -0800136 if (chunksize > LZ4_compressBound(uncomp_chunksize)) {
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700137 error("chunk length is longer than allocated");
138 goto exit_2;
139 }
Yinghai Lu4d4b8662014-08-08 14:23:10 -0700140 size = fill(inp, chunksize);
141 if (size < chunksize) {
142 error("data corrupted");
143 goto exit_2;
144 }
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700145 }
146#ifdef PREBOOT
147 if (out_len >= uncomp_chunksize) {
148 dest_len = uncomp_chunksize;
149 out_len -= dest_len;
150 } else
151 dest_len = out_len;
Sven Schmidte23d54e2017-02-24 15:01:16 -0800152
153 ret = LZ4_decompress_fast(inp, outp, dest_len);
154 chunksize = ret;
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700155#else
156 dest_len = uncomp_chunksize;
Sven Schmidte23d54e2017-02-24 15:01:16 -0800157
158 ret = LZ4_decompress_safe(inp, outp, chunksize, dest_len);
159 dest_len = ret;
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700160#endif
161 if (ret < 0) {
162 error("Decoding failed");
163 goto exit_2;
164 }
165
Jan Beulich2a1d6892014-01-23 15:56:20 -0800166 ret = -1;
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700167 if (flush && flush(outp, dest_len) != dest_len)
168 goto exit_2;
169 if (output)
170 outp += dest_len;
171 if (posp)
172 *posp += chunksize;
173
Yinghai Lu4d4b8662014-08-08 14:23:10 -0700174 if (!fill) {
175 size -= chunksize;
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700176
Yinghai Lu4d4b8662014-08-08 14:23:10 -0700177 if (size == 0)
178 break;
179 else if (size < 0) {
180 error("data corrupted");
181 goto exit_2;
182 }
183 inp += chunksize;
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700184 }
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700185 }
186
187 ret = 0;
188exit_2:
189 if (!input)
190 large_free(inp_start);
191exit_1:
192 if (!output)
193 large_free(outp);
194exit_0:
195 return ret;
196}
197
198#ifdef PREBOOT
Yinghai Lu2d3862d2015-09-09 15:39:12 -0700199STATIC int INIT __decompress(unsigned char *buf, long in_len,
Yinghai Lud97b07c2014-08-08 14:23:14 -0700200 long (*fill)(void*, unsigned long),
201 long (*flush)(void*, unsigned long),
Yinghai Lu2d3862d2015-09-09 15:39:12 -0700202 unsigned char *output, long out_len,
Yinghai Lud97b07c2014-08-08 14:23:14 -0700203 long *posp,
Yinghai Lu2d3862d2015-09-09 15:39:12 -0700204 void (*error)(char *x)
Kyungsik Leee76e1fd2013-07-08 16:01:46 -0700205 )
206{
207 return unlz4(buf, in_len - 4, fill, flush, output, posp, error);
208}
209#endif