Skip to main content

Fdt

Struct Fdt 

Source
pub struct Fdt<'a> { /* private fields */ }
Expand description

A parsed Flattened Device Tree (FDT).

This is the main type for working with device tree blobs. It provides methods for traversing the tree, finding nodes by path, translating addresses, and accessing special nodes like /chosen and /memory.

The Fdt holds a reference to the underlying device tree data and performs zero-copy parsing where possible.

Implementations§

Source§

impl<'a> Fdt<'a>

Source

pub fn from_bytes(data: &'a [u8]) -> Result<Fdt<'a>, FdtError>

Create a new Fdt from a byte slice.

Parses the FDT header and validates the magic number. The slice must contain a complete, valid device tree blob.

§Errors

Returns FdtError if the header is invalid, the magic number doesn’t match, or the buffer is too small.

Source

pub unsafe fn from_ptr(ptr: *mut u8) -> Result<Fdt<'a>, FdtError>

Create a new Fdt from a raw pointer.

Parses an FDT from the memory location pointed to by ptr. This is useful when working with device trees loaded by bootloaders.

§Safety

The caller must ensure that the pointer is valid and points to a memory region of at least totalsize bytes that contains a valid device tree blob. The memory must remain valid for the lifetime 'a.

§Errors

Returns FdtError if the header is invalid or the magic number doesn’t match.

Source

pub fn header(&self) -> &Header

Returns a reference to the FDT header.

Source

pub fn as_slice(&self) -> &'a [u8]

Returns the underlying byte slice.

Source

pub fn all_nodes(&self) -> FdtIter<'a>

Returns an iterator over all nodes in the device tree.

Source

pub fn find_by_path(&self, path: &str) -> Option<Node<'a>>

Find a node by its absolute path or alias.

The path can be an absolute path starting with ‘/’, or an alias defined in the /aliases node. Returns None if the node is not found.

§Example
let node = fdt.find_by_path("/soc@30000000/serial@10000");
let uart = fdt.find_by_path("serial0");  // Using alias
Source

pub fn find_children_by_path(&self, path: &str) -> ChildrenIter<'a>

Find all direct children of a node at the given path.

Returns an iterator over all direct child nodes (one level deeper) of the node at the specified path. Returns None if the node is not found.

Only direct children are yielded — grandchildren and deeper descendants are skipped.

§Example
// List all direct children of /soc
if let Some(children) = fdt.find_children_by_path("/soc") {
    for child in children {
        println!("{}", child.name());
    }
}
Source

pub fn translate_address(&self, path: &'a str, address: u64) -> u64

Translate a device address to a CPU physical address.

This function implements address translation similar to Linux’s of_translate_address. It walks up the device tree hierarchy, applying each parent’s ranges property to translate the child address space to the parent address space.

The translation starts from the node at path and walks up through each parent, applying the ranges property until reaching the root.

§Arguments
  • path - Node path (absolute path starting with ‘/’ or alias name)
  • address - Device address from the node’s reg property
§Returns

The translated CPU physical address. If translation fails at any point (e.g., a parent node has no ranges property), the original address is returned.

Source

pub fn translate_addresses(&self, path: &'a str, addresses: &mut [u64])

Translate multiple device addresses to CPU physical addresses in a single pass.

This is more efficient than calling translate_address multiple times for the same node path, because the tree is walked only once. Each parent node’s ranges property is looked up once and applied to all addresses in the batch.

§Arguments
  • path - Node path (absolute path starting with ‘/’ or alias name)
  • addresses - Mutable slice of device addresses to translate in place. The addresses are modified with the translated CPU physical addresses.

If translation fails for any address at any level, the original address value is preserved for that address.

Source

pub fn memory_reservations(&self) -> MemoryReservationIter<'a>

Returns an iterator over memory reservation entries.

Source

pub fn chosen(&self) -> Option<Chosen<'a>>

Returns the /chosen node if it exists.

Source

pub fn memory(&self) -> impl Iterator<Item = Memory<'a>> + 'a

Returns an iterator over all memory nodes.

Source

pub fn reserved_memory(&self) -> impl Iterator<Item = Node<'a>> + 'a

Returns an iterator over nodes in the /reserved-memory region.

Trait Implementations§

Source§

impl<'a> Clone for Fdt<'a>

Source§

fn clone(&self) -> Fdt<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Fdt<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Fdt<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Fdt<'a>

§

impl<'a> RefUnwindSafe for Fdt<'a>

§

impl<'a> Send for Fdt<'a>

§

impl<'a> Sync for Fdt<'a>

§

impl<'a> Unpin for Fdt<'a>

§

impl<'a> UnwindSafe for Fdt<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.