libfdt: Rework APIs for getting subnode
Existing FdtNodeMut::subnode_with_name_len() has following issues:
- Requires mutability although it's unnecessary.
- Doesn't work with raw bytes if it isn't nul-terminating.
This adds following APIs to replace old API.
- FdtNode::subnode(&self, name: &CStr)
- FdtNode::subnode_with_name_bytes(&self, name: &[u8])
Bug: 277993056
Test: atest liblibfdt.integration_test
Change-Id: I60b10eefb5855571020e67b45aef017a859319cf
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index aae75f7..df57bd3 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -527,6 +527,32 @@
Ok(None)
}
}
+
+ /// Returns the subnode of the given name. The name doesn't need to be nul-terminated.
+ pub fn subnode(&self, name: &CStr) -> Result<Option<Self>> {
+ let offset = self.subnode_offset(name.to_bytes())?;
+ Ok(offset.map(|offset| Self { fdt: self.fdt, offset }))
+ }
+
+ /// Returns the subnode of the given name bytes
+ pub fn subnode_with_name_bytes(&self, name: &[u8]) -> Result<Option<Self>> {
+ let offset = self.subnode_offset(name)?;
+ Ok(offset.map(|offset| Self { fdt: self.fdt, offset }))
+ }
+
+ fn subnode_offset(&self, name: &[u8]) -> Result<Option<c_int>> {
+ let namelen = name.len().try_into().unwrap();
+ // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
+ let ret = unsafe {
+ libfdt_bindgen::fdt_subnode_offset_namelen(
+ self.fdt.as_ptr(),
+ self.offset,
+ name.as_ptr().cast::<_>(),
+ namelen,
+ )
+ };
+ fdt_err_or_option(ret)
+ }
}
impl<'a> PartialEq for FdtNode<'a> {
@@ -751,26 +777,6 @@
fdt_err(ret)
}
- /// Returns the subnode of the given name with len.
- pub fn subnode_with_namelen(&'a mut self, name: &CStr, namelen: usize) -> Result<Option<Self>> {
- let offset = self.subnode_offset(&name.to_bytes()[..namelen])?;
- Ok(offset.map(|offset| Self { fdt: self.fdt, offset }))
- }
-
- fn subnode_offset(&self, name: &[u8]) -> Result<Option<c_int>> {
- let namelen = name.len().try_into().unwrap();
- // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
- let ret = unsafe {
- libfdt_bindgen::fdt_subnode_offset_namelen(
- self.fdt.as_ptr(),
- self.offset,
- name.as_ptr().cast::<_>(),
- namelen,
- )
- };
- fdt_err_or_option(ret)
- }
-
fn parent(&'a self) -> Result<FdtNode<'a>> {
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_parent_offset(self.fdt.as_ptr(), self.offset) };