bootstd: Add a command to read all files for a bootflow

Some bootflows (such as EFI and ChromiumOS) delay reading the kernel until
it is needed to boot. This saves time when scanning and avoids needing to
allocate memory for something that may never be used.

To permit reading of these files, add a new 'bootflow read' command.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/boot/bootflow.c b/boot/bootflow.c
index daf862f..6ef62e1 100644
--- a/boot/bootflow.c
+++ b/boot/bootflow.c
@@ -445,6 +445,22 @@
 	free(bflow);
 }
 
+#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
+int bootflow_read_all(struct bootflow *bflow)
+{
+	int ret;
+
+	if (bflow->state != BOOTFLOWST_READY)
+		return log_msg_ret("rd", -EPROTO);
+
+	ret = bootmeth_read_all(bflow->method, bflow);
+	if (ret)
+		return log_msg_ret("rd2", ret);
+
+	return 0;
+}
+#endif /* BOOTSTD_FULL */
+
 int bootflow_boot(struct bootflow *bflow)
 {
 	int ret;
diff --git a/boot/bootmeth-uclass.c b/boot/bootmeth-uclass.c
index 175eb1d..1d157d5 100644
--- a/boot/bootmeth-uclass.c
+++ b/boot/bootmeth-uclass.c
@@ -61,6 +61,18 @@
 	return ops->set_bootflow(dev, bflow, buf, size);
 }
 
+#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
+int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow)
+{
+	const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
+
+	if (!ops->read_all)
+		return -ENOSYS;
+
+	return ops->read_all(dev, bflow);
+}
+#endif /* BOOTSTD_FULL */
+
 int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
 {
 	const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c
index 06709dd..6c28feb 100644
--- a/boot/bootmeth_cros.c
+++ b/boot/bootmeth_cros.c
@@ -395,13 +395,30 @@
 	return -ENOSYS;
 }
 
+#if CONFIG_IS_ENABLED(BOOSTD_FULL)
+static int cros_read_all(struct udevice *dev, struct bootflow *bflow)
+{
+	int ret;
+
+	if (bflow->buf)
+		return log_msg_ret("ld", -EALREADY);
+	ret = cros_read_kernel(bflow);
+	if (ret)
+		return log_msg_ret("rd", ret);
+
+	return 0;
+}
+#endif /* BOOSTD_FULL */
+
 static int cros_boot(struct udevice *dev, struct bootflow *bflow)
 {
 	int ret;
 
-	ret = cros_read_kernel(bflow);
-	if (ret)
-		return log_msg_ret("rd", ret);
+	if (!bflow->buf) {
+		ret = cros_read_kernel(bflow);
+		if (ret)
+			return log_msg_ret("rd", ret);
+	}
 #ifdef CONFIG_X86
 	zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0,
 		    map_to_sysmem(bflow->x86_setup),
@@ -425,6 +442,9 @@
 	.read_bootflow	= cros_read_bootflow,
 	.read_file	= cros_read_file,
 	.boot		= cros_boot,
+#if CONFIG_IS_ENABLED(BOOSTD_FULL)
+	.read_all	= cros_read_all,
+#endif /* BOOSTD_FULL */
 };
 
 static const struct udevice_id cros_bootmeth_ids[] = {
diff --git a/cmd/bootflow.c b/cmd/bootflow.c
index 9562832..3c3abaf 100644
--- a/cmd/bootflow.c
+++ b/cmd/bootflow.c
@@ -379,6 +379,35 @@
 	return 0;
 }
 
+static int do_bootflow_read(struct cmd_tbl *cmdtp, int flag, int argc,
+			    char *const argv[])
+{
+	struct bootstd_priv *std;
+	struct bootflow *bflow;
+	int ret;
+
+	ret = bootstd_get_priv(&std);
+	if (ret)
+		return CMD_RET_FAILURE;
+
+	/*
+	 * Require a current bootflow. Users can use 'bootflow scan -b' to
+	 * automatically scan and boot, if needed.
+	 */
+	if (!std->cur_bootflow) {
+		printf("No bootflow selected\n");
+		return CMD_RET_FAILURE;
+	}
+	bflow = std->cur_bootflow;
+	ret = bootflow_read_all(bflow);
+	if (ret) {
+		printf("Failed: err=%dE\n", ret);
+		return CMD_RET_FAILURE;
+	}
+
+	return 0;
+}
+
 static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc,
 			    char *const argv[])
 {
@@ -519,7 +548,8 @@
 	"bootflow list [-e]             - list scanned bootflows (-e errors)\n"
 	"bootflow select [<num>|<name>] - select a bootflow\n"
 	"bootflow info [-ds]            - show info on current bootflow (-d dump bootflow)\n"
-	"bootflow boot                  - boot current bootflow (or first available if none selected)\n"
+	"bootflow read                  - read all current-bootflow files\n"
+	"bootflow boot                  - boot current bootflow\n"
 	"bootflow menu [-t]             - show a menu of available bootflows\n"
 	"bootflow cmdline [set|get|clear|delete|auto] <param> [<value>] - update cmdline";
 #else
@@ -533,6 +563,7 @@
 	U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list),
 	U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select),
 	U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info),
+	U_BOOT_SUBCMD_MKENT(read, 1, 1, do_bootflow_read),
 	U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot),
 	U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu),
 	U_BOOT_SUBCMD_MKENT(cmdline, 4, 1, do_bootflow_cmdline),
diff --git a/doc/usage/cmd/bootflow.rst b/doc/usage/cmd/bootflow.rst
index d53f837..ead493d 100644
--- a/doc/usage/cmd/bootflow.rst
+++ b/doc/usage/cmd/bootflow.rst
@@ -12,6 +12,7 @@
     bootflow list [-e]
     bootflow select [<num|name>]
     bootflow info [-ds]
+    bootflow read
     bootflow boot
     bootflow cmdline [set|get|clear|delete|auto] <param> [<value>]
 
@@ -194,10 +195,26 @@
 The `-s` flag shows any x86 setup block, instead of the above.
 
 
+bootflow read
+~~~~~~~~~~~~~
+
+This reads any files related to the bootflow. Some bootflows with large files
+avoid doing this when the bootflow is scanned, since it uses a lot of memory
+and takes extra time. The files are then automatically read when `bootflow boot`
+is used.
+
+This command reads these files immediately. Typically this fills in the bootflow
+`buf` property, which can be used to examine the bootflow.
+
+Note that reading the files does not result in any extra parsing, nor loading of
+images in the files. This is purely used to read in the data ready for
+booting, or examination.
+
+
 bootflow boot
 ~~~~~~~~~~~~~
 
-This boots the current bootflow.
+This boots the current bootflow, reading any required files first.
 
 
 bootflow cmdline
@@ -580,6 +597,67 @@
     Init size           : 1383000
     Handover offset     : 0
 
+This shows reading a bootflow to examine the kernel::
+
+    => bootfl i 0
+    Name:
+    Device:    emmc@1c,0.bootdev
+    Block dev: emmc@1c,0.blk
+    Method:    cros
+    State:     ready
+    Partition: 2
+    Subdir:    (none)
+    Filename:  <NULL>
+    Buffer:    0
+    Size:      63ee00 (6548992 bytes)
+    OS:        ChromeOS
+    Cmdline:   console= loglevel=7 init=/sbin/init cros_secure oops=panic panic=-1 root=PARTUUID=35c775e7-3735-d745-93e5-d9e0238f7ed0/PARTNROFF=1 rootwait rw dm_verity.error_behavior=3 dm_verity.max_bios=-1 dm_verity.dev_wait=0 dm="1 vroot none rw 1,0 3788800 verity payload=ROOT_DEV hashtree=HASH_DEV hashstart=3788800 alg=sha1 root_hexdigest=55052b629d3ac889f25a9583ea12cdcd3ea15ff8 salt=a2d4d9e574069f4fed5e3961b99054b7a4905414b60a25d89974a7334021165c" noinitrd vt.global_cursor_default=0 kern_guid=35c775e7-3735-d745-93e5-d9e0238f7ed0 add_efi_memmap boot=local noresume noswap i915.modeset=1 tpm_tis.force=1 tpm_tis.interrupts=0 nmi_watchdog=panic,lapic disablevmx=off
+    X86 setup: 77b56010
+    Logo:      (none)
+    FDT:       <NULL>
+    Error:     0
+
+Note that `Buffer` is 0 so it has not be read yet. Using `bootflow read`::
+
+    => bootfl read
+    => bootfl info
+    Name:
+    Device:    emmc@1c,0.bootdev
+    Block dev: emmc@1c,0.blk
+    Method:    cros
+    State:     ready
+    Partition: 2
+    Subdir:    (none)
+    Filename:  <NULL>
+    Buffer:    77b7e400
+    Size:      63ee00 (6548992 bytes)
+    OS:        ChromeOS
+    Cmdline:   console= loglevel=7 init=/sbin/init cros_secure oops=panic panic=-1 root=PARTUUID=35c775e7-3735-d745-93e5-d9e0238f7ed0/PARTNROFF=1 rootwait rw dm_verity.error_behavior=3 dm_verity.max_bios=-1 dm_verity.dev_wait=0 dm="1 vroot none rw 1,0 3788800 verity payload=ROOT_DEV hashtree=HASH_DEV hashstart=3788800 alg=sha1 root_hexdigest=55052b629d3ac889f25a9583ea12cdcd3ea15ff8 salt=a2d4d9e574069f4fed5e3961b99054b7a4905414b60a25d89974a7334021165c" noinitrd vt.global_cursor_default=0 kern_guid=35c775e7-3735-d745-93e5-d9e0238f7ed0 add_efi_memmap boot=local noresume noswap i915.modeset=1 tpm_tis.force=1 tpm_tis.interrupts=0 nmi_watchdog=panic,lapic disablevmx=off
+    X86 setup: 781b4400
+    Logo:      (none)
+    FDT:       <NULL>
+    Error:     0
+
+Now the buffer can be accessed::
+
+    => md 77b7e400
+    77b7e400: 1186f6fc 40000002 b8fa0c75 00000018  .......@u.......
+    77b7e410: c08ed88e a68dd08e 000001e8 000000e8  ................
+    77b7e420: ed815d00 00000021 62c280b8 89e80100  .]..!......b....
+    77b7e430: 22f7e8c4 c0850061 22ec850f eb890061  ..."a......"a...
+    77b7e440: 0230868b 01480000 21d0f7c3 00fb81c3  ..0...H....!....
+    77b7e450: 7d010000 0000bb05 c3810100 00d4f000  ...}............
+    77b7e460: 8130858d 85890061 00618132 3095010f  ..0.a...2.a....0
+    77b7e470: 0f006181 c883e020 e0220f20 e000bb8d  .a.. ... .".....
+    77b7e480: c0310062 001800b9 8dabf300 62e000bb  b.1............b
+    77b7e490: 07878d00 89000010 00bb8d07 8d0062f0  .............b..
+    77b7e4a0: 00100787 0004b900 07890000 00100005  ................
+    77b7e4b0: 08c78300 8df37549 630000bb 0183b800  ....Iu.....c....
+    77b7e4c0: 00b90000 89000008 00000507 c7830020  ............ ...
+    77b7e4d0: f3754908 e000838d 220f0062 0080b9d8  .Iu.....b.."....
+    77b7e4e0: 320fc000 08e8ba0f c031300f b8d0000f  ...2.....01.....
+    77b7e4f0: 00000020 6ad8000f 00858d10 50000002   ......j.......P
+
 
 Return value
 ------------
diff --git a/include/bootflow.h b/include/bootflow.h
index fdcfedd..44d3741 100644
--- a/include/bootflow.h
+++ b/include/bootflow.h
@@ -353,6 +353,17 @@
 int bootflow_boot(struct bootflow *bflow);
 
 /**
+ * bootflow_read_all() - Read all bootflow files
+ *
+ * Some bootmeths delay reading of large files until booting is requested. This
+ * causes those files to be read.
+ *
+ * @bflow: Bootflow to read
+ * Return: result of trying to read
+ */
+int bootflow_read_all(struct bootflow *bflow);
+
+/**
  * bootflow_run_boot() - Try to boot a bootflow
  *
  * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
diff --git a/include/bootmeth.h b/include/bootmeth.h
index 7cb7da3..d3d8d60 100644
--- a/include/bootmeth.h
+++ b/include/bootmeth.h
@@ -119,7 +119,16 @@
 	 */
 	int (*read_file)(struct udevice *dev, struct bootflow *bflow,
 			 const char *file_path, ulong addr, ulong *sizep);
-
+#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
+	/**
+	 * readall() - read all files for a bootflow
+	 *
+	 * @dev:	Bootmethod device to boot
+	 * @bflow:	Bootflow to read
+	 * Return: 0 if OK, -EIO on I/O error, other -ve on other error
+	 */
+	int (*read_all)(struct udevice *dev, struct bootflow *bflow);
+#endif /* BOOTSTD_FULL */
 	/**
 	 * boot() - boot a bootflow
 	 *
@@ -224,6 +233,20 @@
 		       const char *file_path, ulong addr, ulong *sizep);
 
 /**
+ * bootmeth_read_all() - read all bootflow files
+ *
+ * Some bootmeths delay reading of large files until booting is requested. This
+ * causes those files to be read.
+ *
+ * @dev:	Bootmethod device to use
+ * @bflow:	Bootflow to read
+ * Return: does not return on success, since it should boot the
+ *	Operating Systemn. Returns -EFAULT if that fails, other -ve on
+ *	other error
+ */
+int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow);
+
+/**
  * bootmeth_boot() - boot a bootflow
  *
  * @dev:	Bootmethod device to boot