2 #include <linux/kernel.h>
4 #include <linux/minix_fs.h>
5 #include <linux/ext2_fs.h>
6 #include <linux/romfs_fs.h>
7 #include <linux/cramfs_fs.h>
8 #include <linux/initrd.h>
9 #include <linux/string.h>
11 #include "do_mounts.h"
13 #define BUILD_CRAMDISK
15 int __initdata rd_prompt
= 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
17 static int __init
prompt_ramdisk(char *str
)
19 rd_prompt
= simple_strtol(str
,NULL
,0) & 1;
22 __setup("prompt_ramdisk=", prompt_ramdisk
);
24 int __initdata rd_image_start
; /* starting block # of image */
26 static int __init
ramdisk_start_setup(char *str
)
28 rd_image_start
= simple_strtol(str
,NULL
,0);
31 __setup("ramdisk_start=", ramdisk_start_setup
);
33 static int __init
crd_load(int in_fd
, int out_fd
);
36 * This routine tries to find a RAM disk image to load, and returns the
37 * number of blocks to read for a non-compressed image, 0 if the image
38 * is a compressed image, and -1 if an image with the right magic
39 * numbers could not be found.
41 * We currently check for the following magic numbers:
49 identify_ramdisk_image(int fd
, int start_block
)
52 struct minix_super_block
*minixsb
;
53 struct ext2_super_block
*ext2sb
;
54 struct romfs_super_block
*romfsb
;
55 struct cramfs_super
*cramfsb
;
59 buf
= kmalloc(size
, GFP_KERNEL
);
63 minixsb
= (struct minix_super_block
*) buf
;
64 ext2sb
= (struct ext2_super_block
*) buf
;
65 romfsb
= (struct romfs_super_block
*) buf
;
66 cramfsb
= (struct cramfs_super
*) buf
;
67 memset(buf
, 0xe5, size
);
70 * Read block 0 to test for gzipped kernel
72 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
73 sys_read(fd
, buf
, size
);
76 * If it matches the gzip magic numbers, return -1
78 if (buf
[0] == 037 && ((buf
[1] == 0213) || (buf
[1] == 0236))) {
80 "RAMDISK: Compressed image found at block %d\n",
86 /* romfs is at block zero too */
87 if (romfsb
->word0
== ROMSB_WORD0
&&
88 romfsb
->word1
== ROMSB_WORD1
) {
90 "RAMDISK: romfs filesystem found at block %d\n",
92 nblocks
= (ntohl(romfsb
->size
)+BLOCK_SIZE
-1)>>BLOCK_SIZE_BITS
;
96 if (cramfsb
->magic
== CRAMFS_MAGIC
) {
98 "RAMDISK: cramfs filesystem found at block %d\n",
100 nblocks
= (cramfsb
->size
+ BLOCK_SIZE
- 1) >> BLOCK_SIZE_BITS
;
105 * Read block 1 to test for minix and ext2 superblock
107 sys_lseek(fd
, (start_block
+1) * BLOCK_SIZE
, 0);
108 sys_read(fd
, buf
, size
);
111 if (minixsb
->s_magic
== MINIX_SUPER_MAGIC
||
112 minixsb
->s_magic
== MINIX_SUPER_MAGIC2
) {
114 "RAMDISK: Minix filesystem found at block %d\n",
116 nblocks
= minixsb
->s_nzones
<< minixsb
->s_log_zone_size
;
121 if (ext2sb
->s_magic
== cpu_to_le16(EXT2_SUPER_MAGIC
)) {
123 "RAMDISK: ext2 filesystem found at block %d\n",
125 nblocks
= le32_to_cpu(ext2sb
->s_blocks_count
);
130 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
134 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
139 int __init
rd_load_image(char *from
)
143 unsigned long rd_blocks
, devblocks
;
144 int nblocks
, i
, disk
;
146 unsigned short rotate
= 0;
147 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
148 char rotator
[4] = { '|' , '/' , '-' , '\\' };
151 out_fd
= sys_open("/dev/ram", O_RDWR
, 0);
155 in_fd
= sys_open(from
, O_RDONLY
, 0);
159 nblocks
= identify_ramdisk_image(in_fd
, rd_image_start
);
164 #ifdef BUILD_CRAMDISK
165 if (crd_load(in_fd
, out_fd
) == 0)
166 goto successful_load
;
169 "RAMDISK: Kernel does not support compressed "
170 "RAM disk images\n");
176 * NOTE NOTE: nblocks suppose that the blocksize is BLOCK_SIZE, so
177 * rd_load_image will work only with filesystem BLOCK_SIZE wide!
178 * So make sure to use 1k blocksize while generating ext2fs
181 if (sys_ioctl(out_fd
, BLKGETSIZE
, (unsigned long)&rd_blocks
) < 0)
186 if (nblocks
> rd_blocks
) {
187 printk("RAMDISK: image too big! (%d/%ld blocks)\n",
193 * OK, time to copy in the data
195 if (sys_ioctl(in_fd
, BLKGETSIZE
, (unsigned long)&devblocks
) < 0)
200 if (strcmp(from
, "/initrd.image") == 0)
203 if (devblocks
== 0) {
204 printk(KERN_ERR
"RAMDISK: could not determine device size\n");
208 buf
= kmalloc(BLOCK_SIZE
, GFP_KERNEL
);
210 printk(KERN_ERR
"RAMDISK: could not allocate buffer\n");
214 printk(KERN_NOTICE
"RAMDISK: Loading %d blocks [%ld disk%s] into ram disk... ",
215 nblocks
, ((nblocks
-1)/devblocks
)+1, nblocks
>devblocks
? "s" : "");
216 for (i
= 0, disk
= 1; i
< nblocks
; i
++) {
217 if (i
&& (i
% devblocks
== 0)) {
218 printk("done disk #%d.\n", disk
++);
220 if (sys_close(in_fd
)) {
221 printk("Error closing the disk.\n");
224 change_floppy("disk #%d", disk
);
225 in_fd
= sys_open(from
, O_RDONLY
, 0);
227 printk("Error opening disk.\n");
230 printk("Loading disk #%d... ", disk
);
232 sys_read(in_fd
, buf
, BLOCK_SIZE
);
233 sys_write(out_fd
, buf
, BLOCK_SIZE
);
234 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
236 printk("%c\b", rotator
[rotate
& 0x3]);
251 sys_unlink("/dev/ram");
255 int __init
rd_load_disk(int n
)
258 change_floppy("root floppy disk to be loaded into RAM disk");
259 create_dev("/dev/root", ROOT_DEV
, root_device_name
);
260 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR
, n
), NULL
);
261 return rd_load_image("/dev/root");
264 #ifdef BUILD_CRAMDISK
270 #define OF(args) args
273 #define memzero(s, n) memset ((s), 0, (n))
276 typedef unsigned char uch
;
277 typedef unsigned short ush
;
278 typedef unsigned long ulg
;
280 #define INBUFSIZ 4096
281 #define WSIZE 0x8000 /* window size--must be a power of two, and */
282 /* at least 32K for zip's deflate method */
287 static unsigned insize
; /* valid bytes in inbuf */
288 static unsigned inptr
; /* index of next byte to be processed in inbuf */
289 static unsigned outcnt
; /* bytes in output buffer */
290 static int exit_code
;
291 static int unzip_error
;
292 static long bytes_out
;
293 static int crd_infd
, crd_outfd
;
295 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
297 /* Diagnostic functions (stubbed out) */
298 #define Assert(cond,msg)
305 #define STATIC static
307 static int fill_inbuf(void);
308 static void flush_window(void);
309 static void *malloc(int size
);
310 static void free(void *where
);
311 static void error(char *m
);
312 static void gzip_mark(void **);
313 static void gzip_release(void **);
315 #include "../lib/inflate.c"
317 static void __init
*malloc(int size
)
319 return kmalloc(size
, GFP_KERNEL
);
322 static void __init
free(void *where
)
327 static void __init
gzip_mark(void **ptr
)
331 static void __init
gzip_release(void **ptr
)
336 /* ===========================================================================
337 * Fill the input buffer. This is called only when the buffer is empty
338 * and at least one byte is really needed.
339 * Returning -1 does not guarantee that gunzip() will ever return.
341 static int __init
fill_inbuf(void)
343 if (exit_code
) return -1;
345 insize
= sys_read(crd_infd
, inbuf
, INBUFSIZ
);
347 error("RAMDISK: ran out of compressed data");
356 /* ===========================================================================
357 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
358 * (Used for the decompressed data only.)
360 static void __init
flush_window(void)
362 ulg c
= crc
; /* temporary variable */
366 written
= sys_write(crd_outfd
, window
, outcnt
);
367 if (written
!= outcnt
&& unzip_error
== 0) {
368 printk(KERN_ERR
"RAMDISK: incomplete write (%d != %d) %ld\n",
369 written
, outcnt
, bytes_out
);
373 for (n
= 0; n
< outcnt
; n
++) {
375 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
378 bytes_out
+= (ulg
)outcnt
;
382 static void __init
error(char *x
)
384 printk(KERN_ERR
"%s\n", x
);
389 static int __init
crd_load(int in_fd
, int out_fd
)
393 insize
= 0; /* valid bytes in inbuf */
394 inptr
= 0; /* index of next byte to be processed in inbuf */
395 outcnt
= 0; /* bytes in output buffer */
398 crc
= (ulg
)0xffffffffL
; /* shift register contents */
402 inbuf
= kmalloc(INBUFSIZ
, GFP_KERNEL
);
404 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip buffer\n");
407 window
= kmalloc(WSIZE
, GFP_KERNEL
);
409 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip window\n");
422 #endif /* BUILD_CRAMDISK */