From d11c1072f9971afb84464758a339e3265aeaa29c Mon Sep 17 00:00:00 2001 From: nub31 Date: Sat, 23 Aug 2025 15:52:54 +0200 Subject: [PATCH] ... --- .clang-format | 2 ++ src/boot/bootloader.asm | 23 ++++++------- src/kernel/entry.asm | 9 +++-- src/kernel/kernel.c | 51 ++++++++++++++++++++++++++-- src/kernel/linker.ld | 22 ++++++++++++ src/kernel/makefile | 10 +++--- src/kernel/print.c | 75 ++++++++++++++++++++++++++--------------- src/kernel/print.h | 42 +---------------------- src/makefile | 8 ++--- 9 files changed, 143 insertions(+), 99 deletions(-) create mode 100644 src/kernel/linker.ld diff --git a/.clang-format b/.clang-format index cb5cd48..7f6796d 100644 --- a/.clang-format +++ b/.clang-format @@ -16,3 +16,5 @@ AllowShortLoopsOnASingleLine: false SeparateDefinitionBlocks: Always BreakBeforeBraces: Allman + +Cpp11BracedListStyle: false diff --git a/src/boot/bootloader.asm b/src/boot/bootloader.asm index 56a84b7..127d9f7 100644 --- a/src/boot/bootloader.asm +++ b/src/boot/bootloader.asm @@ -1,28 +1,27 @@ [org 0x7c00] [bits 16] -KERNEL_LOCATION equ 0x1000 +KERNEL_INIT equ 0x1000 -mov [BOOT_DISK], dl +mov [BOOT_DISK], dl -xor ax, ax +xor ax, ax mov es, ax mov ds, ax mov bp, 0x8000 mov sp, bp -mov bx, KERNEL_LOCATION +mov bx, KERNEL_INIT mov dh, 2 mov ah, 0x02 -mov al, dh +mov al, dh mov ch, 0x00 mov dh, 0x00 mov cl, 0x02 mov dl, [BOOT_DISK] int 0x13 - mov ah, 0x0 mov al, 0x3 int 0x10 @@ -41,9 +40,8 @@ jmp $ BOOT_DISK: db 0 gdt_start: - gdt_null: - dd 0x0 - dd 0x0 + dd 0x0 + dd 0x0 gdt_code: dw 0xffff @@ -75,13 +73,12 @@ start_protected_mode: mov es, ax mov fs, ax mov gs, ax - + mov ebp, 0x90000 mov esp, ebp - jmp KERNEL_LOCATION - jmp $ - + jmp KERNEL_INIT + hlt times 510-($-$$) db 0 dw 0xaa55 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm index 226b798..9335809 100644 --- a/src/kernel/entry.asm +++ b/src/kernel/entry.asm @@ -1,5 +1,4 @@ -section .text - [bits 32] - [extern init] - call init - jmp $ \ No newline at end of file +[extern kernel_init] +[bits 32] +call kernel_init +hlt \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 7b9c90c..7793163 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,7 +1,52 @@ #include "print.h" -void init(void) +/** + * C++ version 0.4 char* style "itoa": + * Written by Lukás Chmela + * Released under GPLv3. + */ +char* itoa(int value, char* result, int base) { - print_init(); - println("Starting nub-os"); + // check that the base if valid + if (base < 2 || base > 36) + { + *result = '\0'; + return result; + } + + char *ptr = result, *ptr1 = result, tmp_char; + int tmp_value; + + do + { + tmp_value = value; + value /= base; + *ptr++ = + "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; + } while (value); + + // Apply negative sign + if (tmp_value < 0) + *ptr++ = '-'; + *ptr-- = '\0'; + + // Reverse the string + while (ptr1 < ptr) + { + tmp_char = *ptr; + *ptr-- = *ptr1; + *ptr1++ = tmp_char; + } + return result; +} + +void kernel_init(void) +{ + for (int i = 0; i < 100; i++) + { + // char buf[10]; + // itoa(i, buf, 10); + // kernel_print(buf); + kernel_print("Starting nub-osStarting"); + } } diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld new file mode 100644 index 0000000..5c77a0b --- /dev/null +++ b/src/kernel/linker.ld @@ -0,0 +1,22 @@ +ENTRY(kernel_init) + +SECTIONS { + . = 0x1000; + + .text : { + *(.text) + } + + .rodata : { + *(.rodata) + } + + .data : { + *(.data) + } + + .bss : { + *(.bss) + *(COMMON) + } +} diff --git a/src/kernel/makefile b/src/kernel/makefile index 745b4d5..11a8c1c 100644 --- a/src/kernel/makefile +++ b/src/kernel/makefile @@ -1,10 +1,7 @@ CC = i386-elf-gcc -ffreestanding -m32 -kernel.bin: entry.o kernel.o mem.o print.o - i386-elf-ld -o kernel.bin -Ttext 0x1000 entry.o kernel.o mem.o print.o --oformat binary - -entry.o: entry.asm - nasm -f elf -o entry.o entry.asm +kernel.bin: entry.o kernel.o linker.ld mem.o print.o + i386-elf-ld -o kernel.bin -T linker.ld entry.o kernel.o mem.o print.o --oformat binary kernel.o: kernel.c $(CC) -c -o kernel.o kernel.c @@ -15,5 +12,8 @@ mem.o: mem.c print.o: print.c $(CC) -c -o print.o print.c +entry.o: entry.asm + nasm -f elf entry.asm + clean: @rm *.o *.bin 2>/dev/null || true diff --git a/src/kernel/print.c b/src/kernel/print.c index 5764bc9..acde0da 100644 --- a/src/kernel/print.c +++ b/src/kernel/print.c @@ -1,41 +1,60 @@ #include "print.h" +#include +#include -#define VGA_WIDTH 80 -#define VGA_HEIGHT 25 +#define ROWS 25 +#define COLUMNS 80 +#define BUF_SIZE ((ROWS * COLUMNS) * 2) -char* vga_text_pos; +#define BUF_START 0xb8000 +#define BUF_END (BUF_START + BUF_SIZE) -void print_init(void) +enum FG_COLOR { - vga_text_pos = (char*)0xb8000; -} + FG_BLACK = 0x0, + FG_BLUE = 0x1, + FG_GREEN = 0x2, + FG_CYAN = 0x3, + FG_RED = 0x4, + FG_MAGENTA = 0x5, + FG_BROWN = 0x6, + FG_LIGHT_GRAY = 0x7, + FG_DARK_GRAY = 0x8, + FG_LIGHT_BLUE = 0x9, + FG_LIGHT_GREEN = 0xA, + FG_LIGHT_CYAN = 0xB, + FG_LIGHT_RED = 0xC, + FG_LIGHT_MAGENTA = 0xD, + FG_YELLOW = 0xE, + FG_WHITE = 0xF, +}; -void print_newline(void) +enum BG_COLOR { - vga_text_pos += 160 - ((vga_text_pos - (char*)0xb8000) % 160); -} + BG_BLACK = 0x0, + BG_BLUE = 0x1, + BG_GREEN = 0x2, + BG_CYAN = 0x3, + BG_RED = 0x4, + BG_MAGENTA = 0x5, + BG_BROWN = 0x6, + BG_LIGHT_GRAY = 0x7, +}; -void print_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color) +typedef struct +{ + uint8_t character; + uint8_t color; +} vga_char; + +static vga_char* print_buf = (vga_char*)BUF_START; + +void kernel_print(const char* string) { for (int i = 0; string[i]; i++) { - *vga_text_pos++ = string[i]; - *vga_text_pos++ = (bg_color << 4) | fg_color; + // todo(nub): scroll if overflowing + + *print_buf++ = (vga_char){ string[i], FG_WHITE | BG_BLACK << 4 }; } } - -void println_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color) -{ - print_clr(string, fg_color, bg_color); - print_newline(); -} - -void print(const char* string) -{ - print_clr(string, VGA_FG_WHITE, VGA_BG_BLACK); -} - -void println(const char* string) -{ - println_clr(string, VGA_FG_WHITE, VGA_BG_BLACK); -} \ No newline at end of file diff --git a/src/kernel/print.h b/src/kernel/print.h index 8f28394..747b3bd 100644 --- a/src/kernel/print.h +++ b/src/kernel/print.h @@ -1,41 +1 @@ -enum VGA_FG_COLOR -{ - VGA_FG_BLACK = 0x0, - VGA_FG_BLUE = 0x1, - VGA_FG_GREEN = 0x2, - VGA_FG_CYAN = 0x3, - VGA_FG_RED = 0x4, - VGA_FG_MAGENTA = 0x5, - VGA_FG_BROWN = 0x6, - VGA_FG_LIGHT_GRAY = 0x7, - VGA_FG_DARK_GRAY = 0x8, - VGA_FG_LIGHT_BLUE = 0x9, - VGA_FG_LIGHT_GREEN = 0xA, - VGA_FG_LIGHT_CYAN = 0xB, - VGA_FG_LIGHT_RED = 0xC, - VGA_FG_LIGHT_MAGENTA = 0xD, - VGA_FG_YELLOW = 0xE, - VGA_FG_WHITE = 0xF, -}; - -enum VGA_BG_COLOR -{ - VGA_BG_BLACK = 0x0, - VGA_BG_BLUE = 0x1, - VGA_BG_GREEN = 0x2, - VGA_BG_CYAN = 0x3, - VGA_BG_RED = 0x4, - VGA_BG_MAGENTA = 0x5, - VGA_BG_BROWN = 0x6, - VGA_BG_LIGHT_GRAY = 0x7, -}; - -void print_init(void); - -void print_newline(void); - -void print(const char* string); -void println(const char* string); - -void print_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color); -void println_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color); \ No newline at end of file +void kernel_print(const char* string); diff --git a/src/makefile b/src/makefile index 2ef10e5..7721464 100644 --- a/src/makefile +++ b/src/makefile @@ -1,4 +1,7 @@ -os.bin: boot/bootloader.bin kernel/kernel.bin zeroes.bin +.PHONY: kernel/kernel.bin boot/bootloader.bin + +os.bin: boot/bootloader.bin kernel/kernel.bin + dd if=/dev/zero of=zeroes.bin bs=1 count=10240 cat boot/bootloader.bin kernel/kernel.bin zeroes.bin > os.bin kernel/kernel.bin: @@ -7,9 +10,6 @@ kernel/kernel.bin: boot/bootloader.bin: pushd boot; make bootloader.bin; popd -zeroes.bin: - dd if=/dev/zero of=zeroes.bin bs=1 count=10240 - run: os.bin qemu-system-x86_64 -drive file=os.bin,format=raw,index=0,media=disk