efi_device_path_to_text.c 8.4 KB

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