efi_device_path_to_text.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_ALLOCATE_ANY_PAGES, 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. if (n > 0) {
  62. s += sprintf(s, ",");
  63. for (i = 0; i < n; ++i)
  64. s += sprintf(s, "%02x", vdp->vendor_data[i]);
  65. }
  66. s += sprintf(s, ")");
  67. break;
  68. }
  69. default:
  70. s = dp_unknown(s, dp);
  71. break;
  72. }
  73. return s;
  74. }
  75. static char *dp_acpi(char *s, struct efi_device_path *dp)
  76. {
  77. switch (dp->sub_type) {
  78. case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
  79. struct efi_device_path_acpi_path *adp =
  80. (struct efi_device_path_acpi_path *)dp;
  81. s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
  82. adp->uid);
  83. break;
  84. }
  85. default:
  86. s = dp_unknown(s, dp);
  87. break;
  88. }
  89. return s;
  90. }
  91. static char *dp_msging(char *s, struct efi_device_path *dp)
  92. {
  93. switch (dp->sub_type) {
  94. case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
  95. struct efi_device_path_atapi *ide =
  96. (struct efi_device_path_atapi *)dp;
  97. s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
  98. ide->slave_master, ide->logical_unit_number);
  99. break;
  100. }
  101. case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
  102. struct efi_device_path_scsi *ide =
  103. (struct efi_device_path_scsi *)dp;
  104. s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
  105. ide->logical_unit_number);
  106. break;
  107. }
  108. case DEVICE_PATH_SUB_TYPE_MSG_USB: {
  109. struct efi_device_path_usb *udp =
  110. (struct efi_device_path_usb *)dp;
  111. s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
  112. udp->usb_interface);
  113. break;
  114. }
  115. case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
  116. int i, n = sizeof(struct efi_mac_addr);
  117. struct efi_device_path_mac_addr *mdp =
  118. (struct efi_device_path_mac_addr *)dp;
  119. if (mdp->if_type <= 1)
  120. n = 6;
  121. s += sprintf(s, "MAC(");
  122. for (i = 0; i < n; ++i)
  123. s += sprintf(s, "%02x", mdp->mac.addr[i]);
  124. s += sprintf(s, ",%u)", mdp->if_type);
  125. break;
  126. }
  127. case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
  128. struct efi_device_path_usb_class *ucdp =
  129. (struct efi_device_path_usb_class *)dp;
  130. s += sprintf(s, "UsbClass(0x%x,0x%x,0x%x,0x%x,0x%x)",
  131. ucdp->vendor_id, ucdp->product_id,
  132. ucdp->device_class, ucdp->device_subclass,
  133. ucdp->device_protocol);
  134. break;
  135. }
  136. case DEVICE_PATH_SUB_TYPE_MSG_SATA: {
  137. struct efi_device_path_sata *sdp =
  138. (struct efi_device_path_sata *) dp;
  139. s += sprintf(s, "Sata(0x%x,0x%x,0x%x)",
  140. sdp->hba_port,
  141. sdp->port_multiplier_port,
  142. sdp->logical_unit_number);
  143. break;
  144. }
  145. case DEVICE_PATH_SUB_TYPE_MSG_NVME: {
  146. struct efi_device_path_nvme *ndp =
  147. (struct efi_device_path_nvme *)dp;
  148. u32 ns_id;
  149. int i;
  150. memcpy(&ns_id, &ndp->ns_id, sizeof(ns_id));
  151. s += sprintf(s, "NVMe(0x%x,", ns_id);
  152. for (i = 0; i < sizeof(ndp->eui64); ++i)
  153. s += sprintf(s, "%s%02x", i ? "-" : "",
  154. ndp->eui64[i]);
  155. s += sprintf(s, ")");
  156. break;
  157. }
  158. case DEVICE_PATH_SUB_TYPE_MSG_SD:
  159. case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
  160. const char *typename =
  161. (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
  162. "SD" : "eMMC";
  163. struct efi_device_path_sd_mmc_path *sddp =
  164. (struct efi_device_path_sd_mmc_path *)dp;
  165. s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
  166. break;
  167. }
  168. default:
  169. s = dp_unknown(s, dp);
  170. break;
  171. }
  172. return s;
  173. }
  174. /*
  175. * Convert a media device path node to text.
  176. *
  177. * @s output buffer
  178. * @dp device path node
  179. * @return next unused buffer address
  180. */
  181. static char *dp_media(char *s, struct efi_device_path *dp)
  182. {
  183. switch (dp->sub_type) {
  184. case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
  185. struct efi_device_path_hard_drive_path *hddp =
  186. (struct efi_device_path_hard_drive_path *)dp;
  187. void *sig = hddp->partition_signature;
  188. u64 start;
  189. u64 end;
  190. /* Copy from packed structure to aligned memory */
  191. memcpy(&start, &hddp->partition_start, sizeof(start));
  192. memcpy(&end, &hddp->partition_end, sizeof(end));
  193. switch (hddp->signature_type) {
  194. case SIG_TYPE_MBR: {
  195. u32 signature;
  196. memcpy(&signature, sig, sizeof(signature));
  197. s += sprintf(
  198. s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
  199. hddp->partition_number, signature, start, end);
  200. break;
  201. }
  202. case SIG_TYPE_GUID:
  203. s += sprintf(
  204. s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
  205. hddp->partition_number, sig, start, end);
  206. break;
  207. default:
  208. s += sprintf(
  209. s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
  210. hddp->partition_number, hddp->partmap_type,
  211. start, end);
  212. break;
  213. }
  214. break;
  215. }
  216. case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
  217. struct efi_device_path_cdrom_path *cddp =
  218. (struct efi_device_path_cdrom_path *)dp;
  219. s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
  220. cddp->partition_start, cddp->partition_size);
  221. break;
  222. }
  223. case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
  224. struct efi_device_path_file_path *fp =
  225. (struct efi_device_path_file_path *)dp;
  226. int slen = (dp->length - sizeof(*dp)) / 2;
  227. if (slen > MAX_NODE_LEN - 2)
  228. slen = MAX_NODE_LEN - 2;
  229. s += sprintf(s, "%-.*ls", slen, fp->str);
  230. break;
  231. }
  232. default:
  233. s = dp_unknown(s, dp);
  234. break;
  235. }
  236. return s;
  237. }
  238. /*
  239. * Converts a single node to a char string.
  240. *
  241. * @buffer output buffer
  242. * @dp device path or node
  243. * @return end of string
  244. */
  245. static char *efi_convert_single_device_node_to_text(
  246. char *buffer,
  247. struct efi_device_path *dp)
  248. {
  249. char *str = buffer;
  250. switch (dp->type) {
  251. case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
  252. str = dp_hardware(str, dp);
  253. break;
  254. case DEVICE_PATH_TYPE_ACPI_DEVICE:
  255. str = dp_acpi(str, dp);
  256. break;
  257. case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
  258. str = dp_msging(str, dp);
  259. break;
  260. case DEVICE_PATH_TYPE_MEDIA_DEVICE:
  261. str = dp_media(str, dp);
  262. break;
  263. case DEVICE_PATH_TYPE_END:
  264. break;
  265. default:
  266. str = dp_unknown(str, dp);
  267. }
  268. *str = '\0';
  269. return str;
  270. }
  271. /*
  272. * This function implements the ConvertDeviceNodeToText service of the
  273. * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
  274. * See the Unified Extensible Firmware Interface (UEFI) specification
  275. * for details.
  276. *
  277. * device_node device node to be converted
  278. * display_only true if the shorter text representation shall be used
  279. * allow_shortcuts true if shortcut forms may be used
  280. * @return text representation of the device path
  281. * NULL if out of memory of device_path is NULL
  282. */
  283. static uint16_t EFIAPI *efi_convert_device_node_to_text(
  284. struct efi_device_path *device_node,
  285. bool display_only,
  286. bool allow_shortcuts)
  287. {
  288. char str[MAX_NODE_LEN];
  289. uint16_t *text = NULL;
  290. EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
  291. if (!device_node)
  292. goto out;
  293. efi_convert_single_device_node_to_text(str, device_node);
  294. text = efi_str_to_u16(str);
  295. out:
  296. EFI_EXIT(EFI_SUCCESS);
  297. return text;
  298. }
  299. /*
  300. * This function implements the ConvertDevicePathToText service of the
  301. * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
  302. * See the Unified Extensible Firmware Interface (UEFI) specification
  303. * for details.
  304. *
  305. * device_path device path to be converted
  306. * display_only true if the shorter text representation shall be used
  307. * allow_shortcuts true if shortcut forms may be used
  308. * @return text representation of the device path
  309. * NULL if out of memory of device_path is NULL
  310. */
  311. static uint16_t EFIAPI *efi_convert_device_path_to_text(
  312. struct efi_device_path *device_path,
  313. bool display_only,
  314. bool allow_shortcuts)
  315. {
  316. uint16_t *text = NULL;
  317. char buffer[MAX_PATH_LEN];
  318. char *str = buffer;
  319. EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
  320. if (!device_path)
  321. goto out;
  322. while (device_path &&
  323. str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
  324. *str++ = '/';
  325. str = efi_convert_single_device_node_to_text(str, device_path);
  326. device_path = efi_dp_next(device_path);
  327. }
  328. text = efi_str_to_u16(buffer);
  329. out:
  330. EFI_EXIT(EFI_SUCCESS);
  331. return text;
  332. }
  333. /* helper for debug prints.. efi_free_pool() the result. */
  334. uint16_t *efi_dp_str(struct efi_device_path *dp)
  335. {
  336. return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
  337. }
  338. const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
  339. .convert_device_node_to_text = efi_convert_device_node_to_text,
  340. .convert_device_path_to_text = efi_convert_device_path_to_text,
  341. };