efi_selftest_devicepath.c 11 KB

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