futility: updater: Use model name as default whitelabel signature

In Unibuild, the white label models may use (per model) PreMP key for
devices without VPD 'whitelabel_tag' - this helps dogfooders and lab
machines to run and update properly.

BUG=b:126800200
TEST=make futil; tests/futility/run_test_scripts.sh $(pwd)/build/futility
BRANCH=none

Change-Id: I7249e3fb1a2b7ab8ed281d2aa317aee6cde8f8db
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1501614
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
(cherry picked from commit 9c064133217de36332d184e92d20f467967e4e76)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/1978122
Reviewed-by: Paul Ma <magf@bitland.corp-partner.google.com>
Reviewed-by: Marco Chen <marcochen@chromium.org>
Commit-Queue: Marco Chen <marcochen@chromium.org>
Tested-by: Marco Chen <marcochen@chromium.org>
diff --git a/futility/updater_archive.c b/futility/updater_archive.c
index e072de3..3c3dac0 100644
--- a/futility/updater_archive.c
+++ b/futility/updater_archive.c
@@ -839,6 +839,47 @@
 }
 
 /*
+ * Determines the signature ID to use for white label.
+ * Returns the signature ID for looking up rootkey and vblock files.
+ * Caller must free the returned string.
+ */
+static char *resolve_signature_id(struct model_config *model, const char *image)
+{
+	int is_unibuild = model->signature_id ? 1 : 0;
+	char *wl_tag = vpd_get_value(image, VPD_WHITELABEL_TAG);
+	char *sig_id = NULL;
+
+	/* Unified build: $model.$wl_tag, or $model (b/126800200). */
+	if (is_unibuild) {
+		if (!wl_tag) {
+			WARN("No VPD '%s' set for white label - use model name "
+			     "'%s' as default.", VPD_WHITELABEL_TAG,
+			     model->name);
+			return strdup(model->name);
+		}
+
+		ASPRINTF(&sig_id, "%s-%s", model->name, wl_tag);
+		free(wl_tag);
+		return sig_id;
+	}
+
+	/* Non-Unibuild: Upper($wl_tag), or Upper(${cid%%-*}). */
+	if (!wl_tag) {
+		char *cid = vpd_get_value(image, VPD_CUSTOMIZATION_ID);
+		if (cid) {
+			/* customization_id in format LOEM[-VARIANT]. */
+			char *dash = strchr(cid, '-');
+			if (dash)
+				*dash = '\0';
+			wl_tag = cid;
+		}
+	}
+	if (wl_tag)
+		str_convert(wl_tag, toupper);
+	return wl_tag;
+}
+
+/*
  * Applies white label information to an existing model configuration.
  * Collects signature ID information from either parameter signature_id or
  * image file (via VPD) and updates model.patches for key files.
@@ -854,39 +895,19 @@
 	int r = 0;
 
 	if (!signature_id) {
-		int remove_dash = 0, prefix_model = model->signature_id ? 1 : 0;
-		char *wl_tag = vpd_get_value(image, VPD_WHITELABEL_TAG);
-
-		if (!wl_tag) {
-			if (model->signature_id)
-				return -1;
-			wl_tag = vpd_get_value(image, VPD_CUSTOMIZATION_ID);
-			/* customization_id in format LOEM[-VARIANT]. */
-			remove_dash = 1;
-
-		}
-		if (!wl_tag)
-			return 1;
-
-		if (remove_dash) {
-			char *dash = strchr(wl_tag, '-');
-			if (dash)
-				*dash = '\0';
-		}
-		if (!prefix_model)
-			str_convert(wl_tag, toupper);
-
-		sig_id = wl_tag;
-		if (prefix_model)
-			ASPRINTF(&sig_id, "%s-%s", model->name, wl_tag);
-		else
-			wl_tag = NULL;
-		free(wl_tag);
+		sig_id = resolve_signature_id(model, image);
 		signature_id = sig_id;
 	}
 
-	DEBUG("Find white label patches by signature ID: '%s'.", signature_id);
-	find_patches_for_model(model, archive, signature_id);
+	if (signature_id) {
+		DEBUG("Find white label patches by signature ID: '%s'.",
+		      signature_id);
+		find_patches_for_model(model, archive, signature_id);
+	} else {
+		signature_id = "";
+		WARN("No VPD '%s' set for white label - use default keys.",
+		     VPD_WHITELABEL_TAG);
+	}
 	if (!model->patches.rootkey) {
 		ERROR("No keys found for signature_id: '%s'", signature_id);
 		r = 1;
diff --git a/tests/futility/test_update.sh b/tests/futility/test_update.sh
index d33e37a..eb3fe49 100755
--- a/tests/futility/test_update.sh
+++ b/tests/futility/test_update.sh
@@ -412,6 +412,19 @@
 	"${FROM_IMAGE}.al" "${LINK_BIOS}" \
 	-a "${A}" --wp=0 --sys_props 0,0x10001,1,3 --model=whitetip
 
+# WL-Unibuild without default keys
+test_update "Full update (--a, model=WL, no VPD, no default keys)" \
+	"${FROM_IMAGE}.al" "!Need VPD set for white" \
+	-a "${A}" --wp=0 --sys_props 0,0x10001,1,3 --model=whitetip
+
+# WL-Unibuild with default keys as model name
+cp -f "${TMP}.to/rootkey" "${A}/keyset/rootkey.whitetip"
+cp -f "${TMP}.to/VBLOCK_A" "${A}/keyset/vblock_A.whitetip"
+cp -f "${TMP}.to/VBLOCK_B" "${A}/keyset/vblock_B.whitetip"
+test_update "Full update (-a, model=WL, no VPD, default keys)" \
+	"${FROM_IMAGE}.al" "${LINK_BIOS}" \
+	-a "${A}" --wp=0 --sys_props 0,0x10001,1,3 --model=whitetip
+
 # Test special programmer
 if type flashrom >/dev/null 2>&1; then
 	echo "TEST: Full update (dummy programmer)"