2018-05-24 13:15:20 +00:00
|
|
|
# Driver for Mboot, the MicroPython boot loader
|
|
|
|
# MIT license; Copyright (c) 2018 Damien P. George
|
|
|
|
|
|
|
|
import struct, time, os, hashlib
|
|
|
|
|
|
|
|
|
|
|
|
I2C_CMD_ECHO = 1
|
|
|
|
I2C_CMD_GETID = 2
|
|
|
|
I2C_CMD_GETCAPS = 3
|
|
|
|
I2C_CMD_RESET = 4
|
|
|
|
I2C_CMD_CONFIG = 5
|
|
|
|
I2C_CMD_GETLAYOUT = 6
|
|
|
|
I2C_CMD_MASSERASE = 7
|
|
|
|
I2C_CMD_PAGEERASE = 8
|
|
|
|
I2C_CMD_SETRDADDR = 9
|
|
|
|
I2C_CMD_SETWRADDR = 10
|
|
|
|
I2C_CMD_READ = 11
|
|
|
|
I2C_CMD_WRITE = 12
|
|
|
|
I2C_CMD_COPY = 13
|
|
|
|
I2C_CMD_CALCHASH = 14
|
|
|
|
I2C_CMD_MARKVALID = 15
|
|
|
|
|
|
|
|
|
|
|
|
class Bootloader:
|
|
|
|
def __init__(self, i2c, addr):
|
|
|
|
self.i2c = i2c
|
|
|
|
self.addr = addr
|
|
|
|
self.buf1 = bytearray(1)
|
|
|
|
try:
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(addr, b"")
|
2018-05-24 13:15:20 +00:00
|
|
|
except OSError:
|
2020-02-27 04:36:53 +00:00
|
|
|
raise Exception("no I2C mboot device found")
|
2018-05-24 13:15:20 +00:00
|
|
|
|
|
|
|
def wait_response(self):
|
|
|
|
start = time.ticks_ms()
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
self.i2c.readfrom_into(self.addr, self.buf1)
|
|
|
|
n = self.buf1[0]
|
|
|
|
break
|
|
|
|
except OSError as er:
|
|
|
|
time.sleep_us(500)
|
|
|
|
if time.ticks_diff(time.ticks_ms(), start) > 5000:
|
2020-02-27 04:36:53 +00:00
|
|
|
raise Exception("timeout")
|
2018-05-24 13:15:20 +00:00
|
|
|
if n >= 129:
|
|
|
|
raise Exception(n)
|
|
|
|
if n == 0:
|
2020-02-27 04:36:53 +00:00
|
|
|
return b""
|
2018-05-24 13:15:20 +00:00
|
|
|
else:
|
|
|
|
return self.i2c.readfrom(self.addr, n)
|
|
|
|
|
|
|
|
def wait_empty_response(self):
|
|
|
|
ret = self.wait_response()
|
|
|
|
if ret:
|
2020-02-27 04:36:53 +00:00
|
|
|
raise Exception("expected empty response got %r" % ret)
|
2018-05-24 13:15:20 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def echo(self, data):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<B", I2C_CMD_ECHO) + data)
|
2018-05-24 13:15:20 +00:00
|
|
|
return self.wait_response()
|
|
|
|
|
|
|
|
def getid(self):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<B", I2C_CMD_GETID))
|
2018-05-24 13:15:20 +00:00
|
|
|
ret = self.wait_response()
|
|
|
|
unique_id = ret[:12]
|
2020-02-27 04:36:53 +00:00
|
|
|
mcu_name, board_name = ret[12:].split(b"\x00")
|
|
|
|
return unique_id, str(mcu_name, "ascii"), str(board_name, "ascii")
|
2018-05-24 13:15:20 +00:00
|
|
|
|
|
|
|
def reset(self):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<B", I2C_CMD_RESET))
|
2018-05-24 13:15:20 +00:00
|
|
|
# we don't expect any response
|
|
|
|
|
|
|
|
def getlayout(self):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<B", I2C_CMD_GETLAYOUT))
|
2018-05-24 13:15:20 +00:00
|
|
|
layout = self.wait_response()
|
2020-02-27 04:36:53 +00:00
|
|
|
id, flash_addr, layout = layout.split(b"/")
|
|
|
|
assert id == b"@Internal Flash "
|
2018-05-24 13:15:20 +00:00
|
|
|
flash_addr = int(flash_addr, 16)
|
|
|
|
pages = []
|
2020-02-27 04:36:53 +00:00
|
|
|
for chunk in layout.split(b","):
|
|
|
|
n, sz = chunk.split(b"*")
|
2018-05-24 13:15:20 +00:00
|
|
|
n = int(n)
|
2020-02-27 04:36:53 +00:00
|
|
|
assert sz.endswith(b"Kg")
|
2018-05-24 13:15:20 +00:00
|
|
|
sz = int(sz[:-2]) * 1024
|
|
|
|
for i in range(n):
|
|
|
|
pages.append((flash_addr, sz))
|
|
|
|
flash_addr += sz
|
|
|
|
return pages
|
|
|
|
|
|
|
|
def pageerase(self, addr):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<BI", I2C_CMD_PAGEERASE, addr))
|
2018-05-24 13:15:20 +00:00
|
|
|
self.wait_empty_response()
|
|
|
|
|
|
|
|
def setrdaddr(self, addr):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<BI", I2C_CMD_SETRDADDR, addr))
|
2018-05-24 13:15:20 +00:00
|
|
|
self.wait_empty_response()
|
|
|
|
|
|
|
|
def setwraddr(self, addr):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<BI", I2C_CMD_SETWRADDR, addr))
|
2018-05-24 13:15:20 +00:00
|
|
|
self.wait_empty_response()
|
|
|
|
|
|
|
|
def read(self, n):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<BB", I2C_CMD_READ, n))
|
2018-05-24 13:15:20 +00:00
|
|
|
return self.wait_response()
|
|
|
|
|
|
|
|
def write(self, buf):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<B", I2C_CMD_WRITE) + buf)
|
2018-05-24 13:15:20 +00:00
|
|
|
self.wait_empty_response()
|
|
|
|
|
|
|
|
def calchash(self, n):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<BI", I2C_CMD_CALCHASH, n))
|
2018-05-24 13:15:20 +00:00
|
|
|
return self.wait_response()
|
|
|
|
|
|
|
|
def markvalid(self):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.i2c.writeto(self.addr, struct.pack("<B", I2C_CMD_MARKVALID))
|
2018-05-24 13:15:20 +00:00
|
|
|
self.wait_empty_response()
|
|
|
|
|
|
|
|
def deployfile(self, filename, addr):
|
|
|
|
pages = self.getlayout()
|
|
|
|
page_erased = [False] * len(pages)
|
2020-02-27 04:36:53 +00:00
|
|
|
buf = bytearray(128) # maximum payload supported by I2C protocol
|
2018-05-24 13:15:20 +00:00
|
|
|
start_addr = addr
|
|
|
|
self.setwraddr(addr)
|
|
|
|
fsize = os.stat(filename)[6]
|
|
|
|
local_sha = hashlib.sha256()
|
2020-02-27 04:36:53 +00:00
|
|
|
print("Deploying %s to location 0x%08x" % (filename, addr))
|
|
|
|
with open(filename, "rb") as f:
|
2018-05-24 13:15:20 +00:00
|
|
|
t0 = time.ticks_ms()
|
|
|
|
while True:
|
|
|
|
n = f.readinto(buf)
|
|
|
|
if n == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
# check if we need to erase the page
|
|
|
|
for i, p in enumerate(pages):
|
|
|
|
if p[0] <= addr < p[0] + p[1]:
|
|
|
|
# found page
|
|
|
|
if not page_erased[i]:
|
2020-02-27 04:36:53 +00:00
|
|
|
print(
|
|
|
|
"\r% 3u%% erase 0x%08x"
|
|
|
|
% (100 * (addr - start_addr) // fsize, addr),
|
|
|
|
end="",
|
|
|
|
)
|
2018-05-24 13:15:20 +00:00
|
|
|
self.pageerase(addr)
|
|
|
|
page_erased[i] = True
|
|
|
|
break
|
|
|
|
else:
|
2020-02-27 04:36:53 +00:00
|
|
|
raise Exception("address 0x%08x not valid" % addr)
|
2018-05-24 13:15:20 +00:00
|
|
|
|
|
|
|
# write the data
|
|
|
|
self.write(buf)
|
|
|
|
|
|
|
|
# update local SHA256, with validity bits set
|
|
|
|
if addr == start_addr:
|
|
|
|
buf[0] |= 3
|
|
|
|
if n == len(buf):
|
|
|
|
local_sha.update(buf)
|
|
|
|
else:
|
|
|
|
local_sha.update(buf[:n])
|
|
|
|
|
|
|
|
addr += n
|
|
|
|
ntotal = addr - start_addr
|
|
|
|
if ntotal % 2048 == 0 or ntotal == fsize:
|
2020-02-27 04:36:53 +00:00
|
|
|
print("\r% 3u%% % 7u bytes " % (100 * ntotal // fsize, ntotal), end="")
|
2018-05-24 13:15:20 +00:00
|
|
|
t1 = time.ticks_ms()
|
|
|
|
print()
|
2020-02-27 04:36:53 +00:00
|
|
|
print("rate: %.2f KiB/sec" % (1024 * ntotal / (t1 - t0) / 1000))
|
2018-05-24 13:15:20 +00:00
|
|
|
|
|
|
|
local_sha = local_sha.digest()
|
2020-02-27 04:36:53 +00:00
|
|
|
print("Local SHA256: ", "".join("%02x" % x for x in local_sha))
|
2018-05-24 13:15:20 +00:00
|
|
|
|
|
|
|
self.setrdaddr(start_addr)
|
|
|
|
remote_sha = self.calchash(ntotal)
|
2020-02-27 04:36:53 +00:00
|
|
|
print("Remote SHA256:", "".join("%02x" % x for x in remote_sha))
|
2018-05-24 13:15:20 +00:00
|
|
|
|
|
|
|
if local_sha == remote_sha:
|
2020-02-27 04:36:53 +00:00
|
|
|
print("Marking app firmware as valid")
|
2018-05-24 13:15:20 +00:00
|
|
|
self.markvalid()
|
|
|
|
|
|
|
|
self.reset()
|