Implement sys_execve call
[thunix.git] / init / init.c
blob590f8e151e3509d30f0e3837a72e14ba710ff0db
1 #include <stdio.h>
2 #include <string.h>
3 #include <console.h>
4 #include <asm/system.h>
5 #include <asm/io.h>
6 #include <keyboard.h>
7 #include <timer.h>
8 #include <time.h>
9 #include <thunix.h>
10 #include <fs.h>
11 #include <tfs.h>
12 #include <cache.h>
13 #include <dirent.h>
14 #include <fd.h>
15 #include <err.h>
16 /*#include <sched.h>*/
17 //#include <rd.h>
18 //#include <fs_ext2.h>
20 extern void trap_init(void);
21 extern void con_init(void);
22 extern void keyboard_init(void);
23 extern void timer_init(int);
24 extern long kernel_mktime(struct tm*);
25 extern void timer_interrupt(void);
26 extern void floppy_interrupt(void);
27 //extern void rd_init(void);
28 //extern void ram_ext2fs_init();
29 //extern void ext2_fs_init();
32 #define CMOS_READ(addr) ({ \
33 outb_p(addr, 0x70); \
34 inb_p(0x71); \
37 #define BCD_TO_BIN(val) ((val) = ((val)&0xF) + ((val)>>4)*10)
42 unsigned long get_current_time(struct tm * time)
44 unsigned long current_time;
45 do {
46 time->tm_sec = CMOS_READ(0);
47 time->tm_min = CMOS_READ(2);
48 time->tm_hour = CMOS_READ(4);
49 time->tm_mday = CMOS_READ(7);
50 time->tm_mon = CMOS_READ(8);
51 time->tm_year = CMOS_READ(9);
52 }while(time->tm_sec != CMOS_READ(0));
56 BCD_TO_BIN(time->tm_sec);
57 BCD_TO_BIN(time->tm_min);
58 BCD_TO_BIN(time->tm_hour);
59 BCD_TO_BIN(time->tm_mday);
60 BCD_TO_BIN(time->tm_mon);
61 BCD_TO_BIN(time->tm_year);
63 /* desrease for mktime to get the right seconds*/
64 time->tm_mon--;
65 current_time = kernel_mktime(time);
67 /* restore it */
68 time->tm_mon++;
70 return current_time;
77 /*
78 * Detecting Floppy Drives
80 void detect_floppy_drive()
82 /* First Getting the data from cmos */
83 int high, low;
84 unsigned char c;
85 outb_p (0x10, 0x70);
86 c = inb_p (0x71);
88 /* Decodding it... */
89 high = c >> 4;
90 low = c &0xF;
93 * Now, there are 5 official types of floppy drives that the most
94 * CMOSes can detect:
95 * Type of drive: Number the CMOS gives it:
96 * 360kb 5.25in 1
97 * 1.2mb 5.25in 2
98 * 720kb 3.5in 3
99 * 1.44mb 3.5in 4
100 * 2.88mb 3.5in 5
101 * No drive 0
103 char *drive_type[6] = {
104 "no floppy drive",
105 "360kb 5.25in floppy drive",
106 "1.2mb 5.25in floppy drive",
107 "720kb 3.5in", "1.44mb 3.5in",
108 "2.88mb 3.5in"
110 printk("Floppy drive A is an:\n");
111 printk(drive_type[high]);
112 printk("\nFloppy drive B is an:\n");
113 printk(drive_type[low]);
114 printk("\n");
117 extern unsigned long long * idt;
118 extern unsigned long timer_ticks;
119 extern unsigned long count_down;
120 extern char tmp_floppy_area[1024];
122 extern void keyboard_interrupt(void);
123 extern void do_timer(void);
124 extern unsigned long read_eip();
125 //extern void ram_mke2fs();
126 //extern struct m_inode *ext2_namei(char *);
128 extern void shell_init();
132 char *trap_msg = "Setting Interrupt Handling ...";
133 char *con_msg = "Console initialization ...";
134 char *kb_msg = "Keyboard initialization ...";
135 char *time_msg = "Timer clock initialization ...";
136 char *flp_msg = "Floppy initialization ...";
137 //char *rd_msg = "Ram disk initialization ...";
138 //char *ram_ext2_msg = "Ram ext2 fs initialization ...";
139 //char *ext2_msg = "Ext2 filesystem initialization ...";
140 //char *ram_mke2fs_msg = "Making Ram ext2 filesystem ...";
144 void init(void)
146 char ok[] = "[OK]";
147 unsigned long startup_time;
148 struct tm time;
150 cli();
152 malloc_init();
154 trap_init();
155 con_init();
157 printk("%s", kb_msg);
158 keyboard_init();
159 printk("\t\t%s\n", ok);
161 printk("%s", time_msg);
162 timer_init(100);
163 printk("\t\t%s\n", ok);
165 printk("Getting Current time ....");
166 startup_time = get_current_time(&time);
167 printk("\t\t%s\n", ok);
169 printk("%s", flp_msg);
170 floppy_init();
171 printk("\t\t%s\n", ok);
173 fs_init();
179 printk("%s", rd_msg);
180 rd_init();
181 printk("\t\t%s\n", ok);
183 printk("%s", ram_ext2_msg);
184 ram_ext2fs_init();
185 printk("\t\t%s\n", ok);
187 printk("%s", ext2_msg);
188 ext2_fs_init();
189 printk("\t%s\n", ok);
191 printk("%s",ram_mke2fs_msg);
192 ram_mke2fs();
193 printk("\t\t%s\n", ok);
196 /* Hope it quite safe now */
197 sti();
202 #if 0 /* debug like, read curret eip or divide error */
203 memcpy((char *)0, "ERROR",4);
204 printk("%s\n",(char *)0);
205 printk("eip: %08x\n",read_eip());
206 a = a/b;
207 printk("eip: %08x\n",read_eip());
208 printk("eip: %0xx\n",get_eip());
209 #endif
213 #if 0
214 for (;;) {
215 __asm__ ("movb %%al, 0xb8000+160*24"::"a"(wheel[i]));
216 if (i == sizeof wheel)
217 i = 0;
218 else
219 ++i;
221 #endif
224 #if 0 /* idt addr test */
225 printk("num\taddr\t\tnum\taddr\n");
226 for (i = 0; i < 0x32;) {
227 printk("%x\t%08x\t",i,idt[i]&0xffff);
228 i++;
229 printk("%x\t%08x\n",i,idt[i]&0xffff);
230 i++;
232 #endif
235 #if 0 /* timer ticks test */
236 while (1) {
237 if (timer_ticks % 16 == 0)
238 printk("timer ticks: %08x\n",timer_ticks);
240 #endif
242 #if 0 /* count down And sleep test */
243 count_down = 10;
244 while (count_down) {
245 printk("counter rest : %08x\n", count_down);
246 sleep(9);
250 printk("timer_ticks:%d\n",timer_ticks);
251 sleep(500); /* sleep 5s */
252 printk("timer_ticks:%d\n",timer_ticks);
253 pause();
254 #endif
259 * the floppy driver is not ready , and i think....
262 #if 0 /*
263 * floppy driver test
265 * it seems work now! what a happy day - the last day of 2008
266 * And laterly the first day of 2009.
267 * but it always output something noisy...
270 * Bochs is exiting with the following message:
271 * [FDD ] read/write command: sector size 33554432 not supported
273 * and i got no idea. Please FIX ME.
277 char *buf = (char *)0x10000;
279 //detect_floppy_drive();
280 printk("floppy_interrupt addr:%08x\n",floppy_interrupt);
281 floppy_read(0, buf, 1);
282 printk("returned but not sure read_ok\n");
283 hexdump(buf, 64);
285 #endif
288 #if 0 /* EXT2_FS test */
289 /* debug */
290 extern char dir[8];
291 mkdir("/usr");
292 ls("/","-l");
293 ls(dir, "-l");
294 #endif
297 printk("\n");
298 printk("\t**************************\n");
299 printk("\t* *\n");
300 printk("\t* Welcome to Thunix *\n");
301 printk("\t* *\n");
302 printk("\t**************************\n");
303 printk("\tType 'help' for more information\n");
304 printk("\n");
307 shell_init();
309 pause();