Backing up the EC FW incase EC Sync fails from BIOS

BUG=None
TEST=Ran FW corrupt tests against terra and panther.

Change-Id: Ib8b75e3d794d91d1fea9a42260d4e00c853e1854
Reviewed-on: https://chromium-review.googlesource.com/499153
Commit-Ready: Tyler Reid <twreid@google.com>
Tested-by: Tyler Reid <twreid@google.com>
Reviewed-by: Wai-Hong Tam <waihong@google.com>
diff --git a/client/cros/faft/rpc_functions.py b/client/cros/faft/rpc_functions.py
index fa1f022..1d581af 100755
--- a/client/cros/faft/rpc_functions.py
+++ b/client/cros/faft/rpc_functions.py
@@ -486,6 +486,21 @@
         """Get SHA1 hash of EC RW firmware section."""
         return self._ec_handler.get_section_sha('rw')
 
+    def _ec_dump_whole(self, ec_path):
+        """Dump the current EC firmware to a file, specified by ec_path.
+
+        @param ec_path: The path of the EC image to be written.
+        """
+        self._ec_handler.dump_whole(ec_path)
+
+    def _ec_write_whole(self, ec_path):
+        """Write the firmware from ec_path to the current system.
+
+        @param ec_path: The path of the source EC image.
+        """
+        self._ec_handler.new_image(ec_path)
+        self._ec_handler.write_whole()
+
     @allow_multiple_section_input
     def _ec_corrupt_sig(self, section):
         """Corrupt the requested EC section signature.
diff --git a/server/cros/faft/firmware_test.py b/server/cros/faft/firmware_test.py
index 43cb0d2..0e14be8 100644
--- a/server/cros/faft/firmware_test.py
+++ b/server/cros/faft/firmware_test.py
@@ -1179,7 +1179,7 @@
             corrupt_FVMAIN = (current_sha[1] != self._backup_firmware_sha[1])
             corrupt_VBOOTB = (current_sha[2] != self._backup_firmware_sha[2])
             corrupt_FVMAINB = (current_sha[3] != self._backup_firmware_sha[3])
-            logging.info("Firmware changed:")
+            logging.info('Firmware changed:')
             logging.info('VBOOTA is changed: %s', corrupt_VBOOTA)
             logging.info('VBOOTB is changed: %s', corrupt_VBOOTB)
             logging.info('FVMAIN is changed: %s', corrupt_FVMAIN)
@@ -1196,6 +1196,13 @@
         self.faft_client.bios.dump_whole(remote_bios_path)
         self._client.get_file(remote_bios_path,
                               os.path.join(self.resultsdir, 'bios' + suffix))
+
+        if self.faft_config.chrome_ec:
+            remote_ec_path = os.path.join(remote_temp_dir, 'ec')
+            self.faft_client.ec.dump_whole(remote_ec_path)
+            self._client.get_file(remote_ec_path,
+                              os.path.join(self.resultsdir, 'ec' + suffix))
+
         self._client.run('rm -rf %s' % remote_temp_dir)
         logging.info('Backup firmware stored in %s with suffix %s',
             self.resultsdir, suffix)
@@ -1231,6 +1238,13 @@
 
         self.faft_client.bios.write_whole(
             os.path.join(remote_temp_dir, 'bios'))
+
+        if self.faft_config.chrome_ec:
+            self._client.send_file(os.path.join(self.resultsdir, 'ec' + suffix),
+                os.path.join(remote_temp_dir, 'ec'))
+            self.faft_client.ec.write_whole(
+                os.path.join(remote_temp_dir, 'ec'))
+
         self.switcher.mode_aware_reboot()
         logging.info('Successfully restore firmware.')