@@ -55,28 +55,29 @@ def get_segment_size_addr(elf, segment, path):
55
55
raise Exception ('Unable to find size and start point in file "' + elf + '" for "' + segment + '"' )
56
56
57
57
def read_segment (elf , segment , path ):
58
- tmpfile , dumpfile = tempfile .mkstemp ()
59
- os .close (tmpfile )
60
- p = subprocess .check_call ([path + "/xtensa-lx106-elf-objcopy" , '-O' , 'binary' , '--only-section=' + segment , elf , dumpfile ], stdout = subprocess .PIPE )
61
- binfile = open (dumpfile , "rb" )
62
- raw = binfile .read ()
63
- binfile .close ()
58
+ fd , tmpfile = tempfile .mkstemp ()
59
+ os .close (fd )
60
+ p = subprocess .check_call ([path + "/xtensa-lx106-elf-objcopy" , '-O' , 'binary' , '--only-section=' + segment , elf , tmpfile ], stdout = subprocess .PIPE )
61
+ with open (tmpfile , "rb" ) as f :
62
+ raw = f .read ()
63
+ os .remove (tmpfile )
64
+
64
65
return raw
65
66
66
- def write_bin (out , elf , segments , to_addr , flash_mode , flash_size , flash_freq , path ):
67
- entry = int (get_elf_entry ( elf , path ))
68
- header = [ 0xe9 , len (segments ), fmodeb [flash_mode ], ffreqb [flash_freq ] + 16 * fsizeb [flash_size ],
67
+ def write_bin (out , args , elf , segments , to_addr ):
68
+ entry = int (get_elf_entry ( elf , args . path ))
69
+ header = [ 0xe9 , len (segments ), fmodeb [args . flash_mode ], ffreqb [args . flash_freq ] + 16 * fsizeb [args . flash_size ],
69
70
entry & 255 , (entry >> 8 ) & 255 , (entry >> 16 ) & 255 , (entry >> 24 ) & 255 ]
70
71
out .write (bytearray (header ))
71
72
total_size = 8
72
73
checksum = 0xef
73
74
for segment in segments :
74
- [size , addr ] = get_segment_size_addr (elf , segment , path )
75
+ [size , addr ] = get_segment_size_addr (elf , segment , args . path )
75
76
seghdr = [ addr & 255 , (addr >> 8 ) & 255 , (addr >> 16 ) & 255 , (addr >> 24 ) & 255 ,
76
77
size & 255 , (size >> 8 ) & 255 , (size >> 16 ) & 255 , (size >> 24 ) & 255 ]
77
78
out .write (bytearray (seghdr ));
78
79
total_size += 8 ;
79
- raw = read_segment (elf , segment , path )
80
+ raw = read_segment (elf , segment , args . path )
80
81
if len (raw ) != size :
81
82
raise Exception ('Segment size doesn\' t match read data for "' + segment + '" in "' + elf + '"' )
82
83
out .write (raw )
@@ -152,12 +153,24 @@ def main():
152
153
153
154
args = parser .parse_args ()
154
155
155
- print ('Creating BIN file "' + args .out + '" using "' + args .app + '"' )
156
+ print ('Creating BIN file "{out}" using "{eboot}" and "{app}"' .format (
157
+ out = args .out , eboot = args .eboot , app = args .app ))
158
+
159
+ with open (args .out , "wb" ) as out :
160
+ def wrapper (** kwargs ):
161
+ write_bin (out = out , args = args , ** kwargs )
162
+
163
+ wrapper (
164
+ elf = args .eboot ,
165
+ segments = [".text" ],
166
+ to_addr = 4096
167
+ )
156
168
157
- out = open (args .out , "wb" )
158
- write_bin (out , args .eboot , ['.text' ], 4096 , args .flash_mode , args .flash_size , args .flash_freq , args .path )
159
- write_bin (out , args .app , ['.irom0.text' , '.text' , '.text1' , '.data' , '.rodata' ], 0 , args .flash_mode , args .flash_size , args .flash_freq , args .path )
160
- out .close ()
169
+ wrapper (
170
+ elf = args .app ,
171
+ segments = [".irom0.text" , ".text" , ".text1" , ".data" , ".rodata" ],
172
+ to_addr = 0
173
+ )
161
174
162
175
# Because the CRC includes both eboot and app, can only calculate it after the entire BIN generated
163
176
add_crc (args .out )
0 commit comments