efi_device_path.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. #include <common.h>
  8. #include <blk.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <net.h>
  12. #include <usb.h>
  13. #include <mmc.h>
  14. #include <nvme.h>
  15. #include <efi_loader.h>
  16. #include <part.h>
  17. #include <sandboxblockdev.h>
  18. #include <asm-generic/unaligned.h>
  19. #include <linux/compat.h> /* U16_MAX */
  20. #ifdef CONFIG_SANDBOX
  21. const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
  22. #endif
  23. #ifdef CONFIG_VIRTIO_BLK
  24. const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
  25. #endif
  26. /* template END node: */
  27. static const struct efi_device_path END = {
  28. .type = DEVICE_PATH_TYPE_END,
  29. .sub_type = DEVICE_PATH_SUB_TYPE_END,
  30. .length = sizeof(END),
  31. };
  32. /* template ROOT node: */
  33. static const struct efi_device_path_vendor ROOT = {
  34. .dp = {
  35. .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  36. .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
  37. .length = sizeof(ROOT),
  38. },
  39. .guid = U_BOOT_GUID,
  40. };
  41. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  42. /*
  43. * Determine if an MMC device is an SD card.
  44. *
  45. * @desc block device descriptor
  46. * @return true if the device is an SD card
  47. */
  48. static bool is_sd(struct blk_desc *desc)
  49. {
  50. struct mmc *mmc = find_mmc_device(desc->devnum);
  51. if (!mmc)
  52. return false;
  53. return IS_SD(mmc) != 0U;
  54. }
  55. #endif
  56. static void *dp_alloc(size_t sz)
  57. {
  58. void *buf;
  59. if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
  60. EFI_SUCCESS) {
  61. debug("EFI: ERROR: out of memory in %s\n", __func__);
  62. return NULL;
  63. }
  64. memset(buf, 0, sz);
  65. return buf;
  66. }
  67. /*
  68. * Iterate to next block in device-path, terminating (returning NULL)
  69. * at /End* node.
  70. */
  71. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
  72. {
  73. if (dp == NULL)
  74. return NULL;
  75. if (dp->type == DEVICE_PATH_TYPE_END)
  76. return NULL;
  77. dp = ((void *)dp) + dp->length;
  78. if (dp->type == DEVICE_PATH_TYPE_END)
  79. return NULL;
  80. return (struct efi_device_path *)dp;
  81. }
  82. /*
  83. * Compare two device-paths, stopping when the shorter of the two hits
  84. * an End* node. This is useful to, for example, compare a device-path
  85. * representing a device with one representing a file on the device, or
  86. * a device with a parent device.
  87. */
  88. int efi_dp_match(const struct efi_device_path *a,
  89. const struct efi_device_path *b)
  90. {
  91. while (1) {
  92. int ret;
  93. ret = memcmp(&a->length, &b->length, sizeof(a->length));
  94. if (ret)
  95. return ret;
  96. ret = memcmp(a, b, a->length);
  97. if (ret)
  98. return ret;
  99. a = efi_dp_next(a);
  100. b = efi_dp_next(b);
  101. if (!a || !b)
  102. return 0;
  103. }
  104. }
  105. /*
  106. * We can have device paths that start with a USB WWID or a USB Class node,
  107. * and a few other cases which don't encode the full device path with bus
  108. * hierarchy:
  109. *
  110. * - MESSAGING:USB_WWID
  111. * - MESSAGING:USB_CLASS
  112. * - MEDIA:FILE_PATH
  113. * - MEDIA:HARD_DRIVE
  114. * - MESSAGING:URI
  115. *
  116. * See UEFI spec (section 3.1.2, about short-form device-paths)
  117. */
  118. static struct efi_device_path *shorten_path(struct efi_device_path *dp)
  119. {
  120. while (dp) {
  121. /*
  122. * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
  123. * in practice fallback.efi just uses MEDIA:HARD_DRIVE
  124. * so not sure when we would see these other cases.
  125. */
  126. if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
  127. EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
  128. EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
  129. return dp;
  130. dp = efi_dp_next(dp);
  131. }
  132. return dp;
  133. }
  134. static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
  135. struct efi_device_path **rem)
  136. {
  137. struct efi_object *efiobj;
  138. efi_uintn_t dp_size = efi_dp_instance_size(dp);
  139. list_for_each_entry(efiobj, &efi_obj_list, link) {
  140. struct efi_handler *handler;
  141. struct efi_device_path *obj_dp;
  142. efi_status_t ret;
  143. ret = efi_search_protocol(efiobj,
  144. &efi_guid_device_path, &handler);
  145. if (ret != EFI_SUCCESS)
  146. continue;
  147. obj_dp = handler->protocol_interface;
  148. do {
  149. if (efi_dp_match(dp, obj_dp) == 0) {
  150. if (rem) {
  151. /*
  152. * Allow partial matches, but inform
  153. * the caller.
  154. */
  155. *rem = ((void *)dp) +
  156. efi_dp_instance_size(obj_dp);
  157. return efiobj;
  158. } else {
  159. /* Only return on exact matches */
  160. if (efi_dp_instance_size(obj_dp) ==
  161. dp_size)
  162. return efiobj;
  163. }
  164. }
  165. obj_dp = shorten_path(efi_dp_next(obj_dp));
  166. } while (short_path && obj_dp);
  167. }
  168. return NULL;
  169. }
  170. /*
  171. * Find an efiobj from device-path, if 'rem' is not NULL, returns the
  172. * remaining part of the device path after the matched object.
  173. */
  174. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  175. struct efi_device_path **rem)
  176. {
  177. struct efi_object *efiobj;
  178. /* Search for an exact match first */
  179. efiobj = find_obj(dp, false, NULL);
  180. /* Then for a fuzzy match */
  181. if (!efiobj)
  182. efiobj = find_obj(dp, false, rem);
  183. /* And now for a fuzzy short match */
  184. if (!efiobj)
  185. efiobj = find_obj(dp, true, rem);
  186. return efiobj;
  187. }
  188. /*
  189. * Determine the last device path node that is not the end node.
  190. *
  191. * @dp device path
  192. * @return last node before the end node if it exists
  193. * otherwise NULL
  194. */
  195. const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
  196. {
  197. struct efi_device_path *ret;
  198. if (!dp || dp->type == DEVICE_PATH_TYPE_END)
  199. return NULL;
  200. while (dp) {
  201. ret = (struct efi_device_path *)dp;
  202. dp = efi_dp_next(dp);
  203. }
  204. return ret;
  205. }
  206. /* get size of the first device path instance excluding end node */
  207. efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
  208. {
  209. efi_uintn_t sz = 0;
  210. if (!dp || dp->type == DEVICE_PATH_TYPE_END)
  211. return 0;
  212. while (dp) {
  213. sz += dp->length;
  214. dp = efi_dp_next(dp);
  215. }
  216. return sz;
  217. }
  218. /* get size of multi-instance device path excluding end node */
  219. efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
  220. {
  221. const struct efi_device_path *p = dp;
  222. if (!p)
  223. return 0;
  224. while (p->type != DEVICE_PATH_TYPE_END ||
  225. p->sub_type != DEVICE_PATH_SUB_TYPE_END)
  226. p = (void *)p + p->length;
  227. return (void *)p - (void *)dp;
  228. }
  229. /* copy multi-instance device path */
  230. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
  231. {
  232. struct efi_device_path *ndp;
  233. size_t sz = efi_dp_size(dp) + sizeof(END);
  234. if (!dp)
  235. return NULL;
  236. ndp = dp_alloc(sz);
  237. if (!ndp)
  238. return NULL;
  239. memcpy(ndp, dp, sz);
  240. return ndp;
  241. }
  242. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  243. const struct efi_device_path *dp2)
  244. {
  245. struct efi_device_path *ret;
  246. if (!dp1 && !dp2) {
  247. /* return an end node */
  248. ret = efi_dp_dup(&END);
  249. } else if (!dp1) {
  250. ret = efi_dp_dup(dp2);
  251. } else if (!dp2) {
  252. ret = efi_dp_dup(dp1);
  253. } else {
  254. /* both dp1 and dp2 are non-null */
  255. unsigned sz1 = efi_dp_size(dp1);
  256. unsigned sz2 = efi_dp_size(dp2);
  257. void *p = dp_alloc(sz1 + sz2 + sizeof(END));
  258. if (!p)
  259. return NULL;
  260. memcpy(p, dp1, sz1);
  261. /* the end node of the second device path has to be retained */
  262. memcpy(p + sz1, dp2, sz2 + sizeof(END));
  263. ret = p;
  264. }
  265. return ret;
  266. }
  267. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  268. const struct efi_device_path *node)
  269. {
  270. struct efi_device_path *ret;
  271. if (!node && !dp) {
  272. ret = efi_dp_dup(&END);
  273. } else if (!node) {
  274. ret = efi_dp_dup(dp);
  275. } else if (!dp) {
  276. size_t sz = node->length;
  277. void *p = dp_alloc(sz + sizeof(END));
  278. if (!p)
  279. return NULL;
  280. memcpy(p, node, sz);
  281. memcpy(p + sz, &END, sizeof(END));
  282. ret = p;
  283. } else {
  284. /* both dp and node are non-null */
  285. size_t sz = efi_dp_size(dp);
  286. void *p = dp_alloc(sz + node->length + sizeof(END));
  287. if (!p)
  288. return NULL;
  289. memcpy(p, dp, sz);
  290. memcpy(p + sz, node, node->length);
  291. memcpy(p + sz + node->length, &END, sizeof(END));
  292. ret = p;
  293. }
  294. return ret;
  295. }
  296. struct efi_device_path *efi_dp_create_device_node(const u8 type,
  297. const u8 sub_type,
  298. const u16 length)
  299. {
  300. struct efi_device_path *ret;
  301. if (length < sizeof(struct efi_device_path))
  302. return NULL;
  303. ret = dp_alloc(length);
  304. if (!ret)
  305. return ret;
  306. ret->type = type;
  307. ret->sub_type = sub_type;
  308. ret->length = length;
  309. return ret;
  310. }
  311. struct efi_device_path *efi_dp_append_instance(
  312. const struct efi_device_path *dp,
  313. const struct efi_device_path *dpi)
  314. {
  315. size_t sz, szi;
  316. struct efi_device_path *p, *ret;
  317. if (!dpi)
  318. return NULL;
  319. if (!dp)
  320. return efi_dp_dup(dpi);
  321. sz = efi_dp_size(dp);
  322. szi = efi_dp_instance_size(dpi);
  323. p = dp_alloc(sz + szi + 2 * sizeof(END));
  324. if (!p)
  325. return NULL;
  326. ret = p;
  327. memcpy(p, dp, sz + sizeof(END));
  328. p = (void *)p + sz;
  329. p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
  330. p = (void *)p + sizeof(END);
  331. memcpy(p, dpi, szi);
  332. p = (void *)p + szi;
  333. memcpy(p, &END, sizeof(END));
  334. return ret;
  335. }
  336. struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
  337. efi_uintn_t *size)
  338. {
  339. size_t sz;
  340. struct efi_device_path *p;
  341. if (size)
  342. *size = 0;
  343. if (!dp || !*dp)
  344. return NULL;
  345. sz = efi_dp_instance_size(*dp);
  346. p = dp_alloc(sz + sizeof(END));
  347. if (!p)
  348. return NULL;
  349. memcpy(p, *dp, sz + sizeof(END));
  350. *dp = (void *)*dp + sz;
  351. if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
  352. *dp = (void *)*dp + sizeof(END);
  353. else
  354. *dp = NULL;
  355. if (size)
  356. *size = sz + sizeof(END);
  357. return p;
  358. }
  359. bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
  360. {
  361. const struct efi_device_path *p = dp;
  362. if (!p)
  363. return false;
  364. while (p->type != DEVICE_PATH_TYPE_END)
  365. p = (void *)p + p->length;
  366. return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
  367. }
  368. #ifdef CONFIG_DM
  369. /* size of device-path not including END node for device and all parents
  370. * up to the root device.
  371. */
  372. __maybe_unused static unsigned int dp_size(struct udevice *dev)
  373. {
  374. if (!dev || !dev->driver)
  375. return sizeof(ROOT);
  376. switch (dev->driver->id) {
  377. case UCLASS_ROOT:
  378. case UCLASS_SIMPLE_BUS:
  379. /* stop traversing parents at this point: */
  380. return sizeof(ROOT);
  381. case UCLASS_ETH:
  382. return dp_size(dev->parent) +
  383. sizeof(struct efi_device_path_mac_addr);
  384. #ifdef CONFIG_BLK
  385. case UCLASS_BLK:
  386. switch (dev->parent->uclass->uc_drv->id) {
  387. #ifdef CONFIG_IDE
  388. case UCLASS_IDE:
  389. return dp_size(dev->parent) +
  390. sizeof(struct efi_device_path_atapi);
  391. #endif
  392. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  393. case UCLASS_SCSI:
  394. return dp_size(dev->parent) +
  395. sizeof(struct efi_device_path_scsi);
  396. #endif
  397. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  398. case UCLASS_MMC:
  399. return dp_size(dev->parent) +
  400. sizeof(struct efi_device_path_sd_mmc_path);
  401. #endif
  402. #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
  403. case UCLASS_AHCI:
  404. return dp_size(dev->parent) +
  405. sizeof(struct efi_device_path_sata);
  406. #endif
  407. #if defined(CONFIG_NVME)
  408. case UCLASS_NVME:
  409. return dp_size(dev->parent) +
  410. sizeof(struct efi_device_path_nvme);
  411. #endif
  412. #ifdef CONFIG_SANDBOX
  413. case UCLASS_ROOT:
  414. /*
  415. * Sandbox's host device will be represented
  416. * as vendor device with extra one byte for
  417. * device number
  418. */
  419. return dp_size(dev->parent)
  420. + sizeof(struct efi_device_path_vendor) + 1;
  421. #endif
  422. #ifdef CONFIG_VIRTIO_BLK
  423. case UCLASS_VIRTIO:
  424. /*
  425. * Virtio devices will be represented as a vendor
  426. * device node with an extra byte for the device
  427. * number.
  428. */
  429. return dp_size(dev->parent)
  430. + sizeof(struct efi_device_path_vendor) + 1;
  431. #endif
  432. default:
  433. return dp_size(dev->parent);
  434. }
  435. #endif
  436. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  437. case UCLASS_MMC:
  438. return dp_size(dev->parent) +
  439. sizeof(struct efi_device_path_sd_mmc_path);
  440. #endif
  441. case UCLASS_MASS_STORAGE:
  442. case UCLASS_USB_HUB:
  443. return dp_size(dev->parent) +
  444. sizeof(struct efi_device_path_usb_class);
  445. default:
  446. /* just skip over unknown classes: */
  447. return dp_size(dev->parent);
  448. }
  449. }
  450. /*
  451. * Recursively build a device path.
  452. *
  453. * @buf pointer to the end of the device path
  454. * @dev device
  455. * @return pointer to the end of the device path
  456. */
  457. __maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
  458. {
  459. if (!dev || !dev->driver)
  460. return buf;
  461. switch (dev->driver->id) {
  462. case UCLASS_ROOT:
  463. case UCLASS_SIMPLE_BUS: {
  464. /* stop traversing parents at this point: */
  465. struct efi_device_path_vendor *vdp = buf;
  466. *vdp = ROOT;
  467. return &vdp[1];
  468. }
  469. #ifdef CONFIG_DM_ETH
  470. case UCLASS_ETH: {
  471. struct efi_device_path_mac_addr *dp =
  472. dp_fill(buf, dev->parent);
  473. struct eth_pdata *pdata = dev->plat;
  474. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  475. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  476. dp->dp.length = sizeof(*dp);
  477. memset(&dp->mac, 0, sizeof(dp->mac));
  478. /* We only support IPv4 */
  479. memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
  480. /* Ethernet */
  481. dp->if_type = 1;
  482. return &dp[1];
  483. }
  484. #endif
  485. #ifdef CONFIG_BLK
  486. case UCLASS_BLK:
  487. switch (dev->parent->uclass->uc_drv->id) {
  488. #ifdef CONFIG_SANDBOX
  489. case UCLASS_ROOT: {
  490. /* stop traversing parents at this point: */
  491. struct efi_device_path_vendor *dp;
  492. struct blk_desc *desc = dev_get_uclass_plat(dev);
  493. dp_fill(buf, dev->parent);
  494. dp = buf;
  495. ++dp;
  496. dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  497. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
  498. dp->dp.length = sizeof(*dp) + 1;
  499. memcpy(&dp->guid, &efi_guid_host_dev,
  500. sizeof(efi_guid_t));
  501. dp->vendor_data[0] = desc->devnum;
  502. return &dp->vendor_data[1];
  503. }
  504. #endif
  505. #ifdef CONFIG_VIRTIO_BLK
  506. case UCLASS_VIRTIO: {
  507. struct efi_device_path_vendor *dp;
  508. struct blk_desc *desc = dev_get_uclass_plat(dev);
  509. dp_fill(buf, dev->parent);
  510. dp = buf;
  511. ++dp;
  512. dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  513. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
  514. dp->dp.length = sizeof(*dp) + 1;
  515. memcpy(&dp->guid, &efi_guid_virtio_dev,
  516. sizeof(efi_guid_t));
  517. dp->vendor_data[0] = desc->devnum;
  518. return &dp->vendor_data[1];
  519. }
  520. #endif
  521. #ifdef CONFIG_IDE
  522. case UCLASS_IDE: {
  523. struct efi_device_path_atapi *dp =
  524. dp_fill(buf, dev->parent);
  525. struct blk_desc *desc = dev_get_uclass_plat(dev);
  526. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  527. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
  528. dp->dp.length = sizeof(*dp);
  529. dp->logical_unit_number = desc->devnum;
  530. dp->primary_secondary = IDE_BUS(desc->devnum);
  531. dp->slave_master = desc->devnum %
  532. (CONFIG_SYS_IDE_MAXDEVICE /
  533. CONFIG_SYS_IDE_MAXBUS);
  534. return &dp[1];
  535. }
  536. #endif
  537. #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
  538. case UCLASS_SCSI: {
  539. struct efi_device_path_scsi *dp =
  540. dp_fill(buf, dev->parent);
  541. struct blk_desc *desc = dev_get_uclass_plat(dev);
  542. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  543. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
  544. dp->dp.length = sizeof(*dp);
  545. dp->logical_unit_number = desc->lun;
  546. dp->target_id = desc->target;
  547. return &dp[1];
  548. }
  549. #endif
  550. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  551. case UCLASS_MMC: {
  552. struct efi_device_path_sd_mmc_path *sddp =
  553. dp_fill(buf, dev->parent);
  554. struct blk_desc *desc = dev_get_uclass_plat(dev);
  555. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  556. sddp->dp.sub_type = is_sd(desc) ?
  557. DEVICE_PATH_SUB_TYPE_MSG_SD :
  558. DEVICE_PATH_SUB_TYPE_MSG_MMC;
  559. sddp->dp.length = sizeof(*sddp);
  560. sddp->slot_number = dev_seq(dev);
  561. return &sddp[1];
  562. }
  563. #endif
  564. #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
  565. case UCLASS_AHCI: {
  566. struct efi_device_path_sata *dp =
  567. dp_fill(buf, dev->parent);
  568. struct blk_desc *desc = dev_get_uclass_plat(dev);
  569. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  570. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
  571. dp->dp.length = sizeof(*dp);
  572. dp->hba_port = desc->devnum;
  573. /* default 0xffff implies no port multiplier */
  574. dp->port_multiplier_port = 0xffff;
  575. dp->logical_unit_number = desc->lun;
  576. return &dp[1];
  577. }
  578. #endif
  579. #if defined(CONFIG_NVME)
  580. case UCLASS_NVME: {
  581. struct efi_device_path_nvme *dp =
  582. dp_fill(buf, dev->parent);
  583. u32 ns_id;
  584. dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  585. dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
  586. dp->dp.length = sizeof(*dp);
  587. nvme_get_namespace_id(dev, &ns_id, dp->eui64);
  588. memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
  589. return &dp[1];
  590. }
  591. #endif
  592. default:
  593. debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
  594. __FILE__, __LINE__, __func__,
  595. dev->name, dev->parent->uclass->uc_drv->id);
  596. return dp_fill(buf, dev->parent);
  597. }
  598. #endif
  599. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  600. case UCLASS_MMC: {
  601. struct efi_device_path_sd_mmc_path *sddp =
  602. dp_fill(buf, dev->parent);
  603. struct mmc *mmc = mmc_get_mmc_dev(dev);
  604. struct blk_desc *desc = mmc_get_blk_desc(mmc);
  605. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  606. sddp->dp.sub_type = is_sd(desc) ?
  607. DEVICE_PATH_SUB_TYPE_MSG_SD :
  608. DEVICE_PATH_SUB_TYPE_MSG_MMC;
  609. sddp->dp.length = sizeof(*sddp);
  610. sddp->slot_number = dev_seq(dev);
  611. return &sddp[1];
  612. }
  613. #endif
  614. case UCLASS_MASS_STORAGE:
  615. case UCLASS_USB_HUB: {
  616. struct efi_device_path_usb_class *udp =
  617. dp_fill(buf, dev->parent);
  618. struct usb_device *udev = dev_get_parent_priv(dev);
  619. struct usb_device_descriptor *desc = &udev->descriptor;
  620. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  621. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
  622. udp->dp.length = sizeof(*udp);
  623. udp->vendor_id = desc->idVendor;
  624. udp->product_id = desc->idProduct;
  625. udp->device_class = desc->bDeviceClass;
  626. udp->device_subclass = desc->bDeviceSubClass;
  627. udp->device_protocol = desc->bDeviceProtocol;
  628. return &udp[1];
  629. }
  630. default:
  631. debug("%s(%u) %s: unhandled device class: %s (%u)\n",
  632. __FILE__, __LINE__, __func__,
  633. dev->name, dev->driver->id);
  634. return dp_fill(buf, dev->parent);
  635. }
  636. }
  637. #endif
  638. static unsigned dp_part_size(struct blk_desc *desc, int part)
  639. {
  640. unsigned dpsize;
  641. #ifdef CONFIG_BLK
  642. {
  643. struct udevice *dev;
  644. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  645. if (ret)
  646. dev = desc->bdev->parent;
  647. dpsize = dp_size(dev);
  648. }
  649. #else
  650. dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
  651. #endif
  652. if (part == 0) /* the actual disk, not a partition */
  653. return dpsize;
  654. if (desc->part_type == PART_TYPE_ISO)
  655. dpsize += sizeof(struct efi_device_path_cdrom_path);
  656. else
  657. dpsize += sizeof(struct efi_device_path_hard_drive_path);
  658. return dpsize;
  659. }
  660. /*
  661. * Create a device node for a block device partition.
  662. *
  663. * @buf buffer to which the device path is written
  664. * @desc block device descriptor
  665. * @part partition number, 0 identifies a block device
  666. */
  667. static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
  668. {
  669. struct disk_partition info;
  670. part_get_info(desc, part, &info);
  671. if (desc->part_type == PART_TYPE_ISO) {
  672. struct efi_device_path_cdrom_path *cddp = buf;
  673. cddp->boot_entry = part;
  674. cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  675. cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
  676. cddp->dp.length = sizeof(*cddp);
  677. cddp->partition_start = info.start;
  678. cddp->partition_size = info.size;
  679. buf = &cddp[1];
  680. } else {
  681. struct efi_device_path_hard_drive_path *hddp = buf;
  682. hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  683. hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
  684. hddp->dp.length = sizeof(*hddp);
  685. hddp->partition_number = part;
  686. hddp->partition_start = info.start;
  687. hddp->partition_end = info.size;
  688. if (desc->part_type == PART_TYPE_EFI)
  689. hddp->partmap_type = 2;
  690. else
  691. hddp->partmap_type = 1;
  692. switch (desc->sig_type) {
  693. case SIG_TYPE_NONE:
  694. default:
  695. hddp->signature_type = 0;
  696. memset(hddp->partition_signature, 0,
  697. sizeof(hddp->partition_signature));
  698. break;
  699. case SIG_TYPE_MBR:
  700. hddp->signature_type = 1;
  701. memset(hddp->partition_signature, 0,
  702. sizeof(hddp->partition_signature));
  703. memcpy(hddp->partition_signature, &desc->mbr_sig,
  704. sizeof(desc->mbr_sig));
  705. break;
  706. case SIG_TYPE_GUID:
  707. hddp->signature_type = 2;
  708. memcpy(hddp->partition_signature, &desc->guid_sig,
  709. sizeof(hddp->partition_signature));
  710. break;
  711. }
  712. buf = &hddp[1];
  713. }
  714. return buf;
  715. }
  716. /*
  717. * Create a device path for a block device or one of its partitions.
  718. *
  719. * @buf buffer to which the device path is written
  720. * @desc block device descriptor
  721. * @part partition number, 0 identifies a block device
  722. */
  723. static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
  724. {
  725. #ifdef CONFIG_BLK
  726. {
  727. struct udevice *dev;
  728. int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  729. if (ret)
  730. dev = desc->bdev->parent;
  731. buf = dp_fill(buf, dev);
  732. }
  733. #else
  734. /*
  735. * We *could* make a more accurate path, by looking at if_type
  736. * and handling all the different cases like we do for non-
  737. * legacy (i.e. CONFIG_BLK=y) case. But most important thing
  738. * is just to have a unique device-path for if_type+devnum.
  739. * So map things to a fictitious USB device.
  740. */
  741. struct efi_device_path_usb *udp;
  742. memcpy(buf, &ROOT, sizeof(ROOT));
  743. buf += sizeof(ROOT);
  744. udp = buf;
  745. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  746. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
  747. udp->dp.length = sizeof(*udp);
  748. udp->parent_port_number = desc->if_type;
  749. udp->usb_interface = desc->devnum;
  750. buf = &udp[1];
  751. #endif
  752. if (part == 0) /* the actual disk, not a partition */
  753. return buf;
  754. return dp_part_node(buf, desc, part);
  755. }
  756. /* Construct a device-path from a partition on a block device: */
  757. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
  758. {
  759. void *buf, *start;
  760. start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
  761. if (!buf)
  762. return NULL;
  763. buf = dp_part_fill(buf, desc, part);
  764. *((struct efi_device_path *)buf) = END;
  765. return start;
  766. }
  767. /*
  768. * Create a device node for a block device partition.
  769. *
  770. * @buf buffer to which the device path is written
  771. * @desc block device descriptor
  772. * @part partition number, 0 identifies a block device
  773. */
  774. struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
  775. {
  776. efi_uintn_t dpsize;
  777. void *buf;
  778. if (desc->part_type == PART_TYPE_ISO)
  779. dpsize = sizeof(struct efi_device_path_cdrom_path);
  780. else
  781. dpsize = sizeof(struct efi_device_path_hard_drive_path);
  782. buf = dp_alloc(dpsize);
  783. dp_part_node(buf, desc, part);
  784. return buf;
  785. }
  786. /**
  787. * path_to_uefi() - convert UTF-8 path to an UEFI style path
  788. *
  789. * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
  790. * separators and UTF-16).
  791. *
  792. * @src: source buffer
  793. * @uefi: target buffer, possibly unaligned
  794. */
  795. static void path_to_uefi(void *uefi, const char *src)
  796. {
  797. u16 *pos = uefi;
  798. /*
  799. * efi_set_bootdev() calls this routine indirectly before the UEFI
  800. * subsystem is initialized. So we cannot assume unaligned access to be
  801. * enabled.
  802. */
  803. allow_unaligned();
  804. while (*src) {
  805. s32 code = utf8_get(&src);
  806. if (code < 0)
  807. code = '?';
  808. else if (code == '/')
  809. code = '\\';
  810. utf16_put(code, &pos);
  811. }
  812. *pos = 0;
  813. }
  814. /*
  815. * If desc is NULL, this creates a path with only the file component,
  816. * otherwise it creates a full path with both device and file components
  817. */
  818. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  819. const char *path)
  820. {
  821. struct efi_device_path_file_path *fp;
  822. void *buf, *start;
  823. size_t dpsize = 0, fpsize;
  824. if (desc)
  825. dpsize = dp_part_size(desc, part);
  826. fpsize = sizeof(struct efi_device_path) +
  827. 2 * (utf8_utf16_strlen(path) + 1);
  828. if (fpsize > U16_MAX)
  829. return NULL;
  830. dpsize += fpsize;
  831. start = buf = dp_alloc(dpsize + sizeof(END));
  832. if (!buf)
  833. return NULL;
  834. if (desc)
  835. buf = dp_part_fill(buf, desc, part);
  836. /* add file-path: */
  837. fp = buf;
  838. fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  839. fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  840. fp->dp.length = (u16)fpsize;
  841. path_to_uefi(fp->str, path);
  842. buf += fpsize;
  843. *((struct efi_device_path *)buf) = END;
  844. return start;
  845. }
  846. #ifdef CONFIG_NET
  847. struct efi_device_path *efi_dp_from_eth(void)
  848. {
  849. #ifndef CONFIG_DM_ETH
  850. struct efi_device_path_mac_addr *ndp;
  851. #endif
  852. void *buf, *start;
  853. unsigned dpsize = 0;
  854. assert(eth_get_dev());
  855. #ifdef CONFIG_DM_ETH
  856. dpsize += dp_size(eth_get_dev());
  857. #else
  858. dpsize += sizeof(ROOT);
  859. dpsize += sizeof(*ndp);
  860. #endif
  861. start = buf = dp_alloc(dpsize + sizeof(END));
  862. if (!buf)
  863. return NULL;
  864. #ifdef CONFIG_DM_ETH
  865. buf = dp_fill(buf, eth_get_dev());
  866. #else
  867. memcpy(buf, &ROOT, sizeof(ROOT));
  868. buf += sizeof(ROOT);
  869. ndp = buf;
  870. ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  871. ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  872. ndp->dp.length = sizeof(*ndp);
  873. ndp->if_type = 1; /* Ethernet */
  874. memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
  875. buf = &ndp[1];
  876. #endif
  877. *((struct efi_device_path *)buf) = END;
  878. return start;
  879. }
  880. #endif
  881. /* Construct a device-path for memory-mapped image */
  882. struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  883. uint64_t start_address,
  884. uint64_t end_address)
  885. {
  886. struct efi_device_path_memory *mdp;
  887. void *buf, *start;
  888. start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
  889. if (!buf)
  890. return NULL;
  891. mdp = buf;
  892. mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  893. mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
  894. mdp->dp.length = sizeof(*mdp);
  895. mdp->memory_type = memory_type;
  896. mdp->start_address = start_address;
  897. mdp->end_address = end_address;
  898. buf = &mdp[1];
  899. *((struct efi_device_path *)buf) = END;
  900. return start;
  901. }
  902. /**
  903. * efi_dp_split_file_path() - split of relative file path from device path
  904. *
  905. * Given a device path indicating a file on a device, separate the device
  906. * path in two: the device path of the actual device and the file path
  907. * relative to this device.
  908. *
  909. * @full_path: device path including device and file path
  910. * @device_path: path of the device
  911. * @file_path: relative path of the file or NULL if there is none
  912. * Return: status code
  913. */
  914. efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
  915. struct efi_device_path **device_path,
  916. struct efi_device_path **file_path)
  917. {
  918. struct efi_device_path *p, *dp, *fp = NULL;
  919. *device_path = NULL;
  920. *file_path = NULL;
  921. dp = efi_dp_dup(full_path);
  922. if (!dp)
  923. return EFI_OUT_OF_RESOURCES;
  924. p = dp;
  925. while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
  926. p = efi_dp_next(p);
  927. if (!p)
  928. goto out;
  929. }
  930. fp = efi_dp_dup(p);
  931. if (!fp)
  932. return EFI_OUT_OF_RESOURCES;
  933. p->type = DEVICE_PATH_TYPE_END;
  934. p->sub_type = DEVICE_PATH_SUB_TYPE_END;
  935. p->length = sizeof(*p);
  936. out:
  937. *device_path = dp;
  938. *file_path = fp;
  939. return EFI_SUCCESS;
  940. }
  941. /**
  942. * efi_dp_from_name() - convert U-Boot device and file path to device path
  943. *
  944. * @dev: U-Boot device, e.g. 'mmc'
  945. * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
  946. * @path: file path relative to U-Boot device, may be NULL
  947. * @device: pointer to receive device path of the device
  948. * @file: pointer to receive device path for the file
  949. * Return: status code
  950. */
  951. efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
  952. const char *path,
  953. struct efi_device_path **device,
  954. struct efi_device_path **file)
  955. {
  956. int is_net;
  957. struct blk_desc *desc = NULL;
  958. struct disk_partition fs_partition;
  959. int part = 0;
  960. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  961. char *s;
  962. if (path && !file)
  963. return EFI_INVALID_PARAMETER;
  964. is_net = !strcmp(dev, "Net");
  965. if (!is_net) {
  966. part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
  967. 1);
  968. if (part < 0 || !desc)
  969. return EFI_INVALID_PARAMETER;
  970. if (device)
  971. *device = efi_dp_from_part(desc, part);
  972. } else {
  973. #ifdef CONFIG_NET
  974. if (device)
  975. *device = efi_dp_from_eth();
  976. #endif
  977. }
  978. if (!path)
  979. return EFI_SUCCESS;
  980. snprintf(filename, sizeof(filename), "%s", path);
  981. /* DOS style file path: */
  982. s = filename;
  983. while ((s = strchr(s, '/')))
  984. *s++ = '\\';
  985. *file = efi_dp_from_file(is_net ? NULL : desc, part, filename);
  986. if (!*file)
  987. return EFI_INVALID_PARAMETER;
  988. return EFI_SUCCESS;
  989. }
  990. /**
  991. * efi_dp_check_length() - check length of a device path
  992. *
  993. * @dp: pointer to device path
  994. * @maxlen: maximum length of the device path
  995. * Return:
  996. * * length of the device path if it is less or equal @maxlen
  997. * * -1 if the device path is longer then @maxlen
  998. * * -1 if a device path node has a length of less than 4
  999. * * -EINVAL if maxlen exceeds SSIZE_MAX
  1000. */
  1001. ssize_t efi_dp_check_length(const struct efi_device_path *dp,
  1002. const size_t maxlen)
  1003. {
  1004. ssize_t ret = 0;
  1005. u16 len;
  1006. if (maxlen > SSIZE_MAX)
  1007. return -EINVAL;
  1008. for (;;) {
  1009. len = dp->length;
  1010. if (len < 4)
  1011. return -1;
  1012. ret += len;
  1013. if (ret > maxlen)
  1014. return -1;
  1015. if (dp->type == DEVICE_PATH_TYPE_END &&
  1016. dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
  1017. return ret;
  1018. dp = (const struct efi_device_path *)((const u8 *)dp + len);
  1019. }
  1020. }