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

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