scripts: upgrade-support: Add more information into log message
[platform/core/system/upgrade.git] / scripts / upgrade-support / upgrade-trigger.sh
1 #!/bin/bash
2 # Master script for upgrade. Called by update-manager/manually to start upgrade procedure.
3 set -o errexit
4 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
5 set -o errtrace -e -o pipefail
6
7 if [ ! -z "${UPGRADE_DEBUG}" ]; then
8         set -x
9 fi
10
11 PATH=/bin:/usr/bin:/sbin:/usr/sbin
12 FOTA_DIR="/opt/usr/data/fota"
13 STATUS_DIR="/opt/data/update"
14 DOWNLOAD_DELTA="$1"
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"
22
23 prepare_fota_dir() {
24         if [ -d "$FOTA_DIR" ]; then
25                 # Cleanup FOTA_DIR
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)"
30                 else
31                         # If provided delta is from outside the FOTA_DIR delete everything
32                         rm -rf -- "$FOTA_DIR/*"
33                 fi
34         else
35                 log "[Info] Create fota dir..." "$LOG_FILE"
36                 mkdir -p "$FOTA_DIR"
37         fi
38
39         if [ ! -d "$STATUS_DIR" ]; then
40                 log "[Info] Create status dir..." "$LOG_FILE"
41                 mkdir -p "$STATUS_DIR"
42         fi
43 }
44
45 log() {
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"
50         fi
51         echo "$LOG"
52
53         return 0
54 }
55
56 is_ab_upgrade() {
57         if grep -q partition_ab= /proc/cmdline; then
58                 return $TRUE
59         fi
60         return $FALSE
61 }
62
63 do_update() {
64         if is_ab_upgrade; then
65                 set_upgrade_status 5
66
67                 if [ "$(device_board_get_partition_ab_cloned)" -eq 0 ]; then
68                         log "Starting to prepare the partitions for upgrade.." "$LOG_FILE"
69
70                         unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PREPARE_PARTITIONS}"
71                         if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_PREPARE_PARTITIONS}"; then
72                                 exit_error
73                         fi
74                 else
75                         echo "[Info] Partitions already cloned"
76                 fi
77
78                 set_upgrade_status 20
79
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
83
84                 unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PARTIAL}"
85                 unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_FOTA}"
86
87                 if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_PARTIAL}" "${DOWNLOAD_DELTA}"; then
88                         log "[Error] Failed to run $SCRIPT_UPGRADE_PARTIAL ${DOWNLOAD_DELTA}"
89                         exit_error
90                 fi
91
92                 set_upgrade_status 40
93
94                 if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_FOTA}"; then
95                         exit_error
96                 fi
97
98                 set_upgrade_status 80
99         else
100                 echo "[Error] Non-A/B upgrade is unsupported"
101                 device_board_set_upgrade_status -1
102                 exit 1
103         fi
104 }
105
106 verify_file() {
107         FILE_PATH="$1"
108         FILE_NAME=$(basename "$FILE_PATH")
109
110         VALID_CHECKSUM=$(awk "\$2 ~ /^$FILE_NAME\$/ {print \$1}" "$FOTA_DIR/checksum.SHA1")
111         if [ "$VALID_CHECKSUM" == "" ]; then
112                 echo "[Error] No $FILE_NAME in checksum.SHA1"
113                 device_board_set_upgrade_status -1
114                 exit 1
115         fi
116
117         if ! echo "$VALID_CHECKSUM  $FILE_PATH" | sha1sum --check --status; then
118                 echo "[Error] Checksum for file $FILE_NAME is invalid"
119                 device_board_set_upgrade_status -1
120                 exit 1
121         fi
122
123         echo "[Info] Checksum of $FILE_NAME is OK"
124 }
125
126 (
127 flock 9
128
129 rm -f "$LOG_FILE" && touch "$LOG_FILE"
130
131 if [ "$#" != "1" ] || [ ! -f "$1" ]; then
132         log "[Error] Usage: $0 path_to_delta.tar[.gz]" "$LOG_FILE"
133         exit 1
134 fi
135
136 log "[Info] Triggered upgrade.." "$LOG_FILE"
137 log "[Info] Using <$DOWNLOAD_DELTA> delta file." "$LOG_FILE"
138
139 prepare_fota_dir
140 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" checksum.SHA1
141 verify_file "$0"
142 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" upgrade-common.inc
143 verify_file "$FOTA_DIR/upgrade-common.inc"
144 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" update-info.ini
145 verify_file "$FOTA_DIR/update-info.ini"
146 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" delta-verifier
147 verify_file "$FOTA_DIR/delta-verifier"
148
149 log "[Info] Begin delta verification"
150 RET=0
151 # '||'' used to stop script form exiting (errexit is set)
152 "${FOTA_DIR}/delta-verifier" --update_info_path "$FOTA_DIR/update-info.ini" || RET=$?
153
154 if [ ${RET} -ne 0 ]; then
155         log "[Error] Delta verification unsuccessful"
156         device_board_set_upgrade_status -1
157         exit 1
158 fi
159 log "[Info] Delta verification success"
160
161 . "$FOTA_DIR"/upgrade-common.inc
162
163 set_upgrade_status 1
164 verify_img "${DOWNLOAD_DELTA}"
165 do_update
166
167 critical_log "RO update: $SCRIPT_NAME success" "$LOG_FILE"
168 ) 9> "$FLOCK_PATH"