From 006138e13403e7c391a5c0de85daf7a024e22738 Mon Sep 17 00:00:00 2001 From: Tatiana Leon Date: Tue, 21 Mar 2023 17:31:45 +0100 Subject: [PATCH] dualboot: update-firmware: rework the script to use functions and avoid repetitions Signed-off-by: Tatiana Leon --- .../dualboot/dualboot/update-firmware | 210 ++++++++++-------- 1 file changed, 118 insertions(+), 92 deletions(-) diff --git a/meta-digi-dey/recipes-digi/dualboot/dualboot/update-firmware b/meta-digi-dey/recipes-digi/dualboot/dualboot/update-firmware index 4d06821ff..ea9e30526 100755 --- a/meta-digi-dey/recipes-digi/dualboot/dualboot/update-firmware +++ b/meta-digi-dey/recipes-digi/dualboot/dualboot/update-firmware @@ -24,7 +24,14 @@ SCRIPTNAME="$(basename "$(readlink -f "${0}")")" VERBOSE="" PUBLIC_KEY="/etc/ssl/certs/key.pub" ACTIVE_SYSTEM="$(fw_printenv -n active_system 2>/dev/null)" +SHOW_ACTIVE_SYSTEM=0 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\).*\.*/\1/g;T;p" /proc/mounts 2>/dev/null)" ## Local functions usage() { @@ -41,11 +48,7 @@ EOF } show_active_system() { - if [ "${ACTIVE_SYSTEM}" = "linux_a" ]; then - echo "Active system is A" - else - echo "Active system is B" - fi + echo "Active system is $(echo ${ACTIVE_SYSTEM} | cut -d'_' -f2 | tr [:lower:] [:upper:])" } reboot_system() { @@ -57,9 +60,100 @@ reboot_system() { 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 case $1 in - -a|--active) show_active_system;exit + -a|--active) SHOW_ACTIVE_SYSTEM=1 ;; --no-reboot) REBOOT=0 ;; @@ -74,95 +168,27 @@ while :; do shift 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. if [ -z "${UPDATE_FILE}" ]; then echo "[ERROR] Update file not specified" exit +elif [ ! -f "${UPDATE_FILE}" ]; then + echo "[ERROR] Update file '${UPDATE_FILE}' does not exist" + exit fi -# Check if the rootfs is ubifs to determine if it is a nand or emmc device -NANDROOTFS="$(sed -ne "s/\(rootfs\).*\.*/\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 +update_device