|
| 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::fmt::{self, Display, Formatter}; |
| 10 | +use core::ops::{BitOr, Shl}; |
| 11 | + |
| 12 | +use zerocopy::big_endian; |
| 13 | + |
| 14 | +use crate::error::FdtError; |
| 15 | + |
| 16 | +/// The value of a `reg` property. |
| 17 | +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 18 | +pub struct Reg<'a> { |
| 19 | + /// The address of the device within the address space of the parent bus. |
| 20 | + pub address: &'a [big_endian::U32], |
| 21 | + /// The size of the device within the address space of the parent bus. |
| 22 | + pub size: &'a [big_endian::U32], |
| 23 | +} |
| 24 | + |
| 25 | +impl Reg<'_> { |
| 26 | + /// Attempts to return the address as the given type, if it will fit. |
| 27 | + pub fn address<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>( |
| 28 | + self, |
| 29 | + ) -> Result<T, FdtError> { |
| 30 | + if size_of::<T>() < self.address.len() * size_of::<u32>() { |
| 31 | + Err(FdtError::AddressTooBig { |
| 32 | + cells: self.address.len(), |
| 33 | + }) |
| 34 | + } else { |
| 35 | + let mut value = Default::default(); |
| 36 | + for cell in self.address { |
| 37 | + value = (value << 32) | cell.get().into(); |
| 38 | + } |
| 39 | + Ok(value) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Attempts to return the size as the given type, if it will fit. |
| 44 | + pub fn size<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>( |
| 45 | + self, |
| 46 | + ) -> Result<T, FdtError> { |
| 47 | + if size_of::<T>() < self.size.len() * size_of::<u32>() { |
| 48 | + Err(FdtError::SizeTooBig { |
| 49 | + cells: self.size.len(), |
| 50 | + }) |
| 51 | + } else { |
| 52 | + let mut value = Default::default(); |
| 53 | + for cell in self.size { |
| 54 | + value = value << size_of::<u32>() | cell.get().into(); |
| 55 | + } |
| 56 | + Ok(value) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl Display for Reg<'_> { |
| 62 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 63 | + f.write_str("0x")?; |
| 64 | + for part in self.address { |
| 65 | + write!(f, "{part:08x}")?; |
| 66 | + } |
| 67 | + f.write_str(" 0x")?; |
| 68 | + for part in self.size { |
| 69 | + write!(f, "{part:08x}")?; |
| 70 | + } |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use super::*; |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn format_reg() { |
| 81 | + let reg = Reg { |
| 82 | + address: &[0x12345678.into(), 0xabcd0000.into()], |
| 83 | + size: &[0x11223344.into()], |
| 84 | + }; |
| 85 | + assert_eq!(format!("{}", reg), "0x12345678abcd0000 0x11223344"); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn address_size() { |
| 90 | + let reg = Reg { |
| 91 | + address: &[0x12345678.into(), 0xabcd0000.into()], |
| 92 | + size: &[0x11223344.into()], |
| 93 | + }; |
| 94 | + assert_eq!( |
| 95 | + reg.address::<u32>(), |
| 96 | + Err(FdtError::AddressTooBig { cells: 2 }) |
| 97 | + ); |
| 98 | + assert_eq!(reg.address::<u64>(), Ok(0x12345678_abcd0000)); |
| 99 | + assert_eq!(reg.size::<u32>(), Ok(0x11223344)); |
| 100 | + assert_eq!(reg.size::<u64>(), Ok(0x11223344)); |
| 101 | + } |
| 102 | +} |
0 commit comments