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"
12 #include "../fs/squashfs/squashfs_fs.h"
14 int __initdata rd_prompt
= 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
16 static int __init
prompt_ramdisk(char *str
)
18 rd_prompt
= simple_strtol(str
,NULL
,0) & 1;
21 __setup("prompt_ramdisk=", prompt_ramdisk
);
23 int __initdata rd_image_start
; /* starting block # of image */
25 static int __init
ramdisk_start_setup(char *str
)
27 rd_image_start
= simple_strtol(str
,NULL
,0);
30 __setup("ramdisk_start=", ramdisk_start_setup
);
32 static int __init
crd_load(int in_fd
, int out_fd
);
35 * This routine tries to find a RAM disk image to load, and returns the
36 * number of blocks to read for a non-compressed image, 0 if the image
37 * is a compressed image, and -1 if an image with the right magic
38 * numbers could not be found.
40 * 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
;
56 struct squashfs_super_block
*squashfsb
;
60 buf
= kmalloc(size
, GFP_KERNEL
);
64 minixsb
= (struct minix_super_block
*) buf
;
65 ext2sb
= (struct ext2_super_block
*) buf
;
66 romfsb
= (struct romfs_super_block
*) buf
;
67 cramfsb
= (struct cramfs_super
*) buf
;
68 squashfsb
= (struct squashfs_super_block
*) buf
;
69 memset(buf
, 0xe5, size
);
72 * Read block 0 to test for gzipped kernel
74 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
75 sys_read(fd
, buf
, size
);
78 * If it matches the gzip magic numbers, return 0
80 if (buf
[0] == 037 && ((buf
[1] == 0213) || (buf
[1] == 0236))) {
82 "RAMDISK: Compressed image found at block %d\n",
88 /* romfs is at block zero too */
89 if (romfsb
->word0
== ROMSB_WORD0
&&
90 romfsb
->word1
== ROMSB_WORD1
) {
92 "RAMDISK: romfs filesystem found at block %d\n",
94 nblocks
= (ntohl(romfsb
->size
)+BLOCK_SIZE
-1)>>BLOCK_SIZE_BITS
;
98 if (cramfsb
->magic
== CRAMFS_MAGIC
) {
100 "RAMDISK: cramfs filesystem found at block %d\n",
102 nblocks
= (cramfsb
->size
+ BLOCK_SIZE
- 1) >> BLOCK_SIZE_BITS
;
106 /* squashfs is at block zero too */
107 if (le32_to_cpu(squashfsb
->s_magic
) == SQUASHFS_MAGIC
) {
109 "RAMDISK: squashfs filesystem found at block %d\n",
111 nblocks
= (le64_to_cpu(squashfsb
->bytes_used
) + BLOCK_SIZE
- 1)
117 * Read block 1 to test for minix and ext2 superblock
119 sys_lseek(fd
, (start_block
+1) * BLOCK_SIZE
, 0);
120 sys_read(fd
, buf
, size
);
123 if (minixsb
->s_magic
== MINIX_SUPER_MAGIC
||
124 minixsb
->s_magic
== MINIX_SUPER_MAGIC2
) {
126 "RAMDISK: Minix filesystem found at block %d\n",
128 nblocks
= minixsb
->s_nzones
<< minixsb
->s_log_zone_size
;
133 if (ext2sb
->s_magic
== cpu_to_le16(EXT2_SUPER_MAGIC
)) {
135 "RAMDISK: ext2 filesystem found at block %d\n",
137 nblocks
= le32_to_cpu(ext2sb
->s_blocks_count
) <<
138 le32_to_cpu(ext2sb
->s_log_block_size
);
143 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
147 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
152 int __init
rd_load_image(char *from
)
156 unsigned long rd_blocks
, devblocks
;
157 int nblocks
, i
, disk
;
159 unsigned short rotate
= 0;
160 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
161 char rotator
[4] = { '|' , '/' , '-' , '\\' };
164 out_fd
= sys_open("/dev/ram", O_RDWR
, 0);
168 in_fd
= sys_open(from
, O_RDONLY
, 0);
172 nblocks
= identify_ramdisk_image(in_fd
, rd_image_start
);
177 if (crd_load(in_fd
, out_fd
) == 0)
178 goto successful_load
;
183 * NOTE NOTE: nblocks is not actually blocks but
184 * the number of kibibytes of data to load into a ramdisk.
185 * So any ramdisk block size that is a multiple of 1KiB should
186 * work when the appropriate ramdisk_blocksize is specified
187 * on the command line.
189 * The default ramdisk_blocksize is 1KiB and it is generally
190 * silly to use anything else, so make sure to use 1KiB
191 * blocksize while generating ext2fs ramdisk-images.
193 if (sys_ioctl(out_fd
, BLKGETSIZE
, (unsigned long)&rd_blocks
) < 0)
198 if (nblocks
> rd_blocks
) {
199 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
205 * OK, time to copy in the data
207 if (sys_ioctl(in_fd
, BLKGETSIZE
, (unsigned long)&devblocks
) < 0)
212 if (strcmp(from
, "/initrd.image") == 0)
215 if (devblocks
== 0) {
216 printk(KERN_ERR
"RAMDISK: could not determine device size\n");
220 buf
= kmalloc(BLOCK_SIZE
, GFP_KERNEL
);
222 printk(KERN_ERR
"RAMDISK: could not allocate buffer\n");
226 printk(KERN_NOTICE
"RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
227 nblocks
, ((nblocks
-1)/devblocks
)+1, nblocks
>devblocks
? "s" : "");
228 for (i
= 0, disk
= 1; i
< nblocks
; i
++) {
229 if (i
&& (i
% devblocks
== 0)) {
230 printk("done disk #%d.\n", disk
++);
232 if (sys_close(in_fd
)) {
233 printk("Error closing the disk.\n");
236 change_floppy("disk #%d", disk
);
237 in_fd
= sys_open(from
, O_RDONLY
, 0);
239 printk("Error opening disk.\n");
242 printk("Loading disk #%d... ", disk
);
244 sys_read(in_fd
, buf
, BLOCK_SIZE
);
245 sys_write(out_fd
, buf
, BLOCK_SIZE
);
246 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
248 printk("%c\b", rotator
[rotate
& 0x3]);
263 sys_unlink("/dev/ram");
267 int __init
rd_load_disk(int n
)
270 change_floppy("root floppy disk to be loaded into RAM disk");
271 create_dev("/dev/root", ROOT_DEV
);
272 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR
, n
));
273 return rd_load_image("/dev/root");
280 #define OF(args) args
283 #define memzero(s, n) memset ((s), 0, (n))
286 typedef unsigned char uch
;
287 typedef unsigned short ush
;
288 typedef unsigned long ulg
;
290 #define INBUFSIZ 4096
291 #define WSIZE 0x8000 /* window size--must be a power of two, and */
292 /* at least 32K for zip's deflate method */
297 static unsigned insize
; /* valid bytes in inbuf */
298 static unsigned inptr
; /* index of next byte to be processed in inbuf */
299 static unsigned outcnt
; /* bytes in output buffer */
300 static int exit_code
;
301 static int unzip_error
;
302 static long bytes_out
;
303 static int crd_infd
, crd_outfd
;
305 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
307 /* Diagnostic functions (stubbed out) */
308 #define Assert(cond,msg)
315 #define STATIC static
318 static int __init
fill_inbuf(void);
319 static void __init
flush_window(void);
320 static void __init
error(char *m
);
322 #define NO_INFLATE_MALLOC
324 #include "../lib/inflate.c"
326 /* ===========================================================================
327 * Fill the input buffer. This is called only when the buffer is empty
328 * and at least one byte is really needed.
329 * Returning -1 does not guarantee that gunzip() will ever return.
331 static int __init
fill_inbuf(void)
333 if (exit_code
) return -1;
335 insize
= sys_read(crd_infd
, inbuf
, INBUFSIZ
);
337 error("RAMDISK: ran out of compressed data");
346 /* ===========================================================================
347 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
348 * (Used for the decompressed data only.)
350 static void __init
flush_window(void)
352 ulg c
= crc
; /* temporary variable */
356 written
= sys_write(crd_outfd
, window
, outcnt
);
357 if (written
!= outcnt
&& unzip_error
== 0) {
358 printk(KERN_ERR
"RAMDISK: incomplete write (%d != %d) %ld\n",
359 written
, outcnt
, bytes_out
);
363 for (n
= 0; n
< outcnt
; n
++) {
365 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
368 bytes_out
+= (ulg
)outcnt
;
372 static void __init
error(char *x
)
374 printk(KERN_ERR
"%s\n", x
);
379 static int __init
crd_load(int in_fd
, int out_fd
)
383 insize
= 0; /* valid bytes in inbuf */
384 inptr
= 0; /* index of next byte to be processed in inbuf */
385 outcnt
= 0; /* bytes in output buffer */
388 crc
= (ulg
)0xffffffffL
; /* shift register contents */
392 inbuf
= kmalloc(INBUFSIZ
, GFP_KERNEL
);
394 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip buffer\n");
397 window
= kmalloc(WSIZE
, GFP_KERNEL
);
399 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip window\n");