def _WriteConfigBlock(self, rowNumber=0, data=[], securityKey=[0xA5, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0x5A], ebid=0): ''' This method writes a row of flash data wrapped in the config block format. ''' # Start with the flash row number rowNumberMSB = (rowNumber >> 8) & 0xff rowNumberLSB = (rowNumber) & 0xff dataToWrite = [rowNumberMSB, rowNumberLSB] # Then send the flash row size flashSizeMSB = (self.flashRowSize >> 8) & 0xff flashSizeLSB = (self.flashRowSize) & 0xff dataToWrite.append(flashSizeMSB) dataToWrite.append(flashSizeLSB) # Set the EBID dataToWrite.append(ebid) # Then send the flash block data for i in range(0, self.flashRowSize): if i < len(data): dataToWrite.append(data[i]) else: dataToWrite.append(0) # Follow everything with the 8-byte security key for value in securityKey: dataToWrite.append(value) # End the whole packet with a CRC crc = CalculateCRC.calculate_flash_config(data) crcMSB = (crc >> 8) & 0xff crcLSB = (crc) & 0xff dataToWrite.append(crcMSB) dataToWrite.append(crcLSB) # Make sure we are in CAT mode self.EnterCmdMode() # Send the write command self.SendCatCommand(0x04, dataToWrite) # Get the return value data = self.ReadCatCommand()[0:5] # Get the error code errorCode = data[0] if errorCode == 1: errorMessage = 'Invalid security code.' elif errorCode == 2: errorMessage = 'Invalid block number.' elif errorCode == 3: errorMessage = 'Failed flash write.' else: errorMessage = '' return (errorCode, errorMessage)
def UpdateCRC(self): ''' Calculates the CRC on the uncommitted data, then updates it. ''' # Get the address of the CRC crcReg = self._GetRegister('CONFIG_CRC') crcAddr = crcReg.relativeAddress # Get the data to test data = self._UncommittedData[0:crcAddr] # Calculate the CRC crc = CalculateCRC.calculate_flash_config(data) # Set the CRC self.SetCRC(crc) return
def _ParseConfigBlock(self, data=[], calculatedCRC=0, storedCRC=0): ''' Parses the config block from a flash row read. ''' blockData = None if len(data) > 0: # Flatten the row data into one massive array data = flatten(data) # Extract the data size dataSize = (data[1] << 8) + (data[0]) if dataSize > 0: # Get the maximum block size maximumSize = (data[3] << 8) + (data[2]) # Get the block data blockData = data[0:maximumSize+4] # Calculate the CRC based on the block data blockCRC = CalculateCRC.calculate_flash_config(data[0:dataSize]) # Get the CRC try: CRC = (data[maximumSize+1] << 8) + (data[maximumSize]) except: pass ''' # Make sure the CRC matches the stored CRC if (CRC != storedCRC): print 'CRC:', CRC print 'Stored CRC:', storedCRC print 'Block CRC:', blockCRC print 'Calculated CRC:', calculatedCRC raise Exception(fileName, '_ParseConfigBlock', 1, 'ERROR: The stored CRC does not match the returned CRC value (%d != %d).' % (CRC, storedCRC)) # Make sure the calculated CRC matches the internal calculated CRC if blockCRC != calculatedCRC: raise Exception(fileName, '_ParseConfigBlock', 2, 'ERROR: The calculated CRC does not match the internally calculated CRC value (%d != %d).' % (blockCRC, calculatedCRC)) ''' return blockData
def _CalculateCRC(self, flashData): # Flatten the flash data allData = [] for row in flashData: for byte in row: allData.append(byte) # Get the offset and size for the CRC register [offset, size] = self.flash.GetOffsetForRegister('CONFIG_CRC') # Make sure we don't include the CRC data allData = allData[offset+size:] # Calculate the CRC crc = CalculateCRC.calculate(allData) return crc
def _CalculateCRC(self, flashData): # Flatten the flash data allData = [] for row in flashData: for byte in row: allData.append(byte) # Get the offset and size for the CRC register [offset, size] = self.flash.GetOffsetForRegister('CONFIG_CRC') # Make sure we don't include the CRC data allData = allData[offset + size:] # Calculate the CRC crc = CalculateCRC.calculate(allData) return crc
def _WriteConfigBlock( self, rowNumber=0, data=[], securityKey=[0xA5, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0x5A], ebid=0): ''' This method writes a row of flash data wrapped in the config block format. ''' # Start with the flash row number rowNumberMSB = (rowNumber >> 8) & 0xff rowNumberLSB = (rowNumber) & 0xff dataToWrite = [rowNumberMSB, rowNumberLSB] # Then send the flash row size flashSizeMSB = (self.flashRowSize >> 8) & 0xff flashSizeLSB = (self.flashRowSize) & 0xff dataToWrite.append(flashSizeMSB) dataToWrite.append(flashSizeLSB) # Set the EBID dataToWrite.append(ebid) # Then send the flash block data for i in range(0, self.flashRowSize): if i < len(data): dataToWrite.append(data[i]) else: dataToWrite.append(0) # Follow everything with the 8-byte security key for value in securityKey: dataToWrite.append(value) # End the whole packet with a CRC crc = CalculateCRC.calculate_flash_config(data) crcMSB = (crc >> 8) & 0xff crcLSB = (crc) & 0xff dataToWrite.append(crcMSB) dataToWrite.append(crcLSB) # Make sure we are in CAT mode self.EnterCmdMode() # Send the write command self.SendCatCommand(0x04, dataToWrite) # Get the return value data = self.ReadCatCommand()[0:5] # Get the error code errorCode = data[0] if errorCode == 1: errorMessage = 'Invalid security code.' elif errorCode == 2: errorMessage = 'Invalid block number.' elif errorCode == 3: errorMessage = 'Failed flash write.' else: errorMessage = '' return (errorCode, errorMessage)