efi_device_path.c 31 KB

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