root/init/main.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_options
  2. profile_setup
  3. ramdisk_setup
  4. checksetup
  5. calibrate_delay
  6. parse_options
  7. start_kernel
  8. printf
  9. init

   1 /*
   2  *  linux/init/main.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #define __KERNEL_SYSCALLS__
   8 #include <stdarg.h>
   9 
  10 #include <asm/system.h>
  11 #include <asm/io.h>
  12 
  13 #include <linux/types.h>
  14 #include <linux/fcntl.h>
  15 #include <linux/config.h>
  16 #include <linux/sched.h>
  17 #include <linux/tty.h>
  18 #include <linux/head.h>
  19 #include <linux/unistd.h>
  20 #include <linux/string.h>
  21 #include <linux/timer.h>
  22 #include <linux/fs.h>
  23 #include <linux/ctype.h>
  24 #include <linux/delay.h>
  25 #include <linux/utsname.h>
  26 #include <linux/ioport.h>
  27 #include <linux/hdreg.h>
  28 #include <linux/mm.h>
  29 
  30 #include <asm/bugs.h>
  31 
  32 extern char _stext, _etext;
  33 extern char *linux_banner;
  34 
  35 static char printbuf[1024];
  36 
  37 extern int console_loglevel;
  38 
  39 extern void init(void);
  40 extern void init_IRQ(void);
  41 extern void init_modules(void);
  42 extern long console_init(long, long);
  43 extern long kmalloc_init(long,long);
  44 extern long blk_dev_init(long,long);
  45 extern long chr_dev_init(long,long);
  46 extern void sock_init(void);
  47 extern long rd_init(long mem_start, int length);
  48 unsigned long net_dev_init(unsigned long, unsigned long);
  49 extern long pci_init(long, long);
  50 
  51 extern void bmouse_setup(char *str, int *ints);
  52 extern void eth_setup(char *str, int *ints);
  53 extern void xd_setup(char *str, int *ints);
  54 extern void floppy_setup(char *str, int *ints);
  55 extern void st_setup(char *str, int *ints);
  56 extern void st0x_setup(char *str, int *ints);
  57 extern void tmc8xx_setup(char *str, int *ints);
  58 extern void t128_setup(char *str, int *ints);
  59 extern void pas16_setup(char *str, int *ints);
  60 extern void generic_NCR5380_setup(char *str, int *intr);
  61 extern void aha152x_setup(char *str, int *ints);
  62 extern void aha1542_setup(char *str, int *ints);
  63 extern void aic7xxx_setup(char *str, int *ints);
  64 extern void buslogic_setup(char *str, int *ints);
  65 extern void fdomain_setup(char *str, int *ints);
  66 extern void NCR53c406a_setup(char *str, int *ints);
  67 extern void scsi_luns_setup(char *str, int *ints);
  68 extern void sound_setup(char *str, int *ints);
  69 #ifdef CONFIG_CDU31A
  70 extern void cdu31a_setup(char *str, int *ints);
  71 #endif CONFIG_CDU31A
  72 #ifdef CONFIG_MCD
  73 extern void mcd_setup(char *str, int *ints);
  74 #endif CONFIG_MCD
  75 #ifdef CONFIG_MCDX
  76 extern void mcdx_setup(char *str, int *ints);
  77 #endif CONFIG_MCDX
  78 #ifdef CONFIG_SBPCD
  79 extern void sbpcd_setup(char *str, int *ints);
  80 #endif CONFIG_SBPCD
  81 #ifdef CONFIG_AZTCD
  82 extern void aztcd_setup(char *str, int *ints);
  83 #endif CONFIG_AZTCD
  84 #ifdef CONFIG_CDU535
  85 extern void sonycd535_setup(char *str, int *ints);
  86 #endif CONFIG_CDU535
  87 #ifdef CONFIG_GSCD
  88 extern void gscd_setup(char *str, int *ints);
  89 #endif CONFIG_GSCD
  90 #ifdef CONFIG_CM206
  91 extern void cm206_setup(char *str, int *ints);
  92 #endif CONFIG_CM206
  93 #ifdef CONFIG_OPTCD
  94 extern void optcd_setup(char *str, int *ints);
  95 #endif CONFIG_OPTCD
  96 #ifdef CONFIG_SJCD
  97 extern void sjcd_setup(char *str, int *ints);
  98 #endif CONFIG_SJCD
  99 void ramdisk_setup(char *str, int *ints);
 100 
 101 #ifdef CONFIG_SYSVIPC
 102 extern void ipc_init(void);
 103 #endif
 104 #ifdef CONFIG_SCSI
 105 extern unsigned long scsi_dev_init(unsigned long, unsigned long);
 106 #endif
 107 
 108 /*
 109  * Boot command-line arguments
 110  */
 111 #define MAX_INIT_ARGS 8
 112 #define MAX_INIT_ENVS 8
 113 
 114 extern void time_init(void);
 115 
 116 static unsigned long memory_start = 0;
 117 static unsigned long memory_end = 0;
 118 
 119 int rows, cols;
 120 
 121 int ramdisk_size;
 122 int root_mountflags = 0;
 123 
 124 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
 125 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
 126 
 127 static char * argv_rc[] = { "/bin/sh", NULL };
 128 static char * envp_rc[] = { "HOME=/", "TERM=linux", NULL };
 129 
 130 static char * argv[] = { "-/bin/sh",NULL };
 131 static char * envp[] = { "HOME=/usr/root", "TERM=linux", NULL };
 132 
 133 char *get_options(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 134 {
 135         char *cur = str;
 136         int i=1;
 137 
 138         while (cur && isdigit(*cur) && i <= 10) {
 139                 ints[i++] = simple_strtoul(cur,NULL,0);
 140                 if ((cur = strchr(cur,',')) != NULL)
 141                         cur++;
 142         }
 143         ints[0] = i-1;
 144         return(cur);
 145 }
 146 
 147 static void profile_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149         if (ints[0] > 0)
 150                 prof_shift = (unsigned long) ints[1];
 151         else
 152 #ifdef CONFIG_PROFILE_SHIFT
 153                 prof_shift = CONFIG_PROFILE_SHIFT;
 154 #else
 155                 prof_shift = 2;
 156 #endif
 157 }
 158 
 159 struct {
 160         const char *str;
 161         void (*setup_func)(char *, int *);
 162 } bootsetups[] = {
 163         { "reserve=", reserve_setup },
 164         { "profile=", profile_setup },
 165         { "ramdisk=", ramdisk_setup },
 166 #ifdef CONFIG_BUGi386
 167         { "no-hlt", no_halt },
 168         { "no387", no_387 },
 169 #endif
 170 #ifdef CONFIG_INET
 171         { "ether=", eth_setup },
 172 #endif
 173 #ifdef CONFIG_SCSI
 174         { "max_scsi_luns=", scsi_luns_setup },
 175 #endif
 176 #if defined(CONFIG_BLK_DEV_HD)
 177         { "hd=", hd_setup },
 178 #endif
 179 #ifdef CONFIG_CHR_DEV_ST
 180         { "st=", st_setup },
 181 #endif
 182 #ifdef CONFIG_BUSMOUSE
 183         { "bmouse=", bmouse_setup },
 184 #endif
 185 #ifdef CONFIG_SCSI_SEAGATE
 186         { "st0x=", st0x_setup },
 187         { "tmc8xx=", tmc8xx_setup },
 188 #endif
 189 #ifdef CONFIG_SCSI_T128
 190         { "t128=", t128_setup },
 191 #endif
 192 #ifdef CONFIG_SCSI_PAS16
 193         { "pas16=", pas16_setup },
 194 #endif
 195 #ifdef CONFIG_SCSI_GENERIC_NCR5380
 196         { "ncr5380=", generic_NCR5380_setup },
 197 #endif
 198 #ifdef CONFIG_SCSI_AHA152X
 199         { "aha152x=", aha152x_setup},
 200 #endif
 201 #ifdef CONFIG_SCSI_AHA1542
 202         { "aha1542=", aha1542_setup},
 203 #endif
 204 #ifdef CONFIG_SCSI_AIC7XXX
 205         { "aic7xxx=", aic7xxx_setup},
 206 #endif
 207 #ifdef CONFIG_SCSI_BUSLOGIC
 208         { "buslogic=", buslogic_setup},
 209 #endif
 210 #ifdef CONFIG_SCSI_NCR53C406A
 211         { "ncr53c406a=", NCR53c406a_setup},
 212 #endif
 213 #ifdef CONFIG_SCSI_FUTURE_DOMAIN
 214         { "fdomain=", fdomain_setup},
 215 #endif
 216 #ifdef CONFIG_BLK_DEV_XD
 217         { "xd=", xd_setup },
 218 #endif
 219 #ifdef CONFIG_BLK_DEV_FD
 220         { "floppy=", floppy_setup },
 221 #endif
 222 #ifdef CONFIG_CDU31A
 223         { "cdu31a=", cdu31a_setup },
 224 #endif CONFIG_CDU31A
 225 #ifdef CONFIG_MCD
 226         { "mcd=", mcd_setup },
 227 #endif CONFIG_MCD
 228 #ifdef CONFIG_MCDX
 229         { "mcdx=", mcdx_setup },
 230 #endif CONFIG_MCDX
 231 #ifdef CONFIG_SBPCD
 232         { "sbpcd=", sbpcd_setup },
 233 #endif CONFIG_SBPCD
 234 #ifdef CONFIG_AZTCD
 235         { "aztcd=", aztcd_setup },
 236 #endif CONFIG_AZTCD
 237 #ifdef CONFIG_CDU535
 238         { "sonycd535=", sonycd535_setup },
 239 #endif CONFIG_CDU535
 240 #ifdef CONFIG_GSCD
 241         { "gscd=", gscd_setup },
 242 #endif CONFIG_GSCD
 243 #ifdef CONFIG_CM206
 244         { "cm206=", cm206_setup },
 245 #endif CONFIG_CM206
 246 #ifdef CONFIG_OPTCD
 247         { "optcd=", optcd_setup },
 248 #endif CONFIG_OPTCD
 249 #ifdef CONFIG_SJCD
 250         { "sjcd=", sjcd_setup },
 251 #endif CONFIG_SJCD
 252 #ifdef CONFIG_SOUND
 253         { "sound=", sound_setup },
 254 #endif
 255         { 0, 0 }
 256 };
 257 
 258 void ramdisk_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 259 {
 260    if (ints[0] > 0 && ints[1] >= 0)
 261       ramdisk_size = ints[1];
 262 }
 263 
 264 static int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266         int i = 0;
 267         int ints[11];
 268 
 269 #ifdef CONFIG_BLK_DEV_IDE
 270         /* ide driver needs the basic string, rather than pre-processed values */
 271         if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '=')) {
 272                 ide_setup(line);
 273                 return 1;
 274         }
 275 #endif
 276         while (bootsetups[i].str) {
 277                 int n = strlen(bootsetups[i].str);
 278                 if (!strncmp(line,bootsetups[i].str,n)) {
 279                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 280                         return 1;
 281                 }
 282                 i++;
 283         }
 284         return 0;
 285 }
 286 
 287 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
 288    still work even if initally too large, it will just take slightly longer */
 289 unsigned long loops_per_sec = (1<<12);
 290 
 291 /* This is the number of bits of precision for the loops_per_second.  Each
 292    bit takes on average 1.5/HZ seconds.  This (like the original) is a little
 293    better than 1% */
 294 #define LPS_PREC 8
 295 
 296 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 297 {
 298         int ticks;
 299         int loopbit;
 300         int lps_precision = LPS_PREC;
 301 
 302         printk("Calibrating delay loop.. ");
 303         while (loops_per_sec <<= 1) {
 304                 /* wait for "start of" clock tick */
 305                 ticks = jiffies;
 306                 while (ticks == jiffies)
 307                         /* nothing */;
 308                 /* Go .. */
 309                 ticks = jiffies;
 310                 __delay(loops_per_sec);
 311                 ticks = jiffies - ticks;
 312                 if (ticks)
 313                         break;
 314                 }
 315 
 316 /* Do a binary approximation to get loops_per_second set to equal one clock
 317    (up to lps_precision bits) */
 318         loops_per_sec >>= 1;
 319         loopbit = loops_per_sec;
 320         while ( lps_precision-- && (loopbit >>= 1) ) {
 321                 loops_per_sec |= loopbit;
 322                 ticks = jiffies;
 323                 while (ticks == jiffies);
 324                 ticks = jiffies;
 325                 __delay(loops_per_sec);
 326                 if (jiffies != ticks)   /* longer than 1 tick */
 327                         loops_per_sec &= ~loopbit;
 328         }
 329 
 330 /* finally, adjust loops per second in terms of seconds instead of clocks */    
 331         loops_per_sec *= HZ;
 332 /* Round the value and print it */      
 333         printk("ok - %lu.%02lu BogoMIPS\n",
 334                 (loops_per_sec+2500)/500000,
 335                 ((loops_per_sec+2500)/5000) % 100);
 336 }
 337 
 338 /*
 339  * This is a simple kernel command line parsing function: it parses
 340  * the command line, and fills in the arguments/environment to init
 341  * as appropriate. Any cmd-line option is taken to be an environment
 342  * variable if it contains the character '='.
 343  *
 344  *
 345  * This routine also checks for options meant for the kernel.
 346  * These options are not given to init - they are for internal kernel use only.
 347  */
 348 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 349 {
 350         char *next;
 351         static const char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 352         static int devnums[]    = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 353         int args, envs;
 354 
 355         if (!*line)
 356                 return;
 357         args = 0;
 358         envs = 1;       /* TERM is set to 'linux' by default */
 359         next = line;
 360         while ((line = next) != NULL) {
 361                 if ((next = strchr(line,' ')) != NULL)
 362                         *next++ = 0;
 363                 /*
 364                  * check for kernel options first..
 365                  */
 366                 if (!strncmp(line,"root=",5)) {
 367                         int n;
 368                         line += 5;
 369                         if (strncmp(line,"/dev/",5)) {
 370                                 ROOT_DEV = to_kdev_t(
 371                                              simple_strtoul(line,NULL,16));
 372                                 continue;
 373                         }
 374                         line += 5;
 375                         for (n = 0 ; devnames[n] ; n++) {
 376                                 int len = strlen(devnames[n]);
 377                                 if (!strncmp(line,devnames[n],len)) {
 378                                         ROOT_DEV = to_kdev_t(devnums[n]+
 379                                              simple_strtoul(line+len,NULL,0));
 380                                         break;
 381                                 }
 382                         }
 383                         continue;
 384                 }
 385                 if (!strcmp(line,"ro")) {
 386                         root_mountflags |= MS_RDONLY;
 387                         continue;
 388                 }
 389                 if (!strcmp(line,"rw")) {
 390                         root_mountflags &= ~MS_RDONLY;
 391                         continue;
 392                 }
 393                 if (!strcmp(line,"debug")) {
 394                         console_loglevel = 10;
 395                         continue;
 396                 }
 397                 if (checksetup(line))
 398                         continue;
 399                 /*
 400                  * Then check if it's an environment variable or
 401                  * an option.
 402                  */
 403                 if (strchr(line,'=')) {
 404                         if (envs >= MAX_INIT_ENVS)
 405                                 break;
 406                         envp_init[++envs] = line;
 407                 } else {
 408                         if (args >= MAX_INIT_ARGS)
 409                                 break;
 410                         argv_init[++args] = line;
 411                 }
 412         }
 413         argv_init[args+1] = NULL;
 414         envp_init[envs+1] = NULL;
 415 }
 416 
 417 extern void setup_arch(char **, unsigned long *, unsigned long *);
 418 
 419 static char init_stack[PAGE_SIZE];
 420 
 421 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 422 {
 423         char * command_line;
 424 /*
 425  * Interrupts are still disabled. Do necessary setups, then
 426  * enable them
 427  */
 428         setup_arch(&command_line, &memory_start, &memory_end);
 429         memory_start = paging_init(memory_start,memory_end);
 430         trap_init();
 431         init_IRQ();
 432         sched_init();
 433         time_init();
 434         parse_options(command_line);
 435         init_modules();
 436 #ifdef CONFIG_PROFILE
 437         if (!prof_shift)
 438 #ifdef CONFIG_PROFILE_SHIFT
 439                 prof_shift = CONFIG_PROFILE_SHIFT;
 440 #else
 441                 prof_shift = 2;
 442 #endif
 443 #endif
 444         if (prof_shift) {
 445                 prof_buffer = (unsigned long *) memory_start;
 446                 /* only text is profiled */
 447                 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
 448                 prof_len >>= prof_shift;
 449                 memory_start += prof_len * sizeof(unsigned long);
 450         }
 451         memory_start = console_init(memory_start,memory_end);
 452 #ifdef CONFIG_PCI
 453         memory_start = pci_init(memory_start,memory_end);
 454 #endif
 455         memory_start = kmalloc_init(memory_start,memory_end);
 456         sti();
 457         calibrate_delay();
 458         memory_start = chr_dev_init(memory_start,memory_end);
 459         memory_start = blk_dev_init(memory_start,memory_end);
 460         sti();
 461 #ifdef CONFIG_SCSI
 462         memory_start = scsi_dev_init(memory_start,memory_end);
 463 #endif
 464 #ifdef CONFIG_INET
 465         memory_start = net_dev_init(memory_start,memory_end);
 466 #endif
 467         memory_start = inode_init(memory_start,memory_end);
 468         memory_start = file_table_init(memory_start,memory_end);
 469         memory_start = name_cache_init(memory_start,memory_end);
 470         mem_init(memory_start,memory_end);
 471         buffer_init();
 472         sock_init();
 473 #ifdef CONFIG_SYSVIPC
 474         ipc_init();
 475 #endif
 476         sti();
 477         check_bugs();
 478 
 479         printk(linux_banner);
 480 
 481         /* we count on the clone going ok */
 482         if (!clone(CLONE_VM, init_stack+sizeof(init_stack)))
 483                 init();
 484 /*
 485  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 486  * it might do some general things like count free pages or it could be
 487  * used to implement a reasonable LRU algorithm for the paging routines:
 488  * anything that can be useful, but shouldn't take time from the real
 489  * processes.
 490  *
 491  * Right now task[0] just does a infinite idle loop.
 492  */
 493         for(;;)
 494                 idle();
 495 }
 496 
 497 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 498 {
 499         va_list args;
 500         int i;
 501 
 502         va_start(args, fmt);
 503         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 504         va_end(args);
 505         return i;
 506 }
 507 
 508 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 509 {
 510         int pid,i;
 511 
 512         setup();
 513 
 514         #ifdef CONFIG_UMSDOS_FS
 515         {
 516                 /*
 517                         When mounting a umsdos fs as root, we detect
 518                         the pseudo_root (/linux) and initialise it here.
 519                         pseudo_root is defined in fs/umsdos/inode.c
 520                 */
 521                 extern struct inode *pseudo_root;
 522                 if (pseudo_root != NULL){
 523                         current->fs->root = pseudo_root;
 524                         current->fs->pwd  = pseudo_root;
 525                 }
 526         }
 527         #endif
 528 
 529         (void) open("/dev/tty1",O_RDWR,0);
 530         (void) dup(0);
 531         (void) dup(0);
 532 
 533         execve("/etc/init",argv_init,envp_init);
 534         execve("/bin/init",argv_init,envp_init);
 535         execve("/sbin/init",argv_init,envp_init);
 536         /* if this fails, fall through to original stuff */
 537 
 538         if (!(pid=fork())) {
 539                 close(0);
 540                 if (open("/etc/rc",O_RDONLY,0))
 541                         _exit(1);
 542                 execve("/bin/sh",argv_rc,envp_rc);
 543                 _exit(2);
 544         }
 545         if (pid>0)
 546                 while (pid != wait(&i))
 547                         /* nothing */;
 548         while (1) {
 549                 if ((pid = fork()) < 0) {
 550                         printf("Fork failed in init\n\r");
 551                         continue;
 552                 }
 553                 if (!pid) {
 554                         close(0);close(1);close(2);
 555                         setsid();
 556                         (void) open("/dev/tty1",O_RDWR,0);
 557                         (void) dup(0);
 558                         (void) dup(0);
 559                         _exit(execve("/bin/sh",argv,envp));
 560                 }
 561                 while (1)
 562                         if (pid == wait(&i))
 563                                 break;
 564                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 565                 sync();
 566         }
 567         _exit(0);
 568 }

/* [previous][next][first][last][top][bottom][index][help] */