efi_selftest_manageprotocols.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. handle1 = NULL;
  76. handle2 = NULL;
  77. boottime = systable->boottime;
  78. ret = boottime->install_protocol_interface(&handle1, &guid3,
  79. EFI_NATIVE_INTERFACE,
  80. &interface3);
  81. if (ret != EFI_SUCCESS) {
  82. efi_st_error("InstallProtocolInterface failed\n");
  83. return EFI_ST_FAILURE;
  84. }
  85. if (!handle1) {
  86. efi_st_error("InstallProtocolInterface failed to create handle\n");
  87. return EFI_ST_FAILURE;
  88. }
  89. handle = handle1;
  90. ret = boottime->install_protocol_interface(&handle1, &guid1,
  91. EFI_NATIVE_INTERFACE,
  92. &interface1);
  93. if (ret != EFI_SUCCESS) {
  94. efi_st_error("InstallProtocolInterface failed\n");
  95. return EFI_ST_FAILURE;
  96. }
  97. if (handle != handle1) {
  98. efi_st_error("InstallProtocolInterface failed to use handle\n");
  99. return EFI_ST_FAILURE;
  100. }
  101. ret = boottime->install_multiple_protocol_interfaces(&handle2,
  102. &guid1, &interface4, &guid2, &interface2, NULL);
  103. if (ret != EFI_SUCCESS) {
  104. efi_st_error("InstallMultipleProtocolInterfaces failed\n");
  105. return EFI_ST_FAILURE;
  106. }
  107. if (!handle2 || handle1 == handle2) {
  108. efi_st_error("InstallMultipleProtocolInterfaces failed to create handle\n");
  109. return EFI_ST_FAILURE;
  110. }
  111. return EFI_ST_SUCCESS;
  112. }
  113. /*
  114. * Tear down unit test.
  115. *
  116. */
  117. static int teardown(void)
  118. {
  119. return EFI_ST_SUCCESS;
  120. }
  121. /*
  122. * Execute unit test.
  123. *
  124. */
  125. static int execute(void)
  126. {
  127. struct interface *interface;
  128. efi_status_t ret;
  129. efi_handle_t *buffer;
  130. size_t buffer_size;
  131. efi_uintn_t count = 0;
  132. efi_guid_t **prot_buffer;
  133. efi_uintn_t prot_count;
  134. /*
  135. * Test HandleProtocol
  136. */
  137. ret = boottime->handle_protocol(handle1, &guid3, (void **)&interface);
  138. if (ret != EFI_SUCCESS) {
  139. efi_st_error("HandleProtocol failed to retrieve interface\n");
  140. return EFI_ST_FAILURE;
  141. }
  142. if (interface != &interface3) {
  143. efi_st_error("HandleProtocol returned wrong interface\n");
  144. return EFI_ST_FAILURE;
  145. }
  146. ret = boottime->handle_protocol(handle1, &guid2, (void **)&interface);
  147. if (ret == EFI_SUCCESS) {
  148. efi_st_error("HandleProtocol returned not installed interface\n");
  149. return EFI_ST_FAILURE;
  150. }
  151. /*
  152. * Test LocateHandleBuffer with AllHandles
  153. */
  154. ret = boottime->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
  155. &count, &buffer);
  156. if (ret != EFI_SUCCESS) {
  157. efi_st_error("LocateHandleBuffer with AllHandles failed\n");
  158. return EFI_ST_FAILURE;
  159. }
  160. buffer_size = count;
  161. ret = find_in_buffer(handle1, count, buffer);
  162. if (ret != EFI_SUCCESS) {
  163. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  164. return EFI_ST_FAILURE;
  165. }
  166. ret = find_in_buffer(handle2, count, buffer);
  167. if (ret != EFI_SUCCESS) {
  168. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  169. return EFI_ST_FAILURE;
  170. }
  171. /* Release buffer */
  172. ret = boottime->free_pool(buffer);
  173. if (ret != EFI_SUCCESS) {
  174. efi_st_error("FreePool failed\n");
  175. return EFI_ST_FAILURE;
  176. }
  177. /*
  178. * Test error handling in UninstallMultipleProtocols
  179. *
  180. * These are the installed protocol interfaces on handle 2:
  181. *
  182. * guid1 interface4
  183. * guid2 interface2
  184. *
  185. * Try to uninstall more protocols than there are installed. This
  186. * should return an error EFI_INVALID_PARAMETER. All deleted protocols
  187. * should be reinstalled.
  188. */
  189. ret = boottime->uninstall_multiple_protocol_interfaces(
  190. handle2,
  191. &guid1, &interface4,
  192. &guid2, &interface2,
  193. &guid3, &interface3,
  194. NULL);
  195. if (ret != EFI_INVALID_PARAMETER) {
  196. printf("%lx", ret);
  197. efi_st_error("UninstallMultipleProtocolInterfaces did not catch error\n");
  198. return EFI_ST_FAILURE;
  199. }
  200. /*
  201. * Test LocateHandleBuffer with ByProtocol
  202. *
  203. * These are the handles with a guid1 protocol interface installed:
  204. *
  205. * handle1, handle2
  206. */
  207. count = buffer_size;
  208. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  209. &count, &buffer);
  210. if (ret != EFI_SUCCESS) {
  211. efi_st_error("LocateHandleBuffer failed to locate new handles\n");
  212. return EFI_ST_FAILURE;
  213. }
  214. if (count != 2) {
  215. efi_st_error("UninstallMultipleProtocolInterfaces deleted handle\n");
  216. return EFI_ST_FAILURE;
  217. }
  218. ret = find_in_buffer(handle1, count, buffer);
  219. if (ret != EFI_SUCCESS) {
  220. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  221. return EFI_ST_FAILURE;
  222. }
  223. ret = find_in_buffer(handle2, count, buffer);
  224. if (ret != EFI_SUCCESS) {
  225. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  226. return EFI_ST_FAILURE;
  227. }
  228. /* Clear the buffer, we are reusing it it the next step. */
  229. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  230. /*
  231. * Test LocateHandle with ByProtocol
  232. */
  233. count = buffer_size * sizeof(efi_handle_t);
  234. ret = boottime->locate_handle(BY_PROTOCOL, &guid1, NULL,
  235. &count, buffer);
  236. if (ret != EFI_SUCCESS) {
  237. efi_st_error("LocateHandle with ByProtocol failed\n");
  238. return EFI_ST_FAILURE;
  239. }
  240. if (count / sizeof(efi_handle_t) != 2) {
  241. efi_st_error("LocateHandle failed to locate new handles\n");
  242. return EFI_ST_FAILURE;
  243. }
  244. buffer_size = count;
  245. ret = find_in_buffer(handle1, count, buffer);
  246. if (ret != EFI_SUCCESS) {
  247. efi_st_error("LocateHandle failed to locate new handles\n");
  248. return EFI_ST_FAILURE;
  249. }
  250. ret = find_in_buffer(handle2, count, buffer);
  251. if (ret != EFI_SUCCESS) {
  252. efi_st_error("LocateHandle failed to locate new handles\n");
  253. return EFI_ST_FAILURE;
  254. }
  255. /* Release buffer */
  256. ret = boottime->free_pool(buffer);
  257. if (ret != EFI_SUCCESS) {
  258. efi_st_error("FreePool failed\n");
  259. return EFI_ST_FAILURE;
  260. }
  261. /*
  262. * Test LocateProtocol
  263. */
  264. ret = boottime->locate_protocol(&guid1, NULL, (void **)&interface);
  265. if (ret != EFI_SUCCESS) {
  266. efi_st_error("LocateProtocol failed\n");
  267. return EFI_ST_FAILURE;
  268. }
  269. if (interface != &interface1 && interface != &interface4) {
  270. efi_st_error("LocateProtocol failed to locate protocol\n");
  271. return EFI_ST_FAILURE;
  272. }
  273. /*
  274. * Test UninstallMultipleProtocols
  275. */
  276. ret = boottime->uninstall_multiple_protocol_interfaces(
  277. handle2,
  278. &guid1, &interface4,
  279. &guid2, &interface2,
  280. NULL);
  281. if (ret != EFI_SUCCESS) {
  282. efi_st_error("UninstallMultipleProtocolInterfaces failed\n");
  283. return EFI_ST_FAILURE;
  284. }
  285. /*
  286. * Check that the protocols are really uninstalled.
  287. */
  288. count = buffer_size;
  289. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  290. &count, &buffer);
  291. if (ret != EFI_SUCCESS) {
  292. efi_st_error("LocateHandleBuffer failed\n");
  293. return EFI_ST_FAILURE;
  294. }
  295. if (count != 1) {
  296. efi_st_error("UninstallMultipleProtocolInterfaces failed to uninstall protocols\n");
  297. return EFI_ST_FAILURE;
  298. }
  299. ret = find_in_buffer(handle1, count, buffer);
  300. if (ret != EFI_SUCCESS) {
  301. efi_st_error("Failed to locate new handle\n");
  302. return EFI_ST_FAILURE;
  303. }
  304. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  305. /*
  306. * Test ProtocolsPerHandle
  307. */
  308. ret = boottime->protocols_per_handle(handle1,
  309. &prot_buffer, &prot_count);
  310. if (ret != EFI_SUCCESS) {
  311. efi_st_error("Failed to get protocols per handle\n");
  312. return EFI_ST_FAILURE;
  313. }
  314. if (prot_count != 2) {
  315. efi_st_error("Failed to get protocols per handle\n");
  316. return EFI_ST_FAILURE;
  317. }
  318. if (memcmp(prot_buffer[0], &guid1, 16) &&
  319. memcmp(prot_buffer[1], &guid1, 16)) {
  320. efi_st_error("Failed to get protocols per handle\n");
  321. return EFI_ST_FAILURE;
  322. }
  323. if (memcmp(prot_buffer[0], &guid3, 16) &&
  324. memcmp(prot_buffer[1], &guid3, 16)) {
  325. efi_st_error("Failed to get protocols per handle\n");
  326. return EFI_ST_FAILURE;
  327. }
  328. /* Release buffer */
  329. ret = boottime->free_pool(prot_buffer);
  330. if (ret != EFI_SUCCESS) {
  331. efi_st_error("FreePool failed\n");
  332. return EFI_ST_FAILURE;
  333. }
  334. /*
  335. * Uninstall remaining protocols
  336. */
  337. ret = boottime->uninstall_protocol_interface(handle1, &guid1,
  338. &interface1);
  339. if (ret != EFI_SUCCESS) {
  340. efi_st_error("UninstallProtocolInterface failed\n");
  341. return EFI_ST_FAILURE;
  342. }
  343. ret = boottime->handle_protocol(handle1, &guid1, (void **)&interface);
  344. if (ret == EFI_SUCCESS) {
  345. efi_st_error("UninstallProtocolInterface failed\n");
  346. return EFI_ST_FAILURE;
  347. }
  348. ret = boottime->uninstall_protocol_interface(handle1, &guid3,
  349. &interface3);
  350. if (ret != EFI_SUCCESS) {
  351. efi_st_error("UninstallProtocolInterface failed\n");
  352. return EFI_ST_FAILURE;
  353. }
  354. return EFI_ST_SUCCESS;
  355. }
  356. EFI_UNIT_TEST(protserv) = {
  357. .name = "manage protocols",
  358. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  359. .setup = setup,
  360. .execute = execute,
  361. .teardown = teardown,
  362. };