bootflow.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Test for bootdev functions. All start with 'bootdev'
  4. *
  5. * Copyright 2021 Google LLC
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #include <common.h>
  9. #include <bootdev.h>
  10. #include <bootflow.h>
  11. #include <bootmeth.h>
  12. #include <bootstd.h>
  13. #include <cli.h>
  14. #include <dm.h>
  15. #include <expo.h>
  16. #ifdef CONFIG_SANDBOX
  17. #include <asm/test.h>
  18. #endif
  19. #include <dm/device-internal.h>
  20. #include <dm/lists.h>
  21. #include <test/suites.h>
  22. #include <test/ut.h>
  23. #include "bootstd_common.h"
  24. #include "../../boot/bootflow_internal.h"
  25. #include "../../boot/scene_internal.h"
  26. DECLARE_GLOBAL_DATA_PTR;
  27. extern U_BOOT_DRIVER(bootmeth_2script);
  28. static int inject_response(struct unit_test_state *uts)
  29. {
  30. /*
  31. * The image being booted presents a menu of options:
  32. *
  33. * Fedora-Workstation-armhfp-31-1.9 Boot Options.
  34. * 1: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
  35. * Enter choice:
  36. *
  37. * Provide input for this, to avoid waiting two seconds for a timeout.
  38. */
  39. ut_asserteq(2, console_in_puts("1\n"));
  40. return 0;
  41. }
  42. /* Check 'bootflow scan/list' commands */
  43. static int bootflow_cmd(struct unit_test_state *uts)
  44. {
  45. console_record_reset_enable();
  46. ut_assertok(run_command("bootdev select 1", 0));
  47. ut_assert_console_end();
  48. ut_assertok(run_command("bootflow scan -lH", 0));
  49. ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
  50. ut_assert_nextline("Seq Method State Uclass Part Name Filename");
  51. ut_assert_nextlinen("---");
  52. ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
  53. ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
  54. ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
  55. ut_assert_nextline("No more bootdevs");
  56. ut_assert_nextlinen("---");
  57. ut_assert_nextline("(1 bootflow, 1 valid)");
  58. ut_assert_console_end();
  59. ut_assertok(run_command("bootflow list", 0));
  60. ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
  61. ut_assert_nextline("Seq Method State Uclass Part Name Filename");
  62. ut_assert_nextlinen("---");
  63. ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
  64. ut_assert_nextlinen("---");
  65. ut_assert_nextline("(1 bootflow, 1 valid)");
  66. ut_assert_console_end();
  67. return 0;
  68. }
  69. BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  70. /* Check 'bootflow scan' with a label / seq */
  71. static int bootflow_cmd_label(struct unit_test_state *uts)
  72. {
  73. test_set_eth_enable(false);
  74. console_record_reset_enable();
  75. ut_assertok(run_command("bootflow scan -lH mmc1", 0));
  76. ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
  77. ut_assert_skip_to_line("(1 bootflow, 1 valid)");
  78. ut_assert_console_end();
  79. ut_assertok(run_command("bootflow scan -lH 0", 0));
  80. ut_assert_nextline("Scanning for bootflows with label '0'");
  81. ut_assert_skip_to_line("(0 bootflows, 0 valid)");
  82. ut_assert_console_end();
  83. /*
  84. * with ethernet enabled we have 8 devices ahead of the mmc ones:
  85. *
  86. * ut_assertok(run_command("bootdev list", 0));
  87. * Seq Probed Status Uclass Name
  88. * --- ------ ------ -------- ------------------
  89. * 0 [ + ] OK ethernet eth@10002000.bootdev
  90. * 1 [ ] OK ethernet eth@10003000.bootdev
  91. * 2 [ ] OK ethernet sbe5.bootdev
  92. * 3 [ ] OK ethernet eth@10004000.bootdev
  93. * 4 [ ] OK ethernet phy-test-eth.bootdev
  94. * 5 [ ] OK ethernet dsa-test-eth.bootdev
  95. * 6 [ ] OK ethernet dsa-test@0.bootdev
  96. * 7 [ ] OK ethernet dsa-test@1.bootdev
  97. * 8 [ ] OK mmc mmc2.bootdev
  98. * 9 [ + ] OK mmc mmc1.bootdev
  99. * a [ ] OK mmc mmc0.bootdev
  100. */
  101. ut_assertok(run_command("bootflow scan -lH 9", 0));
  102. ut_assert_nextline("Scanning for bootflows with label '9'");
  103. ut_assert_skip_to_line("(1 bootflow, 1 valid)");
  104. ut_assertok(run_command("bootflow scan -lH 0", 0));
  105. ut_assert_nextline("Scanning for bootflows with label '0'");
  106. ut_assert_skip_to_line("(0 bootflows, 0 valid)");
  107. ut_assert_console_end();
  108. return 0;
  109. }
  110. BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
  111. UT_TESTF_ETH_BOOTDEV);
  112. /* Check 'bootflow scan/list' commands using all bootdevs */
  113. static int bootflow_cmd_glob(struct unit_test_state *uts)
  114. {
  115. ut_assertok(bootstd_test_drop_bootdev_order(uts));
  116. console_record_reset_enable();
  117. ut_assertok(run_command("bootflow scan -lGH", 0));
  118. ut_assert_nextline("Scanning for bootflows in all bootdevs");
  119. ut_assert_nextline("Seq Method State Uclass Part Name Filename");
  120. ut_assert_nextlinen("---");
  121. ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
  122. ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
  123. ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
  124. ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
  125. ut_assert_nextline("No more bootdevs");
  126. ut_assert_nextlinen("---");
  127. ut_assert_nextline("(1 bootflow, 1 valid)");
  128. ut_assert_console_end();
  129. ut_assertok(run_command("bootflow list", 0));
  130. ut_assert_nextline("Showing all bootflows");
  131. ut_assert_nextline("Seq Method State Uclass Part Name Filename");
  132. ut_assert_nextlinen("---");
  133. ut_assert_nextline(" 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
  134. ut_assert_nextlinen("---");
  135. ut_assert_nextline("(1 bootflow, 1 valid)");
  136. ut_assert_console_end();
  137. return 0;
  138. }
  139. BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  140. /* Check 'bootflow scan -e' */
  141. static int bootflow_cmd_scan_e(struct unit_test_state *uts)
  142. {
  143. ut_assertok(bootstd_test_drop_bootdev_order(uts));
  144. console_record_reset_enable();
  145. ut_assertok(run_command("bootflow scan -aleGH", 0));
  146. ut_assert_nextline("Scanning for bootflows in all bootdevs");
  147. ut_assert_nextline("Seq Method State Uclass Part Name Filename");
  148. ut_assert_nextlinen("---");
  149. ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
  150. ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole <NULL>");
  151. ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
  152. ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole <NULL>");
  153. ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
  154. ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
  155. ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole <NULL>");
  156. ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
  157. ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole <NULL>");
  158. ut_assert_nextline(" ** No partition found, err=-2: No such file or directory");
  159. ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
  160. ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 efi/boot/bootsbox.efi");
  161. ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
  162. ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole <NULL>");
  163. ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported");
  164. ut_assert_nextline("No more bootdevs");
  165. ut_assert_nextlinen("---");
  166. ut_assert_nextline("(64 bootflows, 1 valid)");
  167. ut_assert_console_end();
  168. ut_assertok(run_command("bootflow list", 0));
  169. ut_assert_nextline("Showing all bootflows");
  170. ut_assert_nextline("Seq Method State Uclass Part Name Filename");
  171. ut_assert_nextlinen("---");
  172. ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole <NULL>");
  173. ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole <NULL>");
  174. ut_assert_skip_to_line(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
  175. ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole <NULL>");
  176. ut_assert_nextlinen("---");
  177. ut_assert_nextline("(64 bootflows, 1 valid)");
  178. ut_assert_console_end();
  179. return 0;
  180. }
  181. BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  182. /* Check 'bootflow info' */
  183. static int bootflow_cmd_info(struct unit_test_state *uts)
  184. {
  185. console_record_reset_enable();
  186. ut_assertok(run_command("bootdev select 1", 0));
  187. ut_assert_console_end();
  188. ut_assertok(run_command("bootflow scan", 0));
  189. ut_assert_console_end();
  190. ut_assertok(run_command("bootflow select 0", 0));
  191. ut_assert_console_end();
  192. ut_assertok(run_command("bootflow info", 0));
  193. ut_assert_nextline("Name: mmc1.bootdev.part_1");
  194. ut_assert_nextline("Device: mmc1.bootdev");
  195. ut_assert_nextline("Block dev: mmc1.blk");
  196. ut_assert_nextline("Method: extlinux");
  197. ut_assert_nextline("State: ready");
  198. ut_assert_nextline("Partition: 1");
  199. ut_assert_nextline("Subdir: (none)");
  200. ut_assert_nextline("Filename: /extlinux/extlinux.conf");
  201. ut_assert_nextlinen("Buffer: ");
  202. ut_assert_nextline("Size: 253 (595 bytes)");
  203. ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)");
  204. ut_assert_nextline("Cmdline: (none)");
  205. ut_assert_nextline("Logo: (none)");
  206. ut_assert_nextline("FDT: <NULL>");
  207. ut_assert_nextline("Error: 0");
  208. ut_assert_console_end();
  209. ut_assertok(run_command("bootflow info -d", 0));
  210. ut_assert_nextline("Name: mmc1.bootdev.part_1");
  211. ut_assert_skip_to_line("Error: 0");
  212. ut_assert_nextline("Contents:");
  213. ut_assert_nextline("%s", "");
  214. ut_assert_nextline("# extlinux.conf generated by appliance-creator");
  215. ut_assert_skip_to_line(" initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
  216. ut_assert_console_end();
  217. return 0;
  218. }
  219. BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  220. /* Check 'bootflow scan -b' to boot the first available bootdev */
  221. static int bootflow_scan_boot(struct unit_test_state *uts)
  222. {
  223. console_record_reset_enable();
  224. ut_assertok(inject_response(uts));
  225. ut_assertok(run_command("bootflow scan -b", 0));
  226. ut_assert_nextline(
  227. "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
  228. ut_assert_nextline("Ignoring unknown command: ui");
  229. /*
  230. * We expect it to get through to boot although sandbox always returns
  231. * -EFAULT as it cannot actually boot the kernel
  232. */
  233. ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
  234. ut_assert_nextline("Boot failed (err=-14)");
  235. ut_assert_console_end();
  236. return 0;
  237. }
  238. BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  239. /* Check iterating through available bootflows */
  240. static int bootflow_iter(struct unit_test_state *uts)
  241. {
  242. struct bootflow_iter iter;
  243. struct bootflow bflow;
  244. bootstd_clear_glob();
  245. /* The first device is mmc2.bootdev which has no media */
  246. ut_asserteq(-EPROTONOSUPPORT,
  247. bootflow_scan_first(NULL, NULL, &iter,
  248. BOOTFLOWIF_ALL | BOOTFLOWIF_SKIP_GLOBAL, &bflow));
  249. ut_asserteq(2, iter.num_methods);
  250. ut_asserteq(0, iter.cur_method);
  251. ut_asserteq(0, iter.part);
  252. ut_asserteq(0, iter.max_part);
  253. ut_asserteq_str("extlinux", iter.method->name);
  254. ut_asserteq(0, bflow.err);
  255. /*
  256. * This shows MEDIA even though there is none, since in
  257. * bootdev_find_in_blk() we call part_get_info() which returns
  258. * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
  259. * know.
  260. */
  261. ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
  262. ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
  263. ut_asserteq(2, iter.num_methods);
  264. ut_asserteq(1, iter.cur_method);
  265. ut_asserteq(0, iter.part);
  266. ut_asserteq(0, iter.max_part);
  267. ut_asserteq_str("efi", iter.method->name);
  268. ut_asserteq(0, bflow.err);
  269. ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
  270. bootflow_free(&bflow);
  271. /* The next device is mmc1.bootdev - at first we use the whole device */
  272. ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
  273. ut_asserteq(2, iter.num_methods);
  274. ut_asserteq(0, iter.cur_method);
  275. ut_asserteq(0, iter.part);
  276. ut_asserteq(0x1e, iter.max_part);
  277. ut_asserteq_str("extlinux", iter.method->name);
  278. ut_asserteq(0, bflow.err);
  279. ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
  280. bootflow_free(&bflow);
  281. ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
  282. ut_asserteq(2, iter.num_methods);
  283. ut_asserteq(1, iter.cur_method);
  284. ut_asserteq(0, iter.part);
  285. ut_asserteq(0x1e, iter.max_part);
  286. ut_asserteq_str("efi", iter.method->name);
  287. ut_asserteq(0, bflow.err);
  288. ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
  289. bootflow_free(&bflow);
  290. /* Then more to partition 1 where we find something */
  291. ut_assertok(bootflow_scan_next(&iter, &bflow));
  292. ut_asserteq(2, iter.num_methods);
  293. ut_asserteq(0, iter.cur_method);
  294. ut_asserteq(1, iter.part);
  295. ut_asserteq(0x1e, iter.max_part);
  296. ut_asserteq_str("extlinux", iter.method->name);
  297. ut_asserteq(0, bflow.err);
  298. ut_asserteq(BOOTFLOWST_READY, bflow.state);
  299. bootflow_free(&bflow);
  300. ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
  301. ut_asserteq(2, iter.num_methods);
  302. ut_asserteq(1, iter.cur_method);
  303. ut_asserteq(1, iter.part);
  304. ut_asserteq(0x1e, iter.max_part);
  305. ut_asserteq_str("efi", iter.method->name);
  306. ut_asserteq(0, bflow.err);
  307. ut_asserteq(BOOTFLOWST_FS, bflow.state);
  308. bootflow_free(&bflow);
  309. /* Then more to partition 2 which exists but is not bootable */
  310. ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow));
  311. ut_asserteq(2, iter.num_methods);
  312. ut_asserteq(0, iter.cur_method);
  313. ut_asserteq(2, iter.part);
  314. ut_asserteq(0x1e, iter.max_part);
  315. ut_asserteq_str("extlinux", iter.method->name);
  316. ut_asserteq(0, bflow.err);
  317. ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
  318. bootflow_free(&bflow);
  319. bootflow_iter_uninit(&iter);
  320. ut_assert_console_end();
  321. return 0;
  322. }
  323. BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  324. #if defined(CONFIG_SANDBOX) && defined(CONFIG_BOOTMETH_GLOBAL)
  325. /* Check using the system bootdev */
  326. static int bootflow_system(struct unit_test_state *uts)
  327. {
  328. struct udevice *bootstd, *dev;
  329. if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
  330. return -EAGAIN;
  331. ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
  332. ut_assertok(device_bind(bootstd, DM_DRIVER_GET(bootmeth_efi_mgr),
  333. "efi_mgr", 0, ofnode_null(), &dev));
  334. ut_assertok(device_probe(dev));
  335. sandbox_set_fake_efi_mgr_dev(dev, true);
  336. /* We should get a single 'bootmgr' method right at the end */
  337. bootstd_clear_glob();
  338. console_record_reset_enable();
  339. ut_assertok(run_command("bootflow scan -lH", 0));
  340. ut_assert_skip_to_line(
  341. " 0 efi_mgr ready (none) 0 <NULL> <NULL>");
  342. ut_assert_skip_to_line("No more bootdevs");
  343. ut_assert_skip_to_line("(2 bootflows, 2 valid)");
  344. ut_assert_console_end();
  345. return 0;
  346. }
  347. BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_PDATA |
  348. UT_TESTF_SCAN_FDT);
  349. #endif
  350. /* Check disabling a bootmethod if it requests it */
  351. static int bootflow_iter_disable(struct unit_test_state *uts)
  352. {
  353. struct udevice *bootstd, *dev;
  354. struct bootflow_iter iter;
  355. struct bootflow bflow;
  356. int i;
  357. /* Add the EFI bootmgr driver */
  358. ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
  359. ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
  360. &dev));
  361. ut_assertok(bootstd_test_drop_bootdev_order(uts));
  362. bootstd_clear_glob();
  363. console_record_reset_enable();
  364. ut_assertok(inject_response(uts));
  365. ut_assertok(run_command("bootflow scan -lbH", 0));
  366. /* Try to boot the bootmgr flow, which will fail */
  367. console_record_reset_enable();
  368. ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
  369. ut_asserteq(3, iter.num_methods);
  370. ut_asserteq_str("sandbox", iter.method->name);
  371. ut_assertok(inject_response(uts));
  372. ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
  373. ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
  374. ut_assert_console_end();
  375. /* Check that the sandbox bootmeth has been removed */
  376. ut_asserteq(2, iter.num_methods);
  377. for (i = 0; i < iter.num_methods; i++)
  378. ut_assert(strcmp("sandbox", iter.method_order[i]->name));
  379. return 0;
  380. }
  381. BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  382. /* Check 'bootflow scan' with a bootmeth ordering including a global bootmeth */
  383. static int bootflow_scan_glob_bootmeth(struct unit_test_state *uts)
  384. {
  385. if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL))
  386. return -EAGAIN;
  387. ut_assertok(bootstd_test_drop_bootdev_order(uts));
  388. /*
  389. * Make sure that the -G flag makes the scan fail, since this is not
  390. * supported when an ordering is provided
  391. */
  392. console_record_reset_enable();
  393. ut_assertok(bootmeth_set_order("efi firmware0"));
  394. ut_assertok(run_command("bootflow scan -lGH", 0));
  395. ut_assert_nextline("Scanning for bootflows in all bootdevs");
  396. ut_assert_nextline(
  397. "Seq Method State Uclass Part Name Filename");
  398. ut_assert_nextlinen("---");
  399. ut_assert_nextlinen("---");
  400. ut_assert_nextline("(0 bootflows, 0 valid)");
  401. ut_assert_console_end();
  402. ut_assertok(run_command("bootflow scan -lH", 0));
  403. ut_assert_nextline("Scanning for bootflows in all bootdevs");
  404. ut_assert_nextline(
  405. "Seq Method State Uclass Part Name Filename");
  406. ut_assert_nextlinen("---");
  407. ut_assert_nextline("Scanning global bootmeth 'firmware0':");
  408. ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
  409. ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
  410. ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
  411. ut_assert_nextline("No more bootdevs");
  412. ut_assert_nextlinen("---");
  413. ut_assert_nextline("(0 bootflows, 0 valid)");
  414. ut_assert_console_end();
  415. return 0;
  416. }
  417. BOOTSTD_TEST(bootflow_scan_glob_bootmeth, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  418. /* Check 'bootflow boot' to boot a selected bootflow */
  419. static int bootflow_cmd_boot(struct unit_test_state *uts)
  420. {
  421. console_record_reset_enable();
  422. ut_assertok(run_command("bootdev select 1", 0));
  423. ut_assert_console_end();
  424. ut_assertok(run_command("bootflow scan", 0));
  425. ut_assert_console_end();
  426. ut_assertok(run_command("bootflow select 0", 0));
  427. ut_assert_console_end();
  428. ut_assertok(inject_response(uts));
  429. ut_asserteq(1, run_command("bootflow boot", 0));
  430. ut_assert_nextline(
  431. "** Booting bootflow 'mmc1.bootdev.part_1' with extlinux");
  432. ut_assert_nextline("Ignoring unknown command: ui");
  433. /*
  434. * We expect it to get through to boot although sandbox always returns
  435. * -EFAULT as it cannot actually boot the kernel
  436. */
  437. ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
  438. ut_assert_nextline("Boot failed (err=-14)");
  439. ut_assert_console_end();
  440. return 0;
  441. }
  442. BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  443. /**
  444. * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian
  445. *
  446. * @uts: Unit test state
  447. * Returns 0 on success, -ve on failure
  448. */
  449. static int prep_mmc4_bootdev(struct unit_test_state *uts)
  450. {
  451. static const char *order[] = {"mmc2", "mmc1", "mmc4", NULL};
  452. struct udevice *dev, *bootstd;
  453. struct bootstd_priv *std;
  454. const char **old_order;
  455. ofnode node;
  456. /* Enable the mmc4 node since we need a second bootflow */
  457. node = ofnode_path("/mmc4");
  458. ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
  459. /* Enable the script bootmeth too */
  460. ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
  461. ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script),
  462. "bootmeth_script", 0, ofnode_null(), &dev));
  463. /* Change the order to include mmc4 */
  464. std = dev_get_priv(bootstd);
  465. old_order = std->bootdev_order;
  466. std->bootdev_order = order;
  467. console_record_reset_enable();
  468. ut_assertok(run_command("bootflow scan", 0));
  469. ut_assert_console_end();
  470. /* Restore the order used by the device tree */
  471. std->bootdev_order = old_order;
  472. return 0;
  473. }
  474. /* Check 'bootflow menu' to select a bootflow */
  475. static int bootflow_cmd_menu(struct unit_test_state *uts)
  476. {
  477. char prev[3];
  478. ut_assertok(prep_mmc4_bootdev(uts));
  479. /* Add keypresses to move to and select the second one in the list */
  480. prev[0] = CTL_CH('n');
  481. prev[1] = '\r';
  482. prev[2] = '\0';
  483. ut_asserteq(2, console_in_puts(prev));
  484. ut_assertok(run_command("bootflow menu", 0));
  485. ut_assert_nextline("Selected: Armbian");
  486. ut_assert_console_end();
  487. return 0;
  488. }
  489. BOOTSTD_TEST(bootflow_cmd_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  490. /* Check searching for a single bootdev using the hunters */
  491. static int bootflow_cmd_hunt_single(struct unit_test_state *uts)
  492. {
  493. struct bootstd_priv *std;
  494. /* get access to the used hunters */
  495. ut_assertok(bootstd_get_priv(&std));
  496. ut_assertok(bootstd_test_drop_bootdev_order(uts));
  497. console_record_reset_enable();
  498. ut_assertok(run_command("bootflow scan -l mmc1", 0));
  499. ut_assert_nextline("Scanning for bootflows with label 'mmc1'");
  500. ut_assert_skip_to_line("(1 bootflow, 1 valid)");
  501. ut_assert_console_end();
  502. /* check that the hunter was used */
  503. ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
  504. return 0;
  505. }
  506. BOOTSTD_TEST(bootflow_cmd_hunt_single, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  507. /* Check searching for a uclass label using the hunters */
  508. static int bootflow_cmd_hunt_label(struct unit_test_state *uts)
  509. {
  510. struct bootstd_priv *std;
  511. /* get access to the used hunters */
  512. ut_assertok(bootstd_get_priv(&std));
  513. test_set_skip_delays(true);
  514. test_set_eth_enable(false);
  515. ut_assertok(bootstd_test_drop_bootdev_order(uts));
  516. console_record_reset_enable();
  517. ut_assertok(run_command("bootflow scan -l mmc", 0));
  518. /* check that the hunter was used */
  519. ut_asserteq(BIT(MMC_HUNTER) | BIT(1), std->hunters_used);
  520. /* check that we got the mmc1 bootflow */
  521. ut_assert_nextline("Scanning for bootflows with label 'mmc'");
  522. ut_assert_nextlinen("Seq");
  523. ut_assert_nextlinen("---");
  524. ut_assert_nextline("Hunting with: simple_bus");
  525. ut_assert_nextline("Found 2 extension board(s).");
  526. ut_assert_nextline("Hunting with: mmc");
  527. ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
  528. ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
  529. ut_assert_nextline(
  530. " 0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf");
  531. ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
  532. ut_assert_skip_to_line("(1 bootflow, 1 valid)");
  533. ut_assert_console_end();
  534. return 0;
  535. }
  536. BOOTSTD_TEST(bootflow_cmd_hunt_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  537. /**
  538. * check_font() - Check that the font size for an item matches expectations
  539. *
  540. * @uts: Unit test state
  541. * @scn: Scene containing the text object
  542. * @id: ID of the text object
  543. * Returns 0 on success, -ve on failure
  544. */
  545. static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
  546. int font_size)
  547. {
  548. struct scene_obj_txt *txt;
  549. txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
  550. ut_assertnonnull(txt);
  551. ut_asserteq(font_size, txt->font_size);
  552. return 0;
  553. }
  554. /* Check themes work with a bootflow menu */
  555. static int bootflow_menu_theme(struct unit_test_state *uts)
  556. {
  557. const int font_size = 30;
  558. struct scene *scn;
  559. struct expo *exp;
  560. ofnode node;
  561. int i;
  562. ut_assertok(prep_mmc4_bootdev(uts));
  563. ut_assertok(bootflow_menu_new(&exp));
  564. node = ofnode_path("/bootstd/theme");
  565. ut_assert(ofnode_valid(node));
  566. ut_assertok(bootflow_menu_apply_theme(exp, node));
  567. scn = expo_lookup_scene_id(exp, MAIN);
  568. ut_assertnonnull(scn);
  569. /*
  570. * Check that the txt objects have the correct font size from the
  571. * device tree node: bootstd/theme
  572. *
  573. * Check both menu items, since there are two bootflows
  574. */
  575. ut_assertok(check_font(uts, scn, OBJ_PROMPT, font_size));
  576. ut_assertok(check_font(uts, scn, OBJ_POINTER, font_size));
  577. for (i = 0; i < 2; i++) {
  578. ut_assertok(check_font(uts, scn, ITEM_DESC + i, font_size));
  579. ut_assertok(check_font(uts, scn, ITEM_KEY + i, font_size));
  580. ut_assertok(check_font(uts, scn, ITEM_LABEL + i, font_size));
  581. }
  582. expo_destroy(exp);
  583. return 0;
  584. }
  585. BOOTSTD_TEST(bootflow_menu_theme, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
  586. /**
  587. * check_arg() - Check both the normal case and the buffer-overflow case
  588. *
  589. * @uts: Unit-test state
  590. * @expect_ret: Expected return value (i.e. buffer length)
  591. * @expect_str: String expected to be returned
  592. * @buf: Buffer to use
  593. * @from: Original cmdline to update
  594. * @arg: Argument to update (e.g. "console")
  595. * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
  596. * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
  597. * it without any value ("initrd")
  598. */
  599. static int check_arg(struct unit_test_state *uts, int expect_ret,
  600. const char *expect_str, char *buf, const char *from,
  601. const char *arg, const char *val)
  602. {
  603. /* check for writing outside the reported bounds */
  604. buf[expect_ret] = '[';
  605. ut_asserteq(expect_ret,
  606. cmdline_set_arg(buf, expect_ret, from, arg, val, NULL));
  607. ut_asserteq_str(expect_str, buf);
  608. ut_asserteq('[', buf[expect_ret]);
  609. /* do the test again but with one less byte in the buffer */
  610. ut_asserteq(-E2BIG, cmdline_set_arg(buf, expect_ret - 1, from, arg,
  611. val, NULL));
  612. return 0;
  613. }
  614. /* Test of bootflow_cmdline_set_arg() */
  615. static int test_bootflow_cmdline_set(struct unit_test_state *uts)
  616. {
  617. char buf[50];
  618. const int size = sizeof(buf);
  619. /*
  620. * note that buffer-overflow tests are immediately each test case, just
  621. * top keep the code together
  622. */
  623. /* add an arg that doesn't already exist, starting from empty */
  624. ut_asserteq(-ENOENT, cmdline_set_arg(buf, size, NULL, "me", NULL,
  625. NULL));
  626. ut_assertok(check_arg(uts, 3, "me", buf, NULL, "me", BOOTFLOWCL_EMPTY));
  627. ut_assertok(check_arg(uts, 4, "me=", buf, NULL, "me", ""));
  628. ut_assertok(check_arg(uts, 8, "me=fred", buf, NULL, "me", "fred"));
  629. /* add an arg that doesn't already exist, starting from non-empty */
  630. ut_assertok(check_arg(uts, 11, "arg=123 me", buf, "arg=123", "me",
  631. BOOTFLOWCL_EMPTY));
  632. ut_assertok(check_arg(uts, 12, "arg=123 me=", buf, "arg=123", "me",
  633. ""));
  634. ut_assertok(check_arg(uts, 16, "arg=123 me=fred", buf, "arg=123", "me",
  635. "fred"));
  636. /* update an arg at the start */
  637. ut_assertok(check_arg(uts, 1, "", buf, "arg=123", "arg", NULL));
  638. ut_assertok(check_arg(uts, 4, "arg", buf, "arg=123", "arg",
  639. BOOTFLOWCL_EMPTY));
  640. ut_assertok(check_arg(uts, 5, "arg=", buf, "arg=123", "arg", ""));
  641. ut_assertok(check_arg(uts, 6, "arg=1", buf, "arg=123", "arg", "1"));
  642. ut_assertok(check_arg(uts, 9, "arg=1234", buf, "arg=123", "arg",
  643. "1234"));
  644. /* update an arg at the end */
  645. ut_assertok(check_arg(uts, 5, "mary", buf, "mary arg=123", "arg",
  646. NULL));
  647. ut_assertok(check_arg(uts, 9, "mary arg", buf, "mary arg=123", "arg",
  648. BOOTFLOWCL_EMPTY));
  649. ut_assertok(check_arg(uts, 10, "mary arg=", buf, "mary arg=123", "arg",
  650. ""));
  651. ut_assertok(check_arg(uts, 11, "mary arg=1", buf, "mary arg=123", "arg",
  652. "1"));
  653. ut_assertok(check_arg(uts, 14, "mary arg=1234", buf, "mary arg=123",
  654. "arg", "1234"));
  655. /* update an arg in the middle */
  656. ut_assertok(check_arg(uts, 16, "mary=abc john=2", buf,
  657. "mary=abc arg=123 john=2", "arg", NULL));
  658. ut_assertok(check_arg(uts, 20, "mary=abc arg john=2", buf,
  659. "mary=abc arg=123 john=2", "arg",
  660. BOOTFLOWCL_EMPTY));
  661. ut_assertok(check_arg(uts, 21, "mary=abc arg= john=2", buf,
  662. "mary=abc arg=123 john=2", "arg", ""));
  663. ut_assertok(check_arg(uts, 22, "mary=abc arg=1 john=2", buf,
  664. "mary=abc arg=123 john=2", "arg", "1"));
  665. ut_assertok(check_arg(uts, 25, "mary=abc arg=1234 john=2", buf,
  666. "mary=abc arg=123 john=2", "arg", "1234"));
  667. /* handle existing args with quotes */
  668. ut_assertok(check_arg(uts, 16, "mary=\"abc\" john", buf,
  669. "mary=\"abc\" arg=123 john", "arg", NULL));
  670. /* handle existing args with quoted spaces */
  671. ut_assertok(check_arg(uts, 20, "mary=\"abc def\" john", buf,
  672. "mary=\"abc def\" arg=123 john", "arg", NULL));
  673. ut_assertok(check_arg(uts, 34, "mary=\"abc def\" arg=123 john def=4",
  674. buf, "mary=\"abc def\" arg=123 john", "def",
  675. "4"));
  676. /* quote at the start */
  677. ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
  678. "mary=\"abc def\" arg=\"123 456\"",
  679. "arg", "\"4 5 6", NULL));
  680. /* quote at the end */
  681. ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
  682. "mary=\"abc def\" arg=\"123 456\"",
  683. "arg", "4 5 6\"", NULL));
  684. /* quote in the middle */
  685. ut_asserteq(-EBADF, cmdline_set_arg(buf, size,
  686. "mary=\"abc def\" arg=\"123 456\"",
  687. "arg", "\"4 \"5 6\"", NULL));
  688. /* handle updating a quoted arg */
  689. ut_assertok(check_arg(uts, 27, "mary=\"abc def\" arg=\"4 5 6\"", buf,
  690. "mary=\"abc def\" arg=\"123 456\"", "arg",
  691. "4 5 6"));
  692. /* changing a quoted arg to a non-quoted arg */
  693. ut_assertok(check_arg(uts, 23, "mary=\"abc def\" arg=789", buf,
  694. "mary=\"abc def\" arg=\"123 456\"", "arg",
  695. "789"));
  696. /* changing a non-quoted arg to a quoted arg */
  697. ut_assertok(check_arg(uts, 29, "mary=\"abc def\" arg=\"456 789\"", buf,
  698. "mary=\"abc def\" arg=123", "arg", "456 789"));
  699. /* handling of spaces */
  700. ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
  701. ut_assertok(check_arg(uts, 8, "arg=123", buf, " ", "arg", "123"));
  702. ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john ", "arg",
  703. "123"));
  704. ut_assertok(check_arg(uts, 13, "john arg=123", buf, " john arg=123 ",
  705. "arg", "123"));
  706. ut_assertok(check_arg(uts, 18, "john arg=123 mary", buf,
  707. " john arg=123 mary ", "arg", "123"));
  708. /* unchanged arg */
  709. ut_assertok(check_arg(uts, 3, "me", buf, "me", "me", BOOTFLOWCL_EMPTY));
  710. /* arg which starts with the same name */
  711. ut_assertok(check_arg(uts, 28, "mary=abc johnathon=2 john=3", buf,
  712. "mary=abc johnathon=2 john=1", "john", "3"));
  713. return 0;
  714. }
  715. BOOTSTD_TEST(test_bootflow_cmdline_set, 0);
  716. /* Test of bootflow_cmdline_set_arg() */
  717. static int bootflow_set_arg(struct unit_test_state *uts)
  718. {
  719. struct bootflow s_bflow, *bflow = &s_bflow;
  720. ulong mem_start;
  721. ut_assertok(env_set("bootargs", NULL));
  722. mem_start = ut_check_delta(0);
  723. /* Do a simple sanity check. Rely on bootflow_cmdline() for the rest */
  724. bflow->cmdline = NULL;
  725. ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", "123", false));
  726. ut_asserteq_str(bflow->cmdline, "fred=123");
  727. ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "and here", false));
  728. ut_asserteq_str(bflow->cmdline, "fred=123 mary=\"and here\"");
  729. ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", NULL, false));
  730. ut_asserteq_str(bflow->cmdline, "fred=123");
  731. ut_assertok(bootflow_cmdline_set_arg(bflow, "fred", NULL, false));
  732. ut_asserteq_ptr(bflow->cmdline, NULL);
  733. ut_asserteq(0, ut_check_delta(mem_start));
  734. ut_assertok(bootflow_cmdline_set_arg(bflow, "mary", "here", true));
  735. ut_asserteq_str("mary=here", env_get("bootargs"));
  736. ut_assertok(env_set("bootargs", NULL));
  737. return 0;
  738. }
  739. BOOTSTD_TEST(bootflow_set_arg, 0);
  740. /* Test of bootflow_cmdline_get_arg() */
  741. static int bootflow_cmdline_get(struct unit_test_state *uts)
  742. {
  743. int pos;
  744. /* empty string */
  745. ut_asserteq(-ENOENT, cmdline_get_arg("", "fred", &pos));
  746. /* arg with empty value */
  747. ut_asserteq(0, cmdline_get_arg("fred= mary", "fred", &pos));
  748. ut_asserteq(5, pos);
  749. /* arg with a value */
  750. ut_asserteq(2, cmdline_get_arg("fred=23", "fred", &pos));
  751. ut_asserteq(5, pos);
  752. /* arg with a value */
  753. ut_asserteq(3, cmdline_get_arg("mary=1 fred=234", "fred", &pos));
  754. ut_asserteq(12, pos);
  755. /* arg with a value, after quoted arg */
  756. ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=234", "fred", &pos));
  757. ut_asserteq(16, pos);
  758. /* arg in the middle */
  759. ut_asserteq(0, cmdline_get_arg("mary=\"1 2\" fred john=23", "fred",
  760. &pos));
  761. ut_asserteq(15, pos);
  762. /* quoted arg */
  763. ut_asserteq(3, cmdline_get_arg("mary=\"1 2\" fred=\"3 4\" john=23",
  764. "fred", &pos));
  765. ut_asserteq(17, pos);
  766. /* args starting with the same prefix */
  767. ut_asserteq(1, cmdline_get_arg("mary=abc johnathon=3 john=1", "john",
  768. &pos));
  769. ut_asserteq(26, pos);
  770. return 0;
  771. }
  772. BOOTSTD_TEST(bootflow_cmdline_get, 0);
  773. static int bootflow_cmdline(struct unit_test_state *uts)
  774. {
  775. ut_assertok(run_command("bootflow scan mmc", 0));
  776. ut_assertok(run_command("bootflow sel 0", 0));
  777. console_record_reset_enable();
  778. ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
  779. ut_assert_nextline("Argument not found");
  780. ut_assert_console_end();
  781. ut_asserteq(0, run_command("bootflow cmdline set fred 123", 0));
  782. ut_asserteq(0, run_command("bootflow cmdline get fred", 0));
  783. ut_assert_nextline("123");
  784. ut_asserteq(0, run_command("bootflow cmdline set mary abc", 0));
  785. ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
  786. ut_assert_nextline("abc");
  787. ut_asserteq(0, run_command("bootflow cmdline delete fred", 0));
  788. ut_asserteq(1, run_command("bootflow cmdline get fred", 0));
  789. ut_assert_nextline("Argument not found");
  790. ut_asserteq(0, run_command("bootflow cmdline clear mary", 0));
  791. ut_asserteq(0, run_command("bootflow cmdline get mary", 0));
  792. ut_assert_nextline_empty();
  793. ut_assert_console_end();
  794. return 0;
  795. }
  796. BOOTSTD_TEST(bootflow_cmdline, 0);