Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | // SPDX-License-Identifier: GPL-2.0+ /* * EFI efi_selftest * * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> */ #include <command.h> #include <efi_selftest.h> #include <vsprintf.h> /* Constants for test step bitmap */ #define EFI_ST_SETUP 1 #define EFI_ST_EXECUTE 2 #define EFI_ST_TEARDOWN 4 const struct efi_system_table *st_systable; const struct efi_boot_services *st_boottime; static const struct efi_runtime_services *runtime; static efi_handle_t handle; static u16 reset_message[] = u"Selftest completed"; static int *setup_status; /* * Exit the boot services. * * The size of the memory map is determined. * Pool memory is allocated to copy the memory map. * The memory map is copied and the map key is obtained. * The map key is used to exit the boot services. */ void efi_st_exit_boot_services(void) { efi_uintn_t map_size = 0; efi_uintn_t map_key; efi_uintn_t desc_size; u32 desc_version; efi_status_t ret; struct efi_mem_desc *memory_map; /* Do not detach devices in ExitBootServices. We need the console. */ efi_st_keep_devices = true; ret = st_boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size, &desc_version); if (ret != EFI_BUFFER_TOO_SMALL) { efi_st_error( "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n"); return; } /* Allocate extra space for newly allocated memory */ map_size += sizeof(struct efi_mem_desc); ret = st_boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size, (void **)&memory_map); if (ret != EFI_SUCCESS) { efi_st_error("AllocatePool did not return EFI_SUCCESS\n"); return; } ret = st_boottime->get_memory_map(&map_size, memory_map, &map_key, &desc_size, &desc_version); if (ret != EFI_SUCCESS) { efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n"); return; } ret = st_boottime->exit_boot_services(handle, map_key); if (ret != EFI_SUCCESS) { efi_st_error("ExitBootServices did not return EFI_SUCCESS\n"); return; } efi_st_printc(EFI_WHITE, "\nBoot services terminated\n"); } /* * Set up a test. * * @test the test to be executed * @failures counter that will be incremented if a failure occurs * Return: EFI_ST_SUCCESS for success */ static int setup(struct efi_unit_test *test, unsigned int *failures) { int ret; if (!test->setup) return EFI_ST_SUCCESS; efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name); ret = test->setup(handle, st_systable); if (ret != EFI_ST_SUCCESS) { efi_st_error("Setting up '%s' failed\n", test->name); ++*failures; } else { efi_st_printc(EFI_LIGHTGREEN, "Setting up '%s' succeeded\n", test->name); } return ret; } /* * Execute a test. * * @test the test to be executed * @failures counter that will be incremented if a failure occurs * Return: EFI_ST_SUCCESS for success */ static int execute(struct efi_unit_test *test, unsigned int *failures) { int ret; if (!test->execute) return EFI_ST_SUCCESS; efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name); ret = test->execute(); if (ret != EFI_ST_SUCCESS) { efi_st_error("Executing '%s' failed\n", test->name); ++*failures; } else { efi_st_printc(EFI_LIGHTGREEN, "Executing '%s' succeeded\n", test->name); } return ret; } /* * Tear down a test. * * @test the test to be torn down * @failures counter that will be incremented if a failure occurs * Return: EFI_ST_SUCCESS for success */ static int teardown(struct efi_unit_test *test, unsigned int *failures) { int ret; if (!test->teardown) return EFI_ST_SUCCESS; efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name); ret = test->teardown(); if (ret != EFI_ST_SUCCESS) { efi_st_error("Tearing down '%s' failed\n", test->name); ++*failures; } else { efi_st_printc(EFI_LIGHTGREEN, "Tearing down '%s' succeeded\n", test->name); } return ret; } /* * Check that a test requiring reset exists. * * @testname: name of the test * Return: test, or NULL if not found */ static bool need_reset(const u16 *testname) { struct efi_unit_test *test; for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { if (testname && efi_st_strcmp_16_8(testname, test->name)) continue; if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT || test->phase == EFI_SETTING_VIRTUAL_ADDRESS_MAP) return true; } return false; } /* * Check that a test exists. * * @testname: name of the test * Return: test, or NULL if not found */ static struct efi_unit_test *find_test(const u16 *testname) { struct efi_unit_test *test; for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { if (!efi_st_strcmp_16_8(testname, test->name)) return test; } efi_st_printf("\nTest '%ps' not found\n", testname); return NULL; } /* * List all available tests. */ static void list_all_tests(void) { struct efi_unit_test *test; /* List all tests */ efi_st_printf("\nAvailable tests:\n"); for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { efi_st_printf("'%s'%s\n", test->name, test->on_request ? " - on request" : ""); } } /* * Execute test steps of one phase. * * @testname name of a single selected test or NULL * @phase test phase * @steps steps to execute (mask with bits from EFI_ST_...) * failures returns EFI_ST_SUCCESS if all test steps succeeded */ void efi_st_do_tests(const u16 *testname, unsigned int phase, unsigned int steps, unsigned int *failures) { int i = 0; struct efi_unit_test *test; for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test, ++i) { if (testname ? efi_st_strcmp_16_8(testname, test->name) : test->on_request) continue; if (test->phase != phase) continue; if (steps & EFI_ST_SETUP) setup_status[i] = setup(test, failures); if (steps & EFI_ST_EXECUTE && setup_status[i] == EFI_ST_SUCCESS) execute(test, failures); if (steps & EFI_ST_TEARDOWN) teardown(test, failures); } } /* * Execute selftest of the EFI API * * This is the main entry point of the EFI selftest application. * * All tests use a driver model and are run in three phases: * setup, execute, teardown. * * A test may be setup and executed at st_boottime, * it may be setup at st_boottime and executed at runtime, * or it may be setup and executed at runtime. * * After executing all tests the system is reset. * * @image_handle: handle of the loaded EFI image * @systab: EFI system table */ efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle, struct efi_system_table *systab) { unsigned int failures = 0; const u16 *testname = NULL; struct efi_loaded_image *loaded_image; efi_status_t ret; st_systable = systab; st_boottime = st_systable->boottime; runtime = st_systable->runtime; handle = image_handle; con_out = st_systable->con_out; con_in = st_systable->con_in; ret = st_boottime->handle_protocol(image_handle, &efi_guid_loaded_image, (void **)&loaded_image); if (ret != EFI_SUCCESS) { efi_st_error("Cannot open loaded image protocol\n"); return ret; } if (loaded_image->load_options) testname = (u16 *)loaded_image->load_options; if (testname) { if (!efi_st_strcmp_16_8(testname, "list") || !find_test(testname)) { list_all_tests(); /* * TODO: * Once the Exit st_boottime service is correctly * implemented we should call * st_boottime->exit(image_handle, EFI_SUCCESS, 0, NULL); * here, cf. * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html */ return EFI_SUCCESS; } } efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n"); if (testname) efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname); else efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n", ll_entry_count(struct efi_unit_test, efi_unit_test)); /* Allocate buffer for setup results */ ret = st_boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) * ll_entry_count(struct efi_unit_test, efi_unit_test), (void **)&setup_status); if (ret != EFI_SUCCESS) { efi_st_error("Allocate pool failed\n"); return ret; } /* Execute st_boottime tests */ efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures); if (!need_reset(testname)) { if (failures) ret = EFI_PROTOCOL_ERROR; /* Give feedback */ efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures); return ret; } /* Execute mixed tests */ efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT, EFI_ST_SETUP, &failures); efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP, EFI_ST_SETUP, &failures); efi_st_exit_boot_services(); efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT, EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures); /* Execute test setting the virtual address map */ efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP, EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures); /* Give feedback */ efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures); /* Reset system */ efi_st_printf("Preparing for reset. Press any key...\n"); efi_st_get_key(); if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) { runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY, sizeof(reset_message), reset_message); } else { efi_restore_gd(); do_reset(NULL, 0, 0, NULL); } efi_st_printf("\n"); efi_st_error("Reset failed\n"); return EFI_UNSUPPORTED; } |