def getStackImgRange(projdir, stackHexFileName, boundary_address, outputPath): stack_boundary, flash_limit = getStackRange(boundary_address) # Generate binary file if stackHexFileName.endswith('.hex'): outfileStackPath = os.path.abspath(stackHexFileName[:-4] + ".bin") else: raise Exception("Stack hex not valid") intelhex.hex2bin(stackHexFileName, outfileStackPath, start=stack_boundary, end=flash_limit) # Get the image range imgRange = imgBinUtil.getImgRange([outfileStackPath, imgStartOffset, imgEndOffset, imgLenOffset]) return imgRange
def createAppStackBinfile(projType, projdir, binaryFileType, appHexFileName, bdFileName, outputPath, stackHexFileName=None): # Find boundary, if there is one if (bdFileName is not None): if projType == 'ccs': boundary_address = getBoundary_CCS(bdFileName) else: boundary_address = getBoundary(bdFileName) if (binaryFileType == 3 or binaryFileType == 7): #Merged App + Stack or app + stack library app_path = os.path.join(projdir, appHexFileName) outfileAppPath = os.path.join(projdir, outputPath) outfileAppPath = os.path.abspath(outfileAppPath + ".bin") # Generate application binary intelhex.hex2bin(app_path, outfileAppPath) # Get the image range if (binaryFileType == 7): imgStartOffset = 52 #0x34 imgRngParams = [ outfileAppPath, imgStartOffset, imgEndOffset, imgLenOffset ] imgStAddr, imgEndAddr = imgBinUtil.getImgRange(imgRngParams) #pad end if it is not word aligned imgLen = imgEndAddr - imgStAddr + 1 if (imgLen & 3): imgEndAddr = imgEndAddr - (imgLen & 3) + 4 # Run the hex2bin to get correct length binary image intelhex.hex2bin(app_path, outfileAppPath, start=imgStAddr, end=imgEndAddr) # Update imageLength field imgBinUtil.updateImageLen( [outfileAppPath, imgStartOffset, imgEndOffset, imgLenOffset]) if (binaryFileType == 3): # Update image segment's length field imgBinUtil.updateImageSegLen([ outfileAppPath, imgStartOffset, imgEndOffset, (fixedHdrLen + boundrySegLen + 4), fixedHdrLen + boundrySegLen ]) # Update file type field imgBinUtil.fileTypeTag([outfileAppPath, "3", "18"]) else: # Update image segment's length field imgBinUtil.updateImageSegLen([ outfileAppPath, imgStartOffset, imgEndOffset, (fixedHdrLen + 4), fixedHdrLen ]) # Update file type field imgBinUtil.fileTypeTag([outfileAppPath, "7", "18"]) # Update crc32 field computeCRC32.computeCRC32([outfileAppPath, "12", "8"]) elif (binaryFileType == 2): # Stack only stack_boundary, flash_limit = getStackRange(boundary_address) stack_path = os.path.join(projdir, appHexFileName) outfileStackPath = os.path.abspath(stack_path + ".bin") # Generate binary file outfileStackPath = os.path.join(projdir, outputPath) outfileStackPath = outfileStackPath + ".bin" intelhex.hex2bin(stack_path, outfileStackPath, start=stack_boundary, end=flash_limit) # Get the image range args = [outfileStackPath, imgStartOffset, imgEndOffset, imgLenOffset] imgStAddr, imgEndAddr = imgBinUtil.getImgRange(args) #pad end if it is not word aligned imgLen = imgEndAddr - imgStAddr + 1 if (imgLen & 3): imgEndAddr = imgEndAddr - (imgLen & 3) + 4 # Rerun hex2bin to get correct length of binary image intelhex.hex2bin(stack_path, outfileStackPath, start=imgStAddr, end=imgEndAddr) imgBinUtil.updateImageLen( [outfileStackPath, imgStartOffset, imgEndOffset, imgLenOffset]) imgBinUtil.updateImageSegLen([ outfileStackPath, imgStartOffset, imgEndOffset, payloadSegLenOffset, fixedHdrLen + boundrySegLen ]) computeCRC32.computeCRC32([outfileStackPath, "12", "8"]) elif (binaryFileType == 1): # Get app image range appImgStartAddr, app_boundary = getAppRange(boundary_address) # To get the actual stack range, need to build stack binary and then # read the image header to find the actual address stack_boundary, flash_limit = getStackImgRange(projdir, stackHexFileName, boundary_address, outputPath) app_path = os.path.join(projdir, appHexFileName) stack_path = os.path.join(projdir, stackHexFileName) outfileAppPath = os.path.join(projdir, outputPath) + ".bin" # Generate application binary intelhex.hex2bin(app_path, outfileAppPath, start=appImgStartAddr, end=app_boundary) # Get the image range imgRngParams = [ outfileAppPath, imgStartOffset, imgEndOffset, imgLenOffset ] imgStAddr, imgEndAddr = imgBinUtil.getImgRange(imgRngParams) #pad end if it is not word aligned imgLen = imgEndAddr - imgStAddr + 1 if (imgLen & 3): imgEndAddr = imgEndAddr - (imgLen & 3) + 4 # Rerun the hex2bin to get correct length binary image intelhex.hex2bin(app_path, outfileAppPath, start=imgStAddr, end=imgEndAddr) # Update file type field imgBinUtil.fileTypeTag([outfileAppPath, "1", "18"]) print "outfileAppPath: " + str( outfileAppPath) + "imgStartOffset: " + str( imgStartOffset) + "imgEndOffset" + str( imgEndOffset) + "imgLenOffset" + str(imgLenOffset) # Update imageLength field imgBinUtil.updateImageLen( [outfileAppPath, imgStartOffset, imgEndOffset, imgLenOffset]) # Update image segment's length field imgBinUtil.updateImageSegLen([ outfileAppPath, imgStartOffset, imgEndOffset, payloadSegLenOffset, fixedHdrLen + boundrySegLen ]) # Update crc32 field computeCRC32.computeCRC32([outfileAppPath, "12", "8"]) # Generate merged binary flashRange = str(hex(appImgStartAddr)) + ':' + str(hex(flash_limit)) mergedBinLen = getMergeBinLen(flashRange) # Create IntelHex objects for each hex appHex = intelhex.IntelHex(app_path) stackHex = intelhex.IntelHex(stack_path) # Define boundaries for each hex appHex = appHex[appImgStartAddr:app_boundary + 1] stackHex = stackHex[stack_boundary:flash_limit + 1] # Merge hex's inputHexFiles = [appHex, stackHex] mergedHex = intelhex.IntelHex() for hexFile in inputHexFiles: try: mergedHex.merge(hexFile, overlap="replace") except: print("Fatal Error: FAILED merge due to overlap when merging") sys.exit(1) # Define boundaries for merged hex mergedHex = mergedHex[appImgStartAddr:flash_limit + 1] # Write hex to file # mergeHexFilePath = os.path.abspath(outputPath + "_merged.hex") # print "mergeHexFilePath:::: " + mergeHexFilePath mergeHexFilePath = os.path.join(projdir, outputPath) + "_merged.hex" mergedHex.write_hex_file(mergeHexFilePath) # Convert hex to bin outFileMergedPath = os.path.join(projdir, outputPath) + "_merged.bin" intelhex.hex2bin(mergeHexFilePath, outFileMergedPath, start=appImgStartAddr, end=flash_limit) imgBinUtil.fileTypeTag([outFileMergedPath, "3", "18"]) # Update imageLength field imgBinUtil.updateMergedImageLen( [outFileMergedPath, mergedBinLen, imgLenOffset]) args = [ outFileMergedPath, mergedBinLen, imgEndOffset, payloadSegLenOffset ] imgBinUtil.updateMergedImageSegLen(args) mergedImgEndAddr = getMergeBinEndAddr(flashRange) imgBinUtil.updateMergedImageEndAddr( [outFileMergedPath, mergedImgEndAddr, imgEndOffset]) # Add crc32 bytes computeCRC32.computeCRC32([outFileMergedPath, "12", "8"])