1 #define __KERNEL_SYSCALLS__
2 #include <linux/config.h>
3 #include <linux/slab.h>
4 #include <linux/devfs_fs_kernel.h>
5 #include <linux/unistd.h>
6 #include <linux/ctype.h>
7 #include <linux/blk.h>
8 #include <linux/fd.h>
9 #include <linux/delay.h>
10 #include <linux/tty.h>
11 #include <linux/init.h>
12 
13 #include <linux/nfs_fs.h>
14 #include <linux/nfs_fs_sb.h>
15 #include <linux/nfs_mount.h>
16 #include <linux/minix_fs.h>
17 #include <linux/ext2_fs.h>
18 #include <linux/romfs_fs.h>
19 #include <linux/cramfs_fs.h>
20 
21 #define BUILD_CRAMDISK
22 
23 extern int get_filesystem_list(char * buf);
24 
25 extern asmlinkage long sys_mount(char *dev_name, char *dir_name, char *type,
26 	 unsigned long flags, void *data);
27 extern asmlinkage long sys_mkdir(const char *name, int mode);
28 extern asmlinkage long sys_chdir(const char *name);
29 extern asmlinkage long sys_fchdir(int fd);
30 extern asmlinkage long sys_chroot(const char *name);
31 extern asmlinkage long sys_unlink(const char *name);
32 extern asmlinkage long sys_symlink(const char *old, const char *new);
33 extern asmlinkage long sys_mknod(const char *name, int mode, dev_t dev);
34 extern asmlinkage long sys_umount(char *name, int flags);
35 extern asmlinkage long sys_ioctl(int fd, int cmd, unsigned long arg);
36 
37 #ifdef CONFIG_BLK_DEV_INITRD
38 unsigned int real_root_dev;	/* do_proc_dointvec cannot handle kdev_t */
39 static int __initdata mount_initrd = 1;
40 
no_initrd(char * str)41 static int __init no_initrd(char *str)
42 {
43 	mount_initrd = 0;
44 	return 1;
45 }
46 
47 __setup("noinitrd", no_initrd);
48 #else
49 static int __initdata mount_initrd = 0;
50 #endif
51 
52 int __initdata rd_doload;	/* 1 = load RAM disk, 0 = don't load */
53 
54 int root_mountflags = MS_RDONLY | MS_VERBOSE;
55 static char root_device_name[64];
56 
57 /* this is initialized in init/main.c */
58 kdev_t ROOT_DEV;
59 
60 static int do_devfs = 0;
61 
load_ramdisk(char * str)62 static int __init load_ramdisk(char *str)
63 {
64 	rd_doload = simple_strtol(str,NULL,0) & 3;
65 	return 1;
66 }
67 __setup("load_ramdisk=", load_ramdisk);
68 
readonly(char * str)69 static int __init readonly(char *str)
70 {
71 	if (*str)
72 		return 0;
73 	root_mountflags |= MS_RDONLY;
74 	return 1;
75 }
76 
readwrite(char * str)77 static int __init readwrite(char *str)
78 {
79 	if (*str)
80 		return 0;
81 	root_mountflags &= ~MS_RDONLY;
82 	return 1;
83 }
84 
85 __setup("ro", readonly);
86 __setup("rw", readwrite);
87 
88 static struct dev_name_struct {
89 	const char *name;
90 	const int num;
91 } root_dev_names[] __initdata = {
92 	{ "nfs",     MKDEV(NFS_MAJOR, NFS_MINOR) },
93 	{ "hda",     0x0300 },
94 	{ "hdb",     0x0340 },
95 	{ "loop",    0x0700 },
96 	{ "hdc",     0x1600 },
97 	{ "hdd",     0x1640 },
98 	{ "hde",     0x2100 },
99 	{ "hdf",     0x2140 },
100 	{ "hdg",     0x2200 },
101 	{ "hdh",     0x2240 },
102 	{ "hdi",     0x3800 },
103 	{ "hdj",     0x3840 },
104 	{ "hdk",     0x3900 },
105 	{ "hdl",     0x3940 },
106 	{ "hdm",     0x5800 },
107 	{ "hdn",     0x5840 },
108 	{ "hdo",     0x5900 },
109 	{ "hdp",     0x5940 },
110 	{ "hdq",     0x5A00 },
111 	{ "hdr",     0x5A40 },
112 	{ "hds",     0x5B00 },
113 	{ "hdt",     0x5B40 },
114 	{ "sda",     0x0800 },
115 	{ "sdb",     0x0810 },
116 	{ "sdc",     0x0820 },
117 	{ "sdd",     0x0830 },
118 	{ "sde",     0x0840 },
119 	{ "sdf",     0x0850 },
120 	{ "sdg",     0x0860 },
121 	{ "sdh",     0x0870 },
122 	{ "sdi",     0x0880 },
123 	{ "sdj",     0x0890 },
124 	{ "sdk",     0x08a0 },
125 	{ "sdl",     0x08b0 },
126 	{ "sdm",     0x08c0 },
127 	{ "sdn",     0x08d0 },
128 	{ "sdo",     0x08e0 },
129 	{ "sdp",     0x08f0 },
130 	{ "ada",     0x1c00 },
131 	{ "adb",     0x1c10 },
132 	{ "adc",     0x1c20 },
133 	{ "add",     0x1c30 },
134 	{ "ade",     0x1c40 },
135 	{ "fd",      0x0200 },
136 	{ "md",      0x0900 },
137 	{ "xda",     0x0d00 },
138 	{ "xdb",     0x0d40 },
139 	{ "ram",     0x0100 },
140 	{ "scd",     0x0b00 },
141 	{ "mcd",     0x1700 },
142 	{ "cdu535",  0x1800 },
143 	{ "sonycd",  0x1800 },
144 	{ "aztcd",   0x1d00 },
145 	{ "cm206cd", 0x2000 },
146 	{ "gscd",    0x1000 },
147 	{ "sbpcd",   0x1900 },
148 	{ "eda",     0x2400 },
149 	{ "edb",     0x2440 },
150 	{ "pda",	0x2d00 },
151 	{ "pdb",	0x2d10 },
152 	{ "pdc",	0x2d20 },
153 	{ "pdd",	0x2d30 },
154 	{ "pcd",	0x2e00 },
155 	{ "pf",		0x2f00 },
156 	{ "apblock", APBLOCK_MAJOR << 8},
157 	{ "ddv", DDV_MAJOR << 8},
158 	{ "jsfd",    JSFD_MAJOR << 8},
159 #if defined(CONFIG_ARCH_S390)
160 	{ "dasda", (DASD_MAJOR << MINORBITS) },
161 	{ "dasdb", (DASD_MAJOR << MINORBITS) + (1 << 2) },
162 	{ "dasdc", (DASD_MAJOR << MINORBITS) + (2 << 2) },
163 	{ "dasdd", (DASD_MAJOR << MINORBITS) + (3 << 2) },
164 	{ "dasde", (DASD_MAJOR << MINORBITS) + (4 << 2) },
165 	{ "dasdf", (DASD_MAJOR << MINORBITS) + (5 << 2) },
166 	{ "dasdg", (DASD_MAJOR << MINORBITS) + (6 << 2) },
167 	{ "dasdh", (DASD_MAJOR << MINORBITS) + (7 << 2) },
168 #endif
169 	{ "ida/c0d0p",0x4800 },
170 	{ "ida/c0d1p",0x4810 },
171 	{ "ida/c0d2p",0x4820 },
172 	{ "ida/c0d3p",0x4830 },
173 	{ "ida/c0d4p",0x4840 },
174 	{ "ida/c0d5p",0x4850 },
175 	{ "ida/c0d6p",0x4860 },
176 	{ "ida/c0d7p",0x4870 },
177 	{ "ida/c0d8p",0x4880 },
178 	{ "ida/c0d9p",0x4890 },
179 	{ "ida/c0d10p",0x48A0 },
180 	{ "ida/c0d11p",0x48B0 },
181 	{ "ida/c0d12p",0x48C0 },
182 	{ "ida/c0d13p",0x48D0 },
183 	{ "ida/c0d14p",0x48E0 },
184 	{ "ida/c0d15p",0x48F0 },
185 	{ "ida/c1d0p",0x4900 },
186 	{ "ida/c2d0p",0x4A00 },
187 	{ "ida/c3d0p",0x4B00 },
188 	{ "ida/c4d0p",0x4C00 },
189 	{ "ida/c5d0p",0x4D00 },
190 	{ "ida/c6d0p",0x4E00 },
191 	{ "ida/c7d0p",0x4F00 },
192 	{ "cciss/c0d0p",0x6800 },
193 	{ "cciss/c0d1p",0x6810 },
194 	{ "cciss/c0d2p",0x6820 },
195 	{ "cciss/c0d3p",0x6830 },
196 	{ "cciss/c0d4p",0x6840 },
197 	{ "cciss/c0d5p",0x6850 },
198 	{ "cciss/c0d6p",0x6860 },
199 	{ "cciss/c0d7p",0x6870 },
200 	{ "cciss/c0d8p",0x6880 },
201 	{ "cciss/c0d9p",0x6890 },
202 	{ "cciss/c0d10p",0x68A0 },
203 	{ "cciss/c0d11p",0x68B0 },
204 	{ "cciss/c0d12p",0x68C0 },
205 	{ "cciss/c0d13p",0x68D0 },
206 	{ "cciss/c0d14p",0x68E0 },
207 	{ "cciss/c0d15p",0x68F0 },
208 	{ "cciss/c1d0p",0x6900 },
209 	{ "cciss/c2d0p",0x6A00 },
210 	{ "cciss/c3d0p",0x6B00 },
211 	{ "cciss/c4d0p",0x6C00 },
212 	{ "cciss/c5d0p",0x6D00 },
213 	{ "cciss/c6d0p",0x6E00 },
214 	{ "cciss/c7d0p",0x6F00 },
215 	{ "ataraid/d0p",0x7200 },
216 	{ "ataraid/d1p",0x7210 },
217 	{ "ataraid/d2p",0x7220 },
218 	{ "ataraid/d3p",0x7230 },
219 	{ "ataraid/d4p",0x7240 },
220 	{ "ataraid/d5p",0x7250 },
221 	{ "ataraid/d6p",0x7260 },
222 	{ "ataraid/d7p",0x7270 },
223 	{ "ataraid/d8p",0x7280 },
224 	{ "ataraid/d9p",0x7290 },
225 	{ "ataraid/d10p",0x72A0 },
226 	{ "ataraid/d11p",0x72B0 },
227 	{ "ataraid/d12p",0x72C0 },
228 	{ "ataraid/d13p",0x72D0 },
229 	{ "ataraid/d14p",0x72E0 },
230 	{ "ataraid/d15p",0x72F0 },
231         { "rd/c0d0p",0x3000 },
232         { "rd/c0d0p1",0x3001 },
233         { "rd/c0d0p2",0x3002 },
234         { "rd/c0d0p3",0x3003 },
235         { "rd/c0d0p4",0x3004 },
236         { "rd/c0d0p5",0x3005 },
237         { "rd/c0d0p6",0x3006 },
238         { "rd/c0d0p7",0x3007 },
239         { "rd/c0d0p8",0x3008 },
240         { "rd/c0d1p",0x3008 },
241         { "rd/c0d1p1",0x3009 },
242         { "rd/c0d1p2",0x300a },
243         { "rd/c0d1p3",0x300b },
244         { "rd/c0d1p4",0x300c },
245         { "rd/c0d1p5",0x300d },
246         { "rd/c0d1p6",0x300e },
247         { "rd/c0d1p7",0x300f },
248         { "rd/c0d1p8",0x3010 },
249 	{ "nftla", 0x5d00 },
250 	{ "nftlb", 0x5d10 },
251 	{ "nftlc", 0x5d20 },
252 	{ "nftld", 0x5d30 },
253 	{ "ftla", 0x2c00 },
254 	{ "ftlb", 0x2c08 },
255 	{ "ftlc", 0x2c10 },
256 	{ "ftld", 0x2c18 },
257 	{ "mtdblock", 0x1f00 },
258 	{ "nb", 0x2b00 },
259 	{ NULL, 0 }
260 };
261 
name_to_kdev_t(char * line)262 kdev_t __init name_to_kdev_t(char *line)
263 {
264 	int base = 0, offs;
265 	char *end;
266 
267 	if (strncmp(line,"/dev/",5) == 0) {
268 		struct dev_name_struct *dev = root_dev_names;
269 		line += 5;
270 		do {
271 			int len = strlen(dev->name);
272 			if (strncmp(line,dev->name,len) == 0) {
273 				line += len;
274 				base = dev->num;
275 				break;
276 			}
277 			dev++;
278 		} while (dev->name);
279 	}
280 	offs = simple_strtoul(line, &end, base?10:16);
281 	if (*end)
282 		offs = 0;
283 	return to_kdev_t(base + offs);
284 }
285 
root_dev_setup(char * line)286 static int __init root_dev_setup(char *line)
287 {
288 	int i;
289 	char ch;
290 
291 	ROOT_DEV = name_to_kdev_t(line);
292 	memset (root_device_name, 0, sizeof root_device_name);
293 	if (strncmp (line, "/dev/", 5) == 0) line += 5;
294 	for (i = 0; i < sizeof root_device_name - 1; ++i)
295 	{
296 	    ch = line[i];
297 	    if ( isspace (ch) || (ch == ',') || (ch == '\0') ) break;
298 	    root_device_name[i] = ch;
299 	}
300 	return 1;
301 }
302 
303 __setup("root=", root_dev_setup);
304 
305 static char * __initdata root_mount_data;
root_data_setup(char * str)306 static int __init root_data_setup(char *str)
307 {
308 	root_mount_data = str;
309 	return 1;
310 }
311 
312 static char * __initdata root_fs_names;
fs_names_setup(char * str)313 static int __init fs_names_setup(char *str)
314 {
315 	root_fs_names = str;
316 	return 1;
317 }
318 
319 static unsigned int __initdata root_delay;
root_delay_setup(char * str)320 static int __init root_delay_setup(char *str)
321 {
322 	root_delay = simple_strtoul(str, NULL, 0);
323 	return 1;
324 }
325 
326 __setup("rootflags=", root_data_setup);
327 __setup("rootfstype=", fs_names_setup);
328 __setup("rootdelay=", root_delay_setup);
329 
get_fs_names(char * page)330 static void __init get_fs_names(char *page)
331 {
332 	char *s = page;
333 
334 	if (root_fs_names) {
335 		strcpy(page, root_fs_names);
336 		while (*s++) {
337 			if (s[-1] == ',')
338 				s[-1] = '\0';
339 		}
340 	} else {
341 		int len = get_filesystem_list(page);
342 		char *p, *next;
343 
344 		page[len] = '\0';
345 		for (p = page-1; p; p = next) {
346 			next = strchr(++p, '\n');
347 			if (*p++ != '\t')
348 				continue;
349 			while ((*s++ = *p++) != '\n')
350 				;
351 			s[-1] = '\0';
352 		}
353 	}
354 	*s = '\0';
355 }
mount_block_root(char * name,int flags)356 static void __init mount_block_root(char *name, int flags)
357 {
358 	char *fs_names = __getname();
359 	char *p;
360 
361 	get_fs_names(fs_names);
362 retry:
363 	for (p = fs_names; *p; p += strlen(p)+1) {
364 		int err = sys_mount(name, "/root", p, flags, root_mount_data);
365 		switch (err) {
366 			case 0:
367 				goto out;
368 			case -EACCES:
369 				flags |= MS_RDONLY;
370 				goto retry;
371 			case -EINVAL:
372 		        case -EBUSY:
373 				continue;
374 		}
375 	        /*
376 		 * Allow the user to distinguish between failed open
377 		 * and bad superblock on root device.
378 		 */
379 		printk ("VFS: Cannot open root device \"%s\" or %s\n",
380 			root_device_name, kdevname (ROOT_DEV));
381 		printk ("Please append a correct \"root=\" boot option\n");
382 		panic("VFS: Unable to mount root fs on %s",
383 			kdevname(ROOT_DEV));
384 	}
385 	panic("VFS: Unable to mount root fs on %s", kdevname(ROOT_DEV));
386 out:
387 	putname(fs_names);
388 	sys_chdir("/root");
389 	ROOT_DEV = current->fs->pwdmnt->mnt_sb->s_dev;
390 	printk("VFS: Mounted root (%s filesystem)%s.\n",
391 		current->fs->pwdmnt->mnt_sb->s_type->name,
392 		(current->fs->pwdmnt->mnt_sb->s_flags & MS_RDONLY) ? " readonly" : "");
393 }
394 
395 #ifdef CONFIG_ROOT_NFS
mount_nfs_root(void)396 static int __init mount_nfs_root(void)
397 {
398 	void *data = nfs_root_data();
399 
400 	if (data && sys_mount("/dev/root","/root","nfs",root_mountflags,data) == 0)
401 		return 1;
402 	return 0;
403 }
404 #endif
405 
create_dev(char * name,kdev_t dev,char * devfs_name)406 static int __init create_dev(char *name, kdev_t dev, char *devfs_name)
407 {
408 	void *handle;
409 	char path[64];
410 	int n;
411 
412 	sys_unlink(name);
413 	if (!do_devfs)
414 		return sys_mknod(name, S_IFBLK|0600, kdev_t_to_nr(dev));
415 
416 	handle = devfs_find_handle(NULL, dev ? NULL : devfs_name,
417 				MAJOR(dev), MINOR(dev), DEVFS_SPECIAL_BLK, 1);
418 	if (!handle)
419 		return -1;
420 	n = devfs_generate_path(handle, path + 5, sizeof (path) - 5);
421 	if (n < 0)
422 		return -1;
423 	return sys_symlink(path + n + 5, name);
424 }
425 
426 #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
change_floppy(char * fmt,...)427 static void __init change_floppy(char *fmt, ...)
428 {
429 	struct termios termios;
430 	char buf[80];
431 	char c;
432 	int fd;
433 	va_list args;
434 	va_start(args, fmt);
435 	vsprintf(buf, fmt, args);
436 	va_end(args);
437 	fd = open("/dev/root", O_RDWR | O_NDELAY, 0);
438 	if (fd >= 0) {
439 		sys_ioctl(fd, FDEJECT, 0);
440 		close(fd);
441 	}
442 	printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
443 	fd = open("/dev/console", O_RDWR, 0);
444 	if (fd >= 0) {
445 		sys_ioctl(fd, TCGETS, (long)&termios);
446 		termios.c_lflag &= ~ICANON;
447 		sys_ioctl(fd, TCSETSF, (long)&termios);
448 		read(fd, &c, 1);
449 		termios.c_lflag |= ICANON;
450 		sys_ioctl(fd, TCSETSF, (long)&termios);
451 		close(fd);
452 	}
453 }
454 #endif
455 
456 #ifdef CONFIG_BLK_DEV_RAM
457 
458 int __initdata rd_prompt = 1;	/* 1 = prompt for RAM disk, 0 = don't prompt */
459 
prompt_ramdisk(char * str)460 static int __init prompt_ramdisk(char *str)
461 {
462 	rd_prompt = simple_strtol(str,NULL,0) & 1;
463 	return 1;
464 }
465 __setup("prompt_ramdisk=", prompt_ramdisk);
466 
467 int __initdata rd_image_start;		/* starting block # of image */
468 
ramdisk_start_setup(char * str)469 static int __init ramdisk_start_setup(char *str)
470 {
471 	rd_image_start = simple_strtol(str,NULL,0);
472 	return 1;
473 }
474 __setup("ramdisk_start=", ramdisk_start_setup);
475 
476 static int __init crd_load(int in_fd, int out_fd);
477 
478 /*
479  * This routine tries to find a RAM disk image to load, and returns the
480  * number of blocks to read for a non-compressed image, 0 if the image
481  * is a compressed image, and -1 if an image with the right magic
482  * numbers could not be found.
483  *
484  * We currently check for the following magic numbers:
485  * 	minix
486  * 	ext2
487  *	romfs
488  *	cramfs
489  * 	gzip
490  */
491 static int __init
identify_ramdisk_image(int fd,int start_block)492 identify_ramdisk_image(int fd, int start_block)
493 {
494 	const int size = 512;
495 	struct minix_super_block *minixsb;
496 	struct ext2_super_block *ext2sb;
497 	struct romfs_super_block *romfsb;
498 	struct cramfs_super *cramfsb;
499 	int nblocks = -1;
500 	unsigned char *buf;
501 
502 	buf = kmalloc(size, GFP_KERNEL);
503 	if (buf == 0)
504 		return -1;
505 
506 	minixsb = (struct minix_super_block *) buf;
507 	ext2sb = (struct ext2_super_block *) buf;
508 	romfsb = (struct romfs_super_block *) buf;
509 	cramfsb = (struct cramfs_super *) buf;
510 	memset(buf, 0xe5, size);
511 
512 	/*
513 	 * Read block 0 to test for gzipped kernel
514 	 */
515 	lseek(fd, start_block * BLOCK_SIZE, 0);
516 	read(fd, buf, size);
517 
518 	/*
519 	 * If it matches the gzip magic numbers, return -1
520 	 */
521 	if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
522 		printk(KERN_NOTICE
523 		       "RAMDISK: Compressed image found at block %d\n",
524 		       start_block);
525 		nblocks = 0;
526 		goto done;
527 	}
528 
529 	/* romfs is at block zero too */
530 	if (romfsb->word0 == ROMSB_WORD0 &&
531 	    romfsb->word1 == ROMSB_WORD1) {
532 		printk(KERN_NOTICE
533 		       "RAMDISK: romfs filesystem found at block %d\n",
534 		       start_block);
535 		nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
536 		goto done;
537 	}
538 
539 	if (cramfsb->magic == CRAMFS_MAGIC) {
540 		printk(KERN_NOTICE
541 		       "RAMDISK: cramfs filesystem found at block %d\n",
542 		       start_block);
543 		nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
544 		goto done;
545 	}
546 
547 	/*
548 	 * Read block 1 to test for minix and ext2 superblock
549 	 */
550 	lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
551 	read(fd, buf, size);
552 
553 	/* Try minix */
554 	if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
555 	    minixsb->s_magic == MINIX_SUPER_MAGIC2) {
556 		printk(KERN_NOTICE
557 		       "RAMDISK: Minix filesystem found at block %d\n",
558 		       start_block);
559 		nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
560 		goto done;
561 	}
562 
563 	/* Try ext2 */
564 	if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
565 		printk(KERN_NOTICE
566 		       "RAMDISK: ext2 filesystem found at block %d\n",
567 		       start_block);
568 		nblocks = le32_to_cpu(ext2sb->s_blocks_count);
569 		goto done;
570 	}
571 
572 	printk(KERN_NOTICE
573 	       "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
574 	       start_block);
575 
576 done:
577 	lseek(fd, start_block * BLOCK_SIZE, 0);
578 	kfree(buf);
579 	return nblocks;
580 }
581 #endif
582 
rd_load_image(char * from)583 static int __init rd_load_image(char *from)
584 {
585 	int res = 0;
586 
587 #ifdef CONFIG_BLK_DEV_RAM
588 	int in_fd, out_fd;
589 	unsigned long rd_blocks, devblocks;
590 	int nblocks, i;
591 	char *buf;
592 	unsigned short rotate = 0;
593 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
594 	char rotator[4] = { '|' , '/' , '-' , '\\' };
595 #endif
596 
597 	out_fd = open("/dev/ram", O_RDWR, 0);
598 	if (out_fd < 0)
599 		goto out;
600 
601 	in_fd = open(from, O_RDONLY, 0);
602 	if (in_fd < 0)
603 		goto noclose_input;
604 
605 	nblocks = identify_ramdisk_image(in_fd, rd_image_start);
606 	if (nblocks < 0)
607 		goto done;
608 
609 	if (nblocks == 0) {
610 #ifdef BUILD_CRAMDISK
611 		if (crd_load(in_fd, out_fd) == 0)
612 			goto successful_load;
613 #else
614 		printk(KERN_NOTICE
615 		       "RAMDISK: Kernel does not support compressed "
616 		       "RAM disk images\n");
617 #endif
618 		goto done;
619 	}
620 
621 	/*
622 	 * NOTE NOTE: nblocks suppose that the blocksize is BLOCK_SIZE, so
623 	 * rd_load_image will work only with filesystem BLOCK_SIZE wide!
624 	 * So make sure to use 1k blocksize while generating ext2fs
625 	 * ramdisk-images.
626 	 */
627 	if (sys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
628 		rd_blocks = 0;
629 	else
630 		rd_blocks >>= 1;
631 
632 	if (nblocks > rd_blocks) {
633 		printk("RAMDISK: image too big! (%d/%lu blocks)\n",
634 		       nblocks, rd_blocks);
635 		goto done;
636 	}
637 
638 	/*
639 	 * OK, time to copy in the data
640 	 */
641 	buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
642 	if (buf == 0) {
643 		printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
644 		goto done;
645 	}
646 
647 	if (sys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
648 		devblocks = 0;
649 	else
650 		devblocks >>= 1;
651 
652 	if (strcmp(from, "/dev/initrd") == 0)
653 		devblocks = nblocks;
654 
655 	if (devblocks == 0) {
656 		printk(KERN_ERR "RAMDISK: could not determine device size\n");
657 		goto done;
658 	}
659 
660 	printk(KERN_NOTICE "RAMDISK: Loading %d blocks [%ld disk%s] into ram disk... ",
661 		nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
662 	for (i=0; i < nblocks; i++) {
663 		if (i && (i % devblocks == 0)) {
664 			printk("done disk #%ld.\n", i/devblocks);
665 			rotate = 0;
666 			if (close(in_fd)) {
667 				printk("Error closing the disk.\n");
668 				goto noclose_input;
669 			}
670 			change_floppy("disk #%d", i/devblocks+1);
671 			in_fd = open(from, O_RDONLY, 0);
672 			if (in_fd < 0)  {
673 				printk("Error opening disk.\n");
674 				goto noclose_input;
675 			}
676 			printk("Loading disk #%ld... ", i/devblocks+1);
677 		}
678 		read(in_fd, buf, BLOCK_SIZE);
679 		write(out_fd, buf, BLOCK_SIZE);
680 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
681 		if (!(i % 16)) {
682 			printk("%c\b", rotator[rotate & 0x3]);
683 			rotate++;
684 		}
685 #endif
686 	}
687 	printk("done.\n");
688 	kfree(buf);
689 
690 successful_load:
691 	res = 1;
692 done:
693 	close(in_fd);
694 noclose_input:
695 	close(out_fd);
696 out:
697 	sys_unlink("/dev/ram");
698 #endif
699 	return res;
700 }
701 
rd_load_disk(int n)702 static int __init rd_load_disk(int n)
703 {
704 #ifdef CONFIG_BLK_DEV_RAM
705 	if (rd_prompt)
706 		change_floppy("root floppy disk to be loaded into RAM disk");
707 	create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n), NULL);
708 #endif
709 	return rd_load_image("/dev/root");
710 }
711 
712 #ifdef CONFIG_DEVFS_FS
713 
convert_name(char * prefix,char * name,char * p,int part)714 static void __init convert_name(char *prefix, char *name, char *p, int part)
715 {
716 	int host, bus, target, lun;
717 	char dest[64];
718 	char src[64];
719 	char *base = p - 1;
720 
721 	/*  Decode "c#b#t#u#"  */
722 	if (*p++ != 'c')
723 		return;
724 	host = simple_strtol(p, &p, 10);
725 	if (*p++ != 'b')
726 		return;
727 	bus = simple_strtol(p, &p, 10);
728 	if (*p++ != 't')
729 		return;
730 	target = simple_strtol(p, &p, 10);
731 	if (*p++ != 'u')
732 		return;
733 	lun = simple_strtol(p, &p, 10);
734 	if (!part)
735 		sprintf(dest, "%s/host%d/bus%d/target%d/lun%d",
736 				prefix, host, bus, target, lun);
737 	else if (*p++ == 'p')
738 		sprintf(dest, "%s/host%d/bus%d/target%d/lun%d/part%s",
739 				prefix, host, bus, target, lun, p);
740 	else
741 		sprintf(dest, "%s/host%d/bus%d/target%d/lun%d/disc",
742 				prefix, host, bus, target, lun);
743 	*base = '\0';
744 	sprintf(src, "/dev/%s", name);
745 	sys_mkdir(src, 0755);
746 	*base = '/';
747 	sprintf(src, "/dev/%s", name);
748 	sys_symlink(dest, src);
749 }
750 
devfs_make_root(char * name)751 static void __init devfs_make_root(char *name)
752 {
753 
754 	if (!strncmp(name, "sd/", 3))
755 		convert_name("../scsi", name, name+3, 1);
756 	else if (!strncmp(name, "sr/", 3))
757 		convert_name("../scsi", name, name+3, 0);
758 	else if (!strncmp(name, "ide/hd/", 7))
759 		convert_name("..", name, name + 7, 1);
760 	else if (!strncmp(name, "ide/cd/", 7))
761 		convert_name("..", name, name + 7, 0);
762 }
763 #else
devfs_make_root(char * name)764 static void __init devfs_make_root(char *name)
765 {
766 }
767 #endif
768 
mount_root(void)769 static void __init mount_root(void)
770 {
771 #ifdef CONFIG_ROOT_NFS
772        if (MAJOR(ROOT_DEV) == NFS_MAJOR
773            && MINOR(ROOT_DEV) == NFS_MINOR) {
774 		if (mount_nfs_root()) {
775 			sys_chdir("/root");
776 			ROOT_DEV = current->fs->pwdmnt->mnt_sb->s_dev;
777 			printk("VFS: Mounted root (nfs filesystem).\n");
778 			return;
779 		}
780 		printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
781 		ROOT_DEV = MKDEV(FLOPPY_MAJOR, 0);
782 	}
783 #endif
784 	devfs_make_root(root_device_name);
785 	create_dev("/dev/root", ROOT_DEV, root_device_name);
786 #ifdef CONFIG_BLK_DEV_FD
787 	if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
788 		/* rd_doload is 2 for a dual initrd/ramload setup */
789 		if (rd_doload==2) {
790 			if (rd_load_disk(1)) {
791 				ROOT_DEV = MKDEV(RAMDISK_MAJOR, 1);
792 				create_dev("/dev/root", ROOT_DEV, NULL);
793 			}
794 		} else
795 			change_floppy("root floppy");
796 	}
797 #endif
798 	mount_block_root("/dev/root", root_mountflags);
799 }
800 
801 #ifdef CONFIG_BLK_DEV_INITRD
802 static int old_fd, root_fd;
do_linuxrc(void * shell)803 static int do_linuxrc(void * shell)
804 {
805 	static char *argv[] = { "linuxrc", NULL, };
806 	extern char * envp_init[];
807 
808 	close(old_fd);
809 	close(root_fd);
810 	close(0);
811 	close(1);
812 	close(2);
813 	setsid();
814 	(void) open("/dev/console",O_RDWR,0);
815 	(void) dup(0);
816 	(void) dup(0);
817 	return execve(shell, argv, envp_init);
818 }
819 
820 #endif
821 
handle_initrd(void)822 static void __init handle_initrd(void)
823 {
824 #ifdef CONFIG_BLK_DEV_INITRD
825 	int ram0 = kdev_t_to_nr(MKDEV(RAMDISK_MAJOR,0));
826 	int error;
827 	int i, pid;
828 
829 	create_dev("/dev/root.old", ram0, NULL);
830 	/* mount initrd on rootfs' /root */
831 	mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
832 	sys_mkdir("/old", 0700);
833 	root_fd = open("/", 0, 0);
834 	old_fd = open("/old", 0, 0);
835 	/* move initrd over / and chdir/chroot in initrd root */
836 	sys_chdir("/root");
837 	sys_mount(".", "/", NULL, MS_MOVE, NULL);
838 	sys_chroot(".");
839 	mount_devfs_fs ();
840 
841 	pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);
842 	if (pid > 0) {
843 		while (pid != wait(&i))
844 			yield();
845 	}
846 
847 	/* move initrd to rootfs' /old */
848 	sys_fchdir(old_fd);
849 	sys_mount("/", ".", NULL, MS_MOVE, NULL);
850 	/* switch root and cwd back to / of rootfs */
851 	sys_fchdir(root_fd);
852 	sys_chroot(".");
853 	sys_umount("/old/dev", 0);
854 	close(old_fd);
855 	close(root_fd);
856 
857 	if (real_root_dev == ram0) {
858 		sys_chdir("/old");
859 		return;
860 	}
861 
862 	ROOT_DEV = real_root_dev;
863 	mount_root();
864 
865 	printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
866 	error = sys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
867 	if (!error)
868 		printk("okay\n");
869 	else {
870 		int fd = open("/dev/root.old", O_RDWR, 0);
871 		printk("failed\n");
872 		printk(KERN_NOTICE "Unmounting old root\n");
873 		sys_umount("/old", MNT_DETACH);
874 		printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
875 		if (fd < 0) {
876 			error = fd;
877 		} else {
878 			error = sys_ioctl(fd, BLKFLSBUF, 0);
879 			close(fd);
880 		}
881 		printk(!error ? "okay\n" : "failed\n");
882 	}
883 #endif
884 }
885 
initrd_load(void)886 static int __init initrd_load(void)
887 {
888 #ifdef CONFIG_BLK_DEV_INITRD
889 	create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, 0), NULL);
890 	create_dev("/dev/initrd", MKDEV(RAMDISK_MAJOR, INITRD_MINOR), NULL);
891 #endif
892 	return rd_load_image("/dev/initrd");
893 }
894 
895 /*
896  * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
897  */
prepare_namespace(void)898 void prepare_namespace(void)
899 {
900 	int is_floppy;
901 
902 	if (root_delay) {
903 		printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
904 		       root_delay);
905 		ssleep(root_delay);
906 	}
907 
908 	is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
909 #ifdef CONFIG_ALL_PPC
910 	extern void arch_discover_root(void);
911 	arch_discover_root();
912 #endif /* CONFIG_ALL_PPC */
913 #ifdef CONFIG_BLK_DEV_INITRD
914 	if (!initrd_start)
915 		mount_initrd = 0;
916 	real_root_dev = ROOT_DEV;
917 #endif
918 	sys_mkdir("/dev", 0700);
919 	sys_mkdir("/root", 0700);
920 	sys_mknod("/dev/console", S_IFCHR|0600, MKDEV(TTYAUX_MAJOR, 1));
921 #ifdef CONFIG_DEVFS_FS
922 	sys_mount("devfs", "/dev", "devfs", 0, NULL);
923 	do_devfs = 1;
924 #endif
925 
926 	create_dev("/dev/root", ROOT_DEV, NULL);
927 	if (mount_initrd) {
928 		if (initrd_load() && ROOT_DEV != MKDEV(RAMDISK_MAJOR, 0)) {
929 			handle_initrd();
930 			goto out;
931 		}
932 	} else if (is_floppy && rd_doload && rd_load_disk(0))
933 		ROOT_DEV = MKDEV(RAMDISK_MAJOR, 0);
934 	mount_root();
935 out:
936 	sys_umount("/dev", 0);
937 	sys_mount(".", "/", NULL, MS_MOVE, NULL);
938 	sys_chroot(".");
939 	mount_devfs_fs ();
940 }
941 
942 #ifdef CONFIG_BLK_DEV_RAM
943 
944 #if defined(BUILD_CRAMDISK) && defined(CONFIG_BLK_DEV_RAM)
945 
946 /*
947  * gzip declarations
948  */
949 
950 #define OF(args)  args
951 
952 #ifndef memzero
953 #define memzero(s, n)     memset ((s), 0, (n))
954 #endif
955 
956 typedef unsigned char  uch;
957 typedef unsigned short ush;
958 typedef unsigned long  ulg;
959 
960 #define INBUFSIZ 4096
961 #define WSIZE 0x8000    /* window size--must be a power of two, and */
962 			/*  at least 32K for zip's deflate method */
963 
964 static uch *inbuf;
965 static uch *window;
966 
967 static unsigned insize;  /* valid bytes in inbuf */
968 static unsigned inptr;   /* index of next byte to be processed in inbuf */
969 static unsigned outcnt;  /* bytes in output buffer */
970 static int exit_code;
971 static long bytes_out;
972 static int crd_infd, crd_outfd;
973 
974 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
975 
976 /* Diagnostic functions (stubbed out) */
977 #define Assert(cond,msg)
978 #define Trace(x)
979 #define Tracev(x)
980 #define Tracevv(x)
981 #define Tracec(c,x)
982 #define Tracecv(c,x)
983 
984 #define STATIC static
985 
986 static int  fill_inbuf(void);
987 static void flush_window(void);
988 static void *malloc(int size);
989 static void free(void *where);
990 static void error(char *m);
991 static void gzip_mark(void **);
992 static void gzip_release(void **);
993 
994 #include "../lib/inflate.c"
995 
malloc(int size)996 static void __init *malloc(int size)
997 {
998 	return kmalloc(size, GFP_KERNEL);
999 }
1000 
free(void * where)1001 static void __init free(void *where)
1002 {
1003 	kfree(where);
1004 }
1005 
gzip_mark(void ** ptr)1006 static void __init gzip_mark(void **ptr)
1007 {
1008 }
1009 
gzip_release(void ** ptr)1010 static void __init gzip_release(void **ptr)
1011 {
1012 }
1013 
1014 
1015 /* ===========================================================================
1016  * Fill the input buffer. This is called only when the buffer is empty
1017  * and at least one byte is really needed.
1018  */
fill_inbuf(void)1019 static int __init fill_inbuf(void)
1020 {
1021 	if (exit_code) return -1;
1022 
1023 	insize = read(crd_infd, inbuf, INBUFSIZ);
1024 	if (insize == 0) return -1;
1025 
1026 	inptr = 1;
1027 
1028 	return inbuf[0];
1029 }
1030 
1031 /* ===========================================================================
1032  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
1033  * (Used for the decompressed data only.)
1034  */
flush_window(void)1035 static void __init flush_window(void)
1036 {
1037     ulg c = crc;         /* temporary variable */
1038     unsigned n;
1039     uch *in, ch;
1040 
1041     write(crd_outfd, window, outcnt);
1042     in = window;
1043     for (n = 0; n < outcnt; n++) {
1044 	    ch = *in++;
1045 	    c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
1046     }
1047     crc = c;
1048     bytes_out += (ulg)outcnt;
1049     outcnt = 0;
1050 }
1051 
error(char * x)1052 static void __init error(char *x)
1053 {
1054 	printk(KERN_ERR "%s", x);
1055 	exit_code = 1;
1056 }
1057 
crd_load(int in_fd,int out_fd)1058 static int __init crd_load(int in_fd, int out_fd)
1059 {
1060 	int result;
1061 
1062 	insize = 0;		/* valid bytes in inbuf */
1063 	inptr = 0;		/* index of next byte to be processed in inbuf */
1064 	outcnt = 0;		/* bytes in output buffer */
1065 	exit_code = 0;
1066 	bytes_out = 0;
1067 	crc = (ulg)0xffffffffL; /* shift register contents */
1068 
1069 	crd_infd = in_fd;
1070 	crd_outfd = out_fd;
1071 	inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
1072 	if (inbuf == 0) {
1073 		printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
1074 		return -1;
1075 	}
1076 	window = kmalloc(WSIZE, GFP_KERNEL);
1077 	if (window == 0) {
1078 		printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
1079 		kfree(inbuf);
1080 		return -1;
1081 	}
1082 	makecrc();
1083 	result = gunzip();
1084 	kfree(inbuf);
1085 	kfree(window);
1086 	return result;
1087 }
1088 
1089 #endif  /* BUILD_CRAMDISK && CONFIG_BLK_DEV_RAM */
1090 #endif  /* CONFIG_BLK_DEV_RAM */
1091