| #!/bin/bash |
| |
| # $1 -- Filesystem path |
| # $2 -- Block device |
| function check_emmc_matches { |
| local CURRENT=$(mktemp) |
| local SIZE=$(stat -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} |
| echo ${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 |
| local EMMC_MATCHES=$(check_emmc_matches $1 $2) |
| if [[ ${EMMC_MATCHES} -eq 0 ]]; then |
| UPDATE_SUCCESS=true |
| break |
| fi |
| done |
| 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) |
| |
| 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_MATCHS} -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_MATCHS} -ne 0 ]]; then |
| UPDATE_SUCCESS=$(write_emmc /boot/u-boot-env.bin mmcblk0boot1) |
| if [[ "${UPDATE_SUCCESS}" != true ]]; then |
| echo "Failed to update u-boot-env.bin! Rebooting is unsafe!" |
| exit 1 |
| fi |
| fi |