Skip to content

Commit 8cccd2d

Browse files
committed
uefi: add memory map unit test based on real-world data
1 parent 67c2778 commit 8cccd2d

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

uefi/src/table/boot.rs

+136-1
Original file line numberDiff line numberDiff line change
@@ -2155,7 +2155,7 @@ impl<'a> HandleBuffer<'a> {
21552155
pub struct ProtocolSearchKey(NonNull<c_void>);
21562156

21572157
#[cfg(test)]
2158-
mod tests {
2158+
mod tests_mmap_artifical {
21592159
use core::mem::{size_of, size_of_val};
21602160

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

0 commit comments

Comments
 (0)