Enable Wacom FW update for Zerg systems

Previously, Wacom's firmware updater would simply look at a fixed
filename in /lib/firmware for the firmware binary when it tried to
update.  This imposes a one-wacom-device-per-build limitation since
we can't include more than one wacom fw binary per build.  As Zerg
builds (more than one device sharing a build) become more common,
we need to allow our firmware updaters to support this.  Since Wacom
doesn't support a read-only product_id like our other touch devices,
we have to make due with what we can by implimenting a similar hack
as was used to differentiate Kip and Kip+ when we made the first
Zerg build -- looking at VPD to tell which device we're on.

This CL appends the "customization_id" to the filename when looking
up the Wacom firmware filename in a similar fashion to how we use
the product_id on other devices.  If such a firmware isn't found, it
fails back to the old static filename for backwards compatability.

This relaxes the restriction from one-wacom-device-per-build to
one-wacom-device-per-customization-id, which is still more
restrictive than is ideal but is the best we can do with the hardware
at this time.

BUG=chrome-os-partner:60010
TEST=manually emerged and deployed on a non-zerg Wacom system and the
updater still works smoothly.  Similarly I tested it on Kip to make
sure it didn't regress and that VPD was loaded smoothly.

Change-Id: I780c8a0e07269b44b2ab318817d0e87789e1ce6e
Signed-off-by: Charlie Mooney <charliemooney@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/414252
Reviewed-by: Andrew de los Reyes <adlr@chromium.org>
diff --git a/scripts/chromeos-elan-touch-firmware-update.sh b/scripts/chromeos-elan-touch-firmware-update.sh
index e301607..9834d5c 100644
--- a/scripts/chromeos-elan-touch-firmware-update.sh
+++ b/scripts/chromeos-elan-touch-firmware-update.sh
@@ -17,8 +17,6 @@
 ELANTS_I2C_FW_VERSION_SYSFS="fw_version"
 ELANTS_I2C_PRODUCT_ID_SYSFS="hw_version"
 
-VPD_CACHE_FILE="/mnt/stateful_partition/unencrypted/cache/vpd/filtered.txt"
-
 # Parse command line
 FLAGS "$@" || exit 1
 eval set -- "${FLAGS_ARGV}"
@@ -73,14 +71,10 @@
   # we see that value and the device is Kip, we need to differentiate them
   # and manually set the product ID to recover the touchpad. crosbug.com/p/46266
   if [  -n "$(echo ${board} | grep "^kip")" ] && [ "${reported_product_id}" = "255.0" ]; then
-    if [ -e "${VPD_CACHE_FILE}" ]; then
-      local customization_id="$(sed -nre 's/^"customization_id"="(.*)"$/\1/p' \
-                                < ${VPD_CACHE_FILE})"
-      if [ "${customization_id}" = "HP-KIP14" ]; then
-        echo "72.0"
-      else
-        echo "75.0"
-      fi
+    if [ "$(get_customization_id)" = "HP-KIP14" ]; then
+      echo "72.0"
+    else
+      echo "75.0"
     fi
 
   # If an elan device is reporting 255.0 and there is only an elan_i2c.bin FW
diff --git a/scripts/chromeos-touch-common.sh b/scripts/chromeos-touch-common.sh
index 5616735..5a10fda 100644
--- a/scripts/chromeos-touch-common.sh
+++ b/scripts/chromeos-touch-common.sh
@@ -219,3 +219,12 @@
 
   echo "${update_type}"
 }
+
+
+get_customization_id() {
+  # Get the "customization id" from the Chromebook's VPD.  This ID is
+  # mandatory for Zerg systems (where 2 different devices use the same build)
+  # and can be used to differentiate them.  If the entry doesn't exist, this
+  # will simply not print anything.
+  dump_vpd_log --stdout | grep '^"customization_id"=".*"$' | cut -d '"' -f 4
+}
diff --git a/scripts/chromeos-wacom-touch-firmware-update.sh b/scripts/chromeos-wacom-touch-firmware-update.sh
index db1d456..526256b 100755
--- a/scripts/chromeos-wacom-touch-firmware-update.sh
+++ b/scripts/chromeos-wacom-touch-firmware-update.sh
@@ -10,7 +10,7 @@
 DEFINE_boolean 'recovery' ${FLAGS_FALSE} "Recovery. Allows for rollback" 'r'
 DEFINE_string 'device' '' "device_name" 'd'
 
-FW_LINK_PATH="/lib/firmware/wacom_firmware.hex"
+FW_LINK_BASE="wacom_firmware.hex"
 WACOMFLASH="/usr/sbin/wacom_flash"
 GET_ACTIVE_FIRMVER="-a"
 
@@ -21,12 +21,11 @@
 update_firmware() {
   # Actually trigger a firmware update by running the Wacom update tool
   # in minijail to limit the syscalls it can access.
+  local fw_link="$1"
   local cmd_log=""
   cmd_log="$(
     minijail0 -S /opt/google/touch/policies/wacom_flash.update.policy \
-      "${WACOMFLASH}" "${FW_LINK_PATH}" \
-      ${FLAGS_recovery} ${FLAGS_device} \
-      2>&1
+      "${WACOMFLASH}" "${fw_link}" ${FLAGS_recovery} ${FLAGS_device} 2>&1
   )"
 
   if [ "$?" -ne 0 ]; then
@@ -36,16 +35,18 @@
 
 get_fw_version_from_disk() {
   # The on-disk FW version is determined by reading the filename which
-  # is in the format "wacom_FWVERSION.hex".  This function just
-  # strips away everything but the FW version.
+  # is in the format "wacom_FWVERSION.hex".  We follow the fw's link
+  # to the actual file then strip away everything in the FW's filename
+  # but the FW version.
+  local fw_link="$1"
   local fw_filepath=""
   local fw_filename=""
   local fw_ver=""
 
-  if [ ! -L "${FW_LINK_PATH}" ]; then
+  if [ ! -L "${fw_link}" ]; then
     return
   fi
-  fw_filepath="$(readlink -f "${FW_LINK_PATH}")"
+  fw_filepath="$(readlink -f "${fw_link}")"
   if [ ! -e "${fw_filepath}" ]; then
     return
   fi
@@ -62,7 +63,7 @@
 
   active_fw_ver="$(
     minijail0 -S /opt/google/touch/policies/wacom_flash.query.policy \
-      "${WACOMFLASH}" "${FW_LINK_PATH}" "${GET_ACTIVE_FIRMVER}" \
+      "${WACOMFLASH}" "dummy_unused_argument" "${GET_ACTIVE_FIRMVER}" \
       "${FLAGS_device}"
   )"
   if [ "$?" -eq 0 ]; then
@@ -104,10 +105,12 @@
   local update_type=""
   local update_needed=""
 
-  log_msg "Attempting to Load FW: '${FW_LINK_PATH}'"
+  local customization_id="$(get_customization_id)"
+  local fw_link="$(find_fw_link_path "${customization_id}" "${FW_LINK_BASE}")"
+  log_msg "Attempting to Load FW: '${fw_link}'"
 
   active_fw_ver="$(get_active_fw_version)"
-  new_fw_ver="$(get_fw_version_from_disk)"
+  new_fw_ver="$(get_fw_version_from_disk "${fw_link}")"
   log_msg "Active firmware version: ${active_fw_ver}"
   log_msg "New firmware version: ${new_fw_ver}"
   if [ -z "${active_fw_ver}" ]; then
@@ -123,7 +126,7 @@
 
   if [ "${update_needed}" -eq "${FLAGS_TRUE}" ]; then
     log_msg "Update FW to ${new_fw_ver}"
-    update_firmware
+    update_firmware "${fw_link}"
 
     # Check if update was successful
     active_fw_ver="$(get_active_fw_version)"