-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
129 lines (103 loc) · 2.79 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <levos/levos.h>
void parse_multiboot(struct multiboot *mboot)
{
arch_do_mboot(mboot);
}
#define DRIVER_INIT(x) rc = x(); if (rc) { printk(#x \
" failed to init with rc=%d\n", rc); }
int drivers_init()
{
int rc;
DRIVER_INIT(vfs_init);
DRIVER_INIT(tty_init);
DRIVER_INIT(ata_init);
DRIVER_INIT(keyboard_init);
return 0;
}
void do_test()
{
/* try /bin/sh */
char *p = "/levex.txt";
printk("test: trying to find inode of %s\n", p);
int i = ext2_find_file_inode(__get__fs(), p);
if (i < 0) {
printk("test: %s does not exist!\n", p);
return;
}
printk("test: inode of %s is %d\n", p, i);
struct stat st;
int rc = vfs_stat(p, &st);
if (rc < 0) {
printk("test: an error occured while stating\n");
return;
}
printk("test: size of %s is %d bytes\n", p, st.st_size);
struct file *f = vfs_open(p);
if ((uintptr_t)f <= 0) {
printk("test: failed to open %s\n", p);
return;
}
void *buffer = kmalloc(st.st_size);
rc = f->fops->read(f, buffer, st.st_size);
if (rc < 0) {
printk("test: failed to read from file %s\n", p);
return;
}
printk("test: contents: %s", buffer);
}
void do_mount() {
for(int i = 0; i < get_num_of_devices(); i++)
vfs_mount("/", device_get(i));
if (!vfs_root_mounted())
panic("unable to mount root directory\n");
}
void start_sched()
{
int rc = sched_init();
if (rc)
return;
sched_start_idle();
}
extern int kernel_end;
extern int kernel_base;
/* this is called by kinit (pid1) */
int main_init() {
int retval = 0;
retval = call_syscall(4, 1, (int) "Hello, world!\n", 14);
/* asm volatile("int $0x80"
:"=r"(retval)
:"a"(4),"b"(1),"c"("Hello, world!\n"),"d"(14)); */
/* fire up kernel shell */
kernel_shell_start();
return -ENOENT;
}
void main(struct multiboot *mboot) {
int rc;
/* init the kmsg buffer and printk */
console_sys_init();
kmsg_init();
console_register(&kmsg_con);
/* arch might want to setup stuff */
arch_early_init();
printk("kmsg: buffer setup!\n");
printk("sys: kernel relocation: 0x%x -> 0x%x size 0x%x\n",
&kernel_base, &kernel_end,
(uintptr_t)&kernel_end - (uintptr_t)&kernel_base);
parse_multiboot(mboot);
paging_fini();
heap_install();
/* arch should finish init now */
arch_late_init();
printk("sys: init done!\n");
drivers_init();
do_mount();
do_test();
syscall_init();
/* wait forever */
asm volatile("mov $0x1337, %eax");
asm volatile("mov $0x1337, %ebx");
asm volatile("mov $0x1337, %ecx");
asm volatile("mov $0x1337, %edx");
start_sched();
panic("finished with main, but no idle task was started\n");
}