|  | 
| 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::memory_map(); | 
| 13 | 9 | 
 | 
| 14 | 10 |     global::alloc_vec(); | 
| 15 | 11 |     global::alloc_alignment(); | 
| 16 |  | - | 
| 17 |  | -    test_memory_map(); | 
| 18 | 12 | } | 
| 19 | 13 | 
 | 
| 20 | 14 | /// Tests that directly use UEFI boot services to allocate memory. | 
| 21 | 15 | mod bootservices { | 
|  | 16 | +    use alloc::vec::Vec; | 
| 22 | 17 |     use uefi::boot; | 
| 23 | 18 |     use uefi::boot::AllocateType; | 
|  | 19 | +    use uefi::mem::memory_map::{MemoryMap, MemoryMapMut}; | 
| 24 | 20 |     use uefi_raw::table::boot::MemoryType; | 
| 25 | 21 | 
 | 
| 26 | 22 |     /// Tests the `allocate_pages` boot service. | 
| @@ -53,6 +49,44 @@ mod bootservices { | 
| 53 | 49 |         } | 
| 54 | 50 |         unsafe { boot::free_pool(ptr) }.unwrap(); | 
| 55 | 51 |     } | 
|  | 52 | + | 
|  | 53 | +    /// Tests getting the memory map and performing a few sanity checks on it. | 
|  | 54 | +    pub fn memory_map() { | 
|  | 55 | +        info!("Testing memory map functions"); | 
|  | 56 | + | 
|  | 57 | +        let mut memory_map = | 
|  | 58 | +            boot::memory_map(MemoryType::LOADER_DATA).expect("Failed to retrieve UEFI memory map"); | 
|  | 59 | + | 
|  | 60 | +        memory_map.sort(); | 
|  | 61 | + | 
|  | 62 | +        // Collect the descriptors into a vector | 
|  | 63 | +        let descriptors = memory_map.entries().copied().collect::<Vec<_>>(); | 
|  | 64 | + | 
|  | 65 | +        // Ensured we have at least one entry. | 
|  | 66 | +        // Real memory maps usually have dozens of entries. | 
|  | 67 | +        assert!(!descriptors.is_empty(), "Memory map is empty"); | 
|  | 68 | + | 
|  | 69 | +        let mut curr_value = descriptors[0]; | 
|  | 70 | + | 
|  | 71 | +        for value in descriptors.iter().skip(1) { | 
|  | 72 | +            if value.phys_start <= curr_value.phys_start { | 
|  | 73 | +                panic!("memory map sorting failed"); | 
|  | 74 | +            } | 
|  | 75 | +            curr_value = *value; | 
|  | 76 | +        } | 
|  | 77 | + | 
|  | 78 | +        // This is pretty much a basic sanity test to ensure returned memory | 
|  | 79 | +        // isn't filled with random values. | 
|  | 80 | +        let first_desc = descriptors[0]; | 
|  | 81 | + | 
|  | 82 | +        #[cfg(target_arch = "x86_64")] | 
|  | 83 | +        { | 
|  | 84 | +            let phys_start = first_desc.phys_start; | 
|  | 85 | +            assert_eq!(phys_start, 0, "Memory does not start at address 0"); | 
|  | 86 | +        } | 
|  | 87 | +        let page_count = first_desc.page_count; | 
|  | 88 | +        assert!(page_count != 0, "Memory map entry has size zero"); | 
|  | 89 | +    } | 
| 56 | 90 | } | 
| 57 | 91 | 
 | 
| 58 | 92 | /// Tests that use [`uefi::allocator::Allocator`], which is configured as the | 
| @@ -97,40 +131,3 @@ mod global { | 
| 97 | 131 |         } | 
| 98 | 132 |     } | 
| 99 | 133 | } | 
| 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