efi_device_path_to_text.c 8.2 KB

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