imp: pass around root dtb node instead of global value

This commit is contained in:
Jordan ⌨️ 2025-04-20 02:12:13 +02:00
parent f7bcada952
commit f2ac114278
3 changed files with 11 additions and 10 deletions

View file

@ -16,8 +16,8 @@ void rv32_start(__attribute__((unused)) size_t hartid, uintptr_t dtb) {
write_csr$(stvec, (uint32_t)interrupt_kernel);
BumpAllocator alloc = bump_allocator_create((void*)early_heap, sizeof(early_heap));
dtb_init(dtb, &alloc.base);
DTBNode* mem = dtb_lookup("reserved-memory");
DTBNode* root = dtb_init(dtb, &alloc.base);
DTBNode* mem = dtb_lookup(root, "reserved-memory");
if (mem == NULL) {
error$("Failed to find reserved-memory node in device tree");

View file

@ -6,15 +6,14 @@
#include "mod.h"
static DTBNode* root = NULL;
static uint32_t read_offset(uint8_t** offset) {
uint32_t value = from_be32(*(uint32_t*)*offset);
*offset += sizeof(uint32_t);
return value;
}
static void dtb_parse(FDTHeader* dtb_header, Allocator* alloc) {
static DTBNode* dtb_parse(FDTHeader* dtb_header, Allocator* alloc) {
DTBNode* root = NULL;
if (dtb_header == NULL) {
error$("Device tree blob not initialized");
abort();
@ -126,9 +125,11 @@ static void dtb_parse(FDTHeader* dtb_header, Allocator* alloc) {
}
}
}
return root;
}
void dtb_init(uintptr_t dtb, Allocator* alloc) {
DTBNode* dtb_init(uintptr_t dtb, Allocator* alloc) {
FDTHeader* dtb_header = (FDTHeader*)dtb;
if (from_be32(dtb_header->magic) != DTB_MAGIC) {
@ -136,10 +137,10 @@ void dtb_init(uintptr_t dtb, Allocator* alloc) {
abort();
}
dtb_parse(dtb_header, alloc);
return dtb_parse(dtb_header, alloc);
}
DTBNode* dtb_lookup(char const* name) {
DTBNode* dtb_lookup(DTBNode* root, char const* name) {
DTBNode* node = root->children;
while (node->next != NULL) {

View file

@ -40,6 +40,6 @@ typedef struct _DTBNode {
struct _DTBNode* parent;
} DTBNode;
void dtb_init(uintptr_t dtb, Allocator* alloc);
DTBNode* dtb_init(uintptr_t dtb, Allocator* alloc);
DTBNode* dtb_lookup(char const* name);
DTBNode* dtb_lookup(DTBNode* root, char const* name);