power: suspend_stress_test: Count firmware errors before suspend

cbmem may output errors berfore suspend and cause test failure even if
the script suspends and resumes successfully.

BUG=b:176048961
TEST=suspend_stress_test with DUTs contain firmware errors

Change-Id: I3522f6ddea3396b3c6b88a62520e9e97f1bda603
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2604843
Reviewed-by: Eric Caruso <ejcaruso@chromium.org>
Tested-by: Cheng Yueh <cyueh@chromium.org>
Commit-Queue: Cheng Yueh <cyueh@chromium.org>
diff --git a/power_manager/tools/suspend_stress_test b/power_manager/tools/suspend_stress_test
index e11daa5..d664bee 100755
--- a/power_manager/tools/suspend_stress_test
+++ b/power_manager/tools/suspend_stress_test
@@ -134,6 +134,10 @@
   fi
 }
 
+get_firmware_error_counts() {
+  echo $(cbmem -1 | grep ERROR | wc -l)
+}
+
 FLAGS "$@" || exit 1
 
 # Some environment variables (USER/EUID) are not set in the factory, so use
@@ -221,6 +225,7 @@
 last_ignore_count=0
 premature_wake_count=0
 late_wake_count=0
+last_firmware_errors=$(get_firmware_error_counts)
 
 while true; do
   : $(( cur += 1 ))
@@ -285,8 +290,11 @@
   fi
 
   # Look for errors in firmware log.
-  if cbmem -1 | grep ERROR; then
+  cur_firmware_errors=$(get_firmware_error_counts)
+  number_of_new_errors=$(( ${cur_firmware_errors} - ${last_firmware_errors} ))
+  if [ ${number_of_new_errors} -gt 0 ]; then
     : $(( firmware_errors += 1 ))
+    cbmem -1 | grep ERROR | tail -n ${number_of_new_errors}
     if [ ${FLAGS_errors_fatal} -eq ${FLAGS_TRUE} -a \
          ${FLAGS_fw_errors_fatal} -eq ${FLAGS_TRUE} ]; then
       exit_loop=1
@@ -295,6 +303,7 @@
       echo "Firmware error found (ignoring)."
     fi
   fi
+  last_firmware_errors=${cur_firmware_errors}
   # Make sure suspend succeeded
   cur_successes=$(get_success_count)
   cur_ignore_count=$(get_wakeup_source_event_count \