Merge pull request #12 from 0xKSor/feat/dtb

Feat/dtb
This commit is contained in:
hwachakarter
2026-04-26 15:53:55 +09:00
committed by GitHub
8 changed files with 82 additions and 5 deletions
+1
View File
@@ -1,2 +1,3 @@
CompileFlags: CompileFlags:
CompilationDatabase: ../.build/temp/Kernel CompilationDatabase: ../.build/temp/Kernel
Add: [-std=c23]
+4
View File
@@ -8,3 +8,7 @@ static inline UInt32 BytesSwap32(UInt32 value) {
static inline UInt64 BytesSwap64(UInt64 value) { static inline UInt64 BytesSwap64(UInt64 value) {
return __builtin_bswap64(value); return __builtin_bswap64(value);
} }
static inline UInt64 Merge32To64(UInt32 low, UInt32 high) {
return ((UInt64)high << 32) | low;
}
+2
View File
@@ -16,3 +16,5 @@ const ASCII* StringFindLastOccurrenceOfCharacter(const ASCII* string, ASCII sepa
Int32 StringFormatVariadic(ASCII* string, Size size, const ASCII* format, va_list args); Int32 StringFormatVariadic(ASCII* string, Size size, const ASCII* format, va_list args);
Int32 StringFormat(ASCII* destination, UInt64 size, const ASCII* format, ...); Int32 StringFormat(ASCII* destination, UInt64 size, const ASCII* format, ...);
Boolean StringStartsWith(const ASCII* string, const ASCII* prefix);
+2
View File
@@ -17,6 +17,7 @@ typedef unsigned long long UInt;
typedef void* Pointer; typedef void* Pointer;
typedef UInt Address; typedef UInt Address;
typedef UInt32 Address32;
typedef UInt8* BytePointer; typedef UInt8* BytePointer;
typedef UInt8* MemoryPointer; typedef UInt8* MemoryPointer;
@@ -30,3 +31,4 @@ typedef UInt64 Size;
typedef char ASCII; typedef char ASCII;
typedef _Bool Boolean; typedef _Bool Boolean;
#define loop while (1)
+63
View File
@@ -0,0 +1,63 @@
#include <Arch/DTB.h>
#include <OS/Panic.h>
#include <OS/Log.h>
#include <Lib/Bytes.h>
#include <Lib/Align.h>
#include <Lib/String.h>
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;
ASCII* currentNode = "";
while (true) {
UInt32 token = BytesSwap32(*(UInt32*)structs);
structs += 4;
switch (token) {
case FDTTokenBeginNode: {
currentNode = (ASCII*)structs;
UInt32 nameLength = StringGetLength(currentNode);
structs += (nameLength + 1);
structs = (BytePointer)AlignUp64((Address)structs, 4);
break;
}
case FDTTokenProperty: {
UInt32 propertyLength = BytesSwap32(*(UInt32*)structs);
UInt32 nameOffset = BytesSwap32(*(UInt32*)(structs + 4));
structs += 8;
ASCII* propertyName = strings + nameOffset;
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)AlignUp64((Address)structs, 4);
break;
}
case FDTTokenEndNode: break;
case FDTTokenNOP: continue;
case FDTTokenEnd: return;
default:
OSPanic("Invalid DTB token");
}
}
}
+1 -1
View File
@@ -3,6 +3,6 @@
#include <IO/Serial.h> #include <IO/Serial.h>
#include <OS/Panic.h> #include <OS/Panic.h>
void ExceptionsHandler(ExceptionsContext* frame, ExceptionsType type) { void ExceptionsHandler(ExceptionsContext* frame, [[maybe_unused]]ExceptionsType type) {
OSPanicException(frame); OSPanicException(frame);
} }
+4 -3
View File
@@ -1,7 +1,8 @@
#include <OS/Log.h>
#include "../Common/bootinfo.h" #include "../Common/bootinfo.h"
#include <Arch/DTB.h>
#include <OS/Log.h>
void KernelMain(Bootinfo* bootinfo) { void KernelMain(Bootinfo* bootinfo) {
OSLog("DTB located at 0x%x\n", bootinfo->dtb); OSLog("Kernel started.\n");
OSLog("Kernel located at 0x%x\n", bootinfo->kernelInfo.kernelAddress); DTBParse(bootinfo->dtb);
} }
+4
View File
@@ -177,3 +177,7 @@ Int32 StringFormat(ASCII* destination, UInt64 size, const ASCII* format, ...) {
va_end(args); va_end(args);
return returnValue; return returnValue;
} }
Boolean StringStartsWith(const ASCII* string, const ASCII* prefix) {
return StringCompareWithLimit(string, prefix, StringGetLength(prefix)) == 0;
}