2 #include <linux/kernel.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>
11 #include "do_mounts.h"
13 #define BUILD_CRAMDISK
15 int __initdata rd_prompt
= 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
17 static int __init
prompt_ramdisk(char *str
)
19 rd_prompt
= simple_strtol(str
,NULL
,0) & 1;
22 __setup("prompt_ramdisk=", prompt_ramdisk
);
24 int __initdata rd_image_start
; /* starting block # of image */
26 static int __init
ramdisk_start_setup(char *str
)
28 rd_image_start
= simple_strtol(str
,NULL
,0);
31 __setup("ramdisk_start=", ramdisk_start_setup
);
33 static int __init
crd_load(int in_fd
, int out_fd
);
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.
41 * We currently check for the following magic numbers:
49 identify_ramdisk_image(int fd
, int start_block
)
52 struct minix_super_block
*minixsb
;
53 struct ext2_super_block
*ext2sb
;
54 struct romfs_super_block
*romfsb
;
55 struct cramfs_super
*cramfsb
;
59 buf
= kmalloc(size
, GFP_KERNEL
);
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
);
70 * Read block 0 to test for gzipped kernel
72 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
73 sys_read(fd
, buf
, size
);
76 * If it matches the gzip magic numbers, return -1
78 if (buf
[0] == 037 && ((buf
[1] == 0213) || (buf
[1] == 0236))) {
80 "RAMDISK: Compressed image found at block %d\n",
86 /* romfs is at block zero too */
87 if (romfsb
->word0
== ROMSB_WORD0
&&
88 romfsb
->word1
== ROMSB_WORD1
) {
90 "RAMDISK: romfs filesystem found at block %d\n",
92 nblocks
= (ntohl(romfsb
->size
)+BLOCK_SIZE
-1)>>BLOCK_SIZE_BITS
;
96 if (cramfsb
->magic
== CRAMFS_MAGIC
) {
98 "RAMDISK: cramfs filesystem found at block %d\n",
100 nblocks
= (cramfsb
->size
+ BLOCK_SIZE
- 1) >> BLOCK_SIZE_BITS
;
105 * Read block 1 to test for minix and ext2 superblock
107 sys_lseek(fd
, (start_block
+1) * BLOCK_SIZE
, 0);
108 sys_read(fd
, buf
, size
);
111 if (minixsb
->s_magic
== MINIX_SUPER_MAGIC
||
112 minixsb
->s_magic
== MINIX_SUPER_MAGIC2
) {
114 "RAMDISK: Minix filesystem found at block %d\n",
116 nblocks
= minixsb
->s_nzones
<< minixsb
->s_log_zone_size
;
121 if (ext2sb
->s_magic
== cpu_to_le16(EXT2_SUPER_MAGIC
)) {
123 "RAMDISK: ext2 filesystem found at block %d\n",
125 nblocks
= le32_to_cpu(ext2sb
->s_blocks_count
) <<
126 le32_to_cpu(ext2sb
->s_log_block_size
);
131 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
135 sys_lseek(fd
, start_block
* BLOCK_SIZE
, 0);
140 int __init
rd_load_image(char *from
)
144 unsigned long rd_blocks
, devblocks
;
145 int nblocks
, i
, disk
;
147 unsigned short rotate
= 0;
148 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
149 char rotator
[4] = { '|' , '/' , '-' , '\\' };
152 out_fd
= sys_open("/dev/ram", O_RDWR
, 0);
156 in_fd
= sys_open(from
, O_RDONLY
, 0);
160 nblocks
= identify_ramdisk_image(in_fd
, rd_image_start
);
165 #ifdef BUILD_CRAMDISK
166 if (crd_load(in_fd
, out_fd
) == 0)
167 goto successful_load
;
170 "RAMDISK: Kernel does not support compressed "
171 "RAM disk images\n");
177 * NOTE NOTE: nblocks is not actually blocks but
178 * the number of kibibytes of data to load into a ramdisk.
179 * So any ramdisk block size that is a multiple of 1KiB should
180 * work when the appropriate ramdisk_blocksize is specified
181 * on the command line.
183 * The default ramdisk_blocksize is 1KiB and it is generally
184 * silly to use anything else, so make sure to use 1KiB
185 * blocksize while generating ext2fs ramdisk-images.
187 if (sys_ioctl(out_fd
, BLKGETSIZE
, (unsigned long)&rd_blocks
) < 0)
192 if (nblocks
> rd_blocks
) {
193 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
199 * OK, time to copy in the data
201 if (sys_ioctl(in_fd
, BLKGETSIZE
, (unsigned long)&devblocks
) < 0)
206 if (strcmp(from
, "/initrd.image") == 0)
209 if (devblocks
== 0) {
210 printk(KERN_ERR
"RAMDISK: could not determine device size\n");
214 buf
= kmalloc(BLOCK_SIZE
, GFP_KERNEL
);
216 printk(KERN_ERR
"RAMDISK: could not allocate buffer\n");
220 printk(KERN_NOTICE
"RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
221 nblocks
, ((nblocks
-1)/devblocks
)+1, nblocks
>devblocks
? "s" : "");
222 for (i
= 0, disk
= 1; i
< nblocks
; i
++) {
223 if (i
&& (i
% devblocks
== 0)) {
224 printk("done disk #%d.\n", disk
++);
226 if (sys_close(in_fd
)) {
227 printk("Error closing the disk.\n");
230 change_floppy("disk #%d", disk
);
231 in_fd
= sys_open(from
, O_RDONLY
, 0);
233 printk("Error opening disk.\n");
236 printk("Loading disk #%d... ", disk
);
238 sys_read(in_fd
, buf
, BLOCK_SIZE
);
239 sys_write(out_fd
, buf
, BLOCK_SIZE
);
240 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
242 printk("%c\b", rotator
[rotate
& 0x3]);
257 sys_unlink("/dev/ram");
261 int __init
rd_load_disk(int n
)
264 change_floppy("root floppy disk to be loaded into RAM disk");
265 create_dev("/dev/root", ROOT_DEV
, root_device_name
);
266 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR
, n
), NULL
);
267 return rd_load_image("/dev/root");
270 #ifdef BUILD_CRAMDISK
276 #define OF(args) args
279 #define memzero(s, n) memset ((s), 0, (n))
282 typedef unsigned char uch
;
283 typedef unsigned short ush
;
284 typedef unsigned long ulg
;
286 #define INBUFSIZ 4096
287 #define WSIZE 0x8000 /* window size--must be a power of two, and */
288 /* at least 32K for zip's deflate method */
293 static unsigned insize
; /* valid bytes in inbuf */
294 static unsigned inptr
; /* index of next byte to be processed in inbuf */
295 static unsigned outcnt
; /* bytes in output buffer */
296 static int exit_code
;
297 static int unzip_error
;
298 static long bytes_out
;
299 static int crd_infd
, crd_outfd
;
301 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
303 /* Diagnostic functions (stubbed out) */
304 #define Assert(cond,msg)
311 #define STATIC static
314 static int __init
fill_inbuf(void);
315 static void __init
flush_window(void);
316 static void __init
*malloc(size_t size
);
317 static void __init
free(void *where
);
318 static void __init
error(char *m
);
319 static void __init
gzip_mark(void **);
320 static void __init
gzip_release(void **);
322 #include "../lib/inflate.c"
324 static void __init
*malloc(size_t size
)
326 return kmalloc(size
, GFP_KERNEL
);
329 static void __init
free(void *where
)
334 static void __init
gzip_mark(void **ptr
)
338 static void __init
gzip_release(void **ptr
)
343 /* ===========================================================================
344 * Fill the input buffer. This is called only when the buffer is empty
345 * and at least one byte is really needed.
346 * Returning -1 does not guarantee that gunzip() will ever return.
348 static int __init
fill_inbuf(void)
350 if (exit_code
) return -1;
352 insize
= sys_read(crd_infd
, inbuf
, INBUFSIZ
);
354 error("RAMDISK: ran out of compressed data");
363 /* ===========================================================================
364 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
365 * (Used for the decompressed data only.)
367 static void __init
flush_window(void)
369 ulg c
= crc
; /* temporary variable */
373 written
= sys_write(crd_outfd
, window
, outcnt
);
374 if (written
!= outcnt
&& unzip_error
== 0) {
375 printk(KERN_ERR
"RAMDISK: incomplete write (%d != %d) %ld\n",
376 written
, outcnt
, bytes_out
);
380 for (n
= 0; n
< outcnt
; n
++) {
382 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
385 bytes_out
+= (ulg
)outcnt
;
389 static void __init
error(char *x
)
391 printk(KERN_ERR
"%s\n", x
);
396 static int __init
crd_load(int in_fd
, int out_fd
)
400 insize
= 0; /* valid bytes in inbuf */
401 inptr
= 0; /* index of next byte to be processed in inbuf */
402 outcnt
= 0; /* bytes in output buffer */
405 crc
= (ulg
)0xffffffffL
; /* shift register contents */
409 inbuf
= kmalloc(INBUFSIZ
, GFP_KERNEL
);
411 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip buffer\n");
414 window
= kmalloc(WSIZE
, GFP_KERNEL
);
416 printk(KERN_ERR
"RAMDISK: Couldn't allocate gzip window\n");
429 #endif /* BUILD_CRAMDISK */