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

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