[PATCH v3 28/31] bootstd: Add tests for bootstd including all uclasses

Simon Glass sjg at chromium.org
Wed Jan 19 02:43:12 CET 2022


Add a set of combined tests for the bootdev, bootflow and bootmeth
commands, along with associated functionality.

Expand the sandbox console-recording limit so that these can work.

These tests rely on a filesystem script which is not yet added to the
Python tests. It is included here as a shell script.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v3:
- Update bootdev_test_order() for boot_targets env var
- Add tests for the "bootmeths" env var
- Update tests for the new 'bootmeth order' syntax

 MAINTAINERS                        |   1 +
 arch/sandbox/dts/test.dts          |  18 ++
 configs/sandbox_defconfig          |   3 +-
 configs/sandbox_flattree_defconfig |   3 +-
 include/test/suites.h              |   2 +
 test/Makefile                      |   1 +
 test/boot/Makefile                 |   5 +
 test/boot/bootdev.c                | 223 ++++++++++++++++
 test/boot/bootflow.c               | 398 +++++++++++++++++++++++++++++
 test/boot/bootmeth.c               | 122 +++++++++
 test/boot/bootstd_common.c         |  35 +++
 test/boot/bootstd_common.h         |  27 ++
 test/cmd_ut.c                      |   7 +
 13 files changed, 843 insertions(+), 2 deletions(-)
 create mode 100644 test/boot/Makefile
 create mode 100644 test/boot/bootdev.c
 create mode 100644 test/boot/bootflow.c
 create mode 100644 test/boot/bootmeth.c
 create mode 100644 test/boot/bootstd_common.c
 create mode 100644 test/boot/bootstd_common.h

diff --git a/MAINTAINERS b/MAINTAINERS
index fe168b51a59..8ad70d3d968 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -675,6 +675,7 @@ F:	include/bootflow.h
 F:	include/bootmeth.h
 F:	include/bootstd.h
 F:	net/eth_bootdevice.c
+F:	test/boot/
 
 BTRFS
 M:	Marek Behun <marek.behun at nic.cz>
diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 48ca3e1e472..228411f9696 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -71,6 +71,21 @@
 		};
 	};
 
+	bootstd {
+		compatible = "u-boot,boot-std";
+
+		filename-prefixes = "/", "/boot/";
+		bootdev-order = "mmc2", "mmc1";
+
+		syslinux {
+			compatible = "u-boot,distro-syslinux";
+		};
+
+		efi {
+			compatible = "u-boot,distro-efi";
+		};
+	};
+
 	reboot-mode0 {
 		compatible = "reboot-mode-gpio";
 		gpios = <&gpio_c 0 GPIO_ACTIVE_HIGH>, <&gpio_c 1 GPIO_ACTIVE_HIGH>;
@@ -891,10 +906,13 @@
 		non-removable;
 	};
 
+	/* This is used for the bootdev tests */
 	mmc1 {
 		compatible = "sandbox,mmc";
+		filename = "mmc1.img";
 	};
 
+	/* This is used for the fastboot tests */
 	mmc0 {
 		compatible = "sandbox,mmc";
 	};
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index 4f413582fb2..5825ac1fc14 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -28,7 +28,7 @@ CONFIG_AUTOBOOT_NEVER_TIMEOUT=y
 CONFIG_AUTOBOOT_STOP_STR_ENABLE=y
 CONFIG_AUTOBOOT_STOP_STR_CRYPT="$5$rounds=640000$HrpE65IkB8CM5nCL$BKT3QdF98Bo8fJpTr9tjZLZQyzqPASBY20xuK5Rent9"
 CONFIG_CONSOLE_RECORD=y
-CONFIG_CONSOLE_RECORD_OUT_SIZE=0x1000
+CONFIG_CONSOLE_RECORD_OUT_SIZE=0x6000
 CONFIG_PRE_CONSOLE_BUFFER=y
 CONFIG_LOG=y
 CONFIG_DISPLAY_BOARDINFO_LATE=y
@@ -118,6 +118,7 @@ CONFIG_ENV_IS_IN_EXT4=y
 CONFIG_ENV_EXT4_INTERFACE="host"
 CONFIG_ENV_EXT4_DEVICE_AND_PART="0:0"
 CONFIG_ENV_IMPORT_FDT=y
+# CONFIG_BOOTDEV_ETH is not set
 CONFIG_BOOTP_SEND_HOSTNAME=y
 CONFIG_NETCONSOLE=y
 CONFIG_IP_DEFRAG=y
diff --git a/configs/sandbox_flattree_defconfig b/configs/sandbox_flattree_defconfig
index 4d5a73fce09..817b8bb82fc 100644
--- a/configs/sandbox_flattree_defconfig
+++ b/configs/sandbox_flattree_defconfig
@@ -18,7 +18,7 @@ CONFIG_BOOTSTAGE_FDT=y
 CONFIG_BOOTSTAGE_STASH=y
 CONFIG_BOOTSTAGE_STASH_SIZE=0x4096
 CONFIG_CONSOLE_RECORD=y
-CONFIG_CONSOLE_RECORD_OUT_SIZE=0x1000
+CONFIG_CONSOLE_RECORD_OUT_SIZE=0x6000
 CONFIG_DISPLAY_BOARDINFO_LATE=y
 CONFIG_MISC_INIT_F=y
 CONFIG_CMD_CPU=y
@@ -73,6 +73,7 @@ CONFIG_ENV_IS_NOWHERE=y
 CONFIG_ENV_IS_IN_EXT4=y
 CONFIG_ENV_EXT4_INTERFACE="host"
 CONFIG_ENV_EXT4_DEVICE_AND_PART="0:0"
+# CONFIG_BOOTDEV_ETH is not set
 CONFIG_BOOTP_SEND_HOSTNAME=y
 CONFIG_NETCONSOLE=y
 CONFIG_IP_DEFRAG=y
diff --git a/include/test/suites.h b/include/test/suites.h
index d35cd83a4eb..bf79b008c1a 100644
--- a/include/test/suites.h
+++ b/include/test/suites.h
@@ -29,6 +29,8 @@ int cmd_ut_category(const char *name, const char *prefix,
 int do_ut_addrmap(struct cmd_tbl *cmdtp, int flag, int argc,
 		  char *const argv[]);
 int do_ut_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
+int do_ut_bootstd(struct cmd_tbl *cmdtp, int flag, int argc,
+		  char *const argv[]);
 int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
 		   char *const argv[]);
 int do_ut_common(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
diff --git a/test/Makefile b/test/Makefile
index b3b2902e2e7..abd605a4351 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -22,6 +22,7 @@ obj-$(CONFIG_UT_TIME) += time_ut.o
 obj-y += ut.o
 
 ifeq ($(CONFIG_SPL_BUILD),)
+obj-$(CONFIG_UNIT_TEST) += boot/
 obj-$(CONFIG_UNIT_TEST) += common/
 obj-$(CONFIG_UNIT_TEST) += lib/
 obj-y += log/
diff --git a/test/boot/Makefile b/test/boot/Makefile
new file mode 100644
index 00000000000..1730792b5fa
--- /dev/null
+++ b/test/boot/Makefile
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2021 Google LLC
+
+obj-$(CONFIG_BOOTSTD) += bootdev.o bootstd_common.o bootflow.o bootmeth.o
diff --git a/test/boot/bootdev.c b/test/boot/bootdev.c
new file mode 100644
index 00000000000..1c2a79fb108
--- /dev/null
+++ b/test/boot/bootdev.c
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for bootdev functions. All start with 'bootdev'
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <bootstd.h>
+#include <dm.h>
+#include <bootdev.h>
+#include <bootflow.h>
+#include <mapmem.h>
+#include <os.h>
+#include <test/suites.h>
+#include <test/ut.h>
+#include "bootstd_common.h"
+
+/* Allow reseting the USB-started flag */
+extern char usb_started;
+
+/* Check 'bootdev list' command */
+static int bootdev_test_cmd_list(struct unit_test_state *uts)
+{
+	int probed;
+
+	console_record_reset_enable();
+	for (probed = 0; probed < 2; probed++) {
+		int probe_ch = probed ? '+' : ' ';
+
+		ut_assertok(run_command(probed ? "bootdev list -p" :
+			"bootdev list", 0));
+		ut_assert_nextline("Seq  Probed  Status  Uclass    Name");
+		ut_assert_nextlinen("---");
+		ut_assert_nextline("%3x   [ %c ]  %6s  %-8s  %s", 0, probe_ch, "OK",
+				   "mmc", "mmc2.bootdev");
+		ut_assert_nextline("%3x   [ %c ]  %6s  %-8s  %s", 1, probe_ch, "OK",
+				   "mmc", "mmc1.bootdev");
+		ut_assert_nextline("%3x   [ %c ]  %6s  %-8s  %s", 2, probe_ch, "OK",
+				   "mmc", "mmc0.bootdev");
+		ut_assert_nextlinen("---");
+		ut_assert_nextline("(3 bootdevs)");
+		ut_assert_console_end();
+	}
+
+	return 0;
+}
+BOOTSTD_TEST(bootdev_test_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootdev select' and 'info' commands */
+static int bootdev_test_cmd_select(struct unit_test_state *uts)
+{
+	struct bootstd_priv *std;
+
+	/* get access to the CLI's cur_bootdev */
+	ut_assertok(bootstd_get_priv(&std));
+
+	console_record_reset_enable();
+	ut_asserteq(1, run_command("bootdev info", 0));
+	ut_assert_nextlinen("Please use");
+	ut_assert_console_end();
+
+	/* select by sequence */
+	ut_assertok(run_command("bootdev select 0", 0));
+	ut_assert_console_end();
+
+	ut_assertok(run_command("bootdev info", 0));
+	ut_assert_nextline("Name:      mmc2.bootdev");
+	ut_assert_nextline("Sequence:  0");
+	ut_assert_nextline("Status:    Probed");
+	ut_assert_nextline("Uclass:    mmc");
+	ut_assert_nextline("Bootflows: 0 (0 valid)");
+	ut_assert_console_end();
+
+	/* select by bootdev name */
+	ut_assertok(run_command("bootdev select mmc1.bootdev", 0));
+	ut_assert_console_end();
+	ut_assertnonnull(std->cur_bootdev);
+	ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
+
+	/* select by bootdev label*/
+	ut_assertok(run_command("bootdev select mmc1", 0));
+	ut_assert_console_end();
+	ut_assertnonnull(std->cur_bootdev);
+	ut_asserteq_str("mmc1.bootdev", std->cur_bootdev->name);
+
+	/* deselect */
+	ut_assertok(run_command("bootdev select", 0));
+	ut_assert_console_end();
+	ut_assertnull(std->cur_bootdev);
+
+	ut_asserteq(1, run_command("bootdev info", 0));
+	ut_assert_nextlinen("Please use");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootdev_test_cmd_select, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check bootdev labels */
+static int bootdev_test_labels(struct unit_test_state *uts)
+{
+	struct udevice *dev, *media;
+
+	ut_assertok(bootdev_find_by_label("mmc2", &dev));
+	ut_asserteq(UCLASS_BOOTDEV, device_get_uclass_id(dev));
+	media = dev_get_parent(dev);
+	ut_asserteq(UCLASS_MMC, device_get_uclass_id(media));
+	ut_asserteq_str("mmc2", media->name);
+
+	/* Check invalid uclass */
+	ut_asserteq(-EINVAL, bootdev_find_by_label("fred0", &dev));
+
+	/* Check unknown sequence number */
+	ut_asserteq(-ENOENT, bootdev_find_by_label("mmc6", &dev));
+
+	return 0;
+}
+BOOTSTD_TEST(bootdev_test_labels, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check bootdev ordering with the bootdev-order property */
+static int bootdev_test_order(struct unit_test_state *uts)
+{
+	struct bootflow_iter iter;
+	struct bootflow bflow;
+
+	/*
+	 * First try the order set by the bootdev-order property
+	 * Like all sandbox unit tests this relies on the devicetree setting up
+	 * the required devices:
+	 *
+	 * mmc0 - nothing connected
+	 * mmc1 - connected to mmc1.img file
+	 * mmc2 - nothing connected
+	 */
+	ut_assertok(env_set("boot_targets", NULL));
+	ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
+	ut_asserteq(2, iter.num_devs);
+	ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
+	ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
+	bootflow_iter_uninit(&iter);
+
+	/* Use the environment variable to override it */
+	ut_assertok(env_set("boot_targets", "mmc1 mmc2"));
+	ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
+	ut_asserteq(2, iter.num_devs);
+	ut_asserteq_str("mmc1.bootdev", iter.dev_order[0]->name);
+	ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
+	bootflow_iter_uninit(&iter);
+
+	/*
+	 * Now drop both orderings, to check the default (prioriy/sequence)
+	 * ordering
+	 */
+	ut_assertok(env_set("boot_targets", NULL));
+	ut_assertok(bootstd_test_drop_bootdev_order(uts));
+
+	ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
+	ut_asserteq(3, iter.num_devs);
+	ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
+	ut_asserteq_str("mmc1.bootdev", iter.dev_order[1]->name);
+	ut_asserteq_str("mmc0.bootdev", iter.dev_order[2]->name);
+
+	/*
+	 * Check that adding aliases for the bootdevs works. We just fake it by
+	 * setting the sequence numbers directly.
+	 */
+	iter.dev_order[0]->seq_ = 0;
+	iter.dev_order[1]->seq_ = 3;
+	iter.dev_order[2]->seq_ = 2;
+	bootflow_iter_uninit(&iter);
+
+	ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
+	ut_asserteq(3, iter.num_devs);
+	ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
+	ut_asserteq_str("mmc0.bootdev", iter.dev_order[1]->name);
+	ut_asserteq_str("mmc1.bootdev", iter.dev_order[2]->name);
+	bootflow_iter_uninit(&iter);
+
+	return 0;
+}
+BOOTSTD_TEST(bootdev_test_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check bootdev ordering with the uclass priority */
+static int bootdev_test_prio(struct unit_test_state *uts)
+{
+	struct bootdev_uc_plat *ucp;
+	struct bootflow_iter iter;
+	struct bootflow bflow;
+	struct udevice *blk;
+
+	/* Start up USB which gives us three additional bootdevs */
+	usb_started = false;
+	ut_assertok(run_command("usb start", 0));
+
+	ut_assertok(bootstd_test_drop_bootdev_order(uts));
+
+	/* 3 MMC and 3 USB bootdevs: MMC should come before USB */
+	console_record_reset_enable();
+	ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
+	ut_asserteq(6, iter.num_devs);
+	ut_asserteq_str("mmc2.bootdev", iter.dev_order[0]->name);
+	ut_asserteq_str("usb_mass_storage.lun0.bootdev",
+			iter.dev_order[3]->name);
+
+	ut_assertok(bootdev_get_sibling_blk(iter.dev_order[3], &blk));
+	ut_asserteq_str("usb_mass_storage.lun0", blk->name);
+
+	/* adjust the priority of the first USB bootdev to the highest */
+	ucp = dev_get_uclass_plat(iter.dev_order[3]);
+	ucp->prio = 1;
+
+	bootflow_iter_uninit(&iter);
+	ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
+	ut_asserteq(6, iter.num_devs);
+	ut_asserteq_str("usb_mass_storage.lun0.bootdev",
+			iter.dev_order[0]->name);
+	ut_asserteq_str("mmc2.bootdev", iter.dev_order[1]->name);
+
+	return 0;
+}
+BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c
new file mode 100644
index 00000000000..b1287958eb5
--- /dev/null
+++ b/test/boot/bootflow.c
@@ -0,0 +1,398 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for bootdev functions. All start with 'bootdev'
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <bootdev.h>
+#include <bootflow.h>
+#include <bootstd.h>
+#include <dm.h>
+#include <dm/lists.h>
+#include <test/suites.h>
+#include <test/ut.h>
+#include "bootstd_common.h"
+
+/* Check 'bootflow scan/list' commands */
+static int bootflow_cmd(struct unit_test_state *uts)
+{
+	console_record_reset_enable();
+	ut_assertok(run_command("bootdev select 1", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootflow scan -l", 0));
+	ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
+	ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("  0  syslinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(1 bootflow, 1 valid)");
+	ut_assert_console_end();
+
+	ut_assertok(run_command("bootflow list", 0));
+	ut_assert_nextline("Showing bootflows for bootdev 'mmc1.bootdev'");
+	ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("  0  syslinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(1 bootflow, 1 valid)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_cmd, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootflow scan' with a name / label / seq */
+static int bootflow_cmd_label(struct unit_test_state *uts)
+{
+	console_record_reset_enable();
+	ut_assertok(run_command("bootflow scan -l mmc1", 0));
+	ut_assert_nextline("Scanning for bootflows in bootdev 'mmc1.bootdev'");
+	ut_assert_skip_to_line("(1 bootflow, 1 valid)");
+	ut_assert_console_end();
+
+	ut_assertok(run_command("bootflow scan -l mmc0.bootdev", 0));
+	ut_assert_nextline("Scanning for bootflows in bootdev 'mmc0.bootdev'");
+	ut_assert_skip_to_line("(0 bootflows, 0 valid)");
+	ut_assert_console_end();
+
+	ut_assertok(run_command("bootflow scan -l 0", 0));
+	ut_assert_nextline("Scanning for bootflows in bootdev 'mmc2.bootdev'");
+	ut_assert_skip_to_line("(0 bootflows, 0 valid)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_cmd_label, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootflow scan/list' commands using all bootdevs */
+static int bootflow_cmd_glob(struct unit_test_state *uts)
+{
+	ut_assertok(bootstd_test_drop_bootdev_order(uts));
+
+	console_record_reset_enable();
+	ut_assertok(run_command("bootflow scan -l", 0));
+	ut_assert_nextline("Scanning for bootflows in all bootdevs");
+	ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
+	ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
+	ut_assert_nextline("  0  syslinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
+	ut_assert_nextline("Scanning bootdev 'mmc0.bootdev':");
+	ut_assert_nextline("No more bootdevs");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(1 bootflow, 1 valid)");
+	ut_assert_console_end();
+
+	ut_assertok(run_command("bootflow list", 0));
+	ut_assert_nextline("Showing all bootflows");
+	ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("  0  syslinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(1 bootflow, 1 valid)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_cmd_glob, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootflow scan -e' */
+static int bootflow_cmd_scan_e(struct unit_test_state *uts)
+{
+	ut_assertok(bootstd_test_drop_bootdev_order(uts));
+
+	console_record_reset_enable();
+	ut_assertok(run_command("bootflow scan -ale", 0));
+	ut_assert_nextline("Scanning for bootflows in all bootdevs");
+	ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':");
+	ut_assert_nextline("  0  syslinux     media   mmc          0  mmc2.bootdev.whole        <NULL>");
+	ut_assert_nextline("     ** No partition found, err=-93");
+	ut_assert_nextline("  1  efi          media   mmc          0  mmc2.bootdev.whole        <NULL>");
+	ut_assert_nextline("     ** No partition found, err=-93");
+
+	ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':");
+	ut_assert_nextline("  2  syslinux     media   mmc          0  mmc1.bootdev.whole        <NULL>");
+	ut_assert_nextline("     ** No partition found, err=-2");
+	ut_assert_nextline("  3  efi          media   mmc          0  mmc1.bootdev.whole        <NULL>");
+	ut_assert_nextline("     ** No partition found, err=-2");
+	ut_assert_nextline("  4  syslinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
+	ut_assert_nextline("  5  efi          fs      mmc          1  mmc1.bootdev.part_1       efi/boot/bootsbox.efi");
+
+	ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':");
+	ut_assert_skip_to_line(" 3f  efi          media   mmc          0  mmc0.bootdev.whole        <NULL>");
+	ut_assert_nextline("     ** No partition found, err=-93");
+	ut_assert_nextline("No more bootdevs");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(64 bootflows, 1 valid)");
+	ut_assert_console_end();
+
+	ut_assertok(run_command("bootflow list", 0));
+	ut_assert_nextline("Showing all bootflows");
+	ut_assert_nextline("Seq  Method       State   Uclass    Part  Name                      Filename");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("  0  syslinux     media   mmc          0  mmc2.bootdev.whole        <NULL>");
+	ut_assert_nextline("  1  efi          media   mmc          0  mmc2.bootdev.whole        <NULL>");
+	ut_assert_skip_to_line("  4  syslinux     ready   mmc          1  mmc1.bootdev.part_1       /extlinux/extlinux.conf");
+	ut_assert_skip_to_line(" 3f  efi          media   mmc          0  mmc0.bootdev.whole        <NULL>");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(64 bootflows, 1 valid)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_cmd_scan_e, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootflow info' */
+static int bootflow_cmd_info(struct unit_test_state *uts)
+{
+	console_record_reset_enable();
+	ut_assertok(run_command("bootdev select 1", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootflow scan", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootflow select 0", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootflow info", 0));
+	ut_assert_nextline("Name:      mmc1.bootdev.part_1");
+	ut_assert_nextline("Device:    mmc1.bootdev");
+	ut_assert_nextline("Block dev: mmc1.blk");
+	ut_assert_nextline("Method:    syslinux");
+	ut_assert_nextline("State:     ready");
+	ut_assert_nextline("Partition: 1");
+	ut_assert_nextline("Subdir:    (none)");
+	ut_assert_nextline("Filename:  /extlinux/extlinux.conf");
+	ut_assert_nextlinen("Buffer:    ");
+	ut_assert_nextline("Size:      253 (595 bytes)");
+	ut_assert_nextline("Error:     0");
+	ut_assert_console_end();
+
+	ut_assertok(run_command("bootflow info -d", 0));
+	ut_assert_nextline("Name:      mmc1.bootdev.part_1");
+	ut_assert_skip_to_line("Error:     0");
+	ut_assert_nextline("Contents:");
+	ut_assert_nextline("%s", "");
+	ut_assert_nextline("# extlinux.conf generated by appliance-creator");
+	ut_assert_skip_to_line("        initrd /initramfs-5.3.7-301.fc31.armv7hl.img");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_cmd_info, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootflow scan -b' to boot the first available bootdev */
+static int bootflow_scan_boot(struct unit_test_state *uts)
+{
+	console_record_reset_enable();
+	ut_assertok(run_command("bootflow scan -b", 0));
+	ut_assert_nextline("** Booting bootflow 'mmc1.bootdev.part_1'");
+	ut_assert_nextline("Ignoring unknown command: ui");
+
+	/*
+	 * We expect it to get through to boot although sandbox always returns
+	 * -EFAULT as it cannot actually boot the kernel
+	 */
+	ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
+	ut_assert_nextline("Boot failed (err=-14)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_scan_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check iterating through available bootflows */
+static int bootflow_iter(struct unit_test_state *uts)
+{
+	struct bootflow_iter iter;
+	struct bootflow bflow;
+
+	bootstd_clear_glob();
+
+	/* The first device is mmc2.bootdev which has no media */
+	ut_asserteq(-EPROTONOSUPPORT,
+		    bootflow_scan_first(&iter, BOOTFLOWF_ALL, &bflow));
+	ut_asserteq(2, iter.num_methods);
+	ut_asserteq(0, iter.cur_method);
+	ut_asserteq(0, iter.part);
+	ut_asserteq(0, iter.max_part);
+	ut_asserteq_str("syslinux", iter.method->name);
+	ut_asserteq(0, bflow.err);
+
+	/*
+	 * This shows MEDIA even though there is none, since int
+	 * bootdev_find_in_blk() we call part_get_info() which returns
+	 * -EPROTONOSUPPORT. Ideally it would return -EEOPNOTSUPP and we would
+	 * know.
+	 */
+	ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
+
+	ut_asserteq(-EPROTONOSUPPORT, bootflow_scan_next(&iter, &bflow));
+	ut_asserteq(2, iter.num_methods);
+	ut_asserteq(1, iter.cur_method);
+	ut_asserteq(0, iter.part);
+	ut_asserteq(0, iter.max_part);
+	ut_asserteq_str("efi", iter.method->name);
+	ut_asserteq(0, bflow.err);
+	ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
+	bootflow_free(&bflow);
+
+	/* The next device is mmc1.bootdev - at first we use the whole device */
+	ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
+	ut_asserteq(2, iter.num_methods);
+	ut_asserteq(0, iter.cur_method);
+	ut_asserteq(0, iter.part);
+	ut_asserteq(0x1e, iter.max_part);
+	ut_asserteq_str("syslinux", iter.method->name);
+	ut_asserteq(0, bflow.err);
+	ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
+	bootflow_free(&bflow);
+
+	ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
+	ut_asserteq(2, iter.num_methods);
+	ut_asserteq(1, iter.cur_method);
+	ut_asserteq(0, iter.part);
+	ut_asserteq(0x1e, iter.max_part);
+	ut_asserteq_str("efi", iter.method->name);
+	ut_asserteq(0, bflow.err);
+	ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
+	bootflow_free(&bflow);
+
+	/* Then more to partition 1 where we find something */
+	ut_assertok(bootflow_scan_next(&iter, &bflow));
+	ut_asserteq(2, iter.num_methods);
+	ut_asserteq(0, iter.cur_method);
+	ut_asserteq(1, iter.part);
+	ut_asserteq(0x1e, iter.max_part);
+	ut_asserteq_str("syslinux", iter.method->name);
+	ut_asserteq(0, bflow.err);
+	ut_asserteq(BOOTFLOWST_READY, bflow.state);
+	bootflow_free(&bflow);
+
+	ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
+	ut_asserteq(2, iter.num_methods);
+	ut_asserteq(1, iter.cur_method);
+	ut_asserteq(1, iter.part);
+	ut_asserteq(0x1e, iter.max_part);
+	ut_asserteq_str("efi", iter.method->name);
+	ut_asserteq(0, bflow.err);
+	ut_asserteq(BOOTFLOWST_FS, bflow.state);
+	bootflow_free(&bflow);
+
+	/* Then more to partition 2 which doesn't exist */
+	ut_asserteq(-ENOENT, bootflow_scan_next(&iter, &bflow));
+	ut_asserteq(2, iter.num_methods);
+	ut_asserteq(0, iter.cur_method);
+	ut_asserteq(2, iter.part);
+	ut_asserteq(0x1e, iter.max_part);
+	ut_asserteq_str("syslinux", iter.method->name);
+	ut_asserteq(0, bflow.err);
+	ut_asserteq(BOOTFLOWST_MEDIA, bflow.state);
+	bootflow_free(&bflow);
+
+	bootflow_iter_uninit(&iter);
+
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_iter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check using the system bootdev */
+static int bootflow_system(struct unit_test_state *uts)
+{
+	struct udevice *bootstd, *dev;
+
+	/* Add the EFI bootmgr driver */
+	ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
+	ut_assertok(device_bind_driver(bootstd, "bootmeth_efi_mgr", "bootmgr",
+				       &dev));
+
+	/* Add the system bootdev that it uses */
+	ut_assertok(device_bind_driver(bootstd, "system_bootdev",
+				       "system-bootdev", &dev));
+
+	ut_assertok(bootstd_test_drop_bootdev_order(uts));
+
+	/* We should get a single 'bootmgr' method right at the end */
+	bootstd_clear_glob();
+	console_record_reset_enable();
+	ut_assertok(run_command("bootflow scan -l", 0));
+	ut_assert_skip_to_line("  1  bootmgr      ready   bootstd      0  <NULL>                    <NULL>");
+	ut_assert_nextline("No more bootdevs");
+	ut_assert_skip_to_line("(2 bootflows, 2 valid)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_system, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check disabling a bootmethod if it requests it */
+static int bootflow_iter_disable(struct unit_test_state *uts)
+{
+	struct udevice *bootstd, *dev;
+	struct bootflow_iter iter;
+	struct bootflow bflow;
+	int i;
+
+	/* Add the EFI bootmgr driver */
+	ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
+	ut_assertok(device_bind_driver(bootstd, "bootmeth_sandbox", "sandbox",
+				       &dev));
+
+	/* Add the system bootdev that it uses */
+	ut_assertok(device_bind_driver(bootstd, "system_bootdev",
+				       "system-bootdev", &dev));
+
+	ut_assertok(bootstd_test_drop_bootdev_order(uts));
+
+	bootstd_clear_glob();
+	ut_assertok(run_command("bootflow scan -lb", 0));
+
+	/* Try to boot the bootmgr flow, which will fail */
+	console_record_reset_enable();
+	ut_assertok(bootflow_scan_first(&iter, 0, &bflow));
+	ut_asserteq(3, iter.num_methods);
+	ut_asserteq_str("sandbox", iter.method->name);
+	ut_asserteq(-ENOTSUPP, bootflow_run_boot(&iter, &bflow));
+
+	ut_assert_skip_to_line("Boot method 'sandbox' failed and will not be retried");
+	ut_assert_console_end();
+
+	/* Check that the sandbox bootmeth has been removed */
+	ut_asserteq(2, iter.num_methods);
+	for (i = 0; i < iter.num_methods; i++)
+		ut_assert(strcmp("sandbox", iter.method_order[i]->name));
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_iter_disable, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootflow boot' to boot a selected bootflow */
+static int bootflow_cmd_boot(struct unit_test_state *uts)
+{
+	console_record_reset_enable();
+	ut_assertok(run_command("bootdev select 1", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootflow scan", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootflow select 0", 0));
+	ut_assert_console_end();
+	ut_asserteq(1, run_command("bootflow boot", 0));
+	ut_assert_nextline("** Booting bootflow 'mmc1.bootdev.part_1'");
+	ut_assert_nextline("Ignoring unknown command: ui");
+
+	/*
+	 * We expect it to get through to boot although sandbox always returns
+	 * -EFAULT as it cannot actually boot the kernel
+	 */
+	ut_assert_skip_to_line("sandbox: continuing, as we cannot run Linux");
+	ut_assert_nextline("Boot failed (err=-14)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
diff --git a/test/boot/bootmeth.c b/test/boot/bootmeth.c
new file mode 100644
index 00000000000..955eea2426a
--- /dev/null
+++ b/test/boot/bootmeth.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for bootdev functions. All start with 'bootdev'
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <bootstd.h>
+#include <test/suites.h>
+#include <test/ut.h>
+#include "bootstd_common.h"
+
+/* Check 'bootmeth list' command */
+static int bootmeth_cmd_list(struct unit_test_state *uts)
+{
+	console_record_reset_enable();
+	ut_assertok(run_command("bootmeth list", 0));
+	ut_assert_nextline("Order  Seq  Name                Description");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("    0    0  syslinux            Syslinux boot from a block device");
+	ut_assert_nextline("    1    1  efi                 EFI boot from a .efi file");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(2 bootmeths)");
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootmeth_cmd_list, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootmeth order' command */
+static int bootmeth_cmd_order(struct unit_test_state *uts)
+{
+	/* Select just one bootmethod */
+	console_record_reset_enable();
+	ut_assertok(run_command("bootmeth order syslinux", 0));
+	ut_assert_console_end();
+	ut_assertnonnull(env_get("bootmeths"));
+	ut_asserteq_str("syslinux", env_get("bootmeths"));
+
+	/* Only that one should be listed */
+	ut_assertok(run_command("bootmeth list", 0));
+	ut_assert_nextline("Order  Seq  Name                Description");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("    0    0  syslinux            Syslinux boot from a block device");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(1 bootmeth)");
+	ut_assert_console_end();
+
+	/* Check the -a flag, efi should show as not in the order ("-") */
+	ut_assertok(run_command("bootmeth list -a", 0));
+	ut_assert_nextline("Order  Seq  Name                Description");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("    0    0  syslinux            Syslinux boot from a block device");
+	ut_assert_nextline("    -    1  efi                 EFI boot from a .efi file");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(2 bootmeths)");
+	ut_assert_console_end();
+
+	/* Check the -a flag with the reverse order */
+	ut_assertok(run_command("bootmeth order \"efi syslinux\"", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootmeth list -a", 0));
+	ut_assert_nextline("Order  Seq  Name                Description");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("    1    0  syslinux            Syslinux boot from a block device");
+	ut_assert_nextline("    0    1  efi                 EFI boot from a .efi file");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(2 bootmeths)");
+	ut_assert_console_end();
+
+	/* Now reset the order to empty, which should show all of them again */
+	ut_assertok(run_command("bootmeth order", 0));
+	ut_assert_console_end();
+	ut_assertnull(env_get("bootmeths"));
+	ut_assertok(run_command("bootmeth list", 0));
+	ut_assert_skip_to_line("(2 bootmeths)");
+
+	/* Try reverse order */
+	ut_assertok(run_command("bootmeth order \"efi syslinux\"", 0));
+	ut_assert_console_end();
+	ut_assertok(run_command("bootmeth list", 0));
+	ut_assert_nextline("Order  Seq  Name                Description");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("    0    1  efi                 EFI boot from a .efi file");
+	ut_assert_nextline("    1    0  syslinux            Syslinux boot from a block device");
+	ut_assert_nextlinen("---");
+	ut_assert_nextline("(2 bootmeths)");
+	ut_assertnonnull(env_get("bootmeths"));
+	ut_asserteq_str("efi syslinux", env_get("bootmeths"));
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootmeth_cmd_order, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
+
+/* Check 'bootmeths' env var */
+static int bootmeth_env(struct unit_test_state *uts)
+{
+	struct bootstd_priv *std;
+
+	ut_assertok(bootstd_get_priv(&std));
+
+	/* Select just one bootmethod */
+	console_record_reset_enable();
+	ut_assertok(env_set("bootmeths", "syslinux"));
+	ut_asserteq(1, std->bootmeth_count);
+
+	/* Select an invalid bootmethod */
+	ut_asserteq(1, run_command("setenv bootmeths fred", 0));
+	ut_assert_nextline("Unknown bootmeth 'fred'");
+	ut_assert_nextlinen("## Error inserting");
+	ut_assert_console_end();
+
+	ut_assertok(env_set("bootmeths", "efi syslinux"));
+	ut_asserteq(2, std->bootmeth_count);
+	ut_assert_console_end();
+
+	return 0;
+}
+BOOTSTD_TEST(bootmeth_env, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
diff --git a/test/boot/bootstd_common.c b/test/boot/bootstd_common.c
new file mode 100644
index 00000000000..05347d87106
--- /dev/null
+++ b/test/boot/bootstd_common.c
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for bootdev functions. All start with 'bootdev'
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <bootstd.h>
+#include <dm.h>
+#include <test/suites.h>
+#include <test/ut.h>
+#include "bootstd_common.h"
+
+int bootstd_test_drop_bootdev_order(struct unit_test_state *uts)
+{
+	struct bootstd_priv *priv;
+	struct udevice *bootstd;
+
+	ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd));
+	priv = dev_get_priv(bootstd);
+	priv->bootdev_order = NULL;
+
+	return 0;
+}
+
+int do_ut_bootstd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct unit_test *tests = UNIT_TEST_SUITE_START(bootstd_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(bootstd_test);
+
+	return cmd_ut_category("bootstd", "bootstd_test_",
+			       tests, n_ents, argc, argv);
+}
diff --git a/test/boot/bootstd_common.h b/test/boot/bootstd_common.h
new file mode 100644
index 00000000000..676ef0a57f9
--- /dev/null
+++ b/test/boot/bootstd_common.h
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Common header file for bootdev, bootflow, bootmeth tests
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#ifndef __bootstd_common_h
+#define __bootstd_common_h
+
+/* Declare a new bootdev test */
+#define BOOTSTD_TEST(_name, _flags) \
+		UNIT_TEST(_name, _flags, bootstd_test)
+
+struct unit_test_state;
+
+/**
+ * bootstd_test_drop_bootdev_order() - Remove the existing boot order
+ *
+ * Drop the boot order so that all bootdevs are used in their alias order
+ *
+ * @uts: Unit test state to use for ut_assert...() functions
+ */
+int bootstd_test_drop_bootdev_order(struct unit_test_state *uts);
+
+#endif
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index 90b260f72d6..67a13ee32b8 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -28,6 +28,10 @@ int cmd_ut_category(const char *name, const char *prefix,
 
 static struct cmd_tbl cmd_ut_sub[] = {
 	U_BOOT_CMD_MKENT(all, CONFIG_SYS_MAXARGS, 1, do_ut_all, "", ""),
+#ifdef CONFIG_BOOTSTD
+	U_BOOT_CMD_MKENT(bootstd, CONFIG_SYS_MAXARGS, 1, do_ut_bootstd,
+			 "", ""),
+#endif
 	U_BOOT_CMD_MKENT(common, CONFIG_SYS_MAXARGS, 1, do_ut_common, "", ""),
 #if defined(CONFIG_UT_DM)
 	U_BOOT_CMD_MKENT(dm, CONFIG_SYS_MAXARGS, 1, do_ut_dm, "", ""),
@@ -115,6 +119,9 @@ static char ut_help_text[] =
 	"ut bloblist - Test bloblist implementation\n"
 	"ut compression - Test compressors and bootm decompression\n"
 #endif
+#ifdef CONFIG_BOOTSTD
+	"ut bootstd - Test standard boot implementation\n"
+#endif
 #ifdef CONFIG_UT_DM
 	"ut dm [test-name]\n"
 #endif
-- 
2.34.1.703.g22d0c6ccf7-goog



More information about the U-Boot mailing list