def computeAndWriteAppCheckSumBin(self, version_tuple, destinationfilename = 'Output\code.bin'): DebugOut( 'Memory.computeAndWriteAppCheckSum' ) FW_CRC32_LOCATION = 0x0 FW_SIZE_LOCATION = 0x4 FW_DATE_LOCATION = 0xC FW_VERSION_LOCATION = 0x8 FW_CRC16_LOCATION = 0x1C FW_CODE_LOCATION = 0x20 FW_size = len(self.__data)-FW_CODE_LOCATION FW_version = filter FW_date = int((datetime.now() - datetime(1970,1,1)).total_seconds()) #Write code size self.__data[FW_SIZE_LOCATION+3] = chr((FW_size >> 24) & 0xFF) self.__data[FW_SIZE_LOCATION+2] = chr((FW_size >> 16) & 0xFF) self.__data[FW_SIZE_LOCATION+1] = chr((FW_size >> 8) & 0xFF) self.__data[FW_SIZE_LOCATION] = chr(FW_size & 0xFF) #Write code date self.__data[FW_DATE_LOCATION+3] = chr((FW_date >> 24) & 0xFF) self.__data[FW_DATE_LOCATION+2] = chr((FW_date >> 16) & 0xFF) self.__data[FW_DATE_LOCATION+1] = chr((FW_date >> 8) & 0xFF) self.__data[FW_DATE_LOCATION] = chr(FW_date & 0xFF) #Write code version print 'ver: ', version_tuple self.__data[FW_VERSION_LOCATION+3] = chr(version_tuple[3]) self.__data[FW_VERSION_LOCATION+2] = chr(version_tuple[2]) self.__data[FW_VERSION_LOCATION+1] = chr(version_tuple[1]) self.__data[FW_VERSION_LOCATION] = chr(version_tuple[0]) # Compute Checksum codebuff = self.__data[FW_CODE_LOCATION : len(self.__data)] nCRC16val = CRC16.computeCRC(0xFFFF, codebuff, len(codebuff)) # Write checksum back into memory location little endian self.__data[FW_CRC16_LOCATION+1] = chr((nCRC16val >> 8) & 0xFF) self.__data[FW_CRC16_LOCATION] = chr(nCRC16val & 0xFF) # calculate CRC32 starting after the CRC32 value to the end of the code codebuff = self.__data[FW_SIZE_LOCATION : len(self.__data)] codebuffstr = ''.join(codebuff) FW_crc32 = zlib.crc32(codebuffstr) print 'Code sz: ', hex(FW_size), 'crc16: ', hex(nCRC16val) print 'date: ', '0x{:08x}'.format(FW_date) print 'CRC32: ', '0x{:08x}'.format(FW_crc32 & 0xFFFFFFFF), 'size', FW_size, 'sz2 ',len(codebuff) # Write code CRC32 self.__data[FW_CRC32_LOCATION+3] = chr((FW_crc32 >> 24) & 0xFF) self.__data[FW_CRC32_LOCATION+2] = chr((FW_crc32 >> 16) & 0xFF) self.__data[FW_CRC32_LOCATION+1] = chr((FW_crc32 >> 8) & 0xFF) self.__data[FW_CRC32_LOCATION] = chr(FW_crc32 & 0xFF) codeHeaderstr = ''.join(self.__data) file = open(destinationfilename, 'wb') file.write(codeHeaderstr) file.close()
def computeAndWriteAppCheckSum(self): DebugOut( 'Memory.computeAndWriteAppCheckSum' ) #Create code buffer [0 - 0x3FF] and [0xA00 - 0xFFD] codebuff = self.__data[0 : 0x400] codebuff += self.__data[0xA00 : 0xFFFE] #Compute Checksum [0 - 0x3FF] and [0xA00 - 0xFFD] nCRC16val = CRC16.computeCRC(0xFFFF, codebuff, len(codebuff)) DebugOut( 'Memory.computeAndWriteAppCheckSum: nCRC16val = %s' % (nCRC16val) ) #Write checksum back into memory location LoCRC = nCRC16val & 0xFF HiCRC = (nCRC16val >> 8) & 0xFF self.__data[0xFFFE] = chr(HiCRC) self.__data[0xFFFF] = chr(LoCRC)