Skip to content

Add getters and types for standard reg, ranges and dma-ranges properties#19

Merged
m4tx merged 7 commits into
mainfrom
typedaccessors
Dec 17, 2025
Merged

Add getters and types for standard reg, ranges and dma-ranges properties#19
m4tx merged 7 commits into
mainfrom
typedaccessors

Conversation

@qwandor

@qwandor qwandor commented Dec 16, 2025

Copy link
Copy Markdown
Collaborator

I've also fixed lifetimes of values returned by various methods not to be tied unnecessarily to the lifetime of the Fdt or FdtNode itself, changed methods on Copy types to take self by value rather than reference, and improved the FdtProperty::as_prop_encoded_array helper method to do more of the work.

@qwandor qwandor force-pushed the typedaccessors branch 3 times, most recently from b62159d to 033efbb Compare December 17, 2025 13:09
@qwandor qwandor changed the title Add getter for standard reg property Add getters and types for standard reg, ranges and dma-ranges properties Dec 17, 2025
Comment thread src/fdt/property.rs Outdated
/// An integer value split into several big-endian u32 parts.
///
/// This is generally used in prop-encoded-array properties.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/fdt/node.rs Outdated
Comment on lines +23 to +26
/// The `#size-cells` property of this node's parent node.
pub(crate) parent_size_cells: u32,
/// The `#address-cells` property of this node's parent node.
pub(crate) parent_address_cells: u32,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to duplicate this here and in FdtChildIter. Maybe we could define a separate structure called something like ParentMetadata (or more specifically, "BusAddressSpaceParams"), and define Default trait impl as well as from_node() associated function to encapsulate the logic?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/fdt/property.rs Outdated
///
/// This is generally used in prop-encoded-array properties.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Cells<'a>(pub &'a [big_endian::U32]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expose a type from another library (zerocopy) here, which really is just an implementation detail. This means that each new major version of this library will also mean a new major version for us.
I don't think there's a strong reason to expose this, especially since each sensible use case will probably also require map(|elem| elem.get()) on this slice - can we hide this type somehow? Perhaps by only exposing an ExactSizedIterator over u32, or maybe not exposing this data at all.

Also, this is probably not actionable at this moment, but we'll need a mutable version of this so that we can construct standard properties easily. It might be worth thinking about how should it look like - maybe similarly to Cow with an "owned" or "borrowed" variant. But again, this can be left for future PRs; I'm just leaving this here as a food for thought.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major versions of zerocopy are hopefully rare, but fair enough. The main downside is that it prevents anyone outside the crate from constructing an instance of this (e.g. for tests), but maybe that is fine.

Comment thread src/standard/memory.rs
fn from_cells([ea_high, ea_low, pa_high, pa_low, size]: [big_endian::U32; 5]) -> Self {
#[expect(
clippy::unwrap_used,
reason = "The Cells passed are always the correct size"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document here what are the correct sizes ([2, 2, 1]).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/fdt/property.rs Outdated
/// in the given type.
pub fn to_intsize<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>(
self,
field: &'static str,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't exactly like that this is public API and this parameter is only used for error handling - this function doesn't actually care at all what's the field name. It sounds like a piece of metadata that should be added at a higher level. Perhaps this could return its own error struct that would be then wrapped and combined with the field name by the caller?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the field name from the error variant, it's probably clear from context anyway.

@qwandor qwandor requested a review from m4tx December 17, 2025 14:58
Base automatically changed from compatible to main December 17, 2025 17:20
It is Copy and only contains a single reference, so there's no point
passing it by reference. This also means that the lifetimes of things
returned from getters are only tied to the underlying slice, not the
Fdt itself, which may be more convenient.
This requires keeping track of the #address-cells and #size-cells of the
parent node.
This saves work splitting the result in the caller.
@m4tx m4tx merged commit 4e25df1 into main Dec 17, 2025
25 checks passed
@m4tx m4tx deleted the typedaccessors branch December 17, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants