예제 #1
0
def bootheader_update_flash_pll_crc(bootheader_data):
    flash_cfg_start = 8
    flash_cfg_len = 92
    flash_cfg = bootheader_data[flash_cfg_start + 4:flash_cfg_start + flash_cfg_len - 4]
    crcarray = bflb_utils.get_crc32_bytearray(flash_cfg)
    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 = 16
    pll_cfg = bootheader_data[pll_cfg_start + 4:pll_cfg_start + pll_cfg_len - 4]
    crcarray = bflb_utils.get_crc32_bytearray(pll_cfg)
    bootheader_data[pll_cfg_start + pll_cfg_len - 4:pll_cfg_start + pll_cfg_len] = crcarray
    return bootheader_data
def create_partition_table(values):
    entry_table = bytearray(36 * entry_max)
    entry_cnt = 0
    for i in range(entry_max):
        entry_type = values[('entry' + str(i + 1) + '_type')]
        entry_name = values[('entry' + str(i + 1) + '_name')]
        entry_addr0 = values[('entry' + str(i + 1) + '_addr0')]
        entry_addr1 = values[('entry' + str(i + 1) + '_addr1')]
        entry_maxlen0 = values[('entry' + str(i + 1) + '_maxlen0')]
        entry_maxlen1 = values[('entry' + str(i + 1) + '_maxlen1')]
        if entry_type != '':
            entry_table[36 * entry_cnt + 0] = bflb_utils.int_to_2bytearray_l(
                int(entry_type))[0]
            if len(entry_name) >= 8:
                return 'Entry name is too long!'
            else:
                entry_table[36 * entry_cnt + 3:36 * entry_cnt + 3 +
                            len(entry_name)] = bflb_utils.get_byte_array(
                                entry_name)
                entry_table[36 * entry_cnt + 12:36 * entry_cnt +
                            16] = bflb_utils.int_to_4bytearray_l(
                                int(entry_addr0, 16))
                entry_table[36 * entry_cnt + 16:36 * entry_cnt +
                            20] = bflb_utils.int_to_4bytearray_l(
                                int(entry_addr1, 16))
                entry_table[36 * entry_cnt + 20:36 * entry_cnt +
                            24] = bflb_utils.int_to_4bytearray_l(
                                int(entry_maxlen0, 16))
                entry_table[36 * entry_cnt + 24:36 * entry_cnt +
                            28] = bflb_utils.int_to_4bytearray_l(
                                int(entry_maxlen1, 16))
                entry_cnt += 1

    pt_table = bytearray(16)
    pt_table[0] = 66
    pt_table[1] = 70
    pt_table[2] = 80
    pt_table[3] = 84
    pt_table[6:8] = bflb_utils.int_to_2bytearray_l(int(entry_cnt))
    pt_table[12:16] = bflb_utils.get_crc32_bytearray(pt_table[0:12])
    entry_table[36 * entry_cnt:36 * entry_cnt +
                4] = bflb_utils.get_crc32_bytearray(entry_table[0:36 *
                                                                entry_cnt])
    data = pt_table + entry_table[0:36 * entry_cnt + 4]
    fp = open(partition_path, 'wb+')
    fp.write(data)
    fp.close()
    return True
예제 #3
0
def img_create_read_file_append_crc(file, crc):
    fp = open(file, 'rb')
    read_data = bytearray(fp.read())
    crcarray = bytearray(0)
    if crc:
        crcarray = bflb_utils.get_crc32_bytearray(read_data)
    fp.close()
    return read_data + crcarray
예제 #4
0
    def __create_pt_table_do(self, lists, file):
        entry_table = bytearray(36 * self.entry_max)
        entry_cnt = 0
        for item in lists:
            entry_type = item['type']
            entry_name = item['name']
            entry_device = item['device']
            entry_addr0 = item['address0']
            entry_addr1 = item['address1']
            entry_maxlen0 = item['size0']
            entry_maxlen1 = item['size1']
            entry_len = item['len']
            entry_table[36 * entry_cnt +
                        0] = bflb_utils.int_to_2bytearray_l(entry_type)[0]
            if len(entry_name) >= 8:
                bflb_utils.printf('Entry name is too long!')
                return False
            else:
                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] = bflb_utils.int_to_4bytearray_l(entry_addr0)
                entry_table[36 * entry_cnt + 16:36 * entry_cnt +
                            20] = bflb_utils.int_to_4bytearray_l(entry_addr1)
                entry_table[36 * entry_cnt + 20:36 * entry_cnt +
                            24] = bflb_utils.int_to_4bytearray_l(entry_maxlen0)
                entry_table[36 * entry_cnt + 24:36 * entry_cnt +
                            28] = bflb_utils.int_to_4bytearray_l(entry_maxlen1)
                entry_cnt += 1

        pt_table = bytearray(16)
        pt_table[0] = 66
        pt_table[1] = 70
        pt_table[2] = 80
        pt_table[3] = 84
        pt_table[6:8] = bflb_utils.int_to_2bytearray_l(int(entry_cnt))
        pt_table[12:16] = bflb_utils.get_crc32_bytearray(pt_table[0:12])
        entry_table[36 * entry_cnt:36 * entry_cnt +
                    4] = bflb_utils.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
예제 #5
0
def check_pt_data(data):
    if partition_magic_code != bflb_utils.bytearray_to_int(data[0:4]):
        bflb_utils.printf('partition bin magic check fail ', binascii.hexlify(data[0:4]))
        return (False, 0, 0)
    table_count = bflb_utils.bytearray_to_int(data[6:7]) + (bflb_utils.bytearray_to_int(data[7:8]) << 8)
    if table_count > 16:
        bflb_utils.printf('error, pt enter size > 16')
        return (False, 0, 0)
    crcarray = bflb_utils.get_crc32_bytearray(data[:12])
    if data[12:16] != crcarray:
        bflb_utils.printf('pt table crc fail ', binascii.hexlify(crcarray))
        return (False, 0, 0)
    crcarray = bflb_utils.get_crc32_bytearray(data[16:16 + 36 * table_count])
    if data[16 + 36 * table_count:16 + 36 * table_count + 4] != crcarray:
        bflb_utils.printf('pt entries crc fail ', binascii.hexlify(crcarray))
        return (False, 0, 0)
    age = bflb_utils.bytearray_to_int(data[8:9]) + (bflb_utils.bytearray_to_int(data[9:10]) << 8) + (bflb_utils.bytearray_to_int(data[10:11]) << 16) + (bflb_utils.bytearray_to_int(data[11:12]) << 24)
    return (
     True, table_count, age)
예제 #6
0
def img_create_update_bootheader(bootheader_data, hash, seg_cnt):
    bootheader_data[120:124] = bflb_utils.int_to_4bytearray_l(seg_cnt)
    sign, encrypt, key_sel = img_create_get_sign_encrypt_info(bootheader_data)
    if img_create_get_hash_ignore(bootheader_data) == 1 and sign == 0:
        bflb_utils.printf('Hash ignored')
    else:
        bootheader_data[132:164] = hash
    if img_create_get_crc_ignore(bootheader_data) == 1:
        bflb_utils.printf('Header crc ignored')
    else:
        hd_crcarray = bflb_utils.get_crc32_bytearray(bootheader_data[0:172])
        bootheader_data[172:176] = hd_crcarray
        bflb_utils.printf('Header crc: ', binascii.hexlify(hd_crcarray))
    return bootheader_data[0:176]
예제 #7
0
def sign_process(data_tohash, file, privatekey_file_uecc, publickey_file, signer):
    fp = open_file(file, 'rb')
    header = bytearray(fp.read())
    header = header + bytearray(0)
    fp.close()
    pk_data, pk_hash, signature_field = img_create_sign_data(data_tohash, privatekey_file_uecc, publickey_file)
    pk_data = pk_data + bflb_utils.get_crc32_bytearray(pk_data)
    pk_pos = 244
    header[pk_pos:pk_pos + 64 + 4] = pk_data
    sign_pos = 384
    header[sign_pos:sign_pos + 4 + 64 + 4] = signature_field
    fp = open_file(file, 'wb+')
    fp.write(header)
    fp.close()
    mask_4bytes = bytearray.fromhex('FFFFFFFF')
    if signer == 'cpu0':
        bflb_utils.printf("CPU0 is signer, add signature for CPU1's image")
        fp = open_file(cfg.get('Img_CPU0_Cfg', 'efuse_file'), 'rb')
        efuse_data = bytearray(fp.read()) + bytearray(0)
        fp.close()
        fp = open_file(cfg.get('Img_CPU0_Cfg', 'efuse_mask_file'), 'rb')
        efuse_mask_data = bytearray(fp.read()) + bytearray(0)
        fp.close()
        efuse_data[keyslot0:keyslot2] = pk_hash
        efuse_mask_data[keyslot0:keyslot2] = mask_4bytes * 8
        fp = open_file(cfg.get('Img_CPU0_Cfg', 'efuse_file'), 'wb+')
        fp.write(efuse_data)
        fp.close()
        fp = open_file(cfg.get('Img_CPU0_Cfg', 'efuse_mask_file'), 'wb+')
        fp.write(efuse_mask_data)
        fp.close()
    else:
        bflb_utils.printf("CPU1 is signer, add signature for CPU0's image")
        fp = open_file(cfg.get('Img_CPU1_Cfg', 'efuse_file'), 'rb')
        efuse_data = bytearray(fp.read()) + bytearray(0)
        fp.close()
        fp = open_file(cfg.get('Img_CPU1_Cfg', 'efuse_mask_file'), 'rb')
        efuse_mask_data = bytearray(fp.read()) + bytearray(0)
        fp.close()
        efuse_data[keyslot5:keyslot5 + 16] = pk_hash[0:16]
        efuse_mask_data[keyslot5:keyslot5 + 16] = mask_4bytes * 4
        efuse_data[keyslot6:keyslot7] = pk_hash[16:32]
        efuse_mask_data[keyslot6:keyslot7] = mask_4bytes * 4
        fp = open_file(cfg.get('Img_CPU1_Cfg', 'efuse_file'), 'wb+')
        fp.write(efuse_data)
        fp.close()
        fp = open_file(cfg.get('Img_CPU1_Cfg', 'efuse_mask_file'), 'wb+')
        fp.write(efuse_mask_data)
        fp.close()
def img_create_sign_data(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()
    bflb_utils.printf('Private key: ', binascii.hexlify(sk.to_string()))
    bflb_utils.printf('Public key: ', binascii.hexlify(pk_data))
    pk_hash = img_create_sha256_data(pk_data)
    bflb_utils.printf('Public key hash=', binascii.hexlify(pk_hash))
    signature = sk.sign(data_bytearray, hashfunc=(hashlib.sha256), sigencode=(ecdsa.util.sigencode_string))
    bflb_utils.printf('Signature=', binascii.hexlify(signature))
    len_array = bflb_utils.int_to_4bytearray_l(len(signature))
    sig_field = len_array + signature
    crcarray = bflb_utils.get_crc32_bytearray(sig_field)
    return (
     pk_data, pk_hash, sig_field + crcarray)
예제 #9
0
def img_creat_process(flash_img):
    global encrypt_key
    encrypt_blk_size = 16
    padding = bytearray(encrypt_blk_size)
    data_tohash = bytearray(0)
    ret = 'OK'
    cfg_section = 'Img_Cfg'
    segheader_file = []
    if flash_img == 0:
        for files in cfg.get(cfg_section, 'segheader_file').split(' '):
            segheader_file.append(str(files))

    segdata_file = []
    for files in cfg.get(cfg_section, 'segdata_file').split(' '):
        segdata_file.append(str(files))
        if flash_img == 1:
            break

    boot_header_file = cfg.get(cfg_section, 'boot_header_file')
    bootheader_data = img_create_read_file_append_crc(boot_header_file, 0)
    encrypt = 0
    sign, encrypt, key_sel = img_create_get_sign_encrypt_info(bootheader_data)
    aesiv_data = bytearray(0)
    pk_data = bytearray(0)
    if sign != 0:
        bflb_utils.printf('Image need sign')
        publickey_file = cfg.get(cfg_section, 'publickey_file')
        privatekey_file_uecc = cfg.get(cfg_section, 'privatekey_file_uecc')
    if encrypt != 0:
        bflb_utils.printf('Image need encrypt ', encrypt)
        encrypt_key_org = bflb_utils.hexstr_to_bytearray(
            cfg.get(cfg_section, 'aes_key_org'))
        if encrypt == 1:
            encrypt_key = encrypt_key_org[0:16]
        elif encrypt == 2:
            encrypt_key = encrypt_key_org[0:32]
        elif encrypt == 3:
            encrypt_key = encrypt_key_org[0:24]
        bflb_utils.printf('Key= ', binascii.hexlify(encrypt_key))
        encrypt_iv = bflb_utils.hexstr_to_bytearray(
            cfg.get(cfg_section, 'aes_iv'))
        iv_crcarray = bflb_utils.get_crc32_bytearray(encrypt_iv)
        aesiv_data = encrypt_iv + iv_crcarray
        data_tohash = data_tohash + aesiv_data
    seg_cnt = len(segheader_file)
    if flash_img == 0:
        if seg_cnt != len(segdata_file):
            bflb_utils.printf('Segheader count and segdata count not match')
            return ('FAIL', data_tohash)
    data_toencrypt = bytearray(0)
    if flash_img == 0:
        i = 0
        seg_header_list = []
        seg_data_list = []
        while i < seg_cnt:
            seg_data = 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
                seg_data += padding[0:padding_size]
            else:
                segdata_crcarray = bflb_utils.get_crc32_bytearray(seg_data)
                seg_data_list.append(seg_data)
                seg_header = img_create_read_file_append_crc(
                    segheader_file[i], 0)
                seg_header = img_create_update_segheader(
                    seg_header, bflb_utils.int_to_4bytearray_l(len(seg_data)),
                    segdata_crcarray)
                segheader_crcarray = bflb_utils.get_crc32_bytearray(seg_header)
                seg_header = seg_header + segheader_crcarray
                seg_header_list.append(seg_header)
                i = i + 1

        i = 0
        while i < seg_cnt:
            data_toencrypt += seg_header_list[i]
            data_toencrypt += seg_data_list[i]
            i += 1

    else:
        seg_data = img_create_read_file_append_crc(segdata_file[0], 0)
        padding_size = 0
        if len(seg_data) % encrypt_blk_size != 0:
            padding_size = encrypt_blk_size - len(seg_data) % encrypt_blk_size
            seg_data += padding[0:padding_size]
        data_toencrypt += seg_data
        seg_cnt = len(data_toencrypt)
    if encrypt != 0:
        data_toencrypt = img_create_encrypt_data(data_toencrypt, encrypt_key,
                                                 encrypt_iv, flash_img)
    fw_data = bytearray(0)
    data_tohash += data_toencrypt
    fw_data = data_toencrypt
    hash = img_create_sha256_data(data_tohash)
    bflb_utils.printf('Image hash is ', binascii.hexlify(hash))
    bootheader_data = img_create_update_bootheader(bootheader_data, hash,
                                                   seg_cnt)
    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_data + bflb_utils.get_crc32_bytearray(pk_data)
    if flash_img == 1:
        bflb_utils.printf('Write flash img')
        bootinfo_file_name = cfg.get(cfg_section, 'bootinfo_file')
        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.write(fw_data)
        fp.close()
        fw_data_hash = img_create_sha256_data(fw_data)
        fp = open(fw_file_name.replace('.bin', '_withhash.bin'), 'wb+')
        fp.write(fw_data + fw_data_hash)
        fp.close()
        if encrypt != 0:
            if encrypt == 1:
                img_update_efuse(
                    sign, pk_hash, 1,
                    encrypt_key + bytearray(32 - len(encrypt_key)), key_sel,
                    None)
            if encrypt == 2:
                img_update_efuse(
                    sign, pk_hash, 3,
                    encrypt_key + bytearray(32 - len(encrypt_key)), key_sel,
                    None)
            if encrypt == 3:
                img_update_efuse(
                    sign, pk_hash, 2,
                    encrypt_key + bytearray(32 - len(encrypt_key)), key_sel,
                    None)
        else:
            img_update_efuse(sign, pk_hash, encrypt, None, key_sel, None)
    else:
        bflb_utils.printf('Write if img')
        whole_img_file_name = cfg.get(cfg_section, 'whole_img_file')
        fp = open(whole_img_file_name, 'wb+')
        img_data = bootheader_data + pk_data + signature + aesiv_data + fw_data
        fp.write(img_data)
        fp.close()
        if encrypt != 0:
            if encrypt == 1:
                img_update_efuse(
                    sign, pk_hash, 1, None, key_sel,
                    encrypt_key + bytearray(32 - len(encrypt_key)))
            if encrypt == 2:
                img_update_efuse(
                    sign, pk_hash, 3, None, key_sel,
                    encrypt_key + bytearray(32 - len(encrypt_key)))
            if encrypt == 3:
                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)