Skip to content

Commit 30d65db

Browse files
Alain KnaffH. Peter Anvin
authored andcommitted
bzip2/lzma: config and initramfs support for bzip2/lzma decompression
Impact: New code for initramfs decompression, new features This is the second part of the bzip2/lzma patch The bzip patch is based on an idea by Christian Ludwig, includes support for compressing the kernel with bzip2 or lzma rather than gzip. Both compressors give smaller sizes than gzip. Lzma's decompresses faster than bzip2. It also supports ramdisks and initramfs' compressed using these two compressors. The functionality has been successfully used for a couple of years by the udpcast project This version applies to "tip" kernel 2.6.28 This part contains: - support for new compressions (bzip2 and lzma) in initramfs and old-style ramdisk - config dialog for kernel compression (but new kernel compressions not yet supported) Signed-off-by: Alain Knaff <alain@knaff.lu> Signed-off-by: H. Peter Anvin <hpa@zytor.com>
1 parent bc22c17 commit 30d65db

5 files changed

Lines changed: 184 additions & 199 deletions

File tree

‎drivers/block/Kconfig‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,31 @@ config BLK_DEV_XIP
358358
will prevent RAM block device backing store memory from being
359359
allocated from highmem (only a problem for highmem systems).
360360

361+
config RD_BZIP2
362+
bool "Initial ramdisk compressed using bzip2"
363+
default n
364+
depends on BLK_DEV_INITRD=y
365+
help
366+
Support loading of a bzip2 encoded initial ramdisk or cpio buffer
367+
If unsure, say N.
368+
369+
config RD_LZMA
370+
bool "Initial ramdisk compressed using lzma"
371+
default n
372+
depends on BLK_DEV_INITRD=y
373+
help
374+
Support loading of a lzma encoded initial ramdisk or cpio buffer
375+
If unsure, say N.
376+
377+
config RD_GZIP
378+
bool "Initial ramdisk compressed using gzip"
379+
default y
380+
depends on BLK_DEV_INITRD=y
381+
select ZLIB_INFLATE
382+
help
383+
Support loading of a gzip encoded initial ramdisk or cpio buffer.
384+
If unsure, say Y.
385+
361386
config CDROM_PKTCDVD
362387
tristate "Packet writing on CD/DVD media"
363388
depends on !UML

‎init/Kconfig‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,56 @@ config LOCALVERSION_AUTO
101101

102102
which is done within the script "scripts/setlocalversion".)
103103

104+
choice
105+
prompt "Kernel compression mode"
106+
default KERNEL_GZIP
107+
help
108+
The linux kernel is a kind of self-extracting executable.
109+
Several compression algorithms are available, which differ
110+
in efficiency, compression and decompression speed.
111+
Compression speed is only relevant when building a kernel.
112+
Decompression speed is relevant at each boot.
113+
114+
If you have any problems with bzip2 or lzma compressed
115+
kernels, mail me (Alain Knaff) <alain@knaff.lu>. (An older
116+
version of this functionality (bzip2 only), for 2.4, was
117+
supplied by Christian Ludwig)
118+
119+
High compression options are mostly useful for users, who
120+
are low on disk space (embedded systems), but for whom ram
121+
size matters less.
122+
123+
If in doubt, select 'gzip'
124+
125+
config KERNEL_GZIP
126+
bool "Gzip"
127+
help
128+
The old and tried gzip compression. Its compression ratio is
129+
the poorest among the 3 choices; however its speed (both
130+
compression and decompression) is the fastest.
131+
132+
config KERNEL_BZIP2
133+
bool "Bzip2"
134+
help
135+
Its compression ratio and speed is intermediate.
136+
Decompression speed is slowest among the 3.
137+
The kernel size is about 10 per cent smaller with bzip2,
138+
in comparison to gzip.
139+
Bzip2 uses a large amount of memory. For modern kernels
140+
you will need at least 8MB RAM or more for booting.
141+
142+
config KERNEL_LZMA
143+
bool "LZMA"
144+
help
145+
The most recent compression algorithm.
146+
Its ratio is best, decompression speed is between the other
147+
2. Compression is slowest.
148+
The kernel size is about 33 per cent smaller with lzma,
149+
in comparison to gzip.
150+
151+
endchoice
152+
153+
104154
config SWAP
105155
bool "Support for paging of anonymous memory (swap)"
106156
depends on MMU && BLOCK

‎init/do_mounts_rd.c‎

Lines changed: 65 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
#include "do_mounts.h"
1212

13+
#include <linux/decompress/generic.h>
14+
15+
#include <linux/decompress/bunzip2.h>
16+
#include <linux/decompress/unlzma.h>
17+
#include <linux/decompress/inflate.h>
18+
1319
int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
1420

1521
static int __init prompt_ramdisk(char *str)
@@ -28,7 +34,7 @@ static int __init ramdisk_start_setup(char *str)
2834
}
2935
__setup("ramdisk_start=", ramdisk_start_setup);
3036

31-
static int __init crd_load(int in_fd, int out_fd);
37+
static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
3238

3339
/*
3440
* This routine tries to find a RAM disk image to load, and returns the
@@ -44,7 +50,7 @@ static int __init crd_load(int in_fd, int out_fd);
4450
* gzip
4551
*/
4652
static int __init
47-
identify_ramdisk_image(int fd, int start_block)
53+
identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
4854
{
4955
const int size = 512;
5056
struct minix_super_block *minixsb;
@@ -70,16 +76,47 @@ identify_ramdisk_image(int fd, int start_block)
7076
sys_lseek(fd, start_block * BLOCK_SIZE, 0);
7177
sys_read(fd, buf, size);
7278

79+
#ifdef CONFIG_RD_GZIP
7380
/*
7481
* If it matches the gzip magic numbers, return 0
7582
*/
7683
if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
7784
printk(KERN_NOTICE
7885
"RAMDISK: Compressed image found at block %d\n",
7986
start_block);
87+
*decompressor = gunzip;
88+
nblocks = 0;
89+
goto done;
90+
}
91+
#endif
92+
93+
#ifdef CONFIG_RD_BZIP2
94+
/*
95+
* If it matches the bzip2 magic numbers, return -1
96+
*/
97+
if (buf[0] == 0x42 && (buf[1] == 0x5a)) {
98+
printk(KERN_NOTICE
99+
"RAMDISK: Bzipped image found at block %d\n",
100+
start_block);
101+
*decompressor = bunzip2;
102+
nblocks = 0;
103+
goto done;
104+
}
105+
#endif
106+
107+
#ifdef CONFIG_RD_LZMA
108+
/*
109+
* If it matches the lzma magic numbers, return -1
110+
*/
111+
if (buf[0] == 0x5d && (buf[1] == 0x00)) {
112+
printk(KERN_NOTICE
113+
"RAMDISK: Lzma image found at block %d\n",
114+
start_block);
115+
*decompressor = unlzma;
80116
nblocks = 0;
81117
goto done;
82118
}
119+
#endif
83120

84121
/* romfs is at block zero too */
85122
if (romfsb->word0 == ROMSB_WORD0 &&
@@ -143,6 +180,7 @@ int __init rd_load_image(char *from)
143180
int nblocks, i, disk;
144181
char *buf = NULL;
145182
unsigned short rotate = 0;
183+
decompress_fn decompressor = NULL;
146184
#if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
147185
char rotator[4] = { '|' , '/' , '-' , '\\' };
148186
#endif
@@ -155,12 +193,12 @@ int __init rd_load_image(char *from)
155193
if (in_fd < 0)
156194
goto noclose_input;
157195

158-
nblocks = identify_ramdisk_image(in_fd, rd_image_start);
196+
nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
159197
if (nblocks < 0)
160198
goto done;
161199

162200
if (nblocks == 0) {
163-
if (crd_load(in_fd, out_fd) == 0)
201+
if (crd_load(in_fd, out_fd, decompressor) == 0)
164202
goto successful_load;
165203
goto done;
166204
}
@@ -259,138 +297,48 @@ int __init rd_load_disk(int n)
259297
return rd_load_image("/dev/root");
260298
}
261299

262-
/*
263-
* gzip declarations
264-
*/
265-
266-
#define OF(args) args
267-
268-
#ifndef memzero
269-
#define memzero(s, n) memset ((s), 0, (n))
270-
#endif
271-
272-
typedef unsigned char uch;
273-
typedef unsigned short ush;
274-
typedef unsigned long ulg;
275-
276-
#define INBUFSIZ 4096
277-
#define WSIZE 0x8000 /* window size--must be a power of two, and */
278-
/* at least 32K for zip's deflate method */
279-
280-
static uch *inbuf;
281-
static uch *window;
282-
283-
static unsigned insize; /* valid bytes in inbuf */
284-
static unsigned inptr; /* index of next byte to be processed in inbuf */
285-
static unsigned outcnt; /* bytes in output buffer */
286300
static int exit_code;
287-
static int unzip_error;
288-
static long bytes_out;
301+
static int decompress_error;
289302
static int crd_infd, crd_outfd;
290303

291-
#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
292-
293-
/* Diagnostic functions (stubbed out) */
294-
#define Assert(cond,msg)
295-
#define Trace(x)
296-
#define Tracev(x)
297-
#define Tracevv(x)
298-
#define Tracec(c,x)
299-
#define Tracecv(c,x)
300-
301-
#define STATIC static
302-
#define INIT __init
303-
304-
static int __init fill_inbuf(void);
305-
static void __init flush_window(void);
306-
static void __init error(char *m);
307-
308-
#define NO_INFLATE_MALLOC
309-
310-
#include "../lib/inflate.c"
311-
312-
/* ===========================================================================
313-
* Fill the input buffer. This is called only when the buffer is empty
314-
* and at least one byte is really needed.
315-
* Returning -1 does not guarantee that gunzip() will ever return.
316-
*/
317-
static int __init fill_inbuf(void)
304+
static int __init compr_fill(void *buf, unsigned int len)
318305
{
319-
if (exit_code) return -1;
320-
321-
insize = sys_read(crd_infd, inbuf, INBUFSIZ);
322-
if (insize == 0) {
323-
error("RAMDISK: ran out of compressed data");
324-
return -1;
325-
}
326-
327-
inptr = 1;
328-
329-
return inbuf[0];
306+
int r = sys_read(crd_infd, buf, len);
307+
if (r < 0)
308+
printk(KERN_ERR "RAMDISK: error while reading compressed data");
309+
else if (r == 0)
310+
printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
311+
return r;
330312
}
331313

332-
/* ===========================================================================
333-
* Write the output window window[0..outcnt-1] and update crc and bytes_out.
334-
* (Used for the decompressed data only.)
335-
*/
336-
static void __init flush_window(void)
314+
static int __init compr_flush(void *window, unsigned int outcnt)
337315
{
338-
ulg c = crc; /* temporary variable */
339-
unsigned n, written;
340-
uch *in, ch;
341-
342-
written = sys_write(crd_outfd, window, outcnt);
343-
if (written != outcnt && unzip_error == 0) {
344-
printk(KERN_ERR "RAMDISK: incomplete write (%d != %d) %ld\n",
345-
written, outcnt, bytes_out);
346-
unzip_error = 1;
347-
}
348-
in = window;
349-
for (n = 0; n < outcnt; n++) {
350-
ch = *in++;
351-
c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
352-
}
353-
crc = c;
354-
bytes_out += (ulg)outcnt;
355-
outcnt = 0;
316+
int written = sys_write(crd_outfd, window, outcnt);
317+
if (written != outcnt) {
318+
if (decompress_error == 0)
319+
printk(KERN_ERR
320+
"RAMDISK: incomplete write (%d != %d)\n",
321+
written, outcnt);
322+
decompress_error = 1;
323+
return -1;
324+
}
325+
return outcnt;
356326
}
357327

358328
static void __init error(char *x)
359329
{
360330
printk(KERN_ERR "%s\n", x);
361331
exit_code = 1;
362-
unzip_error = 1;
332+
decompress_error = 1;
363333
}
364334

365-
static int __init crd_load(int in_fd, int out_fd)
335+
static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
366336
{
367337
int result;
368-
369-
insize = 0; /* valid bytes in inbuf */
370-
inptr = 0; /* index of next byte to be processed in inbuf */
371-
outcnt = 0; /* bytes in output buffer */
372-
exit_code = 0;
373-
bytes_out = 0;
374-
crc = (ulg)0xffffffffL; /* shift register contents */
375-
376338
crd_infd = in_fd;
377339
crd_outfd = out_fd;
378-
inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
379-
if (!inbuf) {
380-
printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
381-
return -1;
382-
}
383-
window = kmalloc(WSIZE, GFP_KERNEL);
384-
if (!window) {
385-
printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
386-
kfree(inbuf);
387-
return -1;
388-
}
389-
makecrc();
390-
result = gunzip();
391-
if (unzip_error)
340+
result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
341+
if (decompress_error)
392342
result = 1;
393-
kfree(inbuf);
394-
kfree(window);
395343
return result;
396344
}

0 commit comments

Comments
 (0)