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 unsigned long loops_per_sec = 1;
 280 
 281 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283         int ticks;
 284 
 285         printk("Calibrating delay loop.. ");
 286         while (loops_per_sec <<= 1) {
 287                 /* wait for "start of" clock tick */
 288                 ticks = jiffies;
 289                 while (ticks == jiffies)
 290                         /* nothing */;
 291                 /* Go .. */
 292                 ticks = jiffies;
 293                 __delay(loops_per_sec);
 294                 ticks = jiffies - ticks;
 295                 if (ticks >= HZ) {
 296                         loops_per_sec = muldiv(loops_per_sec, HZ, ticks);
 297                         printk("ok - %lu.%02lu BogoMIPS\n",
 298                                 loops_per_sec/500000,
 299                                 (loops_per_sec/5000) % 100);
 300                         return;
 301                 }
 302         }
 303         printk("failed\n");
 304 }
 305 
 306 
 307 /*
 308  * This is a simple kernel command line parsing function: it parses
 309  * the command line, and fills in the arguments/environment to init
 310  * as appropriate. Any cmd-line option is taken to be an environment
 311  * variable if it contains the character '='.
 312  *
 313  *
 314  * This routine also checks for options meant for the kernel.
 315  * These options are not given to init - they are for internal kernel use only.
 316  */
 317 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 318 {
 319         char *next;
 320         static const char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 321         static int devnums[]    = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 322         int args, envs;
 323 
 324         if (!*line)
 325                 return;
 326         args = 0;
 327         envs = 1;       /* TERM is set to 'linux' by default */
 328         next = line;
 329         while ((line = next) != NULL) {
 330                 if ((next = strchr(line,' ')) != NULL)
 331                         *next++ = 0;
 332                 /*
 333                  * check for kernel options first..
 334                  */
 335                 if (!strncmp(line,"root=",5)) {
 336                         int n;
 337                         line += 5;
 338                         if (strncmp(line,"/dev/",5)) {
 339                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 340                                 continue;
 341                         }
 342                         line += 5;
 343                         for (n = 0 ; devnames[n] ; n++) {
 344                                 int len = strlen(devnames[n]);
 345                                 if (!strncmp(line,devnames[n],len)) {
 346                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,0);
 347                                         break;
 348                                 }
 349                         }
 350                         continue;
 351                 }
 352                 if (!strcmp(line,"ro")) {
 353                         root_mountflags |= MS_RDONLY;
 354                         continue;
 355                 }
 356                 if (!strcmp(line,"rw")) {
 357                         root_mountflags &= ~MS_RDONLY;
 358                         continue;
 359                 }
 360                 if (!strcmp(line,"debug")) {
 361                         console_loglevel = 10;
 362                         continue;
 363                 }
 364                 if (checksetup(line))
 365                         continue;
 366                 /*
 367                  * Then check if it's an environment variable or
 368                  * an option.
 369                  */
 370                 if (strchr(line,'=')) {
 371                         if (envs >= MAX_INIT_ENVS)
 372                                 break;
 373                         envp_init[++envs] = line;
 374                 } else {
 375                         if (args >= MAX_INIT_ARGS)
 376                                 break;
 377                         argv_init[++args] = line;
 378                 }
 379         }
 380         argv_init[args+1] = NULL;
 381         envp_init[envs+1] = NULL;
 382 }
 383 
 384 extern void setup_arch(char **, unsigned long *, unsigned long *);
 385 
 386 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 387 {
 388         char * command_line;
 389 /*
 390  * Interrupts are still disabled. Do necessary setups, then
 391  * enable them
 392  */
 393         setup_arch(&command_line, &memory_start, &memory_end);
 394         memory_start = paging_init(memory_start,memory_end);
 395         trap_init();
 396         init_IRQ();
 397         sched_init();
 398         parse_options(command_line);
 399         init_modules();
 400 #ifdef CONFIG_PROFILE
 401         if (!prof_shift)
 402 #ifdef CONFIG_PROFILE_SHIFT
 403                 prof_shift = CONFIG_PROFILE_SHIFT;
 404 #else
 405                 prof_shift = 2;
 406 #endif
 407 #endif
 408         if (prof_shift) {
 409                 prof_buffer = (unsigned long *) memory_start;
 410                 /* only text is profiled */
 411                 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
 412                 prof_len >>= prof_shift;
 413                 memory_start += prof_len * sizeof(unsigned long);
 414         }
 415         memory_start = console_init(memory_start,memory_end);
 416 #ifdef CONFIG_PCI
 417         memory_start = pci_init(memory_start,memory_end);
 418 #endif
 419         memory_start = kmalloc_init(memory_start,memory_end);
 420         sti();
 421         calibrate_delay();
 422         memory_start = chr_dev_init(memory_start,memory_end);
 423         memory_start = blk_dev_init(memory_start,memory_end);
 424         sti();
 425 #ifdef CONFIG_SCSI
 426         memory_start = scsi_dev_init(memory_start,memory_end);
 427 #endif
 428 #ifdef CONFIG_INET
 429         memory_start = net_dev_init(memory_start,memory_end);
 430 #endif
 431         memory_start = inode_init(memory_start,memory_end);
 432         memory_start = file_table_init(memory_start,memory_end);
 433         memory_start = name_cache_init(memory_start,memory_end);
 434         mem_init(memory_start,memory_end);
 435         buffer_init();
 436         time_init();
 437         sock_init();
 438 #ifdef CONFIG_SYSVIPC
 439         ipc_init();
 440 #endif
 441         sti();
 442         check_bugs();
 443 
 444         printk(linux_banner);
 445 
 446         if (!fork())            /* we count on this going ok */
 447                 init();
 448 /*
 449  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 450  * it might do some general things like count free pages or it could be
 451  * used to implement a reasonable LRU algorithm for the paging routines:
 452  * anything that can be useful, but shouldn't take time from the real
 453  * processes.
 454  *
 455  * Right now task[0] just does a infinite idle loop.
 456  */
 457         for(;;)
 458                 idle();
 459 }
 460 
 461 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 462 {
 463         va_list args;
 464         int i;
 465 
 466         va_start(args, fmt);
 467         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 468         va_end(args);
 469         return i;
 470 }
 471 
 472 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 473 {
 474         int pid,i;
 475 
 476         setup();
 477 
 478         #ifdef CONFIG_UMSDOS_FS
 479         {
 480                 /*
 481                         When mounting a umsdos fs as root, we detect
 482                         the pseudo_root (/linux) and initialise it here.
 483                         pseudo_root is defined in fs/umsdos/inode.c
 484                 */
 485                 extern struct inode *pseudo_root;
 486                 if (pseudo_root != NULL){
 487                         current->fs->root = pseudo_root;
 488                         current->fs->pwd  = pseudo_root;
 489                 }
 490         }
 491         #endif
 492 
 493         (void) open("/dev/tty1",O_RDWR,0);
 494         (void) dup(0);
 495         (void) dup(0);
 496 
 497         execve("/etc/init",argv_init,envp_init);
 498         execve("/bin/init",argv_init,envp_init);
 499         execve("/sbin/init",argv_init,envp_init);
 500         /* if this fails, fall through to original stuff */
 501 
 502         if (!(pid=fork())) {
 503                 close(0);
 504                 if (open("/etc/rc",O_RDONLY,0))
 505                         _exit(1);
 506                 execve("/bin/sh",argv_rc,envp_rc);
 507                 _exit(2);
 508         }
 509         if (pid>0)
 510                 while (pid != wait(&i))
 511                         /* nothing */;
 512         while (1) {
 513                 if ((pid = fork()) < 0) {
 514                         printf("Fork failed in init\n\r");
 515                         continue;
 516                 }
 517                 if (!pid) {
 518                         close(0);close(1);close(2);
 519                         setsid();
 520                         (void) open("/dev/tty1",O_RDWR,0);
 521                         (void) dup(0);
 522                         (void) dup(0);
 523                         _exit(execve("/bin/sh",argv,envp));
 524                 }
 525                 while (1)
 526                         if (pid == wait(&i))
 527                                 break;
 528                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 529                 sync();
 530         }
 531         _exit(0);
 532 }

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