Release version 0.3
[thunix.git] / init / init.c
blob8afcc34725b2e7a90389303fa50f9359dae119de
1 /*#include <string.h>*/
2 #include <console.h>
3 #include <asm/system.h>
4 #include <asm/io.h>
5 #include <keyboard.h>
6 #include <timer.h>
7 #include <time.h>
8 #include <thunix.h>
9 /*#include <sched.h>*/
10 #include <rd.h>
11 #include <fs_ext2.h>
12 #include <fd.h>
14 extern void trap_init(void);
15 extern void con_init(void);
16 extern void keyboard_init(void);
17 extern void timer_init(int);
18 extern long kernel_mktime(struct tm*);
19 extern int printk(char *fmt, ...);
20 extern void timer_interrupt(void);
21 extern void floppy_interrupt(void);
22 extern void rd_init(void);
23 extern void ram_ext2fs_init();
24 extern void ext2_fs_init();
27 #define CMOS_READ(addr) ({ \
28 outb_p(addr, 0x70); \
29 inb_p(0x71); \
32 #define BCD_TO_BIN(val) ((val) = ((val)&0xF) + ((val)>>4)*10)
37 unsigned long get_current_time(struct tm * time)
39 unsigned long current_time;
40 do {
41 time->tm_sec = CMOS_READ(0);
42 time->tm_min = CMOS_READ(2);
43 time->tm_hour = CMOS_READ(4);
44 time->tm_mday = CMOS_READ(7);
45 time->tm_mon = CMOS_READ(8);
46 time->tm_year = CMOS_READ(9);
47 }while(time->tm_sec != CMOS_READ(0));
51 BCD_TO_BIN(time->tm_sec);
52 BCD_TO_BIN(time->tm_min);
53 BCD_TO_BIN(time->tm_hour);
54 BCD_TO_BIN(time->tm_mday);
55 BCD_TO_BIN(time->tm_mon);
56 BCD_TO_BIN(time->tm_year);
58 /* desrease for mktime to get the right seconds*/
59 time->tm_mon--;
60 current_time = kernel_mktime(time);
62 /* restore it */
63 time->tm_mon++;
65 return current_time;
72 /*
73 * Detecting Floppy Drives
75 void detect_floppy_drive()
77 /* First Getting the data from cmos */
78 int high, low;
79 unsigned char c;
80 outb_p (0x10, 0x70);
81 c = inb_p (0x71);
83 /* Decodding it... */
84 high = c >> 4;
85 low = c &0xF;
88 * Now, there are 5 official types of floppy drives that the most
89 * CMOSes can detect:
90 * Type of drive: Number the CMOS gives it:
91 * 360kb 5.25in 1
92 * 1.2mb 5.25in 2
93 * 720kb 3.5in 3
94 * 1.44mb 3.5in 4
95 * 2.88mb 3.5in 5
96 * No drive 0
98 char *drive_type[6] = {
99 "no floppy drive",
100 "360kb 5.25in floppy drive",
101 "1.2mb 5.25in floppy drive",
102 "720kb 3.5in", "1.44mb 3.5in",
103 "2.88mb 3.5in"
105 printk("Floppy drive A is an:\n");
106 printk(drive_type[high]);
107 printk("\nFloppy drive B is an:\n");
108 printk(drive_type[low]);
109 printk("\n");
114 extern unsigned long long * idt;
115 extern unsigned long timer_ticks;
116 extern unsigned long count_down;
117 extern char tmp_floppy_area[1024];
119 extern void keyboard_interrupt(void);
120 extern void do_timer(void);
121 extern unsigned long read_eip();
122 extern void ram_mke2fs();
123 extern struct m_inode *ext2_namei(char *);
125 extern void shell_init();
129 char *trap_msg = "Setting Interrupt Handling ...";
130 char *con_msg = "Console initialization ...";
131 char *kb_msg = "Keyboard initialization ...";
132 char *time_msg = "Timer clock initialization ...";
133 char *flp_msg = "Floppy initialization ...";
134 char *rd_msg = "Ram disk initialization ...";
135 char *ram_ext2_msg = "Ram ext2 fs initialization ...";
136 char *ext2_msg = "Ext2 filesystem initialization ...";
137 char *ram_mke2fs_msg = "Making Ram ext2 filesystem ...";
140 void init(void)
142 char ok[] = "[OK]";
143 unsigned long startup_time;
144 struct tm time;
146 cli();
148 malloc_init();
150 trap_init();
151 con_init();
153 printk("%s", kb_msg);
154 keyboard_init();
155 printk("\t\t%s\n", ok);
157 printk("%s", time_msg);
158 timer_init(100);
159 printk("\t\t%s\n", ok);
161 printk("Getting Current time ....");
162 startup_time = get_current_time(&time);
163 printk("\t\t%s\n", ok);
165 printk("%s", flp_msg);
166 floppy_init();
167 printk("\t\t%s\n", ok);
169 printk("%s", rd_msg);
170 rd_init();
171 printk("\t\t%s\n", ok);
173 printk("%s", ram_ext2_msg);
174 ram_ext2fs_init();
175 printk("\t\t%s\n", ok);
177 printk("%s", ext2_msg);
178 ext2_fs_init();
179 printk("\t%s\n", ok);
181 printk("%s",ram_mke2fs_msg);
182 ram_mke2fs();
183 printk("\t\t%s\n", ok);
185 /* Hope it quite safe now */
186 sti();
191 #if 0 /* debug like, read curret eip or divide error */
192 memcpy((char *)0, "ERROR",4);
193 printk("%s\n",(char *)0);
194 printk("eip: %08x\n",read_eip());
195 a = a/b;
196 printk("eip: %08x\n",read_eip());
197 printk("eip: %0xx\n",get_eip());
198 #endif
202 #if 0
203 for (;;) {
204 __asm__ ("movb %%al, 0xb8000+160*24"::"a"(wheel[i]));
205 if (i == sizeof wheel)
206 i = 0;
207 else
208 ++i;
210 #endif
213 #if 0 /* idt addr test */
214 printk("num\taddr\t\tnum\taddr\n");
215 for (i = 0; i < 0x32;) {
216 printk("%x\t%08x\t",i,idt[i]&0xffff);
217 i++;
218 printk("%x\t%08x\n",i,idt[i]&0xffff);
219 i++;
221 #endif
224 #if 0 /* timer ticks test */
225 while (1) {
226 if (timer_ticks % 16 == 0)
227 printk("timer ticks: %08x\n",timer_ticks);
229 #endif
231 #if 0 /* count down And sleep test */
232 count_down = 10;
233 while (count_down) {
234 printk("counter rest : %08x\n", count_down);
235 sleep(9);
239 printk("timer_ticks:%d\n",timer_ticks);
240 sleep(500); /* sleep 5s */
241 printk("timer_ticks:%d\n",timer_ticks);
242 pause();
243 #endif
248 * the floppy driver is not ready , and i think....
251 #if 0 /*
252 * floppy driver test
254 * it seems work now! what a happy day - the last day of 2008
255 * And laterly the first day of 2009.
256 * but it always output something noisy...
259 * Bochs is exiting with the following message:
260 * [FDD ] read/write command: sector size 33554432 not supported
262 * and i got no idea. Please FIX ME.
266 char *buf = (char *)0x10000;
268 //detect_floppy_drive();
269 printk("floppy_interrupt addr:%08x\n",floppy_interrupt);
270 floppy_read(0, buf, 1);
271 printk("returned but not sure read_ok\n");
272 hexdump(buf, 64);
274 #endif
277 #if 0 /* EXT2_FS test */
278 /* debug */
279 extern char dir[8];
280 mkdir("/usr");
281 ls("/","-l");
282 ls(dir, "-l");
283 #endif
286 printk("\n");
287 printk("\t**************************\n");
288 printk("\t* *\n");
289 printk("\t* Welcome to Thunix *\n");
290 printk("\t* *\n");
291 printk("\t**************************\n");
292 printk("\tType 'help' for more information\n");
293 printk("\n");
296 shell_init();
298 pause();