2 # Master script for upgrade. Called by update-manager/manually to start upgrade procedure.
4 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
5 set -o errtrace -e -o pipefail
7 if [ ! -z "${UPGRADE_DEBUG}" ]; then
11 PATH=/bin:/usr/bin:/sbin:/usr/sbin
12 FOTA_DIR="/opt/usr/data/fota"
13 STATUS_DIR="/opt/data/update"
15 SCRIPT_NAME="upgrade-trigger.sh"
16 LOG_FILE="/tmp/upgrade-trigger.log"
17 SCRIPT_UPGRADE_PREPARE_PARTITIONS="upgrade-prepare-partitions.sh"
18 SCRIPT_UPGRADE_PARTIAL="upgrade-partial.sh"
19 SCRIPT_UPGRADE_FOTA="upgrade-fota.sh"
20 FLOCK_PATH="/var/lock/clone_partitions.lock"
21 DELTA_VERIFIER="/usr/bin/delta-verifier"
24 if [ -d "$FOTA_DIR" ]; then
26 if [ "$(dirname $DOWNLOAD_DELTA)" = "$FOTA_DIR" ]; then
27 # If provided delta is from inside the FOTA_DIR, delete everything else
28 DELTA_FILE_NAME=$(basename $DOWNLOAD_DELTA)
29 rm -rf -- "$FOTA_DIR/!($DELTA_FILE_NAME)"
31 # If provided delta is from outside the FOTA_DIR delete everything
32 rm -rf -- "$FOTA_DIR/*"
35 log "[Info] Create fota dir..." "$LOG_FILE"
39 if [ ! -d "$STATUS_DIR" ]; then
40 log "[Info] Create status dir..." "$LOG_FILE"
41 mkdir -p "$STATUS_DIR"
46 # log format: [script_name][tag]actual_log
47 LOG="[${SCRIPT_NAME}]$1"
48 if [ "$2" != "" ]; then
49 echo "[$(date +"%d/%m/%Y %H:%M:%S")]" "$LOG" >> "$2"
57 if grep -q partition_ab= /proc/cmdline; then
64 if is_ab_upgrade; then
67 if [ "$(device_board_get_partition_ab_cloned)" -eq 0 ]; then
68 log "Starting to prepare the partitions for upgrade.." "$LOG_FILE"
70 unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PREPARE_PARTITIONS}"
71 if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_PREPARE_PARTITIONS}"; then
75 echo "[Info] Partitions already cloned"
80 # In the next steps the content of the partitions of the second slot will
81 # be modified, so they will not be a clone of the current partitions
82 device_board_clear_partition_ab_cloned
84 unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PARTIAL}"
85 unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_FOTA}"
87 if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_PARTIAL}" "${DOWNLOAD_DELTA}"; then
88 log "[Error] Failed to run $SCRIPT_UPGRADE_PARTIAL ${DOWNLOAD_DELTA}"
94 # During the execution of ${SCRIPT_UPGRADE_FOTA} system will reboot, so the
95 # script will be aborted. We don't want this to be treated as an error.
98 if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_FOTA}"; then
99 # We want to check if we are here becasue the reboot interrupted the
100 # execution of ${SCRIPT_UPGRADE_FOTA} or for some other reason.
101 # If a reboot is currently in progress then /proc/1/exe will point to
102 # something other than "systemd" (e.g. deviced-shudown).
103 if [ "$(basename "$(readlink /proc/1/exe)")" = "systemd" ]; then
104 # No, it's not a reboot. Rather it is a regular error.
107 set_upgrade_status 80
108 echo "[Info] RO update: $SCRIPT_NAME success"
113 echo "[Error] Non-A/B upgrade is unsupported"
114 device_board_set_upgrade_status -1
121 FILE_NAME=$(basename "$FILE_PATH")
123 VALID_CHECKSUM=$(awk "\$2 ~ /^$FILE_NAME\$/ {print \$1}" "$FOTA_DIR/checksum.SHA1")
124 if [ "$VALID_CHECKSUM" == "" ]; then
125 echo "[Error] No $FILE_NAME in checksum.SHA1"
126 device_board_set_upgrade_status -1
130 if ! echo "$VALID_CHECKSUM $FILE_PATH" | sha1sum --check --status; then
131 echo "[Error] Checksum for file $FILE_NAME is invalid"
132 device_board_set_upgrade_status -1
136 echo "[Info] Checksum of $FILE_NAME is OK"
142 rm -f "$LOG_FILE" && touch "$LOG_FILE"
144 if [ "$#" != "1" ] || [ ! -f "$1" ]; then
145 log "[Error] Usage: $0 path_to_delta.tar[.gz]" "$LOG_FILE"
149 log "[Info] Triggered upgrade.." "$LOG_FILE"
150 log "[Info] Using <$DOWNLOAD_DELTA> delta file." "$LOG_FILE"
153 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" checksum.SHA1
155 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" upgrade-common.inc
156 verify_file "$FOTA_DIR/upgrade-common.inc"
157 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" update-info.ini
158 verify_file "$FOTA_DIR/update-info.ini"
159 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" delta-verifier
160 verify_file "$FOTA_DIR/delta-verifier"
162 log "[Info] Begin delta verification"
164 # '||'' used to stop script form exiting (errexit is set)
165 "${FOTA_DIR}/delta-verifier" --update_info_path "$FOTA_DIR/update-info.ini" || RET=$?
167 if [ ${RET} -ne 0 ]; then
168 log "[Error] Delta verification unsuccessful"
169 device_board_set_upgrade_status -1
172 log "[Info] Delta verification success"
174 . "$FOTA_DIR"/upgrade-common.inc
177 verify_img "${DOWNLOAD_DELTA}"