Skip to content

Commit 904823f

Browse files
committed
wip: little refactors + add CPU topology nodes
1 parent 3e15f90 commit 904823f

File tree

14 files changed

+434
-43
lines changed

14 files changed

+434
-43
lines changed

examples/tree_print.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
// use fdt::node::FdtNode;
1+
use fdt::helpers::UnalignedInfallibleNode;
22

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

5-
// fn main() {
6-
// let fdt = fdt::Fdt::new(MY_FDT).unwrap();
5+
fn main() {
6+
let fdt = fdt::Fdt::new_unaligned(MY_FDT).unwrap();
77

8-
// print_node(fdt.find_node("/").unwrap(), 0);
9-
// }
8+
print_node(fdt.find_node("/").unwrap(), 0);
9+
}
1010

11-
// fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) {
12-
// (0..n_spaces).for_each(|_| print!(" "));
13-
// println!("{}/", node.name);
11+
fn print_node(node: UnalignedInfallibleNode<'_>, n_spaces: usize) {
12+
(0..n_spaces).for_each(|_| print!(" "));
13+
println!("{}/", node.name());
1414

15-
// for child in node.children() {
16-
// print_node(child, n_spaces + 4);
17-
// }
18-
// }
19-
20-
fn main() {}
15+
for child in node.children() {
16+
print_node(child, n_spaces + 4);
17+
}
18+
}

src/helpers.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::{
2+
nodes::{root::Root, Node},
3+
parsing::{aligned::AlignedParser, unaligned::UnalignedParser, NoPanic, Panic, ParserWithMode},
4+
};
5+
6+
pub type FallibleParser<'a, P> = (<P as ParserWithMode<'a>>::Parser, NoPanic);
7+
pub type FallibleNode<'a, P> = Node<'a, FallibleParser<'a, P>>;
8+
pub type FallibleRoot<'a, P> = Root<'a, FallibleParser<'a, P>>;
9+
10+
pub type AlignedFallibleNode<'a> = Node<'a, (AlignedParser<'a>, NoPanic)>;
11+
pub type UnalignedFallibleNode<'a> = Node<'a, (UnalignedParser<'a>, NoPanic)>;
12+
13+
pub type AlignedInfallibleNode<'a> = Node<'a, (AlignedParser<'a>, Panic)>;
14+
pub type UnalignedInfallibleNode<'a> = Node<'a, (UnalignedParser<'a>, Panic)>;

src/lib.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ extern crate std;
6262
mod tests;
6363

6464
pub mod cell_collector;
65+
pub mod helpers;
6566
pub mod nodes;
6667
mod parsing;
6768
mod pretty_print;
6869
pub mod properties;
6970
mod util;
7071

72+
use helpers::FallibleParser;
7173
use nodes::{
7274
root::{AllCompatibleIter, AllNodesIter, AllNodesWithNameIter, Root},
73-
FallibleParser, Node,
75+
Node,
7476
};
7577
use parsing::{
7678
aligned::AlignedParser, unaligned::UnalignedParser, NoPanic, Panic, ParseError, Parser, ParserWithMode,
@@ -93,11 +95,12 @@ pub enum FdtError {
9395
SliceTooSmall,
9496
/// An error was encountered during parsing
9597
ParseError(ParseError),
96-
PHandleNotFound(u32),
98+
MissingPHandleNode(u32),
9799
MissingParent,
98100
MissingRequiredNode(&'static str),
99101
MissingRequiredProperty(&'static str),
100102
InvalidPropertyValue,
103+
InvalidNodeName,
101104
CollectCellsError,
102105
}
103106

@@ -114,7 +117,7 @@ impl core::fmt::Display for FdtError {
114117
FdtError::BadPtr => write!(f, "an invalid pointer was passed"),
115118
FdtError::SliceTooSmall => write!(f, "provided slice is too small"),
116119
FdtError::ParseError(e) => core::fmt::Display::fmt(e, f),
117-
FdtError::PHandleNotFound(value) => {
120+
FdtError::MissingPHandleNode(value) => {
118121
write!(f, "a node containing the `phandle` property value of `{value}` was not found")
119122
}
120123
FdtError::MissingParent => write!(f, "node parent is not present but needed to parse a property"),
@@ -125,6 +128,9 @@ impl core::fmt::Display for FdtError {
125128
write!(f, "FDT node is missing a required property `{}`", name)
126129
}
127130
FdtError::InvalidPropertyValue => write!(f, "FDT property value is invalid"),
131+
FdtError::InvalidNodeName => {
132+
write!(f, "FDT node contained invalid characters or did not match the expected format")
133+
}
128134
FdtError::CollectCellsError => {
129135
write!(f, "overflow occurred while collecting `#<specifier>-cells` size values into the desired type")
130136
}

src/nodes.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod memory;
55
pub mod root;
66

77
use crate::{
8+
helpers::FallibleNode,
89
parsing::{
910
aligned::AlignedParser, BigEndianToken, NoPanic, Panic, PanicMode, ParseError, Parser, ParserWithMode,
1011
StringsBlock, StructsBlock,
@@ -95,10 +96,6 @@ impl core::fmt::Display for NodeName<'_> {
9596
}
9697
}
9798

98-
pub type FallibleParser<'a, P> = (<P as ParserWithMode<'a>>::Parser, NoPanic);
99-
pub type FallibleNode<'a, P> = Node<'a, FallibleParser<'a, P>>;
100-
pub type FallibleRoot<'a, P> = Root<'a, FallibleParser<'a, P>>;
101-
10299
/// A generic devicetree node.
103100
pub struct Node<'a, P: ParserWithMode<'a>> {
104101
pub(crate) this: &'a RawNode<<P as Parser<'a>>::Granularity>,

src/nodes/aliases.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use super::{AsNode, FallibleNode, FallibleParser, Node, NodePropertiesIter};
2-
use crate::parsing::{NoPanic, ParserWithMode};
1+
use super::{AsNode, Node, NodePropertiesIter};
2+
use crate::{
3+
helpers::{FallibleNode, FallibleParser},
4+
parsing::{NoPanic, ParserWithMode},
5+
};
36

47
/// [Devicetree 3.3. `/aliases`
58
/// node](https://devicetree-specification.readthedocs.io/en/latest/chapter3-devicenodes.html#aliases-node)

src/nodes/chosen.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
33
// obtain one at https://mozilla.org/MPL/2.0/.
44

5-
use super::{FallibleNode, FallibleParser, FallibleRoot, Node};
5+
use super::{FallibleNode, Node};
66
use crate::{
7+
helpers::{FallibleParser, FallibleRoot},
78
parsing::{aligned::AlignedParser, Panic, ParseError, ParserWithMode},
89
FdtError,
910
};

0 commit comments

Comments
 (0)