efi_device_path.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. #define LOG_CATEGORY LOGL_ERR
  9. #include <common.h>
  10. #include <blk.h>
  11. #include <dm.h>
  12. #include <usb.h>
  13. #include <mmc.h>
  14. #include <efi_loader.h>
  15. #include <inttypes.h>
  16. #include <part.h>
  17. /* template END node: */
  18. static const struct efi_device_path END = {
  19. .type = DEVICE_PATH_TYPE_END,
  20. .sub_type = DEVICE_PATH_SUB_TYPE_END,
  21. .length = sizeof(END),
  22. };
  23. #define U_BOOT_GUID \
  24. EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
  25. 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
  26. /* template ROOT node: */
  27. static const struct efi_device_path_vendor ROOT = {
  28. .dp = {
  29. .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  30. .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
  31. .length = sizeof(ROOT),
  32. },
  33. .guid = U_BOOT_GUID,
  34. };
  35. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  36. /*
  37. * Determine if an MMC device is an SD card.
  38. *
  39. * @desc block device descriptor
  40. * @return true if the device is an SD card
  41. */
  42. static bool is_sd(struct blk_desc *desc)
  43. {
  44. struct mmc *mmc = find_mmc_device(desc->devnum);
  45. if (!mmc)
  46. return false;
  47. return IS_SD(mmc) != 0U;
  48. }
  49. #endif
  50. static void *dp_alloc(size_t sz)
  51. {
  52. void *buf;
  53. if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
  54. EFI_SUCCESS) {
  55. debug("EFI: ERROR: out of memory in %s\n", __func__);
  56. return NULL;
  57. }
  58. memset(buf, 0, sz);
  59. return buf;
  60. }
  61. /*
  62. * Iterate to next block in device-path, terminating (returning NULL)
  63. * at /End* node.
  64. */
  65. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
  66. {
  67. if (dp == NULL)
  68. return NULL;
  69. if (dp->type == DEVICE_PATH_TYPE_END)
  70. return NULL;
  71. dp = ((void *)dp) + dp->length;
  72. if (dp->type == DEVICE_PATH_TYPE_END)
  73. return NULL;
  74. return (struct efi_device_path *)dp;
  75. }
  76. /*
  77. * Compare two device-paths, stopping when the shorter of the two hits
  78. * an End* node. This is useful to, for example, compare a device-path
  79. * representing a device with one representing a file on the device, or
  80. * a device with a parent device.
  81. */
  82. int efi_dp_match(const struct efi_device_path *a,
  83. const struct efi_device_path *b)
  84. {
  85. while (1) {
  86. int ret;
  87. ret = memcmp(&a->length, &b->length, sizeof(a->length));
  88. if (ret)
  89. return ret;
  90. ret = memcmp(a, b, a->length);
  91. if (ret)
  92. return ret;
  93. a = efi_dp_next(a);
  94. b = efi_dp_next(b);
  95. if (!a || !b)
  96. return 0;
  97. }
  98. }
  99. /*
  100. * See UEFI spec (section 3.1.2, about short-form device-paths..
  101. * tl;dr: we can have a device-path that starts with a USB WWID
  102. * or USB Class node, and a few other cases which don't encode
  103. * the full device path with bus hierarchy:
  104. *
  105. * - MESSAGING:USB_WWID
  106. * - MESSAGING:USB_CLASS
  107. * - MEDIA:FILE_PATH
  108. * - MEDIA:HARD_DRIVE
  109. * - MESSAGING:URI
  110. */
  111. static struct efi_device_path *shorten_path(struct efi_device_path *dp)
  112. {
  113. while (dp) {
  114. /*
  115. * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
  116. * in practice fallback.efi just uses MEDIA:HARD_DRIVE
  117. * so not sure when we would see these other cases.
  118. */
  119. if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
  120. EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
  121. EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
  122. return dp;
  123. dp = efi_dp_next(dp);
  124. }
  125. return dp;
  126. }
  127. static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
  128. struct efi_device_path **rem)
  129. {
  130. struct efi_object *efiobj;
  131. unsigned int dp_size = efi_dp_size(dp);
  132. list_for_each_entry(efiobj, &efi_obj_list, link) {
  133. struct efi_handler *handler;
  134. struct efi_device_path *obj_dp;
  135. efi_status_t ret;
  136. ret = efi_search_protocol(efiobj->handle,
  137. &efi_guid_device_path, &handler);
  138. if (ret != EFI_SUCCESS)
  139. continue;
  140. obj_dp = handler->protocol_interface;
  141. do {
  142. if (efi_dp_match(dp, obj_dp) == 0) {
  143. if (rem) {
  144. /*
  145. * Allow partial matches, but inform
  146. * the caller.
  147. */
  148. *rem = ((void *)dp) +
  149. efi_dp_size(obj_dp);
  150. return efiobj;
  151. } else {
  152. /* Only return on exact matches */
  153. if (efi_dp_size(obj_dp) == dp_size)
  154. return efiobj;
  155. }
  156. }
  157. obj_dp = shorten_path(efi_dp_next(obj_dp));
  158. } while (short_path && obj_dp);
  159. }
  160. return NULL;
  161. }
  162. /*
  163. * Find an efiobj from device-path, if 'rem' is not NULL, returns the
  164. * remaining part of the device path after the matched object.
  165. */
  166. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  167. struct efi_device_path **rem)
  168. {
  169. struct efi_object *efiobj;
  170. /* Search for an exact match first */
  171. efiobj = find_obj(dp, false, NULL);
  172. /* Then for a fuzzy match */
  173. if (!efiobj)
  174. efiobj = find_obj(dp, false, rem);
  175. /* And now for a fuzzy short match */
  176. if (!efiobj)
  177. efiobj = find_obj(dp, true, rem);
  178. return efiobj;
  179. }
  180. /*
  181. * Determine the last device path node that is not the end node.
  182. *
  183. * @dp device path
  184. * @return last node before the end node if it exists
  185. * otherwise NULL
  186. */
  187. const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
  188. {
  189. struct efi_device_path *ret;
  190. if (!dp || dp->type == DEVICE_PATH_TYPE_END)
  191. return NULL;
  192. while (dp) {
  193. ret = (struct efi_device_path *)dp;
  194. dp = efi_dp_next(dp);
  195. }
  196. return ret;
  197. }
  198. /* return size not including End node: */
  199. unsigned efi_dp_size(const struct efi_device_path *dp)
  200. {
  201. unsigned sz = 0;
  202. while (dp) {
  203. sz += dp->length;
  204. dp = efi_dp_next(dp);
  205. }
  206. return sz;
  207. }
  208. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
  209. {
  210. struct efi_device_path *ndp;
  211. unsigned sz = efi_dp_size(dp) + sizeof(END);
  212. if (!dp)
  213. return NULL;
  214. ndp = dp_alloc(sz);
  215. if (!ndp)
  216. return NULL;
  217. memcpy(ndp, dp, sz);
  218. return ndp;
  219. }
  220. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  221. const struct efi_device_path *dp2)
  222. {
  223. struct efi_device_path *ret;
  224. if (!dp1) {
  225. ret = efi_dp_dup(dp2);
  226. } else if (!dp2) {
  227. ret = efi_dp_dup(dp1);
  228. } else {
  229. /* both dp1 and dp2 are non-null */
  230. unsigned sz1 = efi_dp_size(dp1);
  231. unsigned sz2 = efi_dp_size(dp2);
  232. void *p = dp_alloc(sz1 + sz2 + sizeof(END));
  233. if (!p)
  234. return NULL;
  235. memcpy(p, dp1, sz1);
  236. memcpy(p + sz1, dp2, sz2);
  237. memcpy(p + sz1 + sz2, &END, sizeof(END));
  238. ret = p;
  239. }
  240. return ret;
  241. }
  242. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  243. const struct efi_device_path *node)
  244. {
  245. struct efi_device_path *ret;
  246. if (!node && !dp) {
  247. ret = efi_dp_dup(&END);
  248. } else if (!node) {
  249. ret = efi_dp_dup(dp);
  250. } else if (!dp) {
  251. unsigned sz = node->length;
  252. void *p = dp_alloc(sz + sizeof(END));
  253. if (!p)
  254. return NULL;
  255. memcpy(p, node, sz);
  256. memcpy(p + sz, &END, sizeof(END));
  257. ret = p;
  258. } else {
  259. /* both dp and node are non-null */
  260. unsigned sz = efi_dp_size(dp);
  261. void *p = dp_alloc(sz + node->length + sizeof(END));
  262. if (!p)
  263. return NULL;
  264. memcpy(p, dp, sz);
  265. memcpy(p + sz, node, node->length);
  266. memcpy(p + sz + node->length, &END, sizeof(END));
  267. ret = p;
  268. }
  269. return ret;
  270. }
  271. struct efi_device_path *efi_dp_create_device_node(const u8 type,
  272. const u8 sub_type,
  273. const u16 length)
  274. {
  275. struct efi_device_path *ret;
  276. ret = dp_alloc(length);
  277. if (!ret)
  278. return ret;
  279. ret->type = type;
  280. ret->sub_type = sub_type;
  281. ret->length = length;
  282. return ret;
  283. }
  284. #ifdef CONFIG_DM
  285. /* size of device-path not including END node for device and all parents
  286. * up to the root device.
  287. */
  288. static unsigned dp_size(struct udevice *dev)
  289. {
  290. if (!dev || !dev->driver)
  291. return sizeof(ROOT);
  292. switch (dev->driver->id) {
  293. case UCLASS_ROOT:
  294. case UCLASS_SIMPLE_BUS:
  295. /* stop traversing parents at this point: */
  296. return sizeof(ROOT);
  297. case UCLASS_ETH:
  298. return dp_size(dev->parent) +
  299. sizeof(struct efi_device_path_mac_addr);
  300. #ifdef CONFIG_BLK
  301. case UCLASS_BLK:
  302. switch (dev->parent->uclass->uc_drv->id) {
  303. #ifdef CONFIG_IDE
  304. case UCLASS_IDE:
  305. return dp_size(dev->parent) +
  306. sizeof(struct efi_device_path_atapi);
  307. #endif
  308. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  309. case UCLASS_SCSI:
  310. return dp_size(dev->parent) +
  311. sizeof(struct efi_device_path_scsi);
  312. #endif
  313. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  314. case UCLASS_MMC:
  315. return dp_size(dev->parent) +
  316. sizeof(struct efi_device_path_sd_mmc_path);
  317. #endif
  318. default:
  319. return dp_size(dev->parent);
  320. }
  321. #endif
  322. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  323. case UCLASS_MMC:
  324. return dp_size(dev->parent) +
  325. sizeof(struct efi_device_path_sd_mmc_path);
  326. #endif
  327. case UCLASS_MASS_STORAGE:
  328. case UCLASS_USB_HUB:
  329. return dp_size(dev->parent) +
  330. sizeof(struct efi_device_path_usb_class);
  331. default:
  332. /* just skip over unknown classes: */
  333. return dp_size(dev->parent);
  334. }
  335. }
  336. /*
  337. * Recursively build a device path.
  338. *
  339. * @buf pointer to the end of the device path
  340. * @dev device
  341. * @return pointer to the end of the device path
  342. */
  343. static void *dp_fill(void *buf, struct udevice *dev)
  344. {
  345. if (!dev || !dev->driver)
  346. return buf;
  347. switch (dev->driver->id) {
  348. case UCLASS_ROOT:
  349. case UCLASS_SIMPLE_BUS: {
  350. /* stop traversing parents at this point: */
  351. struct efi_device_path_vendor *vdp = buf;
  352. *vdp = ROOT;
  353. return &vdp[1];
  354. }
  355. #ifdef CONFIG_DM_ETH
  356. case UCLASS_ETH: {
  357. struct efi_device_path_mac_addr *dp =
  358. dp_fill(buf, dev->parent);
  359. struct eth_pdata *pdata = dev->platdata;
  360. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  361. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  362. dp->dp.length = sizeof(*dp);
  363. memset(&dp->mac, 0, sizeof(dp->mac));
  364. /* We only support IPv4 */
  365. memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
  366. /* Ethernet */
  367. dp->if_type = 1;
  368. return &dp[1];
  369. }
  370. #endif
  371. #ifdef CONFIG_BLK
  372. case UCLASS_BLK:
  373. switch (dev->parent->uclass->uc_drv->id) {
  374. #ifdef CONFIG_IDE
  375. case UCLASS_IDE: {
  376. struct efi_device_path_atapi *dp =
  377. dp_fill(buf, dev->parent);
  378. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  379. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  380. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
  381. dp->dp.length = sizeof(*dp);
  382. dp->logical_unit_number = desc->devnum;
  383. dp->primary_secondary = IDE_BUS(desc->devnum);
  384. dp->slave_master = desc->devnum %
  385. (CONFIG_SYS_IDE_MAXDEVICE /
  386. CONFIG_SYS_IDE_MAXBUS);
  387. return &dp[1];
  388. }
  389. #endif
  390. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  391. case UCLASS_SCSI: {
  392. struct efi_device_path_scsi *dp =
  393. dp_fill(buf, dev->parent);
  394. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  395. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  396. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
  397. dp->dp.length = sizeof(*dp);
  398. dp->logical_unit_number = desc->lun;
  399. dp->target_id = desc->target;
  400. return &dp[1];
  401. }
  402. #endif
  403. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  404. case UCLASS_MMC: {
  405. struct efi_device_path_sd_mmc_path *sddp =
  406. dp_fill(buf, dev->parent);
  407. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  408. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  409. sddp->dp.sub_type = is_sd(desc) ?
  410. DEVICE_PATH_SUB_TYPE_MSG_SD :
  411. DEVICE_PATH_SUB_TYPE_MSG_MMC;
  412. sddp->dp.length = sizeof(*sddp);
  413. sddp->slot_number = dev->seq;
  414. return &sddp[1];
  415. }
  416. #endif
  417. default:
  418. debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
  419. __FILE__, __LINE__, __func__,
  420. dev->name, dev->parent->uclass->uc_drv->id);
  421. return dp_fill(buf, dev->parent);
  422. }
  423. #endif
  424. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  425. case UCLASS_MMC: {
  426. struct efi_device_path_sd_mmc_path *sddp =
  427. dp_fill(buf, dev->parent);
  428. struct mmc *mmc = mmc_get_mmc_dev(dev);
  429. struct blk_desc *desc = mmc_get_blk_desc(mmc);
  430. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  431. sddp->dp.sub_type = is_sd(desc) ?
  432. DEVICE_PATH_SUB_TYPE_MSG_SD :
  433. DEVICE_PATH_SUB_TYPE_MSG_MMC;
  434. sddp->dp.length = sizeof(*sddp);
  435. sddp->slot_number = dev->seq;
  436. return &sddp[1];
  437. }
  438. #endif
  439. case UCLASS_MASS_STORAGE:
  440. case UCLASS_USB_HUB: {
  441. struct efi_device_path_usb_class *udp =
  442. dp_fill(buf, dev->parent);
  443. struct usb_device *udev = dev_get_parent_priv(dev);
  444. struct usb_device_descriptor *desc = &udev->descriptor;
  445. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  446. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
  447. udp->dp.length = sizeof(*udp);
  448. udp->vendor_id = desc->idVendor;
  449. udp->product_id = desc->idProduct;
  450. udp->device_class = desc->bDeviceClass;
  451. udp->device_subclass = desc->bDeviceSubClass;
  452. udp->device_protocol = desc->bDeviceProtocol;
  453. return &udp[1];
  454. }
  455. default:
  456. debug("%s(%u) %s: unhandled device class: %s (%u)\n",
  457. __FILE__, __LINE__, __func__,
  458. dev->name, dev->driver->id);
  459. return dp_fill(buf, dev->parent);
  460. }
  461. }
  462. /* Construct a device-path from a device: */
  463. struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
  464. {
  465. void *buf, *start;
  466. start = buf = dp_alloc(dp_size(dev) + sizeof(END));
  467. if (!buf)
  468. return NULL;
  469. buf = dp_fill(buf, dev);
  470. *((struct efi_device_path *)buf) = END;
  471. return start;
  472. }
  473. #endif
  474. static unsigned dp_part_size(struct blk_desc *desc, int part)
  475. {
  476. unsigned dpsize;
  477. #ifdef CONFIG_BLK
  478. {
  479. struct udevice *dev;
  480. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  481. if (ret)
  482. dev = desc->bdev->parent;
  483. dpsize = dp_size(dev);
  484. }
  485. #else
  486. dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
  487. #endif
  488. if (part == 0) /* the actual disk, not a partition */
  489. return dpsize;
  490. if (desc->part_type == PART_TYPE_ISO)
  491. dpsize += sizeof(struct efi_device_path_cdrom_path);
  492. else
  493. dpsize += sizeof(struct efi_device_path_hard_drive_path);
  494. return dpsize;
  495. }
  496. /*
  497. * Create a device node for a block device partition.
  498. *
  499. * @buf buffer to which the device path is wirtten
  500. * @desc block device descriptor
  501. * @part partition number, 0 identifies a block device
  502. */
  503. static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
  504. {
  505. disk_partition_t info;
  506. part_get_info(desc, part, &info);
  507. if (desc->part_type == PART_TYPE_ISO) {
  508. struct efi_device_path_cdrom_path *cddp = buf;
  509. cddp->boot_entry = part;
  510. cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  511. cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
  512. cddp->dp.length = sizeof(*cddp);
  513. cddp->partition_start = info.start;
  514. cddp->partition_end = info.size;
  515. buf = &cddp[1];
  516. } else {
  517. struct efi_device_path_hard_drive_path *hddp = buf;
  518. hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  519. hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
  520. hddp->dp.length = sizeof(*hddp);
  521. hddp->partition_number = part;
  522. hddp->partition_start = info.start;
  523. hddp->partition_end = info.size;
  524. if (desc->part_type == PART_TYPE_EFI)
  525. hddp->partmap_type = 2;
  526. else
  527. hddp->partmap_type = 1;
  528. switch (desc->sig_type) {
  529. case SIG_TYPE_NONE:
  530. default:
  531. hddp->signature_type = 0;
  532. memset(hddp->partition_signature, 0,
  533. sizeof(hddp->partition_signature));
  534. break;
  535. case SIG_TYPE_MBR:
  536. hddp->signature_type = 1;
  537. memset(hddp->partition_signature, 0,
  538. sizeof(hddp->partition_signature));
  539. memcpy(hddp->partition_signature, &desc->mbr_sig,
  540. sizeof(desc->mbr_sig));
  541. break;
  542. case SIG_TYPE_GUID:
  543. hddp->signature_type = 2;
  544. memcpy(hddp->partition_signature, &desc->guid_sig,
  545. sizeof(hddp->partition_signature));
  546. break;
  547. }
  548. buf = &hddp[1];
  549. }
  550. return buf;
  551. }
  552. /*
  553. * Create a device path for a block device or one of its partitions.
  554. *
  555. * @buf buffer to which the device path is wirtten
  556. * @desc block device descriptor
  557. * @part partition number, 0 identifies a block device
  558. */
  559. static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
  560. {
  561. #ifdef CONFIG_BLK
  562. {
  563. struct udevice *dev;
  564. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  565. if (ret)
  566. dev = desc->bdev->parent;
  567. buf = dp_fill(buf, dev);
  568. }
  569. #else
  570. /*
  571. * We *could* make a more accurate path, by looking at if_type
  572. * and handling all the different cases like we do for non-
  573. * legacy (ie CONFIG_BLK=y) case. But most important thing
  574. * is just to have a unique device-path for if_type+devnum.
  575. * So map things to a fictitious USB device.
  576. */
  577. struct efi_device_path_usb *udp;
  578. memcpy(buf, &ROOT, sizeof(ROOT));
  579. buf += sizeof(ROOT);
  580. udp = buf;
  581. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  582. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
  583. udp->dp.length = sizeof(*udp);
  584. udp->parent_port_number = desc->if_type;
  585. udp->usb_interface = desc->devnum;
  586. buf = &udp[1];
  587. #endif
  588. if (part == 0) /* the actual disk, not a partition */
  589. return buf;
  590. return dp_part_node(buf, desc, part);
  591. }
  592. /* Construct a device-path from a partition on a blk device: */
  593. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
  594. {
  595. void *buf, *start;
  596. start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
  597. if (!buf)
  598. return NULL;
  599. buf = dp_part_fill(buf, desc, part);
  600. *((struct efi_device_path *)buf) = END;
  601. return start;
  602. }
  603. /*
  604. * Create a device node for a block device partition.
  605. *
  606. * @buf buffer to which the device path is wirtten
  607. * @desc block device descriptor
  608. * @part partition number, 0 identifies a block device
  609. */
  610. struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
  611. {
  612. efi_uintn_t dpsize;
  613. void *buf;
  614. if (desc->part_type == PART_TYPE_ISO)
  615. dpsize = sizeof(struct efi_device_path_cdrom_path);
  616. else
  617. dpsize = sizeof(struct efi_device_path_hard_drive_path);
  618. buf = dp_alloc(dpsize);
  619. dp_part_node(buf, desc, part);
  620. return buf;
  621. }
  622. /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
  623. static void path_to_uefi(u16 *uefi, const char *path)
  624. {
  625. while (*path) {
  626. char c = *(path++);
  627. if (c == '/')
  628. c = '\\';
  629. *(uefi++) = c;
  630. }
  631. *uefi = '\0';
  632. }
  633. /*
  634. * If desc is NULL, this creates a path with only the file component,
  635. * otherwise it creates a full path with both device and file components
  636. */
  637. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  638. const char *path)
  639. {
  640. struct efi_device_path_file_path *fp;
  641. void *buf, *start;
  642. unsigned dpsize = 0, fpsize;
  643. if (desc)
  644. dpsize = dp_part_size(desc, part);
  645. fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
  646. dpsize += fpsize;
  647. start = buf = dp_alloc(dpsize + sizeof(END));
  648. if (!buf)
  649. return NULL;
  650. if (desc)
  651. buf = dp_part_fill(buf, desc, part);
  652. /* add file-path: */
  653. fp = buf;
  654. fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  655. fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  656. fp->dp.length = fpsize;
  657. path_to_uefi(fp->str, path);
  658. buf += fpsize;
  659. *((struct efi_device_path *)buf) = END;
  660. return start;
  661. }
  662. #ifdef CONFIG_NET
  663. struct efi_device_path *efi_dp_from_eth(void)
  664. {
  665. #ifndef CONFIG_DM_ETH
  666. struct efi_device_path_mac_addr *ndp;
  667. #endif
  668. void *buf, *start;
  669. unsigned dpsize = 0;
  670. assert(eth_get_dev());
  671. #ifdef CONFIG_DM_ETH
  672. dpsize += dp_size(eth_get_dev());
  673. #else
  674. dpsize += sizeof(ROOT);
  675. dpsize += sizeof(*ndp);
  676. #endif
  677. start = buf = dp_alloc(dpsize + sizeof(END));
  678. if (!buf)
  679. return NULL;
  680. #ifdef CONFIG_DM_ETH
  681. buf = dp_fill(buf, eth_get_dev());
  682. #else
  683. memcpy(buf, &ROOT, sizeof(ROOT));
  684. buf += sizeof(ROOT);
  685. ndp = buf;
  686. ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  687. ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  688. ndp->dp.length = sizeof(*ndp);
  689. ndp->if_type = 1; /* Ethernet */
  690. memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
  691. buf = &ndp[1];
  692. #endif
  693. *((struct efi_device_path *)buf) = END;
  694. return start;
  695. }
  696. #endif
  697. /* Construct a device-path for memory-mapped image */
  698. struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  699. uint64_t start_address,
  700. uint64_t end_address)
  701. {
  702. struct efi_device_path_memory *mdp;
  703. void *buf, *start;
  704. start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
  705. if (!buf)
  706. return NULL;
  707. mdp = buf;
  708. mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  709. mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
  710. mdp->dp.length = sizeof(*mdp);
  711. mdp->memory_type = memory_type;
  712. mdp->start_address = start_address;
  713. mdp->end_address = end_address;
  714. buf = &mdp[1];
  715. *((struct efi_device_path *)buf) = END;
  716. return start;
  717. }
  718. /*
  719. * Helper to split a full device path (containing both device and file
  720. * parts) into it's constituent parts.
  721. */
  722. efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
  723. struct efi_device_path **device_path,
  724. struct efi_device_path **file_path)
  725. {
  726. struct efi_device_path *p, *dp, *fp;
  727. *device_path = NULL;
  728. *file_path = NULL;
  729. dp = efi_dp_dup(full_path);
  730. if (!dp)
  731. return EFI_OUT_OF_RESOURCES;
  732. p = dp;
  733. while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
  734. p = efi_dp_next(p);
  735. if (!p)
  736. return EFI_OUT_OF_RESOURCES;
  737. }
  738. fp = efi_dp_dup(p);
  739. if (!fp)
  740. return EFI_OUT_OF_RESOURCES;
  741. p->type = DEVICE_PATH_TYPE_END;
  742. p->sub_type = DEVICE_PATH_SUB_TYPE_END;
  743. p->length = sizeof(*p);
  744. *device_path = dp;
  745. *file_path = fp;
  746. return EFI_SUCCESS;
  747. }