0001
0002 #include <linux/kernel.h>
0003 #include <linux/fs.h>
0004 #include <linux/minix_fs.h>
0005 #include <linux/ext2_fs.h>
0006 #include <linux/romfs_fs.h>
0007 #include <uapi/linux/cramfs_fs.h>
0008 #include <linux/initrd.h>
0009 #include <linux/string.h>
0010 #include <linux/slab.h>
0011
0012 #include "do_mounts.h"
0013 #include "../fs/squashfs/squashfs_fs.h"
0014
0015 #include <linux/decompress/generic.h>
0016
0017 static struct file *in_file, *out_file;
0018 static loff_t in_pos, out_pos;
0019
0020 static int __init prompt_ramdisk(char *str)
0021 {
0022 pr_warn("ignoring the deprecated prompt_ramdisk= option\n");
0023 return 1;
0024 }
0025 __setup("prompt_ramdisk=", prompt_ramdisk);
0026
0027 int __initdata rd_image_start;
0028
0029 static int __init ramdisk_start_setup(char *str)
0030 {
0031 rd_image_start = simple_strtol(str,NULL,0);
0032 return 1;
0033 }
0034 __setup("ramdisk_start=", ramdisk_start_setup);
0035
0036 static int __init crd_load(decompress_fn deco);
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 static int __init
0058 identify_ramdisk_image(struct file *file, loff_t pos,
0059 decompress_fn *decompressor)
0060 {
0061 const int size = 512;
0062 struct minix_super_block *minixsb;
0063 struct romfs_super_block *romfsb;
0064 struct cramfs_super *cramfsb;
0065 struct squashfs_super_block *squashfsb;
0066 int nblocks = -1;
0067 unsigned char *buf;
0068 const char *compress_name;
0069 unsigned long n;
0070 int start_block = rd_image_start;
0071
0072 buf = kmalloc(size, GFP_KERNEL);
0073 if (!buf)
0074 return -ENOMEM;
0075
0076 minixsb = (struct minix_super_block *) buf;
0077 romfsb = (struct romfs_super_block *) buf;
0078 cramfsb = (struct cramfs_super *) buf;
0079 squashfsb = (struct squashfs_super_block *) buf;
0080 memset(buf, 0xe5, size);
0081
0082
0083
0084
0085 pos = start_block * BLOCK_SIZE;
0086 kernel_read(file, buf, size, &pos);
0087
0088 *decompressor = decompress_method(buf, size, &compress_name);
0089 if (compress_name) {
0090 printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
0091 compress_name, start_block);
0092 if (!*decompressor)
0093 printk(KERN_EMERG
0094 "RAMDISK: %s decompressor not configured!\n",
0095 compress_name);
0096 nblocks = 0;
0097 goto done;
0098 }
0099
0100
0101 if (romfsb->word0 == ROMSB_WORD0 &&
0102 romfsb->word1 == ROMSB_WORD1) {
0103 printk(KERN_NOTICE
0104 "RAMDISK: romfs filesystem found at block %d\n",
0105 start_block);
0106 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
0107 goto done;
0108 }
0109
0110 if (cramfsb->magic == CRAMFS_MAGIC) {
0111 printk(KERN_NOTICE
0112 "RAMDISK: cramfs filesystem found at block %d\n",
0113 start_block);
0114 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
0115 goto done;
0116 }
0117
0118
0119 if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
0120 printk(KERN_NOTICE
0121 "RAMDISK: squashfs filesystem found at block %d\n",
0122 start_block);
0123 nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
0124 >> BLOCK_SIZE_BITS;
0125 goto done;
0126 }
0127
0128
0129
0130
0131 pos = start_block * BLOCK_SIZE + 0x200;
0132 kernel_read(file, buf, size, &pos);
0133
0134 if (cramfsb->magic == CRAMFS_MAGIC) {
0135 printk(KERN_NOTICE
0136 "RAMDISK: cramfs filesystem found at block %d\n",
0137 start_block);
0138 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
0139 goto done;
0140 }
0141
0142
0143
0144
0145 pos = (start_block + 1) * BLOCK_SIZE;
0146 kernel_read(file, buf, size, &pos);
0147
0148
0149 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
0150 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
0151 printk(KERN_NOTICE
0152 "RAMDISK: Minix filesystem found at block %d\n",
0153 start_block);
0154 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
0155 goto done;
0156 }
0157
0158
0159 n = ext2_image_size(buf);
0160 if (n) {
0161 printk(KERN_NOTICE
0162 "RAMDISK: ext2 filesystem found at block %d\n",
0163 start_block);
0164 nblocks = n;
0165 goto done;
0166 }
0167
0168 printk(KERN_NOTICE
0169 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
0170 start_block);
0171
0172 done:
0173 kfree(buf);
0174 return nblocks;
0175 }
0176
0177 static unsigned long nr_blocks(struct file *file)
0178 {
0179 struct inode *inode = file->f_mapping->host;
0180
0181 if (!S_ISBLK(inode->i_mode))
0182 return 0;
0183 return i_size_read(inode) >> 10;
0184 }
0185
0186 int __init rd_load_image(char *from)
0187 {
0188 int res = 0;
0189 unsigned long rd_blocks, devblocks;
0190 int nblocks, i;
0191 char *buf = NULL;
0192 unsigned short rotate = 0;
0193 decompress_fn decompressor = NULL;
0194 #if !defined(CONFIG_S390)
0195 char rotator[4] = { '|' , '/' , '-' , '\\' };
0196 #endif
0197
0198 out_file = filp_open("/dev/ram", O_RDWR, 0);
0199 if (IS_ERR(out_file))
0200 goto out;
0201
0202 in_file = filp_open(from, O_RDONLY, 0);
0203 if (IS_ERR(in_file))
0204 goto noclose_input;
0205
0206 in_pos = rd_image_start * BLOCK_SIZE;
0207 nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
0208 if (nblocks < 0)
0209 goto done;
0210
0211 if (nblocks == 0) {
0212 if (crd_load(decompressor) == 0)
0213 goto successful_load;
0214 goto done;
0215 }
0216
0217
0218
0219
0220
0221 rd_blocks = nr_blocks(out_file);
0222 if (nblocks > rd_blocks) {
0223 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
0224 nblocks, rd_blocks);
0225 goto done;
0226 }
0227
0228
0229
0230
0231 if (strcmp(from, "/initrd.image") == 0)
0232 devblocks = nblocks;
0233 else
0234 devblocks = nr_blocks(in_file);
0235
0236 if (devblocks == 0) {
0237 printk(KERN_ERR "RAMDISK: could not determine device size\n");
0238 goto done;
0239 }
0240
0241 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
0242 if (!buf) {
0243 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
0244 goto done;
0245 }
0246
0247 printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
0248 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
0249 for (i = 0; i < nblocks; i++) {
0250 if (i && (i % devblocks == 0)) {
0251 pr_cont("done disk #1.\n");
0252 rotate = 0;
0253 fput(in_file);
0254 break;
0255 }
0256 kernel_read(in_file, buf, BLOCK_SIZE, &in_pos);
0257 kernel_write(out_file, buf, BLOCK_SIZE, &out_pos);
0258 #if !defined(CONFIG_S390)
0259 if (!(i % 16)) {
0260 pr_cont("%c\b", rotator[rotate & 0x3]);
0261 rotate++;
0262 }
0263 #endif
0264 }
0265 pr_cont("done.\n");
0266
0267 successful_load:
0268 res = 1;
0269 done:
0270 fput(in_file);
0271 noclose_input:
0272 fput(out_file);
0273 out:
0274 kfree(buf);
0275 init_unlink("/dev/ram");
0276 return res;
0277 }
0278
0279 int __init rd_load_disk(int n)
0280 {
0281 create_dev("/dev/root", ROOT_DEV);
0282 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
0283 return rd_load_image("/dev/root");
0284 }
0285
0286 static int exit_code;
0287 static int decompress_error;
0288
0289 static long __init compr_fill(void *buf, unsigned long len)
0290 {
0291 long r = kernel_read(in_file, buf, len, &in_pos);
0292 if (r < 0)
0293 printk(KERN_ERR "RAMDISK: error while reading compressed data");
0294 else if (r == 0)
0295 printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
0296 return r;
0297 }
0298
0299 static long __init compr_flush(void *window, unsigned long outcnt)
0300 {
0301 long written = kernel_write(out_file, window, outcnt, &out_pos);
0302 if (written != outcnt) {
0303 if (decompress_error == 0)
0304 printk(KERN_ERR
0305 "RAMDISK: incomplete write (%ld != %ld)\n",
0306 written, outcnt);
0307 decompress_error = 1;
0308 return -1;
0309 }
0310 return outcnt;
0311 }
0312
0313 static void __init error(char *x)
0314 {
0315 printk(KERN_ERR "%s\n", x);
0316 exit_code = 1;
0317 decompress_error = 1;
0318 }
0319
0320 static int __init crd_load(decompress_fn deco)
0321 {
0322 int result;
0323
0324 if (!deco) {
0325 pr_emerg("Invalid ramdisk decompression routine. "
0326 "Select appropriate config option.\n");
0327 panic("Could not decompress initial ramdisk image.");
0328 }
0329
0330 result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
0331 if (decompress_error)
0332 result = 1;
0333 return result;
0334 }