efi_device_path_to_text.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI device path interface
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt
  6. */
  7. #include <common.h>
  8. #include <blk.h>
  9. #include <efi_loader.h>
  10. #define MAC_OUTPUT_LEN 22
  11. #define UNKNOWN_OUTPUT_LEN 23
  12. #define MAX_NODE_LEN 512
  13. #define MAX_PATH_LEN 1024
  14. const efi_guid_t efi_guid_device_path_to_text_protocol =
  15. EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
  16. /**
  17. * efi_str_to_u16() - convert ASCII string to UTF-16
  18. *
  19. * A u16 buffer is allocated from pool. The ASCII string is copied to the u16
  20. * buffer.
  21. *
  22. * @str: ASCII string
  23. * Return: UTF-16 string. NULL if out of memory.
  24. */
  25. static u16 *efi_str_to_u16(char *str)
  26. {
  27. efi_uintn_t len;
  28. u16 *out, *dst;
  29. efi_status_t ret;
  30. len = sizeof(u16) * (utf8_utf16_strlen(str) + 1);
  31. ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, len, (void **)&out);
  32. if (ret != EFI_SUCCESS)
  33. return NULL;
  34. dst = out;
  35. utf8_utf16_strcpy(&dst, str);
  36. return out;
  37. }
  38. static char *dp_unknown(char *s, struct efi_device_path *dp)
  39. {
  40. s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
  41. return s;
  42. }
  43. static char *dp_hardware(char *s, struct efi_device_path *dp)
  44. {
  45. switch (dp->sub_type) {
  46. case DEVICE_PATH_SUB_TYPE_MEMORY: {
  47. struct efi_device_path_memory *mdp =
  48. (struct efi_device_path_memory *)dp;
  49. s += sprintf(s, "MemoryMapped(0x%x,0x%llx,0x%llx)",
  50. mdp->memory_type,
  51. mdp->start_address,
  52. mdp->end_address);
  53. break;
  54. }
  55. case DEVICE_PATH_SUB_TYPE_VENDOR: {
  56. int i, n;
  57. struct efi_device_path_vendor *vdp =
  58. (struct efi_device_path_vendor *)dp;
  59. s += sprintf(s, "VenHw(%pUl", &vdp->guid);
  60. n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
  61. /* Node must fit into MAX_NODE_LEN) */
  62. if (n > 0 && n < MAX_NODE_LEN / 2 - 22) {
  63. s += sprintf(s, ",");
  64. for (i = 0; i < n; ++i)
  65. s += sprintf(s, "%02x", vdp->vendor_data[i]);
  66. }
  67. s += sprintf(s, ")");
  68. break;
  69. }
  70. default:
  71. s = dp_unknown(s, dp);
  72. break;
  73. }
  74. return s;
  75. }
  76. static char *dp_acpi(char *s, struct efi_device_path *dp)
  77. {
  78. switch (dp->sub_type) {
  79. case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
  80. struct efi_device_path_acpi_path *adp =
  81. (struct efi_device_path_acpi_path *)dp;
  82. s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
  83. adp->uid);
  84. break;
  85. }
  86. default:
  87. s = dp_unknown(s, dp);
  88. break;
  89. }
  90. return s;
  91. }
  92. static char *dp_msging(char *s, struct efi_device_path *dp)
  93. {
  94. switch (dp->sub_type) {
  95. case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
  96. struct efi_device_path_atapi *ide =
  97. (struct efi_device_path_atapi *)dp;
  98. s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
  99. ide->slave_master, ide->logical_unit_number);
  100. break;
  101. }
  102. case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
  103. struct efi_device_path_scsi *ide =
  104. (struct efi_device_path_scsi *)dp;
  105. s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
  106. ide->logical_unit_number);
  107. break;
  108. }
  109. case DEVICE_PATH_SUB_TYPE_MSG_UART: {
  110. struct efi_device_path_uart *uart =
  111. (struct efi_device_path_uart *)dp;
  112. s += sprintf(s, "Uart(%lld,%d,%d,", uart->baud_rate,
  113. uart->data_bits, uart->parity);
  114. switch (uart->stop_bits) {
  115. case 2:
  116. s += sprintf(s, "1.5)");
  117. break;
  118. default:
  119. s += sprintf(s, "%d)", uart->stop_bits);
  120. break;
  121. }
  122. break;
  123. }
  124. case DEVICE_PATH_SUB_TYPE_MSG_USB: {
  125. struct efi_device_path_usb *udp =
  126. (struct efi_device_path_usb *)dp;
  127. s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
  128. udp->usb_interface);
  129. break;
  130. }
  131. case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
  132. int i, n = sizeof(struct efi_mac_addr);
  133. struct efi_device_path_mac_addr *mdp =
  134. (struct efi_device_path_mac_addr *)dp;
  135. if (mdp->if_type <= 1)
  136. n = 6;
  137. s += sprintf(s, "MAC(");
  138. for (i = 0; i < n; ++i)
  139. s += sprintf(s, "%02x", mdp->mac.addr[i]);
  140. s += sprintf(s, ",%u)", mdp->if_type);
  141. break;
  142. }
  143. case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
  144. struct efi_device_path_usb_class *ucdp =
  145. (struct efi_device_path_usb_class *)dp;
  146. s += sprintf(s, "UsbClass(0x%x,0x%x,0x%x,0x%x,0x%x)",
  147. ucdp->vendor_id, ucdp->product_id,
  148. ucdp->device_class, ucdp->device_subclass,
  149. ucdp->device_protocol);
  150. break;
  151. }
  152. case DEVICE_PATH_SUB_TYPE_MSG_SATA: {
  153. struct efi_device_path_sata *sdp =
  154. (struct efi_device_path_sata *) dp;
  155. s += sprintf(s, "Sata(0x%x,0x%x,0x%x)",
  156. sdp->hba_port,
  157. sdp->port_multiplier_port,
  158. sdp->logical_unit_number);
  159. break;
  160. }
  161. case DEVICE_PATH_SUB_TYPE_MSG_NVME: {
  162. struct efi_device_path_nvme *ndp =
  163. (struct efi_device_path_nvme *)dp;
  164. u32 ns_id;
  165. int i;
  166. memcpy(&ns_id, &ndp->ns_id, sizeof(ns_id));
  167. s += sprintf(s, "NVMe(0x%x,", ns_id);
  168. for (i = 0; i < sizeof(ndp->eui64); ++i)
  169. s += sprintf(s, "%s%02x", i ? "-" : "",
  170. ndp->eui64[i]);
  171. s += sprintf(s, ")");
  172. break;
  173. }
  174. case DEVICE_PATH_SUB_TYPE_MSG_URI: {
  175. struct efi_device_path_uri *udp =
  176. (struct efi_device_path_uri *)dp;
  177. int n;
  178. n = (int)udp->dp.length - sizeof(struct efi_device_path_uri);
  179. s += sprintf(s, "Uri(");
  180. if (n > 0 && n < MAX_NODE_LEN - 6)
  181. s += snprintf(s, n, "%s", (char *)udp->uri);
  182. s += sprintf(s, ")");
  183. break;
  184. }
  185. case DEVICE_PATH_SUB_TYPE_MSG_SD:
  186. case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
  187. const char *typename =
  188. (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
  189. "SD" : "eMMC";
  190. struct efi_device_path_sd_mmc_path *sddp =
  191. (struct efi_device_path_sd_mmc_path *)dp;
  192. s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
  193. break;
  194. }
  195. default:
  196. s = dp_unknown(s, dp);
  197. break;
  198. }
  199. return s;
  200. }
  201. /*
  202. * Convert a media device path node to text.
  203. *
  204. * @s output buffer
  205. * @dp device path node
  206. * @return next unused buffer address
  207. */
  208. static char *dp_media(char *s, struct efi_device_path *dp)
  209. {
  210. switch (dp->sub_type) {
  211. case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
  212. struct efi_device_path_hard_drive_path *hddp =
  213. (struct efi_device_path_hard_drive_path *)dp;
  214. void *sig = hddp->partition_signature;
  215. u64 start;
  216. u64 end;
  217. /* Copy from packed structure to aligned memory */
  218. memcpy(&start, &hddp->partition_start, sizeof(start));
  219. memcpy(&end, &hddp->partition_end, sizeof(end));
  220. switch (hddp->signature_type) {
  221. case SIG_TYPE_MBR: {
  222. u32 signature;
  223. memcpy(&signature, sig, sizeof(signature));
  224. s += sprintf(
  225. s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
  226. hddp->partition_number, signature, start, end);
  227. break;
  228. }
  229. case SIG_TYPE_GUID:
  230. s += sprintf(
  231. s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
  232. hddp->partition_number, sig, start, end);
  233. break;
  234. default:
  235. s += sprintf(
  236. s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
  237. hddp->partition_number, hddp->partmap_type,
  238. start, end);
  239. break;
  240. }
  241. break;
  242. }
  243. case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
  244. struct efi_device_path_cdrom_path *cddp =
  245. (struct efi_device_path_cdrom_path *)dp;
  246. s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
  247. cddp->partition_start, cddp->partition_size);
  248. break;
  249. }
  250. case DEVICE_PATH_SUB_TYPE_VENDOR_PATH: {
  251. int i, n;
  252. struct efi_device_path_vendor *vdp =
  253. (struct efi_device_path_vendor *)dp;
  254. s += sprintf(s, "VenMedia(%pUl", &vdp->guid);
  255. n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
  256. /* Node must fit into MAX_NODE_LEN) */
  257. if (n > 0 && n < MAX_NODE_LEN / 2 - 24) {
  258. s += sprintf(s, ",");
  259. for (i = 0; i < n; ++i)
  260. s += sprintf(s, "%02x", vdp->vendor_data[i]);
  261. }
  262. s += sprintf(s, ")");
  263. break;
  264. }
  265. case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
  266. struct efi_device_path_file_path *fp =
  267. (struct efi_device_path_file_path *)dp;
  268. int slen = (dp->length - sizeof(*dp)) / 2;
  269. if (slen > MAX_NODE_LEN - 2)
  270. slen = MAX_NODE_LEN - 2;
  271. s += sprintf(s, "%-.*ls", slen, fp->str);
  272. break;
  273. }
  274. default:
  275. s = dp_unknown(s, dp);
  276. break;
  277. }
  278. return s;
  279. }
  280. /*
  281. * Converts a single node to a char string.
  282. *
  283. * @buffer output buffer
  284. * @dp device path or node
  285. * @return end of string
  286. */
  287. static char *efi_convert_single_device_node_to_text(
  288. char *buffer,
  289. struct efi_device_path *dp)
  290. {
  291. char *str = buffer;
  292. switch (dp->type) {
  293. case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
  294. str = dp_hardware(str, dp);
  295. break;
  296. case DEVICE_PATH_TYPE_ACPI_DEVICE:
  297. str = dp_acpi(str, dp);
  298. break;
  299. case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
  300. str = dp_msging(str, dp);
  301. break;
  302. case DEVICE_PATH_TYPE_MEDIA_DEVICE:
  303. str = dp_media(str, dp);
  304. break;
  305. case DEVICE_PATH_TYPE_END:
  306. break;
  307. default:
  308. str = dp_unknown(str, dp);
  309. }
  310. *str = '\0';
  311. return str;
  312. }
  313. /*
  314. * This function implements the ConvertDeviceNodeToText service of the
  315. * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
  316. * See the Unified Extensible Firmware Interface (UEFI) specification
  317. * for details.
  318. *
  319. * device_node device node to be converted
  320. * display_only true if the shorter text representation shall be used
  321. * allow_shortcuts true if shortcut forms may be used
  322. * @return text representation of the device path
  323. * NULL if out of memory of device_path is NULL
  324. */
  325. static uint16_t EFIAPI *efi_convert_device_node_to_text(
  326. struct efi_device_path *device_node,
  327. bool display_only,
  328. bool allow_shortcuts)
  329. {
  330. char str[MAX_NODE_LEN];
  331. uint16_t *text = NULL;
  332. EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
  333. if (!device_node)
  334. goto out;
  335. efi_convert_single_device_node_to_text(str, device_node);
  336. text = efi_str_to_u16(str);
  337. out:
  338. EFI_EXIT(EFI_SUCCESS);
  339. return text;
  340. }
  341. /*
  342. * This function implements the ConvertDevicePathToText service of the
  343. * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
  344. * See the Unified Extensible Firmware Interface (UEFI) specification
  345. * for details.
  346. *
  347. * device_path device path to be converted
  348. * display_only true if the shorter text representation shall be used
  349. * allow_shortcuts true if shortcut forms may be used
  350. * @return text representation of the device path
  351. * NULL if out of memory of device_path is NULL
  352. */
  353. static uint16_t EFIAPI *efi_convert_device_path_to_text(
  354. struct efi_device_path *device_path,
  355. bool display_only,
  356. bool allow_shortcuts)
  357. {
  358. uint16_t *text = NULL;
  359. char buffer[MAX_PATH_LEN];
  360. char *str = buffer;
  361. EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
  362. if (!device_path)
  363. goto out;
  364. while (device_path && str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
  365. if (device_path->type == DEVICE_PATH_TYPE_END) {
  366. if (device_path->sub_type !=
  367. DEVICE_PATH_SUB_TYPE_INSTANCE_END)
  368. break;
  369. *str++ = ',';
  370. } else {
  371. *str++ = '/';
  372. str = efi_convert_single_device_node_to_text(
  373. str, device_path);
  374. }
  375. *(u8 **)&device_path += device_path->length;
  376. }
  377. text = efi_str_to_u16(buffer);
  378. out:
  379. EFI_EXIT(EFI_SUCCESS);
  380. return text;
  381. }
  382. /* helper for debug prints.. efi_free_pool() the result. */
  383. uint16_t *efi_dp_str(struct efi_device_path *dp)
  384. {
  385. return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
  386. }
  387. const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
  388. .convert_device_node_to_text = efi_convert_device_node_to_text,
  389. .convert_device_path_to_text = efi_convert_device_path_to_text,
  390. };