Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 2 | #include <linux/kernel.h> |
| 3 | #include <linux/fs.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> |
| 10 | |
| 11 | #include "do_mounts.h" |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame^] | 12 | #include "../fs/squashfs/squashfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */ |
| 15 | |
| 16 | static int __init prompt_ramdisk(char *str) |
| 17 | { |
| 18 | rd_prompt = simple_strtol(str,NULL,0) & 1; |
| 19 | return 1; |
| 20 | } |
| 21 | __setup("prompt_ramdisk=", prompt_ramdisk); |
| 22 | |
| 23 | int __initdata rd_image_start; /* starting block # of image */ |
| 24 | |
| 25 | static int __init ramdisk_start_setup(char *str) |
| 26 | { |
| 27 | rd_image_start = simple_strtol(str,NULL,0); |
| 28 | return 1; |
| 29 | } |
| 30 | __setup("ramdisk_start=", ramdisk_start_setup); |
| 31 | |
| 32 | static int __init crd_load(int in_fd, int out_fd); |
| 33 | |
| 34 | /* |
| 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. |
| 39 | * |
| 40 | * We currently check for the following magic numbers: |
| 41 | * minix |
| 42 | * ext2 |
| 43 | * romfs |
| 44 | * cramfs |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame^] | 45 | * squashfs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * gzip |
| 47 | */ |
| 48 | static int __init |
| 49 | identify_ramdisk_image(int fd, int start_block) |
| 50 | { |
| 51 | const int size = 512; |
| 52 | struct minix_super_block *minixsb; |
| 53 | struct ext2_super_block *ext2sb; |
| 54 | struct romfs_super_block *romfsb; |
| 55 | struct cramfs_super *cramfsb; |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame^] | 56 | struct squashfs_super_block *squashfsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | int nblocks = -1; |
| 58 | unsigned char *buf; |
| 59 | |
| 60 | buf = kmalloc(size, GFP_KERNEL); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 61 | if (!buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return -1; |
| 63 | |
| 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; |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame^] | 68 | squashfsb = (struct squashfs_super_block *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | memset(buf, 0xe5, size); |
| 70 | |
| 71 | /* |
| 72 | * Read block 0 to test for gzipped kernel |
| 73 | */ |
| 74 | sys_lseek(fd, start_block * BLOCK_SIZE, 0); |
| 75 | sys_read(fd, buf, size); |
| 76 | |
| 77 | /* |
Geert Uytterhoeven | 93fd85d | 2008-10-15 22:01:32 -0700 | [diff] [blame] | 78 | * If it matches the gzip magic numbers, return 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
| 80 | if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) { |
| 81 | printk(KERN_NOTICE |
| 82 | "RAMDISK: Compressed image found at block %d\n", |
| 83 | start_block); |
| 84 | nblocks = 0; |
| 85 | goto done; |
| 86 | } |
| 87 | |
| 88 | /* romfs is at block zero too */ |
| 89 | if (romfsb->word0 == ROMSB_WORD0 && |
| 90 | romfsb->word1 == ROMSB_WORD1) { |
| 91 | printk(KERN_NOTICE |
| 92 | "RAMDISK: romfs filesystem found at block %d\n", |
| 93 | start_block); |
| 94 | nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS; |
| 95 | goto done; |
| 96 | } |
| 97 | |
| 98 | if (cramfsb->magic == CRAMFS_MAGIC) { |
| 99 | printk(KERN_NOTICE |
| 100 | "RAMDISK: cramfs filesystem found at block %d\n", |
| 101 | start_block); |
| 102 | nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS; |
| 103 | goto done; |
| 104 | } |
| 105 | |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame^] | 106 | /* squashfs is at block zero too */ |
| 107 | if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) { |
| 108 | printk(KERN_NOTICE |
| 109 | "RAMDISK: squashfs filesystem found at block %d\n", |
| 110 | start_block); |
| 111 | nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1) |
| 112 | >> BLOCK_SIZE_BITS; |
| 113 | goto done; |
| 114 | } |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* |
| 117 | * Read block 1 to test for minix and ext2 superblock |
| 118 | */ |
| 119 | sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0); |
| 120 | sys_read(fd, buf, size); |
| 121 | |
| 122 | /* Try minix */ |
| 123 | if (minixsb->s_magic == MINIX_SUPER_MAGIC || |
| 124 | minixsb->s_magic == MINIX_SUPER_MAGIC2) { |
| 125 | printk(KERN_NOTICE |
| 126 | "RAMDISK: Minix filesystem found at block %d\n", |
| 127 | start_block); |
| 128 | nblocks = minixsb->s_nzones << minixsb->s_log_zone_size; |
| 129 | goto done; |
| 130 | } |
| 131 | |
| 132 | /* Try ext2 */ |
| 133 | if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) { |
| 134 | printk(KERN_NOTICE |
| 135 | "RAMDISK: ext2 filesystem found at block %d\n", |
| 136 | start_block); |
| 137 | nblocks = le32_to_cpu(ext2sb->s_blocks_count) << |
| 138 | le32_to_cpu(ext2sb->s_log_block_size); |
| 139 | goto done; |
| 140 | } |
| 141 | |
| 142 | printk(KERN_NOTICE |
| 143 | "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n", |
| 144 | start_block); |
| 145 | |
| 146 | done: |
| 147 | sys_lseek(fd, start_block * BLOCK_SIZE, 0); |
| 148 | kfree(buf); |
| 149 | return nblocks; |
| 150 | } |
| 151 | |
| 152 | int __init rd_load_image(char *from) |
| 153 | { |
| 154 | int res = 0; |
| 155 | int in_fd, out_fd; |
| 156 | unsigned long rd_blocks, devblocks; |
| 157 | int nblocks, i, disk; |
| 158 | char *buf = NULL; |
| 159 | unsigned short rotate = 0; |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 160 | #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | char rotator[4] = { '|' , '/' , '-' , '\\' }; |
| 162 | #endif |
| 163 | |
| 164 | out_fd = sys_open("/dev/ram", O_RDWR, 0); |
| 165 | if (out_fd < 0) |
| 166 | goto out; |
| 167 | |
| 168 | in_fd = sys_open(from, O_RDONLY, 0); |
| 169 | if (in_fd < 0) |
| 170 | goto noclose_input; |
| 171 | |
| 172 | nblocks = identify_ramdisk_image(in_fd, rd_image_start); |
| 173 | if (nblocks < 0) |
| 174 | goto done; |
| 175 | |
| 176 | if (nblocks == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (crd_load(in_fd, out_fd) == 0) |
| 178 | goto successful_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | goto done; |
| 180 | } |
| 181 | |
| 182 | /* |
| 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. |
| 188 | * |
| 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. |
| 192 | */ |
| 193 | if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0) |
| 194 | rd_blocks = 0; |
| 195 | else |
| 196 | rd_blocks >>= 1; |
| 197 | |
| 198 | if (nblocks > rd_blocks) { |
| 199 | printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n", |
| 200 | nblocks, rd_blocks); |
| 201 | goto done; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * OK, time to copy in the data |
| 206 | */ |
| 207 | if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0) |
| 208 | devblocks = 0; |
| 209 | else |
| 210 | devblocks >>= 1; |
| 211 | |
| 212 | if (strcmp(from, "/initrd.image") == 0) |
| 213 | devblocks = nblocks; |
| 214 | |
| 215 | if (devblocks == 0) { |
| 216 | printk(KERN_ERR "RAMDISK: could not determine device size\n"); |
| 217 | goto done; |
| 218 | } |
| 219 | |
| 220 | buf = kmalloc(BLOCK_SIZE, GFP_KERNEL); |
Harvey Harrison | d613c3e | 2008-04-28 14:13:14 -0700 | [diff] [blame] | 221 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | printk(KERN_ERR "RAMDISK: could not allocate buffer\n"); |
| 223 | goto done; |
| 224 | } |
| 225 | |
| 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++); |
| 231 | rotate = 0; |
| 232 | if (sys_close(in_fd)) { |
| 233 | printk("Error closing the disk.\n"); |
| 234 | goto noclose_input; |
| 235 | } |
| 236 | change_floppy("disk #%d", disk); |
| 237 | in_fd = sys_open(from, O_RDONLY, 0); |
| 238 | if (in_fd < 0) { |
| 239 | printk("Error opening disk.\n"); |
| 240 | goto noclose_input; |
| 241 | } |
| 242 | printk("Loading disk #%d... ", disk); |
| 243 | } |
| 244 | sys_read(in_fd, buf, BLOCK_SIZE); |
| 245 | sys_write(out_fd, buf, BLOCK_SIZE); |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 246 | #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if (!(i % 16)) { |
| 248 | printk("%c\b", rotator[rotate & 0x3]); |
| 249 | rotate++; |
| 250 | } |
| 251 | #endif |
| 252 | } |
| 253 | printk("done.\n"); |
| 254 | |
| 255 | successful_load: |
| 256 | res = 1; |
| 257 | done: |
| 258 | sys_close(in_fd); |
| 259 | noclose_input: |
| 260 | sys_close(out_fd); |
| 261 | out: |
| 262 | kfree(buf); |
| 263 | sys_unlink("/dev/ram"); |
| 264 | return res; |
| 265 | } |
| 266 | |
| 267 | int __init rd_load_disk(int n) |
| 268 | { |
| 269 | if (rd_prompt) |
| 270 | change_floppy("root floppy disk to be loaded into RAM disk"); |
Greg Kroah-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 271 | create_dev("/dev/root", ROOT_DEV); |
| 272 | create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return rd_load_image("/dev/root"); |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* |
| 277 | * gzip declarations |
| 278 | */ |
| 279 | |
| 280 | #define OF(args) args |
| 281 | |
| 282 | #ifndef memzero |
| 283 | #define memzero(s, n) memset ((s), 0, (n)) |
| 284 | #endif |
| 285 | |
| 286 | typedef unsigned char uch; |
| 287 | typedef unsigned short ush; |
| 288 | typedef unsigned long ulg; |
| 289 | |
| 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 */ |
| 293 | |
| 294 | static uch *inbuf; |
| 295 | static uch *window; |
| 296 | |
| 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; |
| 304 | |
| 305 | #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) |
| 306 | |
| 307 | /* Diagnostic functions (stubbed out) */ |
| 308 | #define Assert(cond,msg) |
| 309 | #define Trace(x) |
| 310 | #define Tracev(x) |
| 311 | #define Tracevv(x) |
| 312 | #define Tracec(c,x) |
| 313 | #define Tracecv(c,x) |
| 314 | |
| 315 | #define STATIC static |
| 316 | #define INIT __init |
| 317 | |
| 318 | static int __init fill_inbuf(void); |
| 319 | static void __init flush_window(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | static void __init error(char *m); |
Thomas Petazzoni | 2d6ffcc | 2008-07-25 01:45:44 -0700 | [diff] [blame] | 321 | |
| 322 | #define NO_INFLATE_MALLOC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | #include "../lib/inflate.c" |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 330 | */ |
| 331 | static int __init fill_inbuf(void) |
| 332 | { |
| 333 | if (exit_code) return -1; |
| 334 | |
| 335 | insize = sys_read(crd_infd, inbuf, INBUFSIZ); |
| 336 | if (insize == 0) { |
| 337 | error("RAMDISK: ran out of compressed data"); |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | inptr = 1; |
| 342 | |
| 343 | return inbuf[0]; |
| 344 | } |
| 345 | |
| 346 | /* =========================================================================== |
| 347 | * Write the output window window[0..outcnt-1] and update crc and bytes_out. |
| 348 | * (Used for the decompressed data only.) |
| 349 | */ |
| 350 | static void __init flush_window(void) |
| 351 | { |
| 352 | ulg c = crc; /* temporary variable */ |
| 353 | unsigned n, written; |
| 354 | uch *in, ch; |
| 355 | |
| 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); |
| 360 | unzip_error = 1; |
| 361 | } |
| 362 | in = window; |
| 363 | for (n = 0; n < outcnt; n++) { |
| 364 | ch = *in++; |
| 365 | c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); |
| 366 | } |
| 367 | crc = c; |
| 368 | bytes_out += (ulg)outcnt; |
| 369 | outcnt = 0; |
| 370 | } |
| 371 | |
| 372 | static void __init error(char *x) |
| 373 | { |
| 374 | printk(KERN_ERR "%s\n", x); |
| 375 | exit_code = 1; |
| 376 | unzip_error = 1; |
| 377 | } |
| 378 | |
| 379 | static int __init crd_load(int in_fd, int out_fd) |
| 380 | { |
| 381 | int result; |
| 382 | |
| 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 */ |
| 386 | exit_code = 0; |
| 387 | bytes_out = 0; |
| 388 | crc = (ulg)0xffffffffL; /* shift register contents */ |
| 389 | |
| 390 | crd_infd = in_fd; |
| 391 | crd_outfd = out_fd; |
| 392 | inbuf = kmalloc(INBUFSIZ, GFP_KERNEL); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 393 | if (!inbuf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n"); |
| 395 | return -1; |
| 396 | } |
| 397 | window = kmalloc(WSIZE, GFP_KERNEL); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 398 | if (!window) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n"); |
| 400 | kfree(inbuf); |
| 401 | return -1; |
| 402 | } |
| 403 | makecrc(); |
| 404 | result = gunzip(); |
| 405 | if (unzip_error) |
| 406 | result = 1; |
| 407 | kfree(inbuf); |
| 408 | kfree(window); |
| 409 | return result; |
| 410 | } |