Magellan Linux

Contents of /alx-src/tags/kernel26-2.6.12-alx-r9/init/do_mounts_rd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 630 - (show annotations) (download)
Wed Mar 4 11:03:09 2009 UTC (15 years, 3 months ago) by niro
File MIME type: text/plain
File size: 10662 byte(s)
Tag kernel26-2.6.12-alx-r9
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/squashfs_fs.h>
9 #include <linux/initrd.h>
10 #include <linux/string.h>
11
12 #include "do_mounts.h"
13
14 #define BUILD_CRAMDISK
15
16 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
17
18 static int __init prompt_ramdisk(char *str)
19 {
20 rd_prompt = simple_strtol(str,NULL,0) & 1;
21 return 1;
22 }
23 __setup("prompt_ramdisk=", prompt_ramdisk);
24
25 int __initdata rd_image_start; /* starting block # of image */
26
27 static int __init ramdisk_start_setup(char *str)
28 {
29 rd_image_start = simple_strtol(str,NULL,0);
30 return 1;
31 }
32 __setup("ramdisk_start=", ramdisk_start_setup);
33
34 static int __init crd_load(int in_fd, int out_fd);
35
36 /*
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.
41 *
42 * We currently check for the following magic numbers:
43 * squashfs
44 * minix
45 * ext2
46 * romfs
47 * cramfs
48 * gzip
49 */
50 static int __init
51 identify_ramdisk_image(int fd, int start_block)
52 {
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;
61
62 buf = kmalloc(size, GFP_KERNEL);
63 if (buf == 0)
64 return -1;
65
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);
72
73 /*
74 * Read block 0 to test for gzipped kernel
75 */
76 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
77 sys_read(fd, buf, size);
78
79 /*
80 * If it matches the gzip magic numbers, return -1
81 */
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;
88 }
89
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;
98 }
99
100 if (cramfsb->magic == CRAMFS_MAGIC) {
101 printk(KERN_NOTICE
102 "RAMDISK: cramfs filesystem found at block %d\n",
103 start_block);
104 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
105 goto done;
106 }
107
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;
115 }
116
117 /*
118 * Read block 1 to test for minix and ext2 superblock
119 */
120 sys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
121 sys_read(fd, buf, size);
122
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;
131 }
132
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;
141 }
142
143 printk(KERN_NOTICE
144 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
145 start_block);
146
147 done:
148 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
149 kfree(buf);
150 return nblocks;
151 }
152
153 int __init rd_load_image(char *from)
154 {
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_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
162 char rotator[4] = { '|' , '/' , '-' , '\\' };
163 #endif
164
165 out_fd = sys_open("/dev/ram", O_RDWR, 0);
166 if (out_fd < 0)
167 goto out;
168
169 in_fd = sys_open(from, O_RDONLY, 0);
170 if (in_fd < 0)
171 goto noclose_input;
172
173 nblocks = identify_ramdisk_image(in_fd, rd_image_start);
174 if (nblocks < 0)
175 goto done;
176
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;
187 }
188
189 /*
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.
195 *
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.
199 */
200 if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
201 rd_blocks = 0;
202 else
203 rd_blocks >>= 1;
204
205 if (nblocks > rd_blocks) {
206 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
207 nblocks, rd_blocks);
208 goto done;
209 }
210
211 /*
212 * OK, time to copy in the data
213 */
214 if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
215 devblocks = 0;
216 else
217 devblocks >>= 1;
218
219 if (strcmp(from, "/initrd.image") == 0)
220 devblocks = nblocks;
221
222 if (devblocks == 0) {
223 printk(KERN_ERR "RAMDISK: could not determine device size\n");
224 goto done;
225 }
226
227 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
228 if (buf == 0) {
229 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
230 goto done;
231 }
232
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;
242 }
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;
248 }
249 printk("Loading disk #%d... ", disk);
250 }
251 sys_read(in_fd, buf, BLOCK_SIZE);
252 sys_write(out_fd, buf, BLOCK_SIZE);
253 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
254 if (!(i % 16)) {
255 printk("%c\b", rotator[rotate & 0x3]);
256 rotate++;
257 }
258 #endif
259 }
260 printk("done.\n");
261
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;
272 }
273
274 int __init rd_load_disk(int n)
275 {
276 if (rd_prompt)
277 change_floppy("root floppy disk to be loaded into RAM disk");
278 create_dev("/dev/root", ROOT_DEV, root_device_name);
279 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n), NULL);
280 return rd_load_image("/dev/root");
281 }
282
283 #ifdef BUILD_CRAMDISK
284
285 /*
286 * gzip declarations
287 */
288
289 #define OF(args) args
290
291 #ifndef memzero
292 #define memzero(s, n) memset ((s), 0, (n))
293 #endif
294
295 typedef unsigned char uch;
296 typedef unsigned short ush;
297 typedef unsigned long ulg;
298
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 */
302
303 static uch *inbuf;
304 static uch *window;
305
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;
313
314 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
315
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)
323
324 #define STATIC static
325 #define INIT __init
326
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 **);
334
335 #include "../lib/inflate.c"
336
337 static void __init *malloc(size_t size)
338 {
339 return kmalloc(size, GFP_KERNEL);
340 }
341
342 static void __init free(void *where)
343 {
344 kfree(where);
345 }
346
347 static void __init gzip_mark(void **ptr)
348 {
349 }
350
351 static void __init gzip_release(void **ptr)
352 {
353 }
354
355
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.
360 */
361 static int __init fill_inbuf(void)
362 {
363 if (exit_code) return -1;
364
365 insize = sys_read(crd_infd, inbuf, INBUFSIZ);
366 if (insize == 0) {
367 error("RAMDISK: ran out of compressed data");
368 return -1;
369 }
370
371 inptr = 1;
372
373 return inbuf[0];
374 }
375
376 /* ===========================================================================
377 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
378 * (Used for the decompressed data only.)
379 */
380 static void __init flush_window(void)
381 {
382 ulg c = crc; /* temporary variable */
383 unsigned n, written;
384 uch *in, ch;
385
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;
391 }
392 in = window;
393 for (n = 0; n < outcnt; n++) {
394 ch = *in++;
395 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
396 }
397 crc = c;
398 bytes_out += (ulg)outcnt;
399 outcnt = 0;
400 }
401
402 static void __init error(char *x)
403 {
404 printk(KERN_ERR "%s\n", x);
405 exit_code = 1;
406 unzip_error = 1;
407 }
408
409 static int __init crd_load(int in_fd, int out_fd)
410 {
411 int result;
412
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 */
419
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;
426 }
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;
432 }
433 makecrc();
434 result = gunzip();
435 if (unzip_error)
436 result = 1;
437 kfree(inbuf);
438 kfree(window);
439 return result;
440 }
441
442 #endif /* BUILD_CRAMDISK */