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. main
  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 #define ORIG_SWAP_DEV (*(unsigned short *)0x901FA)
  77 
  78 /*
  79  * Yeah, yeah, it's ugly, but I cannot find how to do this correctly
  80  * and this seems to work. I anybody has more info on the real-time
  81  * clock I'd be interested. Most of this was trial and error, and some
  82  * bios-listing reading. Urghh.
  83  */
  84 
  85 #define CMOS_READ(addr) ({ \
  86 outb_p(0x80|addr,0x70); \
  87 inb_p(0x71); \
  88 })
  89 
  90 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
  91 
  92 static void time_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94         struct tm time;
  95 
  96         do {
  97                 time.tm_sec = CMOS_READ(0);
  98                 time.tm_min = CMOS_READ(2);
  99                 time.tm_hour = CMOS_READ(4);
 100                 time.tm_mday = CMOS_READ(7);
 101                 time.tm_mon = CMOS_READ(8);
 102                 time.tm_year = CMOS_READ(9);
 103         } while (time.tm_sec != CMOS_READ(0));
 104         BCD_TO_BIN(time.tm_sec);
 105         BCD_TO_BIN(time.tm_min);
 106         BCD_TO_BIN(time.tm_hour);
 107         BCD_TO_BIN(time.tm_mday);
 108         BCD_TO_BIN(time.tm_mon);
 109         BCD_TO_BIN(time.tm_year);
 110         time.tm_mon--;
 111         startup_time = kernel_mktime(&time);
 112 }
 113 
 114 static long memory_end = 0;
 115 static long buffer_memory_end = 0;
 116 static long main_memory_start = 0;
 117 static char term[32];
 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 main(void)         /* This really IS void, no error here. */
     /* [previous][next][first][last][top][bottom][index][help] */
 128 {                       /* The startup routine assumes (well, ...) this */
 129 /*
 130  * Interrupts are still disabled. Do necessary setups, then
 131  * enable them
 132  */
 133         ROOT_DEV = ORIG_ROOT_DEV;
 134         SWAP_DEV = ORIG_SWAP_DEV;
 135         sprintf(term, "TERM=con%dx%d", CON_COLS, CON_ROWS);
 136         envp[1] = term; 
 137         envp_rc[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
 148                 buffer_memory_end = 1*1024*1024;
 149         main_memory_start = buffer_memory_end;
 150 #ifdef RAMDISK
 151         main_memory_start += rd_init(main_memory_start, RAMDISK*1024);
 152 #endif
 153         mem_init(main_memory_start,memory_end);
 154         trap_init();
 155         blk_dev_init();
 156         chr_dev_init();
 157         tty_init();
 158         time_init();
 159         sched_init();
 160         buffer_init(buffer_memory_end);
 161         hd_init();
 162         floppy_init();
 163         sti();
 164         move_to_user_mode();
 165         if (!fork()) {          /* we count on this going ok */
 166                 init();
 167         }
 168 /*
 169  *   NOTE!!   For any other task 'pause()' would mean we have to get a
 170  * signal to awaken, but task0 is the sole exception (see 'schedule()')
 171  * as task 0 gets activated at every idle moment (when no other tasks
 172  * can run). For task0 'pause()' just means we go check if some other
 173  * task can run, and if not we return here.
 174  */
 175         for(;;)
 176                 __asm__("int $0x80"::"a" (__NR_pause):"ax");
 177 }
 178 
 179 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181         va_list args;
 182         int i;
 183 
 184         va_start(args, fmt);
 185         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 186         va_end(args);
 187         return i;
 188 }
 189 
 190 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 191 {
 192         int pid,i;
 193 
 194         setup((void *) &drive_info);
 195         (void) open("/dev/tty1",O_RDWR,0);
 196         (void) dup(0);
 197         (void) dup(0);
 198         printf("%d buffers = %d bytes buffer space\n\r",NR_BUFFERS,
 199                 NR_BUFFERS*BLOCK_SIZE);
 200         printf("Free mem: %d bytes\n\r",memory_end-main_memory_start);
 201         if (!(pid=fork())) {
 202                 close(0);
 203                 if (open("/etc/rc",O_RDONLY,0))
 204                         _exit(1);
 205                 execve("/bin/sh",argv_rc,envp_rc);
 206                 _exit(2);
 207         }
 208         if (pid>0)
 209                 while (pid != wait(&i))
 210                         /* nothing */;
 211         while (1) {
 212                 if ((pid=fork())<0) {
 213                         printf("Fork failed in init\r\n");
 214                         continue;
 215                 }
 216                 if (!pid) {
 217                         close(0);close(1);close(2);
 218                         setsid();
 219                         (void) open("/dev/tty1",O_RDWR,0);
 220                         (void) dup(0);
 221                         (void) dup(0);
 222                         _exit(execve("/bin/sh",argv,envp));
 223                 }
 224                 while (1)
 225                         if (pid == wait(&i))
 226                                 break;
 227                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 228                 sync();
 229         }
 230         _exit(0);       /* NOTE! _exit, not exit() */
 231 }

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