root/init/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. _syscall0
  2. time_init
  3. parse_options
  4. start_kernel
  5. printf
  6. 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/mktime.h>
  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 
  22 extern unsigned long * prof_buffer;
  23 extern unsigned long prof_len;
  24 extern int end;
  25 extern char *linux_banner;
  26 
  27 /*
  28  * we need this inline - forking from kernel space will result
  29  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  30  * is no problem, but for the stack. This is handled by not letting
  31  * main() use the stack at all after fork(). Thus, no function
  32  * calls - which means inline code for fork too, as otherwise we
  33  * would use the stack upon exit from 'fork()'.
  34  *
  35  * Actually only pause and fork are needed inline, so that there
  36  * won't be any messing with the stack from main(), but we define
  37  * some others too.
  38  */
  39 static inline _syscall0(int,idle)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 static inline _syscall0(int,fork)
  41 static inline _syscall0(int,pause)
  42 static inline _syscall1(int,setup,void *,BIOS)
  43 static inline _syscall0(int,sync)
  44 static inline _syscall0(pid_t,setsid)
  45 static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  46 static inline _syscall1(int,dup,int,fd)
  47 static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  48 static inline _syscall3(int,open,const char *,file,int,flag,int,mode)
  49 static inline _syscall1(int,close,int,fd)
  50 static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  51 
  52 static inline pid_t wait(int * wait_stat)
  53 {
  54         return waitpid(-1,wait_stat,0);
  55 }
  56 
  57 static char printbuf[1024];
  58 
  59 extern int vsprintf();
  60 extern void init(void);
  61 extern void init_IRQ(void);
  62 extern long blk_dev_init(long,long);
  63 extern long chr_dev_init(long,long);
  64 extern void floppy_init(void);
  65 extern void sock_init(void);
  66 extern long rd_init(long mem_start, int length);
  67 extern long kernel_mktime(struct mktime * time);
  68 extern unsigned long simple_strtoul(const char *cp,char **endp,unsigned int
  69     base);
  70 
  71 #ifdef CONFIG_SCSI
  72 extern unsigned long scsi_dev_init(unsigned long, unsigned long);
  73 #endif
  74 
  75 /*
  76  * This is set up by the setup-routine at boot-time
  77  */
  78 #define EXT_MEM_K (*(unsigned short *)0x90002)
  79 #define DRIVE_INFO (*(struct drive_info *)0x90080)
  80 #define SCREEN_INFO (*(struct screen_info *)0x90000)
  81 #define ORIG_ROOT_DEV (*(unsigned short *)0x901FC)
  82 #define AUX_DEVICE_INFO (*(unsigned char *)0x901FF)
  83 
  84 /*
  85  * Boot command-line arguments
  86  */
  87 #define MAX_INIT_ARGS 8
  88 #define MAX_INIT_ENVS 8
  89 #define CL_MAGIC_ADDR (*(unsigned short *) 0x90020)
  90 #define CL_MAGIC 0xa33f
  91 #define CL_BASE_ADDR ((char *) 0x90000)
  92 #define CL_OFFSET (*(unsigned short *) 0x90022)
  93 
  94 /*
  95  * Yeah, yeah, it's ugly, but I cannot find how to do this correctly
  96  * and this seems to work. I anybody has more info on the real-time
  97  * clock I'd be interested. Most of this was trial and error, and some
  98  * bios-listing reading. Urghh.
  99  */
 100 
 101 #define CMOS_READ(addr) ({ \
 102 outb_p(0x80|addr,0x70); \
 103 inb_p(0x71); \
 104 })
 105 
 106 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
 107 
 108 static void time_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 109 {
 110         struct mktime time;
 111 
 112         do {
 113                 time.sec = CMOS_READ(0);
 114                 time.min = CMOS_READ(2);
 115                 time.hour = CMOS_READ(4);
 116                 time.day = CMOS_READ(7);
 117                 time.mon = CMOS_READ(8);
 118                 time.year = CMOS_READ(9);
 119         } while (time.sec != CMOS_READ(0));
 120         BCD_TO_BIN(time.sec);
 121         BCD_TO_BIN(time.min);
 122         BCD_TO_BIN(time.hour);
 123         BCD_TO_BIN(time.day);
 124         BCD_TO_BIN(time.mon);
 125         BCD_TO_BIN(time.year);
 126         time.mon--;
 127         startup_time = kernel_mktime(&time);
 128 }
 129 
 130 static unsigned long memory_start = 0; /* After mem_init, stores the */
 131                                        /* amount of free user memory */
 132 static unsigned long memory_end = 0;
 133 static unsigned long low_memory_start = 0;
 134 
 135 static char * argv_init[MAX_INIT_ARGS+2] = { "/bin/init", NULL, };
 136 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=console", NULL, };
 137 
 138 static char * argv_rc[] = { "/bin/sh", NULL };
 139 static char * envp_rc[] = { "HOME=/", "TERM=console", NULL };
 140 
 141 static char * argv[] = { "-/bin/sh",NULL };
 142 static char * envp[] = { "HOME=/usr/root", "TERM=console", NULL };
 143 
 144 struct drive_info { char dummy[32]; } drive_info;
 145 struct screen_info screen_info;
 146 
 147 unsigned char aux_device_present;
 148 
 149 static char command_line[80] = { 0, };
 150 
 151 /*
 152  * This is a simple kernel command line parsing function: it parses
 153  * the command line, and fills in the arguments/environment to init
 154  * as appropriate. Any cmd-line option is taken to be an environment
 155  * variable if it contains the character '='.
 156  *
 157  *
 158  * This routine also checks for options meant for the kernel - currently
 159  * only the "root=XXXX" option is recognized. These options are not given
 160  * to init - they are for internal kernel use only.
 161  */
 162 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164         char *next;
 165         int args, envs;
 166 
 167         if (!*line)
 168                 return;
 169         args = 0;
 170         envs = 1;       /* TERM is set to 'console' by default */
 171         next = line;
 172         while (line = next) {
 173                 if (next = strchr(line,' '))
 174                         *next++ = 0;
 175                 /*
 176                  * check for kernel options first..
 177                  */
 178                 if (!strncmp(line,"root=",5)) {
 179                         ROOT_DEV = simple_strtoul(line+5,NULL,16);
 180                         continue;
 181                 }
 182                 /*
 183                  * Then check if it's an environment variable or
 184                  * an option.
 185                  */     
 186                 if (strchr(line,'=')) {
 187                         if (envs >= MAX_INIT_ENVS)
 188                                 break;
 189                         envp_init[++envs] = line;
 190                 } else {
 191                         if (args >= MAX_INIT_ARGS)
 192                                 break;
 193                         argv_init[++args] = line;
 194                 }
 195         }
 196         argv_init[args+1] = NULL;
 197         envp_init[envs+1] = NULL;
 198 }
 199 
 200 void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 201 {
 202 /*
 203  * Interrupts are still disabled. Do necessary setups, then
 204  * enable them
 205  */
 206         ROOT_DEV = ORIG_ROOT_DEV;
 207         drive_info = DRIVE_INFO;
 208         screen_info = SCREEN_INFO;
 209         aux_device_present = AUX_DEVICE_INFO;
 210         memory_end = (1<<20) + (EXT_MEM_K<<10);
 211         memory_end &= 0xfffff000;
 212 #ifdef MAX_16M
 213         if (memory_end > 16*1024*1024)
 214                 memory_end = 16*1024*1024;
 215 #endif
 216         memory_start = 1024*1024;
 217         low_memory_start = (unsigned long) &end;
 218         low_memory_start += 0xfff;
 219         low_memory_start &= 0xfffff000;
 220         memory_start = paging_init(memory_start,memory_end);
 221         if (CL_MAGIC_ADDR == CL_MAGIC)
 222                 strcpy(command_line,CL_BASE_ADDR+CL_OFFSET);
 223         trap_init();
 224         init_IRQ();
 225         sched_init();
 226         parse_options(command_line);
 227 #ifdef PROFILE_SHIFT
 228         prof_buffer = (unsigned long *) memory_start;
 229         prof_len = (unsigned long) &end;
 230         prof_len >>= PROFILE_SHIFT;
 231         memory_start += prof_len * sizeof(unsigned long);
 232 #endif
 233         memory_start = chr_dev_init(memory_start,memory_end);
 234         memory_start = blk_dev_init(memory_start,memory_end);
 235 #ifdef CONFIG_SCSI
 236         memory_start = scsi_dev_init(memory_start,memory_end);
 237 #endif
 238         mem_init(low_memory_start,memory_start,memory_end);
 239         buffer_init();
 240         inode_init();
 241         time_init();
 242         floppy_init();
 243         sock_init();
 244         sti();
 245         move_to_user_mode();
 246         if (!fork())            /* we count on this going ok */
 247                 init();
 248 /*
 249  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 250  * it might do some general things like count free pages or it could be
 251  * used to implement a reasonable LRU algorithm for the paging routines:
 252  * anything that can be useful, but shouldn't take time from the real
 253  * processes.
 254  *
 255  * Right now task[0] just does a infinite idle loop.
 256  */
 257         for(;;)
 258                 idle();
 259 }
 260 
 261 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263         va_list args;
 264         int i;
 265 
 266         va_start(args, fmt);
 267         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 268         va_end(args);
 269         return i;
 270 }
 271 
 272 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274         int pid,i;
 275 
 276         setup((void *) &drive_info);
 277         (void) open("/dev/tty1",O_RDWR,0);
 278         (void) dup(0);
 279         (void) dup(0);
 280 
 281         printf(linux_banner);
 282         execve("/etc/init",argv_init,envp_init);
 283         execve("/bin/init",argv_init,envp_init);
 284         /* if this fails, fall through to original stuff */
 285 
 286         if (!(pid=fork())) {
 287                 close(0);
 288                 if (open("/etc/rc",O_RDONLY,0))
 289                         _exit(1);
 290                 execve("/bin/sh",argv_rc,envp_rc);
 291                 _exit(2);
 292         }
 293         if (pid>0)
 294                 while (pid != wait(&i))
 295                         /* nothing */;
 296         while (1) {
 297                 if ((pid=fork())<0) {
 298                         printf("Fork failed in init\r\n");
 299                         continue;
 300                 }
 301                 if (!pid) {
 302                         close(0);close(1);close(2);
 303                         setsid();
 304                         (void) open("/dev/tty1",O_RDWR,0);
 305                         (void) dup(0);
 306                         (void) dup(0);
 307                         _exit(execve("/bin/sh",argv,envp));
 308                 }
 309                 while (1)
 310                         if (pid == wait(&i))
 311                                 break;
 312                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 313                 sync();
 314         }
 315         _exit(0);       /* NOTE! _exit, not exit() */
 316 }

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