Don't exit if which fails to find fastboot

- Remove set -e, and check for the existence of the file returned by
which. If it's not there, die with a message.
- Also, check if the partition table needed exists.

b/129903611

Change-Id: I2bfc2271385fac07b37006deb9fa064394a1648a
diff --git a/flash.sh b/flash.sh
index ff316ed..652d225 100755
--- a/flash.sh
+++ b/flash.sh
@@ -14,8 +14,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-set -e
-
 function partition_table_image {
     if [[ $1 -gt 7000000000 ]] && [[ $1 -lt 8000000000 ]]; then
         echo "partition-table-8gb.img"
@@ -31,16 +29,20 @@
     exit 1
 }
 
+function try {
+    $@ || die "Failed to execute $@"
+}
+
 ROOTDIR=$(dirname $0)/..
 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 
 FASTBOOT_CMD="$(which fastboot)"
-if [[ -z ${FASTBOOT_CMD} ]]; then
+if [[ ! -x ${FASTBOOT_CMD} ]]; then
     die "Couldn't find fastboot on your PATH -- did you install it?"
 fi
 
 if [[ -n ${SERIAL} ]]; then
-   FASTBOOT_CMD="fastboot -s ${SERIAL}"
+    FASTBOOT_CMD="fastboot -s ${SERIAL}"
 fi
 
 # Set USERSPACE_ARCH to arm64, if not set in the environment
@@ -54,10 +56,6 @@
     PRODUCT_OUT=${PRODUCT_OUT:=${ROOTDIR}/out/target/product/imx8m_phanbell}
 fi
 
-for i in u-boot.imx boot_${USERSPACE_ARCH}.img rootfs_${USERSPACE_ARCH}.img; do
-    [[ ! -f ${PRODUCT_OUT}/$i ]] && die "${PRODUCT_OUT}/$i is missing."
-done
-
 # Figure out which partition map we need based upon fastboot vars
 MMC_SIZE=$(${FASTBOOT_CMD} getvar mmc_size 2>&1 | awk '/mmc_size:/ { print $2 }')
 PART_IMAGE=$(partition_table_image ${MMC_SIZE})
@@ -66,18 +64,22 @@
     die "No partition map available for an emmc of size ${MMC_SIZE}"
 fi
 
+for i in u-boot.imx boot_${USERSPACE_ARCH}.img rootfs_${USERSPACE_ARCH}.img ${PART_IMAGE}; do
+    [[ ! -f ${PRODUCT_OUT}/$i ]] && die "${PRODUCT_OUT}/$i is missing."
+done
+
 # Flash bootloader
-${FASTBOOT_CMD} flash bootloader0 ${PRODUCT_OUT}/u-boot.imx
-${FASTBOOT_CMD} reboot-bootloader
+try ${FASTBOOT_CMD} flash bootloader0 ${PRODUCT_OUT}/u-boot.imx
+try ${FASTBOOT_CMD} reboot-bootloader
 sleep 3
 
 # Flash partition table
-${FASTBOOT_CMD} flash gpt ${PRODUCT_OUT}/${PART_IMAGE}
-${FASTBOOT_CMD} reboot-bootloader
+try ${FASTBOOT_CMD} flash gpt ${PRODUCT_OUT}/${PART_IMAGE}
+try ${FASTBOOT_CMD} reboot-bootloader
 sleep 3
 
 # Flash filesystems
-${FASTBOOT_CMD} erase misc
-${FASTBOOT_CMD} flash boot ${PRODUCT_OUT}/boot_${USERSPACE_ARCH}.img
-${FASTBOOT_CMD} flash rootfs ${PRODUCT_OUT}/rootfs_${USERSPACE_ARCH}.img
-${FASTBOOT_CMD} reboot
+try ${FASTBOOT_CMD} erase misc
+try ${FASTBOOT_CMD} flash boot ${PRODUCT_OUT}/boot_${USERSPACE_ARCH}.img
+try ${FASTBOOT_CMD} flash rootfs ${PRODUCT_OUT}/rootfs_${USERSPACE_ARCH}.img
+try ${FASTBOOT_CMD} reboot