efi_selftest.c 9.5 KB

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