-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
226 lines (205 loc) · 8.89 KB
/
kernel.c
File metadata and controls
226 lines (205 loc) · 8.89 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* OneOS-ARM 1 Kernel
* Main kernel entry point
*/
#include "uart.h"
#include "mem.h"
#include "keyboard.h"
#include "string.h"
#include "terminal.h"
#include "vfs.h"
#include "gui.h"
#include "virtio_input.h"
#include "virtio_rng.h"
#include "editor.h"
/* Define basic types for bare-metal */
typedef unsigned char uint8_t;
/* Screen dimensions for mouse bounds checking */
#define SCREEN_W 1024
#define SCREEN_H 768
/* Helper: write a string to both the terminal buffer and render it */
static void term_print(const char *s)
{
terminal_puts(s);
terminal_render();
}
int main(void)
{
/* Initialize UART for serial communication */
uart_init();
/* Initialize memory management */
extern char _end;
unsigned long heap_start = ((unsigned long)&_end + 7UL) & ~7UL;
unsigned long heap_size = 0x00400000; /* 4MB heap */
mem_init((void *)heap_start, heap_size);
/* Initialize keyboard (UART fallback) */
keyboard_init();
/* Initialize virtio-input (QEMU GUI keyboard + mouse) */
virtio_input_init();
/* Initialize virtio-rng (Random Number Generator) */
virtio_rng_init();
/* Initialize terminal UI */
terminal_init();
terminal_clear();
/* Initialize filesystem */
vfs_init();
terminal_puts("=== OneOS-ARM 1 Booting ===\n");
terminal_puts("[OK] Memory initialized\n");
terminal_puts("[OK] Keyboard initialized\n");
terminal_puts("[OK] Terminal initialized\n");
terminal_puts("[OK] Filesystem initialized\n");
terminal_puts("\nWelcome to OneOS-ARM 1!\n");
terminal_puts("Type 'help' for commands\n\n");
terminal_puts("> ");
terminal_render();
/* Command loop */
char cmd_buffer[256];
int cmd_pos = 0;
while (1) {
/* Poll for mouse movement */
int dx = 0, dy = 0, btn = 0;
if (virtio_input_mouse(&dx, &dy, &btn)) {
int new_x = terminal.mouse_x + dx;
int new_y = terminal.mouse_y + dy;
/* Clamp to screen bounds */
if (new_x < 0) new_x = 0;
if (new_x >= SCREEN_W) new_x = SCREEN_W - 1;
if (new_y < 0) new_y = 0;
if (new_y >= SCREEN_H) new_y = SCREEN_H - 1;
terminal.mouse_x = new_x;
terminal.mouse_y = new_y;
terminal_render();
}
uint8_t key = virtio_input_getchar();
if (!key) key = keyboard_read_nonblock(); /* UART fallback */
if (key) {
if (key == '\r' || key == '\n') {
cmd_buffer[cmd_pos] = '\0';
terminal_putchar('\n');
if (cmd_pos > 0) {
if (strcmp(cmd_buffer, "help") == 0) {
terminal_puts("Commands:\n");
terminal_puts(" help - Show this help\n");
terminal_puts(" mem - Show memory info\n");
terminal_puts(" clear - Clear screen\n");
terminal_puts(" test - Run memory test\n");
terminal_puts(" ls - List files\n");
terminal_puts(" cat <f> - Show file contents\n");
terminal_puts(" touch <f>- Create file\n");
terminal_puts(" ste <f> - Edit text file\n");
terminal_puts(" rand - Show random number\n");
terminal_puts(" startgui - Launch GUI\n");
terminal_render();
} else if (strcmp(cmd_buffer, "rand") == 0) {
unsigned int r = virtio_rng_rand();
terminal_puts("Random: 0x");
terminal_puthex(r);
terminal_puts("\n");
terminal_render();
} else if (strcmp(cmd_buffer, "mem") == 0) {
terminal_puts("Memory Info:\n");
terminal_puts(" Heap: 4MB\n");
terminal_render();
} else if (strcmp(cmd_buffer, "clear") == 0) {
terminal_clear();
terminal_render();
} else if (strcmp(cmd_buffer, "test") == 0) {
terminal_puts("Memory test...\n");
terminal_render();
void *p1 = kmalloc(100);
void *p2 = kmalloc(200);
if (p1 && p2) {
term_print("Allocation OK\n");
kfree(p1);
kfree(p2);
term_print("Deallocation OK\n");
} else {
term_print("Allocation FAILED\n");
}
} else if (strcmp(cmd_buffer, "ls") == 0) {
int indices[VFS_MAX_FILES];
int count = vfs_list(vfs_root(), indices, VFS_MAX_FILES);
for (int i = 0; i < count; i++) {
vfs_node_t *n = vfs_get(indices[i]);
if (n) {
if (n->type == VFS_TYPE_DIR) {
terminal_puts(" [DIR] ");
} else {
terminal_puts(" [FILE] ");
}
terminal_puts(n->name);
terminal_puts("\n");
}
}
terminal_render();
} else if (cmd_buffer[0] == 'c' && cmd_buffer[1] == 'a' && cmd_buffer[2] == 't' && cmd_buffer[3] == ' ') {
const char *fname = cmd_buffer + 4;
int idx = vfs_find(fname, vfs_root());
if (idx >= 0) {
char buf[VFS_MAX_CONTENT];
vfs_read(idx, buf, VFS_MAX_CONTENT);
terminal_puts(buf);
terminal_puts("\n");
} else {
terminal_puts("File not found: ");
terminal_puts(fname);
terminal_puts("\n");
}
terminal_render();
} else if (cmd_buffer[0] == 's' && cmd_buffer[1] == 't' && cmd_buffer[2] == 'e' && cmd_buffer[3] == ' ') {
const char *fname = cmd_buffer + 4;
terminal_puts("Opening editor for: ");
terminal_puts(fname);
terminal_puts("\n");
terminal_render();
editor_open(fname);
terminal_puts("> ");
terminal_render();
} else if (cmd_buffer[0] == 't' && cmd_buffer[1] == 'o' && cmd_buffer[2] == 'u' &&
cmd_buffer[3] == 'c' && cmd_buffer[4] == 'h' && cmd_buffer[5] == ' ') {
const char *fname = cmd_buffer + 6;
int idx = vfs_create(fname, vfs_root(), VFS_TYPE_FILE);
if (idx >= 0) {
terminal_puts("Created: ");
terminal_puts(fname);
terminal_puts("\n");
} else {
terminal_puts("Error creating file\n");
}
terminal_render();
} else if (strcmp(cmd_buffer, "startgui") == 0) {
terminal_puts("Launching GUI...\n");
terminal_render();
gui_set_time(14, 26);
gui_init();
gui_run();
/* Return to terminal mode */
terminal_clear();
terminal_puts("Returned to terminal.\n");
terminal_puts("Type 'help' for commands\n\n");
terminal_render();
} else {
terminal_puts("Unknown: ");
terminal_puts(cmd_buffer);
terminal_puts("\n");
terminal_render();
}
}
cmd_pos = 0;
terminal_puts("> ");
terminal_render();
} else if (key == 0x08) {
if (cmd_pos > 0) {
cmd_pos--;
terminal_handle_input(key);
terminal_render();
}
} else if (key >= 32 && key <= 126) {
cmd_buffer[cmd_pos++] = (char)key;
terminal_handle_input(key);
terminal_render();
}
}
/* Small delay */
for (volatile int i = 0; i < 5000; i++) {}
}
}