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

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