Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) Microsoft Corporation |
| 3 | # SPDX-License-Identifier: MIT |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 4 | # Copied from 27e322f of https://github.com/microsoft/uf2/blob/master/utils/uf2conv.py |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 5 | # pylint: skip-file |
| 6 | import sys |
| 7 | import struct |
| 8 | import subprocess |
| 9 | import re |
| 10 | import os |
| 11 | import os.path |
| 12 | import argparse |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 13 | import json |
| 14 | from time import sleep |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 15 | |
| 16 | |
| 17 | UF2_MAGIC_START0 = 0x0A324655 # "UF2\n" |
| 18 | UF2_MAGIC_START1 = 0x9E5D5157 # Randomly selected |
| 19 | UF2_MAGIC_END = 0x0AB16F30 # Ditto |
| 20 | |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 21 | INFO_FILE = "/INFO_UF2.TXT" |
| 22 | |
| 23 | appstartaddr = 0x2000 |
| 24 | familyid = 0x0 |
| 25 | |
| 26 | |
| 27 | def is_uf2(buf): |
| 28 | w = struct.unpack("<II", buf[0:8]) |
| 29 | return w[0] == UF2_MAGIC_START0 and w[1] == UF2_MAGIC_START1 |
| 30 | |
| 31 | def is_hex(buf): |
| 32 | try: |
| 33 | w = buf[0:30].decode("utf-8") |
| 34 | except UnicodeDecodeError: |
| 35 | return False |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 36 | if w[0] == ':' and re.match(rb"^[:0-9a-fA-F\r\n]+$", buf): |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 37 | return True |
| 38 | return False |
| 39 | |
| 40 | def convert_from_uf2(buf): |
| 41 | global appstartaddr |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 42 | global familyid |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 43 | numblocks = len(buf) // 512 |
| 44 | curraddr = None |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 45 | currfamilyid = None |
| 46 | families_found = {} |
| 47 | prev_flag = None |
| 48 | all_flags_same = True |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 49 | outp = [] |
| 50 | for blockno in range(numblocks): |
| 51 | ptr = blockno * 512 |
| 52 | block = buf[ptr:ptr + 512] |
| 53 | hd = struct.unpack(b"<IIIIIIII", block[0:32]) |
| 54 | if hd[0] != UF2_MAGIC_START0 or hd[1] != UF2_MAGIC_START1: |
| 55 | print("Skipping block at " + ptr + "; bad magic") |
| 56 | continue |
| 57 | if hd[2] & 1: |
| 58 | # NO-flash flag set; skip block |
| 59 | continue |
| 60 | datalen = hd[4] |
| 61 | if datalen > 476: |
| 62 | assert False, "Invalid UF2 data size at " + ptr |
| 63 | newaddr = hd[3] |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 64 | if (hd[2] & 0x2000) and (currfamilyid == None): |
| 65 | currfamilyid = hd[7] |
| 66 | if curraddr == None or ((hd[2] & 0x2000) and hd[7] != currfamilyid): |
| 67 | currfamilyid = hd[7] |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 68 | curraddr = newaddr |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 69 | if familyid == 0x0 or familyid == hd[7]: |
| 70 | appstartaddr = newaddr |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 71 | padding = newaddr - curraddr |
| 72 | if padding < 0: |
| 73 | assert False, "Block out of order at " + ptr |
| 74 | if padding > 10*1024*1024: |
| 75 | assert False, "More than 10M of padding needed at " + ptr |
| 76 | if padding % 4 != 0: |
| 77 | assert False, "Non-word padding size at " + ptr |
| 78 | while padding > 0: |
| 79 | padding -= 4 |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 80 | outp.append(b"\x00\x00\x00\x00") |
| 81 | if familyid == 0x0 or ((hd[2] & 0x2000) and familyid == hd[7]): |
| 82 | outp.append(block[32 : 32 + datalen]) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 83 | curraddr = newaddr + datalen |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 84 | if hd[2] & 0x2000: |
| 85 | if hd[7] in families_found.keys(): |
| 86 | if families_found[hd[7]] > newaddr: |
| 87 | families_found[hd[7]] = newaddr |
| 88 | else: |
| 89 | families_found[hd[7]] = newaddr |
| 90 | if prev_flag == None: |
| 91 | prev_flag = hd[2] |
| 92 | if prev_flag != hd[2]: |
| 93 | all_flags_same = False |
| 94 | if blockno == (numblocks - 1): |
| 95 | print("--- UF2 File Header Info ---") |
| 96 | families = load_families() |
| 97 | for family_hex in families_found.keys(): |
| 98 | family_short_name = "" |
| 99 | for name, value in families.items(): |
| 100 | if value == family_hex: |
| 101 | family_short_name = name |
| 102 | print("Family ID is {:s}, hex value is 0x{:08x}".format(family_short_name,family_hex)) |
| 103 | print("Target Address is 0x{:08x}".format(families_found[family_hex])) |
| 104 | if all_flags_same: |
| 105 | print("All block flag values consistent, 0x{:04x}".format(hd[2])) |
| 106 | else: |
| 107 | print("Flags were not all the same") |
| 108 | print("----------------------------") |
| 109 | if len(families_found) > 1 and familyid == 0x0: |
| 110 | outp = [] |
| 111 | appstartaddr = 0x0 |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 112 | return b"".join(outp) |
| 113 | |
| 114 | def convert_to_carray(file_content): |
| 115 | outp = "const unsigned long bindata_len = %d;\n" % len(file_content) |
| 116 | outp += "const unsigned char bindata[] __attribute__((aligned(16))) = {" |
| 117 | for i in range(len(file_content)): |
| 118 | if i % 16 == 0: |
| 119 | outp += "\n" |
| 120 | outp += "0x%02x, " % file_content[i] |
| 121 | outp += "\n};\n" |
| 122 | return bytes(outp, "utf-8") |
| 123 | |
| 124 | def convert_to_uf2(file_content): |
| 125 | global familyid |
| 126 | datapadding = b"" |
| 127 | while len(datapadding) < 512 - 256 - 32 - 4: |
| 128 | datapadding += b"\x00\x00\x00\x00" |
| 129 | numblocks = (len(file_content) + 255) // 256 |
| 130 | outp = [] |
| 131 | for blockno in range(numblocks): |
| 132 | ptr = 256 * blockno |
| 133 | chunk = file_content[ptr:ptr + 256] |
| 134 | flags = 0x0 |
| 135 | if familyid: |
| 136 | flags |= 0x2000 |
| 137 | hd = struct.pack(b"<IIIIIIII", |
| 138 | UF2_MAGIC_START0, UF2_MAGIC_START1, |
| 139 | flags, ptr + appstartaddr, 256, blockno, numblocks, familyid) |
| 140 | while len(chunk) < 256: |
| 141 | chunk += b"\x00" |
| 142 | block = hd + chunk + datapadding + struct.pack(b"<I", UF2_MAGIC_END) |
| 143 | assert len(block) == 512 |
| 144 | outp.append(block) |
| 145 | return b"".join(outp) |
| 146 | |
| 147 | class Block: |
| 148 | def __init__(self, addr): |
| 149 | self.addr = addr |
| 150 | self.bytes = bytearray(256) |
| 151 | |
| 152 | def encode(self, blockno, numblocks): |
| 153 | global familyid |
| 154 | flags = 0x0 |
| 155 | if familyid: |
| 156 | flags |= 0x2000 |
| 157 | hd = struct.pack("<IIIIIIII", |
| 158 | UF2_MAGIC_START0, UF2_MAGIC_START1, |
| 159 | flags, self.addr, 256, blockno, numblocks, familyid) |
| 160 | hd += self.bytes[0:256] |
| 161 | while len(hd) < 512 - 4: |
| 162 | hd += b"\x00" |
| 163 | hd += struct.pack("<I", UF2_MAGIC_END) |
| 164 | return hd |
| 165 | |
| 166 | def convert_from_hex_to_uf2(buf): |
| 167 | global appstartaddr |
| 168 | appstartaddr = None |
| 169 | upper = 0 |
| 170 | currblock = None |
| 171 | blocks = [] |
| 172 | for line in buf.split('\n'): |
| 173 | if line[0] != ":": |
| 174 | continue |
| 175 | i = 1 |
| 176 | rec = [] |
| 177 | while i < len(line) - 1: |
| 178 | rec.append(int(line[i:i+2], 16)) |
| 179 | i += 2 |
| 180 | tp = rec[3] |
| 181 | if tp == 4: |
| 182 | upper = ((rec[4] << 8) | rec[5]) << 16 |
| 183 | elif tp == 2: |
| 184 | upper = ((rec[4] << 8) | rec[5]) << 4 |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 185 | elif tp == 1: |
| 186 | break |
| 187 | elif tp == 0: |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 188 | addr = upper + ((rec[1] << 8) | rec[2]) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 189 | if appstartaddr == None: |
| 190 | appstartaddr = addr |
| 191 | i = 4 |
| 192 | while i < len(rec) - 1: |
| 193 | if not currblock or currblock.addr & ~0xff != addr & ~0xff: |
| 194 | currblock = Block(addr & ~0xff) |
| 195 | blocks.append(currblock) |
| 196 | currblock.bytes[addr & 0xff] = rec[i] |
| 197 | addr += 1 |
| 198 | i += 1 |
| 199 | numblocks = len(blocks) |
| 200 | resfile = b"" |
| 201 | for i in range(0, numblocks): |
| 202 | resfile += blocks[i].encode(i, numblocks) |
| 203 | return resfile |
| 204 | |
| 205 | def to_str(b): |
| 206 | return b.decode("utf-8") |
| 207 | |
| 208 | def get_drives(): |
| 209 | drives = [] |
| 210 | if sys.platform == "win32": |
| 211 | r = subprocess.check_output(["wmic", "PATH", "Win32_LogicalDisk", |
| 212 | "get", "DeviceID,", "VolumeName,", |
| 213 | "FileSystem,", "DriveType"]) |
| 214 | for line in to_str(r).split('\n'): |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 215 | words = re.split(r'\s+', line) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 216 | if len(words) >= 3 and words[1] == "2" and words[2] == "FAT": |
| 217 | drives.append(words[0]) |
| 218 | else: |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 219 | searchpaths = ["/media"] |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 220 | if sys.platform == "darwin": |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 221 | searchpaths = ["/Volumes"] |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 222 | elif sys.platform == "linux": |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 223 | searchpaths += ["/media/" + os.environ["USER"], '/run/media/' + os.environ["USER"]] |
| 224 | |
| 225 | for rootpath in searchpaths: |
| 226 | if os.path.isdir(rootpath): |
| 227 | for d in os.listdir(rootpath): |
| 228 | if os.path.isdir(rootpath): |
| 229 | drives.append(os.path.join(rootpath, d)) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 230 | |
| 231 | |
| 232 | def has_info(d): |
| 233 | try: |
| 234 | return os.path.isfile(d + INFO_FILE) |
| 235 | except: |
| 236 | return False |
| 237 | |
| 238 | return list(filter(has_info, drives)) |
| 239 | |
| 240 | |
| 241 | def board_id(path): |
| 242 | with open(path + INFO_FILE, mode='r') as file: |
| 243 | file_content = file.read() |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 244 | return re.search(r"Board-ID: ([^\r\n]*)", file_content).group(1) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 245 | |
| 246 | |
| 247 | def list_drives(): |
| 248 | for d in get_drives(): |
| 249 | print(d, board_id(d)) |
| 250 | |
| 251 | |
| 252 | def write_file(name, buf): |
| 253 | with open(name, "wb") as f: |
| 254 | f.write(buf) |
| 255 | print("Wrote %d bytes to %s" % (len(buf), name)) |
| 256 | |
| 257 | |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 258 | def load_families(): |
| 259 | # The expectation is that the `uf2families.json` file is in the same |
| 260 | # directory as this script. Make a path that works using `__file__` |
| 261 | # which contains the full path to this script. |
| 262 | filename = "uf2families.json" |
| 263 | pathname = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename) |
| 264 | with open(pathname) as f: |
| 265 | raw_families = json.load(f) |
| 266 | |
| 267 | families = {} |
| 268 | for family in raw_families: |
| 269 | families[family["short_name"]] = int(family["id"], 0) |
| 270 | |
| 271 | return families |
| 272 | |
| 273 | |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 274 | def main(): |
| 275 | global appstartaddr, familyid |
| 276 | def error(msg): |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 277 | print(msg, file=sys.stderr) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 278 | sys.exit(1) |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 279 | parser = argparse.ArgumentParser(description='Convert to UF2 or flash directly.') |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 280 | parser.add_argument('input', metavar='INPUT', type=str, nargs='?', |
| 281 | help='input file (HEX, BIN or UF2)') |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 282 | parser.add_argument('-b', '--base', dest='base', type=str, |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 283 | default="0x2000", |
| 284 | help='set base address of application for BIN format (default: 0x2000)') |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 285 | parser.add_argument('-f', '--family', dest='family', type=str, |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 286 | default="0x0", |
| 287 | help='specify familyID - number or name (default: 0x0)') |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 288 | parser.add_argument('-o', '--output', metavar="FILE", dest='output', type=str, |
| 289 | help='write output to named file; defaults to "flash.uf2" or "flash.bin" where sensible') |
| 290 | parser.add_argument('-d', '--device', dest="device_path", |
| 291 | help='select a device path to flash') |
| 292 | parser.add_argument('-l', '--list', action='store_true', |
| 293 | help='list connected devices') |
| 294 | parser.add_argument('-c', '--convert', action='store_true', |
| 295 | help='do not flash, just convert') |
| 296 | parser.add_argument('-D', '--deploy', action='store_true', |
| 297 | help='just flash, do not convert') |
| 298 | parser.add_argument('-w', '--wait', action='store_true', |
| 299 | help='wait for device to flash') |
| 300 | parser.add_argument('-C', '--carray', action='store_true', |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 301 | help='convert binary file to a C array, not UF2') |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 302 | parser.add_argument('-i', '--info', action='store_true', |
| 303 | help='display header information from UF2, do not convert') |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 304 | args = parser.parse_args() |
| 305 | appstartaddr = int(args.base, 0) |
| 306 | |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 307 | families = load_families() |
| 308 | |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 309 | if args.family.upper() in families: |
| 310 | familyid = families[args.family.upper()] |
| 311 | else: |
| 312 | try: |
| 313 | familyid = int(args.family, 0) |
| 314 | except ValueError: |
| 315 | error("Family ID needs to be a number or one of: " + ", ".join(families.keys())) |
| 316 | |
| 317 | if args.list: |
| 318 | list_drives() |
| 319 | else: |
| 320 | if not args.input: |
| 321 | error("Need input file") |
| 322 | with open(args.input, mode='rb') as f: |
| 323 | inpbuf = f.read() |
| 324 | from_uf2 = is_uf2(inpbuf) |
| 325 | ext = "uf2" |
| 326 | if args.deploy: |
| 327 | outbuf = inpbuf |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 328 | elif from_uf2 and not args.info: |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 329 | outbuf = convert_from_uf2(inpbuf) |
| 330 | ext = "bin" |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 331 | elif from_uf2 and args.info: |
| 332 | outbuf = "" |
| 333 | convert_from_uf2(inpbuf) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 334 | elif is_hex(inpbuf): |
| 335 | outbuf = convert_from_hex_to_uf2(inpbuf.decode("utf-8")) |
| 336 | elif args.carray: |
| 337 | outbuf = convert_to_carray(inpbuf) |
| 338 | ext = "h" |
| 339 | else: |
| 340 | outbuf = convert_to_uf2(inpbuf) |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 341 | if not args.deploy and not args.info: |
| 342 | print("Converted to %s, output size: %d, start address: 0x%x" % |
| 343 | (ext, len(outbuf), appstartaddr)) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 344 | if args.convert or ext != "uf2": |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 345 | if args.output == None: |
| 346 | args.output = "flash." + ext |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 347 | if args.output: |
| 348 | write_file(args.output, outbuf) |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 349 | if ext == "uf2" and not args.convert and not args.info: |
| 350 | drives = get_drives() |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 351 | if len(drives) == 0: |
Peter Johanson | b064a2c | 2023-12-01 09:51:08 -0800 | [diff] [blame] | 352 | if args.wait: |
| 353 | print("Waiting for drive to deploy...") |
| 354 | while len(drives) == 0: |
| 355 | sleep(0.1) |
| 356 | drives = get_drives() |
| 357 | elif not args.output: |
| 358 | error("No drive to deploy.") |
| 359 | for d in drives: |
| 360 | print("Flashing %s (%s)" % (d, board_id(d))) |
| 361 | write_file(d + "/NEW.UF2", outbuf) |
Pete Johanson | 310a464 | 2020-12-31 16:51:52 -0500 | [diff] [blame] | 362 | |
| 363 | |
| 364 | if __name__ == "__main__": |
| 365 | main() |