├── .gitignore ├── README.md ├── LICENSE └── merge_bin_esp.py /.gitignore: -------------------------------------------------------------------------------- 1 | output/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32 binary merger 2 | 3 | This script has been made to merge all bin file from ESP32 build, but can be used for other MCU type. 4 | 5 | # Example 6 | 7 | Merging 4 binaries : 8 | 9 | ``` 10 | $ python merge_bin_esp.py --output_name app_output.bin --bin_path bootloader.bin app.bin partition-table.bin ota_data_initial.bin --bin_address 0x1000 0x10000 0x8000 0xd000 11 | Add bootloader.bin from 0x1000 to 0x70f0 (0x60f0) 12 | Add partition-table.bin from 0x8000 to 0x8c00 (0xc00) 13 | Add ota_data_initial.bin from 0xd000 to 0xf000 (0x2000) 14 | Add app.bin from 0x10000 to 0x155d80 (0x145d80) 15 | app_output.bin generated with success ! 16 | ``` 17 | 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Tony Martinet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /merge_bin_esp.py: -------------------------------------------------------------------------------- 1 | import os 2 | import argparse 3 | 4 | class bin(): 5 | def __init__(self, file_path, addr): 6 | self.file_path = file_path 7 | self.file_name = os.path.basename(file_path) 8 | self.addr = addr 9 | self.size = self.get_size() 10 | 11 | def get_size(self): 12 | return int(os.path.getsize(self.file_path)) 13 | 14 | class multiple_bin(): 15 | def __init__(self, name, output_folder): 16 | self.name = name 17 | self.output_folder = output_folder 18 | try: 19 | os.makedirs(os.path.realpath(self.output_folder)) 20 | except: 21 | pass 22 | self.output_path = os.path.realpath(os.path.join(self.output_folder,self.name)) 23 | self.bin_array = [] 24 | 25 | def add_bin(self, file_path, addr): 26 | self.bin_array.append(bin(file_path, addr)) 27 | 28 | def sort_bin(self): 29 | swapped = True 30 | while swapped: 31 | swapped = False 32 | for i in range(len(self.bin_array) - 1): 33 | if self.bin_array[i].addr > self.bin_array[i + 1].addr: 34 | self.bin_array[i], self.bin_array[i + 1] = self.bin_array[i + 1], self.bin_array[i] 35 | swapped = True 36 | 37 | def add_bin_to_other_bin(self, previous, binary): 38 | with open(self.output_path, "ab") as output_file: 39 | output_file.write( b'\xff' * (binary.addr-previous)) 40 | print ("Add %s from 0x%x to 0x%x (0x%x)"%(binary.file_name, binary.addr, binary.addr+binary.size, binary.size)) 41 | with open(self.output_path, "ab") as output_file, open(binary.file_path, "rb") as bin_file: 42 | output_file.write(bin_file.read()) 43 | return binary.addr+binary.size 44 | 45 | def create_bin(self): 46 | new_start = 0 47 | open(self.output_path, "wb").close 48 | for b in self.bin_array: 49 | new_start = self.add_bin_to_other_bin(new_start, b) 50 | 51 | def check_if_possible(self): 52 | for i in range(1, len(self.bin_array)): 53 | if(self.bin_array[i].addr < (self.bin_array[i-1].addr+self.bin_array[i-1].size)): 54 | print (self.bin_array[i].addr, (self.bin_array[i-1].addr+self.bin_array[i-1].size)) 55 | raise Exception("Not possible to create this bin, overlapping between %s and %s"%(self.bin_array[i].file_name, self.bin_array[i-1].file_name)) 56 | 57 | def main(): 58 | parser = argparse.ArgumentParser(description='Script to merge *.bin file at different position') 59 | parser.add_argument( 60 | '--output_name', 61 | help = 'Output file name', default = "output.bin") 62 | parser.add_argument( 63 | '--output_folder', default = "output", 64 | help = 'Output folder path') 65 | parser.add_argument( 66 | '--input_folder', default = "", 67 | help = 'Input folder path') 68 | parser.add_argument( 69 | '--bin_path',nargs='+',required=True, 70 | help = 'List of bin path, same order as bin_address (space seperated)') 71 | parser.add_argument( 72 | '--bin_address',nargs='+',type=lambda x: int(x,0),required=True, 73 | help = 'List of addr, same order as bin_path (space seperated)') 74 | parser.add_argument 75 | args = parser.parse_args() 76 | mb = multiple_bin(args.output_name, args.output_folder) 77 | for path,address in zip(args.bin_path, args.bin_address): 78 | mb.add_bin(os.path.join(args.input_folder, path), address) 79 | mb.sort_bin() 80 | mb.check_if_possible() 81 | mb.create_bin() 82 | print ("%s generated with success ! (size %u)"%(args.output_name, int(os.path.getsize(mb.output_path)))) 83 | 84 | if __name__ == "__main__": 85 | exit(main()) 86 | --------------------------------------------------------------------------------