efi_device_path.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 (dev->driver->id) {
  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 (dev->driver->id) {
  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. debug("%s(%u) %s: unhandled device class: %s (%u)\n",
  685. __FILE__, __LINE__, __func__,
  686. dev->name, dev->driver->id);
  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;
  694. int ret;
  695. ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  696. if (ret)
  697. dev = desc->bdev->parent;
  698. dpsize = dp_size(dev);
  699. if (part == 0) /* the actual disk, not a partition */
  700. return dpsize;
  701. if (desc->part_type == PART_TYPE_ISO)
  702. dpsize += sizeof(struct efi_device_path_cdrom_path);
  703. else
  704. dpsize += sizeof(struct efi_device_path_hard_drive_path);
  705. return dpsize;
  706. }
  707. /*
  708. * Create a device node for a block device partition.
  709. *
  710. * @buf buffer to which the device path is written
  711. * @desc block device descriptor
  712. * @part partition number, 0 identifies a block device
  713. */
  714. static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
  715. {
  716. struct disk_partition info;
  717. part_get_info(desc, part, &info);
  718. if (desc->part_type == PART_TYPE_ISO) {
  719. struct efi_device_path_cdrom_path *cddp = buf;
  720. cddp->boot_entry = part;
  721. cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  722. cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
  723. cddp->dp.length = sizeof(*cddp);
  724. cddp->partition_start = info.start;
  725. cddp->partition_size = info.size;
  726. buf = &cddp[1];
  727. } else {
  728. struct efi_device_path_hard_drive_path *hddp = buf;
  729. hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  730. hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
  731. hddp->dp.length = sizeof(*hddp);
  732. hddp->partition_number = part;
  733. hddp->partition_start = info.start;
  734. hddp->partition_end = info.size;
  735. if (desc->part_type == PART_TYPE_EFI)
  736. hddp->partmap_type = 2;
  737. else
  738. hddp->partmap_type = 1;
  739. switch (desc->sig_type) {
  740. case SIG_TYPE_NONE:
  741. default:
  742. hddp->signature_type = 0;
  743. memset(hddp->partition_signature, 0,
  744. sizeof(hddp->partition_signature));
  745. break;
  746. case SIG_TYPE_MBR:
  747. hddp->signature_type = 1;
  748. memset(hddp->partition_signature, 0,
  749. sizeof(hddp->partition_signature));
  750. memcpy(hddp->partition_signature, &desc->mbr_sig,
  751. sizeof(desc->mbr_sig));
  752. break;
  753. case SIG_TYPE_GUID:
  754. hddp->signature_type = 2;
  755. if (uuid_str_to_bin(info.uuid,
  756. hddp->partition_signature, 1))
  757. log_warning(
  758. "Partition no. %d: invalid guid: %s\n",
  759. part, info.uuid);
  760. break;
  761. }
  762. buf = &hddp[1];
  763. }
  764. return buf;
  765. }
  766. /*
  767. * Create a device path for a block device or one of its partitions.
  768. *
  769. * @buf buffer to which the device path is written
  770. * @desc block device descriptor
  771. * @part partition number, 0 identifies a block device
  772. */
  773. static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
  774. {
  775. struct udevice *dev;
  776. int ret;
  777. ret = blk_find_device(desc->if_type, desc->devnum, &dev);
  778. if (ret)
  779. dev = desc->bdev->parent;
  780. buf = dp_fill(buf, dev);
  781. if (part == 0) /* the actual disk, not a partition */
  782. return buf;
  783. return dp_part_node(buf, desc, part);
  784. }
  785. /* Construct a device-path from a partition on a block device: */
  786. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
  787. {
  788. void *buf, *start;
  789. start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
  790. if (!buf)
  791. return NULL;
  792. buf = dp_part_fill(buf, desc, part);
  793. *((struct efi_device_path *)buf) = END;
  794. return start;
  795. }
  796. /*
  797. * Create a device node for a block device partition.
  798. *
  799. * @buf buffer to which the device path is written
  800. * @desc block device descriptor
  801. * @part partition number, 0 identifies a block device
  802. */
  803. struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
  804. {
  805. efi_uintn_t dpsize;
  806. void *buf;
  807. if (desc->part_type == PART_TYPE_ISO)
  808. dpsize = sizeof(struct efi_device_path_cdrom_path);
  809. else
  810. dpsize = sizeof(struct efi_device_path_hard_drive_path);
  811. buf = dp_alloc(dpsize);
  812. dp_part_node(buf, desc, part);
  813. return buf;
  814. }
  815. /**
  816. * path_to_uefi() - convert UTF-8 path to an UEFI style path
  817. *
  818. * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
  819. * separators and UTF-16).
  820. *
  821. * @src: source buffer
  822. * @uefi: target buffer, possibly unaligned
  823. */
  824. static void path_to_uefi(void *uefi, const char *src)
  825. {
  826. u16 *pos = uefi;
  827. /*
  828. * efi_set_bootdev() calls this routine indirectly before the UEFI
  829. * subsystem is initialized. So we cannot assume unaligned access to be
  830. * enabled.
  831. */
  832. allow_unaligned();
  833. while (*src) {
  834. s32 code = utf8_get(&src);
  835. if (code < 0)
  836. code = '?';
  837. else if (code == '/')
  838. code = '\\';
  839. utf16_put(code, &pos);
  840. }
  841. *pos = 0;
  842. }
  843. /*
  844. * If desc is NULL, this creates a path with only the file component,
  845. * otherwise it creates a full path with both device and file components
  846. */
  847. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  848. const char *path)
  849. {
  850. struct efi_device_path_file_path *fp;
  851. void *buf, *start;
  852. size_t dpsize = 0, fpsize;
  853. if (desc)
  854. dpsize = dp_part_size(desc, part);
  855. fpsize = sizeof(struct efi_device_path) +
  856. 2 * (utf8_utf16_strlen(path) + 1);
  857. if (fpsize > U16_MAX)
  858. return NULL;
  859. dpsize += fpsize;
  860. start = buf = dp_alloc(dpsize + sizeof(END));
  861. if (!buf)
  862. return NULL;
  863. if (desc)
  864. buf = dp_part_fill(buf, desc, part);
  865. /* add file-path: */
  866. fp = buf;
  867. fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  868. fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  869. fp->dp.length = (u16)fpsize;
  870. path_to_uefi(fp->str, path);
  871. buf += fpsize;
  872. *((struct efi_device_path *)buf) = END;
  873. return start;
  874. }
  875. struct efi_device_path *efi_dp_from_uart(void)
  876. {
  877. void *buf, *pos;
  878. struct efi_device_path_uart *uart;
  879. size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
  880. buf = dp_alloc(dpsize);
  881. if (!buf)
  882. return NULL;
  883. pos = buf;
  884. memcpy(pos, &ROOT, sizeof(ROOT));
  885. pos += sizeof(ROOT);
  886. uart = pos;
  887. uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  888. uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
  889. uart->dp.length = sizeof(*uart);
  890. pos += sizeof(*uart);
  891. memcpy(pos, &END, sizeof(END));
  892. return buf;
  893. }
  894. #ifdef CONFIG_NET
  895. struct efi_device_path *efi_dp_from_eth(void)
  896. {
  897. #ifndef CONFIG_DM_ETH
  898. struct efi_device_path_mac_addr *ndp;
  899. #endif
  900. void *buf, *start;
  901. unsigned dpsize = 0;
  902. assert(eth_get_dev());
  903. #ifdef CONFIG_DM_ETH
  904. dpsize += dp_size(eth_get_dev());
  905. #else
  906. dpsize += sizeof(ROOT);
  907. dpsize += sizeof(*ndp);
  908. #endif
  909. start = buf = dp_alloc(dpsize + sizeof(END));
  910. if (!buf)
  911. return NULL;
  912. #ifdef CONFIG_DM_ETH
  913. buf = dp_fill(buf, eth_get_dev());
  914. #else
  915. memcpy(buf, &ROOT, sizeof(ROOT));
  916. buf += sizeof(ROOT);
  917. ndp = buf;
  918. ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  919. ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  920. ndp->dp.length = sizeof(*ndp);
  921. ndp->if_type = 1; /* Ethernet */
  922. memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
  923. buf = &ndp[1];
  924. #endif
  925. *((struct efi_device_path *)buf) = END;
  926. return start;
  927. }
  928. #endif
  929. /* Construct a device-path for memory-mapped image */
  930. struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  931. uint64_t start_address,
  932. uint64_t end_address)
  933. {
  934. struct efi_device_path_memory *mdp;
  935. void *buf, *start;
  936. start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
  937. if (!buf)
  938. return NULL;
  939. mdp = buf;
  940. mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  941. mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
  942. mdp->dp.length = sizeof(*mdp);
  943. mdp->memory_type = memory_type;
  944. mdp->start_address = start_address;
  945. mdp->end_address = end_address;
  946. buf = &mdp[1];
  947. *((struct efi_device_path *)buf) = END;
  948. return start;
  949. }
  950. /**
  951. * efi_dp_split_file_path() - split of relative file path from device path
  952. *
  953. * Given a device path indicating a file on a device, separate the device
  954. * path in two: the device path of the actual device and the file path
  955. * relative to this device.
  956. *
  957. * @full_path: device path including device and file path
  958. * @device_path: path of the device
  959. * @file_path: relative path of the file or NULL if there is none
  960. * Return: status code
  961. */
  962. efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
  963. struct efi_device_path **device_path,
  964. struct efi_device_path **file_path)
  965. {
  966. struct efi_device_path *p, *dp, *fp = NULL;
  967. *device_path = NULL;
  968. *file_path = NULL;
  969. dp = efi_dp_dup(full_path);
  970. if (!dp)
  971. return EFI_OUT_OF_RESOURCES;
  972. p = dp;
  973. while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
  974. p = efi_dp_next(p);
  975. if (!p)
  976. goto out;
  977. }
  978. fp = efi_dp_dup(p);
  979. if (!fp)
  980. return EFI_OUT_OF_RESOURCES;
  981. p->type = DEVICE_PATH_TYPE_END;
  982. p->sub_type = DEVICE_PATH_SUB_TYPE_END;
  983. p->length = sizeof(*p);
  984. out:
  985. *device_path = dp;
  986. *file_path = fp;
  987. return EFI_SUCCESS;
  988. }
  989. /**
  990. * efi_dp_from_name() - convert U-Boot device and file path to device path
  991. *
  992. * @dev: U-Boot device, e.g. 'mmc'
  993. * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
  994. * @path: file path relative to U-Boot device, may be NULL
  995. * @device: pointer to receive device path of the device
  996. * @file: pointer to receive device path for the file
  997. * Return: status code
  998. */
  999. efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
  1000. const char *path,
  1001. struct efi_device_path **device,
  1002. struct efi_device_path **file)
  1003. {
  1004. struct blk_desc *desc = NULL;
  1005. struct disk_partition fs_partition;
  1006. int part = 0;
  1007. char *filename;
  1008. char *s;
  1009. if (path && !file)
  1010. return EFI_INVALID_PARAMETER;
  1011. if (!strcmp(dev, "Net")) {
  1012. #ifdef CONFIG_NET
  1013. if (device)
  1014. *device = efi_dp_from_eth();
  1015. #endif
  1016. } else if (!strcmp(dev, "Uart")) {
  1017. if (device)
  1018. *device = efi_dp_from_uart();
  1019. } else {
  1020. part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
  1021. 1);
  1022. if (part < 0 || !desc)
  1023. return EFI_INVALID_PARAMETER;
  1024. if (device)
  1025. *device = efi_dp_from_part(desc, part);
  1026. }
  1027. if (!path)
  1028. return EFI_SUCCESS;
  1029. filename = calloc(1, strlen(path) + 1);
  1030. if (!filename)
  1031. return EFI_OUT_OF_RESOURCES;
  1032. sprintf(filename, "%s", path);
  1033. /* DOS style file path: */
  1034. s = filename;
  1035. while ((s = strchr(s, '/')))
  1036. *s++ = '\\';
  1037. *file = efi_dp_from_file(desc, part, filename);
  1038. free(filename);
  1039. if (!*file)
  1040. return EFI_INVALID_PARAMETER;
  1041. return EFI_SUCCESS;
  1042. }
  1043. /**
  1044. * efi_dp_check_length() - check length of a device path
  1045. *
  1046. * @dp: pointer to device path
  1047. * @maxlen: maximum length of the device path
  1048. * Return:
  1049. * * length of the device path if it is less or equal @maxlen
  1050. * * -1 if the device path is longer then @maxlen
  1051. * * -1 if a device path node has a length of less than 4
  1052. * * -EINVAL if maxlen exceeds SSIZE_MAX
  1053. */
  1054. ssize_t efi_dp_check_length(const struct efi_device_path *dp,
  1055. const size_t maxlen)
  1056. {
  1057. ssize_t ret = 0;
  1058. u16 len;
  1059. if (maxlen > SSIZE_MAX)
  1060. return -EINVAL;
  1061. for (;;) {
  1062. len = dp->length;
  1063. if (len < 4)
  1064. return -1;
  1065. ret += len;
  1066. if (ret > maxlen)
  1067. return -1;
  1068. if (dp->type == DEVICE_PATH_TYPE_END &&
  1069. dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
  1070. return ret;
  1071. dp = (const struct efi_device_path *)((const u8 *)dp + len);
  1072. }
  1073. }
  1074. /**
  1075. * efi_dp_from_lo() - Get the instance of a VenMedia node in a
  1076. * multi-instance device path that matches
  1077. * a specific GUID. This kind of device paths
  1078. * is found in Boot#### options describing an
  1079. * initrd location
  1080. *
  1081. * @lo: EFI_LOAD_OPTION containing a valid device path
  1082. * @guid: guid to search for
  1083. *
  1084. * Return:
  1085. * device path including the VenMedia node or NULL.
  1086. * Caller must free the returned value.
  1087. */
  1088. struct
  1089. efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
  1090. const efi_guid_t *guid)
  1091. {
  1092. struct efi_device_path *fp = lo->file_path;
  1093. struct efi_device_path_vendor *vendor;
  1094. int lo_len = lo->file_path_length;
  1095. for (; lo_len >= sizeof(struct efi_device_path);
  1096. lo_len -= fp->length, fp = (void *)fp + fp->length) {
  1097. if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
  1098. break;
  1099. if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
  1100. fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
  1101. continue;
  1102. vendor = (struct efi_device_path_vendor *)fp;
  1103. if (!guidcmp(&vendor->guid, guid))
  1104. return efi_dp_dup(efi_dp_next(fp));
  1105. }
  1106. log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
  1107. return NULL;
  1108. }
  1109. /**
  1110. * search_gpt_dp_node() - search gpt device path node
  1111. *
  1112. * @device_path: device path
  1113. *
  1114. * Return: pointer to the gpt device path node
  1115. */
  1116. struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
  1117. {
  1118. struct efi_device_path *dp = device_path;
  1119. while (dp) {
  1120. if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
  1121. dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
  1122. struct efi_device_path_hard_drive_path *hd_dp =
  1123. (struct efi_device_path_hard_drive_path *)dp;
  1124. if (hd_dp->partmap_type == PART_FORMAT_GPT &&
  1125. hd_dp->signature_type == SIG_TYPE_GUID)
  1126. return dp;
  1127. }
  1128. dp = efi_dp_next(dp);
  1129. }
  1130. return NULL;
  1131. }