blob: 207184eef4ec7bab074176cacf710602f6e2aab7 [file] [log] [blame]
#!/bin/bash
# $1 -- Filesystem path
# $2 -- Block device
function check_emmc_matches {
local CURRENT=$(mktemp)
local SIZE=$(stat -L -c%s $1)
dd if=/dev/$2 of=${CURRENT} \
count=${SIZE} \
bs=512 \
iflag=count_bytes
diff ${CURRENT} $1
local DIFF_RETURN_CODE=$?
rm ${CURRENT}
return ${DIFF_RETURN_CODE}
}
# $1 -- Filesystem path
# $2 -- Block device
function write_emmc {
for i in `seq 1 5`
do
dd if=$1 of=/dev/$2 bs=512
$(check_emmc_matches $1 $2)
local EMMC_MATCHES=$?
if [[ ${EMMC_MATCHES} -eq 0 ]]; then
UPDATE_SUCCESS=true
break
fi
done
echo ${UPDATE_SUCCESS}
}
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
# 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/bl2.img mmcblk0boot0)
# Restore whatever the setting was before
echo ${MMCBLK_RO} > ${MMCBLK_RO_PATH}
if [[ "${UPDATE_SUCCESS}" != true ]]; then
echo "Failed to update bl2! Rebooting is unsafe!"
exit 1
fi
fi
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!"
exit 1
fi
fi
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
fi
fi