Major refactoring, added Makefile

This commit is contained in:
Nocturn9x 2022-11-17 21:19:36 +01:00
parent 0193c26413
commit b812a9899f
14 changed files with 115 additions and 46 deletions

.gitignore vendored
View File

@ -58,7 +58,6 @@ CMakeCache.txt

.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,17 @@
"configurations": [
"name": "Linux",
"includePath": [
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
"version": 4

Makefile Normal file
View File

@ -0,0 +1,91 @@
# Note: Your cross-compilation toolchain must be in PATH for this
# to work. Change these variables if it isn't (it should be, trust me)
CC := i386-elf-gcc
GDB := i386-elf-gdb
LD := i386-elf-ld
AS := nasm
IDIR := include
SRCDIR := src
BUILDDIR := build
OBJDIR := obj
DISTDIR := dist
CFLAGS := -g -I $(IDIR) -Wall -pedantic -Wno-builtin-declaration-mismatch -ffreestanding
KERNEL_SOURCES := $(shell find src/kernel -name '*.c' -type f)
KERNEL_OBJS := $(subst src/,$(OBJDIR)/,$(KERNEL_OBJS))
DRIVERS_SOURCES := $(shell find src/kernel/drivers -name '*.c' -type f)
DRIVERS_OBJS := $(subst src/,$(OBJDIR)/,$(DRIVERS_OBJS))
BOOTLOADER_SOURCES := $(shell find src/boot -name '*.s' -type f)
BUILD_PATHS := $(subst src/,$(OBJDIR),$(dir $(KERNEL_OBJS)))
BUILD_PATHS += $(subst src/,$(OBJDIR),$(dir $(DRIVERS_OBJS)))
mkdir -p $(BUILD_PATHS)
rm -rf obj/
rm -rf build/
rm -rf dist/
# Produce obj/path/to/file.o in the ELF format
# if src/path/to/file.s exists
$(OBJDIR)/%.o: prepare $(SRCDIR)/%.s
$(AS) -f elf $(word 2,$^) -o $@
# Produce build/path/to/file.bin in the binary
# format if src/path/to/file.s exists
$(BUILDDIR)/%.bin: prepare $(SRCDIR)/%.s
$(AS) -f bin $(word 2,$^) -o $@
# Special case for our master boot record
$(BUILDDIR)/mbr.bin: prepare $(SRCDIR)/boot/mbr.s
$(AS) -f bin $(word 2,$^) -o $(BUILDDIR)/mbr.bin
# Produce obj/path/to/file.o using gcc if
# src/path/to/file.c exists
$(OBJDIR)/%.o: prepare $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c $(word 2,$^) -o $@
# Compile kernel drivers
drivers: $(DRIVERS_OBJS)
# Compile and link the kernel
$(BUILDDIR)/kernel.bin: $(OBJDIR)/entrypoint.o $(KERNEL_OBJS) $(DRIVERS_OBJS)
$(LD) -o $@ -Ttext 0x1000 $^ --oformat binary
kernel: $(BUILDDIR)/kernel.bin
# Compile the bootloader
bootloader: $(BUILDDIR)/mbr.bin
# Compile and link everything
all: bootloader drivers kernel
image: $(BUILDDIR)/mbr.bin $(BUILDDIR)/kernel.bin
mkdir -p $(DISTDIR)/
cat $^ > $(DISTDIR)/os.img
run: image
qemu-system-x86_64 -drive format=raw,file=$(DISTDIR)/os.img,index=0,media=disk

View File

@ -1,19 +0,0 @@
mkdir /tmp/src
cd /tmp/src
curl -O
tar xf binutils-2.39.tar.gz
mkdir binutils-build
cd binutils-build
../binutils-2.39/configure --target=$TARGET --enable-interwork --enable-multilib --disable-nls --disable-werror --prefix=$PREFIX 2>&1 | tee configure.log
sudo make all install 2>&1 | tee make.log
cd /tmp/src
curl -O
tar xf gcc-12.2.0.tar.gz
mkdir gcc-build
cd gcc-build
../gcc-12.2.0/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-libssp --enable-languages=c --without-headers
sudo make all-gcc
sudo make all-target-libgcc
sudo make install-gcc
sudo make install-target-libgcc

View File

@ -1,12 +0,0 @@
# Compile the kernel and its drivers
i386-elf-gcc -ffreestanding -I src/ -c src/kernel/main.c -o obj/kernel.o
i386-elf-gcc -ffreestanding -I src/ -c src/kernel/drivers/vga/screen.c -o obj/screen.o
i386-elf-gcc -ffreestanding -I src/ -c src/kernel/drivers/ports/ports.c -o obj/ports.o
# Compile the assembly entry point and the MBR
nasm -f elf src/entrypoint.s -o obj/entry.o
nasm -f bin src/boot/mbr.s -o build/mbr.bin
# Link everything together
i386-elf-ld -o build/kernel.bin -Ttext 0x1000 entry.o kernel.o ports.o screen.o --oformat binary
# Produce a bootable image
cat build/mbr.bin build/kernel.bin > build/os.img

View File

View File

@ -1,6 +0,0 @@
export CC=/usr/bin/gcc
export LD=/usr/bin/gcc
export PREFIX="/usr/local/i386elfgcc"
export TARGET=i386-elf
export PATH=$PATH:$PREFIX/bin

View File

@ -4,6 +4,7 @@
#include "kernel/ktypes.h"
#include "kernel/drivers/ports/ports.h"
#define VMEM_ADDRESS 0xb8000
#define VMEM_BUF ((byte*)VMEM_ADDRESS)
#define MAX_ROWS 25
@ -21,8 +22,8 @@
// Public API
void clearScreen(void);
void kprintAt(byte* message, i32 col, i32 row);
void kprint(byte* message);
void kprintAt(char* message, i32 col, i32 row);
void kprint(char* message);

View File

View File

@ -1,2 +0,0 @@
qemu-system-x86_64 -drive format=raw,file=build/os.bin,index=0,media=disk

View File

@ -1,6 +1,6 @@
// Utilities for writing to and reading from I/O ports
#include "ports.h"
#include "kernel/drivers/ports/ports.h"
// Note: We use the volatile modifier everywhere because

View File

@ -1,5 +1,5 @@
// Implementation of a simple text-only VGA driver
#include "screen.h"
#include "kernel/drivers/vga/screen.h"
#include "kernel/ktypes.h"
@ -13,7 +13,7 @@ i32 getColumn(i32 offset);
// Public API below
void kprintAt(byte* message, i32 col, i32 row) {
void kprintAt(char* message, i32 col, i32 row) {
Prints a null-terminated string to the VGA
text buffer at the specified row and column.
@ -42,7 +42,7 @@ void kprintAt(byte* message, i32 col, i32 row) {
void kprint(byte* message) {
void kprint(char* message) {
Prints a null-terminated string to the
VGA text buffer