25
25
26
26
# Constant Variables
27
27
RAM2_RSVD = 0x3131373835393138
28
+ RAM3_RSVD = 0xFFFFFFFFFFFFFFFF
29
+ IMG2_OFFSET = 0x10006000 #default
28
30
29
31
def write_fixed_width_string (value , width , output ):
30
32
# cut string to list & reverse
@@ -44,22 +46,39 @@ def write_fixed_width_value(value, width, output):
44
46
output .write ("" .join ([chr (long (b , 16 )) for b in line ]))
45
47
46
48
def append_image_file (image , output ):
47
- input = open (image , "rb" )
48
- output .write (input .read ())
49
+ try :
50
+ input = open (image , "rb" )
51
+ output .write (input .read ())
52
+ except Exception :
53
+ return
49
54
input .close ()
50
55
51
56
def prepend (image , image_prepend , toolchain , info ):
57
+ if info ['size' ] == 0 :
58
+ return
52
59
output = open (image_prepend , "wb" )
53
60
write_fixed_width_value (info ['size' ], 8 , output )
54
61
write_fixed_width_value (info ['addr' ], 8 , output )
55
- write_fixed_width_value (RAM2_RSVD , 16 , output )
56
- with open (image , "rb" ) as input :
57
- if toolchain == "IAR" :
58
- input .seek (info ['addr' ])
59
- output .write (input .read (info ['size' ]))
62
+ if info ['img' ] == 2 :
63
+ write_fixed_width_value (RAM2_RSVD , 16 , output )
64
+ elif info ['img' ] == 3 :
65
+ write_fixed_width_value (RAM3_RSVD , 16 , output )
66
+ if os .path .isfile (image ):
67
+ with open (image , "rb" ) as input :
68
+ if toolchain == "IAR" :
69
+ input .seek (info ['addr' ])
70
+ elif info ['img' ] == 3 : #toolchain is not IAR
71
+ input .seek (info ['addr' ]- IMG2_OFFSET )
72
+ output .write (input .read (info ['size' ]))
73
+ else :
74
+ image = os .path .join (image , info ['name' ])
75
+ with open (image , "rb" ) as input :
76
+ output .write (input .read (info ['size' ]))
77
+
60
78
output .close ()
79
+
61
80
62
- def parse_section (toolchain , elf , section ):
81
+ def _parse_section (toolchain , elf , section ):
63
82
info = {'addr' :None , 'size' :0 };
64
83
if toolchain not in ["GCC_ARM" , "ARM_STD" , "ARM" , "ARM_MICRO" , "IAR" ]:
65
84
print "[ERROR] unsupported toolchain " + toolchain
@@ -104,38 +123,60 @@ def parse_section(toolchain, elf, section):
104
123
print "[ERROR] cannot find the address of section " + section
105
124
return info
106
125
126
+ def parse_section (toolchain , elf , sections , img ):
127
+ img_info = {'name' :"" , 'addr' :None , 'size' :0 , 'img' :img }
128
+ for section in sections :
129
+ section_info = _parse_section (toolchain , elf , section )
130
+ if img_info ['addr' ] is None or img_info ['addr' ] > section_info ['addr' ]:
131
+ img_info ['addr' ] = section_info ['addr' ]
132
+ img_info ['name' ] = section
133
+ img_info ['size' ] = img_info ['size' ] + section_info ['size' ]
134
+ return img_info
135
+
107
136
# ----------------------------
108
137
# main function
109
138
# ----------------------------
110
139
def rtl8195a_elf2bin (toolchain , image_elf , image_bin ):
111
140
if toolchain == "GCC_ARM" :
112
141
img2_sections = [".image2.table" , ".text" , ".data" ]
142
+ img3_sections = [".sdr_all" ]
113
143
elif toolchain in ["ARM_STD" , "ARM" , "ARM_MICRO" ]:
114
144
img2_sections = [".image2.table" , ".text" , ".data" ]
145
+ img3_sections = ["_DRAM_CODE" ]
115
146
elif toolchain == "IAR" :
116
- # actually it's block
117
147
img2_sections = ["IMAGE2" ]
148
+ img3_sections = ["SDRAM" ]
118
149
else :
119
150
print ("[error] unsupported toolchain" ) + toolchain
120
151
return
121
- ram2_info = {'addr' :None , 'size' :0 }
152
+ image2_info = {'addr' :None , 'size' :0 , 'img' :2 }
153
+ image3_info = {'addr' :None , 'size' :0 , 'img' :3 }
122
154
image_name = os .path .splitext (image_elf )[0 ]
123
155
124
- ram1_prepend_bin = os .path .join (TOOLS_BOOTLOADERS , "REALTEK_RTL8195AM" , "ram_1_prepend.bin" )
125
- ram2_prepend_bin = image_name + '-ram_2_prepend.bin'
156
+ img1_prepend_bin = os .path .join (TOOLS_BOOTLOADERS , "REALTEK_RTL8195AM" , "ram_1_prepend.bin" )
157
+ img2_prepend_bin = image_name + '-ram_2_prepend.bin'
158
+ img3_prepend_bin = image_name + '-ram_3_prepend.bin'
159
+
160
+ old_bin = image_name + '.bin'
161
+
162
+ img_info = parse_section (toolchain , image_elf , img2_sections , 2 )
163
+ prepend (old_bin , img2_prepend_bin , toolchain , img_info )
164
+ img_info = parse_section (toolchain , image_elf , img3_sections , 3 )
165
+ prepend (old_bin , img3_prepend_bin , toolchain , img_info )
126
166
127
- old_bin = image_name + '.bin'
128
- for section in img2_sections :
129
- section_info = parse_section ( toolchain , image_elf , section )
130
- if ram2_info [ 'addr' ] is None or ram2_info [ 'addr' ] > section_info [ 'addr' ] :
131
- ram2_info [ 'addr' ] = section_info [ 'addr' ]
132
- ram2_info [ 'size' ] = ram2_info [ 'size' ] + section_info [ 'size' ]
133
-
134
- prepend ( old_bin , ram2_prepend_bin , toolchain , ram2_info )
167
+ #delete original binary
168
+ if os . path . isfile ( image_bin ) :
169
+ os . remove ( image_bin )
170
+ else :
171
+ for i in os . listdir ( image_bin ):
172
+ os . remove ( os . path . join ( image_bin , i ))
173
+ os . removedirs ( image_bin )
174
+
135
175
# write output file
136
176
output = open (image_bin , "wb" )
137
- append_image_file (ram1_prepend_bin , output )
138
- append_image_file (ram2_prepend_bin , output )
177
+ append_image_file (img1_prepend_bin , output )
178
+ append_image_file (img2_prepend_bin , output )
179
+ append_image_file (img3_prepend_bin , output )
139
180
output .close ()
140
181
# post built done
141
182
0 commit comments