fingerprint_test: Flash FPMCU on cleanup only when necessary

FingerprintSigner test doesn't call setup_test() method, so
_dut_needs_reboot attribute isn't present. During cleanup,
_initialize_running_fw_version() is called. This function checks if
FPMCU firmware version is not correct or rollback block id is not 1
and calls flash_rw_ro_firmware() to re-flash FPMCU if necessary.
Because _dut_needs_reboot was not initialized, flash_rw_ro_firmware()
was not rebooting zork after flashing, so as a result test was reporting
that communication with FPMCU is not possible.

Another observation is that FingerprintSigner test doesn't modify
FPMCU flash really, so in fact there is nothing to clean up. The
problem was fixed by adding _need_fw_restore flag and setting it when
we suspect that the firmware needs to be restored or when we flashed
the firmware.

BUG=b:195952425
TEST=Run fingerprint test suite on zork (morphius or berknip)
     `test_that --board zork suite:fingerprint` and make sure that
     firmware_FingerprintSigner test passes.

Change-Id: I5a9862936befddbffd22aa1063a11876c7c8fb51
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/3220476
Tested-by: Patryk Duda <patrykd@google.com>
Reviewed-by: Tom Hughes <tomhughes@chromium.org>
Commit-Queue: Patryk Duda <patrykd@google.com>
(cherry picked from commit ff7b233091f810a27e7db3f37bc59b95a7f60a67)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/3231039
diff --git a/server/cros/faft/fingerprint_test.py b/server/cros/faft/fingerprint_test.py
index bf398cf..fd2465b 100644
--- a/server/cros/faft/fingerprint_test.py
+++ b/server/cros/faft/fingerprint_test.py
@@ -296,11 +296,13 @@
 
     def cleanup(self):
         """Restores original state."""
-        # Once the tests complete we need to make sure we're running the
-        # original firmware (not dev version) and potentially reset rollback.
-        self._initialize_running_fw_version(use_dev_signed_fw=False,
-                                            force_firmware_flashing=False)
-        self._initialize_fw_entropy()
+        if hasattr(self, '_need_fw_restore') and self._need_fw_restore:
+            # Once the tests complete we need to make sure we're running the
+            # original firmware (not dev version) and potentially reset rollback.
+            self._initialize_running_fw_version(use_dev_signed_fw=False,
+                                                force_firmware_flashing=False)
+            self._initialize_fw_entropy()
+
         # Re-enable biod and updater after flashing and initializing entropy so
         # that they don't interfere if there was a reboot.
         if hasattr(self, '_dut_needs_reboot') and self._dut_needs_reboot:
@@ -817,6 +819,8 @@
 
     def flash_rw_ro_firmware(self, fw_path):
         """Flashes *all* firmware (both RO and RW)."""
+        # Check if FPMCU firmware needs to be re-flashed during cleanup
+        self._need_fw_restore = True
         self.set_hardware_write_protect(False)
         flash_cmd = 'flash_fp_mcu' + ' --noservices ' + fw_path
         logging.info('Running flash cmd: %s', flash_cmd)
diff --git a/server/site_tests/firmware_Fingerprint/firmware_Fingerprint.py b/server/site_tests/firmware_Fingerprint/firmware_Fingerprint.py
index c045c78..3fc04ca 100644
--- a/server/site_tests/firmware_Fingerprint/firmware_Fingerprint.py
+++ b/server/site_tests/firmware_Fingerprint/firmware_Fingerprint.py
@@ -34,6 +34,8 @@
             enable_software_write_protect, force_firmware_flashing,
             init_entropy)
 
+        # Check if FPMCU firmware needs to be re-flashed during cleanup
+        self._need_fw_restore = True
         self._test_exe = test_exe
 
         # Convert the arguments (test image names) to the actual filenames of