panda/python/flash_release.py

75 lines
2.0 KiB
Python
Raw Normal View History

2019-09-24 18:50:53 -06:00
#!/usr/bin/env python3
2019-09-24 23:33:46 -06:00
2017-08-23 11:43:40 -06:00
import sys
import time
2017-08-23 21:46:34 -06:00
import requests
import json
2019-09-24 23:33:46 -06:00
import io
2017-08-23 11:43:40 -06:00
2017-08-23 21:46:34 -06:00
def flash_release(path=None, st_serial=None):
from panda import Panda, PandaDFU
2017-08-23 21:46:34 -06:00
from zipfile import ZipFile
2017-08-23 11:43:40 -06:00
2017-08-23 21:46:34 -06:00
def status(x):
print("\033[1;32;40m" + x + "\033[00m")
2017-08-23 21:46:34 -06:00
if st_serial is not None:
2017-08-23 21:46:34 -06:00
# look for Panda
panda_list = Panda.list()
if len(panda_list) == 0:
raise Exception("panda not found, make sure it's connected and your user can access it")
elif len(panda_list) > 1:
raise Exception("Please only connect one panda")
st_serial = panda_list[0]
print("Using panda with serial %s" % st_serial)
2020-09-07 02:26:27 -06:00
if path is None:
2017-08-23 21:46:34 -06:00
print("Fetching latest firmware from github.com/commaai/panda-artifacts")
r = requests.get("https://raw.githubusercontent.com/commaai/panda-artifacts/master/latest.json")
url = json.loads(r.text)['url']
r = requests.get(url)
print("Fetching firmware from %s" % url)
2020-09-07 02:26:27 -06:00
path = io.BytesIO(r.content)
2017-08-23 21:46:34 -06:00
zf = ZipFile(path)
2017-08-23 11:43:40 -06:00
zf.printdir()
2020-09-07 02:26:27 -06:00
version = zf.read("version").decode()
status("0. Preparing to flash " + str(version))
2017-08-23 11:43:40 -06:00
code_bootstub = zf.read("bootstub.panda.bin")
code_panda = zf.read("panda.bin")
# enter DFU mode
status("1. Entering DFU mode")
panda = Panda(st_serial)
2020-09-07 02:26:27 -06:00
panda.reset(enter_bootstub=True)
panda.reset(enter_bootloader=True)
2017-08-23 11:43:40 -06:00
time.sleep(1)
# program bootstub
status("2. Programming bootstub")
dfu = PandaDFU(PandaDFU.st_serial_to_dfu_serial(st_serial))
2017-08-28 11:42:23 -06:00
dfu.program_bootstub(code_bootstub)
2017-08-23 11:43:40 -06:00
time.sleep(1)
# flash main code
status("3. Flashing main code")
panda = Panda(st_serial)
panda.flash(code=code_panda)
panda.close()
# check for connection
status("4. Verifying version")
2017-08-23 11:43:40 -06:00
panda = Panda(st_serial)
my_version = panda.get_version()
2017-08-24 11:35:56 -06:00
print("dongle id: %s" % panda.get_serial()[0])
2017-08-23 11:43:40 -06:00
print(my_version, "should be", version)
assert(str(version) == str(my_version))
# done!
status("6. Success!")
2019-07-19 20:35:50 -06:00
2017-08-23 21:46:34 -06:00
if __name__ == "__main__":
flash_release(*sys.argv[1:])