efi_device_path_to_text.c 8.2 KB

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