futility: updater: Add '--servo_port' to select from multiple servods

When multiple servo boards are connected to the host, we usually want to
select the right servo by specifying its port, get the servo serial
number, and pass that to flashrom programmer.

The new --servo_port (or environment variable SERVOD_PORT) now allows
developers to flash firmware via specific servod using futility updater
easily:

 futility update --servo_port 9998 -i PATH_TO/image.bin

BRANCH=None
BUG=None
TEST=make runtest; sudo futility update --servo_port 9998 -i image.bin

Change-Id: Ic302f841abf745801995ff233fc209726ed039c8
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2228258
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-by: Joel Kitching <kitching@chromium.org>
Commit-Queue: Douglas Anderson <dianders@chromium.org>
diff --git a/futility/cmd_update.c b/futility/cmd_update.c
index e83095f..db29db2 100644
--- a/futility/cmd_update.c
+++ b/futility/cmd_update.c
@@ -7,6 +7,7 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <getopt.h>
 
 #include "futility.h"
@@ -29,6 +30,7 @@
 	OPT_QUIRKS_LIST,
 	OPT_REPACK,
 	OPT_SERVO,
+	OPT_SERVO_PORT,
 	OPT_SIGNATURE,
 	OPT_SYS_PROPS,
 	OPT_UNPACK,
@@ -51,6 +53,7 @@
 
 	{"ccd", 0, NULL, OPT_CCD},
 	{"servo", 0, NULL, OPT_SERVO},
+	{"servo_port", 1, NULL, OPT_SERVO_PORT},
 	{"emulate", 1, NULL, OPT_EMULATE},
 	{"factory", 0, NULL, OPT_FACTORY},
 	{"fast", 0, NULL, OPT_FAST},
@@ -113,6 +116,7 @@
 		"    --model=MODEL   \tOverride system model for images\n"
 		"    --ccd           \tDo fast,force,wp=0,p=raiden_debug_spi\n"
 		"    --servo         \tFlash using Servo (v2, v4, micro, ...)\n"
+		"    --servo_port=PRT\tOverride servod port, implies --servo\n"
 		"    --signature_id=S\tOverride signature ID for key files\n"
 		"    --sys_props=LIST\tList of system properties to override\n"
 		"-d, --debug         \tPrint debugging messages\n"
@@ -127,6 +131,7 @@
 	struct updater_config_arguments args = {0};
 	int i, errorcnt = 0, do_update = 1;
 	int detect_servo = 0, do_servo_cpu_fw_spi = 0;
+	char *servo_programmer = NULL;
 
 	cfg = updater_new_config();
 	assert(cfg);
@@ -226,6 +231,14 @@
 			args.host_only = 1;
 			detect_servo = 1;
 			break;
+		case OPT_SERVO_PORT:
+			setenv(ENV_SERVOD_PORT, optarg, 1);
+			args.fast_update = 1;
+			args.force_update = 1;
+			args.write_protection = "0";
+			args.host_only = 1;
+			detect_servo = 1;
+			break;
 
 		case OPT_DUMMY:
 			break;
@@ -250,9 +263,13 @@
 		ERROR("Unexpected arguments.\n");
 	}
 
-	if (!errorcnt && detect_servo)
-		errorcnt += host_detect_servo(&args.programmer,
-					      &do_servo_cpu_fw_spi);
+	if (!errorcnt && detect_servo) {
+		servo_programmer = host_detect_servo(&do_servo_cpu_fw_spi);
+		if (!servo_programmer)
+			errorcnt++;
+		else if (!args.programmer)
+			args.programmer = servo_programmer;
+	}
 	/*
 	 * Some boards may need to fetch firmware before starting to
 	 * update (i.e., in updater_setup_config) so we want to turn on
@@ -280,6 +297,7 @@
 
 	if (do_servo_cpu_fw_spi)
 		free(host_shell("dut-control cpu_fw_spi:off"));
+	free(servo_programmer);
 
 	updater_delete_config(cfg);
 	return !!errorcnt;
diff --git a/futility/updater_utils.c b/futility/updater_utils.c
index 75f5c0f..6e2d358 100644
--- a/futility/updater_utils.c
+++ b/futility/updater_utils.c
@@ -451,20 +451,37 @@
 }
 
 /*
- * Helper function to detect type of Servo board attached to host,
- * and store the right programmer / prepare settings to arguments.
- * Returns 0 if success, non-zero if error.
+ * Helper function to detect type of Servo board attached to host.
+ * Returns a string as programmer parameter on success, otherwise NULL.
  */
-int host_detect_servo(const char **programmer_ptr, int *need_prepare_ptr)
+char *host_detect_servo(int *need_prepare_ptr)
 {
-	int ret = 0;
+	const char *servo_port = getenv(ENV_SERVOD_PORT);
 	char *servo_type = host_shell("dut-control -o servo_type 2>/dev/null");
 	const char *programmer = NULL;
+	char *ret = NULL;
 	int need_prepare = 0;  /* To prepare by dut-control cpu_fw_spi:on */
+	char *servo_serial = NULL;
+
+	/* Get serial name if servo port is provided. */
+	if (servo_port && *servo_port) {
+		const char *cmd = "dut-control -o serialname 2>/dev/null";
+
+		VB2_DEBUG("Select servod using port: %s\n", servo_port);
+		if (strstr(servo_type, "with_servo_micro"))
+			cmd = ("dut-control -o servo_micro_serialname"
+			       " 2>/dev/null");
+		else if (strstr(servo_type, "with_ccd"))
+			cmd = "dut-control -o ccd_serialname 2>/dev/null";
+
+		servo_serial = host_shell(cmd);
+		VB2_DEBUG("Servo SN=%s (serial cmd: %s)\n", servo_serial, cmd);
+	}
 
 	if (!*servo_type) {
 		ERROR("Failed to get servo type. Check servod.\n");
-		ret = 1;
+	} else if (servo_serial && !*servo_serial) {
+		ERROR("Failed to get serial at servo port %s.\n", servo_port);
 	} else if (strstr(servo_type, "servo_micro")) {
 		VB2_DEBUG("Selected Servo Micro.\n");
 		programmer = "raiden_debug_spi";
@@ -478,9 +495,19 @@
 		need_prepare = 1;
 	}
 
+	if (programmer) {
+		if (!servo_serial) {
+			ret = strdup(programmer);
+		} else {
+			const char prefix = strchr(programmer, ':') ? ',' : ':';
+			ASPRINTF(&ret, "%s%cserial=%s", programmer, prefix,
+				 servo_serial);
+		}
+		VB2_DEBUG("Servo programmer: %s\n", ret);
+	}
+
 	free(servo_type);
-	if (programmer && !*programmer_ptr)
-		*programmer_ptr = programmer;
+	free(servo_serial);
 	*need_prepare_ptr = need_prepare;
 
 	return ret;
diff --git a/futility/updater_utils.h b/futility/updater_utils.h
index eb8f62b..2531675 100644
--- a/futility/updater_utils.h
+++ b/futility/updater_utils.h
@@ -177,12 +177,14 @@
 /* Helper function to return write protection status via given programmer. */
 enum wp_state host_get_wp(const char *programmer);
 
+/* The environment variable name for setting servod port. */
+#define ENV_SERVOD_PORT	"SERVOD_PORT"
+
 /*
- * Helper function to detect type of Servo board attached to host,
- * and store the right programmer / prepare settings to arguments.
- * Returns 0 if success, non-zero if error.
+ * Helper function to detect type of Servo board attached to host.
+ * Returns a string as programmer parameter on success, otherwise NULL.
  */
-int host_detect_servo(const char **programmer_ptr, int *need_prepare_ptr);
+char *host_detect_servo(int *need_prepare_ptr);
 
 /*
  * Returns 1 if a given file (cbfs_entry_name) exists inside a particular CBFS