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+
1319int __initdata rd_prompt = 1 ;/* 1 = prompt for RAM disk, 0 = don't prompt */
1420
1521static 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 */
4652static 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 */
286300static int exit_code ;
287- static int unzip_error ;
288- static long bytes_out ;
301+ static int decompress_error ;
289302static 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
358328static 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