Skip to content

Commit bb63cd3

Browse files
committed
uefi-test-runner: streamline
1 parent 04e87f7 commit bb63cd3

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

uefi-test-runner/src/boot/memory.rs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

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-
83
pub fn test() {
94
info!("Testing memory functions");
105

116
bootservices::allocate_pages();
127
bootservices::allocate_pool();
8+
bootservices::test_memory_map();
139

1410
global::alloc_vec();
1511
global::alloc_alignment();
@@ -19,8 +15,10 @@ pub fn test() {
1915

2016
/// Tests that directly use UEFI boot services to allocate memory.
2117
mod bootservices {
18+
use alloc::vec::Vec;
2219
use uefi::boot;
2320
use uefi::boot::AllocateType;
21+
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};
2422
use uefi_raw::table::boot::MemoryType;
2523

2624
/// Tests the `allocate_pages` boot service.
@@ -53,6 +51,44 @@ mod bootservices {
5351
}
5452
unsafe { boot::free_pool(ptr) }.unwrap();
5553
}
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+
}
5692
}
5793

5894
/// Tests that use [`uefi::allocator::Allocator`], which is configured as the
@@ -97,40 +133,3 @@ mod global {
97133
}
98134
}
99135
}
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

Comments
 (0)