efi_selftest.c 8.1 KB

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