forked from Relys/patchrom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exefs2elf.py
68 lines (53 loc) · 1.75 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
60
61
62
63
64
65
66
67
68
# convert exefs to elf
import sys
import os
import glob
import struct
CC = "arm-none-eabi-gcc"
CP = "arm-none-eabi-g++"
OC = "arm-none-eabi-objcopy"
LD = "arm-none-eabi-ld"
def allFile(pattern):
s = "";
for file in glob.glob(pattern):
s += file + " ";
return s;
def run(cmd):
os.system(cmd)
def writefile(path, s):
with open(path, "wb") as f:
f.write(s);
with open("workdir/exh.bin", "rb") as f:
exh = f.read(64);
(textBase, textPages, roPages, rwPages, bssSize) =struct.unpack(
'16xii4x4x4xi4x4x4xi4xi', exh);
textSize = textPages * 0x1000;
roSize = roPages * 0x1000;
rwSize = rwPages * 0x1000;
bssSize = ( (bssSize / 0x1000) + 1 ) * 0x1000;
print("textBase: %08x\ntextSize: %08x\nroSize: %08x\nrwSize: %08x\nbssSize: %08x\n" % (textBase, textSize, roSize, rwSize, bssSize))
bssSize += 0x4000; # reserve stack region for our payload
if (textBase != 0x100000):
print('textBase mismatch, might be an encrypted exheader file.');
exit(0);
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('%bsssize%', str(bssSize));
with open('workdir/e2elf.ld', 'wb') as f:
f.write(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')):
a, b = i;
run(OC + ' -I binary -O elf32-littlearm --rename-section .data=.' + b + ' '
+ exefsPath + a + '.bin ' + exefsPath + a + '.o');
objfiles += exefsPath + a + '.o' + ' ';
print objfiles;
run (LD + ' --accept-unknown-input-arch -T workdir/e2elf.ld -o workdir/exefs.elf ' + objfiles);