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