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/squashfs_fs.h>
9 #include <linux/initrd.h>
10 #include <linux/string.h>
12 #include "do_mounts.h"
14 #define BUILD_CRAMDISK
16 int __initdata rd_prompt
= 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
18 static int __init
prompt_ramdisk(char *str
)
20 rd_prompt
= simple_strtol(str
,NULL
,0) & 1;
23 __setup("prompt_ramdisk=", prompt_ramdisk
);
25 int __initdata rd_image_start
; /* starting block # of image */
27 static int __init
ramdisk_start_setup(char *str
)
29 rd_image_start
= simple_strtol(str
,NULL
,0);
32 __setup("ramdisk_start=", ramdisk_start_setup
);
34 static int __init
crd_load(int in_fd
, int out_fd
);
37 * This routine tries to find a RAM disk image to load, and returns the
38 * number of blocks to read for a non-compressed image, 0 if the image
39 * is a compressed image, and -1 if an image with the right magic
40 * numbers could not be found.
42 * We currently check for the following magic numbers:
51 identify_ramdisk_image(int fd
, int start_block
)
54 struct minix_super_block
*minixsb
;
55 struct ext2_super_block
*ext2sb
;
56 struct romfs_super_block
*romfsb
;
57 struct cramfs_super
*cramfsb
;
58 struct squashfs_super_block
*squashfsb
;
62 buf
= kmalloc(size
, GFP_KERNEL
);
66 minixsb
= (struct minix_super_block
*) buf
;
67 ext2sb
= (struct ext2_super_block
*) buf
;
68 romfsb
= (struct romfs_super_block
*) buf
;
69 cramfsb
= (struct cramfs_super
*) buf
;
70 squashfsb
= (struct squashfs_super_block
*) buf
;
71 memset(buf
, 0xe5, size
);
74 * Read block 0 to test for gzipped kernel
76 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
77 sys_read(fd
, buf
, size
);
80 * If it matches the gzip magic numbers, return -1
82 if (buf
[0] == 037 && ((buf
[1] == 0213) || (buf
[1] == 0236))) {
84 "RAMDISK: Compressed image found at block %d\n",
90 /* romfs is at block zero too */
91 if (romfsb
->word0
== ROMSB_WORD0
&&
92 romfsb
->word1
== ROMSB_WORD1
) {
94 "RAMDISK: romfs filesystem found at block %d\n",
96 nblocks
= (ntohl(romfsb
->size
)+BLOCK_SIZE
-1)>>BLOCK_SIZE_BITS
;
100 if (cramfsb
->magic
== le32_to_cpu(CRAMFS_MAGIC
)) {
102 "RAMDISK: cramfs filesystem found at block %d\n",
104 nblocks
= (le32_to_cpu(cramfsb
->size
) + BLOCK_SIZE
- 1) >> BLOCK_SIZE_BITS
;
108 /* squashfs is at block zero too */
109 if (squashfsb
->s_magic
== SQUASHFS_MAGIC
) {
111 "RAMDISK: squashfs filesystem found at block %d\n",
113 nblocks
= (squashfsb
->bytes_used
+BLOCK_SIZE
-1)>>BLOCK_SIZE_BITS
;
118 * Read block 1 to test for minix and ext2 superblock
120 sys_lseek(fd
, (start_block
+1) * BLOCK_SIZE
, 0);
121 sys_read(fd
, buf
, size
);
124 if (minixsb
->s_magic
== MINIX_SUPER_MAGIC
||
125 minixsb
->s_magic
== MINIX_SUPER_MAGIC2
) {
127 "RAMDISK: Minix filesystem found at block %d\n",
129 nblocks
= minixsb
->s_nzones
<< minixsb
->s_log_zone_size
;
134 if (ext2sb
->s_magic
== cpu_to_le16(EXT2_SUPER_MAGIC
)) {
136 "RAMDISK: ext2 filesystem found at block %d\n",
138 nblocks
= le32_to_cpu(ext2sb
->s_blocks_count
) <<
139 le32_to_cpu(ext2sb
->s_log_block_size
);
144 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
148 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
153 int __init
rd_load_image(char *from
)
157 unsigned long rd_blocks
, devblocks
;
158 int nblocks
, i
, disk
;
160 unsigned short rotate
= 0;
161 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
162 char rotator
[4] = { '|' , '/' , '-' , '\\' };
165 out_fd
= sys_open("/dev/ram", O_RDWR
, 0);
169 in_fd
= sys_open(from
, O_RDONLY
, 0);
173 nblocks
= identify_ramdisk_image(in_fd
, rd_image_start
);
178 #ifdef BUILD_CRAMDISK
179 if (crd_load(in_fd
, out_fd
) == 0)
180 goto successful_load
;
183 "RAMDISK: Kernel does not support compressed "
184 "RAM disk images\n");
190 * NOTE NOTE: nblocks is not actually blocks but
191 * the number of kibibytes of data to load into a ramdisk.
192 * So any ramdisk block size that is a multiple of 1KiB should
193 * work when the appropriate ramdisk_blocksize is specified
194 * on the command line.
196 * The default ramdisk_blocksize is 1KiB and it is generally
197 * silly to use anything else, so make sure to use 1KiB
198 * blocksize while generating ext2fs ramdisk-images.
200 if (sys_ioctl(out_fd
, BLKGETSIZE
, (unsigned long)&rd_blocks
) < 0)
205 if (nblocks
> rd_blocks
) {
206 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
212 * OK, time to copy in the data
214 if (sys_ioctl(in_fd
, BLKGETSIZE
, (unsigned long)&devblocks
) < 0)
219 if (strcmp(from
, "/initrd.image") == 0)
222 if (devblocks
== 0) {
223 printk(KERN_ERR
"RAMDISK: could not determine device size\n");
227 buf
= kmalloc(BLOCK_SIZE
, GFP_KERNEL
);
229 printk(KERN_ERR
"RAMDISK: could not allocate buffer\n");
233 printk(KERN_NOTICE
"RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
234 nblocks
, ((nblocks
-1)/devblocks
)+1, nblocks
>devblocks
? "s" : "");
235 for (i
= 0, disk
= 1; i
< nblocks
; i
++) {
236 if (i
&& (i
% devblocks
== 0)) {
237 printk("done disk #%d.\n", disk
++);
239 if (sys_close(in_fd
)) {
240 printk("Error closing the disk.\n");
243 change_floppy("disk #%d", disk
);
244 in_fd
= sys_open(from
, O_RDONLY
, 0);
246 printk("Error opening disk.\n");
249 printk("Loading disk #%d... ", disk
);
251 sys_read(in_fd
, buf
, BLOCK_SIZE
);
252 sys_write(out_fd
, buf
, BLOCK_SIZE
);
253 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
255 printk("%c\b", rotator
[rotate
& 0x3]);
270 sys_unlink("/dev/ram");
274 int __init
rd_load_disk(int n
)
277 change_floppy("root floppy disk to be loaded into RAM disk");
278 create_dev("/dev/root", ROOT_DEV
);
279 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR
, n
));
280 return rd_load_image("/dev/root");
283 #ifdef BUILD_CRAMDISK
289 #define OF(args) args
292 #define memzero(s, n) memset ((s), 0, (n))
295 typedef unsigned char uch
;
296 typedef unsigned short ush
;
297 typedef unsigned long ulg
;
299 #define INBUFSIZ 4096
300 #define WSIZE 0x8000 /* window size--must be a power of two, and */
301 /* at least 32K for zip's deflate method */
306 static unsigned insize
; /* valid bytes in inbuf */
307 static unsigned inptr
; /* index of next byte to be processed in inbuf */
308 static unsigned outcnt
; /* bytes in output buffer */
309 static int exit_code
;
310 static int unzip_error
;
311 static long bytes_out
;
312 static int crd_infd
, crd_outfd
;
314 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
316 /* Diagnostic functions (stubbed out) */
317 #define Assert(cond,msg)
324 #define STATIC static
327 static int __init
fill_inbuf(void);
328 static void __init
flush_window(void);
329 static void __init
*malloc(size_t size
);
330 static void __init
free(void *where
);
331 static void __init
error(char *m
);
332 static void __init
gzip_mark(void **);
333 static void __init
gzip_release(void **);
335 #include "../lib/inflate.c"
337 static void __init
*malloc(size_t size
)
339 return kmalloc(size
, GFP_KERNEL
);
342 static void __init
free(void *where
)
347 static void __init
gzip_mark(void **ptr
)
351 static void __init
gzip_release(void **ptr
)
356 /* ===========================================================================
357 * Fill the input buffer. This is called only when the buffer is empty
358 * and at least one byte is really needed.
359 * Returning -1 does not guarantee that gunzip() will ever return.
361 static int __init
fill_inbuf(void)
363 if (exit_code
) return -1;
365 insize
= sys_read(crd_infd
, inbuf
, INBUFSIZ
);
367 error("RAMDISK: ran out of compressed data");
376 /* ===========================================================================
377 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
378 * (Used for the decompressed data only.)
380 static void __init
flush_window(void)
382 ulg c
= crc
; /* temporary variable */
386 written
= sys_write(crd_outfd
, window
, outcnt
);
387 if (written
!= outcnt
&& unzip_error
== 0) {
388 printk(KERN_ERR
"RAMDISK: incomplete write (%d != %d) %ld\n",
389 written
, outcnt
, bytes_out
);
393 for (n
= 0; n
< outcnt
; n
++) {
395 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
398 bytes_out
+= (ulg
)outcnt
;
402 static void __init
error(char *x
)
404 printk(KERN_ERR
"%s\n", x
);
409 static int __init
crd_load(int in_fd
, int out_fd
)
413 insize
= 0; /* valid bytes in inbuf */
414 inptr
= 0; /* index of next byte to be processed in inbuf */
415 outcnt
= 0; /* bytes in output buffer */
418 crc
= (ulg
)0xffffffffL
; /* shift register contents */
422 inbuf
= kmalloc(INBUFSIZ
, GFP_KERNEL
);
424 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip buffer\n");
427 window
= kmalloc(WSIZE
, GFP_KERNEL
);
429 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip window\n");
442 #endif /* BUILD_CRAMDISK */