firmware_Cr50CCDFirmwareUpdate: recover fw by chromeos-firmwareupdate

This patch let firmware restored using 'chromeos-firmwareupdate
--mode=recovery' command by default in cleanup.
If the argument, '--arg="backup_fw=True"' was given in test_that, then
the test makes a backup during initiliazation() and restore firmware
with those backup.

BUG=b:158477212
TEST=ran firmware_Cr50CCDFirmwareUpdate on coral.

$ test_that --board coral ... --args="backup_fw=True"
firmware_Cr50CCDFirmwareUpdate
// chromeos-firmware was not executed.

$ test_that --board coral ... --args="backup_fw=False"
firmware_Cr50CCDFirmwareUpdate
// chromeos-firmware was executed.

$ test_that --board coral ... firmware_Cr50CCDFirmwareUpdate
// chromeos-firmware was executed.

Change-Id: Ib4db766721f21bc1282cca2b76b1168e83d453d1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2277159
Tested-by: Namyoon Woo <namyoon@chromium.org>
Commit-Queue: Mary Ruthven <mruthven@chromium.org>
Reviewed-by: Mary Ruthven <mruthven@chromium.org>
diff --git a/server/site_tests/firmware_Cr50CCDFirmwareUpdate/control b/server/site_tests/firmware_Cr50CCDFirmwareUpdate/control
index fc7f3ad..6eb54d5 100644
--- a/server/site_tests/firmware_Cr50CCDFirmwareUpdate/control
+++ b/server/site_tests/firmware_Cr50CCDFirmwareUpdate/control
@@ -14,6 +14,9 @@
 
 DOC = """
 This is a test to program both EC and AP firmware through ccd_cr50 device.
+
+If you intend to backup and restore the original firmware, then add
+'--args="backup_fw=True"' to your test_that command line.
 """
 
 if 'args_dict' not in locals():
diff --git a/server/site_tests/firmware_Cr50CCDFirmwareUpdate/firmware_Cr50CCDFirmwareUpdate.py b/server/site_tests/firmware_Cr50CCDFirmwareUpdate/firmware_Cr50CCDFirmwareUpdate.py
index 2bf006cc8..d6b3d64 100644
--- a/server/site_tests/firmware_Cr50CCDFirmwareUpdate/firmware_Cr50CCDFirmwareUpdate.py
+++ b/server/site_tests/firmware_Cr50CCDFirmwareUpdate/firmware_Cr50CCDFirmwareUpdate.py
@@ -14,6 +14,7 @@
     """A test that can provision a machine to the correct firmware version."""
 
     version = 1
+    should_restore_fw = False
 
     def initialize(self, host, cmdline_args, full_args):
         """Initialize the test and check if cr50 exists.
@@ -32,14 +33,30 @@
         servo_type = self.servo.get_servo_version()
         if 'ccd_cr50' not in servo_type:
             raise error.TestNAError('unsupported servo type: %s' % servo_type)
-        self.backup_firmware()
+
+        if eval(full_args.get('backup_fw', 'False')):
+            self.backup_firmware()
 
     def cleanup(self):
         try:
+            if not self.should_restore_fw:
+                return
+
+            self.cr50.reboot()
+            self.switcher.mode_aware_reboot(reboot_type='cold')
+
             if self.is_firmware_saved():
-                self.cr50.reboot()
-                self.switcher.mode_aware_reboot(reboot_type='cold')
+                logging.info('Restoring firmware')
                 self.restore_firmware()
+            else:
+                logging.info('chromeos-firmwareupdate --mode=recovery')
+                result = self._client.run('chromeos-firmwareupdate'
+                                          ' --mode=recovery',
+                                          ignore_status=True)
+                if result.exit_status != 0:
+                    logging.error('chromeos-firmwareupdate failed: %s',
+                                  result.stdout.strip())
+                self._client.reboot()
         except Exception as e:
             logging.error("Caught exception: %s", str(e))
         finally:
@@ -84,5 +101,6 @@
         if self.servo.get('ec_chip') == 'it83xx':
             self.cr50.set_cap('I2C', 'Always')
 
+        self.should_restore_fw = True
         host.firmware_install(build=value, rw_only=rw_only,
                               dest=self.resultsdir, verify_version=True)