1
1
// SPDX-License-Identifier: MIT OR Apache-2.0
2
2
3
- use alloc:: vec:: Vec ;
4
- use uefi:: boot;
5
- use uefi:: mem:: memory_map:: { MemoryMap , MemoryMapMut } ;
6
- use uefi_raw:: table:: boot:: MemoryType ;
7
-
8
3
pub fn test ( ) {
9
4
info ! ( "Testing memory functions" ) ;
10
5
11
6
bootservices:: allocate_pages ( ) ;
12
7
bootservices:: allocate_pool ( ) ;
8
+ bootservices:: test_memory_map ( ) ;
13
9
14
10
global:: alloc_vec ( ) ;
15
11
global:: alloc_alignment ( ) ;
@@ -19,8 +15,10 @@ pub fn test() {
19
15
20
16
/// Tests that directly use UEFI boot services to allocate memory.
21
17
mod bootservices {
18
+ use alloc:: vec:: Vec ;
22
19
use uefi:: boot;
23
20
use uefi:: boot:: AllocateType ;
21
+ use uefi:: mem:: memory_map:: { MemoryMap , MemoryMapMut } ;
24
22
use uefi_raw:: table:: boot:: MemoryType ;
25
23
26
24
/// Tests the `allocate_pages` boot service.
@@ -53,6 +51,44 @@ mod bootservices {
53
51
}
54
52
unsafe { boot:: free_pool ( ptr) } . unwrap ( ) ;
55
53
}
54
+
55
+ /// Tests getting the memory map.
56
+ pub fn test_memory_map ( ) {
57
+ info ! ( "Testing memory map functions" ) ;
58
+
59
+ let mut memory_map =
60
+ boot:: memory_map ( MemoryType :: LOADER_DATA ) . expect ( "Failed to retrieve UEFI memory map" ) ;
61
+
62
+ memory_map. sort ( ) ;
63
+
64
+ // Collect the descriptors into a vector
65
+ let descriptors = memory_map. entries ( ) . copied ( ) . collect :: < Vec < _ > > ( ) ;
66
+
67
+ // Ensured we have at least one entry.
68
+ // Real memory maps usually have dozens of entries.
69
+ assert ! ( !descriptors. is_empty( ) , "Memory map is empty" ) ;
70
+
71
+ let mut curr_value = descriptors[ 0 ] ;
72
+
73
+ for value in descriptors. iter ( ) . skip ( 1 ) {
74
+ if value. phys_start <= curr_value. phys_start {
75
+ panic ! ( "memory map sorting failed" ) ;
76
+ }
77
+ curr_value = * value;
78
+ }
79
+
80
+ // This is pretty much a basic sanity test to ensure returned memory
81
+ // isn't filled with random values.
82
+ let first_desc = descriptors[ 0 ] ;
83
+
84
+ #[ cfg( target_arch = "x86_64" ) ]
85
+ {
86
+ let phys_start = first_desc. phys_start ;
87
+ assert_eq ! ( phys_start, 0 , "Memory does not start at address 0" ) ;
88
+ }
89
+ let page_count = first_desc. page_count ;
90
+ assert ! ( page_count != 0 , "Memory map entry has size zero" ) ;
91
+ }
56
92
}
57
93
58
94
/// Tests that use [`uefi::allocator::Allocator`], which is configured as the
@@ -97,40 +133,3 @@ mod global {
97
133
}
98
134
}
99
135
}
100
-
101
- fn test_memory_map ( ) {
102
- info ! ( "Testing memory map functions" ) ;
103
-
104
- let mut memory_map =
105
- boot:: memory_map ( MemoryType :: LOADER_DATA ) . expect ( "Failed to retrieve UEFI memory map" ) ;
106
-
107
- memory_map. sort ( ) ;
108
-
109
- // Collect the descriptors into a vector
110
- let descriptors = memory_map. entries ( ) . copied ( ) . collect :: < Vec < _ > > ( ) ;
111
-
112
- // Ensured we have at least one entry.
113
- // Real memory maps usually have dozens of entries.
114
- assert ! ( !descriptors. is_empty( ) , "Memory map is empty" ) ;
115
-
116
- let mut curr_value = descriptors[ 0 ] ;
117
-
118
- for value in descriptors. iter ( ) . skip ( 1 ) {
119
- if value. phys_start <= curr_value. phys_start {
120
- panic ! ( "memory map sorting failed" ) ;
121
- }
122
- curr_value = * value;
123
- }
124
-
125
- // This is pretty much a basic sanity test to ensure returned memory
126
- // isn't filled with random values.
127
- let first_desc = descriptors[ 0 ] ;
128
-
129
- #[ cfg( target_arch = "x86_64" ) ]
130
- {
131
- let phys_start = first_desc. phys_start ;
132
- assert_eq ! ( phys_start, 0 , "Memory does not start at address 0" ) ;
133
- }
134
- let page_count = first_desc. page_count ;
135
- assert ! ( page_count != 0 , "Memory map entry has size zero" ) ;
136
- }
0 commit comments