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. start_kernel
  4. printf
  5. init

   1 /*
   2  *  linux/init/main.c
   3  *
   4  *  (C) 1991  Linus Torvalds
   5  */
   6 
   7 #define __LIBRARY__
   8 #include <unistd.h>
   9 #include <time.h>
  10 
  11 /*
  12  * we need this inline - forking from kernel space will result
  13  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  14  * is no problem, but for the stack. This is handled by not letting
  15  * main() use the stack at all after fork(). Thus, no function
  16  * calls - which means inline code for fork too, as otherwise we
  17  * would use the stack upon exit from 'fork()'.
  18  *
  19  * Actually only pause and fork are needed inline, so that there
  20  * won't be any messing with the stack from main(), but we define
  21  * some others too.
  22  */
  23 static inline _syscall0(int,fork)
     /* [previous][next][first][last][top][bottom][index][help] */
  24 static inline _syscall0(int,pause)
  25 static inline _syscall1(int,setup,void *,BIOS)
  26 static inline _syscall0(int,sync)
  27 
  28 #include <linux/tty.h>
  29 #include <linux/sched.h>
  30 #include <linux/head.h>
  31 #include <asm/system.h>
  32 #include <asm/io.h>
  33 
  34 #include <stddef.h>
  35 #include <stdarg.h>
  36 #include <unistd.h>
  37 #include <fcntl.h>
  38 #include <sys/types.h>
  39 
  40 #include <linux/fs.h>
  41 
  42 #include <string.h>
  43 
  44 static char printbuf[1024];
  45 
  46 extern char *strcpy();
  47 extern int vsprintf();
  48 extern void init(void);
  49 extern void blk_dev_init(void);
  50 extern void chr_dev_init(void);
  51 extern void hd_init(void);
  52 extern void floppy_init(void);
  53 extern void mem_init(long start, long end);
  54 extern long rd_init(long mem_start, int length);
  55 extern long kernel_mktime(struct tm * tm);
  56 
  57 static int sprintf(char * str, const char *fmt, ...)
  58 {
  59         va_list args;
  60         int i;
  61 
  62         va_start(args, fmt);
  63         i = vsprintf(str, fmt, args);
  64         va_end(args);
  65         return i;
  66 }
  67 
  68 /*
  69  * This is set up by the setup-routine at boot-time
  70  */
  71 #define EXT_MEM_K (*(unsigned short *)0x90002)
  72 #define CON_ROWS ((*(unsigned short *)0x9000e) & 0xff)
  73 #define CON_COLS (((*(unsigned short *)0x9000e) & 0xff00) >> 8)
  74 #define DRIVE_INFO (*(struct drive_info *)0x90080)
  75 #define ORIG_ROOT_DEV (*(unsigned short *)0x901FC)
  76 
  77 /*
  78  * Yeah, yeah, it's ugly, but I cannot find how to do this correctly
  79  * and this seems to work. I anybody has more info on the real-time
  80  * clock I'd be interested. Most of this was trial and error, and some
  81  * bios-listing reading. Urghh.
  82  */
  83 
  84 #define CMOS_READ(addr) ({ \
  85 outb_p(0x80|addr,0x70); \
  86 inb_p(0x71); \
  87 })
  88 
  89 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
  90 
  91 static void time_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93         struct tm time;
  94 
  95         do {
  96                 time.tm_sec = CMOS_READ(0);
  97                 time.tm_min = CMOS_READ(2);
  98                 time.tm_hour = CMOS_READ(4);
  99                 time.tm_mday = CMOS_READ(7);
 100                 time.tm_mon = CMOS_READ(8);
 101                 time.tm_year = CMOS_READ(9);
 102         } while (time.tm_sec != CMOS_READ(0));
 103         BCD_TO_BIN(time.tm_sec);
 104         BCD_TO_BIN(time.tm_min);
 105         BCD_TO_BIN(time.tm_hour);
 106         BCD_TO_BIN(time.tm_mday);
 107         BCD_TO_BIN(time.tm_mon);
 108         BCD_TO_BIN(time.tm_year);
 109         time.tm_mon--;
 110         startup_time = kernel_mktime(&time);
 111 }
 112 
 113 static long memory_end = 0;
 114 static long buffer_memory_end = 0;
 115 static long main_memory_start = 0;
 116 static char term[32];
 117 
 118 static char * argv_init[] = { "/bin/init", NULL };
 119 static char * envp_init[] = { "HOME=/", NULL, NULL };
 120 
 121 static char * argv_rc[] = { "/bin/sh", NULL };
 122 static char * envp_rc[] = { "HOME=/", NULL ,NULL };
 123 
 124 static char * argv[] = { "-/bin/sh",NULL };
 125 static char * envp[] = { "HOME=/usr/root", NULL, NULL };
 126 
 127 struct drive_info { char dummy[32]; } drive_info;
 128 
 129 void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131 /*
 132  * Interrupts are still disabled. Do necessary setups, then
 133  * enable them
 134  */
 135         ROOT_DEV = ORIG_ROOT_DEV;
 136         sprintf(term, "TERM=con%dx%d", CON_COLS, CON_ROWS);
 137         envp[1] = term; 
 138         envp_rc[1] = term;
 139         envp_init[1] = term;
 140         drive_info = DRIVE_INFO;
 141         memory_end = (1<<20) + (EXT_MEM_K<<10);
 142         memory_end &= 0xfffff000;
 143         if (memory_end > 16*1024*1024)
 144                 memory_end = 16*1024*1024;
 145         if (memory_end >= 12*1024*1024) 
 146                 buffer_memory_end = 4*1024*1024;
 147         else if (memory_end >= 6*1024*1024)
 148                 buffer_memory_end = 2*1024*1024;
 149         else if (memory_end >= 4*1024*1024)
 150                 buffer_memory_end = 3*512*1024;
 151         else
 152                 buffer_memory_end = 1*1024*1024;
 153         main_memory_start = buffer_memory_end;
 154 #ifdef RAMDISK
 155         main_memory_start += rd_init(main_memory_start, RAMDISK*1024);
 156 #endif
 157         mem_init(main_memory_start,memory_end);
 158         trap_init();
 159         blk_dev_init();
 160         chr_dev_init();
 161         tty_init();
 162         time_init();
 163         sched_init();
 164         buffer_init(buffer_memory_end);
 165         hd_init();
 166         floppy_init();
 167         sti();
 168         move_to_user_mode();
 169         if (!fork()) {          /* we count on this going ok */
 170                 init();
 171         }
 172 /*
 173  *   NOTE!!   For any other task 'pause()' would mean we have to get a
 174  * signal to awaken, but task0 is the sole exception (see 'schedule()')
 175  * as task 0 gets activated at every idle moment (when no other tasks
 176  * can run). For task0 'pause()' just means we go check if some other
 177  * task can run, and if not we return here.
 178  */
 179         for(;;)
 180                 __asm__("int $0x80"::"a" (__NR_pause):"ax");
 181 }
 182 
 183 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185         va_list args;
 186         int i;
 187 
 188         va_start(args, fmt);
 189         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 190         va_end(args);
 191         return i;
 192 }
 193 
 194 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         int pid,i;
 197 
 198         setup((void *) &drive_info);
 199         (void) open("/dev/tty1",O_RDWR,0);
 200         (void) dup(0);
 201         (void) dup(0);
 202         printf("%d buffers = %d bytes buffer space\n\r",NR_BUFFERS,
 203                 NR_BUFFERS*BLOCK_SIZE);
 204         printf("Free mem: %d bytes\n\r",memory_end-main_memory_start);
 205 
 206         execve("/etc/init",argv_init,envp_init);
 207         execve("/bin/init",argv_init,envp_init);
 208         /* if this fails, fall through to original stuff */
 209 
 210         if (!(pid=fork())) {
 211                 close(0);
 212                 if (open("/etc/rc",O_RDONLY,0))
 213                         _exit(1);
 214                 execve("/bin/sh",argv_rc,envp_rc);
 215                 _exit(2);
 216         }
 217         if (pid>0)
 218                 while (pid != wait(&i))
 219                         /* nothing */;
 220         while (1) {
 221                 if ((pid=fork())<0) {
 222                         printf("Fork failed in init\r\n");
 223                         continue;
 224                 }
 225                 if (!pid) {
 226                         close(0);close(1);close(2);
 227                         setsid();
 228                         (void) open("/dev/tty1",O_RDWR,0);
 229                         (void) dup(0);
 230                         (void) dup(0);
 231                         _exit(execve("/bin/sh",argv,envp));
 232                 }
 233                 while (1)
 234                         if (pid == wait(&i))
 235                                 break;
 236                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 237                 sync();
 238         }
 239         _exit(0);       /* NOTE! _exit, not exit() */
 240 }

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