-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxf2bin.py
190 lines (162 loc) · 6.96 KB
/
axf2bin.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
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
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
# Copyright (c) 2024 nixgnauhcuy
# All rights reserved.
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain
# the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce
# the above copyright notice, this list of conditions
# and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the author nor the names
# of its contributors may be used to endorse
# or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Convert .axf file to .bin file."""
import sys
import os
import getopt
import struct
VERSION = '1.0.1'
USAGE = '''Convert .axf file to .bin file.
Usage:
axf2bin.exe [options] FILE
Options:
-h, --help this help message.
-v, --version version info.
--header output axf file header information.
--program-headers output program headers information.
-o, --output=FILENAME output file name(if option is not specified, use original name by default).
Arguments:
FILE .axf file.
'''
EI_NIDENT = 16
class Elf32_Struct(object):
# @see https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-43405/index.html
def __init__(self, data) -> None:
# ELF Header:
self.e_ident = data[:EI_NIDENT]
(
self.e_type,
self.e_machine,
self.e_version,
self.e_entry,
self.e_phoff,
self.e_shoff,
self.e_flags,
self.e_ehsize,
self.e_phentsize,
self.e_phnum,
self.e_shentsize,
self.e_shnum,
self.e_shstrndx
) = struct.unpack('<HHIIIIIHHHHHH', data[EI_NIDENT:EI_NIDENT+struct.calcsize('<HHIIIIIHHHHHH')])
# ELF Program Headers:
(
self.p_type,
self.p_flags,
self.p_offset,
self.p_vaddr,
self.p_paddr,
self.p_filesz,
self.p_memsz,
self.p_align
) = struct.unpack('<IIIIIIII', data[self.e_phoff:self.e_phoff+struct.calcsize('<IIIIIIII')])
def Elf32_Header_Print(self):
print("ELF Header:")
print("Magic: ", " ".join(f"{byte:02x}" for byte in self.e_ident))
print("Class: ", self.e_type)
print("Machine: ", self.e_machine)
print("Version: ", self.e_version)
print("Entry point address: ", hex(self.e_entry))
print("Program header table's file offset: ", self.e_phoff, " (bytes into file)")
print("Section header table's file offset: ", self.e_shoff, " (bytes into file)")
print("Flags: ", hex(self.e_flags))
print("Header size: ", self.e_ehsize, " (bytes)")
print("Program header table size: ", self.e_phentsize, " (bytes)")
print("Number of program header entries: ", self.e_phnum)
print("Section header's size : ", self.e_shentsize, " (bytes)")
print("Number of section header entries: ", self.e_shnum)
print("Section header table index: ", self.e_shstrndx)
def Elf32_Program_Header_Print(self):
print("Program Header:")
print("Type: ", self.p_type)
print("Flags: ", hex(self.p_flags))
print("Offset from the beginning of the file: ", hex(self.p_offset))
print("Virtual address: ", hex(self.p_vaddr))
print("Segment's physical address: ", hex(self.p_paddr))
print("Number of bytes in the file image: ", hex(self.p_filesz))
print("Number of bytes in the memory image: ", hex(self.p_memsz))
print("Alignment: ", hex(self.p_align))
def is_valid_axf_file(filepath):
if not os.path.exists(filepath) or not filepath.endswith('.axf'):
return False
return True
def main(args=None):
output_file = ""
print_header_flag = False
print_program_headers_flag = False
if args is None:
args = sys.argv[1:]
try:
opts, args = getopt.gnu_getopt(args, 'hvo:',
['help', 'version', 'header', 'program-headers', 'output='])
for o, a in opts:
if o in ('-h', '--help'):
print(USAGE)
return 0
elif o in ('-v', '--version'):
print(VERSION)
return 0
elif o in ('--header'):
print_header_flag = True
elif o in ('--program-headers'):
print_program_headers_flag = True
elif o in ('-o', '--output'):
output_file = a
axf_file = args[0]
if not is_valid_axf_file(axf_file):
raise ValueError("Input file is not a valid .axf file.")
with open(axf_file, 'rb') as input_file:
input_data = input_file.read()
elf_object = Elf32_Struct(input_data)
if print_header_flag:
elf_object.Elf32_Header_Print()
return 0
if print_program_headers_flag:
elf_object.Elf32_Program_Header_Print()
return 0
if not output_file:
output_file = os.path.splitext(axf_file)[0] + '.bin'
if os.path.exists(output_file):
os.remove(output_file)
output_data = input_data[elf_object.e_ehsize:elf_object.p_paddr+elf_object.e_ehsize]
with open(output_file, 'wb') as output:
output.write(output_data)
except Exception as e:
e = sys.exc_info()[1] # current exception
sys.stderr.write(str(e)+"\n\n")
sys.stderr.write(USAGE+"\n")
return 1
return 0
if __name__ == '__main__':
sys.exit(main())