dualboot: update-firmware: rework the script to use functions and avoid repetitions

Signed-off-by: Tatiana Leon <Tatiana.Leon@digi.com>
This commit is contained in:
Tatiana Leon 2023-03-21 17:31:45 +01:00
parent e37d52ebd7
commit 006138e134
1 changed files with 118 additions and 92 deletions

View File

@ -24,7 +24,14 @@ SCRIPTNAME="$(basename "$(readlink -f "${0}")")"
VERBOSE="" VERBOSE=""
PUBLIC_KEY="/etc/ssl/certs/key.pub" PUBLIC_KEY="/etc/ssl/certs/key.pub"
ACTIVE_SYSTEM="$(fw_printenv -n active_system 2>/dev/null)" ACTIVE_SYSTEM="$(fw_printenv -n active_system 2>/dev/null)"
SHOW_ACTIVE_SYSTEM=0
REBOOT=1 REBOOT=1
UPDATE_FILE=""
ALT_BOOT=""
ALT_ROOTFS=""
# Check if the rootfs is ubifs to determine if it is a nand or emmc device
NANDROOTFS="$(sed -ne "s/\(rootfs\).*\<ubifs\>.*/\1/g;T;p" /proc/mounts 2>/dev/null)"
## Local functions ## Local functions
usage() { usage() {
@ -41,11 +48,7 @@ EOF
} }
show_active_system() { show_active_system() {
if [ "${ACTIVE_SYSTEM}" = "linux_a" ]; then echo "Active system is $(echo ${ACTIVE_SYSTEM} | cut -d'_' -f2 | tr [:lower:] [:upper:])"
echo "Active system is A"
else
echo "Active system is B"
fi
} }
reboot_system() { reboot_system() {
@ -57,9 +60,100 @@ reboot_system() {
fi fi
} }
swap_active_system() {
if [ -z "${NANDROOTFS}" ]; then
local PART_UUID=""
# Get boot and rootfs partition index
local MMC_PART="$(realpath /dev/disk/by-partlabel/${ALT_BOOT} | grep -o '[[:digit:]]\+$')"
# Search rootfs UUID
local MMCROOT_DEV="$(realpath /dev/disk/by-partlabel/${ALT_ROOTFS})"
for uuid in /dev/disk/by-partuuid/*; do
if [ "$(realpath "${uuid}")" = "${MMCROOT_DEV}" ]; then
PART_UUID="$(basename "${uuid}")"
break
fi
done
if [ -z "${MMC_PART}" ] || [ -z "${PART_UUID}" ]; then
echo "[ERROR] Unable to detect partitions."
return 1
fi
fw_setenv mmcroot "PARTUUID=${PART_UUID}"
fw_setenv mmcpart "${MMC_PART}"
else
fw_setenv mtdbootpart ${ALT_BOOT}
fw_setenv mtdrootfspart ${ALT_ROOTFS}
fw_setenv rootfsvol ${ALT_ROOTFS}
fi
fw_setenv active_system ${ALT_BOOT}
fw_setenv bootcount 0
return 0
}
update_emmc() {
local IMAGE_SET="mmc,primary"
# Get current partition information so we can
# determine where to flash the images.
[ "${ACTIVE_SYSTEM}" = "linux_a" ] && IMAGE_SET="mmc,secondary"
echo ""
echo "Updating '${IMAGE_SET}' image set from '${UPDATE_FILE}'..."
echo ""
# Execute the update.
swupdate ${VERBOSE} -i "${UPDATE_FILE}" -e "${IMAGE_SET}"
}
update_nand() {
local IMAGE_SET="mtd,primary"
# Get current partition information so we can
# determine where to flash the images.
[ "${ACTIVE_SYSTEM}" = "linux_a" ] && IMAGE_SET="mtd,secondary"
echo ""
echo "Updating '${IMAGE_SET}' image set from '${UPDATE_FILE}'..."
echo ""
# Execute the update.
if [ -f "${PUBLIC_KEY}" ]; then
swupdate ${VERBOSE} -i "${UPDATE_FILE}" -e "${IMAGE_SET}" -k "${PUBLIC_KEY}"
else
swupdate ${VERBOSE} -i "${UPDATE_FILE}" -e "${IMAGE_SET}"
fi
}
update_device() {
local ret
show_active_system
echo "Updating system on $(echo ${ALT_BOOT} | cut -d'_' -f2 | tr [:lower:] [:upper:])"
if [ -z "${NANDROOTFS}" ]; then
update_emmc
else
update_nand
fi
if [ "$?" = "0" ]; then
if ! swap_active_system; then
exit 1
fi
reboot_system
else
echo "[ERROR] $? There was an error performing the update"
fi
}
while :; do while :; do
case $1 in case $1 in
-a|--active) show_active_system;exit -a|--active) SHOW_ACTIVE_SYSTEM=1
;; ;;
--no-reboot) REBOOT=0 --no-reboot) REBOOT=0
;; ;;
@ -74,95 +168,27 @@ while :; do
shift shift
done done
# Show active system.
if [ ${SHOW_ACTIVE_SYSTEM} -eq 1 ]; then
show_active_system
exit
fi
if [ "${ACTIVE_SYSTEM}" = "linux_a" ]; then
ALT_BOOT="linux_b"
ALT_ROOTFS="rootfs_b"
else
ALT_BOOT="linux_a"
ALT_ROOTFS="rootfs_a"
fi
# Check update file parameter. # Check update file parameter.
if [ -z "${UPDATE_FILE}" ]; then if [ -z "${UPDATE_FILE}" ]; then
echo "[ERROR] Update file not specified" echo "[ERROR] Update file not specified"
exit exit
elif [ ! -f "${UPDATE_FILE}" ]; then
echo "[ERROR] Update file '${UPDATE_FILE}' does not exist"
exit
fi fi
# Check if the rootfs is ubifs to determine if it is a nand or emmc device update_device
NANDROOTFS="$(sed -ne "s/\(rootfs\).*\<ubifs\>.*/\1/g;T;p" /proc/mounts 2>/dev/null)"
if [ -z "${NANDROOTFS}" ]; then
# Get current partition information so we can
# determine where to flash the images.
if [ "${ACTIVE_SYSTEM}" = "linux_a" ]; then
echo "Current system is A; Updating system on B"
KERNELBOOT="linux_b"
ROOTFS="rootfs_b"
IMAGE_SET="mmc,secondary"
else
echo "Current system is B; Updating system on A"
KERNELBOOT="linux_a"
ROOTFS="rootfs_a"
IMAGE_SET="mmc,primary"
fi
# get boot and rootfs partition index
MMC_PART="$(realpath /dev/disk/by-partlabel/${KERNELBOOT} | grep -o '[[:digit:]]\+$')"
# search rootfs UUID
MMCROOT_DEV="$(realpath /dev/disk/by-partlabel/${ROOTFS})"
for uuid in /dev/disk/by-partuuid/*; do
if [ "$(realpath "${uuid}")" = "${MMCROOT_DEV}" ]; then
PART_UUID="$(basename "${uuid}")"
break
fi
done
if [ -z "${MMC_PART}" ] || [ -z "${PART_UUID}" ]; then
echo "[ERROR] Detecting partitions to update."
exit
fi
echo ""
echo "Updating '${IMAGE_SET}' image set from '${UPDATE_FILE}'..."
echo ""
# Execute the update.
swupdate ${VERBOSE} -i "${UPDATE_FILE}" -e "${IMAGE_SET}"
if [ "$?" = "0" ]; then
fw_setenv mmcroot "PARTUUID=${PART_UUID}"
fw_setenv mmcpart "${MMC_PART}"
fw_setenv active_system ${KERNELBOOT}
fw_setenv bootcount 0
reboot_system
else
echo "[ERROR] $? There was an error performing the update"
fi
else
# Get current partition information so we can
# determine where to flash the images.
if [ "${ACTIVE_SYSTEM}" = "linux_a" ]; then
echo "Current system is A; Updating system on B"
KERNELBOOT="linux_b"
ROOTFS="rootfs_b"
IMAGE_SET="mtd,secondary"
else
echo "Current system is B; Updating system on A"
KERNELBOOT="linux_a"
ROOTFS="rootfs_a"
IMAGE_SET="mtd,primary"
fi
echo ""
echo "Updating '${IMAGE_SET}' image set from '${UPDATE_FILE}'..."
echo ""
# Execute the update.
if [ -f "${PUBLIC_KEY}" ]; then
swupdate ${VERBOSE} -i "${UPDATE_FILE}" -e "${IMAGE_SET}" -k "${PUBLIC_KEY}"
else
swupdate ${VERBOSE} -i "${UPDATE_FILE}" -e "${IMAGE_SET}"
fi
if [ "$?" = "0" ]; then
fw_setenv mtdbootpart ${KERNELBOOT}
fw_setenv mtdrootfspart ${ROOTFS}
fw_setenv rootfsvol ${ROOTFS}
fw_setenv active_system ${KERNELBOOT}
fw_setenv bootcount 0
reboot_system
else
echo "[ERROR] $? There was an error performing the update"
fi
fi