factory_installer: Reduce external script dependency and revise default behavior.

- netboot_postinst and ping_shopfloor have been deprecated.
- /root/factory_verify.fio can be created on the fly.
- merged factory/setup/complete_script_sample.sh, allowing LSB value
  "FACTORY_INSTALL_COMPLETE_PROMPT=false" to prevent waiting for ENTER.

BUG=None
TEST=manual.

Change-Id: I4ed38b1f60845d0bf016fab060c8b0e24d61c36e
Reviewed-on: https://chromium-review.googlesource.com/1053615
Commit-Ready: Cheng-Han Yang <chenghan@chromium.org>
Tested-by: Cheng-Han Yang <chenghan@chromium.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/factory_install.sh b/factory_install.sh
index e448207..1d4c401 100644
--- a/factory_install.sh
+++ b/factory_install.sh
@@ -36,6 +36,9 @@
 OMAHA="$(normalize_server_url "$(findLSBValue CHROMEOS_AUSERVER)")"
 # - Override this for The default action if no keys were pressed before timeout.
 DEFAULT_ACTION="$(findLSBValue FACTORY_INSTALL_DEFAULT_ACTION)"
+# - Override this to 'false' to prevent waiting for prompt after installation.
+COMPLETE_PROMPT="$(findLSBValue FACTORY_INSTALL_COMPLETE_PROMPT |
+                   tr 'A-Z' 'a-z')"
 
 # Variables prepared by image_tool or netboot initramfs code.
 NETBOOT_RAMFS="$(findLSBValue NETBOOT_RAMFS)"
@@ -290,6 +293,34 @@
   setterm -blank 0 -powersave off -powerdown 0
 }
 
+echo_huge_ok() {
+  cat <<HERE
+
+               ######                     ######         ######
+         ##################               ######        ######
+        ####################              ######       ######
+      #########      #########            ######      ######
+    ########            ########          ######     ######
+   #######                #######         ######    ######
+   ######                  ######         ######   ######
+   ######                  ######         ######  ######
+   ######                  ######         ###### ######
+   ######                  ######         ############
+   ######                  ######         ############
+   ######                  ######         ############
+   ######                  ######         ###### ######
+   ######                  ######         ######  ######
+   ######                  ######         ######   ######
+   #######                #######         ######    ######
+    ########            ########          ######     ######
+      #########      #########            ######      ######
+        ####################              ######       ######
+         ##################               ######        ######
+               ######                     ######         ######
+
+HERE
+}
+
 clear_fwwp() {
   log "Firmware Write Protect disabled, clearing status registers."
   if [ ${EC_PRESENT} -eq 1 ]; then
@@ -397,6 +428,7 @@
   log "Checking if TPM should be recovered (for version and owner)"
   # To clear TPM, we need it unlocked (only in recovery boot).
   # Booting with USB in developer mode (Ctrl-U) does not work.
+
   if crossystem "mainfw_type?recovery"; then
     if ! chromeos-tpm-recovery; then
       colorize yellow
@@ -1073,19 +1105,25 @@
     factory_install_network
   fi
 
-  log "Factory Installer Complete."
   sync
-  sleep 3
   factory_on_complete
-
   # Some installation procedure may clear or reset NVdata, so we want to ensure
   # TPM will be cleared again.
   crossystem clear_tpm_owner_request=1 || true
 
+  colorize green
+  echo_huge_ok
+  log "Factory Installer Complete."
+  sync
+  if [ "${COMPLETE_PROMPT}" != "false" ] && [ "${TTY}" != /dev/null ]; then
+    printf "Press Enter to restart... "
+    head -c 1 >/dev/null
+  else
+    sleep 3
+  fi
+
   # Default action after installation: reboot.
   trap - EXIT
-  sync
-  sleep 3
 
   # Cr50 factory mode can only be enabled when hardware write protection is
   # disabled. Assume we only do netboot in factory, so that in netboot
diff --git a/factory_shim_service.sh b/factory_shim_service.sh
index 2671dbf..23c923c 100644
--- a/factory_shim_service.sh
+++ b/factory_shim_service.sh
@@ -59,7 +59,7 @@
   # Service may be executed as 'exec' so we have to explicitly sleep here
   # otherwise kernel will panic with 'init aborted' and hard to debug.
   exec setsid sh -c \
-    "exec script -afqc 'factory_install.sh || sleep 1d' ${TTY_FILE} \
+    "exec script -afqc 'TTY=${TTY} factory_install.sh || sleep 1d' ${TTY_FILE} \
      <${TTY} >>${TTY} 2>&1"
 }
 
diff --git a/factory_verify.sh b/factory_verify.sh
index 8554cdb..9a33e51 100644
--- a/factory_verify.sh
+++ b/factory_verify.sh
@@ -221,9 +221,32 @@
       return 1
   esac
 
+  local fio_script="$(mktemp fio_config_XXXXXX)"
+  cat >"${fio_script}" <<HERE
+; Copyright 2014 The Chromium Authors. All rights reserved.
+; Use of this source code is governed by a BSD-style license that can be
+; found in the LICENSE file.
+;
+; Erase the drive with a pattern, except for the begining and the end.
+;
+[secure]
+filename=\${FIO_DEV}
+ioengine=libaio
+iodepth=32
+direct=1
+readwrite=write
+bs=256k
+
+offset=1m
+size=\${FIO_DEV_MAIN_AREA_SIZE}
+do_verify=1
+verify=md5
+verify_only=\${FIO_VERIFY_ONLY}
+HERE
   # Write a pattern on the media for future verification.
   # fio configuration file use DEV, DEV_MAIN_AREA_SIZE and VERIFY_ONLY
-  fio /root/factory_verify.fio --output "${fio_output}"
+  fio "${fio_script}" --output "${fio_output}"
+  rm -f "${fio_script}"
   fio_err=$(sed -nr "${fio_regex}" "${fio_output}")
   if [ -z "${fio_err}" ]; then
     echo "-- output of fio not understood --"
diff --git a/netboot_postinst.sh b/netboot_postinst.sh
deleted file mode 100644
index 72e1af4..0000000
--- a/netboot_postinst.sh
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/sh
-
-# This is a dummy file and should be removed after migration in
-# src/platform/initramfs is done.
diff --git a/ping_shopfloor.sh b/ping_shopfloor.sh
deleted file mode 100644
index 72e1af4..0000000
--- a/ping_shopfloor.sh
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/sh
-
-# This is a dummy file and should be removed after migration in
-# src/platform/initramfs is done.