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