From 80b9e839e383e4d736791a602342b8a97ad5e3ba Mon Sep 17 00:00:00 2001 From: Mateusz Moscicki Date: Fri, 1 Apr 2022 18:34:59 +0200 Subject: [PATCH] Move a common part of ugprade scripts to a separate file Change-Id: If3b6f887d245cc0b99cd648742b4d9312e90543c --- packaging/tota-ua.spec | 1 + scripts/upgrade-common.inc | 178 ++++++++++++++++++++++++++++++++++ scripts/upgrade-fota.sh | 71 ++------------ scripts/upgrade-full.sh | 137 +++----------------------- scripts/upgrade-legacy.sh | 34 +------ scripts/upgrade-partial.sh | 127 +++--------------------- scripts/upgrade-prepare-partitions.sh | 84 ++-------------- scripts/upgrade-trigger.sh | 80 +++++---------- 8 files changed, 242 insertions(+), 470 deletions(-) create mode 100644 scripts/upgrade-common.inc diff --git a/packaging/tota-ua.spec b/packaging/tota-ua.spec index 98bda33..432c3c2 100755 --- a/packaging/tota-ua.spec +++ b/packaging/tota-ua.spec @@ -76,6 +76,7 @@ mkdir -p %{buildroot}%{img_verifier_root_ca_dir} %{_bindir}/upgrade-prepare-partitions.sh %{_bindir}/upgrade-fota.sh %{_bindir}/upgrade-full.sh +%{_bindir}/upgrade-common.inc %attr(700,-,-) %{tota_ua_list_dir}/40-tota-ua.list diff --git a/scripts/upgrade-common.inc b/scripts/upgrade-common.inc new file mode 100644 index 0000000..76bd03f --- /dev/null +++ b/scripts/upgrade-common.inc @@ -0,0 +1,178 @@ +IMG_VERIFIER="/usr/sbin/img-verifier" +STATUS_DIR="/opt/data/update" +HAL_UPGRADE_CFG_DIR="/hal/etc/upgrade/" +HAL_PART_MAP_FILE="label_map.list" +HAL_PART_LIST_FILE="background_copy.list" +CONFIG_FILE="update.cfg" + +#------------------------------------------------ +# log msg [file] +#------------------------------------------------ +log() { + # log format: [script_name][tag]actual_log + LOG="[${SCRIPT_NAME}]$1" + if [ "$2" != "" ]; then + echo "$LOG" >> "$2" + fi + echo "$LOG" +} + +flog() { + log "$1" "$LOG_FILE" +} + +unpack_file() { + ARCHIVE_NAME="$1" + FILE_NAME="$2" + tar xpf "${ARCHIVE_NAME}" -C "${FOTA_DIR}" "${FILE_NAME}" 2> /dev/null + if [ ! -e "${FOTA_DIR}/${FILE_NAME}" ]; then + flog "[Error] There is no ${FILE_NAME}" + fi +} + +verify_img() { + DELTA_FILE="$1" + if [ -e "$IMG_VERIFIER" ]; then + log "[Info] Package verifier is found. Verify $DELTA_FILE" "$LOG_FILE" + if ! "$IMG_VERIFIER" -i "$DELTA_FILE" -l "/opt/var/log/last_iv.log"; then + log "[Error] Update package verification FAILED..." "$LOG_FILE" + echo 5 > "$STATUS_DIR"/result + exit 1 + else + log "[Info] Update package verification PASSED!" "$LOG_FILE" + fi + fi +} + +check_ab_partition_scheme() { + CURRENT_AB="$(/bin/sed -E 's|.*(partition_ab=)([a-b]).*|\2|' /proc/cmdline)" + if [ "$CURRENT_AB" != "a" ] && [ "$CURRENT_AB" != "b" ]; then + flog "[Info] There is no A/B partition scheme" + exit 1 + fi + NEXT_AB="b" + if [ "$CURRENT_AB" == "b" ]; then + NEXT_AB="a" + fi +} + +check_used_block_device() { + ROOTFS_DEVICE="$(findmnt / -no SOURCE)" + if MAPPER_DEVICE="$(echo ${ROOTFS_DEVICE} | grep /dev/mapper)"; then + DM_NAME=$(basename "${MAPPER_DEVICE}") + DM_N=$(dmsetup ls -o blkdevname | grep "${DM_NAME}" | sed -e 's/^.*(\([^)]\+\))/\1/') + ROOTFS_DEVICE="/dev/$(ls /sys/class/block/${DM_N}/slaves/)" + fi + + if ! EMMC_DEVICE="/dev/$(/bin/lsblk -ndo pkname "${ROOTFS_DEVICE}")" ||\ + [ "$EMMC_DEVICE" == "/dev/" ]; then + flog "[Error] Unable to find used block device: $EMMC_DEVICE, for /" + exit 1 + fi +} + +background_copy() { + flog "[Info] Background copying A|B partitions for update..." + for partition_name in ${PARTITION_LIST}; do + # echo is there to suspend abort when partition will not be found e.g. hal + if ! CURRENT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$partition_name" "$CURRENT_AB" |\ + sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|' || echo "__FALSE__")" || \ + [ "$CURRENT_PARTITION" == "__FALSE__" ]; then + flog "[Error] Unable to find: $partition_name current partition on $EMMC_DEVICE device on $CURRENT_AB slot" + check_optional_partition "$partition_name" 1 + continue + fi + if ! NEXT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$partition_name" "$NEXT_AB" |\ + sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|')"; then + flog "[Error] Unable to find: $partition_name next partition on $EMMC_DEVICE device on $CURRENT_AB slot" + check_optional_partition "$partition_name" 1 + continue + fi + if [ "$CURRENT_PARTITION" == "" ] || [ "$NEXT_PARTITION" = "" ]; then + flog "[Error] current: $CURRENT_PARTITION or next: $NEXT_PARTITION partition is empty on $EMMC_DEVICE device" + exit 1 + fi + if [ "$CURRENT_PARTITION" == "$NEXT_PARTITION" ]; then + flog "[Info] $partition_name partition current and next are the same: $CURRENT_PARTITION on $EMMC_DEVICE device" + continue + fi + flog "[Info] Background copy $partition_name, from: $CURRENT_PARTITION to $NEXT_PARTITION" + /bin/dd if="$CURRENT_PARTITION" of="$NEXT_PARTITION" bs=4096 + flog "[Info] Finished background copy $partition_name from $CURRENT_PARTITION to $NEXT_PARTITION" + done +} + +load_background_copy_list() { + PARTITION_LIST=$(grep -v -e "^#" -e "^$" ${HAL_UPGRADE_CFG_DIR}/${HAL_PART_LIST_FILE}) +} + +upgrade_images() { + DELTA_TAR="$1" + flog "[Info] Flash images for update..." + + LABEL_MAP_PATH=${HAL_UPGRADE_CFG_DIR}/${HAL_} + while read LABEL PARTLABEL; do + declare "LABEL_MAP_${LABEL}=${PARTLABEL}" + done < <(grep -v -e "^#" -e "^#" ${LABEL_MAP_PATH}/${HAL_PART_MAP_FILE}) + + # _OFFSET _SIZE _HASH1 _HASH2 + while read -r LABEL_NAME DELTA_NAME TYPE DEV _ _ _ _ + do + LABEL_NAME="$(echo "$LABEL_NAME" | /bin/awk '{print tolower($0)}')" + + # Translate LABEL to PARTLABEL using label_map.list + TMP="LABEL_MAP_${LABEL_NAME}" + PART_NAME=${!TMP} + if [ -z "${PART_NAME}" ]; then + PART_NAME=${LABEL_NAME} + fi + + if [ "$TYPE" == "PRE_UA" ]; then + if ! /bin/tar tf "$DELTA_TAR" "$DELTA_NAME"; then + flog "[Info] There is no delta $DELTA_NAME for label $LABEL_NAME from part $PART_NAME" + continue + fi + + NEXT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$PART_NAME" "$NEXT_AB" |\ + /bin/sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|')" + + flog "[Info] Flashing $DELTA_NAME... to $NEXT_PARTITION" + /bin/tar xOf "$DELTA_TAR" "$DELTA_NAME" > "$NEXT_PARTITION" + flog "[Info] Finished flashing $DELTA_NAME to $NEXT_PARTITION" + else + PARTS_NAME_TO_UPDATE+=( "$PART_NAME:$TYPE" ) + fi + done < "$FOTA_DIR/$CONFIG_FILE" +} + +run_setup_script() { + DELTA_TAR="$1" + SETUP_SCRIPT_NAME=setup.sh + SETUP_SCRIPT_PATH=$FOTA_DIR/$SETUP_SCRIPT_NAME + + /bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" "$SETUP_SCRIPT_NAME" 2>/dev/null ||\ + (log "[Info] setup.sh does not exist, skipping." "$LOG_FILE" && return) + + if [ -e "$SETUP_SCRIPT_PATH" ]; then + /bin/sh "$SETUP_SCRIPT_PATH" + rm "$SETUP_SCRIPT_PATH" + fi +} + +write_version_info() { + VERSION_FILE="$1" + if [ -f "$VERSION_FILE" ]; then + return + fi + OLD_VER=$(/bin/grep platform.version\" /etc/config/model-config.xml \ + | sed -e 's/.*>\(.*\)<.*/\1/' | head -1) + i=0 + VER=(0 0 0 0) + for ENT in $(echo "$OLD_VER" | tr "." "\n"); do + VER[$i]=$ENT + ((i++)) + done + CVT_VER=${VER[0]}.${VER[1]}.${VER[2]}.${VER[3]} + + echo "OLD_VER=$CVT_VER" > "$VERSION_FILE" +} diff --git a/scripts/upgrade-fota.sh b/scripts/upgrade-fota.sh index b01a364..41581ab 100755 --- a/scripts/upgrade-fota.sh +++ b/scripts/upgrade-fota.sh @@ -3,8 +3,15 @@ set -o errexit trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR set -o errtrace -e -o pipefail + +if [ ! -z "${UPGRADE_DEBUG}" ]; then + set -x +fi + PATH=/bin:/usr/bin:/sbin:/usr/sbin +FOTA_DIR="/opt/usr/data/fota" +. "$FOTA_DIR"/upgrade-common.inc FOTA_UPDATE_PREFIX="/run/upgrade-sysroot" VERSION_FILE="$FOTA_UPDATE_PREFIX/opt/etc/version" @@ -12,8 +19,6 @@ DO_RW_UPDATE_FILE="$FOTA_UPDATE_PREFIX/opt/.do_rw_update" SCRIPT_NAME="update-fota.sh" LOG_FILE="/tmp/update-fota.log" -FOTA_DIR="/opt/usr/data/fota" -CONFIG_FILE="update.cfg" untrap() { trap '' ERR @@ -23,66 +28,6 @@ retrap() { trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR } -#------------------------------------------------ -# log msg [file] -#------------------------------------------------ -log() { - # log format: [script_name][tag]actual_log - LOG="[${SCRIPT_NAME}]$1" - if [ "$2" != "" ]; then - echo "$LOG" >> "$2" - fi - echo "$LOG" -} - -flog() { - log "$1" "$LOG_FILE" -} - -check_ab_partition_scheme() { - CURRENT_AB="$(/bin/sed -E 's|.*(partition_ab=)([a-b]).*|\2|' /proc/cmdline)" - if [ "$CURRENT_AB" != "a" ] && [ "$CURRENT_AB" != "b" ]; then - flog "[Info] There is no A/B partition scheme" - exit 1 - fi - NEXT_AB="b" - if [ "$CURRENT_AB" == "b" ]; then - NEXT_AB="a" - fi -} - -check_used_block_device() { - ROOTFS_DEVICE="$(findmnt / -no SOURCE)" - if MAPPER_DEVICE="$(echo ${ROOTFS_DEVICE} | grep /dev/mapper)"; then - DM_NAME=$(basename "${MAPPER_DEVICE}") - DM_N=$(dmsetup ls -o blkdevname | grep "${DM_NAME}" | sed -e 's/^.*(\([^)]\+\))/\1/') - ROOTFS_DEVICE="/dev/$(ls /sys/class/block/${DM_N}/slaves/)" - fi - - if ! EMMC_DEVICE="/dev/$(/bin/lsblk -ndo pkname "${ROOTFS_DEVICE}")" ||\ - [ "$EMMC_DEVICE" == "/dev/" ]; then - flog "[Error] Unable to find used block device: $EMMC_DEVICE, for /" - exit 1 - fi -} - -write_version_info() { - if [ -f "$VERSION_FILE" ]; then - return - fi - OLD_VER=$(/bin/grep platform.version\" /etc/config/model-config.xml \ - | sed -e 's/.*>\(.*\)<.*/\1/' | head -1) - i=0 - VER=(0 0 0 0) - for ENT in $(echo "$OLD_VER" | tr "." "\n"); do - VER[$i]=$ENT - ((i++)) - done - CVT_VER=${VER[0]}.${VER[1]}.${VER[2]}.${VER[3]} - - echo "OLD_VER=$CVT_VER" > "$VERSION_FILE" -} - mount_partition() { GPT_LABEL="$1" DST="$2" @@ -180,7 +125,7 @@ if ! mount; then cleanup exit 1 fi -if ! write_version_info; then +if ! write_version_info "$VERSION_FILE"; then cleanup exit 1 fi diff --git a/scripts/upgrade-full.sh b/scripts/upgrade-full.sh index 8decd04..0cd55b2 100755 --- a/scripts/upgrade-full.sh +++ b/scripts/upgrade-full.sh @@ -5,8 +5,16 @@ set -o errexit trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR set -o errtrace -e -o pipefail +if [ ! -z "${UPGRADE_DEBUG}" ]; then + set -x +fi + PATH=/bin:/usr/bin:/sbin:/usr/sbin + FOTA_DIR="/opt/usr/data/fota" + +. "${FOTA_DIR}"/upgrade-common.inc + DOWNLOAD_DELTA=$1 DO_RW_UPDATE_FILE="$FOTA_UPDATE_PREFIX/opt/.do_rw_update" @@ -15,21 +23,6 @@ LOG_FILE="/tmp/upgrade-trigger.log" CONFIG_FILE="update.cfg" HAL_UPGRADE_CFG_DIR="/hal/etc/upgrade/" HAL_PART_LIST_FILE="background_copy.list" -#------------------------------------------------ -# log msg [file] -#------------------------------------------------ -log() { - # log format: [script_name][tag]actual_log - LOG="[${SCRIPT_NAME}]$1" - if [ "$2" != "" ]; then - echo "$LOG" >> "$2" - fi - echo "$LOG" -} - -flog() { - log "$1" "$LOG_FILE" -} if [ "$#" != "1" ] || [ ! -f "$1" ]; then log "[Error] Usage: $0 path_to_upgrade.tar[.gz]" "$LOG_FILE" @@ -38,115 +31,7 @@ fi DELTA_TAR="$DOWNLOAD_DELTA" -log "[Info] Using <$DELTA_TAR> delta file." "$LOG_FILE" - -check_ab_partition_scheme() { - CURRENT_AB="$(/bin/sed -E 's|.*(partition_ab=)([a-b]).*|\2|' /proc/cmdline)" - if [ "$CURRENT_AB" != "a" ] && [ "$CURRENT_AB" != "b" ]; then - flog "[Info] There is no A/B partition scheme" - exit 1 - fi - NEXT_AB="b" - if [ "$CURRENT_AB" == "b" ]; then - NEXT_AB="a" - fi -} - -check_used_block_device() { - ROOTFS_DEVICE="$(findmnt / -no SOURCE)" - if MAPPER_DEVICE="$(echo ${ROOTFS_DEVICE} | grep /dev/mapper)"; then - DM_NAME=$(basename "${MAPPER_DEVICE}") - DM_N=$(dmsetup ls -o blkdevname | grep "${DM_NAME}" | sed -e 's/^.*(\([^)]\+\))/\1/') - ROOTFS_DEVICE="/dev/$(ls /sys/class/block/${DM_N}/slaves/)" - fi - - if ! EMMC_DEVICE="/dev/$(/bin/lsblk -ndo pkname "${ROOTFS_DEVICE}")" ||\ - [ "$EMMC_DEVICE" == "/dev/" ]; then - flog "[Error] Unable to find used block device: $EMMC_DEVICE, for /" - exit 1 - fi -} - -upgrade_images() { - flog "[Info] Flash images for update..." - - LABEL_MAP_PATH=${HAL_UPGRADE_CFG_DIR}/${HAL_} - while read LABEL PARTLABEL; do - declare "LABEL_MAP_${LABEL}=${PARTLABEL}" - done < <(grep -v -e "^#" -e "^#" ${LABEL_MAP_PATH}/${HAL_PART_MAP_FILE}) - - # _OFFSET _SIZE _HASH1 _HASH2 - while read -r LABEL_NAME DELTA_NAME TYPE DEV _ _ _ _ - do - LABEL_NAME="$(echo "$LABEL_NAME" | /bin/awk '{print tolower($0)}')" - - # Translate LABEL to PARTLABEL using label_map.list - TMP="LABEL_MAP_${LABEL_NAME}" - PART_NAME=${!TMP} - if [ -z "${PART_NAME}" ]; then - PART_NAME=${LABEL_NAME} - fi - - if [ "$TYPE" == "PRE_UA" ]; then - if ! /bin/tar tf "$DELTA_TAR" "$DELTA_NAME"; then - flog "[Info] There is no delta $DELTA_NAME for label $LABEL_NAME from part $PART_NAME" - continue - fi - - NEXT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$PART_NAME" "$NEXT_AB" |\ - /bin/sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|')" - - flog "[Info] Flashing $DELTA_NAME... to $NEXT_PARTITION" - /bin/tar xOf "$DELTA_TAR" "$DELTA_NAME" > "$NEXT_PARTITION" - flog "[Info] Finished flashing $DELTA_NAME to $NEXT_PARTITION" - else - PARTS_NAME_TO_UPDATE+=( "$PART_NAME:$TYPE" ) - fi - done < "$FOTA_DIR/$CONFIG_FILE" -} - -background_copy() { - flog "[Info] Background copying A|B partitions for update..." - for partition_name in ${PARTITION_LIST}; do - # echo is there to suspend abort when partition will not be found e.g. hal - if ! CURRENT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$partition_name" "$CURRENT_AB" |\ - sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|' || echo "__FALSE__")" || \ - [ "$CURRENT_PARTITION" == "__FALSE__" ]; then - flog "[Error] Unable to find: $partition_name current partition on $EMMC_DEVICE device on $CURRENT_AB slot" - check_optional_partition "$partition_name" 1 - continue - fi - if ! NEXT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$partition_name" "$NEXT_AB" |\ - sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|')"; then - flog "[Error] Unable to find: $partition_name next partition on $EMMC_DEVICE device on $CURRENT_AB slot" - check_optional_partition "$partition_name" 1 - continue - fi - if [ "$CURRENT_PARTITION" == "" ] || [ "$NEXT_PARTITION" = "" ]; then - flog "[Error] current: $CURRENT_PARTITION or next: $NEXT_PARTITION partition is empty on $EMMC_DEVICE device" - exit 1 - fi - if [ "$CURRENT_PARTITION" == "$NEXT_PARTITION" ]; then - flog "[Info] $partition_name partition current and next are the same: $CURRENT_PARTITION on $EMMC_DEVICE device" - continue - fi - flog "[Info] Background copy $partition_name, from: $CURRENT_PARTITION to $NEXT_PARTITION" - /bin/dd if="$CURRENT_PARTITION" of="$NEXT_PARTITION" bs=4096 - flog "[Info] Finished background copy $partition_name from $CURRENT_PARTITION to $NEXT_PARTITION" - done -} - -load_background_copy_list() { - PARTITION_LIST=$(grep -v -e "^#" -e "^$" ${HAL_UPGRADE_CFG_DIR}/${HAL_PART_LIST_FILE}) -} - -unpack_update_cfg() { - /bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" "$CONFIG_FILE" - if [ ! -e "$FOTA_DIR/$CONFIG_FILE" ]; then - flog "[Error] There is no $CONFIG_FILE" - return - fi -} +flog "[Info] Using <$DELTA_TAR> delta file." reboot_to_fota() { flog "[Info] Write paths..." @@ -168,8 +53,8 @@ reboot_to_fota() { check_ab_partition_scheme check_used_block_device -unpack_update_cfg +unpack_file "$DELTA_TAR" "$CONFIG_FILE" load_background_copy_list background_copy -upgrade_images +upgrade_images "$DOWNLOAD_DELTA" reboot_to_fota diff --git a/scripts/upgrade-legacy.sh b/scripts/upgrade-legacy.sh index 77474ec..823cd43 100644 --- a/scripts/upgrade-legacy.sh +++ b/scripts/upgrade-legacy.sh @@ -9,24 +9,11 @@ FOTA_DIR="/opt/usr/data/fota" STATUS_DIR="/opt/data/update" DOWNLOAD_DELTA=$1 +. "$FOTA_DIR"/upgrade-common.inc + SCRIPT_NAME="upgrade-trigger.sh" LOG_FILE="/tmp/upgrade-trigger.log" -#------------------------------------------------ -# log msg [file] -#------------------------------------------------ -log() { - # log format: [script_name][tag]actual_log - LOG="[${SCRIPT_NAME}]$1" - if [ "$2" != "" ]; then - echo "$LOG" >> "$2" - fi - echo "$LOG" -} -unpack_file() { - FILE_NAME="$1" - /bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" "$FILE_NAME" -} if [ "$#" != "1" ] || [ ! -f "$1" ]; then log "[Error] Usage: $0 path_to_delta.tar[.gz]" "$LOG_FILE" @@ -83,19 +70,6 @@ flash_pre_image() { done < "$FOTA_DIR/$CONFIG_FILE" } -run_setup_script() { - SETUP_SCRIPT_NAME=setup.sh - SETUP_SCRIPT_PATH=$FOTA_DIR/$SETUP_SCRIPT_NAME - - /bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" "$SETUP_SCRIPT_NAME" 2>/dev/null ||\ - (log "[Info] setup.sh does not exist, skipping." "$LOG_FILE" && return) - - if [ -e "$SETUP_SCRIPT_PATH" ]; then - /bin/sh "$SETUP_SCRIPT_PATH" - rm "$SETUP_SCRIPT_PATH" - fi -} - if [ "$DOWNLOAD_DELTA" -ef "$DELTA_TAR" ]; then log "[Info] delta.tar[.gz] is already placed in correct directory." "$LOG_FILE" else @@ -113,7 +87,7 @@ fi sync # Run setup script if exist -run_setup_script +run_setup_script "$DELTA_TAR" # Flash images # - in case of some new image was included @@ -122,7 +96,7 @@ flash_pre_image # Extract delta.ua log "[Info] Extract delta.ua..." "$LOG_FILE" -/bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" delta.ua 2>/dev/null +unpack_file "$DELTA_TAR" delta.ua # FOTA: /usr/bin/rw-update-prepare.sh diff --git a/scripts/upgrade-partial.sh b/scripts/upgrade-partial.sh index bf9a5b9..2290844 100755 --- a/scripts/upgrade-partial.sh +++ b/scripts/upgrade-partial.sh @@ -3,13 +3,17 @@ set -o errexit trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR set -o errtrace -e -o pipefail +if [ ! -z "${UPGRADE_DEBUG}" ]; then + set -x +fi + PATH=/bin:/usr/bin:/sbin:/usr/sbin +FOTA_DIR="/opt/usr/data/fota" + +. "$FOTA_DIR"/upgrade-common.inc SCRIPT_NAME="upgrade-partial.sh" LOG_FILE="/tmp/upgrade-partial.log" -FOTA_DIR="/opt/usr/data/fota" -HAL_UPGRADE_CFG_DIR="/hal/etc/upgrade/" -HAL_PART_MAP_FILE="label_map.list" CONFIG_FILE="update.cfg" untrap() { @@ -20,49 +24,6 @@ retrap() { trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR } -#------------------------------------------------ -# log msg [file] -#------------------------------------------------ -log() { - # log format: [script_name][tag]actual_log - LOG="[${SCRIPT_NAME}]$1" - if [ "$2" != "" ]; then - echo "$LOG" >> "$2" - fi - echo "$LOG" -} - -flog() { - log "$1" "$LOG_FILE" -} - -check_ab_partition_scheme() { - CURRENT_AB="$(/bin/sed -E 's|.*(partition_ab=)([a-b]).*|\2|' /proc/cmdline)" - if [ "$CURRENT_AB" != "a" ] && [ "$CURRENT_AB" != "b" ]; then - flog "[Info] There is no A/B partition scheme" - exit 1 - fi - NEXT_AB="b" - if [ "$CURRENT_AB" == "b" ]; then - NEXT_AB="a" - fi -} - -check_used_block_device() { - ROOTFS_DEVICE="$(findmnt / -no SOURCE)" - if MAPPER_DEVICE="$(echo ${ROOTFS_DEVICE} | grep /dev/mapper)"; then - DM_NAME=$(basename "${MAPPER_DEVICE}") - DM_N=$(dmsetup ls -o blkdevname | grep "${DM_NAME}" | sed -e 's/^.*(\([^)]\+\))/\1/') - ROOTFS_DEVICE="/dev/$(ls /sys/class/block/${DM_N}/slaves/)" - fi - - if ! EMMC_DEVICE="/dev/$(/bin/lsblk -ndo pkname "${ROOTFS_DEVICE}")" ||\ - [ "$EMMC_DEVICE" == "/dev/" ]; then - flog "[Error] Unable to find used block device: $EMMC_DEVICE, for /" - exit 1 - fi -} - check_args() { DOWNLOAD_DELTA="$1" if [ ! -f "$DOWNLOAD_DELTA" ]; then @@ -100,47 +61,6 @@ copy_delta() { fi } -upgrade_images() { - flog "[Info] Flash images for update..." - - LABEL_MAP_PATH=${HAL_UPGRADE_CFG_DIR}/${HAL_} - while read LABEL PARTLABEL; do - declare "LABEL_MAP_${LABEL}=${PARTLABEL}" - done < <(grep -v -e "^#" -e "^#" ${LABEL_MAP_PATH}/${HAL_PART_MAP_FILE}) - - rm -f "$FOTA_DIR/delta.ua" - # _OFFSET _SIZE _HASH1 _HASH2 - while read -r LABEL_NAME DELTA_NAME TYPE DEV _ _ _ _ - do - LABEL_NAME="$(echo "$LABEL_NAME" | /bin/awk '{print tolower($0)}')" - - # Translate LABEL to PARTLABEL using label_map.list - TMP="LABEL_MAP_${LABEL_NAME}" - PART_NAME=${!TMP} - if [ -z "${PART_NAME}" ]; then - PART_NAME=${LABEL_NAME} - fi - - if [ "$TYPE" == "PRE_UA" ]; then - /bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" "$DELTA_NAME" - if [ ! -e "$FOTA_DIR/$DELTA_NAME" ]; then - flog "[Info] There is no delta $DELTA_NAME for label $LABEL_NAME from part $PART_NAME" - continue - fi - - NEXT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$PART_NAME" "$NEXT_AB" |\ - /bin/sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|')" - - flog "[Info] Flashing $DELTA_NAME... to $NEXT_PARTITION" - dd if="$FOTA_DIR/$DELTA_NAME" of="$NEXT_PARTITION" bs=4096 - rm -f "$FOTA_DIR/$DELTA_NAME" - flog "[Info] Finished flashing $DELTA_NAME to $NEXT_PARTITION" - else - PARTS_NAME_TO_UPDATE+=( "$PART_NAME:$TYPE" ) - fi - done < "$FOTA_DIR/$CONFIG_FILE" -} - FOTA_UPDATE_PREFIX="/run/upgrade-sysroot" mount_partition() { @@ -225,25 +145,8 @@ prepare_for_ua() { return 0 } -run_setup_script() { - SETUP_SCRIPT_NAME=setup.sh - SETUP_SCRIPT_PATH=$FOTA_DIR/$SETUP_SCRIPT_NAME - - /bin/tar xfp "$DELTA_TAR" -C "$FOTA_DIR" "$SETUP_SCRIPT_NAME" 2>/dev/null ||\ - (flog "[Info] setup.sh does not exist, skipping." && return 0) - - if [ -e "$SETUP_SCRIPT_PATH" ]; then - if ! /bin/sh "$SETUP_SCRIPT_PATH"; then - flog "[Error] setup script failure" - return 1 - fi - rm "$SETUP_SCRIPT_PATH" - fi - return 0 -} - run_ro_update() { - /bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" "delta.ua" + unpack_file "$DELTA_TAR" delta.ua /bin/chmod +x "$FOTA_DIR/delta.ua" cd / if ! "$FOTA_DIR/delta.ua" /opt/usr/data/fota /opt/data/update 0 "$NEXT_AB" "$EMMC_DEVICE"; then @@ -257,28 +160,20 @@ prepare_fota_update_dir() { mkdir -p ${FOTA_UPDATE_PREFIX} } -unpack_update_cfg() { - /bin/tar xvfp "$DELTA_TAR" -C "$FOTA_DIR" "$CONFIG_FILE" - if [ ! -e "$FOTA_DIR/$CONFIG_FILE" ]; then - flog "[Error] There is no $CONFIG_FILE" - return - fi -} - prepare_fota_update_dir check_ab_partition_scheme check_used_block_device check_args "$@" copy_delta -unpack_update_cfg -upgrade_images +unpack_file "$DELTA_TAR" "$CONFIG_FILE" +upgrade_images "$DELTA_TAR" if ! prepare_for_ua; then flog "[Error] Unable to prepare_for_ua" cleanup exit 1 fi -if ! run_setup_script; then +if ! run_setup_script "$DELTA_TAR"; then flog "[Error] Unable to run_setup_script" cleanup exit 1 diff --git a/scripts/upgrade-prepare-partitions.sh b/scripts/upgrade-prepare-partitions.sh index 75f70d3..be47f44 100755 --- a/scripts/upgrade-prepare-partitions.sh +++ b/scripts/upgrade-prepare-partitions.sh @@ -3,56 +3,21 @@ set -o errexit trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR set -o errtrace -e -o pipefail +if [ ! -z "${UPGRADE_DEBUG}" ]; then + set -x +fi + PATH=/bin:/usr/bin:/sbin:/usr/sbin FOTA_DIR="/opt/usr/data/fota" -STATUS_DIR="/opt/data/update" + +. "$FOTA_DIR"/upgrade-common.inc + SCRIPT_NAME="upgrade-prepare-partitions.sh" LOG_FILE="/tmp/upgrade-prepare-partitions.log" -HAL_UPGRADE_CFG_DIR="/hal/etc/upgrade/" -HAL_PART_LIST_FILE="background_copy.list" #------------------------------------------------ # log msg [file] #------------------------------------------------ -log() { - # log format: [script_name][tag]actual_log - LOG="[${SCRIPT_NAME}]$1" - if [ "$2" != "" ]; then - echo "$LOG" >> "$2" - fi - echo "$LOG" -} - -flog() { - log "$1" "$LOG_FILE" -} - -check_ab_partition_scheme() { - CURRENT_AB="$(/bin/sed -E 's|.*(partition_ab=)([a-b]).*|\2|' /proc/cmdline)" - if [ "$CURRENT_AB" != "a" ] && [ "$CURRENT_AB" != "b" ]; then - flog "[Info] There is no A/B partition scheme" - exit 1 - fi - NEXT_AB="b" - if [ "$CURRENT_AB" == "b" ]; then - NEXT_AB="a" - fi -} - -check_used_block_device() { - ROOTFS_DEVICE="$(findmnt / -no SOURCE)" - if MAPPER_DEVICE="$(echo ${ROOTFS_DEVICE} | grep /dev/mapper)"; then - DM_NAME=$(basename "${MAPPER_DEVICE}") - DM_N=$(dmsetup ls -o blkdevname | grep "${DM_NAME}" | sed -e 's/^.*(\([^)]\+\))/\1/') - ROOTFS_DEVICE="/dev/$(ls /sys/class/block/${DM_N}/slaves/)" - fi - - if ! EMMC_DEVICE="/dev/$(/bin/lsblk -ndo pkname "${ROOTFS_DEVICE}")" ||\ - [ "$EMMC_DEVICE" == "/dev/" ]; then - flog "[Error] Unable to find used block device: $EMMC_DEVICE, for /" - exit 1 - fi -} create_update_dirs() { # Check fota directory @@ -79,41 +44,6 @@ check_optional_partition() { exit "$exit_code" } -background_copy() { - flog "[Info] Background copying A|B partitions for update..." - for partition_name in ${PARTITION_LIST}; do - # echo is there to suspend abort when partition will not be found e.g. hal - if ! CURRENT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$partition_name" "$CURRENT_AB" |\ - sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|' || echo "__FALSE__")" || \ - [ "$CURRENT_PARTITION" == "__FALSE__" ]; then - flog "[Error] Unable to find: $partition_name current partition on $EMMC_DEVICE device on $CURRENT_AB slot" - check_optional_partition "$partition_name" 1 - continue - fi - if ! NEXT_PARTITION="$(/usr/bin/blkid-print "$EMMC_DEVICE" "$partition_name" "$NEXT_AB" |\ - sed -E 's|(part_nr: [0-9]+ )\((.*)\): (.*)|\3|')"; then - flog "[Error] Unable to find: $partition_name next partition on $EMMC_DEVICE device on $CURRENT_AB slot" - check_optional_partition "$partition_name" 1 - continue - fi - if [ "$CURRENT_PARTITION" == "" ] || [ "$NEXT_PARTITION" = "" ]; then - flog "[Error] current: $CURRENT_PARTITION or next: $NEXT_PARTITION partition is empty on $EMMC_DEVICE device" - exit 1 - fi - if [ "$CURRENT_PARTITION" == "$NEXT_PARTITION" ]; then - flog "[Info] $partition_name partition current and next are the same: $CURRENT_PARTITION on $EMMC_DEVICE device" - continue - fi - flog "[Info] Background copy $partition_name, from: $CURRENT_PARTITION to $NEXT_PARTITION" - /bin/dd if="$CURRENT_PARTITION" of="$NEXT_PARTITION" bs=4096 - flog "[Info] Finished background copy $partition_name from $CURRENT_PARTITION to $NEXT_PARTITION" - done -} - -load_background_copy_list() { - PARTITION_LIST=$(grep -v -e "^#" -e "^$" ${HAL_UPGRADE_CFG_DIR}/${HAL_PART_LIST_FILE}) -} - check_ab_partition_scheme check_used_block_device create_update_dirs diff --git a/scripts/upgrade-trigger.sh b/scripts/upgrade-trigger.sh index fc2c10c..21c8649 100755 --- a/scripts/upgrade-trigger.sh +++ b/scripts/upgrade-trigger.sh @@ -4,13 +4,14 @@ set -o errexit trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR set -o errtrace -e -o pipefail +if [ ! -z "${UPGRADE_DEBUG}" ]; then + set -x +fi + PATH=/bin:/usr/bin:/sbin:/usr/sbin FOTA_DIR="/opt/usr/data/fota" STATUS_DIR="/opt/data/update" -VERSION_FILE="/opt/etc/version" -DOWNLOAD_DELTA=$1 -IMG_VERIFIER="/usr/sbin/img-verifier" - +DOWNLOAD_DELTA="$1" SCRIPT_NAME="upgrade-trigger.sh" LOG_FILE="/tmp/upgrade-trigger.log" SCRIPT_UPGRADE_LEGACY="upgrade-legacy.sh" @@ -18,17 +19,6 @@ SCRIPT_UPGRADE_FULL="upgrade-full.sh" SCRIPT_UPGRADE_PREPARE_PARTITIONS="upgrade-prepare-partitions.sh" SCRIPT_UPGRADE_PARTIAL="upgrade-partial.sh" SCRIPT_UPGRADE_FOTA="upgrade-fota.sh" -#------------------------------------------------ -# log msg [file] -#------------------------------------------------ -log() { - # log format: [script_name][tag]actual_log - LOG="[${SCRIPT_NAME}]$1" - if [ "$2" != "" ]; then - echo "$LOG" >> "$2" - fi - echo "$LOG" -} prepare_fota_dir() { if [ ! -d "$FOTA_DIR" ]; then @@ -42,22 +32,13 @@ prepare_fota_dir() { fi } -verify_img() { - if [ -e "$IMG_VERIFIER" ]; then - log "[Info] Package verifier is found. Verify $DOWNLOAD_DELTA" "$LOG_FILE" - if ! "$IMG_VERIFIER" -i "$DOWNLOAD_DELTA" -l "/opt/var/log/last_iv.log"; then - log "[Error] Update package verification FAILED..." "$LOG_FILE" - echo 5 > "$STATUS_DIR"/result - exit 1 - else - log "[Info] Update package verification PASSED!" "$LOG_FILE" - fi +log() { + # log format: [script_name][tag]actual_log + LOG="[${SCRIPT_NAME}]$1" + if [ "$2" != "" ]; then + echo "$LOG" >> "$2" fi -} - -unpack_file() { - FILE_NAME="$1" - tar xpf "${DOWNLOAD_DELTA}" -C "${FOTA_DIR}" "${FILE_NAME}" 2> /dev/null + echo "$LOG" } is_full_upgrade() { @@ -70,22 +51,6 @@ is_ab_upgrade() { [ ! -z "${AB_SLOT}" ] } -write_version_info() { - if [ ! -f "$VERSION_FILE" ]; then - OLD_VER=$(grep platform.version\" /etc/config/model-config.xml \ - | sed -e 's/.*>\(.*\)<.*/\1/' | head -1) - i=0 - VER=(0 0 0 0) - for ENT in $(echo "$OLD_VER" | tr "." "\n"); do - VER[$i]=$ENT - ((i++)) - done - CVT_VER=${VER[0]}.${VER[1]}.${VER[2]}.${VER[3]} - - echo "OLD_VER=$CVT_VER" > "$VERSION_FILE" - fi -} - check_update_type() { if is_ab_upgrade; then if is_full_upgrade; then @@ -93,13 +58,13 @@ check_update_type() { # it a full upgrade. In this case the provided images will be flashed to # the appropriate partitions directly from the archive so as not to # consume additional space on the device. - unpack_file "${SCRIPT_UPGRADE_FULL}" "${DOWNLOAD_DELTA}" + unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_FULL}" "${FOTA_DIR}/${SCRIPT_UPGRADE_FULL}" "${DOWNLOAD_DELTA}" else # Regular A/B Upgrade - unpack_file "${SCRIPT_UPGRADE_PREPARE_PARTITIONS}" - unpack_file "${SCRIPT_UPGRADE_PARTIAL}" - unpack_file "${SCRIPT_UPGRADE_FOTA}" + unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PREPARE_PARTITIONS}" + unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PARTIAL}" + unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_FOTA}" if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_PREPARE_PARTITIONS}"; then exit 1 @@ -113,7 +78,7 @@ check_update_type() { fi else # Legacy upgrade (non-A/B) - unpack_file "${SCRIPT_UPGRADE_LEGACY}" + unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_LEGACY}" "${FOTA_DIR}/${SCRIPT_UPGRADE_LEGACY}" "${DOWNLOAD_DELTA}" fi } @@ -127,12 +92,11 @@ fi log "[Info] Using <$DOWNLOAD_DELTA> delta file." "$LOG_FILE" -if [ ! -d "$STATUS_DIR" ]; then - log "[Info] Create status dir..." "$LOG_FILE" - mkdir -p "$STATUS_DIR" -fi - prepare_fota_dir -verify_img -write_version_info + +tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" upgrade-common.inc + +. "$FOTA_DIR"/upgrade-common.inc + +verify_img "${DOWNLOAD_DELTA}" check_update_type -- 2.7.4