8000 micropython/espflash: Use hashlib.md5 to generate file digest. · micropython/micropython-lib@1918c47 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1918c47

Browse files
committed
micropython/espflash: Use hashlib.md5 to generate file digest.
* Use hashlib to generate the file MD5 digest when available, and if none is given in the args.
1 parent 9cb1b34 commit 1918c47

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

micropython/espflash/espflash.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from machine import UART
3333
from micropython import const
3434
from time import sleep
35+
import binascii
3536

3637
_CMD_SYNC = const(0x08)
3738
_CMD_CHANGE_BAUDRATE = const(0x0F)
@@ -76,6 +77,13 @@ def __init__(
7677
self.reset_pin = Pin(reset, Pin.OUT)
7778
self.gpio0_pin = Pin(gpio0, Pin.OUT)
7879
self.set_baudrate(self.uart_baudrate)
80+
self.md5sum = None
81+
try:
82+
import hashlib
83+
84+
self.md5sum = hashlib.md5()
85+
except ImportError:
86+
pass
7987

8088
def _log(self, data, out=True):
8189
if self.log:
@@ -266,8 +274,11 @@ def flash_write_file(self, path, blksize=0x1000):
266274
subseq = 0
267275
for i in range(total_blocks):
268276
buf = f.read(blksize)
277+
# Update digest
278+
if self.md5sum is not None:
279+
self.md5sum.update(buf)
280+
# The last data block should be padded to the block size with 0xFF bytes.
269281
if len(buf) < blksize:
270-
# The last data block should be padded to the block size with 0xFF bytes.
271282
buf += b"\xFF" * (blksize - len(buf))
272283
checksum = self.checksum(buf)
273284
if seq % erase_blocks == 0:
@@ -288,15 +299,22 @@ def flash_write_file(self, path, blksize=0x1000):
288299

289300
print("Flash write finished")
290301

291-
def flash_verify_file(self, path, md5sum, offset=0):
302+
def flash_verify_file(self, path, digest=None, offset=0):
303+
if digest is None:
304+
if self.md5sum is None:
305+
raise Exception(f"MD5 checksum missing.")
306+
digest = binascii.hexlify(self.md5sum.digest())
307+
292308
size = os.stat(path)[6]
293309
val, data = self.command(_CMD_SPI_FLASH_MD5, struct.pack("<IIII", offset, size, 0, 0))
294-
print(f"Flash verify file MD5 {md5sum}")
295-
print(f"Flash verify flash MD5 {bytes(data[0:32])}")
296-
if md5sum == data[0:32]:
297-
print("Firmware write verified")
310+
311+
print(f"Flash verify: File MD5 {digest}")
312+
print(f"Flash verify: Flash MD5 {bytes(data[0:32])}")
313+
314+
if digest == data[0:32]:
315+
print("Firmware verified.")
298316
else:
299-
raise Exception(f"Firmware verification failed")
317+
raise Exception(f"Firmware verification failed.")
300318

301319
def reboot(self):
302320
payload = struct.pack("<I", 0)

0 commit comments

Comments
 (0)
0