-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
process_ipl.py
executable file
·164 lines (129 loc) · 3.65 KB
/
process_ipl.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
import math
import struct
import sys
# bootrom descrambler reversed by segher
def scramble(data, *, qoobsx=False):
acc = 0
nacc = 0
t = 0x2953
u = 0xD9C2
v = 0x3FF1
x = 1
it = 0
while it < len(data):
t0 = t & 1
t1 = (t >> 1) & 1
u0 = u & 1
u1 = (u >> 1) & 1
v0 = v & 1
x ^= t1 ^ v0
x ^= u0 | u1
x ^= (t0 ^ u1 ^ v0) & (t0 ^ u0)
if t0 == u0:
v >>= 1
if v0:
v ^= 0xB3D0
if t0 == 0:
u >>= 1
if u0:
u ^= 0xFB10
t >>= 1
if t0:
t ^= 0xA740
nacc = (nacc + 1) % 256
acc = (acc * 2 + x) % 256
if nacc == 8:
data[it] ^= acc
nacc = 0
it += 1
return data
def flatten_dol(data):
header = struct.unpack(">64I", data[:256])
offsets = header[:18]
addresses = header[18:36]
sizes = header[36:54]
bss_address = header[54]
bss_size = header[55]
entry = header[56]
dol_min = min(a for a in addresses if a)
dol_max = max(a + s for a, s in zip(addresses, sizes))
img = bytearray(dol_max - dol_min)
for offset, address, size in zip(offsets, addresses, sizes):
img[address - dol_min:address + size - dol_min] = data[offset:offset + size]
# Entry point, load address, memory image
return entry, dol_min, img
def pack_uf2(data, base_address):
ret = bytearray()
seq = 0
addr = base_address
chunk_size = 256
total_chunks = math.ceil(len(data) / chunk_size)
while data:
chunk = data[:chunk_size]
data = data[chunk_size:]
ret += struct.pack(
"< 8I 476B I",
0x0A324655, # Magic 1 "UF2\n"
0x9E5D5157, # Magic 2
0x00002000, # Flags (family ID present)
addr,
chunk_size,
seq,
total_chunks,
0xE48BFF56, # Board family: Raspberry Pi RP2040
*chunk.ljust(476, b"\x00"),
0x0AB16F30, # Final magic
)
seq += 1
addr += chunk_size
return ret
def main():
if len(sys.argv) not in range(3, 4 + 1):
print(f"Usage: {sys.argv[0]} <output> <executable>")
return 1
output = sys.argv[1]
executable = sys.argv[2]
with open(executable, "rb") as f:
exe = bytearray(f.read())
if executable.endswith(".dol"):
entry, load, img = flatten_dol(exe)
entry &= 0x017FFFFF
entry |= 0x80000000
load &= 0x017FFFFF
size = len(img)
print(f"Entry point: 0x{entry:0{8}X}")
print(f"Load address: 0x{load:0{8}X}")
print(f"Image size: {size} bytes ({size // 1024}K)")
elif executable.endswith(".elf"):
pass
else:
print("Unknown input format")
return -1
if entry != 0x81300000 or load != 0x01300000:
print("Invalid entry point and base address (must be 0x81300000)")
return -1
img = scramble(bytearray(0x720) + img)[0x720:]
align_size = 4
img = (
img.ljust(math.ceil(len(img) / align_size) * align_size, b"\x00")
+ b"PICO"
)
header_size = 32
header = struct.pack(
"> 8B I 20x",
*b"IPLBOOT ",
len(img) + header_size,
)
assert len(header) == header_size
if output.endswith(".bin"):
out = header + img
elif output.endswith(".uf2"):
out = pack_uf2(header + img, 0x10080000)
else:
print("Unknown output format")
return -1
with open(output, "wb") as f:
f.write(out)
if __name__ == "__main__":
sys.exit(main())