cleanup for tfs
[thunix.git] / init / init.c
blob120e629a86eaf81490dc521b109994b027ed63aa
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 <tfs.h>
11 #include <cache.h>
12 #include <dirent.h>
13 #include <fd.h>
14 /*#include <sched.h>*/
15 //#include <rd.h>
16 //#include <fs_ext2.h>
18 extern void trap_init(void);
19 extern void con_init(void);
20 extern void keyboard_init(void);
21 extern void timer_init(int);
22 extern long kernel_mktime(struct tm*);
23 extern void timer_interrupt(void);
24 extern void floppy_interrupt(void);
25 //extern void rd_init(void);
26 //extern void ram_ext2fs_init();
27 //extern void ext2_fs_init();
30 #define CMOS_READ(addr) ({ \
31 outb_p(addr, 0x70); \
32 inb_p(0x71); \
35 #define BCD_TO_BIN(val) ((val) = ((val)&0xF) + ((val)>>4)*10)
40 unsigned long get_current_time(struct tm * time)
42 unsigned long current_time;
43 do {
44 time->tm_sec = CMOS_READ(0);
45 time->tm_min = CMOS_READ(2);
46 time->tm_hour = CMOS_READ(4);
47 time->tm_mday = CMOS_READ(7);
48 time->tm_mon = CMOS_READ(8);
49 time->tm_year = CMOS_READ(9);
50 }while(time->tm_sec != CMOS_READ(0));
54 BCD_TO_BIN(time->tm_sec);
55 BCD_TO_BIN(time->tm_min);
56 BCD_TO_BIN(time->tm_hour);
57 BCD_TO_BIN(time->tm_mday);
58 BCD_TO_BIN(time->tm_mon);
59 BCD_TO_BIN(time->tm_year);
61 /* desrease for mktime to get the right seconds*/
62 time->tm_mon--;
63 current_time = kernel_mktime(time);
65 /* restore it */
66 time->tm_mon++;
68 return current_time;
75 /*
76 * Detecting Floppy Drives
78 void detect_floppy_drive()
80 /* First Getting the data from cmos */
81 int high, low;
82 unsigned char c;
83 outb_p (0x10, 0x70);
84 c = inb_p (0x71);
86 /* Decodding it... */
87 high = c >> 4;
88 low = c &0xF;
91 * Now, there are 5 official types of floppy drives that the most
92 * CMOSes can detect:
93 * Type of drive: Number the CMOS gives it:
94 * 360kb 5.25in 1
95 * 1.2mb 5.25in 2
96 * 720kb 3.5in 3
97 * 1.44mb 3.5in 4
98 * 2.88mb 3.5in 5
99 * No drive 0
101 char *drive_type[6] = {
102 "no floppy drive",
103 "360kb 5.25in floppy drive",
104 "1.2mb 5.25in floppy drive",
105 "720kb 3.5in", "1.44mb 3.5in",
106 "2.88mb 3.5in"
108 printk("Floppy drive A is an:\n");
109 printk(drive_type[high]);
110 printk("\nFloppy drive B is an:\n");
111 printk(drive_type[low]);
112 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 ...";
143 struct tfs_sb_info *tfs_sbi;
145 void init(void)
147 char ok[] = "[OK]";
148 unsigned long startup_time;
149 struct tm time;
151 cli();
153 malloc_init();
155 trap_init();
156 con_init();
158 printk("%s", kb_msg);
159 keyboard_init();
160 printk("\t\t%s\n", ok);
162 printk("%s", time_msg);
163 timer_init(100);
164 printk("\t\t%s\n", ok);
166 printk("Getting Current time ....");
167 startup_time = get_current_time(&time);
168 printk("\t\t%s\n", ok);
170 printk("%s", flp_msg);
171 floppy_init();
172 printk("\t\t%s\n", ok);
174 printk("mounting tfs image as root fs...");
175 tfs_sbi = tfs_mount();
176 printk("\t%s\n", ok);
178 printk("Cache system initialiaztion...");
179 cache_init(tfs_sbi);
180 printk("\t\t%s\n", ok);
182 printk("Cd into root... ");
183 this_dir = tfs_opendir(tfs_sbi, "/");
184 if (!this_dir) {
185 printf("cd into root dir failed!\n");
187 printk("\t\t\t%s\n", ok);
192 printk("%s", rd_msg);
193 rd_init();
194 printk("\t\t%s\n", ok);
196 printk("%s", ram_ext2_msg);
197 ram_ext2fs_init();
198 printk("\t\t%s\n", ok);
200 printk("%s", ext2_msg);
201 ext2_fs_init();
202 printk("\t%s\n", ok);
204 printk("%s",ram_mke2fs_msg);
205 ram_mke2fs();
206 printk("\t\t%s\n", ok);
209 /* Hope it quite safe now */
210 sti();
215 #if 0 /* debug like, read curret eip or divide error */
216 memcpy((char *)0, "ERROR",4);
217 printk("%s\n",(char *)0);
218 printk("eip: %08x\n",read_eip());
219 a = a/b;
220 printk("eip: %08x\n",read_eip());
221 printk("eip: %0xx\n",get_eip());
222 #endif
226 #if 0
227 for (;;) {
228 __asm__ ("movb %%al, 0xb8000+160*24"::"a"(wheel[i]));
229 if (i == sizeof wheel)
230 i = 0;
231 else
232 ++i;
234 #endif
237 #if 0 /* idt addr test */
238 printk("num\taddr\t\tnum\taddr\n");
239 for (i = 0; i < 0x32;) {
240 printk("%x\t%08x\t",i,idt[i]&0xffff);
241 i++;
242 printk("%x\t%08x\n",i,idt[i]&0xffff);
243 i++;
245 #endif
248 #if 0 /* timer ticks test */
249 while (1) {
250 if (timer_ticks % 16 == 0)
251 printk("timer ticks: %08x\n",timer_ticks);
253 #endif
255 #if 0 /* count down And sleep test */
256 count_down = 10;
257 while (count_down) {
258 printk("counter rest : %08x\n", count_down);
259 sleep(9);
263 printk("timer_ticks:%d\n",timer_ticks);
264 sleep(500); /* sleep 5s */
265 printk("timer_ticks:%d\n",timer_ticks);
266 pause();
267 #endif
272 * the floppy driver is not ready , and i think....
275 #if 0 /*
276 * floppy driver test
278 * it seems work now! what a happy day - the last day of 2008
279 * And laterly the first day of 2009.
280 * but it always output something noisy...
283 * Bochs is exiting with the following message:
284 * [FDD ] read/write command: sector size 33554432 not supported
286 * and i got no idea. Please FIX ME.
290 char *buf = (char *)0x10000;
292 //detect_floppy_drive();
293 printk("floppy_interrupt addr:%08x\n",floppy_interrupt);
294 floppy_read(0, buf, 1);
295 printk("returned but not sure read_ok\n");
296 hexdump(buf, 64);
298 #endif
301 #if 0 /* EXT2_FS test */
302 /* debug */
303 extern char dir[8];
304 mkdir("/usr");
305 ls("/","-l");
306 ls(dir, "-l");
307 #endif
310 printk("\n");
311 printk("\t**************************\n");
312 printk("\t* *\n");
313 printk("\t* Welcome to Thunix *\n");
314 printk("\t* *\n");
315 printk("\t**************************\n");
316 printk("\tType 'help' for more information\n");
317 printk("\n");
320 shell_init();
322 pause();