efi_selftest.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /* Constants for test step bitmap */
  10. #define EFI_ST_SETUP 1
  11. #define EFI_ST_EXECUTE 2
  12. #define EFI_ST_TEARDOWN 4
  13. static const struct efi_system_table *systable;
  14. static const struct efi_boot_services *boottime;
  15. static const struct efi_runtime_services *runtime;
  16. static efi_handle_t handle;
  17. static u16 reset_message[] = L"Selftest completed";
  18. static int *setup_status;
  19. /*
  20. * Exit the boot services.
  21. *
  22. * The size of the memory map is determined.
  23. * Pool memory is allocated to copy the memory map.
  24. * The memory map is copied and the map key is obtained.
  25. * The map key is used to exit the boot services.
  26. */
  27. void efi_st_exit_boot_services(void)
  28. {
  29. efi_uintn_t map_size = 0;
  30. efi_uintn_t map_key;
  31. efi_uintn_t desc_size;
  32. u32 desc_version;
  33. efi_status_t ret;
  34. struct efi_mem_desc *memory_map;
  35. ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
  36. &desc_version);
  37. if (ret != EFI_BUFFER_TOO_SMALL) {
  38. efi_st_error(
  39. "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
  40. return;
  41. }
  42. /* Allocate extra space for newly allocated memory */
  43. map_size += sizeof(struct efi_mem_desc);
  44. ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
  45. (void **)&memory_map);
  46. if (ret != EFI_SUCCESS) {
  47. efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
  48. return;
  49. }
  50. ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
  51. &desc_size, &desc_version);
  52. if (ret != EFI_SUCCESS) {
  53. efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
  54. return;
  55. }
  56. ret = boottime->exit_boot_services(handle, map_key);
  57. if (ret != EFI_SUCCESS) {
  58. efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
  59. return;
  60. }
  61. efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
  62. }
  63. /*
  64. * Set up a test.
  65. *
  66. * @test the test to be executed
  67. * @failures counter that will be incremented if a failure occurs
  68. * @return EFI_ST_SUCCESS for success
  69. */
  70. static int setup(struct efi_unit_test *test, unsigned int *failures)
  71. {
  72. int ret;
  73. if (!test->setup)
  74. return EFI_ST_SUCCESS;
  75. efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
  76. ret = test->setup(handle, systable);
  77. if (ret != EFI_ST_SUCCESS) {
  78. efi_st_error("Setting up '%s' failed\n", test->name);
  79. ++*failures;
  80. } else {
  81. efi_st_printc(EFI_LIGHTGREEN,
  82. "Setting up '%s' succeeded\n", test->name);
  83. }
  84. return ret;
  85. }
  86. /*
  87. * Execute a test.
  88. *
  89. * @test the test to be executed
  90. * @failures counter that will be incremented if a failure occurs
  91. * @return EFI_ST_SUCCESS for success
  92. */
  93. static int execute(struct efi_unit_test *test, unsigned int *failures)
  94. {
  95. int ret;
  96. if (!test->execute)
  97. return EFI_ST_SUCCESS;
  98. efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
  99. ret = test->execute();
  100. if (ret != EFI_ST_SUCCESS) {
  101. efi_st_error("Executing '%s' failed\n", test->name);
  102. ++*failures;
  103. } else {
  104. efi_st_printc(EFI_LIGHTGREEN,
  105. "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_printc(EFI_LIGHTBLUE, "\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_printc(EFI_LIGHTGREEN,
  128. "Tearing down '%s' succeeded\n", test->name);
  129. }
  130. return ret;
  131. }
  132. /*
  133. * Check that a test exists.
  134. *
  135. * @testname: name of the test
  136. * @return: test, or NULL if not found
  137. */
  138. static struct efi_unit_test *find_test(const u16 *testname)
  139. {
  140. struct efi_unit_test *test;
  141. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  142. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  143. if (!efi_st_strcmp_16_8(testname, test->name))
  144. return test;
  145. }
  146. efi_st_printf("\nTest '%ps' not found\n", testname);
  147. return NULL;
  148. }
  149. /*
  150. * List all available tests.
  151. */
  152. static void list_all_tests(void)
  153. {
  154. struct efi_unit_test *test;
  155. /* List all tests */
  156. efi_st_printf("\nAvailable tests:\n");
  157. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  158. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  159. efi_st_printf("'%s'%s\n", test->name,
  160. test->on_request ? " - on request" : "");
  161. }
  162. }
  163. /*
  164. * Execute test steps of one phase.
  165. *
  166. * @testname name of a single selected test or NULL
  167. * @phase test phase
  168. * @steps steps to execute (mask with bits from EFI_ST_...)
  169. * failures returns EFI_ST_SUCCESS if all test steps succeeded
  170. */
  171. void efi_st_do_tests(const u16 *testname, unsigned int phase,
  172. unsigned int steps, unsigned int *failures)
  173. {
  174. int i = 0;
  175. struct efi_unit_test *test;
  176. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  177. test < ll_entry_end(struct efi_unit_test, efi_unit_test);
  178. ++test, ++i) {
  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_status[i] = setup(test, failures);
  186. if (steps & EFI_ST_EXECUTE && setup_status[i] == 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. /* Allocate buffer for setup results */
  253. ret = boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) *
  254. ll_entry_count(struct efi_unit_test,
  255. efi_unit_test),
  256. (void **)&setup_status);
  257. if (ret != EFI_SUCCESS) {
  258. efi_st_error("Allocate pool failed\n");
  259. return ret;
  260. }
  261. /* Execute boottime tests */
  262. efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  263. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  264. &failures);
  265. /* Execute mixed tests */
  266. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  267. EFI_ST_SETUP, &failures);
  268. efi_st_exit_boot_services();
  269. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  270. EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
  271. /* Execute runtime tests */
  272. efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
  273. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  274. &failures);
  275. /* Give feedback */
  276. efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
  277. /* Reset system */
  278. efi_st_printf("Preparing for reset. Press any key...\n");
  279. efi_st_get_key();
  280. runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
  281. sizeof(reset_message), reset_message);
  282. efi_st_printf("\n");
  283. efi_st_error("Reset failed\n");
  284. return EFI_UNSUPPORTED;
  285. }