CHERRY-PICK: Candy: Add wipe option support per factory_wipe_option

1. battery_cut_off: add discharge battery to ensure battery capacity is
in desired range before battery cut-off
2. shutdown: directly shutdown after wiping
3. rma_battery_cut_off: skip checking battery capacity for rma battery
cut-off

BUG=chrome-os-partner:35369
TEST=Factory wipe and check the different wipe option behavior

Change-Id: I53be31f6a2be72d59820f2e69d73c172771ff08a
Reviewed-on: https://chromium-review.googlesource.com/240883
Reviewed-by: Bowgo Tsai <bowgotsai@chromium.org>
Commit-Queue: JAMES C Chen <james.c.chen@intel.com>
Tested-by: JAMES C Chen <james.c.chen@intel.com>
Reviewed-on: https://chromium-review.googlesource.com/244283
Commit-Queue: Huki Huang <huki.huang@intel.com>
Tested-by: Nicole Li <nicole.li@intel.com>
diff --git a/overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1-r3.ebuild b/overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1-r4.ebuild
similarity index 100%
rename from overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1-r3.ebuild
rename to overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1-r4.ebuild
diff --git a/overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1.ebuild b/overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1.ebuild
index e950d4f..10dfb46 100644
--- a/overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1.ebuild
+++ b/overlay-candy/chromeos-base/chromeos-bsp-candy/chromeos-bsp-candy-0.0.1.ebuild
@@ -34,6 +34,8 @@
 	dosbin "${FILESDIR}/board_factory_wipe.sh"
 	dosbin "${FILESDIR}/board_factory_reset.sh"
 	dosbin "${FILESDIR}/board_charge_battery.sh"
+        dosbin "${FILESDIR}/board_discharge_battery.sh"
+        dosbin "${FILESDIR}/rma_battery_cut_off.sh"
 
 	insinto "/usr/share/factory/images"
 	doins "${FILESDIR}/remove_ac.png"
@@ -41,6 +43,7 @@
 	doins "${FILESDIR}/cutoff_failed.png"
 	doins "${FILESDIR}/charging.png"
 	doins "${FILESDIR}/connect_ac.png"
+        doins "${FILESDIR}/discharging.png"
 
 	# Install Bluetooth ID override.
 	insinto "/etc/bluetooth"
diff --git a/overlay-candy/chromeos-base/chromeos-bsp-candy/files/battery_cut_off.sh b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/battery_cut_off.sh
index 3079dcf..fb1ab2a 100755
--- a/overlay-candy/chromeos-base/chromeos-bsp-candy/files/battery_cut_off.sh
+++ b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/battery_cut_off.sh
@@ -25,6 +25,9 @@
   done
 fi
 
+# Discharge battery to ensure battery capacity in desired range
+/usr/sbin/board_discharge_battery.sh
+
 if [ -e "${IMG_PATH}/cutting_off.png" ]; then
   ply-image --clear 0x000000 "${IMG_PATH}/cutting_off.png"
 else
diff --git a/overlay-candy/chromeos-base/chromeos-bsp-candy/files/board_discharge_battery.sh b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/board_discharge_battery.sh
new file mode 100755
index 0000000..e9ab840
--- /dev/null
+++ b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/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-candy/chromeos-base/chromeos-bsp-candy/files/board_factory_wipe.sh b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/board_factory_wipe.sh
index 79a0fc1..6563aea 100644
--- a/overlay-candy/chromeos-base/chromeos-bsp-candy/files/board_factory_wipe.sh
+++ b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/board_factory_wipe.sh
@@ -14,7 +14,21 @@
 sleep 3
 
 # this script is called by clobber-state
-/usr/sbin/battery_cut_off.sh
+for WIPE_OPTION in "$@"; do
+  if [ "$WIPE_OPTION" = "battery_cut_off" ]; then
+    # battery cut-off after factory wipe-out
+    echo "start battery cut-off"
+    /usr/sbin/battery_cut_off.sh
+  elif [ "$WIPE_OPTION" = "shutdown" ]; then
+    # shutdown after factory wipe-out
+    echo "start system shutdown"
+    /sbin/shutdown -h now
+  elif [ "$WIPE_OPTION" = "rma_battery_cut_off" ]; then
+    # rma shim battery cut-off
+    echo "start rma shim battery cut-off"
+    /usr/sbin/rma_battery_cut_off.sh
+  fi
+done
 
 # Battery cut-off is failed if it returns with 1.
 if [ $? -eq 1 ]; then
diff --git a/overlay-candy/chromeos-base/chromeos-bsp-candy/files/discharging.png b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/discharging.png
new file mode 100644
index 0000000..5969704
--- /dev/null
+++ b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/discharging.png
Binary files differ
diff --git a/overlay-candy/chromeos-base/chromeos-bsp-candy/files/rma_battery_cut_off.sh b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/rma_battery_cut_off.sh
new file mode 100755
index 0000000..95945a4
--- /dev/null
+++ b/overlay-candy/chromeos-base/chromeos-bsp-candy/files/rma_battery_cut_off.sh
@@ -0,0 +1,49 @@
+#!/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 uses ectool to send command to EC to cut off the battery power.
+#
+
+IMG_PATH="/usr/share/factory/images"
+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"
+  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
+fi
+
+if [ -e "${IMG_PATH}/cutting_off.png" ]; then
+  ply-image --clear 0x000000 "${IMG_PATH}/cutting_off.png"
+else
+  echo "===============================================" >"$TTY"
+  echo "==== Cutting off battery. Wait 10 seconds. ====" >"$TTY"
+  echo "===============================================" >"$TTY"
+fi
+ectool batterycutoff
+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"
+fi
+
+sleep 1d
+exit 1