vpd_get_value: Support reading VPD value even if dump_vpd_log failed.

Some upstart jobs, for example accelerator-init, may be started by
udev-early before chromeos_startup is finished, which implies
/mnt/stateful_partition is not available so calling dump_vpd_log won't
get cache generated.

As a result, vpd_get_value should fallback to
'dump_vpd_log --full --stdout' and use the output.

BRANCH=None
BUG=chrome-os-partner:62342
TEST=rm -rf /mnt/stateful_partition/unencrypted/cache/vpd
     touch /mnt/stateful_partition/unencrypted/cache/vpd
     vpd_get_value region

Change-Id: I24ad8b5cc2432e94da2504ea3d522cec56f6f038
Reviewed-on: https://chromium-review.googlesource.com/448396
Commit-Ready: Hung-Te Lin <hungte@chromium.org>
Tested-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
diff --git a/util/vpd_get_value b/util/vpd_get_value
index 99de55e..2e37fec 100755
--- a/util/vpd_get_value
+++ b/util/vpd_get_value
@@ -11,6 +11,7 @@
 # CACHE_FILE is defined from dump_vpd_log.
 CACHE_FILE=/mnt/stateful_partition/unencrypted/cache/vpd/full-v2.txt
 SYS_VPD=/sys/firmware/vpd
+TMP_FILE=
 
 : "${VPD_IGNORE_CACHE:=}"
 : "${VPD_IGNORE_SYS_CACHE}"
@@ -42,11 +43,16 @@
       cat_if_exists "${SYS_VPD}/rw/${key}"
   else
     if [ ! -f "${CACHE_FILE}" ]; then
-      # /etc/init/vpd-log.conf was not executed, or cache corrupted.
-      dump_vpd_log
+      # /etc/init/vpd-log.conf was not executed, or cache corrupted, or if
+      # system stateful partition was not mounted yet.
+      TMP_FILE="$(mktemp)"
+      dump_vpd_log --full --stdout >"${TMP_FILE}"
+      CACHE_FILE="${TMP_FILE}"
     fi
-    # Read from cache
     sed -n "s/^\"${key}\"=\"\(.*\)\"\$/\1/p" "${CACHE_FILE}" | head -n 1
+    if [ -e "${TMP_FILE}" ]; then
+      rm -f "${TMP_FILE}"
+    fi
   fi
 }
 main "$@"