Consider adding checksum verification to mpremote file transfers
Description
I've been using mpremote heavily over the past week to upload .py/.mpy files to my board via UART. Twice, my MCU ran into mysterious errors that were fixed simply by re-uploading the same file. That strongly suggested the transferred file got corrupted during transmission (likely due to marginal serial line quality).
Would it be possible to add a simple verification mechanism (like a checksum or CRC) for file transfers? (Commands need checksum also if it is a long function)
Code Size
We can implement this to mpremote pc tool. In that case no firmware size incresement
Implementation
I hope the MicroPython maintainers or community will implement this feature
Code of Conduct
Yes, I agree
Update:
In addition to verifying the transmitted file, I believe we should also consider verifying the commands sent from PC to MCU. Like this :
mcu_need_run='''
def cmd_from_mpremote():
...
long function
...
cmd_from_mpremote()
'''
hash_of_mcu_need_run='6789a1b2c3....'
if check(mcu_need_run, hash_of_mcu_need_run):
exec(mcu_need_run)
That sounds will require a lot of changes. But I think it makes sense. From my understanding, mpremote works by sending Python code as string via UART to MCU REPL.
Update again:
When we use mpremote cat, mpremote cp to fetch files from mcu to pc, also, checksum is needed.
tools/mpremote: Add streaming hash verification to file transfers
Summary
Addresses #18420 by adding streaming SHA256 verification to file transfers. Computes hash incrementally during transfer to detect transmission corruption on unreliable serial connections, without requiring flash re-read.
Testing
Tested on ESP32 via CH340 USB-UART with files from 62B to 25KB. Overhead is ~180ms absolute per file (2-3% for large files, 20-28% for small files). Verified bidirectional transfers and --force bypass.
Trade-offs and Alternatives
Hash computed on device during transfer using data already in RAM. More efficient than post-transfer flash re-read. Small files have higher relative overhead but minimal absolute penalty. Acceptable trade-off for corruption detection on marginal connections.