forked from CLF78/HideNSeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.py
117 lines (96 loc) · 3.51 KB
/
make.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
import os
from shutil import rmtree
from subprocess import call
from elftools.elf.elffile import ELFFile as elf
# Locate various things
asm = 'powerpc-eabi-as'
gcc = 'powerpc-eabi-gcc'
objcopy = 'powerpc-eabi-objcopy'
# Initialize variables
startHook = 0x8000629C
debug = False
regionlist = ['P', 'E', 'J', 'K']
def build(isBootStrap):
# Initialize lists
asmlist = []
cpplist = []
if isBootStrap:
mainpath = 'bootstrap'
outname = 'Loader'
print('Building bootstrap...')
else:
mainpath = 'src'
outname = 'HideNSeek'
print('Building payload...')
# Get all files in the source folder
for root, subfolder, files in os.walk(mainpath):
for item in files:
if item.lower().endswith('.s'):
filename = os.path.join(root, item)
asmlist.append(filename)
elif item.lower().endswith('.c'):
filename = os.path.join(root, item)
# A hack because i'm lazy
if item == 'main.c':
cpplist.insert(0, filename)
else:
cpplist.append(filename)
for region in regionlist:
# Make a clean build folder
if os.path.isdir('build'):
rmtree('build')
os.mkdir('build')
# FUCK DEVKIT
with open('asm_setup.S', 'r+') as f:
f.seek(0, 2)
f.seek(f.tell()-4, 0)
f.write(region)
# Compile the asm
for file in asmlist:
c = call([asm, '-mregnames', '-m750cl', file, '-o', os.path.join('build', os.path.basename(file)[:-2] + '.o')])
if c != 0:
print('Build failed!')
return
file = file.replace('.S', '.o')
# Initialize GCC command
cc_command = [gcc, '-Iinclude', '-nostartfiles', '-nostdinc', '-D', 'REGION_{}'.format(region), '-Os', '-Wl,-T,{}/mem.ld,-T,rmc.ld,-T,rmc{}.ld'.format(mainpath, region.lower()), '-ffunction-sections', '-fdata-sections', '-mcpu=750', '-meabi', '-mhard-float']
# Add debug macro if debug is on
if debug:
cc_command += ['-D', 'DEBUG']
# Add all cpp files and the destination
cc_command += cpplist
cc_command += asmlist
cc_command += ['-o', 'build/{}{}.o'.format(outname, region)]
# Call GCC and Objcopy
c = call(cc_command)
if c != 0:
print('Build failed!')
return
# Get offset to start function
if isBootStrap:
with open('build/{}{}.o'.format(outname,region), 'rb') as f:
elfData = elf(f)
symtab = elfData.get_section_by_name('.symtab')
startFunc = symtab.get_symbol_by_name('start')[0].entry['st_value']
instruction = (((startFunc-startHook) & 0x3FFFFFF ) | 0x48000000)
print('New instruction is', hex(instruction))
c = call([objcopy, '-O', 'binary', '-R', '.eh_frame', '-R', '.eh_frame_hdr', 'build/{}{}.o'.format(outname, region), 'bin/{}{}.bin'.format(outname, region)])
if c != 0:
print('Build failed!')
return
else:
print('Built', region + '!')
# We're done!
rmtree('build')
print('All built!')
def main():
# Debug prompt
debug = input('Enable debug mode? (Y/N): ').lower() == 'y'
# Make a clean bin folder
if os.path.isdir('bin'):
rmtree('bin')
os.mkdir('bin')
# Build it!
build(False)
build(True)
main()