Skip to content

Commit

Permalink
adding nanoshell (KFS-2 bonus)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatateDu609 committed Jul 11, 2022
1 parent c5a0eeb commit da7fce3
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 17 deletions.
8 changes: 4 additions & 4 deletions .gdb_history
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
p idt
p /x idt
n
next
n
cont
n
Expand Down Expand Up @@ -254,3 +250,7 @@ n
p formats
p sizeof(formats)
p sizeof(formats) / sizeof(format_t)
b strcmp
cont
n
p str2
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ BASENAME := kernel.c \
system/boot/pic.s \
system/boot/GDT/load.s \
system/boot/GDT/init.c \
system/boot/GDT/print_stack.c \
system/boot/IDT/load.s \
system/boot/IDT/init.c \
system/boot/IDT/isr.c \
Expand All @@ -49,6 +50,13 @@ BASENAME := kernel.c \
system/CPU/mode.c \
system/CPU/interrupts.c \
\
nanoshell/commands.c \
nanoshell/shell.c \
nanoshell/commands/reboot.c \
nanoshell/commands/shutdown.c \
nanoshell/commands/halt.c \
nanoshell/commands/stack.c \
\
libc/string/strlen.c \
libc/string/strcpy.c \
libc/string/strncpy.c \
Expand Down
8 changes: 8 additions & 0 deletions include/CPU/GDT.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@

#define KMCS 0x08 // Kernel Mode, Code Segment


#define GET_STACK_POINTER(x) \
asm("mov %%esp, %0" : "=r" (x));
#define GET_STACK_FRAME(x) \
asm("mov %%ebp, %0" : "=r" (x));

struct gdtr
{
uint16_t limit;
Expand All @@ -64,4 +70,6 @@ struct gdtr

void load_gdt(struct gdtr *gdt);

void print_stack(void *sp, void *sf);

#endif
25 changes: 25 additions & 0 deletions include/nanoshell/commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef NANOSHELL_COMMANDS_H
#define NANOSHELL_COMMANDS_H

#define COMMAND_LENGTH_MAX 256

#define COMMAND_NB 4
#define HALT_COMMAND "halt"
#define STACK_COMMAND "stack"
#define REBOOT_COMMAND "reboot"
#define SHUTDOWN_COMMAND "shutdown"

typedef struct
{
char *name;
void (*handler)(char *args);
} command_t;

extern command_t commands[COMMAND_NB];

void halt(char *args);
void stack(char *args);
void reboot(char *args);
void shutdown(char *args);

#endif
8 changes: 8 additions & 0 deletions include/nanoshell/shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef NANOSHELL_SHELL_H
#define NANOSHELL_SHELL_H

#include "commands.h"

void execute(char *command);

#endif
4 changes: 1 addition & 3 deletions src/kernel.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include <stdbool.h>
#include <stddef.h>

#include "CPU/mode.h"
#include "CPU/GDT.h"
#include "IO/terminal.h"
#include "IO/write.h"
#include "IO/printk.h"

#if defined(__linux__)
#error "You are not using a cross-compiler, you will most likely run into trouble"
Expand Down
7 changes: 6 additions & 1 deletion src/libc/string/strcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

int strcmp(const char *str1, const char *str2)
{
while (*str1 && *str1 == *str2)
while (*str1 && *str2 && *str1 == *str2)
{
if (*str1 != *str2)
return *str1 - *str2;
str1++;
str2++;
}
return *str1 - *str2;
}
8 changes: 8 additions & 0 deletions src/nanoshell/commands.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "nanoshell/shell.h"

command_t commands[] = {
{ "reboot", reboot },
{ "shutdown", shutdown },
{ "halt", halt },
{ "stack", stack },
};
7 changes: 7 additions & 0 deletions src/nanoshell/commands/halt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "nanoshell/commands.h"
#include "utils.h"

void halt(__unused char *args)
{
asm("cli; hlt");
}
17 changes: 17 additions & 0 deletions src/nanoshell/commands/reboot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "nanoshell/commands.h"
#include "CPU/port.h"
#include "utils.h"
#include <stdint.h>


void reboot(__unused char *args)
{
uint8_t good = 0x02;
while (good & 0x02)
good = inb(0x64);
outb(0x64, 0xFE);

loop:
asm volatile("hlt");
goto loop;
}
8 changes: 8 additions & 0 deletions src/nanoshell/commands/shutdown.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "nanoshell/commands.h"
#include "CPU/port.h"
#include "utils.h"

void shutdown(__unused char *args)
{
outw(0x604, 0x2000); // QEMU specific
}
14 changes: 14 additions & 0 deletions src/nanoshell/commands/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "CPU/GDT.h"
#include "nanoshell/commands.h"
#include "utils.h"

void stack(__unused char *args)
{
void *sp;
void *sf;

GET_STACK_POINTER(sp);
GET_STACK_FRAME(sf);

print_stack(sp - 16, sf + 16);
}
17 changes: 17 additions & 0 deletions src/nanoshell/shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <string.h>
#include "nanoshell/shell.h"
#include "IO/printk.h"

void execute(char *command)
{
int i;
for (i = 0; i < COMMAND_NB; i++)
{
if (strcmp(commands[i].name, command) == 0)
{
commands[i].handler(command);
return;
}
}
printk("Unknown command: %s\n", command);
}
23 changes: 14 additions & 9 deletions src/system/IO/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ uint16_t *terminal_buffer = (uint16_t *)0xB8000;
void terminal_motd(void)
{
terminal_setcolor(vga_entry(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK));
terminal_writestring(" ::: :::::::::\n");
printk(" :+: :+: :+:\n");
printk(" +:+ +:+ +:+ \n");
printk(" +#+ +:+ +#+ \n");
printk(" +#+#+#+#+#+ +#+ \n");
printk(" #+# #+# \n");
printk(" ### ########.kfs ");
terminal_setcolor(vga_entry(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_BLACK));
terminal_writestring("By gboucett\n\n");
if (!term_cur)
{
printk(" ::: :::::::::\n");
printk(" :+: :+: :+:\n");
printk(" +:+ +:+ +:+ \n");
printk(" +#+ +:+ +#+ \n");
printk(" +#+#+#+#+#+ +#+ \n");
printk(" #+# #+# \n");
printk(" ### ########.kfs ");
terminal_setcolor(vga_entry(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_BLACK));
printk("By gboucett\n\n");
}
terminal_setcolor(vga_entry(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK));
printk("This is TTY%d\n\n", term_cur);
}

void terminal_initialize(void)
Expand Down
7 changes: 7 additions & 0 deletions src/system/IO/printk/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ int print_hex(va_list *args)
return i;
}

int print_pointer(va_list *arg)
{
terminal_writestring("0x");
return print_hex(arg) + 2;
}

static format_t formats[] = {
{'c', print_char},
{'%', print_percent},
{'s', print_string},
{'u', print_uint},
{'d', print_int},
{'x', print_hex},
{'p', print_pointer},
};

int printk(const char *format, ...)
Expand Down
23 changes: 23 additions & 0 deletions src/system/IO/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include "IO/keyboard.h"
#include <string.h>
#include <ctype.h>
#include "nanoshell/shell.h"

static size_t PS1_end = 0;
static char command[COMMAND_LENGTH_MAX];
static int index = 0;

static void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{
Expand Down Expand Up @@ -63,7 +66,27 @@ static void terminal_screen(unsigned char c)
void terminal_entry(unsigned char c)
{
if (isprint(c) || c == '\n' || c == '\b')
{
if (c == '\n')
{
command[index] = '\0';
prompt = false;
terminal_putchar('\n');
execute(command);
prompt = true;
terminal_prompt();
index = 0;
return ;
}
else if (c != '\n' && c != '\b')
command[index++] = c;
else if (c == '\b')
{
if (index > 0)
index--;
}
terminal_putchar(c);
}
if (c == UP || c == DOWN || c == LEFT || c == RIGHT)
terminal_update_cursor(c);
if (IS_F(c))
Expand Down
40 changes: 40 additions & 0 deletions src/system/boot/GDT/print_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "CPU/GDT.h"
#include <stddef.h>
#include "IO/printk.h"
#include "IO/terminal.h"

static void fixed_print(uint8_t c)
{
uint8_t u = c & 0x0F;
uint8_t d = (c & 0xF0) >> 4;
char *hex = "0123456789ABCDEF";

printk("%c%c ", hex[d], hex[u]);
}

void print_stack(void *sp, void *sf)
{
uint8_t color;

// Print a hex dump of the stack.
size_t i = 0;
while (sp < sf)
{
if (i % 16 == 0)
{
color = vga_entry_color(VGA_COLOR_MAGENTA, VGA_COLOR_BLACK);
terminal_setcolor(color);
printk("%p: ", sp);
color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_setcolor(color);
}
fixed_print(*(uint8_t *)sp);
sp++;
i++;
if (i % 16 == 0)
{
terminal_putchar('\n');
}
}
printk("\n\n");
}

0 comments on commit da7fce3

Please sign in to comment.