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