efi_device_path.c 22 KB

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