efi_device_path_to_text.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * EFI device path interface
  3. *
  4. * Copyright (c) 2017 Heinrich Schuchardt
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #define MAC_OUTPUT_LEN 22
  11. #define UNKNOWN_OUTPUT_LEN 23
  12. const efi_guid_t efi_guid_device_path_to_text_protocol =
  13. EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
  14. static char *dp_unknown(char *s, struct efi_device_path *dp)
  15. {
  16. s += sprintf(s, "/UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
  17. return s;
  18. }
  19. static char *dp_hardware(char *s, struct efi_device_path *dp)
  20. {
  21. switch (dp->sub_type) {
  22. case DEVICE_PATH_SUB_TYPE_VENDOR: {
  23. struct efi_device_path_vendor *vdp =
  24. (struct efi_device_path_vendor *)dp;
  25. s += sprintf(s, "/VenHw(%pUl)", &vdp->guid);
  26. break;
  27. }
  28. default:
  29. s = dp_unknown(s, dp);
  30. break;
  31. }
  32. return s;
  33. }
  34. static char *dp_acpi(char *s, struct efi_device_path *dp)
  35. {
  36. switch (dp->sub_type) {
  37. case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
  38. struct efi_device_path_acpi_path *adp =
  39. (struct efi_device_path_acpi_path *)dp;
  40. s += sprintf(s, "/Acpi(PNP%04x", EISA_PNP_NUM(adp->hid));
  41. if (adp->uid)
  42. s += sprintf(s, ",%d", adp->uid);
  43. s += sprintf(s, ")");
  44. break;
  45. }
  46. default:
  47. s = dp_unknown(s, dp);
  48. break;
  49. }
  50. return s;
  51. }
  52. static char *dp_msging(char *s, struct efi_device_path *dp)
  53. {
  54. switch (dp->sub_type) {
  55. case DEVICE_PATH_SUB_TYPE_MSG_USB: {
  56. struct efi_device_path_usb *udp =
  57. (struct efi_device_path_usb *)dp;
  58. s += sprintf(s, "/Usb(0x%x,0x%x)", udp->parent_port_number,
  59. udp->usb_interface);
  60. break;
  61. }
  62. case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
  63. struct efi_device_path_mac_addr *mdp =
  64. (struct efi_device_path_mac_addr *)dp;
  65. if (mdp->if_type != 0 && mdp->if_type != 1)
  66. break;
  67. s += sprintf(s, "/MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
  68. mdp->mac.addr[0], mdp->mac.addr[1],
  69. mdp->mac.addr[2], mdp->mac.addr[3],
  70. mdp->mac.addr[4], mdp->mac.addr[5],
  71. mdp->if_type);
  72. break;
  73. }
  74. case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
  75. struct efi_device_path_usb_class *ucdp =
  76. (struct efi_device_path_usb_class *)dp;
  77. s += sprintf(s, "/USBClass(%x,%x,%x,%x,%x)",
  78. ucdp->vendor_id, ucdp->product_id,
  79. ucdp->device_class, ucdp->device_subclass,
  80. ucdp->device_protocol);
  81. break;
  82. }
  83. case DEVICE_PATH_SUB_TYPE_MSG_SD:
  84. case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
  85. const char *typename =
  86. (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
  87. "SDCard" : "MMC";
  88. struct efi_device_path_sd_mmc_path *sddp =
  89. (struct efi_device_path_sd_mmc_path *)dp;
  90. s += sprintf(s, "/%s(Slot%u)", typename, sddp->slot_number);
  91. break;
  92. }
  93. default:
  94. s = dp_unknown(s, dp);
  95. break;
  96. }
  97. return s;
  98. }
  99. static char *dp_media(char *s, struct efi_device_path *dp)
  100. {
  101. switch (dp->sub_type) {
  102. case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
  103. struct efi_device_path_hard_drive_path *hddp =
  104. (struct efi_device_path_hard_drive_path *)dp;
  105. void *sig = hddp->partition_signature;
  106. switch (hddp->signature_type) {
  107. case SIG_TYPE_MBR:
  108. s += sprintf(s, "/HD(Part%d,Sig%08x)",
  109. hddp->partition_number,
  110. *(uint32_t *)sig);
  111. break;
  112. case SIG_TYPE_GUID:
  113. s += sprintf(s, "/HD(Part%d,Sig%pUl)",
  114. hddp->partition_number, sig);
  115. default:
  116. s += sprintf(s, "/HD(Part%d,MBRType=%02x,SigType=%02x)",
  117. hddp->partition_number, hddp->partmap_type,
  118. hddp->signature_type);
  119. }
  120. break;
  121. }
  122. case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
  123. struct efi_device_path_cdrom_path *cddp =
  124. (struct efi_device_path_cdrom_path *)dp;
  125. s += sprintf(s, "/CDROM(0x%x)", cddp->boot_entry);
  126. break;
  127. }
  128. case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
  129. struct efi_device_path_file_path *fp =
  130. (struct efi_device_path_file_path *)dp;
  131. int slen = (dp->length - sizeof(*dp)) / 2;
  132. s += sprintf(s, "/%-*ls", slen, fp->str);
  133. break;
  134. }
  135. default:
  136. s = dp_unknown(s, dp);
  137. break;
  138. }
  139. return s;
  140. }
  141. static uint16_t *efi_convert_device_node_to_text(
  142. struct efi_device_path *dp,
  143. bool display_only,
  144. bool allow_shortcuts)
  145. {
  146. unsigned long len;
  147. efi_status_t r;
  148. char buf[512]; /* this ought be be big enough for worst case */
  149. char *str = buf;
  150. uint16_t *out;
  151. while (dp) {
  152. switch (dp->type) {
  153. case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
  154. str = dp_hardware(str, dp);
  155. break;
  156. case DEVICE_PATH_TYPE_ACPI_DEVICE:
  157. str = dp_acpi(str, dp);
  158. break;
  159. case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
  160. str = dp_msging(str, dp);
  161. break;
  162. case DEVICE_PATH_TYPE_MEDIA_DEVICE:
  163. str = dp_media(str, dp);
  164. break;
  165. default:
  166. str = dp_unknown(str, dp);
  167. }
  168. dp = efi_dp_next(dp);
  169. }
  170. *str++ = '\0';
  171. len = str - buf;
  172. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, 2 * len, (void **)&out);
  173. if (r != EFI_SUCCESS)
  174. return NULL;
  175. ascii2unicode(out, buf);
  176. out[len - 1] = 0;
  177. return out;
  178. }
  179. /* helper for debug prints.. efi_free_pool() the result. */
  180. uint16_t *efi_dp_str(struct efi_device_path *dp)
  181. {
  182. return efi_convert_device_node_to_text(dp, true, true);
  183. }
  184. static uint16_t EFIAPI *efi_convert_device_node_to_text_ext(
  185. struct efi_device_path *device_node,
  186. bool display_only,
  187. bool allow_shortcuts)
  188. {
  189. uint16_t *buffer;
  190. EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
  191. buffer = efi_convert_device_node_to_text(device_node, display_only,
  192. allow_shortcuts);
  193. EFI_EXIT(EFI_SUCCESS);
  194. return buffer;
  195. }
  196. static uint16_t EFIAPI *efi_convert_device_path_to_text(
  197. struct efi_device_path *device_path,
  198. bool display_only,
  199. bool allow_shortcuts)
  200. {
  201. uint16_t *buffer;
  202. EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
  203. /*
  204. * Our device paths are all of depth one. So its is sufficient to
  205. * to convert the first node.
  206. */
  207. buffer = efi_convert_device_node_to_text(device_path, display_only,
  208. allow_shortcuts);
  209. EFI_EXIT(EFI_SUCCESS);
  210. return buffer;
  211. }
  212. const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
  213. .convert_device_node_to_text = efi_convert_device_node_to_text_ext,
  214. .convert_device_path_to_text = efi_convert_device_path_to_text,
  215. };