From a668443bea3fa506fdd5bbc7f52ee703a6c5cf89 Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Tue, 18 Mar 2025 09:51:41 +0200 Subject: [PATCH] dtb: make dtb struct more useful --- output.log | 8 -------- src/util/dtb.zig | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) delete mode 100644 output.log diff --git a/output.log b/output.log deleted file mode 100644 index fff21f0..0000000 --- a/output.log +++ /dev/null @@ -1,8 +0,0 @@ -Hello, dtb is at 0x48000000 -Memory: 'memory@40000000', base 0x40000000, size 0x10000000 -Reserved: 'kernel', base 0x40080000, size 0x1d000 -Reserved: 'fdt', base 0x48000000, size 0x100000 -Reserved: 'page-array', base 0x4009d000, size 0x100000 -Available memory: 253 MiB, page array mem.phys.Page@4009d000 -QEMU: Terminated - \ No newline at end of file diff --git a/src/util/dtb.zig b/src/util/dtb.zig index 536fa14..ee2dc8f 100644 --- a/src/util/dtb.zig +++ b/src/util/dtb.zig @@ -88,6 +88,11 @@ pub const FdtNodeProp = struct { return .{ .prop = self }; } + pub inline fn getString(self: *const @This()) []const u8 { + var sa = self.getStringArray(); + return sa.next() orelse ""; + } + pub inline fn lenU32(self: *const @This()) usize { return self.value.len / @sizeOf(u32); } @@ -382,6 +387,32 @@ pub const Fdt = struct { } } + pub fn find(self: *const @This(), path: []const u8) ?FdtNode { + const trimmedPath = std.mem.trimLeft(u8, path, "/"); + var pathElements = std.mem.splitScalar(u8, trimmedPath, '/'); + var currentNode = self.rootNode(); + if (trimmedPath.len == 0) { + return currentNode; + } + while (pathElements.next()) |element| { + var found: ?FdtNode = null; + var children = currentNode.children(); + while (children.next()) |child| { + if (std.mem.eql(u8, child.name, element)) { + found = child; + break; + } + } + if (found) |f| { + log.info("{s}", .{ element }); + currentNode = f; + } else { + return null; + } + } + return currentNode; + } + fn dump_property(property: *const FdtNodeProp, depth: usize, allStrings: bool) void { for (0..depth) |_| { log.writeRaw(" ");