firmware_WriteProtectFunc: decouple from chromeos-firmwareupdate

The test used the image shipped in the firmware updater which
creates issues if that image and the written image differ in certain
significant ways (e.g. the fmap layout). Rework it to use the flashed
firmware and go from there.

BUG=b:172330099
TEST=test_that -b eve 10.99.0.100 --fast --no-retries
firmware_WriteProtectFunc passes twice in a row

Change-Id: I5db5950cf4efcb1f4419532b3eda3e6c1e2aed9c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2545832
Tested-by: Patrick Georgi <pgeorgi@chromium.org>
Reviewed-by: Brent Peterson <brentpeterson@chromium.org>
Commit-Queue: Patrick Georgi <pgeorgi@chromium.org>
diff --git a/server/site_tests/firmware_WriteProtectFunc/firmware_WriteProtectFunc.py b/server/site_tests/firmware_WriteProtectFunc/firmware_WriteProtectFunc.py
index 8af34c8..144785d 100644
--- a/server/site_tests/firmware_WriteProtectFunc/firmware_WriteProtectFunc.py
+++ b/server/site_tests/firmware_WriteProtectFunc/firmware_WriteProtectFunc.py
@@ -37,6 +37,8 @@
             self._original_sw_wps[target] = sw_wp_dict['enabled']
         self._original_hw_wp = 'on' in self.servo.get('fw_wp_state')
         self.backup_firmware()
+        self.work_path = self.faft_client.system.create_temp_dir(
+                'flashrom_', '/mnt/stateful_partition/')
 
     def cleanup(self):
         """Cleanup the test"""
@@ -63,6 +65,7 @@
         except Exception as e:
             logging.error('Caught exception: %s', str(e))
 
+        self.faft_client.system.remove_dir(self.work_path)
         super(firmware_WriteProtectFunc, self).cleanup()
 
     def _set_write_protect(self, target, enable):
@@ -167,26 +170,49 @@
                                          'accross %s' %
                                          (target.upper(), reboot_name))
 
-        work_path = self.faft_client.updater.get_work_path()
-
+        work_path = self.work_path
         # Check if RO FW really can't be overwritten when WP is enabled.
         for target in self._targets:
+            # Current firmware image as read from flash
             ro_before = os.path.join(work_path, '%s_ro_before.bin' % target)
-            ro_after = os.path.join(work_path, '%s_ro_after.bin' % target)
+            # Current firmware image with modification to test writing
             ro_test = os.path.join(work_path, '%s_ro_test.bin' % target)
+            # Firmware as read after writing flash
+            ro_after = os.path.join(work_path, '%s_ro_after.bin' % target)
 
-            # Use the firmware blobs unpacked from the firmware updater for
-            # testing. To ensure there is difference in WP_RO section between
-            # the firmware on the DUT and the firmware unpacked from the
-            # firmware updater, we mess around FRID.
-            self.faft_client.updater.modify_image_fwids(target, ['ro'])
+            # Fetch firmware from flash. This serves as the base of ro_test
+            self.run_cmd(
+                    'flashrom -p %s -r -i WP_RO:%s ' %
+                    (self._flashrom_targets[target], ro_before), 'SUCCESS')
 
-            test = os.path.join(work_path, self._get_relative_path(target))
-            self.get_wp_ro_firmware_section(test, ro_test)
+            lines = self.run_cmd('dump_fmap -p %s' % ro_before)
+            FMAP_AREA_NAMES = ['name', 'offset', 'size']
 
-            self.run_cmd('flashrom -p %s -r -i WP_RO:%s' %
-                    (self._flashrom_targets[target], ro_before),
-                    'SUCCESS')
+            modified = False
+            wpro_offset = -1
+            for line in lines:
+                region = dict(zip(FMAP_AREA_NAMES, line.split()))
+                if region['name'] == 'WP_RO':
+                    wpro_offset = int(region['offset'])
+            if wpro_offset == -1:
+                raise error.TestFail('WP_RO not found in fmap')
+            for line in lines:
+                region = dict(zip(FMAP_AREA_NAMES, line.split()))
+                if region['name'] == 'RO_FRID':
+                    modified = True
+                    self.run_cmd('cp %s %s' % (ro_before, ro_test))
+                    self.run_cmd(
+                            'dd if=%s bs=1 count=%d skip=%d '
+                            '| tr "[a-zA-Z]" "[A-Za-z]" '
+                            '| dd of=%s bs=1 count=%d seek=%d conv=notrunc' %
+                            (ro_test, int(region['size']),
+                             int(region['offset']) - wpro_offset, ro_test,
+                             int(region['size']),
+                             int(region['offset']) - wpro_offset))
+
+            if not modified:
+                raise error.TestFail('Could not find RO_FRID in %s' %
+                                     target.upper())
 
             # Writing WP_RO section is expected to fail.
             self.run_cmd('flashrom -p %s -w -i WP_RO:%s' %