@@ -14,6 +14,8 @@ use crate::configurator::{BootConfigurator, BootParams, Error as BootConfigurato
14
14
/// Errors specific to the device tree boot protocol configuration.
15
15
#[ derive( Debug , PartialEq ) ]
16
16
pub enum Error {
17
+ /// FDT does not fit in guest memory.
18
+ FDTPastRamEnd ,
17
19
/// Error writing FDT in memory.
18
20
WriteFDTToMemory ,
19
21
}
@@ -22,6 +24,7 @@ impl StdError for Error {
22
24
fn description ( & self ) -> & str {
23
25
use Error :: * ;
24
26
match self {
27
+ FDTPastRamEnd => "FDT does not fit in guest memory." ,
25
28
WriteFDTToMemory => "Error writing FDT in guest memory." ,
26
29
}
27
30
}
@@ -57,6 +60,10 @@ impl BootConfigurator for FdtBootConfigurator {
57
60
where
58
61
M : GuestMemory ,
59
62
{
63
+ guest_memory
64
+ . checked_offset ( params. header_start , params. header . len ( ) )
65
+ . ok_or ( Error :: FDTPastRamEnd ) ?;
66
+
60
67
// The VMM has filled an FDT and passed it as a `ByteValued` object.
61
68
guest_memory
62
69
. write_slice ( params. header . as_slice ( ) , params. header_start )
@@ -86,23 +93,17 @@ mod tests {
86
93
let guest_memory = create_guest_mem ( ) ;
87
94
88
95
// Error case: FDT doesn't fit in guest memory.
89
- let fdt_addr = guest_memory
90
- . last_addr ( )
91
- . checked_sub ( FDT_MAX_SIZE as u64 - 2 )
92
- . unwrap ( ) ;
96
+ let fdt_addr = GuestAddress ( guest_memory. last_addr ( ) . raw_value ( ) - FDT_MAX_SIZE as u64 + 1 ) ;
93
97
assert_eq ! (
94
98
FdtBootConfigurator :: write_bootparams:: <GuestMemoryMmap >(
95
99
BootParams :: new:: <FdtPlaceholder >( & fdt, fdt_addr) ,
96
100
& guest_memory,
97
101
)
98
102
. err( ) ,
99
- Some ( Error :: WriteFDTToMemory . into( ) )
103
+ Some ( Error :: FDTPastRamEnd . into( ) )
100
104
) ;
101
105
102
- let fdt_addr = guest_memory
103
- . last_addr ( )
104
- . checked_sub ( FDT_MAX_SIZE as u64 - 1 )
105
- . unwrap ( ) ;
106
+ let fdt_addr = GuestAddress ( guest_memory. last_addr ( ) . raw_value ( ) - FDT_MAX_SIZE as u64 ) ;
106
107
assert ! ( FdtBootConfigurator :: write_bootparams:: <GuestMemoryMmap >(
107
108
BootParams :: new:: <FdtPlaceholder >( & fdt, fdt_addr) ,
108
109
& guest_memory,
@@ -112,9 +113,13 @@ mod tests {
112
113
113
114
#[ test]
114
115
fn test_error_messages ( ) {
116
+ assert_eq ! (
117
+ format!( "{}" , Error :: FDTPastRamEnd ) ,
118
+ "Device Tree Boot Configurator Error: FDT does not fit in guest memory."
119
+ ) ;
115
120
assert_eq ! (
116
121
format!( "{}" , Error :: WriteFDTToMemory ) ,
117
122
"Device Tree Boot Configurator Error: Error writing FDT in guest memory."
118
- )
123
+ ) ;
119
124
}
120
125
}
0 commit comments