const std = @import("std"); const bigToNative = std.mem.bigToNative; const Self = @This(); const Header = struct { magic: u32, total_size: u32, off_dt_struct: u32, off_dt_strings: u32, off_mem_rsvmap: u32, version: u32, last_comp_version: u32, boot_cpuid_phys: u32, size_dt_strings: u32, size_dt_struct: u32, }; base: [*]const u8, pub fn new(base: [*]const u8) Self { return .{ .base = base }; } fn getHeaderField(self: Self, comptime field_name: []const u8) u32 { const ptr: *const u32 = @ptrCast( @alignCast(self.base + @offsetOf(Header, field_name)), ); return bigToNative(u32, ptr.*); } pub fn magic(self: Self) u32 { return self.getHeaderField("magic"); } pub fn totalSize(self: Self) u32 { return self.getHeaderField("total_size"); } pub fn version(self: Self) u32 { return self.getHeaderField("version"); } pub const ReserveEntry = struct { address: u64, size: u64, }; const MemRsvmapIter = struct { cur: [*]const u64, pub fn next(self: *@This()) ?ReserveEntry { const address = bigToNative(u64, self.cur[0]); const size = bigToNative(u64, self.cur[1]); if (address == 0 and size == 0) { return null; } else { self.cur += 2; return .{ .address = address, .size = size, }; } } }; pub fn memRsvmap(self: Self) MemRsvmapIter { const offset = self.getHeaderField("off_mem_rsvmap"); return .{ .cur = @ptrCast(@alignCast(self.base + offset)), }; }