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.
220 if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
225 if (nblocks > rd_blocks) {
226 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
232 * OK, time to copy in the data
234 if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
239 if (strcmp(from, "/initrd.image") == 0)
242 if (devblocks == 0) {
243 printk(KERN_ERR "RAMDISK: could not determine device size\n");
247 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
249 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
253 printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
254 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
255 for (i = 0, disk = 1; i < nblocks; i++) {
256 if (i && (i % devblocks == 0)) {
257 printk("done disk #%d.\n", disk++);
259 if (sys_close(in_fd)) {
260 printk("Error closing the disk.\n");
263 change_floppy("disk #%d", disk);
264 in_fd = sys_open(from, O_RDONLY, 0);
266 printk("Error opening disk.\n");
269 printk("Loading disk #%d... ", disk);
271 sys_read(in_fd, buf, BLOCK_SIZE);
272 sys_write(out_fd, buf, BLOCK_SIZE);
273 #if !defined(CONFIG_S390)
275 printk("%c\b", rotator[rotate & 0x3]);
290 sys_unlink("/dev/ram");
294 int __init rd_load_disk(int n)
297 change_floppy("root floppy disk to be loaded into RAM disk");
298 create_dev("/dev/root", ROOT_DEV);
299 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
300 return rd_load_image("/dev/root");
303 static int exit_code;
304 static int decompress_error;
305 static int crd_infd, crd_outfd;
307 static long __init compr_fill(void *buf, unsigned long len)
309 long r = sys_read(crd_infd, buf, len);
311 printk(KERN_ERR "RAMDISK: error while reading compressed data");
313 printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
317 static long __init compr_flush(void *window, unsigned long outcnt)
319 long written = sys_write(crd_outfd, window, outcnt);
320 if (written != outcnt) {
321 if (decompress_error == 0)
323 "RAMDISK: incomplete write (%ld != %ld)\n",
325 decompress_error = 1;
331 static void __init error(char *x)
333 printk(KERN_ERR "%s\n", x);
335 decompress_error = 1;
338 static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
345 pr_emerg("Invalid ramdisk decompression routine. "
346 "Select appropriate config option.\n");
347 panic("Could not decompress initial ramdisk image.");
350 result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
351 if (decompress_error)