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