MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / init / do_mounts_rd.c
blobe9ed48840d09ef6b03c4fdfe95cc9bbf24762f97
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/squashfs_fs.h>
9 #include <linux/initrd.h>
10 #include <linux/string.h>
12 #include "do_mounts.h"
14 #define BUILD_CRAMDISK
16 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
18 static int __init prompt_ramdisk(char *str)
20 rd_prompt = simple_strtol(str,NULL,0) & 1;
21 return 1;
23 __setup("prompt_ramdisk=", prompt_ramdisk);
25 int __initdata rd_image_start; /* starting block # of image */
27 static int __init ramdisk_start_setup(char *str)
29 rd_image_start = simple_strtol(str,NULL,0);
30 return 1;
32 __setup("ramdisk_start=", ramdisk_start_setup);
34 static int __init crd_load(int in_fd, int out_fd);
37 * This routine tries to find a RAM disk image to load, and returns the
38 * number of blocks to read for a non-compressed image, 0 if the image
39 * is a compressed image, and -1 if an image with the right magic
40 * numbers could not be found.
42 * We currently check for the following magic numbers:
43 * squashfs
44 * minix
45 * ext2
46 * romfs
47 * cramfs
48 * gzip
50 static int __init
51 identify_ramdisk_image(int fd, int start_block)
53 const int size = 512;
54 struct minix_super_block *minixsb;
55 struct ext2_super_block *ext2sb;
56 struct romfs_super_block *romfsb;
57 struct cramfs_super *cramfsb;
58 struct squashfs_super_block *squashfsb;
59 int nblocks = -1;
60 unsigned char *buf;
62 buf = kmalloc(size, GFP_KERNEL);
63 if (buf == 0)
64 return -1;
66 minixsb = (struct minix_super_block *) buf;
67 ext2sb = (struct ext2_super_block *) buf;
68 romfsb = (struct romfs_super_block *) buf;
69 cramfsb = (struct cramfs_super *) buf;
70 squashfsb = (struct squashfs_super_block *) buf;
71 memset(buf, 0xe5, size);
74 * Read block 0 to test for gzipped kernel
76 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
77 sys_read(fd, buf, size);
80 * If it matches the gzip magic numbers, return -1
82 if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
83 printk(KERN_NOTICE
84 "RAMDISK: Compressed image found at block %d\n",
85 start_block);
86 nblocks = 0;
87 goto done;
90 /* romfs is at block zero too */
91 if (romfsb->word0 == ROMSB_WORD0 &&
92 romfsb->word1 == ROMSB_WORD1) {
93 printk(KERN_NOTICE
94 "RAMDISK: romfs filesystem found at block %d\n",
95 start_block);
96 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
97 goto done;
100 if (cramfsb->magic == le32_to_cpu(CRAMFS_MAGIC)) {
101 printk(KERN_NOTICE
102 "RAMDISK: cramfs filesystem found at block %d\n",
103 start_block);
104 nblocks = (le32_to_cpu(cramfsb->size) + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
105 goto done;
108 /* squashfs is at block zero too */
109 if (squashfsb->s_magic == SQUASHFS_MAGIC) {
110 printk(KERN_NOTICE
111 "RAMDISK: squashfs filesystem found at block %d\n",
112 start_block);
113 nblocks = (squashfsb->bytes_used+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
114 goto done;
118 * Read block 1 to test for minix and ext2 superblock
120 sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
121 sys_read(fd, buf, size);
123 /* Try minix */
124 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
125 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
126 printk(KERN_NOTICE
127 "RAMDISK: Minix filesystem found at block %d\n",
128 start_block);
129 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
130 goto done;
133 /* Try ext2 */
134 if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
135 printk(KERN_NOTICE
136 "RAMDISK: ext2 filesystem found at block %d\n",
137 start_block);
138 nblocks = le32_to_cpu(ext2sb->s_blocks_count) <<
139 le32_to_cpu(ext2sb->s_log_block_size);
140 goto done;
143 printk(KERN_NOTICE
144 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
145 start_block);
147 done:
148 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
149 kfree(buf);
150 return nblocks;
153 int __init rd_load_image(char *from)
155 int res = 0;
156 int in_fd, out_fd;
157 unsigned long rd_blocks, devblocks;
158 int nblocks, i, disk;
159 char *buf = NULL;
160 unsigned short rotate = 0;
161 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
162 char rotator[4] = { '|' , '/' , '-' , '\\' };
163 #endif
165 out_fd = sys_open("/dev/ram", O_RDWR, 0);
166 if (out_fd < 0)
167 goto out;
169 in_fd = sys_open(from, O_RDONLY, 0);
170 if (in_fd < 0)
171 goto noclose_input;
173 nblocks = identify_ramdisk_image(in_fd, rd_image_start);
174 if (nblocks < 0)
175 goto done;
177 if (nblocks == 0) {
178 #ifdef BUILD_CRAMDISK
179 if (crd_load(in_fd, out_fd) == 0)
180 goto successful_load;
181 #else
182 printk(KERN_NOTICE
183 "RAMDISK: Kernel does not support compressed "
184 "RAM disk images\n");
185 #endif
186 goto done;
190 * NOTE NOTE: nblocks is not actually blocks but
191 * the number of kibibytes of data to load into a ramdisk.
192 * So any ramdisk block size that is a multiple of 1KiB should
193 * work when the appropriate ramdisk_blocksize is specified
194 * on the command line.
196 * The default ramdisk_blocksize is 1KiB and it is generally
197 * silly to use anything else, so make sure to use 1KiB
198 * blocksize while generating ext2fs ramdisk-images.
200 if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
201 rd_blocks = 0;
202 else
203 rd_blocks >>= 1;
205 if (nblocks > rd_blocks) {
206 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
207 nblocks, rd_blocks);
208 goto done;
212 * OK, time to copy in the data
214 if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
215 devblocks = 0;
216 else
217 devblocks >>= 1;
219 if (strcmp(from, "/initrd.image") == 0)
220 devblocks = nblocks;
222 if (devblocks == 0) {
223 printk(KERN_ERR "RAMDISK: could not determine device size\n");
224 goto done;
227 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
228 if (buf == 0) {
229 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
230 goto done;
233 printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
234 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
235 for (i = 0, disk = 1; i < nblocks; i++) {
236 if (i && (i % devblocks == 0)) {
237 printk("done disk #%d.\n", disk++);
238 rotate = 0;
239 if (sys_close(in_fd)) {
240 printk("Error closing the disk.\n");
241 goto noclose_input;
243 change_floppy("disk #%d", disk);
244 in_fd = sys_open(from, O_RDONLY, 0);
245 if (in_fd < 0) {
246 printk("Error opening disk.\n");
247 goto noclose_input;
249 printk("Loading disk #%d... ", disk);
251 sys_read(in_fd, buf, BLOCK_SIZE);
252 sys_write(out_fd, buf, BLOCK_SIZE);
253 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
254 if (!(i % 16)) {
255 printk("%c\b", rotator[rotate & 0x3]);
256 rotate++;
258 #endif
260 printk("done.\n");
262 successful_load:
263 res = 1;
264 done:
265 sys_close(in_fd);
266 noclose_input:
267 sys_close(out_fd);
268 out:
269 kfree(buf);
270 sys_unlink("/dev/ram");
271 return res;
274 int __init rd_load_disk(int n)
276 if (rd_prompt)
277 change_floppy("root floppy disk to be loaded into RAM disk");
278 create_dev("/dev/root", ROOT_DEV);
279 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
280 return rd_load_image("/dev/root");
283 #ifdef BUILD_CRAMDISK
286 * gzip declarations
289 #define OF(args) args
291 #ifndef memzero
292 #define memzero(s, n) memset ((s), 0, (n))
293 #endif
295 typedef unsigned char uch;
296 typedef unsigned short ush;
297 typedef unsigned long ulg;
299 #define INBUFSIZ 4096
300 #define WSIZE 0x8000 /* window size--must be a power of two, and */
301 /* at least 32K for zip's deflate method */
303 static uch *inbuf;
304 static uch *window;
306 static unsigned insize; /* valid bytes in inbuf */
307 static unsigned inptr; /* index of next byte to be processed in inbuf */
308 static unsigned outcnt; /* bytes in output buffer */
309 static int exit_code;
310 static int unzip_error;
311 static long bytes_out;
312 static int crd_infd, crd_outfd;
314 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
316 /* Diagnostic functions (stubbed out) */
317 #define Assert(cond,msg)
318 #define Trace(x)
319 #define Tracev(x)
320 #define Tracevv(x)
321 #define Tracec(c,x)
322 #define Tracecv(c,x)
324 #define STATIC static
325 #define INIT __init
327 static int __init fill_inbuf(void);
328 static void __init flush_window(void);
329 static void __init *malloc(size_t size);
330 static void __init free(void *where);
331 static void __init error(char *m);
332 static void __init gzip_mark(void **);
333 static void __init gzip_release(void **);
335 #include "../lib/inflate.c"
337 static void __init *malloc(size_t size)
339 return kmalloc(size, GFP_KERNEL);
342 static void __init free(void *where)
344 kfree(where);
347 static void __init gzip_mark(void **ptr)
351 static void __init gzip_release(void **ptr)
356 /* ===========================================================================
357 * Fill the input buffer. This is called only when the buffer is empty
358 * and at least one byte is really needed.
359 * Returning -1 does not guarantee that gunzip() will ever return.
361 static int __init fill_inbuf(void)
363 if (exit_code) return -1;
365 insize = sys_read(crd_infd, inbuf, INBUFSIZ);
366 if (insize == 0) {
367 error("RAMDISK: ran out of compressed data");
368 return -1;
371 inptr = 1;
373 return inbuf[0];
376 /* ===========================================================================
377 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
378 * (Used for the decompressed data only.)
380 static void __init flush_window(void)
382 ulg c = crc; /* temporary variable */
383 unsigned n, written;
384 uch *in, ch;
386 written = sys_write(crd_outfd, window, outcnt);
387 if (written != outcnt && unzip_error == 0) {
388 printk(KERN_ERR "RAMDISK: incomplete write (%d != %d) %ld\n",
389 written, outcnt, bytes_out);
390 unzip_error = 1;
392 in = window;
393 for (n = 0; n < outcnt; n++) {
394 ch = *in++;
395 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
397 crc = c;
398 bytes_out += (ulg)outcnt;
399 outcnt = 0;
402 static void __init error(char *x)
404 printk(KERN_ERR "%s\n", x);
405 exit_code = 1;
406 unzip_error = 1;
409 static int __init crd_load(int in_fd, int out_fd)
411 int result;
413 insize = 0; /* valid bytes in inbuf */
414 inptr = 0; /* index of next byte to be processed in inbuf */
415 outcnt = 0; /* bytes in output buffer */
416 exit_code = 0;
417 bytes_out = 0;
418 crc = (ulg)0xffffffffL; /* shift register contents */
420 crd_infd = in_fd;
421 crd_outfd = out_fd;
422 inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
423 if (inbuf == 0) {
424 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
425 return -1;
427 window = kmalloc(WSIZE, GFP_KERNEL);
428 if (window == 0) {
429 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
430 kfree(inbuf);
431 return -1;
433 makecrc();
434 result = gunzip();
435 if (unzip_error)
436 result = 1;
437 kfree(inbuf);
438 kfree(window);
439 return result;
442 #endif /* BUILD_CRAMDISK */