-
Notifications
You must be signed in to change notification settings - Fork 6
/
exefs2elf.py
executable file
·59 lines (47 loc) · 1.68 KB
/
exefs2elf.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
# convert exefs to elf
import sys
import os
import struct
CC = "arm-none-eabi-gcc"
CP = "arm-none-eabi-g++"
OC = "arm-none-eabi-objcopy"
LD = "arm-none-eabi-ld"
def run(cmd):
os.system(cmd)
def writefile(path, s):
with open(path, "wb") as f:
f.write(str(s))
with open("workdir/exh.bin", "rb") as f:
exh = f.read(64)
(textBase, textPages, roPages, rwPages, bssSize) = struct.unpack('16x ii 12x i 12x i 4x i', exh)
textSize = textPages * 0x1000
roSize = roPages * 0x1000
rwSize = rwPages * 0x1000
bssSize = (int(bssSize / 0x1000) + 1) * 0x1000
print("textBase: {:08x}".format(textBase))
print("textSize: {:08x}".format(textSize))
print("roSize: {:08x}".format(roSize))
print("rwSize: {:08x}".format(rwSize))
print("bssSize: {:08x}".format(bssSize))
if (textBase != 0x100000):
print('WARNING: textBase mismatch, might be an encrypted exheader file.')
exefsPath = 'workdir/exefs/'
with open(exefsPath + 'code.bin', "rb") as f:
text = f.read(textSize)
ro = f.read(roSize)
rw = f.read(rwSize)
with open('e2elf.ld', 'r') as f:
ldscript = f.read()
ldscript = ldscript.replace('%memorigin%', str(textBase))
ldscript = ldscript.replace('%bsssize%', str(bssSize))
writefile('workdir/e2elf.ld', ldscript)
writefile(exefsPath + 'text.bin', text)
writefile(exefsPath + 'ro.bin', ro)
writefile(exefsPath + 'rw.bin', rw)
objfiles = ''
for i in (('text', 'text'), ('ro', 'rodata'), ('rw', 'data')):
desc, sec_name = i
run('{0} -I binary -O elf32-littlearm --rename-section .data=.{1} {2}{3}.bin {2}{3}.o'
.format(OC, sec_name, exefsPath, desc))
objfiles += '{0}{1}.o '.format(exefsPath, desc)
run(LD + ' --accept-unknown-input-arch -T workdir/e2elf.ld -o workdir/exefs.elf ' + objfiles)