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

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