efi_device_path_to_text.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_NVME: {
  137. struct efi_device_path_nvme *ndp =
  138. (struct efi_device_path_nvme *)dp;
  139. u32 ns_id;
  140. int i;
  141. memcpy(&ns_id, &ndp->ns_id, sizeof(ns_id));
  142. s += sprintf(s, "NVMe(0x%x,", ns_id);
  143. for (i = 0; i < sizeof(ndp->eui64); ++i)
  144. s += sprintf(s, "%s%02x", i ? "-" : "",
  145. ndp->eui64[i]);
  146. s += sprintf(s, ")");
  147. break;
  148. }
  149. case DEVICE_PATH_SUB_TYPE_MSG_SD:
  150. case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
  151. const char *typename =
  152. (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
  153. "SD" : "eMMC";
  154. struct efi_device_path_sd_mmc_path *sddp =
  155. (struct efi_device_path_sd_mmc_path *)dp;
  156. s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
  157. break;
  158. }
  159. default:
  160. s = dp_unknown(s, dp);
  161. break;
  162. }
  163. return s;
  164. }
  165. /*
  166. * Convert a media device path node to text.
  167. *
  168. * @s output buffer
  169. * @dp device path node
  170. * @return next unused buffer address
  171. */
  172. static char *dp_media(char *s, struct efi_device_path *dp)
  173. {
  174. switch (dp->sub_type) {
  175. case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
  176. struct efi_device_path_hard_drive_path *hddp =
  177. (struct efi_device_path_hard_drive_path *)dp;
  178. void *sig = hddp->partition_signature;
  179. u64 start;
  180. u64 end;
  181. /* Copy from packed structure to aligned memory */
  182. memcpy(&start, &hddp->partition_start, sizeof(start));
  183. memcpy(&end, &hddp->partition_end, sizeof(end));
  184. switch (hddp->signature_type) {
  185. case SIG_TYPE_MBR: {
  186. u32 signature;
  187. memcpy(&signature, sig, sizeof(signature));
  188. s += sprintf(
  189. s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
  190. hddp->partition_number, signature, start, end);
  191. break;
  192. }
  193. case SIG_TYPE_GUID:
  194. s += sprintf(
  195. s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
  196. hddp->partition_number, sig, start, end);
  197. break;
  198. default:
  199. s += sprintf(
  200. s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
  201. hddp->partition_number, hddp->partmap_type,
  202. start, end);
  203. break;
  204. }
  205. break;
  206. }
  207. case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
  208. struct efi_device_path_cdrom_path *cddp =
  209. (struct efi_device_path_cdrom_path *)dp;
  210. s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
  211. cddp->partition_start, cddp->partition_size);
  212. break;
  213. }
  214. case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
  215. struct efi_device_path_file_path *fp =
  216. (struct efi_device_path_file_path *)dp;
  217. int slen = (dp->length - sizeof(*dp)) / 2;
  218. if (slen > MAX_NODE_LEN - 2)
  219. slen = MAX_NODE_LEN - 2;
  220. s += sprintf(s, "%-.*ls", slen, fp->str);
  221. break;
  222. }
  223. default:
  224. s = dp_unknown(s, dp);
  225. break;
  226. }
  227. return s;
  228. }
  229. /*
  230. * Converts a single node to a char string.
  231. *
  232. * @buffer output buffer
  233. * @dp device path or node
  234. * @return end of string
  235. */
  236. static char *efi_convert_single_device_node_to_text(
  237. char *buffer,
  238. struct efi_device_path *dp)
  239. {
  240. char *str = buffer;
  241. switch (dp->type) {
  242. case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
  243. str = dp_hardware(str, dp);
  244. break;
  245. case DEVICE_PATH_TYPE_ACPI_DEVICE:
  246. str = dp_acpi(str, dp);
  247. break;
  248. case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
  249. str = dp_msging(str, dp);
  250. break;
  251. case DEVICE_PATH_TYPE_MEDIA_DEVICE:
  252. str = dp_media(str, dp);
  253. break;
  254. case DEVICE_PATH_TYPE_END:
  255. break;
  256. default:
  257. str = dp_unknown(str, dp);
  258. }
  259. *str = '\0';
  260. return str;
  261. }
  262. /*
  263. * This function implements the ConvertDeviceNodeToText service of the
  264. * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
  265. * See the Unified Extensible Firmware Interface (UEFI) specification
  266. * for details.
  267. *
  268. * device_node device node to be converted
  269. * display_only true if the shorter text representation shall be used
  270. * allow_shortcuts true if shortcut forms may be used
  271. * @return text representation of the device path
  272. * NULL if out of memory of device_path is NULL
  273. */
  274. static uint16_t EFIAPI *efi_convert_device_node_to_text(
  275. struct efi_device_path *device_node,
  276. bool display_only,
  277. bool allow_shortcuts)
  278. {
  279. char str[MAX_NODE_LEN];
  280. uint16_t *text = NULL;
  281. EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
  282. if (!device_node)
  283. goto out;
  284. efi_convert_single_device_node_to_text(str, device_node);
  285. text = efi_str_to_u16(str);
  286. out:
  287. EFI_EXIT(EFI_SUCCESS);
  288. return text;
  289. }
  290. /*
  291. * This function implements the ConvertDevicePathToText service of the
  292. * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
  293. * See the Unified Extensible Firmware Interface (UEFI) specification
  294. * for details.
  295. *
  296. * device_path device path to be converted
  297. * display_only true if the shorter text representation shall be used
  298. * allow_shortcuts true if shortcut forms may be used
  299. * @return text representation of the device path
  300. * NULL if out of memory of device_path is NULL
  301. */
  302. static uint16_t EFIAPI *efi_convert_device_path_to_text(
  303. struct efi_device_path *device_path,
  304. bool display_only,
  305. bool allow_shortcuts)
  306. {
  307. uint16_t *text = NULL;
  308. char buffer[MAX_PATH_LEN];
  309. char *str = buffer;
  310. EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
  311. if (!device_path)
  312. goto out;
  313. while (device_path &&
  314. str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
  315. *str++ = '/';
  316. str = efi_convert_single_device_node_to_text(str, device_path);
  317. device_path = efi_dp_next(device_path);
  318. }
  319. text = efi_str_to_u16(buffer);
  320. out:
  321. EFI_EXIT(EFI_SUCCESS);
  322. return text;
  323. }
  324. /* helper for debug prints.. efi_free_pool() the result. */
  325. uint16_t *efi_dp_str(struct efi_device_path *dp)
  326. {
  327. return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
  328. }
  329. const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
  330. .convert_device_node_to_text = efi_convert_device_node_to_text,
  331. .convert_device_path_to_text = efi_convert_device_path_to_text,
  332. };