@@ -55,28 +55,29 @@ def get_segment_size_addr(elf, segment, path):
5555 raise Exception ('Unable to find size and start point in file "' + elf + '" for "' + segment + '"' )
5656
5757def 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+
6465 return raw
6566
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 ],
6970 entry & 255 , (entry >> 8 ) & 255 , (entry >> 16 ) & 255 , (entry >> 24 ) & 255 ]
7071 out .write (bytearray (header ))
7172 total_size = 8
7273 checksum = 0xef
7374 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 )
7576 seghdr = [ addr & 255 , (addr >> 8 ) & 255 , (addr >> 16 ) & 255 , (addr >> 24 ) & 255 ,
7677 size & 255 , (size >> 8 ) & 255 , (size >> 16 ) & 255 , (size >> 24 ) & 255 ]
7778 out .write (bytearray (seghdr ));
7879 total_size += 8 ;
79- raw = read_segment (elf , segment , path )
80+ raw = read_segment (elf , segment , args . path )
8081 if len (raw ) != size :
8182 raise Exception ('Segment size doesn\' t match read data for "' + segment + '" in "' + elf + '"' )
8283 out .write (raw )
@@ -152,12 +153,24 @@ def main():
152153
153154 args = parser .parse_args ()
154155
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+ )
156168
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+ )
161174
162175 # Because the CRC includes both eboot and app, can only calculate it after the entire BIN generated
163176 add_crc (args .out )
0 commit comments