diff --git a/src/fdt/mod.rs b/src/fdt/mod.rs index 0ddaab9..0e1b607 100644 --- a/src/fdt/mod.rs +++ b/src/fdt/mod.rs @@ -19,8 +19,9 @@ mod node; mod property; use core::ffi::CStr; +use core::fmt::{self, Debug, Display, Formatter}; use core::mem::offset_of; -use core::{fmt, ptr}; +use core::ptr; use zerocopy::byteorder::big_endian; use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned}; @@ -108,11 +109,22 @@ impl FdtHeader { } /// A flattened device tree. -#[derive(Debug, Clone, Copy)] +#[derive(Clone, Copy)] pub struct Fdt<'a> { pub(crate) data: &'a [u8], } +impl Debug for Fdt<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!( + f, + "Fdt {{ data: {} bytes at {:?} }}", + self.data.len(), + self.data.as_ptr() + ) + } +} + /// A token in the device tree structure. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum FdtToken { @@ -549,8 +561,8 @@ impl<'a> Fdt<'a> { } } -impl fmt::Display for Fdt<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +impl Display for Fdt<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { writeln!(f, "/dts-v1/;")?; for reservation in self.memory_reservations() { let reservation = reservation.map_err(|_| fmt::Error)?; diff --git a/src/fdt/node.rs b/src/fdt/node.rs index f3f33fd..7031c06 100644 --- a/src/fdt/node.rs +++ b/src/fdt/node.rs @@ -8,7 +8,7 @@ //! A read-only API for inspecting a device tree node. -use core::fmt; +use core::fmt::{self, Display, Formatter}; use super::{FDT_TAGSIZE, Fdt, FdtToken}; use crate::error::FdtParseError; @@ -211,7 +211,7 @@ impl<'a> FdtNode<'a> { FdtChildIter::Start { node: *self } } - pub(crate) fn fmt_recursive(&self, f: &mut fmt::Formatter<'_>, indent: usize) -> fmt::Result { + pub(crate) fn fmt_recursive(&self, f: &mut Formatter, indent: usize) -> fmt::Result { let name = self.name().map_err(|_| fmt::Error)?; if name.is_empty() { writeln!(f, "{:indent$}/ {{", "", indent = indent)?; @@ -249,6 +249,12 @@ impl<'a> FdtNode<'a> { } } +impl Display for FdtNode<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + self.fmt_recursive(f, 0) + } +} + /// An iterator over the children of a device tree node. enum FdtChildIter<'a> { Start { diff --git a/src/fdt/property.rs b/src/fdt/property.rs index ce22a76..5d2fc52 100644 --- a/src/fdt/property.rs +++ b/src/fdt/property.rs @@ -205,6 +205,12 @@ impl<'a> FdtProperty<'a> { } } +impl Display for FdtProperty<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + self.fmt(f, 0) + } +} + /// An iterator over the properties of a device tree node. pub(crate) enum FdtPropIter<'a> { Start { fdt: Fdt<'a>, offset: usize }, diff --git a/src/standard/cpus.rs b/src/standard/cpus.rs index 89b85a5..45d32c2 100644 --- a/src/standard/cpus.rs +++ b/src/standard/cpus.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use core::fmt::{self, Display, Formatter}; use core::ops::Deref; use crate::error::{FdtError, FdtParseError}; @@ -41,6 +42,12 @@ impl<'a> Deref for Cpus<'a> { } } +impl Display for Cpus<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + self.node.fmt(f) + } +} + impl<'a> Cpus<'a> { /// Returns an iterator over the `/cpus/cpu@*` nodes. pub fn cpus(&self) -> impl Iterator, FdtParseError>> + use<'a> { @@ -76,6 +83,12 @@ impl<'a> Deref for Cpu<'a> { } } +impl Display for Cpu<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + self.node.fmt(f) + } +} + impl<'a> Cpu<'a> { /// Returns an iterator over the IDs of the CPU, from the standard `reg` /// property. diff --git a/src/standard/memory.rs b/src/standard/memory.rs index 49d150e..5281655 100644 --- a/src/standard/memory.rs +++ b/src/standard/memory.rs @@ -6,13 +6,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use core::fmt::{self, Display, Formatter}; use core::ops::Deref; use crate::error::FdtError; use crate::fdt::{Cells, Fdt, FdtNode}; -impl Fdt<'_> { - /// Returns the /memory node. +impl<'a> Fdt<'a> { + /// Returns the `/memory` node. /// /// This should always be included in a valid device tree. /// @@ -21,13 +22,13 @@ impl Fdt<'_> { /// Returns a parse error if there was a problem reading the FDT structure /// to find the node, or `FdtError::MemoryMissing` if the memory node is /// missing. - pub fn memory(&self) -> Result, FdtError> { + pub fn memory(self) -> Result, FdtError> { let node = self.find_node("/memory")?.ok_or(FdtError::MemoryMissing)?; Ok(Memory { node }) } } -/// Typed wrapper for a "/memory" node. +/// Typed wrapper for a `/memory` node. #[derive(Clone, Copy, Debug)] pub struct Memory<'a> { node: FdtNode<'a>, @@ -41,6 +42,12 @@ impl<'a> Deref for Memory<'a> { } } +impl Display for Memory<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + self.node.fmt(f) + } +} + impl<'a> Memory<'a> { /// Returns the value of the standard `initial-mapped-area` property of the /// memory node. diff --git a/src/standard/reg.rs b/src/standard/reg.rs index f0bf387..5d24390 100644 --- a/src/standard/reg.rs +++ b/src/standard/reg.rs @@ -6,14 +6,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core::fmt::{self, Display, Formatter}; +use core::fmt::{self, Debug, Display, Formatter}; use core::ops::{BitOr, Shl}; use crate::error::FdtError; use crate::fdt::Cells; /// The value of a `reg` property. -#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +#[derive(Clone, Copy, Default, Eq, PartialEq)] pub struct Reg<'a> { /// The address of the device within the address space of the parent bus. pub address: Cells<'a>, @@ -55,6 +55,16 @@ impl Display for Reg<'_> { } } +impl Debug for Reg<'_> { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!( + f, + "Reg {{ address: {}, size: {} }}", + self.address, self.size + ) + } +} + #[cfg(test)] mod tests { use super::*;