efi_device_path.c 30 KB

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