summaryrefslogtreecommitdiff
path: root/stage3/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'stage3/main.c')
-rw-r--r--stage3/main.c183
1 files changed, 48 insertions, 135 deletions
diff --git a/stage3/main.c b/stage3/main.c
index 5362984..c8ff971 100644
--- a/stage3/main.c
+++ b/stage3/main.c
@@ -14,16 +14,40 @@ void init()
#include "interrupts.h"
#include "pic.h"
#include "io.h"
-#include "memory.h"
#include "ata.h"
-#include "fs.h"
-#include "string.h"
#include "pci.h"
+#include "ps2.h"
+#include "thread.h"
+#include "shell.h"
-void eat_whitespace(char **str)
+char keymap[256] = { '\0' };
+
+void keyboard_handler()
{
- while (**str == ' ' || **str == '\t')
- (*str)++;
+ str buffer = NILS;
+ usize cap = 0;
+ print(S("$ "));
+
+ for (;;) {
+ event *e = yield(nil);
+
+ char c = keymap[e->data.scancode];
+ if (c != '\0') {
+ print_char(c);
+
+ if (c == '\n') {
+ shell_run_cmd(buffer);
+ buffer.len = 0;
+ print(S("$ "));
+ } else {
+ if (buffer.len == cap)
+ buffer.data = realloc(buffer.data, cap = cap ? cap*2 : 1);
+ buffer.data[buffer.len++] = c;
+ }
+ }
+
+ free(e);
+ }
}
void kmain()
@@ -61,142 +85,31 @@ void kmain()
font_load_classic();
// memory map
- print("memory map:\n");
+ print(S("memory map:\n"));
for (usize i = 0; i < n_mreg; i++) {
- print_num((u64) mregs[i].start, 16, 16);
- print(" | ");
- print_num((u64) mregs[i].start + mregs[i].size, 16, 16);
- print(" | ");
- print_num(mregs[i].used, 10, 0);
- print("\n");
+ print_num_pad((u64) mregs[i].start, 16, 16, ' ');
+ print(S(" | "));
+ print_num_pad((u64) mregs[i].start + mregs[i].size, 16, 16, ' ');
+ print(S(" | "));
+ print_dec(mregs[i].used);
+ print(S("\n"));
}
- init_interrupts();
+ interrupts_init();
pic_init();
- ata_init();
-
- file f = fs_read("init");
- if (f.data == nil)
- panic("no init script");
-
- // this is horrible horrible horrible
- // if you read this i am genuinely sorry
-
- char *init = malloc(f.len+1);
- char *init_orig = init;
- init[f.len] = '\0';
- memcpy(init, f.data, f.len);
- free(f.data);
-
- for (;;) {
- eat_whitespace(&init);
+ thread_init();
- if (*init == '\0')
- break;
- else if (*init == '\n') {
- init++;
- continue;
- }
-
- if (strncmp(init, "echo ", strlen("echo ")) == 0) {
- init += strlen("echo ");
- usize idx = find_char(init, '\n');
- printn(init, idx);
- print("\n");
- init += idx;
- } else if (strncmp(init, "cat ", strlen("cat ")) == 0) {
- init += strlen("cat ");
- usize idx = find_char(init, '\n');
-
- char filename[idx+1];
- filename[idx] = '\0';
- memcpy(filename, init, idx);
-
- file f = fs_read(filename);
- if (f.data == nil) {
- print("cat: file not found: ");
- print(filename);
- print("\n");
- } else {
- printn(f.data, f.len);
- free(f.data);
- }
-
- init += idx;
- } else if (strncmp(init, "font ", strlen("font ")) == 0) {
- init += strlen("font ");
- usize idx = find_char(init, '\n');
-
- char filename[idx+1];
- filename[idx] = '\0';
- memcpy(filename, init, idx);
-
- file f = fs_read(filename);
- if (f.data == nil) {
- print("font: file not found: ");
- print(filename);
- print("\n");
- } else {
- if (f.len == 16*256)
- font_load_blob(f.data);
- else
- print("font: file has wrong size\n");
- free(f.data);
- }
+ ata_init();
+ ps2_init();
- init += idx;
- } else if (strncmp(init, "img ", strlen("img ")) == 0) {
- init += strlen("img ");
- usize idx = find_char(init, '\n');
+ shell_run_file(S("init"));
- char filename[idx+1];
- filename[idx] = '\0';
- memcpy(filename, init, idx);
+ thread *keyboard_thread = thread_create(S("keyboard"), &keyboard_handler);
+ irq_services[1] = keyboard_thread;
- file f = fs_read(filename);
- if (f.data == nil) {
- print("img: file not found: ");
- print(filename);
- print("\n");
- } else {
- if (f.len < 2 * sizeof(u32))
- print("img: missing header\n");
- else {
- u32 width = ((u32 *) f.data)[0];
- u32 height = ((u32 *) f.data)[1];
- if (f.len != 2 * sizeof(u32) + width * height * sizeof(color))
- panic("img: invalid file size\n");
- gfx_draw_img(gfx_info->width-width, 0, width, height, f.data + 2 * sizeof(u32));
- }
- free(f.data);
- }
-
- init += idx;
- } else if (strncmp(init, "lspci", strlen("lspci")) == 0) {
- pci_enumerate();
- init += find_char(init, '\n');
- } else if (strncmp(init, "charset_demo", strlen("charset_demo")) == 0) {
- const u8 max = '~' - '!' + 1;
-
- char str[max + 1];
- str[max] = '\0';
-
- for (u8 i = 0; i < max; i++)
- str[i] = i + '!';
-
- print("charset demo:\n");
- print(str);
- print("\n");
- init += find_char(init, '\n');
- } else {
- print("unknown command: ");
- usize idx = find_char(init, '\n');
- printn(init, idx);
- print("\n");
- init += idx;
- }
- }
+ unmask_irq(1);
+ enable_irqs();
- free(init_orig);
- halt();
+ thread_sched(nil, nil);
+ freeze();
}