efi_device_path.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  35. /*
  36. * Determine if an MMC device is an SD card.
  37. *
  38. * @desc block device descriptor
  39. * @return true if the device is an SD card
  40. */
  41. static bool is_sd(struct blk_desc *desc)
  42. {
  43. struct mmc *mmc = find_mmc_device(desc->devnum);
  44. if (!mmc)
  45. return false;
  46. return IS_SD(mmc) != 0U;
  47. }
  48. #endif
  49. static void *dp_alloc(size_t sz)
  50. {
  51. void *buf;
  52. if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) != EFI_SUCCESS)
  53. return NULL;
  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. unsigned int dp_size = efi_dp_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_size(obj_dp);
  145. return efiobj;
  146. } else {
  147. /* Only return on exact matches */
  148. if (efi_dp_size(obj_dp) == dp_size)
  149. return efiobj;
  150. }
  151. }
  152. obj_dp = shorten_path(efi_dp_next(obj_dp));
  153. } while (short_path && obj_dp);
  154. }
  155. return NULL;
  156. }
  157. /*
  158. * Find an efiobj from device-path, if 'rem' is not NULL, returns the
  159. * remaining part of the device path after the matched object.
  160. */
  161. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  162. struct efi_device_path **rem)
  163. {
  164. struct efi_object *efiobj;
  165. /* Search for an exact match first */
  166. efiobj = find_obj(dp, false, NULL);
  167. /* Then for a fuzzy match */
  168. if (!efiobj)
  169. efiobj = find_obj(dp, false, rem);
  170. /* And now for a fuzzy short match */
  171. if (!efiobj)
  172. efiobj = find_obj(dp, true, rem);
  173. return efiobj;
  174. }
  175. /* return size not including End node: */
  176. unsigned efi_dp_size(const struct efi_device_path *dp)
  177. {
  178. unsigned sz = 0;
  179. while (dp) {
  180. sz += dp->length;
  181. dp = efi_dp_next(dp);
  182. }
  183. return sz;
  184. }
  185. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
  186. {
  187. struct efi_device_path *ndp;
  188. unsigned sz = efi_dp_size(dp) + sizeof(END);
  189. if (!dp)
  190. return NULL;
  191. ndp = dp_alloc(sz);
  192. memcpy(ndp, dp, sz);
  193. return ndp;
  194. }
  195. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  196. const struct efi_device_path *dp2)
  197. {
  198. struct efi_device_path *ret;
  199. if (!dp1) {
  200. ret = efi_dp_dup(dp2);
  201. } else if (!dp2) {
  202. ret = efi_dp_dup(dp1);
  203. } else {
  204. /* both dp1 and dp2 are non-null */
  205. unsigned sz1 = efi_dp_size(dp1);
  206. unsigned sz2 = efi_dp_size(dp2);
  207. void *p = dp_alloc(sz1 + sz2 + sizeof(END));
  208. memcpy(p, dp1, sz1);
  209. memcpy(p + sz1, dp2, sz2);
  210. memcpy(p + sz1 + sz2, &END, sizeof(END));
  211. ret = p;
  212. }
  213. return ret;
  214. }
  215. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  216. const struct efi_device_path *node)
  217. {
  218. struct efi_device_path *ret;
  219. if (!node && !dp) {
  220. ret = efi_dp_dup(&END);
  221. } else if (!node) {
  222. ret = efi_dp_dup(dp);
  223. } else if (!dp) {
  224. unsigned sz = node->length;
  225. void *p = dp_alloc(sz + sizeof(END));
  226. memcpy(p, node, sz);
  227. memcpy(p + sz, &END, sizeof(END));
  228. ret = p;
  229. } else {
  230. /* both dp and node are non-null */
  231. unsigned sz = efi_dp_size(dp);
  232. void *p = dp_alloc(sz + node->length + sizeof(END));
  233. memcpy(p, dp, sz);
  234. memcpy(p + sz, node, node->length);
  235. memcpy(p + sz + node->length, &END, sizeof(END));
  236. ret = p;
  237. }
  238. return ret;
  239. }
  240. #ifdef CONFIG_DM
  241. /* size of device-path not including END node for device and all parents
  242. * up to the root device.
  243. */
  244. static unsigned dp_size(struct udevice *dev)
  245. {
  246. if (!dev || !dev->driver)
  247. return sizeof(ROOT);
  248. switch (dev->driver->id) {
  249. case UCLASS_ROOT:
  250. case UCLASS_SIMPLE_BUS:
  251. /* stop traversing parents at this point: */
  252. return sizeof(ROOT);
  253. #ifdef CONFIG_BLK
  254. case UCLASS_BLK:
  255. switch (dev->parent->uclass->uc_drv->id) {
  256. #ifdef CONFIG_IDE
  257. case UCLASS_IDE:
  258. return dp_size(dev->parent) +
  259. sizeof(struct efi_device_path_atapi);
  260. #endif
  261. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  262. case UCLASS_SCSI:
  263. return dp_size(dev->parent) +
  264. sizeof(struct efi_device_path_scsi);
  265. #endif
  266. default:
  267. return dp_size(dev->parent);
  268. }
  269. #endif
  270. case UCLASS_MMC:
  271. return dp_size(dev->parent) +
  272. sizeof(struct efi_device_path_sd_mmc_path);
  273. case UCLASS_MASS_STORAGE:
  274. case UCLASS_USB_HUB:
  275. return dp_size(dev->parent) +
  276. sizeof(struct efi_device_path_usb_class);
  277. default:
  278. /* just skip over unknown classes: */
  279. return dp_size(dev->parent);
  280. }
  281. }
  282. /*
  283. * Recursively build a device path.
  284. *
  285. * @buf pointer to the end of the device path
  286. * @dev device
  287. * @return pointer to the end of the device path
  288. */
  289. static void *dp_fill(void *buf, struct udevice *dev)
  290. {
  291. if (!dev || !dev->driver)
  292. return buf;
  293. switch (dev->driver->id) {
  294. case UCLASS_ROOT:
  295. case UCLASS_SIMPLE_BUS: {
  296. /* stop traversing parents at this point: */
  297. struct efi_device_path_vendor *vdp = buf;
  298. *vdp = ROOT;
  299. return &vdp[1];
  300. }
  301. #ifdef CONFIG_BLK
  302. case UCLASS_BLK:
  303. switch (dev->parent->uclass->uc_drv->id) {
  304. #ifdef CONFIG_IDE
  305. case UCLASS_IDE: {
  306. struct efi_device_path_atapi *dp =
  307. dp_fill(buf, dev->parent);
  308. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  309. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  310. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
  311. dp->dp.length = sizeof(*dp);
  312. dp->logical_unit_number = desc->devnum;
  313. dp->primary_secondary = IDE_BUS(desc->devnum);
  314. dp->slave_master = desc->devnum %
  315. (CONFIG_SYS_IDE_MAXDEVICE /
  316. CONFIG_SYS_IDE_MAXBUS);
  317. return &dp[1];
  318. }
  319. #endif
  320. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  321. case UCLASS_SCSI: {
  322. struct efi_device_path_scsi *dp =
  323. dp_fill(buf, dev->parent);
  324. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  325. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  326. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
  327. dp->dp.length = sizeof(*dp);
  328. dp->logical_unit_number = desc->lun;
  329. dp->target_id = desc->target;
  330. return &dp[1];
  331. }
  332. #endif
  333. default:
  334. printf("unhandled parent class: %s (%u)\n",
  335. dev->name, dev->driver->id);
  336. return dp_fill(buf, dev->parent);
  337. }
  338. #endif
  339. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  340. case UCLASS_MMC: {
  341. struct efi_device_path_sd_mmc_path *sddp =
  342. dp_fill(buf, dev->parent);
  343. struct mmc *mmc = mmc_get_mmc_dev(dev);
  344. struct blk_desc *desc = mmc_get_blk_desc(mmc);
  345. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  346. sddp->dp.sub_type = is_sd(desc) ?
  347. DEVICE_PATH_SUB_TYPE_MSG_SD :
  348. DEVICE_PATH_SUB_TYPE_MSG_MMC;
  349. sddp->dp.length = sizeof(*sddp);
  350. sddp->slot_number = dev->seq;
  351. return &sddp[1];
  352. }
  353. #endif
  354. case UCLASS_MASS_STORAGE:
  355. case UCLASS_USB_HUB: {
  356. struct efi_device_path_usb_class *udp =
  357. dp_fill(buf, dev->parent);
  358. struct usb_device *udev = dev_get_parent_priv(dev);
  359. struct usb_device_descriptor *desc = &udev->descriptor;
  360. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  361. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
  362. udp->dp.length = sizeof(*udp);
  363. udp->vendor_id = desc->idVendor;
  364. udp->product_id = desc->idProduct;
  365. udp->device_class = desc->bDeviceClass;
  366. udp->device_subclass = desc->bDeviceSubClass;
  367. udp->device_protocol = desc->bDeviceProtocol;
  368. return &udp[1];
  369. }
  370. default:
  371. debug("unhandled device class: %s (%u)\n",
  372. dev->name, dev->driver->id);
  373. return dp_fill(buf, dev->parent);
  374. }
  375. }
  376. /* Construct a device-path from a device: */
  377. struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
  378. {
  379. void *buf, *start;
  380. start = buf = dp_alloc(dp_size(dev) + sizeof(END));
  381. buf = dp_fill(buf, dev);
  382. *((struct efi_device_path *)buf) = END;
  383. return start;
  384. }
  385. #endif
  386. static unsigned dp_part_size(struct blk_desc *desc, int part)
  387. {
  388. unsigned dpsize;
  389. #ifdef CONFIG_BLK
  390. {
  391. struct udevice *dev;
  392. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  393. if (ret)
  394. dev = desc->bdev->parent;
  395. dpsize = dp_size(dev);
  396. }
  397. #else
  398. dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
  399. #endif
  400. if (part == 0) /* the actual disk, not a partition */
  401. return dpsize;
  402. if (desc->part_type == PART_TYPE_ISO)
  403. dpsize += sizeof(struct efi_device_path_cdrom_path);
  404. else
  405. dpsize += sizeof(struct efi_device_path_hard_drive_path);
  406. return dpsize;
  407. }
  408. /*
  409. * Create a device path for a block device or one of its partitions.
  410. *
  411. * @buf buffer to which the device path is wirtten
  412. * @desc block device descriptor
  413. * @part partition number, 0 identifies a block device
  414. */
  415. static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
  416. {
  417. disk_partition_t info;
  418. #ifdef CONFIG_BLK
  419. {
  420. struct udevice *dev;
  421. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  422. if (ret)
  423. dev = desc->bdev->parent;
  424. buf = dp_fill(buf, dev);
  425. }
  426. #else
  427. /*
  428. * We *could* make a more accurate path, by looking at if_type
  429. * and handling all the different cases like we do for non-
  430. * legacy (ie CONFIG_BLK=y) case. But most important thing
  431. * is just to have a unique device-path for if_type+devnum.
  432. * So map things to a fictitious USB device.
  433. */
  434. struct efi_device_path_usb *udp;
  435. memcpy(buf, &ROOT, sizeof(ROOT));
  436. buf += sizeof(ROOT);
  437. udp = buf;
  438. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  439. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
  440. udp->dp.length = sizeof(*udp);
  441. udp->parent_port_number = desc->if_type;
  442. udp->usb_interface = desc->devnum;
  443. buf = &udp[1];
  444. #endif
  445. if (part == 0) /* the actual disk, not a partition */
  446. return buf;
  447. part_get_info(desc, part, &info);
  448. if (desc->part_type == PART_TYPE_ISO) {
  449. struct efi_device_path_cdrom_path *cddp = buf;
  450. cddp->boot_entry = part;
  451. cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  452. cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
  453. cddp->dp.length = sizeof(*cddp);
  454. cddp->partition_start = info.start;
  455. cddp->partition_end = info.size;
  456. buf = &cddp[1];
  457. } else {
  458. struct efi_device_path_hard_drive_path *hddp = buf;
  459. hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  460. hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
  461. hddp->dp.length = sizeof(*hddp);
  462. hddp->partition_number = part;
  463. hddp->partition_start = info.start;
  464. hddp->partition_end = info.size;
  465. if (desc->part_type == PART_TYPE_EFI)
  466. hddp->partmap_type = 2;
  467. else
  468. hddp->partmap_type = 1;
  469. switch (desc->sig_type) {
  470. case SIG_TYPE_NONE:
  471. default:
  472. hddp->signature_type = 0;
  473. memset(hddp->partition_signature, 0,
  474. sizeof(hddp->partition_signature));
  475. break;
  476. case SIG_TYPE_MBR:
  477. hddp->signature_type = 1;
  478. memset(hddp->partition_signature, 0,
  479. sizeof(hddp->partition_signature));
  480. memcpy(hddp->partition_signature, &desc->mbr_sig,
  481. sizeof(desc->mbr_sig));
  482. break;
  483. case SIG_TYPE_GUID:
  484. hddp->signature_type = 2;
  485. memcpy(hddp->partition_signature, &desc->guid_sig,
  486. sizeof(hddp->partition_signature));
  487. break;
  488. }
  489. buf = &hddp[1];
  490. }
  491. return buf;
  492. }
  493. /* Construct a device-path from a partition on a blk device: */
  494. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
  495. {
  496. void *buf, *start;
  497. start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
  498. buf = dp_part_fill(buf, desc, part);
  499. *((struct efi_device_path *)buf) = END;
  500. return start;
  501. }
  502. /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
  503. static void path_to_uefi(u16 *uefi, const char *path)
  504. {
  505. while (*path) {
  506. char c = *(path++);
  507. if (c == '/')
  508. c = '\\';
  509. *(uefi++) = c;
  510. }
  511. *uefi = '\0';
  512. }
  513. /*
  514. * If desc is NULL, this creates a path with only the file component,
  515. * otherwise it creates a full path with both device and file components
  516. */
  517. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  518. const char *path)
  519. {
  520. struct efi_device_path_file_path *fp;
  521. void *buf, *start;
  522. unsigned dpsize = 0, fpsize;
  523. if (desc)
  524. dpsize = dp_part_size(desc, part);
  525. fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
  526. dpsize += fpsize;
  527. start = buf = dp_alloc(dpsize + sizeof(END));
  528. if (desc)
  529. buf = dp_part_fill(buf, desc, part);
  530. /* add file-path: */
  531. fp = buf;
  532. fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  533. fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  534. fp->dp.length = fpsize;
  535. path_to_uefi(fp->str, path);
  536. buf += fpsize;
  537. *((struct efi_device_path *)buf) = END;
  538. return start;
  539. }
  540. #ifdef CONFIG_NET
  541. struct efi_device_path *efi_dp_from_eth(void)
  542. {
  543. struct efi_device_path_mac_addr *ndp;
  544. void *buf, *start;
  545. unsigned dpsize = 0;
  546. assert(eth_get_dev());
  547. #ifdef CONFIG_DM_ETH
  548. dpsize += dp_size(eth_get_dev());
  549. #else
  550. dpsize += sizeof(ROOT);
  551. #endif
  552. dpsize += sizeof(*ndp);
  553. start = buf = dp_alloc(dpsize + sizeof(END));
  554. #ifdef CONFIG_DM_ETH
  555. buf = dp_fill(buf, eth_get_dev());
  556. #else
  557. memcpy(buf, &ROOT, sizeof(ROOT));
  558. buf += sizeof(ROOT);
  559. #endif
  560. ndp = buf;
  561. ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  562. ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  563. ndp->dp.length = sizeof(*ndp);
  564. memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
  565. buf = &ndp[1];
  566. *((struct efi_device_path *)buf) = END;
  567. return start;
  568. }
  569. #endif
  570. /* Construct a device-path for memory-mapped image */
  571. struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  572. uint64_t start_address,
  573. uint64_t end_address)
  574. {
  575. struct efi_device_path_memory *mdp;
  576. void *buf, *start;
  577. start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
  578. mdp = buf;
  579. mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  580. mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
  581. mdp->dp.length = sizeof(*mdp);
  582. mdp->memory_type = memory_type;
  583. mdp->start_address = start_address;
  584. mdp->end_address = end_address;
  585. buf = &mdp[1];
  586. *((struct efi_device_path *)buf) = END;
  587. return start;
  588. }
  589. /*
  590. * Helper to split a full device path (containing both device and file
  591. * parts) into it's constituent parts.
  592. */
  593. void efi_dp_split_file_path(struct efi_device_path *full_path,
  594. struct efi_device_path **device_path,
  595. struct efi_device_path **file_path)
  596. {
  597. struct efi_device_path *p, *dp, *fp;
  598. dp = efi_dp_dup(full_path);
  599. p = dp;
  600. while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
  601. p = efi_dp_next(p);
  602. fp = efi_dp_dup(p);
  603. p->type = DEVICE_PATH_TYPE_END;
  604. p->sub_type = DEVICE_PATH_SUB_TYPE_END;
  605. p->length = sizeof(*p);
  606. *device_path = dp;
  607. *file_path = fp;
  608. }