From 4d55b05585001057913994c6c1d8cc207e91ed64 Mon Sep 17 00:00:00 2001 From: Tyler McGurrin Date: Tue, 17 Dec 2024 23:28:07 -0500 Subject: [PATCH] KERNEL TIME BOYS --- Makefile | 4 +- build_scripts/toolchain.mk | 4 +- src/bootloader/stage2/Makefile | 1 - src/bootloader/stage2/fat.c | 452 +++++++++++++++++--------------- src/bootloader/stage2/main.c | 49 ++-- src/bootloader/stage2/memdefs.h | 5 + src/bootloader/stage2/stdio.c | 1 + src/kernel/Makefile | 26 +- src/kernel/arch/i686/io.asm | 21 ++ src/kernel/arch/i686/io.h | 11 + src/kernel/linker.ld | 9 +- src/kernel/main.c | 23 +- src/kernel/memory.c | 36 +++ src/kernel/memory.h | 11 + src/kernel/stdio.c | 6 +- src/kernel/stdio.h | 1 + 16 files changed, 399 insertions(+), 261 deletions(-) create mode 100644 src/kernel/arch/i686/io.asm create mode 100644 src/kernel/arch/i686/io.h create mode 100644 src/kernel/memory.c create mode 100644 src/kernel/memory.h diff --git a/Makefile b/Makefile index a467caf..05510e6 100644 --- a/Makefile +++ b/Makefile @@ -18,9 +18,9 @@ $(BUILD_DIR)/main_floppy.img: bootloader kernel dd if=$(BUILD_DIR)/stage1.bin of=$(BUILD_DIR)/main_floppy.img conv=notrunc mcopy -i $(BUILD_DIR)/main_floppy.img $(BUILD_DIR)/stage2.bin "::stage2.bin" mmd -i $(BUILD_DIR)/main_floppy.img "::boot" - mcopy -i $(BUILD_DIR)/main_floppy.img $(BUILD_DIR)/kernel.bin "::boot/kernel.bin" + mcopy -i $(BUILD_DIR)/main_floppy.img $(BUILD_DIR)/kernel.bin "::kernel.bin" # move to boot dir mmd -i $(BUILD_DIR)/main_floppy.img "::misc" - mcopy -i $(BUILD_DIR)/main_floppy.img test.txt "::misc/test.txt" + mcopy -i $(BUILD_DIR)/main_floppy.img test.txt "::test.txt" # move to misc dir # # Bootloader diff --git a/build_scripts/toolchain.mk b/build_scripts/toolchain.mk index 19e5bdf..08e6509 100644 --- a/build_scripts/toolchain.mk +++ b/build_scripts/toolchain.mk @@ -11,7 +11,7 @@ toolchain_binutils: $(BINUTILS_SRC).tar.gz cd toolchain && tar -xf binutils-$(BINUTILS_VERSION).tar.gz mkdir -p $(BINUTILS_BUILD) - cd $(BINUTILS_BUILD) && ../binutils-$(BINUTILS_VERSION)/configure \ + cd $(BINUTILS_BUILD) && CFLAGS = ASM = ASMFLAGS = CC = CXX = LD = LINKFLAGS = LIBS = ../binutils-$(BINUTILS_VERSION)/configure \ --prefix="$(TOOLCHAIN_PREFIX)" \ --target=$(TARGET) \ --with-sysroot \ @@ -31,7 +31,7 @@ GCC_BUILD = toolchain/gcc-build-$(GCC_VERSION) toolchain_gcc: toolchain_binutils $(GCC_SRC).tar.gz cd toolchain && tar -xf gcc-$(GCC_VERSION).tar.gz mkdir -p $(GCC_BUILD) - cd $(GCC_BUILD) && ../gcc-$(GCC_VERSION)/configure \ + cd $(GCC_BUILD) && CFLAGS = ASM = ASMFLAGS = CC = CXX = LD = LINKFLAGS = LIBS = ../gcc-$(GCC_VERSION)/configure \ --prefix="$(TOOLCHAIN_PREFIX)" \ --target=$(TARGET) \ --disable-nls \ diff --git a/src/bootloader/stage2/Makefile b/src/bootloader/stage2/Makefile index 0df9738..914feba 100644 --- a/src/bootloader/stage2/Makefile +++ b/src/bootloader/stage2/Makefile @@ -3,7 +3,6 @@ TARGET_CFLAGS += -ffreestanding -nostdlib TARGET_LIBS += -lgcc TARGET_LINKFLAGS += -T linker.ld -nostdlib - SOURCES_C=$(wildcard *.c) SOURCES_ASM=$(wildcard *.asm) OBJECTS_C=$(patsubst %.c, $(BUILD_DIR)/stage2/c/%.obj, $(SOURCES_C)) diff --git a/src/bootloader/stage2/fat.c b/src/bootloader/stage2/fat.c index 300c8e2..df2e9b6 100644 --- a/src/bootloader/stage2/fat.c +++ b/src/bootloader/stage2/fat.c @@ -9,13 +9,13 @@ #include "string.h" #include "memory.h" #include "ctype.h" -#include "minmax.h" #include +#include "minmax.h" -#define SECTOR_SIZE 512 -#define MAX_PATH_SIZE 256 -#define MAX_FILE_HANDLES 10 -#define ROOT_DIRECTORY_HANDLE -1 +#define SECTOR_SIZE 512 +#define MAX_PATH_SIZE 256 +#define MAX_FILE_HANDLES 10 +#define ROOT_DIRECTORY_HANDLE -1 typedef struct { @@ -76,83 +76,96 @@ static FAT_Data* g_Data; static uint8_t* g_Fat = NULL; static uint32_t g_DataSectionLba; -bool FAT_ReadBootSector(DISK* disk) { - return DISK_ReadSectors(disk, 0, 1, &g_Data->BS.BootSectorBytes); + +bool FAT_ReadBootSector(DISK* disk) +{ + return DISK_ReadSectors(disk, 0, 1, g_Data->BS.BootSectorBytes); } bool FAT_ReadFat(DISK* disk) { - return DISK_ReadSectors(disk, g_Data->BS.BootSector.ReservedSectors, g_Data->BS.BootSector.SectorsPerFat, g_Fat); + return DISK_ReadSectors(disk, g_Data->BS.BootSector.ReservedSectors, g_Data->BS.BootSector.SectorsPerFat, g_Fat); } -bool FAT_Initialize(DISK* disk) { - g_Data = (FAT_Data*)MEMORY_FAT_ADDR; +bool FAT_Initialize(DISK* disk) +{ + g_Data = (FAT_Data*)MEMORY_FAT_ADDR; - // read bootsector - if (!FAT_ReadBootSector(disk)) { - printf("FAT: Bootsector Read Failed!\r\n"); - return false; - } + // read boot sector + if (!FAT_ReadBootSector(disk)) + { + printf("FAT: read boot sector failed\r\n"); + return false; + } - // read FAT - g_Fat = (uint8_t*)(g_Data + sizeof(FAT_Data)); - uint32_t fatSize = g_Data->BS.BootSector.BytesPerSector * g_Data->BS.BootSector.SectorsPerFat; - if(sizeof(FAT_Data) + fatSize >= MEMORY_FAT_SIZE) { - printf("FAT: Not Enough Memory to Read FAT!\r\nNeeded %u, Only Have %u\r\n", sizeof(FAT_Data) + fatSize, MEMORY_FAT_SIZE); - return false; - } + // read FAT + g_Fat = (uint8_t*)g_Data + sizeof(FAT_Data); + uint32_t fatSize = g_Data->BS.BootSector.BytesPerSector * g_Data->BS.BootSector.SectorsPerFat; + if (sizeof(FAT_Data) + fatSize >= MEMORY_FAT_SIZE) + { + printf("FAT: not enough memory to read FAT! Required %lu, only have %u\r\n", sizeof(FAT_Data) + fatSize, MEMORY_FAT_SIZE); + return false; + } - if (!FAT_ReadFat(disk)) { - printf("FAT: Failed to Read FAT!\r\n"); - return false; - } + if (!FAT_ReadFat(disk)) + { + printf("FAT: read FAT failed\r\n"); + return false; + } - // open root dir - uint32_t rootDirLba = g_Data->BS.BootSector.ReservedSectors + g_Data->BS.BootSector.SectorsPerFat * g_Data->BS.BootSector.FatCount; - uint32_t rootDirSize = sizeof(FAT_DirectoryEntry) * g_Data->BS.BootSector.DirEntryCount; + // open root directory file + uint32_t rootDirLba = g_Data->BS.BootSector.ReservedSectors + g_Data->BS.BootSector.SectorsPerFat * g_Data->BS.BootSector.FatCount; + uint32_t rootDirSize = sizeof(FAT_DirectoryEntry) * g_Data->BS.BootSector.DirEntryCount; - g_Data->RootDirectory.Public.Handle = ROOT_DIRECTORY_HANDLE; - g_Data->RootDirectory.Public.IsDirectory = true; - g_Data->RootDirectory.Public.Position = 0; - g_Data->RootDirectory.Public.Size = sizeof(FAT_DirectoryEntry) * g_Data->BS.BootSector.DirEntryCount; - g_Data->RootDirectory.Opened = true; - g_Data->RootDirectory.FirstCluster = rootDirLba; - g_Data->RootDirectory.CurrentCluster = 0; - g_Data->RootDirectory.CurrentSectorInCluster = 0; + g_Data->RootDirectory.Public.Handle = ROOT_DIRECTORY_HANDLE; + g_Data->RootDirectory.Public.IsDirectory = true; + g_Data->RootDirectory.Public.Position = 0; + g_Data->RootDirectory.Public.Size = sizeof(FAT_DirectoryEntry) * g_Data->BS.BootSector.DirEntryCount; + g_Data->RootDirectory.Opened = true; + g_Data->RootDirectory.FirstCluster = rootDirLba; + g_Data->RootDirectory.CurrentCluster = rootDirLba; + g_Data->RootDirectory.CurrentSectorInCluster = 0; - if (!DISK_ReadSectors(disk, rootDirLba, 1, g_Data->RootDirectory.Buffer)) { - printf("FAT: Failed to Read Root Directory!\r\n"); - return false; - } - //calculate data - uint32_t rootDirSectors = (rootDirSize + g_Data->BS.BootSector.BytesPerSector - 1) / g_Data->BS.BootSector.BytesPerSector; - g_DataSectionLba = rootDirLba + rootDirSectors; + if (!DISK_ReadSectors(disk, rootDirLba, 1, g_Data->RootDirectory.Buffer)) + { + printf("FAT: read root directory failed\r\n"); + return false; + } - // reset opened files - for (int i = 0; i < MAX_FILE_HANDLES; i++) - g_Data->OpenedFiles[i].Opened = false; + // calculate data section + uint32_t rootDirSectors = (rootDirSize + g_Data->BS.BootSector.BytesPerSector - 1) / g_Data->BS.BootSector.BytesPerSector; + g_DataSectionLba = rootDirLba + rootDirSectors; - return true; + // reset opened files + for (int i = 0; i < MAX_FILE_HANDLES; i++) + g_Data->OpenedFiles[i].Opened = false; + + return true; } -uint32_t FAT_ClusterToLba(uint32_t cluster) { +uint32_t FAT_ClusterToLba(uint32_t cluster) +{ return g_DataSectionLba + (cluster - 2) * g_Data->BS.BootSector.SectorsPerCluster; } -FAT_File* FAT_OpenEntry(DISK* disk, FAT_DirectoryEntry* entry) { - // find empty handle - int handle = -1; - for (int i = 0; i < MAX_FILE_HANDLES && handle < 0; i++) { - if (!g_Data->OpenedFiles[i].Opened) - handle = i; - } - // out of handles - if (handle < 0) { - printf("FAT: Out of File Handles!\r\n"); - return false; - } +FAT_File* FAT_OpenEntry(DISK* disk, FAT_DirectoryEntry* entry) +{ + // find empty handle + int handle = -1; + for (int i = 0; i < MAX_FILE_HANDLES && handle < 0; i++) + { + if (!g_Data->OpenedFiles[i].Opened) + handle = i; + } - // setup vars + // out of handles + if (handle < 0) + { + printf("FAT: out of file handles\r\n"); + return false; + } + + // setup vars FAT_FileData* fd = &g_Data->OpenedFiles[handle]; fd->Public.Handle = handle; fd->Public.IsDirectory = (entry->Attributes & FAT_ATTRIBUTE_DIRECTORY) != 0; @@ -162,175 +175,202 @@ FAT_File* FAT_OpenEntry(DISK* disk, FAT_DirectoryEntry* entry) { fd->CurrentCluster = fd->FirstCluster; fd->CurrentSectorInCluster = 0; - if (!DISK_ReadSectors(disk, FAT_ClusterToLba(fd->CurrentCluster), 1, fd->Buffer)) { - printf("FAT: Failed to Open Entry, Read Error!\nCluster Is: %u\n", fd->CurrentCluster); - for (int i = 0; i < 11; i++) - printf("%c", entry->Name[i]); - printf("\n"); - return false; - } + if (!DISK_ReadSectors(disk, FAT_ClusterToLba(fd->CurrentCluster), 1, fd->Buffer)) + { + printf("FAT: open entry failed - read error cluster=%u lba=%u\n", fd->CurrentCluster, FAT_ClusterToLba(fd->CurrentCluster)); + for (int i = 0; i < 11; i++) + printf("%c", entry->Name[i]); + printf("\n"); + return false; + } - fd->Opened = true; - return &fd->Public; + fd->Opened = true; + return &fd->Public; } -uint32_t FAT_NextCluster(uint32_t currentCluster) { - uint32_t fatIndex = currentCluster * 3 / 2; - if (currentCluster % 2 == 0) - return (*(uint16_t*)(g_Fat + fatIndex)) & 0x0FFF; - else - return (*(uint16_t*)(g_Fat + fatIndex)) >> 4; +uint32_t FAT_NextCluster(uint32_t currentCluster) +{ + uint32_t fatIndex = currentCluster * 3 / 2; + if (currentCluster % 2 == 0) + return (*(uint16_t*)(g_Fat + fatIndex)) & 0x0FFF; + else + return (*(uint16_t*)(g_Fat + fatIndex)) >> 4; } -uint32_t FAT_Read(DISK* disk, FAT_File* file, uint32_t byteCount, void* dataOut) { - // get file data - FAT_FileData* fd = (file->Handle == ROOT_DIRECTORY_HANDLE) - ? &g_Data->RootDirectory - : &g_Data->OpenedFiles[file->Handle]; +uint32_t FAT_Read(DISK* disk, FAT_File* file, uint32_t byteCount, void* dataOut) +{ + // get file data + FAT_FileData* fd = (file->Handle == ROOT_DIRECTORY_HANDLE) + ? &g_Data->RootDirectory + : &g_Data->OpenedFiles[file->Handle]; - uint8_t* u8DataOut = (uint8_t*)dataOut; - - // don't read past EOF - if (!fd->Public.IsDirectory || (fd->Public.IsDirectory && fd->Public.Size != 0)) - byteCount = min(byteCount, fd->Public.Size - fd->Public.Position); - - while (byteCount > 0) { - uint32_t leftInBuffer = SECTOR_SIZE - (fd->Public.Position % SECTOR_SIZE); - uint32_t take = min(byteCount, leftInBuffer); - memcpy(u8DataOut, fd->Buffer + fd->Public.Position % SECTOR_SIZE, take); - u8DataOut += take; - fd->Public.Position += take; - byteCount -= take; + uint8_t* u8DataOut = (uint8_t*)dataOut; - // see if we need to read more data - if (leftInBuffer == take) { - // root dir handler - if (fd->Public.Handle == ROOT_DIRECTORY_HANDLE) { - ++fd->CurrentCluster; - // read next sect. - if (!DISK_ReadSectors(disk, fd->CurrentCluster, 1, fd->Buffer)) { - printf("FAT: Read Error!\r\n"); - break; - } - } - else { - // read next sect. - if (++fd->CurrentSectorInCluster >= g_Data->BS.BootSector.SectorsPerCluster) { - // calc next cluster & sect. to read - fd->CurrentSectorInCluster = 0; - fd->CurrentCluster = FAT_NextCluster(fd->CurrentCluster); - } - if (fd->CurrentCluster >= 0x0FF8) { - // mark EOF - fd->Public.Size = fd->Public.Position; - break; + // don't read past the end of the file + if (!fd->Public.IsDirectory || (fd->Public.IsDirectory && fd->Public.Size != 0)) + byteCount = min(byteCount, fd->Public.Size - fd->Public.Position); - } + while (byteCount > 0) + { + uint32_t leftInBuffer = SECTOR_SIZE - (fd->Public.Position % SECTOR_SIZE); + uint32_t take = min(byteCount, leftInBuffer); - // read next sect. - if (!DISK_ReadSectors(disk, FAT_ClusterToLba(fd->CurrentCluster) + fd->CurrentSectorInCluster, 1, fd->Buffer)) { - printf("FAT: Read Error!\r\n"); - break; - } - } - } - } + memcpy(u8DataOut, fd->Buffer + fd->Public.Position % SECTOR_SIZE, take); + u8DataOut += take; + fd->Public.Position += take; + byteCount -= take; - return u8DataOut - (uint8_t*)dataOut; -} -bool FAT_ReadEntry(DISK* disk, FAT_File* file, FAT_DirectoryEntry* dirEntry) { - return FAT_Read(disk, file, sizeof(FAT_DirectoryEntry), dirEntry) == sizeof(FAT_DirectoryEntry); + // printf("leftInBuffer=%lu take=%lu\r\n", leftInBuffer, take); + // See if we need to read more data + if (leftInBuffer == take) + { + // Special handling for root directory + if (fd->Public.Handle == ROOT_DIRECTORY_HANDLE) + { + ++fd->CurrentCluster; -} -void FAT_Close(FAT_File* file) { - if (file->Handle == ROOT_DIRECTORY_HANDLE) { - file->Position = 0; - g_Data->RootDirectory.CurrentCluster = g_Data->RootDirectory.FirstCluster; - } - else { - g_Data->OpenedFiles[file->Handle].Opened = false; - } + // read next sector + if (!DISK_ReadSectors(disk, fd->CurrentCluster, 1, fd->Buffer)) + { + printf("FAT: read error!\r\n"); + break; + } + } + else + { + // calculate next cluster & sector to read + if (++fd->CurrentSectorInCluster >= g_Data->BS.BootSector.SectorsPerCluster) + { + fd->CurrentSectorInCluster = 0; + fd->CurrentCluster = FAT_NextCluster(fd->CurrentCluster); + } + + if (fd->CurrentCluster >= 0xFF8) + { + // Mark end of file + fd->Public.Size = fd->Public.Position; + break; + } + + // read next sector + if (!DISK_ReadSectors(disk, FAT_ClusterToLba(fd->CurrentCluster) + fd->CurrentSectorInCluster, 1, fd->Buffer)) + { + printf("FAT: read error!\r\n"); + break; + } + } + } + } + + return u8DataOut - (uint8_t*)dataOut; } -bool FAT_FindFile(DISK* disk, FAT_File* file, const char* name, FAT_DirectoryEntry* entryOut) { - char fatName[11]; - FAT_DirectoryEntry entry; +bool FAT_ReadEntry(DISK* disk, FAT_File* file, FAT_DirectoryEntry* dirEntry) +{ + return FAT_Read(disk, file, sizeof(FAT_DirectoryEntry), dirEntry) == sizeof(FAT_DirectoryEntry); +} - // convert from name to fat name - memset(fatName, ' ', sizeof(fatName)); - fatName[11] + '\0'; +void FAT_Close(FAT_File* file) +{ + if (file->Handle == ROOT_DIRECTORY_HANDLE) + { + file->Position = 0; + g_Data->RootDirectory.CurrentCluster = g_Data->RootDirectory.FirstCluster; + } + else + { + g_Data->OpenedFiles[file->Handle].Opened = false; + } +} - const char* ext = strchr(name, '.'); - if (ext == NULL) - ext = name + 11; - for (int i = 0; i < 8 && name[i] && name + i < ext; i++) - fatName[i] = toupper(name[i]); - if (ext != name + 11) { +bool FAT_FindFile(DISK* disk, FAT_File* file, const char* name, FAT_DirectoryEntry* entryOut) +{ + char fatName[12]; + FAT_DirectoryEntry entry; + + // convert from name to fat name + memset(fatName, ' ', sizeof(fatName)); + fatName[11] = '\0'; + + const char* ext = strchr(name, '.'); + if (ext == NULL) + ext = name + 11; + + for (int i = 0; i < 8 && name[i] && name + i < ext; i++) + fatName[i] = toupper(name[i]); + + if (ext != name + 11) + { for (int i = 0; i < 3 && ext[i + 1]; i++) fatName[i + 8] = toupper(ext[i + 1]); } - while (FAT_ReadEntry(disk, file, &entry)) { - // for (int i = 0; i < 11; i++) - // printf("%c", fatName); - // for (int i = 0; i < 11; i++) - // printf("%c", entry.Name[i]); - if (memcmp(fatName, entry.Name, 11) == 0) { - *entryOut = entry; - return true; - } - } - - return false; + while (FAT_ReadEntry(disk, file, &entry)) + { + if (memcmp(fatName, entry.Name, 11) == 0) + { + *entryOut = entry; + return true; + } + } + + return false; } -FAT_File* FAT_Open(DISK* disk, const char* path) { - char name[MAX_PATH_SIZE]; - - // ignore leading slash - if (path[0] == '/') - path++; - - FAT_File* current = &g_Data->RootDirectory.Public; +FAT_File* FAT_Open(DISK* disk, const char* path) +{ + char name[MAX_PATH_SIZE]; - while (*path) { - // extract next file name from path - bool isLast = false; - const char* delim = strchr(path, '/'); - if (delim != NULL) { - memcpy(name, path, delim - path); - name[delim - path + 1] = '\0'; - path = delim + 1; - } - else { - unsigned len = strlen(path); - memcpy(name, path, len); - name[len + 1] = '\0'; - path += len; - bool isLast = true; - } - // find directory entry in current dir - FAT_DirectoryEntry entry; - if (FAT_FindFile(disk, current, name, &entry)) { - FAT_Close(current); + // ignore leading slash + if (path[0] == '/') + path++; - // check if DIR - if (!isLast && entry.Attributes & FAT_ATTRIBUTE_DIRECTORY == 0) { - printf("FAT: %s is NOT a Directory!\r\n", name); - return NULL; - } + FAT_File* current = &g_Data->RootDirectory.Public; - // open new DIR entry - current = FAT_OpenEntry(disk, &entry); - } - else { - FAT_Close(current); - printf("FAT: %s NOT Found!", name); - return NULL; - } + while (*path) { + // extract next file name from path + bool isLast = false; + const char* delim = strchr(path, '/'); + if (delim != NULL) + { + memcpy(name, path, delim - path); + name[delim - path + 1] = '\0'; + path = delim + 1; + } + else + { + unsigned len = strlen(path); + memcpy(name, path, len); + name[len + 1] = '\0'; + path += len; + isLast = true; + } + + // find directory entry in current directory + FAT_DirectoryEntry entry; + if (FAT_FindFile(disk, current, name, &entry)) + { + FAT_Close(current); - } + // check if directory + if (!isLast && entry.Attributes & FAT_ATTRIBUTE_DIRECTORY == 0) + { + printf("FAT: %s not a directory\r\n", name); + return NULL; + } - return current; + // open new directory entry + current = FAT_OpenEntry(disk, &entry); + } + else + { + FAT_Close(current); + + printf("FAT: %s not found\r\n", name); + return NULL; + } + } + + return current; } \ No newline at end of file diff --git a/src/bootloader/stage2/main.c b/src/bootloader/stage2/main.c index 0b13089..8c036ec 100644 --- a/src/bootloader/stage2/main.c +++ b/src/bootloader/stage2/main.c @@ -8,10 +8,18 @@ #include "x86.h" #include "disk.h" #include "fat.h" +#include "memdefs.h" +#include "memory.h" #define LOGO " _ _____ _ __________________\n / | / / | / | / / _/_ __/ ____/\n / |/ / /| | / |/ // / / / / __/ \n / /| / ___ |/ /| // / / / / /___ \n/_/ |_/_/ |_/_/ |_/___/ /_/ /_____/ \n" #define VERSION "v0.0.1" + +uint8_t* KernelLoadBuffer = (uint8_t*)MEMORY_LOAD_KERNEL; +uint8_t* Kernel = (uint8_t*)MEMORY_KERNEL_ADDR; + +typedef void (*KernelStart)(); + void* g_data = (void*)0x20000; void puts_realmode(const char* str) { @@ -57,36 +65,43 @@ void* g_data = (void*)0x20000; goto end; } printf("Done!\n"); - // printf("Listing Root DIR...\n"); - // // browse files in root - FAT_File* fd = FAT_Open(&disk, "/"); - FAT_DirectoryEntry entry; - int i = 0; - // while (FAT_ReadEntry(&disk, fd, &entry) && i++ < 5) { - // printf(" "); - // for (int i = 0; i < 11; i++) - // putc(entry.Name[i]); - // printf("\n"); - // } - // FAT_Close(fd); + // test fat driver printf("Testing FAT Driver..."); // read test.txt + FAT_File* ft = FAT_Open(&disk, "/"); char buffer[100]; - uint32_t read; - fd = FAT_Open(&disk, "misc/test.txt"); - while ((read = FAT_Read(&disk, fd, sizeof(buffer), buffer))) + uint32_t testread; + ft = FAT_Open(&disk, "test.txt"); // move test.txt in MISC folder later (TM) + while ((testread = FAT_Read(&disk, ft, sizeof(buffer), buffer))) { - for (uint32_t i = 0; i < read; i++) + for (uint32_t i = 0; i < testread; i++) { if (buffer[i] == '\n') putc('\r'); putc(buffer[i]); } } + FAT_Close(ft); + + // load kernel from disk + printf("Loading Kernel..."); + FAT_File* fd = FAT_Open(&disk, "/kernel.bin"); // move to /boot later????? (TM) + uint32_t read; + uint8_t* kernelBuffer = Kernel; + while ((read = FAT_Read(&disk, fd, MEMORY_LOAD_SIZE, KernelLoadBuffer))) + { + memcpy(kernelBuffer, KernelLoadBuffer, read); + kernelBuffer += read; + } FAT_Close(fd); - printf("Loading Kernel..."); + // execute kernel + KernelStart kernelStart = (KernelStart)Kernel; + kernelStart(); + + + end: for (;;); diff --git a/src/bootloader/stage2/memdefs.h b/src/bootloader/stage2/memdefs.h index b000f5a..ed3820e 100644 --- a/src/bootloader/stage2/memdefs.h +++ b/src/bootloader/stage2/memdefs.h @@ -15,6 +15,9 @@ #define MEMORY_FAT_ADDR ((void*)0x20000) #define MEMORY_FAT_SIZE 0x00010000 +#define MEMORY_LOAD_KERNEL ((void*)0x30000) +#define MEMORY_LOAD_SIZE 0x00010000 + // 0x00020000 - 0x00030000 - stage2 // 0x00030000 - 0x00080000 - free @@ -22,3 +25,5 @@ // 0x00080000 - 0x0009FFFF - Extended BIOS data area // 0x000A0000 - 0x000C7FFF - Video // 0x000C8000 - 0x000FFFFF - BIOS + +#define MEMORY_KERNEL_ADDR ((void*)0x100000) \ No newline at end of file diff --git a/src/bootloader/stage2/stdio.c b/src/bootloader/stage2/stdio.c index 03ace46..5704577 100644 --- a/src/bootloader/stage2/stdio.c +++ b/src/bootloader/stage2/stdio.c @@ -293,6 +293,7 @@ void printf(const char* fmt, ...) { length = PRINTF_LENGTH_DEFAULT; radix = 10; sign = false; + bool number = false; break; } diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 0f31d9b..bdbc6f0 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -1,11 +1,10 @@ TARGET_ASMFLAGS += -f elf -TARGET_CFLAGS += -ffreestanding -nostdlib +TARGET_CFLAGS += -ffreestanding -nostdlib -I. TARGET_LIBS += -lgcc TARGET_LINKFLAGS += -T linker.ld -nostdlib - -SOURCES_C=$(wildcard *.c) -SOURCES_ASM=$(wildcard *.asm) +SOURCES_C=$(shell find . -type f -name "*.c") +SOURCES_ASM=$(shell find . -type f -name "*.asm") OBJECTS_C=$(patsubst %.c, $(BUILD_DIR)/kernel/c/%.obj, $(SOURCES_C)) OBJECTS_ASM=$(patsubst %.asm, $(BUILD_DIR)/kernel/asm/%.obj, $(SOURCES_ASM)) @@ -16,17 +15,18 @@ all: kernel kernel: $(BUILD_DIR)/kernel.bin $(BUILD_DIR)/kernel.bin: $(OBJECTS_ASM) $(OBJECTS_C) - $(TARGET_LD) $(TARGET_LINKFLAGS) -Wl,-Map=$(BUILD_DIR)/kernel.map -o $@ $^ $(TARGET_LIBS) + @$(TARGET_LD) $(TARGET_LINKFLAGS) -Wl,-Map=$(BUILD_DIR)/kernel.map -o $@ $^ $(TARGET_LIBS) + @echo "--> Created: kernel.bin" -$(BUILD_DIR)/kernel/c/%.obj: %.c always - $(TARGET_CC) $(TARGET_CFLAGS) -c -o $@ $< +$(BUILD_DIR)/kernel/c/%.obj: %.c + @mkdir -p $(@D) + @$(TARGET_CC) $(TARGET_CFLAGS) -c -o $@ $< + @echo "--> Compiled: " $< -$(BUILD_DIR)/kernel/asm/%.obj: %.asm always - $(TARGET_ASM) $(TARGET_ASMFLAGS) -o $@ $< - -always: - mkdir -p $(BUILD_DIR)/kernel/c - mkdir -p $(BUILD_DIR)/kernel/asm +$(BUILD_DIR)/kernel/asm/%.obj: %.asm + @mkdir -p $(@D) + @$(TARGET_ASM) $(TARGET_ASMFLAGS) -o $@ $< + @echo "--> Compiled: " $< clean: rm -f $(BUILD_DIR)/kernel.bin \ No newline at end of file diff --git a/src/kernel/arch/i686/io.asm b/src/kernel/arch/i686/io.asm new file mode 100644 index 0000000..7c76dfc --- /dev/null +++ b/src/kernel/arch/i686/io.asm @@ -0,0 +1,21 @@ +;/////////////////////; +;Nanite OS ; +;COPYRIGHT (C) 2024 ; +;Tyler McGurrin ; +;/////////////////////; + +global x86_outb +x86_outb: + [bits 32] + mov dx, [esp + 4] + mov al, [esp + 8] + out dx, al + ret + +global x86_inb +x86_inb: + [bits 32] + mov dx, [esp + 4] + xor eax, eax + in al, dx + ret diff --git a/src/kernel/arch/i686/io.h b/src/kernel/arch/i686/io.h new file mode 100644 index 0000000..b3aa253 --- /dev/null +++ b/src/kernel/arch/i686/io.h @@ -0,0 +1,11 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ +#pragma once +#include +#include + +void __attribute__((cdecl)) x86_outb(uint16_t port, uint8_t value); +uint8_t __attribute__((cdecl)) x86_inb(uint16_t port); diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld index e827302..fa612c1 100644 --- a/src/kernel/linker.ld +++ b/src/kernel/linker.ld @@ -1,8 +1,9 @@ -ENTRY(entry) +ENTRY(start) OUTPUT_FORMAT("binary") -phys = 0x000100000; +phys = 0x00100000; -SECTIONS { +SECTIONS +{ . = phys; .entry : { __entry_start = .; *(.entry) } @@ -10,6 +11,6 @@ SECTIONS { .data : { __data_start = .; *(.data) } .rodata : { __rodata_start = .; *(.rodata) } .bss : { __bss_start = .; *(.bss) } - + __end = .; } \ No newline at end of file diff --git a/src/kernel/main.c b/src/kernel/main.c index aa77c14..632f98d 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -4,25 +4,20 @@ |Tyler McGurrin | \*----------------*/ #include -#include "stdio.h" -#include "x86.h" -#include "disk.h" -#include "fat.h" +#include +#include + +void* memcpy(void* dst, const void* src, uint16_t num); +void* memset(void* ptr, int value, uint16_t num); +int memcmp(const void* ptr1, const void * ptr2, uint16_t num); diff --git a/src/kernel/stdio.c b/src/kernel/stdio.c index 03ace46..3dba0f0 100644 --- a/src/kernel/stdio.c +++ b/src/kernel/stdio.c @@ -3,8 +3,8 @@ |Copyright (C) 2024| |Tyler McGurrin | \*----------------*/ -#include "stdio.h" -#include "x86.h" +#include +#include #include #include @@ -293,6 +293,8 @@ void printf(const char* fmt, ...) { length = PRINTF_LENGTH_DEFAULT; radix = 10; sign = false; + bool number = false; + break; } diff --git a/src/kernel/stdio.h b/src/kernel/stdio.h index 10dcdb8..e455425 100644 --- a/src/kernel/stdio.h +++ b/src/kernel/stdio.h @@ -11,3 +11,4 @@ void putc(char c); void puts(const char* str); void printf(const char* fmt, ...); void print_buffer(const char* msg, const void* buffer, uint32_t count); +void setcursor(int x, int y); \ No newline at end of file