Blame view

lib/decompress_unlz4.c 4.02 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
e76e1fdfa   Kyungsik Lee   lib: add support ...
2
3
4
5
  /*
   * Wrapper for decompressing LZ4-compressed kernel, initramfs, and initrd
   *
   * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
e76e1fdfa   Kyungsik Lee   lib: add support ...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   */
  
  #ifdef STATIC
  #define PREBOOT
  #include "lz4/lz4_decompress.c"
  #else
  #include <linux/decompress/unlz4.h>
  #endif
  #include <linux/types.h>
  #include <linux/lz4.h>
  #include <linux/decompress/mm.h>
  #include <linux/compiler.h>
  
  #include <asm/unaligned.h>
  
  /*
   * Note: Uncompressed chunk size is used in the compressor side
   * (userspace side for compression).
   * It is hardcoded because there is not proper way to extract it
   * from the binary stream which is generated by the preliminary
   * version of LZ4 tool so far.
   */
  #define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
  #define ARCHIVE_MAGICNUMBER 0x184C2102
d97b07c54   Yinghai Lu   initramfs: suppor...
30
31
32
33
  STATIC inline int INIT unlz4(u8 *input, long in_len,
  				long (*fill)(void *, unsigned long),
  				long (*flush)(void *, unsigned long),
  				u8 *output, long *posp,
e76e1fdfa   Kyungsik Lee   lib: add support ...
34
35
36
37
38
39
40
41
  				void (*error) (char *x))
  {
  	int ret = -1;
  	size_t chunksize = 0;
  	size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE;
  	u8 *inp;
  	u8 *inp_start;
  	u8 *outp;
d97b07c54   Yinghai Lu   initramfs: suppor...
42
  	long size = in_len;
e76e1fdfa   Kyungsik Lee   lib: add support ...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #ifdef PREBOOT
  	size_t out_len = get_unaligned_le32(input + in_len);
  #endif
  	size_t dest_len;
  
  
  	if (output) {
  		outp = output;
  	} else if (!flush) {
  		error("NULL output pointer and no flush function provided");
  		goto exit_0;
  	} else {
  		outp = large_malloc(uncomp_chunksize);
  		if (!outp) {
  			error("Could not allocate output buffer");
  			goto exit_0;
  		}
  	}
  
  	if (input && fill) {
  		error("Both input pointer and fill function provided,");
  		goto exit_1;
  	} else if (input) {
  		inp = input;
  	} else if (!fill) {
  		error("NULL input pointer and missing fill function");
  		goto exit_1;
  	} else {
e23d54e48   Sven Schmidt   lib/decompress_un...
71
  		inp = large_malloc(LZ4_compressBound(uncomp_chunksize));
e76e1fdfa   Kyungsik Lee   lib: add support ...
72
73
74
75
76
77
78
79
80
  		if (!inp) {
  			error("Could not allocate input buffer");
  			goto exit_1;
  		}
  	}
  	inp_start = inp;
  
  	if (posp)
  		*posp = 0;
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
81
82
83
84
85
86
87
  	if (fill) {
  		size = fill(inp, 4);
  		if (size < 4) {
  			error("data corrupted");
  			goto exit_2;
  		}
  	}
e76e1fdfa   Kyungsik Lee   lib: add support ...
88
89
90
  
  	chunksize = get_unaligned_le32(inp);
  	if (chunksize == ARCHIVE_MAGICNUMBER) {
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
91
92
93
94
  		if (!fill) {
  			inp += 4;
  			size -= 4;
  		}
e76e1fdfa   Kyungsik Lee   lib: add support ...
95
96
97
98
99
100
101
102
103
  	} else {
  		error("invalid header");
  		goto exit_2;
  	}
  
  	if (posp)
  		*posp += 4;
  
  	for (;;) {
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
104
105
106
107
108
109
110
111
112
  		if (fill) {
  			size = fill(inp, 4);
  			if (size == 0)
  				break;
  			if (size < 4) {
  				error("data corrupted");
  				goto exit_2;
  			}
  		}
e76e1fdfa   Kyungsik Lee   lib: add support ...
113
114
115
  
  		chunksize = get_unaligned_le32(inp);
  		if (chunksize == ARCHIVE_MAGICNUMBER) {
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
116
117
118
119
  			if (!fill) {
  				inp += 4;
  				size -= 4;
  			}
e76e1fdfa   Kyungsik Lee   lib: add support ...
120
121
122
123
  			if (posp)
  				*posp += 4;
  			continue;
  		}
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
124

e76e1fdfa   Kyungsik Lee   lib: add support ...
125
126
127
  
  		if (posp)
  			*posp += 4;
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
128
129
130
131
  		if (!fill) {
  			inp += 4;
  			size -= 4;
  		} else {
e23d54e48   Sven Schmidt   lib/decompress_un...
132
  			if (chunksize > LZ4_compressBound(uncomp_chunksize)) {
e76e1fdfa   Kyungsik Lee   lib: add support ...
133
134
135
  				error("chunk length is longer than allocated");
  				goto exit_2;
  			}
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
136
137
138
139
140
  			size = fill(inp, chunksize);
  			if (size < chunksize) {
  				error("data corrupted");
  				goto exit_2;
  			}
e76e1fdfa   Kyungsik Lee   lib: add support ...
141
142
143
144
145
146
147
  		}
  #ifdef PREBOOT
  		if (out_len >= uncomp_chunksize) {
  			dest_len = uncomp_chunksize;
  			out_len -= dest_len;
  		} else
  			dest_len = out_len;
e23d54e48   Sven Schmidt   lib/decompress_un...
148
149
150
  
  		ret = LZ4_decompress_fast(inp, outp, dest_len);
  		chunksize = ret;
e76e1fdfa   Kyungsik Lee   lib: add support ...
151
152
  #else
  		dest_len = uncomp_chunksize;
e23d54e48   Sven Schmidt   lib/decompress_un...
153
154
155
  
  		ret = LZ4_decompress_safe(inp, outp, chunksize, dest_len);
  		dest_len = ret;
e76e1fdfa   Kyungsik Lee   lib: add support ...
156
157
158
159
160
  #endif
  		if (ret < 0) {
  			error("Decoding failed");
  			goto exit_2;
  		}
2a1d689c9   Jan Beulich   lib/decompress_un...
161
  		ret = -1;
e76e1fdfa   Kyungsik Lee   lib: add support ...
162
163
164
165
166
167
  		if (flush && flush(outp, dest_len) != dest_len)
  			goto exit_2;
  		if (output)
  			outp += dest_len;
  		if (posp)
  			*posp += chunksize;
4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
168
169
  		if (!fill) {
  			size -= chunksize;
e76e1fdfa   Kyungsik Lee   lib: add support ...
170

4d4b866ae   Yinghai Lu   initrd: fix lz4 d...
171
172
173
174
175
176
177
  			if (size == 0)
  				break;
  			else if (size < 0) {
  				error("data corrupted");
  				goto exit_2;
  			}
  			inp += chunksize;
e76e1fdfa   Kyungsik Lee   lib: add support ...
178
  		}
e76e1fdfa   Kyungsik Lee   lib: add support ...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  	}
  
  	ret = 0;
  exit_2:
  	if (!input)
  		large_free(inp_start);
  exit_1:
  	if (!output)
  		large_free(outp);
  exit_0:
  	return ret;
  }
  
  #ifdef PREBOOT
2d3862d26   Yinghai Lu   lib/decompressors...
193
  STATIC int INIT __decompress(unsigned char *buf, long in_len,
d97b07c54   Yinghai Lu   initramfs: suppor...
194
195
  			      long (*fill)(void*, unsigned long),
  			      long (*flush)(void*, unsigned long),
2d3862d26   Yinghai Lu   lib/decompressors...
196
  			      unsigned char *output, long out_len,
d97b07c54   Yinghai Lu   initramfs: suppor...
197
  			      long *posp,
2d3862d26   Yinghai Lu   lib/decompressors...
198
  			      void (*error)(char *x)
e76e1fdfa   Kyungsik Lee   lib: add support ...
199
200
201
202
203
  	)
  {
  	return unlz4(buf, in_len - 4, fill, flush, output, posp, error);
  }
  #endif