@@ -11,6 +11,7 @@ use core::ops::Deref;
1111
1212use crate :: error:: { PropertyError , StandardError } ;
1313use crate :: fdt:: { Fdt , FdtNode } ;
14+ use crate :: standard:: Reg ;
1415use crate :: { Cells , Node , Property } ;
1516
1617impl < ' a > Fdt < ' a > {
@@ -27,6 +28,16 @@ impl<'a> Fdt<'a> {
2728 . ok_or ( StandardError :: MemoryMissing ) ?;
2829 Ok ( Memory { node } )
2930 }
31+
32+ /// Returns the `/reserved-memory/*` nodes, if any.
33+ #[ must_use]
34+ pub fn reserved_memory ( self ) -> Option < impl Iterator < Item = ReservedMemory < FdtNode < ' a > > > > {
35+ Some (
36+ self . find_node ( "/reserved-memory" ) ?
37+ . children ( )
38+ . map ( |node| ReservedMemory { node } ) ,
39+ )
40+ }
3041}
3142
3243/// Typed wrapper for a `/memory` node.
@@ -107,3 +118,92 @@ impl InitialMappedArea {
107118 }
108119 }
109120}
121+
122+ /// Typed wrapper for a `/reserved-memory/*` node.
123+ #[ derive( Clone , Copy , Debug ) ]
124+ pub struct ReservedMemory < N > {
125+ node : N ,
126+ }
127+
128+ impl < N > Deref for ReservedMemory < N > {
129+ type Target = N ;
130+
131+ fn deref ( & self ) -> & Self :: Target {
132+ & self . node
133+ }
134+ }
135+
136+ impl < N : Display > Display for ReservedMemory < N > {
137+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
138+ self . node . fmt ( f)
139+ }
140+ }
141+
142+ impl < ' a , N : Node < ' a > > ReservedMemory < N > {
143+ /// Returns the value of the standard `size` property of the reserved memory
144+ /// node, if it is present.
145+ ///
146+ /// # Errors
147+ ///
148+ /// Returns an error if the value of the property isn't a multiple of 4
149+ /// bytes long.
150+ pub fn size ( & self ) -> Result < Option < Cells < ' a > > , PropertyError > {
151+ self . node
152+ . property ( "size" )
153+ . map ( |value| value. as_cells ( ) )
154+ . transpose ( )
155+ }
156+
157+ /// Returns the value of the standard `alignment` property of the reserved
158+ /// memory node, if it is present.
159+ ///
160+ /// # Errors
161+ ///
162+ /// Returns an error if the value of the property isn't a multiple of 4
163+ /// bytes long.
164+ pub fn alignment ( & self ) -> Result < Option < Cells < ' a > > , PropertyError > {
165+ self . node
166+ . property ( "alignment" )
167+ . map ( |value| value. as_cells ( ) )
168+ . transpose ( )
169+ }
170+
171+ /// Returns whether the standard `no-map` property is present.
172+ pub fn no_map ( & self ) -> bool {
173+ self . node . property ( "no-map" ) . is_some ( )
174+ }
175+
176+ /// Returns whether the standard `no-map-fixup` property is present.
177+ pub fn no_map_fixup ( & self ) -> bool {
178+ self . node . property ( "no-map-fixup" ) . is_some ( )
179+ }
180+
181+ /// Returns whether the standard `reusable` property is present.
182+ pub fn reusable ( & self ) -> bool {
183+ self . node . property ( "reusable" ) . is_some ( )
184+ }
185+ }
186+
187+ impl < ' a > ReservedMemory < FdtNode < ' a > > {
188+ /// Returns the value of the standard `alloc-ranges` property.
189+ ///
190+ /// # Errors
191+ ///
192+ /// Returns an error if the size of the value isn't a multiple of the
193+ /// expected number of address and size cells.
194+ pub fn alloc_ranges (
195+ & self ,
196+ ) -> Result < Option < impl Iterator < Item = Reg < ' a > > + use < ' a > > , StandardError > {
197+ let address_cells = self . node . parent_address_space . address_cells as usize ;
198+ let size_cells = self . node . parent_address_space . size_cells as usize ;
199+ if let Some ( property) = self . property ( "alloc_ranges" ) {
200+ Ok ( Some (
201+ property
202+ . as_prop_encoded_array ( [ address_cells, size_cells] ) ?
203+ . map ( Reg :: from_cells) ,
204+ ) )
205+ } else {
206+ Ok ( None )
207+ }
208+ }
209+ }
0 commit comments