-
Notifications
You must be signed in to change notification settings - Fork 5
/
patch.py
44 lines (36 loc) · 1.33 KB
/
patch.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
import os
import struct
import subprocess
import sys
elf = sys.argv[1]
result = subprocess.run([os.environ["DEVKITARM"] + r'/bin/arm-none-eabi-objdump', '--section-headers', elf], stdout=subprocess.PIPE)
lines = str(result.stdout).split('\\n')
sectionsInfo = [line.split()[1:6] for line in lines if line.split() and line.split()[0].isdigit()]
sections = ((int(sec[2],16), int(sec[4],16), int(sec[1],16)) for sec in sectionsInfo if int(sec[2],16) != 0)
off = lambda vaddr: struct.pack(">I",vaddr - 0x100000)[1:]
sz = lambda size: struct.pack(">H", size)
ips = b'PATCH'
with open(elf, 'rb') as e:
for vaddr, offset, size in sections:
if vaddr >= 0x4CA000 and vaddr < 0x5C7000: #this is not good lol just trying to avoid __GNU_EH_FRAME_HDR section
continue
e.seek(offset, 0)
while size > 65535:
patch = e.read(65535)
print('{:0x}'.format(vaddr))
ips += off(vaddr)
ips += sz(65535)
ips += patch
vaddr += 65535
offset += 65535
size -= 65535
patch = e.read(size)
if len(patch) != 0:
print('{:0x}'.format(vaddr))
ips += off(vaddr)
ips += sz(size)
ips += patch
ips += b'EOF'
with open("code.ips", 'wb') as patchFile:
patchFile.write(ips)
print("created ips")