efi_selftest_open_protocol.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_open_protocol
  4. *
  5. * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This unit test checks that open protocol information is correctly updated
  8. * when calling:
  9. * HandleProtocol, OpenProtocol, OpenProtocolInformation, CloseProtocol.
  10. */
  11. #include <efi_selftest.h>
  12. /*
  13. * The test currently does not actually call the interface function.
  14. * So this is just a dummy structure.
  15. */
  16. struct interface {
  17. void (EFIAPI *inc)(void);
  18. };
  19. static struct efi_boot_services *boottime;
  20. static efi_guid_t guid1 =
  21. EFI_GUID(0x492a0e38, 0x1442, 0xf819,
  22. 0x14, 0xaa, 0x4b, 0x8d, 0x09, 0xfe, 0x5a, 0xb9);
  23. static efi_handle_t handle1;
  24. static struct interface interface1;
  25. /*
  26. * Setup unit test.
  27. *
  28. * Create a handle and install a protocol interface on it.
  29. *
  30. * @handle: handle of the loaded image
  31. * @systable: system table
  32. */
  33. static int setup(const efi_handle_t img_handle,
  34. const struct efi_system_table *systable)
  35. {
  36. efi_status_t ret;
  37. boottime = systable->boottime;
  38. ret = boottime->install_protocol_interface(&handle1, &guid1,
  39. EFI_NATIVE_INTERFACE,
  40. &interface1);
  41. if (ret != EFI_SUCCESS) {
  42. efi_st_error("InstallProtocolInterface failed\n");
  43. return EFI_ST_FAILURE;
  44. }
  45. if (!handle1) {
  46. efi_st_error
  47. ("InstallProtocolInterface failed to create handle\n");
  48. return EFI_ST_FAILURE;
  49. }
  50. return EFI_ST_SUCCESS;
  51. }
  52. /*
  53. * Tear down unit test.
  54. *
  55. */
  56. static int teardown(void)
  57. {
  58. efi_status_t ret;
  59. if (handle1) {
  60. ret = boottime->uninstall_protocol_interface(handle1, &guid1,
  61. &interface1);
  62. if (ret != EFI_SUCCESS) {
  63. efi_st_error("UninstallProtocolInterface failed\n");
  64. return EFI_ST_FAILURE;
  65. }
  66. }
  67. return EFI_ST_SUCCESS;
  68. }
  69. /*
  70. * Execute unit test.
  71. *
  72. * Open the installed protocol twice via HandleProtocol() and once via
  73. * OpenProtocol(EFI_OPEN_PROTOCOL_GET_PROTOCOL). Read the open protocol
  74. * information and check the open counts. Finally close the protocol and
  75. * check again.
  76. */
  77. static int execute(void)
  78. {
  79. void *interface;
  80. struct efi_open_protocol_info_entry *entry_buffer;
  81. efi_uintn_t entry_count;
  82. efi_handle_t firmware_handle;
  83. efi_status_t ret;
  84. ret = boottime->handle_protocol(handle1, &guid1, &interface);
  85. if (ret != EFI_SUCCESS) {
  86. efi_st_error("HandleProtocol failed\n");
  87. return EFI_ST_FAILURE;
  88. }
  89. if (interface != &interface1) {
  90. efi_st_error("HandleProtocol returned wrong interface\n");
  91. return EFI_ST_FAILURE;
  92. }
  93. ret = boottime->open_protocol_information(handle1, &guid1,
  94. &entry_buffer, &entry_count);
  95. if (ret != EFI_SUCCESS) {
  96. efi_st_error("OpenProtocolInformation failed\n");
  97. return EFI_ST_FAILURE;
  98. }
  99. if (entry_count != 1) {
  100. efi_st_error("Incorrect OpenProtocolInformation count\n");
  101. efi_st_printf("Expected 1, got %u\n",
  102. (unsigned int)entry_count);
  103. return EFI_ST_FAILURE;
  104. }
  105. ret = boottime->free_pool(entry_buffer);
  106. if (ret != EFI_SUCCESS) {
  107. efi_st_error("FreePool failed\n");
  108. return EFI_ST_FAILURE;
  109. }
  110. ret = boottime->handle_protocol(handle1, &guid1, &interface);
  111. if (ret != EFI_SUCCESS) {
  112. efi_st_error("HandleProtocol failed\n");
  113. return EFI_ST_FAILURE;
  114. }
  115. ret = boottime->open_protocol_information(handle1, &guid1,
  116. &entry_buffer, &entry_count);
  117. if (ret != EFI_SUCCESS) {
  118. efi_st_error("OpenProtocolInformation failed\n");
  119. return EFI_ST_FAILURE;
  120. }
  121. if (entry_count != 1) {
  122. efi_st_error("Incorrect OpenProtocolInformation count\n");
  123. efi_st_printf("Expected 1, got %u\n",
  124. (unsigned int)entry_count);
  125. return EFI_ST_FAILURE;
  126. }
  127. if (entry_buffer[0].open_count != 2) {
  128. efi_st_error("Incorrect open count: expected 2 got %u\n",
  129. entry_buffer[0].open_count);
  130. return EFI_ST_FAILURE;
  131. }
  132. firmware_handle = entry_buffer[0].agent_handle;
  133. ret = boottime->free_pool(entry_buffer);
  134. if (ret != EFI_SUCCESS) {
  135. efi_st_error("FreePool failed\n");
  136. return EFI_ST_FAILURE;
  137. }
  138. ret = boottime->open_protocol(handle1, &guid1, &interface,
  139. firmware_handle, NULL,
  140. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  141. if (ret != EFI_SUCCESS) {
  142. efi_st_error("OpenProtocol failed\n");
  143. return EFI_ST_FAILURE;
  144. }
  145. ret = boottime->open_protocol_information(handle1, &guid1,
  146. &entry_buffer, &entry_count);
  147. if (ret != EFI_SUCCESS) {
  148. efi_st_error("OpenProtocolInformation failed\n");
  149. return EFI_ST_FAILURE;
  150. }
  151. if (entry_count != 2) {
  152. efi_st_error("Incorrect OpenProtocolInformation count\n");
  153. efi_st_printf("Expected 2, got %u\n",
  154. (unsigned int)entry_count);
  155. return EFI_ST_FAILURE;
  156. }
  157. if (entry_buffer[0].open_count + entry_buffer[1].open_count != 3) {
  158. efi_st_error("Incorrect open count: expected 3 got %u\n",
  159. entry_buffer[0].open_count +
  160. entry_buffer[1].open_count);
  161. return EFI_ST_FAILURE;
  162. }
  163. ret = boottime->free_pool(entry_buffer);
  164. if (ret != EFI_SUCCESS) {
  165. efi_st_error("FreePool failed\n");
  166. return EFI_ST_FAILURE;
  167. }
  168. ret = boottime->close_protocol(handle1, &guid1, firmware_handle, NULL);
  169. if (ret != EFI_SUCCESS) {
  170. efi_st_error("CloseProtocol failed\n");
  171. return EFI_ST_FAILURE;
  172. }
  173. ret = boottime->open_protocol_information(handle1, &guid1,
  174. &entry_buffer, &entry_count);
  175. if (ret != EFI_SUCCESS) {
  176. efi_st_error("OpenProtocolInformation failed\n");
  177. return EFI_ST_FAILURE;
  178. }
  179. if (entry_count) {
  180. efi_st_error("Incorrect OpenProtocolInformation count\n");
  181. efi_st_printf("Expected 0, got %u\n",
  182. (unsigned int)entry_count);
  183. return EFI_ST_FAILURE;
  184. }
  185. return EFI_ST_SUCCESS;
  186. }
  187. EFI_UNIT_TEST(openprot) = {
  188. .name = "open protocol",
  189. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  190. .setup = setup,
  191. .execute = execute,
  192. .teardown = teardown,
  193. };