b0917e81f014d20f1a531760eca0ac5a6789ed52
[platform/core/system/upgrade.git] / scripts / upgrade-support / upgrade-fota.sh
1 #!/bin/bash
2 set -o errexit
3 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
4 set -o errtrace -e -o pipefail
5
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
14 . "$FOTA_DIR"/upgrade-common.inc
15
16 FOTA_UPDATE_PREFIX="/run/upgrade-sysroot"
17 VERSION_FILE="$FOTA_UPDATE_PREFIX/opt/etc/version"
18
19 SCRIPT_NAME="upgrade-fota.sh"
20 LOG_FILE="/tmp/update-fota.log"
21
22 cleanup() {
23         cleanup_partitions
24         cleanup_files
25 }
26
27 cleanup_files() {
28         rm -f -- "$FOTA_DIR"/upgrade-common.inc
29         rm -f -- "$FOTA_DIR/$SCRIPT_NAME"
30         rm -f -- "$FOTA_DIR/$CONFIG_FILE"
31         rm -f -- "$FOTA_DIR/upgrade-trigger.sh"
32         rm -f -- "$FOTA_DIR/checksum.SHA1"
33 }
34
35 mount() {
36         # We need to mount the system-data partition to update /opt/etc/version and
37         # create a .do_rw_update file
38         mkdir -p "$FOTA_UPDATE_PREFIX/opt"
39
40         if ! mount_partition system-data "$FOTA_UPDATE_PREFIX/opt" "rw"; then
41                 critical_flog "[Error] Unable to mount opt"
42                 return 1
43         fi
44         return 0
45 }
46
47 reboot_to_fota() {
48         flog "[Info] Write paths..."
49         touch "$DO_RW_UPDATE_FILE"
50         flog "[Info] calling sync"
51
52         flog "[Info] Switching board partition from $CURRENT_AB, to $NEXT_AB"
53         if ! device_board_switch_partition $NEXT_AB; then
54                 critical_flog "[Error] Failed to switch board slot to $NEXT_AB"
55                 exit 1
56         fi
57         cleanup_files
58         /bin/sync
59         flog "[Info] Rebooting to fota"
60         if ! /sbin/reboot fota; then
61                 critical_flog "[Error] Failed to reboot fota"
62                 exit 1
63         fi
64 }
65
66 prepare_fota_update_dir() {
67         mkdir -p ${FOTA_UPDATE_PREFIX}
68 }
69
70 prepare_fota_update_dir
71 check_ab_partition_scheme
72 check_used_block_device
73 if ! mount; then
74         cleanup
75         exit 1
76 fi
77 if ! write_version_info "$VERSION_FILE"; then
78         cleanup
79         exit 1
80 fi
81
82 if ! reboot_to_fota; then
83         cleanup
84         exit 1
85 fi
86 cleanup
87
88 critical_log "RO update: $SCRIPT_NAME success" "$LOG_FILE"