initramfs: Conditionally include and use ectool

In order to support systems without Chromium EC the recovery script is
changed to look for the ectool binary before using it, and the build
system is changed to conditionally include the ectool binary only if
the "cros_ec" USE flag is set.

BUG=chromium:913766
TEST="USE=factory_netboot_ramfs emerge-sarien chromeos-initramfs"
CQ-DEPEND=CL:1372284

Change-Id: I0c117cff5bed3dee2f79404c2392961faefe04dd
Signed-off-by: Duncan Laurie <dlaurie@google.com>
Reviewed-on: https://chromium-review.googlesource.com/1372266
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/factory_netboot/Makefile b/factory_netboot/Makefile
index 19341fc..966c621 100644
--- a/factory_netboot/Makefile
+++ b/factory_netboot/Makefile
@@ -47,12 +47,16 @@
 	/usr/bin/crossystem \
 	/usr/bin/futility \
 	/usr/sbin/chromeos-install \
-	/usr/sbin/ectool \
 	/usr/sbin/factory_*.sh \
 	/usr/sbin/mosys \
 	/usr/sbin/secure_less.sh \
 	/usr/sbin/vpd
 
+# Conditionally depend on Chromium EC utility.
+ifeq ($(INCLUDE_ECTOOL),1)
+	CROS_DEPS += /usr/sbin/ectool
+endif
+
 # Non-executables.
 DATA_DEPS=\
 	/root/.pmbr_code \
diff --git a/recovery/recovery_init.sh b/recovery/recovery_init.sh
index bc15650..f922e27 100644
--- a/recovery/recovery_init.sh
+++ b/recovery/recovery_init.sh
@@ -851,6 +851,13 @@
   return 1
 }
 
+cold_reset() {
+  # Issue cold reset via Chromium EC if ectool is available.
+  if [ -x "${USB_MNT}/usr/sbin/ectool" ]; then
+    chroot "${USB_MNT}" /usr/sbin/ectool reboot_ec cold
+  fi
+}
+
 # Checks whether the root includes a TPM firmware update and installs it if
 # applicable. Note that this must happen before locking TPM physical presence,
 # because this is the only way to recover from a failed previous firmware
@@ -893,9 +900,7 @@
       ${EXIT_CODE_SUCCESS}|${EXIT_CODE_SUCCESS_COLD_REBOOT})
         # We need to reboot to get the TPM back into operational state.
         save_log_files
-        if [ "${status}" = "${EXIT_CODE_SUCCESS_COLD_REBOOT}" ]; then
-          chroot "${USB_MNT}" /usr/sbin/ectool reboot_ec cold
-        fi
+        [ "${status}" = "${EXIT_CODE_SUCCESS_COLD_REBOOT}" ] && cold_reset
         reboot -f
         exit 0
         ;;