add a first draft of precursor_updater.py

this should be an "all in one" updater.
This commit is contained in:
bunnie 2022-07-22 03:46:31 +08:00
parent bad4865ea3
commit 8f6fa5f53d
16 changed files with 990 additions and 0 deletions

202
tools/updater/LICENSE Normal file
View file

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

8
tools/updater/README.md Normal file
View file

@ -0,0 +1,8 @@
# Precursor Updater
This script can automatically update a Precursor device. Please run with `--help` to see
the available options.
When the script is finished running, you will need to reset the device by pressing on the
switch inside the hole in the lower right hand side of the case. See https://ci.betrusted.io/i/reset.jpg
for an example of how to do this.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,4 @@
Metadata-Version: 2.1
Name: precursor-updater
Version: 0.0.1
License-File: LICENSE

View file

@ -0,0 +1,11 @@
LICENSE
README.md
pyproject.toml
precursor_updater/__init__.py
precursor_updater/precursor_updater.py
precursor_updater/precursorusb.py
precursor_updater.egg-info/PKG-INFO
precursor_updater.egg-info/SOURCES.txt
precursor_updater.egg-info/dependency_links.txt
precursor_updater.egg-info/requires.txt
precursor_updater.egg-info/top_level.txt

View file

@ -0,0 +1,8 @@
requests
pyusb
progressbar2
csv
hashlib
[:python_version < "3.8"]
importlib-metadata

View file

@ -0,0 +1 @@
precursor_updater

View file

@ -0,0 +1,333 @@
#! /usr/bin/env python3
import argparse
import usb.core
import usb.util
from datetime import datetime
import requests
from precursorusb import PrecursorUsb
from progressbar.bar import ProgressBar
QR_CODE = """
██████████████ ██ ████ ██████ ██ ██████████████
██ ██ ██████ ██ ████ ██ ██ ██
██ ██████ ██ ████████████ ██ ████ ██ ██████ ██
██ ██████ ██ ████ ██ ██ ██ ██ ██████ ██
██ ██████ ██ ████ ████ ██ ██ ██████ ██
██ ██ ██████ ████ ██████ ██ ██
██████████████ ██ ██ ██ ██ ██ ██ ██ ██████████████
██ ██ ████ ██████
██ ██ ████ ██████████ ████ ██ ██ ██ ██
██████ ██ ██ ████ ██████ ██ ████ ████
██ ██ ██████ ██ ████████████████ ████ ████ ██
████ ██ ██ ██ ██ ██ ████ ██
████ ████ ██████ ██████████ ██████ ████ ██
██ ██ ██ ██ ████████████ ██████████ ████
████████ ██ ████ ████ ████ ██ ████ ██
██████ ████ ████ ██████ ██ ██ ██ ████
████████████████████ ████ ██ ████ ████ ██
██ ████ ████ ██████████ ██████
████ ██ ██ ██ ██ ████████ ██████████ ████ ██
██ ██ ██ ██████ ██ ██ ██
████ ████ ████████ ████████ ██ ██████████████ ██
██ ██████ ████ ██████ ██
██████████████ ████████ ██████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██████ ██ ████
██ ██████ ██ ████ ██ ██ ██████████████ ████
██ ██████ ██ ████ ████████ ██████ ██
██ ██████ ██ ████████ ██████ ██████ ██ ████
██ ██ ████ ██ ██ ████████ ██████
██████████████ ████ ██ ████████ ██ ████████ ██
"""
def get_with_progress(url, name='Downloading'):
r = requests.get(url, stream=True)
total_length = int(r.headers.get('content-length'))
ret = bytearray()
progress = ProgressBar(min_value=0, max_value=total_length, prefix=name + ' ').start()
for chunk in r.iter_content(chunk_size=65536):
if chunk:
ret += bytearray(chunk)
progress.update(len(ret))
progress.finish()
return ret
def get_usb_interface(config=False, peek=None, override_csr=None, force=False):
dev = usb.core.find(idProduct=0x5bf0, idVendor=0x1209)
if dev is None:
raise ValueError('Precursor device not found')
dev.set_configuration()
if config:
cfg = dev.get_active_configuration()
print(cfg)
pc_usb = PrecursorUsb(dev)
if peek:
pc_usb.peek(peek, display=True)
exit(0)
pc_usb.load_csrs(override_csr) # prime the CSR values
if "v0.8" in pc_usb.gitrev:
locs = {
"LOC_SOC" : [0x0000_0000, "soc_csr.bin"],
"LOC_STAGING": [0x0028_0000, "pass"],
"LOC_LOADER" : [0x0050_0000, "loader.bin"],
"LOC_KERNEL" : [0x0098_0000, "xous.img"],
"LOC_WF200" : [0x07F8_0000, "pass"],
"LOC_EC" : [0x07FC_E000, "pass"],
"LOC_AUDIO" : [0x0634_0000, "short_8khz.wav"],
"LEN_AUDIO" : [0x01C4_0000, "pass"],
"LOC_PDDB" : [0x0100_0000, "pass"],
}
elif "v0.9" in pc_usb.gitrev:
locs = {
"LOC_SOC" : [0x0000_0000, "soc_csr.bin"],
"LOC_STAGING": [0x0028_0000, "pass"],
"LOC_LOADER" : [0x0050_0000, "loader.bin"],
"LOC_KERNEL" : [0x0098_0000, "xous.img"],
"LOC_WF200" : [0x07F8_0000, "pass"],
"LOC_EC" : [0x07FC_E000, "pass"],
"LOC_AUDIO" : [0x0634_0000, "short_8khz.wav"],
"LEN_AUDIO" : [0x01C4_0000, "pass"],
"LOC_PDDB" : [0x01D8_0000, "pass"],
}
elif force == True:
# try the v0.9 offsets
locs = {
"LOC_SOC" : [0x00000000, "soc_csr.bin"],
"LOC_STAGING": [0x00280000, "pass"],
"LOC_LOADER" : [0x00500000, "loader.bin"],
"LOC_KERNEL" : [0x00980000, "xous.img"],
"LOC_WF200" : [0x07F80000, "pass"],
"LOC_EC" : [0x07FCE000, "pass"],
"LOC_AUDIO" : [0x06340000, "short_8khz.wav"],
"LEN_AUDIO" : [0x01C40000, "pass"],
"LOC_PDDB" : [0x01D80000, "pass"],
}
else:
print("SoC is from an unknow rev '{}', use --force to continue anyways with v0.9 firmware offsets".format(pc_usb.load_csrs()))
exit(1)
return (locs, pc_usb)
def main():
parser = argparse.ArgumentParser(description="Precursor USB Updater v2")
parser.add_argument(
"-b", "--bleeding-edge", required=False, help="Update to bleeding-edge CI build", action='store_true'
)
parser.add_argument(
"-l", "--language", help="Select Xous language [en|ja|zh|en-tts]", required=False, type=str, default="en"
)
parser.add_argument(
"--factory-reset", required=False, help="Delete passwords and do a factory reset", action='store_true'
)
parser.add_argument(
"--paranoid", required=False, help="Do a full-wipe of the PDDB after the factory reset", action='store_true'
)
parser.add_argument(
"--config", required=False, help="Print the Precursor USB descriptor", action='store_true'
)
parser.add_argument(
"--override-csr", required=False, help="CSR file to use instead of CSR values stored with the image. Used to recover in case of a partially corrupted gateware", type=str,
)
parser.add_argument(
"--peek", required=False, help="Inspect an address, then quit. Useful for sanity testing your config.", type=auto_int, metavar=('ADDR')
)
parser.add_argument(
"--force", required=False, help="Ignore gitrev version on SoC and try to burn an image anyways", action="store_true"
)
parser.add_argument(
"--dry-run", required=False, help="Don't actually burn anything, but print what would happen if we did", action="store_true"
)
args = parser.parse_args()
VALID_LANGUAGES = {
"en",
"en-tts",
"ja",
"zh",
}
language = args.language.lower()
if language not in VALID_LANGUAGES:
print("Language selection '{}' is not valid. Please select one of {}".format(language, VALID_LANGUAGES))
exit(1)
# initial check to see if the Precursor device is there
try:
(locs, pc_usb) = get_usb_interface(args.config, args.peek, args.override_csr, args.force)
except ValueError:
print("Precursor device not found. Please check the USB cable and ensure that `usb debug` was run in Shellchat")
exit(1)
# now try to download all the artifacts and check their versions
# this list should visit kernels in order from newest to oldest.
URL_BLEEDING = 'https://ci.betrusted.io/releases/latest/'
URL_STABLE = 'https://ci.betrusted.io/latest-ci/'
print("Phase 1: Download the update")
if args.bleeding_edge:
print("Bleeding edge CI build selected")
url = URL_BLEEDING
else:
print("Latest stable build selected")
url = URL_STABLE
try:
while True:
# first try the stable branch and see if it meets the version requirement
kernel = get_with_progress(url + 'xous-' + language + '.img', 'Kernel')
if int.from_bytes(kernel[:4], 'little') != 1:
print("Downloaded kernel image has unexpected signature version. Aborting.")
exit(1)
kern_len = int.from_bytes(kernel[4:8], 'little') + 0x1000
if len(kernel) != kern_len:
print("Downloaded kernel has the wrong length. Aborting.")
attempt += 1
exit(1)
curver_loc = kern_len - 4 - 4 - 16
curver = SemVer(kernel[curver_loc:curver_loc + 16])
loader = get_with_progress(url + 'loader.bin', 'Loader')
soc_csr = get_with_progress(url + 'soc_csr.bin', 'Gateware')
ec_fw = get_with_progress(url + 'ec_fw.bin', 'Embedded Controller')
wf200 = get_with_progress(url + 'wf200_fw.bin', 'WF200')
print("Downloaded Xous version {}".format(curver.as_str()))
break
except Exception as e:
if False == single_yes_or_no_question("Error: '{}' encountered downloading the update. Retry? ".format(e)):
print("Abort by user request.")
exit(0)
if args.factory_reset:
if False == single_yes_or_no_question("This will permanently erase user data on the device. Proceed? "):
print("Abort by user request.")
exit(0)
worklist = [
['erase', "Disabling boot by erasing loader...", locs['LOC_LOADER'][0], 1024 * 256],
['prog', "Uploading kernel", locs['LOC_KERNEL'][0], kernel],
['prog', "Uploading EC", locs['LOC_EC'][0], ec_fw],
['prog', "Uploading wf200", locs['LOC_WF200'][0], wf200],
['prog', "Overwriting gateware", locs['LOC_SOC'][0], soc_csr],
]
if args.paranoid:
# erase the entire area -- about 10-15 minutes
worklist += [['erase', "Full erase of PDDB", locs['LOC_PDDB'][0], 0x620_0000]]
worklist += [['prog', "Restoring loader", locs['LOC_LOADER'][0], loader]]
else:
# just deletes the page table -- about 10 seconds
worklist += [['erase', "Shallow-delete of PDDB", locs['LOC_PDDB'][0], 1024 * 512]]
worklist += [['prog', "Restoring loader", locs['LOC_LOADER'][0], loader]]
else:
worklist = [
['erase', "Disabling boot by erasing loader...", locs['LOC_LOADER'][0], 1024 * 256],
['prog', "Uploading kernel", locs['LOC_KERNEL'][0], kernel],
['prog', "Uploading EC", locs['LOC_EC'][0], ec_fw],
['prog', "Uploading wf200", locs['LOC_WF200'][0], wf200],
['prog', "Staging gateware", locs['LOC_STAGING'][0], soc_csr],
['prog', "Restoring loader", locs['LOC_LOADER'][0], loader],
]
print("\nPhase 2: Apply the update")
print("Halting CPU for update.")
vexdbg_addr = int(pc_usb.regions['vexriscv_debug'][0], 0)
pc_usb.ping_wdt()
pc_usb.poke(vexdbg_addr, 0x00020000)
for work in worklist:
retry_usb = False
while True:
if retry_usb:
print("Trying to re-aquire Precursor device...")
try:
(locs, pc_usb) = get_usb_interface(args.config, args.peek, args.override_csr, args.force)
except Exception as e:
if False == single_yes_or_no_question("Failed to find Precursor device. Try again? "):
print("Abort by user request!\n\nSystem may not be bootable, but you can retry an update as long as you do not power-off or hard-reset the device.")
exit(0)
vexdbg_addr = int(pc_usb.regions['vexriscv_debug'][0], 0)
pc_usb.ping_wdt()
pc_usb.poke(vexdbg_addr, 0x00020000)
retry_usb = False
try:
print(work[1])
if work[0] == 'erase':
if args.dry_run:
print("DRYRUN: would erase at 0x{:x}, len 0x{:x}".format(work[2], work[3]))
else:
pc_usb.erase_region(work[2], work[3])
break
else:
if args.dry_run:
print("DRYRUN: Would write at 0x{:x}".format(work[2]))
else:
pc_usb.flash_program(work[2], work[3], verify=False)
break
except Exception as e:
print("Error encountered while {}: '{}'".format(work[1], e))
print("Try reseating the USB connection.")
if False == single_yes_or_no_question("Try again? "):
print("Abort by user request!\n\nSystem may not be bootable, but you can retry an update as long as you do not power-off or hard-reset the device.")
exit(0)
retry_usb = True
print("Resuming CPU.")
pc_usb.poke(vexdbg_addr, 0x02000000)
print("Resetting SOC...")
try:
pc_usb.poke(pc_usb.register('reboot_soc_reset'), 0xac, display=False)
except usb.core.USBError:
pass # we expect an error because we reset the SOC and that includes the USB core
print("\nUpdate finished!\n")
print("{}\nVisit the QR code above to help locate the hole, or go to https://ci.betrusted.io/i/reset.jpg.".format(QR_CODE))
print("Reboot by inserting a paperclip in the hole in the lower right hand side, then follow the on-device instructions.")
def auto_int(x):
return int(x, 0)
class SemVer:
def __init__(self, b):
self.maj = int.from_bytes(b[0:2], 'little')
self.min = int.from_bytes(b[2:4], 'little')
self.rev = int.from_bytes(b[4:6], 'little')
self.extra = int.from_bytes(b[6:8], 'little')
self.has_commit = int.from_bytes(b[12:16], 'little')
# note: very old kernel will return a version of 0.0.0
if self.has_commit != 0:
self.commit = int.from_bytes(b[8:12], 'little')
def ord(self): # returns a number that you can use to compare if versions are bigger or smaller than each other
return self.maj << 48 | self.min << 32 | self.rev << 16 | self.extra
def as_str(self):
if self.has_commit == 0:
return "v{}.{}.{}-{}".format(self.maj, self.min, self.rev, self.extra)
else:
return "v{}.{}.{}-{}-g{:x}".format(self.maj, self.min, self.rev, self.extra, self.commit)
def single_yes_or_no_question(question, default_no=False):
choices = ' [y/N]: ' if default_no else ' [Y/n]: '
default_answer = 'n' if default_no else 'y'
reply = str(input(question + choices)).lower().strip() or default_answer
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
else:
return False if default_no else True
if __name__ == "__main__":
main()
exit(0)

View file

@ -0,0 +1,407 @@
import array
import hashlib
import csv
from progressbar.bar import ProgressBar
class PrecursorUsb:
def __init__(self, dev):
self.dev = dev
self.RDSR = 0x05
self.RDSCUR = 0x2B
self.RDID = 0x9F
self.WREN = 0x06
self.WRDI = 0x04
self.SE4B = 0x21
self.BE4B = 0xDC
self.PP4B = 0x12
self.registers = {}
self.regions = {}
self.gitrev = ''
def register(self, name):
return int(self.registers[name], 0)
def peek(self, addr, display=False):
_dummy_s = '\x00'.encode('utf-8')
data = array.array('B', _dummy_s * 4)
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
data_or_wLength=data, timeout=500)
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
if display == True:
print("0x{:08x}".format(read_data))
return read_data
def poke(self, addr, wdata, check=False, display=False):
if check == True:
_dummy_s = '\x00'.encode('utf-8')
data = array.array('B', _dummy_s * 4)
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
data_or_wLength=data, timeout=500)
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
print("before poke: 0x{:08x}".format(read_data))
data = array.array('B', wdata.to_bytes(4, 'little'))
numwritten = self.dev.ctrl_transfer(bmRequestType=(0x00 | 0x43), bRequest=0,
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
data_or_wLength=data, timeout=500)
if check == True:
_dummy_s = '\x00'.encode('utf-8')
data = array.array('B', _dummy_s * 4)
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
wValue=(addr & 0xffff), wIndex=((addr >> 16) & 0xffff),
data_or_wLength=data, timeout=500)
read_data = int.from_bytes(data.tobytes(), byteorder='little', signed=False)
print("after poke: 0x{:08x}".format(read_data))
if display == True:
print("wrote 0x{:08x} to 0x{:08x}".format(wdata, addr))
def burst_read(self, addr, len):
_dummy_s = '\x00'.encode('utf-8')
maxlen = 4096
ret = bytearray()
packet_count = len // maxlen
if (len % maxlen) != 0:
packet_count += 1
for pkt_num in range(packet_count):
cur_addr = addr + pkt_num * maxlen
if pkt_num == packet_count - 1:
if len % maxlen != 0:
bufsize = len % maxlen
else:
bufsize = maxlen
else:
bufsize = maxlen
data = array.array('B', _dummy_s * bufsize)
numread = self.dev.ctrl_transfer(bmRequestType=(0x80 | 0x43), bRequest=0,
wValue=(cur_addr & 0xffff), wIndex=((cur_addr >> 16) & 0xffff),
data_or_wLength=data, timeout=500)
if numread != bufsize:
print("Burst read error: {} bytes requested, {} bytes read at 0x{:08x}".format(bufsize, numread, cur_addr))
exit(1)
ret = ret + data
return ret
def burst_write(self, addr, data):
if len(data) == 0:
return
# the actual "addr" doesn't matter for a burst_write, because it's specified
# as an argument to the flash_pp4b command. We lock out access to the base of
# SPINOR because it's part of the gateware, so, we pick a "safe" address to
# write to instead. The page write responder will aggregate any write data
# to anywhere in the SPINOR address range.
writebuf_addr = 0x2098_0000 # the current start address of the kernel, for example
maxlen = 4096
packet_count = len(data) // maxlen
if (len(data) % maxlen) != 0:
packet_count += 1
for pkt_num in range(packet_count):
cur_addr = addr + pkt_num * maxlen
if pkt_num == packet_count - 1:
if len(data) % maxlen != 0:
bufsize = len(data) % maxlen
else:
bufsize = maxlen
else:
bufsize = maxlen
wdata = array.array('B', data[(pkt_num * maxlen):(pkt_num * maxlen) + bufsize])
numwritten = self.dev.ctrl_transfer(bmRequestType=(0x00 | 0x43), bRequest=0,
# note use of writebuf_addr instead of cur_addr -> see comment above about the quirk of write addressing
wValue=(writebuf_addr & 0xffff), wIndex=((writebuf_addr >> 16) & 0xffff),
data_or_wLength=wdata, timeout=500)
if numwritten != bufsize:
print("Burst write error: {} bytes requested, {} bytes written at 0x{:08x}".format(bufsize, numwritten, cur_addr))
exit(1)
def ping_wdt(self):
self.poke(self.register('wdt_watchdog'), 1, display=False)
self.poke(self.register('wdt_watchdog'), 1, display=False)
def spinor_command_value(self, exec=0, lock_reads=0, cmd_code=0, dummy_cycles=0, data_words=0, has_arg=0):
return ((exec & 1) << 1 |
(lock_reads & 1) << 24 |
(cmd_code & 0xff) << 2 |
(dummy_cycles & 0x1f) << 11 |
(data_words & 0xff) << 16 |
(has_arg & 1) << 10
)
def flash_rdsr(self, lock_reads):
self.poke(self.register('spinor_cmd_arg'), 0)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, lock_reads=lock_reads, cmd_code=self.RDSR, dummy_cycles=4, data_words=1, has_arg=1)
)
return self.peek(self.register('spinor_cmd_rbk_data'), display=False)
def flash_rdscur(self):
self.poke(self.register('spinor_cmd_arg'), 0)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, lock_reads=1, cmd_code=self.RDSCUR, dummy_cycles=4, data_words=1, has_arg=1)
)
return self.peek(self.register('spinor_cmd_rbk_data'), display=False)
def flash_rdid(self, offset):
self.poke(self.register('spinor_cmd_arg'), 0)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, cmd_code=self.RDID, dummy_cycles=4, data_words=offset, has_arg=1)
)
return self.peek(self.register('spinor_cmd_rbk_data'), display=False)
def flash_wren(self):
self.poke(self.register('spinor_cmd_arg'), 0)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, lock_reads=1, cmd_code=self.WREN)
)
def flash_wrdi(self):
self.poke(self.register('spinor_cmd_arg'), 0)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, lock_reads=1, cmd_code=self.WRDI)
)
def flash_se4b(self, sector_address):
self.poke(self.register('spinor_cmd_arg'), sector_address)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, lock_reads=1, cmd_code=self.SE4B, has_arg=1)
)
def flash_be4b(self, block_address):
self.poke(self.register('spinor_cmd_arg'), block_address)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, lock_reads=1, cmd_code=self.BE4B, has_arg=1)
)
def flash_pp4b(self, address, data_bytes):
self.poke(self.register('spinor_cmd_arg'), address)
self.poke(self.register('spinor_command'),
self.spinor_command_value(exec=1, lock_reads=1, cmd_code=self.PP4B, has_arg=1, data_words=(data_bytes//2))
)
def load_csrs(self, fname=None):
LOC_CSRCSV = 0x20277000 # this address shouldn't change because it's how we figure out our version number
# CSR extraction:
# dd if=soc_csr.bin of=csr_data_0.9.6.bin skip=2524 count=32 bs=1024
if fname == None:
csr_data = self.burst_read(LOC_CSRCSV, 0x8000)
else:
with open(fname, "rb") as f:
csr_data = f.read(0x8000)
hasher = hashlib.sha512()
hasher.update(csr_data[:0x7FC0])
digest = hasher.digest()
if digest != csr_data[0x7fc0:]:
print("Could not find a valid csr.csv descriptor on the device, aborting!")
exit(1)
csr_len = int.from_bytes(csr_data[:4], 'little')
csr_extracted = csr_data[4:4+csr_len]
decoded = csr_extracted.decode('utf-8')
# strip comments
stripped = []
for line in decoded.split('\n'):
if line.startswith('#') == False:
stripped.append(line)
# create database
csr_db = csv.reader(stripped)
for row in csr_db:
if len(row) > 1:
if 'csr_register' in row[0]:
self.registers[row[1]] = row[2]
if 'memory_region' in row[0]:
self.regions[row[1]] = [row[2], row[3]]
if 'git_rev' in row[0]:
self.gitrev = row[1]
print("Using SoC {} registers".format(self.gitrev))
def erase_region(self, addr, length):
# ID code check
code = self.flash_rdid(1)
print("ID code bytes 1-2: 0x{:08x}".format(code))
if code != 0x8080c2c2:
print("ID code mismatch")
exit(1)
code = self.flash_rdid(2)
print("ID code bytes 2-3: 0x{:08x}".format(code))
if code != 0x3b3b8080:
print("ID code mismatch")
exit(1)
# block erase
progress = ProgressBar(min_value=0, max_value=length, prefix='Erasing ').start()
erased = 0
while erased < length:
self.ping_wdt()
if (length - erased >= 65536) and ((addr & 0xFFFF) == 0):
blocksize = 65536
else:
blocksize = 4096
while True:
self.flash_wren()
status = self.flash_rdsr(1)
if status & 0x02 != 0:
break
if blocksize == 4096:
self.flash_se4b(addr + erased)
else:
self.flash_be4b(addr + erased)
erased += blocksize
while (self.flash_rdsr(1) & 0x01) != 0:
pass
result = self.flash_rdscur()
if result & 0x60 != 0:
print("E_FAIL/P_FAIL set on erase, programming may fail, but trying anyways...")
if self.flash_rdsr(1) & 0x02 != 0:
self.flash_wrdi()
while (self.flash_rdsr(1) & 0x02) != 0:
pass
if erased < length:
progress.update(erased)
progress.finish()
print("Erase finished")
# addr is relative to the base of FLASH (not absolute)
def flash_program(self, addr, data, verify=True):
flash_region = int(self.regions['spiflash'][0], 0)
flash_len = int(self.regions['spiflash'][1], 0)
if (addr + len(data) > flash_len):
print("Write data out of bounds! Aborting.")
exit(1)
# ID code check
code = self.flash_rdid(1)
print("ID code bytes 1-2: 0x{:08x}".format(code))
if code != 0x8080c2c2:
print("ID code mismatch")
exit(1)
code = self.flash_rdid(2)
print("ID code bytes 2-3: 0x{:08x}".format(code))
if code != 0x3b3b8080:
print("ID code mismatch")
exit(1)
# block erase
progress = ProgressBar(min_value=0, max_value=len(data), prefix='Erasing ').start()
erased = 0
while erased < len(data):
self.ping_wdt()
if (len(data) - erased >= 65536) and ((addr & 0xFFFF) == 0):
blocksize = 65536
else:
blocksize = 4096
while True:
self.flash_wren()
status = self.flash_rdsr(1)
if status & 0x02 != 0:
break
if blocksize == 4096:
self.flash_se4b(addr + erased)
else:
self.flash_be4b(addr + erased)
erased += blocksize
while (self.flash_rdsr(1) & 0x01) != 0:
pass
result = self.flash_rdscur()
if result & 0x60 != 0:
print("E_FAIL/P_FAIL set on erase, programming may fail, but trying anyways...")
if self.flash_rdsr(1) & 0x02 != 0:
self.flash_wrdi()
while (self.flash_rdsr(1) & 0x02) != 0:
pass
if erased < len(data):
progress.update(erased)
progress.finish()
print("Erase finished")
# program
# pad out to the nearest word length
if len(data) % 4 != 0:
data += bytearray([0xff] * (4 - (len(data) % 4)))
written = 0
progress = ProgressBar(min_value=0, max_value=len(data), prefix='Writing ').start()
while written < len(data):
self.ping_wdt()
if len(data) - written > 256:
chunklen = 256
else:
chunklen = len(data) - written
while True:
self.flash_wren()
status = self.flash_rdsr(1)
if status & 0x02 != 0:
break
self.burst_write(self.register('spinor_wdata'), data[written:(written+chunklen)])
self.flash_pp4b(addr + written, chunklen)
written += chunklen
if written < len(data):
progress.update(written)
progress.finish()
print("Write finished")
if self.flash_rdsr(1) & 0x02 != 0:
self.flash_wrdi()
while (self.flash_rdsr(1) & 0x02) != 0:
pass
# dummy reads to clear the "read lock" bit
self.flash_rdsr(0)
# verify
self.ping_wdt()
if verify:
print("Performing readback for verification...")
self.ping_wdt()
rbk_data = self.burst_read(addr + flash_region, len(data))
if rbk_data != data:
errs = 0
err_thresh = 64
for i in range(0, len(rbk_data)):
if rbk_data[i] != data[i]:
if errs < err_thresh:
print("Error at 0x{:x}: {:x}->{:x}".format(i, data[i], rbk_data[i]))
errs += 1
if errs == err_thresh:
print("Too many errors, stopping print...")
print("Errors were found in verification, programming failed")
print("Total byte errors: {}".format(errs))
exit(1)
else:
print("Verification passed.")
else:
print("Skipped verification at user request")
self.ping_wdt()

View file

@ -0,0 +1,15 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "precursor_updater"
version = "0.0.1"
dependencies = [
"requests",
"pyusb",
"progressbar2",
"csv",
"hashlib",
'importlib-metadata; python_version<"3.8"',
]