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 b414b986eb
3 changed files with 12 additions and 11 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,24 +6,23 @@
#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) {
if (dtb_header == NULL) {
error$("Device tree blob not initialized");
abort();
}
DTBNode* current_node = NULL;
DTBNode* root = NULL;
char const* strtab = (char const*)((uint8_t*)dtb_header + from_be32(dtb_header->off_dt_strings));
uint8_t* offset = (uint8_t*)dtb_header + from_be32(dtb_header->off_dt_struct);
int found_end = 0;
DTBNode* current_node = NULL;
while (!found_end) {
uint32_t token = read_offset(&offset);
@ -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);