Backup and recovery

Creating a backup

scooterflasher backup --device xiaomi-m365 --target xiaomi-m365-esc --adapter stlink

This connects read-only, confirms the detected chip matches the profile's target (refuses to proceed otherwise), halts the core, reads every region declared on the target, and writes them to a timestamped directory under the platform backup directory (scooterflasher doctor shows the exact path; override with --output-dir).

Every backup directory contains one <region>.bin per region plus a manifest.json recording the model/target id, creation time, and each region's sha256. The directory is built in a temp location and only renamed into its final path once every file is written — a crash mid-backup can never leave a partial backup at the expected location.

flash and restore also take a backup automatically before any destructive step (unless you pass --skip-backup, which itself requires --confirm skip_backup) — see architecture.md.

Restoring a backup

scooterflasher restore --device xiaomi-m365 --target xiaomi-m365-esc \
  --backup-dir <path-from-backup-command> --adapter stlink \
  --confirm experimental_profile:experimental

restore is not a separate, less-scrutinized code path: it turns the backup's region data into the exact same FlashOptions/FlashOperation pipeline flash uses, including its own backup-before-write and confirmation gates. It refuses to restore a backup taken from a different target_id than the one you're pointing it at. Use --region <name> (repeatable) to restore only specific regions instead of the whole backup.

Verifying without erasing

scooterflasher verify --device xiaomi-m365 --target xiaomi-m365-esc \
  --write firmware=./my-firmware.bin --adapter stlink

Reads back the given region(s) and compares against the given content, without touching flash. Exits non-zero (VERIFICATION_ERROR, code 17) on any mismatch.

Limitations — read this before your first flash

  • A backup requires the target to be readable. If read-out protection (RDP/APPROTECT) is enabled, the backup may fail or be incomplete. The OpenOCD backend cannot detect protection (is_protected() always returns None), so no warning and no unlock_protected_target confirmation appears: judge for yourself before flashing. Unlocking a protected chip commonly requires a mass erase, and there is no way to recover data from a chip that was already protected before you could back it up.
  • A mass erase is not guaranteed reversible. restore can write the backed-up bytes back, but if the erase or the restore itself is interrupted (power loss, disconnected adapter), the target may be left in a state neither the old nor the new image. Do not disconnect power or the programming adapter during any flash/restore operation.
  • Verification is real but bounded. verify_memory does an independent read-back comparison — it is not inferred from OpenOCD's own "verified" log line. It confirms the bytes you asked to write are the bytes now on the device; it does not prove the firmware image itself is correct or safe to run.
  • The OpenOCD backend has had very little hardware testing (see support-matrix.md). Rehearse the backup/plan/flash/verify/restore flow against FakeBackend (which the automated test suite does extensively) before trusting it, and start with a device you can afford to lose.