forked from hannahherbig/arcin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhidloader_append.py
executable file
·53 lines (38 loc) · 1005 Bytes
/
hidloader_append.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
#!/usr/bin/env python
# Usage: hidloader_append.py arcin.elf hidloader_v2.exe arcin_flash_foo.exe
import sys, struct, shutil
from elftools.elf.elffile import ELFFile
e = ELFFile(open(sys.argv[1], 'rb'))
shutil.copyfile(sys.argv[2], sys.argv[3])
shutil.copymode(sys.argv[2], sys.argv[3])
f = open(sys.argv[3], 'ab')
buf = ''
for segment in sorted(e.iter_segments(), key = lambda x: x.header.p_paddr):
if segment.header.p_type != 'PT_LOAD':
continue
data = segment.data()
lma = segment.header.p_paddr
# Workaround for LD aligning segments to a larger boundary than 8k.
if lma == 0x8000000:
lma += 0x2000
data = data[0x2000:]
# Add padding if necessary.
buf += '\0' * (lma - 0x8002000 - len(buf))
buf += data
# Align to 64B
if len(buf) & (64 - 1):
buf += '\0' * (64 - (len(buf) & (64 - 1)))
fsize = f.tell()
f.write(struct.pack('<IHHHHHHI',
0xb007f00d,
1,
0x1d50,
0x6080,
0x1d50,
0x6084,
0x0110,
len(buf),
))
f.write(buf)
f.write(struct.pack('<I', fsize))
f.close()