bootdev-uclass.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2021 Google LLC
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #define LOG_CATEGORY UCLASS_BOOTSTD
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <bootdev.h>
  10. #include <bootflow.h>
  11. #include <bootmeth.h>
  12. #include <bootstd.h>
  13. #include <fs.h>
  14. #include <log.h>
  15. #include <malloc.h>
  16. #include <part.h>
  17. #include <sort.h>
  18. #include <dm/device-internal.h>
  19. #include <dm/lists.h>
  20. #include <dm/uclass-internal.h>
  21. enum {
  22. /*
  23. * Set some sort of limit on the number of partitions a bootdev can
  24. * have. Note that for disks this limits the partitions numbers that
  25. * are scanned to 1..MAX_BOOTFLOWS_PER_BOOTDEV
  26. */
  27. MAX_PART_PER_BOOTDEV = 30,
  28. /* Maximum supported length of the "boot_targets" env string */
  29. BOOT_TARGETS_MAX_LEN = 100,
  30. };
  31. int bootdev_add_bootflow(struct bootflow *bflow)
  32. {
  33. struct bootstd_priv *std;
  34. struct bootflow *new;
  35. int ret;
  36. assert(bflow->dev);
  37. ret = bootstd_get_priv(&std);
  38. if (ret)
  39. return ret;
  40. new = malloc(sizeof(*bflow));
  41. if (!new)
  42. return log_msg_ret("bflow", -ENOMEM);
  43. memcpy(new, bflow, sizeof(*bflow));
  44. list_add_tail(&new->glob_node, &std->glob_head);
  45. if (bflow->dev) {
  46. struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
  47. list_add_tail(&new->bm_node, &ucp->bootflow_head);
  48. }
  49. return 0;
  50. }
  51. int bootdev_first_bootflow(struct udevice *dev, struct bootflow **bflowp)
  52. {
  53. struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
  54. if (list_empty(&ucp->bootflow_head))
  55. return -ENOENT;
  56. *bflowp = list_first_entry(&ucp->bootflow_head, struct bootflow,
  57. bm_node);
  58. return 0;
  59. }
  60. int bootdev_next_bootflow(struct bootflow **bflowp)
  61. {
  62. struct bootflow *bflow = *bflowp;
  63. struct bootdev_uc_plat *ucp = dev_get_uclass_plat(bflow->dev);
  64. *bflowp = NULL;
  65. if (list_is_last(&bflow->bm_node, &ucp->bootflow_head))
  66. return -ENOENT;
  67. *bflowp = list_entry(bflow->bm_node.next, struct bootflow, bm_node);
  68. return 0;
  69. }
  70. int bootdev_bind(struct udevice *parent, const char *drv_name, const char *name,
  71. struct udevice **devp)
  72. {
  73. struct udevice *dev;
  74. char dev_name[30];
  75. char *str;
  76. int ret;
  77. snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
  78. str = strdup(dev_name);
  79. if (!str)
  80. return -ENOMEM;
  81. ret = device_bind_driver(parent, drv_name, str, &dev);
  82. if (ret)
  83. return ret;
  84. device_set_name_alloced(dev);
  85. *devp = dev;
  86. return 0;
  87. }
  88. int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
  89. struct bootflow_iter *iter, struct bootflow *bflow)
  90. {
  91. struct blk_desc *desc = dev_get_uclass_plat(blk);
  92. struct disk_partition info;
  93. char partstr[20];
  94. char name[60];
  95. int ret;
  96. /* Sanity check */
  97. if (iter->part >= MAX_PART_PER_BOOTDEV)
  98. return log_msg_ret("max", -ESHUTDOWN);
  99. bflow->blk = blk;
  100. if (iter->part)
  101. snprintf(partstr, sizeof(partstr), "part_%x", iter->part);
  102. else
  103. strcpy(partstr, "whole");
  104. snprintf(name, sizeof(name), "%s.%s", dev->name, partstr);
  105. bflow->name = strdup(name);
  106. if (!bflow->name)
  107. return log_msg_ret("name", -ENOMEM);
  108. bflow->part = iter->part;
  109. ret = bootmeth_check(bflow->method, iter);
  110. if (ret)
  111. return log_msg_ret("check", ret);
  112. /*
  113. * partition numbers start at 0 so this cannot succeed, but it can tell
  114. * us whether there is valid media there
  115. */
  116. ret = part_get_info(desc, iter->part, &info);
  117. if (!iter->part && ret == -ENOENT)
  118. ret = 0;
  119. /*
  120. * This error indicates the media is not present. Otherwise we just
  121. * blindly scan the next partition. We could be more intelligent here
  122. * and check which partition numbers actually exist.
  123. */
  124. if (ret == -EOPNOTSUPP)
  125. ret = -ESHUTDOWN;
  126. else
  127. bflow->state = BOOTFLOWST_MEDIA;
  128. if (ret) {
  129. /* allow partition 1 to be missing */
  130. if (iter->part == 1) {
  131. iter->max_part = 3;
  132. ret = -ENOENT;
  133. }
  134. return log_msg_ret("part", ret);
  135. }
  136. /*
  137. * Currently we don't get the number of partitions, so just
  138. * assume a large number
  139. */
  140. iter->max_part = MAX_PART_PER_BOOTDEV;
  141. /* If this is the whole disk, check if we have bootable partitions */
  142. if (!iter->part) {
  143. iter->first_bootable = part_get_bootable(desc);
  144. log_debug("checking bootable=%d\n", iter->first_bootable);
  145. /* if there are bootable partitions, scan only those */
  146. } else if (iter->first_bootable ? !info.bootable : iter->part != 1) {
  147. return log_msg_ret("boot", -EINVAL);
  148. } else {
  149. ret = fs_set_blk_dev_with_part(desc, bflow->part);
  150. bflow->state = BOOTFLOWST_PART;
  151. if (ret)
  152. return log_msg_ret("fs", ret);
  153. /* Use an #ifdef due to info.sys_ind */
  154. #ifdef CONFIG_DOS_PARTITION
  155. log_debug("%s: Found partition %x type %x fstype %d\n",
  156. blk->name, bflow->part, info.sys_ind,
  157. ret ? -1 : fs_get_type());
  158. #endif
  159. bflow->blk = blk;
  160. bflow->state = BOOTFLOWST_FS;
  161. }
  162. ret = bootmeth_read_bootflow(bflow->method, bflow);
  163. if (ret)
  164. return log_msg_ret("method", ret);
  165. return 0;
  166. }
  167. void bootdev_list(bool probe)
  168. {
  169. struct udevice *dev;
  170. int ret;
  171. int i;
  172. printf("Seq Probed Status Uclass Name\n");
  173. printf("--- ------ ------ -------- ------------------\n");
  174. if (probe)
  175. ret = uclass_first_device_check(UCLASS_BOOTDEV, &dev);
  176. else
  177. ret = uclass_find_first_device(UCLASS_BOOTDEV, &dev);
  178. for (i = 0; dev; i++) {
  179. printf("%3x [ %c ] %6s %-9.9s %s\n", dev_seq(dev),
  180. device_active(dev) ? '+' : ' ',
  181. ret ? simple_itoa(-ret) : "OK",
  182. dev_get_uclass_name(dev_get_parent(dev)), dev->name);
  183. if (probe)
  184. ret = uclass_next_device_check(&dev);
  185. else
  186. ret = uclass_find_next_device(&dev);
  187. }
  188. printf("--- ------ ------ -------- ------------------\n");
  189. printf("(%d bootdev%s)\n", i, i != 1 ? "s" : "");
  190. }
  191. int bootdev_setup_for_dev(struct udevice *parent, const char *drv_name)
  192. {
  193. struct udevice *bdev;
  194. int ret;
  195. ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV,
  196. &bdev);
  197. if (ret) {
  198. if (ret != -ENODEV) {
  199. log_debug("Cannot access bootdev device\n");
  200. return ret;
  201. }
  202. ret = bootdev_bind(parent, drv_name, "bootdev", &bdev);
  203. if (ret) {
  204. log_debug("Cannot create bootdev device\n");
  205. return ret;
  206. }
  207. }
  208. return 0;
  209. }
  210. static int bootdev_get_suffix_start(struct udevice *dev, const char *suffix)
  211. {
  212. int len, slen;
  213. len = strlen(dev->name);
  214. slen = strlen(suffix);
  215. if (len > slen && !strcmp(suffix, dev->name + len - slen))
  216. return len - slen;
  217. return len;
  218. }
  219. int bootdev_setup_for_sibling_blk(struct udevice *blk, const char *drv_name)
  220. {
  221. struct udevice *parent, *dev;
  222. char dev_name[50];
  223. int ret, len;
  224. len = bootdev_get_suffix_start(blk, ".blk");
  225. snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
  226. "bootdev");
  227. parent = dev_get_parent(blk);
  228. ret = device_find_child_by_name(parent, dev_name, &dev);
  229. if (ret) {
  230. char *str;
  231. if (ret != -ENODEV) {
  232. log_debug("Cannot access bootdev device\n");
  233. return ret;
  234. }
  235. str = strdup(dev_name);
  236. if (!str)
  237. return -ENOMEM;
  238. ret = device_bind_driver(parent, drv_name, str, &dev);
  239. if (ret) {
  240. log_debug("Cannot create bootdev device\n");
  241. return ret;
  242. }
  243. device_set_name_alloced(dev);
  244. }
  245. return 0;
  246. }
  247. int bootdev_get_sibling_blk(struct udevice *dev, struct udevice **blkp)
  248. {
  249. struct udevice *parent = dev_get_parent(dev);
  250. struct udevice *blk;
  251. int ret, len;
  252. if (device_get_uclass_id(dev) != UCLASS_BOOTDEV)
  253. return -EINVAL;
  254. /*
  255. * This should always work if bootdev_setup_for_sibling_blk() was used
  256. */
  257. len = bootdev_get_suffix_start(dev, ".bootdev");
  258. ret = device_find_child_by_namelen(parent, dev->name, len, &blk);
  259. if (ret) {
  260. char dev_name[50];
  261. snprintf(dev_name, sizeof(dev_name), "%.*s.blk", len,
  262. dev->name);
  263. ret = device_find_child_by_name(parent, dev_name, &blk);
  264. if (ret)
  265. return log_msg_ret("find", ret);
  266. }
  267. ret = device_probe(blk);
  268. if (ret)
  269. return log_msg_ret("act", ret);
  270. *blkp = blk;
  271. return 0;
  272. }
  273. static int bootdev_get_from_blk(struct udevice *blk, struct udevice **bootdevp)
  274. {
  275. struct udevice *parent = dev_get_parent(blk);
  276. struct udevice *bootdev;
  277. char dev_name[50];
  278. int ret, len;
  279. if (device_get_uclass_id(blk) != UCLASS_BLK)
  280. return -EINVAL;
  281. /* This should always work if bootdev_setup_for_sibling_blk() was used */
  282. len = bootdev_get_suffix_start(blk, ".blk");
  283. snprintf(dev_name, sizeof(dev_name), "%.*s.%s", len, blk->name,
  284. "bootdev");
  285. ret = device_find_child_by_name(parent, dev_name, &bootdev);
  286. if (ret)
  287. return log_msg_ret("find", ret);
  288. *bootdevp = bootdev;
  289. return 0;
  290. }
  291. int bootdev_unbind_dev(struct udevice *parent)
  292. {
  293. struct udevice *dev;
  294. int ret;
  295. ret = device_find_first_child_by_uclass(parent, UCLASS_BOOTDEV, &dev);
  296. if (!ret) {
  297. ret = device_remove(dev, DM_REMOVE_NORMAL);
  298. if (ret)
  299. return log_msg_ret("rem", ret);
  300. ret = device_unbind(dev);
  301. if (ret)
  302. return log_msg_ret("unb", ret);
  303. }
  304. return 0;
  305. }
  306. /**
  307. * label_to_uclass() - Convert a label to a uclass and sequence number
  308. *
  309. * @label: Label to look up (e.g. "mmc1" or "mmc0")
  310. * @seqp: Returns the sequence number, or -1 if none
  311. * @method_flagsp: If non-NULL, returns any flags implied by the label
  312. * (enum bootflow_meth_flags_t), 0 if none
  313. * Returns: sequence number on success, -EPFNOSUPPORT is the uclass is not
  314. * known, other -ve error code on other error
  315. */
  316. static int label_to_uclass(const char *label, int *seqp, int *method_flagsp)
  317. {
  318. int seq, len, method_flags;
  319. enum uclass_id id;
  320. const char *end;
  321. method_flags = 0;
  322. seq = trailing_strtoln_end(label, NULL, &end);
  323. len = end - label;
  324. if (!len)
  325. return -EINVAL;
  326. id = uclass_get_by_namelen(label, len);
  327. log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
  328. uclass_get_name(id));
  329. if (id == UCLASS_INVALID) {
  330. /* try some special cases */
  331. if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
  332. !strncmp("spi", label, len)) {
  333. id = UCLASS_SPI_FLASH;
  334. } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
  335. !strncmp("pxe", label, len)) {
  336. id = UCLASS_ETH;
  337. method_flags |= BOOTFLOW_METHF_PXE_ONLY;
  338. } else if (IS_ENABLED(CONFIG_BOOTDEV_ETH) &&
  339. !strncmp("dhcp", label, len)) {
  340. id = UCLASS_ETH;
  341. method_flags |= BOOTFLOW_METHF_DHCP_ONLY;
  342. } else {
  343. return -EPFNOSUPPORT;
  344. }
  345. }
  346. if (id == UCLASS_USB)
  347. id = UCLASS_MASS_STORAGE;
  348. *seqp = seq;
  349. if (method_flagsp)
  350. *method_flagsp = method_flags;
  351. return id;
  352. }
  353. int bootdev_find_by_label(const char *label, struct udevice **devp,
  354. int *method_flagsp)
  355. {
  356. int seq, ret, method_flags = 0;
  357. struct udevice *media;
  358. struct uclass *uc;
  359. enum uclass_id id;
  360. ret = label_to_uclass(label, &seq, &method_flags);
  361. if (ret < 0)
  362. return log_msg_ret("uc", ret);
  363. id = ret;
  364. /* Iterate through devices in the media uclass (e.g. UCLASS_MMC) */
  365. uclass_id_foreach_dev(id, media, uc) {
  366. struct udevice *bdev, *blk;
  367. int ret;
  368. /* if there is no seq, match anything */
  369. if (seq != -1 && dev_seq(media) != seq) {
  370. log_debug("- skip, media seq=%d\n", dev_seq(media));
  371. continue;
  372. }
  373. ret = device_find_first_child_by_uclass(media, UCLASS_BOOTDEV,
  374. &bdev);
  375. if (ret) {
  376. log_debug("- looking via blk, seq=%d, id=%d\n", seq,
  377. id);
  378. ret = blk_find_device(id, seq, &blk);
  379. if (!ret) {
  380. log_debug("- get from blk %s\n", blk->name);
  381. ret = bootdev_get_from_blk(blk, &bdev);
  382. }
  383. }
  384. if (!ret) {
  385. log_debug("- found %s\n", bdev->name);
  386. *devp = bdev;
  387. /*
  388. * if no sequence number was provided, we must scan all
  389. * bootdevs for this media uclass
  390. */
  391. if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && seq == -1)
  392. method_flags |= BOOTFLOW_METHF_SINGLE_UCLASS;
  393. if (method_flagsp)
  394. *method_flagsp = method_flags;
  395. return 0;
  396. }
  397. log_debug("- no device in %s\n", media->name);
  398. }
  399. return -ENOENT;
  400. }
  401. int bootdev_find_by_any(const char *name, struct udevice **devp,
  402. int *method_flagsp)
  403. {
  404. struct udevice *dev;
  405. int method_flags = 0;
  406. int ret = -ENODEV, seq;
  407. char *endp;
  408. seq = simple_strtol(name, &endp, 16);
  409. /* Select by name, label or number */
  410. if (*endp) {
  411. ret = uclass_get_device_by_name(UCLASS_BOOTDEV, name, &dev);
  412. if (ret == -ENODEV) {
  413. ret = bootdev_find_by_label(name, &dev, &method_flags);
  414. if (ret) {
  415. printf("Cannot find bootdev '%s' (err=%d)\n",
  416. name, ret);
  417. return log_msg_ret("lab", ret);
  418. }
  419. ret = device_probe(dev);
  420. }
  421. if (ret) {
  422. printf("Cannot probe bootdev '%s' (err=%d)\n", name,
  423. ret);
  424. return log_msg_ret("pro", ret);
  425. }
  426. } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
  427. ret = uclass_get_device_by_seq(UCLASS_BOOTDEV, seq, &dev);
  428. method_flags |= BOOTFLOW_METHF_SINGLE_DEV;
  429. }
  430. if (ret) {
  431. printf("Cannot find '%s' (err=%d)\n", name, ret);
  432. return ret;
  433. }
  434. *devp = dev;
  435. if (method_flagsp)
  436. *method_flagsp = method_flags;
  437. return 0;
  438. }
  439. int bootdev_hunt_and_find_by_label(const char *label, struct udevice **devp,
  440. int *method_flagsp)
  441. {
  442. int ret;
  443. ret = bootdev_hunt(label, false);
  444. if (ret)
  445. return log_msg_ret("scn", ret);
  446. ret = bootdev_find_by_label(label, devp, method_flagsp);
  447. if (ret)
  448. return log_msg_ret("fnd", ret);
  449. return 0;
  450. }
  451. static int default_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
  452. struct bootflow *bflow)
  453. {
  454. struct udevice *blk;
  455. int ret;
  456. ret = bootdev_get_sibling_blk(dev, &blk);
  457. log_debug("sibling_blk ret=%d, blk=%s\n", ret,
  458. ret ? "(none)" : blk->name);
  459. /*
  460. * If there is no media, indicate that no more partitions should be
  461. * checked
  462. */
  463. if (ret == -EOPNOTSUPP)
  464. ret = -ESHUTDOWN;
  465. if (ret)
  466. return log_msg_ret("blk", ret);
  467. assert(blk);
  468. ret = bootdev_find_in_blk(dev, blk, iter, bflow);
  469. if (ret)
  470. return log_msg_ret("find", ret);
  471. return 0;
  472. }
  473. int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
  474. struct bootflow *bflow)
  475. {
  476. const struct bootdev_ops *ops = bootdev_get_ops(dev);
  477. log_debug("->get_bootflow %s=%p\n", dev->name, ops->get_bootflow);
  478. bootflow_init(bflow, dev, iter->method);
  479. if (!ops->get_bootflow)
  480. return default_get_bootflow(dev, iter, bflow);
  481. return ops->get_bootflow(dev, iter, bflow);
  482. }
  483. void bootdev_clear_bootflows(struct udevice *dev)
  484. {
  485. struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
  486. while (!list_empty(&ucp->bootflow_head)) {
  487. struct bootflow *bflow;
  488. bflow = list_first_entry(&ucp->bootflow_head, struct bootflow,
  489. bm_node);
  490. bootflow_remove(bflow);
  491. }
  492. }
  493. int bootdev_next_label(struct bootflow_iter *iter, struct udevice **devp,
  494. int *method_flagsp)
  495. {
  496. struct udevice *dev;
  497. log_debug("next\n");
  498. for (dev = NULL; !dev && iter->labels[++iter->cur_label];) {
  499. const char *label = iter->labels[iter->cur_label];
  500. int ret;
  501. log_debug("Scanning: %s\n", label);
  502. ret = bootdev_hunt_and_find_by_label(label, &dev,
  503. method_flagsp);
  504. if (iter->flags & BOOTFLOWIF_SHOW) {
  505. if (ret == -EPFNOSUPPORT) {
  506. log_warning("Unknown uclass '%s' in label\n",
  507. label);
  508. } else if (ret == -ENOENT) {
  509. /*
  510. * looking for, e.g. 'scsi0' should find
  511. * something if SCSI is present
  512. */
  513. if (!trailing_strtol(label)) {
  514. log_warning("No bootdevs for '%s'\n",
  515. label);
  516. }
  517. }
  518. }
  519. }
  520. if (!dev)
  521. return log_msg_ret("fin", -ENODEV);
  522. *devp = dev;
  523. return 0;
  524. }
  525. int bootdev_next_prio(struct bootflow_iter *iter, struct udevice **devp)
  526. {
  527. struct udevice *dev = *devp;
  528. bool found;
  529. int ret;
  530. /* find the next device with this priority */
  531. *devp = NULL;
  532. log_debug("next prio %d: dev=%p/%s\n", iter->cur_prio, dev,
  533. dev ? dev->name : "none");
  534. do {
  535. /*
  536. * Don't probe devices here since they may not be of the
  537. * required priority
  538. */
  539. if (!dev)
  540. uclass_find_first_device(UCLASS_BOOTDEV, &dev);
  541. else
  542. uclass_find_next_device(&dev);
  543. found = false;
  544. /* scan for the next device with the correct priority */
  545. while (dev) {
  546. struct bootdev_uc_plat *plat;
  547. plat = dev_get_uclass_plat(dev);
  548. log_debug("- %s: %d, want %d\n", dev->name, plat->prio,
  549. iter->cur_prio);
  550. if (plat->prio == iter->cur_prio)
  551. break;
  552. uclass_find_next_device(&dev);
  553. }
  554. /* none found for this priority, so move to the next */
  555. if (!dev) {
  556. log_debug("None found at prio %d, moving to %d\n",
  557. iter->cur_prio, iter->cur_prio + 1);
  558. if (++iter->cur_prio == BOOTDEVP_COUNT)
  559. return log_msg_ret("fin", -ENODEV);
  560. if (iter->flags & BOOTFLOWIF_HUNT) {
  561. /* hunt to find new bootdevs */
  562. ret = bootdev_hunt_prio(iter->cur_prio,
  563. iter->flags &
  564. BOOTFLOWIF_SHOW);
  565. log_debug("- bootdev_hunt_prio() ret %d\n",
  566. ret);
  567. if (ret)
  568. return log_msg_ret("hun", ret);
  569. }
  570. } else {
  571. ret = device_probe(dev);
  572. if (ret) {
  573. log_debug("Device '%s' failed to probe\n",
  574. dev->name);
  575. dev = NULL;
  576. }
  577. }
  578. } while (!dev);
  579. *devp = dev;
  580. return 0;
  581. }
  582. int bootdev_setup_iter(struct bootflow_iter *iter, const char *label,
  583. struct udevice **devp, int *method_flagsp)
  584. {
  585. struct udevice *bootstd, *dev = NULL;
  586. bool show = iter->flags & BOOTFLOWIF_SHOW;
  587. int method_flags;
  588. int ret;
  589. ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
  590. if (ret) {
  591. log_err("Missing bootstd device\n");
  592. return log_msg_ret("std", ret);
  593. }
  594. /* hunt for any pre-scan devices */
  595. if (iter->flags & BOOTFLOWIF_HUNT) {
  596. ret = bootdev_hunt_prio(BOOTDEVP_1_PRE_SCAN, show);
  597. log_debug("- bootdev_hunt_prio() ret %d\n", ret);
  598. if (ret)
  599. return log_msg_ret("pre", ret);
  600. }
  601. /* Handle scanning a single device */
  602. if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && label) {
  603. if (iter->flags & BOOTFLOWIF_HUNT) {
  604. ret = bootdev_hunt(label, show);
  605. if (ret)
  606. return log_msg_ret("hun", ret);
  607. }
  608. ret = bootdev_find_by_any(label, &dev, &method_flags);
  609. if (ret)
  610. return log_msg_ret("lab", ret);
  611. log_debug("method_flags: %x\n", method_flags);
  612. if (method_flags & BOOTFLOW_METHF_SINGLE_UCLASS)
  613. iter->flags |= BOOTFLOWIF_SINGLE_UCLASS;
  614. else if (method_flags & BOOTFLOW_METHF_SINGLE_DEV)
  615. iter->flags |= BOOTFLOWIF_SINGLE_DEV;
  616. else
  617. iter->flags |= BOOTFLOWIF_SINGLE_MEDIA;
  618. log_debug("Selected label: %s, flags %x\n", label, iter->flags);
  619. } else {
  620. bool ok;
  621. /* This either returns a non-empty list or NULL */
  622. iter->labels = bootstd_get_bootdev_order(bootstd, &ok);
  623. if (!ok)
  624. return log_msg_ret("ord", -ENOMEM);
  625. log_debug("setup labels %p\n", iter->labels);
  626. if (iter->labels) {
  627. iter->cur_label = -1;
  628. ret = bootdev_next_label(iter, &dev, &method_flags);
  629. } else {
  630. ret = bootdev_next_prio(iter, &dev);
  631. method_flags = 0;
  632. }
  633. if (!dev)
  634. return log_msg_ret("fin", -ENOENT);
  635. log_debug("Selected bootdev: %s\n", dev->name);
  636. }
  637. ret = device_probe(dev);
  638. if (ret)
  639. return log_msg_ret("probe", ret);
  640. if (method_flagsp)
  641. *method_flagsp = method_flags;
  642. *devp = dev;
  643. return 0;
  644. }
  645. static int bootdev_hunt_drv(struct bootdev_hunter *info, uint seq, bool show)
  646. {
  647. const char *name = uclass_get_name(info->uclass);
  648. struct bootstd_priv *std;
  649. int ret;
  650. ret = bootstd_get_priv(&std);
  651. if (ret)
  652. return log_msg_ret("std", ret);
  653. if (!(std->hunters_used & BIT(seq))) {
  654. if (show)
  655. printf("Hunting with: %s\n",
  656. uclass_get_name(info->uclass));
  657. log_debug("Hunting with: %s\n", name);
  658. if (info->hunt) {
  659. ret = info->hunt(info, show);
  660. log_debug(" - hunt result %d\n", ret);
  661. if (ret)
  662. return ret;
  663. }
  664. std->hunters_used |= BIT(seq);
  665. }
  666. return 0;
  667. }
  668. int bootdev_hunt(const char *spec, bool show)
  669. {
  670. struct bootdev_hunter *start;
  671. const char *end;
  672. int n_ent, i;
  673. int result;
  674. size_t len;
  675. start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
  676. n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
  677. result = 0;
  678. len = SIZE_MAX;
  679. if (spec) {
  680. trailing_strtoln_end(spec, NULL, &end);
  681. len = end - spec;
  682. }
  683. for (i = 0; i < n_ent; i++) {
  684. struct bootdev_hunter *info = start + i;
  685. const char *name = uclass_get_name(info->uclass);
  686. int ret;
  687. log_debug("looking at %.*s for %s\n",
  688. (int)max(strlen(name), len), spec, name);
  689. if (spec && strncmp(spec, name, max(strlen(name), len))) {
  690. if (info->uclass != UCLASS_ETH ||
  691. (strcmp("dhcp", spec) && strcmp("pxe", spec)))
  692. continue;
  693. }
  694. ret = bootdev_hunt_drv(info, i, show);
  695. if (ret)
  696. result = ret;
  697. }
  698. return result;
  699. }
  700. int bootdev_hunt_prio(enum bootdev_prio_t prio, bool show)
  701. {
  702. struct bootdev_hunter *start;
  703. int n_ent, i;
  704. int result;
  705. start = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
  706. n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
  707. result = 0;
  708. log_debug("Hunting for priority %d\n", prio);
  709. for (i = 0; i < n_ent; i++) {
  710. struct bootdev_hunter *info = start + i;
  711. int ret;
  712. if (prio != info->prio)
  713. continue;
  714. ret = bootdev_hunt_drv(info, i, show);
  715. log_debug("bootdev_hunt_drv() return %d\n", ret);
  716. if (ret && ret != -ENOENT)
  717. result = ret;
  718. }
  719. log_debug("exit %d\n", result);
  720. return result;
  721. }
  722. void bootdev_list_hunters(struct bootstd_priv *std)
  723. {
  724. struct bootdev_hunter *orig, *start;
  725. int n_ent, i;
  726. orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
  727. n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
  728. /*
  729. * workaround for strange bug in clang-12 which sees all the below data
  730. * as zeroes. Any access of start seems to fix it, such as
  731. *
  732. * printf("%p", start);
  733. *
  734. * Use memcpy() to force the correct behaviour.
  735. */
  736. memcpy(&start, &orig, sizeof(orig));
  737. printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
  738. printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
  739. for (i = 0; i < n_ent; i++) {
  740. struct bootdev_hunter *info = start + i;
  741. printf("%4d %4s %-15s %s\n", info->prio,
  742. std->hunters_used & BIT(i) ? "*" : "",
  743. uclass_get_name(info->uclass),
  744. info->drv ? info->drv->name : "(none)");
  745. }
  746. printf("(total hunters: %d)\n", n_ent);
  747. }
  748. static int bootdev_post_bind(struct udevice *dev)
  749. {
  750. struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
  751. INIT_LIST_HEAD(&ucp->bootflow_head);
  752. return 0;
  753. }
  754. static int bootdev_pre_unbind(struct udevice *dev)
  755. {
  756. bootdev_clear_bootflows(dev);
  757. return 0;
  758. }
  759. UCLASS_DRIVER(bootdev) = {
  760. .id = UCLASS_BOOTDEV,
  761. .name = "bootdev",
  762. .flags = DM_UC_FLAG_SEQ_ALIAS,
  763. .per_device_plat_auto = sizeof(struct bootdev_uc_plat),
  764. .post_bind = bootdev_post_bind,
  765. .pre_unbind = bootdev_pre_unbind,
  766. };