-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathbin2cbundle.py
More file actions
178 lines (137 loc) · 4.76 KB
/
bin2cbundle.py
File metadata and controls
178 lines (137 loc) · 4.76 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import argparse
import sys
from elftools.elf.elffile import ELFFile
PAGE_SIZE_DEFAULT = 65536 # 64k covers 4k/16k/64k Linux kernels
PAGE_SIZE_WINDOWS = 4096 # x86_64 Windows / WHP uses 4k pages
AARCH64_LOAD_ADDR = '0x80000000'
def write_header(ofile, bundle_name, page_size):
ofile.write('#include <stddef.h>\n')
ofile.write('__attribute__ ((aligned ({}))) char {}_BUNDLE[] = \n"'.format(page_size, bundle_name))
def write_padding(ofile, padding, col):
while padding > 0:
ofile.write('\\x0')
if col == 15:
ofile.write('"\n"')
col = 0
else:
col = col + 1
padding = padding - 1
def write_elf_cbundle(ifile, ofile, page_size) -> int:
elffile = ELFFile(ifile)
entry_addr = elffile['e_entry']
load_segments = [ ]
for segment in elffile.iter_segments():
if segment['p_type'] == 'PT_LOAD':
load_segments.append(segment)
col = 0
total_size = 0
prev_paddr = None
for segment in load_segments:
if prev_paddr == None:
load_addr = segment['p_vaddr'] & 0xfffffff
else:
padding = segment['p_paddr'] - prev_paddr - prev_filesz
write_padding(ofile, padding, col)
total_size = total_size + padding
assert((segment['p_paddr'] - load_addr) == total_size)
for byte in segment.data():
ofile.write('\\x{:x}'.format(byte))
if col == 15:
ofile.write('"\n"')
col = 0
else:
col = col + 1
prev_paddr = segment['p_paddr']
prev_filesz = segment['p_filesz']
total_size = total_size + prev_filesz
rounded_size = int((total_size + page_size - 1) / page_size) * page_size
padding = rounded_size - total_size
write_padding(ofile, padding, col)
return load_addr, entry_addr
def write_raw_cbundle(ifile, ofile, page_size) -> int:
col = 0
total_size = 0
byte = ifile.read(1)
while byte:
ofile.write('\\x{:x}'.format(byte[0]))
if col == 15:
ofile.write('"\n"')
col = 0
else:
col = col + 1
total_size = total_size + 1
byte = ifile.read(1)
rounded_size = int((total_size + page_size - 1) / page_size) * page_size
padding = rounded_size - total_size
write_padding(ofile, padding, col)
def write_footer_generic(ofile, bundle_name):
footer = """
char * krunfw_get_{}(size_t *size)
{{
*size = sizeof({}_BUNDLE) - 1;
return &{}_BUNDLE[0];
}}
"""
ofile.write('";\n')
ofile.write(footer.format(bundle_name.lower(), bundle_name, bundle_name))
def write_footer_kernel(ofile, load_addr, entry_addr):
footer = """
char * krunfw_get_kernel(size_t *load_addr, size_t *entry_addr, size_t *size)
{{
*load_addr = {};
*entry_addr = {};
*size = sizeof(KERNEL_BUNDLE) - 1;
return &KERNEL_BUNDLE[0];
}}
int krunfw_get_version()
{{
return ABI_VERSION;
}}
"""
ofile.write('";\n')
ofile.write(footer.format(load_addr, entry_addr))
def main() -> int:
parser = argparse.ArgumentParser(description='Generate C blob from a binary')
parser.add_argument('input_file', type=str,
help='Input file')
parser.add_argument('output_file', type=str,
help='Output file')
parser.add_argument('-t', type=str, help='Bundle type (vmlinux, Image, qboot, initrd)')
parser.add_argument('--os', type=str, default='Linux',
help='Target OS (Linux, Darwin, Windows)')
args = parser.parse_args()
page_size = PAGE_SIZE_WINDOWS if args.os == 'Windows' else PAGE_SIZE_DEFAULT
bundle_name = None
ifmt = None
if args.t == 'vmlinux':
bundle_name = 'KERNEL'
ifmt = 'elf'
elif args.t == 'Image':
bundle_name = 'KERNEL'
ifmt = 'raw'
elif args.t == 'qboot':
bundle_name = 'QBOOT'
ifmt = 'raw'
elif args.t == 'initrd':
bundle_name = 'INITRD'
ifmt = 'raw'
else:
print('Invalid bundle type')
return -1
ifile = open(args.input_file, 'rb')
ofile = open(args.output_file, 'w')
write_header(ofile, bundle_name, page_size)
if ifmt == 'elf':
load_addr, entry_addr = write_elf_cbundle(ifile, ofile, page_size)
elif ifmt == 'raw':
write_raw_cbundle(ifile, ofile, page_size)
if bundle_name == 'KERNEL':
if ifmt == 'raw':
load_addr = AARCH64_LOAD_ADDR;
entry_addr = AARCH64_LOAD_ADDR;
write_footer_kernel(ofile, load_addr, entry_addr)
else:
write_footer_generic(ofile, bundle_name)
return 0
if __name__ == '__main__':
sys.exit(main())