Struct fdt::Fdt

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

A flattened devicetree located somewhere in memory

Note on Debug impl: by default the Debug impl of this struct will not print any useful information, if you would like a best-effort tree print which looks similar to dtc’s output, enable the pretty-printing feature

Implementations§

source§

impl<'a> Fdt<'a>

source

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

Construct a new Fdt from a byte buffer

Note: this function does not require that the data be 4-byte aligned

Examples found in repository?
examples/tree_print.rs (line 6)
5
6
7
8
9
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    print_node(fdt.find_node("/").unwrap(), 0);
}
More examples
Hide additional examples
examples/basic_info.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    println!("This is a devicetree representation of a {}", fdt.root().model());
    println!("...which is compatible with at least: {}", fdt.root().compatible().first());
    println!("...and has {} CPU(s)", fdt.cpus().count());
    println!(
        "...and has at least one memory location at: {:#X}\n",
        fdt.memory().regions().next().unwrap().starting_address as usize
    );

    let chosen = fdt.chosen();
    if let Some(bootargs) = chosen.bootargs() {
        println!("The bootargs are: {:?}", bootargs);
    }

    if let Some(stdout) = chosen.stdout() {
        println!("It would write stdout to: {}", stdout.name);
    }

    let soc = fdt.find_node("/soc");
    println!("Does it have a `/soc` node? {}", if soc.is_some() { "yes" } else { "no" });
    if let Some(soc) = soc {
        println!("...and it has the following children:");
        for child in soc.children() {
            println!("    {}", child.name);
        }
    }
}
source

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

Safety

This function performs a read to verify the magic value. If the pointer is invalid this can result in undefined behavior.

Note: this function does not require that the data be 4-byte aligned

source

pub fn aliases(&self) -> Option<Aliases<'_, 'a>>

Return the /aliases node, if one exists

source

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

Searches for the /chosen node, which is always available

Examples found in repository?
examples/basic_info.rs (line 14)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    println!("This is a devicetree representation of a {}", fdt.root().model());
    println!("...which is compatible with at least: {}", fdt.root().compatible().first());
    println!("...and has {} CPU(s)", fdt.cpus().count());
    println!(
        "...and has at least one memory location at: {:#X}\n",
        fdt.memory().regions().next().unwrap().starting_address as usize
    );

    let chosen = fdt.chosen();
    if let Some(bootargs) = chosen.bootargs() {
        println!("The bootargs are: {:?}", bootargs);
    }

    if let Some(stdout) = chosen.stdout() {
        println!("It would write stdout to: {}", stdout.name);
    }

    let soc = fdt.find_node("/soc");
    println!("Does it have a `/soc` node? {}", if soc.is_some() { "yes" } else { "no" });
    if let Some(soc) = soc {
        println!("...and it has the following children:");
        for child in soc.children() {
            println!("    {}", child.name);
        }
    }
}
source

pub fn cpus(&self) -> impl Iterator<Item = Cpu<'_, 'a>>

Return the /cpus node, which is always available

Examples found in repository?
examples/basic_info.rs (line 8)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    println!("This is a devicetree representation of a {}", fdt.root().model());
    println!("...which is compatible with at least: {}", fdt.root().compatible().first());
    println!("...and has {} CPU(s)", fdt.cpus().count());
    println!(
        "...and has at least one memory location at: {:#X}\n",
        fdt.memory().regions().next().unwrap().starting_address as usize
    );

    let chosen = fdt.chosen();
    if let Some(bootargs) = chosen.bootargs() {
        println!("The bootargs are: {:?}", bootargs);
    }

    if let Some(stdout) = chosen.stdout() {
        println!("It would write stdout to: {}", stdout.name);
    }

    let soc = fdt.find_node("/soc");
    println!("Does it have a `/soc` node? {}", if soc.is_some() { "yes" } else { "no" });
    if let Some(soc) = soc {
        println!("...and it has the following children:");
        for child in soc.children() {
            println!("    {}", child.name);
        }
    }
}
source

pub fn memory(&self) -> Memory<'_, 'a>

Returns the memory node, which is always available

Examples found in repository?
examples/basic_info.rs (line 11)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    println!("This is a devicetree representation of a {}", fdt.root().model());
    println!("...which is compatible with at least: {}", fdt.root().compatible().first());
    println!("...and has {} CPU(s)", fdt.cpus().count());
    println!(
        "...and has at least one memory location at: {:#X}\n",
        fdt.memory().regions().next().unwrap().starting_address as usize
    );

    let chosen = fdt.chosen();
    if let Some(bootargs) = chosen.bootargs() {
        println!("The bootargs are: {:?}", bootargs);
    }

    if let Some(stdout) = chosen.stdout() {
        println!("It would write stdout to: {}", stdout.name);
    }

    let soc = fdt.find_node("/soc");
    println!("Does it have a `/soc` node? {}", if soc.is_some() { "yes" } else { "no" });
    if let Some(soc) = soc {
        println!("...and it has the following children:");
        for child in soc.children() {
            println!("    {}", child.name);
        }
    }
}
source

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

Returns an iterator over the memory reservations

source

pub fn root(&self) -> Root<'_, 'a>

Return the root (/) node, which is always available

Examples found in repository?
examples/basic_info.rs (line 6)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    println!("This is a devicetree representation of a {}", fdt.root().model());
    println!("...which is compatible with at least: {}", fdt.root().compatible().first());
    println!("...and has {} CPU(s)", fdt.cpus().count());
    println!(
        "...and has at least one memory location at: {:#X}\n",
        fdt.memory().regions().next().unwrap().starting_address as usize
    );

    let chosen = fdt.chosen();
    if let Some(bootargs) = chosen.bootargs() {
        println!("The bootargs are: {:?}", bootargs);
    }

    if let Some(stdout) = chosen.stdout() {
        println!("It would write stdout to: {}", stdout.name);
    }

    let soc = fdt.find_node("/soc");
    println!("Does it have a `/soc` node? {}", if soc.is_some() { "yes" } else { "no" });
    if let Some(soc) = soc {
        println!("...and it has the following children:");
        for child in soc.children() {
            println!("    {}", child.name);
        }
    }
}
source

pub fn find_node(&self, path: &str) -> Option<FdtNode<'_, 'a>>

Returns the first node that matches the node path, if you want all that match the path, use find_all_nodes. This will automatically attempt to resolve aliases if path is not found.

Node paths must begin with a leading / and are ASCII only. Passing in an invalid node path or non-ASCII node name in the path will return None, as they will not be found within the devicetree structure.

Note: if the address of a node name is left out, the search will find the first node that has a matching name, ignoring the address portion if it exists.

Examples found in repository?
examples/tree_print.rs (line 8)
5
6
7
8
9
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    print_node(fdt.find_node("/").unwrap(), 0);
}
More examples
Hide additional examples
examples/basic_info.rs (line 23)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let fdt = fdt::Fdt::new(MY_FDT).unwrap();

    println!("This is a devicetree representation of a {}", fdt.root().model());
    println!("...which is compatible with at least: {}", fdt.root().compatible().first());
    println!("...and has {} CPU(s)", fdt.cpus().count());
    println!(
        "...and has at least one memory location at: {:#X}\n",
        fdt.memory().regions().next().unwrap().starting_address as usize
    );

    let chosen = fdt.chosen();
    if let Some(bootargs) = chosen.bootargs() {
        println!("The bootargs are: {:?}", bootargs);
    }

    if let Some(stdout) = chosen.stdout() {
        println!("It would write stdout to: {}", stdout.name);
    }

    let soc = fdt.find_node("/soc");
    println!("Does it have a `/soc` node? {}", if soc.is_some() { "yes" } else { "no" });
    if let Some(soc) = soc {
        println!("...and it has the following children:");
        for child in soc.children() {
            println!("    {}", child.name);
        }
    }
}
source

pub fn find_compatible(&self, with: &[&str]) -> Option<FdtNode<'_, 'a>>

Searches for a node which contains a compatible property and contains one of the strings inside of with

source

pub fn find_phandle(&self, phandle: u32) -> Option<FdtNode<'_, 'a>>

Searches for the given phandle

source

pub fn find_all_nodes(
&self,
path: &'a str
) -> impl Iterator<Item = FdtNode<'_, 'a>>

Returns an iterator over all of the available nodes with the given path. This does not attempt to find any node with the same name as the provided path, if you’re looking to do that, Fdt::all_nodes will allow you to iterate over each node’s name and filter for the desired node(s).

For example:

static MY_FDT: &[u8] = include_bytes!("../dtb/test.dtb");

let fdt = fdt::Fdt::new(MY_FDT).unwrap();

for node in fdt.find_all_nodes("/soc/virtio_mmio") {
    println!("{}", node.name);
}

prints:

virtio_mmio@10008000
virtio_mmio@10007000
virtio_mmio@10006000
virtio_mmio@10005000
virtio_mmio@10004000
virtio_mmio@10003000
virtio_mmio@10002000
virtio_mmio@10001000
source

pub fn all_nodes(&self) -> impl Iterator<Item = FdtNode<'_, 'a>>

Returns an iterator over all of the nodes in the devicetree, depth-first

source

pub fn strings(&self) -> impl Iterator<Item = &'a str>

Returns an iterator over all of the strings inside of the strings block

source

pub fn total_size(&self) -> usize

Total size of the devicetree in bytes

Trait Implementations§

source§

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

source§

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

Returns a copy 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<'a> Copy for Fdt<'a>

Auto Trait Implementations§

§

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 Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.