-
Notifications
You must be signed in to change notification settings - Fork 0
Add in-memory Device Tree representation #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| //! A read-write, in-memory representation of a device tree. | ||
| //! | ||
| //! This module provides the [`DeviceTree`], [`DeviceTreeNode`], and | ||
| //! [`DeviceTreeProperty`] structs, which can be used to create or modify a | ||
| //! device tree in memory. The [`DeviceTree`] can then be serialized to a | ||
| //! flattened device tree blob. | ||
|
|
||
| use alloc::vec::Vec; | ||
|
|
||
| use crate::error::FdtError; | ||
| use crate::fdt::Fdt; | ||
| use crate::memreserve::MemoryReservation; | ||
| mod node; | ||
| mod property; | ||
| pub use node::{DeviceTreeNode, DeviceTreeNodeBuilder}; | ||
| pub use property::DeviceTreeProperty; | ||
|
|
||
| /// A mutable, in-memory representation of a device tree. | ||
| /// | ||
| /// This struct provides a high-level API for creating and modifying a device | ||
| /// tree. It can be created from scratch or by parsing an existing FDT blob. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::model::{DeviceTree, DeviceTreeNode}; | ||
| /// let mut tree = DeviceTree::new(); | ||
| /// tree.root.add_child(DeviceTreeNode::new("child")); | ||
| /// let child = tree.find_node_mut("/child").unwrap(); | ||
| /// ``` | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| #[non_exhaustive] | ||
| pub struct DeviceTree { | ||
m4tx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// The root node for this device tree. | ||
| pub root: DeviceTreeNode, | ||
| /// The memory reservations for this device tree. | ||
| pub memory_reservations: Vec<MemoryReservation>, | ||
| } | ||
|
|
||
| impl DeviceTree { | ||
| /// Creates a new `DeviceTree` with the given root node. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::model::{DeviceTree, DeviceTreeNode}; | ||
| /// let tree = DeviceTree::new(); | ||
| /// ``` | ||
| #[must_use] | ||
| pub fn new() -> Self { | ||
| Self { | ||
| root: DeviceTreeNode::new("/"), | ||
| memory_reservations: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Creates a new `DeviceTree` from a `Fdt`. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::{fdt::Fdt, model::DeviceTree}; | ||
| /// # let dtb = include_bytes!("../../tests/dtb/test.dtb"); | ||
| /// let fdt = Fdt::new(dtb).unwrap(); | ||
| /// let tree = DeviceTree::from_fdt(&fdt).unwrap(); | ||
| /// ``` | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if the root node of the `Fdt` cannot be parsed. | ||
| pub fn from_fdt(fdt: &Fdt<'_>) -> Result<Self, FdtError> { | ||
| let root = DeviceTreeNode::try_from(fdt.root()?)?; | ||
| let memory_reservations: Result<Vec<_>, _> = fdt.memory_reservations().collect(); | ||
| Ok(DeviceTree { | ||
| root, | ||
| memory_reservations: memory_reservations?, | ||
| }) | ||
| } | ||
|
|
||
| /// Finds a node by its path and returns a mutable reference to it. | ||
| /// | ||
| /// # Performance | ||
| /// | ||
| /// This method traverses the device tree, but since child lookup is a | ||
| /// constant-time operation, performance is linear in the number of path | ||
| /// segments. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::model::{DeviceTree, DeviceTreeNode}; | ||
| /// let mut tree = DeviceTree::new(); | ||
| /// tree.root.add_child(DeviceTreeNode::new("child")); | ||
| /// let child = tree.find_node_mut("/child").unwrap(); | ||
| /// assert_eq!(child.name(), "child"); | ||
| /// ``` | ||
| pub fn find_node_mut(&mut self, path: &str) -> Option<&mut DeviceTreeNode> { | ||
| if !path.starts_with('/') { | ||
| return None; | ||
| } | ||
| let mut current_node = &mut self.root; | ||
| if path == "/" { | ||
| return Some(current_node); | ||
| } | ||
| for component in path.split('/').filter(|s| !s.is_empty()) { | ||
| match current_node.child_mut(component) { | ||
| Some(node) => current_node = node, | ||
| None => return None, | ||
| } | ||
| } | ||
| Some(current_node) | ||
| } | ||
| } | ||
|
|
||
| impl Default for DeviceTree { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks enabling the
allocfeature withoutwritehas no meaningful effect, why not just have a single feature?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we might eventually have some functionality that will require the
alloccrate, but won't be used for the intermediate DT representation (which requires its own set of additional libraries). For instance, in-place FDT modification might optionally requirealloccrate to support extending the FDT size to relocate nodes when adding new data - but this is indeed too forward-looking and we can just re-introduce the feature flag then.