-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_appbin.py
executable file
·301 lines (263 loc) · 9.45 KB
/
gen_appbin.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/python2
#
# Integrated into espQToggle and modified to allow adding extra payload to the user bin files.
#
# File: gen_appbin.py
# This file is part of Espressif's generate bin script.
#
# ESPRESSIF MIT License
#
# Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
#
# Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
# it is free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
import string
import sys
import os
import re
import binascii
import struct
import zlib
TEXT_ADDRESS = 0x40100000
# app_entry = 0
# data_address = 0x3ffb0000
# data_end = 0x40000000
# text_end = 0x40120000
CHECKSUM_INIT = 0xEF
chk_sum = CHECKSUM_INIT
blocks = 0
def write_file(file_name,data,at=None):
if file_name is None:
print('file_name cannot be none\n')
sys.exit(0)
fp = open(file_name,'ab')
size = fp.tell()
if at is not None:
zero = chr(0) * (at - size)
fp.write(zero)
fp.write(data)
fp.close()
def combine_bin(file_name,dest_file_name,start_offset_addr,need_chk):
global chk_sum
global blocks
if dest_file_name is None:
print('dest_file_name cannot be none\n')
sys.exit(0)
if file_name:
fp = open(file_name,'rb')
if fp:
########## write text ##########
fp.seek(0,os.SEEK_END)
data_len = fp.tell()
if data_len:
if need_chk:
tmp_len = (data_len + 3) & (~3)
else:
tmp_len = (data_len + 15) & (~15)
data_bin = struct.pack('<II',start_offset_addr,tmp_len)
write_file(dest_file_name,data_bin)
fp.seek(0,os.SEEK_SET)
data_bin = fp.read(data_len)
write_file(dest_file_name,data_bin)
if need_chk:
for loop in range(len(data_bin)):
chk_sum ^= ord(data_bin[loop])
# print('%s size is %d(0x%x),align 4 bytes,\nultimate size is %d(0x%x)'%(file_name,data_len,data_len,tmp_len,tmp_len))
tmp_len = tmp_len - data_len
if tmp_len:
data_str = ['00']*(tmp_len)
data_bin = binascii.a2b_hex(''.join(data_str))
write_file(dest_file_name,data_bin)
if need_chk:
for loop in range(len(data_bin)):
chk_sum ^= ord(data_bin[loop])
blocks = blocks + 1
fp.close()
else:
print('!!!Open %s fail!!!'%(file_name))
def getFileCRC(_path):
try:
blocksize = 1024 * 64
f = open(_path,"rb")
str = f.read(blocksize)
crc = 0
while(len(str) != 0):
crc = binascii.crc32(str, crc)
str = f.read(blocksize)
f.close()
except:
print('get file crc error!')
return 0
return crc
def gen_appbin():
global chk_sum
global crc_sum
global blocks
if len(sys.argv) < 7:
print('Usage: gen_appbin.py eagle.app.out boot_mode flash_mode flash_clk_div flash_size_map user_bin [extra.bin]')
sys.exit(0)
elf_file = sys.argv[1]
boot_mode = sys.argv[2]
flash_mode = sys.argv[3]
flash_clk_div = sys.argv[4]
flash_size_map = sys.argv[5]
user_bin = sys.argv[6]
if len(sys.argv) > 7:
extra_bin = sys.argv[7]
else:
extra_bin = None
flash_data_line = 16
data_line_bits = 0xf
irom0text_bin_name = 'eagle.app.v6.irom0text.bin'
text_bin_name = 'eagle.app.v6.text.bin'
data_bin_name = 'eagle.app.v6.data.bin'
rodata_bin_name = 'eagle.app.v6.rodata.bin'
flash_bin_name ='eagle.app.flash.bin'
BIN_MAGIC_FLASH = 0xE9
BIN_MAGIC_IROM = 0xEA
data_str = ''
sum_size = 0
cmd = 'xtensa-lx106-elf-nm -g ' + elf_file + ' > eagle.app.sym'
os.system(cmd)
fp = open('./eagle.app.sym')
if fp is None:
print("open sym file error\n")
sys.exit(0)
lines = fp.readlines()
fp.close()
entry_addr = None
p = re.compile('(\w*)(\sT\s)(call_user_start)$')
for line in lines:
m = p.search(line)
if m != None:
entry_addr = m.group(1)
# print(entry_addr)
if entry_addr is None:
print('no entry point!!')
sys.exit(0)
data_start_addr = '0'
p = re.compile('(\w*)(\sA\s)(_data_start)$')
for line in lines:
m = p.search(line)
if m != None:
data_start_addr = m.group(1)
# print(data_start_addr)
rodata_start_addr = '0'
p = re.compile('(\w*)(\sA\s)(_rodata_start)$')
for line in lines:
m = p.search(line)
if m != None:
rodata_start_addr = m.group(1)
# print(rodata_start_addr)
# write flash bin header
#============================
# SPI FLASH PARAMS
#-------------------
#flash_mode=
# 0: QIO
# 1: QOUT
# 2: DIO
# 3: DOUT
#-------------------
#flash_clk_div=
# 0 : 80m / 2
# 1 : 80m / 3
# 2 : 80m / 4
# 0xf: 80m / 1
#-------------------
#flash_size_map=
# 0 : 512 KB (256 KB + 256 KB)
# 1 : 256 KB
# 2 : 1024 KB (512 KB + 512 KB)
# 3 : 2048 KB (512 KB + 512 KB)
# 4 : 4096 KB (512 KB + 512 KB)
# 5 : 2048 KB (1024 KB + 1024 KB)
# 6 : 4096 KB (1024 KB + 1024 KB)
#-------------------
# END OF SPI FLASH PARAMS
#============================
byte2=int(flash_mode)&0xff
byte3=(((int(flash_size_map)<<4)| int(flash_clk_div))&0xff)
app=int(user_bin)&0xff
if boot_mode == '2':
# write irom bin head
#data_bin = struct.pack('<BBBBI',BIN_MAGIC_IROM,4,byte2,byte3,int(entry_addr,16))
data_bin = struct.pack('<BBBBI',BIN_MAGIC_IROM,4,0,app,int(entry_addr,16))
sum_size = len(data_bin)
write_file(flash_bin_name,data_bin)
# irom0.text.bin
combine_bin(irom0text_bin_name,flash_bin_name,0x0,0)
if boot_mode == '1':
data_bin = struct.pack('<BBBBI',BIN_MAGIC_FLASH,3,0,app,int(entry_addr,16))
else:
data_bin = struct.pack('<BBBBI',BIN_MAGIC_FLASH,3,byte2,byte3,int(entry_addr,16))
sum_size = len(data_bin)
write_file(flash_bin_name,data_bin)
# text.bin
combine_bin(text_bin_name,flash_bin_name,TEXT_ADDRESS,1)
# data.bin
if data_start_addr:
combine_bin(data_bin_name,flash_bin_name,int(data_start_addr,16),1)
# rodata.bin
combine_bin(rodata_bin_name,flash_bin_name,int(rodata_start_addr,16),1)
# write checksum header
sum_size = os.path.getsize(flash_bin_name) + 1
sum_size = flash_data_line - (data_line_bits&sum_size)
if sum_size:
data_str = ['00']*(sum_size)
data_bin = binascii.a2b_hex(''.join(data_str))
write_file(flash_bin_name,data_bin)
write_file(flash_bin_name,chr(chk_sum & 0xFF))
flash_image_size = os.stat(flash_bin_name).st_size
# extra.bin
if extra_bin:
with open(extra_bin, 'rb') as fp:
data_bin = fp.read()
size_packed = struct.pack('<I', len(data_bin))
write_file(flash_bin_name,size_packed,at=476*1024)
write_file(flash_bin_name,data_bin,at=476*1024 + 4)
if boot_mode == '1':
sum_size = os.path.getsize(flash_bin_name)
data_str = ['FF']*(0x10000-sum_size)
data_bin = binascii.a2b_hex(''.join(data_str))
write_file(flash_bin_name,data_bin)
fp = open(irom0text_bin_name,'rb')
if fp:
data_bin = fp.read()
write_file(flash_bin_name,data_bin)
fp.close()
else :
print('!!!Open %s fail!!!'%(flash_bin_name))
sys.exit(0)
if boot_mode == '1' or boot_mode == '2':
all_bin_crc = getFileCRC(flash_bin_name)
if all_bin_crc < 0:
all_bin_crc = abs(all_bin_crc) - 1
else :
all_bin_crc = abs(all_bin_crc) + 1
write_file(flash_bin_name,chr((all_bin_crc & 0x000000FF))+chr((all_bin_crc & 0x0000FF00) >> 8)+chr((all_bin_crc & 0x00FF0000) >> 16)+chr((all_bin_crc & 0xFF000000) >> 24))
cmd = 'rm eagle.app.sym'
os.system(cmd)
print('Flash program image size: %d bytes' % flash_image_size)
extra_bin_size = os.stat(extra_bin).st_size if extra_bin else 0
if extra_bin:
print('Flash extra data size: %d bytes' % extra_bin_size)
print('Remaining program space: %d bytes' % (476 * 1024 - flash_image_size))
if __name__=='__main__':
gen_appbin()