efi_selftest.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. static const struct efi_system_table *systable;
  11. static const struct efi_boot_services *boottime;
  12. static const struct efi_runtime_services *runtime;
  13. static efi_handle_t handle;
  14. static u16 reset_message[] = L"Selftest completed";
  15. /*
  16. * Exit the boot services.
  17. *
  18. * The size of the memory map is determined.
  19. * Pool memory is allocated to copy the memory map.
  20. * The memory amp is copied and the map key is obtained.
  21. * The map key is used to exit the boot services.
  22. */
  23. void efi_st_exit_boot_services(void)
  24. {
  25. unsigned long map_size = 0;
  26. unsigned long map_key;
  27. unsigned long desc_size;
  28. u32 desc_version;
  29. efi_status_t ret;
  30. struct efi_mem_desc *memory_map;
  31. ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
  32. &desc_version);
  33. if (ret != EFI_BUFFER_TOO_SMALL) {
  34. efi_st_error(
  35. "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
  36. return;
  37. }
  38. /* Allocate extra space for newly allocated memory */
  39. map_size += sizeof(struct efi_mem_desc);
  40. ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
  41. (void **)&memory_map);
  42. if (ret != EFI_SUCCESS) {
  43. efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
  44. return;
  45. }
  46. ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
  47. &desc_size, &desc_version);
  48. if (ret != EFI_SUCCESS) {
  49. efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
  50. return;
  51. }
  52. ret = boottime->exit_boot_services(handle, map_key);
  53. if (ret != EFI_SUCCESS) {
  54. efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
  55. return;
  56. }
  57. efi_st_printf("\nBoot services terminated\n");
  58. }
  59. /*
  60. * Set up a test.
  61. *
  62. * @test the test to be executed
  63. * @failures counter that will be incremented if a failure occurs
  64. * @return EFI_ST_SUCCESS for success
  65. */
  66. static int setup(struct efi_unit_test *test, unsigned int *failures)
  67. {
  68. int ret;
  69. if (!test->setup)
  70. return EFI_ST_SUCCESS;
  71. efi_st_printf("\nSetting up '%s'\n", test->name);
  72. ret = test->setup(handle, systable);
  73. if (ret != EFI_ST_SUCCESS) {
  74. efi_st_error("Setting up '%s' failed\n", test->name);
  75. ++*failures;
  76. } else {
  77. efi_st_printf("Setting up '%s' succeeded\n", test->name);
  78. }
  79. return ret;
  80. }
  81. /*
  82. * Execute a test.
  83. *
  84. * @test the test to be executed
  85. * @failures counter that will be incremented if a failure occurs
  86. * @return EFI_ST_SUCCESS for success
  87. */
  88. static int execute(struct efi_unit_test *test, unsigned int *failures)
  89. {
  90. int ret;
  91. if (!test->execute)
  92. return EFI_ST_SUCCESS;
  93. efi_st_printf("\nExecuting '%s'\n", test->name);
  94. ret = test->execute();
  95. if (ret != EFI_ST_SUCCESS) {
  96. efi_st_error("Executing '%s' failed\n", test->name);
  97. ++*failures;
  98. } else {
  99. efi_st_printf("Executing '%s' succeeded\n", test->name);
  100. }
  101. return ret;
  102. }
  103. /*
  104. * Tear down a test.
  105. *
  106. * @test the test to be torn down
  107. * @failures counter that will be incremented if a failure occurs
  108. * @return EFI_ST_SUCCESS for success
  109. */
  110. static int teardown(struct efi_unit_test *test, unsigned int *failures)
  111. {
  112. int ret;
  113. if (!test->teardown)
  114. return EFI_ST_SUCCESS;
  115. efi_st_printf("\nTearing down '%s'\n", test->name);
  116. ret = test->teardown();
  117. if (ret != EFI_ST_SUCCESS) {
  118. efi_st_error("Tearing down '%s' failed\n", test->name);
  119. ++*failures;
  120. } else {
  121. efi_st_printf("Tearing down '%s' succeeded\n", test->name);
  122. }
  123. return ret;
  124. }
  125. /*
  126. * Execute selftest of the EFI API
  127. *
  128. * This is the main entry point of the EFI selftest application.
  129. *
  130. * All tests use a driver model and are run in three phases:
  131. * setup, execute, teardown.
  132. *
  133. * A test may be setup and executed at boottime,
  134. * it may be setup at boottime and executed at runtime,
  135. * or it may be setup and executed at runtime.
  136. *
  137. * After executing all tests the system is reset.
  138. *
  139. * @image_handle: handle of the loaded EFI image
  140. * @systab: EFI system table
  141. */
  142. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  143. struct efi_system_table *systab)
  144. {
  145. struct efi_unit_test *test;
  146. unsigned int failures = 0;
  147. systable = systab;
  148. boottime = systable->boottime;
  149. runtime = systable->runtime;
  150. handle = image_handle;
  151. con_out = systable->con_out;
  152. con_in = systable->con_in;
  153. efi_st_printf("\nTesting EFI API implementation\n");
  154. efi_st_printf("\nNumber of tests to execute: %u\n",
  155. ll_entry_count(struct efi_unit_test, efi_unit_test));
  156. /* Execute boottime tests */
  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. if (test->phase == EFI_EXECUTE_BEFORE_BOOTTIME_EXIT) {
  160. setup(test, &failures);
  161. execute(test, &failures);
  162. teardown(test, &failures);
  163. }
  164. }
  165. /* Execute mixed tests */
  166. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  167. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  168. if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT)
  169. setup(test, &failures);
  170. }
  171. efi_st_exit_boot_services();
  172. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  173. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  174. if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT) {
  175. execute(test, &failures);
  176. teardown(test, &failures);
  177. }
  178. }
  179. /* Execute runtime tests */
  180. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  181. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  182. if (test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT) {
  183. setup(test, &failures);
  184. execute(test, &failures);
  185. teardown(test, &failures);
  186. }
  187. }
  188. /* Give feedback */
  189. efi_st_printf("\nSummary: %u failures\n\n", failures);
  190. /* Reset system */
  191. efi_st_printf("Preparing for reset. Press any key.\n");
  192. efi_st_get_key();
  193. runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
  194. sizeof(reset_message), reset_message);
  195. efi_st_printf("\n");
  196. efi_st_error("Reset failed.\n");
  197. return EFI_UNSUPPORTED;
  198. }