Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #10

Merged
merged 6 commits into from
Aug 27, 2024
Merged

Dev #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

BaboscOS: <i>Bare And Basic Open Source Console-based Operating System</i> (insane naming, i know)

Super bare and basic 64bit operating system featuring a limine protocol compliant (higher half) kernel.
## What's this?

I wanted to build an os one day, and so now im finally attempting that. I decided to start off with this [limine template](https://github.com/limine-bootloader/limine-c-template) after reading the osdev wiki for so dang long. I figured it was a better place to start than to write my own bootloader lol (but i do plan on doing that one day).
This is just a tinkering project (so expect thing to be messy)<br />
It's basically a tiny useless 64-bit operating system that only works on x86 arch.
I have some cool ideas for this project, but I'm taking things one at a time, so we'll see where this goes.

I got some cool ideas for this project, but we're taking things one at a time. idk if im going to write seperate docs on this or just really take the time to just write out my thought process in comments but yeah
## What's the reason?

I wanted to build an os, and so now im finally attempting that. I decided to start off with this [limine template](https://github.com/limine-bootloader/limine-c-template) after reading the osdev wiki for a while. I figured it was a better place to start than to write my own bootloader lol (but i do plan on doing that one day).
8 changes: 8 additions & 0 deletions kernel/asm/idt.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; This is just the assembly refrence for the load_idt function. It's actually done with inline assembly in "../src/drivers/idt.c"

section .text
global load_idt

load_idt:
lidt [rdi]
ret
24 changes: 24 additions & 0 deletions kernel/include/drivers/idt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef IDT_H
#define IDT_H

#include <stdint.h>

struct idt_entry {
uint16_t offset_low;
uint16_t selector;
uint8_t ist;
uint8_t type_attr;
uint16_t offset_mid;
uint32_t offset_high;
uint32_t zero;
} __attribute__((packed));

struct idt_ptr {
uint16_t limit;
uint64_t base;
} __attribute__((packed));

void idt_set_gate(int n, uint64_t handler);
void idt_install();

#endif
9 changes: 9 additions & 0 deletions kernel/include/drivers/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef IO_H
#define IO_H

#include <stdint.h>

uint8_t inb(uint16_t port);
void outb(uint16_t port, uint8_t data);

#endif
7 changes: 7 additions & 0 deletions kernel/include/drivers/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H

void keyboard_init();
void keyboard_interrupt_handler();

#endif
6 changes: 3 additions & 3 deletions kernel/include/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static const uint8_t font[96][8] = {
{0x03,0x06,0x0C,0x18,0x30,0x60,0x40,0x00}, // '\'
{0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00}, // ']'
{0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00}, // '^'
{0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // '_'
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF}, // '_'
{0x0C,0x18,0x30,0x00,0x00,0x00,0x00,0x00}, // '`'
{0x00,0x00,0x3E,0x60,0x7E,0x63,0x7E,0x00}, // 'a'
{0x07,0x06,0x3E,0x66,0x66,0x66,0x3B,0x00}, // 'b'
Expand Down Expand Up @@ -96,10 +96,10 @@ static const uint8_t font[96][8] = {
{0x00,0x00,0x63,0x36,0x1C,0x36,0x63,0x00}, // 'x'
{0x00,0x00,0x63,0x63,0x63,0x7E,0x60,0x3F}, // 'y'
{0x00,0x00,0x7F,0x30,0x18,0x06,0x7F,0x00}, // 'z'
{0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18}, // '|'
{0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00}, // '{'
{0x00,0x00,0x00,0x76,0xDC,0x00,0x00,0x00}, // '~'
{0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18}, // '|'
{0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00}, // '}'
{0x00,0x00,0x00,0x76,0xDC,0x00,0x00,0x00}, // '~'
};

// TODO: reverse bits so that the characters aren't flipped
Expand Down
2 changes: 2 additions & 0 deletions kernel/include/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct terminal {
int scale;
};

extern struct terminal term;

void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer, uint32_t color, int scale);
void terminal_edit(struct terminal *term, uint32_t color, int scale);
void terminal_write(struct terminal *term, const char *str);
Expand Down
39 changes: 39 additions & 0 deletions kernel/src/drivers/idt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../../include/drivers/idt.h"
#include <string.h>

#define IDT_SIZE 256

extern void keyboard_interrupt_handler();

struct idt_entry idt[IDT_SIZE];
struct idt_ptr idt_ptr;

struct terminal* global_term;

void idt_set_gate(int n, uint64_t handler) {
idt[n].offset_low = handler & 0xFFFF;
idt[n].selector = 0x08; // kernel code segment
idt[n].ist = 0;
idt[n].type_attr = 0x8E; // interrupt gate
idt[n].offset_mid = (handler >> 16) & 0xFFFF;
idt[n].offset_high = (handler >> 32) & 0xFFFFFFFF;
idt[n].zero = 0;
}

void load_idt(struct idt_ptr* idt_ptr) {
asm volatile ("lidt (%0)" : : "r"(idt_ptr)); // load idt
asm volatile ("sti"); // enable interrupts
}

void idt_install(struct terminal* term) {
idt_ptr.limit = sizeof(struct idt_entry) * IDT_SIZE - 1;
idt_ptr.base = (uint64_t)&idt;

memset(&idt, 0, sizeof(struct idt_entry) * IDT_SIZE);

global_term = term;

idt_set_gate(33, (uint64_t)keyboard_interrupt_handler); // IRQ1

load_idt(&idt_ptr);
}
14 changes: 14 additions & 0 deletions kernel/src/drivers/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../../include/drivers/io.h"
#include <stdint.h>

// ins
uint8_t inb(uint16_t port) {
uint8_t result;
asm volatile ("inb %1, %0" : "=a"(result) : "Nd"(port));
return result;
}

// outs
void outb(uint16_t port, uint8_t data) {
asm volatile ("outb %0, %1" : : "a"(data), "Nd"(port));
}
72 changes: 72 additions & 0 deletions kernel/src/drivers/keyboard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "../../include/drivers/keyboard.h"
#include "../../include/terminal.h"
#include "../../include/drivers/io.h"
#include <stdint.h>

extern struct terminal term;

#define KEYBOARD_DATA_PORT 0x60
#define KEYBOARD_STATUS_PORT 0x64
#define PIC1_COMMAND 0x20
#define PIC1_DATA 0x21
#define PIC_EOI 0x20

static uint8_t keyboard_map[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0, 0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};

void keyboard_init() {
// unmask the keyboard interrupt (IRQ1)
uint8_t mask = inb(PIC1_DATA);
mask &= ~(1 << 1);
outb(PIC1_DATA, mask);
}

void keyboard_interrupt_handler(struct terminal* term) {
uint8_t status = inb(KEYBOARD_STATUS_PORT);
if (status & 0x01) {
uint8_t keycode = inb(KEYBOARD_DATA_PORT);
if (keycode < 128) {
char c = keyboard_map[keycode];
if (c) {
terminal_write_char(&term, c);
}
}
}
// send End of Interrupt (EOI) signal to PIC
outb(PIC1_COMMAND, PIC_EOI);
}
22 changes: 17 additions & 5 deletions kernel/src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <limine.h>
#include "../include/colors.h"
#include "../include/terminal.h"
#include "../include/drivers/idt.h"
#include "../include/drivers/keyboard.h"

// Set the base revision to 2, this is recommended as this is the latest
// base revision described by the Limine boot protocol specification.
Expand Down Expand Up @@ -63,14 +65,24 @@ void _start(void) {
// Fetch the first framebuffer.
struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0];

// init the terminal
struct terminal term;
terminal_init(&term, framebuffer, COLOR_WHITE, 2);
terminal_init(&term, framebuffer, COLOR_WHITE, 1);
terminal_write(&term, "Terminal initialized...\n");

terminal_write(&term, "BaboscOS booted up successfully!BaboscOS booted up successfully!BaboscOS booted up successfully!");
terminal_edit(&term, COLOR_RED, 3);
terminal_write(&term, "red color and scalesadasdasdsads 3");
idt_install(&term);
terminal_write(&term, "IDT installed...\n");
keyboard_init();
terminal_write(&term, "Keyboard initialized...\n");

terminal_edit(&term, COLOR_WHITE, 1);
terminal_write(&term, "BaboscOS booted up successfully!\n\n\n");
terminal_edit(&term, COLOR_YELLOW, 8);
terminal_write(&term, "BaboscOS");
terminal_edit(&term, COLOR_YELLOW, 2);
terminal_write(&term, " v0.0.1\n\n\n\n");
terminal_edit(&term, COLOR_GREEN, 2);
terminal_write(&term, "user@babosc:~$_");

// We're done, just hang...
hcf();
}
6 changes: 6 additions & 0 deletions kernel/src/terminal.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "../include/terminal.h"
#include "../include/font.h"

struct terminal term;

void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer, uint32_t color, int scale) {
term->framebuffer = framebuffer;
term->start_x = 4;
Expand All @@ -11,6 +13,10 @@ void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer
term->scale = scale;
}

/*
This allows us to change the color and scale of the terminal,
if we change our minds from what we initiallized the terminal with
*/
void terminal_edit(struct terminal *term, uint32_t color, int scale) {
term->color = color;
term->scale = scale;
Expand Down
Loading