efi_selftest_manageprotocols.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_manageprotocols
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This unit test checks the following protocol services:
  8. * InstallProtocolInterface, UninstallProtocolInterface,
  9. * InstallMultipleProtocolsInterfaces, UninstallMultipleProtocolsInterfaces,
  10. * HandleProtocol, ProtocolsPerHandle,
  11. * LocateHandle, LocateHandleBuffer.
  12. */
  13. #include <efi_selftest.h>
  14. /*
  15. * The test currently does not actually call the interface function.
  16. * So this is just a dummy structure.
  17. */
  18. struct interface {
  19. void (EFIAPI * inc)(void);
  20. };
  21. static struct efi_boot_services *boottime;
  22. static efi_guid_t guid1 =
  23. EFI_GUID(0x2e7ca819, 0x21d3, 0x0a3a,
  24. 0xf7, 0x91, 0x82, 0x1f, 0x7a, 0x83, 0x67, 0xaf);
  25. static efi_guid_t guid2 =
  26. EFI_GUID(0xf909f2bb, 0x90a8, 0x0d77,
  27. 0x94, 0x0c, 0x3e, 0xa8, 0xea, 0x38, 0xd6, 0x6f);
  28. static efi_guid_t guid3 =
  29. EFI_GUID(0x06d641a3, 0xf4e7, 0xe0c9,
  30. 0xe7, 0x8d, 0x41, 0x2d, 0x72, 0xa6, 0xb1, 0x24);
  31. static efi_handle_t handle1;
  32. static efi_handle_t handle2;
  33. static struct interface interface1;
  34. static struct interface interface2;
  35. static struct interface interface3;
  36. static struct interface interface4;
  37. /*
  38. * Find a handle in an array.
  39. *
  40. * @handle: handle to find
  41. * @count: number of entries in the array
  42. * @buffer: array to search
  43. */
  44. efi_status_t find_in_buffer(efi_handle_t handle, size_t count,
  45. efi_handle_t *buffer)
  46. {
  47. size_t i;
  48. for (i = 0; i < count; ++i) {
  49. if (buffer[i] == handle)
  50. return EFI_SUCCESS;
  51. }
  52. return EFI_NOT_FOUND;
  53. }
  54. /*
  55. * Setup unit test.
  56. *
  57. * Create two handles and install two out of three protocol interfaces on each
  58. * of them:
  59. *
  60. * handle1
  61. * guid1 interface1
  62. * guid3 interface3
  63. * handle2
  64. * guid1 interface4
  65. * guid2 interface2
  66. *
  67. * @handle: handle of the loaded image
  68. * @systable: system table
  69. */
  70. static int setup(const efi_handle_t img_handle,
  71. const struct efi_system_table *systable)
  72. {
  73. efi_status_t ret;
  74. efi_handle_t handle;
  75. boottime = systable->boottime;
  76. ret = boottime->install_protocol_interface(&handle1, &guid3,
  77. EFI_NATIVE_INTERFACE,
  78. &interface3);
  79. if (ret != EFI_SUCCESS) {
  80. efi_st_error("InstallProtocolInterface failed\n");
  81. return EFI_ST_FAILURE;
  82. }
  83. if (!handle1) {
  84. efi_st_error("InstallProtocolInterface failed to create handle\n");
  85. return EFI_ST_FAILURE;
  86. }
  87. handle = handle1;
  88. ret = boottime->install_protocol_interface(&handle1, &guid1,
  89. EFI_NATIVE_INTERFACE,
  90. &interface1);
  91. if (ret != EFI_SUCCESS) {
  92. efi_st_error("InstallProtocolInterface failed\n");
  93. return EFI_ST_FAILURE;
  94. }
  95. if (handle != handle1) {
  96. efi_st_error("InstallProtocolInterface failed to use handle\n");
  97. return EFI_ST_FAILURE;
  98. }
  99. ret = boottime->install_multiple_protocol_interfaces(&handle2,
  100. &guid1, &interface4, &guid2, &interface2, NULL);
  101. if (ret != EFI_SUCCESS) {
  102. efi_st_error("InstallMultipleProtocolInterfaces failed\n");
  103. return EFI_ST_FAILURE;
  104. }
  105. if (!handle2 || handle1 == handle2) {
  106. efi_st_error("InstallMultipleProtocolInterfaces failed to create handle\n");
  107. return EFI_ST_FAILURE;
  108. }
  109. return EFI_ST_SUCCESS;
  110. }
  111. /*
  112. * Tear down unit test.
  113. *
  114. */
  115. static int teardown(void)
  116. {
  117. return EFI_ST_SUCCESS;
  118. }
  119. /*
  120. * Execute unit test.
  121. *
  122. */
  123. static int execute(void)
  124. {
  125. struct interface *interface;
  126. efi_status_t ret;
  127. efi_handle_t *buffer;
  128. size_t buffer_size;
  129. efi_uintn_t count = 0;
  130. efi_guid_t **prot_buffer;
  131. efi_uintn_t prot_count;
  132. /*
  133. * Test HandleProtocol
  134. */
  135. ret = boottime->handle_protocol(handle1, &guid3, (void **)&interface);
  136. if (ret != EFI_SUCCESS) {
  137. efi_st_error("HandleProtocol failed to retrieve interface\n");
  138. return EFI_ST_FAILURE;
  139. }
  140. if (interface != &interface3) {
  141. efi_st_error("HandleProtocol returned wrong interface\n");
  142. return EFI_ST_FAILURE;
  143. }
  144. ret = boottime->handle_protocol(handle1, &guid2, (void **)&interface);
  145. if (ret == EFI_SUCCESS) {
  146. efi_st_error("HandleProtocol returned not installed interface\n");
  147. return EFI_ST_FAILURE;
  148. }
  149. /*
  150. * Test LocateHandleBuffer with AllHandles
  151. */
  152. ret = boottime->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
  153. &count, &buffer);
  154. if (ret != EFI_SUCCESS) {
  155. efi_st_error("LocateHandleBuffer with AllHandles failed\n");
  156. return EFI_ST_FAILURE;
  157. }
  158. buffer_size = count;
  159. ret = find_in_buffer(handle1, count, buffer);
  160. if (ret != EFI_SUCCESS) {
  161. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  162. return EFI_ST_FAILURE;
  163. }
  164. ret = find_in_buffer(handle2, count, buffer);
  165. if (ret != EFI_SUCCESS) {
  166. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  167. return EFI_ST_FAILURE;
  168. }
  169. /* Release buffer */
  170. ret = boottime->free_pool(buffer);
  171. if (ret != EFI_SUCCESS) {
  172. efi_st_error("FreePool failed\n");
  173. return EFI_ST_FAILURE;
  174. }
  175. /*
  176. * Test error handling in UninstallMultipleProtocols
  177. *
  178. * Try to uninstall more protocols than there are installed.
  179. */
  180. ret = boottime->uninstall_multiple_protocol_interfaces(
  181. handle2,
  182. &guid1, &interface4,
  183. &guid2, &interface2,
  184. &guid3, &interface3,
  185. NULL);
  186. if (ret == EFI_SUCCESS) {
  187. efi_st_error("UninstallMultipleProtocolInterfaces did not catch error\n");
  188. return EFI_ST_FAILURE;
  189. }
  190. /*
  191. * Test LocateHandleBuffer with ByProtocol
  192. */
  193. count = buffer_size;
  194. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  195. &count, &buffer);
  196. if (ret != EFI_SUCCESS) {
  197. efi_st_error("LocateHandleBuffer failed to locate new handles\n");
  198. return EFI_ST_FAILURE;
  199. }
  200. if (count != 2) {
  201. efi_st_error("LocateHandleBuffer failed to locate new handles\n");
  202. return EFI_ST_FAILURE;
  203. }
  204. ret = find_in_buffer(handle1, count, buffer);
  205. if (ret != EFI_SUCCESS) {
  206. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  207. return EFI_ST_FAILURE;
  208. }
  209. ret = find_in_buffer(handle2, count, buffer);
  210. if (ret != EFI_SUCCESS) {
  211. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  212. return EFI_ST_FAILURE;
  213. }
  214. /* Clear the buffer, we are reusing it it the next step. */
  215. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  216. /*
  217. * Test LocateHandle with ByProtocol
  218. */
  219. count = buffer_size * sizeof(efi_handle_t);
  220. ret = boottime->locate_handle(BY_PROTOCOL, &guid1, NULL,
  221. &count, buffer);
  222. if (ret != EFI_SUCCESS) {
  223. efi_st_error("LocateHandle with ByProtocol failed\n");
  224. return EFI_ST_FAILURE;
  225. }
  226. if (count / sizeof(efi_handle_t) != 2) {
  227. efi_st_error("LocateHandle failed to locate new handles\n");
  228. return EFI_ST_FAILURE;
  229. }
  230. buffer_size = count;
  231. ret = find_in_buffer(handle1, count, buffer);
  232. if (ret != EFI_SUCCESS) {
  233. efi_st_error("LocateHandle failed to locate new handles\n");
  234. return EFI_ST_FAILURE;
  235. }
  236. ret = find_in_buffer(handle2, count, buffer);
  237. if (ret != EFI_SUCCESS) {
  238. efi_st_error("LocateHandle failed to locate new handles\n");
  239. return EFI_ST_FAILURE;
  240. }
  241. /* Release buffer */
  242. ret = boottime->free_pool(buffer);
  243. if (ret != EFI_SUCCESS) {
  244. efi_st_error("FreePool failed\n");
  245. return EFI_ST_FAILURE;
  246. }
  247. /*
  248. * Test LocateProtocol
  249. */
  250. ret = boottime->locate_protocol(&guid1, NULL, (void **)&interface);
  251. if (ret != EFI_SUCCESS) {
  252. efi_st_error("LocateProtocol failed\n");
  253. return EFI_ST_FAILURE;
  254. }
  255. if (interface != &interface1 && interface != &interface4) {
  256. efi_st_error("LocateProtocol failed to locate protocol\n");
  257. return EFI_ST_FAILURE;
  258. }
  259. /*
  260. * Test UninstallMultipleProtocols
  261. */
  262. ret = boottime->uninstall_multiple_protocol_interfaces(
  263. handle2,
  264. &guid1, &interface4,
  265. &guid2, &interface2,
  266. NULL);
  267. if (ret != EFI_SUCCESS) {
  268. efi_st_error("UninstallMultipleProtocolInterfaces failed\n");
  269. return EFI_ST_FAILURE;
  270. }
  271. /*
  272. * Check that the protocols are really uninstalled.
  273. */
  274. count = buffer_size;
  275. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  276. &count, &buffer);
  277. if (ret != EFI_SUCCESS) {
  278. efi_st_error("LocateHandleBuffer failed\n");
  279. return EFI_ST_FAILURE;
  280. }
  281. if (count != 1) {
  282. efi_st_error("UninstallMultipleProtocolInterfaces failed to uninstall protocols\n");
  283. return EFI_ST_FAILURE;
  284. }
  285. ret = find_in_buffer(handle1, count, buffer);
  286. if (ret != EFI_SUCCESS) {
  287. efi_st_error("Failed to locate new handle\n");
  288. return EFI_ST_FAILURE;
  289. }
  290. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  291. /*
  292. * Test ProtocolsPerHandle
  293. */
  294. ret = boottime->protocols_per_handle(handle1,
  295. &prot_buffer, &prot_count);
  296. if (ret != EFI_SUCCESS) {
  297. efi_st_error("Failed to get protocols per handle\n");
  298. return EFI_ST_FAILURE;
  299. }
  300. if (prot_count != 2) {
  301. efi_st_error("Failed to get protocols per handle\n");
  302. return EFI_ST_FAILURE;
  303. }
  304. if (efi_st_memcmp(prot_buffer[0], &guid1, 16) &&
  305. efi_st_memcmp(prot_buffer[1], &guid1, 16)) {
  306. efi_st_error("Failed to get protocols per handle\n");
  307. return EFI_ST_FAILURE;
  308. }
  309. if (efi_st_memcmp(prot_buffer[0], &guid3, 16) &&
  310. efi_st_memcmp(prot_buffer[1], &guid3, 16)) {
  311. efi_st_error("Failed to get protocols per handle\n");
  312. return EFI_ST_FAILURE;
  313. }
  314. /* Release buffer */
  315. ret = boottime->free_pool(prot_buffer);
  316. if (ret != EFI_SUCCESS) {
  317. efi_st_error("FreePool failed\n");
  318. return EFI_ST_FAILURE;
  319. }
  320. /*
  321. * Uninstall remaining protocols
  322. */
  323. ret = boottime->uninstall_protocol_interface(handle1, &guid1,
  324. &interface1);
  325. if (ret != EFI_SUCCESS) {
  326. efi_st_error("UninstallProtocolInterface failed\n");
  327. return EFI_ST_FAILURE;
  328. }
  329. ret = boottime->handle_protocol(handle1, &guid1, (void **)&interface);
  330. if (ret == EFI_SUCCESS) {
  331. efi_st_error("UninstallProtocolInterface failed\n");
  332. return EFI_ST_FAILURE;
  333. }
  334. ret = boottime->uninstall_protocol_interface(handle1, &guid3,
  335. &interface3);
  336. if (ret != EFI_SUCCESS) {
  337. efi_st_error("UninstallProtocolInterface failed\n");
  338. return EFI_ST_FAILURE;
  339. }
  340. return EFI_ST_SUCCESS;
  341. }
  342. EFI_UNIT_TEST(protserv) = {
  343. .name = "manage protocols",
  344. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  345. .setup = setup,
  346. .execute = execute,
  347. .teardown = teardown,
  348. };