Pierre-Clément Tosi | fbb5ee2 | 2023-12-21 13:49:59 +0000 | [diff] [blame] | 1 | // Copyright 2024, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Low-level libfdt_bindgen wrapper, easy to integrate safely in higher-level APIs. |
Pierre-Clément Tosi | d83741d | 2024-02-02 10:44:55 +0000 | [diff] [blame] | 16 | //! |
| 17 | //! These traits decouple the safe libfdt C function calls from the representation of those |
| 18 | //! user-friendly higher-level types, allowing the trait to be shared between different ones, |
| 19 | //! adapted to their use-cases (e.g. alloc-based userspace or statically allocated no_std). |
Pierre-Clément Tosi | fbb5ee2 | 2023-12-21 13:49:59 +0000 | [diff] [blame] | 20 | |
Pierre-Clément Tosi | d83741d | 2024-02-02 10:44:55 +0000 | [diff] [blame] | 21 | use core::ffi::{c_int, CStr}; |
| 22 | use core::ptr; |
| 23 | |
Pierre-Clément Tosi | ecd5bbc | 2023-12-21 15:12:45 +0000 | [diff] [blame] | 24 | use crate::{fdt_err, fdt_err_expect_zero, fdt_err_or_option, FdtError, Phandle, Result}; |
Pierre-Clément Tosi | fbb5ee2 | 2023-12-21 13:49:59 +0000 | [diff] [blame] | 25 | |
| 26 | // Function names are the C function names without the `fdt_` prefix. |
| 27 | |
| 28 | /// Safe wrapper around `fdt_create_empty_tree()` (C function). |
| 29 | pub(crate) fn create_empty_tree(fdt: &mut [u8]) -> Result<()> { |
| 30 | let len = fdt.len().try_into().unwrap(); |
| 31 | let fdt = fdt.as_mut_ptr().cast(); |
| 32 | // SAFETY: fdt_create_empty_tree() only write within the specified length, |
| 33 | // and returns error if buffer was insufficient. |
| 34 | // There will be no memory write outside of the given fdt. |
| 35 | let ret = unsafe { libfdt_bindgen::fdt_create_empty_tree(fdt, len) }; |
| 36 | |
| 37 | fdt_err_expect_zero(ret) |
| 38 | } |
| 39 | |
| 40 | /// Safe wrapper around `fdt_check_full()` (C function). |
| 41 | pub(crate) fn check_full(fdt: &[u8]) -> Result<()> { |
| 42 | let len = fdt.len(); |
| 43 | let fdt = fdt.as_ptr().cast(); |
| 44 | // SAFETY: Only performs read accesses within the limits of the slice. If successful, this |
| 45 | // call guarantees to other unsafe calls that the header contains a valid totalsize (w.r.t. |
| 46 | // 'len' i.e. the self.fdt slice) that those C functions can use to perform bounds |
| 47 | // checking. The library doesn't maintain an internal state (such as pointers) between |
| 48 | // calls as it expects the client code to keep track of the objects (DT, nodes, ...). |
| 49 | let ret = unsafe { libfdt_bindgen::fdt_check_full(fdt, len) }; |
| 50 | |
| 51 | fdt_err_expect_zero(ret) |
| 52 | } |
Pierre-Clément Tosi | d83741d | 2024-02-02 10:44:55 +0000 | [diff] [blame] | 53 | |
| 54 | /// Wrapper for the read-only libfdt.h functions. |
| 55 | /// |
| 56 | /// # Safety |
| 57 | /// |
| 58 | /// Implementors must ensure that at any point where a method of this trait is called, the |
| 59 | /// underlying type returns the bytes of a valid device tree (as validated by `check_full`) |
| 60 | /// through its `.as_fdt_slice` method. |
| 61 | pub(crate) unsafe trait Libfdt { |
| 62 | /// Provides an immutable slice containing the device tree. |
| 63 | /// |
| 64 | /// The implementation must ensure that the size of the returned slice and |
| 65 | /// `fdt_header::totalsize` match. |
| 66 | fn as_fdt_slice(&self) -> &[u8]; |
| 67 | |
| 68 | /// Safe wrapper around `fdt_path_offset_namelen()` (C function). |
| 69 | fn path_offset_namelen(&self, path: &[u8]) -> Result<Option<c_int>> { |
| 70 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 71 | // *_namelen functions don't include the trailing nul terminator in 'len'. |
| 72 | let len = path.len().try_into().map_err(|_| FdtError::BadPath)?; |
| 73 | let path = path.as_ptr().cast(); |
| 74 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) and the |
| 75 | // function respects the passed number of characters. |
| 76 | let ret = unsafe { libfdt_bindgen::fdt_path_offset_namelen(fdt, path, len) }; |
| 77 | |
| 78 | fdt_err_or_option(ret) |
| 79 | } |
| 80 | |
Pierre-Clément Tosi | ecd5bbc | 2023-12-21 15:12:45 +0000 | [diff] [blame] | 81 | /// Safe wrapper around `fdt_node_offset_by_phandle()` (C function). |
| 82 | fn node_offset_by_phandle(&self, phandle: Phandle) -> Result<Option<c_int>> { |
| 83 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 84 | let phandle = phandle.into(); |
| 85 | // SAFETY: Accesses are constrained to the DT totalsize. |
| 86 | let ret = unsafe { libfdt_bindgen::fdt_node_offset_by_phandle(fdt, phandle) }; |
| 87 | |
| 88 | fdt_err_or_option(ret) |
| 89 | } |
| 90 | |
Pierre-Clément Tosi | d83741d | 2024-02-02 10:44:55 +0000 | [diff] [blame] | 91 | /// Safe wrapper around `fdt_node_offset_by_compatible()` (C function). |
| 92 | fn node_offset_by_compatible(&self, prev: c_int, compatible: &CStr) -> Result<Option<c_int>> { |
| 93 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 94 | let compatible = compatible.as_ptr(); |
| 95 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 96 | let ret = unsafe { libfdt_bindgen::fdt_node_offset_by_compatible(fdt, prev, compatible) }; |
| 97 | |
| 98 | fdt_err_or_option(ret) |
| 99 | } |
| 100 | |
| 101 | /// Safe wrapper around `fdt_next_node()` (C function). |
| 102 | fn next_node(&self, node: c_int, depth: usize) -> Result<Option<(c_int, usize)>> { |
| 103 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 104 | let mut depth = depth.try_into().unwrap(); |
| 105 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 106 | let ret = unsafe { libfdt_bindgen::fdt_next_node(fdt, node, &mut depth) }; |
| 107 | |
| 108 | match fdt_err_or_option(ret)? { |
| 109 | Some(offset) if depth >= 0 => { |
| 110 | let depth = depth.try_into().unwrap(); |
| 111 | Ok(Some((offset, depth))) |
| 112 | } |
| 113 | _ => Ok(None), |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// Safe wrapper around `fdt_parent_offset()` (C function). |
| 118 | /// |
| 119 | /// Note that this function returns a `Err` when called on a root. |
| 120 | fn parent_offset(&self, node: c_int) -> Result<c_int> { |
| 121 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 122 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 123 | let ret = unsafe { libfdt_bindgen::fdt_parent_offset(fdt, node) }; |
| 124 | |
| 125 | fdt_err(ret) |
| 126 | } |
| 127 | |
| 128 | /// Safe wrapper around `fdt_supernode_atdepth_offset()` (C function). |
| 129 | /// |
| 130 | /// Note that this function returns a `Err` when called on a node at a depth shallower than |
| 131 | /// the provided `depth`. |
| 132 | fn supernode_atdepth_offset(&self, node: c_int, depth: usize) -> Result<c_int> { |
| 133 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 134 | let depth = depth.try_into().unwrap(); |
| 135 | let nodedepth = ptr::null_mut(); |
| 136 | let ret = |
| 137 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 138 | unsafe { libfdt_bindgen::fdt_supernode_atdepth_offset(fdt, node, depth, nodedepth) }; |
| 139 | |
| 140 | fdt_err(ret) |
| 141 | } |
| 142 | |
| 143 | /// Safe wrapper around `fdt_subnode_offset_namelen()` (C function). |
| 144 | fn subnode_offset_namelen(&self, parent: c_int, name: &[u8]) -> Result<Option<c_int>> { |
| 145 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 146 | let namelen = name.len().try_into().unwrap(); |
| 147 | let name = name.as_ptr().cast(); |
| 148 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 149 | let ret = unsafe { libfdt_bindgen::fdt_subnode_offset_namelen(fdt, parent, name, namelen) }; |
| 150 | |
| 151 | fdt_err_or_option(ret) |
| 152 | } |
| 153 | /// Safe wrapper around `fdt_first_subnode()` (C function). |
| 154 | fn first_subnode(&self, node: c_int) -> Result<Option<c_int>> { |
| 155 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 156 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 157 | let ret = unsafe { libfdt_bindgen::fdt_first_subnode(fdt, node) }; |
| 158 | |
| 159 | fdt_err_or_option(ret) |
| 160 | } |
| 161 | |
| 162 | /// Safe wrapper around `fdt_next_subnode()` (C function). |
| 163 | fn next_subnode(&self, node: c_int) -> Result<Option<c_int>> { |
| 164 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 165 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 166 | let ret = unsafe { libfdt_bindgen::fdt_next_subnode(fdt, node) }; |
| 167 | |
| 168 | fdt_err_or_option(ret) |
| 169 | } |
| 170 | |
| 171 | /// Safe wrapper around `fdt_address_cells()` (C function). |
| 172 | fn address_cells(&self, node: c_int) -> Result<usize> { |
| 173 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 174 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 175 | let ret = unsafe { libfdt_bindgen::fdt_address_cells(fdt, node) }; |
| 176 | |
| 177 | Ok(fdt_err(ret)?.try_into().unwrap()) |
| 178 | } |
| 179 | |
| 180 | /// Safe wrapper around `fdt_size_cells()` (C function). |
| 181 | fn size_cells(&self, node: c_int) -> Result<usize> { |
| 182 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 183 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 184 | let ret = unsafe { libfdt_bindgen::fdt_size_cells(fdt, node) }; |
| 185 | |
| 186 | Ok(fdt_err(ret)?.try_into().unwrap()) |
| 187 | } |
Pierre-Clément Tosi | ecd5bbc | 2023-12-21 15:12:45 +0000 | [diff] [blame] | 188 | |
Pierre-Clément Tosi | 60282ae | 2023-12-21 16:00:02 +0000 | [diff] [blame] | 189 | /// Safe wrapper around `fdt_get_name()` (C function). |
| 190 | fn get_name(&self, node: c_int) -> Result<&[u8]> { |
| 191 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 192 | let mut len = 0; |
| 193 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). On success, the |
| 194 | // function returns valid null terminating string and otherwise returned values are dropped. |
| 195 | let name = unsafe { libfdt_bindgen::fdt_get_name(fdt, node, &mut len) }; |
| 196 | let len = usize::try_from(fdt_err(len)?).unwrap().checked_add(1).unwrap(); |
| 197 | |
| 198 | get_slice_at_ptr(self.as_fdt_slice(), name.cast(), len).ok_or(FdtError::Internal) |
| 199 | } |
| 200 | |
Pierre-Clément Tosi | 410e46a | 2023-12-24 11:33:11 +0000 | [diff] [blame^] | 201 | /// Safe wrapper around `fdt_getprop_namelen()` (C function). |
| 202 | fn getprop_namelen(&self, node: c_int, name: &[u8]) -> Result<Option<&[u8]>> { |
| 203 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 204 | let namelen = name.len().try_into().map_err(|_| FdtError::BadPath)?; |
| 205 | let name = name.as_ptr().cast(); |
| 206 | let mut len = 0; |
| 207 | let prop = |
| 208 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) and the |
| 209 | // function respects the passed number of characters. |
| 210 | unsafe { libfdt_bindgen::fdt_getprop_namelen(fdt, node, name, namelen, &mut len) }; |
| 211 | |
| 212 | if let Some(len) = fdt_err_or_option(len)? { |
| 213 | let len = usize::try_from(len).unwrap(); |
| 214 | let bytes = get_slice_at_ptr(self.as_fdt_slice(), prop.cast(), len); |
| 215 | |
| 216 | Ok(Some(bytes.ok_or(FdtError::Internal)?)) |
| 217 | } else { |
| 218 | Ok(None) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /// Safe wrapper around `fdt_get_property_by_offset()` (C function). |
| 223 | fn get_property_by_offset(&self, offset: c_int) -> Result<&libfdt_bindgen::fdt_property> { |
| 224 | let mut len = 0; |
| 225 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 226 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 227 | let prop = unsafe { libfdt_bindgen::fdt_get_property_by_offset(fdt, offset, &mut len) }; |
| 228 | if prop.is_null() { |
| 229 | fdt_err(len)?; |
| 230 | return Err(FdtError::Internal); // shouldn't happen. |
| 231 | } |
| 232 | |
| 233 | // SAFETY: prop is only returned when it points to valid libfdt_bindgen. |
| 234 | Ok(unsafe { &*prop }) |
| 235 | } |
| 236 | |
| 237 | /// Safe wrapper around `fdt_first_property_offset()` (C function). |
| 238 | fn first_property_offset(&self, node: c_int) -> Result<Option<c_int>> { |
| 239 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 240 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 241 | let ret = unsafe { libfdt_bindgen::fdt_first_property_offset(fdt, node) }; |
| 242 | |
| 243 | fdt_err_or_option(ret) |
| 244 | } |
| 245 | |
| 246 | /// Safe wrapper around `fdt_next_property_offset()` (C function). |
| 247 | fn next_property_offset(&self, prev: c_int) -> Result<Option<c_int>> { |
| 248 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 249 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 250 | let ret = unsafe { libfdt_bindgen::fdt_next_property_offset(fdt, prev) }; |
| 251 | |
| 252 | fdt_err_or_option(ret) |
| 253 | } |
| 254 | |
Pierre-Clément Tosi | ecd5bbc | 2023-12-21 15:12:45 +0000 | [diff] [blame] | 255 | /// Safe wrapper around `fdt_find_max_phandle()` (C function). |
| 256 | fn find_max_phandle(&self) -> Result<Phandle> { |
| 257 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 258 | let mut phandle = 0; |
| 259 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 260 | let ret = unsafe { libfdt_bindgen::fdt_find_max_phandle(fdt, &mut phandle) }; |
| 261 | |
| 262 | fdt_err_expect_zero(ret)?; |
| 263 | |
| 264 | phandle.try_into() |
| 265 | } |
Pierre-Clément Tosi | 60282ae | 2023-12-21 16:00:02 +0000 | [diff] [blame] | 266 | |
| 267 | /// Safe wrapper around `fdt_string()` (C function). |
| 268 | fn string(&self, offset: c_int) -> Result<&CStr> { |
| 269 | let fdt = self.as_fdt_slice().as_ptr().cast(); |
| 270 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 271 | let ptr = unsafe { libfdt_bindgen::fdt_string(fdt, offset) }; |
Pierre-Clément Tosi | a4eaba9 | 2024-01-23 21:24:37 +0000 | [diff] [blame] | 272 | let bytes = |
| 273 | get_slice_from_ptr(self.as_fdt_slice(), ptr.cast()).ok_or(FdtError::Internal)?; |
Pierre-Clément Tosi | 60282ae | 2023-12-21 16:00:02 +0000 | [diff] [blame] | 274 | |
Pierre-Clément Tosi | a4eaba9 | 2024-01-23 21:24:37 +0000 | [diff] [blame] | 275 | CStr::from_bytes_until_nul(bytes).map_err(|_| FdtError::Internal) |
Pierre-Clément Tosi | 60282ae | 2023-12-21 16:00:02 +0000 | [diff] [blame] | 276 | } |
Pierre-Clément Tosi | d83741d | 2024-02-02 10:44:55 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /// Wrapper for the read-write libfdt.h functions. |
| 280 | /// |
| 281 | /// # Safety |
| 282 | /// |
| 283 | /// Implementors must ensure that at any point where a method of this trait is called, the |
| 284 | /// underlying type returns the bytes of a valid device tree (as validated by `check_full`) |
| 285 | /// through its `.as_fdt_slice_mut` method. |
| 286 | /// |
| 287 | /// Some methods may make previously returned values such as node or string offsets or phandles |
| 288 | /// invalid by modifying the device tree (e.g. by inserting or removing new nodes or properties). |
| 289 | /// As most methods take or return such values, instead of marking them all as unsafe, this trait |
| 290 | /// is marked as unsafe as implementors must ensure that methods that modify the validity of those |
| 291 | /// values are never called while the values are still in use. |
| 292 | pub(crate) unsafe trait LibfdtMut { |
| 293 | /// Provides a mutable pointer to a buffer containing the device tree. |
| 294 | /// |
| 295 | /// The implementation must ensure that the size of the returned slice is at least |
| 296 | /// `fdt_header::totalsize`, to allow for device tree growth. |
| 297 | fn as_fdt_slice_mut(&mut self) -> &mut [u8]; |
| 298 | |
| 299 | /// Safe wrapper around `fdt_nop_node()` (C function). |
| 300 | fn nop_node(&mut self, node: c_int) -> Result<()> { |
| 301 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 302 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 303 | let ret = unsafe { libfdt_bindgen::fdt_nop_node(fdt, node) }; |
| 304 | |
| 305 | fdt_err_expect_zero(ret) |
| 306 | } |
| 307 | |
| 308 | /// Safe wrapper around `fdt_add_subnode_namelen()` (C function). |
| 309 | fn add_subnode_namelen(&mut self, node: c_int, name: &[u8]) -> Result<c_int> { |
| 310 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 311 | let namelen = name.len().try_into().unwrap(); |
| 312 | let name = name.as_ptr().cast(); |
| 313 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 314 | let ret = unsafe { libfdt_bindgen::fdt_add_subnode_namelen(fdt, node, name, namelen) }; |
| 315 | |
| 316 | fdt_err(ret) |
| 317 | } |
Pierre-Clément Tosi | 843845d | 2024-01-23 23:35:01 +0000 | [diff] [blame] | 318 | |
Pierre-Clément Tosi | 410e46a | 2023-12-24 11:33:11 +0000 | [diff] [blame^] | 319 | /// Safe wrapper around `fdt_setprop()` (C function). |
| 320 | fn setprop(&mut self, node: c_int, name: &CStr, value: &[u8]) -> Result<()> { |
| 321 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 322 | let name = name.as_ptr(); |
| 323 | let len = value.len().try_into().map_err(|_| FdtError::BadValue)?; |
| 324 | let value = value.as_ptr().cast(); |
| 325 | // SAFETY: New value size is constrained to the DT totalsize |
| 326 | // (validated by underlying libfdt). |
| 327 | let ret = unsafe { libfdt_bindgen::fdt_setprop(fdt, node, name, value, len) }; |
| 328 | |
| 329 | fdt_err_expect_zero(ret) |
| 330 | } |
| 331 | |
Pierre-Clément Tosi | 843845d | 2024-01-23 23:35:01 +0000 | [diff] [blame] | 332 | /// Safe wrapper around `fdt_setprop_placeholder()` (C function). |
| 333 | fn setprop_placeholder(&mut self, node: c_int, name: &CStr, size: usize) -> Result<&mut [u8]> { |
| 334 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 335 | let name = name.as_ptr(); |
| 336 | let len = size.try_into().unwrap(); |
| 337 | let mut data = ptr::null_mut(); |
| 338 | let ret = |
| 339 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 340 | unsafe { libfdt_bindgen::fdt_setprop_placeholder(fdt, node, name, len, &mut data) }; |
| 341 | |
| 342 | fdt_err_expect_zero(ret)?; |
| 343 | |
| 344 | get_mut_slice_at_ptr(self.as_fdt_slice_mut(), data.cast(), size).ok_or(FdtError::Internal) |
| 345 | } |
Pierre-Clément Tosi | 410e46a | 2023-12-24 11:33:11 +0000 | [diff] [blame^] | 346 | |
| 347 | /// Safe wrapper around `fdt_setprop_inplace()` (C function). |
| 348 | fn setprop_inplace(&mut self, node: c_int, name: &CStr, value: &[u8]) -> Result<()> { |
| 349 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 350 | let name = name.as_ptr(); |
| 351 | let len = value.len().try_into().map_err(|_| FdtError::BadValue)?; |
| 352 | let value = value.as_ptr().cast(); |
| 353 | // SAFETY: New value size is constrained to the DT totalsize |
| 354 | // (validated by underlying libfdt). |
| 355 | let ret = unsafe { libfdt_bindgen::fdt_setprop_inplace(fdt, node, name, value, len) }; |
| 356 | |
| 357 | fdt_err_expect_zero(ret) |
| 358 | } |
| 359 | |
| 360 | /// Safe wrapper around `fdt_appendprop()` (C function). |
| 361 | fn appendprop(&mut self, node: c_int, name: &CStr, value: &[u8]) -> Result<()> { |
| 362 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 363 | let name = name.as_ptr(); |
| 364 | let len = value.len().try_into().map_err(|_| FdtError::BadValue)?; |
| 365 | let value = value.as_ptr().cast(); |
| 366 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 367 | let ret = unsafe { libfdt_bindgen::fdt_appendprop(fdt, node, name, value, len) }; |
| 368 | |
| 369 | fdt_err_expect_zero(ret) |
| 370 | } |
| 371 | |
| 372 | /// Safe wrapper around `fdt_appendprop_addrrange()` (C function). |
| 373 | fn appendprop_addrrange( |
| 374 | &mut self, |
| 375 | parent: c_int, |
| 376 | node: c_int, |
| 377 | name: &CStr, |
| 378 | addr: u64, |
| 379 | size: u64, |
| 380 | ) -> Result<()> { |
| 381 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 382 | let name = name.as_ptr(); |
| 383 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 384 | let ret = unsafe { |
| 385 | libfdt_bindgen::fdt_appendprop_addrrange(fdt, parent, node, name, addr, size) |
| 386 | }; |
| 387 | |
| 388 | fdt_err_expect_zero(ret) |
| 389 | } |
| 390 | |
| 391 | /// Safe wrapper around `fdt_delprop()` (C function). |
| 392 | fn delprop(&mut self, node: c_int, name: &CStr) -> Result<()> { |
| 393 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 394 | let name = name.as_ptr(); |
| 395 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) when the |
| 396 | // library locates the node's property. Removing the property may shift the offsets of |
| 397 | // other nodes and properties but the borrow checker should prevent this function from |
| 398 | // being called when FdtNode instances are in use. |
| 399 | let ret = unsafe { libfdt_bindgen::fdt_delprop(fdt, node, name) }; |
| 400 | |
| 401 | fdt_err_expect_zero(ret) |
| 402 | } |
| 403 | |
| 404 | /// Safe wrapper around `fdt_nop_property()` (C function). |
| 405 | fn nop_property(&mut self, node: c_int, name: &CStr) -> Result<()> { |
| 406 | let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast(); |
| 407 | let name = name.as_ptr(); |
| 408 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) when the |
| 409 | // library locates the node's property. |
| 410 | let ret = unsafe { libfdt_bindgen::fdt_nop_property(fdt, node, name) }; |
| 411 | |
| 412 | fdt_err_expect_zero(ret) |
| 413 | } |
Pierre-Clément Tosi | d83741d | 2024-02-02 10:44:55 +0000 | [diff] [blame] | 414 | } |
Pierre-Clément Tosi | 60282ae | 2023-12-21 16:00:02 +0000 | [diff] [blame] | 415 | |
| 416 | pub(crate) fn get_slice_at_ptr(s: &[u8], p: *const u8, len: usize) -> Option<&[u8]> { |
| 417 | let offset = get_slice_ptr_offset(s, p)?; |
| 418 | |
| 419 | s.get(offset..offset.checked_add(len)?) |
| 420 | } |
| 421 | |
Pierre-Clément Tosi | 843845d | 2024-01-23 23:35:01 +0000 | [diff] [blame] | 422 | fn get_mut_slice_at_ptr(s: &mut [u8], p: *mut u8, len: usize) -> Option<&mut [u8]> { |
| 423 | let offset = get_slice_ptr_offset(s, p)?; |
| 424 | |
| 425 | s.get_mut(offset..offset.checked_add(len)?) |
| 426 | } |
| 427 | |
Pierre-Clément Tosi | a4eaba9 | 2024-01-23 21:24:37 +0000 | [diff] [blame] | 428 | fn get_slice_from_ptr(s: &[u8], p: *const u8) -> Option<&[u8]> { |
| 429 | s.get(get_slice_ptr_offset(s, p)?..) |
| 430 | } |
| 431 | |
Pierre-Clément Tosi | 60282ae | 2023-12-21 16:00:02 +0000 | [diff] [blame] | 432 | fn get_slice_ptr_offset(s: &[u8], p: *const u8) -> Option<usize> { |
| 433 | s.as_ptr_range().contains(&p).then(|| { |
| 434 | // SAFETY: Both pointers are in bounds, derive from the same object, and size_of::<T>()=1. |
| 435 | (unsafe { p.offset_from(s.as_ptr()) }) as usize |
| 436 | // TODO(stable_feature(ptr_sub_ptr)): p.sub_ptr() |
| 437 | }) |
| 438 | } |