futility: update: add new quirk 'no_check_platform'

Some devices may have flashed firmware with different platform name in
their early stage (especially in the first build of leading devices),
so we do want to provide an explicit way (not just --force) to skip
checking platform name.

The change CL:2059621 does not help because the loaded system
firmware looks good.

This is implemented as a quirk so we can enable it using a CBFS quirk
file, making it easier to be deployed by auto update.

BRANCH=None
BUG=None
TEST=make runtests

Change-Id: I888d5848921d31c9b7cba1b96c42d38fda71927e
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2573999
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
diff --git a/futility/updater.c b/futility/updater.c
index 71a498e..fa00854 100644
--- a/futility/updater.c
+++ b/futility/updater.c
@@ -1209,8 +1209,13 @@
 	       image_from->file_name, image_from->ro_version,
 	       image_from->rw_version_a, image_from->rw_version_b);
 
-	if (cfg->check_platform && check_compatible_platform(cfg))
+	try_apply_quirk(QUIRK_NO_CHECK_PLATFORM, cfg);
+	if (cfg->check_platform && check_compatible_platform(cfg)) {
+		ERROR("The firmware image is not compatible with your system. "
+		      "If you really want to proceed, please run again with: "
+		      "--quirks=no_check_platform\n");
 		return UPDATE_ERR_PLATFORM;
+	}
 
 	wp_enabled = is_write_protection_enabled(cfg);
 	STATUS("Write protection: %d (%s; HW=%d, SW=%d).\n", wp_enabled,
diff --git a/futility/updater.h b/futility/updater.h
index a4f5580..69ba5e2 100644
--- a/futility/updater.h
+++ b/futility/updater.h
@@ -45,6 +45,7 @@
 	QUIRK_EC_PARTIAL_RECOVERY,
 	QUIRK_OVERRIDE_SIGNATURE_ID,
 	QUIRK_PRESERVE_ME,
+	QUIRK_NO_CHECK_PLATFORM,
 	QUIRK_MAX,
 };
 
diff --git a/futility/updater_quirks.c b/futility/updater_quirks.c
index b3dea93..b9bdb2c 100644
--- a/futility/updater_quirks.c
+++ b/futility/updater_quirks.c
@@ -415,6 +415,16 @@
 }
 
 /*
+ * Disable checking platform compatibility.
+ */
+static int quirk_no_check_platform(struct updater_config *cfg)
+{
+	WARN("Disabled checking platform. You are on your own.\n");
+	cfg->check_platform = 0;
+	return 0;
+}
+
+/*
  * Registers known quirks to a updater_config object.
  */
 void updater_register_quirks(struct updater_config *cfg)
@@ -473,6 +483,11 @@
 	quirks->help = "b/165590952; Preserve ME during firmware update except "
 		       "for factory update or developer images.";
 	quirks->apply = quirk_preserve_me;
+
+	quirks = &cfg->quirks[QUIRK_NO_CHECK_PLATFORM];
+	quirks->name = "no_check_platform";
+	quirks->help = "Do not check platform name.";
+	quirks->apply = quirk_no_check_platform;
 }
 
 /*
diff --git a/tests/futility/test_update.sh b/tests/futility/test_update.sh
index ae4a362..31f3ecb 100755
--- a/tests/futility/test_update.sh
+++ b/tests/futility/test_update.sh
@@ -48,6 +48,7 @@
 cp -f ${PEPPY_BIOS} ${FROM_IMAGE}
 "${FUTILITY}" load_fmap "${FROM_IMAGE}" \
 	RO_VPD:"${RO_VPD_BLOB}" RW_VPD:"${RO_VPD_BLOB}"
+cp -f "${FROM_IMAGE}" "${FROM_IMAGE}".unpatched
 
 patch_file() {
 	local file="$1"
@@ -347,6 +348,15 @@
 	--quirks min_platform_version=3 \
 	-i "${TO_IMAGE}" --wp=0 --sys_props 0,0x10001,1,3
 
+test_update "Full update (incompatible platform)" \
+	"${FROM_IMAGE}".unpatched "!platform is not compatible" \
+	-i "${TO_IMAGE}" --wp=0 --sys_props 0,0x10001,1
+
+test_update "Full update (--quirks no_check_platform)" \
+	"${FROM_IMAGE}".unpatched "${TMP}.expected.full" \
+	--quirks no_check_platform \
+	-i "${TO_IMAGE}" --wp=0 --sys_props 0,0x10001,1
+
 # Test archive and manifest.
 A="${TMP}.archive"
 mkdir -p "${A}/bin"