Generate demo mode resources during image build

Updates build_image to create imageloader supported image from test demo
mode resourced directory created under local rootfs build directory
(which are finalized during base image finalization), and install them
to the test image.
Also, generates standalone demo mode resources artifact (that is
intended to eventually be added to appropriate images' stateful
partitions during factory flow).

BUG=chromium:827363
TEST=build image

Change-Id: Ie05e71e966b755926a80a2405d3a3ca736a024de
Reviewed-on: https://chromium-review.googlesource.com/1069997
Commit-Ready: Toni Barzic <tbarzic@chromium.org>
Tested-by: Toni Barzic <tbarzic@chromium.org>
Reviewed-by: Wei-Han Chen <stimim@chromium.org>
diff --git a/build_image b/build_image
index 4334b16..3f095b7 100755
--- a/build_image
+++ b/build_image
@@ -116,6 +116,7 @@
 . "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1
 . "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1
 . "${BUILD_LIBRARY_DIR}/build_image_util.sh" || exit 1
+. "${BUILD_LIBRARY_DIR}/imageloader_util.sh" || exit 1
 . "${BUILD_LIBRARY_DIR}/base_image_util.sh" || exit 1
 . "${BUILD_LIBRARY_DIR}/dev_image_util.sh" || exit 1
 . "${BUILD_LIBRARY_DIR}/test_image_util.sh" || exit 1
diff --git a/build_library/base_image_util.sh b/build_library/base_image_util.sh
index b11405c..4d590cd 100755
--- a/build_library/base_image_util.sh
+++ b/build_library/base_image_util.sh
@@ -140,6 +140,34 @@
       --strip-components=3 "${libc_excludes[@]/#/--exclude=}"
 }
 
+# Generates imageloader images containing demo mode resources.
+# Generates two imageloader images under BUILD_DIR:
+#   * standalone demo resources - archive containing demo resources to be added
+#     to demo mode enabled release images. The demo mode resources will be added
+#     to target image stateful partition during factory flow.
+#   * test demo resources - demo resources that will be moved to the test images
+#     stateful partition during mod_image_for_test.
+generate_demo_mode_resources_images() {
+  local demo_resources_src="$1"
+
+  # Used to enable demo mode on test images, but with reduced set of demo apps.
+  if [[ -d "${demo_resources_src}/test" ]]; then
+    mkdir -p  "${BUILD_DIR}/test_demo_resources"
+    generate_imageloader_image "0.0.1" \
+        "${demo_resources_src}/test/image" \
+        "${BUILD_DIR}/test_demo_resources"
+  fi
+
+  if [[ -d "${demo_resources_src}/standalone" ]]; then
+    # Used to generate the demo resources archive to be bundled with release
+    # images in factory - these are intended to contain the full set of demo
+    # mode apps.
+    generate_and_tar_imageloader_image "0.0.1" \
+        "${demo_resources_src}/standalone/image" \
+        "${BUILD_DIR}/demo_resources.tar.gz"
+  fi
+}
+
 create_base_image() {
   local image_name=$1
   local rootfs_verification_enabled=$2
@@ -351,6 +379,9 @@
     fi
   fi
 
+  generate_demo_mode_resources_images \
+      "${root_fs_dir}/build/rootfs/chromeos-assets/demo_resources"
+
   # Clean up symlinks so they work on a running target rooted at "/".
   # Here development packages are rooted at /usr/local.  However, do not
   # create /usr/local or /var on host (already exist on target).
diff --git a/build_library/imageloader_util.sh b/build_library/imageloader_util.sh
index 47fc64f..603264d 100755
--- a/build_library/imageloader_util.sh
+++ b/build_library/imageloader_util.sh
@@ -95,3 +95,18 @@
   generate_imageloader_manifest "${version}" "${output}"
   return 0
 }
+
+# Creates a tar archive that contains imageloder supported image of a
+# directory. See |generate_imageloader_image| for more info.
+generate_and_tar_imageloader_image() {
+  local version="$1"
+  local src="$2"
+  local output="$3"
+
+  local image=$(mktemp -d)
+
+  generate_imageloader_image "0.0.1" "${src}" "${image}"
+  tar caf "${output}" -C "${image}" \
+      image.squash table imageloader.json imageloader.sig.2
+  rm -rf "${image}"
+}
diff --git a/mod_for_test_scripts/250enableForDemoMode b/mod_for_test_scripts/250enableForDemoMode
new file mode 100755
index 0000000..816d6d4
--- /dev/null
+++ b/mod_for_test_scripts/250enableForDemoMode
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# Copyright 2018 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.
+#
+if [[ -d "${BUILD_DIR}/test_demo_resources" ]]; then
+  mv "${BUILD_DIR}/test_demo_resources" \
+      "${STATEFUL_DIR}/unencrypted/demo_mode_resources"
+fi