diff --git a/Kernel/.clangd b/Kernel/.clangd index 9992ca2..fb73a7a 100644 --- a/Kernel/.clangd +++ b/Kernel/.clangd @@ -1,2 +1,3 @@ CompileFlags: CompilationDatabase: ../.build/temp/Kernel + Add: [-std=c23] \ No newline at end of file diff --git a/Kernel/Include/Types.h b/Kernel/Include/Types.h index 8f9a059..16ab7bf 100644 --- a/Kernel/Include/Types.h +++ b/Kernel/Include/Types.h @@ -17,6 +17,7 @@ typedef unsigned long long UInt; typedef void* Pointer; typedef UInt Address; +typedef UInt32 Address32; typedef UInt8* BytePointer; typedef UInt8* MemoryPointer; @@ -30,3 +31,4 @@ typedef UInt64 Size; typedef char ASCII; typedef _Bool Boolean; +#define loop while (1) \ No newline at end of file diff --git a/Kernel/Source/Arch/DTB.c b/Kernel/Source/Arch/DTB.c new file mode 100644 index 0000000..ef41555 --- /dev/null +++ b/Kernel/Source/Arch/DTB.c @@ -0,0 +1,63 @@ +#include "Types.h" +#include +#include +#include +#include +#include +#include + +void DTBParse(Pointer dtb) { + FDTHeader* header = (FDTHeader*)dtb; + if (BytesSwap32(header->magic) != kFDTHeaderMagic) { + OSPanic("Invalid DTB magic"); + } + + UInt32 offStruct = BytesSwap32(header->offDtStruct); + UInt32 offStrings = BytesSwap32(header->offDtStrings); + + BytePointer structs = (BytePointer)dtb + offStruct; + ASCII* strings = (ASCII*)dtb + offStrings; + + Int depth = 0; + + while (true) { + UInt32 token = BytesSwap32(*(UInt32*)structs); + structs += 4; + + switch (token) { + case FDTTokenBeginNode: { + ASCII* name = (ASCII*)structs; + + OSLog("Node [%d]: %s\n", depth, name[0] ? name : "ROOT"); + + UInt32 nameLength = StringGetLength(name); + structs += (nameLength + 1); + structs = (BytePointer)AlignUp32((Address)structs, 4); + + depth++; + break; + } + case FDTTokenProperty: { + UInt32 propertyLength = BytesSwap32(*(UInt32*)structs); + UInt32 nameOffset = BytesSwap32(*(UInt32*)(structs + 4)); + structs += 8; + + ASCII* propertyName = strings + nameOffset; + BytePointer propertyData = structs; + OSLog("Property %s (len %d)\n", propertyName, propertyLength); + + structs += propertyLength; + structs = (BytePointer)AlignUp32((Address)structs, 4); + + break; + } + + case FDTTokenEndNode: depth--; break; + case FDTTokenNOP: continue; + case FDTTokenEnd: return; + default: + OSPanic("Invalid DTB token"); + } + } + +} \ No newline at end of file diff --git a/Kernel/Source/KernelMain.c b/Kernel/Source/KernelMain.c index b36806a..07cb91c 100644 --- a/Kernel/Source/KernelMain.c +++ b/Kernel/Source/KernelMain.c @@ -1,7 +1,8 @@ -#include #include "../Common/bootinfo.h" +#include +#include void KernelMain(Bootinfo* bootinfo) { - OSLog("DTB located at 0x%x\n", bootinfo->dtb); - OSLog("Kernel located at 0x%x\n", bootinfo->kernelInfo.kernelAddress); + OSLog("Kernel started.\n"); + DTBParse(bootinfo->dtb); } \ No newline at end of file