efi_selftest_devicepath.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_devicepath
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This unit test checks the following protocol services:
  8. * DevicePathToText
  9. */
  10. #include <efi_selftest.h>
  11. static struct efi_boot_services *boottime;
  12. static efi_handle_t handle1;
  13. static efi_handle_t handle2;
  14. static efi_handle_t handle3;
  15. struct interface {
  16. void (EFIAPI * inc)(void);
  17. } interface;
  18. static efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
  19. static efi_guid_t guid_device_path_to_text_protocol =
  20. EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
  21. static efi_guid_t guid_protocol =
  22. EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
  23. 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0x7d);
  24. static efi_guid_t guid_vendor1 =
  25. EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
  26. 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xb1);
  27. static efi_guid_t guid_vendor2 =
  28. EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
  29. 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xa2);
  30. static efi_guid_t guid_vendor3 =
  31. EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
  32. 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xc3);
  33. static u8 *dp1;
  34. static u8 *dp2;
  35. static u8 *dp3;
  36. struct efi_device_path_to_text_protocol *device_path_to_text;
  37. /*
  38. * Setup unit test.
  39. *
  40. * Create three handles. Install a new protocol on two of them and
  41. * provide device paths.
  42. *
  43. * handle1
  44. * guid interface
  45. * handle2
  46. * guid interface
  47. * handle3
  48. *
  49. * @handle: handle of the loaded image
  50. * @systable: system table
  51. */
  52. static int setup(const efi_handle_t img_handle,
  53. const struct efi_system_table *systable)
  54. {
  55. struct efi_device_path_vendor vendor_node;
  56. struct efi_device_path end_node;
  57. efi_status_t ret;
  58. boottime = systable->boottime;
  59. ret = boottime->locate_protocol(&guid_device_path_to_text_protocol,
  60. NULL, (void **)&device_path_to_text);
  61. if (ret != EFI_SUCCESS) {
  62. device_path_to_text = NULL;
  63. efi_st_error(
  64. "Device path to text protocol is not available.\n");
  65. return EFI_ST_FAILURE;
  66. }
  67. ret = boottime->allocate_pool(EFI_LOADER_DATA,
  68. sizeof(struct efi_device_path_vendor) +
  69. sizeof(struct efi_device_path),
  70. (void **)&dp1);
  71. if (ret != EFI_SUCCESS)
  72. goto out_of_memory;
  73. ret = boottime->allocate_pool(EFI_LOADER_DATA, 2 *
  74. sizeof(struct efi_device_path_vendor) +
  75. sizeof(struct efi_device_path),
  76. (void **)&dp2);
  77. if (ret != EFI_SUCCESS)
  78. goto out_of_memory;
  79. ret = boottime->allocate_pool(EFI_LOADER_DATA, 3 *
  80. sizeof(struct efi_device_path_vendor) +
  81. sizeof(struct efi_device_path),
  82. (void **)&dp3);
  83. if (ret != EFI_SUCCESS)
  84. goto out_of_memory;
  85. vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  86. vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
  87. vendor_node.dp.length = sizeof(struct efi_device_path_vendor);
  88. boottime->copy_mem(&vendor_node.guid, &guid_vendor1,
  89. sizeof(efi_guid_t));
  90. boottime->copy_mem(dp1, &vendor_node,
  91. sizeof(struct efi_device_path_vendor));
  92. boottime->copy_mem(dp2, &vendor_node,
  93. sizeof(struct efi_device_path_vendor));
  94. boottime->copy_mem(dp3, &vendor_node,
  95. sizeof(struct efi_device_path_vendor));
  96. boottime->copy_mem(&vendor_node.guid, &guid_vendor2,
  97. sizeof(efi_guid_t));
  98. boottime->copy_mem(dp2 + sizeof(struct efi_device_path_vendor),
  99. &vendor_node, sizeof(struct efi_device_path_vendor));
  100. boottime->copy_mem(dp3 + sizeof(struct efi_device_path_vendor),
  101. &vendor_node, sizeof(struct efi_device_path_vendor));
  102. boottime->copy_mem(&vendor_node.guid, &guid_vendor3,
  103. sizeof(efi_guid_t));
  104. boottime->copy_mem(dp3 + 2 * sizeof(struct efi_device_path_vendor),
  105. &vendor_node, sizeof(struct efi_device_path_vendor));
  106. end_node.type = DEVICE_PATH_TYPE_END;
  107. end_node.sub_type = DEVICE_PATH_SUB_TYPE_END;
  108. end_node.length = sizeof(struct efi_device_path);
  109. boottime->copy_mem(dp1 + sizeof(struct efi_device_path_vendor),
  110. &end_node, sizeof(struct efi_device_path));
  111. boottime->copy_mem(dp2 + 2 * sizeof(struct efi_device_path_vendor),
  112. &end_node, sizeof(struct efi_device_path));
  113. boottime->copy_mem(dp3 + 3 * sizeof(struct efi_device_path_vendor),
  114. &end_node, sizeof(struct efi_device_path));
  115. ret = boottime->install_protocol_interface(&handle1,
  116. &guid_device_path,
  117. EFI_NATIVE_INTERFACE,
  118. dp1);
  119. if (ret != EFI_SUCCESS) {
  120. efi_st_error("InstallProtocolInterface failed\n");
  121. return EFI_ST_FAILURE;
  122. }
  123. ret = boottime->install_protocol_interface(&handle1,
  124. &guid_protocol,
  125. EFI_NATIVE_INTERFACE,
  126. &interface);
  127. if (ret != EFI_SUCCESS) {
  128. efi_st_error("InstallProtocolInterface failed\n");
  129. return EFI_ST_FAILURE;
  130. }
  131. ret = boottime->install_protocol_interface(&handle2,
  132. &guid_device_path,
  133. EFI_NATIVE_INTERFACE,
  134. dp2);
  135. if (ret != EFI_SUCCESS) {
  136. efi_st_error("InstallProtocolInterface failed\n");
  137. return EFI_ST_FAILURE;
  138. }
  139. ret = boottime->install_protocol_interface(&handle2,
  140. &guid_protocol,
  141. EFI_NATIVE_INTERFACE,
  142. &interface);
  143. if (ret != EFI_SUCCESS) {
  144. efi_st_error("InstallProtocolInterface failed\n");
  145. return EFI_ST_FAILURE;
  146. }
  147. ret = boottime->install_protocol_interface(&handle3,
  148. &guid_device_path,
  149. EFI_NATIVE_INTERFACE,
  150. dp3);
  151. if (ret != EFI_SUCCESS) {
  152. efi_st_error("InstallProtocolInterface failed\n");
  153. return EFI_ST_FAILURE;
  154. }
  155. return EFI_ST_SUCCESS;
  156. out_of_memory:
  157. efi_st_error("Out of memory\n");
  158. return EFI_ST_FAILURE;
  159. }
  160. /*
  161. * Tear down unit test.
  162. *
  163. */
  164. static int teardown(void)
  165. {
  166. efi_status_t ret;
  167. ret = boottime->uninstall_protocol_interface(handle1,
  168. &guid_device_path,
  169. dp1);
  170. if (ret != EFI_SUCCESS) {
  171. efi_st_error("UninstallProtocolInterface failed\n");
  172. return EFI_ST_FAILURE;
  173. }
  174. ret = boottime->uninstall_protocol_interface(handle1,
  175. &guid_protocol,
  176. &interface);
  177. if (ret != EFI_SUCCESS) {
  178. efi_st_error("UninstallProtocolInterface failed\n");
  179. return EFI_ST_FAILURE;
  180. }
  181. ret = boottime->uninstall_protocol_interface(handle2,
  182. &guid_device_path,
  183. dp2);
  184. if (ret != EFI_SUCCESS) {
  185. efi_st_error("UninstallProtocolInterface failed\n");
  186. return EFI_ST_FAILURE;
  187. }
  188. ret = boottime->uninstall_protocol_interface(handle2,
  189. &guid_protocol,
  190. &interface);
  191. if (ret != EFI_SUCCESS) {
  192. efi_st_error("UninstallProtocolInterface failed\n");
  193. return EFI_ST_FAILURE;
  194. }
  195. ret = boottime->uninstall_protocol_interface(handle3,
  196. &guid_device_path,
  197. dp3);
  198. if (ret != EFI_SUCCESS) {
  199. efi_st_error("UninstallProtocolInterface failed\n");
  200. return EFI_ST_FAILURE;
  201. }
  202. if (dp1) {
  203. ret = boottime->free_pool(dp1);
  204. if (ret != EFI_SUCCESS) {
  205. efi_st_error("FreePool failed\n");
  206. return EFI_ST_FAILURE;
  207. }
  208. }
  209. if (dp2) {
  210. ret = boottime->free_pool(dp2);
  211. if (ret != EFI_SUCCESS) {
  212. efi_st_error("FreePool failed\n");
  213. return EFI_ST_FAILURE;
  214. }
  215. }
  216. if (dp3) {
  217. ret = boottime->free_pool(dp3);
  218. if (ret != EFI_SUCCESS) {
  219. efi_st_error("FreePool failed\n");
  220. return EFI_ST_FAILURE;
  221. }
  222. }
  223. return EFI_ST_SUCCESS;
  224. }
  225. /*
  226. * Execute unit test.
  227. *
  228. */
  229. static int execute(void)
  230. {
  231. struct efi_device_path *remaining_dp;
  232. efi_handle_t handle;
  233. /*
  234. * This device path node ends with the letter 't' of 'u-boot'.
  235. * The following '.bin' does not belong to the node but is
  236. * helps to test the correct truncation.
  237. */
  238. struct {
  239. struct efi_device_path dp;
  240. u16 text[12];
  241. } __packed dp_node = {
  242. { DEVICE_PATH_TYPE_MEDIA_DEVICE,
  243. DEVICE_PATH_SUB_TYPE_FILE_PATH,
  244. sizeof(struct efi_device_path) + 12},
  245. L"u-boot.bin",
  246. };
  247. u16 *string;
  248. efi_status_t ret;
  249. efi_uintn_t i, no_handles;
  250. efi_handle_t *handles;
  251. struct efi_device_path *dp;
  252. /* Display all available device paths */
  253. ret = boottime->locate_handle_buffer(BY_PROTOCOL,
  254. &guid_device_path,
  255. NULL, &no_handles, &handles);
  256. if (ret != EFI_SUCCESS) {
  257. efi_st_error("Cannot retrieve device path protocols.\n");
  258. return EFI_ST_FAILURE;
  259. }
  260. efi_st_printf("Installed device path protocols:\n");
  261. for (i = 0; i < no_handles; ++i) {
  262. ret = boottime->open_protocol(handles[i], &guid_device_path,
  263. (void **)&dp, NULL, NULL,
  264. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  265. if (ret != EFI_SUCCESS) {
  266. efi_st_error("Cannot open device path protocol.\n");
  267. return EFI_ST_FAILURE;
  268. }
  269. string = device_path_to_text->convert_device_path_to_text(
  270. dp, true, false);
  271. if (!string) {
  272. efi_st_error("ConvertDevicePathToText failed\n");
  273. return EFI_ST_FAILURE;
  274. }
  275. efi_st_printf("%ps\n", string);
  276. ret = boottime->free_pool(string);
  277. if (ret != EFI_SUCCESS) {
  278. efi_st_error("FreePool failed\n");
  279. return EFI_ST_FAILURE;
  280. }
  281. /*
  282. * CloseProtocol cannot be called without agent handle.
  283. * There is no need to close the device path protocol.
  284. */
  285. }
  286. ret = boottime->free_pool(handles);
  287. if (ret != EFI_SUCCESS) {
  288. efi_st_error("FreePool failed\n");
  289. return EFI_ST_FAILURE;
  290. }
  291. /* Test ConvertDevicePathToText */
  292. string = device_path_to_text->convert_device_path_to_text(
  293. (struct efi_device_path *)dp2, true, false);
  294. if (!string) {
  295. efi_st_error("ConvertDevicePathToText failed\n");
  296. return EFI_ST_FAILURE;
  297. }
  298. if (efi_st_strcmp_16_8(
  299. string,
  300. "/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbb1)/VenHw(dbca4c98-6cb0-694d-0872-819c650cbba2)")
  301. ) {
  302. efi_st_printf("dp2: %ps\n", string);
  303. efi_st_error("Incorrect text from ConvertDevicePathToText\n");
  304. return EFI_ST_FAILURE;
  305. }
  306. ret = boottime->free_pool(string);
  307. if (ret != EFI_SUCCESS) {
  308. efi_st_error("FreePool failed\n");
  309. return EFI_ST_FAILURE;
  310. }
  311. /* Test ConvertDeviceNodeToText */
  312. string = device_path_to_text->convert_device_node_to_text(
  313. (struct efi_device_path *)&dp_node, true, false);
  314. if (!string) {
  315. efi_st_error("ConvertDeviceNodeToText failed\n");
  316. return EFI_ST_FAILURE;
  317. }
  318. if (efi_st_strcmp_16_8(string, "u-boot")) {
  319. efi_st_printf("dp_node: %ps\n", string);
  320. efi_st_error(
  321. "Incorrect conversion by ConvertDeviceNodeToText\n");
  322. return EFI_ST_FAILURE;
  323. }
  324. ret = boottime->free_pool(string);
  325. if (ret != EFI_SUCCESS) {
  326. efi_st_error("FreePool failed\n");
  327. return EFI_ST_FAILURE;
  328. }
  329. /* Test LocateDevicePath */
  330. remaining_dp = (struct efi_device_path *)dp3;
  331. ret = boottime->locate_device_path(&guid_protocol, &remaining_dp,
  332. &handle);
  333. if (ret != EFI_SUCCESS) {
  334. efi_st_error("LocateDevicePath failed\n");
  335. return EFI_ST_FAILURE;
  336. }
  337. if (handle != handle2) {
  338. efi_st_error("LocateDevicePath returned wrong handle\n");
  339. return EFI_ST_FAILURE;
  340. }
  341. string = device_path_to_text->convert_device_path_to_text(remaining_dp,
  342. true, false);
  343. if (!string) {
  344. efi_st_error("ConvertDevicePathToText failed\n");
  345. return EFI_ST_FAILURE;
  346. }
  347. if (efi_st_strcmp_16_8(string,
  348. "/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbc3)")
  349. ) {
  350. efi_st_printf("remaining device path: %ps\n", string);
  351. efi_st_error("LocateDevicePath: wrong remaining device path\n");
  352. return EFI_ST_FAILURE;
  353. }
  354. ret = boottime->free_pool(string);
  355. if (ret != EFI_SUCCESS) {
  356. efi_st_error("FreePool failed\n");
  357. return EFI_ST_FAILURE;
  358. }
  359. return EFI_ST_SUCCESS;
  360. }
  361. EFI_UNIT_TEST(devicepath) = {
  362. .name = "device path",
  363. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  364. .setup = setup,
  365. .execute = execute,
  366. .teardown = teardown,
  367. };