Skip to content

Commit

Permalink
Feature/67786 factoryreset checks (#145)
Browse files Browse the repository at this point in the history
* add factory-reset sanity checks
* add factory-reset sanity checks for cli variant
* add FAIL status when incompatible FIT uploaded for factoryreset
* change fail texts for incompatible fits
* wb-factoryreset: increase wait time for reboot
* install_update.sh: suppress mount output on factory reset
* add factoryreset.fit mode/ownership change at wb-watch-update startup (required for web-based factory reset feature)
add --from-emmc-factoryreset flag to wb-factoryreset for proper factoryreset.original.fit work
  • Loading branch information
lostpoint-ru authored Dec 5, 2023
1 parent 4e5ab25 commit 9065eb0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
wb-utils (4.20.1) stable; urgency=medium

* More fixes for factoryreset support, no functional changes

-- Aleksandr Kazadaev <[email protected]> Thu, 30 Nov 2023 22:49:29 +0600

wb-utils (4.20.0) stable; urgency=medium

* Now publishing factoryreset fits info to mqtt topics
Expand Down
37 changes: 34 additions & 3 deletions utils/bin/wb-factoryreset
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@
DIR_RESTORE="/mnt/data/.wb-restore"
DIR_UPDATE="/mnt/data/.wb-update"

fit_prop() {
local node=$1
local prop=$2

local tmp=`fit_info -f $FIT -n $node -p $prop`
local len=`sed -rn 's#LEN: (.*)#\1#p' <<< "$tmp"`
local off=`sed -rn 's#OFF: (.*)#\1#p' <<< "$tmp"`
[[ -n "$len" && -n "$off" ]] || return 1
tail -c +$((off + 1)) $FIT 2>/dev/null | head -c $len 2>/dev/null
}

fit_prop_string() {
fit_prop "$@" | tr -d '\0'
}

fw_compatible() {
local feature="$1"
local fw_compat
fw_compat=$(FIT="$DIR_RESTORE/factoryreset.fit" fit_prop_string / firmware-compatible)

case "$fw_compat" in
*"+$feature "*) return 0 ;;
*) return 1 ;;
esac
}

if ! fw_compatible fit-factory-reset; then
echo "Factory reset is not supported by this firmware"
exit 1
fi

if [[ "$1" != "--force" ]]; then
echo "This will erase all your data and restore the system to factory defaults."
echo "Type \"factoryreset\" to confirm:"
Expand All @@ -14,13 +45,13 @@ if [[ "$1" != "--force" ]]; then
fi

mkdir -p "$DIR_UPDATE"
echo "--factoryreset " > "$DIR_UPDATE/install_update.web.flags"
echo "--factoryreset --from-emmc-factoryreset " > "$DIR_UPDATE/install_update.web.flags"
ln "$DIR_RESTORE/factoryreset.fit" "$DIR_UPDATE/webupd.fit"
touch "$DIR_UPDATE/webupd.fit # to trigger wb-watch-update"
touch "$DIR_UPDATE/webupd.fit" # to trigger wb-watch-update

echo "Factory reset initiated, system will reboot soon"

sleep 30
sleep 60

echo "System did not reboot, something went wrong, check /mnt/data/.wb-update/wb-console.log"

Expand Down
30 changes: 29 additions & 1 deletion utils/bin/wb-watch-update
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,17 @@ publish_factoryreset_fits_info() {
publish_fit_info "$factoryreset_original_fit" "factoryreset-original"
}

publish_factoryreset_fit_info
make_factoryreset_fit_accessible() {
if [ -f /mnt/data/.wb-restore/factoryreset.fit ]; then
chown root:www-data /mnt/data/.wb-restore/factoryreset.fit
chmod 0660 /mnt/data/.wb-restore/factoryreset.fit
fi
}

# Web FR needs rw access to factory fit to create hardlink and open it for writing to trigger wb-watch-update
make_factoryreset_fit_accessible

publish_factoryreset_fits_info

maybe_publish_singlemode_update_status

Expand Down Expand Up @@ -183,6 +193,24 @@ while read EVENT_DIR EVENT_TYPE EVENT_FILE; do
# Remove old updates, we won't run it anyway
cleanup_watch_dir -not -name ${EVENT_FILE} -not -name 'install_update.*'

# Some sanity checks
web_flags_file="$(dirname "$FIT")/install_update.web.flags"
if [ -e "$web_flags_file" ]; then
if grep "\--factoryreset " "$web_flags_file"; then
if ! fw_compatible fit-factory-reset; then
msg="Factory reset requested, but FIT file does not support it, cancelling operation"
log "$msg"
mqtt_log "$msg"
mqtt_status "ERROR Incompatible FIT file"
mqtt_status "IDLE"
rm -f "$web_flags_file"
rm -f "$FIT"
LAST_FIT=""
continue
fi
fi
fi

msg="Received good update FIT $FIT, starting update"
log "$msg"
mqtt_log "$msg"
Expand Down
2 changes: 1 addition & 1 deletion utils/lib/wb-image-update/fit/install_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ maybe_factory_reset() {

mkdir -p /mnt
mkdir -p /mnt/data
mount -t auto $DATA_PART /mnt/data || true
mount -t auto $DATA_PART /mnt/data 2>/dev/null || true

rm -rf /tmp/empty && mkdir /tmp/empty
rsync -a --delete --exclude="/.wb-restore/" --exclude="/.wb-update/" /tmp/empty/ /mnt/data/
Expand Down

0 comments on commit 9065eb0

Please sign in to comment.