replace our arm LZMA loader with the one that goes upstream
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.31 / 050-lzo_compressed_kernels.patch
1 diff --git a/include/linux/decompress/unlzo.h b/include/linux/decompress/unlzo.h
2 new file mode 100644
3 index 0000000..9872297
4 --- /dev/null
5 +++ b/include/linux/decompress/unlzo.h
6 @@ -0,0 +1,10 @@
7 +#ifndef DECOMPRESS_UNLZO_H
8 +#define DECOMPRESS_UNLZO_H
9 +
10 +int unlzo(unsigned char *inbuf, int len,
11 +       int(*fill)(void*, unsigned int),
12 +       int(*flush)(void*, unsigned int),
13 +       unsigned char *output,
14 +       int *pos,
15 +       void(*error)(char *x));
16 +#endif
17 diff --git a/init/Kconfig b/init/Kconfig
18 index f515864..eb65318 100644
19 --- a/init/Kconfig
20 +++ b/init/Kconfig
21 @@ -115,10 +115,13 @@ config HAVE_KERNEL_BZIP2
22  config HAVE_KERNEL_LZMA
23         bool
24  
25 +config HAVE_KERNEL_LZO
26 +       bool
27 +
28  choice
29         prompt "Kernel compression mode"
30         default KERNEL_GZIP
31 -       depends on HAVE_KERNEL_GZIP || HAVE_KERNEL_BZIP2 || HAVE_KERNEL_LZMA
32 +       depends on HAVE_KERNEL_GZIP || HAVE_KERNEL_BZIP2 || HAVE_KERNEL_LZMA || HAVE_KERNEL_LZO
33         help
34           The linux kernel is a kind of self-extracting executable.
35           Several compression algorithms are available, which differ
36 @@ -141,9 +144,8 @@ config KERNEL_GZIP
37         bool "Gzip"
38         depends on HAVE_KERNEL_GZIP
39         help
40 -         The old and tried gzip compression. Its compression ratio is
41 -         the poorest among the 3 choices; however its speed (both
42 -         compression and decompression) is the fastest.
43 +         The old and tried gzip compression. It provides a good balance
44 +         between compression ratio and decompression speed.
45  
46  config KERNEL_BZIP2
47         bool "Bzip2"
48 @@ -164,6 +166,14 @@ config KERNEL_LZMA
49           two. Compression is slowest.  The kernel size is about 33%
50           smaller with LZMA in comparison to gzip.
51  
52 +config KERNEL_LZO
53 +       bool "LZO"
54 +       depends on HAVE_KERNEL_LZO
55 +       help
56 +         Its compression ratio is the poorest among the 4. The kernel
57 +         size is about about 10% bigger than gzip; however its speed
58 +         (both compression and decompression) is the fastest.
59 +
60  endchoice
61  
62  config SWAP
63 diff --git a/lib/decompress_unlzo.c b/lib/decompress_unlzo.c
64 new file mode 100644
65 index 0000000..2bb736f
66 --- /dev/null
67 +++ b/lib/decompress_unlzo.c
68 @@ -0,0 +1,208 @@
69 +/*
70 + * LZO decompressor for the Linux kernel. Code borrowed from the lzo
71 + * implementation by Markus Franz Xaver Johannes Oberhumer.
72 + *
73 + * Linux kernel adaptation:
74 + * Copyright (C) 2009
75 + * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
76 + *
77 + * Original code:
78 + * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer
79 + * All Rights Reserved.
80 + *
81 + * lzop and the LZO library are free software; you can redistribute them
82 + * and/or modify them under the terms of the GNU General Public License as
83 + * published by the Free Software Foundation; either version 2 of
84 + * the License, or (at your option) any later version.
85 + *
86 + * This program is distributed in the hope that it will be useful,
87 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
88 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
89 + * GNU General Public License for more details.
90 + *
91 + * You should have received a copy of the GNU General Public License
92 + * along with this program; see the file COPYING.
93 + * If not, write to the Free Software Foundation, Inc.,
94 + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
95 + *
96 + * Markus F.X.J. Oberhumer
97 + * <markus@oberhumer.com>
98 + * http://www.oberhumer.com/opensource/lzop/
99 + */
100 +
101 +#ifdef STATIC
102 +#include "lzo/lzo1x_decompress.c"
103 +#else
104 +#include <linux/slab.h>
105 +#include <linux/decompress/unlzo.h>
106 +#endif
107 +
108 +#include <linux/types.h>
109 +#include <linux/lzo.h>
110 +#include <linux/decompress/mm.h>
111 +
112 +#include <linux/compiler.h>
113 +#include <asm/unaligned.h>
114 +
115 +static const unsigned char lzop_magic[] =
116 +       { 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
117 +
118 +#define LZO_BLOCK_SIZE        (256*1024l)
119 +#define HEADER_HAS_FILTER      0x00000800L
120 +
121 +STATIC inline int INIT parse_header(u8 *input, u8 *skip)
122 +{
123 +       int l;
124 +       u8 *parse = input;
125 +       u8 level = 0;
126 +       u16 version;
127 +
128 +       /* read magic: 9 first bits */
129 +       for (l = 0; l < 9; l++) {
130 +               if (*parse++ != lzop_magic[l])
131 +                       return 0;
132 +       }
133 +       /* get version (2bytes), skip library version (2),
134 +        * 'need to be extracted' version (2) and
135 +        * method (1) */
136 +       version = get_unaligned_be16(parse);
137 +       parse += 7;
138 +       if (version >= 0x0940)
139 +               level = *parse++;
140 +       if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
141 +               parse += 8; /* flags + filter info */
142 +       else
143 +               parse += 4; /* flags */
144 +
145 +       /* skip mode and mtime_low */
146 +       parse += 8;
147 +       if (version >= 0x0940)
148 +               parse += 4;     /* skip mtime_high */
149 +
150 +       l = *parse++;
151 +       /* don't care about the file name, and skip checksum */
152 +       parse += l + 4;
153 +
154 +       *skip = parse - input;
155 +       return 1;
156 +}
157 +
158 +STATIC inline int INIT unlzo(u8 *input, int in_len,
159 +                               int (*fill) (void *, unsigned int),
160 +                               int (*flush) (void *, unsigned int),
161 +                               u8 *output, int *posp,
162 +                               void (*error_fn) (char *x))
163 +{
164 +       u8 skip = 0, r = 0;
165 +       u32 src_len, dst_len;
166 +       size_t tmp;
167 +       u8 *in_buf, *in_buf_save, *out_buf;
168 +       int obytes_processed = 0;
169 +
170 +       set_error_fn(error_fn);
171 +
172 +       if (output)
173 +               out_buf = output;
174 +       else if (!flush) {
175 +               error("NULL output pointer and no flush function provided");
176 +               goto exit;
177 +       } else {
178 +               out_buf = malloc(LZO_BLOCK_SIZE);
179 +               if (!out_buf) {
180 +                       error("Could not allocate output buffer");
181 +                       goto exit;
182 +               }
183 +       }
184 +
185 +       if (input && fill) {
186 +               error("Both input pointer and fill function provided, don't know what to do");
187 +               goto exit_1;
188 +       } else if (input)
189 +               in_buf = input;
190 +       else if (!fill || !posp) {
191 +               error("NULL input pointer and missing position pointer or fill function");
192 +               goto exit_1;
193 +       } else {
194 +               in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
195 +               if (!in_buf) {
196 +                       error("Could not allocate input buffer");
197 +                       goto exit_1;
198 +               }
199 +       }
200 +       in_buf_save = in_buf;
201 +
202 +       if (posp)
203 +               *posp = 0;
204 +
205 +       if (fill)
206 +               fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
207 +
208 +       if (!parse_header(input, &skip)) {
209 +               error("invalid header");
210 +               goto exit_2;
211 +       }
212 +       in_buf += skip;
213 +
214 +       if (posp)
215 +               *posp = skip;
216 +
217 +       for (;;) {
218 +               /* read uncompressed block size */
219 +               dst_len = get_unaligned_be32(in_buf);
220 +               in_buf += 4;
221 +
222 +               /* exit if last block */
223 +               if (dst_len == 0) {
224 +                       if (posp)
225 +                               *posp += 4;
226 +                       break;
227 +               }
228 +
229 +               if (dst_len > LZO_BLOCK_SIZE) {
230 +                       error("dest len longer than block size");
231 +                       goto exit_2;
232 +               }
233 +
234 +               /* read compressed block size, and skip block checksum info */
235 +               src_len = get_unaligned_be32(in_buf);
236 +               in_buf += 8;
237 +
238 +               if (src_len <= 0 || src_len > dst_len) {
239 +                       error("file corrupted");
240 +                       goto exit_2;
241 +               }
242 +
243 +               /* decompress */
244 +               tmp = dst_len;
245 +               r = lzo1x_decompress_safe((u8 *) in_buf, src_len, out_buf, &tmp);
246 +
247 +               if (r != LZO_E_OK || dst_len != tmp) {
248 +                       error("Compressed data violation");
249 +                       goto exit_2;
250 +               }
251 +
252 +               obytes_processed += dst_len;
253 +               if (flush)
254 +                       flush(out_buf, dst_len);
255 +               if (output)
256 +                       out_buf += dst_len;
257 +               if (posp)
258 +                       *posp += src_len + 12;
259 +               if (fill) {
260 +                       in_buf = in_buf_save;
261 +                       fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
262 +               } else
263 +                       in_buf += src_len;
264 +       }
265 +
266 +exit_2:
267 +       if (!input)
268 +               free(in_buf);
269 +exit_1:
270 +       if (!output)
271 +               free(out_buf);
272 +exit:
273 +       return obytes_processed;
274 +}
275 +
276 +#define decompress unlzo
277 diff --git a/lib/lzo/lzo1x_decompress.c b/lib/lzo/lzo1x_decompress.c
278 index 5dc6b29..f2fd098 100644
279 --- a/lib/lzo/lzo1x_decompress.c
280 +++ b/lib/lzo/lzo1x_decompress.c
281 @@ -11,11 +11,13 @@
282   *  Richard Purdie <rpurdie@openedhand.com>
283   */
284  
285 +#ifndef STATIC
286  #include <linux/module.h>
287  #include <linux/kernel.h>
288 -#include <linux/lzo.h>
289 -#include <asm/byteorder.h>
290 +#endif
291 +
292  #include <asm/unaligned.h>
293 +#include <linux/lzo.h>
294  #include "lzodefs.h"
295  
296  #define HAVE_IP(x, ip_end, ip) ((size_t)(ip_end - ip) < (x))
297 @@ -244,9 +246,10 @@ lookbehind_overrun:
298         *out_len = op - out;
299         return LZO_E_LOOKBEHIND_OVERRUN;
300  }
301 -
302 +#ifndef STATIC
303  EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
304  
305  MODULE_LICENSE("GPL");
306  MODULE_DESCRIPTION("LZO1X Decompressor");
307  
308 +#endif
309 diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
310 index ffdafb2..39c3483 100644
311 --- a/scripts/Makefile.lib
312 +++ b/scripts/Makefile.lib
313 @@ -230,3 +230,8 @@ quiet_cmd_lzma = LZMA    $@
314  cmd_lzma = (cat $(filter-out FORCE,$^) | \
315         lzma -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
316         (rm -f $@ ; false)
317 +
318 +quiet_cmd_lzo = LZO    $@
319 +cmd_lzo = (cat $(filter-out FORCE,$^) | \
320 +       lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
321 +       (rm -f $@ ; false)