Parent directory

uf2conv.py

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