A FAFT test case of corrupting both firmware body A and B.

This test corrupts both firmware body A and B and checks the next boot results.

The expected behavior is different if the firmware preamble USE_RO_NORMAL
flag is enabled. In the case USE_RO_NORMAL ON, the firmware corruption
doesn't hurt the boot results since it boots the RO path directly and does
not load and verify the RW firmware body. In the case USE_RO_NORMAL OFF,
the firmware verification fails on loading RW firmware and enters recovery
mode. In this case, it requires a USB disk plugged-in, which contains a
Chrome OS test image (built by "build_image --test").

BUG=chromium-os:19710;chrome-os-partner:6882
TEST=tested on both USE_RO_NORMAL enabled and disabled devices:
$ run_remote_tests.sh --remote=$REMOTE_IP -a "xml_config=$OVERLAY_XML \
      servo_vid=0x18d1 servo_pid=0x5001" CorruptBothFwBodyAB/control.both

Change-Id: I37661366e41196abacbf78756032ac6af3f1805e
Reviewed-on: https://gerrit.chromium.org/gerrit/12973
Commit-Ready: Tom Wai-Hong Tam <waihong@chromium.org>
Reviewed-by: Tom Wai-Hong Tam <waihong@chromium.org>
Tested-by: Tom Wai-Hong Tam <waihong@chromium.org>
diff --git a/server/site_tests/firmware_CorruptBothFwBodyAB/control b/server/site_tests/firmware_CorruptBothFwBodyAB/control
new file mode 120000
index 0000000..65441e6
--- /dev/null
+++ b/server/site_tests/firmware_CorruptBothFwBodyAB/control
@@ -0,0 +1 @@
+control.both
\ No newline at end of file
diff --git a/server/site_tests/firmware_CorruptBothFwBodyAB/control.both b/server/site_tests/firmware_CorruptBothFwBodyAB/control.both
new file mode 100644
index 0000000..92520be
--- /dev/null
+++ b/server/site_tests/firmware_CorruptBothFwBodyAB/control.both
@@ -0,0 +1,35 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+AUTHOR = "Chrome OS Team"
+NAME = "firmware_CorruptFwBodyAB"
+PURPOSE = "Servo based both firmware body A and B corruption test"
+CRITERIA = "This test will fail if firmware does not enter recovery mode"
+TIME = "LONG"
+TEST_CATEGORY = "Functional"
+TEST_CLASS = "firmware"
+TEST_TYPE = "server"
+
+DOC = """
+This test corrupts both firmware body A and B and checks the next boot results.
+
+The expected behavior is different if the firmware preamble USE_RO_NORMAL
+flag is enabled. In the case USE_RO_NORMAL ON, the firmware corruption 
+doesn't hurt the boot results since it boots the RO path directly and does
+not load and verify the RW firmware body. In the case USE_RO_NORMAL OFF,
+the firmware verification fails on loading RW firmware and enters recovery
+mode. In this case, it requires a USB disk plugged-in, which contains a
+Chrome OS test image (built by "build_image --test").
+"""
+
+def run_corruptbothfwbodyab(machine):
+    host = hosts.create_host(machine)
+    job.run_test("firmware_CorruptBothFwBodyAB", host=host, cmdline_args=args,
+                 use_faft=True, disable_sysinfo=True,
+                 dev_mode=False, tag="normal")
+    job.run_test("firmware_CorruptBothFwBodyAB", host=host, cmdline_args=args,
+                 use_faft=True, disable_sysinfo=True,
+                 dev_mode=True, tag="dev")
+
+parallel_simple(run_corruptbothfwbodyab, machines)
diff --git a/server/site_tests/firmware_CorruptBothFwBodyAB/control.dev b/server/site_tests/firmware_CorruptBothFwBodyAB/control.dev
new file mode 100644
index 0000000..3e159ec
--- /dev/null
+++ b/server/site_tests/firmware_CorruptBothFwBodyAB/control.dev
@@ -0,0 +1,32 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+AUTHOR = "Chrome OS Team"
+NAME = "firmware_CorruptFwBodyAB"
+PURPOSE = "Servo based both firmware body A and B corruption test"
+CRITERIA = "This test will fail if firmware does not enter recovery mode"
+TIME = "LONG"
+TEST_CATEGORY = "Functional"
+TEST_CLASS = "firmware"
+TEST_TYPE = "server"
+
+DOC = """
+This test corrupts both firmware body A and B and checks the next boot results.
+
+The expected behavior is different if the firmware preamble USE_RO_NORMAL
+flag is enabled. In the case USE_RO_NORMAL ON, the firmware corruption 
+doesn't hurt the boot results since it boots the RO path directly and does
+not load and verify the RW firmware body. In the case USE_RO_NORMAL OFF,
+the firmware verification fails on loading RW firmware and enters recovery
+mode. In this case, it requires a USB disk plugged-in, which contains a
+Chrome OS test image (built by "build_image --test").
+"""
+
+def run_corruptbothfwbodyab(machine):
+    host = hosts.create_host(machine)
+    job.run_test("firmware_CorruptBothFwBodyAB", host=host, cmdline_args=args,
+                 use_faft=True, disable_sysinfo=True,
+                 dev_mode=True, tag="dev")
+
+parallel_simple(run_corruptbothfwbodyab, machines)
diff --git a/server/site_tests/firmware_CorruptBothFwBodyAB/control.normal b/server/site_tests/firmware_CorruptBothFwBodyAB/control.normal
new file mode 100644
index 0000000..22d2913
--- /dev/null
+++ b/server/site_tests/firmware_CorruptBothFwBodyAB/control.normal
@@ -0,0 +1,32 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+AUTHOR = "Chrome OS Team"
+NAME = "firmware_CorruptFwBodyAB"
+PURPOSE = "Servo based both firmware body A and B corruption test"
+CRITERIA = "This test will fail if firmware does not enter recovery mode"
+TIME = "LONG"
+TEST_CATEGORY = "Functional"
+TEST_CLASS = "firmware"
+TEST_TYPE = "server"
+
+DOC = """
+This test corrupts both firmware body A and B and checks the next boot results.
+
+The expected behavior is different if the firmware preamble USE_RO_NORMAL
+flag is enabled. In the case USE_RO_NORMAL ON, the firmware corruption 
+doesn't hurt the boot results since it boots the RO path directly and does
+not load and verify the RW firmware body. In the case USE_RO_NORMAL OFF,
+the firmware verification fails on loading RW firmware and enters recovery
+mode. In this case, it requires a USB disk plugged-in, which contains a
+Chrome OS test image (built by "build_image --test").
+"""
+
+def run_corruptbothfwbodyab(machine):
+    host = hosts.create_host(machine)
+    job.run_test("firmware_CorruptBothFwBodyAB", host=host, cmdline_args=args,
+                 use_faft=True, disable_sysinfo=True,
+                 dev_mode=False, tag="normal")
+
+parallel_simple(run_corruptbothfwbodyab, machines)
diff --git a/server/site_tests/firmware_CorruptBothFwBodyAB/firmware_CorruptBothFwBodyAB.py b/server/site_tests/firmware_CorruptBothFwBodyAB/firmware_CorruptBothFwBodyAB.py
new file mode 100644
index 0000000..447de30
--- /dev/null
+++ b/server/site_tests/firmware_CorruptBothFwBodyAB/firmware_CorruptBothFwBodyAB.py
@@ -0,0 +1,111 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+
+from autotest_lib.server.cros.faftsequence import FAFTSequence
+
+
+class firmware_CorruptBothFwBodyAB(FAFTSequence):
+    """
+    Servo based both firmware body A and B corruption test.
+
+    The expected behavior is different if the firmware preamble USE_RO_NORMAL
+    flag is enabled. In the case USE_RO_NORMAL ON, the firmware corruption
+    doesn't hurt the boot results since it boots the RO path directly and does
+    not load and verify the RW firmware body. In the case USE_RO_NORMAL OFF,
+    the firmware verification fails on loading RW firmware and enters recovery
+    mode. In this case, it requires a USB disk plugged-in, which contains a
+    Chrome OS test image (built by "build_image --test").
+    """
+    version = 1
+
+    PREAMBLE_USE_RO_NORMAL = 1
+
+    use_ro = False
+
+
+    def ensure_normal_boot(self):
+        """Ensure normal boot this time.
+
+        If not, it may be a test failure during step 2, try to recover to
+        normal mode by recovering the firmware and rebooting.
+        """
+        if self.crossystem_checker({'mainfw_type': 'recovery'}):
+            self.run_faft_step({
+                'userspace_action': (self.faft_client.run_shell_command,
+                    'chromeos-firmwareupdate --mode recovery')
+            })
+
+
+    def setup(self, dev_mode=False):
+        super(firmware_CorruptBothFwBodyAB, self).setup()
+        if (self.faft_client.get_firmware_flags('a') &
+                self.PREAMBLE_USE_RO_NORMAL):
+            self.use_ro = True
+            self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
+        else:
+            self.assert_test_image_in_usb_disk()
+            self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
+        self.setup_dev_mode(dev_mode)
+
+
+    def cleanup(self):
+        self.ensure_normal_boot()
+        super(firmware_CorruptBothFwBodyAB, self).cleanup()
+
+
+    def run_once(self, host=None):
+        if self.use_ro:
+            # USE_RO_NORMAL flag is ON. Firmware body corruption doesn't
+            # hurt the booting results.
+            logging.info('The firmware USE_RO_NORMAL flag is enabled.')
+            self.register_faft_sequence((
+                {   # Step 1, corrupt both firmware body A and B
+                    'state_checker': (self.crossystem_checker, {
+                        'mainfw_type': ('normal', 'developer'),
+                        'recoverysw_boot': '0',
+                    }),
+                    'userspace_action': (self.faft_client.corrupt_firmware_body,
+                                         ('a', 'b')),
+                },
+                {   # Step 2, still expected normal/developer boot and restore
+                    'state_checker': (self.crossystem_checker, {
+                        'mainfw_type': ('normal', 'developer'),
+                        'recoverysw_boot': '0',
+                    }),
+                    'userspace_action': (self.faft_client.restore_firmware_body,
+                                         ('a', 'b')),
+                },
+            ))
+        else:
+            self.register_faft_sequence((
+                {   # Step 1, corrupt both firmware body A and B
+                    'state_checker': (self.crossystem_checker, {
+                        'mainfw_type': ('normal', 'developer'),
+                        'recoverysw_boot': '0',
+                    }),
+                    'userspace_action': (self.faft_client.corrupt_firmware_body,
+                                         ('a', 'b')),
+                    'firmware_action': self.wait_fw_screen_and_plug_usb,
+                    'install_deps_after_boot': True,
+                },
+                {   # Step 2, expected recovery boot and restore firmware
+                    'state_checker': (self.crossystem_checker, {
+                        'mainfw_type': 'recovery',
+                        'recovery_reason':
+                            self.RECOVERY_REASON['RO_INVALID_RW'],
+                        'recoverysw_boot': '0',
+                    }),
+                    'userspace_action': (self.faft_client.restore_firmware_body,
+                                         ('a', 'b')),
+                },
+                {   # Step 3, expected normal boot, done
+                    'state_checker': (self.crossystem_checker, {
+                        'mainfw_type': ('normal', 'developer'),
+                        'recoverysw_boot': '0',
+                    }),
+                },
+            ))
+        self.run_faft_sequence()