-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuf2_to_bin.py
More file actions
48 lines (37 loc) · 1.3 KB
/
uf2_to_bin.py
File metadata and controls
48 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# uf2_to_bin.py - Convert UF2 to BIN format
import sys
import struct
def uf2_to_bin(uf2_file, bin_file):
with open(uf2_file, 'rb') as f:
uf2_data = f.read()
# UF2 block structure
blocks = []
pos = 0
while pos < len(uf2_data):
block = uf2_data[pos:pos+476]
if len(block) < 476:
break
# Parse UF2 header
magic1 = struct.unpack('<I', block[0:4])[0]
if magic1 != 0x0A324655: # "UF2\n"
pos += 476
continue
target_addr = struct.unpack('<I', block[12:16])[0]
payload_size = struct.unpack('<I', block[16:20])[0]
payload = block[20:20+payload_size]
blocks.append((target_addr, payload))
pos += 476
# Determine address range
min_addr = min(b[0] for b in blocks)
max_addr = max(b[0] + len(b[1]) for b in blocks)
# Create binary
binary = bytearray(max_addr - min_addr)
for addr, data in blocks:
offset = addr - min_addr
binary[offset:offset+len(data)] = data
with open(bin_file, 'wb') as f:
f.write(binary)
print(f"Converted {uf2_file} to {bin_file}")
print(f"Size: {len(binary)} bytes (0x{len(binary):X})")
if __name__ == "__main__":
uf2_to_bin("firmware.uf2", "firmware.bin")