efi_selftest.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. 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. int ret;
  74. if (!test->setup)
  75. return EFI_ST_SUCCESS;
  76. efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
  77. ret = test->setup(handle, systable);
  78. if (ret != EFI_ST_SUCCESS) {
  79. efi_st_error("Setting up '%s' failed\n", test->name);
  80. ++*failures;
  81. } else {
  82. efi_st_printc(EFI_LIGHTGREEN,
  83. "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_printc(EFI_LIGHTBLUE, "\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_printc(EFI_LIGHTGREEN,
  106. "Executing '%s' succeeded\n", test->name);
  107. }
  108. return ret;
  109. }
  110. /*
  111. * Tear down a test.
  112. *
  113. * @test the test to be torn down
  114. * @failures counter that will be incremented if a failure occurs
  115. * @return EFI_ST_SUCCESS for success
  116. */
  117. static int teardown(struct efi_unit_test *test, unsigned int *failures)
  118. {
  119. int ret;
  120. if (!test->teardown)
  121. return EFI_ST_SUCCESS;
  122. efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
  123. ret = test->teardown();
  124. if (ret != EFI_ST_SUCCESS) {
  125. efi_st_error("Tearing down '%s' failed\n", test->name);
  126. ++*failures;
  127. } else {
  128. efi_st_printc(EFI_LIGHTGREEN,
  129. "Tearing down '%s' succeeded\n", test->name);
  130. }
  131. return ret;
  132. }
  133. /*
  134. * Check that a test requiring reset exists.
  135. *
  136. * @testname: name of the test
  137. * @return: test, or NULL if not found
  138. */
  139. static bool need_reset(const u16 *testname)
  140. {
  141. struct efi_unit_test *test;
  142. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  143. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  144. if (testname && efi_st_strcmp_16_8(testname, test->name))
  145. continue;
  146. if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT ||
  147. test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT)
  148. return true;
  149. }
  150. return false;
  151. }
  152. /*
  153. * Check that a test exists.
  154. *
  155. * @testname: name of the test
  156. * @return: test, or NULL if not found
  157. */
  158. static struct efi_unit_test *find_test(const u16 *testname)
  159. {
  160. struct efi_unit_test *test;
  161. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  162. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  163. if (!efi_st_strcmp_16_8(testname, test->name))
  164. return test;
  165. }
  166. efi_st_printf("\nTest '%ps' not found\n", testname);
  167. return NULL;
  168. }
  169. /*
  170. * List all available tests.
  171. */
  172. static void list_all_tests(void)
  173. {
  174. struct efi_unit_test *test;
  175. /* List all tests */
  176. efi_st_printf("\nAvailable tests:\n");
  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. efi_st_printf("'%s'%s\n", test->name,
  180. test->on_request ? " - on request" : "");
  181. }
  182. }
  183. /*
  184. * Execute test steps of one phase.
  185. *
  186. * @testname name of a single selected test or NULL
  187. * @phase test phase
  188. * @steps steps to execute (mask with bits from EFI_ST_...)
  189. * failures returns EFI_ST_SUCCESS if all test steps succeeded
  190. */
  191. void efi_st_do_tests(const u16 *testname, unsigned int phase,
  192. unsigned int steps, unsigned int *failures)
  193. {
  194. int i = 0;
  195. struct efi_unit_test *test;
  196. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  197. test < ll_entry_end(struct efi_unit_test, efi_unit_test);
  198. ++test, ++i) {
  199. if (testname ?
  200. efi_st_strcmp_16_8(testname, test->name) : test->on_request)
  201. continue;
  202. if (test->phase != phase)
  203. continue;
  204. if (steps & EFI_ST_SETUP)
  205. setup_status[i] = setup(test, failures);
  206. if (steps & EFI_ST_EXECUTE && setup_status[i] == EFI_ST_SUCCESS)
  207. execute(test, failures);
  208. if (steps & EFI_ST_TEARDOWN)
  209. teardown(test, failures);
  210. }
  211. }
  212. /*
  213. * Execute selftest of the EFI API
  214. *
  215. * This is the main entry point of the EFI selftest application.
  216. *
  217. * All tests use a driver model and are run in three phases:
  218. * setup, execute, teardown.
  219. *
  220. * A test may be setup and executed at boottime,
  221. * it may be setup at boottime and executed at runtime,
  222. * or it may be setup and executed at runtime.
  223. *
  224. * After executing all tests the system is reset.
  225. *
  226. * @image_handle: handle of the loaded EFI image
  227. * @systab: EFI system table
  228. */
  229. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  230. struct efi_system_table *systab)
  231. {
  232. unsigned int failures = 0;
  233. const u16 *testname = NULL;
  234. struct efi_loaded_image *loaded_image;
  235. efi_status_t ret;
  236. systable = systab;
  237. boottime = systable->boottime;
  238. runtime = systable->runtime;
  239. handle = image_handle;
  240. con_out = systable->con_out;
  241. con_in = systable->con_in;
  242. ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
  243. (void **)&loaded_image);
  244. if (ret != EFI_SUCCESS) {
  245. efi_st_error("Cannot open loaded image protocol\n");
  246. return ret;
  247. }
  248. if (loaded_image->load_options)
  249. testname = (u16 *)loaded_image->load_options;
  250. if (testname) {
  251. if (!efi_st_strcmp_16_8(testname, "list") ||
  252. !find_test(testname)) {
  253. list_all_tests();
  254. /*
  255. * TODO:
  256. * Once the Exit boottime service is correctly
  257. * implemented we should call
  258. * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
  259. * here, cf.
  260. * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
  261. */
  262. return EFI_SUCCESS;
  263. }
  264. }
  265. efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
  266. if (testname)
  267. efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
  268. else
  269. efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
  270. ll_entry_count(struct efi_unit_test,
  271. efi_unit_test));
  272. /* Allocate buffer for setup results */
  273. ret = boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) *
  274. ll_entry_count(struct efi_unit_test,
  275. efi_unit_test),
  276. (void **)&setup_status);
  277. if (ret != EFI_SUCCESS) {
  278. efi_st_error("Allocate pool failed\n");
  279. return ret;
  280. }
  281. /* Execute boottime tests */
  282. efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  283. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  284. &failures);
  285. if (!need_reset(testname)) {
  286. if (failures)
  287. ret = EFI_PROTOCOL_ERROR;
  288. /* Give feedback */
  289. efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n",
  290. failures);
  291. return ret;
  292. }
  293. /* Execute mixed tests */
  294. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  295. EFI_ST_SETUP, &failures);
  296. efi_st_exit_boot_services();
  297. efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  298. EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
  299. /* Execute runtime tests */
  300. efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
  301. EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
  302. &failures);
  303. /* Give feedback */
  304. efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
  305. /* Reset system */
  306. efi_st_printf("Preparing for reset. Press any key...\n");
  307. efi_st_get_key();
  308. if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) {
  309. runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
  310. sizeof(reset_message), reset_message);
  311. } else {
  312. efi_restore_gd();
  313. do_reset(NULL, 0, 0, NULL);
  314. }
  315. efi_st_printf("\n");
  316. efi_st_error("Reset failed\n");
  317. return EFI_UNSUPPORTED;
  318. }