efi_device_path.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * EFI device path from u-boot device-model mapping
  3. *
  4. * (C) Copyright 2017 Rob Clark
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <blk.h>
  10. #include <dm.h>
  11. #include <usb.h>
  12. #include <mmc.h>
  13. #include <efi_loader.h>
  14. #include <inttypes.h>
  15. #include <part.h>
  16. /* template END node: */
  17. static const struct efi_device_path END = {
  18. .type = DEVICE_PATH_TYPE_END,
  19. .sub_type = DEVICE_PATH_SUB_TYPE_END,
  20. .length = sizeof(END),
  21. };
  22. #define U_BOOT_GUID \
  23. EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
  24. 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
  25. /* template ROOT node: */
  26. static const struct efi_device_path_vendor ROOT = {
  27. .dp = {
  28. .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
  29. .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
  30. .length = sizeof(ROOT),
  31. },
  32. .guid = U_BOOT_GUID,
  33. };
  34. static void *dp_alloc(size_t sz)
  35. {
  36. void *buf;
  37. if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) != EFI_SUCCESS)
  38. return NULL;
  39. return buf;
  40. }
  41. /*
  42. * Iterate to next block in device-path, terminating (returning NULL)
  43. * at /End* node.
  44. */
  45. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
  46. {
  47. if (dp == NULL)
  48. return NULL;
  49. if (dp->type == DEVICE_PATH_TYPE_END)
  50. return NULL;
  51. dp = ((void *)dp) + dp->length;
  52. if (dp->type == DEVICE_PATH_TYPE_END)
  53. return NULL;
  54. return (struct efi_device_path *)dp;
  55. }
  56. /*
  57. * Compare two device-paths, stopping when the shorter of the two hits
  58. * an End* node. This is useful to, for example, compare a device-path
  59. * representing a device with one representing a file on the device, or
  60. * a device with a parent device.
  61. */
  62. int efi_dp_match(struct efi_device_path *a, struct efi_device_path *b)
  63. {
  64. while (1) {
  65. int ret;
  66. ret = memcmp(&a->length, &b->length, sizeof(a->length));
  67. if (ret)
  68. return ret;
  69. ret = memcmp(a, b, a->length);
  70. if (ret)
  71. return ret;
  72. a = efi_dp_next(a);
  73. b = efi_dp_next(b);
  74. if (!a || !b)
  75. return 0;
  76. }
  77. }
  78. /*
  79. * See UEFI spec (section 3.1.2, about short-form device-paths..
  80. * tl;dr: we can have a device-path that starts with a USB WWID
  81. * or USB Class node, and a few other cases which don't encode
  82. * the full device path with bus hierarchy:
  83. *
  84. * - MESSAGING:USB_WWID
  85. * - MESSAGING:USB_CLASS
  86. * - MEDIA:FILE_PATH
  87. * - MEDIA:HARD_DRIVE
  88. * - MESSAGING:URI
  89. */
  90. static struct efi_device_path *shorten_path(struct efi_device_path *dp)
  91. {
  92. while (dp) {
  93. /*
  94. * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
  95. * in practice fallback.efi just uses MEDIA:HARD_DRIVE
  96. * so not sure when we would see these other cases.
  97. */
  98. if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
  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. static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
  107. struct efi_device_path **rem)
  108. {
  109. struct efi_object *efiobj;
  110. list_for_each_entry(efiobj, &efi_obj_list, link) {
  111. int i;
  112. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  113. struct efi_handler *handler = &efiobj->protocols[i];
  114. struct efi_device_path *obj_dp;
  115. if (!handler->guid)
  116. break;
  117. if (guidcmp(handler->guid, &efi_guid_device_path))
  118. continue;
  119. obj_dp = handler->protocol_interface;
  120. do {
  121. if (efi_dp_match(dp, obj_dp) == 0) {
  122. if (rem) {
  123. *rem = ((void *)dp) +
  124. efi_dp_size(obj_dp);
  125. }
  126. return efiobj;
  127. }
  128. obj_dp = shorten_path(efi_dp_next(obj_dp));
  129. } while (short_path && obj_dp);
  130. }
  131. }
  132. return NULL;
  133. }
  134. /*
  135. * Find an efiobj from device-path, if 'rem' is not NULL, returns the
  136. * remaining part of the device path after the matched object.
  137. */
  138. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  139. struct efi_device_path **rem)
  140. {
  141. struct efi_object *efiobj;
  142. efiobj = find_obj(dp, false, rem);
  143. if (!efiobj)
  144. efiobj = find_obj(dp, true, rem);
  145. return efiobj;
  146. }
  147. /* return size not including End node: */
  148. unsigned efi_dp_size(const struct efi_device_path *dp)
  149. {
  150. unsigned sz = 0;
  151. while (dp) {
  152. sz += dp->length;
  153. dp = efi_dp_next(dp);
  154. }
  155. return sz;
  156. }
  157. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
  158. {
  159. struct efi_device_path *ndp;
  160. unsigned sz = efi_dp_size(dp) + sizeof(END);
  161. if (!dp)
  162. return NULL;
  163. ndp = dp_alloc(sz);
  164. memcpy(ndp, dp, sz);
  165. return ndp;
  166. }
  167. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  168. const struct efi_device_path *dp2)
  169. {
  170. struct efi_device_path *ret;
  171. if (!dp1) {
  172. ret = efi_dp_dup(dp2);
  173. } else if (!dp2) {
  174. ret = efi_dp_dup(dp1);
  175. } else {
  176. /* both dp1 and dp2 are non-null */
  177. unsigned sz1 = efi_dp_size(dp1);
  178. unsigned sz2 = efi_dp_size(dp2);
  179. void *p = dp_alloc(sz1 + sz2 + sizeof(END));
  180. memcpy(p, dp1, sz1);
  181. memcpy(p + sz1, dp2, sz2);
  182. memcpy(p + sz1 + sz2, &END, sizeof(END));
  183. ret = p;
  184. }
  185. return ret;
  186. }
  187. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  188. const struct efi_device_path *node)
  189. {
  190. struct efi_device_path *ret;
  191. if (!node && !dp) {
  192. ret = efi_dp_dup(&END);
  193. } else if (!node) {
  194. ret = efi_dp_dup(dp);
  195. } else if (!dp) {
  196. unsigned sz = node->length;
  197. void *p = dp_alloc(sz + sizeof(END));
  198. memcpy(p, node, sz);
  199. memcpy(p + sz, &END, sizeof(END));
  200. ret = p;
  201. } else {
  202. /* both dp and node are non-null */
  203. unsigned sz = efi_dp_size(dp);
  204. void *p = dp_alloc(sz + node->length + sizeof(END));
  205. memcpy(p, dp, sz);
  206. memcpy(p + sz, node, node->length);
  207. memcpy(p + sz + node->length, &END, sizeof(END));
  208. ret = p;
  209. }
  210. return ret;
  211. }
  212. #ifdef CONFIG_DM
  213. /* size of device-path not including END node for device and all parents
  214. * up to the root device.
  215. */
  216. static unsigned dp_size(struct udevice *dev)
  217. {
  218. if (!dev || !dev->driver)
  219. return sizeof(ROOT);
  220. switch (dev->driver->id) {
  221. case UCLASS_ROOT:
  222. case UCLASS_SIMPLE_BUS:
  223. /* stop traversing parents at this point: */
  224. return sizeof(ROOT);
  225. case UCLASS_MMC:
  226. return dp_size(dev->parent) +
  227. sizeof(struct efi_device_path_sd_mmc_path);
  228. case UCLASS_MASS_STORAGE:
  229. case UCLASS_USB_HUB:
  230. return dp_size(dev->parent) +
  231. sizeof(struct efi_device_path_usb_class);
  232. default:
  233. /* just skip over unknown classes: */
  234. return dp_size(dev->parent);
  235. }
  236. }
  237. static void *dp_fill(void *buf, struct udevice *dev)
  238. {
  239. if (!dev || !dev->driver)
  240. return buf;
  241. switch (dev->driver->id) {
  242. case UCLASS_ROOT:
  243. case UCLASS_SIMPLE_BUS: {
  244. /* stop traversing parents at this point: */
  245. struct efi_device_path_vendor *vdp = buf;
  246. *vdp = ROOT;
  247. return &vdp[1];
  248. }
  249. #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
  250. case UCLASS_MMC: {
  251. struct efi_device_path_sd_mmc_path *sddp =
  252. dp_fill(buf, dev->parent);
  253. struct mmc *mmc = mmc_get_mmc_dev(dev);
  254. struct blk_desc *desc = mmc_get_blk_desc(mmc);
  255. sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  256. sddp->dp.sub_type = (desc->if_type == IF_TYPE_MMC) ?
  257. DEVICE_PATH_SUB_TYPE_MSG_MMC :
  258. DEVICE_PATH_SUB_TYPE_MSG_SD;
  259. sddp->dp.length = sizeof(*sddp);
  260. sddp->slot_number = dev->seq;
  261. return &sddp[1];
  262. }
  263. #endif
  264. case UCLASS_MASS_STORAGE:
  265. case UCLASS_USB_HUB: {
  266. struct efi_device_path_usb_class *udp =
  267. dp_fill(buf, dev->parent);
  268. struct usb_device *udev = dev_get_parent_priv(dev);
  269. struct usb_device_descriptor *desc = &udev->descriptor;
  270. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  271. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
  272. udp->dp.length = sizeof(*udp);
  273. udp->vendor_id = desc->idVendor;
  274. udp->product_id = desc->idProduct;
  275. udp->device_class = desc->bDeviceClass;
  276. udp->device_subclass = desc->bDeviceSubClass;
  277. udp->device_protocol = desc->bDeviceProtocol;
  278. return &udp[1];
  279. }
  280. default:
  281. debug("unhandled device class: %s (%u)\n",
  282. dev->name, dev->driver->id);
  283. return dp_fill(buf, dev->parent);
  284. }
  285. }
  286. /* Construct a device-path from a device: */
  287. struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
  288. {
  289. void *buf, *start;
  290. start = buf = dp_alloc(dp_size(dev) + sizeof(END));
  291. buf = dp_fill(buf, dev);
  292. *((struct efi_device_path *)buf) = END;
  293. return start;
  294. }
  295. #endif
  296. static unsigned dp_part_size(struct blk_desc *desc, int part)
  297. {
  298. unsigned dpsize;
  299. #ifdef CONFIG_BLK
  300. dpsize = dp_size(desc->bdev->parent);
  301. #else
  302. dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
  303. #endif
  304. if (part == 0) /* the actual disk, not a partition */
  305. return dpsize;
  306. if (desc->part_type == PART_TYPE_ISO)
  307. dpsize += sizeof(struct efi_device_path_cdrom_path);
  308. else
  309. dpsize += sizeof(struct efi_device_path_hard_drive_path);
  310. return dpsize;
  311. }
  312. static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
  313. {
  314. disk_partition_t info;
  315. #ifdef CONFIG_BLK
  316. buf = dp_fill(buf, desc->bdev->parent);
  317. #else
  318. /*
  319. * We *could* make a more accurate path, by looking at if_type
  320. * and handling all the different cases like we do for non-
  321. * legacy (ie CONFIG_BLK=y) case. But most important thing
  322. * is just to have a unique device-path for if_type+devnum.
  323. * So map things to a fictional USB device:
  324. */
  325. struct efi_device_path_usb *udp;
  326. memcpy(buf, &ROOT, sizeof(ROOT));
  327. buf += sizeof(ROOT);
  328. udp = buf;
  329. udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  330. udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
  331. udp->dp.length = sizeof(*udp);
  332. udp->parent_port_number = desc->if_type;
  333. udp->usb_interface = desc->devnum;
  334. buf = &udp[1];
  335. #endif
  336. if (part == 0) /* the actual disk, not a partition */
  337. return buf;
  338. part_get_info(desc, part, &info);
  339. if (desc->part_type == PART_TYPE_ISO) {
  340. struct efi_device_path_cdrom_path *cddp = buf;
  341. cddp->boot_entry = part - 1;
  342. cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  343. cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
  344. cddp->dp.length = sizeof(*cddp);
  345. cddp->partition_start = info.start;
  346. cddp->partition_end = info.size;
  347. buf = &cddp[1];
  348. } else {
  349. struct efi_device_path_hard_drive_path *hddp = buf;
  350. hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  351. hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
  352. hddp->dp.length = sizeof(*hddp);
  353. hddp->partition_number = part - 1;
  354. hddp->partition_start = info.start;
  355. hddp->partition_end = info.size;
  356. if (desc->part_type == PART_TYPE_EFI)
  357. hddp->partmap_type = 2;
  358. else
  359. hddp->partmap_type = 1;
  360. hddp->signature_type = desc->sig_type;
  361. if (hddp->signature_type != 0)
  362. memcpy(hddp->partition_signature, &desc->guid_sig,
  363. sizeof(hddp->partition_signature));
  364. buf = &hddp[1];
  365. }
  366. return buf;
  367. }
  368. /* Construct a device-path from a partition on a blk device: */
  369. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
  370. {
  371. void *buf, *start;
  372. start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
  373. buf = dp_part_fill(buf, desc, part);
  374. *((struct efi_device_path *)buf) = END;
  375. return start;
  376. }
  377. /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
  378. static void path_to_uefi(u16 *uefi, const char *path)
  379. {
  380. while (*path) {
  381. char c = *(path++);
  382. if (c == '/')
  383. c = '\\';
  384. *(uefi++) = c;
  385. }
  386. *uefi = '\0';
  387. }
  388. /*
  389. * If desc is NULL, this creates a path with only the file component,
  390. * otherwise it creates a full path with both device and file components
  391. */
  392. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  393. const char *path)
  394. {
  395. struct efi_device_path_file_path *fp;
  396. void *buf, *start;
  397. unsigned dpsize = 0, fpsize;
  398. if (desc)
  399. dpsize = dp_part_size(desc, part);
  400. fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
  401. dpsize += fpsize;
  402. start = buf = dp_alloc(dpsize + sizeof(END));
  403. if (desc)
  404. buf = dp_part_fill(buf, desc, part);
  405. /* add file-path: */
  406. fp = buf;
  407. fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
  408. fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
  409. fp->dp.length = fpsize;
  410. path_to_uefi(fp->str, path);
  411. buf += fpsize;
  412. *((struct efi_device_path *)buf) = END;
  413. return start;
  414. }
  415. #ifdef CONFIG_NET
  416. struct efi_device_path *efi_dp_from_eth(void)
  417. {
  418. struct efi_device_path_mac_addr *ndp;
  419. void *buf, *start;
  420. unsigned dpsize = 0;
  421. assert(eth_get_dev());
  422. #ifdef CONFIG_DM_ETH
  423. dpsize += dp_size(eth_get_dev());
  424. #else
  425. dpsize += sizeof(ROOT);
  426. #endif
  427. dpsize += sizeof(*ndp);
  428. start = buf = dp_alloc(dpsize + sizeof(END));
  429. #ifdef CONFIG_DM_ETH
  430. buf = dp_fill(buf, eth_get_dev());
  431. #else
  432. memcpy(buf, &ROOT, sizeof(ROOT));
  433. buf += sizeof(ROOT);
  434. #endif
  435. ndp = buf;
  436. ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  437. ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
  438. ndp->dp.length = sizeof(*ndp);
  439. memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
  440. buf = &ndp[1];
  441. *((struct efi_device_path *)buf) = END;
  442. return start;
  443. }
  444. #endif
  445. /* Construct a device-path for memory-mapped image */
  446. struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  447. uint64_t start_address,
  448. uint64_t end_address)
  449. {
  450. struct efi_device_path_memory *mdp;
  451. void *buf, *start;
  452. start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
  453. mdp = buf;
  454. mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
  455. mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
  456. mdp->dp.length = sizeof(*mdp);
  457. mdp->memory_type = memory_type;
  458. mdp->start_address = start_address;
  459. mdp->end_address = end_address;
  460. buf = &mdp[1];
  461. *((struct efi_device_path *)buf) = END;
  462. return start;
  463. }
  464. /*
  465. * Helper to split a full device path (containing both device and file
  466. * parts) into it's constituent parts.
  467. */
  468. void efi_dp_split_file_path(struct efi_device_path *full_path,
  469. struct efi_device_path **device_path,
  470. struct efi_device_path **file_path)
  471. {
  472. struct efi_device_path *p, *dp, *fp;
  473. dp = efi_dp_dup(full_path);
  474. p = dp;
  475. while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
  476. p = efi_dp_next(p);
  477. fp = efi_dp_dup(p);
  478. p->type = DEVICE_PATH_TYPE_END;
  479. p->sub_type = DEVICE_PATH_SUB_TYPE_END;
  480. p->length = sizeof(*p);
  481. *device_path = dp;
  482. *file_path = fp;
  483. }