efi_selftest_manageprotocols.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. * These are the installed protocol interfaces on handle 2:
  179. *
  180. * guid1 interface4
  181. * guid2 interface2
  182. *
  183. * Try to uninstall more protocols than there are installed. This
  184. * should return an error EFI_INVALID_PARAMETER. All deleted protocols
  185. * should be reinstalled.
  186. */
  187. ret = boottime->uninstall_multiple_protocol_interfaces(
  188. handle2,
  189. &guid1, &interface4,
  190. &guid2, &interface2,
  191. &guid3, &interface3,
  192. NULL);
  193. if (ret != EFI_INVALID_PARAMETER) {
  194. printf("%lx", ret);
  195. efi_st_error("UninstallMultipleProtocolInterfaces did not catch error\n");
  196. return EFI_ST_FAILURE;
  197. }
  198. /*
  199. * Test LocateHandleBuffer with ByProtocol
  200. *
  201. * These are the handles with a guid1 protocol interface installed:
  202. *
  203. * handle1, handle2
  204. */
  205. count = buffer_size;
  206. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  207. &count, &buffer);
  208. if (ret != EFI_SUCCESS) {
  209. efi_st_error("LocateHandleBuffer failed to locate new handles\n");
  210. return EFI_ST_FAILURE;
  211. }
  212. if (count != 2) {
  213. efi_st_error("UninstallMultipleProtocolInterfaces deleted handle\n");
  214. return EFI_ST_FAILURE;
  215. }
  216. ret = find_in_buffer(handle1, count, buffer);
  217. if (ret != EFI_SUCCESS) {
  218. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  219. return EFI_ST_FAILURE;
  220. }
  221. ret = find_in_buffer(handle2, count, buffer);
  222. if (ret != EFI_SUCCESS) {
  223. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  224. return EFI_ST_FAILURE;
  225. }
  226. /* Clear the buffer, we are reusing it it the next step. */
  227. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  228. /*
  229. * Test LocateHandle with ByProtocol
  230. */
  231. count = buffer_size * sizeof(efi_handle_t);
  232. ret = boottime->locate_handle(BY_PROTOCOL, &guid1, NULL,
  233. &count, buffer);
  234. if (ret != EFI_SUCCESS) {
  235. efi_st_error("LocateHandle with ByProtocol failed\n");
  236. return EFI_ST_FAILURE;
  237. }
  238. if (count / sizeof(efi_handle_t) != 2) {
  239. efi_st_error("LocateHandle failed to locate new handles\n");
  240. return EFI_ST_FAILURE;
  241. }
  242. buffer_size = count;
  243. ret = find_in_buffer(handle1, count, buffer);
  244. if (ret != EFI_SUCCESS) {
  245. efi_st_error("LocateHandle failed to locate new handles\n");
  246. return EFI_ST_FAILURE;
  247. }
  248. ret = find_in_buffer(handle2, count, buffer);
  249. if (ret != EFI_SUCCESS) {
  250. efi_st_error("LocateHandle failed to locate new handles\n");
  251. return EFI_ST_FAILURE;
  252. }
  253. /* Release buffer */
  254. ret = boottime->free_pool(buffer);
  255. if (ret != EFI_SUCCESS) {
  256. efi_st_error("FreePool failed\n");
  257. return EFI_ST_FAILURE;
  258. }
  259. /*
  260. * Test LocateProtocol
  261. */
  262. ret = boottime->locate_protocol(&guid1, NULL, (void **)&interface);
  263. if (ret != EFI_SUCCESS) {
  264. efi_st_error("LocateProtocol failed\n");
  265. return EFI_ST_FAILURE;
  266. }
  267. if (interface != &interface1 && interface != &interface4) {
  268. efi_st_error("LocateProtocol failed to locate protocol\n");
  269. return EFI_ST_FAILURE;
  270. }
  271. /*
  272. * Test UninstallMultipleProtocols
  273. */
  274. ret = boottime->uninstall_multiple_protocol_interfaces(
  275. handle2,
  276. &guid1, &interface4,
  277. &guid2, &interface2,
  278. NULL);
  279. if (ret != EFI_SUCCESS) {
  280. efi_st_error("UninstallMultipleProtocolInterfaces failed\n");
  281. return EFI_ST_FAILURE;
  282. }
  283. /*
  284. * Check that the protocols are really uninstalled.
  285. */
  286. count = buffer_size;
  287. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  288. &count, &buffer);
  289. if (ret != EFI_SUCCESS) {
  290. efi_st_error("LocateHandleBuffer failed\n");
  291. return EFI_ST_FAILURE;
  292. }
  293. if (count != 1) {
  294. efi_st_error("UninstallMultipleProtocolInterfaces failed to uninstall protocols\n");
  295. return EFI_ST_FAILURE;
  296. }
  297. ret = find_in_buffer(handle1, count, buffer);
  298. if (ret != EFI_SUCCESS) {
  299. efi_st_error("Failed to locate new handle\n");
  300. return EFI_ST_FAILURE;
  301. }
  302. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  303. /*
  304. * Test ProtocolsPerHandle
  305. */
  306. ret = boottime->protocols_per_handle(handle1,
  307. &prot_buffer, &prot_count);
  308. if (ret != EFI_SUCCESS) {
  309. efi_st_error("Failed to get protocols per handle\n");
  310. return EFI_ST_FAILURE;
  311. }
  312. if (prot_count != 2) {
  313. efi_st_error("Failed to get protocols per handle\n");
  314. return EFI_ST_FAILURE;
  315. }
  316. if (memcmp(prot_buffer[0], &guid1, 16) &&
  317. memcmp(prot_buffer[1], &guid1, 16)) {
  318. efi_st_error("Failed to get protocols per handle\n");
  319. return EFI_ST_FAILURE;
  320. }
  321. if (memcmp(prot_buffer[0], &guid3, 16) &&
  322. memcmp(prot_buffer[1], &guid3, 16)) {
  323. efi_st_error("Failed to get protocols per handle\n");
  324. return EFI_ST_FAILURE;
  325. }
  326. /* Release buffer */
  327. ret = boottime->free_pool(prot_buffer);
  328. if (ret != EFI_SUCCESS) {
  329. efi_st_error("FreePool failed\n");
  330. return EFI_ST_FAILURE;
  331. }
  332. /*
  333. * Uninstall remaining protocols
  334. */
  335. ret = boottime->uninstall_protocol_interface(handle1, &guid1,
  336. &interface1);
  337. if (ret != EFI_SUCCESS) {
  338. efi_st_error("UninstallProtocolInterface failed\n");
  339. return EFI_ST_FAILURE;
  340. }
  341. ret = boottime->handle_protocol(handle1, &guid1, (void **)&interface);
  342. if (ret == EFI_SUCCESS) {
  343. efi_st_error("UninstallProtocolInterface failed\n");
  344. return EFI_ST_FAILURE;
  345. }
  346. ret = boottime->uninstall_protocol_interface(handle1, &guid3,
  347. &interface3);
  348. if (ret != EFI_SUCCESS) {
  349. efi_st_error("UninstallProtocolInterface failed\n");
  350. return EFI_ST_FAILURE;
  351. }
  352. return EFI_ST_SUCCESS;
  353. }
  354. EFI_UNIT_TEST(protserv) = {
  355. .name = "manage protocols",
  356. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  357. .setup = setup,
  358. .execute = execute,
  359. .teardown = teardown,
  360. };