Skip to content
Open
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
27 changes: 5 additions & 22 deletions src/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ impl<'a> Tree<'a> for Fdt<'a> {
type Node = FdtNode<'a>;

fn root(self) -> FdtNode<'a> {
let offset = self.header().off_dt_struct() as usize;
FdtNode::new(self, offset)
FdtNode::new(self, self.header().off_dt_struct())
}
}

Expand Down Expand Up @@ -537,11 +536,12 @@ impl<'a> Fdt<'a> {
self.validate_mem_reservations()?;

// Validate structure block
let offset = header.off_dt_struct() as usize;
let off_dt_struct = header.off_dt_struct();
let offset = off_dt_struct as usize;
// Check root node
self.expect_token(offset, FdtToken::BeginNode)?;

let end_offset = self.validate_structure(offset, options)?;
let end_offset = self.validate_structure(off_dt_struct, options)?;

// Check FDT_END
self.expect_token(end_offset, FdtToken::End)?;
Expand Down Expand Up @@ -582,7 +582,7 @@ impl<'a> Fdt<'a> {
}
fn validate_structure(
self,
start_offset: usize,
start_offset: u32,
options: &FdtOptions,
) -> Result<usize, FdtParseError> {
let mut cursor = RawSubtreeCursor::new(start_offset);
Expand Down Expand Up @@ -891,23 +891,6 @@ impl<'a> Fdt<'a> {
}
}

pub(crate) fn next_sibling_offset(self, offset: usize) -> Result<usize, FdtParseError> {
RawSubtreeCursor::new(offset).end_offset(self)
}

pub(crate) fn next_property_offset(self, offset: usize) -> Result<usize, FdtParseError> {
let (header, _) = FdtPropertyHeader::ref_from_prefix(self.data_at_offset(offset)?)
.map_err(|_e| FdtParseError::new(FdtErrorKind::InvalidLength, offset))?;

let len = header.len() as usize;
let prop_offset = offset + size_of::<FdtPropertyHeader>();
if len > self.data.len() - prop_offset {
return Err(FdtParseError::new(FdtErrorKind::InvalidLength, prop_offset));
}

Ok(Self::align_tag_offset(prop_offset + len))
}

pub(crate) fn data_at_offset(&self, offset: usize) -> Result<&[u8], FdtParseError> {
self.data
.get(offset..)
Expand Down
119 changes: 33 additions & 86 deletions src/fdt/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use core::fmt::{self, Display, Formatter};

use super::{FDT_TAGSIZE, Fdt, FdtToken};
use super::Fdt;
use crate::Node;
use crate::fdt::event::FdtEventIter;
use crate::fdt::property::{FdtPropIter, FdtProperty};
Expand All @@ -22,7 +22,7 @@ use crate::util::write_indent;
#[derive(Debug, Clone, Copy)]
pub struct FdtNode<'a> {
pub(crate) fdt: Fdt<'a>,
pub(crate) offset: usize,
pub(crate) offset: u32,
/// The `#address-cells` and `#size-cells` properties of this node's parent
/// node.
pub(crate) parent_address_space: AddressSpaceProperties,
Expand All @@ -41,7 +41,7 @@ impl<'a> Node<'a> for FdtNode<'a> {
/// [`Fdt::new_unchecked`] or [`Fdt::from_raw_unchecked`] and the FDT is not
/// valid.
fn name(self) -> &'a str {
RawCursor::from_usize(self.offset)
RawCursor::new(self.offset)
.enter_node(self.fdt)
.expect("Fdt should be valid")
.name(self.fdt)
Expand All @@ -51,12 +51,15 @@ impl<'a> Node<'a> for FdtNode<'a> {
FdtPropIter::new(self.fdt, self.offset)
}

/// Returns an iterator over the children of this node.
///
/// # Panics
///
/// Panics if the [`Fdt`] structure was constructed using
/// [`Fdt::new_unchecked`] or [`Fdt::from_raw_unchecked`] and the FDT is not
/// valid.
fn children(self) -> private::FdtChildIter<'a> {
private::FdtChildIter {
fdt: self.fdt,
inner: InnerChildIter::new(self.offset),
parent_address_space: self.address_space(),
}
private::FdtChildIter::new(self.fdt, self.offset, self.address_space())
}

fn parent_address_space(self) -> AddressSpaceProperties {
Expand All @@ -65,7 +68,7 @@ impl<'a> Node<'a> for FdtNode<'a> {
}

impl<'a> FdtNode<'a> {
pub(crate) fn new(fdt: Fdt<'a>, offset: usize) -> Self {
pub(crate) fn new(fdt: Fdt<'a>, offset: u32) -> Self {
Self {
fdt,
offset,
Expand Down Expand Up @@ -101,7 +104,7 @@ impl<'a> FdtNode<'a> {
/// ```
#[must_use]
pub fn offset(self) -> usize {
self.offset - self.fdt.header().off_dt_struct() as usize
self.offset as usize - self.fdt.header().off_dt_struct() as usize
}

/// Returns an iterator over the events in the structure block of the
Expand Down Expand Up @@ -203,97 +206,41 @@ impl Display for FdtNode<'_> {
}
}

#[derive(Debug, Clone)]
pub(crate) enum InnerChildIter {
Start { offset: usize },
Running { offset: usize },
}

impl InnerChildIter {
#[must_use]
pub(crate) fn new(offset: usize) -> Self {
Self::Start { offset }
}

#[must_use]
pub(crate) fn next(&mut self, fdt: Fdt<'_>) -> Option<usize> {
match self {
Self::Start { offset } => {
let mut off = *offset;
off += FDT_TAGSIZE; // Skip FDT_BEGIN_NODE
off = fdt.find_string_end(off).expect("Fdt should be valid");
off = Fdt::align_tag_offset(off);

let result = Self::skip_props(fdt, &mut off);
*self = Self::Running { offset: off };
result
}
Self::Running { offset } => Self::next_child_parsed(fdt, offset),
}
}

#[must_use]
fn skip_props(fdt: Fdt<'_>, offset: &mut usize) -> Option<usize> {
loop {
let token = fdt.read_token(*offset).expect("Fdt should be valid");
match token {
FdtToken::BeginNode => return Some(*offset),
FdtToken::Prop => {
*offset = fdt
.next_property_offset(*offset)
.expect("Fdt should be valid");
}
FdtToken::EndNode | FdtToken::End => return None,
FdtToken::Nop => *offset += FDT_TAGSIZE,
}
}
}

#[must_use]
fn next_child_parsed(fdt: Fdt<'_>, offset: &mut usize) -> Option<usize> {
// advance past the child unless it was removed or iteration ended
if fdt.read_token(*offset).expect("Fdt should be valid") == FdtToken::BeginNode {
*offset = fdt
.next_sibling_offset(*offset)
.expect("Fdt should be valid");
}
loop {
let token = fdt.read_token(*offset).expect("Fdt should be valid");
match token {
FdtToken::BeginNode => {
return Some(*offset);
}
FdtToken::Prop => {
panic!("Fdt should be valid");
}
FdtToken::EndNode | FdtToken::End => return None,
FdtToken::Nop => *offset += FDT_TAGSIZE,
}
}
}
}

pub(crate) mod private {
use crate::fdt::node::InnerChildIter;
use crate::fdt::raw::CursorState;
use crate::fdt::{Fdt, FdtNode};
use crate::standard::AddressSpaceProperties;

/// An iterator over the children of a device tree node.
#[derive(Debug, Clone)]
pub struct FdtChildIter<'a> {
pub(crate) fdt: Fdt<'a>,
pub(crate) inner: InnerChildIter,
pub(crate) parent_address_space: AddressSpaceProperties,
fdt: Fdt<'a>,
state: CursorState,
parent_address_space: AddressSpaceProperties,
}

impl<'a> FdtChildIter<'a> {
pub(super) const fn new(
fdt: Fdt<'a>,
parent_offset: u32,
parent_address_space: AddressSpaceProperties,
) -> Self {
Self {
fdt,
state: CursorState::Start(parent_offset),
parent_address_space,
}
}
}

impl<'a> Iterator for FdtChildIter<'a> {
type Item = FdtNode<'a>;

fn next(&mut self) -> Option<Self::Item> {
let node_offset = self.inner.next(self.fdt)?;
let offset = self.state.next_child(self.fdt)?;
Some(FdtNode {
fdt: self.fdt,
offset: node_offset,
offset,
parent_address_space: self.parent_address_space,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/fdt/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ pub struct FdtPropIter<'a> {
}

impl<'a> FdtPropIter<'a> {
pub(crate) fn new(fdt: Fdt<'a>, node_offset: usize) -> Self {
pub(crate) const fn new(fdt: Fdt<'a>, node_offset: u32) -> Self {
Self {
fdt,
state: CursorState::start(node_offset),
state: CursorState::Start(node_offset),
}
}
}
Expand Down
Loading
Loading