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 #include <linux/decompress/generic.h>
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, decompress_fn deco);
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:
50 identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
53 struct minix_super_block *minixsb;
54 struct ext2_super_block *ext2sb;
55 struct romfs_super_block *romfsb;
56 struct cramfs_super *cramfsb;
59 const char *compress_name;
61 buf = kmalloc(size, GFP_KERNEL);
65 minixsb = (struct minix_super_block *) buf;
66 ext2sb = (struct ext2_super_block *) buf;
67 romfsb = (struct romfs_super_block *) buf;
68 cramfsb = (struct cramfs_super *) buf;
69 memset(buf, 0xe5, size);
72 * Read block 0 to test for compressed kernel
74 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
75 sys_read(fd, buf, size);
77 *decompressor = decompress_method(buf, size, &compress_name);
79 printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
80 compress_name, start_block);
85 /* romfs is at block zero too */
86 if (romfsb->word0 == ROMSB_WORD0 &&
87 romfsb->word1 == ROMSB_WORD1) {
89 "RAMDISK: romfs filesystem found at block %d\n",
91 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
95 if (cramfsb->magic == CRAMFS_MAGIC) {
97 "RAMDISK: cramfs filesystem found at block %d\n",
99 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
104 * Read block 1 to test for minix and ext2 superblock
106 sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
107 sys_read(fd, buf, size);
110 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
111 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
113 "RAMDISK: Minix filesystem found at block %d\n",
115 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
120 if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
122 "RAMDISK: ext2 filesystem found at block %d\n",
124 nblocks = le32_to_cpu(ext2sb->s_blocks_count) <<
125 le32_to_cpu(ext2sb->s_log_block_size);
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 decompress_fn decompressor = NULL;
148 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
149 char rotator[4] = { '|' , '/' , '-' , '\\' };
152 out_fd = sys_open("/dev/ram", O_RDWR, 0);
156 in_fd = sys_open(from, O_RDONLY, 0);
160 nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
165 if (crd_load(in_fd, out_fd, decompressor) == 0)
166 goto successful_load;
171 * NOTE NOTE: nblocks is not actually blocks but
172 * the number of kibibytes of data to load into a ramdisk.
173 * So any ramdisk block size that is a multiple of 1KiB should
174 * work when the appropriate ramdisk_blocksize is specified
175 * on the command line.
177 * The default ramdisk_blocksize is 1KiB and it is generally
178 * silly to use anything else, so make sure to use 1KiB
179 * blocksize while generating ext2fs ramdisk-images.
181 if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
186 if (nblocks > rd_blocks) {
187 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\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 %dKiB [%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_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);
260 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
261 return rd_load_image("/dev/root");
264 static int exit_code;
265 static int decompress_error;
266 static int crd_infd, crd_outfd;
268 static int __init compr_fill(void *buf, unsigned int len)
270 int r = sys_read(crd_infd, buf, len);
272 printk(KERN_ERR "RAMDISK: error while reading compressed data");
274 printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
278 static int __init compr_flush(void *window, unsigned int outcnt)
280 int written = sys_write(crd_outfd, window, outcnt);
281 if (written != outcnt) {
282 if (decompress_error == 0)
284 "RAMDISK: incomplete write (%d != %d)\n",
286 decompress_error = 1;
292 static void __init error(char *x)
294 printk(KERN_ERR "%s\n", x);
296 decompress_error = 1;
299 static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
304 result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
305 if (decompress_error)