root/init/main.c

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

DEFINITIONS

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

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