libfdt: Simplify locating nodes at the root
As the DTSpec defines the offset of the root as 0, simplify .root*() to
instantiate the FdtNode* directly, without involving libfdt. Note that a
future patch may turn these functions from -> Result<_> to -> _ as they
should never fail.
Use that to locate nodes at hard-coded paths which are subnodes of the
root by calling .root().subnode(), which removes the need for libfdt to
do any path parsing and results in a single (and simple) C call:
fdt_subnode_offset_namelen(fdt, 0, name, name.len());
which simply parses the root.
Note that the same change can't be made for the mutable versions because
the borrow checker complains about the way we are getting subnodes of
FdtNodeMut instances.
Test: m pvmfw
Test: atest liblibfdt.integration_test
Change-Id: I1bcdc9f7a4883e31ef60cfaed61f26465386c996
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index c0d8d55..d153303 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -668,7 +668,7 @@
///
/// NOTE: This does not support individual "/memory@XXXX" banks.
pub fn memory(&self) -> Result<MemRegIterator> {
- let node = self.node(cstr!("/memory"))?.ok_or(FdtError::NotFound)?;
+ let node = self.root()?.subnode(cstr!("memory"))?.ok_or(FdtError::NotFound)?;
if node.device_type()? != Some(cstr!("memory")) {
return Err(FdtError::BadValue);
}
@@ -682,7 +682,7 @@
/// Returns the standard /chosen node.
pub fn chosen(&self) -> Result<Option<FdtNode>> {
- self.node(cstr!("/chosen"))
+ self.root()?.subnode(cstr!("chosen"))
}
/// Returns the standard /chosen node as mutable.
@@ -692,12 +692,12 @@
/// Returns the root node of the tree.
pub fn root(&self) -> Result<FdtNode> {
- self.node(cstr!("/"))?.ok_or(FdtError::Internal)
+ Ok(FdtNode { fdt: self, offset: NodeOffset::ROOT })
}
/// Returns the standard /__symbols__ node.
pub fn symbols(&self) -> Result<Option<FdtNode>> {
- self.node(cstr!("/__symbols__"))
+ self.root()?.subnode(cstr!("__symbols__"))
}
/// Returns the standard /__symbols__ node as mutable
@@ -738,7 +738,7 @@
/// Returns the mutable root node of the tree.
pub fn root_mut(&mut self) -> Result<FdtNodeMut> {
- self.node_mut(cstr!("/"))?.ok_or(FdtError::Internal)
+ Ok(FdtNodeMut { fdt: self, offset: NodeOffset::ROOT })
}
/// Returns a mutable tree node by its full path.