efi_device_path.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. #ifdef CONFIG_DM
  272. /* size of device-path not including END node for device and all parents
  273. * up to the root device.
  274. */
  275. static unsigned dp_size(struct udevice *dev)
  276. {
  277. if (!dev || !dev->driver)
  278. return sizeof(ROOT);
  279. switch (dev->driver->id) {
  280. case UCLASS_ROOT:
  281. case UCLASS_SIMPLE_BUS:
  282. /* stop traversing parents at this point: */
  283. return sizeof(ROOT);
  284. case UCLASS_ETH:
  285. return dp_size(dev->parent) +
  286. sizeof(struct efi_device_path_mac_addr);
  287. #ifdef CONFIG_BLK
  288. case UCLASS_BLK:
  289. switch (dev->parent->uclass->uc_drv->id) {
  290. #ifdef CONFIG_IDE
  291. case UCLASS_IDE:
  292. return dp_size(dev->parent) +
  293. sizeof(struct efi_device_path_atapi);
  294. #endif
  295. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  296. case UCLASS_SCSI:
  297. return dp_size(dev->parent) +
  298. sizeof(struct efi_device_path_scsi);
  299. #endif
  300. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  301. case UCLASS_MMC:
  302. return dp_size(dev->parent) +
  303. sizeof(struct efi_device_path_sd_mmc_path);
  304. #endif
  305. default:
  306. return dp_size(dev->parent);
  307. }
  308. #endif
  309. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  310. case UCLASS_MMC:
  311. return dp_size(dev->parent) +
  312. sizeof(struct efi_device_path_sd_mmc_path);
  313. #endif
  314. case UCLASS_MASS_STORAGE:
  315. case UCLASS_USB_HUB:
  316. return dp_size(dev->parent) +
  317. sizeof(struct efi_device_path_usb_class);
  318. default:
  319. /* just skip over unknown classes: */
  320. return dp_size(dev->parent);
  321. }
  322. }
  323. /*
  324. * Recursively build a device path.
  325. *
  326. * @buf pointer to the end of the device path
  327. * @dev device
  328. * @return pointer to the end of the device path
  329. */
  330. static void *dp_fill(void *buf, struct udevice *dev)
  331. {
  332. if (!dev || !dev->driver)
  333. return buf;
  334. switch (dev->driver->id) {
  335. case UCLASS_ROOT:
  336. case UCLASS_SIMPLE_BUS: {
  337. /* stop traversing parents at this point: */
  338. struct efi_device_path_vendor *vdp = buf;
  339. *vdp = ROOT;
  340. return &vdp[1];
  341. }
  342. #ifdef CONFIG_DM_ETH
  343. case UCLASS_ETH: {
  344. struct efi_device_path_mac_addr *dp =
  345. dp_fill(buf, dev->parent);
  346. struct eth_pdata *pdata = dev->platdata;
  347. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  348. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  349. dp->dp.length = sizeof(*dp);
  350. memset(&dp->mac, 0, sizeof(dp->mac));
  351. /* We only support IPv4 */
  352. memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
  353. /* Ethernet */
  354. dp->if_type = 1;
  355. return &dp[1];
  356. }
  357. #endif
  358. #ifdef CONFIG_BLK
  359. case UCLASS_BLK:
  360. switch (dev->parent->uclass->uc_drv->id) {
  361. #ifdef CONFIG_IDE
  362. case UCLASS_IDE: {
  363. struct efi_device_path_atapi *dp =
  364. dp_fill(buf, dev->parent);
  365. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  366. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  367. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
  368. dp->dp.length = sizeof(*dp);
  369. dp->logical_unit_number = desc->devnum;
  370. dp->primary_secondary = IDE_BUS(desc->devnum);
  371. dp->slave_master = desc->devnum %
  372. (CONFIG_SYS_IDE_MAXDEVICE /
  373. CONFIG_SYS_IDE_MAXBUS);
  374. return &dp[1];
  375. }
  376. #endif
  377. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  378. case UCLASS_SCSI: {
  379. struct efi_device_path_scsi *dp =
  380. dp_fill(buf, dev->parent);
  381. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  382. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  383. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
  384. dp->dp.length = sizeof(*dp);
  385. dp->logical_unit_number = desc->lun;
  386. dp->target_id = desc->target;
  387. return &dp[1];
  388. }
  389. #endif
  390. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  391. case UCLASS_MMC: {
  392. struct efi_device_path_sd_mmc_path *sddp =
  393. dp_fill(buf, dev->parent);
  394. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  395. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  396. sddp->dp.sub_type = is_sd(desc) ?
  397. DEVICE_PATH_SUB_TYPE_MSG_SD :
  398. DEVICE_PATH_SUB_TYPE_MSG_MMC;
  399. sddp->dp.length = sizeof(*sddp);
  400. sddp->slot_number = dev->seq;
  401. return &sddp[1];
  402. }
  403. #endif
  404. default:
  405. debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
  406. __FILE__, __LINE__, __func__,
  407. dev->name, dev->parent->uclass->uc_drv->id);
  408. return dp_fill(buf, dev->parent);
  409. }
  410. #endif
  411. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  412. case UCLASS_MMC: {
  413. struct efi_device_path_sd_mmc_path *sddp =
  414. dp_fill(buf, dev->parent);
  415. struct mmc *mmc = mmc_get_mmc_dev(dev);
  416. struct blk_desc *desc = mmc_get_blk_desc(mmc);
  417. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  418. sddp->dp.sub_type = is_sd(desc) ?
  419. DEVICE_PATH_SUB_TYPE_MSG_SD :
  420. DEVICE_PATH_SUB_TYPE_MSG_MMC;
  421. sddp->dp.length = sizeof(*sddp);
  422. sddp->slot_number = dev->seq;
  423. return &sddp[1];
  424. }
  425. #endif
  426. case UCLASS_MASS_STORAGE:
  427. case UCLASS_USB_HUB: {
  428. struct efi_device_path_usb_class *udp =
  429. dp_fill(buf, dev->parent);
  430. struct usb_device *udev = dev_get_parent_priv(dev);
  431. struct usb_device_descriptor *desc = &udev->descriptor;
  432. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  433. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
  434. udp->dp.length = sizeof(*udp);
  435. udp->vendor_id = desc->idVendor;
  436. udp->product_id = desc->idProduct;
  437. udp->device_class = desc->bDeviceClass;
  438. udp->device_subclass = desc->bDeviceSubClass;
  439. udp->device_protocol = desc->bDeviceProtocol;
  440. return &udp[1];
  441. }
  442. default:
  443. debug("%s(%u) %s: unhandled device class: %s (%u)\n",
  444. __FILE__, __LINE__, __func__,
  445. dev->name, dev->driver->id);
  446. return dp_fill(buf, dev->parent);
  447. }
  448. }
  449. /* Construct a device-path from a device: */
  450. struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
  451. {
  452. void *buf, *start;
  453. start = buf = dp_alloc(dp_size(dev) + sizeof(END));
  454. if (!buf)
  455. return NULL;
  456. buf = dp_fill(buf, dev);
  457. *((struct efi_device_path *)buf) = END;
  458. return start;
  459. }
  460. #endif
  461. static unsigned dp_part_size(struct blk_desc *desc, int part)
  462. {
  463. unsigned dpsize;
  464. #ifdef CONFIG_BLK
  465. {
  466. struct udevice *dev;
  467. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  468. if (ret)
  469. dev = desc->bdev->parent;
  470. dpsize = dp_size(dev);
  471. }
  472. #else
  473. dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
  474. #endif
  475. if (part == 0) /* the actual disk, not a partition */
  476. return dpsize;
  477. if (desc->part_type == PART_TYPE_ISO)
  478. dpsize += sizeof(struct efi_device_path_cdrom_path);
  479. else
  480. dpsize += sizeof(struct efi_device_path_hard_drive_path);
  481. return dpsize;
  482. }
  483. /*
  484. * Create a device node for a block device partition.
  485. *
  486. * @buf buffer to which the device path is wirtten
  487. * @desc block device descriptor
  488. * @part partition number, 0 identifies a block device
  489. */
  490. static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
  491. {
  492. disk_partition_t info;
  493. part_get_info(desc, part, &info);
  494. if (desc->part_type == PART_TYPE_ISO) {
  495. struct efi_device_path_cdrom_path *cddp = buf;
  496. cddp->boot_entry = part;
  497. cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  498. cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
  499. cddp->dp.length = sizeof(*cddp);
  500. cddp->partition_start = info.start;
  501. cddp->partition_end = info.size;
  502. buf = &cddp[1];
  503. } else {
  504. struct efi_device_path_hard_drive_path *hddp = buf;
  505. hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  506. hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
  507. hddp->dp.length = sizeof(*hddp);
  508. hddp->partition_number = part;
  509. hddp->partition_start = info.start;
  510. hddp->partition_end = info.size;
  511. if (desc->part_type == PART_TYPE_EFI)
  512. hddp->partmap_type = 2;
  513. else
  514. hddp->partmap_type = 1;
  515. switch (desc->sig_type) {
  516. case SIG_TYPE_NONE:
  517. default:
  518. hddp->signature_type = 0;
  519. memset(hddp->partition_signature, 0,
  520. sizeof(hddp->partition_signature));
  521. break;
  522. case SIG_TYPE_MBR:
  523. hddp->signature_type = 1;
  524. memset(hddp->partition_signature, 0,
  525. sizeof(hddp->partition_signature));
  526. memcpy(hddp->partition_signature, &desc->mbr_sig,
  527. sizeof(desc->mbr_sig));
  528. break;
  529. case SIG_TYPE_GUID:
  530. hddp->signature_type = 2;
  531. memcpy(hddp->partition_signature, &desc->guid_sig,
  532. sizeof(hddp->partition_signature));
  533. break;
  534. }
  535. buf = &hddp[1];
  536. }
  537. return buf;
  538. }
  539. /*
  540. * Create a device path for a block device or one of its partitions.
  541. *
  542. * @buf buffer to which the device path is wirtten
  543. * @desc block device descriptor
  544. * @part partition number, 0 identifies a block device
  545. */
  546. static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
  547. {
  548. #ifdef CONFIG_BLK
  549. {
  550. struct udevice *dev;
  551. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  552. if (ret)
  553. dev = desc->bdev->parent;
  554. buf = dp_fill(buf, dev);
  555. }
  556. #else
  557. /*
  558. * We *could* make a more accurate path, by looking at if_type
  559. * and handling all the different cases like we do for non-
  560. * legacy (ie CONFIG_BLK=y) case. But most important thing
  561. * is just to have a unique device-path for if_type+devnum.
  562. * So map things to a fictitious USB device.
  563. */
  564. struct efi_device_path_usb *udp;
  565. memcpy(buf, &ROOT, sizeof(ROOT));
  566. buf += sizeof(ROOT);
  567. udp = buf;
  568. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  569. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
  570. udp->dp.length = sizeof(*udp);
  571. udp->parent_port_number = desc->if_type;
  572. udp->usb_interface = desc->devnum;
  573. buf = &udp[1];
  574. #endif
  575. if (part == 0) /* the actual disk, not a partition */
  576. return buf;
  577. return dp_part_node(buf, desc, part);
  578. }
  579. /* Construct a device-path from a partition on a blk device: */
  580. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
  581. {
  582. void *buf, *start;
  583. start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
  584. if (!buf)
  585. return NULL;
  586. buf = dp_part_fill(buf, desc, part);
  587. *((struct efi_device_path *)buf) = END;
  588. return start;
  589. }
  590. /*
  591. * Create a device node for a block device partition.
  592. *
  593. * @buf buffer to which the device path is wirtten
  594. * @desc block device descriptor
  595. * @part partition number, 0 identifies a block device
  596. */
  597. struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
  598. {
  599. efi_uintn_t dpsize;
  600. void *buf;
  601. if (desc->part_type == PART_TYPE_ISO)
  602. dpsize = sizeof(struct efi_device_path_cdrom_path);
  603. else
  604. dpsize = sizeof(struct efi_device_path_hard_drive_path);
  605. buf = dp_alloc(dpsize);
  606. dp_part_node(buf, desc, part);
  607. return buf;
  608. }
  609. /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
  610. static void path_to_uefi(u16 *uefi, const char *path)
  611. {
  612. while (*path) {
  613. char c = *(path++);
  614. if (c == '/')
  615. c = '\\';
  616. *(uefi++) = c;
  617. }
  618. *uefi = '\0';
  619. }
  620. /*
  621. * If desc is NULL, this creates a path with only the file component,
  622. * otherwise it creates a full path with both device and file components
  623. */
  624. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  625. const char *path)
  626. {
  627. struct efi_device_path_file_path *fp;
  628. void *buf, *start;
  629. unsigned dpsize = 0, fpsize;
  630. if (desc)
  631. dpsize = dp_part_size(desc, part);
  632. fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
  633. dpsize += fpsize;
  634. start = buf = dp_alloc(dpsize + sizeof(END));
  635. if (!buf)
  636. return NULL;
  637. if (desc)
  638. buf = dp_part_fill(buf, desc, part);
  639. /* add file-path: */
  640. fp = buf;
  641. fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  642. fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  643. fp->dp.length = fpsize;
  644. path_to_uefi(fp->str, path);
  645. buf += fpsize;
  646. *((struct efi_device_path *)buf) = END;
  647. return start;
  648. }
  649. #ifdef CONFIG_NET
  650. struct efi_device_path *efi_dp_from_eth(void)
  651. {
  652. #ifndef CONFIG_DM_ETH
  653. struct efi_device_path_mac_addr *ndp;
  654. #endif
  655. void *buf, *start;
  656. unsigned dpsize = 0;
  657. assert(eth_get_dev());
  658. #ifdef CONFIG_DM_ETH
  659. dpsize += dp_size(eth_get_dev());
  660. #else
  661. dpsize += sizeof(ROOT);
  662. dpsize += sizeof(*ndp);
  663. #endif
  664. start = buf = dp_alloc(dpsize + sizeof(END));
  665. if (!buf)
  666. return NULL;
  667. #ifdef CONFIG_DM_ETH
  668. buf = dp_fill(buf, eth_get_dev());
  669. #else
  670. memcpy(buf, &ROOT, sizeof(ROOT));
  671. buf += sizeof(ROOT);
  672. ndp = buf;
  673. ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  674. ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  675. ndp->dp.length = sizeof(*ndp);
  676. ndp->if_type = 1; /* Ethernet */
  677. memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
  678. buf = &ndp[1];
  679. #endif
  680. *((struct efi_device_path *)buf) = END;
  681. return start;
  682. }
  683. #endif
  684. /* Construct a device-path for memory-mapped image */
  685. struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  686. uint64_t start_address,
  687. uint64_t end_address)
  688. {
  689. struct efi_device_path_memory *mdp;
  690. void *buf, *start;
  691. start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
  692. if (!buf)
  693. return NULL;
  694. mdp = buf;
  695. mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  696. mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
  697. mdp->dp.length = sizeof(*mdp);
  698. mdp->memory_type = memory_type;
  699. mdp->start_address = start_address;
  700. mdp->end_address = end_address;
  701. buf = &mdp[1];
  702. *((struct efi_device_path *)buf) = END;
  703. return start;
  704. }
  705. /*
  706. * Helper to split a full device path (containing both device and file
  707. * parts) into it's constituent parts.
  708. */
  709. efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
  710. struct efi_device_path **device_path,
  711. struct efi_device_path **file_path)
  712. {
  713. struct efi_device_path *p, *dp, *fp;
  714. *device_path = NULL;
  715. *file_path = NULL;
  716. dp = efi_dp_dup(full_path);
  717. if (!dp)
  718. return EFI_OUT_OF_RESOURCES;
  719. p = dp;
  720. while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
  721. p = efi_dp_next(p);
  722. if (!p)
  723. return EFI_OUT_OF_RESOURCES;
  724. }
  725. fp = efi_dp_dup(p);
  726. if (!fp)
  727. return EFI_OUT_OF_RESOURCES;
  728. p->type = DEVICE_PATH_TYPE_END;
  729. p->sub_type = DEVICE_PATH_SUB_TYPE_END;
  730. p->length = sizeof(*p);
  731. *device_path = dp;
  732. *file_path = fp;
  733. return EFI_SUCCESS;
  734. }