Skip to content

Commit f0c9f37

Browse files
committed
uefi: add memory map unit test based on real-world data
1 parent f8d5670 commit f0c9f37

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

uefi/src/table/boot.rs

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ impl<'a> HandleBuffer<'a> {
21592159
pub struct ProtocolSearchKey(NonNull<c_void>);
21602160

21612161
#[cfg(test)]
2162-
mod tests {
2162+
mod tests_mmap_artificial {
21632163
use core::mem::{size_of, size_of_val};
21642164

21652165
use crate::table::boot::{MemoryAttribute, MemoryMap, MemoryType};
@@ -2290,3 +2290,138 @@ mod tests {
22902290
true
22912291
}
22922292
}
2293+
2294+
#[cfg(test)]
2295+
mod tests_mmap_real {
2296+
use super::*;
2297+
use core::mem::size_of;
2298+
2299+
const MMAP_META: MemoryMapMeta = MemoryMapMeta {
2300+
map_size: MMAP_RAW.len() * size_of::<u64>(),
2301+
desc_size: 48,
2302+
map_key: MemoryMapKey(0),
2303+
desc_version: 1,
2304+
};
2305+
/// Sample with 10 entries of a real UEFI memory map extracted from our
2306+
/// UEFI test runner.
2307+
const MMAP_RAW: [u64; 60] = [
2308+
3, 0, 0, 1, 15, 0, 7, 4096, 0, 134, 15, 0, 4, 552960, 0, 1, 15, 0, 7, 557056, 0, 24, 15, 0,
2309+
7, 1048576, 0, 1792, 15, 0, 10, 8388608, 0, 8, 15, 0, 7, 8421376, 0, 3, 15, 0, 10, 8433664,
2310+
0, 1, 15, 0, 7, 8437760, 0, 4, 15, 0, 10, 8454144, 0, 240, 15, 0,
2311+
];
2312+
extern crate std;
2313+
#[test]
2314+
fn basic_functionality() {
2315+
let mut buf = MMAP_RAW;
2316+
let buf =
2317+
unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<u8>(), MMAP_META.map_size) };
2318+
let mut mmap = MemoryMap::from_raw(buf, MMAP_META.desc_size);
2319+
mmap.sort();
2320+
2321+
let entries = mmap.entries().copied().collect::<Vec<_>>();
2322+
2323+
let expected = [
2324+
MemoryDescriptor {
2325+
ty: MemoryType::BOOT_SERVICES_CODE,
2326+
phys_start: 0x0,
2327+
virt_start: 0x0,
2328+
page_count: 0x1,
2329+
att: MemoryAttribute::UNCACHEABLE
2330+
| MemoryAttribute::WRITE_COMBINE
2331+
| MemoryAttribute::WRITE_THROUGH
2332+
| MemoryAttribute::WRITE_BACK,
2333+
},
2334+
MemoryDescriptor {
2335+
ty: MemoryType::CONVENTIONAL,
2336+
phys_start: 0x1000,
2337+
virt_start: 0x0,
2338+
page_count: 0x86,
2339+
att: MemoryAttribute::UNCACHEABLE
2340+
| MemoryAttribute::WRITE_COMBINE
2341+
| MemoryAttribute::WRITE_THROUGH
2342+
| MemoryAttribute::WRITE_BACK,
2343+
},
2344+
MemoryDescriptor {
2345+
ty: MemoryType::BOOT_SERVICES_DATA,
2346+
phys_start: 0x87000,
2347+
virt_start: 0x0,
2348+
page_count: 0x1,
2349+
att: MemoryAttribute::UNCACHEABLE
2350+
| MemoryAttribute::WRITE_COMBINE
2351+
| MemoryAttribute::WRITE_THROUGH
2352+
| MemoryAttribute::WRITE_BACK,
2353+
},
2354+
MemoryDescriptor {
2355+
ty: MemoryType::CONVENTIONAL,
2356+
phys_start: 0x88000,
2357+
virt_start: 0x0,
2358+
page_count: 0x18,
2359+
att: MemoryAttribute::UNCACHEABLE
2360+
| MemoryAttribute::WRITE_COMBINE
2361+
| MemoryAttribute::WRITE_THROUGH
2362+
| MemoryAttribute::WRITE_BACK,
2363+
},
2364+
MemoryDescriptor {
2365+
ty: MemoryType::CONVENTIONAL,
2366+
phys_start: 0x100000,
2367+
virt_start: 0x0,
2368+
page_count: 0x700,
2369+
att: MemoryAttribute::UNCACHEABLE
2370+
| MemoryAttribute::WRITE_COMBINE
2371+
| MemoryAttribute::WRITE_THROUGH
2372+
| MemoryAttribute::WRITE_BACK,
2373+
},
2374+
MemoryDescriptor {
2375+
ty: MemoryType::ACPI_NON_VOLATILE,
2376+
phys_start: 0x800000,
2377+
virt_start: 0x0,
2378+
page_count: 0x8,
2379+
att: MemoryAttribute::UNCACHEABLE
2380+
| MemoryAttribute::WRITE_COMBINE
2381+
| MemoryAttribute::WRITE_THROUGH
2382+
| MemoryAttribute::WRITE_BACK,
2383+
},
2384+
MemoryDescriptor {
2385+
ty: MemoryType::CONVENTIONAL,
2386+
phys_start: 0x808000,
2387+
virt_start: 0x0,
2388+
page_count: 0x3,
2389+
att: MemoryAttribute::UNCACHEABLE
2390+
| MemoryAttribute::WRITE_COMBINE
2391+
| MemoryAttribute::WRITE_THROUGH
2392+
| MemoryAttribute::WRITE_BACK,
2393+
},
2394+
MemoryDescriptor {
2395+
ty: MemoryType::ACPI_NON_VOLATILE,
2396+
phys_start: 0x80b000,
2397+
virt_start: 0x0,
2398+
page_count: 0x1,
2399+
att: MemoryAttribute::UNCACHEABLE
2400+
| MemoryAttribute::WRITE_COMBINE
2401+
| MemoryAttribute::WRITE_THROUGH
2402+
| MemoryAttribute::WRITE_BACK,
2403+
},
2404+
MemoryDescriptor {
2405+
ty: MemoryType::CONVENTIONAL,
2406+
phys_start: 0x80c000,
2407+
virt_start: 0x0,
2408+
page_count: 0x4,
2409+
att: MemoryAttribute::UNCACHEABLE
2410+
| MemoryAttribute::WRITE_COMBINE
2411+
| MemoryAttribute::WRITE_THROUGH
2412+
| MemoryAttribute::WRITE_BACK,
2413+
},
2414+
MemoryDescriptor {
2415+
ty: MemoryType::ACPI_NON_VOLATILE,
2416+
phys_start: 0x810000,
2417+
virt_start: 0x0,
2418+
page_count: 0xf0,
2419+
att: MemoryAttribute::UNCACHEABLE
2420+
| MemoryAttribute::WRITE_COMBINE
2421+
| MemoryAttribute::WRITE_THROUGH
2422+
| MemoryAttribute::WRITE_BACK,
2423+
},
2424+
];
2425+
assert_eq!(entries.as_slice(), &expected);
2426+
}
2427+
}

0 commit comments

Comments
 (0)