def make_common_binary_header(): SIZE_OF_HEADERSIZE = 2 SIZE_OF_BINVER = 4 SIZE_OF_BINSIZE = 4 # Calculate binary header size header_size = SIZE_OF_HEADERSIZE + SIZE_OF_BINVER + SIZE_OF_BINSIZE # Get binary version bin_ver = util.get_value_from_file(cfg_path, "CONFIG_COMMON_BINARY_VERSION=").replace('"','').replace('\n','') if bin_ver < 0 : print("Error : Not Found config for version, CONFIG_COMMON_BINARY_VERSION") sys.exit(1) elif bin_ver < 101 or bin_ver > 991231 : print("Error : Invalid value. It has 'YYMMDD' format so it should be in (101, 991231)") sys.exit(1) with open(file_path, 'rb') as fp: # binary data copy to 'data' data = fp.read() file_size = fp.tell() fp.close() fp = open(file_path, 'wb') # Generate binary with header data fp.write(struct.pack('H', header_size)) fp.write(struct.pack('I', int(bin_ver))) fp.write(struct.pack('I', file_size)) fp.write(data)
def make_kernel_binary_header(): secure_header_size = sys.argv[3] # Path to directory of this file mkbinheader_path = os.path.dirname(__file__) SIZE_OF_HEADERSIZE = 2 SIZE_OF_BINVER = 4 SIZE_OF_BINSIZE = 4 SIZE_OF_SECURE_HEADER_SIZE = 2 # Calculate binary header size header_size = SIZE_OF_HEADERSIZE + SIZE_OF_BINVER + SIZE_OF_BINSIZE + SIZE_OF_SECURE_HEADER_SIZE # Get binary version bin_ver = util.get_value_from_file(cfg_path, "CONFIG_BOARD_BUILD_DATE=").replace( '"', '').replace('\n', '') if bin_ver == 'None': print("Error : Not Found config for version, CONFIG_BOARD_BUILD_DATE") sys.exit(1) bin_ver = int(bin_ver) if bin_ver < 101 or bin_ver > 991231: print("Error : Invalid Kernel Binary Version, ", bin_ver, ".") print( " Please check CONFIG_BOARD_BUILD_DATE with 'YYMMDD' format in (101, 991231)" ) sys.exit(1) with open(file_path, 'rb') as fp: # binary data copy to 'data' data = fp.read() file_size = fp.tell() fp.close() fp = open(file_path, 'wb') # Generate binary with header data fp.write(struct.pack('H', header_size)) fp.write(struct.pack('I', int(bin_ver))) fp.write(struct.pack('I', file_size)) fp.write(struct.pack('H', int(secure_header_size))) fp.write(data) fp.close()
def make_user_binary_header(): binary_format = sys.argv[3] binary_name = sys.argv[4] binary_ver = sys.argv[5] dynamic_ram_size = sys.argv[6] main_stack_size = sys.argv[7] main_priority = sys.argv[8] loading_priority = sys.argv[9] # Path to directory of this file mkbinheader_path = os.path.dirname(__file__) SIZE_OF_HEADERSIZE = 2 SIZE_OF_BINTYPE = 1 SIZE_OF_MAINPRIORITY = 1 SIZE_OF_LOADINGPRIORITY = 1 SIZE_OF_BINSIZE = 4 SIZE_OF_BINNAME = 16 SIZE_OF_BINVER = 4 SIZE_OF_BINRAMSIZE = 4 SIZE_OF_MAINSTACKSIZE = 4 SIZE_OF_KERNELVER = 4 header_size = SIZE_OF_HEADERSIZE + SIZE_OF_BINTYPE + SIZE_OF_MAINPRIORITY + SIZE_OF_LOADINGPRIORITY + SIZE_OF_BINSIZE + SIZE_OF_BINNAME + SIZE_OF_BINVER + SIZE_OF_BINRAMSIZE + SIZE_OF_MAINSTACKSIZE + SIZE_OF_KERNELVER # Loading priority LOADING_LOW = 1 LOADING_MID = 2 LOADING_HIGH = 3 # Scheduling priority MAX/MIN SCHED_PRIORITY_MIN = 1 SCHED_PRIORITY_MAX = 255 # BM priority MAX/MIN BM_PRIORITY_MAX = 205 BM_PRIORITY_MIN = 200 if int(main_stack_size) >= int(dynamic_ram_size): print( "Error : Dynamic ram size should be bigger than Main stack size.") print("Dynamic ram size : %d, Main stack size : %d" % (int(dynamic_ram_size), int(main_stack_size))) sys.exit(1) priority = int( util.get_value_from_file(cfg_path, "CONFIG_BM_PRIORITY_MAX=").replace( '"', '').replace('\n', '')) if priority > 0: BM_PRIORITY_MAX = priority priority = int( util.get_value_from_file(cfg_path, "CONFIG_BM_PRIORITY_MIN=").replace( '"', '').replace('\n', '')) if priority > 0: BM_PRIORITY_MIN = priority with open(file_path, 'rb') as fp: # binary data copy to 'data' data = fp.read() file_size = fp.tell() fp.close() if binary_format == 'elf' or binary_format == 'ELF': bin_type = ELF else: # Not supported. bin_type = 0 print("Error : Not supported Binary Type") sys.exit(1) main_priority = int(main_priority) if (main_priority < SCHED_PRIORITY_MIN) or ( main_priority > SCHED_PRIORITY_MAX) or (BM_PRIORITY_MIN <= main_priority and main_priority <= BM_PRIORITY_MAX): print("Error : This binary priority ", main_priority, " is not valid") sys.exit(1) # Loading priority if loading_priority == 'H' or loading_priority == 'HIGH': loading_priority = LOADING_HIGH elif loading_priority == 'M' or loading_priority == 'MID': loading_priority = LOADING_MID elif loading_priority == 'L' or loading_priority == 'LOW': loading_priority = LOADING_LOW else: #Not supported. print("Error : Not supported Binary loading priority") sys.exit(1) static_ram_size = get_static_ram_size(bin_type) if util.check_config_existence( cfg_path, 'CONFIG_OPTIMIZE_APP_RELOAD_TIME=y') == True: binary_ram_size = int(dynamic_ram_size) else: binary_ram_size = int(static_ram_size) + int(dynamic_ram_size) binary_ram_size = roundup_power_two(binary_ram_size) # Get kernel binary version kernel_ver = util.get_value_from_file( cfg_path, "CONFIG_BOARD_BUILD_DATE=").replace('"', '').replace('\n', '') if kernel_ver == 'None': print( "Error : Not Found config for kernel version, CONFIG_BOARD_BUILD_DATE" ) sys.exit(1) kernel_ver = int(kernel_ver) if kernel_ver < 101 or kernel_ver > 991231: print("Error : Invalid Kernel Binary Version, ", kernel_ver, ".") print( " Please check CONFIG_BOARD_BUILD_DATE with 'YYMMDD' format in (101, 991231)" ) sys.exit(1) fp = open(file_path, 'wb') fp.write(struct.pack('H', header_size)) fp.write(struct.pack('B', bin_type)) fp.write(struct.pack('B', main_priority)) fp.write(struct.pack('B', loading_priority)) fp.write(struct.pack('I', file_size)) fp.write('{:{}{}.{}}'.format(binary_name, '<', SIZE_OF_BINNAME, SIZE_OF_BINNAME - 1).replace(' ', '\0')) fp.write(struct.pack('I', int(binary_ver))) fp.write(struct.pack('I', binary_ram_size)) fp.write(struct.pack('I', int(main_stack_size))) fp.write(struct.pack('I', int(kernel_ver))) fp.write(data) fp.close()
def make_user_binary_header(): binary_format = sys.argv[3] binary_name = sys.argv[4] binary_ver = sys.argv[5] dynamic_ram_size = sys.argv[6] main_stack_size = sys.argv[7] main_priority = sys.argv[8] comp_enabled = sys.argv[9] comp_blk_size = sys.argv[10] loading_priority = sys.argv[11] # Path to directory of this file mkbinheader_path = os.path.dirname(__file__) SIZE_OF_HEADERSIZE = 2 SIZE_OF_BINTYPE = 1 SIZE_OF_MAINPRIORITY = 1 SIZE_OF_LOADINGPRIORITY = 1 SIZE_OF_BINSIZE = 4 SIZE_OF_BINNAME = 16 SIZE_OF_BINVER = 4 SIZE_OF_BINRAMSIZE = 4 SIZE_OF_MAINSTACKSIZE = 4 SIZE_OF_KERNELVER = 4 header_size = SIZE_OF_HEADERSIZE + SIZE_OF_BINTYPE + SIZE_OF_MAINPRIORITY + SIZE_OF_LOADINGPRIORITY + SIZE_OF_BINSIZE + SIZE_OF_BINNAME + SIZE_OF_BINVER + SIZE_OF_BINRAMSIZE + SIZE_OF_MAINSTACKSIZE + SIZE_OF_KERNELVER COMP_NONE = 0 COMP_LZMA = 1 COMP_MINIZ = 2 COMP_MAX = COMP_MINIZ # Loading priority LOADING_LOW = 1 LOADING_MID = 2 LOADING_HIGH = 3 # Scheduling priority MAX/MIN SCHED_PRIORITY_MIN = 1 SCHED_PRIORITY_MAX = 255 # BM priority MAX/MIN BM_PRIORITY_MAX = 205 BM_PRIORITY_MIN = 200 if int(main_stack_size) >= int(dynamic_ram_size) : print("Error : Dynamic ram size should be bigger than Main stack size.") print("Dynamic ram size : %d, Main stack size : %d" %(int(dynamic_ram_size), int(main_stack_size))) sys.exit(1) priority = int(util.get_value_from_file(cfg_path, "CONFIG_BM_PRIORITY_MAX=").replace('"','').replace('\n','')) if priority > 0 : BM_PRIORITY_MAX = priority priority = int(util.get_value_from_file(cfg_path, "CONFIG_BM_PRIORITY_MIN=").replace('"','').replace('\n','')) if priority > 0 : BM_PRIORITY_MIN = priority with open(file_path, 'rb') as fp: # binary data copy to 'data' data = fp.read() file_size = fp.tell() fp.close() if binary_format == 'elf' or binary_format == 'ELF' : bin_type = ELF else : # Not supported. bin_type = 0 print("Error : Not supported Binary Type") sys.exit(1) main_priority = int(main_priority) if (main_priority < SCHED_PRIORITY_MIN) or (main_priority > SCHED_PRIORITY_MAX) or (BM_PRIORITY_MIN <= main_priority and main_priority <= BM_PRIORITY_MAX) : print("Error : This binary priority ", main_priority, " is not valid") sys.exit(1) # Loading priority if loading_priority == 'H' or loading_priority == 'HIGH' : loading_priority = LOADING_HIGH elif loading_priority == 'M' or loading_priority == 'MID' : loading_priority = LOADING_MID elif loading_priority == 'L' or loading_priority == 'LOW' : loading_priority = LOADING_LOW else : #Not supported. print("Error : Not supported Binary loading priority") sys.exit(1) static_ram_size = get_static_ram_size(bin_type) if util.check_config_existence(cfg_path, 'CONFIG_OPTIMIZE_APP_RELOAD_TIME=y') == True: binary_ram_size = int(dynamic_ram_size) else: binary_ram_size = int(static_ram_size) + int(dynamic_ram_size) binary_ram_size = roundup_power_two(binary_ram_size) # Get kernel binary version kernel_ver = util.get_value_from_file(cfg_path, "CONFIG_BOARD_BUILD_DATE=").replace('"','').replace('\n','') if kernel_ver == 'None' : print("Error : Not Found config for kernel version, CONFIG_BOARD_BUILD_DATE") sys.exit(1) kernel_ver = int(kernel_ver) if kernel_ver < 101 or kernel_ver > 991231 : print("Error : Invalid value. It has 'YYMMDD' format so it should be in (101, 991231)") sys.exit(1) # based on comp_enabled, check if we need to compress binary. # If yes, assign to bin_comp value for compression algorithm to use. # Else, assign 0 to bin_comp to represent no compression if 0 < int(comp_enabled) <= COMP_MAX : bin_comp = int(comp_enabled) else : bin_comp = 0 # Compress data according to Compression Algorithm represented by bin_comp # Run mkcompressimg tool with provided options. Read output compressed file into data. if bin_comp > COMP_NONE : os.system('cp ' + file_path + ' ' + file_path + '.uncomp') fp_tmp = open("tmp", 'wb+') fp_tmp.write(data) fp_tmp.close() if os.system(mkbinheader_path + '/compression/mkcompressimg ' + comp_blk_size + ' ' + comp_enabled + ' tmp' + ' tmp_comp') != 0 : sys.exit(1) fp_tmp = open("tmp_comp", 'rb') data = fp_tmp.read() file_size = fp_tmp.tell() fp_tmp.close() os.system('rm tmp tmp_comp') fp = open(file_path, 'wb') fp.write(struct.pack('H', header_size)) fp.write(struct.pack('B', bin_type)) fp.write(struct.pack('B', main_priority)) fp.write(struct.pack('B', loading_priority)) fp.write(struct.pack('I', file_size)) fp.write('{:{}{}.{}}'.format(binary_name, '<', SIZE_OF_BINNAME, SIZE_OF_BINNAME - 1).replace(' ','\0')) fp.write(struct.pack('I', int(binary_ver))) fp.write(struct.pack('I', binary_ram_size)) fp.write(struct.pack('I', int(main_stack_size))) fp.write(struct.pack('I', int(kernel_ver))) fp.write(data) fp.close()
# Compare the partition size and its binary size if PARTITION_SIZE < int(BINARY_SIZE): print(" !!!!!!!! ERROR !!!!!!!") print(" Built " + bin_type + " (" + number_with_comma(BINARY_SIZE) + " bytes) is greater than its partition (" + number_with_comma(PARTITION_SIZE) + " bytes).") print( " " + bin_type + " Binary will be deleted. Need to re-configure the partition using menuconfig and to re-build." ) os.remove(output_path) FAIL_TO_BUILD = True PARTITION_SIZE_LIST = util.get_value_from_file(cfg_file, "CONFIG_FLASH_PART_SIZE=") PARTITION_NAME_LIST = util.get_value_from_file(cfg_file, "CONFIG_FLASH_PART_NAME=") CONFIG_APP_BINARY_SEPARATION = util.get_value_from_file( cfg_file, "CONFIG_APP_BINARY_SEPARATION=").rstrip('\n') CONFIG_SUPPORT_COMMON_BINARY = util.get_value_from_file( cfg_file, "CONFIG_SUPPORT_COMMON_BINARY=").rstrip('\n') if PARTITION_SIZE_LIST == 'None': sys.exit(0) NAME_LIST = PARTITION_NAME_LIST.replace('"', '').split(",") SIZE_LIST = PARTITION_SIZE_LIST.replace('"', '').split(",") # Find Partition Index
if ("kernel" in bin_name): f.write('KERNEL_BIN_NAME=' + bin_name + '\n') if ("app1" in bin_name): f.write('APP1_BIN_NAME=' + bin_name + '\n') if ("app2" in bin_name): f.write('APP2_BIN_NAME=' + bin_name + '\n') if ("common" in bin_name): f.write('COMMON_BIN_NAME=' + bin_name + '\n') # Delete previous .bininfo if os.path.isfile(os_folder + '/.bininfo'): os.remove(os_folder + '/.bininfo') # Check the board type. Because kernel binary name is different based on board type. BOARD_TYPE = util.get_value_from_file(cfg_file, "CONFIG_ARCH_BOARD=").replace( '"', '').rstrip("\n") # Extract Common binary name CONFIG_CMN_BIN_NAME = util.get_value_from_file( cfg_file, "CONFIG_COMMON_BINARY_NAME=").replace('"', '').rstrip("\n") if util.check_config_existence(cfg_file, 'CONFIG_BOARD_BUILD_DATE') == True: BIN_VERSION = util.get_value_from_file(cfg_file, "CONFIG_BOARD_BUILD_DATE=").replace( '"', '').rstrip("\n") BIN_NAME = 'kernel_' + BOARD_TYPE + '_' + BIN_VERSION else: BIN_NAME = 'kernel_' + BOARD_TYPE # Read the kernel binary name from board_metadata.txt. metadata_file = build_folder + '/configs/' + BOARD_TYPE + '/board_metadata.txt'
def make_bootparam(): print "========== Start to make boot parameters ==========" SIZE_OF_CHECKSUM = 4 SIZE_OF_BP_VERSION = 4 SIZE_OF_BP_FORMAT_VERSION = 4 SIZE_OF_KERNEL_INDEX = 1 SIZE_OF_KERNEL_FIRST_ADDR = 4 SIZE_OF_KERNEL_SECOND_ADDR = 4 kernel_data_size = SIZE_OF_CHECKSUM + SIZE_OF_BP_VERSION + SIZE_OF_BP_VERSION + SIZE_OF_KERNEL_INDEX + SIZE_OF_KERNEL_FIRST_ADDR + SIZE_OF_KERNEL_SECOND_ADDR FLASH_START_ADDR = util.get_value_from_file(config_file_path, "CONFIG_FLASH_START_ADDR=") FLASH_SIZE = util.get_value_from_file(config_file_path, "CONFIG_FLASH_SIZE=") names = util.get_value_from_file(config_file_path, "CONFIG_FLASH_PART_NAME=").replace( '"', '').replace('\n', '').split(",") sizes = util.get_value_from_file(config_file_path, "CONFIG_FLASH_PART_SIZE=").replace( '"', '').replace('\n', '').split(",") names = filter(None, names) sizes = filter(None, sizes) INITIAL_ACTIVE_IDX = 0 # Check partitions of kernel and bootparam kernel_address = [] offset = 0 bp_part_size = 0 for index, name in enumerate(names): if name == "kernel": # Get addresses of kernel partitions address = hex(int(FLASH_START_ADDR, 16) + offset) kernel_address.append(address) if name == "bootparam": # Get size of bootparam partition bp_part_size = int(sizes[index]) * 1024 # Verify partition size and offset of boot parameters if bp_part_size == 0: print "FAIL!! No bootparam partition." sys.exit(1) elif bp_part_size != SIZE_OF_BP_PARTITION: print "FAIL!! Bootparam partition size should be 8K. Please re-configure." sys.exit(1) elif offset != int(FLASH_SIZE) - int(SIZE_OF_BP_PARTITION): print "FAIL!! Bootparam should be located at the end of flash with 8K. Please re-configure." sys.exit(1) offset += int(sizes[index]) * 1024 # Verify kernel partitions if len(kernel_address) == 0 or len(kernel_address) > 2: print "FAIL!! No found kernel partition" sys.exit(1) with open(bootparam_file_path, 'wb') as fp: # Write Data bootparam_version = 1 bootparam_format_version = 1 fp.write(struct.pack('I', bootparam_version)) fp.write(struct.pack('I', bootparam_format_version)) # Kernel data fp.write(struct.pack('B', INITIAL_ACTIVE_IDX)) for address in kernel_address: fp.write(struct.pack('I', int(address, 16))) # Get User data if os.path.exists(user_file_dir): user_file_list = os.listdir(user_file_dir) else: user_file_list = [] app_data_size = 0 user_app_count = len(user_file_list) if user_app_count > 0: SIZE_OF_BINCOUNT = 1 SIZE_OF_BINNAME = 16 SIZE_OF_BINIDX = 1 app_data_size = SIZE_OF_BINCOUNT + (user_app_count * (SIZE_OF_BINNAME + SIZE_OF_BINIDX)) with open(bootparam_file_path, 'a') as fp: # User Data fp.write(struct.pack('B', user_app_count)) for user_file in user_file_list: fp.write('{:{}{}.{}}'.format(user_file, '<', SIZE_OF_BINNAME, SIZE_OF_BINNAME - 1).replace( ' ', '\0')) fp.write(struct.pack('B', INITIAL_ACTIVE_IDX)) # Fill remaining space with '0xff' with open(bootparam_file_path, 'a') as fp: remain_size = SIZE_OF_BPx - (kernel_data_size + app_data_size) fp.write(b'\xff' * remain_size) # Add checksum for BP1 mkchecksum_path = os.path.dirname(__file__) + '/mkchecksum.py' os.system('python %s %s' % (mkchecksum_path, bootparam_file_path)) # Fill remaining space with '0xff' for BP2 with open(bootparam_file_path, 'a') as fp: fp.write(b'\xff' * SIZE_OF_BPx)