From 82735b8750d61558ab9a29e2d7bf8514326a297b Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Wed, 28 Oct 2020 19:41:32 +0200 Subject: [PATCH] Linter applied for flash_build.py --- image_conf/flash_build.py | 866 ++++++++++++++++++++++++-------------- 1 file changed, 542 insertions(+), 324 deletions(-) diff --git a/image_conf/flash_build.py b/image_conf/flash_build.py index d43ab6018..8d6fd91e1 100644 --- a/image_conf/flash_build.py +++ b/image_conf/flash_build.py @@ -18,7 +18,7 @@ app_path = "" chip_name = "" -bl_factory_params_file_prefix = 'bl_factory_params_' +bl_factory_params_file_prefix = "bl_factory_params_" bin_build_out_path = "build_out" default_conf_path = "" efuse_mask_file = "" @@ -42,7 +42,7 @@ def bl_find_file_list(key_val, endswith): # return find_file if file_path_list != []: return file_path_list - conf_path = os.path.join(os.path.abspath('..'), "image_conf", default_conf_path) + conf_path = os.path.join(os.path.abspath(".."), "image_conf", default_conf_path) files = os.listdir(conf_path) for f in files: if key_val in f and f.endswith(endswith): @@ -53,22 +53,22 @@ def bl_find_file_list(key_val, endswith): def bl_find_file(key_val, endswith): - conf_path = os.path.join(app_path, "img_conf") + conf_path = os.path.join(app_path, "img_conf") if os.path.exists(conf_path): files = os.listdir(conf_path) for f in files: if key_val in f and f.endswith(endswith): find_file = os.path.join(conf_path, f) return find_file - conf_path = os.path.join(os.path.abspath('..'), "image_conf", default_conf_path) + conf_path = os.path.join(os.path.abspath(".."), "image_conf", default_conf_path) files = os.listdir(conf_path) for f in files: if key_val in f and f.endswith(endswith): find_file = os.path.join(conf_path, f) return find_file -class bl_efuse_boothd_gen(): +class bl_efuse_boothd_gen: def __init__(self): self.utils = bl_utils() @@ -76,25 +76,29 @@ def bootheader_update_flash_pll_crc(self, bootheader_data): flash_cfg_start = 8 flash_cfg_len = 4 + 84 + 4 # magic+......+CRC32 - flash_cfg = bootheader_data[flash_cfg_start + 4:flash_cfg_start + flash_cfg_len - 4] + flash_cfg = bootheader_data[ + flash_cfg_start + 4 : flash_cfg_start + flash_cfg_len - 4 + ] crcarray = self.utils.get_crc32_bytearray(flash_cfg) - bootheader_data[flash_cfg_start + flash_cfg_len - 4:flash_cfg_start + flash_cfg_len] = crcarray + bootheader_data[ + flash_cfg_start + flash_cfg_len - 4 : flash_cfg_start + flash_cfg_len + ] = crcarray pll_cfg_start = flash_cfg_start + flash_cfg_len pll_cfg_len = 4 + 8 + 4 # magic+......+CRC32 - pll_cfg = bootheader_data[pll_cfg_start + 4:pll_cfg_start + pll_cfg_len - 4] + pll_cfg = bootheader_data[pll_cfg_start + 4 : pll_cfg_start + pll_cfg_len - 4] crcarray = self.utils.get_crc32_bytearray(pll_cfg) - bootheader_data[pll_cfg_start + pll_cfg_len - 4:pll_cfg_start + pll_cfg_len] = crcarray + bootheader_data[ + pll_cfg_start + pll_cfg_len - 4 : pll_cfg_start + pll_cfg_len + ] = crcarray return bootheader_data - - + def get_int_mask(self, pos, length): ones = "1" * 32 zeros = "0" * 32 - mask = ones[0:32 - pos - length] + zeros[0:length] + ones[0:pos] + mask = ones[0 : 32 - pos - length] + zeros[0:length] + ones[0:pos] return int(mask, 2) - - + def update_data_from_cfg(self, config_keys, config_file, section): cfg = BFConfigParser() cfg.read(config_file) @@ -119,41 +123,53 @@ def update_data_from_cfg(self, config_keys, config_file, section): offset = int(config_keys.get(key)["offset"], 10) pos = int(config_keys.get(key)["pos"], 10) bitlen = int(config_keys.get(key)["bitlen"], 10) - - oldval = self.utils.bytearray_to_int(self.utils.bytearray_reverse(data[offset:offset + 4])) - oldval_mask = self.utils.bytearray_to_int(self.utils.bytearray_reverse(data_mask[offset:offset + 4])) + + oldval = self.utils.bytearray_to_int( + self.utils.bytearray_reverse(data[offset : offset + 4]) + ) + oldval_mask = self.utils.bytearray_to_int( + self.utils.bytearray_reverse(data_mask[offset : offset + 4]) + ) newval = (oldval & self.get_int_mask(pos, bitlen)) + (val << pos) if val != 0: - newval_mask = (oldval_mask | (~self.get_int_mask(pos, bitlen))) + newval_mask = oldval_mask | (~self.get_int_mask(pos, bitlen)) else: newval_mask = oldval_mask - data[offset:offset + 4] = self.utils.int_to_4bytearray_l(newval) - data_mask[offset:offset + 4] = self.utils.int_to_4bytearray_l(newval_mask) + data[offset : offset + 4] = self.utils.int_to_4bytearray_l(newval) + data_mask[offset : offset + 4] = self.utils.int_to_4bytearray_l(newval_mask) return data, data_mask - - - def bootheader_create_do(self, chipname, chiptype, config_file, section, output_file=None, if_img=False): + + def bootheader_create_do( + self, chipname, chiptype, config_file, section, output_file=None, if_img=False + ): efuse_bootheader_path = os.path.join(app_path, bin_build_out_path) try: # sub_module = __import__("bootheader_cfg_keys", fromlist=[chiptype]) - bh_data, tmp = self.update_data_from_cfg(B_CFG_KEYS.bootheader_cfg_keys, config_file, section) + bh_data, tmp = self.update_data_from_cfg( + B_CFG_KEYS.bootheader_cfg_keys, config_file, section + ) # bh_data, tmp = self.update_data_from_cfg(sub_module.bootheader_cfg_keys, config_file, section) bh_data = self.bootheader_update_flash_pll_crc(bh_data) if output_file == None: - fp = open(efuse_bootheader_path + "/" + section.lower().replace("_cfg", ".bin"), 'wb+') + fp = open( + efuse_bootheader_path + + "/" + + section.lower().replace("_cfg", ".bin"), + "wb+", + ) else: - fp = open(output_file, 'wb+') + fp = open(output_file, "wb+") if section == "BOOTHEADER_CFG" and chiptype == "bl60x": final_data = bytearray(8 * 1024) # add sp core feature # halt - bh_data[118] = (bh_data[118] | (1 << 2)) + bh_data[118] = bh_data[118] | (1 << 2) final_data[0:176] = bh_data - final_data[4096 + 0:4096 + 176] = bh_data + final_data[4096 + 0 : 4096 + 176] = bh_data # change magic final_data[4096 + 2] = 65 # change waydis to 0xf - final_data[117] = (final_data[117] | (15 << 4)) + final_data[117] = final_data[117] | (15 << 4) # change crc and hash ignore final_data[4096 + 118] = final_data[4096 + 118] | 0x03 bh_data = final_data @@ -166,89 +182,110 @@ def bootheader_create_do(self, chipname, chiptype, config_file, section, output_ else: fp.write(bh_data) fp.close() - fp = open(efuse_bootheader_path + "/flash_para.bin", 'wb+') - fp.write(bh_data[12:12 + 84]) + fp = open(efuse_bootheader_path + "/flash_para.bin", "wb+") + fp.write(bh_data[12 : 12 + 84]) fp.close() except Exception as err: print("bootheader_create_do fail!!") print(err) traceback.print_exc(limit=5, file=sys.stdout) - - def bootheader_create_process(self, chipname, chiptype, config_file, output_file1=None, output_file2=None, if_img=False): - fp = open(config_file, 'r') + + def bootheader_create_process( + self, + chipname, + chiptype, + config_file, + output_file1=None, + output_file2=None, + if_img=False, + ): + fp = open(config_file, "r") data = fp.read() fp.close() if "BOOTHEADER_CFG" in data: - self.bootheader_create_do(chipname, chiptype, config_file, "BOOTHEADER_CFG", output_file1, if_img) + self.bootheader_create_do( + chipname, chiptype, config_file, "BOOTHEADER_CFG", output_file1, if_img + ) if "BOOTHEADER_CPU0_CFG" in data: - self.bootheader_create_do(chipname, chiptype, config_file, "BOOTHEADER_CPU0_CFG", output_file1, if_img) + self.bootheader_create_do( + chipname, + chiptype, + config_file, + "BOOTHEADER_CPU0_CFG", + output_file1, + if_img, + ) if "BOOTHEADER_CPU1_CFG" in data: - self.bootheader_create_do(chipname, chiptype, config_file, "BOOTHEADER_CPU1_CFG", output_file2, if_img) - - + self.bootheader_create_do( + chipname, + chiptype, + config_file, + "BOOTHEADER_CPU1_CFG", + output_file2, + if_img, + ) + def efuse_create_process(self, chipname, chiptype, config_file, output_file=None): efuse_file = os.path.join(app_path, bin_build_out_path, "efusedata.bin") # sub_module = __import__("efuse_cfg_keys.py", fromlist=[chiptype]) - efuse_data, mask = self.update_data_from_cfg(E_CFG_KEYS.efuse_cfg_keys, config_file, "EFUSE_CFG") + efuse_data, mask = self.update_data_from_cfg( + E_CFG_KEYS.efuse_cfg_keys, config_file, "EFUSE_CFG" + ) # efuse_data, mask = self.update_data_from_cfg(sub_module.efuse_cfg_keys, config_file, "EFUSE_CFG") if output_file == None: - fp = open(efuse_file, 'wb+') + fp = open(efuse_file, "wb+") else: - fp = open(output_file, 'wb+') + fp = open(output_file, "wb+") fp.write(efuse_data) fp.close() - efuse_mask_file = os.path.join(app_path, bin_build_out_path, "efusedata_mask.bin") + efuse_mask_file = os.path.join( + app_path, bin_build_out_path, "efusedata_mask.bin" + ) if output_file == None: - fp = open(efuse_mask_file, 'wb+') + fp = open(efuse_mask_file, "wb+") else: - fp = open(output_file.replace(".bin", "_mask.bin"), 'wb+') + fp = open(output_file.replace(".bin", "_mask.bin"), "wb+") fp.write(mask) fp.close() - + def efuse_boothd_create_process(self, chipname, chiptype, config_file): self.bootheader_create_process(chipname, chiptype, config_file) self.efuse_create_process(chipname, chiptype, config_file) -class bl_utils(): - - #12345678->0x12,0x34,0x56,0x78 + +class bl_utils: + + # 12345678->0x12,0x34,0x56,0x78 def hexstr_to_bytearray_b(self, hexstring): return bytearray.fromhex(hexstring) - - + def hexstr_to_bytearray(self, hexstring): return bytearray.fromhex(hexstring) - - + def hexstr_to_bytearray_l(self, hexstring): b = bytearray.fromhex(hexstring) b.reverse() return b - - + def int_to_2bytearray_l(self, intvalue): return struct.pack("H", intvalue) - - + def int_to_4bytearray_l(self, intvalue): src = bytearray(4) - src[3] = ((intvalue >> 24) & 0xFF) - src[2] = ((intvalue >> 16) & 0xFF) - src[1] = ((intvalue >> 8) & 0xFF) - src[0] = ((intvalue >> 0) & 0xFF) + src[3] = (intvalue >> 24) & 0xFF + src[2] = (intvalue >> 16) & 0xFF + src[1] = (intvalue >> 8) & 0xFF + src[0] = (intvalue >> 0) & 0xFF return src - - + def int_to_4bytearray_b(self, intvalue): val = int_to_4bytearray_l(intvalue) val.reverse() return val - - + def bytearray_reverse(self, a): l = len(a) b = bytearray(l) @@ -257,20 +294,16 @@ def bytearray_reverse(self, a): b[i] = a[l - i - 1] i = i + 1 return b - - + def bytearray_to_int(self, b): return int(binascii.hexlify(b), 16) - - + def string_to_bytearray(self, string): return bytes(string, encoding="utf8") - - + def bytearray_to_str(self, bytesarray): return str(bytesarray) - - + def get_random_hexstr(self, n_bytes): hextring = "" i = 0 @@ -278,13 +311,11 @@ def get_random_hexstr(self, n_bytes): hextring = hextring + str(binascii.hexlify(random.randint(0, 255))) i = i + 1 return hextring - - + def get_crc32_bytearray(self, data): crc = binascii.crc32(data) return self.int_to_4bytearray_l(crc) - - + def copyfile(self, srcfile, dstfile): if os.path.isfile(srcfile): fpath, fname = os.path.split(dstfile) @@ -294,31 +325,31 @@ def copyfile(self, srcfile, dstfile): else: print("Src file not exists") sys.exit() - - def enable_udp_send_log(self, server,local_echo): - global udp_send_log,udp_socket_server,upd_log_local_echo - udp_send_log=True - upd_log_local_echo=local_echo - udp_socket_server=server - - def add_udp_client(self, tid,upd_client): - udp_clinet_dict[tid]=upd_client - + + def enable_udp_send_log(self, server, local_echo): + global udp_send_log, udp_socket_server, upd_log_local_echo + udp_send_log = True + upd_log_local_echo = local_echo + udp_socket_server = server + + def add_udp_client(self, tid, upd_client): + udp_clinet_dict[tid] = upd_client + def remove_udp_client(self, tid): del udp_clinet_dict[tid] - + def Update_Cfg(self, cfg, section, key, value): if cfg.has_option(section, key): cfg.set(section, key, str(value)) else: - #print key," not found,adding it" + # print key," not found,adding it" cfg.set(section, key, str(value)) - - + def get_byte_array(self, str): return str.encode("utf-8") - -#class BFConfigParser(configparser.ConfigParser): + + +# class BFConfigParser(configparser.ConfigParser): # def __init__(self, defaults=None): # configparser.ConfigParser.__init__(self, defaults=defaults) # @@ -326,7 +357,7 @@ def get_byte_array(self, str): # return optionstr -class BFConfigParser(): +class BFConfigParser: cfg_infile = None cfg_obj = ConfigObj() @@ -336,12 +367,12 @@ def __init__(self, file=None): def read(self, file=None): self.cfg_infile = file - self.cfg_obj = ConfigObj(self.cfg_infile, encoding='UTF8') + self.cfg_obj = ConfigObj(self.cfg_infile, encoding="UTF8") return self.cfg_obj def get(self, section, key): ret = self.cfg_obj[section][key] - if ret == "\"\"": + if ret == '""': return "" else: return ret @@ -387,12 +418,12 @@ def write(self, outfile=None, flag=None): self.cfg_obj.filename = outfile self.cfg_obj.write() -class PtCreater(bl_utils): +class PtCreater(bl_utils): def __init__(self, config_file): - #if not os.path.exists(config_file): + # if not os.path.exists(config_file): # config_file = os.path.join(default_conf_path, "partition_cfg_2M.toml") - #config_file = bl_find_file("partition_cfg_", ".toml") + # config_file = bl_find_file("partition_cfg_", ".toml") self.parsed_toml = toml.load(config_file) self.entry_max = 16 self.pt_new = False @@ -414,14 +445,24 @@ def __create_pt_table_do(self, lists, file): if len(entry_name) >= 8: print("Entry name is too long!") return False - entry_table[36 * entry_cnt + 3:36 * entry_cnt + 3 + len(entry_name)] = bytearray(entry_name, "utf-8") + bytearray(0) - entry_table[36 * entry_cnt + 12:36 * entry_cnt + 16] = self.int_to_4bytearray_l(entry_addr0) - entry_table[36 * entry_cnt + 16:36 * entry_cnt + 20] = self.int_to_4bytearray_l(entry_addr1) - entry_table[36 * entry_cnt + 20:36 * entry_cnt + 24] = self.int_to_4bytearray_l(entry_maxlen0) - entry_table[36 * entry_cnt + 24:36 * entry_cnt + 28] = self.int_to_4bytearray_l(entry_maxlen1) + entry_table[ + 36 * entry_cnt + 3 : 36 * entry_cnt + 3 + len(entry_name) + ] = bytearray(entry_name, "utf-8") + bytearray(0) + entry_table[ + 36 * entry_cnt + 12 : 36 * entry_cnt + 16 + ] = self.int_to_4bytearray_l(entry_addr0) + entry_table[ + 36 * entry_cnt + 16 : 36 * entry_cnt + 20 + ] = self.int_to_4bytearray_l(entry_addr1) + entry_table[ + 36 * entry_cnt + 20 : 36 * entry_cnt + 24 + ] = self.int_to_4bytearray_l(entry_maxlen0) + entry_table[ + 36 * entry_cnt + 24 : 36 * entry_cnt + 28 + ] = self.int_to_4bytearray_l(entry_maxlen1) entry_cnt += 1 - #partition table header - #0x54504642 + # partition table header + # 0x54504642 pt_table = bytearray(16) pt_table[0] = 0x42 pt_table[1] = 0x46 @@ -429,9 +470,11 @@ def __create_pt_table_do(self, lists, file): pt_table[3] = 0x54 pt_table[6:8] = self.int_to_2bytearray_l(int(entry_cnt)) pt_table[12:16] = self.get_crc32_bytearray(pt_table[0:12]) - entry_table[36 * entry_cnt:36 * entry_cnt + 4] = self.get_crc32_bytearray(entry_table[0:36 * entry_cnt]) - data = pt_table + entry_table[0:36 * entry_cnt + 4] - fp = open(file, 'wb+') + entry_table[36 * entry_cnt : 36 * entry_cnt + 4] = self.get_crc32_bytearray( + entry_table[0 : 36 * entry_cnt] + ) + data = pt_table + entry_table[0 : 36 * entry_cnt + 4] + fp = open(file, "wb+") fp.write(data) fp.close() return True @@ -448,26 +491,26 @@ def get_pt_table_addr(self): def construct_table(self): parcel = {} if self.pt_new == True: - parcel['pt_new'] = True + parcel["pt_new"] = True else: - parcel['pt_new'] = False - parcel['pt_addr0'] = self.parsed_toml["pt_table"]["address0"] - parcel['pt_addr1'] = self.parsed_toml["pt_table"]["address1"] + parcel["pt_new"] = False + parcel["pt_addr0"] = self.parsed_toml["pt_table"]["address0"] + parcel["pt_addr1"] = self.parsed_toml["pt_table"]["address1"] for tbl_item in self.parsed_toml["pt_entry"]: - if tbl_item['name'] == 'factory': - parcel['conf_addr'] = tbl_item['address0'] - if tbl_item['name'] == 'FW_CPU0': - parcel['fw_cpu0_addr'] = tbl_item['address0'] - if tbl_item['name'] == 'FW': - parcel['fw_addr'] = tbl_item['address0'] - if tbl_item['name'] == 'media': - parcel['media_addr'] = tbl_item['address0'] - if tbl_item['name'] == 'mfg': - parcel['mfg_addr'] = tbl_item['address0'] + if tbl_item["name"] == "factory": + parcel["conf_addr"] = tbl_item["address0"] + if tbl_item["name"] == "FW_CPU0": + parcel["fw_cpu0_addr"] = tbl_item["address0"] + if tbl_item["name"] == "FW": + parcel["fw_addr"] = tbl_item["address0"] + if tbl_item["name"] == "media": + parcel["media_addr"] = tbl_item["address0"] + if tbl_item["name"] == "mfg": + parcel["mfg_addr"] = tbl_item["address0"] return parcel -class bl_img_create_do(bl_utils): +class bl_img_create_do(bl_utils): def __init__(self): cfg = BFConfigParser() @@ -501,27 +544,29 @@ def __init__(self): rd_lock_key_slot_5 = 31 #####################update efuse info########################################## - def img_update_efuse(self, sign, pk_hash, flash_encryp_type, flash_key, sec_eng_key_sel, sec_eng_key): - fp = open(cfg.get("Img_Cfg", "efuse_file"), 'rb') + def img_update_efuse( + self, sign, pk_hash, flash_encryp_type, flash_key, sec_eng_key_sel, sec_eng_key + ): + fp = open(cfg.get("Img_Cfg", "efuse_file"), "rb") efuse_data = bytearray(fp.read()) + bytearray(0) fp.close() - fp = open(cfg.get("Img_Cfg", "efuse_mask_file"), 'rb') + fp = open(cfg.get("Img_Cfg", "efuse_mask_file"), "rb") efuse_mask_data = bytearray(fp.read()) + bytearray(0) fp.close() mask_4bytes = bytearray.fromhex("FFFFFFFF") efuse_data[0] |= flash_encryp_type - efuse_data[0] |= (sign << 2) + efuse_data[0] |= sign << 2 if flash_encryp_type > 0: efuse_data[0] |= 0x80 - efuse_mask_data[0] |= 0xff + efuse_mask_data[0] |= 0xFF rw_lock = 0 if pk_hash != None: efuse_data[keyslot0:keyslot2] = pk_hash efuse_mask_data[keyslot0:keyslot2] = mask_4bytes * 8 - rw_lock |= (1 << wr_lock_key_slot_0) - rw_lock |= (1 << wr_lock_key_slot_1) + rw_lock |= 1 << wr_lock_key_slot_0 + rw_lock |= 1 << wr_lock_key_slot_1 if flash_key != None: if flash_encryp_type == 1: # aes 128 @@ -536,10 +581,10 @@ def img_update_efuse(self, sign, pk_hash, flash_encryp_type, flash_key, sec_eng_ efuse_data[keyslot2:keyslot4] = flash_key efuse_mask_data[keyslot2:keyslot4] = mask_4bytes * 8 - rw_lock |= (1 << wr_lock_key_slot_2) - rw_lock |= (1 << wr_lock_key_slot_3) - rw_lock |= (1 << rd_lock_key_slot_2) - rw_lock |= (1 << rd_lock_key_slot_3) + rw_lock |= 1 << wr_lock_key_slot_2 + rw_lock |= 1 << wr_lock_key_slot_3 + rw_lock |= 1 << rd_lock_key_slot_2 + rw_lock |= 1 << rd_lock_key_slot_3 if sec_eng_key != None: if flash_encryp_type == 0: @@ -547,69 +592,61 @@ def img_update_efuse(self, sign, pk_hash, flash_encryp_type, flash_key, sec_eng_ efuse_data[keyslot2:keyslot3] = sec_eng_key[16:32] efuse_data[keyslot3:keyslot4] = sec_eng_key[0:16] efuse_mask_data[keyslot2:keyslot4] = mask_4bytes * 8 - rw_lock |= (1 << wr_lock_key_slot_2) - rw_lock |= (1 << wr_lock_key_slot_3) - rw_lock |= (1 << rd_lock_key_slot_2) - rw_lock |= (1 << rd_lock_key_slot_3) + rw_lock |= 1 << wr_lock_key_slot_2 + rw_lock |= 1 << wr_lock_key_slot_3 + rw_lock |= 1 << rd_lock_key_slot_2 + rw_lock |= 1 << rd_lock_key_slot_3 if sec_eng_key_sel == 1: efuse_data[keyslot3:keyslot4] = sec_eng_key[16:32] efuse_data[keyslot2:keyslot3] = sec_eng_key[0:16] efuse_mask_data[keyslot2:keyslot4] = mask_4bytes * 8 - rw_lock |= (1 << wr_lock_key_slot_2) - rw_lock |= (1 << wr_lock_key_slot_3) - rw_lock |= (1 << rd_lock_key_slot_2) - rw_lock |= (1 << rd_lock_key_slot_3) + rw_lock |= 1 << wr_lock_key_slot_2 + rw_lock |= 1 << wr_lock_key_slot_3 + rw_lock |= 1 << rd_lock_key_slot_2 + rw_lock |= 1 << rd_lock_key_slot_3 if flash_encryp_type == 1: if sec_eng_key_sel == 0: efuse_data[keyslot4:keyslot5] = sec_eng_key[0:16] efuse_mask_data[keyslot4:keyslot5] = mask_4bytes * 4 - rw_lock |= (1 << wr_lock_key_slot_4_l) - rw_lock |= (1 << wr_lock_key_slot_4_h) - rw_lock |= (1 << rd_lock_key_slot_4) + rw_lock |= 1 << wr_lock_key_slot_4_l + rw_lock |= 1 << wr_lock_key_slot_4_h + rw_lock |= 1 << rd_lock_key_slot_4 if sec_eng_key_sel == 1: efuse_data[keyslot4:keyslot5] = sec_eng_key[0:16] efuse_mask_data[keyslot4:keyslot5] = mask_4bytes * 4 - rw_lock |= (1 << wr_lock_key_slot_4_l) - rw_lock |= (1 << wr_lock_key_slot_4_h) - rw_lock |= (1 << rd_lock_key_slot_4) + rw_lock |= 1 << wr_lock_key_slot_4_l + rw_lock |= 1 << wr_lock_key_slot_4_h + rw_lock |= 1 << rd_lock_key_slot_4 # set read write lock key efuse_data[124:128] = self.int_to_4bytearray_l(rw_lock) efuse_mask_data[124:128] = self.int_to_4bytearray_l(rw_lock) - fp = open(cfg.get("Img_Cfg", "efuse_file"), 'wb+') + fp = open(cfg.get("Img_Cfg", "efuse_file"), "wb+") fp.write(efuse_data) fp.close() - fp = open(cfg.get("Img_Cfg", "efuse_mask_file"), 'wb+') + fp = open(cfg.get("Img_Cfg", "efuse_mask_file"), "wb+") fp.write(efuse_mask_data) fp.close() - ####################get sign and encrypt info########################################## - def img_create_get_sign_encrypt_info(self, bootheader_data): sign = bootheader_data[116] & 0x3 - encrypt = ((bootheader_data[116] >> 2) & 0x3) - key_sel = ((bootheader_data[116] >> 4) & 0x3) + encrypt = (bootheader_data[116] >> 2) & 0x3 + key_sel = (bootheader_data[116] >> 4) & 0x3 return sign, encrypt, key_sel - ####################get hash ignore ignore########################################## - def img_create_get_hash_ignore(self, bootheader_data): return (bootheader_data[118] >> 1) & 0x1 - ####################get crc ignore ignore########################################## - def img_create_get_crc_ignore(self, bootheader_data): return bootheader_data[118] & 0x1 - #####################update boot header info########################################## - def img_create_update_bootheader(self, bootheader_data, hash, seg_cnt): # update segment count bootheader_data[120:124] = self.int_to_4bytearray_l(seg_cnt) @@ -627,55 +664,55 @@ def img_create_update_bootheader(self, bootheader_data, hash, seg_cnt): # do nothing pass else: - hd_crcarray = self.get_crc32_bytearray(bootheader_data[0:176 - 4]) - bootheader_data[176 - 4:176] = hd_crcarray + hd_crcarray = self.get_crc32_bytearray(bootheader_data[0 : 176 - 4]) + bootheader_data[176 - 4 : 176] = hd_crcarray print("Header crc: ", binascii.hexlify(hd_crcarray)) return bootheader_data[0:176] - #####################update segment header according segdata######################### - def img_create_update_segheader(self, segheader, segdatalen, segdatacrc): segheader[4:8] = segdatalen segheader[8:12] = segdatacrc return segheader - #####################do hash of image################################################ - def img_create_sha256_data(self, data_bytearray): hashfun = SHA256.new() hashfun.update(data_bytearray) return self.hexstr_to_bytearray(hashfun.hexdigest()) - #####################encrypt image, mainly segdata##################################### - - def img_create_encrypt_data(self, data_bytearray, key_bytearray, iv_bytearray, flash_img): + def img_create_encrypt_data( + self, data_bytearray, key_bytearray, iv_bytearray, flash_img + ): if flash_img == 0: cryptor = AES.new(key_bytearray, AES.MODE_CBC, iv_bytearray) ciphertext = cryptor.encrypt(data_bytearray) else: - #iv = Crypto.Util.Counter.new(128, initial_value = long(binascii.hexlify(iv_bytearray),16)) + # iv = Crypto.Util.Counter.new(128, initial_value = long(binascii.hexlify(iv_bytearray),16)) iv = Counter.new(128, initial_value=int(binascii.hexlify(iv_bytearray), 16)) cryptor = AES.new(key_bytearray, AES.MODE_CTR, counter=iv) ciphertext = cryptor.encrypt(data_bytearray) return ciphertext - #####################sign image(hash code)##################################### - - def img_create_sign_data(self, data_bytearray, privatekey_file_uecc, publickey_file): + def img_create_sign_data( + self, data_bytearray, privatekey_file_uecc, publickey_file + ): sk = ecdsa.SigningKey.from_pem(open(privatekey_file_uecc).read()) vk = ecdsa.VerifyingKey.from_pem(open(publickey_file).read()) pk_data = vk.to_string() pk_hash = self.img_create_sha256_data(pk_data) - signature = sk.sign(data_bytearray, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_string) + signature = sk.sign( + data_bytearray, + hashfunc=hashlib.sha256, + sigencode=ecdsa.util.sigencode_string, + ) # return len+signature+crc len_array = self.int_to_4bytearray_l(len(signature)) @@ -683,12 +720,10 @@ def img_create_sign_data(self, data_bytearray, privatekey_file_uecc, publickey_f crcarray = self.get_crc32_bytearray(sig_field) return pk_data, pk_hash, sig_field + crcarray - ######################## read one file and append crc if needed##################### - def img_create_read_file_append_crc(self, file, crc): - fp = open(file, 'rb') + fp = open(file, "rb") read_data = bytearray(fp.read()) crcarray = bytearray(0) if crc: @@ -696,12 +731,11 @@ def img_create_read_file_append_crc(self, file, crc): fp.close() return read_data + crcarray - def img_creat_process(self, flash_img): encrypt_blk_size = 16 padding = bytearray(encrypt_blk_size) data_tohash = bytearray(0) - ret = 'OK' + ret = "OK" cfg_section = "Img_Cfg" # get segdata to deal with @@ -728,7 +762,9 @@ def img_creat_process(self, flash_img): privatekey_file_uecc = cfg.get(cfg_section, "privatekey_file_uecc") if encrypt != 0: - encrypt_key_org = self.hexstr_to_bytearray(cfg.get(cfg_section, "aes_key_org")) + encrypt_key_org = self.hexstr_to_bytearray( + cfg.get(cfg_section, "aes_key_org") + ) global encrypt_key if encrypt == 1: encrypt_key = encrypt_key_org[0:16] @@ -757,15 +793,18 @@ def img_creat_process(self, flash_img): seg_data = self.img_create_read_file_append_crc(segdata_file[i], 0) padding_size = 0 if len(seg_data) % encrypt_blk_size != 0: - padding_size = encrypt_blk_size - \ - len(seg_data) % encrypt_blk_size + padding_size = encrypt_blk_size - len(seg_data) % encrypt_blk_size seg_data += padding[0:padding_size] segdata_crcarray = self.get_crc32_bytearray(seg_data) seg_data_list.append(seg_data) # read seg header and replace segdata's CRC seg_header = self.img_create_read_file_append_crc(segheader_file[i], 0) - seg_header = img_create_update_segheader(seg_header, self.int_to_4bytearray_l(len(seg_data)), segdata_crcarray) + seg_header = img_create_update_segheader( + seg_header, + self.int_to_4bytearray_l(len(seg_data)), + segdata_crcarray, + ) segheader_crcarray = self.get_crc32_bytearray(seg_header) seg_header = seg_header + segheader_crcarray seg_header_list.append(seg_header) @@ -789,7 +828,9 @@ def img_creat_process(self, flash_img): # do encrypt if encrypt != 0: - data_toencrypt = img_create_encrypt_data(data_toencrypt, encrypt_key, encrypt_iv, flash_img) + data_toencrypt = img_create_encrypt_data( + data_toencrypt, encrypt_key, encrypt_iv, flash_img + ) # get fw data fw_data = bytearray(0) @@ -799,42 +840,67 @@ def img_creat_process(self, flash_img): # hash fw img hash = self.img_create_sha256_data(data_tohash) # update boot header and recalculate crc - bootheader_data = self.img_create_update_bootheader(bootheader_data, hash, seg_cnt) + bootheader_data = self.img_create_update_bootheader( + bootheader_data, hash, seg_cnt + ) # add signautre signature = bytearray(0) pk_hash = None if sign == 1: - pk_data, pk_hash, signature = img_create_sign_data(data_tohash, privatekey_file_uecc, publickey_file) + pk_data, pk_hash, signature = img_create_sign_data( + data_tohash, privatekey_file_uecc, publickey_file + ) pk_data = pk_data + self.get_crc32_bytearray(pk_data) # write whole image if flash_img == 1: bootinfo_file_name = cfg.get(cfg_section, "bootinfo_file") - fp = open(bootinfo_file_name, 'wb+') + fp = open(bootinfo_file_name, "wb+") bootinfo = bootheader_data + pk_data + signature + aesiv_data fp.write(bootinfo) fp.close() fw_file_name = cfg.get(cfg_section, "img_file") - fp = open(fw_file_name, 'wb+') + fp = open(fw_file_name, "wb+") fp.write(fw_data) fp.close() # update efuse if encrypt != 0: if encrypt == 1: # AES 128 - img_update_efuse(sign, pk_hash, 1, encrypt_key + bytearray(32 - len(encrypt_key)), key_sel, None) + img_update_efuse( + sign, + pk_hash, + 1, + encrypt_key + bytearray(32 - len(encrypt_key)), + key_sel, + None, + ) if encrypt == 2: # AES 256 - img_update_efuse(sign, pk_hash, 3, encrypt_key + bytearray(32 - len(encrypt_key)), key_sel, None) + img_update_efuse( + sign, + pk_hash, + 3, + encrypt_key + bytearray(32 - len(encrypt_key)), + key_sel, + None, + ) if encrypt == 3: # AES 192 - img_update_efuse(sign, pk_hash, 2, encrypt_key + bytearray(32 - len(encrypt_key)), key_sel, None) + img_update_efuse( + sign, + pk_hash, + 2, + encrypt_key + bytearray(32 - len(encrypt_key)), + key_sel, + None, + ) else: self.img_update_efuse(sign, pk_hash, encrypt, None, key_sel, None) else: whole_img_file_name = cfg.get(cfg_section, "whole_img_file") - fp = open(whole_img_file_name, 'wb+') + fp = open(whole_img_file_name, "wb+") img_data = bootheader_data + pk_data + signature + aesiv_data + fw_data fp.write(img_data) fp.close() @@ -842,27 +908,45 @@ def img_creat_process(self, flash_img): if encrypt != 0: if encrypt == 1: # AES 128 - img_update_efuse(sign, pk_hash, 1, None, key_sel, encrypt_key + bytearray(32 - len(encrypt_key))) + img_update_efuse( + sign, + pk_hash, + 1, + None, + key_sel, + encrypt_key + bytearray(32 - len(encrypt_key)), + ) if encrypt == 2: # AES 256 - img_update_efuse(sign, pk_hash, 3, None, key_sel, encrypt_key + bytearray(32 - len(encrypt_key))) + img_update_efuse( + sign, + pk_hash, + 3, + None, + key_sel, + encrypt_key + bytearray(32 - len(encrypt_key)), + ) if encrypt == 3: # AES 192 - img_update_efuse(sign, pk_hash, 2, None, key_sel, encrypt_key + bytearray(32 - len(encrypt_key))) + img_update_efuse( + sign, + pk_hash, + 2, + None, + key_sel, + encrypt_key + bytearray(32 - len(encrypt_key)), + ) else: img_update_efuse(sign, pk_hash, 0, None, key_sel, bytearray(32)) return "OK", data_tohash - def usage(): print(sys.argv[0], "\n") print("-i/--img_type= :image type:media or if") print("-h/--help :helper") - ####################################################################### - def img_create_do(self, options, img_dir_path=None, config_file=None): print("Image create path: ", img_dir_path) @@ -878,7 +962,9 @@ def img_create_do(self, options, img_dir_path=None, config_file=None): data_tohash = bytearray(0) try: - opts, args = getopt.getopt(options, 'i:s:Hh', ["img_type=", "signer=", "help"]) + opts, args = getopt.getopt( + options, "i:s:Hh", ["img_type=", "signer=", "help"] + ) for option, value in opts: if option in ["-h", "-H"]: usage() @@ -890,7 +976,6 @@ def img_create_do(self, options, img_dir_path=None, config_file=None): # will something like "option -a not recognized") print(err) usage() - if img_type == "media": flash_img = 1 @@ -910,8 +995,16 @@ def create_sp_media_image(self, config, cpu_type=None): cfg.read(config) self.img_creat_process(1) + class bl_img_create(bl_img_create_do): - def img_create(self, options, chipname="bl60x", chiptype="bl60x", img_dir=None, config_file=None): + def img_create( + self, + options, + chipname="bl60x", + chiptype="bl60x", + img_dir=None, + config_file=None, + ): img_dir_path = os.path.join(app_path, chipname, "img_create") if img_dir is None: self.img_create_do(options, img_dir_path, config_file) @@ -921,7 +1014,8 @@ def img_create(self, options, chipname="bl60x", chiptype="bl60x", img_dir=None, def create_sp_media_image_file(self, config, chiptype="bl60x", cpu_type=None): self.create_sp_media_image(config, cpu_type) -class bl_device_tree(): + +class bl_device_tree: def bl_dts2dtb(self, src_addr="", dest_addr=""): if "" == src_addr or "" == dest_addr: print("bl_dts2dtb please check arg.") @@ -940,11 +1034,12 @@ def bl_ro_params_device_tree(self, in_dts_config, out_bin_file): bin_file = out_bin_file self.bl_dts2dtb(dts_config, bin_file) -class bl_whole_img_generate(): + +class bl_whole_img_generate: def bl_create_flash_default_data(self, length): datas = bytearray(length) for i in range(length): - datas[i] = 0xff + datas[i] = 0xFF return datas def bl_get_largest_addr(self, addrs, files): @@ -959,7 +1054,7 @@ def bl_get_largest_addr(self, addrs, files): def bl_get_file_data(self, files): datas = [] for file in files: - with open(file, 'rb') as fp: + with open(file, "rb") as fp: data = fp.read() datas.append(data) return datas @@ -970,34 +1065,50 @@ def bl_write_flash_img(self, d_addrs, d_files, flash_size): filedatas = self.bl_get_file_data(d_files) for i in range(len(d_addrs)): start_addr = int(d_addrs[i], 16) - whole_img_data[start_addr:start_addr + len(filedatas[i])] = filedatas[i] + whole_img_data[start_addr : start_addr + len(filedatas[i])] = filedatas[i] # dst_file = os.path.join(app_path, bin_build_out_path, "whole_flash_data.bin") - dst_file = os.path.join(app_path, bin_build_out_path, "whole_{}.bin".format(file_finally_name)) - fp = open(dst_file, 'wb+') + dst_file = os.path.join( + app_path, bin_build_out_path, "whole_{}.bin".format(file_finally_name) + ) + fp = open(dst_file, "wb+") fp.write(whole_img_data) - print("Generating BIN File to %s" %(dst_file)) + print("Generating BIN File to %s" % (dst_file)) fp.close() - def bl_image_gen_cfg(self, raw_bin_name, bintype, key=None, iv=None, cfg_ini=None, cpu_type=None): + def bl_image_gen_cfg( + self, raw_bin_name, bintype, key=None, iv=None, cfg_ini=None, cpu_type=None + ): cfg = BFConfigParser() - if cfg_ini in [None, '']: + if cfg_ini in [None, ""]: f_org = bl_find_file("img_create_cfg", ".conf") f = os.path.join(app_path, bin_build_out_path, "img_create_cfg.ini") - #if os.path.isfile(f) == False: + # if os.path.isfile(f) == False: shutil.copy(f_org, f) else: f = cfg_ini cfg.read(f) if bintype == "fw": if cpu_type == None: - bootinfo_file = os.path.join(app_path, bin_build_out_path, "bootinfo.bin") + bootinfo_file = os.path.join( + app_path, bin_build_out_path, "bootinfo.bin" + ) img_file = os.path.join(app_path, bin_build_out_path, "img.bin") else: - bootinfo_file = os.path.join(app_path, bin_build_out_path, "bootinfo_{0}.bin".format(cpu_type.lower())) - img_file = os.path.join(app_path, bin_build_out_path, "img_{0}.bin".format(cpu_type.lower())) + bootinfo_file = os.path.join( + app_path, + bin_build_out_path, + "bootinfo_{0}.bin".format(cpu_type.lower()), + ) + img_file = os.path.join( + app_path, bin_build_out_path, "img_{0}.bin".format(cpu_type.lower()) + ) else: - bootinfo_file = os.path.join(app_path, bin_build_out_path, "bootinfo_{0}.bin".format(bintype)) - img_file = os.path.join(app_path, bin_build_out_path, "img_{0}.bin".format(bintype)) + bootinfo_file = os.path.join( + app_path, bin_build_out_path, "bootinfo_{0}.bin".format(bintype) + ) + img_file = os.path.join( + app_path, bin_build_out_path, "img_{0}.bin".format(bintype) + ) if cpu_type != None: img_section_name = "Img_" + cpu_type + "_Cfg" @@ -1009,28 +1120,39 @@ def bl_image_gen_cfg(self, raw_bin_name, bintype, key=None, iv=None, cfg_ini=Non bh_file = os.path.join(app_path, bin_build_out_path, "bootheader.bin") efuse_file = os.path.join(app_path, bin_build_out_path, "efusedata.bin") - efuse_mask_file = os.path.join(app_path, bin_build_out_path, "efusedata_mask.bin") - cfg.set(img_section_name, 'boot_header_file', bh_file) - cfg.set(img_section_name, 'efuse_file', efuse_file) - cfg.set(img_section_name, 'efuse_mask_file', efuse_mask_file) - cfg.set(img_section_name, 'segdata_file', raw_bin_name) - cfg.set(img_section_name, 'bootinfo_file', bootinfo_file) - cfg.set(img_section_name, 'img_file', img_file) + efuse_mask_file = os.path.join( + app_path, bin_build_out_path, "efusedata_mask.bin" + ) + cfg.set(img_section_name, "boot_header_file", bh_file) + cfg.set(img_section_name, "efuse_file", efuse_file) + cfg.set(img_section_name, "efuse_mask_file", efuse_mask_file) + cfg.set(img_section_name, "segdata_file", raw_bin_name) + cfg.set(img_section_name, "bootinfo_file", bootinfo_file) + cfg.set(img_section_name, "img_file", img_file) if key: - cfg.set(img_section_name, 'aes_key_org', key) + cfg.set(img_section_name, "aes_key_org", key) if iv: - cfg.set(img_section_name, 'aes_iv', iv) - cfg.write(f, 'w') + cfg.set(img_section_name, "aes_iv", iv) + cfg.write(f, "w") return f def bl_image_gen(self, bintype, raw_bin_name, key=None, iv=None, cfg_ini=None): # python bflb_img_create.py -c np -i media -s none f = self.bl_image_gen_cfg(raw_bin_name, bintype) - #exe_genitor(['bflb_img_create.exe', '-c', 'np', '-i', 'media', '-s', 'none']) - img_create = bl_img_create() + # exe_genitor(['bflb_img_create.exe', '-c', 'np', '-i', 'media', '-s', 'none']) + img_create = bl_img_create() img_create.create_sp_media_image_file(f) - def bl_fw_boot_head_gen(self, boot2, xtal, config, encrypt=False, chipname="bl60x", chiptype="bl60x", cpu_type=None): + def bl_fw_boot_head_gen( + self, + boot2, + xtal, + config, + encrypt=False, + chipname="bl60x", + chiptype="bl60x", + cpu_type=None, + ): cfg = BFConfigParser() cfg.read(config) @@ -1043,68 +1165,74 @@ def bl_fw_boot_head_gen(self, boot2, xtal, config, encrypt=False, chipname="bl60 bootheader_section_name = "BOOTHEADER_CFG" if boot2 == True: - cfg.set(bootheader_section_name, 'img_start', '0x2000') - cfg.set(bootheader_section_name, 'cache_enable', '1') - cfg.set(bootheader_section_name, 'crc_ignore', '1') - cfg.set(bootheader_section_name, 'hash_ignore', '1') - #cfg.set(bootheader_section_name,'sfctrl_clk_delay', '0') + cfg.set(bootheader_section_name, "img_start", "0x2000") + cfg.set(bootheader_section_name, "cache_enable", "1") + cfg.set(bootheader_section_name, "crc_ignore", "1") + cfg.set(bootheader_section_name, "hash_ignore", "1") + # cfg.set(bootheader_section_name,'sfctrl_clk_delay', '0') if cpu_type != None: - cfg.set(bootheader_section_name, 'halt_cpu1', '1') - cfg.set(bootheader_section_name, 'key_sel', '0') + cfg.set(bootheader_section_name, "halt_cpu1", "1") + cfg.set(bootheader_section_name, "key_sel", "0") if encrypt: - cfg.set(bootheader_section_name, 'encrypt_type', '1') + cfg.set(bootheader_section_name, "encrypt_type", "1") else: - cfg.set(bootheader_section_name, 'encrypt_type', '0') + cfg.set(bootheader_section_name, "encrypt_type", "0") - cfg.set(bootheader_section_name, 'xtal_type', dict_xtal[xtal]) + cfg.set(bootheader_section_name, "xtal_type", dict_xtal[xtal]) cfg.write(config) create = bl_efuse_boothd_gen() create.efuse_boothd_create_process(chipname, chiptype, config) - def bl_whole_flash_bin_create(self, bin_file, boot2, ro_params, pt_parcel, media, mfg, flash_opt="1M"): - + def bl_whole_flash_bin_create( + self, bin_file, boot2, ro_params, pt_parcel, media, mfg, flash_opt="1M" + ): + d_files = [] d_addrs = [] if pt_parcel == None: return False - + if boot2 == True: - d_files.append(os.path.join(app_path, bin_build_out_path, "bootinfo_boot2.bin")) + d_files.append( + os.path.join(app_path, bin_build_out_path, "bootinfo_boot2.bin") + ) d_addrs.append("00000000") d_files.append(os.path.join(app_path, bin_build_out_path, "img_boot2.bin")) d_addrs.append("00002000") - if pt_parcel != None and len(pt_parcel) > 0 and pt_parcel['pt_new'] == True: + if pt_parcel != None and len(pt_parcel) > 0 and pt_parcel["pt_new"] == True: d_files.append(os.path.join(app_path, bin_build_out_path, "partition.bin")) - d_addrs.append(hex(pt_parcel['pt_addr0'])[2:]) + d_addrs.append(hex(pt_parcel["pt_addr0"])[2:]) d_files.append(os.path.join(app_path, bin_build_out_path, "partition.bin")) - d_addrs.append(hex(pt_parcel['pt_addr1'])[2:]) + d_addrs.append(hex(pt_parcel["pt_addr1"])[2:]) - if bin_file == True and 'fw_addr' in pt_parcel: + if bin_file == True and "fw_addr" in pt_parcel: d_files.append(os.path.join(app_path, bin_build_out_path, "bootinfo.bin")) - d_addrs.append(hex(pt_parcel['fw_addr'])[2:]) + d_addrs.append(hex(pt_parcel["fw_addr"])[2:]) d_files.append(os.path.join(app_path, bin_build_out_path, "img.bin")) - d_addrs.append(hex(pt_parcel['fw_addr'] + 0x1000)[2:]) + d_addrs.append(hex(pt_parcel["fw_addr"] + 0x1000)[2:]) - if ro_params != None and len(ro_params) > 0 and pt_parcel['conf_addr'] != None: + if ro_params != None and len(ro_params) > 0 and pt_parcel["conf_addr"] != None: bl_ro_device_tree = bl_device_tree() dtb_file = os.path.join(app_path, bin_build_out_path, "ro_params.dtb") bl_ro_device_tree.bl_ro_params_device_tree(ro_params, dtb_file) d_files.append(os.path.join(app_path, bin_build_out_path, "ro_params.dtb")) - d_addrs.append(hex(pt_parcel['conf_addr'])[2:]) + d_addrs.append(hex(pt_parcel["conf_addr"])[2:]) - if media == True and pt_parcel['media_addr'] != None: + if media == True and pt_parcel["media_addr"] != None: d_files.append(os.path.join(app_path, bin_build_out_path, "media.bin")) - d_addrs.append(hex(pt_parcel['media_addr'])[2:]) + d_addrs.append(hex(pt_parcel["media_addr"])[2:]) if mfg == True: - d_files.append(os.path.join(app_path, bin_build_out_path, "bootinfo_mfg.bin")) - d_addrs.append(hex(pt_parcel['mfg_addr'])[2:]) + d_files.append( + os.path.join(app_path, bin_build_out_path, "bootinfo_mfg.bin") + ) + d_addrs.append(hex(pt_parcel["mfg_addr"])[2:]) d_files.append(os.path.join(app_path, bin_build_out_path, "img_mfg.bin")) - d_addrs.append(hex(pt_parcel['mfg_addr'] + 0x1000)[2:]) + d_addrs.append(hex(pt_parcel["mfg_addr"] + 0x1000)[2:]) if len(d_files) > 0 and len(d_addrs) > 0: cfg = BFConfigParser() @@ -1112,14 +1240,15 @@ def bl_whole_flash_bin_create(self, bin_file, boot2, ro_params, pt_parcel, media self.bl_write_flash_img(d_addrs, d_files, flash_opt) files_str = " ".join(d_files) addrs_str = " ".join(d_addrs) - cfg.set('FLASH_CFG', 'file', files_str) - cfg.set('FLASH_CFG', 'address', addrs_str) - cfg.write(eflash_loader_cfg, 'w') + cfg.set("FLASH_CFG", "file", files_str) + cfg.set("FLASH_CFG", "address", addrs_str) + cfg.write(eflash_loader_cfg, "w") return True else: return False -class bl_img_ota(): + +class bl_img_ota: def bl_mfg_ota_header(self, file_bytearray, use_xz): ota_conf = bl_find_file("ota", ".toml") parsed_toml = toml.load(ota_conf) @@ -1129,19 +1258,19 @@ def bl_mfg_ota_header(self, file_bytearray, use_xz): m = hashlib.sha256() # 16 Bytes header - data = b'BL60X_OTA_Ver1.0' + data = b"BL60X_OTA_Ver1.0" for b in data: header.append(b) # 4 Byte ota file type if use_xz: - data = b'XZ ' + data = b"XZ " else: - data = b'RAW ' + data = b"RAW " for b in data: header.append(b) # 4 Bytes file length - file_len_bytes = file_len.to_bytes(4, byteorder='little') + file_len_bytes = file_len.to_bytes(4, byteorder="little") for b in file_len_bytes: header.append(b) @@ -1186,29 +1315,44 @@ def bl_mfg_ota_header(self, file_bytearray, use_xz): def bl_mfg_ota_xz_gen(self, chipname="bl60x", chiptype="bl60x", cpu_type=None): bl60x_xz_filters = [ - { - "id": lzma.FILTER_LZMA2, - "dict_size": 32768 - }, + {"id": lzma.FILTER_LZMA2, "dict_size": 32768}, ] fw_ota_bin = bytearray() fw_ota_bin_xz = bytearray() if cpu_type == None: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/FW_OTA.bin".format(file_finally_name)) + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/FW_OTA.bin".format(file_finally_name), + ) else: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin") + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin", + ) with open(FW_OTA_path, mode="rb") as bin_f: file_bytes = bin_f.read() for b in file_bytes: fw_ota_bin.append(b) if cpu_type == None: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/FW_OTA.bin.xz".format(file_finally_name)) + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/FW_OTA.bin.xz".format(file_finally_name), + ) else: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin.xz") - with lzma.open(FW_OTA_path, mode="wb", check=lzma.CHECK_CRC32, filters=bl60x_xz_filters) as xz_f: + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin.xz", + ) + with lzma.open( + FW_OTA_path, mode="wb", check=lzma.CHECK_CRC32, filters=bl60x_xz_filters + ) as xz_f: xz_f.write(fw_ota_bin) - print("Generating BIN File to %s" %(FW_OTA_path)) + print("Generating BIN File to %s" % (FW_OTA_path)) with open(FW_OTA_path, mode="rb") as f: file_bytes = f.read() for b in file_bytes: @@ -1217,26 +1361,37 @@ def bl_mfg_ota_xz_gen(self, chipname="bl60x", chiptype="bl60x", cpu_type=None): for b in fw_ota_bin_xz: fw_ota_bin_xz_ota.append(b) if cpu_type == None: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/FW_OTA.bin.xz.ota".format(file_finally_name)) + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/FW_OTA.bin.xz.ota".format(file_finally_name), + ) else: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin.xz.ota") + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin.xz.ota", + ) with open(FW_OTA_path, mode="wb") as f: f.write(fw_ota_bin_xz_ota) - print("Generating BIN File to %s" %(FW_OTA_path)) - + print("Generating BIN File to %s" % (FW_OTA_path)) def bl_mfg_ota_bin_gen(self, chipname="bl60x", chiptype="bl60x", cpu_type=None): fw_header_len = 4096 fw_ota_bin = bytearray() ota_path = os.path.join(app_path, bin_build_out_path) - if os.path.isdir(ota_path)==False: + if os.path.isdir(ota_path) == False: os.mkdir(ota_path) if cpu_type == None: - bootinfo_fw_path = os.path.join(app_path, bin_build_out_path, "bootinfo.bin") + bootinfo_fw_path = os.path.join( + app_path, bin_build_out_path, "bootinfo.bin" + ) else: - bootinfo_fw_path = os.path.join(app_path, bin_build_out_path, "bootinfo_" + cpu_type.lower() + ".bin") + bootinfo_fw_path = os.path.join( + app_path, bin_build_out_path, "bootinfo_" + cpu_type.lower() + ".bin" + ) with open(bootinfo_fw_path, mode="rb") as f: file_bytes = f.read(4096) for b in file_bytes: @@ -1248,13 +1403,15 @@ def bl_mfg_ota_bin_gen(self, chipname="bl60x", chiptype="bl60x", cpu_type=None): if cpu_type == None: img_fw_path = os.path.join(app_path, bin_build_out_path, "img.bin") else: - img_fw_path = os.path.join(app_path, bin_build_out_path, "img_" + cpu_type.lower() + ".bin") + img_fw_path = os.path.join( + app_path, bin_build_out_path, "img_" + cpu_type.lower() + ".bin" + ) with open(img_fw_path, mode="rb") as f: file_bytes = f.read() for b in file_bytes: fw_ota_bin.append(b) fw_ota_bin_header = self.bl_mfg_ota_header(fw_ota_bin, use_xz=0) - + FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota") if not os.path.exists(FW_OTA_path): os.makedirs(FW_OTA_path) @@ -1267,29 +1424,38 @@ def bl_mfg_ota_bin_gen(self, chipname="bl60x", chiptype="bl60x", cpu_type=None): FW_OTA_path = os.path.join(FW_OTA_path, cpu_type + "_OTA.bin") with open(FW_OTA_path, mode="wb") as f: f.write(fw_ota_bin) - print("Generating BIN File to %s" %(FW_OTA_path)) + print("Generating BIN File to %s" % (FW_OTA_path)) for b in fw_ota_bin: fw_ota_bin_header.append(b) if cpu_type == None: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/FW_OTA.bin.ota".format(file_finally_name)) + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/FW_OTA.bin.ota".format(file_finally_name), + ) else: - FW_OTA_path = os.path.join(app_path, bin_build_out_path, "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin.ota") + FW_OTA_path = os.path.join( + app_path, + bin_build_out_path, + "ota/{}/".format(file_finally_name) + cpu_type + "_OTA.bin.ota", + ) with open(FW_OTA_path, mode="wb") as f: f.write(fw_ota_bin_header) - print("Generating BIN File to %s" %(FW_OTA_path)) + print("Generating BIN File to %s" % (FW_OTA_path)) self.bl_mfg_ota_xz_gen(chipname, chiptype, cpu_type) -class bl_flash_select(): + +class bl_flash_select: def get_suitable_file_name(self, cfg_dir, flash_id): conf_files = [] for home, dirs, files in os.walk(cfg_dir): for filename in files: - if filename.split('_')[-1] == flash_id + '.conf': + if filename.split("_")[-1] == flash_id + ".conf": conf_files.append(filename) if len(conf_files) > 1: for i in range(len(conf_files)): - tmp = conf_files[i].split('.')[0] + tmp = conf_files[i].split(".")[0] print("%d:%s" % (i + 1, tmp)) return conf_files[i] elif len(conf_files) == 1: @@ -1297,14 +1463,16 @@ def get_suitable_file_name(self, cfg_dir, flash_id): else: return "" - def update_flash_cfg_do(self, chipname, chiptype, flash_id, file=None, create=False, section=None): + def update_flash_cfg_do( + self, chipname, chiptype, flash_id, file=None, create=False, section=None + ): cfg_dir = os.path.join(os.getcwd(), chiptype, "flash_select") conf_name = self.get_suitable_file_name(cfg_dir, flash_id) print(os.path.join(cfg_dir, conf_name)) value_key = [] if os.path.isfile(os.path.join(cfg_dir, conf_name)) == False: return False - fp = open(os.path.join(cfg_dir, conf_name), 'r') + fp = open(os.path.join(cfg_dir, conf_name), "r") for line in fp.readlines(): value = line.split("=")[0].strip() if value == "[FLASH_CFG]": @@ -1316,7 +1484,9 @@ def update_flash_cfg_do(self, chipname, chiptype, flash_id, file=None, create=Fa cfg2 = BFConfigParser() cfg2.read(file) for i in range(len(value_key)): - if cfg1.has_option("FLASH_CFG", value_key[i]) and cfg2.has_option(section, value_key[i]) : + if cfg1.has_option("FLASH_CFG", value_key[i]) and cfg2.has_option( + section, value_key[i] + ): tmp_value = cfg1.get("FLASH_CFG", value_key[i]) bflb_utils = bl_utils() bflb_utils.Update_Cfg(cfg2, section, value_key[i], tmp_value) @@ -1324,14 +1494,16 @@ def update_flash_cfg_do(self, chipname, chiptype, flash_id, file=None, create=Fa cfg2.write(file, "w+") def bl_flash_loader_list(self, chipname, chiptype, bh_cfg_file): - eflash_loader_cfg = os.path.join(app_path, bin_build_out_path, "eflash_loader_cfg.ini") + eflash_loader_cfg = os.path.join( + app_path, bin_build_out_path, "eflash_loader_cfg.ini" + ) cfg = BFConfigParser() cfg.read(eflash_loader_cfg) if cfg.has_option("FLASH_CFG", "flash_id"): flash_id_str = cfg.get("FLASH_CFG", "flash_id") if type(flash_id_str) is str: - flash_id_list = flash_id_str.split(',') + flash_id_list = flash_id_str.split(",") return flash_id_list elif type(flash_id_str) is list: return flash_id_str @@ -1352,47 +1524,86 @@ def bl_flash_loader_list(self, chipname, chiptype, bh_cfg_file): def bl_flash_update(self, chipname, chiptype, bh_cfg_file, flash_id): print("========= chip flash id: %s =========" % flash_id) if chiptype == "bl602": - if self.update_flash_cfg_do(chipname, chiptype, flash_id, bh_cfg_file, False, "BOOTHEADER_CFG") == False: + if ( + self.update_flash_cfg_do( + chipname, chiptype, flash_id, bh_cfg_file, False, "BOOTHEADER_CFG" + ) + == False + ): error = "flash_id:" + flash_id + " do not support" return error elif chiptype == "bl60x": - if self.update_flash_cfg_do(chipname, chiptype, flash_id, bh_cfg_file, False, - "BOOTHEADER_CPU0_CFG") == False: + if ( + self.update_flash_cfg_do( + chipname, + chiptype, + flash_id, + bh_cfg_file, + False, + "BOOTHEADER_CPU0_CFG", + ) + == False + ): error = "flash_id:" + flash_id + " do not support" return error def bl_flash_loader(self, chipname, chiptype, bh_cfg_file): - eflash_loader_cfg = os.path.join(app_path, bin_build_out_path, "eflash_loader_cfg.ini") + eflash_loader_cfg = os.path.join( + app_path, bin_build_out_path, "eflash_loader_cfg.ini" + ) cfg = BFConfigParser() cfg.read(eflash_loader_cfg) if cfg.has_option("FLASH_CFG", "flash_id"): flash_id_str = cfg.get("FLASH_CFG", "flash_id") - flash_id_list = flash_id_str.split(',') - print('++++++++') + flash_id_list = flash_id_str.split(",") + print("++++++++") print(flash_id_list, type(flash_id_list)) for flash_id in flash_id_list: print(flash_id) print("========= chip flash id: %s =========" % flash_id) if chiptype == "bl602": - if self.update_flash_cfg_do(chipname, chiptype, flash_id, bh_cfg_file, False, "BOOTHEADER_CFG") == False: - error = "flash_id:" + flash_id + " do not support" + if ( + self.update_flash_cfg_do( + chipname, + chiptype, + flash_id, + bh_cfg_file, + False, + "BOOTHEADER_CFG", + ) + == False + ): + error = "flash_id:" + flash_id + " do not support" return error elif chiptype == "bl60x": - if self.update_flash_cfg_do(chipname, chiptype, flash_id, bh_cfg_file, False, "BOOTHEADER_CPU0_CFG") == False: - error = "flash_id:" + flash_id + " do not support" + if ( + self.update_flash_cfg_do( + chipname, + chiptype, + flash_id, + bh_cfg_file, + False, + "BOOTHEADER_CPU0_CFG", + ) + == False + ): + error = "flash_id:" + flash_id + " do not support" return error else: error = "Do not find flash_id in eflash_loader_cfg.ini" return error -if __name__ == '__main__': - abs_path = os.path.abspath('..') + +if __name__ == "__main__": + abs_path = os.path.abspath("..") app_path = os.path.join(abs_path, "customer_app", sys.argv[1]) demo_name = sys.argv[1] chip_name = sys.argv[2].lower() default_conf_path = chip_name eflash_loader_cfg_org = bl_find_file("eflash_loader_cfg", ".conf") - eflash_loader_cfg = os.path.join(app_path, bin_build_out_path, "eflash_loader_cfg.ini") + eflash_loader_cfg = os.path.join( + app_path, bin_build_out_path, "eflash_loader_cfg.ini" + ) shutil.copy(eflash_loader_cfg_org, eflash_loader_cfg) # 找到efuse_bootheader_cfg @@ -1409,49 +1620,56 @@ def bl_flash_loader(self, chipname, chiptype, bh_cfg_file): ro_list = bl_find_file_list(bl_factory_params_file_prefix, ".dts") img_boot2_file_list = bl_find_file_list("blsp_boot2_", ".bin") - arrange_group_list = list(itertools.product(pt_file_list, ro_list, img_boot2_file_list, flashid_list)) + arrange_group_list = list( + itertools.product(pt_file_list, ro_list, img_boot2_file_list, flashid_list) + ) for group in arrange_group_list: # 找到partition # pt_file = bl_find_file("partition_cfg_", ".toml") pt_file = group[0] pt_name = pt_file.split("partition_cfg_") - pt_name = pt_name[1].split('.toml') - pt_name = 'pt{}'.format(pt_name[0]) + pt_name = pt_name[1].split(".toml") + pt_name = "pt{}".format(pt_name[0]) pt_helper = PtCreater(pt_file) - pt_helper.create_pt_table(os.path.join(app_path, bin_build_out_path, "partition.bin")) + pt_helper.create_pt_table( + os.path.join(app_path, bin_build_out_path, "partition.bin") + ) pt_parcel = pt_helper.construct_table() # flashid flash_sele.bl_flash_update(chip_name, chip_name, f, group[3]) flash_id_name = group[3] - #找到device_tree + # 找到device_tree # ro = bl_find_file(bl_factory_params_file_prefix, ".dts") ro = group[1] xtal = ro.split("IoTKitA_") xtal = xtal[1].split(".dts") xtal = xtal[0] - dts_name = 'dts{}'.format(xtal) + dts_name = "dts{}".format(xtal) img_gen = bl_whole_img_generate() img_gen.bl_fw_boot_head_gen(True, xtal, f, False, chip_name, chip_name) - #找到boot2 + # 找到boot2 # img_boot2_file = bl_find_file("blsp_boot2_", ".bin") img_boot2_file = group[2] boot2_name = img_boot2_file.split("blsp_boot2_") - boot2_name = boot2_name[1].split('.bin') - boot2_name = 'boot2{}'.format(boot2_name[0]) + boot2_name = boot2_name[1].split(".bin") + boot2_name = "boot2{}".format(boot2_name[0]) - file_finally_name = '{}_{}_{}_{}'.format(dts_name, pt_name, boot2_name, flash_id_name) + file_finally_name = "{}_{}_{}_{}".format( + dts_name, pt_name, boot2_name, flash_id_name + ) img_gen.bl_image_gen("boot2", img_boot2_file) img_gen.bl_fw_boot_head_gen(False, xtal, f, False, chip_name, chip_name) - img_gen.bl_image_gen("fw", os.path.join(app_path, "build_out", demo_name + ".bin")) + img_gen.bl_image_gen( + "fw", os.path.join(app_path, "build_out", demo_name + ".bin") + ) img_ota = bl_img_ota() img_ota.bl_mfg_ota_bin_gen(chip_name, chip_name, None) img_gen.bl_whole_flash_bin_create(True, True, ro, pt_parcel, None, None, "2M") -