From 7d68e78cb24bb4a5b3d584eab6cfcb79852f1787 Mon Sep 17 00:00:00 2001 From: karina Date: Sat, 25 Apr 2026 14:21:32 +0400 Subject: [PATCH 1/3] wip: dtb: --- Kernel/.clangd | 1 + Kernel/Include/Types.h | 2 ++ Kernel/Source/Arch/DTB.c | 63 ++++++++++++++++++++++++++++++++++++++ Kernel/Source/KernelMain.c | 7 +++-- 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 Kernel/Source/Arch/DTB.c 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 From f15a146608a627cddcc14aba2f57f539610f24aa Mon Sep 17 00:00:00 2001 From: karina Date: Sun, 26 Apr 2026 08:50:57 +0400 Subject: [PATCH 2/3] wip: dtb; --- Kernel/Source/Arch/DTB.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Kernel/Source/Arch/DTB.c b/Kernel/Source/Arch/DTB.c index ef41555..978262e 100644 --- a/Kernel/Source/Arch/DTB.c +++ b/Kernel/Source/Arch/DTB.c @@ -1,4 +1,3 @@ -#include "Types.h" #include #include #include From 32931021d2f912779d6caeb8cf37e38a15e6fd5b Mon Sep 17 00:00:00 2001 From: karina Date: Sun, 26 Apr 2026 10:48:54 +0400 Subject: [PATCH 3/3] feat: parsing dtb --- Kernel/Include/Lib/Bytes.h | 4 ++++ Kernel/Include/Lib/String.h | 4 +++- Kernel/Source/Arch/DTB.c | 25 +++++++++++++------------ Kernel/Source/Arch/Exceptions.c | 2 +- Kernel/Source/Lib/String.c | 4 ++++ 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Kernel/Include/Lib/Bytes.h b/Kernel/Include/Lib/Bytes.h index 852c199..ecb90c1 100644 --- a/Kernel/Include/Lib/Bytes.h +++ b/Kernel/Include/Lib/Bytes.h @@ -8,3 +8,7 @@ static inline UInt32 BytesSwap32(UInt32 value) { static inline UInt64 BytesSwap64(UInt64 value) { return __builtin_bswap64(value); } + +static inline UInt64 Merge32To64(UInt32 low, UInt32 high) { + return ((UInt64)high << 32) | low; +} diff --git a/Kernel/Include/Lib/String.h b/Kernel/Include/Lib/String.h index 89572df..1db07ac 100644 --- a/Kernel/Include/Lib/String.h +++ b/Kernel/Include/Lib/String.h @@ -15,4 +15,6 @@ Size StringGetLength(const ASCII* string); const ASCII* StringFindLastOccurrenceOfCharacter(const ASCII* string, ASCII separator); Int32 StringFormatVariadic(ASCII* string, Size size, const ASCII* format, va_list args); -Int32 StringFormat(ASCII* destination, UInt64 size, const ASCII* format, ...); \ No newline at end of file +Int32 StringFormat(ASCII* destination, UInt64 size, const ASCII* format, ...); + +Boolean StringStartsWith(const ASCII* string, const ASCII* prefix); \ No newline at end of file diff --git a/Kernel/Source/Arch/DTB.c b/Kernel/Source/Arch/DTB.c index 978262e..9cd89ee 100644 --- a/Kernel/Source/Arch/DTB.c +++ b/Kernel/Source/Arch/DTB.c @@ -16,8 +16,7 @@ void DTBParse(Pointer dtb) { BytePointer structs = (BytePointer)dtb + offStruct; ASCII* strings = (ASCII*)dtb + offStrings; - - Int depth = 0; + ASCII* currentNode = ""; while (true) { UInt32 token = BytesSwap32(*(UInt32*)structs); @@ -25,15 +24,12 @@ void DTBParse(Pointer dtb) { switch (token) { case FDTTokenBeginNode: { - ASCII* name = (ASCII*)structs; + currentNode = (ASCII*)structs; - OSLog("Node [%d]: %s\n", depth, name[0] ? name : "ROOT"); - - UInt32 nameLength = StringGetLength(name); + UInt32 nameLength = StringGetLength(currentNode); structs += (nameLength + 1); - structs = (BytePointer)AlignUp32((Address)structs, 4); + structs = (BytePointer)AlignUp64((Address)structs, 4); - depth++; break; } case FDTTokenProperty: { @@ -42,16 +38,21 @@ void DTBParse(Pointer dtb) { structs += 8; ASCII* propertyName = strings + nameOffset; - BytePointer propertyData = structs; - OSLog("Property %s (len %d)\n", propertyName, propertyLength); + if (StringCompare(propertyName, "reg") == 0 && StringStartsWith(currentNode, "memory")) { + UInt32* cells = (UInt32*)structs; + + UInt64 base = Merge32To64(BytesSwap32(cells[0]), BytesSwap32(cells[1])); + UInt64 size = Merge32To64(BytesSwap32(cells[2]), BytesSwap32(cells[3])); + OSLog("Memory: base=0x%x, size=0x%x\n", base, size); + } structs += propertyLength; - structs = (BytePointer)AlignUp32((Address)structs, 4); + structs = (BytePointer)AlignUp64((Address)structs, 4); break; } - case FDTTokenEndNode: depth--; break; + case FDTTokenEndNode: break; case FDTTokenNOP: continue; case FDTTokenEnd: return; default: diff --git a/Kernel/Source/Arch/Exceptions.c b/Kernel/Source/Arch/Exceptions.c index 532bbb2..b3aa22d 100644 --- a/Kernel/Source/Arch/Exceptions.c +++ b/Kernel/Source/Arch/Exceptions.c @@ -3,6 +3,6 @@ #include #include -void ExceptionsHandler(ExceptionsContext* frame, ExceptionsType type) { +void ExceptionsHandler(ExceptionsContext* frame, [[maybe_unused]]ExceptionsType type) { OSPanicException(frame); } \ No newline at end of file diff --git a/Kernel/Source/Lib/String.c b/Kernel/Source/Lib/String.c index 5a23995..ccd5bda 100644 --- a/Kernel/Source/Lib/String.c +++ b/Kernel/Source/Lib/String.c @@ -176,4 +176,8 @@ Int32 StringFormat(ASCII* destination, UInt64 size, const ASCII* format, ...) { Int32 returnValue = StringFormatVariadic(destination, size, format, args); va_end(args); return returnValue; +} + +Boolean StringStartsWith(const ASCII* string, const ASCII* prefix) { + return StringCompareWithLimit(string, prefix, StringGetLength(prefix)) == 0; } \ No newline at end of file