87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
import serial
|
|
import time
|
|
import sys
|
|
|
|
PORT = '/dev/cu.usbmodem101'
|
|
print("=== MicroPython Robust Flash Wipe Tool ===")
|
|
print(f"Waiting for a clean, active connection on {PORT}...")
|
|
print("Please press the physical RESET button on the board now...")
|
|
|
|
ser = None
|
|
while True:
|
|
try:
|
|
# Open port
|
|
ser = serial.Serial(PORT, 115200, timeout=1.0)
|
|
# Try writing Ctrl-C to verify if the link is active
|
|
ser.write(b'\x03')
|
|
time.sleep(0.01)
|
|
ser.write(b'\x03')
|
|
|
|
# If write succeeded, we have an active link!
|
|
print("\n[+] Active connection established!")
|
|
break
|
|
except (serial.SerialException, OSError, Exception) as e:
|
|
if ser is not None:
|
|
try:
|
|
ser.close()
|
|
except:
|
|
pass
|
|
ser = None
|
|
# Print dot to show progress, stay on same line
|
|
print(".", end="")
|
|
sys.stdout.flush()
|
|
time.sleep(0.1)
|
|
|
|
print("[*] Sending remaining Ctrl-C storm to break into REPL...")
|
|
for _ in range(80):
|
|
try:
|
|
ser.write(b'\x03')
|
|
time.sleep(0.005)
|
|
except Exception as e:
|
|
print(f"\n[-] Write error during storm: {e}")
|
|
break
|
|
|
|
time.sleep(0.1)
|
|
try:
|
|
ser.reset_input_buffer()
|
|
except Exception as e:
|
|
print(f"[-] Buffer reset failed: {e}")
|
|
|
|
# Check if we have REPL access
|
|
print("[*] Verifying REPL responsiveness...")
|
|
try:
|
|
ser.write(b'\r\n')
|
|
time.sleep(0.2)
|
|
if ser.in_waiting:
|
|
resp = ser.read(ser.in_waiting)
|
|
print(f"REPL Response:\n{resp.decode('utf-8', errors='ignore')}")
|
|
else:
|
|
ser.write(b'\x03\r\n')
|
|
time.sleep(0.2)
|
|
resp = ser.read(ser.in_waiting)
|
|
print(f"REPL Response:\n{resp.decode('utf-8', errors='ignore')}")
|
|
except Exception as e:
|
|
print(f"[-] REPL verification failed: {e}")
|
|
ser.close()
|
|
sys.exit(1)
|
|
|
|
print("[*] Deleting all files on the flash...")
|
|
wipe_code = b"import os; print('FILES_BEFORE:', os.listdir()); [os.remove(f) for f in os.listdir() if not (os.stat(f)[0] & 0x4000)]; print('FILES_AFTER:', os.listdir()); print('WIPE_SUCCESS')\r\n"
|
|
|
|
try:
|
|
ser.write(wipe_code)
|
|
time.sleep(1.0)
|
|
if ser.in_waiting:
|
|
result = ser.read(ser.in_waiting).decode('utf-8', errors='ignore')
|
|
print(f"Execution Output:\n{result}")
|
|
if "WIPE_SUCCESS" in result:
|
|
print("[+] SUCCESS! Flash has been wiped clean.")
|
|
else:
|
|
print("[-] Wipe did not finish successfully.")
|
|
else:
|
|
print("[-] No response to wipe command.")
|
|
except Exception as e:
|
|
print(f"[-] Error during wipe command execution: {e}")
|
|
|
|
ser.close()
|