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         const 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 #if defined(CONFIG_BLK_DEV_HD)
 164         { "hd=", hd_setup },
 165 #endif
 166 #ifdef CONFIG_CHR_DEV_ST
 167         { "st=", st_setup },
 168 #endif
 169 #ifdef CONFIG_BUSMOUSE
 170         { "bmouse=", bmouse_setup },
 171 #endif
 172 #ifdef CONFIG_SCSI_SEAGATE
 173         { "st0x=", st0x_setup },
 174         { "tmc8xx=", tmc8xx_setup },
 175 #endif
 176 #ifdef CONFIG_SCSI_T128
 177         { "t128=", t128_setup },
 178 #endif
 179 #ifdef CONFIG_SCSI_PAS16
 180         { "pas16=", pas16_setup },
 181 #endif
 182 #ifdef CONFIG_SCSI_GENERIC_NCR5380
 183         { "ncr5380=", generic_NCR5380_setup },
 184 #endif
 185 #ifdef CONFIG_SCSI_AHA152X
 186         { "aha152x=", aha152x_setup},
 187 #endif
 188 #ifdef CONFIG_SCSI_AHA1542
 189         { "aha1542=", aha1542_setup},
 190 #endif
 191 #ifdef CONFIG_SCSI_AIC7XXX
 192         { "aic7xxx=", aic7xxx_setup},
 193 #endif
 194 #ifdef CONFIG_SCSI_BUSLOGIC
 195         { "buslogic=", buslogic_setup},
 196 #endif
 197 #ifdef CONFIG_BLK_DEV_XD
 198         { "xd=", xd_setup },
 199 #endif
 200 #ifdef CONFIG_BLK_DEV_FD
 201         { "floppy=", floppy_setup },
 202 #endif
 203 #ifdef CONFIG_CDU31A
 204         { "cdu31a=", cdu31a_setup },
 205 #endif CONFIG_CDU31A
 206 #ifdef CONFIG_MCD
 207         { "mcd=", mcd_setup },
 208 #endif CONFIG_MCD
 209 #ifdef CONFIG_MCDX
 210         { "mcdx=", mcdx_setup },
 211 #endif CONFIG_MCDX
 212 #ifdef CONFIG_SBPCD
 213         { "sbpcd=", sbpcd_setup },
 214 #endif CONFIG_SBPCD
 215 #ifdef CONFIG_AZTCD
 216         { "aztcd=", aztcd_setup },
 217 #endif CONFIG_AZTCD
 218 #ifdef CONFIG_CDU535
 219         { "sonycd535=", sonycd535_setup },
 220 #endif CONFIG_CDU535
 221 #ifdef CONFIG_GSCD
 222         { "gscd=", gscd_setup },
 223 #endif CONFIG_GSCD
 224 #ifdef CONFIG_CM206
 225         { "cm206=", cm206_setup },
 226 #endif CONFIG_CM206
 227 #ifdef CONFIG_OPTCD
 228         { "optcd=", optcd_setup },
 229 #endif CONFIG_OPTCD
 230 #ifdef CONFIG_SJCD
 231         { "sjcd=", sjcd_setup },
 232 #endif CONFIG_SJCD
 233 #ifdef CONFIG_SOUND
 234         { "sound=", sound_setup },
 235 #endif
 236         { 0, 0 }
 237 };
 238 
 239 void ramdisk_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241    if (ints[0] > 0 && ints[1] >= 0)
 242       ramdisk_size = ints[1];
 243 }
 244 
 245 static int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 246 {
 247         int i = 0;
 248         int ints[11];
 249 
 250 #ifdef CONFIG_BLK_DEV_IDE
 251         /* ide driver needs the basic string, rather than pre-processed values */
 252         if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '=')) {
 253                 ide_setup(line);
 254                 return 1;
 255         }
 256 #endif
 257         while (bootsetups[i].str) {
 258                 int n = strlen(bootsetups[i].str);
 259                 if (!strncmp(line,bootsetups[i].str,n)) {
 260                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 261                         return 1;
 262                 }
 263                 i++;
 264         }
 265         return 0;
 266 }
 267 
 268 unsigned long loops_per_sec = 1;
 269 
 270 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272         int ticks;
 273 
 274         printk("Calibrating delay loop.. ");
 275         while (loops_per_sec <<= 1) {
 276                 /* wait for "start of" clock tick */
 277                 ticks = jiffies;
 278                 while (ticks == jiffies)
 279                         /* nothing */;
 280                 /* Go .. */
 281                 ticks = jiffies;
 282                 __delay(loops_per_sec);
 283                 ticks = jiffies - ticks;
 284                 if (ticks >= HZ) {
 285                         loops_per_sec = muldiv(loops_per_sec, HZ, ticks);
 286                         printk("ok - %lu.%02lu BogoMips\n",
 287                                 loops_per_sec/500000,
 288                                 (loops_per_sec/5000) % 100);
 289                         return;
 290                 }
 291         }
 292         printk("failed\n");
 293 }
 294 
 295 
 296 /*
 297  * This is a simple kernel command line parsing function: it parses
 298  * the command line, and fills in the arguments/environment to init
 299  * as appropriate. Any cmd-line option is taken to be an environment
 300  * variable if it contains the character '='.
 301  *
 302  *
 303  * This routine also checks for options meant for the kernel.
 304  * These options are not given to init - they are for internal kernel use only.
 305  */
 306 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308         char *next;
 309         static const char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 310         static int devnums[]    = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 311         int args, envs;
 312 
 313         if (!*line)
 314                 return;
 315         args = 0;
 316         envs = 1;       /* TERM is set to 'linux' by default */
 317         next = line;
 318         while ((line = next) != NULL) {
 319                 if ((next = strchr(line,' ')) != NULL)
 320                         *next++ = 0;
 321                 /*
 322                  * check for kernel options first..
 323                  */
 324                 if (!strncmp(line,"root=",5)) {
 325                         int n;
 326                         line += 5;
 327                         if (strncmp(line,"/dev/",5)) {
 328                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 329                                 continue;
 330                         }
 331                         line += 5;
 332                         for (n = 0 ; devnames[n] ; n++) {
 333                                 int len = strlen(devnames[n]);
 334                                 if (!strncmp(line,devnames[n],len)) {
 335                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,0);
 336                                         break;
 337                                 }
 338                         }
 339                         continue;
 340                 }
 341                 if (!strcmp(line,"ro")) {
 342                         root_mountflags |= MS_RDONLY;
 343                         continue;
 344                 }
 345                 if (!strcmp(line,"rw")) {
 346                         root_mountflags &= ~MS_RDONLY;
 347                         continue;
 348                 }
 349                 if (!strcmp(line,"debug")) {
 350                         console_loglevel = 10;
 351                         continue;
 352                 }
 353                 if (checksetup(line))
 354                         continue;
 355                 /*
 356                  * Then check if it's an environment variable or
 357                  * an option.
 358                  */
 359                 if (strchr(line,'=')) {
 360                         if (envs >= MAX_INIT_ENVS)
 361                                 break;
 362                         envp_init[++envs] = line;
 363                 } else {
 364                         if (args >= MAX_INIT_ARGS)
 365                                 break;
 366                         argv_init[++args] = line;
 367                 }
 368         }
 369         argv_init[args+1] = NULL;
 370         envp_init[envs+1] = NULL;
 371 }
 372 
 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] */