From 32931021d2f912779d6caeb8cf37e38a15e6fd5b Mon Sep 17 00:00:00 2001 From: karina Date: Sun, 26 Apr 2026 10:48:54 +0400 Subject: [PATCH] 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