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

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