bootstd: Remove special-case code for boot_targets

Rather than implement this as its own case in build_order(), process the
boot_targets environment variable in the bootstd_get_bootdev_order()
function. This allows build_order() to be simplified.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/boot/bootdev-uclass.c b/boot/bootdev-uclass.c
index affe0d3..696efb4 100644
--- a/boot/bootdev-uclass.c
+++ b/boot/bootdev-uclass.c
@@ -12,7 +12,6 @@
 #include <bootflow.h>
 #include <bootmeth.h>
 #include <bootstd.h>
-#include <env.h>
 #include <fs.h>
 #include <log.h>
 #include <malloc.h>
@@ -504,34 +503,13 @@
 	const char *overflow_target = NULL;
 	const char *const *labels;
 	struct udevice *dev;
-	const char *targets;
 	int i, ret, count;
+	bool ok;
 
-	targets = env_get("boot_targets");
-	labels = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
-		bootstd_get_bootdev_order(bootstd) : NULL;
-	if (targets) {
-		char str[BOOT_TARGETS_MAX_LEN];
-		char *target;
-
-		if (strlen(targets) >= BOOT_TARGETS_MAX_LEN)
-			return log_msg_ret("len", -E2BIG);
-
-		/* make a copy of the string, since strok() will change it */
-		strcpy(str, targets);
-		for (i = 0, target = strtok(str, " "); target;
-		     target = strtok(NULL, " ")) {
-			ret = bootdev_find_by_label(target, &dev);
-			if (!ret) {
-				if (i == max_count) {
-					overflow_target = target;
-					break;
-				}
-				order[i++] = dev;
-			}
-		}
-		count = i;
-	} else if (labels) {
+	labels = bootstd_get_bootdev_order(bootstd, &ok);
+	if (!ok)
+		return log_msg_ret("ord", -ENOMEM);
+	if (labels) {
 		int upto;
 
 		upto = 0;
diff --git a/boot/bootstd-uclass.c b/boot/bootstd-uclass.c
index 7887acd..81555d3 100644
--- a/boot/bootstd-uclass.c
+++ b/boot/bootstd-uclass.c
@@ -10,6 +10,7 @@
 #include <bootflow.h>
 #include <bootstd.h>
 #include <dm.h>
+#include <env.h>
 #include <log.h>
 #include <malloc.h>
 #include <dm/device-internal.h>
@@ -72,9 +73,23 @@
 	return 0;
 }
 
-const char *const *const bootstd_get_bootdev_order(struct udevice *dev)
+const char *const *const bootstd_get_bootdev_order(struct udevice *dev,
+						   bool *okp)
 {
 	struct bootstd_priv *std = dev_get_priv(dev);
+	const char *targets = env_get("boot_targets");
+
+	*okp = true;
+	log_debug("- targets %s %p\n", targets, std->bootdev_order);
+	if (targets && *targets) {
+		str_free_list(std->env_order);
+		std->env_order = str_to_list(targets);
+		if (!std->env_order) {
+			*okp = false;
+			return NULL;
+		}
+		return std->env_order;
+	}
 
 	return std->bootdev_order;
 }
diff --git a/include/bootstd.h b/include/bootstd.h
index 4fa0d53..bd30509 100644
--- a/include/bootstd.h
+++ b/include/bootstd.h
@@ -22,7 +22,10 @@
  * @prefixes: NULL-terminated list of prefixes to use for bootflow filenames,
  *	e.g. "/", "/boot/"; NULL if none
  * @bootdev_order: Order to use for bootdevs (or NULL if none), with each item
- *	being a bootdev label, e.g. "mmc2", "mmc1";
+ *	being a bootdev label, e.g. "mmc2", "mmc1" (NULL terminated)
+ * @env_order: Order as specified by the boot_targets env var (or NULL if none),
+ *	with each item being a bootdev label, e.g. "mmc2", "mmc1" (NULL
+ *	terminated)
  * @cur_bootdev: Currently selected bootdev (for commands)
  * @cur_bootflow: Currently selected bootflow (for commands)
  * @glob_head: Head for the global list of all bootflows across all bootdevs
@@ -34,6 +37,7 @@
 struct bootstd_priv {
 	const char **prefixes;
 	const char **bootdev_order;
+	const char **env_order;
 	struct udevice *cur_bootdev;
 	struct bootflow *cur_bootflow;
 	struct list_head glob_head;
@@ -51,9 +55,13 @@
  * The list is alloced by the bootstd driver so should not be freed. That is the
  * reason for all the const stuff in the function signature
  *
- * Return: list of string points, terminated by NULL; or NULL if no boot order
+ * @dev: bootstd device
+ * @okp: returns true if OK, false if out of memory
+ * Return: list of string pointers, terminated by NULL; or NULL if no boot
+ * order. Note that this returns NULL in the case of an empty list
  */
-const char *const *const bootstd_get_bootdev_order(struct udevice *dev);
+const char *const *const bootstd_get_bootdev_order(struct udevice *dev,
+						   bool *okp);
 
 /**
  * bootstd_get_prefixes() - Get the filename-prefixes list