#!/usr/bin/env python3 # coding=utf-8 # ----------------------------------------------------------------------------------------- #Purpose:part of hupg build scripts #Copyright CompanyNameMagicTag 2018-2019.All rights reserved #Author: CompanyName #------------------------------------------------------------------------------------------ import os, time, string, re ,shutil, time, hashlib, binascii, sys,subprocess import copy import struct from ctypes import * from pkt_cfg_parser import PktCfgParser from compress import encode_file_with_lzma1 import base64 import os from Crypto import Random from Crypto.Hash import SHA from Crypto.Hash import SHA256 from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5 from Crypto.Signature import PKCS1_PSS as Signature_pss ###############################Defining basic types############################################### g_support_secure_upg=None g_support_partition_info=None g_root_dir=None TD_CHAR=c_char TD_S8=c_byte TD_U8=c_ubyte TD_S16=c_short TD_U16=c_ushort TD_S32=c_int UAPI_U32=c_uint class UAPI_PARTITION_VER_S(Structure): _fields_ = [ ("boot_support",UAPI_U32,1), ("reserve",UAPI_U32,10), ("ver_num",UAPI_U32,5), ("bit_map",UAPI_U32,16), ] class uapi_upg_secure_info_s(Structure): _fields_ = [ ("hash_alg",UAPI_U32,16), ("sign_alg",UAPI_U32,6), ("sign_param",UAPI_U32,10), ("signature_1",TD_U8*256), ("signature_2",TD_U8*256), ("key_n_2",TD_U8*256), ] class uapi_21_upg_tail_s(Structure): _fields_ = [ ("file_len",UAPI_U32), ("head_end_magic",UAPI_U32), ] class uapi_21_upg_head_section_s(Structure): _fields_ = [ ("attribute_compress",UAPI_U32,1), ("attribute_pad",UAPI_U32,31), ("offset",UAPI_U32), ("compress_size", UAPI_U32), ] class uapi_21_upg_head_product_s(Structure): _fields_ = [ ("min_id",TD_U16), ("max_id",TD_U16), ] class uapi_21_upg_head_s(Structure):# Upgrade file structure _fields_ = [ ("magic", UAPI_U32), ("check_sum", TD_U8*32),# Does not include check_sum itself ("file_len", UAPI_U32), ("ver_magic", UAPI_U32), ("attribute_pad", UAPI_U32), ("manufacture_code", TD_U16), ("struct_type", TD_U8), ("file_type", TD_U8), ("product_list", uapi_21_upg_head_product_s*2), ("section_count", TD_U8), ("pad", TD_U8*3), ("section_list_offset", UAPI_U32), ("section_list_size", UAPI_U32), ("secure_info_offset", UAPI_U32), ("secure_info_size", UAPI_U32), ("partition_info_offset", UAPI_U32), ("partition_info_size", UAPI_U32), ("partion_ver", UAPI_PARTITION_VER_S), ] class uapi_start_tbl_section_s(Structure): _fields_ = [ ("offset", UAPI_U32), ("size", UAPI_U32), ("check_sum", UAPI_U32), ] class uapi_start_tbl_s(Structure):# Startup configuration area structure _fields_ = [ ("check_sum", UAPI_U32),# Does not include check_sum itself ("tbl_len", UAPI_U32), ("ver_magic", UAPI_U32), ("attribute_check_nvfile", UAPI_U32,1), ("attribute_check_nvfile_ver", UAPI_U32,1), ("attribute_pad_1", UAPI_U32,30), ("tbl_type",TD_U8), ("pad", TD_U8*3), ("info_2_kernel", UAPI_U32), ("secure_info_offset", UAPI_U32), ("secure_info_size", UAPI_U32), ("sctions", uapi_start_tbl_section_s*1), ] class UAPI_TEST_HUPG_HEAD(Structure):# Startup configuration area structure _fields_ = [ ("magic", UAPI_U32),# Does not include check_sum itself ("len", UAPI_U32), ] def fake_random(x): bin=bytes(32) return bin def lzma_compress_bin(src_file, dst_file): encode_file_with_lzma1(dst_file, src_file) def print_secure_head(secure_head_bin): global g_support_secure_upg if g_support_secure_upg==1: st_secure_head=uapi_upg_secure_info_s.from_buffer(secure_head_bin) print('[secure_head][hash_alg=0x%x][sign_alg=0x%x][sign_param=0x%x]' %(st_secure_head.hash_alg,st_secure_head.sign_alg,st_secure_head.sign_param)) print('[secure_head][signature_1[0,1,254,255]]=[0x%x][0x%x][0x%x][0x%x]' %(st_secure_head.signature_1[0],st_secure_head.signature_1[1],st_secure_head.signature_1[254],st_secure_head.signature_1[255])) print('[secure_head][signature_2[0,1,254,255]]=[0x%x][0x%x][0x%x][0x%x]' %(st_secure_head.signature_2[0],st_secure_head.signature_2[1],st_secure_head.signature_2[254],st_secure_head.signature_2[255])) print('[secure_head][key_n_2[0,1,254,255]]=[0x%x][0x%x][0x%x][0x%x]' %(st_secure_head.key_n_2[0],st_secure_head.key_n_2[1],st_secure_head.key_n_2[254],st_secure_head.key_n_2[255])) def print_upg_info(image_file,hupg_bin): log_hupg_tail_bin=hupg_bin[-8:] st_hupg=uapi_21_upg_head_s.from_buffer(hupg_bin) st_upg_tail=uapi_21_upg_tail_s.from_buffer(log_hupg_tail_bin) print("-------------%s image_info:"%(image_file)) print("[check_sum[0,1,30,31]=[0x%x][0x%x][0x%x][0x%x]]"%(st_hupg.check_sum[0],st_hupg.check_sum[1],st_hupg.check_sum[30],st_hupg.check_sum[31])) print("[attribute_pad=0x%x]"%(st_hupg.attribute_pad)) print("[file_len=0x%x][magic=0x%x][ver_magic=0x%x][manufacture_code=0x%x]"%(st_hupg.file_len,st_hupg.magic,st_hupg.ver_magic,st_hupg.manufacture_code)) print("[struct_type=0x%x][file_type=0x%x][section_count=0x%x]"%(st_hupg.struct_type,st_hupg.file_type,st_hupg.section_count)) print("[product_type[0x%x,0x%x]][product_type[0x%x,0x%x]]"%(st_hupg.product_list[0].min_id,st_hupg.product_list[0].max_id,st_hupg.product_list[1].min_id,st_hupg.product_list[1].max_id)) print("[section_list_offset=0x%x][section_list_size=0x%x]"%(st_hupg.section_list_offset,st_hupg.section_list_size)) print("[secure_info_offset=0x%x][secure_info_size=0x%x]"%(st_hupg.secure_info_offset,st_hupg.secure_info_size)) print("[partition_info_offset=0x%x][partition_info_size=0x%x]"%(st_hupg.partition_info_offset,st_hupg.partition_info_size)) print("[head_end_magic=0x%x][end_file_len=0x%x]"%(st_upg_tail.head_end_magic,st_upg_tail.file_len)) print("[partion_ver][boot_support=0x%x][ver_num=0x%x][bit_map=0x%x][reserve=0x%x]" %(st_hupg.partion_ver.boot_support,st_hupg.partion_ver.ver_num,st_hupg.partion_ver.bit_map,st_hupg.partion_ver.reserve)) # Print security head print_secure_head(hupg_bin[st_hupg.secure_info_offset : st_hupg.secure_info_offset+st_hupg.secure_info_size]) section_list_offset=st_hupg.section_list_offset i=0 while i (ptb['PRODUCT_CFG_PARTITION_UPG_SIZE'] + 4)))): sys.exit("[ERR]upg size>PRODUCT_CFG_PARTITION_UPG_SIZE FROM:%s"%os.path.realpath(__file__)) # Fill partition_info if(g_support_partition_info==1) and partition_info_bin!=None: hupg_bin[partition_info_offset:partition_info_offset+partition_info_size]=partition_info_bin # Fill the description header & content of the subsection i=0 while iPRODUCT_CFG_PARTITION_KERNEL_SIZE FROM:%s"%os.path.realpath(__file__)) section_bin_list=[] section_bin_list.append(tbl_kernel_z_bin) section_bin_list.append(nv_z_bin) section_compress_list = [] section_compress_list.append(1) section_compress_list.append(1) hupg_bin=make_upg_only(cfg_parser,root_dir,1,section_bin_list,section_compress_list,image_file,magic, ver_magic,manufacture_code,product_list_1,product_list_2,None, role_type) print_upg_info(image_file,hupg_bin) print_tbl_info(image_file,tbl_and_kernel_bin) with open(image_file,'wb+') as fp: fp.write(hupg_bin) return def make_hbin(root_dir,flash_boot_file, factory_nv_file, normal_nv_file, kernel_file, image_file, role_type, product_ver, ver_magic, app_name): print("make_hbin flash_boot_file=",flash_boot_file) print("make_hbin factory_nv_file=",factory_nv_file) print("make_hbin normal_nv_file=",normal_nv_file) print("make_hbin kernel_file=",kernel_file) print("make_hbin image_file=",image_file) print("make_hbin role_type=",role_type) print("make_hbin product_ver=",product_ver) print("make_hbin ver_magic=",ver_magic) cfg_parser = PktCfgParser(root_dir, app_name) cfg_parser.read() pt_cfg = cfg_parser.get_partition_table_settings() image_dir=os.path.dirname(image_file) tmp_dir=os.path.join(image_dir,'tmp') (kernel_base_name,str_temp)=os.path.splitext(os.path.basename(kernel_file)) (nv_base_name,str_temp)=os.path.splitext(os.path.basename(normal_nv_file)) tbl_kernel_file = os.path.join(tmp_dir,'%s_tbl.bin'%kernel_base_name)# NV compressed file path tbl_kernel_z_file = os.path.join(tmp_dir,'%s_tbl.bin.z'%kernel_base_name)# NV compressed file path ptl = pt_cfg[role_type.lower()] with open(flash_boot_file,'rb') as fp: flash_boot_bin=fp.read() if len(flash_boot_bin)>ptl['PRODUCT_CFG_PARTITION_BOOT_SIZE']: sys.exit("[ERR]boot size>PRODUCT_CFG_PARTITION_BOOT_SIZE FROM:%s"%os.path.realpath(__file__)) with open(factory_nv_file,'rb') as fp: factory_nv_bin=fp.read() with open(normal_nv_file,'rb') as fp: normal_nv_bin=fp.read() with open(tbl_kernel_file,'rb') as fp: tbl_kernel_bin=fp.read() par_boot_st_addr=ptl['PRODUCT_CFG_PARTITION_BOOT_ST_ADDR'] par_boot_size=ptl['PRODUCT_CFG_PARTITION_BOOT_SIZE'] par_ftm1_st_addr=ptl['PRODUCT_CFG_PARTITION_FTM1_ST_ADDR'] par_ftm1_size=ptl['PRODUCT_CFG_PARTITION_FTM1_SIZE'] par_ftm2_st_addr=ptl['PRODUCT_CFG_PARTITION_FTM2_ST_ADDR'] par_ftm2_size=ptl['PRODUCT_CFG_PARTITION_FTM2_SIZE'] par_nv_file_st_addr=ptl['PRODUCT_CFG_PARTITION_NV_FILE_ST_ADDR'] par_nv_file_size=ptl['PRODUCT_CFG_PARTITION_NV_FILE_SIZE'] par_kernel_st_addr=ptl['PRODUCT_CFG_PARTITION_KERNEL_ST_ADDR'] par_kernel_size=ptl['PRODUCT_CFG_PARTITION_KERNEL_SIZE'] par_upg_st_addr=ptl['PRODUCT_CFG_PARTITION_UPG_ST_ADDR'] par_upg_size=ptl['PRODUCT_CFG_PARTITION_UPG_SIZE'] par_bin_total_size=par_boot_size+par_ftm1_size+par_ftm2_size+par_nv_file_size+len(tbl_kernel_bin) boot_nv_kernel_bin = bytearray(par_bin_total_size) boot_nv_kernel_bin[par_boot_st_addr:par_boot_st_addr+par_boot_size]=flash_boot_bin boot_nv_kernel_bin[par_ftm1_st_addr:par_ftm1_st_addr+par_ftm1_size]=factory_nv_bin boot_nv_kernel_bin[par_ftm2_st_addr:par_ftm2_st_addr+par_ftm2_size]=factory_nv_bin boot_nv_kernel_bin[par_nv_file_st_addr:par_nv_file_st_addr+par_nv_file_size]=normal_nv_bin boot_nv_kernel_bin[par_kernel_st_addr:par_kernel_st_addr+len(tbl_kernel_bin)]=tbl_kernel_bin print_tbl_info(image_file,bytearray(tbl_kernel_bin)) with open(image_file,"wb+") as fp: fp.write(boot_nv_kernel_bin) return def make_bootupg(root_dir,flash_boot_file, image_file, role_type, product_ver, ver_magic_not_use): #print('make_bootupg flash_boot_file: ',flash_boot_file) #print('make_bootupg image_file: ',image_file) #print('make_bootupg role_type: ',role_type) #print('make_bootupg product_ver: ',product_ver) #print('make_bootupg ver_magic: ',ver_magic) cfg_parser = PktCfgParser(root_dir, 'boot') cfg_parser.read(boot=True) pt_cfg = cfg_parser.get_partition_table_settings() ver_magic=cfg_parser.getint('MACRO','PRODUCT_CFG_BOOT_VER_MAGIC') (tmp_dir,product_list_1,product_list_2,magic,manufacture_code)=make_upg_init_cfg(cfg_parser,image_file, 'boot') tmp_dir=os.path.dirname(image_file) tmp_dir=os.path.join(tmp_dir,'tmp') with open(flash_boot_file,'rb') as fp: boot_bin=fp.read() if len(boot_bin)>pt_cfg['boot']['PRODUCT_CFG_PARTITION_BOOT_SIZE']: sys.exit("[ERR]boot size>PRODUCT_CFG_PARTITION_BOOT_SIZE FROM:%s"%os.path.realpath(__file__)) section_bin_list=[] section_bin_list.append(boot_bin) section_compress_list = [] section_compress_list.append(0) hupg_bin=make_upg_only(cfg_parser, root_dir, 3, section_bin_list, section_compress_list, image_file, magic, ver_magic, manufacture_code, product_list_1, product_list_2, None, 'boot') print_upg_info(image_file,hupg_bin) with open(image_file,'wb+') as fp: fp.write(hupg_bin) return def make_partion_upg(root_dir,partition_bin_file, image_file,role_type,product_ver,ver_magic, app_name): cfg_parser = PktCfgParser(root_dir, app_name) cfg_parser.read() (tmp_dir,product_list_1,product_list_2,magic,manufacture_code)=make_upg_init_cfg(cfg_parser,image_file, role_type) tmp_dir=os.path.dirname(image_file) tmp_dir=os.path.join(tmp_dir,'tmp') with open(partition_bin_file,'rb') as fp: partition_info_bin=fp.read() section_bin_list=[] section_compress_list = [] hupg_bin=make_upg_only(cfg_parser, root_dir, 4, section_bin_list, section_compress_list, image_file, magic, ver_magic, manufacture_code, product_list_1, product_list_2, partition_info_bin, role_type) print_upg_info(image_file,hupg_bin) with open(image_file,'wb+') as fp: fp.write(hupg_bin) return g_test_root_dir=r'F:\code\upg_test_3' g_test_image_dir=os.path.join(g_test_root_dir,r'target\delivery\DW21_Release\mk_image\image') g_test_src_dir=os.path.join(g_test_root_dir,r'target\delivery\DW21_Release\mk_image\src') def test_make_hupg(ver_magic,product_ver,image_name_only): kernel_file= os.path.join(g_test_src_dir,r'dw21_CCO_demo.bin') normal_nv_file= os.path.join(g_test_src_dir,r'cco_demo_normal.hnv') partition_bin_file= os.path.join(g_test_src_dir,r'cco_flash_partition_table.bin') image_file= os.path.join(g_test_image_dir,image_name_only) role_type='cco' make_hupg(g_test_root_dir,kernel_file, normal_nv_file,partition_bin_file,image_file,role_type,product_ver,ver_magic) def test_make_bootupg(ver_magic,product_ver,image_name_only): flash_boot_file=os.path.join(g_test_src_dir,r'dw21_flash_boot_secure_flash.bin') image_file=os.path.join(g_test_image_dir,image_name_only) role_type='boot' make_bootupg(g_test_root_dir,flash_boot_file, image_file, role_type, product_ver, ver_magic) def test_make_hbin(ver_magic,product_ver,image_name_only): flash_boot_file=os.path.join(g_test_src_dir,r'dw21_flash_boot_secure_flash.bin') factory_nv_file= os.path.join(g_test_src_dir,r'cco_demo_factory.hnv') normal_nv_file= os.path.join(g_test_src_dir,r'cco_demo_normal.hnv') kernel_file= os.path.join(g_test_src_dir,r'dw21_CCO_demo.bin') image_file= image_file= os.path.join(g_test_image_dir,image_name_only) role_type='cco' make_hbin(g_test_root_dir,flash_boot_file, factory_nv_file, normal_nv_file, kernel_file, image_file, role_type, product_ver, ver_magic, 'demo') def test_make_hbin_with_hupg_phase_1(end_addr,hbin_dir,hupg_dir,image_dir): with open(hbin_dir,'rb') as fp: hbin_bin=fp.read() with open(hupg_dir,'rb') as fp: hupg_bin=fp.read() print("total_size=0x%08x"%(len(hbin_bin)+len(hupg_bin))) temp_bin=bytearray(end_addr) temp_bin[0:len(hbin_bin)]=hbin_bin temp_bin[end_addr-len(hupg_bin):end_addr]=hupg_bin with open(image_dir,'wb+') as fp: fp.write(temp_bin) def test_make_hbin_with_hupg(hbin_name,hupg_name,hbin_hupg_bin): hbin_dir=os.path.join(g_test_image_dir,hbin_name) hupg_dir=os.path.join(g_test_image_dir,hupg_name) image_dir=os.path.join(g_test_image_dir,hbin_hupg_bin) test_make_hbin_with_hupg_phase_1(0x70000,hbin_dir,hupg_dir,image_dir) import random if __name__ == '__main__': random_magic=1 if random_magic==1: ver_magic=random.randint(1,0x12345678) else: ver_magic=0x11111111 product_ver= r'V100R001C00B021' test_name_1_kernel_hupg="test_1_kernel.hupg" test_name_1_boot_hupg="test_1_boot.hupg" test_name_1_hbin="test_1.hbin" test_make_hupg(ver_magic,product_ver,test_name_1_kernel_hupg) test_make_bootupg(ver_magic,product_ver,test_name_1_boot_hupg) test_make_hbin(ver_magic,product_ver,test_name_1_hbin) if random_magic==1: ver_magic=random.randint(1,0x12345678) else: ver_magic=0x22222222 product_ver= r'V100R001C00B022' test_name_2_kernel_hupg="test_2_kernel.hupg" test_name_2_boot_hupg="test_2_boot.hupg" test_name_2_hbin="test_2.hbin" test_make_hupg(ver_magic,product_ver,test_name_2_kernel_hupg) test_make_bootupg(ver_magic,product_ver,test_name_2_boot_hupg) test_make_hbin(ver_magic,product_ver,test_name_2_hbin) test_make_hbin_with_hupg(test_name_1_hbin,test_name_2_kernel_hupg,'burn_1hbin_2kerupg.hbin') #test_make_hbin_with_hupg(test_name_1_hbin,test_name_2_boot_hupg,'burn_1hbin_2bootupg.hbin')