faft: Unify ro_normal_checker method in FAFTSequence

We define a common used ro_normal_checker method in FAFTSequence. It
also checks the VbSharedData flags and active EC firmware for the
All-A/All-B/All-RO design on Chrome EC devices.

BUG=chromium-os:34147
TEST=Run the following tests:
$ run_remote_test.sh --remote=$IP RONormalBoot/control$
$ run_remote_test.sh --remote=$IP UpdateECBin/control$
$ run_remote_test.sh --remote=$IP ECWriteProtect/control$

Change-Id: Iee3104d0da1931aa21d256e0245eb706f4e080e1
Reviewed-on: https://gerrit.chromium.org/gerrit/32203
Reviewed-by: Mike Truty <truty@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Ready: Tom Wai-Hong Tam <waihong@chromium.org>
Tested-by: Tom Wai-Hong Tam <waihong@chromium.org>
diff --git a/server/cros/faftsequence.py b/server/cros/faftsequence.py
index f77746d..33d0603 100644
--- a/server/cros/faftsequence.py
+++ b/server/cros/faftsequence.py
@@ -649,6 +649,30 @@
         return True
 
 
+    def ro_normal_checker(self, expected_fw=None, twostop=False):
+        """Check the current boot uses RO boot.
+
+        Args:
+          expected_fw: A string of expected firmware, 'A', 'B', or
+                       None if don't care.
+          twostop: True to expect a TwoStop boot; False to expect a RO boot.
+
+        Returns:
+          True if the currect boot firmware matched and used RO boot;
+          otherwise, False.
+        """
+        crossystem_dict = {'tried_fwb': '0'}
+        if expected_fw:
+            crossystem_dict['mainfw_act'] = expected_fw.upper()
+        if self.check_ec_capability():
+            crossystem_dict['ecfw_act'] = ('RW' if twostop else 'RO')
+
+        return (self.vdat_flags_checker(
+                    self.VDAT_FLAG_LF_USE_RO_NORMAL,
+                    0 if twostop else self.VDAT_FLAG_LF_USE_RO_NORMAL) and
+                self.crossystem_checker(crossystem_dict))
+
+
     def root_part_checker(self, expected_part):
         """Check the partition number of the root device matched.
 
diff --git a/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py b/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
index 17c855f..cc35700 100644
--- a/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
+++ b/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
@@ -43,22 +43,6 @@
             return False
 
 
-    def ro_normal_checker(self):
-        return self.crossystem_checker({
-            'mainfw_act': 'A',
-            'tried_fwb': '0',
-            'ecfw_act': 'RO',
-        })
-
-
-    def twostop_checker(self):
-        return self.crossystem_checker({
-            'mainfw_act': 'A',
-            'tried_fwb': '0',
-            'ecfw_act': 'RW',
-        })
-
-
     def setup(self, dev_mode=False):
         super(firmware_ECWriteProtect, self).setup()
         self.setup_dev_mode(dev_mode)
@@ -78,12 +62,12 @@
 
         self.register_faft_sequence((
             {   # Step 1, expected EC RO boot, enable WP and reboot EC.
-                'state_checker': self.ro_normal_checker,
+                'state_checker': (self.ro_normal_checker, 'A'),
                 'reboot_action': (self.set_EC_write_protect_and_reboot, True),
             },
             {   # Step 2, expected EC RO boot, write protected. Disable RO flag
                 #         and reboot EC.
-                'state_checker': (lambda: self.ro_normal_checker() and
+                'state_checker': (lambda: self.ro_normal_checker('A') and
                                           self.write_protect_checker()),
                 'userspace_action': (self.faft_client.set_firmware_flags,
                                      'a', 0),
@@ -91,20 +75,22 @@
             },
             {   # Step 3, expected EC RW boot, write protected. Reboot EC by
                 #         ectool.
-                'state_checker': (lambda: self.twostop_checker() and
+                'state_checker': (lambda: self.ro_normal_checker('A',
+                                              twostop=True) and
                                           self.write_protect_checker()),
                 'reboot_action': (self.sync_and_ec_reboot, 'cold'),
             },
             {   # Step 4, expected EC RW boot, write protected. Restore RO
                 #         normal flag and deactivate write protect.
-                'state_checker': (lambda: self.twostop_checker() and
+                'state_checker': (lambda: self.ro_normal_checker('A',
+                                              twostop=True) and
                                           self.write_protect_checker()),
                 'userspace_action': (self.faft_client.set_firmware_flags,
                                      'a', self.PREAMBLE_USE_RO_NORMAL),
                 'reboot_action': (self.set_EC_write_protect_and_reboot, False),
             },
             {   # Step 5, expected EC RO boot.
-                'state_checker': self.ro_normal_checker,
+                'state_checker': (self.ro_normal_checker, 'A'),
             },
         ))
         self.run_faft_sequence()
diff --git a/server/site_tests/firmware_RONormalBoot/firmware_RONormalBoot.py b/server/site_tests/firmware_RONormalBoot/firmware_RONormalBoot.py
index e35415b..4902400 100644
--- a/server/site_tests/firmware_RONormalBoot/firmware_RONormalBoot.py
+++ b/server/site_tests/firmware_RONormalBoot/firmware_RONormalBoot.py
@@ -46,33 +46,26 @@
 
     def run_once(self, host=None):
         flags = self.faft_client.get_firmware_flags('a')
-        if flags & self.PREAMBLE_USE_RO_NORMAL:
-            self.register_faft_sequence((
-                {   # Step 1, disable the RO normal boot flag
-                    'state_checker': (self.crossystem_checker, {
-                        'mainfw_act': 'A',
-                        'tried_fwb': '0',
-                    }),
-                    'userspace_action': (self.faft_client.set_firmware_flags,
-                                         'a',
-                                         flags ^ self.PREAMBLE_USE_RO_NORMAL),
-                },
-                {   # Step 2, expected boot ok, restore the original flags
-                    'state_checker': (self.crossystem_checker, {
-                        'mainfw_act': 'A',
-                        'tried_fwb': '0',
-                    }),
-                    'userspace_action': (self.faft_client.set_firmware_flags,
-                                         'a',
-                                         flags),
-                },
-                {   # Step 3, done
-                    'state_checker': (self.crossystem_checker, {
-                        'mainfw_act': 'A',
-                        'tried_fwb': '0',
-                    }),
-                },
-            ))
-            self.run_faft_sequence()
-        else:
+        if flags & self.PREAMBLE_USE_RO_NORMAL == 0:
             logging.info('The firmware USE_RO_NORMAL flag is disabled.')
+            return
+
+        self.register_faft_sequence((
+            {   # Step 1, disable the RO normal boot flag
+                'state_checker': (self.ro_normal_checker, 'A'),
+                'userspace_action': (self.faft_client.set_firmware_flags,
+                                     'a',
+                                     flags ^ self.PREAMBLE_USE_RO_NORMAL),
+            },
+            {   # Step 2, expected TwoStop boot, restore the original flags
+                'state_checker': (lambda: self.ro_normal_checker('A',
+                                              twostop=True)),
+                'userspace_action': (self.faft_client.set_firmware_flags,
+                                     'a',
+                                     flags),
+            },
+            {   # Step 3, done
+                'state_checker': (self.ro_normal_checker, 'A'),
+            },
+        ))
+        self.run_faft_sequence()
diff --git a/server/site_tests/firmware_UpdateECBin/firmware_UpdateECBin.py b/server/site_tests/firmware_UpdateECBin/firmware_UpdateECBin.py
index 5da9ac6..9589494 100644
--- a/server/site_tests/firmware_UpdateECBin/firmware_UpdateECBin.py
+++ b/server/site_tests/firmware_UpdateECBin/firmware_UpdateECBin.py
@@ -94,23 +94,15 @@
 
         self.register_faft_sequence((
             {   # Step 1, expected EC RO boot, update EC and disable RO flag
-                'state_checker': (self.crossystem_checker, {
-                    'mainfw_act': 'A',
-                    'tried_fwb': '0',
-                    'ecfw_act': 'RO',
-                }),
+                'state_checker': (self.ro_normal_checker, 'A'),
                 'userspace_action': self.do_ronormal_update,
                 'reboot_action': self.sync_and_warm_reboot,
             },
             {   # Step 2, expected new EC and RW boot, restore the original BIOS
                 'state_checker': (
-                    lambda: self.crossystem_checker({
-                                'mainfw_act': 'A',
-                                'tried_fwb': '0',
-                                'ecfw_act': 'RW',
-                            }) and (
-                               self.faft_client.get_EC_firmware_sha() ==
-                               self.new_ec_sha)),
+                    lambda: self.ro_normal_checker('A', twostop=True) and
+                            (self.faft_client.get_EC_firmware_sha() ==
+                                 self.new_ec_sha)),
                 'userspace_action': self.do_twostop_update,
                 # We use warm reboot here to test the following EC behavior:
                 #   If EC is already into RW before powering on the AP, the AP
@@ -121,23 +113,15 @@
             },
             {   # Step 3, expected different EC and RW boot, enable RO flag
                 'state_checker': (
-                    lambda: self.crossystem_checker({
-                                'mainfw_act': 'A',
-                                'tried_fwb': '0',
-                                'ecfw_act': 'RW',
-                            }) and (
-                               self.faft_client.get_EC_firmware_sha() !=
-                               self.new_ec_sha)),
+                    lambda: self.ro_normal_checker('A', twostop=True) and
+                            (self.faft_client.get_EC_firmware_sha() !=
+                                 self.new_ec_sha)),
                 'userspace_action': (self.faft_client.set_firmware_flags,
                                      'a', flags),
                 'reboot_action': self.sync_and_warm_reboot,
             },
             {   # Step 4, expected EC RO boot, done
-                'state_checker': (self.crossystem_checker, {
-                    'mainfw_act': 'A',
-                    'tried_fwb': '0',
-                    'ecfw_act': 'RO',
-                }),
+                'state_checker': (self.ro_normal_checker, 'A'),
             },
         ))
         self.run_faft_sequence()