Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/fdt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)?;
Expand Down
10 changes: 8 additions & 2 deletions src/fdt/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions src/fdt/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
13 changes: 13 additions & 0 deletions src/standard/cpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Item = Result<Cpu<'a>, FdtParseError>> + use<'a> {
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions src/standard/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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<Memory<'_>, FdtError> {
pub fn memory(self) -> Result<Memory<'a>, 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>,
Expand All @@ -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.
Expand Down
14 changes: 12 additions & 2 deletions src/standard/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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::*;
Expand Down
Loading