patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / init / do_mounts_rd.c
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"
12
13 #define BUILD_CRAMDISK
14
15 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
16
17 static int __init prompt_ramdisk(char *str)
18 {
19         rd_prompt = simple_strtol(str,NULL,0) & 1;
20         return 1;
21 }
22 __setup("prompt_ramdisk=", prompt_ramdisk);
23
24 int __initdata rd_image_start;          /* starting block # of image */
25
26 static int __init ramdisk_start_setup(char *str)
27 {
28         rd_image_start = simple_strtol(str,NULL,0);
29         return 1;
30 }
31 __setup("ramdisk_start=", ramdisk_start_setup);
32
33 static int __init crd_load(int in_fd, int out_fd);
34
35 /*
36  * This routine tries to find a RAM disk image to load, and returns the
37  * number of blocks to read for a non-compressed image, 0 if the image
38  * is a compressed image, and -1 if an image with the right magic
39  * numbers could not be found.
40  *
41  * We currently check for the following magic numbers:
42  *      minix
43  *      ext2
44  *      romfs
45  *      cramfs
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;
56         int nblocks = -1;
57         unsigned char *buf;
58
59         buf = kmalloc(size, GFP_KERNEL);
60         if (buf == 0)
61                 return -1;
62
63         minixsb = (struct minix_super_block *) buf;
64         ext2sb = (struct ext2_super_block *) buf;
65         romfsb = (struct romfs_super_block *) buf;
66         cramfsb = (struct cramfs_super *) buf;
67         memset(buf, 0xe5, size);
68
69         /*
70          * Read block 0 to test for gzipped kernel
71          */
72         sys_lseek(fd, start_block * BLOCK_SIZE, 0);
73         sys_read(fd, buf, size);
74
75         /*
76          * If it matches the gzip magic numbers, return -1
77          */
78         if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
79                 printk(KERN_NOTICE
80                        "RAMDISK: Compressed image found at block %d\n",
81                        start_block);
82                 nblocks = 0;
83                 goto done;
84         }
85
86         /* romfs is at block zero too */
87         if (romfsb->word0 == ROMSB_WORD0 &&
88             romfsb->word1 == ROMSB_WORD1) {
89                 printk(KERN_NOTICE
90                        "RAMDISK: romfs filesystem found at block %d\n",
91                        start_block);
92                 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
93                 goto done;
94         }
95
96         if (cramfsb->magic == CRAMFS_MAGIC) {
97                 printk(KERN_NOTICE
98                        "RAMDISK: cramfs filesystem found at block %d\n",
99                        start_block);
100                 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
101                 goto done;
102         }
103
104         /*
105          * Read block 1 to test for minix and ext2 superblock
106          */
107         sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
108         sys_read(fd, buf, size);
109
110         /* Try minix */
111         if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
112             minixsb->s_magic == MINIX_SUPER_MAGIC2) {
113                 printk(KERN_NOTICE
114                        "RAMDISK: Minix filesystem found at block %d\n",
115                        start_block);
116                 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
117                 goto done;
118         }
119
120         /* Try ext2 */
121         if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
122                 printk(KERN_NOTICE
123                        "RAMDISK: ext2 filesystem found at block %d\n",
124                        start_block);
125                 nblocks = le32_to_cpu(ext2sb->s_blocks_count);
126                 goto done;
127         }
128
129         printk(KERN_NOTICE
130                "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
131                start_block);
132         
133 done:
134         sys_lseek(fd, start_block * BLOCK_SIZE, 0);
135         kfree(buf);
136         return nblocks;
137 }
138
139 int __init rd_load_image(char *from)
140 {
141         int res = 0;
142         int in_fd, out_fd;
143         unsigned long rd_blocks, devblocks;
144         int nblocks, i, disk;
145         char *buf = NULL;
146         unsigned short rotate = 0;
147 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
148         char rotator[4] = { '|' , '/' , '-' , '\\' };
149 #endif
150
151         out_fd = sys_open("/dev/ram", O_RDWR, 0);
152         if (out_fd < 0)
153                 goto out;
154
155         in_fd = sys_open(from, O_RDONLY, 0);
156         if (in_fd < 0)
157                 goto noclose_input;
158
159         nblocks = identify_ramdisk_image(in_fd, rd_image_start);
160         if (nblocks < 0)
161                 goto done;
162
163         if (nblocks == 0) {
164 #ifdef BUILD_CRAMDISK
165                 if (crd_load(in_fd, out_fd) == 0)
166                         goto successful_load;
167 #else
168                 printk(KERN_NOTICE
169                        "RAMDISK: Kernel does not support compressed "
170                        "RAM disk images\n");
171 #endif
172                 goto done;
173         }
174
175         /*
176          * NOTE NOTE: nblocks suppose that the blocksize is BLOCK_SIZE, so
177          * rd_load_image will work only with filesystem BLOCK_SIZE wide!
178          * So make sure to use 1k blocksize while generating ext2fs
179          * ramdisk-images.
180          */
181         if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
182                 rd_blocks = 0;
183         else
184                 rd_blocks >>= 1;
185
186         if (nblocks > rd_blocks) {
187                 printk("RAMDISK: image too big! (%d/%ld blocks)\n",
188                        nblocks, rd_blocks);
189                 goto done;
190         }
191                 
192         /*
193          * OK, time to copy in the data
194          */
195         if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
196                 devblocks = 0;
197         else
198                 devblocks >>= 1;
199
200         if (strcmp(from, "/initrd.image") == 0)
201                 devblocks = nblocks;
202
203         if (devblocks == 0) {
204                 printk(KERN_ERR "RAMDISK: could not determine device size\n");
205                 goto done;
206         }
207
208         buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
209         if (buf == 0) {
210                 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
211                 goto done;
212         }
213
214         printk(KERN_NOTICE "RAMDISK: Loading %d blocks [%ld disk%s] into ram disk... ", 
215                 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
216         for (i = 0, disk = 1; i < nblocks; i++) {
217                 if (i && (i % devblocks == 0)) {
218                         printk("done disk #%d.\n", disk++);
219                         rotate = 0;
220                         if (sys_close(in_fd)) {
221                                 printk("Error closing the disk.\n");
222                                 goto noclose_input;
223                         }
224                         change_floppy("disk #%d", disk);
225                         in_fd = sys_open(from, O_RDONLY, 0);
226                         if (in_fd < 0)  {
227                                 printk("Error opening disk.\n");
228                                 goto noclose_input;
229                         }
230                         printk("Loading disk #%d... ", disk);
231                 }
232                 sys_read(in_fd, buf, BLOCK_SIZE);
233                 sys_write(out_fd, buf, BLOCK_SIZE);
234 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
235                 if (!(i % 16)) {
236                         printk("%c\b", rotator[rotate & 0x3]);
237                         rotate++;
238                 }
239 #endif
240         }
241         printk("done.\n");
242
243 successful_load:
244         res = 1;
245 done:
246         sys_close(in_fd);
247 noclose_input:
248         sys_close(out_fd);
249 out:
250         kfree(buf);
251         sys_unlink("/dev/ram");
252         return res;
253 }
254
255 int __init rd_load_disk(int n)
256 {
257         if (rd_prompt)
258                 change_floppy("root floppy disk to be loaded into RAM disk");
259         create_dev("/dev/root", ROOT_DEV, root_device_name);
260         create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n), NULL);
261         return rd_load_image("/dev/root");
262 }
263
264 #ifdef BUILD_CRAMDISK
265
266 /*
267  * gzip declarations
268  */
269
270 #define OF(args)  args
271
272 #ifndef memzero
273 #define memzero(s, n)     memset ((s), 0, (n))
274 #endif
275
276 typedef unsigned char  uch;
277 typedef unsigned short ush;
278 typedef unsigned long  ulg;
279
280 #define INBUFSIZ 4096
281 #define WSIZE 0x8000    /* window size--must be a power of two, and */
282                         /*  at least 32K for zip's deflate method */
283
284 static uch *inbuf;
285 static uch *window;
286
287 static unsigned insize;  /* valid bytes in inbuf */
288 static unsigned inptr;   /* index of next byte to be processed in inbuf */
289 static unsigned outcnt;  /* bytes in output buffer */
290 static int exit_code;
291 static int unzip_error;
292 static long bytes_out;
293 static int crd_infd, crd_outfd;
294
295 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
296                 
297 /* Diagnostic functions (stubbed out) */
298 #define Assert(cond,msg)
299 #define Trace(x)
300 #define Tracev(x)
301 #define Tracevv(x)
302 #define Tracec(c,x)
303 #define Tracecv(c,x)
304
305 #define STATIC static
306
307 static int  fill_inbuf(void);
308 static void flush_window(void);
309 static void *malloc(size_t size);
310 static void free(void *where);
311 static void error(char *m);
312 static void gzip_mark(void **);
313 static void gzip_release(void **);
314
315 #include "../lib/inflate.c"
316
317 static void __init *malloc(size_t size)
318 {
319         return kmalloc(size, GFP_KERNEL);
320 }
321
322 static void __init free(void *where)
323 {
324         kfree(where);
325 }
326
327 static void __init gzip_mark(void **ptr)
328 {
329 }
330
331 static void __init gzip_release(void **ptr)
332 {
333 }
334
335
336 /* ===========================================================================
337  * Fill the input buffer. This is called only when the buffer is empty
338  * and at least one byte is really needed.
339  * Returning -1 does not guarantee that gunzip() will ever return.
340  */
341 static int __init fill_inbuf(void)
342 {
343         if (exit_code) return -1;
344         
345         insize = sys_read(crd_infd, inbuf, INBUFSIZ);
346         if (insize == 0) {
347                 error("RAMDISK: ran out of compressed data");
348                 return -1;
349         }
350
351         inptr = 1;
352
353         return inbuf[0];
354 }
355
356 /* ===========================================================================
357  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
358  * (Used for the decompressed data only.)
359  */
360 static void __init flush_window(void)
361 {
362     ulg c = crc;         /* temporary variable */
363     unsigned n, written;
364     uch *in, ch;
365     
366     written = sys_write(crd_outfd, window, outcnt);
367     if (written != outcnt && unzip_error == 0) {
368         printk(KERN_ERR "RAMDISK: incomplete write (%d != %d) %ld\n",
369                written, outcnt, bytes_out);
370         unzip_error = 1;
371     }
372     in = window;
373     for (n = 0; n < outcnt; n++) {
374             ch = *in++;
375             c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
376     }
377     crc = c;
378     bytes_out += (ulg)outcnt;
379     outcnt = 0;
380 }
381
382 static void __init error(char *x)
383 {
384         printk(KERN_ERR "%s\n", x);
385         exit_code = 1;
386         unzip_error = 1;
387 }
388
389 static int __init crd_load(int in_fd, int out_fd)
390 {
391         int result;
392
393         insize = 0;             /* valid bytes in inbuf */
394         inptr = 0;              /* index of next byte to be processed in inbuf */
395         outcnt = 0;             /* bytes in output buffer */
396         exit_code = 0;
397         bytes_out = 0;
398         crc = (ulg)0xffffffffL; /* shift register contents */
399
400         crd_infd = in_fd;
401         crd_outfd = out_fd;
402         inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
403         if (inbuf == 0) {
404                 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
405                 return -1;
406         }
407         window = kmalloc(WSIZE, GFP_KERNEL);
408         if (window == 0) {
409                 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
410                 kfree(inbuf);
411                 return -1;
412         }
413         makecrc();
414         result = gunzip();
415         if (unzip_error)
416                 result = 1;
417         kfree(inbuf);
418         kfree(window);
419         return result;
420 }
421
422 #endif  /* BUILD_CRAMDISK */