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