efi_device_path_to_text.c 6.1 KB

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