1import argparse 2import os 3import sys 4 5def bin2c_convert(binfile_name, array_name, padding_len=0): 6 7 with open(binfile_name, 'rb') as in_file: 8 bindata = in_file.read() 9 10 out = 'const char %s[%d] = {\n' % (array_name, len(bindata)+padding_len) 11 line = '' 12 first_char=True 13 nb_char_in_line = 0 14 for byte in bindata: 15 if first_char: 16 line += ' 0x%02x' % byte 17 first_char = False 18 else: 19 line += ',0x%02x' % byte 20 nb_char_in_line = nb_char_in_line + 1 21 if nb_char_in_line == 16: 22 out += ' ' + line + '\n' 23 nb_char_in_line = 0 24 line = ' ' 25 while padding_len > 0: 26 line += ',0xff' 27 padding_len = padding_len-1 28 # add the last line 29 if nb_char_in_line < 16: 30 out += ' ' + line + '\n' 31 out += '};\n' 32 return out 33 34 35def bin2c_file(bin_file_name, out_src_file_name, dest_path): 36 37 bin_file_name=dest_path+'/../fw_bin/'+bin_file_name 38 if not os.path.isfile(bin_file_name): 39 print('File "%s" is not found!' % bin_file_name) 40 return '' 41 42 array_name = out_src_file_name 43 out_src_file_name = dest_path + '/' + out_src_file_name + '.c' 44 with open(out_src_file_name, 'w') as out_file: 45 c_file = bin2c_convert(bin_file_name, array_name, 4) 46 out_file.write(c_file) 47 48def generate_sb_src(conn_fwloader_dir, chip_revision): 49 50 a1_sb_list = [ 51 {'file': 'rw61x_sb_wifi_a1.bin', 'array_name': 'fw_cpu1'}, 52 {'file': 'rw61x_sb_ble_a1.bin', 'array_name': 'fw_cpu2_ble'}, 53 {'file': 'rw61x_sb_ble_15d4_combo_a1.bin', 'array_name': 'fw_cpu2_combo'} 54 ] 55 a2_sb_list = [ 56 {'file': 'rw61x_sb_wifi_a2.bin', 'array_name': 'fw_cpu1'}, 57 {'file': 'rw61x_sb_ble_a2.bin', 'array_name': 'fw_cpu2_ble'}, 58 {'file': 'rw61x_sb_ble_15d4_combo_a2.bin', 'array_name': 'fw_cpu2_combo'} 59 ] 60 61 if (chip_revision == 'A2'): 62 binlist = a2_sb_list 63 elif (chip_revision == 'A1'): 64 binlist = a1_sb_list 65 else: 66 return 67 dest_dir=conn_fwloader_dir+'/'+chip_revision 68 if not os.path.exists(dest_dir): 69 os.makedirs(dest_dir) 70 # SB3 format built for A0 71 for i in range(0,3): 72 input_file = binlist[i]['file'] 73 print(input_file, binlist[i]['array_name']) 74 bin2c_file(input_file, binlist[i]['array_name'], dest_dir) 75 76 77def generate_raw_src(conn_fwloader_dir, chip_revision): 78 79 a1_raw_list = [ 80 {'file': 'rw61xw_raw_cpu1_a1.bin', 'array_name': 'fw_cpu1'}, 81 {'file': 'rw61xn_raw_cpu2_ble_a1.bin', 'array_name': 'fw_cpu2_ble'}, 82 {'file': 'rw61xn_combo_raw_cpu2_ble_15_4_combo_a1.bin', 'array_name': 'fw_cpu2_combo'} 83 ] 84 a2_raw_list = [ 85 {'file': 'rw61xw_raw_cpu1_a2.bin', 'array_name': 'fw_cpu1'}, 86 {'file': 'rw61xn_raw_cpu2_ble_a2.bin', 'array_name': 'fw_cpu2_ble'}, 87 {'file': 'rw61xn_combo_raw_cpu2_ble_15_4_combo_a2.bin', 'array_name': 'fw_cpu2_combo'} 88 ] 89 90 if (chip_revision == 'A2'): 91 binlist = a2_raw_list 92 elif (chip_revision == 'A1'): 93 binlist = a1_raw_list 94 else: 95 return 96 dest_dir=conn_fwloader_dir+'/'+chip_revision 97 if not os.path.exists(dest_dir): 98 os.makedirs(dest_dir) 99 # SB3 format built for A0 100 for i in range(0,3): 101 #print(binlist[i]['file'], binlist[i]['array_name']) 102 bin2c_file(binlist[i]['file'], binlist[i]['array_name'], dest_dir) 103 104 105 106def main(): 107 parser = argparse.ArgumentParser(description='Bin2C Generator') 108 parser.add_argument('in_path', help="Directory to fw_bin containing binaries.") 109 parser.add_argument('out_dir', nargs='?') 110 parser.add_argument('-v', '--verbose', action='store_true', default=False, help="Sets verbose mode") 111 parser.add_argument('-t', '--type', nargs='?', default='sb', help='generate SB3 version or raw version') 112 parser.add_argument('-r', '--rev', default='all', help='generate for chip revisions A0, A1, A2 or all' ) 113 args = parser.parse_args() 114 115 if args.type == 'sb': 116 if args.rev == 'all': 117 generate_sb_src(args.in_path, 'A1') 118 generate_sb_src(args.in_path, 'A2') 119 else: 120 if args.rev == 'A1' or args.rev == 'A2': 121 generate_sb_src(args.in_path, args.rev, args.verbose) 122 elif args.type == 'raw': 123 if args.rev == 'all': 124 generate_raw_src(args.in_path, 'A1') 125 generate_raw_src(args.in_path, 'A2') 126 else: 127 if args.rev == 'A1' or args.rev == 'A2': 128 generate_raw_src(args.in_path, args.rev, args.verbose) 129 else: 130 print("error wrong chip revision argument must be in A0, A1, A2, or all") 131 else: 132 print("error wrong type argument") 133 134 135if __name__ == '__main__': 136 main() 137 138 139 140