diff --git a/Makefile b/Makefile deleted file mode 100644 index fea8033..0000000 --- a/Makefile +++ /dev/null @@ -1,100 +0,0 @@ -# Variables -CC := i686-elf-gcc -LD := i686-elf-ld -AS := nasm - -CCFLAGS := -g -std=c17 -ffreestanding -fno-builtin -fno-stack-protector -fno-pie \ - -Os -nostdinc -mgeneral-regs-only -m32 -march=i386 -Wall -LDFLAGS := -z notext -melf_i386 -nostdlib -ASFLAGS := - -include make.config - -BOOT := ./boot -SRC := ./src -BIN := ./bin -LIBK_INC := ./libk/include -LIBC_INC := ./libc/include -LIBC_DIR := ./libc -LIBK_DIR := ./libk -FONT_DIR := ./usr/fonts -LIBK_SRC := $(shell find $(LIBK_DIR) -type f -name "**.c") - -FONTS := testfont term16n - -BOOTLOADER := $(BIN)/stage1.bin $(BIN)/stage2.bin $(BIN)/stage3.bin -KERNEL := $(BIN)/kernel.bin -LIBC := $(BIN)/libc.o -LIBK := $(BIN)/libk.o -ISO := kern.iso - -.PHONY: os clean run - -# Main compilation target -os: $(ISO) -$(ISO): $(BIN) $(BOOTLOADER) $(KERNEL) $(C_FILES) $(FONTS) - cc ./makedisk.c -o ./makedisk - ./makedisk # TODO: List of files - @echo "\nDone!\n" - -$(BIN): - [ -d $(BIN) ] || mkdir $(BIN) - rm -rf $(BIN)/* - -# Bootloader: This target is responsible for creating stage?.bin (1,2,3) -$(BOOTLOADER): $(BOOT)/stage1.asm $(BOOT)/stage2.asm $(BOOT)/stage3.c $(LIBC) $(LIBK) - @echo "Assembling $(BOOT)/stage1.asm..." - $(AS) -fbin -o $(BIN)/stage1.bin $(BOOT)/stage1.asm $(ASFLAGS) - @echo "Assembling $(BOOT)/stage2.asm..." - $(AS) -fbin -o $(BIN)/stage2.bin $(BOOT)/stage2.asm $(ASFLAGS) - @echo "Compiling $(BOOT)/stage3.c..." - $(CC) $(CCFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/stage3.o -c $(BOOT)/stage3.c - $(LD) $(LDFLAGS) -T$(BOOT)/stage3.ld --oformat binary -o $(BIN)/stage3.bin $(BIN)/stage3.o $(LIBC) $(LIBK) - rm $(BIN)/stage3.o - -# Kernel: This target is responsible for creating kernel.bin -$(KERNEL): $(SRC)/kernel.c $(LIBC) #TODO: $(LIBK) - @echo "Compiling $(SRC)/kernel.c..." - $(CC) $(CCFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/kernel.o -c $(SRC)/kernel.c - $(LD) $(LDFLAGS) -T$(SRC)/kernel.ld --oformat binary -o $(BIN)/kernel.bin $(BIN)/kernel.o $(LIBC) $(LIBK) - rm $(BIN)/kernel.o - -# TODO: user programs - -$(LIBC): $(LIBC_DIR)/**.c - @echo "Compiling $(LIBC)..." - mkdir -p $(BIN)/libc - for f in libc/*.c; do\ - filename=$$(basename $$f .c);\ - $(CC) $(CCFLAGS) -I$(LIBC_INC) -o $(BIN)/libc/$$filename.o -c $$f;\ - done - $(LD) $(LDFLAGS) -r -o $(LIBC) $(BIN)/libc/**.o - -$(LIBK): $(LIBK_SRC) - @echo "Compiling $(LIBK)..." - mkdir -p $(BIN)/libk - for f in $(LIBK_SRC); do\ - filename=$$(basename $$f .c);\ - $(CC) $(CCFLAGS) -I$(LIBC_INC) -I$(LIBK_INC) -o $(BIN)/libk/$$filename.o -c $$f;\ - done - $(LD) $(LDFLAGS) -r -o $@ $(BIN)/libk/**.o - - -# This target retrieves all fonts inside src/fonts -$(FONTS): - cp $(FONT_DIR)/$@.fnt $(BIN)/$@.fnt - -# Run QEMU -run: - qemu-system-x86_64\ - -drive format=raw,file=$(ISO),index=0,media=disk\ - -m 256M -accel tcg -monitor stdio\ - -rtc base=localtime,clock=host,driftfix=slew\ - -audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0 - -# Clean all build files -clean: - rm -f kern.iso - rm -rf $(BIN) - rm -f makedisk - diff --git a/boot/makefile b/boot/makefile new file mode 100644 index 0000000..62259bf --- /dev/null +++ b/boot/makefile @@ -0,0 +1,15 @@ +# +# Bootloader +# + +include make.defaults + +bootloader: $(BOOT_DIR)/stage1.asm $(BOOT_DIR)/stage2.asm $(BOOT_DIR)/stage3.c $(LIBC) $(LIBK) + @echo "Assembling $(BOOT_DIR)/stage1.asm..." + $(AS) -fbin -o $(BIN)/stage1.bin $(BOOT_DIR)/stage1.asm $(ASFLAGS) + @echo "Assembling $(BOOT_DIR)/stage2.asm..." + $(AS) -fbin -o $(BIN)/stage2.bin $(BOOT_DIR)/stage2.asm $(ASFLAGS) + @echo "Compiling $(BOOT_DIR)/stage3.c..." + $(CC) $(CFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/boot/stage3.o -c $(BOOT_DIR)/stage3.c + $(LD) $(LDFLAGS) -T$(BOOT_DIR)/stage3.ld --oformat binary -o $(BIN)/stage3.bin $(BIN)/boot/stage3.o $(LIBC) $(LIBK) + diff --git a/docs/fileStructure.txt b/docs/fileStructure.txt index 8871f7a..96d20c3 100644 --- a/docs/fileStructure.txt +++ b/docs/fileStructure.txt @@ -2,11 +2,15 @@ Root contains the Makefile that is used to build kern and some documents. It also contains these directories: - src/ - The source code of kern - include/ - C header files that can be included by the OS. include/c/ can be used by any programs, but other headers shall only be included in the kernel - build/ - This directory is created by the Makefile and contains intermediary built obj file and binaries - other/ - Things that are not nescessarly part of the OS - docs/ - Some basic documentation. Mostly for me so that i don't forget stuff + src/ - Kernel source code + boot/ - Bootloader code + libc/ - C Standard library + libk/ - Kernel library + docs/ - Some documentation (mainly for me) + other/ - Things like screenshot, etc. + usr/ - Userspace files like fonts and programs + bin/ - Intermediary binaries -After running the Makefile and succesfully building the OS a 'kern.iso' file will be created in the root directory +After running the Makefile and succesfully building the OS +a 'kern.iso' file will be created in the root directory diff --git a/kern.iso b/kern.img similarity index 99% rename from kern.iso rename to kern.img index 209d78c..835c4c9 100644 Binary files a/kern.iso and b/kern.img differ diff --git a/src/kernel.c b/kernel/kernel.c similarity index 98% rename from src/kernel.c rename to kernel/kernel.c index e0e0a80..023888e 100644 --- a/src/kernel.c +++ b/kernel/kernel.c @@ -57,7 +57,7 @@ void main() { setupTimer(); setupRtc(); setupKeyboard(); - if (setupSerial(PORT_COM1) != 0) { printf("\e[1MFailed to initialize serial interface\e[8M\n"); } + if (setupSerial(PORT_COM1) != 0) { printf("\x1b[1MFailed to initialize serial interface\x1b[8M\n"); } else { serialPrint(PORT_COM1, "\x1b[H\x1b[J"); serialPrint(PORT_COM1, "kern. \x1b[36m(Serial console)\x1b[0m\n\n"); @@ -98,7 +98,7 @@ void main() { // Run Interactive Shell Program // TODO: Make it in another file while (1) { - printf("\e[6M\e[8N%s \e[2M%s \e[8M", cwd, PROMPT); + printf("\x1b[6M\x1b[8N%s \x1b[2M%s \x1b[8M", cwd, PROMPT); char input[256]; char* inputPtr = input; diff --git a/src/kernel.ld b/kernel/kernel.ld similarity index 100% rename from src/kernel.ld rename to kernel/kernel.ld diff --git a/kernel/makefile b/kernel/makefile new file mode 100644 index 0000000..06cd3f1 --- /dev/null +++ b/kernel/makefile @@ -0,0 +1,11 @@ +# +# Kernel makefile +# + +include make.defaults + +$(KERNEL): $(KERNEL_SRC) $(LIBC) $(LIBK) + @echo "Compiling $(KERNEL_SRC)..." + $(CC) $(CFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/kernel/kernel.o -c $(KERNEL_SRC) + $(LD) $(LDFLAGS) -T$(KERNEL_DIR)/kernel.ld --oformat binary -o $(BIN)/kernel.bin $(BIN)/kernel/kernel.o $(LIBC) $(LIBK) + diff --git a/libc/_fileio.c b/libc/_fileio.c index faa2ac1..67a3bea 100644 --- a/libc/_fileio.c +++ b/libc/_fileio.c @@ -30,7 +30,7 @@ size_t fread(void* ptr, size_t size, size_t count, FILE* stream) { int fd = stream->_file; while (readEl != count) { - if (read(fd, (void*)(ptr + offset), size) != size) break; + if (read(fd, (void*)(ptr + offset), size) != (int)size) break; offset += size; readEl++; } @@ -44,7 +44,7 @@ size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream) { int fd = stream->_file; while (writtenEl != count) { - if (write(fd, (void*)(ptr + offset), size) != size) break; + if (write(fd, (void*)(ptr + offset), size) != (int)size) break; offset += size; writtenEl++; } diff --git a/libc/makefile b/libc/makefile new file mode 100644 index 0000000..1111e3a --- /dev/null +++ b/libc/makefile @@ -0,0 +1,14 @@ +# +# C Standard library +# + +include make.defaults + +$(LIBC): $(LIBC_SRC) + @echo "Building $(LIBC)..." + for f in $(LIBC_SRC); do\ + filename=$$(basename $$f .c);\ + $(CC) $(CFLAGS) -I$(LIBC_INC) -o $(BIN)/libc/$$filename.o -c $$f;\ + done + $(LD) $(LDFLAGS) -r -o $(LIBC) $(BIN)/libc/**.o + diff --git a/libc/math.c b/libc/math.c index e23694d..e87434f 100644 --- a/libc/math.c +++ b/libc/math.c @@ -1,16 +1,4 @@ -#ifndef MATH_H -#define MATH_H +#include -// Constants -#define PI 3.141592653589 // Should be good enough -#define E 2.718281828459 +/* TODO */ -// Macros -#define abs(x) ( (x) > 0 ? (x) : -(x) ) -#define sign(x) ( (x) == 0 ? 0 : ((x) < 0 ? -1 : 1 ) ) - -#define floor(x) ( (int)(x) ) // C floors floats when converting to ints -#define round(x) ( (int)((x) + 0.5) ) // Normal rounding -#define ceil(x) ( (int)((x) + 1) ) // Always rounds up - -#endif diff --git a/libc/stdbool.c b/libc/stdbool.c deleted file mode 100644 index 98349dc..0000000 --- a/libc/stdbool.c +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef BOOL_H -#define BOOL_H - -#define bool _Bool -#define true 1 -#define false 0 - -#endif diff --git a/libk/makefile b/libk/makefile new file mode 100644 index 0000000..b9bd448 --- /dev/null +++ b/libk/makefile @@ -0,0 +1,14 @@ +# +# Kernel library +# + +include make.defaults + +$(LIBK): $(LIBK_SRC) + @echo "Building $(LIBK)..." + for f in $(LIBK_SRC); do\ + filename=$$(basename $$f .c);\ + $(CC) $(CFLAGS) -I$(LIBC_INC) -I$(LIBK_INC) -o $(BIN)/libk/$$filename.o -c $$f;\ + done + $(LD) $(LDFLAGS) -r -o $(LIBK) $(BIN)/libk/**.o + diff --git a/make.config b/make.config deleted file mode 100644 index a27d5f1..0000000 --- a/make.config +++ /dev/null @@ -1,9 +0,0 @@ -# Kern Config -# Uncomment a line to enable it -# You may also change values - -#ASFLAGS := $(ASFLAGS) -D DEF_RES # Use default resolution variables -ASFLAGS := $(ASFLAGS) -D DEF_W=1920 # Default width -ASFLAGS := $(ASFLAGS) -D DEF_H=1080 # Default height -ASFLAGS := $(ASFLAGS) -D DEF_BPP=32 # Default bpp - diff --git a/make.defaults b/make.defaults new file mode 100644 index 0000000..b195fb5 --- /dev/null +++ b/make.defaults @@ -0,0 +1,51 @@ +# +# Makfile variables +# + +# Tools +HOSTCC = cc +CC = i686-elf-gcc +LD = i686-elf-ld +AS = nasm +MAKE = make + +# Flags +CFLAGS = -Wall -Wextra -Wpedantic -Wno-pointer-sign -Wno-pointer-arith\ + -Os -std=c17 -nostdinc -g\ + -ffreestanding -fno-stack-protector -fno-builtin -fno-pie\ + -m32 -mgeneral-regs-only -march=i386 +LDFLAGS = -z notext -melf_i386 -nostdlib +ASFLAGS = + +# Directories +# Outputs +BIN = ./bin +BOOTLOADER = $(BIN)/stage1.bin $(BIN)/stage2.bin $(BIN)/stage3.bin +KERNEL = $(BIN)/kernel.bin +LIBC = $(BIN)/libc.o +LIBK = $(BIN)/libk.o +IMG = kern.img + +# Kernel +KERNEL_DIR = ./kernel +KERNEL_SRC = $(KERNEL_DIR)/kernel.c + +# Bootloader +BOOT_DIR = ./boot + +# Libc +LIBC_DIR = ./libc +LIBC_SRC = $(shell find $(LIBC_DIR) -type f -name "**.c") + +# Libk +LIBK_DIR = ./libk +LIBK_SRC = $(shell find $(LIBK_DIR) -type f -name "**.c") + +# Userspace +FONT_DIR = ./usr/fonts +FONTS = testfont term16n + +# Include directories +LIBC_INC = $(LIBC_DIR)/include +LIBK_INC = $(LIBK_DIR)/include + diff --git a/makedisk.c b/makedisk.c index 698578a..63f7155 100644 --- a/makedisk.c +++ b/makedisk.c @@ -5,9 +5,9 @@ #include #include -#include "libk/include/fs/fs.h" +#include -#define IMAGE_NAME "kern.iso" +#define IMAGE_NAME "kern.img" #define BIN_DIR "./bin/" #define DISK_SIZE (2880 * 512) // 1.44MB diff --git a/makefile b/makefile new file mode 100644 index 0000000..d9ffb50 --- /dev/null +++ b/makefile @@ -0,0 +1,45 @@ +# +# Main kern makefile +# + +# Version +export VERSION = 0.2.0 + +# Configurable variables (eg. CC, CFLAGS, ...) +include make.defaults + +# The directories containing other makefiles +SUBDIRS = libc libk boot kernel + +all: msg $(IMG) +msg: + @echo\ + "\x1b[36mBuilding \x1b[1mkern.\x1b[22m V$(VERSION)\x1b[0m\n\x1b[32m (c) Tom-on 2024\x1b[0m\n" + +# Build the final disk image +$(IMG): $(SUBDIRS) $(FONTS) + $(HOSTCC) ./makedisk.c -I$(LIBK_INC)/fs -o ./makedisk + ./makedisk + @echo "\n\x1b[32mDone!\x1b[0m\n" + +$(SUBDIRS): + mkdir -p $(BIN)/$@ + $(MAKE) -C . -f $@/makefile + +$(FONTS): + cp $(FONT_DIR)/$@.fnt $(BIN)/$@.fnt + +.PHONY: all $(SUBDIRS) fonts clean qemu + +clean: + rm -rf $(BIN) + rm -rf $(IMG) + rm -f makedisk + +qemu: + qemu-system-x86_64\ + -drive format=raw,file=$(IMG),index=0,media=disk\ + -m 256M -accel tcg -monitor stdio\ + -rtc base=localtime,clock=host,driftfix=slew\ + -audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0 + diff --git a/todo.txt b/todo.txt index 4792e09..9c05c1d 100644 --- a/todo.txt +++ b/todo.txt @@ -8,9 +8,9 @@ Legend: - [?] Optional - [!] The Big Refactor - - [?] Use GRUB - - [ ] Multiboot - - [ ] boot.s + - [?] Use UEFI + - [ ] 32-bit (probably) + - [ ] Partitioned disk - [ ] project structure - [ ] system root - [ ] arch/ @@ -19,17 +19,17 @@ Legend: - [ ] cross compiler setup - [x] libc - User space - [x] libk - Kernel libc - - [ ] kernel + - [x] kernel - [x] use libk - - [ ] makefile - - [ ] probably a full rewrite + - [x] makefile + - [?] probably a full rewrite - [ ] userspace - [ ] shell - [ ] calc - [ ] editor - [ ] filesystem - [ ] proper file organization - - [_] Makefile refactorin' + - [X] Makefile refactorin' (finally done!!!) - [x] Multistage Bootloader - [x] Bootloader reads filesystem to find files