efi_device_path_to_text.c 9.0 KB

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