diff --git a/Makefile b/Makefile index faad734..b4eb068 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,10 @@ - EMULATOR = qemu-system-i386 AS = ${HOME}/opt/cross/bin/i686-elf-as CC = ${HOME}/opt/cross/bin/i686-elf-gcc CPP = ${HOME}/opt/cross/bin/i686-elf-g++ -CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra +CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -I source/CoreLib/build/include -OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/Path.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/VFS.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o +OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/VFS.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o SRC_DIR = source BUILD_DIR = build @@ -53,8 +52,7 @@ test_disk: all build_kernel: $(OBJ_LINK_LIST) - $(CC) -T $(SRC_DIR)/kernel//linker.ld -o $(BUILD_DIR)/myos.bin \ - -ffreestanding -ggdb -Og -nostdlib $(OBJ_LINK_LIST) -lgcc + $(CPP) -T $(SRC_DIR)/kernel/linker.ld -o $(BUILD_DIR)/myos.bin -ffreestanding -ggdb -Og -nostdlib $(OBJ_LINK_LIST) -lgcc -L source/CoreLib/build -lCoreLib build_x86_64: $(AS) $(SRC_DIR)/cgc/x86_64/crti.s -o $(BUILD_DIR)/crti_64.o @@ -71,7 +69,7 @@ $(BUILD_DIR)/kterm.o: $(CPP) -c $(SRC_DIR)/kernel/terminal/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/io.o: - $(CPP) -c $(SRC_DIR)/kernel/drivers/io/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/drivers/io/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/idt.o: $(CPP) -c $(SRC_DIR)/kernel/interrupts/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti @@ -82,9 +80,6 @@ $(BUILD_DIR)/gdtc.o: $(BUILD_DIR)/pic.o: $(CPP) -c $(SRC_DIR)/kernel/drivers/pic/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti -$(BUILD_DIR)/string.o: - $(CC) -c $(SRC_DIR)/lib/include/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99 - $(BUILD_DIR)/PhysicalMemoryManager.o: $(CPP) -c $(SRC_DIR)/kernel/memory/PhysicalMemoryManager.cpp -o $(BUILD_DIR)/PhysicalMemoryManager.o $(CFLAGS) -fno-exceptions -fno-rtti @@ -136,9 +131,6 @@ $(BUILD_DIR)/prekernel.o: $(BUILD_DIR)/processor.o: $(CPP) -c $(SRC_DIR)/kernel/i386/processor.cpp -o $(BUILD_DIR)/processor.o $(CFLAGS) -fno-exceptions -fno-rtti -$(BUILD_DIR)/Path.o: - $(CPP) -c $(SRC_DIR)/kernel/vfs/Path.cpp -o $(BUILD_DIR)/Path.o $(CFLAGS) -fno-exceptions -fno-rtti - # Assembly -> Object files $(BUILD_DIR)/boot.o: $(AS) $(SRC_DIR)/kernel/boot/boot.s -o $(BUILD_DIR)/boot.o diff --git a/source/CoreLib/Makefile b/source/CoreLib/Makefile new file mode 100644 index 0000000..b5a0cbb --- /dev/null +++ b/source/CoreLib/Makefile @@ -0,0 +1,33 @@ +CPP = ${HOME}/opt/cross/bin/i686-elf-g++ +CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra + +SRC_DIR = . +BUILD_DIR = build +OBJ_FOLDER = bin +OUTPUTFILE = $(BUILD_DIR)/libCoreLib.a + +OFILES = $(OBJ_FOLDER)/memory.o $(OBJ_FOLDER)/path.o $(OBJ_FOLDER)/stack.o $(OBJ_FOLDER)/string.o $(OBJ_FOLDER)/stringview.o + + +.phony: all +all: $(OUTPUTFILE) + cp *.h build/include/ + +$(OUTPUTFILE): $(OFILES) + ar -rc $(OUTPUTFILE) $(OFILES) + +$(OBJ_FOLDER)/memory.o: Memory.cpp + $(CPP) -c Memory.cpp -o $(OBJ_FOLDER)/memory.o $(CFLAGS) + +$(OBJ_FOLDER)/path.o: Path.cpp + $(CPP) -c Path.cpp -o $(OBJ_FOLDER)/path.o $(CFLAGS) + +$(OBJ_FOLDER)/stack.o: Stack.cpp + $(CPP) -c Stack.cpp -o $(OBJ_FOLDER)/stack.o $(CFLAGS) + +$(OBJ_FOLDER)/string.o: String.cpp + $(CPP) -c String.cpp -o $(OBJ_FOLDER)/string.o $(CFLAGS) + +$(OBJ_FOLDER)/stringview.o: StringView.cpp + $(CPP) -c StringView.cpp -o $(OBJ_FOLDER)/stringview.o $(CFLAGS) + diff --git a/source/CoreLib/Memory.cpp b/source/CoreLib/Memory.cpp new file mode 100644 index 0000000..fce2e66 --- /dev/null +++ b/source/CoreLib/Memory.cpp @@ -0,0 +1,58 @@ +// +// Created by nigel on 19/02/23. +// +#include "Memory.h" + +void* memset (void* ptr, int value, size_t num) +{ + for( int i = 0; i < num; i++ ) + { + unsigned char* data = (unsigned char*)ptr+ i; + *data = (unsigned char)value; + } + return ptr; +} + + +int memcmp( const void* ptr1, const void* ptr2, size_t num) +{ + const unsigned char * cs = (const unsigned char*) ptr1; + const unsigned char * ct = (const unsigned char*) ptr2; + + + for (int i = 0 ; i < num ; i++, cs++, ct++ ){ + if( *cs < *ct){ + return -1; + } else if( *cs > *ct){ + return 1; + } + } + + return 0; + +} + +size_t strlen(const char* str) { + size_t len = 0; + while(str[len]){ + len++; + } + return len; +} + +int strncmp ( const char* str1, const char* str2, size_t num ){ + for( int i = 0; i < num ; i++){ + + if( str1[i] < str2[i]){ + return -1; + } + + if( str1[i] > str2[i] ){ + return 1; + } + + + } + + return 0; +} \ No newline at end of file diff --git a/source/CoreLib/Memory.h b/source/CoreLib/Memory.h new file mode 100644 index 0000000..e31a5dc --- /dev/null +++ b/source/CoreLib/Memory.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +void* memset (void* ptr, int value, size_t num); + +int memcmp( const void* ptr1, const void* ptr2, size_t num); + +size_t strlen(const char* str); + +int strncmp ( const char* str1, const char* str2, size_t num ); \ No newline at end of file diff --git a/source/CoreLib/Path.cpp b/source/CoreLib/Path.cpp new file mode 100644 index 0000000..d23b226 --- /dev/null +++ b/source/CoreLib/Path.cpp @@ -0,0 +1,24 @@ +// +// Created by nigel on 19/02/23. +// +#include "Path.h" + +Path::Path(String path) +: path(path) +{ + +} + +StringView Path::getbasename() +{ + unsigned int path_length = path.length(); + int i = path_length; + while (path[i] != '/') + i--; + + return {path,static_cast(i +1), path_length}; +} + +char* Path::str() { + return path.str(); +} \ No newline at end of file diff --git a/source/CoreLib/Path.h b/source/CoreLib/Path.h new file mode 100644 index 0000000..ec4d955 --- /dev/null +++ b/source/CoreLib/Path.h @@ -0,0 +1,22 @@ +// +// Created by nigel on 19/02/23. +// +#pragma once +#include +#include "StringView.h" + + +class Path{ +public: + explicit Path(String path); + + StringView getbasename(); + + char* str(); + +private: + String path; + +}; + + diff --git a/source/CoreLib/Stack.cpp b/source/CoreLib/Stack.cpp new file mode 100644 index 0000000..af9520e --- /dev/null +++ b/source/CoreLib/Stack.cpp @@ -0,0 +1,3 @@ +// +// Created by nigel on 19/02/23. +// diff --git a/source/lib/include/stack.h b/source/CoreLib/Stack.h similarity index 96% rename from source/lib/include/stack.h rename to source/CoreLib/Stack.h index 0128bf6..b363fed 100644 --- a/source/lib/include/stack.h +++ b/source/CoreLib/Stack.h @@ -1,5 +1,5 @@ #pragma once -#include "../../kernel/memory/KernelHeap.h" +#include "../kernel/memory/KernelHeap.h" #include template diff --git a/source/CoreLib/String.cpp b/source/CoreLib/String.cpp new file mode 100644 index 0000000..1aaf064 --- /dev/null +++ b/source/CoreLib/String.cpp @@ -0,0 +1,39 @@ +#include "String.h" +#include +#include + + +String::String(char* characters) +: chars(characters) + { + + } + +char* String::str(){ + return chars; +} + +unsigned int String::length () +{ + int i = 0; + + while ( chars[i] != '\0'){ + i++; + } + + return i; +} + +// Returns a null character if size exceeds limits +char String::operator[] (size_t idx) +{ + if( idx > this->length()) + return '\0'; + + return chars[idx]; +} + +const char String::operator[](size_t idx) const { +return (const char) chars[idx]; +} + diff --git a/source/CoreLib/String.h b/source/CoreLib/String.h new file mode 100644 index 0000000..8ff3f05 --- /dev/null +++ b/source/CoreLib/String.h @@ -0,0 +1,18 @@ +#pragma once +#include + +class String { +public: + String(char* characters); + String(String&) = default; + unsigned int length(); + + char* str (); + char operator[](size_t index) ; + const char operator[](size_t idx) const; + +protected: + char* chars; + + +}; \ No newline at end of file diff --git a/source/CoreLib/StringView.cpp b/source/CoreLib/StringView.cpp new file mode 100644 index 0000000..5392c87 --- /dev/null +++ b/source/CoreLib/StringView.cpp @@ -0,0 +1,14 @@ +// +// Created by nigel on 19/02/23. +// +#include "StringView.h" + +StringView::StringView(String string, unsigned int start, unsigned int end) + : String(string), begin(start), end(end) +{ + +} + +char* StringView::str(){ + //TODO: Not implemented +} \ No newline at end of file diff --git a/source/CoreLib/StringView.h b/source/CoreLib/StringView.h new file mode 100644 index 0000000..725284b --- /dev/null +++ b/source/CoreLib/StringView.h @@ -0,0 +1,14 @@ +// +// Created by nigel on 19/02/23. +// +#pragma once +#include "String.h" + +class StringView : String { +public: + StringView(String string, unsigned int start, unsigned int end ); + char* str (); +private: + unsigned int begin; + unsigned int end; +}; diff --git a/source/CoreLib/bin/memory.o b/source/CoreLib/bin/memory.o new file mode 100644 index 0000000..7b6893f Binary files /dev/null and b/source/CoreLib/bin/memory.o differ diff --git a/source/CoreLib/bin/path.o b/source/CoreLib/bin/path.o new file mode 100644 index 0000000..120b8eb Binary files /dev/null and b/source/CoreLib/bin/path.o differ diff --git a/source/CoreLib/bin/stack.o b/source/CoreLib/bin/stack.o new file mode 100644 index 0000000..cfae905 Binary files /dev/null and b/source/CoreLib/bin/stack.o differ diff --git a/source/CoreLib/bin/string.o b/source/CoreLib/bin/string.o new file mode 100644 index 0000000..482fce0 Binary files /dev/null and b/source/CoreLib/bin/string.o differ diff --git a/source/CoreLib/bin/stringview.o b/source/CoreLib/bin/stringview.o new file mode 100644 index 0000000..8ca403a Binary files /dev/null and b/source/CoreLib/bin/stringview.o differ diff --git a/source/kernel/drivers/acpi/rsdp.h b/source/kernel/drivers/acpi/rsdp.h index eb5a87a..7d7aecb 100644 --- a/source/kernel/drivers/acpi/rsdp.h +++ b/source/kernel/drivers/acpi/rsdp.h @@ -1,7 +1,8 @@ #pragma once #include #include "./../../terminal/kterm.h" -#include "../../../lib/include/mem.h" +#include + struct RSDPTR { char signature[8]; uint8_t Checksum ; diff --git a/source/kernel/drivers/ata/ataDevice.cpp b/source/kernel/drivers/ata/ataDevice.cpp index 873aa0e..bde914a 100644 --- a/source/kernel/drivers/ata/ataDevice.cpp +++ b/source/kernel/drivers/ata/ataDevice.cpp @@ -12,7 +12,6 @@ void ATA_DEVICE::Soft_Reset(uint8_t DEVICE_CHANNEL,DEVICE_DRIVE drive){ } - void ATA_DEVICE::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){ // lets ignore which port we actually want to check for now ! diff --git a/source/kernel/kernel.cpp b/source/kernel/kernel.cpp index 7bd0ca1..9440716 100644 --- a/source/kernel/kernel.cpp +++ b/source/kernel/kernel.cpp @@ -1,10 +1,6 @@ /* Copyright © Nigel Barink 2023 */ -extern "C"{ -#include "../lib/include/string.h" -} - #include "memory/memory.h" #include "memory/KernelHeap.h" #include "memory/gdt/gdtc.h" diff --git a/source/kernel/memory/PhysicalMemoryManager.h b/source/kernel/memory/PhysicalMemoryManager.h index 9aa9c15..f83fff1 100644 --- a/source/kernel/memory/PhysicalMemoryManager.h +++ b/source/kernel/memory/PhysicalMemoryManager.h @@ -1,8 +1,9 @@ #pragma once #include +#include + #include "../prekernel/bootstructure.h" #include "../terminal/kterm.h" -#include "../../lib/include/mem.h" #include "../bitmap.h" #define BLOCK_SIZE 4092 diff --git a/source/kernel/memory/TaskStateSegment.h b/source/kernel/memory/TaskStateSegment.h index b41a4fb..f26b3ec 100644 --- a/source/kernel/memory/TaskStateSegment.h +++ b/source/kernel/memory/TaskStateSegment.h @@ -1,6 +1,6 @@ #pragma once #include "gdt/gdtc.h" -#include "../../lib/include/string.h" +#include struct TaskStateSegment { uint32_t prev_tss; diff --git a/source/kernel/memory/memory.h b/source/kernel/memory/memory.h index ea609ac..1f2df61 100644 --- a/source/kernel/memory/memory.h +++ b/source/kernel/memory/memory.h @@ -5,7 +5,8 @@ #include "memoryinfo.h" #include "../prekernel/multiboot.h" #include "../terminal/kterm.h" -#include "../../lib/include/mem.h" +#include + #include "../bitmap.h" #define BLOCK_SIZE 4092 diff --git a/source/kernel/supervisorterminal/superVisorTerminal.h b/source/kernel/supervisorterminal/superVisorTerminal.h index ad3c39b..f68456c 100644 --- a/source/kernel/supervisorterminal/superVisorTerminal.h +++ b/source/kernel/supervisorterminal/superVisorTerminal.h @@ -4,8 +4,6 @@ #include "../drivers/pit/pit.h" #include "../drivers/ps-2/keyboard.h" #include "../memory/PhysicalMemoryManager.h" -extern "C" { - #include "../../lib/include/string.h" -} +#include extern "C" void startSuperVisorTerminal(); \ No newline at end of file diff --git a/source/kernel/terminal/kterm.h b/source/kernel/terminal/kterm.h index 7ac6d27..8cce182 100644 --- a/source/kernel/terminal/kterm.h +++ b/source/kernel/terminal/kterm.h @@ -5,10 +5,7 @@ #include "../drivers/vga/colors.h" #include "../drivers/io/io.h" -extern "C" { -#include "../../lib/include/string.h" -} - +#include "CoreLib/Memory.h" void kterm_init(); diff --git a/source/kernel/vfs/Path.cpp b/source/kernel/vfs/Path.cpp deleted file mode 100644 index 3489836..0000000 --- a/source/kernel/vfs/Path.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// -// Created by nigel on 19/02/23. -// -#include "Path.h" -#include "../memory/KernelHeap.h" -String::String(char* characters) -: chars(characters) -{ - -} - -char* String::str(){ - return chars; -} - -unsigned int String::length () -{ - int i = 0; - - while ( chars[i] != '\0'){ - i++; - } - - return i; -} - -// Returns a null character if size exceeds limits -char String::operator[] (size_t idx) -{ - if( idx > this->length()) - return '\0'; - - return chars[idx]; -} - -const char String::operator[](size_t idx) const { - return (const char) chars[idx]; -} - -StringView::StringView(String &string, unsigned int start, unsigned int end) -: String(string), begin(start), end(end) -{ - -} - -char* StringView::str(){ - char* str = (char*) malloc((this->length() * sizeof(char))+1); - - int index = 0; - for ( int i = begin; i < end ; i++){ - str[index] = chars[i]; - index++; - } - chars[index+1] ='\0'; - return str; -} - - -Path::Path(char *path) -: path(String(path)) -{ - -} - -Path::Path(String &path) -: path(path) -{ - -} - -StringView Path::getbasename() -{ - unsigned int path_length = path.length(); - int i = path_length; - while (path[i] != '/') - i--; - - return StringView(path,i +1, path_length); -} - -char* Path::str() { - return path.str(); -} \ No newline at end of file diff --git a/source/kernel/vfs/Path.h b/source/kernel/vfs/Path.h deleted file mode 100644 index 2d6de27..0000000 --- a/source/kernel/vfs/Path.h +++ /dev/null @@ -1,46 +0,0 @@ -// -// Created by nigel on 19/02/23. -// -#pragma once -#include - -class String { -public: - String(char* characters); - String(String&) = default; - unsigned int length(); - - char* str (); - char operator[](size_t index) ; - const char operator[](size_t idx) const; - -protected: - char* chars; - - -}; - -class StringView : String { -public: - StringView(String& string, unsigned int start, unsigned int end ); - char* str (); -private: - unsigned int begin; - unsigned int end; -}; - -class Path{ -public: - Path(String& path); - Path(char* path); - - StringView getbasename(); - - char* str(); - -private: - String path; - -}; - - diff --git a/source/kernel/vfs/VFS.cpp b/source/kernel/vfs/VFS.cpp index 3f59fc2..9fe8d3f 100644 --- a/source/kernel/vfs/VFS.cpp +++ b/source/kernel/vfs/VFS.cpp @@ -4,7 +4,7 @@ #include "../drivers/ata/ataDevice.h" #include "../partitiontable/mbr/MasterBootRecord.h" #include "../memory/KernelHeap.h" -#include "Path.h" +#include "../../CoreLib/Memory.h" #include "../filesystem/FAT/DirectoryEntry.h" MOUNT_INFO mountInfo; @@ -389,6 +389,8 @@ char* FindNextEntryName (char* path ) void FileSystem::ResolvePath(Path &path) { + // See reference material (1) https://man7.org/linux/man-pages/man7/path_resolution.7.html + char* string_path = path.str(); void* cpy = string_path; @@ -410,9 +412,6 @@ void FileSystem::ResolvePath(Path &path) skip = strlen(entry_name); free(entry_name); - - - free(cpy); } \ No newline at end of file diff --git a/source/kernel/vfs/VFS.h b/source/kernel/vfs/VFS.h index 17b92c0..b294244 100644 --- a/source/kernel/vfs/VFS.h +++ b/source/kernel/vfs/VFS.h @@ -1,6 +1,6 @@ #pragma once #include -#include "Path.h" +#include "../../CoreLib/Path.h" #define FS_FILE 0 #define FS_DIRECTORY 1 diff --git a/source/lib/include/mem.h b/source/lib/include/mem.h deleted file mode 100644 index e5ff1dd..0000000 --- a/source/lib/include/mem.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -// NOTE: These should not be inline -inline void* memset (void* ptr, int value, size_t num) -{ - for( int i = 0; i < num; i++ ) - { - unsigned char* data = (unsigned char*)ptr+ i; - *data = (unsigned char)value; - } - return ptr; -} - - - -inline int memcmp( const void* ptr1, const void* ptr2, size_t num) -{ - const unsigned char * cs = (const unsigned char*) ptr1; - const unsigned char * ct = (const unsigned char*) ptr2; - - - for (int i = 0 ; i < num ; i++, cs++, ct++ ){ - if( *cs < *ct){ - return -1; - } else if( *cs > *ct){ - return 1; - } - } - - return 0; - -} \ No newline at end of file diff --git a/source/lib/include/string.c b/source/lib/include/string.c deleted file mode 100644 index 672d1a8..0000000 --- a/source/lib/include/string.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "string.h" - -size_t strlen(const char* str) { - size_t len = 0; - while(str[len]){ - len++; - } - return len; -} - -int strncmp ( const char* str1, const char* str2, size_t num ){ - for( int i = 0; i < num ; i++){ - - if( str1[i] < str2[i]){ - return -1; - } - - if( str1[i] > str2[i] ){ - return 1; - } - - - } - - return 0; -} diff --git a/source/lib/include/string.h b/source/lib/include/string.h deleted file mode 100644 index 7009681..0000000 --- a/source/lib/include/string.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once -#include -size_t strlen(const char* str); -int strncmp ( const char* str1, const char* str2, size_t num ); \ No newline at end of file