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