libfdt: Add FdtNode::supernode_at_depth()
This is helpful to locate special node (e.g. __overlay__).
Bug: 277993056
Test: atest liblibfdt.integration_test
Change-Id: I833e586437f0e5934a448dddf80d1bd947c3bd4f
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index 03a1f8e..5a7bd14 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -29,6 +29,7 @@
use core::fmt;
use core::mem;
use core::ops::Range;
+use core::ptr;
use core::result;
use zerocopy::AsBytes as _;
@@ -279,6 +280,21 @@
Ok(Self { fdt: self.fdt, offset: fdt_err(ret)? })
}
+ /// Returns supernode with depth. Note that root is at depth 0.
+ pub fn supernode_at_depth(&self, depth: usize) -> Result<Self> {
+ // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
+ let ret = unsafe {
+ libfdt_bindgen::fdt_supernode_atdepth_offset(
+ self.fdt.as_ptr(),
+ self.offset,
+ depth.try_into().unwrap(),
+ ptr::null_mut(),
+ )
+ };
+
+ Ok(Self { fdt: self.fdt, offset: fdt_err(ret)? })
+ }
+
/// Returns the standard (deprecated) device_type <string> property.
pub fn device_type(&self) -> Result<Option<&CStr>> {
self.getprop_str(CStr::from_bytes_with_nul(b"device_type\0").unwrap())