2 * Many of the syscalls used in this file expect some of the arguments
3 * to be __user pointers not __kernel pointers. To limit the sparse
4 * noise, turn off sparse checking for this file.
8 #warning "Sparse checking disabled for this file"
11 #include <linux/kernel.h>
13 #include <linux/minix_fs.h>
14 #include <linux/ext2_fs.h>
15 #include <linux/romfs_fs.h>
16 #include <uapi/linux/cramfs_fs.h>
17 #include <linux/initrd.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
21 #include "do_mounts.h"
22 #include "../fs/squashfs/squashfs_fs.h"
24 #include <linux/decompress/generic.h>
27 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
29 static int __init prompt_ramdisk(char *str)
31 rd_prompt = simple_strtol(str,NULL,0) & 1;
34 __setup("prompt_ramdisk=", prompt_ramdisk);
36 int __initdata rd_image_start; /* starting block # of image */
38 static int __init ramdisk_start_setup(char *str)
40 rd_image_start = simple_strtol(str,NULL,0);
43 __setup("ramdisk_start=", ramdisk_start_setup);
45 static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
48 * This routine tries to find a RAM disk image to load, and returns the
49 * number of blocks to read for a non-compressed image, 0 if the image
50 * is a compressed image, and -1 if an image with the right magic
51 * numbers could not be found.
53 * We currently check for the following magic numbers:
67 identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
70 struct minix_super_block *minixsb;
71 struct romfs_super_block *romfsb;
72 struct cramfs_super *cramfsb;
73 struct squashfs_super_block *squashfsb;
76 const char *compress_name;
79 buf = kmalloc(size, GFP_KERNEL);
83 minixsb = (struct minix_super_block *) buf;
84 romfsb = (struct romfs_super_block *) buf;
85 cramfsb = (struct cramfs_super *) buf;
86 squashfsb = (struct squashfs_super_block *) buf;
87 memset(buf, 0xe5, size);
90 * Read block 0 to test for compressed kernel
92 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
93 sys_read(fd, buf, size);
95 *decompressor = decompress_method(buf, size, &compress_name);
97 printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
98 compress_name, start_block);
101 "RAMDISK: %s decompressor not configured!\n",
107 /* romfs is at block zero too */
108 if (romfsb->word0 == ROMSB_WORD0 &&
109 romfsb->word1 == ROMSB_WORD1) {
111 "RAMDISK: romfs filesystem found at block %d\n",
113 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
117 if (cramfsb->magic == CRAMFS_MAGIC) {
119 "RAMDISK: cramfs filesystem found at block %d\n",
121 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
125 /* squashfs is at block zero too */
126 if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
128 "RAMDISK: squashfs filesystem found at block %d\n",
130 nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
136 * Read 512 bytes further to check if cramfs is padded
138 sys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0);
139 sys_read(fd, buf, size);
141 if (cramfsb->magic == CRAMFS_MAGIC) {
143 "RAMDISK: cramfs filesystem found at block %d\n",
145 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
150 * Read block 1 to test for minix and ext2 superblock
152 sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
153 sys_read(fd, buf, size);
156 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
157 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
159 "RAMDISK: Minix filesystem found at block %d\n",
161 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
166 n = ext2_image_size(buf);
169 "RAMDISK: ext2 filesystem found at block %d\n",
176 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
180 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
185 int __init rd_load_image(char *from)
189 unsigned long rd_blocks, devblocks;
190 int nblocks, i, disk;
192 unsigned short rotate = 0;
193 decompress_fn decompressor = NULL;
194 #if !defined(CONFIG_S390)
195 char rotator[4] = { '|' , '/' , '-' , '\\' };
198 out_fd = sys_open("/dev/ram", O_RDWR, 0);
202 in_fd = sys_open(from, O_RDONLY, 0);
206 nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
211 if (crd_load(in_fd, out_fd, decompressor) == 0)
212 goto successful_load;
217 * NOTE NOTE: nblocks is not actually blocks but
218 * the number of kibibytes of data to load into a ramdisk.
219 * So any ramdisk block size that is a multiple of 1KiB should
220 * work when the appropriate ramdisk_blocksize is specified
221 * on the command line.
223 * The default ramdisk_blocksize is 1KiB and it is generally
224 * silly to use anything else, so make sure to use 1KiB
225 * blocksize while generating ext2fs ramdisk-images.
227 if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
232 if (nblocks > rd_blocks) {
233 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
239 * OK, time to copy in the data
241 if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
246 if (strcmp(from, "/initrd.image") == 0)
249 if (devblocks == 0) {
250 printk(KERN_ERR "RAMDISK: could not determine device size\n");
254 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
256 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
260 printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
261 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
262 for (i = 0, disk = 1; i < nblocks; i++) {
263 if (i && (i % devblocks == 0)) {
264 printk("done disk #%d.\n", disk++);
266 if (sys_close(in_fd)) {
267 printk("Error closing the disk.\n");
270 change_floppy("disk #%d", disk);
271 in_fd = sys_open(from, O_RDONLY, 0);
273 printk("Error opening disk.\n");
276 printk("Loading disk #%d... ", disk);
278 sys_read(in_fd, buf, BLOCK_SIZE);
279 sys_write(out_fd, buf, BLOCK_SIZE);
280 #if !defined(CONFIG_S390)
282 printk("%c\b", rotator[rotate & 0x3]);
297 sys_unlink("/dev/ram");
301 int __init rd_load_disk(int n)
304 change_floppy("root floppy disk to be loaded into RAM disk");
305 create_dev("/dev/root", ROOT_DEV);
306 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
307 return rd_load_image("/dev/root");
310 static int exit_code;
311 static int decompress_error;
312 static int crd_infd, crd_outfd;
314 static long __init compr_fill(void *buf, unsigned long len)
316 long r = sys_read(crd_infd, buf, len);
318 printk(KERN_ERR "RAMDISK: error while reading compressed data");
320 printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
324 static long __init compr_flush(void *window, unsigned long outcnt)
326 long written = sys_write(crd_outfd, window, outcnt);
327 if (written != outcnt) {
328 if (decompress_error == 0)
330 "RAMDISK: incomplete write (%ld != %ld)\n",
332 decompress_error = 1;
338 static void __init error(char *x)
340 printk(KERN_ERR "%s\n", x);
342 decompress_error = 1;
345 static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
352 pr_emerg("Invalid ramdisk decompression routine. "
353 "Select appropriate config option.\n");
354 panic("Could not decompress initial ramdisk image.");
357 result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
358 if (decompress_error)