1 /*
2 * Wrapper for decompressing LZ4-compressed kernel, initramfs, and initrd
3 *
4 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
5 *
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.
9 */
10
11 #ifdef STATIC
12 #define PREBOOT
13 #include "lz4/lz4_decompress.c"
14 #else
15 #include <linux/decompress/unlz4.h>
16 #endif
17 #include <linux/types.h>
18 #include <linux/lz4.h>
19 #include <linux/decompress/mm.h>
20 #include <linux/compiler.h>
21
22 #include <asm/unaligned.h>
23
24 /*
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.
30 */
31 #define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
32 #define ARCHIVE_MAGICNUMBER 0x184C2102
33
unlz4(u8 * input,long in_len,long (* fill)(void *,unsigned long),long (* flush)(void *,unsigned long),u8 * output,long * posp,void (* error)(char * x))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))
39 {
40 int ret = -1;
41 size_t chunksize = 0;
42 size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE;
43 u8 *inp;
44 u8 *inp_start;
45 u8 *outp;
46 long size = in_len;
47 #ifdef PREBOOT
48 size_t out_len = get_unaligned_le32(input + in_len);
49 #endif
50 size_t dest_len;
51
52
53 if (output) {
54 outp = output;
55 } else if (!flush) {
56 error("NULL output pointer and no flush function provided");
57 goto exit_0;
58 } else {
59 outp = large_malloc(uncomp_chunksize);
60 if (!outp) {
61 error("Could not allocate output buffer");
62 goto exit_0;
63 }
64 }
65
66 if (input && fill) {
67 error("Both input pointer and fill function provided,");
68 goto exit_1;
69 } else if (input) {
70 inp = input;
71 } else if (!fill) {
72 error("NULL input pointer and missing fill function");
73 goto exit_1;
74 } else {
75 inp = large_malloc(lz4_compressbound(uncomp_chunksize));
76 if (!inp) {
77 error("Could not allocate input buffer");
78 goto exit_1;
79 }
80 }
81 inp_start = inp;
82
83 if (posp)
84 *posp = 0;
85
86 if (fill) {
87 size = fill(inp, 4);
88 if (size < 4) {
89 error("data corrupted");
90 goto exit_2;
91 }
92 }
93
94 chunksize = get_unaligned_le32(inp);
95 if (chunksize == ARCHIVE_MAGICNUMBER) {
96 if (!fill) {
97 inp += 4;
98 size -= 4;
99 }
100 } else {
101 error("invalid header");
102 goto exit_2;
103 }
104
105 if (posp)
106 *posp += 4;
107
108 for (;;) {
109
110 if (fill) {
111 size = fill(inp, 4);
112 if (size == 0)
113 break;
114 if (size < 4) {
115 error("data corrupted");
116 goto exit_2;
117 }
118 } else if (size < 4) {
119 /* empty or end-of-file */
120 goto exit_3;
121 }
122
123 chunksize = get_unaligned_le32(inp);
124 if (chunksize == ARCHIVE_MAGICNUMBER) {
125 if (!fill) {
126 inp += 4;
127 size -= 4;
128 }
129 if (posp)
130 *posp += 4;
131 continue;
132 }
133
134 if (!fill && chunksize == 0) {
135 /* empty or end-of-file */
136 goto exit_3;
137 }
138
139 if (posp)
140 *posp += 4;
141
142 if (!fill) {
143 inp += 4;
144 size -= 4;
145 } else {
146 if (chunksize > lz4_compressbound(uncomp_chunksize)) {
147 error("chunk length is longer than allocated");
148 goto exit_2;
149 }
150 size = fill(inp, chunksize);
151 if (size < chunksize) {
152 error("data corrupted");
153 goto exit_2;
154 }
155 }
156 #ifdef PREBOOT
157 if (out_len >= uncomp_chunksize) {
158 dest_len = uncomp_chunksize;
159 out_len -= dest_len;
160 } else
161 dest_len = out_len;
162 ret = lz4_decompress(inp, &chunksize, outp, dest_len);
163 #else
164 dest_len = uncomp_chunksize;
165 ret = lz4_decompress_unknownoutputsize(inp, chunksize, outp,
166 &dest_len);
167 #endif
168 if (ret < 0) {
169 error("Decoding failed");
170 goto exit_2;
171 }
172
173 ret = -1;
174 if (flush && flush(outp, dest_len) != dest_len)
175 goto exit_2;
176 if (output)
177 outp += dest_len;
178 if (posp)
179 *posp += chunksize;
180
181 if (!fill) {
182 size -= chunksize;
183
184 if (size == 0)
185 break;
186 else if (size < 0) {
187 error("data corrupted");
188 goto exit_2;
189 }
190 inp += chunksize;
191 }
192 }
193
194 exit_3:
195 ret = 0;
196 exit_2:
197 if (!input)
198 large_free(inp_start);
199 exit_1:
200 if (!output)
201 large_free(outp);
202 exit_0:
203 return ret;
204 }
205
206 #ifdef PREBOOT
__decompress(unsigned char * buf,long in_len,long (* fill)(void *,unsigned long),long (* flush)(void *,unsigned long),unsigned char * output,long out_len,long * posp,void (* error)(char * x))207 STATIC int INIT __decompress(unsigned char *buf, long in_len,
208 long (*fill)(void*, unsigned long),
209 long (*flush)(void*, unsigned long),
210 unsigned char *output, long out_len,
211 long *posp,
212 void (*error)(char *x)
213 )
214 {
215 return unlz4(buf, in_len - 4, fill, flush, output, posp, error);
216 }
217 #endif
218