root/init/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_options
  2. ramdisk_setup
  3. checksetup
  4. calibrate_delay
  5. parse_options
  6. start_kernel
  7. printf
  8. 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 unsigned long * prof_buffer;
  33 extern unsigned long prof_len;
  34 extern char _stext, _etext;
  35 extern char *linux_banner;
  36 
  37 static char printbuf[1024];
  38 
  39 extern int console_loglevel;
  40 
  41 extern void init(void);
  42 extern void init_IRQ(void);
  43 extern void init_modules(void);
  44 extern long console_init(long, long);
  45 extern long kmalloc_init(long,long);
  46 extern long blk_dev_init(long,long);
  47 extern long chr_dev_init(long,long);
  48 extern void sock_init(void);
  49 extern long rd_init(long mem_start, int length);
  50 unsigned long net_dev_init(unsigned long, unsigned long);
  51 extern long pci_init(long, long);
  52 
  53 extern void bmouse_setup(char *str, int *ints);
  54 extern void eth_setup(char *str, int *ints);
  55 extern void xd_setup(char *str, int *ints);
  56 extern void floppy_setup(char *str, int *ints);
  57 extern void st_setup(char *str, int *ints);
  58 extern void st0x_setup(char *str, int *ints);
  59 extern void tmc8xx_setup(char *str, int *ints);
  60 extern void t128_setup(char *str, int *ints);
  61 extern void pas16_setup(char *str, int *ints);
  62 extern void generic_NCR5380_setup(char *str, int *intr);
  63 extern void aha152x_setup(char *str, int *ints);
  64 extern void aha1542_setup(char *str, int *ints);
  65 extern void aic7xxx_setup(char *str, int *ints);
  66 extern void buslogic_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 struct {
 148         char *str;
 149         void (*setup_func)(char *, int *);
 150 } bootsetups[] = {
 151         { "reserve=", reserve_setup },
 152         { "ramdisk=", ramdisk_setup },
 153 #ifdef CONFIG_BUGi386
 154         { "no-hlt", no_halt },
 155         { "no387", no_387 },
 156 #endif
 157 #ifdef CONFIG_INET
 158         { "ether=", eth_setup },
 159 #endif
 160 #ifdef CONFIG_SCSI
 161         { "max_scsi_luns=", scsi_luns_setup },
 162 #endif
 163 #ifdef CONFIG_BLK_DEV_IDE
 164         { "hda=", hda_setup },
 165         { "hdb=", hdb_setup },
 166         { "hdc=", hdc_setup },
 167         { "hdd=", hdd_setup },
 168         { "hd=",  ide_setup },
 169 #elif defined(CONFIG_BLK_DEV_HD)
 170         { "hd=", hd_setup },
 171 #endif
 172 #ifdef CONFIG_CHR_DEV_ST
 173         { "st=", st_setup },
 174 #endif
 175 #ifdef CONFIG_BUSMOUSE
 176         { "bmouse=", bmouse_setup },
 177 #endif
 178 #ifdef CONFIG_SCSI_SEAGATE
 179         { "st0x=", st0x_setup },
 180         { "tmc8xx=", tmc8xx_setup },
 181 #endif
 182 #ifdef CONFIG_SCSI_T128
 183         { "t128=", t128_setup },
 184 #endif
 185 #ifdef CONFIG_SCSI_PAS16
 186         { "pas16=", pas16_setup },
 187 #endif
 188 #ifdef CONFIG_SCSI_GENERIC_NCR5380
 189         { "ncr5380=", generic_NCR5380_setup },
 190 #endif
 191 #ifdef CONFIG_SCSI_AHA152X
 192         { "aha152x=", aha152x_setup},
 193 #endif
 194 #ifdef CONFIG_SCSI_AHA1542
 195         { "aha1542=", aha1542_setup},
 196 #endif
 197 #ifdef CONFIG_SCSI_AIC7XXX
 198         { "aic7xxx=", aic7xxx_setup},
 199 #endif
 200 #ifdef CONFIG_SCSI_BUSLOGIC
 201         { "buslogic=", buslogic_setup},
 202 #endif
 203 #ifdef CONFIG_BLK_DEV_XD
 204         { "xd=", xd_setup },
 205 #endif
 206 #ifdef CONFIG_BLK_DEV_FD
 207         { "floppy=", floppy_setup },
 208 #endif
 209 #ifdef CONFIG_CDU31A
 210         { "cdu31a=", cdu31a_setup },
 211 #endif CONFIG_CDU31A
 212 #ifdef CONFIG_MCD
 213         { "mcd=", mcd_setup },
 214 #endif CONFIG_MCD
 215 #ifdef CONFIG_MCDX
 216         { "mcdx=", mcdx_setup },
 217 #endif CONFIG_MCDX
 218 #ifdef CONFIG_SBPCD
 219         { "sbpcd=", sbpcd_setup },
 220 #endif CONFIG_SBPCD
 221 #ifdef CONFIG_AZTCD
 222         { "aztcd=", aztcd_setup },
 223 #endif CONFIG_AZTCD
 224 #ifdef CONFIG_CDU535
 225         { "sonycd535=", sonycd535_setup },
 226 #endif CONFIG_CDU535
 227 #ifdef CONFIG_GSCD
 228         { "gscd=", gscd_setup },
 229 #endif CONFIG_GSCD
 230 #ifdef CONFIG_CM206
 231         { "cm206=", cm206_setup },
 232 #endif CONFIG_CM206
 233 #ifdef CONFIG_OPTCD
 234         { "optcd=", optcd_setup },
 235 #endif CONFIG_OPTCD
 236 #ifdef CONFIG_SJCD
 237         { "sjcd=", sjcd_setup },
 238 #endif CONFIG_SJCD
 239 #ifdef CONFIG_SOUND
 240         { "sound=", sound_setup },
 241 #endif
 242         { 0, 0 }
 243 };
 244 
 245 void ramdisk_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 246 {
 247    if (ints[0] > 0 && ints[1] >= 0)
 248       ramdisk_size = ints[1];
 249 }
 250 
 251 static int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253         int i = 0;
 254         int ints[11];
 255 
 256         while (bootsetups[i].str) {
 257                 int n = strlen(bootsetups[i].str);
 258                 if (!strncmp(line,bootsetups[i].str,n)) {
 259                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 260                         return 1;
 261                 }
 262                 i++;
 263         }
 264         return 0;
 265 }
 266 
 267 unsigned long loops_per_sec = 1;
 268 
 269 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 270 {
 271         int ticks;
 272 
 273         printk("Calibrating delay loop.. ");
 274         while (loops_per_sec <<= 1) {
 275                 /* wait for "start of" clock tick */
 276                 ticks = jiffies;
 277                 while (ticks == jiffies)
 278                         /* nothing */;
 279                 /* Go .. */
 280                 ticks = jiffies;
 281                 __delay(loops_per_sec);
 282                 ticks = jiffies - ticks;
 283                 if (ticks >= HZ) {
 284                         loops_per_sec = muldiv(loops_per_sec, HZ, ticks);
 285                         printk("ok - %lu.%02lu BogoMips\n",
 286                                 loops_per_sec/500000,
 287                                 (loops_per_sec/5000) % 100);
 288                         return;
 289                 }
 290         }
 291         printk("failed\n");
 292 }
 293 
 294 
 295 /*
 296  * This is a simple kernel command line parsing function: it parses
 297  * the command line, and fills in the arguments/environment to init
 298  * as appropriate. Any cmd-line option is taken to be an environment
 299  * variable if it contains the character '='.
 300  *
 301  *
 302  * This routine also checks for options meant for the kernel.
 303  * These options are not given to init - they are for internal kernel use only.
 304  */
 305 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307         char *next;
 308         static char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 309         static int devnums[]    = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 310         int args, envs;
 311 
 312         if (!*line)
 313                 return;
 314         args = 0;
 315         envs = 1;       /* TERM is set to 'linux' by default */
 316         next = line;
 317         while ((line = next) != NULL) {
 318                 if ((next = strchr(line,' ')) != NULL)
 319                         *next++ = 0;
 320                 /*
 321                  * check for kernel options first..
 322                  */
 323                 if (!strncmp(line,"root=",5)) {
 324                         int n;
 325                         line += 5;
 326                         if (strncmp(line,"/dev/",5)) {
 327                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 328                                 continue;
 329                         }
 330                         line += 5;
 331                         for (n = 0 ; devnames[n] ; n++) {
 332                                 int len = strlen(devnames[n]);
 333                                 if (!strncmp(line,devnames[n],len)) {
 334                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,0);
 335                                         break;
 336                                 }
 337                         }
 338                         continue;
 339                 }
 340                 if (!strcmp(line,"ro")) {
 341                         root_mountflags |= MS_RDONLY;
 342                         continue;
 343                 }
 344                 if (!strcmp(line,"rw")) {
 345                         root_mountflags &= ~MS_RDONLY;
 346                         continue;
 347                 }
 348                 if (!strcmp(line,"debug")) {
 349                         console_loglevel = 10;
 350                         continue;
 351                 }
 352                 if (checksetup(line))
 353                         continue;
 354                 /*
 355                  * Then check if it's an environment variable or
 356                  * an option.
 357                  */
 358                 if (strchr(line,'=')) {
 359                         if (envs >= MAX_INIT_ENVS)
 360                                 break;
 361                         envp_init[++envs] = line;
 362                 } else {
 363                         if (args >= MAX_INIT_ARGS)
 364                                 break;
 365                         argv_init[++args] = line;
 366                 }
 367         }
 368         argv_init[args+1] = NULL;
 369         envp_init[envs+1] = NULL;
 370 }
 371 
 372 extern void check_bugs(void);
 373 extern void setup_arch(char **, unsigned long *, unsigned long *);
 374 
 375 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377         char * command_line;
 378 /*
 379  * Interrupts are still disabled. Do necessary setups, then
 380  * enable them
 381  */
 382         setup_arch(&command_line, &memory_start, &memory_end);
 383         memory_start = paging_init(memory_start,memory_end);
 384         trap_init();
 385         init_IRQ();
 386         sched_init();
 387         parse_options(command_line);
 388         init_modules();
 389 #ifdef CONFIG_PROFILE
 390         prof_buffer = (unsigned long *) memory_start;
 391         /* only text is profiled */
 392         prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
 393         prof_len >>= CONFIG_PROFILE_SHIFT;
 394         memory_start += prof_len * sizeof(unsigned long);
 395 #endif
 396         memory_start = console_init(memory_start,memory_end);
 397 #ifdef CONFIG_PCI
 398         memory_start = pci_init(memory_start,memory_end);
 399 #endif
 400         memory_start = kmalloc_init(memory_start,memory_end);
 401         sti();
 402         calibrate_delay();
 403         memory_start = chr_dev_init(memory_start,memory_end);
 404         memory_start = blk_dev_init(memory_start,memory_end);
 405         sti();
 406 #ifdef CONFIG_SCSI
 407         memory_start = scsi_dev_init(memory_start,memory_end);
 408 #endif
 409 #ifdef CONFIG_INET
 410         memory_start = net_dev_init(memory_start,memory_end);
 411 #endif
 412         memory_start = inode_init(memory_start,memory_end);
 413         memory_start = file_table_init(memory_start,memory_end);
 414         memory_start = name_cache_init(memory_start,memory_end);
 415         mem_init(memory_start,memory_end);
 416         buffer_init();
 417         time_init();
 418         sock_init();
 419 #ifdef CONFIG_SYSVIPC
 420         ipc_init();
 421 #endif
 422         sti();
 423         check_bugs();
 424 
 425         printk(linux_banner);
 426 
 427         if (!fork())            /* we count on this going ok */
 428                 init();
 429 /*
 430  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 431  * it might do some general things like count free pages or it could be
 432  * used to implement a reasonable LRU algorithm for the paging routines:
 433  * anything that can be useful, but shouldn't take time from the real
 434  * processes.
 435  *
 436  * Right now task[0] just does a infinite idle loop.
 437  */
 438         for(;;)
 439                 idle();
 440 }
 441 
 442 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 443 {
 444         va_list args;
 445         int i;
 446 
 447         va_start(args, fmt);
 448         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 449         va_end(args);
 450         return i;
 451 }
 452 
 453 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 454 {
 455         int pid,i;
 456 
 457         setup();
 458 
 459         #ifdef CONFIG_UMSDOS_FS
 460         {
 461                 /*
 462                         When mounting a umsdos fs as root, we detect
 463                         the pseudo_root (/linux) and initialise it here.
 464                         pseudo_root is defined in fs/umsdos/inode.c
 465                 */
 466                 extern struct inode *pseudo_root;
 467                 if (pseudo_root != NULL){
 468                         current->fs->root = pseudo_root;
 469                         current->fs->pwd  = pseudo_root;
 470                 }
 471         }
 472         #endif
 473 
 474         (void) open("/dev/tty1",O_RDWR,0);
 475         (void) dup(0);
 476         (void) dup(0);
 477 
 478         execve("/etc/init",argv_init,envp_init);
 479         execve("/bin/init",argv_init,envp_init);
 480         execve("/sbin/init",argv_init,envp_init);
 481         /* if this fails, fall through to original stuff */
 482 
 483         if (!(pid=fork())) {
 484                 close(0);
 485                 if (open("/etc/rc",O_RDONLY,0))
 486                         _exit(1);
 487                 execve("/bin/sh",argv_rc,envp_rc);
 488                 _exit(2);
 489         }
 490         if (pid>0)
 491                 while (pid != wait(&i))
 492                         /* nothing */;
 493         while (1) {
 494                 if ((pid = fork()) < 0) {
 495                         printf("Fork failed in init\n\r");
 496                         continue;
 497                 }
 498                 if (!pid) {
 499                         close(0);close(1);close(2);
 500                         setsid();
 501                         (void) open("/dev/tty1",O_RDWR,0);
 502                         (void) dup(0);
 503                         (void) dup(0);
 504                         _exit(execve("/bin/sh",argv,envp));
 505                 }
 506                 while (1)
 507                         if (pid == wait(&i))
 508                                 break;
 509                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 510                 sync();
 511         }
 512         _exit(0);
 513 }

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