[PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction
From: Ayush Singh
Date: Sat Aug 16 2025 - 01:59:37 EST
Allow applying devicetree overlays from Rust.
The overlay is removed on Drop.
Signed-off-by: Ayush Singh <ayush@xxxxxxxxxxxxxxx>
---
rust/kernel/of.rs | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs
index 74d42dce06f0e9f67a0e5bd1287117b4966d4af9..646148cc959fd5414271f9cf9023ef5d77a2ac2c 100644
--- a/rust/kernel/of.rs
+++ b/rust/kernel/of.rs
@@ -75,3 +75,37 @@ pub fn as_raw(&self) -> *mut bindings::device_node {
self.0.get()
}
}
+
+/// Devicetree overlay present in livetree.
+///
+/// The overlay is removed on Drop
+pub struct OvcsId(kernel::ffi::c_int);
+
+impl OvcsId {
+ /// Create and apply an overlay changeset to the live tree.
+ pub fn of_overlay_fdt_apply(overlay: &[u8], base: &DeviceNode) -> Result<Self> {
+ let mut ovcs_id: kernel::ffi::c_int = 0;
+
+ // Ensure that overlay length fits in u32
+ let Ok(overlay_len) = overlay.len().try_into() else {
+ return Err(crate::error::code::E2BIG);
+ };
+
+ crate::error::to_result(unsafe {
+ bindings::of_overlay_fdt_apply(
+ overlay.as_ptr().cast(),
+ overlay_len,
+ &mut ovcs_id,
+ base.as_raw(),
+ )
+ })?;
+
+ Ok(Self(ovcs_id))
+ }
+}
+
+impl Drop for OvcsId {
+ fn drop(&mut self) {
+ unsafe { bindings::of_overlay_remove(&mut self.0) };
+ }
+}
--
2.50.1