| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# This file is part of the MicroPython project, http://micropython.org/ |
| 4 |
# |
| 5 |
# The MIT License (MIT) |
| 6 |
# |
| 7 |
# Copyright (c) 2020 Jim Mussared |
| 8 |
# |
| 9 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
# of this software and associated documentation files (the "Software"), to deal |
| 11 |
# in the Software without restriction, including without limitation the rights |
| 12 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
# copies of the Software, and to permit persons to whom the Software is |
| 14 |
# furnished to do so, subject to the following conditions: |
| 15 |
# |
| 16 |
# The above copyright notice and this permission notice shall be included in |
| 17 |
# all copies or substantial portions of the Software. |
| 18 |
# |
| 19 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 |
# THE SOFTWARE. |
| 26 |
|
| 27 |
# This script obfuscates the ST wireless binaries so they can be safely copied |
| 28 |
# to the flash filesystem and not be accidentally discovered by the FUS during |
| 29 |
# an update. See more information (and the corresponding de-obfuscation) in |
| 30 |
# rfcore_firmware.py as well as instructions on how to use. |
| 31 |
|
| 32 |
import os |
| 33 |
import re |
| 34 |
import struct |
| 35 |
import sys |
| 36 |
from binascii import crc32 |
| 37 |
from rfcore_firmware import validate_crc, _OBFUSCATION_KEY |
| 38 |
|
| 39 |
|
| 40 |
_FIRMWARE_FILES = { |
| 41 |
"stm32wb5x_FUS_fw_1_0_2.bin": "fus_102.bin", |
| 42 |
"stm32wb5x_FUS_fw.bin": "fus_110.bin", |
| 43 |
"stm32wb5x_BLE_HCILayer_fw.bin": "ws_ble_hci.bin", |
| 44 |
} |
| 45 |
_RELEASE_NOTES = "Release_Notes.html" |
| 46 |
|
| 47 |
|
| 48 |
def get_details(release_notes, filename): |
| 49 |
if not release_notes: |
| 50 |
return None |
| 51 |
file_details = re.findall( |
| 52 |
rb"%s,(((0x[\d\S]+?),)+[vV][\d\.]+)[<,]" % filename.encode(), |
| 53 |
release_notes, |
| 54 |
flags=re.DOTALL, |
| 55 |
) |
| 56 |
# The release note has all past version details also, but current is at top |
| 57 |
latest_details = file_details[0][0].split(b",") |
| 58 |
addr_1m, addr_640k, addr_512k, addr_256k, version = latest_details |
| 59 |
addr_1m = int(addr_1m, 0) |
| 60 |
addr_640k = int(addr_640k, 0) |
| 61 |
addr_512k = int(addr_512k, 0) |
| 62 |
addr_256k = int(addr_256k, 0) |
| 63 |
version = [int(v) for v in version.lower().lstrip(b"v").split(b".")] |
| 64 |
return addr_1m, addr_640k, addr_512k, addr_256k, version |
| 65 |
|
| 66 |
|
| 67 |
def main(src_path, dest_path): |
| 68 |
# Load the release note to parse for important details |
| 69 |
with open(os.path.join(src_path, _RELEASE_NOTES), "rb") as f: |
| 70 |
release_notes = f.read() |
| 71 |
# remove some formatting |
| 72 |
release_notes = re.sub(rb"</?strong>", b"", release_notes) |
| 73 |
# flatten tables |
| 74 |
release_notes = re.sub( |
| 75 |
rb"</t[dh]>\W*\n*\W*", |
| 76 |
b",", |
| 77 |
release_notes.replace(b"<td>", b"").replace(b"<th>", b""), |
| 78 |
) |
| 79 |
if ( |
| 80 |
b"Wireless Coprocessor Binary,STM32WB5xxG(1M),STM32WB5xxY(640k),STM32WB5xxE(512K),STM32WB5xxC(256K),Version," |
| 81 |
not in release_notes |
| 82 |
): |
| 83 |
raise SystemExit( |
| 84 |
"Cannot determine binary load address, please confirm Coprocessor folder / Release Notes format." |
| 85 |
) |
| 86 |
|
| 87 |
for src_filename, dest_file in _FIRMWARE_FILES.items(): |
| 88 |
src_file = os.path.join(src_path, src_filename) |
| 89 |
dest_file = os.path.join(dest_path, dest_file) |
| 90 |
if not os.path.exists(src_file): |
| 91 |
print("Unable to find: {}".format(src_file)) |
| 92 |
continue |
| 93 |
sz = 0 |
| 94 |
with open(src_file, "rb") as src: |
| 95 |
crc = 0 |
| 96 |
with open(dest_file, "wb") as dest: |
| 97 |
while True: |
| 98 |
b = src.read(4) |
| 99 |
if not b: |
| 100 |
break |
| 101 |
(v,) = struct.unpack("<I", b) |
| 102 |
v ^= _OBFUSCATION_KEY |
| 103 |
vs = struct.pack("<I", v) |
| 104 |
dest.write(vs) |
| 105 |
crc = crc32(vs, crc) |
| 106 |
sz += 4 |
| 107 |
|
| 108 |
addr_1m, addr_640k, addr_512k, addr_256k, version = get_details( |
| 109 |
release_notes, src_filename |
| 110 |
) |
| 111 |
|
| 112 |
footer = struct.pack( |
| 113 |
"<37sIIIIbbbI", |
| 114 |
src_filename.encode(), |
| 115 |
addr_1m, |
| 116 |
addr_640k, |
| 117 |
addr_512k, |
| 118 |
addr_256k, |
| 119 |
*version, |
| 120 |
_OBFUSCATION_KEY, |
| 121 |
) |
| 122 |
assert len(footer) == 60 |
| 123 |
dest.write(footer) |
| 124 |
crc = crc32(footer, crc) |
| 125 |
crc = 0xFFFFFFFF & -crc - 1 |
| 126 |
dest.write(struct.pack("<I", crc)) |
| 127 |
sz += 64 |
| 128 |
|
| 129 |
print( |
| 130 |
f"Written {src_filename} v{version[0]}.{version[1]}.{version[2]} to {dest_file} ({sz} bytes)" |
| 131 |
) |
| 132 |
check_file_details(dest_file) |
| 133 |
|
| 134 |
|
| 135 |
def check_file_details(filename): |
| 136 |
"""Should match copy of function in rfcore_firmware.py to confirm operation""" |
| 137 |
with open(filename, "rb") as f: |
| 138 |
if not validate_crc(f): |
| 139 |
raise ValueError("file validation failed: incorrect crc") |
| 140 |
|
| 141 |
f.seek(-64, 2) |
| 142 |
footer = f.read() |
| 143 |
assert len(footer) == 64 |
| 144 |
details = struct.unpack("<37sIIIIbbbII", footer) |
| 145 |
( |
| 146 |
src_filename, |
| 147 |
addr_1m, |
| 148 |
addr_640k, |
| 149 |
addr_512k, |
| 150 |
addr_256k, |
| 151 |
vers_major, |
| 152 |
vers_minor, |
| 153 |
vers_patch, |
| 154 |
KEY, |
| 155 |
crc, |
| 156 |
) = details |
| 157 |
if KEY != _OBFUSCATION_KEY: |
| 158 |
raise ValueError("file validation failed: incorrect key") |
| 159 |
|
| 160 |
return ( |
| 161 |
src_filename, |
| 162 |
addr_1m, |
| 163 |
addr_640k, |
| 164 |
addr_512k, |
| 165 |
addr_256k, |
| 166 |
(vers_major, vers_minor, vers_patch), |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
if __name__ == "__main__": |
| 171 |
if len(sys.argv) != 3: |
| 172 |
print("Usage: {} src_path dest_path".format(sys.argv[0])) |
| 173 |
print() |
| 174 |
print( |
| 175 |
'"src_path" should be the location of the ST binaries from https://github.com/STMicroelectronics/STM32CubeWB/tree/master/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x' |
| 176 |
) |
| 177 |
print( |
| 178 |
'"dest_path" will be where fus_102.bin, fus_110.bin, and ws_ble_hci.bin will be written to.' |
| 179 |
) |
| 180 |
sys.exit(1) |
| 181 |
|
| 182 |
main(sys.argv[1], sys.argv[2]) |