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