efi_device_path.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * EFI device path from u-boot device-model mapping
  3. *
  4. * (C) Copyright 2017 Rob Clark
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <blk.h>
  10. #include <dm.h>
  11. #include <usb.h>
  12. #include <mmc.h>
  13. #include <efi_loader.h>
  14. #include <inttypes.h>
  15. #include <part.h>
  16. /* template END node: */
  17. static const struct efi_device_path END = {
  18. .type = DEVICE_PATH_TYPE_END,
  19. .sub_type = DEVICE_PATH_SUB_TYPE_END,
  20. .length = sizeof(END),
  21. };
  22. #define U_BOOT_GUID \
  23. EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
  24. 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
  25. /* template ROOT node: */
  26. static const struct efi_device_path_vendor ROOT = {
  27. .dp = {
  28. .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  29. .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
  30. .length = sizeof(ROOT),
  31. },
  32. .guid = U_BOOT_GUID,
  33. };
  34. static void *dp_alloc(size_t sz)
  35. {
  36. void *buf;
  37. if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) != EFI_SUCCESS)
  38. return NULL;
  39. return buf;
  40. }
  41. /*
  42. * Iterate to next block in device-path, terminating (returning NULL)
  43. * at /End* node.
  44. */
  45. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
  46. {
  47. if (dp == NULL)
  48. return NULL;
  49. if (dp->type == DEVICE_PATH_TYPE_END)
  50. return NULL;
  51. dp = ((void *)dp) + dp->length;
  52. if (dp->type == DEVICE_PATH_TYPE_END)
  53. return NULL;
  54. return (struct efi_device_path *)dp;
  55. }
  56. /*
  57. * Compare two device-paths, stopping when the shorter of the two hits
  58. * an End* node. This is useful to, for example, compare a device-path
  59. * representing a device with one representing a file on the device, or
  60. * a device with a parent device.
  61. */
  62. int efi_dp_match(const struct efi_device_path *a,
  63. const struct efi_device_path *b)
  64. {
  65. while (1) {
  66. int ret;
  67. ret = memcmp(&a->length, &b->length, sizeof(a->length));
  68. if (ret)
  69. return ret;
  70. ret = memcmp(a, b, a->length);
  71. if (ret)
  72. return ret;
  73. a = efi_dp_next(a);
  74. b = efi_dp_next(b);
  75. if (!a || !b)
  76. return 0;
  77. }
  78. }
  79. /*
  80. * See UEFI spec (section 3.1.2, about short-form device-paths..
  81. * tl;dr: we can have a device-path that starts with a USB WWID
  82. * or USB Class node, and a few other cases which don't encode
  83. * the full device path with bus hierarchy:
  84. *
  85. * - MESSAGING:USB_WWID
  86. * - MESSAGING:USB_CLASS
  87. * - MEDIA:FILE_PATH
  88. * - MEDIA:HARD_DRIVE
  89. * - MESSAGING:URI
  90. */
  91. static struct efi_device_path *shorten_path(struct efi_device_path *dp)
  92. {
  93. while (dp) {
  94. /*
  95. * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
  96. * in practice fallback.efi just uses MEDIA:HARD_DRIVE
  97. * so not sure when we would see these other cases.
  98. */
  99. if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
  100. EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
  101. EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
  102. return dp;
  103. dp = efi_dp_next(dp);
  104. }
  105. return dp;
  106. }
  107. static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
  108. struct efi_device_path **rem)
  109. {
  110. struct efi_object *efiobj;
  111. unsigned int dp_size = efi_dp_size(dp);
  112. list_for_each_entry(efiobj, &efi_obj_list, link) {
  113. struct efi_handler *handler;
  114. struct efi_device_path *obj_dp;
  115. efi_status_t ret;
  116. ret = efi_search_protocol(efiobj->handle,
  117. &efi_guid_device_path, &handler);
  118. if (ret != EFI_SUCCESS)
  119. continue;
  120. obj_dp = handler->protocol_interface;
  121. do {
  122. if (efi_dp_match(dp, obj_dp) == 0) {
  123. if (rem) {
  124. /*
  125. * Allow partial matches, but inform
  126. * the caller.
  127. */
  128. *rem = ((void *)dp) +
  129. efi_dp_size(obj_dp);
  130. return efiobj;
  131. } else {
  132. /* Only return on exact matches */
  133. if (efi_dp_size(obj_dp) == dp_size)
  134. return efiobj;
  135. }
  136. }
  137. obj_dp = shorten_path(efi_dp_next(obj_dp));
  138. } while (short_path && obj_dp);
  139. }
  140. return NULL;
  141. }
  142. /*
  143. * Find an efiobj from device-path, if 'rem' is not NULL, returns the
  144. * remaining part of the device path after the matched object.
  145. */
  146. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  147. struct efi_device_path **rem)
  148. {
  149. struct efi_object *efiobj;
  150. /* Search for an exact match first */
  151. efiobj = find_obj(dp, false, NULL);
  152. /* Then for a fuzzy match */
  153. if (!efiobj)
  154. efiobj = find_obj(dp, false, rem);
  155. /* And now for a fuzzy short match */
  156. if (!efiobj)
  157. efiobj = find_obj(dp, true, rem);
  158. return efiobj;
  159. }
  160. /* return size not including End node: */
  161. unsigned efi_dp_size(const struct efi_device_path *dp)
  162. {
  163. unsigned sz = 0;
  164. while (dp) {
  165. sz += dp->length;
  166. dp = efi_dp_next(dp);
  167. }
  168. return sz;
  169. }
  170. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
  171. {
  172. struct efi_device_path *ndp;
  173. unsigned sz = efi_dp_size(dp) + sizeof(END);
  174. if (!dp)
  175. return NULL;
  176. ndp = dp_alloc(sz);
  177. memcpy(ndp, dp, sz);
  178. return ndp;
  179. }
  180. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  181. const struct efi_device_path *dp2)
  182. {
  183. struct efi_device_path *ret;
  184. if (!dp1) {
  185. ret = efi_dp_dup(dp2);
  186. } else if (!dp2) {
  187. ret = efi_dp_dup(dp1);
  188. } else {
  189. /* both dp1 and dp2 are non-null */
  190. unsigned sz1 = efi_dp_size(dp1);
  191. unsigned sz2 = efi_dp_size(dp2);
  192. void *p = dp_alloc(sz1 + sz2 + sizeof(END));
  193. memcpy(p, dp1, sz1);
  194. memcpy(p + sz1, dp2, sz2);
  195. memcpy(p + sz1 + sz2, &END, sizeof(END));
  196. ret = p;
  197. }
  198. return ret;
  199. }
  200. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  201. const struct efi_device_path *node)
  202. {
  203. struct efi_device_path *ret;
  204. if (!node && !dp) {
  205. ret = efi_dp_dup(&END);
  206. } else if (!node) {
  207. ret = efi_dp_dup(dp);
  208. } else if (!dp) {
  209. unsigned sz = node->length;
  210. void *p = dp_alloc(sz + sizeof(END));
  211. memcpy(p, node, sz);
  212. memcpy(p + sz, &END, sizeof(END));
  213. ret = p;
  214. } else {
  215. /* both dp and node are non-null */
  216. unsigned sz = efi_dp_size(dp);
  217. void *p = dp_alloc(sz + node->length + sizeof(END));
  218. memcpy(p, dp, sz);
  219. memcpy(p + sz, node, node->length);
  220. memcpy(p + sz + node->length, &END, sizeof(END));
  221. ret = p;
  222. }
  223. return ret;
  224. }
  225. #ifdef CONFIG_DM
  226. /* size of device-path not including END node for device and all parents
  227. * up to the root device.
  228. */
  229. static unsigned dp_size(struct udevice *dev)
  230. {
  231. if (!dev || !dev->driver)
  232. return sizeof(ROOT);
  233. switch (dev->driver->id) {
  234. case UCLASS_ROOT:
  235. case UCLASS_SIMPLE_BUS:
  236. /* stop traversing parents at this point: */
  237. return sizeof(ROOT);
  238. case UCLASS_MMC:
  239. return dp_size(dev->parent) +
  240. sizeof(struct efi_device_path_sd_mmc_path);
  241. case UCLASS_MASS_STORAGE:
  242. case UCLASS_USB_HUB:
  243. return dp_size(dev->parent) +
  244. sizeof(struct efi_device_path_usb_class);
  245. default:
  246. /* just skip over unknown classes: */
  247. return dp_size(dev->parent);
  248. }
  249. }
  250. static void *dp_fill(void *buf, struct udevice *dev)
  251. {
  252. if (!dev || !dev->driver)
  253. return buf;
  254. switch (dev->driver->id) {
  255. case UCLASS_ROOT:
  256. case UCLASS_SIMPLE_BUS: {
  257. /* stop traversing parents at this point: */
  258. struct efi_device_path_vendor *vdp = buf;
  259. *vdp = ROOT;
  260. return &vdp[1];
  261. }
  262. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  263. case UCLASS_MMC: {
  264. struct efi_device_path_sd_mmc_path *sddp =
  265. dp_fill(buf, dev->parent);
  266. struct mmc *mmc = mmc_get_mmc_dev(dev);
  267. struct blk_desc *desc = mmc_get_blk_desc(mmc);
  268. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  269. sddp->dp.sub_type = (desc->if_type == IF_TYPE_MMC) ?
  270. DEVICE_PATH_SUB_TYPE_MSG_MMC :
  271. DEVICE_PATH_SUB_TYPE_MSG_SD;
  272. sddp->dp.length = sizeof(*sddp);
  273. sddp->slot_number = dev->seq;
  274. return &sddp[1];
  275. }
  276. #endif
  277. case UCLASS_MASS_STORAGE:
  278. case UCLASS_USB_HUB: {
  279. struct efi_device_path_usb_class *udp =
  280. dp_fill(buf, dev->parent);
  281. struct usb_device *udev = dev_get_parent_priv(dev);
  282. struct usb_device_descriptor *desc = &udev->descriptor;
  283. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  284. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
  285. udp->dp.length = sizeof(*udp);
  286. udp->vendor_id = desc->idVendor;
  287. udp->product_id = desc->idProduct;
  288. udp->device_class = desc->bDeviceClass;
  289. udp->device_subclass = desc->bDeviceSubClass;
  290. udp->device_protocol = desc->bDeviceProtocol;
  291. return &udp[1];
  292. }
  293. default:
  294. debug("unhandled device class: %s (%u)\n",
  295. dev->name, dev->driver->id);
  296. return dp_fill(buf, dev->parent);
  297. }
  298. }
  299. /* Construct a device-path from a device: */
  300. struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
  301. {
  302. void *buf, *start;
  303. start = buf = dp_alloc(dp_size(dev) + sizeof(END));
  304. buf = dp_fill(buf, dev);
  305. *((struct efi_device_path *)buf) = END;
  306. return start;
  307. }
  308. #endif
  309. static unsigned dp_part_size(struct blk_desc *desc, int part)
  310. {
  311. unsigned dpsize;
  312. #ifdef CONFIG_BLK
  313. dpsize = dp_size(desc->bdev->parent);
  314. #else
  315. dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
  316. #endif
  317. if (part == 0) /* the actual disk, not a partition */
  318. return dpsize;
  319. if (desc->part_type == PART_TYPE_ISO)
  320. dpsize += sizeof(struct efi_device_path_cdrom_path);
  321. else
  322. dpsize += sizeof(struct efi_device_path_hard_drive_path);
  323. return dpsize;
  324. }
  325. static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
  326. {
  327. disk_partition_t info;
  328. #ifdef CONFIG_BLK
  329. buf = dp_fill(buf, desc->bdev->parent);
  330. #else
  331. /*
  332. * We *could* make a more accurate path, by looking at if_type
  333. * and handling all the different cases like we do for non-
  334. * legacy (ie CONFIG_BLK=y) case. But most important thing
  335. * is just to have a unique device-path for if_type+devnum.
  336. * So map things to a fictional USB device:
  337. */
  338. struct efi_device_path_usb *udp;
  339. memcpy(buf, &ROOT, sizeof(ROOT));
  340. buf += sizeof(ROOT);
  341. udp = buf;
  342. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  343. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
  344. udp->dp.length = sizeof(*udp);
  345. udp->parent_port_number = desc->if_type;
  346. udp->usb_interface = desc->devnum;
  347. buf = &udp[1];
  348. #endif
  349. if (part == 0) /* the actual disk, not a partition */
  350. return buf;
  351. part_get_info(desc, part, &info);
  352. if (desc->part_type == PART_TYPE_ISO) {
  353. struct efi_device_path_cdrom_path *cddp = buf;
  354. cddp->boot_entry = part - 1;
  355. cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  356. cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
  357. cddp->dp.length = sizeof(*cddp);
  358. cddp->partition_start = info.start;
  359. cddp->partition_end = info.size;
  360. buf = &cddp[1];
  361. } else {
  362. struct efi_device_path_hard_drive_path *hddp = buf;
  363. hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  364. hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
  365. hddp->dp.length = sizeof(*hddp);
  366. hddp->partition_number = part - 1;
  367. hddp->partition_start = info.start;
  368. hddp->partition_end = info.size;
  369. if (desc->part_type == PART_TYPE_EFI)
  370. hddp->partmap_type = 2;
  371. else
  372. hddp->partmap_type = 1;
  373. switch (desc->sig_type) {
  374. case SIG_TYPE_NONE:
  375. default:
  376. hddp->signature_type = 0;
  377. memset(hddp->partition_signature, 0,
  378. sizeof(hddp->partition_signature));
  379. break;
  380. case SIG_TYPE_MBR:
  381. hddp->signature_type = 1;
  382. memset(hddp->partition_signature, 0,
  383. sizeof(hddp->partition_signature));
  384. memcpy(hddp->partition_signature, &desc->mbr_sig,
  385. sizeof(desc->mbr_sig));
  386. break;
  387. case SIG_TYPE_GUID:
  388. hddp->signature_type = 2;
  389. memcpy(hddp->partition_signature, &desc->guid_sig,
  390. sizeof(hddp->partition_signature));
  391. break;
  392. }
  393. buf = &hddp[1];
  394. }
  395. return buf;
  396. }
  397. /* Construct a device-path from a partition on a blk device: */
  398. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
  399. {
  400. void *buf, *start;
  401. start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
  402. buf = dp_part_fill(buf, desc, part);
  403. *((struct efi_device_path *)buf) = END;
  404. return start;
  405. }
  406. /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
  407. static void path_to_uefi(u16 *uefi, const char *path)
  408. {
  409. while (*path) {
  410. char c = *(path++);
  411. if (c == '/')
  412. c = '\\';
  413. *(uefi++) = c;
  414. }
  415. *uefi = '\0';
  416. }
  417. /*
  418. * If desc is NULL, this creates a path with only the file component,
  419. * otherwise it creates a full path with both device and file components
  420. */
  421. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  422. const char *path)
  423. {
  424. struct efi_device_path_file_path *fp;
  425. void *buf, *start;
  426. unsigned dpsize = 0, fpsize;
  427. if (desc)
  428. dpsize = dp_part_size(desc, part);
  429. fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
  430. dpsize += fpsize;
  431. start = buf = dp_alloc(dpsize + sizeof(END));
  432. if (desc)
  433. buf = dp_part_fill(buf, desc, part);
  434. /* add file-path: */
  435. fp = buf;
  436. fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  437. fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  438. fp->dp.length = fpsize;
  439. path_to_uefi(fp->str, path);
  440. buf += fpsize;
  441. *((struct efi_device_path *)buf) = END;
  442. return start;
  443. }
  444. #ifdef CONFIG_NET
  445. struct efi_device_path *efi_dp_from_eth(void)
  446. {
  447. struct efi_device_path_mac_addr *ndp;
  448. void *buf, *start;
  449. unsigned dpsize = 0;
  450. assert(eth_get_dev());
  451. #ifdef CONFIG_DM_ETH
  452. dpsize += dp_size(eth_get_dev());
  453. #else
  454. dpsize += sizeof(ROOT);
  455. #endif
  456. dpsize += sizeof(*ndp);
  457. start = buf = dp_alloc(dpsize + sizeof(END));
  458. #ifdef CONFIG_DM_ETH
  459. buf = dp_fill(buf, eth_get_dev());
  460. #else
  461. memcpy(buf, &ROOT, sizeof(ROOT));
  462. buf += sizeof(ROOT);
  463. #endif
  464. ndp = buf;
  465. ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  466. ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  467. ndp->dp.length = sizeof(*ndp);
  468. memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
  469. buf = &ndp[1];
  470. *((struct efi_device_path *)buf) = END;
  471. return start;
  472. }
  473. #endif
  474. /* Construct a device-path for memory-mapped image */
  475. struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  476. uint64_t start_address,
  477. uint64_t end_address)
  478. {
  479. struct efi_device_path_memory *mdp;
  480. void *buf, *start;
  481. start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
  482. mdp = buf;
  483. mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  484. mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
  485. mdp->dp.length = sizeof(*mdp);
  486. mdp->memory_type = memory_type;
  487. mdp->start_address = start_address;
  488. mdp->end_address = end_address;
  489. buf = &mdp[1];
  490. *((struct efi_device_path *)buf) = END;
  491. return start;
  492. }
  493. /*
  494. * Helper to split a full device path (containing both device and file
  495. * parts) into it's constituent parts.
  496. */
  497. void efi_dp_split_file_path(struct efi_device_path *full_path,
  498. struct efi_device_path **device_path,
  499. struct efi_device_path **file_path)
  500. {
  501. struct efi_device_path *p, *dp, *fp;
  502. dp = efi_dp_dup(full_path);
  503. p = dp;
  504. while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
  505. p = efi_dp_next(p);
  506. fp = efi_dp_dup(p);
  507. p->type = DEVICE_PATH_TYPE_END;
  508. p->sub_type = DEVICE_PATH_SUB_TYPE_END;
  509. p->length = sizeof(*p);
  510. *device_path = dp;
  511. *file_path = fp;
  512. }