David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1 | // Copyright 2022, 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 | //! Wrapper around libfdt library. Provides parsing/generating functionality |
| 16 | //! to a bare-metal environment. |
| 17 | |
| 18 | #![no_std] |
| 19 | |
Andrew Walbran | 55ad01b | 2022-12-05 17:00:40 +0000 | [diff] [blame] | 20 | mod iterators; |
| 21 | |
Jaewan Kim | fe06c85 | 2023-10-05 23:40:06 +0900 | [diff] [blame] | 22 | pub use iterators::{ |
Jaewan Kim | c9e1411 | 2023-12-04 17:05:27 +0900 | [diff] [blame] | 23 | AddressRange, CellIterator, CompatibleIterator, DescendantsIterator, MemRegIterator, |
| 24 | PropertyIterator, RangesIterator, Reg, RegIterator, SubnodeIterator, |
Jaewan Kim | fe06c85 | 2023-10-05 23:40:06 +0900 | [diff] [blame] | 25 | }; |
Andrew Walbran | 55ad01b | 2022-12-05 17:00:40 +0000 | [diff] [blame] | 26 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 27 | use core::ffi::{c_int, c_void, CStr}; |
| 28 | use core::fmt; |
Alice Wang | 2422bdc | 2023-06-12 08:37:55 +0000 | [diff] [blame] | 29 | use core::ops::Range; |
Jaewan Kim | 5b05777 | 2023-10-19 01:02:17 +0900 | [diff] [blame] | 30 | use core::ptr; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 31 | use core::result; |
Pierre-Clément Tosi | 1bf532b | 2023-11-13 11:06:20 +0000 | [diff] [blame] | 32 | use cstr::cstr; |
Pierre-Clément Tosi | c27c427 | 2023-05-19 15:46:26 +0000 | [diff] [blame] | 33 | use zerocopy::AsBytes as _; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 34 | |
| 35 | /// Error type corresponding to libfdt error codes. |
| 36 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 37 | pub enum FdtError { |
| 38 | /// FDT_ERR_NOTFOUND |
| 39 | NotFound, |
| 40 | /// FDT_ERR_EXISTS |
| 41 | Exists, |
| 42 | /// FDT_ERR_NOSPACE |
| 43 | NoSpace, |
| 44 | /// FDT_ERR_BADOFFSET |
| 45 | BadOffset, |
| 46 | /// FDT_ERR_BADPATH |
| 47 | BadPath, |
| 48 | /// FDT_ERR_BADPHANDLE |
| 49 | BadPhandle, |
| 50 | /// FDT_ERR_BADSTATE |
| 51 | BadState, |
| 52 | /// FDT_ERR_TRUNCATED |
| 53 | Truncated, |
| 54 | /// FDT_ERR_BADMAGIC |
| 55 | BadMagic, |
| 56 | /// FDT_ERR_BADVERSION |
| 57 | BadVersion, |
| 58 | /// FDT_ERR_BADSTRUCTURE |
| 59 | BadStructure, |
| 60 | /// FDT_ERR_BADLAYOUT |
| 61 | BadLayout, |
| 62 | /// FDT_ERR_INTERNAL |
| 63 | Internal, |
| 64 | /// FDT_ERR_BADNCELLS |
| 65 | BadNCells, |
| 66 | /// FDT_ERR_BADVALUE |
| 67 | BadValue, |
| 68 | /// FDT_ERR_BADOVERLAY |
| 69 | BadOverlay, |
| 70 | /// FDT_ERR_NOPHANDLES |
| 71 | NoPhandles, |
| 72 | /// FDT_ERR_BADFLAGS |
| 73 | BadFlags, |
| 74 | /// FDT_ERR_ALIGNMENT |
| 75 | Alignment, |
| 76 | /// Unexpected error code |
| 77 | Unknown(i32), |
| 78 | } |
| 79 | |
| 80 | impl fmt::Display for FdtError { |
| 81 | /// Prints error messages from libfdt.h documentation. |
| 82 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 83 | match self { |
| 84 | Self::NotFound => write!(f, "The requested node or property does not exist"), |
| 85 | Self::Exists => write!(f, "Attempted to create an existing node or property"), |
| 86 | Self::NoSpace => write!(f, "Insufficient buffer space to contain the expanded tree"), |
| 87 | Self::BadOffset => write!(f, "Structure block offset is out-of-bounds or invalid"), |
| 88 | Self::BadPath => write!(f, "Badly formatted path"), |
| 89 | Self::BadPhandle => write!(f, "Invalid phandle length or value"), |
| 90 | Self::BadState => write!(f, "Received incomplete device tree"), |
| 91 | Self::Truncated => write!(f, "Device tree or sub-block is improperly terminated"), |
| 92 | Self::BadMagic => write!(f, "Device tree header missing its magic number"), |
| 93 | Self::BadVersion => write!(f, "Device tree has a version which can't be handled"), |
| 94 | Self::BadStructure => write!(f, "Device tree has a corrupt structure block"), |
| 95 | Self::BadLayout => write!(f, "Device tree sub-blocks in unsupported order"), |
| 96 | Self::Internal => write!(f, "libfdt has failed an internal assertion"), |
| 97 | Self::BadNCells => write!(f, "Bad format or value of #address-cells or #size-cells"), |
| 98 | Self::BadValue => write!(f, "Unexpected property value"), |
| 99 | Self::BadOverlay => write!(f, "Overlay cannot be applied"), |
| 100 | Self::NoPhandles => write!(f, "Device tree doesn't have any phandle available anymore"), |
| 101 | Self::BadFlags => write!(f, "Invalid flag or invalid combination of flags"), |
| 102 | Self::Alignment => write!(f, "Device tree base address is not 8-byte aligned"), |
| 103 | Self::Unknown(e) => write!(f, "Unknown libfdt error '{e}'"), |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /// Result type with FdtError enum. |
| 109 | pub type Result<T> = result::Result<T, FdtError>; |
| 110 | |
| 111 | fn fdt_err(val: c_int) -> Result<c_int> { |
| 112 | if val >= 0 { |
| 113 | Ok(val) |
| 114 | } else { |
| 115 | Err(match -val as _ { |
| 116 | libfdt_bindgen::FDT_ERR_NOTFOUND => FdtError::NotFound, |
| 117 | libfdt_bindgen::FDT_ERR_EXISTS => FdtError::Exists, |
| 118 | libfdt_bindgen::FDT_ERR_NOSPACE => FdtError::NoSpace, |
| 119 | libfdt_bindgen::FDT_ERR_BADOFFSET => FdtError::BadOffset, |
| 120 | libfdt_bindgen::FDT_ERR_BADPATH => FdtError::BadPath, |
| 121 | libfdt_bindgen::FDT_ERR_BADPHANDLE => FdtError::BadPhandle, |
| 122 | libfdt_bindgen::FDT_ERR_BADSTATE => FdtError::BadState, |
| 123 | libfdt_bindgen::FDT_ERR_TRUNCATED => FdtError::Truncated, |
| 124 | libfdt_bindgen::FDT_ERR_BADMAGIC => FdtError::BadMagic, |
| 125 | libfdt_bindgen::FDT_ERR_BADVERSION => FdtError::BadVersion, |
| 126 | libfdt_bindgen::FDT_ERR_BADSTRUCTURE => FdtError::BadStructure, |
| 127 | libfdt_bindgen::FDT_ERR_BADLAYOUT => FdtError::BadLayout, |
| 128 | libfdt_bindgen::FDT_ERR_INTERNAL => FdtError::Internal, |
| 129 | libfdt_bindgen::FDT_ERR_BADNCELLS => FdtError::BadNCells, |
| 130 | libfdt_bindgen::FDT_ERR_BADVALUE => FdtError::BadValue, |
| 131 | libfdt_bindgen::FDT_ERR_BADOVERLAY => FdtError::BadOverlay, |
| 132 | libfdt_bindgen::FDT_ERR_NOPHANDLES => FdtError::NoPhandles, |
| 133 | libfdt_bindgen::FDT_ERR_BADFLAGS => FdtError::BadFlags, |
| 134 | libfdt_bindgen::FDT_ERR_ALIGNMENT => FdtError::Alignment, |
| 135 | _ => FdtError::Unknown(val), |
| 136 | }) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | fn fdt_err_expect_zero(val: c_int) -> Result<()> { |
| 141 | match fdt_err(val)? { |
| 142 | 0 => Ok(()), |
| 143 | _ => Err(FdtError::Unknown(val)), |
| 144 | } |
| 145 | } |
| 146 | |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 147 | fn fdt_err_or_option(val: c_int) -> Result<Option<c_int>> { |
| 148 | match fdt_err(val) { |
| 149 | Ok(val) => Ok(Some(val)), |
| 150 | Err(FdtError::NotFound) => Ok(None), |
| 151 | Err(e) => Err(e), |
| 152 | } |
| 153 | } |
| 154 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 155 | /// Value of a #address-cells property. |
Andrew Walbran | b39e692 | 2022-12-05 17:01:20 +0000 | [diff] [blame] | 156 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 157 | enum AddrCells { |
| 158 | Single = 1, |
| 159 | Double = 2, |
Andrew Walbran | b39e692 | 2022-12-05 17:01:20 +0000 | [diff] [blame] | 160 | Triple = 3, |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 163 | impl TryFrom<usize> for AddrCells { |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 164 | type Error = FdtError; |
| 165 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 166 | fn try_from(value: usize) -> Result<Self> { |
| 167 | match value { |
| 168 | x if x == Self::Single as _ => Ok(Self::Single), |
| 169 | x if x == Self::Double as _ => Ok(Self::Double), |
| 170 | x if x == Self::Triple as _ => Ok(Self::Triple), |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 171 | _ => Err(FdtError::BadNCells), |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// Value of a #size-cells property. |
Andrew Walbran | b39e692 | 2022-12-05 17:01:20 +0000 | [diff] [blame] | 177 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 178 | enum SizeCells { |
| 179 | None = 0, |
| 180 | Single = 1, |
| 181 | Double = 2, |
| 182 | } |
| 183 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 184 | impl TryFrom<usize> for SizeCells { |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 185 | type Error = FdtError; |
| 186 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 187 | fn try_from(value: usize) -> Result<Self> { |
| 188 | match value { |
| 189 | x if x == Self::None as _ => Ok(Self::None), |
| 190 | x if x == Self::Single as _ => Ok(Self::Single), |
| 191 | x if x == Self::Double as _ => Ok(Self::Double), |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 192 | _ => Err(FdtError::BadNCells), |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 197 | /// DT property wrapper to abstract endianess changes |
| 198 | #[repr(transparent)] |
| 199 | #[derive(Debug)] |
| 200 | struct FdtPropertyStruct(libfdt_bindgen::fdt_property); |
| 201 | |
Pierre-Clément Tosi | df3037f | 2024-01-22 15:41:43 +0000 | [diff] [blame] | 202 | impl AsRef<FdtPropertyStruct> for libfdt_bindgen::fdt_property { |
| 203 | fn as_ref(&self) -> &FdtPropertyStruct { |
| 204 | let ptr = self as *const _ as *const _; |
| 205 | // SAFETY: Types have the same layout (transparent) so the valid reference remains valid. |
| 206 | unsafe { &*ptr } |
| 207 | } |
| 208 | } |
| 209 | |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 210 | impl FdtPropertyStruct { |
| 211 | fn from_offset(fdt: &Fdt, offset: c_int) -> Result<&Self> { |
| 212 | let mut len = 0; |
| 213 | let prop = |
| 214 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 215 | unsafe { libfdt_bindgen::fdt_get_property_by_offset(fdt.as_ptr(), offset, &mut len) }; |
| 216 | if prop.is_null() { |
| 217 | fdt_err(len)?; |
| 218 | return Err(FdtError::Internal); // shouldn't happen. |
| 219 | } |
| 220 | // SAFETY: prop is only returned when it points to valid libfdt_bindgen. |
Pierre-Clément Tosi | df3037f | 2024-01-22 15:41:43 +0000 | [diff] [blame] | 221 | let prop = unsafe { &*prop }; |
| 222 | Ok(prop.as_ref()) |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | fn name_offset(&self) -> c_int { |
| 226 | u32::from_be(self.0.nameoff).try_into().unwrap() |
| 227 | } |
| 228 | |
| 229 | fn data_len(&self) -> usize { |
| 230 | u32::from_be(self.0.len).try_into().unwrap() |
| 231 | } |
| 232 | |
| 233 | fn data_ptr(&self) -> *const c_void { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 234 | self.0.data.as_ptr().cast() |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | /// DT property. |
| 239 | #[derive(Clone, Copy, Debug)] |
| 240 | pub struct FdtProperty<'a> { |
| 241 | fdt: &'a Fdt, |
| 242 | offset: c_int, |
| 243 | property: &'a FdtPropertyStruct, |
| 244 | } |
| 245 | |
| 246 | impl<'a> FdtProperty<'a> { |
| 247 | fn new(fdt: &'a Fdt, offset: c_int) -> Result<Self> { |
| 248 | let property = FdtPropertyStruct::from_offset(fdt, offset)?; |
| 249 | Ok(Self { fdt, offset, property }) |
| 250 | } |
| 251 | |
| 252 | /// Returns the property name |
| 253 | pub fn name(&self) -> Result<&'a CStr> { |
| 254 | self.fdt.string(self.property.name_offset()) |
| 255 | } |
| 256 | |
| 257 | /// Returns the property value |
| 258 | pub fn value(&self) -> Result<&'a [u8]> { |
| 259 | self.fdt.get_from_ptr(self.property.data_ptr(), self.property.data_len()) |
| 260 | } |
| 261 | |
| 262 | fn next_property(&self) -> Result<Option<Self>> { |
| 263 | let ret = |
| 264 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 265 | unsafe { libfdt_bindgen::fdt_next_property_offset(self.fdt.as_ptr(), self.offset) }; |
| 266 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 267 | if let Some(offset) = fdt_err_or_option(ret)? { |
| 268 | Ok(Some(Self::new(self.fdt, offset)?)) |
| 269 | } else { |
| 270 | Ok(None) |
| 271 | } |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 275 | /// DT node. |
Alice Wang | 9d4df70 | 2023-05-25 14:14:12 +0000 | [diff] [blame] | 276 | #[derive(Clone, Copy, Debug)] |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 277 | pub struct FdtNode<'a> { |
| 278 | fdt: &'a Fdt, |
| 279 | offset: c_int, |
| 280 | } |
| 281 | |
| 282 | impl<'a> FdtNode<'a> { |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 283 | /// Returns parent node. |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 284 | pub fn parent(&self) -> Result<Self> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 285 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 286 | let ret = unsafe { libfdt_bindgen::fdt_parent_offset(self.fdt.as_ptr(), self.offset) }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 287 | let offset = fdt_err(ret)?; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 288 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 289 | Ok(Self { fdt: self.fdt, offset }) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Jaewan Kim | 5b05777 | 2023-10-19 01:02:17 +0900 | [diff] [blame] | 292 | /// Returns supernode with depth. Note that root is at depth 0. |
| 293 | pub fn supernode_at_depth(&self, depth: usize) -> Result<Self> { |
| 294 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 295 | let ret = unsafe { |
| 296 | libfdt_bindgen::fdt_supernode_atdepth_offset( |
| 297 | self.fdt.as_ptr(), |
| 298 | self.offset, |
| 299 | depth.try_into().unwrap(), |
| 300 | ptr::null_mut(), |
| 301 | ) |
| 302 | }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 303 | let offset = fdt_err(ret)?; |
Jaewan Kim | 5b05777 | 2023-10-19 01:02:17 +0900 | [diff] [blame] | 304 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 305 | Ok(Self { fdt: self.fdt, offset }) |
Jaewan Kim | 5b05777 | 2023-10-19 01:02:17 +0900 | [diff] [blame] | 306 | } |
| 307 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 308 | /// Returns the standard (deprecated) device_type <string> property. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 309 | pub fn device_type(&self) -> Result<Option<&CStr>> { |
Jaewan Kim | b635bb0 | 2023-11-01 13:00:34 +0900 | [diff] [blame] | 310 | self.getprop_str(cstr!("device_type")) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 313 | /// Returns the standard reg <prop-encoded-array> property. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 314 | pub fn reg(&self) -> Result<Option<RegIterator<'a>>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 315 | if let Some(cells) = self.getprop_cells(cstr!("reg"))? { |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 316 | let parent = self.parent()?; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 317 | |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 318 | let addr_cells = parent.address_cells()?; |
| 319 | let size_cells = parent.size_cells()?; |
| 320 | |
| 321 | Ok(Some(RegIterator::new(cells, addr_cells, size_cells))) |
| 322 | } else { |
| 323 | Ok(None) |
| 324 | } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 327 | /// Returns the standard ranges property. |
Andrew Walbran | b39e692 | 2022-12-05 17:01:20 +0000 | [diff] [blame] | 328 | pub fn ranges<A, P, S>(&self) -> Result<Option<RangesIterator<'a, A, P, S>>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 329 | if let Some(cells) = self.getprop_cells(cstr!("ranges"))? { |
Andrew Walbran | b39e692 | 2022-12-05 17:01:20 +0000 | [diff] [blame] | 330 | let parent = self.parent()?; |
| 331 | let addr_cells = self.address_cells()?; |
| 332 | let parent_addr_cells = parent.address_cells()?; |
| 333 | let size_cells = self.size_cells()?; |
| 334 | Ok(Some(RangesIterator::<A, P, S>::new( |
| 335 | cells, |
| 336 | addr_cells, |
| 337 | parent_addr_cells, |
| 338 | size_cells, |
| 339 | ))) |
| 340 | } else { |
| 341 | Ok(None) |
| 342 | } |
| 343 | } |
| 344 | |
Jaewan Kim | aa63870 | 2023-09-19 13:34:01 +0900 | [diff] [blame] | 345 | /// Returns the node name. |
| 346 | pub fn name(&self) -> Result<&'a CStr> { |
| 347 | let mut len: c_int = 0; |
| 348 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). On success, the |
| 349 | // function returns valid null terminating string and otherwise returned values are dropped. |
| 350 | let name = unsafe { libfdt_bindgen::fdt_get_name(self.fdt.as_ptr(), self.offset, &mut len) } |
| 351 | as *const c_void; |
| 352 | let len = usize::try_from(fdt_err(len)?).unwrap(); |
| 353 | let name = self.fdt.get_from_ptr(name, len + 1)?; |
| 354 | CStr::from_bytes_with_nul(name).map_err(|_| FdtError::Internal) |
| 355 | } |
| 356 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 357 | /// Returns the value of a given <string> property. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 358 | pub fn getprop_str(&self, name: &CStr) -> Result<Option<&CStr>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 359 | if let Some(bytes) = self.getprop(name)? { |
| 360 | Ok(Some(CStr::from_bytes_with_nul(bytes).map_err(|_| FdtError::BadValue)?)) |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 361 | } else { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 362 | Ok(None) |
| 363 | } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 366 | /// Returns the value of a given property as an array of cells. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 367 | pub fn getprop_cells(&self, name: &CStr) -> Result<Option<CellIterator<'a>>> { |
| 368 | if let Some(cells) = self.getprop(name)? { |
| 369 | Ok(Some(CellIterator::new(cells))) |
| 370 | } else { |
| 371 | Ok(None) |
| 372 | } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 375 | /// Returns the value of a given <u32> property. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 376 | pub fn getprop_u32(&self, name: &CStr) -> Result<Option<u32>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 377 | if let Some(bytes) = self.getprop(name)? { |
| 378 | Ok(Some(u32::from_be_bytes(bytes.try_into().map_err(|_| FdtError::BadValue)?))) |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 379 | } else { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 380 | Ok(None) |
| 381 | } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 384 | /// Returns the value of a given <u64> property. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 385 | pub fn getprop_u64(&self, name: &CStr) -> Result<Option<u64>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 386 | if let Some(bytes) = self.getprop(name)? { |
| 387 | Ok(Some(u64::from_be_bytes(bytes.try_into().map_err(|_| FdtError::BadValue)?))) |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 388 | } else { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 389 | Ok(None) |
| 390 | } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 393 | /// Returns the value of a given property. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 394 | pub fn getprop(&self, name: &CStr) -> Result<Option<&'a [u8]>> { |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 395 | if let Some((prop, len)) = Self::getprop_internal(self.fdt, self.offset, name)? { |
Jaewan Kim | aa63870 | 2023-09-19 13:34:01 +0900 | [diff] [blame] | 396 | Ok(Some(self.fdt.get_from_ptr(prop, len)?)) |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 397 | } else { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 398 | Ok(None) |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 402 | /// Returns the pointer and size of the property named `name`, in a node at offset `offset`, in |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 403 | /// a device tree `fdt`. The pointer is guaranteed to be non-null, in which case error returns. |
| 404 | fn getprop_internal( |
| 405 | fdt: &'a Fdt, |
| 406 | offset: c_int, |
| 407 | name: &CStr, |
| 408 | ) -> Result<Option<(*const c_void, usize)>> { |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 409 | let mut len: i32 = 0; |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 410 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) and the |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 411 | // function respects the passed number of characters. |
| 412 | let prop = unsafe { |
| 413 | libfdt_bindgen::fdt_getprop_namelen( |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 414 | fdt.as_ptr(), |
| 415 | offset, |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 416 | name.as_ptr(), |
| 417 | // *_namelen functions don't include the trailing nul terminator in 'len'. |
| 418 | name.to_bytes().len().try_into().map_err(|_| FdtError::BadPath)?, |
| 419 | &mut len as *mut i32, |
| 420 | ) |
| 421 | } as *const u8; |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 422 | |
| 423 | let Some(len) = fdt_err_or_option(len)? else { |
| 424 | return Ok(None); // Property was not found. |
| 425 | }; |
Jaewan Kim | aa63870 | 2023-09-19 13:34:01 +0900 | [diff] [blame] | 426 | let len = usize::try_from(len).unwrap(); |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 427 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 428 | if prop.is_null() { |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 429 | // We expected an error code in len but still received a valid value?! |
| 430 | return Err(FdtError::Internal); |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 431 | } |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 432 | Ok(Some((prop.cast::<c_void>(), len))) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 435 | /// Returns reference to the containing device tree. |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 436 | pub fn fdt(&self) -> &Fdt { |
| 437 | self.fdt |
| 438 | } |
| 439 | |
Alice Wang | 474c0ee | 2023-09-14 12:52:33 +0000 | [diff] [blame] | 440 | /// Returns the compatible node of the given name that is next after this node. |
| 441 | pub fn next_compatible(self, compatible: &CStr) -> Result<Option<Self>> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 442 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 443 | let ret = unsafe { |
| 444 | libfdt_bindgen::fdt_node_offset_by_compatible( |
| 445 | self.fdt.as_ptr(), |
| 446 | self.offset, |
| 447 | compatible.as_ptr(), |
| 448 | ) |
| 449 | }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 450 | let offset = fdt_err_or_option(ret)?; |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 451 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 452 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Alice Wang | 474c0ee | 2023-09-14 12:52:33 +0000 | [diff] [blame] | 455 | /// Returns the first range of `reg` in this node. |
| 456 | pub fn first_reg(&self) -> Result<Reg<u64>> { |
| 457 | self.reg()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound) |
| 458 | } |
| 459 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 460 | fn address_cells(&self) -> Result<AddrCells> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 461 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 462 | let ret = unsafe { libfdt_bindgen::fdt_address_cells(self.fdt.as_ptr(), self.offset) }; |
| 463 | |
| 464 | usize::try_from(ret).map_err(|_| FdtError::Internal)?.try_into() |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | fn size_cells(&self) -> Result<SizeCells> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 468 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 469 | let ret = unsafe { libfdt_bindgen::fdt_size_cells(self.fdt.as_ptr(), self.offset) }; |
| 470 | |
| 471 | usize::try_from(ret).map_err(|_| FdtError::Internal)?.try_into() |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 472 | } |
Jaewan Kim | bc828d7 | 2023-09-19 15:52:08 +0900 | [diff] [blame] | 473 | |
| 474 | /// Returns an iterator of subnodes |
Jaewan Kim | 4a34b0d | 2024-01-19 13:17:47 +0900 | [diff] [blame] | 475 | pub fn subnodes(&self) -> Result<SubnodeIterator<'a>> { |
Jaewan Kim | bc828d7 | 2023-09-19 15:52:08 +0900 | [diff] [blame] | 476 | SubnodeIterator::new(self) |
| 477 | } |
| 478 | |
| 479 | fn first_subnode(&self) -> Result<Option<Self>> { |
| 480 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 481 | let ret = unsafe { libfdt_bindgen::fdt_first_subnode(self.fdt.as_ptr(), self.offset) }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 482 | let offset = fdt_err_or_option(ret)?; |
Jaewan Kim | bc828d7 | 2023-09-19 15:52:08 +0900 | [diff] [blame] | 483 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 484 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
Jaewan Kim | bc828d7 | 2023-09-19 15:52:08 +0900 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | fn next_subnode(&self) -> Result<Option<Self>> { |
| 488 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 489 | let ret = unsafe { libfdt_bindgen::fdt_next_subnode(self.fdt.as_ptr(), self.offset) }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 490 | let offset = fdt_err_or_option(ret)?; |
Jaewan Kim | bc828d7 | 2023-09-19 15:52:08 +0900 | [diff] [blame] | 491 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 492 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
Jaewan Kim | bc828d7 | 2023-09-19 15:52:08 +0900 | [diff] [blame] | 493 | } |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 494 | |
Jaewan Kim | c9e1411 | 2023-12-04 17:05:27 +0900 | [diff] [blame] | 495 | /// Returns an iterator of descendants |
Jaewan Kim | 1eab723 | 2024-01-04 09:46:16 +0900 | [diff] [blame] | 496 | pub fn descendants(&self) -> DescendantsIterator<'a> { |
Jaewan Kim | c9e1411 | 2023-12-04 17:05:27 +0900 | [diff] [blame] | 497 | DescendantsIterator::new(self) |
| 498 | } |
| 499 | |
| 500 | fn next_node(&self, depth: usize) -> Result<Option<(Self, usize)>> { |
| 501 | let mut next_depth: c_int = depth.try_into().unwrap(); |
| 502 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 503 | let ret = unsafe { |
| 504 | libfdt_bindgen::fdt_next_node(self.fdt.as_ptr(), self.offset, &mut next_depth) |
| 505 | }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 506 | let Ok(depth) = usize::try_from(next_depth) else { |
Jaewan Kim | c9e1411 | 2023-12-04 17:05:27 +0900 | [diff] [blame] | 507 | return Ok(None); |
| 508 | }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 509 | if let Some(offset) = fdt_err_or_option(ret)? { |
| 510 | Ok(Some((Self { fdt: self.fdt, offset }, depth))) |
| 511 | } else { |
| 512 | Ok(None) |
| 513 | } |
Jaewan Kim | c9e1411 | 2023-12-04 17:05:27 +0900 | [diff] [blame] | 514 | } |
| 515 | |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 516 | /// Returns an iterator of properties |
| 517 | pub fn properties(&'a self) -> Result<PropertyIterator<'a>> { |
| 518 | PropertyIterator::new(self) |
| 519 | } |
| 520 | |
| 521 | fn first_property(&self) -> Result<Option<FdtProperty<'a>>> { |
| 522 | let ret = |
| 523 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 524 | unsafe { libfdt_bindgen::fdt_first_property_offset(self.fdt.as_ptr(), self.offset) }; |
| 525 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 526 | if let Some(offset) = fdt_err_or_option(ret)? { |
| 527 | Ok(Some(FdtProperty::new(self.fdt, offset)?)) |
| 528 | } else { |
| 529 | Ok(None) |
| 530 | } |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 531 | } |
Jaewan Kim | f34f4b8 | 2023-11-03 19:38:38 +0900 | [diff] [blame] | 532 | |
| 533 | /// Returns the phandle |
| 534 | pub fn get_phandle(&self) -> Result<Option<Phandle>> { |
| 535 | // This rewrites the fdt_get_phandle() because it doesn't return error code. |
| 536 | if let Some(prop) = self.getprop_u32(cstr!("phandle"))? { |
| 537 | Ok(Some(prop.try_into()?)) |
| 538 | } else if let Some(prop) = self.getprop_u32(cstr!("linux,phandle"))? { |
| 539 | Ok(Some(prop.try_into()?)) |
| 540 | } else { |
| 541 | Ok(None) |
| 542 | } |
| 543 | } |
Jaewan Kim | 5202601 | 2023-12-13 13:49:28 +0900 | [diff] [blame] | 544 | |
| 545 | /// Returns the subnode of the given name. The name doesn't need to be nul-terminated. |
| 546 | pub fn subnode(&self, name: &CStr) -> Result<Option<Self>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 547 | let name = name.to_bytes(); |
| 548 | let offset = self.subnode_offset(name)?; |
| 549 | |
Jaewan Kim | 5202601 | 2023-12-13 13:49:28 +0900 | [diff] [blame] | 550 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
| 551 | } |
| 552 | |
| 553 | /// Returns the subnode of the given name bytes |
| 554 | pub fn subnode_with_name_bytes(&self, name: &[u8]) -> Result<Option<Self>> { |
| 555 | let offset = self.subnode_offset(name)?; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 556 | |
Jaewan Kim | 5202601 | 2023-12-13 13:49:28 +0900 | [diff] [blame] | 557 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
| 558 | } |
| 559 | |
| 560 | fn subnode_offset(&self, name: &[u8]) -> Result<Option<c_int>> { |
| 561 | let namelen = name.len().try_into().unwrap(); |
| 562 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 563 | let ret = unsafe { |
| 564 | libfdt_bindgen::fdt_subnode_offset_namelen( |
| 565 | self.fdt.as_ptr(), |
| 566 | self.offset, |
| 567 | name.as_ptr().cast::<_>(), |
| 568 | namelen, |
| 569 | ) |
| 570 | }; |
| 571 | fdt_err_or_option(ret) |
| 572 | } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Pierre-Clément Tosi | 504b430 | 2023-10-30 12:22:50 +0000 | [diff] [blame] | 575 | impl<'a> PartialEq for FdtNode<'a> { |
| 576 | fn eq(&self, other: &Self) -> bool { |
| 577 | self.fdt.as_ptr() == other.fdt.as_ptr() && self.offset == other.offset |
| 578 | } |
| 579 | } |
| 580 | |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 581 | /// Phandle of a FDT node |
| 582 | #[repr(transparent)] |
Jaewan Kim | 55f438c | 2023-11-15 01:24:36 +0900 | [diff] [blame] | 583 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 584 | pub struct Phandle(u32); |
| 585 | |
| 586 | impl Phandle { |
Pierre-Clément Tosi | eba2779 | 2023-10-30 12:04:12 +0000 | [diff] [blame] | 587 | /// Minimum valid value for device tree phandles. |
| 588 | pub const MIN: Self = Self(1); |
| 589 | /// Maximum valid value for device tree phandles. |
| 590 | pub const MAX: Self = Self(libfdt_bindgen::FDT_MAX_PHANDLE); |
| 591 | |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 592 | /// Creates a new Phandle |
Pierre-Clément Tosi | eba2779 | 2023-10-30 12:04:12 +0000 | [diff] [blame] | 593 | pub const fn new(value: u32) -> Option<Self> { |
| 594 | if Self::MIN.0 <= value && value <= Self::MAX.0 { |
| 595 | Some(Self(value)) |
| 596 | } else { |
| 597 | None |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 598 | } |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | impl From<Phandle> for u32 { |
| 603 | fn from(phandle: Phandle) -> u32 { |
| 604 | phandle.0 |
| 605 | } |
| 606 | } |
| 607 | |
Pierre-Clément Tosi | eba2779 | 2023-10-30 12:04:12 +0000 | [diff] [blame] | 608 | impl TryFrom<u32> for Phandle { |
| 609 | type Error = FdtError; |
| 610 | |
| 611 | fn try_from(value: u32) -> Result<Self> { |
| 612 | Self::new(value).ok_or(FdtError::BadPhandle) |
| 613 | } |
| 614 | } |
| 615 | |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 616 | /// Mutable FDT node. |
Pierre-Clément Tosi | 504b430 | 2023-10-30 12:22:50 +0000 | [diff] [blame] | 617 | #[derive(Debug)] |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 618 | pub struct FdtNodeMut<'a> { |
| 619 | fdt: &'a mut Fdt, |
| 620 | offset: c_int, |
| 621 | } |
| 622 | |
| 623 | impl<'a> FdtNodeMut<'a> { |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 624 | /// Appends a property name-value (possibly empty) pair to the given node. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 625 | pub fn appendprop<T: AsRef<[u8]>>(&mut self, name: &CStr, value: &T) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 626 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 627 | let ret = unsafe { |
| 628 | libfdt_bindgen::fdt_appendprop( |
| 629 | self.fdt.as_mut_ptr(), |
| 630 | self.offset, |
| 631 | name.as_ptr(), |
| 632 | value.as_ref().as_ptr().cast::<c_void>(), |
| 633 | value.as_ref().len().try_into().map_err(|_| FdtError::BadValue)?, |
| 634 | ) |
| 635 | }; |
| 636 | |
| 637 | fdt_err_expect_zero(ret) |
| 638 | } |
| 639 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 640 | /// Appends a (address, size) pair property to the given node. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 641 | pub fn appendprop_addrrange(&mut self, name: &CStr, addr: u64, size: u64) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 642 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 643 | let ret = unsafe { |
| 644 | libfdt_bindgen::fdt_appendprop_addrrange( |
| 645 | self.fdt.as_mut_ptr(), |
| 646 | self.parent()?.offset, |
| 647 | self.offset, |
| 648 | name.as_ptr(), |
| 649 | addr, |
| 650 | size, |
| 651 | ) |
| 652 | }; |
| 653 | |
| 654 | fdt_err_expect_zero(ret) |
| 655 | } |
| 656 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 657 | /// Sets a property name-value pair to the given node. |
| 658 | /// |
| 659 | /// This may create a new prop or replace existing value. |
Jaewan Kim | ba8929b | 2023-01-13 11:13:29 +0900 | [diff] [blame] | 660 | pub fn setprop(&mut self, name: &CStr, value: &[u8]) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 661 | // SAFETY: New value size is constrained to the DT totalsize |
Jaewan Kim | ba8929b | 2023-01-13 11:13:29 +0900 | [diff] [blame] | 662 | // (validated by underlying libfdt). |
| 663 | let ret = unsafe { |
| 664 | libfdt_bindgen::fdt_setprop( |
| 665 | self.fdt.as_mut_ptr(), |
| 666 | self.offset, |
| 667 | name.as_ptr(), |
| 668 | value.as_ptr().cast::<c_void>(), |
| 669 | value.len().try_into().map_err(|_| FdtError::BadValue)?, |
| 670 | ) |
| 671 | }; |
| 672 | |
| 673 | fdt_err_expect_zero(ret) |
| 674 | } |
| 675 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 676 | /// Sets the value of the given property with the given value, and ensure that the given |
| 677 | /// value has the same length as the current value length. |
| 678 | /// |
| 679 | /// This can only be used to replace existing value. |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 680 | pub fn setprop_inplace(&mut self, name: &CStr, value: &[u8]) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 681 | // SAFETY: fdt size is not altered |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 682 | let ret = unsafe { |
| 683 | libfdt_bindgen::fdt_setprop_inplace( |
| 684 | self.fdt.as_mut_ptr(), |
| 685 | self.offset, |
| 686 | name.as_ptr(), |
| 687 | value.as_ptr().cast::<c_void>(), |
| 688 | value.len().try_into().map_err(|_| FdtError::BadValue)?, |
| 689 | ) |
| 690 | }; |
| 691 | |
| 692 | fdt_err_expect_zero(ret) |
| 693 | } |
| 694 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 695 | /// Sets the value of the given (address, size) pair property with the given value, and |
| 696 | /// ensure that the given value has the same length as the current value length. |
| 697 | /// |
| 698 | /// This can only be used to replace existing value. |
Pierre-Clément Tosi | c27c427 | 2023-05-19 15:46:26 +0000 | [diff] [blame] | 699 | pub fn setprop_addrrange_inplace(&mut self, name: &CStr, addr: u64, size: u64) -> Result<()> { |
| 700 | let pair = [addr.to_be(), size.to_be()]; |
| 701 | self.setprop_inplace(name, pair.as_bytes()) |
| 702 | } |
| 703 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 704 | /// Sets a flag-like empty property. |
| 705 | /// |
| 706 | /// This may create a new prop or replace existing value. |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 707 | pub fn setprop_empty(&mut self, name: &CStr) -> Result<()> { |
| 708 | self.setprop(name, &[]) |
| 709 | } |
| 710 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 711 | /// Deletes the given property. |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 712 | pub fn delprop(&mut self, name: &CStr) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 713 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) when the |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 714 | // library locates the node's property. Removing the property may shift the offsets of |
| 715 | // other nodes and properties but the borrow checker should prevent this function from |
| 716 | // being called when FdtNode instances are in use. |
| 717 | let ret = unsafe { |
| 718 | libfdt_bindgen::fdt_delprop(self.fdt.as_mut_ptr(), self.offset, name.as_ptr()) |
| 719 | }; |
| 720 | |
| 721 | fdt_err_expect_zero(ret) |
| 722 | } |
| 723 | |
Jaewan Kim | 4ae0e71 | 2023-10-19 14:16:17 +0900 | [diff] [blame] | 724 | /// Deletes the given property effectively from DT, by setting it with FDT_NOP. |
Pierre-Clément Tosi | be3a97b | 2023-05-19 14:56:23 +0000 | [diff] [blame] | 725 | pub fn nop_property(&mut self, name: &CStr) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 726 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) when the |
Pierre-Clément Tosi | be3a97b | 2023-05-19 14:56:23 +0000 | [diff] [blame] | 727 | // library locates the node's property. |
| 728 | let ret = unsafe { |
| 729 | libfdt_bindgen::fdt_nop_property(self.fdt.as_mut_ptr(), self.offset, name.as_ptr()) |
| 730 | }; |
| 731 | |
| 732 | fdt_err_expect_zero(ret) |
| 733 | } |
| 734 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 735 | /// Trims the size of the given property to new_size. |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 736 | pub fn trimprop(&mut self, name: &CStr, new_size: usize) -> Result<()> { |
| 737 | let (prop, len) = |
| 738 | FdtNode::getprop_internal(self.fdt, self.offset, name)?.ok_or(FdtError::NotFound)?; |
| 739 | if len == new_size { |
| 740 | return Ok(()); |
| 741 | } |
| 742 | if new_size > len { |
| 743 | return Err(FdtError::NoSpace); |
| 744 | } |
| 745 | |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 746 | // SAFETY: new_size is smaller than the old size |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 747 | let ret = unsafe { |
| 748 | libfdt_bindgen::fdt_setprop( |
| 749 | self.fdt.as_mut_ptr(), |
| 750 | self.offset, |
| 751 | name.as_ptr(), |
| 752 | prop.cast::<c_void>(), |
| 753 | new_size.try_into().map_err(|_| FdtError::BadValue)?, |
| 754 | ) |
| 755 | }; |
| 756 | |
| 757 | fdt_err_expect_zero(ret) |
| 758 | } |
| 759 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 760 | /// Returns reference to the containing device tree. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 761 | pub fn fdt(&mut self) -> &mut Fdt { |
| 762 | self.fdt |
| 763 | } |
| 764 | |
Jaewan Kim | f72f4f2 | 2023-11-03 19:21:34 +0900 | [diff] [blame] | 765 | /// Returns immutable FdtNode of this node. |
| 766 | pub fn as_node(&self) -> FdtNode { |
| 767 | FdtNode { fdt: self.fdt, offset: self.offset } |
| 768 | } |
| 769 | |
Jaewan Kim | e636342 | 2024-01-19 14:00:00 +0900 | [diff] [blame] | 770 | /// Adds new subnodes to the given node. |
| 771 | pub fn add_subnodes(&mut self, names: &[&CStr]) -> Result<()> { |
| 772 | for name in names { |
| 773 | self.add_subnode_offset(name.to_bytes())?; |
| 774 | } |
| 775 | Ok(()) |
| 776 | } |
| 777 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 778 | /// Adds a new subnode to the given node and return it as a FdtNodeMut on success. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 779 | pub fn add_subnode(&'a mut self, name: &CStr) -> Result<Self> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 780 | let name = name.to_bytes(); |
| 781 | let offset = self.add_subnode_offset(name)?; |
| 782 | |
Jaewan Kim | 5ab1358 | 2023-10-20 20:56:27 +0900 | [diff] [blame] | 783 | Ok(Self { fdt: self.fdt, offset }) |
| 784 | } |
| 785 | |
| 786 | /// Adds a new subnode to the given node with name and namelen, and returns it as a FdtNodeMut |
| 787 | /// on success. |
| 788 | pub fn add_subnode_with_namelen(&'a mut self, name: &CStr, namelen: usize) -> Result<Self> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 789 | let name = &name.to_bytes()[..namelen]; |
| 790 | let offset = self.add_subnode_offset(name)?; |
| 791 | |
Jaewan Kim | 5ab1358 | 2023-10-20 20:56:27 +0900 | [diff] [blame] | 792 | Ok(Self { fdt: self.fdt, offset }) |
| 793 | } |
| 794 | |
| 795 | fn add_subnode_offset(&mut self, name: &[u8]) -> Result<c_int> { |
| 796 | let namelen = name.len().try_into().unwrap(); |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 797 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 798 | let ret = unsafe { |
Jaewan Kim | 5ab1358 | 2023-10-20 20:56:27 +0900 | [diff] [blame] | 799 | libfdt_bindgen::fdt_add_subnode_namelen( |
| 800 | self.fdt.as_mut_ptr(), |
| 801 | self.offset, |
| 802 | name.as_ptr().cast::<_>(), |
| 803 | namelen, |
| 804 | ) |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 805 | }; |
Jaewan Kim | 5ab1358 | 2023-10-20 20:56:27 +0900 | [diff] [blame] | 806 | fdt_err(ret) |
| 807 | } |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 808 | |
Jaewan Kim | 5f1a603 | 2023-12-18 15:17:58 +0900 | [diff] [blame] | 809 | /// Returns the first subnode of this |
| 810 | pub fn first_subnode(&'a mut self) -> Result<Option<Self>> { |
| 811 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 812 | let ret = unsafe { libfdt_bindgen::fdt_first_subnode(self.fdt.as_ptr(), self.offset) }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 813 | let offset = fdt_err_or_option(ret)?; |
Jaewan Kim | 5f1a603 | 2023-12-18 15:17:58 +0900 | [diff] [blame] | 814 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 815 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
Jaewan Kim | 5f1a603 | 2023-12-18 15:17:58 +0900 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /// Returns the next subnode that shares the same parent with this |
| 819 | pub fn next_subnode(self) -> Result<Option<Self>> { |
| 820 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 821 | let ret = unsafe { libfdt_bindgen::fdt_next_subnode(self.fdt.as_ptr(), self.offset) }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 822 | let offset = fdt_err_or_option(ret)?; |
Jaewan Kim | 5f1a603 | 2023-12-18 15:17:58 +0900 | [diff] [blame] | 823 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 824 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
Jaewan Kim | 5f1a603 | 2023-12-18 15:17:58 +0900 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /// Deletes the current node and returns the next subnode |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 828 | pub fn delete_and_next_subnode(self) -> Result<Option<Self>> { |
Jaewan Kim | 5f1a603 | 2023-12-18 15:17:58 +0900 | [diff] [blame] | 829 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 830 | let ret = unsafe { libfdt_bindgen::fdt_next_subnode(self.fdt.as_ptr(), self.offset) }; |
| 831 | |
| 832 | let next_offset = fdt_err_or_option(ret)?; |
| 833 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 834 | self.delete_and_next(next_offset) |
Jaewan Kim | 5f1a603 | 2023-12-18 15:17:58 +0900 | [diff] [blame] | 835 | } |
| 836 | |
Jaewan Kim | 28a13ea | 2024-01-04 09:22:40 +0900 | [diff] [blame] | 837 | fn next_node_offset(&self, depth: usize) -> Result<Option<(c_int, usize)>> { |
| 838 | let mut next_depth: c_int = depth.try_into().or(Err(FdtError::BadValue))?; |
| 839 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 840 | let ret = unsafe { |
| 841 | libfdt_bindgen::fdt_next_node(self.fdt.as_ptr(), self.offset, &mut next_depth) |
| 842 | }; |
| 843 | let Ok(next_depth) = usize::try_from(next_depth) else { |
| 844 | return Ok(None); |
| 845 | }; |
| 846 | Ok(fdt_err_or_option(ret)?.map(|offset| (offset, next_depth))) |
| 847 | } |
| 848 | |
| 849 | /// Returns the next node |
| 850 | pub fn next_node(self, depth: usize) -> Result<Option<(Self, usize)>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 851 | let next = self.next_node_offset(depth)?; |
| 852 | |
| 853 | Ok(next.map(|(offset, depth)| (Self { fdt: self.fdt, offset }, depth))) |
Jaewan Kim | 28a13ea | 2024-01-04 09:22:40 +0900 | [diff] [blame] | 854 | } |
| 855 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 856 | fn next_node_skip_subnodes(&mut self, depth: usize) -> Result<Option<(c_int, usize)>> { |
Jaewan Kim | 28a13ea | 2024-01-04 09:22:40 +0900 | [diff] [blame] | 857 | let mut iter = self.next_node_offset(depth)?; |
| 858 | while let Some((descendant_offset, descendant_depth)) = iter { |
| 859 | if descendant_depth <= depth { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 860 | return Ok(Some((descendant_offset, descendant_depth))); |
Jaewan Kim | 28a13ea | 2024-01-04 09:22:40 +0900 | [diff] [blame] | 861 | } |
| 862 | let descendant = FdtNodeMut { fdt: self.fdt, offset: descendant_offset }; |
| 863 | iter = descendant.next_node_offset(descendant_depth)?; |
| 864 | } |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 865 | |
| 866 | Ok(None) |
| 867 | } |
| 868 | |
| 869 | /// Deletes this and returns the next node |
| 870 | pub fn delete_and_next_node(mut self, depth: usize) -> Result<Option<(Self, usize)>> { |
| 871 | let next_node = self.next_node_skip_subnodes(depth)?; |
| 872 | if let Some((offset, depth)) = next_node { |
| 873 | let next_node = self.delete_and_next(Some(offset))?.unwrap(); |
| 874 | Ok(Some((next_node, depth))) |
| 875 | } else { |
| 876 | Ok(None) |
| 877 | } |
Jaewan Kim | 28a13ea | 2024-01-04 09:22:40 +0900 | [diff] [blame] | 878 | } |
| 879 | |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 880 | fn parent(&'a self) -> Result<FdtNode<'a>> { |
Pierre-Clément Tosi | df3037f | 2024-01-22 15:41:43 +0000 | [diff] [blame] | 881 | self.as_node().parent() |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 882 | } |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 883 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 884 | /// Returns the compatible node of the given name that is next after this node. |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 885 | pub fn next_compatible(self, compatible: &CStr) -> Result<Option<Self>> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 886 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 887 | let ret = unsafe { |
| 888 | libfdt_bindgen::fdt_node_offset_by_compatible( |
| 889 | self.fdt.as_ptr(), |
| 890 | self.offset, |
| 891 | compatible.as_ptr(), |
| 892 | ) |
| 893 | }; |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 894 | let offset = fdt_err_or_option(ret)?; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 895 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 896 | Ok(offset.map(|offset| Self { fdt: self.fdt, offset })) |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 897 | } |
| 898 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 899 | /// Deletes the node effectively by overwriting this node and its subtree with nop tags. |
| 900 | /// Returns the next compatible node of the given name. |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 901 | // Side note: without this, filterint out excessive compatible nodes from the DT is impossible. |
| 902 | // The reason is that libfdt ensures that the node from where the search for the next |
| 903 | // compatible node is started is always a valid one -- except for the special case of offset = |
| 904 | // -1 which is to find the first compatible node. So, we can't delete a node and then find the |
| 905 | // next compatible node from it. |
| 906 | // |
| 907 | // We can't do in the opposite direction either. If we call next_compatible to find the next |
| 908 | // node, and delete the current node, the Rust borrow checker kicks in. The next node has a |
| 909 | // mutable reference to DT, so we can't use current node (which also has a mutable reference to |
| 910 | // DT). |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 911 | pub fn delete_and_next_compatible(self, compatible: &CStr) -> Result<Option<Self>> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 912 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 913 | let ret = unsafe { |
| 914 | libfdt_bindgen::fdt_node_offset_by_compatible( |
| 915 | self.fdt.as_ptr(), |
| 916 | self.offset, |
| 917 | compatible.as_ptr(), |
| 918 | ) |
| 919 | }; |
| 920 | let next_offset = fdt_err_or_option(ret)?; |
| 921 | |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 922 | self.delete_and_next(next_offset) |
| 923 | } |
| 924 | |
| 925 | fn delete_and_next(mut self, next_offset: Option<c_int>) -> Result<Option<Self>> { |
Jaewan Kim | 4ae0e71 | 2023-10-19 14:16:17 +0900 | [diff] [blame] | 926 | if Some(self.offset) == next_offset { |
| 927 | return Err(FdtError::Internal); |
| 928 | } |
| 929 | |
| 930 | // SAFETY: nop_self() only touches bytes of the self and its properties and subnodes, and |
| 931 | // doesn't alter any other blob in the tree. self.fdt and next_offset would remain valid. |
| 932 | unsafe { self.nop_self()? }; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 933 | |
| 934 | Ok(next_offset.map(|offset| Self { fdt: self.fdt, offset })) |
| 935 | } |
Jaewan Kim | 4ae0e71 | 2023-10-19 14:16:17 +0900 | [diff] [blame] | 936 | |
| 937 | /// Deletes this node effectively from DT, by setting it with FDT_NOP |
| 938 | pub fn nop(mut self) -> Result<()> { |
| 939 | // SAFETY: This consumes self, so invalid node wouldn't be used any further |
| 940 | unsafe { self.nop_self() } |
| 941 | } |
| 942 | |
| 943 | /// Deletes this node effectively from DT, by setting it with FDT_NOP. |
| 944 | /// This only changes bytes of the node and its properties and subnodes, and doesn't alter or |
| 945 | /// move any other part of the tree. |
| 946 | /// SAFETY: This node is no longer valid. |
| 947 | unsafe fn nop_self(&mut self) -> Result<()> { |
| 948 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). |
| 949 | let ret = unsafe { libfdt_bindgen::fdt_nop_node(self.fdt.as_mut_ptr(), self.offset) }; |
| 950 | |
| 951 | fdt_err_expect_zero(ret) |
| 952 | } |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 955 | /// Wrapper around low-level libfdt functions. |
Alice Wang | 9d4df70 | 2023-05-25 14:14:12 +0000 | [diff] [blame] | 956 | #[derive(Debug)] |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 957 | #[repr(transparent)] |
| 958 | pub struct Fdt { |
Pierre-Clément Tosi | ef2030e | 2022-11-28 11:21:20 +0000 | [diff] [blame] | 959 | buffer: [u8], |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | impl Fdt { |
| 963 | /// Wraps a slice containing a Flattened Device Tree. |
| 964 | /// |
| 965 | /// Fails if the FDT does not pass validation. |
| 966 | pub fn from_slice(fdt: &[u8]) -> Result<&Self> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 967 | // SAFETY: The FDT will be validated before it is returned. |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 968 | let fdt = unsafe { Self::unchecked_from_slice(fdt) }; |
| 969 | fdt.check_full()?; |
| 970 | Ok(fdt) |
| 971 | } |
| 972 | |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 973 | /// Wraps a mutable slice containing a Flattened Device Tree. |
| 974 | /// |
| 975 | /// Fails if the FDT does not pass validation. |
| 976 | pub fn from_mut_slice(fdt: &mut [u8]) -> Result<&mut Self> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 977 | // SAFETY: The FDT will be validated before it is returned. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 978 | let fdt = unsafe { Self::unchecked_from_mut_slice(fdt) }; |
| 979 | fdt.check_full()?; |
| 980 | Ok(fdt) |
| 981 | } |
| 982 | |
Jaewan Kim | 4cf20aa | 2023-04-03 10:25:38 +0900 | [diff] [blame] | 983 | /// Creates an empty Flattened Device Tree with a mutable slice. |
| 984 | pub fn create_empty_tree(fdt: &mut [u8]) -> Result<&mut Self> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 985 | // SAFETY: fdt_create_empty_tree() only write within the specified length, |
Jaewan Kim | 4cf20aa | 2023-04-03 10:25:38 +0900 | [diff] [blame] | 986 | // and returns error if buffer was insufficient. |
| 987 | // There will be no memory write outside of the given fdt. |
| 988 | let ret = unsafe { |
| 989 | libfdt_bindgen::fdt_create_empty_tree( |
| 990 | fdt.as_mut_ptr().cast::<c_void>(), |
| 991 | fdt.len() as i32, |
| 992 | ) |
| 993 | }; |
| 994 | fdt_err_expect_zero(ret)?; |
| 995 | |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 996 | // SAFETY: The FDT will be validated before it is returned. |
Jaewan Kim | 4cf20aa | 2023-04-03 10:25:38 +0900 | [diff] [blame] | 997 | let fdt = unsafe { Self::unchecked_from_mut_slice(fdt) }; |
| 998 | fdt.check_full()?; |
| 999 | |
| 1000 | Ok(fdt) |
| 1001 | } |
| 1002 | |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1003 | /// Wraps a slice containing a Flattened Device Tree. |
| 1004 | /// |
| 1005 | /// # Safety |
| 1006 | /// |
Pierre-Clément Tosi | df3037f | 2024-01-22 15:41:43 +0000 | [diff] [blame] | 1007 | /// It is undefined to call this function on a slice that does not contain a valid device tree. |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1008 | pub unsafe fn unchecked_from_slice(fdt: &[u8]) -> &Self { |
Pierre-Clément Tosi | df3037f | 2024-01-22 15:41:43 +0000 | [diff] [blame] | 1009 | let self_ptr = fdt as *const _ as *const _; |
| 1010 | // SAFETY: The pointer is non-null, dereferenceable, and points to allocated memory. |
| 1011 | unsafe { &*self_ptr } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1014 | /// Wraps a mutable slice containing a Flattened Device Tree. |
| 1015 | /// |
| 1016 | /// # Safety |
| 1017 | /// |
Pierre-Clément Tosi | df3037f | 2024-01-22 15:41:43 +0000 | [diff] [blame] | 1018 | /// It is undefined to call this function on a slice that does not contain a valid device tree. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1019 | pub unsafe fn unchecked_from_mut_slice(fdt: &mut [u8]) -> &mut Self { |
Pierre-Clément Tosi | df3037f | 2024-01-22 15:41:43 +0000 | [diff] [blame] | 1020 | let self_mut_ptr = fdt as *mut _ as *mut _; |
| 1021 | // SAFETY: The pointer is non-null, dereferenceable, and points to allocated memory. |
| 1022 | unsafe { &mut *self_mut_ptr } |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Pierre-Clément Tosi | ce0b36d | 2024-01-26 10:50:05 +0000 | [diff] [blame] | 1025 | /// Updates this FDT from another FDT. |
| 1026 | pub fn clone_from(&mut self, other: &Self) -> Result<()> { |
| 1027 | let new_len = other.buffer.len(); |
| 1028 | if self.buffer.len() < new_len { |
| 1029 | return Err(FdtError::NoSpace); |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame] | 1030 | } |
Pierre-Clément Tosi | ce0b36d | 2024-01-26 10:50:05 +0000 | [diff] [blame] | 1031 | |
| 1032 | let zeroed_len = self.totalsize().checked_sub(new_len); |
| 1033 | let (cloned, zeroed) = self.buffer.split_at_mut(new_len); |
| 1034 | |
| 1035 | cloned.clone_from_slice(&other.buffer); |
| 1036 | if let Some(len) = zeroed_len { |
| 1037 | zeroed[..len].fill(0); |
| 1038 | } |
| 1039 | |
| 1040 | Ok(()) |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame] | 1041 | } |
| 1042 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1043 | /// Unpacks the DT to cover the whole slice it is contained in. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1044 | pub fn unpack(&mut self) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 1045 | // SAFETY: "Opens" the DT in-place (supported use-case) by updating its header and |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1046 | // internal structures to make use of the whole self.fdt slice but performs no accesses |
| 1047 | // outside of it and leaves the DT in a state that will be detected by other functions. |
| 1048 | let ret = unsafe { |
| 1049 | libfdt_bindgen::fdt_open_into( |
| 1050 | self.as_ptr(), |
| 1051 | self.as_mut_ptr(), |
| 1052 | self.capacity().try_into().map_err(|_| FdtError::Internal)?, |
| 1053 | ) |
| 1054 | }; |
| 1055 | fdt_err_expect_zero(ret) |
| 1056 | } |
| 1057 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1058 | /// Packs the DT to take a minimum amount of memory. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1059 | /// |
| 1060 | /// Doesn't shrink the underlying memory slice. |
| 1061 | pub fn pack(&mut self) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 1062 | // SAFETY: "Closes" the DT in-place by updating its header and relocating its structs. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1063 | let ret = unsafe { libfdt_bindgen::fdt_pack(self.as_mut_ptr()) }; |
| 1064 | fdt_err_expect_zero(ret) |
| 1065 | } |
| 1066 | |
Pierre-Clément Tosi | 90e1935 | 2022-11-21 17:11:48 +0000 | [diff] [blame] | 1067 | /// Applies a DT overlay on the base DT. |
| 1068 | /// |
| 1069 | /// # Safety |
| 1070 | /// |
| 1071 | /// On failure, the library corrupts the DT and overlay so both must be discarded. |
| 1072 | pub unsafe fn apply_overlay<'a>(&'a mut self, overlay: &'a mut Fdt) -> Result<&'a mut Self> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 1073 | let ret = |
| 1074 | // SAFETY: Both pointers are valid because they come from references, and fdt_overlay_apply |
| 1075 | // doesn't keep them after it returns. It may corrupt their contents if there is an error, |
| 1076 | // but that's our caller's responsibility. |
| 1077 | unsafe { libfdt_bindgen::fdt_overlay_apply(self.as_mut_ptr(), overlay.as_mut_ptr()) }; |
| 1078 | fdt_err_expect_zero(ret)?; |
Pierre-Clément Tosi | 90e1935 | 2022-11-21 17:11:48 +0000 | [diff] [blame] | 1079 | Ok(self) |
| 1080 | } |
| 1081 | |
Alice Wang | 2422bdc | 2023-06-12 08:37:55 +0000 | [diff] [blame] | 1082 | /// Returns an iterator of memory banks specified the "/memory" node. |
| 1083 | /// Throws an error when the "/memory" is not found in the device tree. |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1084 | /// |
| 1085 | /// NOTE: This does not support individual "/memory@XXXX" banks. |
Alice Wang | 2422bdc | 2023-06-12 08:37:55 +0000 | [diff] [blame] | 1086 | pub fn memory(&self) -> Result<MemRegIterator> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 1087 | let node = self.node(cstr!("/memory"))?.ok_or(FdtError::NotFound)?; |
| 1088 | if node.device_type()? != Some(cstr!("memory")) { |
Alice Wang | 2422bdc | 2023-06-12 08:37:55 +0000 | [diff] [blame] | 1089 | return Err(FdtError::BadValue); |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 1090 | } |
Alice Wang | 2422bdc | 2023-06-12 08:37:55 +0000 | [diff] [blame] | 1091 | node.reg()?.ok_or(FdtError::BadValue).map(MemRegIterator::new) |
| 1092 | } |
| 1093 | |
| 1094 | /// Returns the first memory range in the `/memory` node. |
| 1095 | pub fn first_memory_range(&self) -> Result<Range<usize>> { |
| 1096 | self.memory()?.next().ok_or(FdtError::NotFound) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1099 | /// Returns the standard /chosen node. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 1100 | pub fn chosen(&self) -> Result<Option<FdtNode>> { |
Jaewan Kim | b635bb0 | 2023-11-01 13:00:34 +0900 | [diff] [blame] | 1101 | self.node(cstr!("/chosen")) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1104 | /// Returns the standard /chosen node as mutable. |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 1105 | pub fn chosen_mut(&mut self) -> Result<Option<FdtNodeMut>> { |
Jaewan Kim | b635bb0 | 2023-11-01 13:00:34 +0900 | [diff] [blame] | 1106 | self.node_mut(cstr!("/chosen")) |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1109 | /// Returns the root node of the tree. |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 1110 | pub fn root(&self) -> Result<FdtNode> { |
Jaewan Kim | b635bb0 | 2023-11-01 13:00:34 +0900 | [diff] [blame] | 1111 | self.node(cstr!("/"))?.ok_or(FdtError::Internal) |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Jaewan Kim | f163d76 | 2023-11-01 13:12:50 +0900 | [diff] [blame] | 1114 | /// Returns the standard /__symbols__ node. |
| 1115 | pub fn symbols(&self) -> Result<Option<FdtNode>> { |
| 1116 | self.node(cstr!("/__symbols__")) |
| 1117 | } |
| 1118 | |
| 1119 | /// Returns the standard /__symbols__ node as mutable |
| 1120 | pub fn symbols_mut(&mut self) -> Result<Option<FdtNodeMut>> { |
| 1121 | self.node_mut(cstr!("/__symbols__")) |
| 1122 | } |
| 1123 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1124 | /// Returns a tree node by its full path. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 1125 | pub fn node(&self, path: &CStr) -> Result<Option<FdtNode>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 1126 | let offset = self.path_offset(path.to_bytes())?; |
| 1127 | |
| 1128 | Ok(offset.map(|offset| FdtNode { fdt: self, offset })) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Pierre-Clément Tosi | 41c158e | 2022-11-21 19:16:25 +0000 | [diff] [blame] | 1131 | /// Iterate over nodes with a given compatible string. |
| 1132 | pub fn compatible_nodes<'a>(&'a self, compatible: &'a CStr) -> Result<CompatibleIterator<'a>> { |
| 1133 | CompatibleIterator::new(self, compatible) |
| 1134 | } |
| 1135 | |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 1136 | /// Returns max phandle in the tree. |
| 1137 | pub fn max_phandle(&self) -> Result<Phandle> { |
| 1138 | let mut phandle: u32 = 0; |
| 1139 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 1140 | let ret = unsafe { libfdt_bindgen::fdt_find_max_phandle(self.as_ptr(), &mut phandle) }; |
| 1141 | |
| 1142 | fdt_err_expect_zero(ret)?; |
Pierre-Clément Tosi | eba2779 | 2023-10-30 12:04:12 +0000 | [diff] [blame] | 1143 | phandle.try_into() |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /// Returns a node with the phandle |
| 1147 | pub fn node_with_phandle(&self, phandle: Phandle) -> Result<Option<FdtNode>> { |
Jaewan Kim | c63246d | 2023-11-09 15:41:01 +0900 | [diff] [blame] | 1148 | let offset = self.node_offset_with_phandle(phandle)?; |
| 1149 | Ok(offset.map(|offset| FdtNode { fdt: self, offset })) |
| 1150 | } |
| 1151 | |
| 1152 | /// Returns a mutable node with the phandle |
| 1153 | pub fn node_mut_with_phandle(&mut self, phandle: Phandle) -> Result<Option<FdtNodeMut>> { |
| 1154 | let offset = self.node_offset_with_phandle(phandle)?; |
| 1155 | Ok(offset.map(|offset| FdtNodeMut { fdt: self, offset })) |
| 1156 | } |
| 1157 | |
| 1158 | fn node_offset_with_phandle(&self, phandle: Phandle) -> Result<Option<c_int>> { |
| 1159 | // SAFETY: Accesses are constrained to the DT totalsize. |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 1160 | let ret = unsafe { libfdt_bindgen::fdt_node_offset_by_phandle(self.as_ptr(), phandle.0) }; |
Jaewan Kim | c63246d | 2023-11-09 15:41:01 +0900 | [diff] [blame] | 1161 | fdt_err_or_option(ret) |
Jaewan Kim | 17ba7a3 | 2023-10-19 13:25:15 +0900 | [diff] [blame] | 1162 | } |
| 1163 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1164 | /// Returns the mutable root node of the tree. |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1165 | pub fn root_mut(&mut self) -> Result<FdtNodeMut> { |
Jaewan Kim | b635bb0 | 2023-11-01 13:00:34 +0900 | [diff] [blame] | 1166 | self.node_mut(cstr!("/"))?.ok_or(FdtError::Internal) |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1169 | /// Returns a mutable tree node by its full path. |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 1170 | pub fn node_mut(&mut self, path: &CStr) -> Result<Option<FdtNodeMut>> { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 1171 | let offset = self.path_offset(path.to_bytes())?; |
| 1172 | |
| 1173 | Ok(offset.map(|offset| FdtNodeMut { fdt: self, offset })) |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1176 | /// Returns the device tree as a slice (may be smaller than the containing buffer). |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 1177 | pub fn as_slice(&self) -> &[u8] { |
| 1178 | &self.buffer[..self.totalsize()] |
| 1179 | } |
| 1180 | |
Jaewan Kim | bab4259 | 2023-10-13 15:47:19 +0900 | [diff] [blame] | 1181 | fn path_offset(&self, path: &[u8]) -> Result<Option<c_int>> { |
| 1182 | let len = path.len().try_into().map_err(|_| FdtError::BadPath)?; |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 1183 | // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor) and the |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1184 | // function respects the passed number of characters. |
| 1185 | let ret = unsafe { |
| 1186 | // *_namelen functions don't include the trailing nul terminator in 'len'. |
Jaewan Kim | bab4259 | 2023-10-13 15:47:19 +0900 | [diff] [blame] | 1187 | libfdt_bindgen::fdt_path_offset_namelen(self.as_ptr(), path.as_ptr().cast::<_>(), len) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1188 | }; |
| 1189 | |
Pierre-Clément Tosi | b244d93 | 2022-11-24 16:45:53 +0000 | [diff] [blame] | 1190 | fdt_err_or_option(ret) |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | fn check_full(&self) -> Result<()> { |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 1194 | // SAFETY: Only performs read accesses within the limits of the slice. If successful, this |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1195 | // call guarantees to other unsafe calls that the header contains a valid totalsize (w.r.t. |
| 1196 | // 'len' i.e. the self.fdt slice) that those C functions can use to perform bounds |
| 1197 | // checking. The library doesn't maintain an internal state (such as pointers) between |
| 1198 | // calls as it expects the client code to keep track of the objects (DT, nodes, ...). |
Pierre-Clément Tosi | 02017da | 2023-09-26 17:57:04 +0100 | [diff] [blame] | 1199 | let ret = unsafe { libfdt_bindgen::fdt_check_full(self.as_ptr(), self.capacity()) }; |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1200 | fdt_err_expect_zero(ret) |
| 1201 | } |
| 1202 | |
Jaewan Kim | aa63870 | 2023-09-19 13:34:01 +0900 | [diff] [blame] | 1203 | fn get_from_ptr(&self, ptr: *const c_void, len: usize) -> Result<&[u8]> { |
| 1204 | let ptr = ptr as usize; |
| 1205 | let offset = ptr.checked_sub(self.as_ptr() as usize).ok_or(FdtError::Internal)?; |
| 1206 | self.buffer.get(offset..(offset + len)).ok_or(FdtError::Internal) |
| 1207 | } |
| 1208 | |
Jaewan Kim | 72d1090 | 2023-10-12 21:59:26 +0900 | [diff] [blame] | 1209 | fn string(&self, offset: c_int) -> Result<&CStr> { |
| 1210 | // SAFETY: Accesses (read-only) are constrained to the DT totalsize. |
| 1211 | let res = unsafe { libfdt_bindgen::fdt_string(self.as_ptr(), offset) }; |
| 1212 | if res.is_null() { |
| 1213 | return Err(FdtError::Internal); |
| 1214 | } |
| 1215 | |
| 1216 | // SAFETY: Non-null return from fdt_string() is valid null-terminating string within FDT. |
| 1217 | Ok(unsafe { CStr::from_ptr(res) }) |
| 1218 | } |
| 1219 | |
Jaewan Kim | b3dcfc2 | 2023-09-20 10:20:52 +0900 | [diff] [blame] | 1220 | /// Returns a shared pointer to the device tree. |
Pierre-Clément Tosi | 8036b4f | 2023-02-17 10:31:31 +0000 | [diff] [blame] | 1221 | pub fn as_ptr(&self) -> *const c_void { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 1222 | self.buffer.as_ptr().cast() |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1223 | } |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1224 | |
| 1225 | fn as_mut_ptr(&mut self) -> *mut c_void { |
Pierre-Clément Tosi | 0dcc75e | 2023-05-02 13:43:55 +0000 | [diff] [blame] | 1226 | self.buffer.as_mut_ptr().cast::<_>() |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | fn capacity(&self) -> usize { |
Pierre-Clément Tosi | ef2030e | 2022-11-28 11:21:20 +0000 | [diff] [blame] | 1230 | self.buffer.len() |
Pierre-Clément Tosi | 1b0d890 | 2022-11-21 18:16:59 +0000 | [diff] [blame] | 1231 | } |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 1232 | |
| 1233 | fn header(&self) -> &libfdt_bindgen::fdt_header { |
Pierre-Clément Tosi | cb92b51 | 2024-01-22 15:55:25 +0000 | [diff] [blame^] | 1234 | let p = self.as_ptr().cast(); |
Andrew Walbran | 84b9a23 | 2023-07-05 14:01:40 +0000 | [diff] [blame] | 1235 | // SAFETY: A valid FDT (verified by constructor) must contain a valid fdt_header. |
Pierre-Clément Tosi | 0dcc75e | 2023-05-02 13:43:55 +0000 | [diff] [blame] | 1236 | unsafe { &*p } |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | fn totalsize(&self) -> usize { |
| 1240 | u32::from_be(self.header().totalsize) as usize |
| 1241 | } |
David Brazdil | 1baa9a9 | 2022-06-28 14:47:50 +0100 | [diff] [blame] | 1242 | } |