-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for reading properties in FDT #4
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| // 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-only API for inspecting a device tree property. | ||
|
|
||
| use core::ffi::CStr; | ||
|
|
||
| use zerocopy::{FromBytes, big_endian}; | ||
|
|
||
| use super::{FDT_TAGSIZE, Fdt, FdtToken}; | ||
| use crate::error::{FdtError, FdtErrorKind}; | ||
|
|
||
| /// A property of a device tree node. | ||
| #[derive(Debug, PartialEq)] | ||
| pub struct FdtProperty<'a> { | ||
| name: &'a str, | ||
| value: &'a [u8], | ||
| value_offset: usize, | ||
| } | ||
|
|
||
| impl<'a> FdtProperty<'a> { | ||
| /// Returns the name of this property. | ||
| #[must_use] | ||
| pub fn name(&self) -> &'a str { | ||
| self.name | ||
| } | ||
|
|
||
| /// Returns the value of this property. | ||
| #[must_use] | ||
| pub fn value(&self) -> &'a [u8] { | ||
| self.value | ||
| } | ||
m4tx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Returns the value of this property as a `u32`. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an [`FdtErrorKind::InvalidLength`] if the property's value is | ||
| /// not 4 bytes long. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::fdt::Fdt; | ||
| /// # let dtb = include_bytes!("../../tests/dtb/test_props.dtb"); | ||
| /// let fdt = Fdt::new(dtb).unwrap(); | ||
| /// let node = fdt.find_node("/test-props").unwrap().unwrap(); | ||
| /// let prop = node.property("u32-prop").unwrap().unwrap(); | ||
| /// assert_eq!(prop.as_u32().unwrap(), 0x12345678); | ||
| /// ``` | ||
| pub fn as_u32(&self) -> Result<u32, FdtError> { | ||
| big_endian::U32::ref_from_bytes(self.value) | ||
| .map(|val| val.get()) | ||
| .map_err(|_e| FdtError::new(FdtErrorKind::InvalidLength, self.value_offset)) | ||
| } | ||
|
|
||
| /// Returns the value of this property as a `u64`. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an [`FdtErrorKind::InvalidLength`] if the property's value is | ||
| /// not 8 bytes long. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::fdt::Fdt; | ||
| /// # let dtb = include_bytes!("../../tests/dtb/test_props.dtb"); | ||
| /// let fdt = Fdt::new(dtb).unwrap(); | ||
| /// let node = fdt.find_node("/test-props").unwrap().unwrap(); | ||
| /// let prop = node.property("u64-prop").unwrap().unwrap(); | ||
| /// assert_eq!(prop.as_u64().unwrap(), 0x1122334455667788); | ||
| /// ``` | ||
| pub fn as_u64(&self) -> Result<u64, FdtError> { | ||
| big_endian::U64::ref_from_bytes(self.value) | ||
| .map(|val| val.get()) | ||
| .map_err(|_e| FdtError::new(FdtErrorKind::InvalidLength, self.value_offset)) | ||
| } | ||
|
|
||
| /// Returns the value of this property as a string. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an [`FdtErrorKind::InvalidString`] if the property's value is | ||
| /// not a null-terminated string or contains invalid UTF-8. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::fdt::Fdt; | ||
| /// # let dtb = include_bytes!("../../tests/dtb/test_props.dtb"); | ||
| /// let fdt = Fdt::new(dtb).unwrap(); | ||
| /// let node = fdt.find_node("/test-props").unwrap().unwrap(); | ||
| /// let prop = node.property("str-prop").unwrap().unwrap(); | ||
| /// assert_eq!(prop.as_str().unwrap(), "hello world"); | ||
| /// ``` | ||
| pub fn as_str(&self) -> Result<&'a str, FdtError> { | ||
| let cstr = CStr::from_bytes_with_nul(self.value) | ||
| .map_err(|_| FdtError::new(FdtErrorKind::InvalidString, self.value_offset))?; | ||
| cstr.to_str() | ||
| .map_err(|_| FdtError::new(FdtErrorKind::InvalidString, self.value_offset)) | ||
| } | ||
|
|
||
| /// Returns an iterator over the strings in this property. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use dtoolkit::fdt::Fdt; | ||
| /// # let dtb = include_bytes!("../../tests/dtb/test_props.dtb"); | ||
| /// let fdt = Fdt::new(dtb).unwrap(); | ||
| /// let node = fdt.find_node("/test-props").unwrap().unwrap(); | ||
| /// let prop = node.property("str-list-prop").unwrap().unwrap(); | ||
| /// let mut str_list = prop.as_str_list(); | ||
| /// assert_eq!(str_list.next(), Some("first")); | ||
| /// assert_eq!(str_list.next(), Some("second")); | ||
| /// assert_eq!(str_list.next(), Some("third")); | ||
| /// assert_eq!(str_list.next(), None); | ||
| /// ``` | ||
| pub fn as_str_list(&self) -> impl Iterator<Item = &'a str> { | ||
| FdtStringListIterator { value: self.value } | ||
| } | ||
| } | ||
|
|
||
| /// An iterator over the properties of a device tree node. | ||
| pub(crate) enum FdtPropIter<'a> { | ||
| Start { fdt: &'a Fdt<'a>, offset: usize }, | ||
| Running { fdt: &'a Fdt<'a>, offset: usize }, | ||
| Error, | ||
| } | ||
|
|
||
| impl<'a> Iterator for FdtPropIter<'a> { | ||
| type Item = Result<FdtProperty<'a>, FdtError>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| match self { | ||
| Self::Start { fdt, offset } => { | ||
| let mut offset = *offset; | ||
| offset += FDT_TAGSIZE; // Skip FDT_BEGIN_NODE | ||
| offset = match fdt.find_string_end(offset) { | ||
| Ok(offset) => offset, | ||
| Err(e) => { | ||
| *self = Self::Error; | ||
| return Some(Err(e)); | ||
| } | ||
| }; | ||
| offset = Fdt::align_tag_offset(offset); | ||
| *self = Self::Running { fdt, offset }; | ||
| self.next() | ||
| } | ||
| Self::Running { fdt, offset } => match Self::try_next(fdt, offset) { | ||
| Some(Ok(val)) => Some(Ok(val)), | ||
| Some(Err(e)) => { | ||
| *self = Self::Error; | ||
| Some(Err(e)) | ||
| } | ||
| None => None, | ||
| }, | ||
| Self::Error => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a> FdtPropIter<'a> { | ||
| fn try_next(fdt: &'a Fdt<'a>, offset: &mut usize) -> Option<Result<FdtProperty<'a>, FdtError>> { | ||
| loop { | ||
| let token = match fdt.read_token(*offset) { | ||
| Ok(token) => token, | ||
| Err(e) => return Some(Err(e)), | ||
| }; | ||
| match token { | ||
| FdtToken::Prop => { | ||
| let len = match big_endian::U32::ref_from_prefix( | ||
| &fdt.data[*offset + FDT_TAGSIZE..], | ||
| ) { | ||
| Ok((val, _)) => val.get() as usize, | ||
| Err(_) => { | ||
| return Some(Err(FdtError::new(FdtErrorKind::InvalidLength, *offset))); | ||
| } | ||
| }; | ||
| let nameoff = match big_endian::U32::ref_from_prefix( | ||
| &fdt.data[*offset + 2 * FDT_TAGSIZE..], | ||
| ) { | ||
| Ok((val, _)) => val.get() as usize, | ||
| Err(_) => { | ||
| return Some(Err(FdtError::new(FdtErrorKind::InvalidLength, *offset))); | ||
| } | ||
| }; | ||
| let prop_offset = *offset + 3 * FDT_TAGSIZE; | ||
| *offset = Fdt::align_tag_offset(prop_offset + len); | ||
| let name = match fdt.string(nameoff) { | ||
| Ok(name) => name, | ||
| Err(e) => return Some(Err(e)), | ||
| }; | ||
| let value = fdt.data.get(prop_offset..prop_offset + len)?; | ||
| return Some(Ok(FdtProperty { | ||
| name, | ||
| value, | ||
| value_offset: prop_offset, | ||
| })); | ||
| } | ||
| FdtToken::Nop => *offset += FDT_TAGSIZE, | ||
| _ => return None, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct FdtStringListIterator<'a> { | ||
| value: &'a [u8], | ||
| } | ||
|
|
||
| impl<'a> Iterator for FdtStringListIterator<'a> { | ||
| type Item = &'a str; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| if self.value.is_empty() { | ||
| return None; | ||
| } | ||
| let cstr = CStr::from_bytes_until_nul(self.value).ok()?; | ||
| let s = cstr.to_str().ok()?; | ||
| self.value = &self.value[s.len() + 1..]; | ||
| Some(s) | ||
| } | ||
| } | ||
Binary file not shown.
Binary file not shown.
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,15 @@ | ||
| /dts-v1/; | ||
|
|
||
| / { | ||
| child1 { | ||
| prop1 = <0x02>; | ||
|
|
||
| child2 { | ||
| prop2 = <0x02>; | ||
| }; | ||
| }; | ||
|
|
||
| child3 { | ||
| prop3 = <0x02>; | ||
| }; | ||
| }; |
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,13 @@ | ||
| /dts-v1/; | ||
|
|
||
| / { | ||
| #address-cells = <2>; | ||
| #size-cells = <2>; | ||
|
|
||
| test-props { | ||
| u32-prop = <0x12345678>; | ||
| u64-prop = <0x11223344 0x55667788>; | ||
| str-prop = "hello world"; | ||
| str-list-prop = "first", "second", "third"; | ||
| }; | ||
| }; |
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.
Why is this (and value)
must_use? It would be pointless to call it and ignore the return value, sure, but it's not like you're failing to check for an error condition or something like that.Uh oh!
There was an error while loading. Please reload this page.
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.
Well, the usage of
#[must_use]used to be very inconsistent in the Rust ecosystem and has been a topic of discussion for quite a long time. The reference, however, doesn't specify when exactly should the attribute be used.Nowadays, it seems like the Rust standard library follows something in the lines of "if the function has no side-effects, it should warn when the result is discarded". A while ago over 800 attributes have been added, and you can see that methods such as str::len, or f32::is_nan (or honestly, many many others) do have the attribute. Hence I'm following this to be consistent with the standard library.
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.
Okay, fair enough.