Skip to content

Commit 126b517

Browse files
committed
Move standard property methods to a new module.
1 parent 7f67db8 commit 126b517

File tree

7 files changed

+133
-117
lines changed

7 files changed

+133
-117
lines changed

src/fdt/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
2727

2828
pub use self::node::FdtNode;
2929
pub use self::property::FdtProperty;
30-
pub use self::property::status::Status;
3130
use crate::error::{FdtErrorKind, FdtParseError};
3231
use crate::memreserve::MemoryReservation;
3332

src/fdt/node.rs

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
use core::fmt;
1212

1313
use super::{FDT_TAGSIZE, Fdt, FdtToken};
14-
use crate::error::{FdtError, FdtParseError};
15-
use crate::fdt::Status;
14+
use crate::error::FdtParseError;
1615
use crate::fdt::property::{FdtPropIter, FdtProperty};
1716

18-
const DEFAULT_ADDRESS_CELLS: u32 = 2;
19-
const DEFAULT_SIZE_CELLS: u32 = 1;
20-
2117
/// A node in a flattened device tree.
2218
#[derive(Debug, Clone, Copy)]
2319
pub struct FdtNode<'a> {
@@ -206,114 +202,6 @@ impl<'a> FdtNode<'a> {
206202
}
207203
}
208204

209-
/// Returns the value of the standard `compatible` property.
210-
///
211-
/// # Errors
212-
///
213-
/// Returns an error if a property's name or value cannot be read.
214-
pub fn compatible(
215-
&self,
216-
) -> Result<Option<impl Iterator<Item = &'a str> + use<'a>>, FdtParseError> {
217-
Ok(self
218-
.property("compatible")?
219-
.map(|property| property.as_str_list()))
220-
}
221-
222-
/// Returns the value of the standard `model` property.
223-
///
224-
/// # Errors
225-
///
226-
/// Returns an error if a property's name or value cannot be read, or the
227-
/// value isn't a valid UTF-8 string.
228-
pub fn model(&self) -> Result<Option<&'a str>, FdtError> {
229-
Ok(if let Some(model) = self.property("model")? {
230-
Some(model.as_str()?)
231-
} else {
232-
None
233-
})
234-
}
235-
236-
/// Returns the value of the standard `phandle` property.
237-
///
238-
/// # Errors
239-
///
240-
/// Returns an error if a property's name or value cannot be read, or the
241-
/// value isn't a valid u32.
242-
pub fn phandle(&self) -> Result<Option<u32>, FdtError> {
243-
Ok(if let Some(property) = self.property("phandle")? {
244-
Some(property.as_u32()?)
245-
} else {
246-
None
247-
})
248-
}
249-
250-
/// Returns the value of the standard `status` property.
251-
///
252-
/// If there is no `status` property then `okay` is assumed.
253-
///
254-
/// # Errors
255-
///
256-
/// Returns an error if a property's name or value cannot be read, or the
257-
/// value isn't a valid status.
258-
pub fn status(&self) -> Result<Status, FdtError> {
259-
Ok(if let Some(status) = self.property("status")? {
260-
status.as_str()?.parse()?
261-
} else {
262-
Status::Okay
263-
})
264-
}
265-
266-
/// Returns the value of the standard `#address-cells` property.
267-
///
268-
/// # Errors
269-
///
270-
/// Returns an error if a property's name or value cannot be read, or the
271-
/// value isn't a valid u32.
272-
pub fn address_cells(&self) -> Result<u32, FdtError> {
273-
Ok(if let Some(property) = self.property("#address-cells")? {
274-
property.as_u32()?
275-
} else {
276-
DEFAULT_ADDRESS_CELLS
277-
})
278-
}
279-
280-
/// Returns the value of the standard `#size-cells` property.
281-
///
282-
/// # Errors
283-
///
284-
/// Returns an error if a property's name or value cannot be read, or the
285-
/// value isn't a valid u32.
286-
pub fn size_cells(&self) -> Result<u32, FdtError> {
287-
Ok(if let Some(model) = self.property("#size-cells")? {
288-
model.as_u32()?
289-
} else {
290-
DEFAULT_SIZE_CELLS
291-
})
292-
}
293-
294-
/// Returns the value of the standard `virtual-reg` property.
295-
///
296-
/// # Errors
297-
///
298-
/// Returns an error if a property's name or value cannot be read, or the
299-
/// value isn't a valid u32.
300-
pub fn virtual_reg(&self) -> Result<Option<u32>, FdtError> {
301-
Ok(if let Some(property) = self.property("virtual-reg")? {
302-
Some(property.as_u32()?)
303-
} else {
304-
None
305-
})
306-
}
307-
308-
/// Returns whether the standard `dma-coherent` property is present.
309-
///
310-
/// # Errors
311-
///
312-
/// Returns an error if a property can't be read.
313-
pub fn dma_coherent(&self) -> Result<bool, FdtError> {
314-
Ok(self.property("dma-coherent")?.is_some())
315-
}
316-
317205
pub(crate) fn fmt_recursive(&self, f: &mut fmt::Formatter<'_>, indent: usize) -> fmt::Result {
318206
let name = self.name().map_err(|_| fmt::Error)?;
319207
if name.is_empty() {

src/fdt/property.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
//! A read-only API for inspecting a device tree property.
1010
11-
pub(super) mod status;
12-
1311
use core::ffi::CStr;
1412
use core::fmt;
1513

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ pub mod fdt;
3131
pub mod memreserve;
3232
#[cfg(feature = "write")]
3333
pub mod model;
34+
pub mod standard;

src/standard.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Standard nodes and properties.
10+
11+
mod status;
12+
13+
pub use status::Status;
14+
15+
use crate::error::{FdtError, FdtParseError};
16+
use crate::fdt::FdtNode;
17+
18+
const DEFAULT_ADDRESS_CELLS: u32 = 2;
19+
const DEFAULT_SIZE_CELLS: u32 = 1;
20+
21+
impl<'a> FdtNode<'a> {
22+
/// Returns the value of the standard `compatible` property.
23+
///
24+
/// # Errors
25+
///
26+
/// Returns an error if a property's name or value cannot be read.
27+
pub fn compatible(
28+
&self,
29+
) -> Result<Option<impl Iterator<Item = &'a str> + use<'a>>, FdtParseError> {
30+
Ok(self
31+
.property("compatible")?
32+
.map(|property| property.as_str_list()))
33+
}
34+
35+
/// Returns the value of the standard `model` property.
36+
///
37+
/// # Errors
38+
///
39+
/// Returns an error if a property's name or value cannot be read, or the
40+
/// value isn't a valid UTF-8 string.
41+
pub fn model(&self) -> Result<Option<&'a str>, FdtParseError> {
42+
Ok(if let Some(model) = self.property("model")? {
43+
Some(model.as_str()?)
44+
} else {
45+
None
46+
})
47+
}
48+
49+
/// Returns the value of the standard `phandle` property.
50+
///
51+
/// # Errors
52+
///
53+
/// Returns an error if a property's name or value cannot be read, or the
54+
/// value isn't a valid u32.
55+
pub fn phandle(&self) -> Result<Option<u32>, FdtParseError> {
56+
Ok(if let Some(property) = self.property("phandle")? {
57+
Some(property.as_u32()?)
58+
} else {
59+
None
60+
})
61+
}
62+
63+
/// Returns the value of the standard `status` property.
64+
///
65+
/// If there is no `status` property then `okay` is assumed.
66+
///
67+
/// # Errors
68+
///
69+
/// Returns an error if a property's name or value cannot be read, or the
70+
/// value isn't a valid status.
71+
pub fn status(&self) -> Result<Status, FdtError> {
72+
Ok(if let Some(status) = self.property("status")? {
73+
status.as_str()?.parse()?
74+
} else {
75+
Status::Okay
76+
})
77+
}
78+
79+
/// Returns the value of the standard `#address-cells` property.
80+
///
81+
/// # Errors
82+
///
83+
/// Returns an error if a property's name or value cannot be read, or the
84+
/// value isn't a valid u32.
85+
pub fn address_cells(&self) -> Result<u32, FdtParseError> {
86+
Ok(if let Some(property) = self.property("#address-cells")? {
87+
property.as_u32()?
88+
} else {
89+
DEFAULT_ADDRESS_CELLS
90+
})
91+
}
92+
93+
/// Returns the value of the standard `#size-cells` property.
94+
///
95+
/// # Errors
96+
///
97+
/// Returns an error if a property's name or value cannot be read, or the
98+
/// value isn't a valid u32.
99+
pub fn size_cells(&self) -> Result<u32, FdtParseError> {
100+
Ok(if let Some(model) = self.property("#size-cells")? {
101+
model.as_u32()?
102+
} else {
103+
DEFAULT_SIZE_CELLS
104+
})
105+
}
106+
107+
/// Returns the value of the standard `virtual-reg` property.
108+
///
109+
/// # Errors
110+
///
111+
/// Returns an error if a property's name or value cannot be read, or the
112+
/// value isn't a valid u32.
113+
pub fn virtual_reg(&self) -> Result<Option<u32>, FdtParseError> {
114+
Ok(if let Some(property) = self.property("virtual-reg")? {
115+
Some(property.as_u32()?)
116+
} else {
117+
None
118+
})
119+
}
120+
121+
/// Returns whether the standard `dma-coherent` property is present.
122+
///
123+
/// # Errors
124+
///
125+
/// Returns an error if a property can't be read.
126+
pub fn dma_coherent(&self) -> Result<bool, FdtParseError> {
127+
Ok(self.property("dma-coherent")?.is_some())
128+
}
129+
}

tests/fdt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use dtoolkit::fdt::{Fdt, Status};
9+
use dtoolkit::fdt::Fdt;
10+
use dtoolkit::standard::Status;
1011

1112
#[test]
1213
fn read_child_nodes() {

0 commit comments

Comments
 (0)