From 7ab30244d379e8a4d0734b39c41ee637e777cd21 Mon Sep 17 00:00:00 2001 From: Xircon Date: Tue, 28 Apr 2026 23:08:10 -0400 Subject: [PATCH] Lots of cool shit! like begin port to xtensa! add PMM for I686! and More! --- .ccls | 3 +- Makefile | 6 +- README.md | 29 +- a.out | 0 build.sh => build.i686 | 18 +- build.xtensa | 101 ++++++ build_scripts/config.mk | 5 +- build_scripts/toolchain.mk | 31 +- src/kernel/Makefile | 21 +- src/kernel/arch/i686/cpuid.c | 1 + src/kernel/arch/i686/irq.c | 3 + src/kernel/arch/i686/multiboot.asm | 36 +- src/kernel/core/hal/hal.c | 1 + src/kernel/core/memory/alloc.c | 21 +- src/kernel/core/memory/alloc.h | 7 +- src/kernel/core/memory/pmm.c | 74 ++++ src/kernel/core/memory/pmm.h | 30 ++ src/kernel/core/memory/{page.c => vmm.c} | 11 +- src/kernel/core/memory/{page.h => vmm.h} | 12 + src/kernel/dri/cmos.c | 5 +- src/kernel/dri/keyboard.c | 17 +- src/kernel/dri/serial.c | 12 +- src/kernel/dri/sound/pcspeaker.c | 5 +- src/kernel/dri/timer.c | 2 + src/kernel/dri/video/vesa/vesa.c | 7 + src/kernel/dri/video/vesa/vesa.h | 18 + src/kernel/linker.ld | 12 +- src/kernel/main.c | 59 +++- src/kernel/multiboot.h | 417 +++++++++++++++++++++++ src/kernel/nanstd.c | 21 ++ src/kernel/nanstd.h | 7 + src/kernel/stdio.c | 9 +- src/kernel/util/binary.h | 2 +- src/kernel/version.h | 6 +- 34 files changed, 896 insertions(+), 113 deletions(-) create mode 100644 a.out rename build.sh => build.i686 (83%) create mode 100755 build.xtensa create mode 100644 src/kernel/core/memory/pmm.c create mode 100644 src/kernel/core/memory/pmm.h rename src/kernel/core/memory/{page.c => vmm.c} (88%) rename src/kernel/core/memory/{page.h => vmm.h} (56%) create mode 100644 src/kernel/dri/video/vesa/vesa.c create mode 100644 src/kernel/dri/video/vesa/vesa.h create mode 100644 src/kernel/multiboot.h create mode 100644 src/kernel/nanstd.c create mode 100644 src/kernel/nanstd.h diff --git a/.ccls b/.ccls index e478195..d5a060c 100644 --- a/.ccls +++ b/.ccls @@ -2,4 +2,5 @@ clang %c -std=c99 %c -isystem./src/kernel/ %c -fno-builtin -%c -D__I686 \ No newline at end of file +%c -D__I686 +%c -D__XTENSA \ No newline at end of file diff --git a/Makefile b/Makefile index 5999d52..b6c391e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ include build_scripts/config.mk -.PHONY: all floppy_image kernel bootloader clean always +.PHONY: all floppy_image kernel bootloader clean always test all: kernel @@ -35,7 +35,8 @@ $(BUILD_DIR)/kernel.bin: always always: mkdir -p $(BUILD_DIR) - +test: kernel + qemu-system-i386 -chardev pty,id=serial1 -serial mon:stdio -serial chardev:serial1 -audiodev pa,id=speaker -machine pcspk-audiodev=speaker -kernel build/nanite.bin -m size=16M # # Clean @@ -43,4 +44,3 @@ always: clean: $(MAKE) -C src/kernel BUILD_DIR=$(abspath $(BUILD_DIR)) clean rm -rf $(BUILD_DIR)/* - rm -rf src/bootloader/stage2/*.err diff --git a/README.md b/README.md index 58824df..451a73a 100644 --- a/README.md +++ b/README.md @@ -9,20 +9,25 @@ I however would recomend atleast a Pentium 2 Class System or higher. ## Features - Support for the GNU GRUB 2 Bootloader and Multiboot V2 - Custom Theme for GRUB (Not Complete Yet!) -- Basic Memory Paging +- Basic Memory Management - Support for Floppy Disk Drives (FDDs) - Basic Serial (RS-232) Support - Keyboard Input +### Serial: +Runs at 9600 baud (as its fairly standard) +Serial is mostly used as a debug output (In the future this will be able to be enabled/disabled) + Note: Kernel is currently under very heavy development. All things are subject to change at any time. ## Building Run `./build.sh` inside main directory of repo, however you may first need to run `make toolchain` i would DEFINITELY use more than one job (building GCC takes forever) -To write to a disk, use `./write.sh` WARNING: **is hard coded to /dev/sdb** (sorry i suck at scripting) +To write to a disk, use `./write.sh` just enter a disk in at the prompt and it will build & flash nanite to it (ex. `/dev/sdb`) +You could also run `make all` but the scripts a bit better tbh, it even automates starting QEMU and building an image. -You could also run `make all` but the scripts a bit better tbh, it even automates starting QEMU. +Theres also a way to debug it with BOCHS if need be, just run `./debug.sh` instead of the normal build script. ### Build Requirements - make @@ -35,18 +40,10 @@ You could also run `make all` but the scripts a bit better tbh, it even automate - parted ## How is Testing Done -Testing is mostly done with QEMU These days, but I do sometimes pull out my Dell Latitude D610 to test on real hardware (for anyone wondering its completely maxed out. [2GB of ram Pentium M @ 2.23GHz]) +Testing is mostly done with QEMU at this point as its easier, but I do sometimes pull out my Dell Latitude D610 to test on real hardware (for anyone wondering its completely maxed out. [2GB of ram Pentium M @ 2.23GHz]) I Also do some testing on a Dell Dimension 8110 (its at my desk 24/7 and has its serial port hooked to my desktop, lol) -## Thanks! -Resources I've Used Throughout the Project -- Nanobyte's Youtube Channel: https://www.youtube.com/@nanobyte-dev -- OSDEV Wiki: https://wiki.osdev.org/Expanded_Main_Page -- BrokenThorn: http://www.brokenthorn.com - -And thanks to some of my favorite bands for some nice ass music! -- King Gizzard and The Lizzard Wizzard -- Nine Inch Nails -- The Strokes - -(and if your wondering, yes im a massive nerd about music) \ No newline at end of file +## Resources I've Used Throughout the Project +- Nanobyte's Youtube Channel: https://www.youtube.com/@nanobyte-dev (Used to start off learning about OSDev) +- OSDEV Wiki: https://wiki.osdev.org/Expanded_Main_Page (INSANELY Helpful like SUPER USEFUL basically any info i've needed i've pulled from here) +- BrokenThorn: http://www.brokenthorn.com (Can't remember what i used it for but only used once or twice) \ No newline at end of file diff --git a/a.out b/a.out new file mode 100644 index 0000000..e69de29 diff --git a/build.sh b/build.i686 similarity index 83% rename from build.sh rename to build.i686 index 0371de8..e9a31b9 100755 --- a/build.sh +++ b/build.i686 @@ -38,15 +38,15 @@ case $yn in echo Installing GRUB! echo ---------------- - sudo losetup -d /dev/loop800 + sudo losetup -d /dev/loop969 dd if=/dev/zero of=build/main.img bs=512 count=50000 mkdir -p build/mnt - sudo losetup -P /dev/loop800 build/main.img - sudo parted /dev/loop800 mktable msdos - sudo parted /dev/loop800 mkpart primary fat16 2048s 100% - sudo parted /dev/loop800p1 set 1 boot on - sudo mkfs.fat -F16 /dev/loop800p1 - sudo mount /dev/loop800p1 build/mnt + sudo losetup -P /dev/loop969 build/main.img + sudo parted /dev/loop969 mktable msdos + sudo parted /dev/loop969 mkpart primary fat16 2048s 100% + sudo parted /dev/loop969p1 set 1 boot on + sudo mkfs.fat -F16 /dev/loop969p1 + sudo mount /dev/loop969p1 build/mnt sudo mkdir build/mnt/boot sudo mkdir build/mnt/boot/grub sudo mkdir build/mnt/misc @@ -59,9 +59,9 @@ case $yn in --boot-directory=build/mnt/boot \ --target=i386-pc \ --bootloader-id=GRUB \ - /dev/loop800 -v + /dev/loop969 -v sudo umount build/mnt - sudo losetup -d /dev/loop800 + sudo losetup -d /dev/loop969 ;; n ) echo exiting...; diff --git a/build.xtensa b/build.xtensa new file mode 100755 index 0000000..1bf1a58 --- /dev/null +++ b/build.xtensa @@ -0,0 +1,101 @@ +#!/bin/bash +echo -------- +echo STARTING +echo -------- +read -p "Do you want to clean old build files? (y/n) " yn + +case $yn in + y ) + + echo ---------------------------; + echo Removing Old Build Files...; + echo ---------------------------; + + make -j24 clean; + echo -----; + echo Done!; + echo -----; + ;; + n ) + echo ---------; + echo Proceding; + echo ---------; + ;; +esac +echo ------------ +echo COMPILING OS +echo ------------ + +make TARGET=xtensa-esp32-elf TARGET_DEFINE=__XTENSA TARGET_ASM=xtensa-esp32-elf-as -j24 -s + +echo --------- +echo Finished! +echo --------- +read -p "Do you want to make to make a bootable image? [Requires sudo] (y/n) " yn +case $yn in + y) + echo ---------------- + echo Installing GRUB! + echo ---------------- + + sudo losetup -d /dev/loop969 + dd if=/dev/zero of=build/main.img bs=512 count=50000 + mkdir -p build/mnt + sudo losetup -P /dev/loop969 build/main.img + sudo parted /dev/loop969 mktable msdos + sudo parted /dev/loop969 mkpart primary fat16 2048s 100% + sudo parted /dev/loop969p1 set 1 boot on + sudo mkfs.fat -F16 /dev/loop969p1 + sudo mount /dev/loop969p1 build/mnt + sudo mkdir build/mnt/boot + sudo mkdir build/mnt/boot/grub + sudo mkdir build/mnt/misc + sudo mkdir build/mnt/misc/src + sudo cp -r src/* build/mnt/src + sudo cp -r grub/* build/mnt/boot/grub + sudo cp build/nanite.bin build/mnt/boot/nanite + sudo grub-install \ + --install-modules="part_msdos gettext all_video gfxterm png normal echo multiboot2" \ + --boot-directory=build/mnt/boot \ + --target=i386-pc \ + --bootloader-id=GRUB \ + /dev/loop969 -v + sudo umount build/mnt + sudo losetup -d /dev/loop969 + + ;; + n ) echo exiting...; + exit;; + * ) echo invalid response; + exit 1;; +esac +echo --------- +echo Finished! +echo --------- + +read -p "Do you want to Start QEMU? (y/n) " yn + +case $yn in + y ) + echo ------------- + echo STARTING QEMU + echo ------------- + qemu-system-i386 \ + -chardev pty,id=serial1 \ + -serial mon:stdio \ + -serial chardev:serial1 \ + -audiodev pa,id=speaker \ + -machine pcspk-audiodev=speaker \ + -hda build/main.img \ + -m size=16M + echo -------- + echo Finshed! + echo -------- + + ;; + n ) echo exiting...; + exit;; + * ) echo invalid response; + exit 1;; +esac + diff --git a/build_scripts/config.mk b/build_scripts/config.mk index fc24c17..9c89e30 100644 --- a/build_scripts/config.mk +++ b/build_scripts/config.mk @@ -1,4 +1,4 @@ -export CFLAGS = -std=c99 -g +export CFLAGS = -std=c99 -g -D$(TARGET_DEFINE) export ASM = nasm export ASMFLAGS = export CC = gcc @@ -8,9 +8,10 @@ export LINKFLAGS = export LIBS = export TARGET = i686-elf +export TARGET_DEFINE = __I686 export TARGET_ASM = nasm export TARGET_ASMFLAGS = -export TARGET_CFLAGS = -std=c99 -g +export TARGET_CFLAGS = -std=c99 -g -Wimplicit-function-declaration export TARGET_CC = $(TARGET)-gcc export TARGET_CXX = $(TARGET)-g++ export TARGET_LD = $(TARGET)-gcc diff --git a/build_scripts/toolchain.mk b/build_scripts/toolchain.mk index 2d46286..b5b23a5 100644 --- a/build_scripts/toolchain.mk +++ b/build_scripts/toolchain.mk @@ -1,45 +1,43 @@ TOOLCHAIN_PREFIX = $(abspath toolchain/$(TARGET)) export PATH := $(TOOLCHAIN_PREFIX)/bin:$(PATH) -toolchain: toolchain_binutils toolchain_gcc - +GCC_SRC = toolchain/gcc-$(GCC_VERSION) +GCC_BUILD = toolchain/gcc-build-$(GCC_VERSION) BINUTILS_SRC = toolchain/binutils-$(BINUTILS_VERSION) BINUTILS_BUILD = toolchain/binutils-build-$(BINUTILS_VERSION) -toolchain_binutils: $(TOOLCHAIN_PREFIX)/bin/i686-elf-ld +toolchain: toolchain_binutils toolchain_gcc -$(TOOLCHAIN_PREFIX)/bin/i686-elf-ld: $(BINUTILS_SRC).tar.gz +toolchain_binutils: $(TOOLCHAIN_PREFIX)/bin/$(TARGET)-ld + +$(TOOLCHAIN_PREFIX)/bin/$(TARGET)-ld: $(BINUTILS_SRC).tar.gz cd toolchain && tar -xf binutils-$(BINUTILS_VERSION).tar.gz - mkdir $(BINUTILS_BUILD) + mkdir -p $(BINUTILS_BUILD) cd $(BINUTILS_BUILD) && CFLAGS= ASMFLAGS= CC= CXX= LD= ASM= LINKFLAGS= LIBS= ../binutils-$(BINUTILS_VERSION)/configure \ --prefix="$(TOOLCHAIN_PREFIX)" \ --target=$(TARGET) \ --with-sysroot \ --disable-nls \ --disable-werror - $(MAKE) -j8 -C $(BINUTILS_BUILD) + $(MAKE) -j24 -C $(BINUTILS_BUILD) $(MAKE) -C $(BINUTILS_BUILD) install $(BINUTILS_SRC).tar.gz: mkdir -p toolchain cd toolchain && wget $(BINUTILS_URL) +toolchain_gcc: $(TOOLCHAIN_PREFIX)/bin/$(TARGET)-gcc -GCC_SRC = toolchain/gcc-$(GCC_VERSION) -GCC_BUILD = toolchain/gcc-build-$(GCC_VERSION) - -toolchain_gcc: $(TOOLCHAIN_PREFIX)/bin/i686-elf-gcc - -$(TOOLCHAIN_PREFIX)/bin/i686-elf-gcc: $(TOOLCHAIN_PREFIX)/bin/i686-elf-ld $(GCC_SRC).tar.gz +$(TOOLCHAIN_PREFIX)/bin/$(TARGET)-gcc: $(TOOLCHAIN_PREFIX)/bin/$(TARGET)-ld $(GCC_SRC).tar.gz cd toolchain && tar -xf gcc-$(GCC_VERSION).tar.gz - mkdir $(GCC_BUILD) + mkdir -p $(GCC_BUILD) cd $(GCC_BUILD) && CFLAGS= ASMFLAGS= CC= CXX= LD= ASM= LINKFLAGS= LIBS= ../gcc-$(GCC_VERSION)/configure \ --prefix="$(TOOLCHAIN_PREFIX)" \ --target=$(TARGET) \ --disable-nls \ --enable-languages=c,c++ \ --without-headers - $(MAKE) -j8 -C $(GCC_BUILD) all-gcc all-target-libgcc + $(MAKE) -j24 -C $(GCC_BUILD) all-gcc all-target-libgcc $(MAKE) -C $(GCC_BUILD) install-gcc install-target-libgcc $(GCC_SRC).tar.gz: @@ -50,7 +48,10 @@ $(GCC_SRC).tar.gz: # Clean # clean-toolchain: - rm -rf $(GCC_BUILD) $(GCC_SRC) $(BINUTILS_BUILD) $(BINUTILS_SRC) + rm -rf $(GCC_BUILD) + rm -rf $(GCC_SRC) + rm -rf $(BINUTILS_BUILD) + rm -rf $(BINUTILS_SRC) clean-toolchain-all: rm -rf toolchain/* diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 91eff43..aa60c54 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -1,5 +1,6 @@ +TARGET_DEFINE = __I686 TARGET_ASMFLAGS += -f elf -TARGET_CFLAGS += -ffreestanding -nostdlib -I. -D__I686 +TARGET_CFLAGS += -ffreestanding -nostdlib -I. -D$(TARGET_DEFINE) TARGET_LIBS += -lgcc TARGET_LINKFLAGS += -T linker.ld -nostdlib @@ -18,17 +19,13 @@ SOURCES_C = $(wildcard *.c) \ OBJECTS_C = $(patsubst %.c, $(BUILD_DIR)/kernel/c/%.obj, $(SOURCES_C)) -HEADERS_ASM = $(wildcard *.inc) \ - $(wildcard */*.inc) \ - $(wildcard */*/*.inc) \ - $(wildcard */*/*/*.inc) \ - arch/i686/gen_isr.inc +HEADERS_ASM_i686-elf = $(wildcard arch/i686/*.inc) +SOURCES_ASM_i686-elf = $(wildcard arch/i686/*.asm) -SOURCES_ASM = $(wildcard *.asm) \ - $(wildcard */*.asm) \ - $(wildcard */*/*.asm) \ - $(wildcard */*/*/*.asm) +HEADERS_ASM_xtensa-esp32-elf = $(wildcard arch/xtensa/*.inc) +SOURCES_ASM_xtensa-esp32-elf = $(wildcard arch/xtensa/*.asm) +# TWO ARCHITECTURES! HAHAHAHAH OBJECTS_ASM = $(patsubst %.asm, $(BUILD_DIR)/kernel/asm/%.obj, $(SOURCES_ASM)) @@ -40,7 +37,7 @@ all: kernel kernel: $(BUILD_DIR)/nanite.bin -$(BUILD_DIR)/nanite.bin: $(OBJECTS_ASM) $(OBJECTS_C) +$(BUILD_DIR)/nanite.bin: $(OBJECTS_ASM-$(TARGET)) $(OBJECTS_C) @$(TARGET_LD) $(TARGET_LINKFLAGS) -Wl,-Map=$(BUILD_DIR)/kernel.map -o $@ $^ $(TARGET_LIBS) @echo "--> Created: nanite.bin" @@ -49,7 +46,7 @@ $(BUILD_DIR)/kernel/c/%.obj: %.c $(HEADERS_C) @$(TARGET_CC) $(TARGET_CFLAGS) -c -o $@ $< @echo "--> Compiled: " $< -$(BUILD_DIR)/kernel/asm/%.obj: %.asm $(HEADERS_ASM) +$(BUILD_DIR)/kernel/asm/%.obj: %.asm $(HEADERS_ASM-$(TARGET)) @mkdir -p $(@D) @$(TARGET_ASM) $(TARGET_ASMFLAGS) -o $@ $< @echo "--> Compiled: " $< diff --git a/src/kernel/arch/i686/cpuid.c b/src/kernel/arch/i686/cpuid.c index e6d62bc..91c6468 100644 --- a/src/kernel/arch/i686/cpuid.c +++ b/src/kernel/arch/i686/cpuid.c @@ -8,6 +8,7 @@ #include +/*Yoinks CPU's Vendor*/ char CPUID_Get_Vendor() { uint32_t eax, ebx, ecx, edx; eax = 0x00; diff --git a/src/kernel/arch/i686/irq.c b/src/kernel/arch/i686/irq.c index 0910cc4..09a12ba 100644 --- a/src/kernel/arch/i686/irq.c +++ b/src/kernel/arch/i686/irq.c @@ -34,6 +34,7 @@ void IRQ_Handler(Registers* regs) PIC_SendEndOfInterrupt(irq); } +/*Init IRQ Subsystem*/ void IRQ_Initialize() { PIC_Configure(PIC_REMAP_OFFSET, PIC_REMAP_OFFSET + 8); @@ -46,6 +47,8 @@ void IRQ_Initialize() EnableInterrupts(); } +/*Register an IRQ Handler with the IRQ being `irq` and the function being `handler` +which should always be a void!*/ void IRQ_RegisterHandler(int irq, IRQHandler handler) { g_IRQHandlers[irq] = handler; diff --git a/src/kernel/arch/i686/multiboot.asm b/src/kernel/arch/i686/multiboot.asm index 92b1bea..ccf9ee3 100644 --- a/src/kernel/arch/i686/multiboot.asm +++ b/src/kernel/arch/i686/multiboot.asm @@ -4,15 +4,35 @@ ;Xircon ; ;/////////////////////; [bits 32] + +%macro ELFNOTE 3 ; name, type, descr + align 4 + dd %%2 - %%1 + dd %%4 - %%3 + dd %2 + %%1: + dd %1 + %%2: + align 4 + %%3: + dd %3 + %%4: + align 4 +%endmacro + section .boot -start: +extern start +global _start +_start: push 0 popf push ebx push eax call start + cli + hlt -section .multiboot_header +section .multiboot header_start: dd 0xe85250d6 ; magic number dd 0 ; protected mode code @@ -25,7 +45,15 @@ header_start: dd 8 ; size header_end: +section .note +elf_header: + ELFNOTE "Xen",6,"Nanite" + ELFNOTE "Xen",7,"0.0.1" + ELFNOTE "Xen",8,"generic" + ELFNOTE "Xen",5,"xen-3.0" + ELFNOTE "Xen",1,"_start" + ELFNOTE "Xen",10,0x3 + loop: hlt - jmp loop - \ No newline at end of file + jmp loop \ No newline at end of file diff --git a/src/kernel/core/hal/hal.c b/src/kernel/core/hal/hal.c index d9f869f..c3d2a09 100644 --- a/src/kernel/core/hal/hal.c +++ b/src/kernel/core/hal/hal.c @@ -14,6 +14,7 @@ #include +/*Init Hardware Abstraction Layer (HAL)*/ void HAL_Initialize() { // init GDT // printf("> Initializing GDT..."); diff --git a/src/kernel/core/memory/alloc.c b/src/kernel/core/memory/alloc.c index 2f2fca1..a11af72 100644 --- a/src/kernel/core/memory/alloc.c +++ b/src/kernel/core/memory/alloc.c @@ -6,15 +6,32 @@ #include "alloc.h" #include +#include #include #include extern uint16_t DEBUG_COM_PORT; -void malloc(size_t size) { +typedef char ALIGN[16]; + +union header { + struct { + size_t size; // Size of the block + unsigned is_free; // Status Flag + union header *next; // Next Block in List + } s; + ALIGN stub; // Forces 16 Bit Alignment +}; + +typedef union header header_t; + +header_t *head = NULL, *tail = NULL; + +void alloc(int size) { + Serial_Printf(DEBUG_COM_PORT, "ALLOC:> ALLOCATING %dB", size); } -void free() { +void free(void* buffer) { } \ No newline at end of file diff --git a/src/kernel/core/memory/alloc.h b/src/kernel/core/memory/alloc.h index bcb8362..a8ad1eb 100644 --- a/src/kernel/core/memory/alloc.h +++ b/src/kernel/core/memory/alloc.h @@ -7,8 +7,5 @@ #include -struct memory_block { - size_t size; - char free; - struct memory_block *next_free; -}; +void malloc(int size); +void free(void* buffer); \ No newline at end of file diff --git a/src/kernel/core/memory/pmm.c b/src/kernel/core/memory/pmm.c new file mode 100644 index 0000000..e3cfb79 --- /dev/null +++ b/src/kernel/core/memory/pmm.c @@ -0,0 +1,74 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2026| +|Xircon | +\*----------------*/ +#include "pmm.h" + +#include + +#include +#include +#include + + + +/*Set a bit in memory map*/ +void mmap_set (int bit) { + _mm_memory_map[bit / 32] |= (1 << (bit % 32)); +} + +/*Unset a bit in memory map*/ +void mmap_unset (int bit) { + _mm_memory_map[bit / 32] &= ~ (1 << (bit % 32)); +} + + +/*Test if a memory bit is set*/ +bool mmap_test (int bit) { + return _mm_memory_map[bit / 32] & (1 << (bit % 32)); +} + +/*Returns index of first free bit in memory map as an int*/ +int mmap_ff () { + for (uint32_t i=0; i<(Detect_Memory()/4.00624751171408446391) / 32; i++) + if (_mm_memory_map[i] != 0xFFFFFFFF) + for (int j=0; j<32; j++) { + int bit = 1 << j; + if (!(_mm_memory_map[i] & bit)) + return i*4*8+j; + } +} + +void pmm_init(size_t memSize, physical_addr bitmap) { + _mm_memory_size = memSize; + _mm_memory_map = (uint32_t*) bitmap; + _mm_max_blocks = (Detect_Memory()*1024 / PMM_BLOCK_SIZE); + _mm_used_blocks = Detect_Memory()/4.00624751171408446391; // Using this to detect block count SHOULD be fine? we don't talk about the number. + + memset(_mm_memory_map, 0xf, (Detect_Memory()/4.00624751171408446391) / PMM_BLOCKS_PER_BYTE); +} + +void pmm_init_region (physical_addr base, size_t size) { + int align = base / PMM_BLOCK_SIZE; + int blocks = size / PMM_BLOCK_SIZE; + + for (; blocks > 0; blocks--) { + mmap_unset(align++); + _mm_used_blocks--; + } + + mmap_set(0); +} + +void pmm_deinit_region(physical_addr base, size_t size) { + int align = base / PMM_BLOCK_SIZE; + int blocks = size / PMM_BLOCK_SIZE; + + for (; blocks > 0; blocks--) { + mmap_set(align++); + _mm_used_blocks++; + } +} + + diff --git a/src/kernel/core/memory/pmm.h b/src/kernel/core/memory/pmm.h new file mode 100644 index 0000000..fb75e84 --- /dev/null +++ b/src/kernel/core/memory/pmm.h @@ -0,0 +1,30 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2026| +|Xircon | +\*----------------*/ +#pragma once + +#include +#include + +// Defines + +extern uint8_t __end; + +#define PMM_BLOCKS_PER_BYTE 8 +#define PMM_BLOCK_SIZE 4096 +#define PMM_BLOCK_ALIGN PMM_BLOCK_SIZE + +static uint32_t _mm_memory_size = 0; +static uint32_t _mm_used_blocks = 0; +static uint32_t _mm_max_blocks = 0; +static uint32_t* _mm_memory_map = 0; + +typedef int physical_addr; + +// Functions + +void mmap_set (int bit); +void mmap_unset (int bit); +bool mmap_test (int bit); \ No newline at end of file diff --git a/src/kernel/core/memory/page.c b/src/kernel/core/memory/vmm.c similarity index 88% rename from src/kernel/core/memory/page.c rename to src/kernel/core/memory/vmm.c index c0efa30..0003e6f 100644 --- a/src/kernel/core/memory/page.c +++ b/src/kernel/core/memory/vmm.c @@ -3,7 +3,13 @@ |Copyright (C) 2026| |Xircon | \*----------------*/ -#include "page.h" + +/* + * Virtual Memory Manager + */ + +#include "vmm.h" + // Arch stuff #ifdef __I686 @@ -19,6 +25,7 @@ extern uint16_t DEBUG_COM_PORT; uint32_t Page_Directory[1024] __attribute__((aligned(4096))); uint32_t Page_Table[1024] __attribute__((aligned(4096))); +/*Initializes Memory Paging **WARNING: CAN BORK THINGS WITHOUT MEM MANAGER***/ void Memory_Page_Init() { unsigned long address = 0; @@ -40,4 +47,4 @@ void Memory_Page_Init() // Enable Paging Write_CR3(Page_Directory); Write_CR0(Read_CR0() | 0x80000000); -} \ No newline at end of file +} diff --git a/src/kernel/core/memory/page.h b/src/kernel/core/memory/vmm.h similarity index 56% rename from src/kernel/core/memory/page.h rename to src/kernel/core/memory/vmm.h index a63e80c..bcfb31b 100644 --- a/src/kernel/core/memory/page.h +++ b/src/kernel/core/memory/vmm.h @@ -7,6 +7,18 @@ #include +#define PAGES_PER_TABLE 1024 +#define PAGES_PER_DIR 1024 +#define PAGE_SIZE 4096 + +typedef enum { + PTE +} PAGE_TABLE_FLAGS; + +typedef enum { + PDE +} PAGE_DIR_FLAGS; + void Memory_Page_Init(); struct page { diff --git a/src/kernel/dri/cmos.c b/src/kernel/dri/cmos.c index ba80ed9..3e162ca 100644 --- a/src/kernel/dri/cmos.c +++ b/src/kernel/dri/cmos.c @@ -21,6 +21,7 @@ int CMOS_Timer; +/*IRQ Handler for CMOS real time clock*/ void CMOS_RTC_Handler() { outb(0x70, 0x0C); // select register C @@ -28,11 +29,13 @@ void CMOS_RTC_Handler() CMOS_Timer++; } +/*Writes a value to the CMOS **DISABLED DOES NOT WORK***/ void Write_CMOS(uint8_t Register) { } +/*Reads a value from the onboard CMOS*/ uint8_t Read_CMOS(uint8_t Register) { uint8_t data; @@ -89,7 +92,7 @@ int Slave_FDD_Detect() return FDDType; } -// Basic, However Good Enough. Not like i'll ever support UEFI Lol +/*Basic Memory Detector Using CMOS, Its Good Enough. Not like i'll ever support UEFI Lol*/ int Detect_Memory() { outb(CMOS_ADDPORT, 0x30); int lowmem = inb(CMOS_DATAPORT); diff --git a/src/kernel/dri/keyboard.c b/src/kernel/dri/keyboard.c index 3b09391..969f008 100644 --- a/src/kernel/dri/keyboard.c +++ b/src/kernel/dri/keyboard.c @@ -17,14 +17,15 @@ #include #include -bool _keyboard_disable; -bool _numlock, _capslock, _scrolllock; -bool _ctrl, _alt, _shift; +bool _keyboard_disable; // True if keyboard is disabled, false if not +bool _numlock, _capslock, _scrolllock; // Is true when toggeld +bool _ctrl, _alt, _shift; // True if pressed -uint8_t _keyboard_scancode; +uint8_t _keyboard_scancode; // Buffer for keyboard's Scancodes extern uint16_t DEBUG_COM_PORT; +/*IRQ Handler for Keyboard inputs*/ void Keyboard_Handler() { _keyboard_scancode = inb(KEYBOARD_ENC_INPUT_BUF); @@ -54,6 +55,7 @@ uint8_t Keyboard_Read_Encoder_Buffer() return inb (KEYBOARD_ENC_INPUT_BUF); } +/*Sends a command to the Keyboard Controller, Look at enums in `keyboard.h` for more info*/ void Keyboard_Controller_Send_Command(uint8_t command) { // wait for controller input buffer to be clear @@ -64,6 +66,7 @@ void Keyboard_Controller_Send_Command(uint8_t command) outb (KEYBOARD_CTRL_CMD_REG, command); } +/*Sends a command to the Keyboard Encoder, Look at enums in `keyboard.h` for more info*/ void Keyboard_Encoder_Send_Command(uint8_t command) { // wait for controller input buffer to be clear @@ -76,6 +79,7 @@ void Keyboard_Encoder_Send_Command(uint8_t command) outb (KEYBOARD_ENC_CMD_REG, command); } +/*Sets LEDs for the different Keyboard locks*/ void Keyboard_Set_Lock_LEDs(bool num, bool caps, bool scroll) { uint8_t data = 0; @@ -87,6 +91,7 @@ void Keyboard_Set_Lock_LEDs(bool num, bool caps, bool scroll) Keyboard_Encoder_Send_Command(data); } +/*Tells Keyboard Controller to do a self test*/ bool Keyboard_Self_Test() { Keyboard_Controller_Send_Command(KEYBOARD_CTRL_CMD_SELF_TEST); @@ -98,6 +103,7 @@ bool Keyboard_Self_Test() return (Keyboard_Read_Encoder_Buffer() == 0x55) ? true : false; } +/*Disables Keyboard*/ void Keyboard_Disable() { Keyboard_Controller_Send_Command(KEYBOARD_CTRL_CMD_DISABLE); @@ -105,6 +111,7 @@ void Keyboard_Disable() Serial_Printf(DEBUG_COM_PORT, "KEYBOARD:> Disabled Input.\n"); } +/*Enables Keyboard*/ void Keyboard_Enable() { Keyboard_Controller_Send_Command(KEYBOARD_CTRL_CMD_ENABLE); @@ -112,6 +119,7 @@ void Keyboard_Enable() Serial_Printf(DEBUG_COM_PORT, "KEYBOARD:> Enabled Input.\n"); } +/*Completely Resets Keyboard encoder and controller*/ void Keyboard_Reset_System() { //! writes 11111110 to the output port (sets reset system line low) @@ -119,6 +127,7 @@ void Keyboard_Reset_System() Keyboard_Encoder_Send_Command(0xfe); } +/*Initialize Keyboard drivers*/ void Keyboard_Init() { // set lock keys and led lights diff --git a/src/kernel/dri/serial.c b/src/kernel/dri/serial.c index 56585c2..0931cde 100644 --- a/src/kernel/dri/serial.c +++ b/src/kernel/dri/serial.c @@ -14,8 +14,8 @@ #include #include -char Serial_Read_Buffer; -char Serial_Write_Buffer; +char Serial_Read_Buffer; // Read buffer for serial devices +char Serial_Write_Buffer; // Write buffer for serial devices /* IO Port Offset Setting of DLAB I/O Access Register mapped to this port @@ -34,15 +34,16 @@ IO Port Offset Setting of DLAB I/O Access Register mapped to this port */ -// Cannot Figure out what part of spec triggers this, -// OSDEV wiki just says it exists thats about it... +/*Cannot Figure out what part of spec triggers this, +OSDEV wiki just says it exists thats about it...*/ void COM1_Serial_Handler() { // Serial_Read_Buffer = Read_Serial(COM1_PORT); // printf("Serial Read Buffer: %s", Serial_Read_Buffer); - printf("IRQ 4 Triggered!"); + printf("IRQ 4 Triggered?!"); } +/*Initialize Serial Driver on `COM_Port` port with `baud` as baud rate*/ int Serial_Init(uint16_t COM_Port, int Baud) { uint8_t Divisor = 115200 / Baud; @@ -144,6 +145,7 @@ void Serial_Printf_Signed(uint16_t COM_Port,long long number, int radix) #define PRINTF_LENGTH_LONG 3 #define PRINTF_LENGTH_LONG_LONG 4 +/*Formatted Print function that goes over serial connections, requires serial driver to be initialized*/ void Serial_Printf(uint16_t COM_Port, const char* fmt, ...) { va_list args; diff --git a/src/kernel/dri/sound/pcspeaker.c b/src/kernel/dri/sound/pcspeaker.c index f681c39..c409701 100644 --- a/src/kernel/dri/sound/pcspeaker.c +++ b/src/kernel/dri/sound/pcspeaker.c @@ -20,6 +20,7 @@ extern uint16_t DEBUG_COM_PORT; +/*Play a frequency with the PC Speaker*/ void PCSP_Play(uint32_t nFrequence) { uint32_t Div; @@ -36,7 +37,7 @@ void PCSP_Play(uint32_t nFrequence) } } -// make it shut up +/*Make the PC Speaker shut up */ void PCSP_Mute() { uint8_t tmp = inb(0x61) & 0xFC; @@ -44,7 +45,7 @@ void PCSP_Mute() outb(0x61, tmp); } -// Make a beep +/*Make a beep*/ void PCSP_Beep() { PCSP_Play(1000); diff --git a/src/kernel/dri/timer.c b/src/kernel/dri/timer.c index fcc7647..9eccf1b 100644 --- a/src/kernel/dri/timer.c +++ b/src/kernel/dri/timer.c @@ -12,11 +12,13 @@ extern uint16_t DEBUG_COM_PORT; int uptime; +/*CPU Precision Timer IRQ Handler*/ void Timer_Handler() { uptime++; } +/*Uses CPU Precision Timer to Wait for something*/ void Timer_Wait(int time) { int tmp; diff --git a/src/kernel/dri/video/vesa/vesa.c b/src/kernel/dri/video/vesa/vesa.c new file mode 100644 index 0000000..9c0b6f1 --- /dev/null +++ b/src/kernel/dri/video/vesa/vesa.c @@ -0,0 +1,7 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2026| +|Xircon | +\*----------------*/ +#include "vesa.h" + diff --git a/src/kernel/dri/video/vesa/vesa.h b/src/kernel/dri/video/vesa/vesa.h new file mode 100644 index 0000000..dd5fc91 --- /dev/null +++ b/src/kernel/dri/video/vesa/vesa.h @@ -0,0 +1,18 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2026| +|Xircon | +\*----------------*/ +#pragma once + +#include + +struct VbeInfoBlock { + char VbeSignature[4]; // == "VESA" + uint16_t VbeVersion; // == 0x0300 for VBE 3.0 + uint16_t OemStringPtr[2]; // isa vbeFarPtr + uint8_t Capabilities[4]; + uint16_t VideoModePtr[2]; // isa vbeFarPtr + uint16_t TotalMemory; // as # of 64KB blocks + uint8_t Reserved[492]; +} __attribute__((packed)); \ No newline at end of file diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld index 138f1b6..6022929 100644 --- a/src/kernel/linker.ld +++ b/src/kernel/linker.ld @@ -1,14 +1,14 @@ -ENTRY(start) +ENTRY(_start) SECTIONS { - . = ALIGN(8); - - .boot : { KEEP(*(.multiboot_header)) __boot_start = .; *(.boot) } + .multiboot : { __multiboot_start = .; *(.multiboot)} + .notes : { __notes_start = .; *(.notes) } + .boot : { __boot_start = .; *(.boot) } .entry : { __entry_start = .; *(.entry) } - .text : { __text_start = .; *(.text) } + .text : { __text_start = .; *(.text) } .data : { __data_start = .; *(.data) } - .rodata : { __rodata_start = .; *(.rodata) } + .rodata : { __rodata_start = .; *(.rodata*) } .bss : { __bss_start = .; *(.bss) } __end = .; diff --git a/src/kernel/main.c b/src/kernel/main.c index d3af891..7f5874c 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -13,6 +13,10 @@ #include #endif +#ifdef __XTENSA + +#endif + // stdlibs #include @@ -25,7 +29,7 @@ #include #include #include -#include +#include #include #include #include @@ -33,7 +37,7 @@ // Core #include -#include +#include // Utilities @@ -44,54 +48,73 @@ // Other #include +#include // Declarations -extern uint8_t __bss_start; -extern uint8_t __end; +extern uint8_t __bss_start; // Start of Kernel in memory +extern uint8_t __end; // End of Kernel in memory -extern int uptime; +extern int uptime; // System uptime measured by precision timer unit. -int Memory; // Amount of Available RAM To OS +uint16_t DEBUG_COM_PORT = COM1_PORT; // COM Port to Output Debug strings to -uint16_t DEBUG_COM_PORT = COM1_PORT; -void start() { +/*Main Kernel Function*/ +void start(unsigned long magic, unsigned long address) { + + struct multiboot_tag* tag; // Print logo clrscr(); printf("%s", LOGO); printf("The Nano OS %s\n-------------------------------------\n", VERSION); - - // Init Drivers + + // Multiboot Debug Stuff + // if (magic != MULTIBOOT2_BOOTLOADER_MAGIC) { + // printf ("Invalid magic number: 0x%x\n", (unsigned) magic); + // return; + // } + + // if (address & 7) { + // printf("Unaligned mbi: 0x%x\n", address); + // return; + // } + printf("Initializing Basic Drivers..."); // HAL HAL_Initialize(); // Register IRQs + #ifdef __I686 IRQ_RegisterHandler(0, Timer_Handler); IRQ_RegisterHandler(1, Keyboard_Handler); IRQ_RegisterHandler(4, COM1_Serial_Handler); IRQ_RegisterHandler(6, Floppy_Handler); IRQ_RegisterHandler(8, CMOS_RTC_Handler); + #endif // Other Stuff Serial_Init(DEBUG_COM_PORT, 9600); Keyboard_Init(); // Storage // Floppy_Init(); // This should always be last; its slow as fuck printf("Done!\n"); - // Serial_Printf(DEBUG_COM_PORT, "CPU Vendor: %s\n", CPUID_Get_Vendor()); // CPUID Stuff is pain and suffering, like page faults pain and suffering - printf("Initializing Memory Paging..."); - Memory = Detect_Memory(); - Serial_Printf(DEBUG_COM_PORT, "MEMORY:> There is %dKB of Memory Available\n", Memory); - Memory_Page_Init(); - printf("Done!\n"); + + // Hardware Detection + Serial_Printf(DEBUG_COM_PORT, "MEMORY:> There is %dKB of Memory Available\nThe Kernel is loaded between 0x%x and 0x%x\n", Detect_Memory(), __bss_start, __end); + printf("%dKB Of Memory Availible!\n", Detect_Memory()); + + // Serial_Printf(DEBUG_COM_PORT, "CPU Vendor: %s\n", CPUID_Get_Vendor()); // CPUID Stuff is pain and suffering, like page faults and suffering + + // printf("Initializing Memory Paging..."); + // Memory_Page_Init(); + // printf("Done!\n"); // Finish Up printf("The Current Time and Date Is: %d:%d:%d %d/%d/%d%d\n", BCD2BIN(Read_CMOS(CMOS_RTC_Hours)), - BCD2BIN(Read_CMOS(CMOS_RTC_Minutes)), + BCD2BIN(Read_CMOS(CMOS_RTC_Minutes)), BCD2BIN(Read_CMOS(CMOS_RTC_Seconds)), - BCD2BIN(Read_CMOS(CMOS_RTC_Month)), BCD2BIN(Read_CMOS(CMOS_RTC_Day)), + BCD2BIN(Read_CMOS(CMOS_RTC_Month)), BCD2BIN(Read_CMOS(CMOS_RTC_Century)), BCD2BIN(Read_CMOS(CMOS_RTC_Year))); // Beep! diff --git a/src/kernel/multiboot.h b/src/kernel/multiboot.h new file mode 100644 index 0000000..9ea887b --- /dev/null +++ b/src/kernel/multiboot.h @@ -0,0 +1,417 @@ +/* multiboot2.h - Multiboot 2 header file. */ +/* Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY + * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR + * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef MULTIBOOT_HEADER +#define MULTIBOOT_HEADER 1 + +/* How many bytes from the start of the file we search for the header. */ +#define MULTIBOOT_SEARCH 32768 +#define MULTIBOOT_HEADER_ALIGN 8 + +/* The magic field should contain this. */ +#define MULTIBOOT2_HEADER_MAGIC 0xe85250d6 + +/* This should be in %eax. */ +#define MULTIBOOT2_BOOTLOADER_MAGIC 0x36d76289 + +/* Alignment of multiboot modules. */ +#define MULTIBOOT_MOD_ALIGN 0x00001000 + +/* Alignment of the multiboot info structure. */ +#define MULTIBOOT_INFO_ALIGN 0x00000008 + +/* Flags set in the ’flags’ member of the multiboot header. */ + +#define MULTIBOOT_TAG_ALIGN 8 +#define MULTIBOOT_TAG_TYPE_END 0 +#define MULTIBOOT_TAG_TYPE_CMDLINE 1 +#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 +#define MULTIBOOT_TAG_TYPE_MODULE 3 +#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 +#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 +#define MULTIBOOT_TAG_TYPE_MMAP 6 +#define MULTIBOOT_TAG_TYPE_VBE 7 +#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 +#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9 +#define MULTIBOOT_TAG_TYPE_APM 10 +#define MULTIBOOT_TAG_TYPE_EFI32 11 +#define MULTIBOOT_TAG_TYPE_EFI64 12 +#define MULTIBOOT_TAG_TYPE_SMBIOS 13 +#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14 +#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15 +#define MULTIBOOT_TAG_TYPE_NETWORK 16 +#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17 +#define MULTIBOOT_TAG_TYPE_EFI_BS 18 +#define MULTIBOOT_TAG_TYPE_EFI32_IH 19 +#define MULTIBOOT_TAG_TYPE_EFI64_IH 20 +#define MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR 21 + +#define MULTIBOOT_HEADER_TAG_END 0 +#define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST 1 +#define MULTIBOOT_HEADER_TAG_ADDRESS 2 +#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS 3 +#define MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS 4 +#define MULTIBOOT_HEADER_TAG_FRAMEBUFFER 5 +#define MULTIBOOT_HEADER_TAG_MODULE_ALIGN 6 +#define MULTIBOOT_HEADER_TAG_EFI_BS 7 +#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 8 +#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 9 +#define MULTIBOOT_HEADER_TAG_RELOCATABLE 10 + +#define MULTIBOOT_ARCHITECTURE_I386 0 +#define MULTIBOOT_ARCHITECTURE_MIPS32 4 +#define MULTIBOOT_HEADER_TAG_OPTIONAL 1 + +#define MULTIBOOT_LOAD_PREFERENCE_NONE 0 +#define MULTIBOOT_LOAD_PREFERENCE_LOW 1 +#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2 + +#define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1 +#define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2 + +#ifndef ASM_FILE + +typedef unsigned char multiboot_uint8_t; +typedef unsigned short multiboot_uint16_t; +typedef unsigned int multiboot_uint32_t; +typedef unsigned long long multiboot_uint64_t; + +struct multiboot_header +{ + /* Must be MULTIBOOT_MAGIC - see above. */ + multiboot_uint32_t magic; + + /* ISA */ + multiboot_uint32_t architecture; + + /* Total header length. */ + multiboot_uint32_t header_length; + + /* The above fields plus this one must equal 0 mod 2^32. */ + multiboot_uint32_t checksum; +}; + +struct multiboot_header_tag +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; +}; + +struct multiboot_header_tag_information_request +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; + multiboot_uint32_t requests[0]; +}; + +struct multiboot_header_tag_address +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; + multiboot_uint32_t header_addr; + multiboot_uint32_t load_addr; + multiboot_uint32_t load_end_addr; + multiboot_uint32_t bss_end_addr; +}; + +struct multiboot_header_tag_entry_address +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; + multiboot_uint32_t entry_addr; +}; + +struct multiboot_header_tag_console_flags +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; + multiboot_uint32_t console_flags; +}; + +struct multiboot_header_tag_framebuffer +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; + multiboot_uint32_t width; + multiboot_uint32_t height; + multiboot_uint32_t depth; +}; + +struct multiboot_header_tag_module_align +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; +}; + +struct multiboot_header_tag_relocatable +{ + multiboot_uint16_t type; + multiboot_uint16_t flags; + multiboot_uint32_t size; + multiboot_uint32_t min_addr; + multiboot_uint32_t max_addr; + multiboot_uint32_t align; + multiboot_uint32_t preference; +}; + +struct multiboot_color +{ + multiboot_uint8_t red; + multiboot_uint8_t green; + multiboot_uint8_t blue; +}; + +struct multiboot_mmap_entry +{ + multiboot_uint64_t addr; + multiboot_uint64_t len; +#define MULTIBOOT_MEMORY_AVAILABLE 1 +#define MULTIBOOT_MEMORY_RESERVED 2 +#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 +#define MULTIBOOT_MEMORY_NVS 4 +#define MULTIBOOT_MEMORY_BADRAM 5 + multiboot_uint32_t type; + multiboot_uint32_t zero; +}; +typedef struct multiboot_mmap_entry multiboot_memory_map_t; + +struct multiboot_tag +{ + multiboot_uint32_t type; + multiboot_uint32_t size; +}; + +struct multiboot_tag_string +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + char string[0]; +}; + +struct multiboot_tag_module +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t mod_start; + multiboot_uint32_t mod_end; + char cmdline[0]; +}; + +struct multiboot_tag_basic_meminfo +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t mem_lower; + multiboot_uint32_t mem_upper; +}; + +struct multiboot_tag_bootdev +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t biosdev; + multiboot_uint32_t slice; + multiboot_uint32_t part; +}; + +struct multiboot_tag_mmap +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t entry_size; + multiboot_uint32_t entry_version; + struct multiboot_mmap_entry entries[0]; +}; + +struct multiboot_vbe_info_block +{ + multiboot_uint8_t external_specification[512]; +}; + +struct multiboot_vbe_mode_info_block +{ + multiboot_uint8_t external_specification[256]; +}; + +struct multiboot_tag_vbe +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + + multiboot_uint16_t vbe_mode; + multiboot_uint16_t vbe_interface_seg; + multiboot_uint16_t vbe_interface_off; + multiboot_uint16_t vbe_interface_len; + + struct multiboot_vbe_info_block vbe_control_info; + struct multiboot_vbe_mode_info_block vbe_mode_info; +}; + +struct multiboot_tag_framebuffer_common +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + + multiboot_uint64_t framebuffer_addr; + multiboot_uint32_t framebuffer_pitch; + multiboot_uint32_t framebuffer_width; + multiboot_uint32_t framebuffer_height; + multiboot_uint8_t framebuffer_bpp; +#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0 +#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 +#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 + multiboot_uint8_t framebuffer_type; + multiboot_uint16_t reserved; +}; + +struct multiboot_tag_framebuffer +{ + struct multiboot_tag_framebuffer_common common; + + union + { + struct + { + multiboot_uint16_t framebuffer_palette_num_colors; + struct multiboot_color framebuffer_palette[0]; + }; + struct + { + multiboot_uint8_t framebuffer_red_field_position; + multiboot_uint8_t framebuffer_red_mask_size; + multiboot_uint8_t framebuffer_green_field_position; + multiboot_uint8_t framebuffer_green_mask_size; + multiboot_uint8_t framebuffer_blue_field_position; + multiboot_uint8_t framebuffer_blue_mask_size; + }; + }; +}; + +struct multiboot_tag_elf_sections +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t num; + multiboot_uint32_t entsize; + multiboot_uint32_t shndx; + char sections[0]; +}; + +struct multiboot_tag_apm +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint16_t version; + multiboot_uint16_t cseg; + multiboot_uint32_t offset; + multiboot_uint16_t cseg_16; + multiboot_uint16_t dseg; + multiboot_uint16_t flags; + multiboot_uint16_t cseg_len; + multiboot_uint16_t cseg_16_len; + multiboot_uint16_t dseg_len; +}; + +struct multiboot_tag_efi32 +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t pointer; +}; + +struct multiboot_tag_efi64 +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint64_t pointer; +}; + +struct multiboot_tag_smbios +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint8_t major; + multiboot_uint8_t minor; + multiboot_uint8_t reserved[6]; + multiboot_uint8_t tables[0]; +}; + +struct multiboot_tag_old_acpi +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint8_t rsdp[0]; +}; + +struct multiboot_tag_new_acpi +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint8_t rsdp[0]; +}; + +struct multiboot_tag_network +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint8_t dhcpack[0]; +}; + +struct multiboot_tag_efi_mmap +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t descr_size; + multiboot_uint32_t descr_vers; + multiboot_uint8_t efi_mmap[0]; +}; + +struct multiboot_tag_efi32_ih +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t pointer; +}; + +struct multiboot_tag_efi64_ih +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint64_t pointer; +}; + +struct multiboot_tag_load_base_addr +{ + multiboot_uint32_t type; + multiboot_uint32_t size; + multiboot_uint32_t load_base_addr; +}; + +#endif /* ! ASM_FILE */ + +#endif /* ! MULTIBOOT_HEADER */ \ No newline at end of file diff --git a/src/kernel/nanstd.c b/src/kernel/nanstd.c new file mode 100644 index 0000000..f9319da --- /dev/null +++ b/src/kernel/nanstd.c @@ -0,0 +1,21 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2026| +|Xircon | +\*----------------*/ +#include "nanstd.h" + +#include + +extern uint8_t _keyboard_scancode; + +char kscan() { + switch(_keyboard_scancode) { + case 1: + default: return 0; + } +} + +void kscanHelper(char character) { + +} \ No newline at end of file diff --git a/src/kernel/nanstd.h b/src/kernel/nanstd.h new file mode 100644 index 0000000..e05de0c --- /dev/null +++ b/src/kernel/nanstd.h @@ -0,0 +1,7 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2026| +|Xircon | +\*----------------*/ +#pragma once + diff --git a/src/kernel/stdio.c b/src/kernel/stdio.c index 09dbfaa..00d3944 100644 --- a/src/kernel/stdio.c +++ b/src/kernel/stdio.c @@ -13,7 +13,7 @@ #include -const uint8_t DEFAULT_COLOUR = 0x7; +const uint8_t DEFAULT_COLOUR = 0x6; const unsigned SCREEN_WIDTH = 80; const unsigned SCREEN_HEIGHT = 25; @@ -21,11 +21,13 @@ const unsigned SCREEN_HEIGHT = 25; uint8_t* g_ScreenBuffer = (uint8_t*)0xB8000; int g_ScreenX = 0, g_ScreenY = 0; +/*Moves Cursor to `x`,`y` position given*/ void movecursorpos(int x, int y) { g_ScreenX = x; g_ScreenY = y; } +/*Gets current Cursor Position and outputs the X,Y coords to `x` and `y` respectively*/ void getcursorpos(int x, int y) { x = g_ScreenX; y = g_ScreenY; @@ -61,7 +63,8 @@ void setcursor(int x, int y) outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); } -void clrscr() +/*Clears Screen in BIOS Mode*/ +void clrscr() { for (int y = 0; y < SCREEN_HEIGHT; y++) for (int x = 0; x < SCREEN_WIDTH; x++) @@ -182,6 +185,7 @@ void printf_signed(long long number, int radix) #define PRINTF_LENGTH_LONG 3 #define PRINTF_LENGTH_LONG_LONG 4 +/*Formatted Print function*/ void printf(const char* fmt, ...) { va_list args; @@ -336,3 +340,4 @@ void print_buffer(const char* msg, const void* buffer, uint32_t count) } puts("\n"); } + diff --git a/src/kernel/util/binary.h b/src/kernel/util/binary.h index 6cf709b..3404755 100644 --- a/src/kernel/util/binary.h +++ b/src/kernel/util/binary.h @@ -9,7 +9,7 @@ #define FLAG_SET(x, flag) x |= (flag) #define FLAG_UNSET(x, flag) x &= ~(flag) -#define BCD2BIN(bcd) ((((bcd)&15) + ((bcd)>>4)*10)) +#define BCD2BIN(bcd) ((((bcd)&15) + ((bcd)>>4)*10)) // Takes value in `bcd` and outputs it as binary uint8_t Get_Bit(uint8_t bits, uint8_t pos); diff --git a/src/kernel/version.h b/src/kernel/version.h index ce934d8..ea80392 100644 --- a/src/kernel/version.h +++ b/src/kernel/version.h @@ -5,7 +5,7 @@ \*----------------*/ #pragma once -#define LOGO " _ _____ _ __________________\n / | / / | / | / / _/_ __/ ____/\n / |/ / /| | / |/ // / / / / __/ \n / /| / ___ |/ /| // / / / / /___ \n/_/ |_/_/ |_/_/ |_/___/ /_/ /_____/ \n" -#define VERSION "RD-00054" -#define BOOTLOGO " _ ______ ____ ____ ______\n / | / / __ )/ __ \\/ __ /_ __/\n / |/ / __ / / / / / / // / \n / /| / /_/ / /_/ / /_/ // / \n/_/ |_/_____/\\____/\\____//_/ \n" +#define LOGO " _ _____ _ __________________\n / | / / | / | / / _/_ __/ ____/\n / |/ / /| | / |/ // / / / / __/ \n / /| / ___ |/ /| // / / / / /___ \n/_/ |_/_/ |_/_/ |_/___/ /_/ /_____/ \n" // Nanite's Logo as a formatted string +#define VERSION "RD-00055" // Current Version of Nanite +#define BOOTLOGO " _ ______ ____ ____ ______\n / | / / __ )/ __ \\/ __ /_ __/\n / |/ / __ / / / / / / // / \n / /| / /_/ / /_/ / /_/ // / \n/_/ |_/_____/\\____/\\____//_/ \n" // Old bootloader logo // one day i will re-implement NBOOT V 2