6 OLD_VER_INFO="/opt/etc/version"
7 COLOR_ERROR='\033[01;31m'
8 COLOR_DEBUG='\033[01;34m'
9 COLOR_NOTIFY='\033[01;33m'
10 COLOR_RESET='\033[00;00m'
15 echo -e "${COLOR_DEBUG}${LOG_TEXT}${COLOR_RESET}"
21 echo -e "${COLOR_ERROR}${LOG_TEXT}${COLOR_RESET}"
27 echo -e "${COLOR_NOTIFY}${LOG_TEXT}${COLOR_RESET}"
32 LOG="[$SCRIPT_NAME]$1"
34 if [ "$2" != "" ]; then
40 # Convert version to 4 digits
44 for ENT in $(echo "$1" | tr "." "\n"); do
48 CVT_VER=${VER[0]}.${VER[1]}.${VER[2]}.${VER[3]}
52 if [ -f $OLD_VER_INFO ]; then
55 NEW_VER=$(cat /etc/config/model-config.xml | grep platform.version\" \
56 | sed -e 's/.*>\(.*\)<.*/\1/' | head -1)
57 convert_version $NEW_VER
61 write_version_info() {
63 echo "OLD_VER=$NEW_VER" > $OLD_VER_INFO
64 /bin/chmod 775 $OLD_VER_INFO
65 /bin/chown .system_fw $OLD_VER_INFO
68 BACKUP_ZIP="/usr/system/RestoreDir/opt.zip"
73 TARGET_DIR=$(dirname /$TARGET_FILE)
75 echo "restore_from_zip: $TARGET_FILE"
76 if [ ! -d $TARGET_DIR ]; then
80 # The attributes of directory can not be overwritten by unzip.
81 # Unzip the directory into temp path and copy it to original path.
82 if [ "z${TARGET_FILE: -1}" = "z/" ]; then
83 RESTORE_DIR_PATH="/tmp/restored_dir"
84 mkdir -p $RESTORE_DIR_PATH
85 unzip -oX $BACKUP_ZIP $TARGET_FILE -d $RESTORE_DIR_PATH > /dev/null
86 cp -af $RESTORE_DIR_PATH/$TARGET_FILE $TARGET_DIR
87 rm -rf $RESTORE_DIR_PATH
89 unzip -oX $BACKUP_ZIP $TARGET_FILE -d / > /dev/null
93 TMP=$(mktemp /tmp/smackinfo.XXXXXX)
94 PATH_FOR_SMACK=$(echo $TARGET_FILE | sed -e "s/\/$//")
95 SMACK_VAL=$(grep "$PATH_FOR_SMACK " /usr/system/RestoreDir/smack_label.txt | \
96 { read FILE SMACK; echo $SMACK; })
97 if [ "z$SMACK_VAL" = "z" ]; then
98 echo "No smack label for $PATH_FOR_SMACK"
100 echo "/$PATH_FOR_SMACK $SMACK_VAL" > $TMP
106 restore_backup_file() {
111 while [ "z$1" != "z" ]; do
125 if [ "z$RESTORE_PATH" = "z" ]; then
126 echo "There is no file to restore"
130 if [ ! "z${RESTORE_PATH:0:1}" = "z/" ]; then
131 echo "Full path of file is required"
135 if [ -e "$RESTORE_PATH" ]; then
136 if [ ! "z$OVERWRITE" = "z" ]; then
137 echo "Warning: $RESTORE_PATH already exists. It will be overwritten"
139 echo "Error: $RESTORE_PATH already exists"
144 # Check if the target file is backed up
145 PATH_FOR_ZIP=$(echo $RESTORE_PATH | sed -e "s/^\///")
146 FOUND_FILES=$(unzip -l $BACKUP_ZIP | awk '{print $4}' | \
147 grep "^$PATH_FOR_ZIP")
148 FOUND_FILE=$(echo "$FOUND_FILES" | \
149 grep -E "$PATH_FOR_ZIP$|$PATH_FOR_ZIP/$")
150 if [ "z$FOUND_FILE" = "z" ]; then
151 echo "Error: $RESTORE_PATH was not backed up"
155 echo "restore_backup_file: $RESTORE_PATH"
156 if [ ! "z${FOUND_FILE: -1}" = "z/" ]; then
157 restore_from_zip $FOUND_FILE
159 for FILE in $FOUND_FILES; do
160 restore_from_zip $FILE
168 if ! grep "$PART_PATH" /proc/mounts | \
170 awk -F',' '{for(i=1; i<=NF; i++) {if ($i == "ro") {found=1; exit}}} END {exit found}'; then
172 ERROR "Partition $PART_PATH is mounted as RO, probably due to an error"
178 COMMIT_BOW_PARTITION()
182 BOWDEV_PATH=/dev/mapper/bowdev_${LABEL}
184 if ! IS_MOUNT_RO "$BOWDEV_PATH"; then
185 # If the partition is in read-only mode, it is most likely that
186 # an dm-bow error occurred as a result of insufficient free
187 # space. Therefore, the upgrade must be considered unsuccessful.
191 DM_NUMBER=$(($("${STAT}" -c "0x%T" $(readlink -f ${BOWDEV_PATH}))))
192 echo 2 > /sys/block/dm-${DM_NUMBER}/bow/state
193 NOTIFY "Changes on partition ${LABEL} commited (dm-bow)"
197 COMMIT_F2FS_PARTITION()
202 if mount -o remount,checkpoint=enable "${PART_DEVICE}"; then
203 NOTIFY "Changes on partition ${LABEL} commited (f2fs)"
206 ERROR "Error when commit changes on partition ${LABEL} (f2fs)"
211 DELETE_BTRFS_PARTITION() {
213 MNT_POINT="$("$FINDMNT" "$PART" -o TARGET)"
214 if [ "$MNT_POINT" = "" ]; then
215 ERROR "Unable to find btrfs mountpoint for: $PART"
218 NOTIFY "Deleting btrfs snapshot"
219 umount "${MNT_POINT}"
220 mount -o subvolid=5,rw "${PART}" "${MNT_POINT}"
221 btrfs subvolume delete "$MNT_POINT"/fota/RO_update
222 rm -rf "$MNT_POINT/fota/RO_update"
223 umount "${MOUNT_POINT}"
224 mount -o rw "${PART}" "${MNT_POINT}"
229 COMMIT_BTRFS_PARTITION()
232 PART_SYSTEM_DATA=$(blkid --match-token PARTLABEL="${LABEL}" -o device -l || blkid --match-token LABEL="${LABEL}" -o device -l)
233 if DELETE_BTRFS_PARTITION "${PART_SYSTEM_DATA}"; then
234 NOTIFY "Changes on partition ${LABEL} commited (btrfs)"
245 PART_DEVICE=$(blkid --match-token PARTLABEL="${LABEL}" -o device -l || blkid --match-token LABEL="${LABEL}" -o device -l)
246 if [ -z "${PART_DEVICE}" ]; then
247 NOTIFY "WARNING: Partition ${LABEL} not found"
251 TYPE=$(blkid ${PART_DEVICE} -o value -s TYPE | tail -n 1)
252 if [ "${TYPE}" = "ext4" ]; then
253 COMMIT_BOW_PARTITION "${LABEL}"
254 elif [ "${TYPE}" = "f2fs" ]; then
255 COMMIT_F2FS_PARTITION "${LABEL}" ${PART_DEVICE}
256 elif [ "${TYPE}" = "btrfs" ]; then
257 COMMIT_BTRFS_PARTITION "${LABEL}"
259 ERROR "ERROR: Cannot commit ${LABEL}: Unsupported filesystem ${TYPE}"
266 if ! COMMIT_PARTITION system-data; then
269 if ! COMMIT_PARTITION user; then