efi_selftest.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. */
  65. static int setup(struct efi_unit_test *test, unsigned int *failures)
  66. {
  67. int ret;
  68. if (!test->setup)
  69. return 0;
  70. efi_st_printf("\nSetting up '%s'\n", test->name);
  71. ret = test->setup(handle, systable);
  72. if (ret) {
  73. efi_st_error("Setting up '%s' failed\n", test->name);
  74. ++*failures;
  75. } else {
  76. efi_st_printf("Setting up '%s' succeeded\n", test->name);
  77. }
  78. return ret;
  79. }
  80. /*
  81. * Execute a test.
  82. *
  83. * @test the test to be executed
  84. * @failures counter that will be incremented if a failure occurs
  85. */
  86. static int execute(struct efi_unit_test *test, unsigned int *failures)
  87. {
  88. int ret;
  89. if (!test->execute)
  90. return 0;
  91. efi_st_printf("\nExecuting '%s'\n", test->name);
  92. ret = test->execute();
  93. if (ret) {
  94. efi_st_error("Executing '%s' failed\n", test->name);
  95. ++*failures;
  96. } else {
  97. efi_st_printf("Executing '%s' succeeded\n", test->name);
  98. }
  99. return ret;
  100. }
  101. /*
  102. * Tear down a test.
  103. *
  104. * @test the test to be torn down
  105. * @failures counter that will be incremented if a failure occurs
  106. */
  107. static int teardown(struct efi_unit_test *test, unsigned int *failures)
  108. {
  109. int ret;
  110. if (!test->teardown)
  111. return 0;
  112. efi_st_printf("\nTearing down '%s'\n", test->name);
  113. ret = test->teardown();
  114. if (ret) {
  115. efi_st_error("Tearing down '%s' failed\n", test->name);
  116. ++*failures;
  117. } else {
  118. efi_st_printf("Tearing down '%s' succeeded\n", test->name);
  119. }
  120. return ret;
  121. }
  122. /*
  123. * Execute selftest of the EFI API
  124. *
  125. * This is the main entry point of the EFI selftest application.
  126. *
  127. * All tests use a driver model and are run in three phases:
  128. * setup, execute, teardown.
  129. *
  130. * A test may be setup and executed at boottime,
  131. * it may be setup at boottime and executed at runtime,
  132. * or it may be setup and executed at runtime.
  133. *
  134. * After executing all tests the system is reset.
  135. *
  136. * @image_handle: handle of the loaded EFI image
  137. * @systab: EFI system table
  138. */
  139. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  140. struct efi_system_table *systab)
  141. {
  142. struct efi_unit_test *test;
  143. unsigned int failures = 0;
  144. systable = systab;
  145. boottime = systable->boottime;
  146. runtime = systable->runtime;
  147. handle = image_handle;
  148. con_out = systable->con_out;
  149. con_in = systable->con_in;
  150. efi_st_printf("\nTesting EFI API implementation\n");
  151. efi_st_printf("\nNumber of tests to execute: %u\n",
  152. ll_entry_count(struct efi_unit_test, efi_unit_test));
  153. /* Execute boottime tests */
  154. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  155. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  156. if (test->phase == EFI_EXECUTE_BEFORE_BOOTTIME_EXIT) {
  157. setup(test, &failures);
  158. execute(test, &failures);
  159. teardown(test, &failures);
  160. }
  161. }
  162. /* Execute mixed tests */
  163. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  164. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  165. if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT)
  166. setup(test, &failures);
  167. }
  168. efi_st_exit_boot_services();
  169. for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
  170. test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
  171. if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT) {
  172. execute(test, &failures);
  173. teardown(test, &failures);
  174. }
  175. }
  176. /* Execute runtime tests */
  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 (test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT) {
  180. setup(test, &failures);
  181. execute(test, &failures);
  182. teardown(test, &failures);
  183. }
  184. }
  185. /* Give feedback */
  186. efi_st_printf("\nSummary: %u failures\n\n", failures);
  187. /* Reset system */
  188. efi_st_printf("Preparing for reset. Press any key.\n");
  189. efi_st_get_key();
  190. runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
  191. sizeof(reset_message), reset_message);
  192. efi_st_printf("\n");
  193. efi_st_error("Reset failed.\n");
  194. return EFI_UNSUPPORTED;
  195. }