libfdt: Add setprop_placeholder & Improve trimprop
Add a wrapper for the previously unsupported fdt_setprop_placeholder().
As fdt_setprop() internally calls fdt_setprop_placeholder()+memcpy(),
the previous implementation of trimprop() was effectively resulting in
fdt_setprop_placeholder(..., len, ...);
memcpy(data, data, len);
for which it needed to use getprop_internal() to play some tricks around
the borrow checker to make that memcpy have no effect. Instead, simply
implement it by calling fdt_setprop_placeholder() directly, making the C
calls more efficient and the Rust code simpler and safer.
Test: m pvmfw
Test: atest liblibfdt.integration_test
Change-Id: I4b847dd51c230d4c6c9b6b2efd6c45087544c955
diff --git a/libs/libfdt/src/libfdt.rs b/libs/libfdt/src/libfdt.rs
index a89738f..7fa217b 100644
--- a/libs/libfdt/src/libfdt.rs
+++ b/libs/libfdt/src/libfdt.rs
@@ -261,6 +261,21 @@
fdt_err(ret)
}
+
+ /// Safe wrapper around `fdt_setprop_placeholder()` (C function).
+ fn setprop_placeholder(&mut self, node: c_int, name: &CStr, size: usize) -> Result<&mut [u8]> {
+ let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast();
+ let name = name.as_ptr();
+ let len = size.try_into().unwrap();
+ let mut data = ptr::null_mut();
+ let ret =
+ // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
+ unsafe { libfdt_bindgen::fdt_setprop_placeholder(fdt, node, name, len, &mut data) };
+
+ fdt_err_expect_zero(ret)?;
+
+ get_mut_slice_at_ptr(self.as_fdt_slice_mut(), data.cast(), size).ok_or(FdtError::Internal)
+ }
}
pub(crate) fn get_slice_at_ptr(s: &[u8], p: *const u8, len: usize) -> Option<&[u8]> {
@@ -269,6 +284,12 @@
s.get(offset..offset.checked_add(len)?)
}
+fn get_mut_slice_at_ptr(s: &mut [u8], p: *mut u8, len: usize) -> Option<&mut [u8]> {
+ let offset = get_slice_ptr_offset(s, p)?;
+
+ s.get_mut(offset..offset.checked_add(len)?)
+}
+
fn get_slice_from_ptr(s: &[u8], p: *const u8) -> Option<&[u8]> {
s.get(get_slice_ptr_offset(s, p)?..)
}