Skip to main content

Dtb

Struct Dtb 

Source
pub struct Dtb<'a>(/* private fields */);
Expand description

设备树二进制对象。

Implementations§

Source§

impl Dtb<'static>

Source

pub unsafe fn from_raw_parts(ptr: *const u8) -> Result<Self, HeaderError>

构造设备树二进制对象。

§Safety

如果指针指向一个有效的 DTB 首部,其中描述的整个二进制对象会被切片。

Source

pub unsafe fn from_raw_parts_filtered( ptr: *const u8, f: impl Fn(&HeaderError) -> bool, ) -> Result<Self, HeaderError>

构造设备树二进制对象。

§Safety

如果指针指向一个有效的 DTB 首部,其中描述的整个二进制对象会被切片。

Examples found in repository?
examples/qemu-virt.rs (lines 15-20)
6fn main() {
7    let mut aligned = vec![0usize; DEVICE_TREE.len() / core::mem::size_of::<usize>()];
8    unsafe {
9        aligned
10            .as_mut_ptr()
11            .copy_from_nonoverlapping(DEVICE_TREE.as_ptr() as _, aligned.len());
12    }
13
14    let dtb = unsafe {
15        Dtb::from_raw_parts_filtered(aligned.as_ptr() as _, |e| {
16            matches!(
17                e,
18                HeaderError::Misaligned(4) | HeaderError::LastCompVersion(16)
19            )
20        })
21    }
22    .unwrap();
23    dtb.walk(|path, obj| match obj {
24        DtbObj::SubNode { name } => {
25            println!("{}{path}/{}", indent(path.level(), INDENT_WIDTH), unsafe {
26                core::str::from_utf8_unchecked(name)
27            });
28            WalkOperation::StepInto
29        }
30        DtbObj::Property(prop) => {
31            let indent = indent(path.level(), INDENT_WIDTH);
32            println!("{indent}{prop:?}");
33            WalkOperation::StepOver
34        }
35    });
36    println!("ok");
37}
Source

pub unsafe fn from_raw_parts_unchecked(ptr: *const u8) -> Self

不检查首部正确性,直接构造设备树二进制对象。

§Safety

假设指针指向一个正确的设备树二进制对象,其首部描述的整个二进制对象会被切片。

Source§

impl<'a> Dtb<'a>

Source

pub fn from_slice(slice: &'a [u8]) -> Result<Self, ConvertError>

从内存切片安全地创建设备树二进制对象。

Source§

impl Dtb<'_>

Source

pub const fn total_size(&self) -> usize

返回整个二进制对象的尺寸。

Source

pub fn walk(&self, f: impl FnMut(&Path<'_>, DtbObj<'_>) -> WalkOperation)

遍历。

Examples found in repository?
examples/qemu-virt.rs (lines 23-35)
6fn main() {
7    let mut aligned = vec![0usize; DEVICE_TREE.len() / core::mem::size_of::<usize>()];
8    unsafe {
9        aligned
10            .as_mut_ptr()
11            .copy_from_nonoverlapping(DEVICE_TREE.as_ptr() as _, aligned.len());
12    }
13
14    let dtb = unsafe {
15        Dtb::from_raw_parts_filtered(aligned.as_ptr() as _, |e| {
16            matches!(
17                e,
18                HeaderError::Misaligned(4) | HeaderError::LastCompVersion(16)
19            )
20        })
21    }
22    .unwrap();
23    dtb.walk(|path, obj| match obj {
24        DtbObj::SubNode { name } => {
25            println!("{}{path}/{}", indent(path.level(), INDENT_WIDTH), unsafe {
26                core::str::from_utf8_unchecked(name)
27            });
28            WalkOperation::StepInto
29        }
30        DtbObj::Property(prop) => {
31            let indent = indent(path.level(), INDENT_WIDTH);
32            println!("{indent}{prop:?}");
33            WalkOperation::StepOver
34        }
35    });
36    println!("ok");
37}

Auto Trait Implementations§

§

impl<'a> Freeze for Dtb<'a>

§

impl<'a> RefUnwindSafe for Dtb<'a>

§

impl<'a> Send for Dtb<'a>

§

impl<'a> Sync for Dtb<'a>

§

impl<'a> Unpin for Dtb<'a>

§

impl<'a> UnsafeUnpin for Dtb<'a>

§

impl<'a> UnwindSafe for Dtb<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.