|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +use core::ops::Deref; |
| 10 | + |
| 11 | +use zerocopy::big_endian; |
| 12 | + |
| 13 | +use crate::error::FdtError; |
| 14 | +use crate::fdt::{Fdt, FdtNode}; |
| 15 | + |
| 16 | +impl Fdt<'_> { |
| 17 | + /// Returns the /memory node. |
| 18 | + /// |
| 19 | + /// This should always be included in a valid device tree. |
| 20 | + /// |
| 21 | + /// # Errors |
| 22 | + /// |
| 23 | + /// Returns a parse error if there was a problem reading the FDT structure |
| 24 | + /// to find the node, or `FdtError::MemoryMissing` if the memory node is |
| 25 | + /// missing. |
| 26 | + pub fn memory(&self) -> Result<Memory<'_>, FdtError> { |
| 27 | + let node = self.find_node("/memory")?.ok_or(FdtError::MemoryMissing)?; |
| 28 | + Ok(Memory { node }) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Typed wrapper for a "/memory" node. |
| 33 | +#[derive(Clone, Copy, Debug)] |
| 34 | +pub struct Memory<'a> { |
| 35 | + node: FdtNode<'a>, |
| 36 | +} |
| 37 | + |
| 38 | +impl<'a> Deref for Memory<'a> { |
| 39 | + type Target = FdtNode<'a>; |
| 40 | + |
| 41 | + fn deref(&self) -> &Self::Target { |
| 42 | + &self.node |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<'a> Memory<'a> { |
| 47 | + /// Returns the value of the standard `initial-mapped-area` property of the |
| 48 | + /// memory node. |
| 49 | + /// |
| 50 | + /// # Errors |
| 51 | + /// |
| 52 | + /// Returns an error if a property's name or value cannot be read. |
| 53 | + #[allow(clippy::missing_panics_doc)] |
| 54 | + pub fn initial_mapped_area( |
| 55 | + &self, |
| 56 | + ) -> Result<Option<impl Iterator<Item = InitialMappedArea> + use<'a>>, FdtError> { |
| 57 | + Ok( |
| 58 | + if let Some(property) = self.node.property("initial-mapped-area")? { |
| 59 | + // try_into can't return an error, because we passed a chunk size matching what |
| 60 | + // `InitialMappedArea::from_cells` expects. |
| 61 | + #[allow(clippy::unwrap_used)] |
| 62 | + Some( |
| 63 | + property |
| 64 | + .as_prop_encoded_array(5)? |
| 65 | + .map(|chunk| InitialMappedArea::from_cells(chunk.try_into().unwrap())), |
| 66 | + ) |
| 67 | + } else { |
| 68 | + None |
| 69 | + }, |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + /// Returns the value of the standard `hotpluggable` property of the memory |
| 74 | + /// node. |
| 75 | + /// |
| 76 | + /// # Errors |
| 77 | + /// |
| 78 | + /// Returns an error if a property's name or value cannot be read. |
| 79 | + pub fn hotpluggable(&self) -> Result<bool, FdtError> { |
| 80 | + Ok(self.node.property("hotpluggable")?.is_some()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// The value of an `initial-mapped-area` property. |
| 85 | +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 86 | +pub struct InitialMappedArea { |
| 87 | + /// The effective address. |
| 88 | + pub effective_address: u64, |
| 89 | + /// The physical address. |
| 90 | + pub physical_address: u64, |
| 91 | + /// The size of the area. |
| 92 | + pub size: u32, |
| 93 | +} |
| 94 | + |
| 95 | +impl InitialMappedArea { |
| 96 | + fn from_cells([ea_high, ea_low, pa_high, pa_low, size]: [big_endian::U32; 5]) -> Self { |
| 97 | + Self { |
| 98 | + effective_address: u64::from(ea_high.get()) << 32 | u64::from(ea_low.get()), |
| 99 | + physical_address: u64::from(pa_high.get()) << 32 | u64::from(pa_low.get()), |
| 100 | + size: size.get(), |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments