chroemos-cr50-scripts: support dual Cr50 image configurations

We want to be able to tell what Cr50 image (prod vs pre-pvt) to run on
a Chrome OS device based pn the Board ID flags programmed in the H1.

Both init scripts and postinstall/factory install scripts need to be
able to determine the type of Cr50 image to use at run time.

The new script implements the logic, it retrieves the Board ID flags
from the H1 and checks if the 0x10 bit is set, which is the indication
that the pre-pvt Cr50 image should be used.

Also removed use of BOARD ind cr50-update.conf, as it is not necessary
any more.

BUG=b:65628825
TEST=on a Coral verified that Cr50 is updated successfully from 0.0.22
  to 0.1.0 or 0.0.24 based on the Board ID flag value, both in
  postinstall and init cases.

Change-Id: I9bf47f81642fdf2fa717d0ebd734bd6706ccac6b
Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/846474
Reviewed-by: Nick Sanders <nsanders@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/875128
Commit-Queue: Mary Ruthven <mruthven@chromium.org>
Tested-by: Mary Ruthven <mruthven@chromium.org>
diff --git a/chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1-r9.ebuild b/chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1-r12.ebuild
similarity index 100%
rename from chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1-r9.ebuild
rename to chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1-r12.ebuild
diff --git a/chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1.ebuild b/chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1.ebuild
index d2bb8f3..03f0449 100644
--- a/chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1.ebuild
+++ b/chromeos-base/chromeos-cr50-scripts/chromeos-cr50-scripts-0.0.1.ebuild
@@ -22,6 +22,7 @@
 	doins "${FILESDIR}"/cr50-result.conf
 
 	exeinto /usr/share/cros
-	doexe "${FILESDIR}"/cr50-update.sh
+	doexe "${FILESDIR}"/cr50-get-name.sh
 	doexe "${FILESDIR}"/cr50-set-board-id.sh
+	doexe "${FILESDIR}"/cr50-update.sh
 }
diff --git a/chromeos-base/chromeos-cr50-scripts/files/cr50-get-name.sh b/chromeos-base/chromeos-cr50-scripts/files/cr50-get-name.sh
new file mode 100644
index 0000000..cb54af0
--- /dev/null
+++ b/chromeos-base/chromeos-cr50-scripts/files/cr50-get-name.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+# 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.
+#
+# This helper script is sourced by init and postinstall scripts.
+#
+
+#
+# cr50_get_name
+#
+# Find out which if the two available Cr50 images should be used. The only
+# required command line parameter is the string, a command used to communicate
+# with Cr50 (different invocations are used in case of init and postinstall).
+#
+# The output is the file name of the Cr50 image to use printed to stdout.
+#
+cr50_get_name() {
+  local board_flags
+  local cr50_image_base_name="/opt/google/cr50/firmware/cr50.bin"
+  local ext="prod"  # Prod is a safer default option.
+  local logger_tag="cr50_get_name"
+  local updater="$1"
+
+  logger -t "${logger_tag}" "updater is ${updater}"
+
+  # Determine the type of the Cr50 image to use based on the H1's board ID
+  # flags. The hexadecimal value of flags is reported by 'gsctool -i' in the
+  # last element of a colon separated string of values.
+  board_flags="0x$(${updater} -i | sed 's/.*://')"
+
+  if [ -z "${board_flags}" ]; then
+    # Any error in detecting board flags will force using the prod image,
+    # which the safe option.
+    logger -t  "${logger_tag}" "error detecting board ID flags"
+  else
+    local pre_pvt
+
+    # Flag bit 0x10 is the indication that this is a pre-pvt device.
+    pre_pvt=$(( board_flags & 0x10 ))
+
+    if [ "${pre_pvt}" = "16" ]; then
+      ext="prepvt"
+    fi
+  fi
+
+  logger -t "${logger_tag}" \
+    "board_flags: '${board_flags}', extension: '${ext}'"
+
+  printf "${cr50_image_base_name}.${ext}"
+}
diff --git a/chromeos-base/chromeos-cr50-scripts/files/cr50-update.conf b/chromeos-base/chromeos-cr50-scripts/files/cr50-update.conf
index a4a3d1a..9daf4cf 100644
--- a/chromeos-base/chromeos-cr50-scripts/files/cr50-update.conf
+++ b/chromeos-base/chromeos-cr50-scripts/files/cr50-update.conf
@@ -29,11 +29,12 @@
 
 script
 
+  . /usr/share/cros/cr50-get-name.sh
+
   logit() {
     logger -t ${UPSTART_JOB} "$*"
   }
 
-  CR50_IMAGE="/opt/google/cr50/firmware/cr50.bin.prod"
   CROSSYSTEM="/usr/bin/crossystem"
   STATE_DIR="/var/cache"
   UPDATER="/usr/sbin/gsctool"
@@ -52,16 +53,9 @@
     exit 0
   fi
 
-  BOARD="$(grep CHROMEOS_RELEASE_BOARD /etc/lsb-release)"
-  BOARD="${BOARD#*=}"
   VERSIONF="${STATE_DIR}/cr50-version"
 
-  if [ -z "${BOARD}" ]; then
-    logit "Could not determine board type"
-    exit 4
-  fi
-
-  logit "running on ${BOARD}"
+  CR50_IMAGE="$(cr50_get_name "${UPDATER} -s")"
 
   if [ ! -f "${CR50_IMAGE}" ]; then
     logit "${CR50_IMAGE} not found"
diff --git a/chromeos-base/chromeos-cr50-scripts/files/cr50-update.sh b/chromeos-base/chromeos-cr50-scripts/files/cr50-update.sh
index 0d7bc7d..0184c37 100755
--- a/chromeos-base/chromeos-cr50-scripts/files/cr50-update.sh
+++ b/chromeos-base/chromeos-cr50-scripts/files/cr50-update.sh
@@ -10,8 +10,11 @@
 # tries updating the H1 with the new cr50 image.
 
 script_name="$(basename "$0")"
+script_dir="$(dirname "$0")"
 pid="$$"
 
+. "${script_dir}/cr50-get-name.sh"
+
 # The mount point of the new image is passed in as the first parameter.
 root="$1"
 
@@ -22,12 +25,6 @@
 
 logit "Starting"
 
-CR50_IMAGE="${root}/opt/google/cr50/firmware/cr50.bin.prod"
-if [ ! -f "${CR50_IMAGE}" ]; then
-  logit "${CR50_IMAGE} not found, quitting."
-  exit 1
-fi
-
 GSCTOOL="/usr/sbin/gsctool"
 # Let's determine the best way to communicate with the Cr50.
 if "${GSCTOOL}" -f > /dev/null 2>&1; then
@@ -44,6 +41,12 @@
   exit 1
 fi
 
+CR50_IMAGE="$(cr50_get_name "${UPDATER}")"
+if [ ! -f "${CR50_IMAGE}" ]; then
+  logit "${CR50_IMAGE} not found, quitting."
+  exit 1
+fi
+
 retries=0
 while true; do
   output="$(${UPDATER} -p "${CR50_IMAGE}" 2>&1)"