1
1
use alloc:: vec:: Vec ;
2
- use uefi:: boot;
3
- use uefi:: mem:: memory_map:: { MemoryMap , MemoryMapMut , MemoryMapOwned , MemoryType } ;
4
- use uefi:: table:: boot:: { AllocateType , BootServices } ;
2
+ use uefi:: boot:: { self , AllocateType } ;
3
+ use uefi:: mem:: memory_map:: { MemoryMap , MemoryMapMut , MemoryType } ;
5
4
6
- pub fn test ( bt : & BootServices ) {
5
+ pub fn test ( ) {
7
6
info ! ( "Testing memory functions" ) ;
8
7
9
- test_allocate_pages_freestanding ( ) ;
10
- test_allocate_pool_freestanding ( ) ;
8
+ test_allocate_pages ( ) ;
9
+ test_allocate_pool ( ) ;
11
10
12
- allocate_pages ( bt) ;
13
11
vec_alloc ( ) ;
14
12
alloc_alignment ( ) ;
15
13
16
- memory_map ( bt) ;
17
- memory_map_freestanding ( ) ;
14
+ test_memory_map ( ) ;
18
15
}
19
16
20
- fn test_allocate_pages_freestanding ( ) {
17
+ fn test_allocate_pages ( ) {
21
18
let num_pages = 1 ;
22
19
let ptr =
23
20
boot:: allocate_pages ( AllocateType :: AnyPages , MemoryType :: LOADER_DATA , num_pages) . unwrap ( ) ;
@@ -34,7 +31,7 @@ fn test_allocate_pages_freestanding() {
34
31
unsafe { boot:: free_pages ( ptr, num_pages) } . unwrap ( ) ;
35
32
}
36
33
37
- fn test_allocate_pool_freestanding ( ) {
34
+ fn test_allocate_pool ( ) {
38
35
let ptr = boot:: allocate_pool ( MemoryType :: LOADER_DATA , 10 ) . unwrap ( ) ;
39
36
40
37
// Verify the allocation can be written to.
@@ -46,28 +43,6 @@ fn test_allocate_pool_freestanding() {
46
43
unsafe { boot:: free_pool ( ptr) } . unwrap ( ) ;
47
44
}
48
45
49
- fn allocate_pages ( bt : & BootServices ) {
50
- info ! ( "Allocating some pages of memory" ) ;
51
-
52
- let ty = AllocateType :: AnyPages ;
53
- let mem_ty = MemoryType :: LOADER_DATA ;
54
- let pgs = bt
55
- . allocate_pages ( ty, mem_ty, 1 )
56
- . expect ( "Failed to allocate a page of memory" ) ;
57
-
58
- assert_eq ! ( pgs % 4096 , 0 , "Page pointer is not page-aligned" ) ;
59
-
60
- // Reinterpret the page as an array of bytes
61
- let buf = unsafe { & mut * ( pgs as * mut [ u8 ; 4096 ] ) } ;
62
-
63
- // If these don't fail then we properly allocated some memory.
64
- buf[ 0 ] = 0xF0 ;
65
- buf[ 4095 ] = 0x23 ;
66
-
67
- // Clean up to avoid memory leaks.
68
- unsafe { bt. free_pages ( pgs, 1 ) } . unwrap ( ) ;
69
- }
70
-
71
46
// Simple test to ensure our custom allocator works with the `alloc` crate.
72
47
fn vec_alloc ( ) {
73
48
info ! ( "Allocating a vector through the `alloc` crate" ) ;
@@ -94,7 +69,12 @@ fn alloc_alignment() {
94
69
assert_eq ! ( value. as_ptr( ) as usize % 0x100 , 0 , "Wrong alignment" ) ;
95
70
}
96
71
97
- fn check_memory_map ( mut memory_map : MemoryMapOwned ) {
72
+ fn test_memory_map ( ) {
73
+ info ! ( "Testing memory map functions" ) ;
74
+
75
+ let mut memory_map =
76
+ boot:: memory_map ( MemoryType :: LOADER_DATA ) . expect ( "Failed to retrieve UEFI memory map" ) ;
77
+
98
78
memory_map. sort ( ) ;
99
79
100
80
// Collect the descriptors into a vector
@@ -125,22 +105,3 @@ fn check_memory_map(mut memory_map: MemoryMapOwned) {
125
105
let page_count = first_desc. page_count ;
126
106
assert ! ( page_count != 0 , "Memory map entry has size zero" ) ;
127
107
}
128
-
129
- fn memory_map ( bt : & BootServices ) {
130
- info ! ( "Testing memory map functions" ) ;
131
-
132
- let memory_map = bt
133
- . memory_map ( MemoryType :: LOADER_DATA )
134
- . expect ( "Failed to retrieve UEFI memory map" ) ;
135
-
136
- check_memory_map ( memory_map) ;
137
- }
138
-
139
- fn memory_map_freestanding ( ) {
140
- info ! ( "Testing memory map functions (freestanding)" ) ;
141
-
142
- let memory_map =
143
- boot:: memory_map ( MemoryType :: LOADER_DATA ) . expect ( "Failed to retrieve UEFI memory map" ) ;
144
-
145
- check_memory_map ( memory_map) ;
146
- }
0 commit comments