efi_selftest.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_printf("\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. int ret;
  75. if (!test->setup)
  76. return EFI_ST_SUCCESS;
  77. efi_st_printf("\nSetting up '%s'\n", test->name);
  78. ret = test->setup(handle, systable);
  79. if (ret != EFI_ST_SUCCESS) {
  80. efi_st_error("Setting up '%s' failed\n", test->name);
  81. ++*failures;
  82. } else {
  83. efi_st_printf("Setting up '%s' succeeded\n", test->name);
  84. }
  85. return ret;
  86. }
  87. /*
  88. * Execute a test.
  89. *
  90. * @test the test to be executed
  91. * @failures counter that will be incremented if a failure occurs
  92. * @return EFI_ST_SUCCESS for success
  93. */
  94. static int execute(struct efi_unit_test *test, unsigned int *failures)
  95. {
  96. int ret;
  97. if (!test->execute)
  98. return EFI_ST_SUCCESS;
  99. efi_st_printf("\nExecuting '%s'\n", test->name);
  100. ret = test->execute();
  101. if (ret != EFI_ST_SUCCESS) {
  102. efi_st_error("Executing '%s' failed\n", test->name);
  103. ++*failures;
  104. } else {
  105. efi_st_printf("Executing '%s' succeeded\n", test->name);
  106. }
  107. return ret;
  108. }
  109. /*
  110. * Tear down a test.
  111. *
  112. * @test the test to be torn down
  113. * @failures counter that will be incremented if a failure occurs
  114. * @return EFI_ST_SUCCESS for success
  115. */
  116. static int teardown(struct efi_unit_test *test, unsigned int *failures)
  117. {
  118. int ret;
  119. if (!test->teardown)
  120. return EFI_ST_SUCCESS;
  121. efi_st_printf("\nTearing down '%s'\n", test->name);
  122. ret = test->teardown();
  123. if (ret != EFI_ST_SUCCESS) {
  124. efi_st_error("Tearing down '%s' failed\n", test->name);
  125. ++*failures;
  126. } else {
  127. efi_st_printf("Tearing down '%s' succeeded\n", test->name);
  128. }
  129. return ret;
  130. }
  131. /*
  132. * Check that a test exists.
  133. *
  134. * @testname: name of the test
  135. * @return: test
  136. */
  137. static struct efi_unit_test *find_test(const u16 *testname)
  138. {
  139. struct efi_unit_test *test;
  140. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  141. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  142. if (!efi_st_strcmp_16_8(testname, test->name))
  143. return test;
  144. }
  145. efi_st_printf("\nTest '%ps' not found\n", testname);
  146. return NULL;
  147. }
  148. /*
  149. * List all available tests.
  150. */
  151. static void list_all_tests(void)
  152. {
  153. struct efi_unit_test *test;
  154. /* List all tests */
  155. efi_st_printf("\nAvailable tests:\n");
  156. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  157. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  158. efi_st_printf("'%s'%s\n", test->name,
  159. test->on_request ? " - on request" : "");
  160. }
  161. }
  162. /*
  163. * Execute test steps of one phase.
  164. *
  165. * @testname name of a single selected test or NULL
  166. * @phase test phase
  167. * @steps steps to execute
  168. * failures returns EFI_ST_SUCCESS if all test steps succeeded
  169. */
  170. void efi_st_do_tests(const u16 *testname, unsigned int phase,
  171. unsigned int steps, unsigned int *failures)
  172. {
  173. struct efi_unit_test *test;
  174. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  175. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  176. if (testname ?
  177. efi_st_strcmp_16_8(testname, test->name) : test->on_request)
  178. continue;
  179. if (test->phase != phase)
  180. continue;
  181. if (steps & EFI_ST_SETUP)
  182. setup(test, failures);
  183. if (steps & EFI_ST_EXECUTE)
  184. execute(test, failures);
  185. if (steps & EFI_ST_TEARDOWN)
  186. teardown(test, failures);
  187. }
  188. }
  189. /*
  190. * Execute selftest of the EFI API
  191. *
  192. * This is the main entry point of the EFI selftest application.
  193. *
  194. * All tests use a driver model and are run in three phases:
  195. * setup, execute, teardown.
  196. *
  197. * A test may be setup and executed at boottime,
  198. * it may be setup at boottime and executed at runtime,
  199. * or it may be setup and executed at runtime.
  200. *
  201. * After executing all tests the system is reset.
  202. *
  203. * @image_handle: handle of the loaded EFI image
  204. * @systab: EFI system table
  205. */
  206. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  207. struct efi_system_table *systab)
  208. {
  209. unsigned int failures = 0;
  210. const u16 *testname = NULL;
  211. struct efi_loaded_image *loaded_image;
  212. efi_status_t ret;
  213. systable = systab;
  214. boottime = systable->boottime;
  215. runtime = systable->runtime;
  216. handle = image_handle;
  217. con_out = systable->con_out;
  218. con_in = systable->con_in;
  219. ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
  220. (void **)&loaded_image);
  221. if (ret != EFI_SUCCESS) {
  222. efi_st_error("Cannot open loaded image protocol\n");
  223. return ret;
  224. }
  225. if (loaded_image->load_options)
  226. testname = (u16 *)loaded_image->load_options;
  227. if (testname) {
  228. if (!efi_st_strcmp_16_8(testname, "list") ||
  229. !find_test(testname)) {
  230. list_all_tests();
  231. /*
  232. * TODO:
  233. * Once the Exit boottime service is correctly
  234. * implemented we should call
  235. * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
  236. * here, cf.
  237. * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
  238. */
  239. return EFI_SUCCESS;
  240. }
  241. }
  242. efi_st_printf("\nTesting EFI API implementation\n");
  243. if (testname)
  244. efi_st_printf("\nSelected test: '%ps'\n", testname);
  245. else
  246. efi_st_printf("\nNumber of tests to execute: %u\n",
  247. ll_entry_count(struct efi_unit_test,
  248. efi_unit_test));
  249. /* Execute boottime tests */
  250. efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  251. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  252. &failures);
  253. /* Execute mixed tests */
  254. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  255. EFI_ST_SETUP, &failures);
  256. efi_st_exit_boot_services();
  257. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  258. EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
  259. /* Execute runtime tests */
  260. efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
  261. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  262. &failures);
  263. /* Give feedback */
  264. efi_st_printf("\nSummary: %u failures\n\n", failures);
  265. /* Reset system */
  266. efi_st_printf("Preparing for reset. Press any key.\n");
  267. efi_st_get_key();
  268. runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
  269. sizeof(reset_message), reset_message);
  270. efi_st_printf("\n");
  271. efi_st_error("Reset failed.\n");
  272. return EFI_UNSUPPORTED;
  273. }