Fixes for install-bootloader.sh

- Fix stat not following through symlinks
- Fix misspelled variable names
- Get return code of diff, instead of stdout

Change-Id: I83133998f778549535c47d2875e222a4456f1850
diff --git a/debian/changelog b/debian/changelog
index 889755a..789472e 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+excelsior-bootloader (2-1) mendel-eagle; urgency=medium
+
+  * Fix install-bootloader.sh
+
+ -- Coral Team <coral-support@google.com>  Mon, 21 Sep 2020 16:28:18 -0700
+
 excelsior-bootloader (1-2) mendel-day; urgency=medium
 
   * Add support for both 1G memory and 2G memory.
diff --git a/usr/sbin/install-bootloader.sh b/usr/sbin/install-bootloader.sh
index cb14d8b..207184e 100755
--- a/usr/sbin/install-bootloader.sh
+++ b/usr/sbin/install-bootloader.sh
@@ -4,7 +4,7 @@
 # $2 -- Block device
 function check_emmc_matches {
   local CURRENT=$(mktemp)
-  local SIZE=$(stat -c%s $1)
+  local SIZE=$(stat -L -c%s $1)
   dd if=/dev/$2 of=${CURRENT} \
     count=${SIZE} \
     bs=512 \
@@ -12,7 +12,7 @@
   diff ${CURRENT} $1
   local DIFF_RETURN_CODE=$?
   rm ${CURRENT}
-  echo ${DIFF_RETURN_CODE}
+  return ${DIFF_RETURN_CODE}
 }
 
 # $1 -- Filesystem path
@@ -21,7 +21,8 @@
   for i in `seq 1 5`
   do
     dd if=$1 of=/dev/$2 bs=512
-    local EMMC_MATCHES=$(check_emmc_matches $1 $2)
+    $(check_emmc_matches $1 $2)
+    local EMMC_MATCHES=$?
     if [[ ${EMMC_MATCHES} -eq 0 ]]; then
       UPDATE_SUCCESS=true
       break
@@ -30,9 +31,12 @@
   echo ${UPDATE_SUCCESS}
 }
 
-BL2_MATCHES=$(check_emmc_matches /boot/bl2.img mmcblk0boot0)
-FIP_BIN_MATCHES=$(check_emmc_matches /boot/fip.bin mmcblk0p1)
-U_BOOT_ENV_MATCHES=$(check_emmc_matches /boot/u-boot-env.bin mmcblk0boot1)
+check_emmc_matches /boot/bl2.img mmcblk0boot0
+BL2_MATCHES=$?
+check_emmc_matches /boot/fip.bin mmcblk0p1
+FIP_BIN_MATCHES=$?
+check_emmc_matches /boot/u-boot-env.bin mmcblk0boot1
+U_BOOT_ENV_MATCHES=$?
 
 if [[ ${BL2_MATCHES} -ne 0 ]]; then
   MMCBLK_RO_PATH=/sys/block/mmcblk0boot0/force_ro
@@ -54,7 +58,7 @@
   fi
 fi
 
-if [[ ${FIP_BIN_MATCHS} -ne 0 ]]; then
+if [[ ${FIP_BIN_MATCHES} -ne 0 ]]; then
   UPDATE_SUCCESS=$(write_emmc /boot/fip.bin mmcblk0p1)
   if [[ "${UPDATE_SUCCESS}" != true ]]; then
     echo "Failed to update fip.bin! Rebooting is unsafe!"
@@ -62,8 +66,20 @@
   fi
 fi
 
-if [[ ${U_BOOT_ENV_MATCHS} -ne 0 ]]; then
+if [[ ${U_BOOT_ENV_MATCHES} -ne 0 ]]; then
+  MMCBLK_RO_PATH=/sys/block/mmcblk0boot1/force_ro
+
+  # Get the read-only setting for the bootloader
+  MMCBLK_RO=$(cat ${MMCBLK_RO_PATH})
+
+  # Disable read-only on bootloader block device
+  echo 0 > ${MMCBLK_RO_PATH}
+
   UPDATE_SUCCESS=$(write_emmc /boot/u-boot-env.bin mmcblk0boot1)
+
+  # Restore whatever the setting was before
+  echo ${MMCBLK_RO} > ${MMCBLK_RO_PATH}
+
   if [[ "${UPDATE_SUCCESS}" != true ]]; then
     echo "Failed to update u-boot-env.bin! Rebooting is unsafe!"
     exit 1