Skip to content

Commit 889c92d

Browse files
author
H. Peter Anvin
committed
bzip2/lzma: centralize format detection
Centralize the compression format detection to a common routine in the lib directory, and use it for both initramfs and initrd. Signed-off-by: H. Peter Anvin <[email protected]>
1 parent 6c11b12 commit 889c92d

File tree

5 files changed

+72
-67
lines changed

5 files changed

+72
-67
lines changed

include/linux/decompress/generic.h

+3
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ typedef int (*decompress_fn) (unsigned char *inbuf, int len,
2626
*fill should be called (repeatedly...) to read data, at most IOBUF_SIZE
2727
*/
2828

29+
/* Utility routine to detect the decompression method */
30+
decompress_fn decompress_method(const unsigned char *inbuf, int len,
31+
const char **name);
2932

3033
#endif

init/do_mounts_rd.c

+7-31
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212

1313
#include <linux/decompress/generic.h>
1414

15-
#include <linux/decompress/bunzip2.h>
16-
#include <linux/decompress/unlzma.h>
17-
#include <linux/decompress/inflate.h>
1815

1916
int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
2017

@@ -49,24 +46,6 @@ static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
4946
* cramfs
5047
* gzip
5148
*/
52-
static const struct compress_format {
53-
unsigned char magic[2];
54-
const char *name;
55-
decompress_fn decompressor;
56-
} compressed_formats[] = {
57-
#ifdef CONFIG_RD_GZIP
58-
{ {037, 0213}, "gzip", gunzip },
59-
{ {037, 0236}, "gzip", gunzip },
60-
#endif
61-
#ifdef CONFIG_RD_BZIP2
62-
{ {0x42, 0x5a}, "bzip2", bunzip2 },
63-
#endif
64-
#ifdef CONFIG_RD_LZMA
65-
{ {0x5d, 0x00}, "lzma", unlzma },
66-
#endif
67-
{ {0, 0}, NULL, NULL }
68-
};
69-
7049
static int __init
7150
identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
7251
{
@@ -77,7 +56,7 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
7756
struct cramfs_super *cramfsb;
7857
int nblocks = -1;
7958
unsigned char *buf;
80-
const struct compress_format *cf;
59+
const char *compress_name;
8160

8261
buf = kmalloc(size, GFP_KERNEL);
8362
if (!buf)
@@ -95,15 +74,12 @@ identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
9574
sys_lseek(fd, start_block * BLOCK_SIZE, 0);
9675
sys_read(fd, buf, size);
9776

98-
for (cf = compressed_formats; cf->decompressor; cf++) {
99-
if (buf[0] == cf->magic[0] && buf[1] == cf->magic[1]) {
100-
printk(KERN_NOTICE
101-
"RAMDISK: %s image found at block %d\n",
102-
cf->name, start_block);
103-
*decompressor = cf->decompressor;
104-
nblocks = 0;
105-
goto done;
106-
}
77+
*decompressor = decompress_method(buf, size, &compress_name);
78+
if (*decompressor) {
79+
printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
80+
compress_name, start_block);
81+
nblocks = 0;
82+
goto done;
10783
}
10884

10985
/* romfs is at block zero too */

init/initramfs.c

+7-32
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,13 @@ static int __init flush_buffer(void *bufv, unsigned len)
416416

417417
static unsigned my_inptr; /* index of next byte to be processed in inbuf */
418418

419-
#include <linux/decompress/bunzip2.h>
420-
#include <linux/decompress/unlzma.h>
421-
#include <linux/decompress/inflate.h>
419+
#include <linux/decompress/generic.h>
422420

423421
static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
424422
{
425423
int written;
424+
decompress_fn decompress;
425+
426426
dry_run = check_only;
427427
header_buf = kmalloc(110, GFP_KERNEL);
428428
symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
@@ -450,35 +450,10 @@ static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
450450
continue;
451451
}
452452
this_header = 0;
453-
#ifdef CONFIG_RD_GZIP
454-
if (!gunzip(buf, len, NULL, flush_buffer, NULL,
455-
&my_inptr, error) &&
456-
message == NULL)
457-
goto ok;
458-
#endif
459-
460-
#ifdef CONFIG_RD_BZIP2
461-
message = NULL; /* Zero out message, or else cpio will
462-
think an error has already occured */
463-
if (!bunzip2(buf, len, NULL, flush_buffer, NULL,
464-
&my_inptr, error) &&
465-
message == NULL) {
466-
goto ok;
467-
}
468-
#endif
469-
470-
#ifdef CONFIG_RD_LZMA
471-
message = NULL; /* Zero out message, or else cpio will
472-
think an error has already occured */
473-
if (!unlzma(buf, len, NULL, flush_buffer, NULL,
474-
&my_inptr, error) &&
475-
message == NULL) {
476-
goto ok;
477-
}
478-
#endif
479-
#if defined CONFIG_RD_GZIP || defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA
480-
ok:
481-
#endif
453+
decompress = decompress_method(buf, len, NULL);
454+
if (decompress)
455+
decompress(buf, len, NULL, flush_buffer, NULL,
456+
&my_inptr, error);
482457
if (state != Reset)
483458
error("junk in compressed archive");
484459
this_header = saved_offset + my_inptr;

lib/Makefile

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \
1111
rbtree.o radix-tree.o dump_stack.o \
1212
idr.o int_sqrt.o extable.o prio_tree.o \
1313
sha1.o irq_regs.o reciprocal_div.o argv_split.o \
14-
proportions.o prio_heap.o ratelimit.o show_mem.o is_single_threaded.o
14+
proportions.o prio_heap.o ratelimit.o show_mem.o \
15+
is_single_threaded.o decompress.o
1516

1617
lib-$(CONFIG_MMU) += ioremap.o
1718
lib-$(CONFIG_SMP) += cpumask.o
@@ -65,9 +66,9 @@ obj-$(CONFIG_REED_SOLOMON) += reed_solomon/
6566
obj-$(CONFIG_LZO_COMPRESS) += lzo/
6667
obj-$(CONFIG_LZO_DECOMPRESS) += lzo/
6768

68-
obj-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
69-
obj-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
70-
obj-$(CONFIG_DECOMPRESS_LZMA) += decompress_unlzma.o
69+
lib-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
70+
lib-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
71+
lib-$(CONFIG_DECOMPRESS_LZMA) += decompress_unlzma.o
7172

7273
obj-$(CONFIG_TEXTSEARCH) += textsearch.o
7374
obj-$(CONFIG_TEXTSEARCH_KMP) += ts_kmp.o

lib/decompress.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* decompress.c
3+
*
4+
* Detect the decompression method based on magic number
5+
*/
6+
7+
#include <linux/decompress/generic.h>
8+
9+
#include <linux/decompress/bunzip2.h>
10+
#include <linux/decompress/unlzma.h>
11+
#include <linux/decompress/inflate.h>
12+
13+
#include <linux/types.h>
14+
#include <linux/string.h>
15+
16+
static const struct compress_format {
17+
unsigned char magic[2];
18+
const char *name;
19+
decompress_fn decompressor;
20+
} compressed_formats[] = {
21+
#ifdef CONFIG_DECOMPRESS_GZIP
22+
{ {037, 0213}, "gzip", gunzip },
23+
{ {037, 0236}, "gzip", gunzip },
24+
#endif
25+
#ifdef CONFIG_DECOMPRESS_BZIP2
26+
{ {0x42, 0x5a}, "bzip2", bunzip2 },
27+
#endif
28+
#ifdef CONFIG_DECOMPRESS_LZMA
29+
{ {0x5d, 0x00}, "lzma", unlzma },
30+
#endif
31+
{ {0, 0}, NULL, NULL }
32+
};
33+
34+
decompress_fn decompress_method(const unsigned char *inbuf, int len,
35+
const char **name)
36+
{
37+
const struct compress_format *cf;
38+
39+
if (len < 2)
40+
return NULL; /* Need at least this much... */
41+
42+
for (cf = compressed_formats; cf->decompressor; cf++) {
43+
if (!memcmp(inbuf, cf->magic, 2))
44+
break;
45+
46+
}
47+
if (name)
48+
*name = cf->name;
49+
return cf->decompressor;
50+
}

0 commit comments

Comments
 (0)