2019-02-14 23:05:53 -05:00
|
|
|
# Update Mboot or MicroPython from a .dfu.gz file on the board's filesystem
|
2020-06-23 09:58:26 -04:00
|
|
|
# MIT license; Copyright (c) 2019-2020 Damien P. George
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
import struct, time
|
|
|
|
import uzlib, machine, stm
|
|
|
|
|
2020-06-23 09:58:26 -04:00
|
|
|
# Constants to be used with update_mpy
|
|
|
|
VFS_FAT = 1
|
|
|
|
VFS_LFS1 = 2
|
|
|
|
VFS_LFS2 = 3
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
FLASH_KEY1 = 0x45670123
|
2020-02-26 23:36:53 -05:00
|
|
|
FLASH_KEY2 = 0xCDEF89AB
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
def check_mem_contains(addr, buf):
|
|
|
|
mem8 = stm.mem8
|
|
|
|
r = range(len(buf))
|
|
|
|
for off in r:
|
|
|
|
if mem8[addr + off] != buf[off]:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
def check_mem_erased(addr, size):
|
|
|
|
mem16 = stm.mem16
|
|
|
|
r = range(0, size, 2)
|
|
|
|
for off in r:
|
2020-02-26 23:36:53 -05:00
|
|
|
if mem16[addr + off] != 0xFFFF:
|
2019-02-14 23:05:53 -05:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
def dfu_read(filename):
|
2020-02-26 23:36:53 -05:00
|
|
|
f = open(filename, "rb")
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
hdr = f.read(3)
|
|
|
|
f.seek(0)
|
2020-02-26 23:36:53 -05:00
|
|
|
if hdr == b"Dfu":
|
2019-02-14 23:05:53 -05:00
|
|
|
pass
|
2020-02-26 23:36:53 -05:00
|
|
|
elif hdr == b"\x1f\x8b\x08":
|
2019-02-14 23:05:53 -05:00
|
|
|
f = uzlib.DecompIO(f, 16 + 15)
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Invalid firmware", filename)
|
2019-02-14 23:05:53 -05:00
|
|
|
return None
|
|
|
|
|
|
|
|
elems = []
|
|
|
|
|
|
|
|
hdr = f.read(11)
|
2020-02-26 23:36:53 -05:00
|
|
|
sig, ver, size, num_targ = struct.unpack("<5sBIB", hdr)
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
file_offset = 11
|
|
|
|
|
|
|
|
for i in range(num_targ):
|
|
|
|
hdr = f.read(274)
|
2020-02-26 23:36:53 -05:00
|
|
|
sig, alt, has_name, name, t_size, num_elem = struct.unpack("<6sBi255sII", hdr)
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
file_offset += 274
|
|
|
|
file_offset_t = file_offset
|
|
|
|
for j in range(num_elem):
|
|
|
|
hdr = f.read(8)
|
2020-02-26 23:36:53 -05:00
|
|
|
addr, e_size = struct.unpack("<II", hdr)
|
2019-02-14 23:05:53 -05:00
|
|
|
data = f.read(e_size)
|
|
|
|
elems.append((addr, data))
|
|
|
|
file_offset += 8 + e_size
|
|
|
|
|
|
|
|
if t_size != file_offset - file_offset_t:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("corrupt DFU", t_size, file_offset - file_offset_t)
|
2019-02-14 23:05:53 -05:00
|
|
|
return None
|
|
|
|
|
|
|
|
if size != file_offset:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("corrupt DFU", size, file_offset)
|
2019-02-14 23:05:53 -05:00
|
|
|
return None
|
|
|
|
|
|
|
|
hdr = f.read(16)
|
2020-02-26 23:36:53 -05:00
|
|
|
hdr = struct.unpack("<HHHH3sBI", hdr)
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
return elems
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
def flash_wait_not_busy():
|
|
|
|
while stm.mem32[stm.FLASH + stm.FLASH_SR] & 1 << 16:
|
|
|
|
machine.idle()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
def flash_unlock():
|
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_KEYR] = FLASH_KEY1
|
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_KEYR] = FLASH_KEY2
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
def flash_lock():
|
2020-02-26 23:36:53 -05:00
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_CR] = 1 << 31 # LOCK
|
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
def flash_erase_sector(sector):
|
2020-02-26 23:36:53 -05:00
|
|
|
assert 0 <= sector <= 7 # for F722
|
2019-02-14 23:05:53 -05:00
|
|
|
flash_wait_not_busy()
|
2020-02-26 23:36:53 -05:00
|
|
|
cr = 2 << 8 | sector << 3 | 1 << 1 # PSIZE = 32 bits # SNB # SER
|
2019-02-14 23:05:53 -05:00
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_CR] = cr
|
2020-02-26 23:36:53 -05:00
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_CR] = cr | 1 << 16 # STRT
|
2019-02-14 23:05:53 -05:00
|
|
|
flash_wait_not_busy()
|
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_CR] = 0
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
def flash_write(addr, buf):
|
|
|
|
assert len(buf) % 4 == 0
|
|
|
|
flash_wait_not_busy()
|
2020-02-26 23:36:53 -05:00
|
|
|
cr = 2 << 8 | 1 << 0 # PSIZE = 32 bits # PG
|
2019-02-14 23:05:53 -05:00
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_CR] = cr
|
|
|
|
for off in range(0, len(buf), 4):
|
2020-02-26 23:36:53 -05:00
|
|
|
stm.mem32[addr + off] = struct.unpack_from("I", buf, off)[0]
|
2019-02-14 23:05:53 -05:00
|
|
|
flash_wait_not_busy()
|
|
|
|
stm.mem32[stm.FLASH + stm.FLASH_CR] = 0
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-02-14 23:05:53 -05:00
|
|
|
def update_mboot(filename):
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Loading file", filename)
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
mboot_fw = dfu_read(filename)
|
|
|
|
if mboot_fw is None:
|
|
|
|
return
|
|
|
|
if len(mboot_fw) != 1:
|
|
|
|
assert 0
|
|
|
|
mboot_addr, mboot_fw = mboot_fw[0]
|
|
|
|
if mboot_addr != 0x08000000:
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
# TODO: Validate firmware in a simple way
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Found Mboot data with size %u." % len(mboot_fw))
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
chk = check_mem_contains(mboot_addr, mboot_fw)
|
|
|
|
if chk:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Supplied version of Mboot is already on device.")
|
2019-02-14 23:05:53 -05:00
|
|
|
return
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Programming Mboot, do not turn off!")
|
2019-02-14 23:05:53 -05:00
|
|
|
time.sleep_ms(50)
|
|
|
|
|
|
|
|
irq = machine.disable_irq()
|
|
|
|
flash_unlock()
|
|
|
|
flash_erase_sector(0)
|
|
|
|
if len(mboot_fw) > 16 * 1024 and not check_mem_erased(mboot_addr + 16 * 1024, 16 * 1024):
|
|
|
|
flash_erase_sector(1)
|
|
|
|
flash_write(mboot_addr, mboot_fw)
|
|
|
|
flash_lock()
|
|
|
|
machine.enable_irq(irq)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("New Mboot programmed.")
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
if check_mem_contains(mboot_addr, mboot_fw):
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Verification of new Mboot succeeded.")
|
2019-02-14 23:05:53 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Verification of new Mboot FAILED! Try rerunning.")
|
|
|
|
|
|
|
|
print("Programming finished, can now reset or turn off.")
|
2019-02-14 23:05:53 -05:00
|
|
|
|
|
|
|
|
2020-06-23 09:58:26 -04:00
|
|
|
def update_mpy(filename, fs_base, fs_len, fs_type=VFS_FAT):
|
2021-01-14 11:00:40 -05:00
|
|
|
# Check firmware is of .dfu or .dfu.gz type
|
2019-02-14 23:05:53 -05:00
|
|
|
try:
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(filename, "rb") as f:
|
2019-02-14 23:05:53 -05:00
|
|
|
hdr = uzlib.DecompIO(f, 16 + 15).read(6)
|
|
|
|
except Exception:
|
2021-01-14 11:00:40 -05:00
|
|
|
with open(filename, "rb") as f:
|
|
|
|
hdr = f.read(6)
|
2020-02-26 23:36:53 -05:00
|
|
|
if hdr != b"DfuSe\x01":
|
2021-01-14 11:00:40 -05:00
|
|
|
print("Firmware must be a .dfu(.gz) file.")
|
2019-02-14 23:05:53 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
ELEM_TYPE_END = 1
|
|
|
|
ELEM_TYPE_MOUNT = 2
|
|
|
|
ELEM_TYPE_FSLOAD = 3
|
|
|
|
mount_point = 1
|
2020-06-23 09:58:26 -04:00
|
|
|
mount = struct.pack("<BBBBLL", ELEM_TYPE_MOUNT, 10, mount_point, fs_type, fs_base, fs_len)
|
2020-02-26 23:36:53 -05:00
|
|
|
fsup = struct.pack("<BBB", ELEM_TYPE_FSLOAD, 1 + len(filename), mount_point) + bytes(
|
|
|
|
filename, "ascii"
|
|
|
|
)
|
|
|
|
end = struct.pack("<BB", ELEM_TYPE_END, 0)
|
2019-02-14 23:05:53 -05:00
|
|
|
machine.bootloader(mount + fsup + end)
|