Skip to main content

sparreal_kernel/os/platform/
dtb.rs

1use core::{fmt::Debug, ptr::NonNull};
2
3use fdt_raw::Fdt;
4
5#[derive(Clone, Copy)]
6pub struct DeviceTree {
7    ptr: usize,
8}
9
10impl Debug for DeviceTree {
11    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
12        write!(f, "DeviceTree@{:p}", self.ptr as *const u8)
13    }
14}
15
16impl DeviceTree {
17    pub(crate) fn new(ptr: NonNull<u8>) -> Self {
18        DeviceTree {
19            ptr: ptr.as_ptr() as usize,
20        }
21    }
22
23    pub fn as_slice(&self) -> &[u8] {
24        let ptr = self.ptr as *mut u8;
25        let fdt = unsafe { Fdt::from_ptr(ptr).unwrap() };
26        fdt.as_slice()
27    }
28}