blob: 0f0f0cf3ba9aa97c34d88824fbf4644ca76b0fac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
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 Lougherb8fed872009-01-05 08:46:28 +000012#include "../fs/squashfs/squashfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
15
16static 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
23int __initdata rd_image_start; /* starting block # of image */
24
25static 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
32static 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 Lougherb8fed872009-01-05 08:46:28 +000045 * squashfs
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * gzip
47 */
48static int __init
49identify_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 Lougherb8fed872009-01-05 08:46:28 +000056 struct squashfs_super_block *squashfsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int nblocks = -1;
58 unsigned char *buf;
59
60 buf = kmalloc(size, GFP_KERNEL);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -070061 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 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 Lougherb8fed872009-01-05 08:46:28 +000068 squashfsb = (struct squashfs_super_block *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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 Uytterhoeven93fd85d2008-10-15 22:01:32 -070078 * If it matches the gzip magic numbers, return 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
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 Lougherb8fed872009-01-05 08:46:28 +0000106 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700116 /*
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
146done:
147 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
148 kfree(buf);
149 return nblocks;
150}
151
152int __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 Schwidefsky347a8dc2006-01-06 00:19:28 -0800160#if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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 Torvalds1da177e2005-04-16 15:20:36 -0700177 if (crd_load(in_fd, out_fd) == 0)
178 goto successful_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 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 Harrisond613c3e2008-04-28 14:13:14 -0700221 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 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 Schwidefsky347a8dc2006-01-06 00:19:28 -0800246#if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!(i % 16)) {
248 printk("%c\b", rotator[rotate & 0x3]);
249 rotate++;
250 }
251#endif
252 }
253 printk("done.\n");
254
255successful_load:
256 res = 1;
257done:
258 sys_close(in_fd);
259noclose_input:
260 sys_close(out_fd);
261out:
262 kfree(buf);
263 sys_unlink("/dev/ram");
264 return res;
265}
266
267int __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-Hartmanbdaf8522005-06-20 21:15:16 -0700271 create_dev("/dev/root", ROOT_DEV);
272 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return rd_load_image("/dev/root");
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
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
286typedef unsigned char uch;
287typedef unsigned short ush;
288typedef 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
294static uch *inbuf;
295static uch *window;
296
297static unsigned insize; /* valid bytes in inbuf */
298static unsigned inptr; /* index of next byte to be processed in inbuf */
299static unsigned outcnt; /* bytes in output buffer */
300static int exit_code;
301static int unzip_error;
302static long bytes_out;
303static 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
318static int __init fill_inbuf(void);
319static void __init flush_window(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static void __init error(char *m);
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700321
322#define NO_INFLATE_MALLOC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324#include "../lib/inflate.c"
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/* ===========================================================================
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 */
331static 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 */
350static 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
372static void __init error(char *x)
373{
374 printk(KERN_ERR "%s\n", x);
375 exit_code = 1;
376 unzip_error = 1;
377}
378
379static 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 Hemmingerc80544d2007-10-18 03:07:05 -0700393 if (!inbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
395 return -1;
396 }
397 window = kmalloc(WSIZE, GFP_KERNEL);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700398 if (!window) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 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}