efi_selftest_manageprotocols.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  170. /*
  171. * Test error handling in UninstallMultipleProtocols
  172. *
  173. * Try to uninstall more protocols than there are installed.
  174. */
  175. ret = boottime->uninstall_multiple_protocol_interfaces(
  176. handle2,
  177. &guid1, &interface4,
  178. &guid2, &interface2,
  179. &guid3, &interface3,
  180. NULL);
  181. if (ret == EFI_SUCCESS) {
  182. efi_st_error("UninstallMultipleProtocolInterfaces did not catch error\n");
  183. return EFI_ST_FAILURE;
  184. }
  185. /*
  186. * Test LocateHandleBuffer with ByProtocol
  187. */
  188. count = buffer_size;
  189. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  190. &count, &buffer);
  191. if (ret != EFI_SUCCESS) {
  192. efi_st_error("LocateHandleBuffer failed to locate new handles\n");
  193. return EFI_ST_FAILURE;
  194. }
  195. if (count != 2) {
  196. efi_st_error("LocateHandleBuffer failed to locate new handles\n");
  197. return EFI_ST_FAILURE;
  198. }
  199. ret = find_in_buffer(handle1, count, buffer);
  200. if (ret != EFI_SUCCESS) {
  201. efi_st_error("LocateHandleBuffer failed to locate new handle\n");
  202. return EFI_ST_FAILURE;
  203. }
  204. ret = find_in_buffer(handle2, 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. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  210. /*
  211. * Test LocateHandle with ByProtocol
  212. */
  213. count = buffer_size * sizeof(efi_handle_t);
  214. ret = boottime->locate_handle(BY_PROTOCOL, &guid1, NULL,
  215. &count, buffer);
  216. if (ret != EFI_SUCCESS) {
  217. efi_st_error("LocateHandle with ByProtocol failed\n");
  218. return EFI_ST_FAILURE;
  219. }
  220. if (count / sizeof(efi_handle_t) != 2) {
  221. efi_st_error("LocateHandle failed to locate new handles\n");
  222. return EFI_ST_FAILURE;
  223. }
  224. buffer_size = count;
  225. ret = find_in_buffer(handle1, count, buffer);
  226. if (ret != EFI_SUCCESS) {
  227. efi_st_error("LocateHandle failed to locate new handles\n");
  228. return EFI_ST_FAILURE;
  229. }
  230. ret = find_in_buffer(handle2, count, buffer);
  231. if (ret != EFI_SUCCESS) {
  232. efi_st_error("LocateHandle failed to locate new handles\n");
  233. return EFI_ST_FAILURE;
  234. }
  235. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  236. /*
  237. * Test LocateProtocol
  238. */
  239. ret = boottime->locate_protocol(&guid1, NULL, (void **)&interface);
  240. if (ret != EFI_SUCCESS) {
  241. efi_st_error("LocateProtocol failed\n");
  242. return EFI_ST_FAILURE;
  243. }
  244. if (interface != &interface1 && interface != &interface4) {
  245. efi_st_error("LocateProtocol failed to locate protocol\n");
  246. return EFI_ST_FAILURE;
  247. }
  248. /*
  249. * Test UninstallMultipleProtocols
  250. */
  251. ret = boottime->uninstall_multiple_protocol_interfaces(
  252. handle2,
  253. &guid1, &interface4,
  254. &guid2, &interface2,
  255. NULL);
  256. if (ret != EFI_SUCCESS) {
  257. efi_st_error("UninstallMultipleProtocolInterfaces failed\n");
  258. return EFI_ST_FAILURE;
  259. }
  260. /*
  261. * Check that the protocols are really uninstalled.
  262. */
  263. count = buffer_size;
  264. ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
  265. &count, &buffer);
  266. if (ret != EFI_SUCCESS) {
  267. efi_st_error("LocateHandleBuffer failed\n");
  268. return EFI_ST_FAILURE;
  269. }
  270. if (count != 1) {
  271. efi_st_error("UninstallMultipleProtocolInterfaces failed to uninstall protocols\n");
  272. return EFI_ST_FAILURE;
  273. }
  274. ret = find_in_buffer(handle1, count, buffer);
  275. if (ret != EFI_SUCCESS) {
  276. efi_st_error("Failed to locate new handle\n");
  277. return EFI_ST_FAILURE;
  278. }
  279. boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
  280. /*
  281. * Test ProtocolsPerHandle
  282. */
  283. ret = boottime->protocols_per_handle(handle1,
  284. &prot_buffer, &prot_count);
  285. if (ret != EFI_SUCCESS) {
  286. efi_st_error("Failed to get protocols per handle\n");
  287. return EFI_ST_FAILURE;
  288. }
  289. if (prot_count != 2) {
  290. efi_st_error("Failed to get protocols per handle\n");
  291. return EFI_ST_FAILURE;
  292. }
  293. if (efi_st_memcmp(prot_buffer[0], &guid1, 16) &&
  294. efi_st_memcmp(prot_buffer[1], &guid1, 16)) {
  295. efi_st_error("Failed to get protocols per handle\n");
  296. return EFI_ST_FAILURE;
  297. }
  298. if (efi_st_memcmp(prot_buffer[0], &guid3, 16) &&
  299. efi_st_memcmp(prot_buffer[1], &guid3, 16)) {
  300. efi_st_error("Failed to get protocols per handle\n");
  301. return EFI_ST_FAILURE;
  302. }
  303. /*
  304. * Uninstall remaining protocols
  305. */
  306. ret = boottime->uninstall_protocol_interface(handle1, &guid1,
  307. &interface1);
  308. if (ret != EFI_SUCCESS) {
  309. efi_st_error("UninstallProtocolInterface failed\n");
  310. return EFI_ST_FAILURE;
  311. }
  312. ret = boottime->handle_protocol(handle1, &guid1, (void **)&interface);
  313. if (ret == EFI_SUCCESS) {
  314. efi_st_error("UninstallProtocolInterface failed\n");
  315. return EFI_ST_FAILURE;
  316. }
  317. ret = boottime->uninstall_protocol_interface(handle1, &guid3,
  318. &interface3);
  319. if (ret != EFI_SUCCESS) {
  320. efi_st_error("UninstallProtocolInterface failed\n");
  321. return EFI_ST_FAILURE;
  322. }
  323. return EFI_ST_SUCCESS;
  324. }
  325. EFI_UNIT_TEST(protserv) = {
  326. .name = "manage protocols",
  327. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  328. .setup = setup,
  329. .execute = execute,
  330. .teardown = teardown,
  331. };