fdt/
pretty_print.rs

1// This Source Code Form is subject to the terms of the Mozilla Public License,
2// v. 2.0. If a copy of the MPL was not distributed with this file, You can
3// obtain one at https://mozilla.org/MPL/2.0/.
4
5pub fn print_node(
6    f: &mut core::fmt::Formatter<'_>,
7    node: crate::node::FdtNode<'_, '_>,
8    n_spaces: usize,
9) -> core::fmt::Result {
10    write!(f, "{:width$}", ' ', width = n_spaces)?;
11    writeln!(f, "{} {{", if node.name.is_empty() { "/" } else { node.name })?;
12    let mut were_props = false;
13    for prop in node.properties() {
14        were_props = true;
15
16        match prop.name {
17            "reg" => {
18                write!(f, "{:width$}reg = <", ' ', width = n_spaces + 4)?;
19                for (i, reg) in node.reg().unwrap().enumerate() {
20                    if i > 0 {
21                        write!(f, " ")?;
22                    }
23
24                    match reg.size {
25                        Some(size) => {
26                            write!(f, "{:#x} {:#x}", reg.starting_address as usize, size)?
27                        }
28                        None => write!(f, "{:#x}", reg.starting_address as usize)?,
29                    }
30                }
31                writeln!(f, ">")?;
32            }
33            "compatible" => writeln!(
34                f,
35                "{:width$}compatible = {:?}",
36                ' ',
37                prop.as_str().unwrap(),
38                width = n_spaces + 4
39            )?,
40            name if name.contains("-cells") => {
41                writeln!(
42                    f,
43                    "{:width$}{} = <{:#x}>",
44                    ' ',
45                    name,
46                    prop.as_usize().unwrap(),
47                    width = n_spaces + 4
48                )?;
49            }
50            _ => match prop.as_str() {
51                Some(value)
52                    if (!value.is_empty() && value.chars().all(|c| c.is_ascii_graphic()))
53                        || prop.value == [0] =>
54                {
55                    writeln!(f, "{:width$}{} = {:?}", ' ', prop.name, value, width = n_spaces + 4)?
56                }
57                _ => match prop.value.len() {
58                    4 | 8 => writeln!(
59                        f,
60                        "{:width$}{} = <{:#x}>",
61                        ' ',
62                        prop.name,
63                        prop.as_usize().unwrap(),
64                        width = n_spaces + 4
65                    )?,
66                    _ => writeln!(
67                        f,
68                        "{:width$}{} = {:?}",
69                        ' ',
70                        prop.name,
71                        prop.value,
72                        width = n_spaces + 4
73                    )?,
74                },
75            },
76        }
77    }
78
79    if node.children().next().is_some() && were_props {
80        writeln!(f)?;
81    }
82
83    let mut first = true;
84    for child in node.children() {
85        if !first {
86            writeln!(f)?;
87        }
88
89        print_node(f, child, n_spaces + 4)?;
90        first = false;
91    }
92
93    if n_spaces > 0 {
94        write!(f, "{:width$}", ' ', width = n_spaces)?;
95    }
96
97    writeln!(f, "}};")?;
98
99    Ok(())
100}