efi_selftest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #include <efi_selftest.h>
  9. #include <vsprintf.h>
  10. /*
  11. * Constants for test step bitmap
  12. */
  13. #define EFI_ST_SETUP 1
  14. #define EFI_ST_EXECUTE 2
  15. #define EFI_ST_TEARDOWN 4
  16. static const struct efi_system_table *systable;
  17. static const struct efi_boot_services *boottime;
  18. static const struct efi_runtime_services *runtime;
  19. static efi_handle_t handle;
  20. static u16 reset_message[] = L"Selftest completed";
  21. /*
  22. * Exit the boot services.
  23. *
  24. * The size of the memory map is determined.
  25. * Pool memory is allocated to copy the memory map.
  26. * The memory amp is copied and the map key is obtained.
  27. * The map key is used to exit the boot services.
  28. */
  29. void efi_st_exit_boot_services(void)
  30. {
  31. efi_uintn_t map_size = 0;
  32. efi_uintn_t map_key;
  33. efi_uintn_t desc_size;
  34. u32 desc_version;
  35. efi_status_t ret;
  36. struct efi_mem_desc *memory_map;
  37. ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
  38. &desc_version);
  39. if (ret != EFI_BUFFER_TOO_SMALL) {
  40. efi_st_error(
  41. "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
  42. return;
  43. }
  44. /* Allocate extra space for newly allocated memory */
  45. map_size += sizeof(struct efi_mem_desc);
  46. ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
  47. (void **)&memory_map);
  48. if (ret != EFI_SUCCESS) {
  49. efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
  50. return;
  51. }
  52. ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
  53. &desc_size, &desc_version);
  54. if (ret != EFI_SUCCESS) {
  55. efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
  56. return;
  57. }
  58. ret = boottime->exit_boot_services(handle, map_key);
  59. if (ret != EFI_SUCCESS) {
  60. efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
  61. return;
  62. }
  63. efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
  64. }
  65. /*
  66. * Set up a test.
  67. *
  68. * @test the test to be executed
  69. * @failures counter that will be incremented if a failure occurs
  70. * @return EFI_ST_SUCCESS for success
  71. */
  72. static int setup(struct efi_unit_test *test, unsigned int *failures)
  73. {
  74. if (!test->setup) {
  75. test->setup_ok = EFI_ST_SUCCESS;
  76. return EFI_ST_SUCCESS;
  77. }
  78. efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
  79. test->setup_ok = test->setup(handle, systable);
  80. if (test->setup_ok != 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 test->setup_ok;
  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 exists.
  137. *
  138. * @testname: name of the test
  139. * @return: test
  140. */
  141. static struct efi_unit_test *find_test(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 (!efi_st_strcmp_16_8(testname, test->name))
  147. return test;
  148. }
  149. efi_st_printf("\nTest '%ps' not found\n", testname);
  150. return NULL;
  151. }
  152. /*
  153. * List all available tests.
  154. */
  155. static void list_all_tests(void)
  156. {
  157. struct efi_unit_test *test;
  158. /* List all tests */
  159. efi_st_printf("\nAvailable tests:\n");
  160. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  161. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  162. efi_st_printf("'%s'%s\n", test->name,
  163. test->on_request ? " - on request" : "");
  164. }
  165. }
  166. /*
  167. * Execute test steps of one phase.
  168. *
  169. * @testname name of a single selected test or NULL
  170. * @phase test phase
  171. * @steps steps to execute
  172. * failures returns EFI_ST_SUCCESS if all test steps succeeded
  173. */
  174. void efi_st_do_tests(const u16 *testname, unsigned int phase,
  175. unsigned int steps, unsigned int *failures)
  176. {
  177. struct efi_unit_test *test;
  178. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  179. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  180. if (testname ?
  181. efi_st_strcmp_16_8(testname, test->name) : test->on_request)
  182. continue;
  183. if (test->phase != phase)
  184. continue;
  185. if (steps & EFI_ST_SETUP)
  186. setup(test, failures);
  187. if (steps & EFI_ST_EXECUTE && test->setup_ok == EFI_ST_SUCCESS)
  188. execute(test, failures);
  189. if (steps & EFI_ST_TEARDOWN)
  190. teardown(test, failures);
  191. }
  192. }
  193. /*
  194. * Execute selftest of the EFI API
  195. *
  196. * This is the main entry point of the EFI selftest application.
  197. *
  198. * All tests use a driver model and are run in three phases:
  199. * setup, execute, teardown.
  200. *
  201. * A test may be setup and executed at boottime,
  202. * it may be setup at boottime and executed at runtime,
  203. * or it may be setup and executed at runtime.
  204. *
  205. * After executing all tests the system is reset.
  206. *
  207. * @image_handle: handle of the loaded EFI image
  208. * @systab: EFI system table
  209. */
  210. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  211. struct efi_system_table *systab)
  212. {
  213. unsigned int failures = 0;
  214. const u16 *testname = NULL;
  215. struct efi_loaded_image *loaded_image;
  216. efi_status_t ret;
  217. systable = systab;
  218. boottime = systable->boottime;
  219. runtime = systable->runtime;
  220. handle = image_handle;
  221. con_out = systable->con_out;
  222. con_in = systable->con_in;
  223. ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
  224. (void **)&loaded_image);
  225. if (ret != EFI_SUCCESS) {
  226. efi_st_error("Cannot open loaded image protocol\n");
  227. return ret;
  228. }
  229. if (loaded_image->load_options)
  230. testname = (u16 *)loaded_image->load_options;
  231. if (testname) {
  232. if (!efi_st_strcmp_16_8(testname, "list") ||
  233. !find_test(testname)) {
  234. list_all_tests();
  235. /*
  236. * TODO:
  237. * Once the Exit boottime service is correctly
  238. * implemented we should call
  239. * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
  240. * here, cf.
  241. * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
  242. */
  243. return EFI_SUCCESS;
  244. }
  245. }
  246. efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
  247. if (testname)
  248. efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
  249. else
  250. efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
  251. ll_entry_count(struct efi_unit_test,
  252. efi_unit_test));
  253. /* Execute boottime tests */
  254. efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  255. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  256. &failures);
  257. /* Execute mixed tests */
  258. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  259. EFI_ST_SETUP, &failures);
  260. efi_st_exit_boot_services();
  261. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  262. EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
  263. /* Execute runtime tests */
  264. efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
  265. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  266. &failures);
  267. /* Give feedback */
  268. efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
  269. /* Reset system */
  270. efi_st_printf("Preparing for reset. Press any key.\n");
  271. efi_st_get_key();
  272. runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
  273. sizeof(reset_message), reset_message);
  274. efi_st_printf("\n");
  275. efi_st_error("Reset failed.\n");
  276. return EFI_UNSUPPORTED;
  277. }