Remove use of /root/.factory_test

We are deprecating the tag file /root/.factory_test. Instead of checking
for this tag, let's check for the following two conditions to determine
if we are running factory tests:
  - 'crossystem debug_build' is 1
  - /usr/local/factory exists

BUG=chrome-os-partner:24798
TEST=Build a factory test image without .factory_test tag. Boot and
check mount-encrypted is using factory mode.
CQ-DEPEND=CL:181430

Change-Id: Ib2045cb03e3979f3d726b4ca051fd315a8039ab9
Signed-off-by: Vic (Chun-Ju) Yang <victoryang@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/181216
Reviewed-by: Richard Barnette <jrbarnette@chromium.org>
diff --git a/chromeos_startup b/chromeos_startup
index 685877d..62d0b6e 100755
--- a/chromeos_startup
+++ b/chromeos_startup
@@ -47,10 +47,8 @@
 # different for different targets (e.g., regular Chrome OS vs. embedded).
 . /usr/share/cros/startup_utils.sh
 
-FACTORY_MODE=
-if [ -f /root/.factory_test -o -f /root/.factory_installer ]; then
-  FACTORY_MODE=factory
-fi
+# Factory related functions
+. /usr/share/cros/factory_utils.sh
 
 mkdir -p /dev/shm /dev/pts
 mount -n -t tmpfs -o nodev,noexec,nosuid shmfs /dev/shm
@@ -143,6 +141,13 @@
   date 010200001970.00
 fi
 
+# Now that stateful partition is mounted, we can check if we are in factory
+# mode.
+FACTORY_MODE=
+if is_factory_mode; then
+  FACTORY_MODE=factory
+fi
+
 # CROS_DEBUG equals one if we've booted in developer mode or we've
 # booted a developer image.
 crossystem "cros_debug?1"
diff --git a/factory_utils.sh b/factory_utils.sh
new file mode 100644
index 0000000..8b0f5f4
--- /dev/null
+++ b/factory_utils.sh
@@ -0,0 +1,16 @@
+# Copyright (c) 2013 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.
+
+is_factory_mode() {
+  # The path to factory enabled tag. If this path exists in a debug build,
+  # we assume factory test mode.
+  FACTORY_TAG_PATH="/mnt/stateful_partition/dev_image/factory/enabled"
+
+  # Factory mode can be either:
+  # 1. We are running factory tests if this is a debug build and factory
+  #    enabled tag exists.
+  # 2. We are running a factory installer.
+  (crossystem "debug_build?1" && [ -f "$FACTORY_TAG_PATH" ]) ||
+      [ -f /root/.factory_installer ]
+}