futility: updater: decide if we can use diff-image by programmer

Previously we decide if the flash command can use the image_current as
the diff image by comparing if the target image pointer is identical to
the host image to write (cfg->image). This may not work properly if we
try to write a temporary firmware image object loaded separately.
A more correct way to is check if the image has the same programmer from
the diff image (e.g., image_current).

BUG=b:221137867
TEST=build and run futility tests.
BRANCH=None

Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Change-Id: Iee61cd9b47c0db4b87001bbb348f95a89495b975
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3490386
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Commit-Queue: Yu-Ping Wu <yupingso@chromium.org>
diff --git a/futility/updater.c b/futility/updater.c
index 66f42e4..bc68040 100644
--- a/futility/updater.c
+++ b/futility/updater.c
@@ -392,6 +392,25 @@
 }
 
 /*
+ * Returns 1 if the programmers in image1 and image2 are the same.
+ */
+static int is_the_same_programmer(const struct firmware_image *image1,
+				  const struct firmware_image *image2)
+{
+	assert(image1 && image2);
+
+	/* Including if both are NULL. */
+	if (image1->programmer == image2->programmer)
+		return 1;
+
+	/* Not the same if either one is NULL. */
+	if (!image1->programmer || !image2->programmer)
+		return 0;
+
+	return strcmp(image1->programmer, image2->programmer) == 0;
+}
+
+/*
  * Writes a section from given firmware image to system firmware.
  * If section_name is NULL, write whole image.
  * Returns 0 if success, non-zero if error.
@@ -411,10 +430,9 @@
 				cfg->emulation, image, section_name);
 	}
 
-	if (cfg->use_diff_image && image == &cfg->image &&
-	    cfg->image_current.data) {
+	if (cfg->use_diff_image && cfg->image_current.data &&
+	    is_the_same_programmer(&cfg->image_current, image))
 		diff_image = &cfg->image_current;
-	}
 
 	return write_system_firmware(image, diff_image, section_name,
 				     &cfg->tempfiles, cfg->do_verify,