#! /bin/sh
# Logilin update system - Logilin 2020

# If the following files exists but returns error when executed,
# the boot will not be considered as correct.
# Use it to check that all the condititions needed for correct
# work are respected.
CHECK_CORRECT_BOOT_FILE=/usr/local/bin/check-correct-boot

if [ -e "${CHECK_CORRECT_BOOT_FILE}" ]
then
	if ! "${CHECK_CORRECT_BOOT_FILE}"
	then
		exit 0
	fi
fi


mount /boot -o rw

# Hack for Banana Pi/Pro-based boards:
# ===================================
# We erase the `serial#` env variable, so at next reboot U-boot will be
# forced to re-read the Serial Number efuses, and not keep those on SD-card.
# This is needed to generate unique MAC address, even if the SD-card is
# used successively on different boards.
# To do this, a patch on `fw_setenv` authorizes the deletion of `serial#`
# even if it's a variable protected by U-boot.
if fw_printenv serial# >/dev/null 2>&1
then
	fw_setenv serial# "" >/dev/null 2>&1
fi

line=$(fw_printenv current_boot)
if [ $? -eq 0 ]
then
	export $line
else
	current_boot=$(lsblk | grep 'part[[:blank:]]\+/$' | sed 's/^.*mmcblk.p\(.\).*$/\1/')
fi
fw_setenv boot_next_1 ${current_boot}
fw_setenv boot_next_2 ${current_boot}
fw_setenv boot_next_3 ${current_boot}
umount /boot

exit 0
