Project restructure (no new code added)

Moved all header files into include/ and removed their C counterparts -> code inside .h files
Also added a kernel.ld linker file
since we don't have many code files in src/ now, i removed src/kernel/
only real code change is that i moved memcopy() and memset() into string.h from system.h
This commit is contained in:
2024-04-14 18:27:53 +02:00
parent 60cca470b4
commit 90ac82eebc
25 changed files with 204 additions and 273 deletions
+5 -8
View File
@@ -9,25 +9,22 @@ echo "Assembling ./src/boot.asm..."
nasm -fbin ./src/boot.asm -o ./build/boot.bin nasm -fbin ./src/boot.asm -o ./build/boot.bin
# Kernel # Kernel
echo "Assembling ./src/kernel/entry.asm..." echo "Assembling ./src/entry.asm..."
nasm -felf32 ./src/kernel/entry.asm -o ./build/entry.o nasm -felf32 ./src/entry.asm -o ./build/entry.o
for file in ./src/kernel/*.c; do for file in ./src/*.c; do
filename=$(basename "$file" .c) filename=$(basename "$file" .c)
echo "Compiling $file..." echo "Compiling $file..."
i386-elf-gcc -ffreestanding -Wall -Wextra -m32 -c "$file" -o "./build/$filename.o" i386-elf-gcc -ffreestanding -Wall -Wextra -m32 -Iinclude -c "$file" -o "./build/$filename.o"
done done
echo "Linking kernel object files..." i386-elf-ld -o ./build/kernel.bin -Tkernel.ld --oformat binary ./build/*.o
mv ./build/disk.o ./build/f_disk.o # Very temporary fix to issue with disk.o not linking or something weird
i386-elf-ld -o ./build/kernel.bin -Ttext 0x1000 --oformat binary ./build/*.o
# OS Image # OS Image
echo "Creating kern.iso disk image..." echo "Creating kern.iso disk image..."
cat build/boot.bin build/kernel.bin > build/os.bin cat build/boot.bin build/kernel.bin > build/os.bin
dd if=/dev/zero of=./kern.iso bs=512 count=2880 dd if=/dev/zero of=./kern.iso bs=512 count=2880
dd if=./build/os.bin of=./kern.iso conv=notrunc dd if=./build/os.bin of=./kern.iso conv=notrunc
dd if=./fs/test.txt of=./kern.iso bs=512 seek=21 conv=notrunc # We skip 21 (10.5kB) because that's the bootloader (0.5kB) and kernel (10kB)
echo -e "\nDone!\n" echo -e "\nDone!\n"
-1
View File
@@ -1 +0,0 @@
This is a test file.
+6 -1
View File
@@ -1,4 +1,6 @@
#include "disk.h" #ifndef DRIVE_H
#define DRIVE_H
#include "stdint.h" #include "stdint.h"
#include "system.h" #include "system.h"
@@ -7,6 +9,8 @@
#define WRITE_COMMAND 0x30 #define WRITE_COMMAND 0x30
#define CACHE_FLUSH 0xE7 #define CACHE_FLUSH 0xE7
extern uint8_t driveNum;
void diskRead(uint32_t lbaAddress, uint8_t sectorCount, char* dst) { void diskRead(uint32_t lbaAddress, uint8_t sectorCount, char* dst) {
outb(PIO_PORT + 6, ((lbaAddress >> 24) & 0x0f) | 0xe0); // 6 - Use LBA; 4 - Drive number; 3-0 - Highest lba address nibble outb(PIO_PORT + 6, ((lbaAddress >> 24) & 0x0f) | 0xe0); // 6 - Use LBA; 4 - Drive number; 3-0 - Highest lba address nibble
outb(PIO_PORT + 2, sectorCount); // Sector count register outb(PIO_PORT + 2, sectorCount); // Sector count register
@@ -56,3 +60,4 @@ void diskWrite(uint32_t lbaAddress, uint8_t sectorCount, char* src) {
while (inb(PIO_PORT + 7) & (1 << 7)); // Poll status register untill BSY bit is clear while (inb(PIO_PORT + 7) & (1 << 7)); // Poll status register untill BSY bit is clear
} }
#endif
+15 -1
View File
@@ -1,4 +1,6 @@
#include "idt.h" #ifndef IDT_H
#define IDT_H
#include "stdint.h" #include "stdint.h"
#include "system.h" #include "system.h"
@@ -18,6 +20,8 @@ struct idtPointer {
struct idtEntry idt[256] = { 0 }; struct idtEntry idt[256] = { 0 };
struct idtPointer idtp; struct idtPointer idtp;
extern void idtLoad();
void idtSetGate(uint8_t i, uint32_t offset, uint16_t selector, uint8_t typeAttr) { void idtSetGate(uint8_t i, uint32_t offset, uint16_t selector, uint8_t typeAttr) {
idt[i].offsetLow = (uint16_t)offset; idt[i].offsetLow = (uint16_t)offset;
idt[i].selector = selector; idt[i].selector = selector;
@@ -33,3 +37,13 @@ void setupIdt() {
idtLoad(); idtLoad();
} }
// Contains values of registers and exception info from isrs
struct regs {
unsigned int gs, fs, es, ds;
unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned int intNo, errCode;
unsigned int eip, cs, eflags, useresp, ss;
};
#endif
+35 -16
View File
@@ -1,9 +1,28 @@
#include "irq.h" #ifndef IRQ_H
#define IRQ_H
#include "stdint.h" #include "stdint.h"
#include "system.h" #include "idt.h"
void* irqRoutines[16] = { 0 }; void* irqRoutines[16] = { 0 };
extern void irq0();
extern void irq1();
extern void irq2();
extern void irq3();
extern void irq4();
extern void irq5();
extern void irq6();
extern void irq7();
extern void irq8();
extern void irq9();
extern void irq10();
extern void irq11();
extern void irq12();
extern void irq13();
extern void irq14();
extern void irq15();
void installIrqHandler(uint32_t irq, void (*handler)(struct regs* r)) { void installIrqHandler(uint32_t irq, void (*handler)(struct regs* r)) {
irqRoutines[irq] = handler; irqRoutines[irq] = handler;
} }
@@ -12,20 +31,6 @@ void uninstallIrqHandler(uint32_t irq) {
irqRoutines[irq] = 0; irqRoutines[irq] = 0;
} }
// I have no clue how or what this does, but it makes everything work
void irqRemap(void) {
outb(0xa0, 0x11);
outb(0x20, 0x11);
outb(0x21, 0x20);
outb(0xa1, 0x28);
outb(0x21, 0x04);
outb(0xa1, 0x02);
outb(0x21, 0x01);
outb(0xa1, 0x01);
outb(0x21, 0x0);
outb(0xa1, 0x0);
}
void irqHandler(struct regs* r) { void irqHandler(struct regs* r) {
void (*handler)(struct regs* r); void (*handler)(struct regs* r);
@@ -37,6 +42,19 @@ void irqHandler(struct regs* r) {
outb(0x20, 0x20); outb(0x20, 0x20);
} }
void irqRemap(void) {
outb(0xa0, 0x11);
outb(0x20, 0x11);
outb(0x21, 0x20);
outb(0xa1, 0x28);
outb(0x21, 0x04);
outb(0xa1, 0x02);
outb(0x21, 0x01);
outb(0xa1, 0x01);
outb(0x21, 0x0);
outb(0xa1, 0x0);
}
void setupIrqs() { void setupIrqs() {
irqRemap(); irqRemap();
@@ -59,3 +77,4 @@ void setupIrqs() {
idtSetGate(47, (unsigned)irq15, 0x08, 0x8e); idtSetGate(47, (unsigned)irq15, 0x08, 0x8e);
} }
#endif
+39 -1
View File
@@ -1,4 +1,6 @@
#include "isrs.h" #ifndef ISRS_H
#define ISRS_H
#include "idt.h" #include "idt.h"
#include "screen.h" #include "screen.h"
#include "system.h" #include "system.h"
@@ -39,6 +41,41 @@ char* exceptionMessages[] = {
"Reserved", "Reserved",
}; };
// Repetative isr definitions
extern void isr0();
extern void isr1();
extern void isr2();
extern void isr3();
extern void isr4();
extern void isr5();
extern void isr6();
extern void isr7();
extern void isr8();
extern void isr9();
extern void isr10();
extern void isr11();
extern void isr12();
extern void isr13();
extern void isr14();
extern void isr15();
extern void isr16();
extern void isr17();
extern void isr18();
extern void isr19();
extern void isr20();
extern void isr21();
extern void isr22();
extern void isr23();
extern void isr24();
extern void isr25();
extern void isr26();
extern void isr27();
extern void isr28();
extern void isr29();
extern void isr30();
extern void isr31();
void setupIsrs() { void setupIsrs() {
idtSetGate(0, (unsigned)isr0, 0x08, 0x8e); idtSetGate(0, (unsigned)isr0, 0x08, 0x8e);
idtSetGate(1, (unsigned)isr1, 0x08, 0x8e); idtSetGate(1, (unsigned)isr1, 0x08, 0x8e);
@@ -88,3 +125,4 @@ void faultHandler(struct regs* r) {
} }
} }
#endif
+20 -1
View File
@@ -1,4 +1,6 @@
#include "keyboard.h" #ifndef KEYBOARD_H
#define KEYBOARD_H
#include "irq.h" #include "irq.h"
#include "idt.h" #include "idt.h"
#include "system.h" #include "system.h"
@@ -6,6 +8,22 @@
#include "stdint.h" #include "stdint.h"
#include "stdbool.h" #include "stdbool.h"
// Keycodes
#define LEFT_SHIFT_PRESS 0x2a
#define LEFT_SHIFT_RELEASE (LEFT_SHIFT_PRESS | 0x80)
#define RIGHT_SHIFT_PRESS 0x36
#define RIGHT_SHIFT_RELEASE (RIGHT_SHIFT_PRESS | 0x80)
#define LEFT_CTRL_PRESS 0x1D
#define LEFT_CTRL_RELEASE (LEFT_CTRL_PRESS | 0x80)
#define RIGHT_CTRL_PRESS 0x1D
#define RIGHT_CTRL_RELEASE (RIGHT_CTRL_PRESS | 0x80)
#define LEFT_ALT_PRESS 0x38
#define LEFT_ALT_RELEASE (LEFT_ALT_PRESS | 0x80)
#define RIGHT_ALT_PRESS 0x38
#define RIGHT_ALT_RELEASE (RIGHT_ALT_PRESS | 0x80)
#define MAX_BUFFER_SIZE 256
const char US_Keyboard[2][128] = { const char US_Keyboard[2][128] = {
{ {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
@@ -76,3 +94,4 @@ void setupKeyboard() {
installIrqHandler(1, keyboardHandler); installIrqHandler(1, keyboardHandler);
} }
#endif
+30 -13
View File
@@ -1,6 +1,21 @@
#ifndef SCREEN_H
#define SCREEN_H
#include "screen.h" #include "screen.h"
#include "stdint.h" #include "stdint.h"
#include "system.h" #include "system.h"
#include "string.h"
#define VIDMEM 0xb8000
#define CURSOR_HIGH 14
#define CURSOR_LOW 15
#define CTRLREG 0x3d4
#define DATAREG 0x3d5
// 80x25 VGA Text mode
#define ROWS 25
#define COLS 80
#define CHARS (ROWS*COLS)
uint32_t cursor = 0; uint32_t cursor = 0;
@@ -12,6 +27,19 @@ void updateCursor() {
outb(0x3d5, cursor >> 8); outb(0x3d5, cursor >> 8);
} }
void setCursorPos(uint32_t offset) {
cursor = offset;
updateCursor();
}
uint32_t getCursorPos() {
return cursor;
}
uint32_t calcOffset(uint8_t col, uint8_t row) {
return row * COLS + col;
}
void scroll() { void scroll() {
if (cursor < COLS * ROWS) return; if (cursor < COLS * ROWS) return;
@@ -93,19 +121,6 @@ void clear(uint8_t attr) {
updateCursor(); updateCursor();
} }
void setCursorPos(uint32_t offset) {
cursor = offset;
updateCursor();
}
uint32_t getCursorPos() {
return cursor;
}
uint32_t calcOffset(uint8_t col, uint8_t row) {
return row * COLS + col;
}
void enableCursor(uint8_t start, uint8_t end) { void enableCursor(uint8_t start, uint8_t end) {
outb(0x3d4, 0x0a); // Sending cursor start outb(0x3d4, 0x0a); // Sending cursor start
outb(0x3d5, (inb(0x3d5) & 0xc0) | start); outb(0x3d5, (inb(0x3d5) & 0xc0) | start);
@@ -119,3 +134,5 @@ void disableCursor() {
outb(0x3d4, 0x0b); outb(0x3d4, 0x0b);
} }
#endif
+26 -8
View File
@@ -1,4 +1,6 @@
#include "string.h" #ifndef STRING_H
#define STRING_H
#include "stdint.h" #include "stdint.h"
// Source: http://www.strudel.org.uk/itoa/ // Source: http://www.strudel.org.uk/itoa/
@@ -36,6 +38,13 @@ int isSpace(char c) {
} }
// Some implementations taken from https://en.wikibooks.org/wiki/C_Programming/String_manipulation // Some implementations taken from https://en.wikibooks.org/wiki/C_Programming/String_manipulation
char* strcpy(char* str1, const char* str2) {
char* dst = str1;
const char* src = str2;
while ((*dst++ = *src++) != '\0');
return str1;
}
char* strcat(char* str1, const char* str2) { char* strcat(char* str1, const char* str2) {
char* s = str1; char* s = str1;
while (*s != '\0') { s++; } while (*s != '\0') { s++; }
@@ -61,16 +70,25 @@ int strcmp(const char* str1, const char* str2) {
return ((c1 < c2) ? -1 : (c1 > c2)); return ((c1 < c2) ? -1 : (c1 > c2));
} }
char* strcpy(char* str1, const char* str2) {
char* dst = str1;
const char* src = str2;
while ((*dst++ = *src++) != '\0');
return str1;
}
uint32_t strlen(const char* str) { uint32_t strlen(const char* str) {
const char* p = str; const char* p = str;
while (*++p != '\0'); while (*++p != '\0');
return (uint32_t)(p - str); return (uint32_t)(p - str);
} }
// Memory functions
char* memcopy(char* src, char* dst, uint16_t length) {
for (int i = 0; i < length; i++) {
*(dst + i) = *(src + i);
}
return dst;
}
char* memset(char* dst, char value, uint16_t length) {
for (int i = 0; i < length; i++) {
*(dst + i) = value;
}
return dst;
}
#endif
+6 -14
View File
@@ -1,6 +1,9 @@
#include "system.h" #ifndef SYSTEM_H
#define SYSTEM_H
#include "stdint.h" #include "stdint.h"
// Port functions
uint8_t inb(uint16_t port) { uint8_t inb(uint16_t port) {
uint8_t result; uint8_t result;
asm volatile("inb %1, %0" : "=a"(result) : "Nd"(port)); asm volatile("inb %1, %0" : "=a"(result) : "Nd"(port));
@@ -21,20 +24,8 @@ void outw(uint16_t port, uint16_t data) {
asm volatile("outw %0, %1" :: "a"(data), "Nd"(port)); asm volatile("outw %0, %1" :: "a"(data), "Nd"(port));
} }
char* memcopy(char* src, char* dst, uint16_t length) {
for (int i = 0; i < length; i++) {
*(dst + i) = *(src + i);
}
return dst;
}
char* memset(char* dst, char value, uint16_t length) {
for (int i = 0; i < length; i++) {
*(dst + i) = value;
}
return dst;
}
// Assembly stuff
void cli() { void cli() {
asm volatile("cli"); asm volatile("cli");
} }
@@ -47,3 +38,4 @@ void hlt() {
asm volatile("hlt"); asm volatile("hlt");
} }
#endif
+8 -2
View File
@@ -1,9 +1,13 @@
#include "timer.h" #ifndef TIMER_H
#define TIMER_H
#include "stdint.h" #include "stdint.h"
#include "idt.h" #include "idt.h"
#include "irq.h" #include "irq.h"
#include "system.h" #include "system.h"
#define INPUT_CLK 1193180
uint32_t timerTicks = 0; uint32_t timerTicks = 0;
void timerHandler() { void timerHandler() {
@@ -20,7 +24,7 @@ void timerPhase(uint32_t hz) {
outb(0x43, 0x36); // Tell PIT that we're setting channel 0 outb(0x43, 0x36); // Tell PIT that we're setting channel 0
outb(0x40, div & 0xff); // Low byte outb(0x40, div & 0xff); // Low byte
outb(0X40, div >> 8); // High byte outb(0x40, div >> 8); // High byte
} }
void sleep(uint32_t ticks) { void sleep(uint32_t ticks) {
@@ -29,3 +33,5 @@ void sleep(uint32_t ticks) {
while (timerTicks != eticks); // Should avoid overflows unlike '<' while (timerTicks != eticks); // Should avoid overflows unlike '<'
} }
#endif
+14
View File
@@ -0,0 +1,14 @@
SECTIONS {
.text 0x1000 : {
*(.text*);
}
.data : {
*(.data*);
}
.rodata : {
*(.rodata*);
}
.bss : {
*(.bss*);
}
}
View File
-11
View File
@@ -1,11 +0,0 @@
#ifndef DRIVE_H
#define DRIVE_H
#include "stdint.h"
extern uint8_t driveNum;
void diskRead(uint32_t lbaAddress, uint8_t sectorCount, char* dst);
void diskWrite(uint32_t lbaAddress, uint8_t sectorCount, char* src);
#endif
-19
View File
@@ -1,19 +0,0 @@
#ifndef IDT_H
#define IDT_H
#include "stdint.h"
void setupIdt();
void idtSetGate(uint8_t i, uint32_t offset, uint16_t selector, uint8_t typeAttr);
extern void idtLoad();
// Contains values of registers and exception info from isrs
struct regs {
unsigned int gs, fs, es, ds;
unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned int intNo, errCode;
unsigned int eip, cs, eflags, useresp, ss;
};
#endif
-29
View File
@@ -1,29 +0,0 @@
#ifndef IRQ_H
#define IRQ_H
#include "stdint.h"
#include "idt.h"
void setupIrqs();
void installIrqHandler(uint32_t irq, void (*handler)(struct regs* r));
void uninstallIrqHandler(uint32_t irq);
void irqHandler(struct regs* r);
extern void irq0();
extern void irq1();
extern void irq2();
extern void irq3();
extern void irq4();
extern void irq5();
extern void irq6();
extern void irq7();
extern void irq8();
extern void irq9();
extern void irq10();
extern void irq11();
extern void irq12();
extern void irq13();
extern void irq14();
extern void irq15();
#endif
-40
View File
@@ -1,40 +0,0 @@
#ifndef ISRS_H
#define ISRS_H
void setupIsrs();
// Repetative isr definitions
extern void isr0();
extern void isr1();
extern void isr2();
extern void isr3();
extern void isr4();
extern void isr5();
extern void isr6();
extern void isr7();
extern void isr8();
extern void isr9();
extern void isr10();
extern void isr11();
extern void isr12();
extern void isr13();
extern void isr14();
extern void isr15();
extern void isr16();
extern void isr17();
extern void isr18();
extern void isr19();
extern void isr20();
extern void isr21();
extern void isr22();
extern void isr23();
extern void isr24();
extern void isr25();
extern void isr26();
extern void isr27();
extern void isr28();
extern void isr29();
extern void isr30();
extern void isr31();
#endif
-25
View File
@@ -1,25 +0,0 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "stdint.h"
// Keycodes
#define LEFT_SHIFT_PRESS 0x2a
#define LEFT_SHIFT_RELEASE (LEFT_SHIFT_PRESS | 0x80)
#define RIGHT_SHIFT_PRESS 0x36
#define RIGHT_SHIFT_RELEASE (RIGHT_SHIFT_PRESS | 0x80)
#define LEFT_CTRL_PRESS 0x1D
#define LEFT_CTRL_RELEASE (LEFT_CTRL_PRESS | 0x80)
#define RIGHT_CTRL_PRESS 0x1D
#define RIGHT_CTRL_RELEASE (RIGHT_CTRL_PRESS | 0x80)
#define LEFT_ALT_PRESS 0x38
#define LEFT_ALT_RELEASE (LEFT_ALT_PRESS | 0x80)
#define RIGHT_ALT_PRESS 0x38
#define RIGHT_ALT_RELEASE (RIGHT_ALT_PRESS | 0x80)
#define MAX_BUFFER_SIZE 256
void setupKeyboard();
char* read(char terminator, uint8_t attr);
#endif
-32
View File
@@ -1,32 +0,0 @@
#ifndef SCREEN_H
#define SCREEN_H
#include "stdint.h"
#define VIDMEM 0xb8000
#define CURSOR_HIGH 14
#define CURSOR_LOW 15
#define CTRLREG 0x3d4
#define DATAREG 0x3d5
// 80x25 VGA Text mode
#define ROWS 25
#define COLS 80
#define CHARS (ROWS*COLS)
void putc(char c, uint8_t attr);
void print(char* s, uint8_t attr);
void putcAt(char c, uint8_t attr, uint8_t row, uint8_t col);
void printAt(char* s, uint8_t attr, uint8_t row, uint8_t col);
void clear(uint8_t attr);
void printHex(uint8_t d, uint8_t attr);
void setCursorPos(uint32_t offset);
uint32_t getCursorPos();
uint32_t calcOffset(uint8_t row, uint8_t col);
void scroll();
void enableCursor(uint8_t start, uint8_t end);
void disableCursor();
#endif
-17
View File
@@ -1,17 +0,0 @@
#ifndef STRING_H
#define STRING_H
#include "stdint.h"
char* itoa(uint32_t val, uint8_t base);
uint32_t atoi(const char* str);
int isSpace(char c);
char* strcat(char* str1, const char* str2);
char* strchr(const char* str, char c);
int strcmp(const char* str1, const char* str2);
char* strcpy(char* str1, const char* str2);
uint32_t strlen(const char* str);
#endif
-22
View File
@@ -1,22 +0,0 @@
#ifndef SYSTEM_H
#define SYSTEM_H
#include "stdint.h"
// Port functions
uint8_t inb(uint16_t port);
void outb(uint16_t port, uint8_t data);
uint16_t inw(uint16_t port);
void outw(uint16_t port, uint16_t data);
// Memory functions
char* memcopy(char* src, char* dst, uint16_t length);
char* memset(char* dst, char data, uint16_t length);
// Assembly stuff
void cli();
void sti();
void hlt();
#endif
-12
View File
@@ -1,12 +0,0 @@
#ifndef TIMER_H
#define TIMER_H
#include "stdint.h"
#define INPUT_CLK 1193180
void setupTimer();
void sleep(uint32_t ticks);
void timerPhase(uint32_t hz);
#endif