This source file includes following definitions.
- get_options
- profile_setup
- ramdisk_start_setup
- load_ramdisk
- prompt_ramdisk
- checksetup
- calibrate_delay
- parse_options
- cpu_idle
- start_secondary
- smp_init
- smp_begin
- start_kernel
- printf
- do_rc
- do_shell
- init
1
2
3
4
5
6
7
8
9 #define __KERNEL_SYSCALLS__
10 #include <stdarg.h>
11
12 #include <asm/system.h>
13 #include <asm/io.h>
14
15 #include <linux/types.h>
16 #include <linux/fcntl.h>
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <linux/tty.h>
20 #include <linux/head.h>
21 #include <linux/unistd.h>
22 #include <linux/string.h>
23 #include <linux/timer.h>
24 #include <linux/fs.h>
25 #include <linux/ctype.h>
26 #include <linux/delay.h>
27 #include <linux/utsname.h>
28 #include <linux/ioport.h>
29 #include <linux/hdreg.h>
30 #include <linux/mm.h>
31 #include <linux/major.h>
32 #ifdef CONFIG_APM
33 #include <linux/apm_bios.h>
34 #endif
35
36 #include <asm/bugs.h>
37
38 extern char _stext, _etext;
39 extern char *linux_banner;
40
41 static char printbuf[1024];
42
43 extern int console_loglevel;
44
45 static int init(void *);
46 extern int bdflush(void *);
47
48 extern void init_IRQ(void);
49 extern void init_modules(void);
50 extern long console_init(long, long);
51 extern long kmalloc_init(long,long);
52 extern void sock_init(void);
53 extern long pci_init(long, long);
54
55 extern void swap_setup(char *str, int *ints);
56 extern void buff_setup(char *str, int *ints);
57 extern void bmouse_setup(char *str, int *ints);
58 extern void eth_setup(char *str, int *ints);
59 extern void xd_setup(char *str, int *ints);
60 extern void floppy_setup(char *str, int *ints);
61 extern void st_setup(char *str, int *ints);
62 extern void st0x_setup(char *str, int *ints);
63 extern void tmc8xx_setup(char *str, int *ints);
64 extern void t128_setup(char *str, int *ints);
65 extern void pas16_setup(char *str, int *ints);
66 extern void generic_NCR5380_setup(char *str, int *intr);
67 extern void aha152x_setup(char *str, int *ints);
68 extern void aha1542_setup(char *str, int *ints);
69 extern void aic7xxx_setup(char *str, int *ints);
70 extern void BusLogic_Setup(char *str, int *ints);
71 extern void fdomain_setup(char *str, int *ints);
72 extern void NCR53c406a_setup(char *str, int *ints);
73 extern void scsi_luns_setup(char *str, int *ints);
74 extern void sound_setup(char *str, int *ints);
75 #ifdef CONFIG_CDU31A
76 extern void cdu31a_setup(char *str, int *ints);
77 #endif CONFIG_CDU31A
78 #ifdef CONFIG_MCD
79 extern void mcd_setup(char *str, int *ints);
80 #endif CONFIG_MCD
81 #ifdef CONFIG_MCDX
82 extern void mcdx_setup(char *str, int *ints);
83 #endif CONFIG_MCDX
84 #ifdef CONFIG_SBPCD
85 extern void sbpcd_setup(char *str, int *ints);
86 #endif CONFIG_SBPCD
87 #ifdef CONFIG_AZTCD
88 extern void aztcd_setup(char *str, int *ints);
89 #endif CONFIG_AZTCD
90 #ifdef CONFIG_CDU535
91 extern void sonycd535_setup(char *str, int *ints);
92 #endif CONFIG_CDU535
93 #ifdef CONFIG_GSCD
94 extern void gscd_setup(char *str, int *ints);
95 #endif CONFIG_GSCD
96 #ifdef CONFIG_CM206
97 extern void cm206_setup(char *str, int *ints);
98 #endif CONFIG_CM206
99 #ifdef CONFIG_OPTCD
100 extern void optcd_setup(char *str, int *ints);
101 #endif CONFIG_OPTCD
102 #ifdef CONFIG_SJCD
103 extern void sjcd_setup(char *str, int *ints);
104 #endif CONFIG_SJCD
105 #ifdef CONFIG_BLK_DEV_RAM
106 static void ramdisk_start_setup(char *str, int *ints);
107 static void load_ramdisk(char *str, int *ints);
108 static void prompt_ramdisk(char *str, int *ints);
109 #endif CONFIG_BLK_DEV_RAM
110
111 #ifdef CONFIG_SYSVIPC
112 extern void ipc_init(void);
113 #endif
114
115
116
117
118 #define MAX_INIT_ARGS 8
119 #define MAX_INIT_ENVS 8
120
121 extern void time_init(void);
122
123 static unsigned long memory_start = 0;
124 static unsigned long memory_end = 0;
125
126 int rows, cols;
127
128 #ifdef CONFIG_BLK_DEV_RAM
129 extern int rd_doload;
130 extern int rd_prompt;
131 extern int rd_image_start;
132 #endif
133
134 int root_mountflags = MS_RDONLY;
135 char *execute_command = 0;
136
137 #ifdef CONFIG_ROOT_NFS
138 char nfs_root_name[256] = { NFS_ROOT };
139 char nfs_root_addrs[128] = { "" };
140 #endif
141
142 extern void dquot_init(void);
143
144 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
145 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
146
147 static char * argv_rc[] = { "/bin/sh", NULL };
148 static char * envp_rc[] = { "HOME=/", "TERM=linux", NULL };
149
150 static char * argv[] = { "-/bin/sh",NULL };
151 static char * envp[] = { "HOME=/usr/root", "TERM=linux", NULL };
152
153 char *get_options(char *str, int *ints)
154 {
155 char *cur = str;
156 int i=1;
157
158 while (cur && isdigit(*cur) && i <= 10) {
159 ints[i++] = simple_strtoul(cur,NULL,0);
160 if ((cur = strchr(cur,',')) != NULL)
161 cur++;
162 }
163 ints[0] = i-1;
164 return(cur);
165 }
166
167 static void profile_setup(char *str, int *ints)
168 {
169 if (ints[0] > 0)
170 prof_shift = (unsigned long) ints[1];
171 else
172 #ifdef CONFIG_PROFILE_SHIFT
173 prof_shift = CONFIG_PROFILE_SHIFT;
174 #else
175 prof_shift = 2;
176 #endif
177 }
178
179 struct {
180 const char *str;
181 void (*setup_func)(char *, int *);
182 } bootsetups[] = {
183 { "reserve=", reserve_setup },
184 { "profile=", profile_setup },
185 #ifdef CONFIG_BLK_DEV_RAM
186 { "ramdisk_start=", ramdisk_start_setup },
187 { "load_ramdisk=", load_ramdisk },
188 { "prompt_ramdisk=", prompt_ramdisk },
189 #endif
190 { "swap=", swap_setup },
191 { "buff=", buff_setup },
192 #ifdef CONFIG_BUGi386
193 { "no-hlt", no_halt },
194 { "no387", no_387 },
195 #endif
196 #ifdef CONFIG_INET
197 { "ether=", eth_setup },
198 #endif
199 #ifdef CONFIG_SCSI
200 { "max_scsi_luns=", scsi_luns_setup },
201 #endif
202 #if defined(CONFIG_BLK_DEV_HD)
203 { "hd=", hd_setup },
204 #endif
205 #ifdef CONFIG_CHR_DEV_ST
206 { "st=", st_setup },
207 #endif
208 #ifdef CONFIG_BUSMOUSE
209 { "bmouse=", bmouse_setup },
210 #endif
211 #ifdef CONFIG_SCSI_SEAGATE
212 { "st0x=", st0x_setup },
213 { "tmc8xx=", tmc8xx_setup },
214 #endif
215 #ifdef CONFIG_SCSI_T128
216 { "t128=", t128_setup },
217 #endif
218 #ifdef CONFIG_SCSI_PAS16
219 { "pas16=", pas16_setup },
220 #endif
221 #ifdef CONFIG_SCSI_GENERIC_NCR5380
222 { "ncr5380=", generic_NCR5380_setup },
223 #endif
224 #ifdef CONFIG_SCSI_AHA152X
225 { "aha152x=", aha152x_setup},
226 #endif
227 #ifdef CONFIG_SCSI_AHA1542
228 { "aha1542=", aha1542_setup},
229 #endif
230 #ifdef CONFIG_SCSI_AIC7XXX
231 { "aic7xxx=", aic7xxx_setup},
232 #endif
233 #ifdef CONFIG_SCSI_BUSLOGIC
234 { "BusLogic=", BusLogic_Setup},
235 #endif
236 #ifdef CONFIG_SCSI_NCR53C406A
237 { "ncr53c406a=", NCR53c406a_setup},
238 #endif
239 #ifdef CONFIG_SCSI_FUTURE_DOMAIN
240 { "fdomain=", fdomain_setup},
241 #endif
242 #ifdef CONFIG_BLK_DEV_XD
243 { "xd=", xd_setup },
244 #endif
245 #ifdef CONFIG_BLK_DEV_FD
246 { "floppy=", floppy_setup },
247 #endif
248 #ifdef CONFIG_CDU31A
249 { "cdu31a=", cdu31a_setup },
250 #endif CONFIG_CDU31A
251 #ifdef CONFIG_MCD
252 { "mcd=", mcd_setup },
253 #endif CONFIG_MCD
254 #ifdef CONFIG_MCDX
255 { "mcdx=", mcdx_setup },
256 #endif CONFIG_MCDX
257 #ifdef CONFIG_SBPCD
258 { "sbpcd=", sbpcd_setup },
259 #endif CONFIG_SBPCD
260 #ifdef CONFIG_AZTCD
261 { "aztcd=", aztcd_setup },
262 #endif CONFIG_AZTCD
263 #ifdef CONFIG_CDU535
264 { "sonycd535=", sonycd535_setup },
265 #endif CONFIG_CDU535
266 #ifdef CONFIG_GSCD
267 { "gscd=", gscd_setup },
268 #endif CONFIG_GSCD
269 #ifdef CONFIG_CM206
270 { "cm206=", cm206_setup },
271 #endif CONFIG_CM206
272 #ifdef CONFIG_OPTCD
273 { "optcd=", optcd_setup },
274 #endif CONFIG_OPTCD
275 #ifdef CONFIG_SJCD
276 { "sjcd=", sjcd_setup },
277 #endif CONFIG_SJCD
278 #ifdef CONFIG_SOUND
279 { "sound=", sound_setup },
280 #endif
281 { 0, 0 }
282 };
283
284 #ifdef CONFIG_BLK_DEV_RAM
285 static void ramdisk_start_setup(char *str, int *ints)
286 {
287 if (ints[0] > 0 && ints[1] >= 0)
288 rd_image_start = ints[1];
289 }
290
291 static void load_ramdisk(char *str, int *ints)
292 {
293 if (ints[0] > 0 && ints[1] >= 0)
294 rd_doload = ints[1] & 1;
295 }
296
297 static void prompt_ramdisk(char *str, int *ints)
298 {
299 if (ints[0] > 0 && ints[1] >= 0)
300 rd_prompt = ints[1] & 1;
301 }
302 #endif
303
304 static int checksetup(char *line)
305 {
306 int i = 0;
307 int ints[11];
308
309 #ifdef CONFIG_BLK_DEV_IDE
310
311 if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '=')) {
312 ide_setup(line);
313 return 1;
314 }
315 #endif
316 while (bootsetups[i].str) {
317 int n = strlen(bootsetups[i].str);
318 if (!strncmp(line,bootsetups[i].str,n)) {
319 bootsetups[i].setup_func(get_options(line+n,ints), ints);
320 return 1;
321 }
322 i++;
323 }
324 return 0;
325 }
326
327
328
329 unsigned long loops_per_sec = (1<<12);
330
331
332
333
334 #define LPS_PREC 8
335
336 void calibrate_delay(void)
337 {
338 int ticks;
339 int loopbit;
340 int lps_precision = LPS_PREC;
341
342 loops_per_sec = (1<<12);
343
344 printk("Calibrating delay loop.. ");
345 while (loops_per_sec <<= 1) {
346
347 ticks = jiffies;
348 while (ticks == jiffies)
349 ;
350
351 ticks = jiffies;
352 __delay(loops_per_sec);
353 ticks = jiffies - ticks;
354 if (ticks)
355 break;
356 }
357
358
359
360 loops_per_sec >>= 1;
361 loopbit = loops_per_sec;
362 while ( lps_precision-- && (loopbit >>= 1) ) {
363 loops_per_sec |= loopbit;
364 ticks = jiffies;
365 while (ticks == jiffies);
366 ticks = jiffies;
367 __delay(loops_per_sec);
368 if (jiffies != ticks)
369 loops_per_sec &= ~loopbit;
370 }
371
372
373 loops_per_sec *= HZ;
374
375 printk("ok - %lu.%02lu BogoMIPS\n",
376 (loops_per_sec+2500)/500000,
377 ((loops_per_sec+2500)/5000) % 100);
378 }
379
380
381
382
383
384
385
386
387
388
389
390 static void parse_options(char *line)
391 {
392 char *next;
393 char *devnames[] = { "nfs", "hda", "hdb", "hdc", "hdd", "sda", "sdb",
394 "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
395 int devnums[] = { 0x0FF, 0x300, 0x340, 0x1600, 0x1640, 0x800,
396 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
397 int args, envs;
398 if (!*line)
399 return;
400 args = 0;
401 envs = 1;
402 next = line;
403 while ((line = next) != NULL) {
404 if ((next = strchr(line,' ')) != NULL)
405 *next++ = 0;
406
407
408
409 if (!strncmp(line,"root=",5)) {
410 int n;
411 line += 5;
412 if (strncmp(line,"/dev/",5)) {
413 ROOT_DEV = to_kdev_t(
414 simple_strtoul(line,NULL,16));
415 continue;
416 }
417 line += 5;
418 for (n = 0 ; devnames[n] ; n++) {
419 int len = strlen(devnames[n]);
420 if (!strncmp(line,devnames[n],len)) {
421 ROOT_DEV = to_kdev_t(devnums[n]+
422 simple_strtoul(line+len,NULL,0));
423 break;
424 }
425 }
426 continue;
427 }
428 #ifdef CONFIG_ROOT_NFS
429 if (!strncmp(line, "nfsroot=", 8)) {
430 int n;
431 line += 8;
432 ROOT_DEV = MKDEV(UNNAMED_MAJOR, 255);
433 if (line[0] == '/' || (line[0] >= '0' && line[0] <= '9')) {
434 strncpy(nfs_root_name, line, sizeof(nfs_root_name));
435 nfs_root_name[sizeof(nfs_root_name)-1] = '\0';
436 continue;
437 }
438 n = strlen(line) + strlen(NFS_ROOT);
439 if (n >= sizeof(nfs_root_name))
440 line[sizeof(nfs_root_name) - strlen(NFS_ROOT) - 1] = '\0';
441 sprintf(nfs_root_name, NFS_ROOT, line);
442 continue;
443 }
444 if (!strncmp(line, "nfsaddrs=", 9)) {
445 line += 9;
446 strncpy(nfs_root_addrs, line, sizeof(nfs_root_addrs));
447 nfs_root_addrs[sizeof(nfs_root_addrs)] = '\0';
448 continue;
449 }
450 #endif
451 if (!strcmp(line,"ro")) {
452 root_mountflags |= MS_RDONLY;
453 continue;
454 }
455 if (!strcmp(line,"rw")) {
456 root_mountflags &= ~MS_RDONLY;
457 continue;
458 }
459 if (!strcmp(line,"debug")) {
460 console_loglevel = 10;
461 continue;
462 }
463 if (!strncmp(line,"init=",5)) {
464 line += 5;
465 execute_command = line;
466 continue;
467 }
468 if (checksetup(line))
469 continue;
470
471
472
473
474 if (strchr(line,'=')) {
475 if (envs >= MAX_INIT_ENVS)
476 break;
477 envp_init[++envs] = line;
478 } else {
479 if (args >= MAX_INIT_ARGS)
480 break;
481 argv_init[++args] = line;
482 }
483 }
484 argv_init[args+1] = NULL;
485 envp_init[envs+1] = NULL;
486 }
487
488
489 extern void setup_arch(char **, unsigned long *, unsigned long *);
490
491 #ifndef __SMP__
492
493
494
495
496
497 int cpu_idle(void *unused)
498 {
499 for(;;)
500 idle();
501 }
502
503 #else
504
505
506
507
508
509 extern int cpu_idle(void * unused);
510
511
512
513
514
515 asmlinkage void start_secondary(void)
516 {
517 trap_init();
518 init_IRQ();
519 smp_callin();
520 cpu_idle(NULL);
521 }
522
523
524
525
526
527
528
529 static void smp_init(void)
530 {
531 int i=0;
532 smp_boot_cpus();
533
534
535
536
537
538 for(i=1;i<smp_num_cpus;i++)
539 {
540
541
542
543
544 kernel_thread(cpu_idle, NULL, CLONE_PID);
545
546
547
548 current_set[i]=task[i];
549 current_set[i]->processor=i;
550 }
551 }
552
553
554
555
556
557
558
559 static void smp_begin(void)
560 {
561 smp_threads_ready=1;
562 smp_commence();
563 }
564
565 #endif
566
567
568
569
570
571 asmlinkage void start_kernel(void)
572 {
573 char * command_line;
574
575
576
577
578
579 #ifdef __SMP__
580 static int first_cpu=1;
581
582 if(!first_cpu)
583 start_secondary();
584 first_cpu=0;
585
586 #endif
587
588
589
590
591 setup_arch(&command_line, &memory_start, &memory_end);
592 memory_start = paging_init(memory_start,memory_end);
593 trap_init();
594 init_IRQ();
595 sched_init();
596 time_init();
597 parse_options(command_line);
598 #ifdef CONFIG_MODULES
599 init_modules();
600 #endif
601 #ifdef CONFIG_PROFILE
602 if (!prof_shift)
603 #ifdef CONFIG_PROFILE_SHIFT
604 prof_shift = CONFIG_PROFILE_SHIFT;
605 #else
606 prof_shift = 2;
607 #endif
608 #endif
609 if (prof_shift) {
610 prof_buffer = (unsigned long *) memory_start;
611
612 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
613 prof_len >>= prof_shift;
614 memory_start += prof_len * sizeof(unsigned long);
615 }
616 memory_start = console_init(memory_start,memory_end);
617 #ifdef CONFIG_PCI
618 memory_start = pci_init(memory_start,memory_end);
619 #endif
620 memory_start = kmalloc_init(memory_start,memory_end);
621 sti();
622 calibrate_delay();
623 memory_start = inode_init(memory_start,memory_end);
624 memory_start = file_table_init(memory_start,memory_end);
625 memory_start = name_cache_init(memory_start,memory_end);
626 mem_init(memory_start,memory_end);
627 buffer_init();
628 sock_init();
629 #ifdef CONFIG_SYSVIPC
630 ipc_init();
631 #endif
632 #ifdef CONFIG_APM
633 apm_bios_init();
634 #endif
635 dquot_init();
636 sti();
637 check_bugs();
638
639 printk(linux_banner);
640 #ifdef __SMP__
641 smp_init();
642 #endif
643
644
645
646
647
648 kernel_thread(init, NULL, 0);
649
650
651
652
653
654
655
656
657
658 cpu_idle(NULL);
659 }
660
661 static int printf(const char *fmt, ...)
662 {
663 va_list args;
664 int i;
665
666 va_start(args, fmt);
667 write(1,printbuf,i=vsprintf(printbuf, fmt, args));
668 va_end(args);
669 return i;
670 }
671
672 static int do_rc(void * rc)
673 {
674 close(0);
675 if (open(rc,O_RDONLY,0))
676 return -1;
677 return execve("/bin/sh", argv_rc, envp_rc);
678 }
679
680 static int do_shell(void * shell)
681 {
682 close(0);close(1);close(2);
683 setsid();
684 (void) open("/dev/tty1",O_RDWR,0);
685 (void) dup(0);
686 (void) dup(0);
687 return execve(shell, argv, envp);
688 }
689
690 static int init(void * unused)
691 {
692 int pid,i;
693
694
695 kernel_thread(bdflush, NULL, 0);
696
697 setup();
698
699 #ifdef __SMP__
700
701
702
703
704
705 smp_begin();
706 #endif
707
708 #ifdef CONFIG_UMSDOS_FS
709 {
710
711
712
713
714
715 extern struct inode *pseudo_root;
716 if (pseudo_root != NULL){
717 current->fs->root = pseudo_root;
718 current->fs->pwd = pseudo_root;
719 }
720 }
721 #endif
722
723 (void) open("/dev/tty1",O_RDWR,0);
724 (void) dup(0);
725 (void) dup(0);
726
727 if (!execute_command) {
728 execve("/etc/init",argv_init,envp_init);
729 execve("/bin/init",argv_init,envp_init);
730 execve("/sbin/init",argv_init,envp_init);
731
732
733 pid = kernel_thread(do_rc, "/etc/rc", SIGCHLD);
734 if (pid>0)
735 while (pid != wait(&i))
736 ;
737 }
738
739 while (1) {
740 pid = kernel_thread(do_shell,
741 execute_command ? execute_command : "/bin/sh",
742 SIGCHLD);
743 if (pid < 0) {
744 printf("Fork failed in init\n\r");
745 continue;
746 }
747 while (1)
748 if (pid == wait(&i))
749 break;
750 printf("\n\rchild %d died with code %04x\n\r",pid,i);
751 sync();
752 }
753 return -1;
754 }