def test_sb_write_unaligned_address(self, bl, memType): sb_commandDictionay['writeMemory'].cumulativeWrite = False startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) # Here we just need a small amount data for this case. Set the length as 1KB. length = 0x400 alignedBase = bl.target.programAlignmentSize for offset in range(1, alignedBase): sb_commandDictionay['writeMemory'].length = length sb_commandDictionay['writeMemory'].dataType = 'file_bin' sb_commandDictionay[ 'writeMemory'].startAddress = startAddress + offset sb_commandDictionay[ 'writeMemory'].endAddress = startAddress + length + offset sb_commandDictionay[ 'writeMemory'].data = common_util.generate_random_data_file( bl, sb_commandDictionay['writeMemory'].startAddress, length) sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'writeMemory') status, results = bl.receive_sb_file(sbFilePath) if memType == 'flash': assert status == bootloader.status.kStatus_FlashAlignmentError elif memType == 'ram': assert status == bootloader.status.kStatus_Success
def init_sbCmdDict(bl, encryptionType, dataType, memType): startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) sbCmdDict['writeMemory'].cumulativeWrite = False sbCmdDict['writeMemory'].dataType = dataType if dataType == 'string': sbCmdDict['writeMemory'].data = kStringForSimpleSbFile sbCmdDict['writeMemory'].length = len(kStringForSimpleSbFile) sbCmdDict['writeMemory'].startAddress = startAddress sbCmdDict['writeMemory'].endAddress = startAddress + len( kStringForSimpleSbFile) elif dataType == 'function': sbCmdDict['writeMemory'].data = kFormatStringForFunctionSbFile sbCmdDict['writeMemory'].length = len(kFormatStringForFunctionSbFile) sbCmdDict['writeMemory'].startAddress = startAddress sbCmdDict['writeMemory'].endAddress = startAddress + len( kFormatStringForFunctionSbFile) elif dataType == 'app_bin': (elfFile, hexFile, binFile) = common_util.get_led_demo_path(bl) sbCmdDict['writeMemory'].data = binFile sbCmdDict['writeMemory'].length = os.path.getsize(binFile) sbCmdDict[ 'writeMemory'].startAddress = bl.target.BL_APP_VECTOR_TABLE_ADDRESS sbCmdDict[ 'writeMemory'].endAddress = bl.target.BL_APP_VECTOR_TABLE_ADDRESS + os.path.getsize( binFile)
def test_sb_write_all_available_region(self, bl, memType): sb_commandDictionay['writeMemory'].cumulativeWrite = False startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) sb_commandDictionay['writeMemory'].length = length sb_commandDictionay[ 'writeMemory'].data = common_util.generate_random_data_file( bl, startAddress, length) sb_commandDictionay['writeMemory'].dataType = 'file_bin' sb_commandDictionay['writeMemory'].startAddress = startAddress sb_commandDictionay['writeMemory'].endAddress = startAddress + length sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'writeMemory') status, results = bl.receive_sb_file(sbFilePath) assert status == bootloader.status.kStatus_Success # Read back the data from memory readFile = os.path.join(bl.vectorsDir, 'read_data_from_memory.bin') status, results = bl.read_memory(startAddress, length, readFile) assert status == bootloader.status.kStatus_Success # The two data files should be the same. with open(sb_commandDictionay['writeMemory'].data, 'rb') as fileObj1: data1 = fileObj1.read() fileObj1.close() with open(readFile, 'rb') as fileObj2: data2 = fileObj2.read() fileObj2.close() assert data1 == data2
def config_QSPI_flash(bl, qcbFileName, qcbResident): # get the file path qcbFilePath = os.path.abspath( os.path.join(bl.vectorsDir, 'QSPI', qcbFileName)) # Get available memory address to place the QCB availableRegionStartAddress, availableRegionEndAddress, availableRegionSize = common_util.get_available_memory_region( bl, qcbResident) qcbLocation = availableRegionStartAddress if qcbResident == 'ram': pass elif qcbResident == 'flash': if availableRegionStartAddress == 0: # QCB cannot be located in flash start address as it is used to place the app's vector table. # Let QCB locate at the end of the flash address for the boundary value testing. qcbLocation = qcbLocation + common_util.get_memory_total_size( bl, 'flash') - os.path.getsize(qcbFilePath) else: pass # Erase 1 sector flash before program flashSectorSize = common_util.get_flash_sector_size(bl) startAddressSectorAlign = common_util.flash_align_down( qcbLocation, flashSectorSize) status, results = bl.flash_erase_region(startAddressSectorAlign, flashSectorSize) assert status == bootloader.status.kStatus_Success # write qspi config block data to ram status, results = bl.write_memory(qcbLocation, qcbFilePath) assert status == bootloader.status.kStatus_Success # execute configure-quadspi command status, results = bl.configure_memory(1, qcbLocation) assert status == bootloader.status.kStatus_Success return qcbLocation
def test_sb_fill_all_available_memory(self, bl, memType, pattern_format): sb_commandDictionay['fillMemory'].cumulativeFill = False startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) # --------------------------------------------------------------------------------- if pattern_format == 'byte': pattern = kFilledValue & 0xFF elif pattern_format == 'short': pattern = kFilledValue & 0xFFFF elif pattern_format == 'word': pattern = kFilledValue & 0xFFFFFFFF # --------------------------------------------------------------------------------- sb_commandDictionay['fillMemory'].pattern = pattern sb_commandDictionay['fillMemory'].patternFormat = pattern_format sb_commandDictionay['fillMemory'].startAddress = startAddress sb_commandDictionay['fillMemory'].endAddress = startAddress + length sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'fillMemory') status, results = bl.receive_sb_file(sbFilePath) assert status == bootloader.status.kStatus_Success # Read back the filling data and compare with the filling data. read_file = os.path.join(bl.vectorsDir, 'read_data_from_memory.bin') status, results = bl.read_memory(startAddress, length, read_file) assert common_util.is_fill_memory_correct(read_file, length, pattern, pattern_format) == True
def cumulative_fill(bl, memType, memIndex, usingSbComamnd=False): (availableMemStartAddress, availableMemEndAddress, availableMemSize) = common_util.get_available_memory_region( bl, memType, memIndex) if (availableMemSize == 0 ): # K3S TO1.0, all the M4 ITCM SRAM regions are reserved. print("available%s%dSize = %d Bytes." % (memType.capitalize(), memIndex, availableMemSize)) print("No need to test cumulative fill for %s%d!" % (memType.capitalize(), memIndex)) return else: fillStartAddress = availableMemStartAddress fillBytesLength = 0x400 fillEndAddress = fillStartAddress + fillBytesLength firstFillPattern = 0x00 secondFillPattern = 0xFF if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_fill_memory( bdContent, firstFillPattern, 'word', fillStartAddress, fillEndAddress) bdContent = SBFile.bd_content_update_for_fill_memory( bdContent, secondFillPattern, 'byte', fillStartAddress, fillEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) status, results = bl.receive_sb_file(sbFile) else: status, results = bl.fill_memory(fillStartAddress, fillBytesLength, firstFillPattern, 'byte') assert status == bootloader.status.kStatus_Success print('\nCumulative fill %s:' % memType), status, results = bl.fill_memory(fillStartAddress, fillBytesLength, secondFillPattern, 'byte') if (memType == 'ram'): assert status == bootloader.status.kStatus_Success elif (memType == 'flash'): assert ((status == bootloader.status.kStatus_FlashCommandFailure) or (status == bootloader.status.kStatusMemoryCumulativeWrite)) # EOF
def write_unaligned_memory_address(bl, memType, memIndex, usingSbComamnd=False): (availableMemStartAddress, availableMemEndAddress, availableMemSize) = common_util.get_available_memory_region( bl, memType, memIndex) if (availableMemSize == 0 ): # K3S TO1.0, all the M4 ITCM SRAM regions are reserved. print("available%s%dSize = %d Bytes." % (memType.capitalize(), memIndex, availableMemSize)) print("No need to write the unagined memory address for %s%d!" % (memType.capitalize(), memIndex)) return writeBytesLength = 0x10 for i in range(1, bl.target.programAlignmentSize): writeStartAddress = availableMemStartAddress + i # unaligned address writeEndAddress = writeStartAddress + writeBytesLength # If not in the available region, just quit the loop if (writeStartAddress + writeBytesLength > availableMemEndAddress): break # If in the available region else: binFilePath = common_util.generate_random_data_file( bl, writeStartAddress, writeBytesLength) if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_write_memory( bdContent, binFilePath, writeStartAddress, writeEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) status, results = bl.receive_sb_file(sbFile) else: status, results = bl.write_memory(writeStartAddress, binFilePath) # 1. For SRAM, should return success if (memType == 'ram'): assert status == bootloader.status.kStatus_Success verifyWriteResult = common_util.verify_write_memory( bl, writeStartAddress, binFilePath) assert verifyWriteResult == True elif (memType == 'flash'): assert status == bootloader.status.kStatus_FlashAlignmentError
def fill_start_of_available_memory_region(bl, memType, memIndex, pattern, bytesNumber, format, usingSbComamnd=False): (availableMemStartAddress, availableMemEndAddress, availableMemSize) = common_util.get_available_memory_region( bl, memType, memIndex) if (availableMemSize == 0 ): # K3S TO1.0, all the M4 ITCM SRAM regions are reserved. print("available%s%dSize = %d Bytes." % (memType.capitalize(), memIndex, availableMemSize)) print("No available region for %s%d!" % (memType.capitalize(), memIndex)) return if (bytesNumber == 'halfOfAvailableMemory'): bytesNumber = availableMemSize / 2 elif (bytesNumber == 'allOfAvailableMemory'): bytesNumber = availableMemSize if (bytesNumber <= availableMemSize): fillStartAddress = availableMemStartAddress fillBytesLength = bytesNumber fillEndAddress = fillStartAddress + fillBytesLength if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_fill_memory( bdContent, pattern, format, fillStartAddress, fillEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) status, results = bl.receive_sb_file(sbFile) else: status, results = bl.fill_memory(fillStartAddress, fillBytesLength, pattern, format) assert status == bootloader.status.kStatus_Success verifyFillResult = common_util.verify_fill_memory( bl, fillStartAddress, fillBytesLength, pattern, format) assert verifyFillResult == True else: print("fillLength = %d Bytes > available%s%dSize = %d Bytes." % (bytesNumber, memType.capitalize(), memIndex, availableMemSize))
def write_start_of_available_memory_region(bl, memType, memIndex, bytesNumber, usingSbComamnd=False): (availableMemStartAddress, availableMemEndAddress, availableMemSize) = common_util.get_available_memory_region( bl, memType, memIndex) if (availableMemSize == 0 ): # K3S TO1.0, all the M4 ITCM SRAM regions are reserved. print("available%s%dSize = %d Bytes." % (memType.capitalize(), memIndex, availableMemSize)) print("No available region for %s%d!" % (memType.capitalize(), memIndex)) return if (bytesNumber == 'halfOfAvailableMemory'): bytesNumber = availableMemSize / 2 elif (bytesNumber == 'allOfAvailableMemory'): bytesNumber = availableMemSize if (bytesNumber <= availableMemSize): writeStartAddress = availableMemStartAddress writeBytesLength = bytesNumber writeEndAddress = writeStartAddress + writeBytesLength binFilePath = common_util.generate_random_data_file( bl, writeStartAddress, writeBytesLength) if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_write_memory( bdContent, binFilePath, writeStartAddress, writeEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) status, results = bl.receive_sb_file(sbFile) else: status, results = bl.write_memory(writeStartAddress, binFilePath) assert status == bootloader.status.kStatus_Success verifyWriteResult = common_util.verify_write_memory( bl, writeStartAddress, binFilePath) assert verifyWriteResult == True else: print("writeLength = %d Bytes > available%s%dSize = %d Bytes." % (bytesNumber, memType.capitalize(), memIndex, availableMemSize))
def cumulative_write(bl, memType, memIndex, usingSbComamnd=False): (availableMemStartAddress, availableMemEndAddress, availableMemSize) = common_util.get_available_memory_region( bl, memType, memIndex) if (availableMemSize == 0 ): # K3S TO1.0, all the M4 ITCM SRAM regions are reserved. print("available%s%dSize = %d Bytes." % (memType.capitalize(), memIndex, availableMemSize)) print("No need to test cumulative write for %s%d!" % (memType.capitalize(), memIndex)) return else: writeStartAddress = availableMemStartAddress writeBytesLength = 0x400 writeEndAddress = writeStartAddress + writeBytesLength # Generate two random bin files. binFilePath1 = common_util.generate_random_data_file( bl, writeStartAddress, writeBytesLength) binFilePath2 = common_util.generate_random_data_file( bl, writeStartAddress, writeBytesLength) if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_write_memory( bdContent, binFilePath1, writeStartAddress, writeEndAddress) bdContent = SBFile.bd_content_update_for_write_memory( bdContent, binFilePath2, writeStartAddress, writeEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) status, results = bl.receive_sb_file(sbFile) else: status, results = bl.write_memory(writeStartAddress, binFilePath1) assert status == bootloader.status.kStatus_Success print('\nCumulative write %s:' % memType), status, results = bl.write_memory(writeStartAddress, binFilePath2) if (memType == 'ram'): assert status == bootloader.status.kStatus_Success elif (memType == 'flash'): assert ((status == bootloader.status.kStatus_FlashCommandFailure) or (status == bootloader.status.kStatusMemoryCumulativeWrite)) # EOF
def test_sb_erase_out_of_range(self, bl, memType): startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) # Here we just need a small amount data for this case. Set the erase length as 1 sector. length = common_util.get_flash_sector_size(bl) # Make sure the start_addr is anligned and near the end of the memory region. sb_commandDictionay[ 'flashEraseRegion'].startAddress = endAddress + 1 - bl.target.eraseAlignmentSize sb_commandDictionay[ 'flashEraseRegion'].endAddress = sb_commandDictionay[ 'flashEraseRegion'].startAddress + length sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'flashEraseRegion') status, results = bl.receive_sb_file(sbFilePath) assert status == bootloader.status.kStatusMemoryRangeInvalid
def test_sb_erase_available_flash_region(self, bl, availableFlashSize): startAddress, endAddress, length = common_util.get_available_memory_region( bl, 'flash') # 1. Get actual erased length according to the parameter if availableFlashSize == 'zero': length = 0 elif availableFlashSize == 'oneSectorSize': length = common_util.get_flash_sector_size(bl) elif availableFlashSize == 'allAvailableSize': length = length # 2. erase with blhost command status, results = bl.flash_erase_region(startAddress, length) assert status == bootloader.status.kStatus_Success # 3. write random data to [startAddress, endAddress] randomFile = common_util.generate_random_data_file( bl, startAddress, length) status, results = bl.write_memory(startAddress, randomFile) assert status == bootloader.status.kStatus_Success # 4. generate sb file and erase with sb command sb_commandDictionay['flashEraseRegion'].startAddress = startAddress sb_commandDictionay[ 'flashEraseRegion'].endAddress = startAddress + length sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'flashEraseRegion') status, results = bl.receive_sb_file(sbFilePath) assert status == bootloader.status.kStatus_Success # 5. read data from [startAddress, endAddress] filePath = os.path.join(bl.vectorsDir, 'read_data_from_memory.bin') status, results == bl.read_memory(startAddress, length, filePath) assert status == bootloader.status.kStatus_Success # 6. verify all the data are FF flag = True fileObj = open(filePath, 'rb') for i in range(0, length): fileObj.seek(i) data_tmp = fileObj.read(1) if ord(data_tmp) != 0xFF: flag = False break fileObj.close() assert flag == True if bl.target.bootloaderType == bootsources.kBootROM_ExecuteROM: # 7. unsecure the flash status, results = bl.flash_erase_all_unsecure() assert status == bootloader.status.kStatus_Success else: pass
def test_sb_erase_all_internal_flash(self, bl): # 1. generate sb file with "erase all" and send the sb file to target, should be success sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'internalFlashEraseAll') status, results = bl.receive_sb_file(sbFilePath) assert status == bootloader.status.kStatus_Success # 2. get flash region that can be erased by 'erase all' with sb file startAddress, endAddress, length = common_util.get_available_memory_region( bl, 'flash') # 3. write random data to [startAddress, endAddress], should be success randomFile = common_util.generate_random_data_file( bl, startAddress, length) status, results = bl.write_memory(startAddress, randomFile) assert status == bootloader.status.kStatus_Success # 4. erase all the internal flash with sb file, should be success status, results = bl.receive_sb_file(sbFilePath) assert status == bootloader.status.kStatus_Success # 5. read data from [startAddress, endAddress] filePath = os.path.join(bl.vectorsDir, 'read_data_from_memory.bin') status, results == bl.read_memory(startAddress, length, filePath) assert status == bootloader.status.kStatus_Success # 6. verify all the data are FF flag = True fileObj = open(filePath, 'rb') for i in range(0, length): fileObj.seek(i) data_tmp = fileObj.read(1) if ord(data_tmp) != 0xFF: flag = False break fileObj.close() assert flag == True if bl.target.bootloaderType == bootsources.kBootROM_ExecuteROM: # 7. reset the target, flash should be in secure state status, results = bl.reset() assert status == bootloader.status.kStatus_Success time.sleep(2) status, results = bl.get_property( bootloader.properties.kPropertyTag_FlashSecurityState) assert status == bootloader.status.kStatus_Success assert results[0] == 1 # 8. unsecure the flash status, results = bl.flash_erase_all_unsecure() assert status == bootloader.status.kStatus_Success else: pass
def test_sb_fill_memory_out_of_range(self, bl, memType): sb_commandDictionay['fillMemory'].cumulativeFill = False sb_commandDictionay['fillMemory'].pattern = kFilledValue sb_commandDictionay['fillMemory'].patternFormat = 'word' startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) # Here we just need a small amount data for this case. Set the length as 1KB. length = 0x400 # Make sure the start_addr is anligned and near the end of the memory region. sb_commandDictionay[ 'fillMemory'].startAddress = endAddress + 1 - bl.target.programAlignmentSize sb_commandDictionay['fillMemory'].endAddress = sb_commandDictionay[ 'fillMemory'].startAddress + length sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'fillMemory') status, results = bl.receive_sb_file(sbFilePath) assert status == bootloader.status.kStatusMemoryRangeInvalid
def erase_unaligned_memory_address(bl, memType, memIndex, usingSbComamnd=False): (availableMemStartAddress, availableMemEndAddress, availableMemSize) = common_util.get_available_memory_region( bl, memType, memIndex) if (availableMemSize == 0 ): # K3S TO1.0, all the M4 ITCM SRAM regions are reserved. print("available%s%dSize = %d Bytes." % (memType.capitalize(), memIndex, availableMemSize)) print("No need to erase the unagined memory address for %s%d!" % (memType.capitalize(), memIndex)) return for i in range(1, bl.target.eraseAlignmentSize): eraseStartAddress = availableMemStartAddress + i # unaligned address eraseBytesLength = bl.target.eraseAlignmentSize eraseEndAddress = eraseStartAddress + eraseBytesLength # If not in the available region, just quit the loop if (eraseStartAddress + eraseBytesLength > availableMemEndAddress): break # If in the available region else: if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_flash_erase_region( bdContent, eraseStartAddress, eraseEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) status, results = bl.receive_sb_file(sbFile) else: status, results = bl.flash_erase_region( eraseStartAddress, eraseBytesLength) if (memType == 'ram'): assert status == bootloader.status.kStatus_FlashAddressError elif (memType == 'flash'): assert status == bootloader.status.kStatus_FlashAlignmentError
def test_sb_erase_unaligned_length(self, bl, memType): startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) # Here we just need a small amount data for this case. Set the length as 1KB. length = 0x400 alignBase = bl.target.eraseAlignmentSize for i in range(1, alignBase): sb_commandDictionay['flashEraseRegion'].startAddress = startAddress sb_commandDictionay[ 'flashEraseRegion'].endAddress = startAddress + length + i sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'flashEraseRegion') status, results = bl.receive_sb_file(sbFilePath) if memType == 'flash': assert status == bootloader.status.kStatus_FlashAlignmentError elif memType == 'ram': # erase ram region (including available region and reserved reigion) should return kStatus_FlashAddressError assert status == bootloader.status.kStatus_FlashAddressError
def test_sb_cumulative_fill(self, bl, memType): sb_commandDictionay['fillMemory'].cumulativeFill = True sb_commandDictionay['fillMemory'].pattern = kFilledValue sb_commandDictionay['fillMemory'].patternFormat = 'word' startAddress, endAddress, length = common_util.get_available_memory_region( bl, memType) # Here we just need a small amount data for this case. Set the length as 1KB. length = 0x400 sb_commandDictionay['fillMemory'].startAddress = startAddress sb_commandDictionay['fillMemory'].endAddress = startAddress + length sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'fillMemory') status, results = bl.receive_sb_file(sbFilePath) if memType == 'flash': assert status == bootloader.status.kStatus_FlashCommandFailure or status == bootloader.status.kStatusMemoryCumulativeWrite elif memType == 'ram': assert status == bootloader.status.kStatus_Success
def test_sb_erase_available_ram_region(self, bl, availableRamSize): startAddress, endAddress, length = common_util.get_available_memory_region( bl, 'ram') # 1. Get actual erased length according to the parameter if availableRamSize == 'zero': length = 0 elif availableRamSize == 'oneSectorSize': length = common_util.get_flash_sector_size(bl) elif availableRamSize == 'allAvailableSize': length = length # 2. generate sb file and erase with sb command sb_commandDictionay['flashEraseRegion'].startAddress = startAddress sb_commandDictionay[ 'flashEraseRegion'].endAddress = startAddress + length sbFilePath = sb_command.generate_sb_file(bl, 'unencrypted', '', 'flashEraseRegion') status, results = bl.receive_sb_file(sbFilePath) if length == 0: assert status == bootloader.status.kStatus_Success else: # erase ram region (including available region and reserved reigion) should return kStatus_FlashAddressError assert status == bootloader.status.kStatus_FlashAddressError
def erase_sectors_at_start_of_available_region(bl, memType, memIndex, sectorsNumber, usingSbComamnd=False): (availableMemStartAddress, availableMemEndAddress, availableMemSize) = common_util.get_available_memory_region( bl, memType, memIndex) if (availableMemSize == 0 ): # K3S TO1.0, all the M4 ITCM SRAM regions are reserved. print("available%s%dSize = %d Bytes." % (memType.capitalize(), memIndex, availableMemSize)) print("No available region for %s%d!" % (memType.capitalize(), memIndex)) return if (str(sectorsNumber).isdigit()): # Get flash sector size flashSectorSize = common_util.get_flash_sector_size(bl, memIndex) eraseBytes = sectorsNumber * flashSectorSize elif (sectorsNumber == 'halfOfAvailableMemory'): eraseBytes = availableMemSize / 2 elif (sectorsNumber == 'allOfAvailableMemory'): eraseBytes = availableMemSize if (eraseBytes <= availableMemSize): eraseStartAddress = availableMemStartAddress eraseBytesLength = eraseBytes eraseEndAddress = eraseStartAddress + eraseBytesLength if (memType == 'ram'): if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_flash_erase_region( bdContent, eraseStartAddress, eraseEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) status, results = bl.receive_sb_file(sbFile) assert status == bootloader.status.kStatus_FlashAddressError else: status, results = bl.flash_erase_region( eraseStartAddress, eraseBytesLength) assert status == bootloader.status.kStatus_FlashAddressError elif (memType == 'flash'): print("Program the %s%d:" % (memType.capitalize(), memIndex)), status, results = bl.fill_memory(availableMemStartAddress, eraseBytes, 0x12345678, 'word') assert status == bootloader.status.kStatus_Success if (usingSbComamnd == True): bdContent = SBFile.bd_content_init(bl, needSign=False) bdContent = SBFile.bd_content_update_for_flash_erase_region( bdContent, eraseStartAddress, eraseEndAddress) bdFile = SBFile.bd_content_finish(bl, bdContent) sbFile = SBFile.generate_sb_file(bl, bdFile, needEncrypt=False, encryptionKey=None, needSign=False, s=[], S=[], R=[]) print("Erase the programmed data with erase sb command:"), status, results = bl.receive_sb_file(sbFile) assert status == bootloader.status.kStatus_Success else: print( "Erase the programmed data with flash-erase-region command:" ), status, results = bl.flash_erase_region( eraseStartAddress, eraseBytesLength) assert status == bootloader.status.kStatus_Success verifyEraseResult = common_util.verify_flash_erase( bl, eraseStartAddress, eraseBytesLength) assert verifyEraseResult == True else: print("eraseBytes = %d Bytes > available%s%dSize = %d Bytes." % (eraseBytes, memType.capitalize(), memIndex, availableMemSize))
def test_write_all_available_memory(self, bl, memType): availableRegionStartAddress, availableRegionEndAddress, availableRegionSize = common_util.get_available_memory_region( bl, memType) # Create a random file which contains availableRegionSize bytes. randomFile = common_util.generate_random_data_file( bl, availableRegionStartAddress, availableRegionSize) # write file to the specific available flash region status, results = bl.write_memory(availableRegionStartAddress, randomFile) assert status == bootloader.status.kStatus_Success # Read back the data from Flash readFile = os.path.join(bl.vectorsDir, 'read_data_from_memory.bin') status, results = bl.read_memory(availableRegionStartAddress, availableRegionSize, readFile) assert status == bootloader.status.kStatus_Success # The two data files should be the same. assert True == common_util.file_comparison(randomFile, readFile)
def test_fill_all_available_ram_with_pattern_format( self, bl, patternFormat): availableRegionStartAddress, availableRegionEndAddress, availableRegionSize = common_util.get_available_memory_region( bl, 'ram') verify_fill_memory_with_pattern_format(bl, availableRegionStartAddress, availableRegionSize, patternFormat, availableRegionSize)
def test_all_supported_can_speed(self, bl, canSpeed): # Change the can speed bl.set_CAN_parameters(canSpeed) common_util.reset_with_check(bl) # 1. Get available flash region availableRegionStartAddress, availableRegionEndAddress, availableRegionSize = common_util.get_available_memory_region(bl, 'flash') # 2. Erase all the available flash region status, results = bl.flash_erase_region(availableRegionStartAddress, availableRegionSize) assert status == bootloader.status.kStatus_Success # 3. Program all the available flash region randomFile = common_util.generate_random_data_file(bl, availableRegionStartAddress, availableRegionSize) status, results = bl.write_memory(availableRegionStartAddress, randomFile) assert status == bootloader.status.kStatus_Success # 4. Read back the data readFile = os.path.join(bl.vectorsDir, 'read_data_from_memory.bin') status, results = bl.read_memory(availableRegionStartAddress, availableRegionSize, readFile) assert status == bootloader.status.kStatus_Success # 5. Verify data randomFileObj = file(randomFile, 'rb') data1 = randomFileObj.read() readFileObj = file(readFile, 'rb') data2 = readFileObj.read() assert data1 == data2
def test_erase_all_available_flash(self, bl): availableRegionStartAddress, availableRegionEndAddress, availableRegionSize = common_util.get_available_memory_region( bl, 'flash') # 1. erase all the flash status, results = bl.flash_erase_all() assert status == bootloader.status.kStatus_Success # 2. write random data to [availableRegionStartAddress, availableRegionEndAddress] randomFile = common_util.generate_random_data_file( bl, availableRegionStartAddress, availableRegionSize) status, results = bl.write_memory(availableRegionStartAddress, randomFile) assert status == bootloader.status.kStatus_Success # 3. erase flash status, results = bl.flash_erase_region(availableRegionStartAddress, availableRegionSize) assert status == bootloader.status.kStatus_Success # 4. read data from [availableRegionStartAddress, availableRegionEndAddress] filePath = os.path.join(bl.vectorsDir, 'read_data_from_memory.bin') status, results == bl.read_memory(availableRegionStartAddress, availableRegionSize, filePath) assert status == bootloader.status.kStatus_Success # 5. verify all the data are FF flag = True fileObj = open(filePath, 'rb') for i in range(0, availableRegionSize): fileObj.seek(i) data_tmp = fileObj.read(1) if ord(data_tmp) != 0xFF: flag = False break fileObj.close() assert flag == True
def test_cumulative_fill(self, bl): availableRegionStartAddress, availableRegionEndAddress, availableRegionSize = common_util.get_available_memory_region( bl, 'flash') # fill pattern 0x00 to the specific unreserved flash region status, results = bl.fill_memory(availableRegionStartAddress, 0x400, 0x00, 'byte') assert status == bootloader.status.kStatus_Success # attempt to write 0 to 1, expected return value is kStatus_FlashCommandFailure status, results = bl.fill_memory(availableRegionStartAddress, 0x400, 0xFF, 'byte') assert status == bootloader.status.kStatus_FlashCommandFailure or status == bootloader.status.kStatusMemoryCumulativeWrite
def test_cumulative_write(self, bl): availableRegionStartAddress, availableRegionEndAddress, availableRegionSize = common_util.get_available_memory_region( bl, 'flash') # Create a random file which contains 1KB bytes. randomFile = common_util.generate_random_data_file( bl, availableRegionStartAddress, 0x400) # write file to the specific available flash region status, results = bl.write_memory(availableRegionStartAddress, randomFile) assert status == bootloader.status.kStatus_Success # Create a random file which contains 1KB bytes. randomFile = common_util.generate_random_data_file( bl, availableRegionStartAddress, 0x400) # attempt to write 0 to 1, expected return value is kStatus_FlashCommandFailure status, results = bl.write_memory(availableRegionStartAddress, randomFile) assert status == bootloader.status.kStatus_FlashCommandFailure or status == bootloader.status.kStatusMemoryCumulativeWrite