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