38
38
import sys
39
39
import os
40
40
import getopt
41
+ import struct
41
42
42
- VERSION = '1.0.0 '
43
+ VERSION = '1.0.1 '
43
44
44
45
USAGE = '''Convert .axf file to .bin file.
45
46
49
50
Options:
50
51
-h, --help this help message.
51
52
-v, --version version info.
53
+ --header output axf file header information.
54
+ --program-headers output program headers information.
52
55
-o, --output=FILENAME output file name(if option is not specified, use original name by default).
53
56
54
57
Arguments:
55
58
FILE .axf file.
56
59
'''
57
60
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 ))
60
132
61
133
def is_valid_axf_file (filepath ):
62
134
if not os .path .exists (filepath ) or not filepath .endswith ('.axf' ):
@@ -66,48 +138,54 @@ def is_valid_axf_file(filepath):
66
138
def main (args = None ):
67
139
68
140
output_file = ""
141
+ print_header_flag = False
142
+ print_program_headers_flag = False
69
143
70
144
if args is None :
71
145
args = sys .argv [1 :]
72
146
try :
73
147
opts , args = getopt .gnu_getopt (args , 'hvo:' ,
74
- ['help' , 'version' , 'output=' ])
148
+ ['help' , 'version' , 'header' , 'program-headers' , ' output=' ])
75
149
150
+
76
151
for o , a in opts :
77
152
if o in ('-h' , '--help' ):
78
153
print (USAGE )
79
154
return 0
80
155
elif o in ('-v' , '--version' ):
81
156
print (VERSION )
82
157
return 0
158
+ elif o in ('--header' ):
159
+ print_header_flag = True
160
+ elif o in ('--program-headers' ):
161
+ print_program_headers_flag = True
83
162
elif o in ('-o' , '--output' ):
84
163
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
90
164
91
- try :
92
165
axf_file = args [0 ]
93
166
if not is_valid_axf_file (axf_file ):
94
167
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
95
181
96
182
if not output_file :
97
183
output_file = os .path .splitext (axf_file )[0 ] + '.bin'
98
184
99
185
if os .path .exists (output_file ):
100
186
os .remove (output_file )
101
187
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 ]
111
189
with open (output_file , 'wb' ) as output :
112
190
output .write (output_data )
113
191
except Exception as e :
0 commit comments