Gandof: Modify battery cut-off execution condition

For battery cut-off to run successfully,
- Battery voltage below 12V while AC_PRESENT
- Shutdown while AC_PRESENT

BUG=chrome-os-partner:42170
TEST=Factory wipe and check the battery is cut-off.
     Factory reset and check the battery is cut-off.

Change-Id: I9804af94556c7738ef2af0586eb404a99056826d
Reviewed-on: https://chromium-review.googlesource.com/296202
Reviewed-by: Bowgo Tsai <bowgotsai@chromium.org>
Commit-Queue: Kitty Liu <kitty.liu@intel.com>
Tested-by: Kitty Liu <kitty.liu@intel.com>
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/chromeos-bsp-gandof-0.0.1-r1.ebuild b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/chromeos-bsp-gandof-0.0.1-r2.ebuild
similarity index 100%
rename from overlay-gandof/chromeos-base/chromeos-bsp-gandof/chromeos-bsp-gandof-0.0.1-r1.ebuild
rename to overlay-gandof/chromeos-base/chromeos-bsp-gandof/chromeos-bsp-gandof-0.0.1-r2.ebuild
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/charging.png b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/charging.png
index e5d5aa7..28f00a2 100644
--- a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/charging.png
+++ b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/charging.png
Binary files differ
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/connect_ac_batterycutoff.png b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/connect_ac_batterycutoff.png
new file mode 100644
index 0000000..4319665
--- /dev/null
+++ b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/connect_ac_batterycutoff.png
Binary files differ
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/discharging_voltage.png b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/discharging_voltage.png
new file mode 100755
index 0000000..632d11c
--- /dev/null
+++ b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/images/discharging_voltage.png
Binary files differ
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/battery_cut_off.sh b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/battery_cut_off.sh
index 1e2b6c3..cfe7f3f 100755
--- a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/battery_cut_off.sh
+++ b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/battery_cut_off.sh
@@ -11,16 +11,22 @@
 TTY="/dev/tty1"
 
 modprobe i2c_dev
-if (ectool battery | grep -q AC_PRESENT); then
-  if [ -e "${IMG_PATH}/remove_ac.png" ]; then
-    ply-image --clear 0x000000 "${IMG_PATH}/remove_ac.png"
+
+# Discharge battery to ensure battery capacity in desired range
+/usr/sbin/board_discharge_voltage.sh
+/usr/sbin/board_charge_battery.sh
+
+# AC power is required for battery cutoff.
+if ! (ectool battery | grep -q AC_PRESENT); then
+  if [ -e "${IMG_PATH}/connect_ac_batterycutoff.png" ]; then
+    ply-image --clear 0x000000 "${IMG_PATH}/connect_ac_batterycutoff.png"
   else
     echo "============================================" >"$TTY"
-    echo "========== Unplug AC adapter now. ==========" >"$TTY"
+    echo "========== Connect AC adapter now. =========" >"$TTY"
     echo "============================================" >"$TTY"
     echo "" >"$TTY"
   fi
-  while (ectool battery | grep -q AC_PRESENT) ; do
+  while [ -z "$(ectool battery | grep AC_PRESENT)" ]; do
     sleep 0.5;
   done
 fi
@@ -33,19 +39,15 @@
   echo "===============================================" >"$TTY"
 fi
 
-ectool batterycutoff at-shutdown
-shutdown -h now
-sleep 15
-
-# Couldn't have reached here
-if [ -e "${IMG_PATH}/cutoff_failed.png" ]; then
-  ply-image --clear 0x000000 "${IMG_PATH}/cutoff_failed.png"
-else
-  echo "======================================" >"$TTY"
-  echo "========== Cut off failed!! ==========" >"$TTY"
-  echo "======================================" >"$TTY"
-  echo "" >"$TTY"
+# Set chargecontrol to idle to prevent battery overcharged.
+# chargecontrol discharge/idle only works when WP is disabled (before PVT).
+# This is only a double check in early stages (better than nothing).
+if !(crossystem sw_wpsw_boot?1 wpsw_boot?1); then
+  ectool chargecontrol idle
+  sleep 5
 fi
 
-sleep 1d
+ectool batterycutoff at-shutdown
+shutdown -h now
+
 exit 1
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_charge_battery.sh b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_charge_battery.sh
index 169169c..38fb671 100755
--- a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_charge_battery.sh
+++ b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_charge_battery.sh
@@ -9,7 +9,7 @@
 #
 
 IMG_PATH="/usr/share/factory/images"
-MIN_LEVEL=40
+MIN_LEVEL=20
 TTY="/dev/tty1"
 
 get_percentage() {
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_discharge_battery.sh b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_discharge_battery.sh
new file mode 100755
index 0000000..e9ab840
--- /dev/null
+++ b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_discharge_battery.sh
@@ -0,0 +1,62 @@
+#!/bin/bash
+#
+# Copyright 2015 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.
+#
+# This script checks the battery level is high enough for shipping. If not,
+# it asks for AC power and waits for the battery to charge.
+#
+
+IMG_PATH="/usr/share/factory/images"
+MAX_LEVEL=60
+TTY="/dev/tty1"
+
+get_percentage() {
+   power_supply_info 2>/dev/null \
+   | awk '/percentage/ && !/display/ {print int($2)}'
+}
+
+# Needed by 'power_supply_info'.
+mkdir -p /var/lib/power_manager
+
+if [[ $(get_percentage) -gt $MAX_LEVEL ]]; then
+  if (ectool battery | grep -q AC_PRESENT); then
+    # If WP enabled, unplugging AC is the only way to discharge.
+    if (crossystem sw_wpsw_boot?1 wpsw_boot?1); then
+      if [ -e "${IMG_PATH}/remove_ac.png" ]; then
+        ply-image --clear 0x000000 "${IMG_PATH}/remove_ac.png"
+      else
+        echo "============================================" >"$TTY"
+        echo "========== Unplug AC adapter now. ==========" >"$TTY"
+        echo "============================================" >"$TTY"
+        echo "" >"$TTY"
+      fi
+      while (ectool battery | grep -q AC_PRESENT) ; do
+        sleep 0.5;
+      done
+    else
+      ectool chargecontrol discharge
+    fi
+  fi
+
+  if [ -e "${IMG_PATH}/discharging.png" ]; then
+    ply-image --clear 0x000000 "${IMG_PATH}/discharging.png"
+  else
+    echo "============================================" >"$TTY"
+    echo "================ Discharging ===============" >"$TTY"
+    echo "============================================" >"$TTY"
+    echo "" >"$TTY"
+  fi
+
+  # Wait for battery to discharge to MAX_LEVEL.
+  while [[ $(get_percentage) -gt $MAX_LEVEL ]]; do
+    printf "\033[0;0H\033[K" >"$TTY"
+    echo -n "Current Battery Level: $(get_percentage)%" >"$TTY"
+    sleep 1
+  done
+
+  if (ectool battery | grep -q AC_PRESENT); then
+    ectool chargecontrol normal
+  fi
+fi
diff --git a/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_discharge_voltage.sh b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_discharge_voltage.sh
new file mode 100755
index 0000000..3e1bbb8
--- /dev/null
+++ b/overlay-gandof/chromeos-base/chromeos-bsp-gandof/files/sbin/board_discharge_voltage.sh
@@ -0,0 +1,64 @@
+#!/bin/bash
+#
+# Copyright 2015 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.
+#
+# This script checks the voltage level is high enough for shipping. If not,
+# it asks for AC power and waits for the battery to charge.
+#
+
+IMG_PATH="/usr/share/factory/images"
+MAX_LEVEL=11200
+TTY="/dev/tty1"
+
+get_voltage() {
+    ectool battery 2>/dev/null \
+    | awk '/Present voltage/ {print int($3)}'
+}
+
+# Needed by 'ectool battery'.
+mkdir -p /var/lib/power_manager
+
+if [[ $(get_voltage) -gt $MAX_LEVEL ]]; then
+  if (ectool battery | grep -q AC_PRESENT); then
+    # If WP enabled, unplugging AC is the only way to discharge.
+    if (crossystem sw_wpsw_boot?1 wpsw_boot?1); then
+      if [ -e "${IMG_PATH}/remove_ac.png" ]; then
+        ply-image --clear 0x000000 "${IMG_PATH}/remove_ac.png"
+      else
+        echo "============================================" >"$TTY"
+        echo "========== Unplug AC adapter now. ==========" >"$TTY"
+        echo "============================================" >"$TTY"
+        echo "" >"$TTY"
+      fi
+      while (ectool battery | grep -q AC_PRESENT) ; do
+        sleep 0.5;
+      done
+    else
+      ectool chargecontrol discharge
+    fi
+  fi
+
+  if [ -e "${IMG_PATH}/discharging_voltage.png" ]; then
+    ply-image --clear 0x000000 "${IMG_PATH}/discharging_voltage.png"
+  else
+    echo "============================================" >"$TTY"
+    echo "================ Discharging ===============" >"$TTY"
+    echo "============================================" >"$TTY"
+    echo "" >"$TTY"
+  fi
+
+  # Wait for voltage to discharge to MAX_LEVEL.
+  while [[ $(get_voltage) -gt $MAX_LEVEL ]]; do
+    printf "\033[0;0H\033[K" >"$TTY"
+    echo -n "Current Battery Voltage: $(get_voltage)mV" >"$TTY"
+    sleep 1
+  done
+
+  # Set chargecontrol from discharge to idle if AC
+  # is still connected (when WP is disabled).
+  if (ectool battery | grep -q AC_PRESENT); then
+    ectool chargecontrol idle
+  fi
+fi