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