Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * EFI efi_selftest |
| 3 | * |
| 4 | * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <efi_selftest.h> |
| 10 | #include <vsprintf.h> |
| 11 | |
Heinrich Schuchardt | 1f66a12 | 2017-10-18 18:13:12 +0200 | [diff] [blame^] | 12 | /* |
| 13 | * Constants for test step bitmap |
| 14 | */ |
| 15 | #define EFI_ST_SETUP 1 |
| 16 | #define EFI_ST_EXECUTE 2 |
| 17 | #define EFI_ST_TEARDOWN 4 |
| 18 | |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 19 | static const struct efi_system_table *systable; |
| 20 | static const struct efi_boot_services *boottime; |
| 21 | static const struct efi_runtime_services *runtime; |
| 22 | static efi_handle_t handle; |
| 23 | static u16 reset_message[] = L"Selftest completed"; |
| 24 | |
| 25 | /* |
| 26 | * Exit the boot services. |
| 27 | * |
| 28 | * The size of the memory map is determined. |
| 29 | * Pool memory is allocated to copy the memory map. |
| 30 | * The memory amp is copied and the map key is obtained. |
| 31 | * The map key is used to exit the boot services. |
| 32 | */ |
| 33 | void efi_st_exit_boot_services(void) |
| 34 | { |
Heinrich Schuchardt | fd4a1c4 | 2017-10-18 18:13:10 +0200 | [diff] [blame] | 35 | unsigned long map_size = 0; |
| 36 | unsigned long map_key; |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 37 | unsigned long desc_size; |
| 38 | u32 desc_version; |
| 39 | efi_status_t ret; |
| 40 | struct efi_mem_desc *memory_map; |
| 41 | |
| 42 | ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size, |
| 43 | &desc_version); |
| 44 | if (ret != EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 45 | efi_st_error( |
| 46 | "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n"); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | /* Allocate extra space for newly allocated memory */ |
| 50 | map_size += sizeof(struct efi_mem_desc); |
| 51 | ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size, |
| 52 | (void **)&memory_map); |
| 53 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 54 | efi_st_error("AllocatePool did not return EFI_SUCCESS\n"); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | ret = boottime->get_memory_map(&map_size, memory_map, &map_key, |
| 58 | &desc_size, &desc_version); |
| 59 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 60 | efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n"); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | ret = boottime->exit_boot_services(handle, map_key); |
| 64 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 65 | efi_st_error("ExitBootServices did not return EFI_SUCCESS\n"); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | efi_st_printf("\nBoot services terminated\n"); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Set up a test. |
| 73 | * |
| 74 | * @test the test to be executed |
| 75 | * @failures counter that will be incremented if a failure occurs |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 76 | * @return EFI_ST_SUCCESS for success |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 77 | */ |
| 78 | static int setup(struct efi_unit_test *test, unsigned int *failures) |
| 79 | { |
| 80 | int ret; |
| 81 | |
| 82 | if (!test->setup) |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 83 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 84 | efi_st_printf("\nSetting up '%s'\n", test->name); |
| 85 | ret = test->setup(handle, systable); |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 86 | if (ret != EFI_ST_SUCCESS) { |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 87 | efi_st_error("Setting up '%s' failed\n", test->name); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 88 | ++*failures; |
| 89 | } else { |
| 90 | efi_st_printf("Setting up '%s' succeeded\n", test->name); |
| 91 | } |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Execute a test. |
| 97 | * |
| 98 | * @test the test to be executed |
| 99 | * @failures counter that will be incremented if a failure occurs |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 100 | * @return EFI_ST_SUCCESS for success |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 101 | */ |
| 102 | static int execute(struct efi_unit_test *test, unsigned int *failures) |
| 103 | { |
| 104 | int ret; |
| 105 | |
| 106 | if (!test->execute) |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 107 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 108 | efi_st_printf("\nExecuting '%s'\n", test->name); |
| 109 | ret = test->execute(); |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 110 | if (ret != EFI_ST_SUCCESS) { |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 111 | efi_st_error("Executing '%s' failed\n", test->name); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 112 | ++*failures; |
| 113 | } else { |
| 114 | efi_st_printf("Executing '%s' succeeded\n", test->name); |
| 115 | } |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Tear down a test. |
| 121 | * |
| 122 | * @test the test to be torn down |
| 123 | * @failures counter that will be incremented if a failure occurs |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 124 | * @return EFI_ST_SUCCESS for success |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 125 | */ |
| 126 | static int teardown(struct efi_unit_test *test, unsigned int *failures) |
| 127 | { |
| 128 | int ret; |
| 129 | |
| 130 | if (!test->teardown) |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 131 | return EFI_ST_SUCCESS; |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 132 | efi_st_printf("\nTearing down '%s'\n", test->name); |
| 133 | ret = test->teardown(); |
Heinrich Schuchardt | e67e724 | 2017-10-04 15:31:26 +0200 | [diff] [blame] | 134 | if (ret != EFI_ST_SUCCESS) { |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 135 | efi_st_error("Tearing down '%s' failed\n", test->name); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 136 | ++*failures; |
| 137 | } else { |
| 138 | efi_st_printf("Tearing down '%s' succeeded\n", test->name); |
| 139 | } |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | /* |
Heinrich Schuchardt | 1f66a12 | 2017-10-18 18:13:12 +0200 | [diff] [blame^] | 144 | * Execute test steps of one phase. |
| 145 | * |
| 146 | * @phase test phase |
| 147 | * @steps steps to execute |
| 148 | * failures returns EFI_ST_SUCCESS if all test steps succeeded |
| 149 | */ |
| 150 | void efi_st_do_tests(unsigned int phase, unsigned int steps, |
| 151 | unsigned int *failures) |
| 152 | { |
| 153 | struct efi_unit_test *test; |
| 154 | |
| 155 | for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); |
| 156 | test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { |
| 157 | if (test->phase != phase) |
| 158 | continue; |
| 159 | if (steps & EFI_ST_SETUP) |
| 160 | setup(test, failures); |
| 161 | if (steps & EFI_ST_EXECUTE) |
| 162 | execute(test, failures); |
| 163 | if (steps & EFI_ST_TEARDOWN) |
| 164 | teardown(test, failures); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 169 | * Execute selftest of the EFI API |
| 170 | * |
| 171 | * This is the main entry point of the EFI selftest application. |
| 172 | * |
| 173 | * All tests use a driver model and are run in three phases: |
| 174 | * setup, execute, teardown. |
| 175 | * |
| 176 | * A test may be setup and executed at boottime, |
| 177 | * it may be setup at boottime and executed at runtime, |
| 178 | * or it may be setup and executed at runtime. |
| 179 | * |
| 180 | * After executing all tests the system is reset. |
| 181 | * |
| 182 | * @image_handle: handle of the loaded EFI image |
| 183 | * @systab: EFI system table |
| 184 | */ |
| 185 | efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle, |
| 186 | struct efi_system_table *systab) |
| 187 | { |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 188 | unsigned int failures = 0; |
| 189 | |
| 190 | systable = systab; |
| 191 | boottime = systable->boottime; |
| 192 | runtime = systable->runtime; |
| 193 | handle = image_handle; |
| 194 | con_out = systable->con_out; |
| 195 | con_in = systable->con_in; |
| 196 | |
| 197 | efi_st_printf("\nTesting EFI API implementation\n"); |
| 198 | |
| 199 | efi_st_printf("\nNumber of tests to execute: %u\n", |
| 200 | ll_entry_count(struct efi_unit_test, efi_unit_test)); |
| 201 | |
| 202 | /* Execute boottime tests */ |
Heinrich Schuchardt | 1f66a12 | 2017-10-18 18:13:12 +0200 | [diff] [blame^] | 203 | efi_st_do_tests(EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 204 | EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN, |
| 205 | &failures); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 206 | |
| 207 | /* Execute mixed tests */ |
Heinrich Schuchardt | 1f66a12 | 2017-10-18 18:13:12 +0200 | [diff] [blame^] | 208 | efi_st_do_tests(EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
| 209 | EFI_ST_SETUP, &failures); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 210 | |
| 211 | efi_st_exit_boot_services(); |
| 212 | |
Heinrich Schuchardt | 1f66a12 | 2017-10-18 18:13:12 +0200 | [diff] [blame^] | 213 | efi_st_do_tests(EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
| 214 | EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 215 | |
| 216 | /* Execute runtime tests */ |
Heinrich Schuchardt | 1f66a12 | 2017-10-18 18:13:12 +0200 | [diff] [blame^] | 217 | efi_st_do_tests(EFI_SETUP_AFTER_BOOTTIME_EXIT, |
| 218 | EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN, |
| 219 | &failures); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 220 | |
| 221 | /* Give feedback */ |
| 222 | efi_st_printf("\nSummary: %u failures\n\n", failures); |
| 223 | |
| 224 | /* Reset system */ |
| 225 | efi_st_printf("Preparing for reset. Press any key.\n"); |
| 226 | efi_st_get_key(); |
| 227 | runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY, |
| 228 | sizeof(reset_message), reset_message); |
Heinrich Schuchardt | 037ee6f | 2017-10-04 12:37:02 +0200 | [diff] [blame] | 229 | efi_st_printf("\n"); |
| 230 | efi_st_error("Reset failed.\n"); |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 231 | |
| 232 | return EFI_UNSUPPORTED; |
| 233 | } |