Skip to content

Commit 2b4d27e

Browse files
committed
Fix axf2bin bug, add interface
1 parent 59d6860 commit 2b4d27e

File tree

2 files changed

+101
-19
lines changed

2 files changed

+101
-19
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ axf2bin.exe [options] FILE
9292
```
9393
-h, --help this help message.
9494
-v, --version version info.
95+
--header output axf file header information.
96+
--program-headers output program headers information.
9597
-o, --output=FILENAME output file name(if option is not specified, use original name by default).
9698
```
9799

@@ -105,6 +107,8 @@ axf2bin.exe [options] FILE
105107

106108
``` shell
107109
axf2bin.exe input.axf
110+
axf2bin.exe --header input.axf
111+
axf2bin.exe --program-headers input.axf
108112
axf2bin.exe -o output.bin input.axf
109113
```
110114

Diff for: axf2bin.py

+97-19
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
import sys
3939
import os
4040
import getopt
41+
import struct
4142

42-
VERSION = '1.0.0'
43+
VERSION = '1.0.1'
4344

4445
USAGE = '''Convert .axf file to .bin file.
4546
@@ -49,14 +50,85 @@
4950
Options:
5051
-h, --help this help message.
5152
-v, --version version info.
53+
--header output axf file header information.
54+
--program-headers output program headers information.
5255
-o, --output=FILENAME output file name(if option is not specified, use original name by default).
5356
5457
Arguments:
5558
FILE .axf file.
5659
'''
5760

58-
AXF_HEAD_SIZE = 0x34
59-
BIN_END_MARK = b'\x01\x21\x00\x2F\x0F\x00\x00\x02\x21\x00\x00\x00\x03\x01\x01\x01'
61+
EI_NIDENT = 16
62+
class Elf32_Struct(object):
63+
64+
# @see https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-43405/index.html
65+
def __init__(self, data) -> None:
66+
# ELF Header:
67+
self.e_ident = data[:EI_NIDENT]
68+
(
69+
self.e_type,
70+
self.e_machine,
71+
self.e_version,
72+
self.e_entry,
73+
self.e_phoff,
74+
self.e_shoff,
75+
self.e_flags,
76+
self.e_ehsize,
77+
self.e_phentsize,
78+
self.e_phnum,
79+
self.e_shentsize,
80+
self.e_shnum,
81+
self.e_shstrndx
82+
) = struct.unpack('<HHIIIIIHHHHHH', data[EI_NIDENT:EI_NIDENT+struct.calcsize('<HHIIIIIHHHHHH')])
83+
84+
# ELF Program Headers:
85+
(
86+
self.p_type,
87+
self.p_flags,
88+
self.p_offset,
89+
self.p_vaddr,
90+
self.p_paddr,
91+
self.p_filesz,
92+
self.p_memsz,
93+
self.p_align
94+
) = struct.unpack('<IIIIIIII', data[self.e_phoff:self.e_phoff+struct.calcsize('<IIIIIIII')])
95+
96+
print("p_type: ", self.p_type)
97+
print("p_flags: ", self.p_flags)
98+
print("p_offset: ", self.p_offset)
99+
print("p_vaddr: ", self.p_vaddr)
100+
print("p_paddr: ", self.p_paddr)
101+
print("p_filesz: ", self.p_filesz)
102+
print("p_memsz: ", self.p_memsz)
103+
print("p_align: ", self.p_align)
104+
105+
def Elf32_Header_Print(self):
106+
print("ELF Header:")
107+
print("Magic: ", " ".join(f"{byte:02x}" for byte in self.e_ident))
108+
print("Class: ", self.e_type)
109+
print("Machine: ", self.e_machine)
110+
print("Version: ", self.e_version)
111+
print("Entry point address: ", hex(self.e_entry))
112+
print("Program header table's file offset: ", self.e_phoff, " (bytes into file)")
113+
print("Section header table's file offset: ", self.e_shoff, " (bytes into file)")
114+
print("Flags: ", hex(self.e_flags))
115+
print("Header size: ", self.e_ehsize, " (bytes)")
116+
print("Program header table size: ", self.e_phentsize, " (bytes)")
117+
print("Number of program header entries: ", self.e_phnum)
118+
print("Section header's size : ", self.e_shentsize, " (bytes)")
119+
print("Number of section header entries: ", self.e_shnum)
120+
print("Section header table index: ", self.e_shstrndx)
121+
122+
def Elf32_Program_Header_Print(self):
123+
print("Program Header:")
124+
print("Type: ", self.p_type)
125+
print("Flags: ", hex(self.p_flags))
126+
print("Offset from the beginning of the file: ", hex(self.p_offset))
127+
print("Virtual address: ", hex(self.p_vaddr))
128+
print("Segment's physical address: ", hex(self.p_paddr))
129+
print("Number of bytes in the file image: ", hex(self.p_filesz))
130+
print("Number of bytes in the memory image: ", hex(self.p_memsz))
131+
print("Alignment: ", hex(self.p_align))
60132

61133
def is_valid_axf_file(filepath):
62134
if not os.path.exists(filepath) or not filepath.endswith('.axf'):
@@ -66,48 +138,54 @@ def is_valid_axf_file(filepath):
66138
def main(args=None):
67139

68140
output_file = ""
141+
print_header_flag = False
142+
print_program_headers_flag = False
69143

70144
if args is None:
71145
args = sys.argv[1:]
72146
try:
73147
opts, args = getopt.gnu_getopt(args, 'hvo:',
74-
['help', 'version', 'output='])
148+
['help', 'version', 'header', 'program-headers', 'output='])
75149

150+
76151
for o, a in opts:
77152
if o in ('-h', '--help'):
78153
print(USAGE)
79154
return 0
80155
elif o in ('-v', '--version'):
81156
print(VERSION)
82157
return 0
158+
elif o in ('--header'):
159+
print_header_flag = True
160+
elif o in ('--program-headers'):
161+
print_program_headers_flag = True
83162
elif o in ('-o', '--output'):
84163
output_file = a
85-
except getopt.GetoptError:
86-
e = sys.exc_info()[1] # current exception
87-
sys.stderr.write(str(e)+"\n\n")
88-
sys.stderr.write(USAGE+"\n")
89-
return 1
90164

91-
try:
92165
axf_file = args[0]
93166
if not is_valid_axf_file(axf_file):
94167
raise ValueError("Input file is not a valid .axf file.")
168+
169+
with open(axf_file, 'rb') as input_file:
170+
input_data = input_file.read()
171+
172+
elf_object = Elf32_Struct(input_data)
173+
174+
if print_header_flag:
175+
elf_object.Elf32_Header_Print()
176+
return 0
177+
178+
if print_program_headers_flag:
179+
elf_object.Elf32_Program_Header_Print()
180+
return 0
95181

96182
if not output_file:
97183
output_file = os.path.splitext(axf_file)[0] + '.bin'
98184

99185
if os.path.exists(output_file):
100186
os.remove(output_file)
101187

102-
with open(axf_file, 'rb') as input_file:
103-
input_data = input_file.read()
104-
105-
start_index = AXF_HEAD_SIZE
106-
end_index = input_data.find(BIN_END_MARK, AXF_HEAD_SIZE)
107-
if end_index == -1:
108-
raise ValueError("Convert data failed, can't find BIN_END_MARK.")
109-
110-
output_data = input_data[start_index:end_index]
188+
output_data = input_data[elf_object.e_ehsize:elf_object.p_paddr+elf_object.e_ehsize]
111189
with open(output_file, 'wb') as output:
112190
output.write(output_data)
113191
except Exception as e:

0 commit comments

Comments
 (0)