Skip to content

Commit 676a613

Browse files
committed
Fixes from code review.
1 parent 3e6341d commit 676a613

File tree

8 files changed

+70
-70
lines changed

8 files changed

+70
-70
lines changed

src/error.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ pub enum FdtError {
3737
},
3838
/// Tried to convert part of a prop-encoded-array property to a type which
3939
/// was too small.
40-
#[error("{field} too big for chosen type ({cells} cells)")]
40+
#[error("prop-encoded-array field too big for chosen type ({cells} cells)")]
4141
TooManyCells {
42-
/// The name of the field.
43-
field: &'static str,
44-
/// The value of the relevant `#address-cells` property.
42+
/// The number of (32-bit) cells in the field.
4543
cells: usize,
4644
},
4745
}

src/fdt/node.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@ use core::fmt;
1313
use super::{FDT_TAGSIZE, Fdt, FdtToken};
1414
use crate::error::FdtParseError;
1515
use crate::fdt::property::{FdtPropIter, FdtProperty};
16-
use crate::standard::{DEFAULT_ADDRESS_CELLS, DEFAULT_SIZE_CELLS};
16+
use crate::standard::AddressSpaceProperties;
1717

1818
/// A node in a flattened device tree.
1919
#[derive(Debug, Clone, Copy)]
2020
pub struct FdtNode<'a> {
2121
pub(crate) fdt: Fdt<'a>,
2222
pub(crate) offset: usize,
23-
/// The `#size-cells` property of this node's parent node.
24-
pub(crate) parent_size_cells: u32,
25-
/// The `#address-cells` property of this node's parent node.
26-
pub(crate) parent_address_cells: u32,
23+
/// The `#address-cells` and `#size-cells` properties of this node's parent
24+
/// node.
25+
pub(crate) parent_address_space: AddressSpaceProperties,
2726
}
2827

2928
impl<'a> FdtNode<'a> {
3029
pub(crate) fn new(fdt: Fdt<'a>, offset: usize) -> Self {
3130
Self {
3231
fdt,
3332
offset,
34-
parent_address_cells: DEFAULT_ADDRESS_CELLS,
35-
parent_size_cells: DEFAULT_SIZE_CELLS,
33+
parent_address_space: AddressSpaceProperties::default(),
3634
}
3735
}
3836

@@ -259,8 +257,7 @@ enum FdtChildIter<'a> {
259257
Running {
260258
fdt: Fdt<'a>,
261259
offset: usize,
262-
address_cells: u32,
263-
size_cells: u32,
260+
address_space: AddressSpaceProperties,
264261
},
265262
Error,
266263
}
@@ -271,11 +268,7 @@ impl<'a> Iterator for FdtChildIter<'a> {
271268
fn next(&mut self) -> Option<Self::Item> {
272269
match self {
273270
Self::Start { node } => {
274-
let address_cells = match node.address_cells() {
275-
Ok(value) => value,
276-
Err(e) => return Some(Err(e)),
277-
};
278-
let size_cells = match node.size_cells() {
271+
let address_space = match node.address_space() {
279272
Ok(value) => value,
280273
Err(e) => return Some(Err(e)),
281274
};
@@ -292,17 +285,15 @@ impl<'a> Iterator for FdtChildIter<'a> {
292285
*self = Self::Running {
293286
fdt: node.fdt,
294287
offset,
295-
address_cells,
296-
size_cells,
288+
address_space,
297289
};
298290
self.next()
299291
}
300292
Self::Running {
301293
fdt,
302294
offset,
303-
address_cells,
304-
size_cells,
305-
} => match Self::try_next(*fdt, offset, *address_cells, *size_cells) {
295+
address_space,
296+
} => match Self::try_next(*fdt, offset, *address_space) {
306297
Some(Ok(val)) => Some(Ok(val)),
307298
Some(Err(e)) => {
308299
*self = Self::Error;
@@ -319,8 +310,7 @@ impl<'a> FdtChildIter<'a> {
319310
fn try_next(
320311
fdt: Fdt<'a>,
321312
offset: &mut usize,
322-
parent_address_cells: u32,
323-
parent_size_cells: u32,
313+
parent_address_space: AddressSpaceProperties,
324314
) -> Option<Result<FdtNode<'a>, FdtParseError>> {
325315
loop {
326316
let token = match fdt.read_token(*offset) {
@@ -337,8 +327,7 @@ impl<'a> FdtChildIter<'a> {
337327
return Some(Ok(FdtNode {
338328
fdt,
339329
offset: node_offset,
340-
parent_address_cells,
341-
parent_size_cells,
330+
parent_address_space,
342331
}));
343332
}
344333
FdtToken::Prop => {

src/fdt/property.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ impl<'a> Iterator for FdtStringListIterator<'a> {
319319
/// An integer value split into several big-endian u32 parts.
320320
///
321321
/// This is generally used in prop-encoded-array properties.
322-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
323-
pub struct Cells<'a>(pub &'a [big_endian::U32]);
322+
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
323+
pub struct Cells<'a>(pub(crate) &'a [big_endian::U32]);
324324

325325
impl Cells<'_> {
326326
/// Converts the value to the given integer type.
@@ -329,13 +329,11 @@ impl Cells<'_> {
329329
///
330330
/// Returns `FdtError::TooManyCells` if the value has too many cells to fit
331331
/// in the given type.
332-
pub fn to_intsize<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>(
332+
pub fn to_int<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>(
333333
self,
334-
field: &'static str,
335334
) -> Result<T, FdtError> {
336335
if size_of::<T>() < self.0.len() * size_of::<u32>() {
337336
Err(FdtError::TooManyCells {
338-
field,
339337
cells: self.0.len(),
340338
})
341339
} else if let [size] = self.0 {

src/standard.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ impl<'a> FdtNode<'a> {
139139
})
140140
}
141141

142+
/// Returns the values of the standard `#address-cells` and `#size_cells`
143+
/// properties.
144+
///
145+
/// # Errors
146+
///
147+
/// Returns an error if a property's name or value cannot be read, or the
148+
/// values aren't valid u32s.
149+
pub fn address_space(&self) -> Result<AddressSpaceProperties, FdtParseError> {
150+
Ok(AddressSpaceProperties {
151+
address_cells: self.address_cells()?,
152+
size_cells: self.size_cells()?,
153+
})
154+
}
155+
142156
/// Returns the value of the standard `reg` property.
143157
///
144158
/// # Errors
@@ -147,8 +161,8 @@ impl<'a> FdtNode<'a> {
147161
/// size of the value isn't a multiple of the expected number of address and
148162
/// size cells.
149163
pub fn reg(&self) -> Result<Option<impl Iterator<Item = Reg<'a>> + use<'a>>, FdtError> {
150-
let address_cells = self.parent_address_cells as usize;
151-
let size_cells = self.parent_size_cells as usize;
164+
let address_cells = self.parent_address_space.address_cells as usize;
165+
let size_cells = self.parent_address_space.size_cells as usize;
152166
Ok(if let Some(property) = self.property("reg")? {
153167
Some(
154168
property
@@ -186,7 +200,7 @@ impl<'a> FdtNode<'a> {
186200
property
187201
.as_prop_encoded_array([
188202
self.address_cells()? as usize,
189-
self.parent_address_cells as usize,
203+
self.parent_address_space.address_cells as usize,
190204
self.size_cells()? as usize,
191205
])?
192206
.map(Range::from_cells),
@@ -210,7 +224,7 @@ impl<'a> FdtNode<'a> {
210224
property
211225
.as_prop_encoded_array([
212226
self.address_cells()? as usize,
213-
self.parent_address_cells as usize,
227+
self.parent_address_space.address_cells as usize,
214228
self.size_cells()? as usize,
215229
])?
216230
.map(Range::from_cells),
@@ -229,3 +243,21 @@ impl<'a> FdtNode<'a> {
229243
Ok(self.property("dma-coherent")?.is_some())
230244
}
231245
}
246+
247+
/// The `#address-cells` and `#size-cells` properties of a node.
248+
#[derive(Debug, Clone, Copy)]
249+
pub struct AddressSpaceProperties {
250+
/// The `#address-cells` property.
251+
pub address_cells: u32,
252+
/// The `#size-cells` property.
253+
pub size_cells: u32,
254+
}
255+
256+
impl Default for AddressSpaceProperties {
257+
fn default() -> Self {
258+
Self {
259+
address_cells: DEFAULT_ADDRESS_CELLS,
260+
size_cells: DEFAULT_SIZE_CELLS,
261+
}
262+
}
263+
}

src/standard/memory.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,20 @@ pub struct InitialMappedArea {
8888
}
8989

9090
impl InitialMappedArea {
91+
/// Creates an `InitialMappedArea` from an array of three `Cells` containing
92+
/// the effective address, physical address and size respectively.
93+
///
94+
/// These `Cells` must contain 2, 2 and 1 cells respectively, or the method
95+
/// will panic.
9196
#[expect(
9297
clippy::unwrap_used,
9398
reason = "The Cells passed are always the correct size"
9499
)]
95100
fn from_cells([ea, pa, size]: [Cells; 3]) -> Self {
96101
Self {
97-
effective_address: ea.to_intsize("effective address").unwrap(),
98-
physical_address: pa.to_intsize("physical address").unwrap(),
99-
size: size.to_intsize("size").unwrap(),
102+
effective_address: ea.to_int().unwrap(),
103+
physical_address: pa.to_int().unwrap(),
104+
size: size.to_int().unwrap(),
100105
}
101106
}
102107
}

src/standard/ranges.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a> Range<'a> {
4646
>(
4747
&self,
4848
) -> Result<T, FdtError> {
49-
self.child_bus_address.to_intsize("child-bus-address")
49+
self.child_bus_address.to_int()
5050
}
5151

5252
/// Attempts to return the parent-bus-address as the given type, if it will
@@ -61,7 +61,7 @@ impl<'a> Range<'a> {
6161
>(
6262
&self,
6363
) -> Result<T, FdtError> {
64-
self.parent_bus_address.to_intsize("parent-bus-address")
64+
self.parent_bus_address.to_int()
6565
}
6666

6767
/// Attempts to return the length as the given type, if it will fit.
@@ -72,7 +72,7 @@ impl<'a> Range<'a> {
7272
pub fn length<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>(
7373
&self,
7474
) -> Result<T, FdtError> {
75-
self.length.to_intsize("length")
75+
self.length.to_int()
7676
}
7777
}
7878

src/standard/reg.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'a> Reg<'a> {
3434
pub fn address<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>(
3535
self,
3636
) -> Result<T, FdtError> {
37-
self.address.to_intsize("address")
37+
self.address.to_int()
3838
}
3939

4040
/// Attempts to return the size as the given type, if it will fit.
@@ -45,7 +45,7 @@ impl<'a> Reg<'a> {
4545
pub fn size<T: Default + From<u32> + Shl<usize, Output = T> + BitOr<Output = T>>(
4646
self,
4747
) -> Result<T, FdtError> {
48-
self.size.to_intsize("size")
48+
self.size.to_int()
4949
}
5050
}
5151

@@ -80,10 +80,7 @@ mod tests {
8080
};
8181
assert_eq!(
8282
reg.address::<u32>(),
83-
Err(FdtError::TooManyCells {
84-
field: "address",
85-
cells: 2
86-
})
83+
Err(FdtError::TooManyCells { cells: 2 })
8784
);
8885
assert_eq!(reg.address::<u64>(), Ok(0x1234_5678_abcd_0000));
8986
assert_eq!(reg.size::<u32>(), Ok(0x1122_3344));

tests/fdt.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use dtoolkit::fdt::{Cells, Fdt};
9+
use dtoolkit::fdt::Fdt;
1010
#[cfg(feature = "write")]
1111
use dtoolkit::model::DeviceTree;
12-
use dtoolkit::standard::{InitialMappedArea, Reg, Status};
12+
use dtoolkit::standard::{InitialMappedArea, Status};
1313

1414
#[test]
1515
fn read_child_nodes() {
@@ -135,19 +135,7 @@ fn standard_properties() {
135135
.unwrap()
136136
.unwrap()
137137
.collect::<Vec<_>>();
138-
assert_eq!(
139-
reg,
140-
vec![
141-
Reg {
142-
address: Cells(&[0x1234_5678.into(), 0x3000.into()]),
143-
size: Cells(&[0.into(), 32.into()]),
144-
},
145-
Reg {
146-
address: Cells(&[0.into(), 0xfe00.into()]),
147-
size: Cells(&[0.into(), 256.into()]),
148-
},
149-
]
150-
);
138+
assert_eq!(reg.len(), 2);
151139
assert_eq!(reg[0].address::<u64>().unwrap(), 0x1234_5678_0000_3000);
152140
assert_eq!(reg[0].size::<u64>().unwrap(), 32);
153141
assert_eq!(reg[1].address::<u64>().unwrap(), 0xfe00);
@@ -258,13 +246,6 @@ fn memory() {
258246
assert_eq!(reg.len(), 1);
259247
assert_eq!(reg[0].address::<u32>().unwrap(), 0x8000_0000);
260248
assert_eq!(reg[0].size::<u32>().unwrap(), 0x2000_0000);
261-
assert_eq!(
262-
reg,
263-
vec![Reg {
264-
address: Cells(&[0x8000_0000.into()]),
265-
size: Cells(&[0x2000_0000.into()]),
266-
}]
267-
);
268249
assert!(memory.hotpluggable().unwrap());
269250
assert_eq!(
270251
memory

0 commit comments

Comments
 (0)