Basic shell

This commit is contained in:
Mathieu Maret 2021-11-06 00:13:40 +01:00
parent ec9d255b1c
commit d3a7ab59b7
9 changed files with 118 additions and 7 deletions

View File

@ -380,8 +380,6 @@ void kmain(unsigned long magic, unsigned long addr)
pagingSetup(firstUsedByMem, lastUsedByMem); pagingSetup(firstUsedByMem, lastUsedByMem);
VGAMap(); VGAMap();
printf("[Setup] IRQ handlers\n");
irqSetRoutineWrapped(IRQ_KEYBOARD, keyboard_do_irq);
printf("[Setup] HW interrupts\n"); printf("[Setup] HW interrupts\n");
// Enabling the HW interrupts // Enabling the HW interrupts
@ -394,6 +392,10 @@ void kmain(unsigned long magic, unsigned long addr)
printf("[Setup] allocation system\n"); printf("[Setup] allocation system\n");
areaInit(firstUsedByMem, lastUsedByMem, _stack_bottom, _stack_top); areaInit(firstUsedByMem, lastUsedByMem, _stack_bottom, _stack_top);
printf("[Setup] IRQ handlers\n");
keyboardSetup();
irqSetRoutineWrapped(IRQ_KEYBOARD, keyboard_do_irq);
mmuContextSetup(); mmuContextSetup();
cpu_context_subsystem_setup(); cpu_context_subsystem_setup();

View File

@ -1,3 +1,4 @@
#include "keyboard.h"
#include "klibc.h" #include "klibc.h"
#include "stdarg.h" #include "stdarg.h"
#include "syscall.h" #include "syscall.h"
@ -24,6 +25,9 @@ int syscallExecute(int syscallId, const struct cpu_state *userCtx){
ret = syscallGet1arg(userCtx, &c); ret = syscallGet1arg(userCtx, &c);
putc(c); putc(c);
break; break;
case SYSCALL_ID_READ:
ret = keyboardRead();
break;
default: default:
printf("Unknon syscall id %d\n", syscallId); printf("Unknon syscall id %d\n", syscallId);
ret = -ENOENT; ret = -ENOENT;

View File

@ -6,6 +6,7 @@
#define SYSCALL_ID_EXIT 1 #define SYSCALL_ID_EXIT 1
#define SYSCALL_ID_YOLO 2 #define SYSCALL_ID_YOLO 2
#define SYSCALL_ID_PUTC 3 #define SYSCALL_ID_PUTC 3
#define SYSCALL_ID_READ 4
#ifdef __KERNEL__ #ifdef __KERNEL__
int syscallExecute(int syscallId, const struct cpu_state *user_ctx); int syscallExecute(int syscallId, const struct cpu_state *user_ctx);

View File

@ -1,6 +1,18 @@
#include "keyboard.h" #include "keyboard.h"
#include "io.h" #include "io.h"
#include "klibc.h" #include "klibc.h"
#include "ringbuffer.h"
#define KEYBOARD_BUFFER_SIZE 128
static ringbuffer_t buf;
int keyboardSetup()
{
buf = ringbufferCreate(KEYBOARD_BUFFER_SIZE);
return 0;
}
const char *scancode[128] = { const char *scancode[128] = {
/* 0 */ 0, /* 0 */ 0,
@ -308,6 +320,18 @@ void keyboard_do_irq()
} }
} }
if (key) if (key){
printf(key); printf(key);
ringbufferEnqueue(buf, *key);
}
}
char keyboardRead()
{
uint8_t c;
if (ringbufferDequeue(buf, &c)){
return c;
}
return 0;
} }

View File

@ -6,3 +6,6 @@
#define KEYBOARD_DATA_PORT 0x60 #define KEYBOARD_DATA_PORT 0x60
void keyboard_do_irq(); void keyboard_do_irq();
int keyboardSetup();
char keyboardRead();

View File

@ -6,6 +6,7 @@
#define SYSCALL_ID_EXIT 1 #define SYSCALL_ID_EXIT 1
#define SYSCALL_ID_YOLO 2 #define SYSCALL_ID_YOLO 2
#define SYSCALL_ID_PUTC 3 #define SYSCALL_ID_PUTC 3
#define SYSCALL_ID_READ 4
#ifdef __KERNEL__ #ifdef __KERNEL__
int syscallExecute(int syscallId, const struct cpu_state *user_ctx); int syscallExecute(int syscallId, const struct cpu_state *user_ctx);

View File

@ -512,3 +512,38 @@ void yolo()
{ {
syscall0(SYSCALL_ID_YOLO); syscall0(SYSCALL_ID_YOLO);
} }
char readc()
{
return syscall0(SYSCALL_ID_READ);
}
char readcBlock()
{
char c = 0;
do {
c = readc();
} while (c == 0);
return c;
}
int readline(char *buf, int size)
{
int i = 0;
for (; i < size - 1; i++) {
char key = readcBlock();
if (key == '\n')
break;
if(key == '\b' && i>=1){
buf[i-1] = '\0';
i-=2;
}
buf[i] = key;
}
buf[i] = '\0';
return i == (size-1);
}

View File

@ -37,3 +37,6 @@ int syscall0(int id);
void _exit(int status); void _exit(int status);
void yolo(); void yolo();
char readc();
char readcBlock();
int readline(char *buf, int size);

View File

@ -1,12 +1,50 @@
#include "libc.h" #include "libc.h"
int func_yolo()
{
yolo();
return 0;
}
int func_help()
{
printf("\nAvailable Commands:\n");
printf(" yolo\n");
printf(" suicide\n");
printf(" help\n");
return 0;
}
int func_suicide()
{
printf("User is about to suicide\n");
int *yolo = 0;
*yolo = 1;
return 0;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
yolo(); char buf[64];
printf("User is about to suicide\n"); printf("Shell staring. type \"help\" for help\n");
int *yolo =0; while (1) {
*yolo = 1; printf(">");
if (readline(buf, sizeof(buf)))
continue;
if (strcmp(buf, "yolo") == 0) {
func_yolo();
continue;
}
if (strcmp(buf, "help") == 0) {
func_help();
continue;
}
if (strcmp(buf, "suicide") == 0) {
func_suicide();
continue;
}
}
return 0; return 0;
} }