ANDROID: Automatically perform A/B slot-selection in boot_android.

In order to perform A/B flow as accurately as possible, it should be
triggered from within the boot_android command rather than externally.
This patch makes the slot argument to boot_android optional, and if it
is unspecified, it will be determined automatically via
ab_select_slot().

Bug: 141272741
Test: cuttlefish boots
Change-Id: I5ccafc9fd30d78a794d6d35a2cdd0420f22580d3

Link: https://android-review.googlesource.com/c/platform/external/u-boot/+/1133070/2
Bug: https://baylibre.atlassian.net/browse/RITA-97
Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
diff --git a/cmd/boot_android.c b/cmd/boot_android.c
index cf7e796..7982c34 100644
--- a/cmd/boot_android.c
+++ b/cmd/boot_android.c
@@ -18,8 +18,9 @@
 	disk_partition_t part_info;
 	const char *misc_part_iface;
 	const char *misc_part_desc;
+	const char *slot = NULL;
 
-	if (argc < 4)
+	if (argc < 3)
 		return CMD_RET_USAGE;
 	if (argc > 5)
 		return CMD_RET_USAGE;
@@ -35,6 +36,8 @@
 		else
 			load_address = CONFIG_SYS_LOAD_ADDR;
 	}
+	if (argc >= 4)
+		slot = argv[3];
 
 	/* Lookup the "misc" partition from argv[1] and argv[2] */
 	misc_part_iface = argv[1];
@@ -45,7 +48,7 @@
 						 &dev_desc, &part_info) < 0)
 		return CMD_RET_FAILURE;
 
-	ret = android_bootloader_boot_flow(dev_desc, &part_info, argv[3],
+	ret = android_bootloader_boot_flow(dev_desc, &part_info, slot,
 					   load_address);
 	if (ret < 0) {
 		printf("Android boot failed, error %d.\n", ret);
@@ -57,7 +60,7 @@
 U_BOOT_CMD(
 	boot_android, 5, 0, do_boot_android,
 	"Execute the Android Bootloader flow.",
-	"<interface> <dev[:part|;part_name]> <slot> [<kernel_addr>]\n"
+	"<interface> <dev[:part|;part_name]> [<slot>] [<kernel_addr>]\n"
 	"    - Load the Boot Control Block (BCB) from the partition 'part' on\n"
 	"      device type 'interface' instance 'dev' to determine the boot\n"
 	"      mode, and load and execute the appropriate kernel.\n"
diff --git a/common/android_bootloader.c b/common/android_bootloader.c
index 0cb5cf0..4334079 100644
--- a/common/android_bootloader.c
+++ b/common/android_bootloader.c
@@ -7,8 +7,10 @@
 #include <android_bootloader.h>
 #include <android_bootloader_message.h>
 
+#include <android_ab.h>
 #include <cli.h>
 #include <common.h>
+#include <log.h>
 #include <malloc.h>
 
 #define ANDROID_PARTITION_BOOT "boot"
@@ -312,11 +314,27 @@
 		return android_bootloader_boot_bootloader();
 	}
 
+	/* Look for an optional slot suffix override. */
+	if (!slot || !slot[0])
+		slot = env_get("android_slot_suffix");
+
 	slot_suffix[0] = '\0';
 	if (slot && slot[0]) {
 		slot_suffix[0] = '_';
 		slot_suffix[1] = slot[0];
 		slot_suffix[2] = '\0';
+	} else {
+#ifdef CONFIG_ANDROID_AB
+		bool normal_boot = (mode == ANDROID_BOOT_MODE_NORMAL);
+		int slot_num = ab_select_slot(dev_desc, misc_part_info, normal_boot);
+		if (slot_num < 0) {
+			log_err("Could not determine Android boot slot.\n");
+			return -1;
+		}
+		slot_suffix[0] = '_';
+		slot_suffix[1] = BOOT_SLOT_NAME(slot_num);
+		slot_suffix[2] = '\0';
+#endif
 	}
 
 	/* Load the kernel from the desired "boot" partition. */