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

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