In the process of burning the program, you need to synthesize multiple binary bin files, and to specify the data segment address offset, the address between data segments to fill the default value of 0xFF. Because you don't want to do it manually, you can generate the merged bin file directly from the make command by adding a call to the Python script command in makefile to implement the bin file merge operation, and without really learning the scripting language, Python has the fastest way to get started. I decided to write a script in Python and add it to the makefile.
Python uses the struct library to manipulate binary files like C.
The approximate idea of scripting is:
1. Determine the final size of the synthesized file based on the address offset of the last file data and the size of the last file.
2. Because the first bin file has an offset of 0, create a copy of the first file, add 0xFF or other bin file data to the end of the file based on the data address offset, and after all the bin files have been added, the copy is the merged bin file.
The Python script code is as follows:
ImportOSImportShutilImportStructbin1_path='Demo1.bin'Bin2_path='Demo2.bin'Bin3_path='Demo3.bin'Bin4_path='Demo4.bin'Bin_result_path='Imagedemo.bin'Offset1= 0x00000000Offset2= 0x00008000Offset3= 0x0000a000Offset4= 0x00100000shutil.copyfile (bin1_path,bin_result_path) bin_1= Open (Bin1_path,'RB') bin_2= Open (Bin2_path,'RB') Bin_3= Open (Bin3_path,'RB') Bin_4= Open (Bin4_path,'RB') Bin_merge= Open (Bin_result_path,'AB') Bin4_size=os.path.getsize (bin4_path) bin_result_size=os.path.getsize (bin_result_path) final_size= 1024*1024*1 +Bin4_sizeoffset=Bin_result_sizevalue_default= Struct.pack ('B', 0xFF) whileOffset <final_size:ifoffset = =Offset2:data=Bin_2.read () bin_merge.write (data) offset=Bin_merge.tell ()elifoffset = =Offset3:data=Bin_3.read () bin_merge.write (data) offset=Bin_merge.tell ()elifoffset = =Offset4:data=Bin_4.read () bin_merge.write (data) offset=Bin_merge.tell ()Else: Bin_merge.write (value_default) offset=Bin_merge.tell () bin_1.close () Bin_2.close () Bin_3.close () Bin_4.close () bin_merge.close ()
Python read-write and merge binary bin files