def flashBootableImage ( self ): self._prepareForBootDeviceOperation() imageLen = os.path.getsize(self.destAppFilename) if self.bootDevice == uidef.kBootDevice_SemcNand: semcNandOpt, semcNandFcbOpt, imageInfo = uivar.getBootDeviceConfiguration(self.bootDevice) memEraseLen = misc.align_up(imageLen, self.semcNandBlockSize) for i in range(self.semcNandImageCopies): imageLoadAddr = self.bootDeviceMemBase + (imageInfo[i] >> 16) * self.semcNandBlockSize status, results, cmdStr = self.blhost.flashEraseRegion(imageLoadAddr, memEraseLen, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.writeMemory(imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False elif self.bootDevice == uidef.kBootDevice_FlexspiNor: if not self.isFlexspiNorErasedForImage: self._eraseFlexspiNorForImageLoading() if self.secureBootType == uidef.kSecureBootType_Development or \ self.secureBootType == uidef.kSecureBootType_HabAuth or \ (self.secureBootType == uidef.kSecureBootType_BeeCrypto and self.keyStorageRegion == uidef.kKeyStorageRegion_FlexibleUserKeys): self._programFlexspiNorConfigBlock() if self.secureBootType == uidef.kSecureBootType_BeeCrypto and self.keyStorageRegion == uidef.kKeyStorageRegion_FlexibleUserKeys: self._genDestEncAppFileWithoutCfgBlock() imageLoadAddr = self.bootDeviceMemBase + rundef.kFlexspiNorCfgInfo_Length status, results, cmdStr = self.blhost.writeMemory(imageLoadAddr, self.destEncAppNoCfgBlockFilename, self.bootDeviceMemId) self.printLog(cmdStr) else: imageLoadAddr = self.bootDeviceMemBase + gendef.kIvtOffset_NOR status, results, cmdStr = self.blhost.writeMemory(imageLoadAddr, self.destAppNoPaddingFilename, self.bootDeviceMemId) self.printLog(cmdStr) self.isFlexspiNorErasedForImage = False else: pass
def readBootDeviceMemory( self ): status, memStart, memLength = self._getUserComMemParameters(False) if status: memStart = self._convertComMemStart(memStart) alignedMemStart = misc.align_down(memStart, self.comMemReadUnit) alignedMemLength = misc.align_up(memLength, self.comMemReadUnit) + self.comMemReadUnit if memLength + memStart > alignedMemStart + self.comMemReadUnit: alignedMemLength += self.comMemReadUnit memFilename = 'comMemRead.dat' memFilepath = os.path.join(self.blhostVectorsDir, memFilename) status, results, cmdStr = self.blhost.readMemory(alignedMemStart, alignedMemLength, memFilename, self.bootDeviceMemId) self.printLog(cmdStr) if status == boot.status.kStatus_Success: self.clearMem() memLeft = memLength addr = memStart with open(memFilepath, 'rb') as fileObj: fileObj.seek(memStart - alignedMemStart) while memLeft > 0: contentToShow, memContent = self._getOneLineContentToShow(addr, memLeft, fileObj) memLeft -= len(memContent) addr += len(memContent) self.printMem(contentToShow) else: self.popupMsgBox('Failed to read boot device, error code is %d !' %(status))
def readBootDeviceMemory( self ): status, memStart, memLength = self._getUserComMemParameters(False) if status: memStart = self._convertComMemStart(memStart) alignedMemStart = misc.align_down(memStart, self.comMemReadUnit) alignedMemLength = misc.align_up(memLength, self.comMemReadUnit) if memLength + memStart > alignedMemStart + alignedMemLength: alignedMemLength += self.comMemReadUnit memFilename = 'commonDataFromBootDevice.dat' memFilepath = os.path.join(self.blhostVectorsDir, memFilename) status, results, cmdStr = self.blhost.readMemory(alignedMemStart, alignedMemLength, memFilename, self.bootDeviceMemId) self.printLog(cmdStr) if status == boot.status.kStatus_Success: self.clearMem() if not self.needToSaveReadbackImageData(): memLeft = memLength addr = memStart with open(memFilepath, 'rb') as fileObj: fileObj.seek(memStart - alignedMemStart) while memLeft > 0: contentToShow, memContent = self.getOneLineContentToShow(addr, memLeft, fileObj) memLeft -= len(memContent) addr += len(memContent) self.printMem(contentToShow) else: self.tryToSaveImageDataFile(memFilepath) else: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox('Failed to read boot device, error code is %d !' %(status)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"读取启动设备失败,错误的代码是 %d !" %(status)) else: pass
def writeBootDeviceMemory(self): status, memStart, memBinFile = self._getUserComMemParameters(True) if status: memStart = self._convertComMemStart(memStart) if memStart % self.comMemWriteUnit: self.popupMsgBox( 'Start Address should be aligned with 0x%x !' % (self.comMemWriteUnit)) return eraseMemStart = misc.align_down(memStart, self.comMemEraseUnit) eraseMemEnd = misc.align_up(memStart + os.path.getsize(memBinFile), self.comMemEraseUnit) status, results, cmdStr = self.blhost.flashEraseRegion( eraseMemStart, eraseMemEnd - eraseMemStart, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: self.popupMsgBox( 'Failed to erase boot device, error code is %d !' % (status)) return shutil.copy(memBinFile, self.userFilename) status, results, cmdStr = self.blhost.writeMemory( memStart, self.userFilename, self.bootDeviceMemId) try: os.remove(self.userFilename) except: pass self.printLog(cmdStr) if status != boot.status.kStatus_Success: self.popupMsgBox( 'Failed to write boot device, error code is %d, You may forget to erase boot device first!' % (status))
def _eraseXspiNorForImageLoading(self): imageLen = os.path.getsize(self.destAppFilename) imageLen += RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM memEraseLen = misc.align_up(imageLen, self.comMemEraseUnit) status = None cmdStr = '' if self.isSbFileEnabledToGen: self._RTxxx_addFlashActionIntoSbAppBdContent( " erase " + self.sbAccessBootDeviceMagic + " " + self.convertLongIntHexText( str(hex(self.tgt.flexspiNorMemBase))) + ".." + self.convertLongIntHexText( str(hex(self.tgt.flexspiNorMemBase + memEraseLen))) + ";\n") else: if self.bootDeviceMemId == rundef.kBootDeviceMemId_FlexspiNor: status, results, cmdStr = self.blhost.flashEraseRegion( self.tgt.flexspiNorMemBase, memEraseLen, self.bootDeviceMemId) elif self.bootDeviceMemId == rundef.kBootDeviceMemId_QuadspiNor: status, results, cmdStr = self.blhost.flashEraseRegion( self.tgt.quadspiNorMemBase, memEraseLen, self.bootDeviceMemId) else: pass self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False self.isXspiNorErasedForImage = True return True
def _eraseC040hdNorForImageLoading(self): imageLen = os.path.getsize(self.destAppFilename) memEraseLen = misc.align_up(imageLen, self.comMemEraseUnit) status, results, cmdStr = self.blhost.flashEraseRegion( self.tgt.c040hdNorMemBase, memEraseLen) self.printLog(cmdStr) return (status == boot.status.kStatus_Success)
def _eraseFlexspiNorForImageLoading( self ): imageLen = os.path.getsize(self.destAppFilename) memEraseLen = misc.align_up(imageLen, self.flexspiNorSectorSize) status, results, cmdStr = self.blhost.flashEraseRegion(rundef.kBootDeviceMemBase_FlexspiNor, memEraseLen, rundef.kBootDeviceMemId_FlexspiNor) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False self.isFlexspiNorErasedForImage = True
def eraseBootDeviceMemory( self ): status, memStart, memLength = self._getUserComMemParameters(False) if status: memStart = self._convertComMemStart(memStart) alignedMemStart = misc.align_down(memStart, self.comMemEraseUnit) alignedMemLength = misc.align_up(memLength, self.comMemEraseUnit) if memLength + memStart > alignedMemStart + self.comMemEraseUnit: alignedMemLength += self.comMemEraseUnit status, results, cmdStr = self.blhost.flashEraseRegion(alignedMemStart, alignedMemLength, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: self.popupMsgBox('Failed to erase boot device, error code is %d !' %(status))
def writeBootDeviceMemory(self): status, memStart, memBinFile = self._getUserComMemParameters(True) if status: memStart = self._convertComMemStart(memStart) if memStart % self.comMemWriteUnit: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'Start Address should be aligned with 0x%x !' % (self.comMemWriteUnit)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"起始地址应该以 0x%x 对齐!" % (self.comMemWriteUnit)) else: pass return eraseMemStart = misc.align_down(memStart, self.comMemEraseUnit) eraseMemEnd = misc.align_up(memStart + os.path.getsize(memBinFile), self.comMemEraseUnit) status, results, cmdStr = self.blhost.flashEraseRegion( eraseMemStart, eraseMemEnd - eraseMemStart, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'Failed to erase boot device, error code is %d !' % (status)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"擦除启动设备失败,错误的代码是 %d !" % (status)) else: pass return shutil.copy(memBinFile, self.userFilename) status, results, cmdStr = self.blhost.writeMemory( memStart, self.userFilename, self.bootDeviceMemId) try: os.remove(self.userFilename) except: pass self.printLog(cmdStr) if status != boot.status.kStatus_Success: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'Failed to write boot device, error code is %d, You may forget to erase boot device first!' % (status)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"写入启动设备失败,错误的代码是 %d ,请确认是否先擦除了启动设备!" % (status)) else: pass
def eraseBootDeviceMemory( self ): status, memStart, memLength = self._getUserComMemParameters(False) if status: memStart = self._convertComMemStart(memStart) alignedMemStart = misc.align_down(memStart, self.comMemEraseUnit) alignedMemLength = misc.align_up(memLength, self.comMemEraseUnit) if memLength + memStart > alignedMemStart + alignedMemLength: alignedMemLength += self.comMemEraseUnit status, results, cmdStr = self.blhost.flashEraseRegion(alignedMemStart, alignedMemLength, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox('Failed to erase boot device, error code is %d !' %(status)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"擦除启动设备失败,错误的代码是 %d !" %(status)) else: pass
def _eraseXspiNorForImageLoading( self ): imageLen = os.path.getsize(self.destAppFilename) imageLen += RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM memEraseLen = misc.align_up(imageLen, self.comMemEraseUnit) status = None cmdStr = '' if self.bootDeviceMemId == rundef.kBootDeviceMemId_FlexspiNor: status, results, cmdStr = self.blhost.flashEraseRegion(self.tgt.flexspiNorMemBase, memEraseLen, self.bootDeviceMemId) elif self.bootDeviceMemId == rundef.kBootDeviceMemId_QuadspiNor: status, results, cmdStr = self.blhost.flashEraseRegion(self.tgt.quadspiNorMemBase, memEraseLen, self.bootDeviceMemId) else: pass self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False self.isXspiNorErasedForImage = True return True
def RTxxx_flashBootableImage(self): self._RTxxx_prepareForBootDeviceOperation() imageLen = os.path.getsize(self.destAppFilename) if self.bootDevice == RTxxx_uidef.kBootDevice_FlexspiNor or \ self.bootDevice == RTxxx_uidef.kBootDevice_QuadspiNor: if not self.isXspiNorErasedForImage: if not self._eraseXspiNorForImageLoading(): return False if self.secureBootType == RTxxx_uidef.kSecureBootType_PlainUnsigned or \ self.secureBootType == RTxxx_uidef.kSecureBootType_PlainCrc: if not self._programXspiNorConfigBlock(): self.isXspiNorErasedForImage = False self.isFdcbFromSrcApp = False return False imageLoadAddr = self.bootDeviceMemBase + RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM status, results, cmdStr = self.blhost.writeMemory( imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) self.isXspiNorErasedForImage = False self.isFdcbFromSrcApp = False if status != boot.status.kStatus_Success: return False elif self.bootDevice == RTxxx_uidef.kBootDevice_FlexcommSpiNor: memEraseLen = misc.align_up(imageLen, self.comMemEraseUnit) imageLoadAddr = self.bootDeviceMemBase + RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM status, results, cmdStr = self.blhost.flashEraseRegion( imageLoadAddr, memEraseLen, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.writeMemory( imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False else: pass if self.isConvertedAppUsed: try: os.remove(self.srcAppFilename) except: pass self.isConvertedAppUsed = False return True
def readRamMemory(self): status, memStart, memLength, dummyArg = self._getUserComMemParameters( False) if status: if (self.mcuSeries in uidef.kMcuSeries_iMXRTyyyy and self.isInTheRangeOfFlexram(memStart, memLength)) or \ (self.mcuSeries == uidef.kMcuSeries_iMXRTxxx and self.isInTheRangeOfSram(memStart, memLength)) or \ (self.mcuSeries == uidef.kMcuSeries_LPC and self.isInTheRangeOfSramx(memStart, memLength)) or \ (self.mcuSeries == uidef.kMcuSeries_Kinetis and self.isInTheRangeOfSram(memStart, memLength)): alignedMemStart = misc.align_down(memStart, 0x10) alignedMemLength = misc.align_up(memLength, 0x10) if memLength + memStart > alignedMemStart + alignedMemLength: alignedMemLength += 0x10 memFilename = 'commonDataFromRam.dat' memFilepath = os.path.join(self.blhostVectorsDir, memFilename) status, results, cmdStr = self.blhost.readMemory( alignedMemStart, alignedMemLength, memFilename) self.printLog(cmdStr) if status == boot.status.kStatus_Success: self.clearMem() memLeft = memLength addr = memStart with open(memFilepath, 'rb') as fileObj: fileObj.seek(memStart - alignedMemStart) while memLeft > 0: contentToShow, memContent = self.getOneLineContentToShow( addr, memLeft, fileObj) memLeft -= len(memContent) addr += len(memContent) self.printMem(contentToShow) self.tryToSaveImageDataFile(memFilepath) else: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'Failed to read RAM, error code is %d .' % (status)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"读取FlexRAM失败,错误的代码是 %d 。" % (status)) else: pass else: self.popupMsgBox( uilang.kMsgLanguageContentDict['operImgError_notInRam'][ self.languageIndex])
def prepareForFixedOtpmkEncryption( self ): self._prepareForBootDeviceOperation() self._showOtpmkDek() self._eraseFlexspiNorForImageLoading() otpmkKeyOpt, otpmkEncryptedRegionStart, otpmkEncryptedRegionLength = uivar.getAdvancedSettings(uidef.kAdvancedSettings_OtpmkKey) # Prepare PRDB options #--------------------------------------------------------------------------- # 0xe0120000 is an option for PRDB contruction and image encryption # bit[31:28] tag, fixed to 0x0E # bit[27:24] Key source, fixed to 0 for A0 silicon # bit[23:20] AES mode: 1 - CTR mode # bit[19:16] Encrypted region count # bit[15:00] reserved in A0 #--------------------------------------------------------------------------- encryptedRegionCnt = (otpmkKeyOpt & 0x000F0000) >> 16 if encryptedRegionCnt == 0: otpmkKeyOpt = (otpmkKeyOpt & 0xFFF0FFFF) | (0x1 << 16) encryptedRegionCnt = 1 otpmkEncryptedRegionStart[0] = rundef.kBootDeviceMemBase_FlexspiNor + gendef.kIvtOffset_NOR otpmkEncryptedRegionLength[0] = misc.align_up(os.path.getsize(self.destAppFilename), gendef.kSecFacRegionAlignedUnit) - gendef.kIvtOffset_NOR else: pass status, results, cmdStr = self.blhost.fillMemory(rundef.kRamFreeSpaceStart_LoadPrdbOpt, 0x4, otpmkKeyOpt) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False for i in range(encryptedRegionCnt): status, results, cmdStr = self.blhost.fillMemory(rundef.kRamFreeSpaceStart_LoadPrdbOpt + i * 8 + 4, 0x4, otpmkEncryptedRegionStart[i]) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.fillMemory(rundef.kRamFreeSpaceStart_LoadPrdbOpt + i * 8 + 8, 0x4, otpmkEncryptedRegionLength[i]) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.configureMemory(self.bootDeviceMemId, rundef.kRamFreeSpaceStart_LoadPrdbOpt) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False self._programFlexspiNorConfigBlock()
def RTxxx_flashBootableImage(self): self._RTxxx_prepareForBootDeviceOperation() imageLen = os.path.getsize(self.destAppFilename) if self.bootDevice == RTxxx_uidef.kBootDevice_FlexspiNor or \ self.bootDevice == RTxxx_uidef.kBootDevice_QuadspiNor: image0Size = 0 if not self.isXspiNorErasedForImage: if not self._eraseXspiNorForImageLoading(): return False if self.secureBootType == RTxxx_uidef.kSecureBootType_PlainUnsigned or \ self.secureBootType == RTxxx_uidef.kSecureBootType_PlainCrc: if not self._programXspiNorConfigBlock(): self.isXspiNorErasedForImage = False self.isFdcbFromSrcApp = False return False flexspiNorOpt0, flexspiNorOpt1, flexspiNorDeviceModel, isFdcbKept, flexspiNorDualImageInfoList = uivar.getBootDeviceConfiguration( self.bootDevice) if self.tgt.hasFlexspiNorDualImageBoot and ( (flexspiNorDualImageInfoList[2] & 0xFFFF) != 0): if flexspiNorDualImageInfoList[0] == 0xffffffff: self.flexspiNorImage0Version = flexspiNorDualImageInfoList[ 0] else: self.flexspiNorImage0Version = flexspiNorDualImageInfoList[ 0] + ((flexspiNorDualImageInfoList[0] ^ 0xFFFF) << 16) if self.flexspiNorImage0Version != None and self.flexspiNorImage0Version != rundef.kFlexspiNorContent_Blank32: versionLoadAddr = self.bootDeviceMemBase + gendef.kImgVerOffset_NOR if self.isSbFileEnabledToGen: self._RTxxx_addFlashActionIntoSbAppBdContent( " load " + self.convertLongIntHexText( str(hex(self.flexspiNorImage0Version))) + " > " + self.convertLongIntHexText(str(hex(versionLoadAddr))) + ";\n") status = boot.status.kStatus_Success else: status, results, cmdStr = self.blhost.fillMemory( versionLoadAddr, 0x4, self.flexspiNorImage0Version) self.printLog(cmdStr) imageLoadAddr = self.bootDeviceMemBase + RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM if self.isSbFileEnabledToGen: self._RTxxx_addFlashActionIntoSbAppBdContent( " load " + self.sbAccessBootDeviceMagic + " myBinFile > " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ";\n") status = boot.status.kStatus_Success else: status, results, cmdStr = self.blhost.writeMemory( imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) image0Size = imageLoadAddr - self.bootDeviceMemBase + os.path.getsize( self.destAppFilename) self.isXspiNorErasedForImage = False self.isFdcbFromSrcApp = False if status != boot.status.kStatus_Success: return False else: # Check if dual image boot is enabled if self.tgt.hasFlexspiNorDualImageBoot and ( (flexspiNorDualImageInfoList[2] & 0xFFFF) != 0): image1Start = self.bootDeviceMemBase + ( flexspiNorDualImageInfoList[2] & 0xFFFF) * 256 * 1024 image1Size = image0Size if flexspiNorDualImageInfoList[1] == 0xffffffff: self.flexspiNorImage1Version = flexspiNorDualImageInfoList[ 1] else: self.flexspiNorImage1Version = flexspiNorDualImageInfoList[ 1] + ((flexspiNorDualImageInfoList[1] ^ 0xFFFF) << 16) if not self.flash2ndBootableImageIntoFlexspiNor( image1Start, image1Size, self.flexspiNorImage1Version, self.flexspiNorImage0Version): return False elif self.bootDevice == RTxxx_uidef.kBootDevice_FlexcommSpiNor: memEraseLen = misc.align_up(imageLen, self.comMemEraseUnit) imageLoadAddr = self.bootDeviceMemBase + RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM if self.isSbFileEnabledToGen: self._RTxxx_addFlashActionIntoSbAppBdContent( " erase " + self.sbAccessBootDeviceMagic + " " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ".." + self.convertLongIntHexText( str(hex(imageLoadAddr + memEraseLen))) + ";\n") self._RTxxx_addFlashActionIntoSbAppBdContent( " load " + self.sbAccessBootDeviceMagic + " myBinFile > " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ";\n") else: status, results, cmdStr = self.blhost.flashEraseRegion( imageLoadAddr, memEraseLen, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.writeMemory( imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False elif self.bootDevice == RTxxx_uidef.kBootDevice_UsdhcSd or \ self.bootDevice == RTxxx_uidef.kBootDevice_UsdhcMmc: memEraseLen = misc.align_up(imageLen, self.comMemEraseUnit) imageLoadAddr = self.bootDeviceMemBase + RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM if self.isSbFileEnabledToGen: self._RTxxx_addFlashActionIntoSbAppBdContent( " erase " + self.sbAccessBootDeviceMagic + " " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ".." + self.convertLongIntHexText( str(hex(imageLoadAddr + memEraseLen))) + ";\n") self._RTxxx_addFlashActionIntoSbAppBdContent( " load " + self.sbAccessBootDeviceMagic + " myBinFile > " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ";\n") else: status, results, cmdStr = self.blhost.flashEraseRegion( imageLoadAddr, memEraseLen, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.writeMemory( imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False else: pass if self.isConvertedAppUsed: try: os.remove(self.srcAppFilename) except: pass self.isConvertedAppUsed = False return True
def flashHabDekToGenerateKeyBlob ( self ): if os.path.isfile(self.habDekFilename) and self.habDekDataOffset != None: self._prepareForBootDeviceOperation() imageLen = os.path.getsize(self.destAppFilename) imageCopies = 0x1 eraseUnit = 0x0 if self.bootDevice == uidef.kBootDevice_SemcNand: imageCopies = self.semcNandImageCopies eraseUnit = self.semcNandBlockSize elif self.bootDevice == uidef.kBootDevice_FlexspiNor: eraseUnit = self.flexspiNorSectorSize else: pass # Construct KeyBlob Option #--------------------------------------------------------------------------- # bit [31:28] tag, fixed to 0x0b # bit [27:24] type, 0 - Update KeyBlob context, 1 Program Keyblob to SPI NAND # bit [23:20] keyblob option block size, must equal to 3 if type =0, # reserved if type = 1 # bit [19:08] Reserved # bit [07:04] DEK size, 0-128bit 1-192bit 2-256 bit, only applicable if type=0 # bit [03:00] Firmware Index, only applicable if type = 1 # if type = 0, next words indicate the address that holds dek # the 3rd word #---------------------------------------------------------------------------- keyBlobContextOpt = 0xb0300000 keyBlobDataOpt = 0xb1000000 status, results, cmdStr = self.blhost.writeMemory(rundef.kRamFreeSpaceStart_LoadDekData, self.habDekFilename) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.fillMemory(rundef.kRamFreeSpaceStart_LoadKeyBlobContext, 0x4, keyBlobContextOpt) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.fillMemory(rundef.kRamFreeSpaceStart_LoadKeyBlobContext + 4, 0x4, rundef.kRamFreeSpaceStart_LoadDekData) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.fillMemory(rundef.kRamFreeSpaceStart_LoadKeyBlobContext + 8, 0x4, self.habDekDataOffset) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.configureMemory(self.bootDeviceMemId, rundef.kRamFreeSpaceStart_LoadKeyBlobContext) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False for i in range(imageCopies): ramFreeSpace = rundef.kRamFreeSpaceStart_LoadKeyBlobData + (rundef.kRamFreeSpaceStep_LoadKeyBlobData * i) status, results, cmdStr = self.blhost.fillMemory(ramFreeSpace, 0x4, keyBlobDataOpt + i) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False ######################################################################## # Flashloader will not erase keyblob region automatically, so we need to handle it here manually imageLoadAddr = 0x0 if self.bootDevice == uidef.kBootDevice_SemcNand: semcNandOpt, semcNandFcbOpt, imageInfo = uivar.getBootDeviceConfiguration(self.bootDevice) imageLoadAddr = self.bootDeviceMemBase + (imageInfo[i] >> 16) * self.semcNandBlockSize elif self.bootDevice == uidef.kBootDevice_FlexspiNor: imageLoadAddr = self.bootDeviceMemBase else: pass alignedErasedSize = misc.align_up(imageLen, eraseUnit) needToBeErasedSize = misc.align_up(self.habDekDataOffset + rundef.kKeyBlobMaxSize, eraseUnit) if alignedErasedSize < needToBeErasedSize: memEraseLen = needToBeErasedSize - alignedErasedSize alignedMemEraseAddr = imageLoadAddr + alignedErasedSize status, results, cmdStr = self.blhost.flashEraseRegion(alignedMemEraseAddr, memEraseLen, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False ######################################################################## status, results, cmdStr = self.blhost.configureMemory(self.bootDeviceMemId, ramFreeSpace) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False if self.bootDevice == uidef.kBootDevice_FlexspiNor: self._programFlexspiNorConfigBlock() else: self.popupMsgBox('Dek file hasn\'t been generated!')
def RTxxx_flashBootableImage(self): self._RTxxx_prepareForBootDeviceOperation() imageLen = os.path.getsize(self.destAppFilename) if self.bootDevice == RTxxx_uidef.kBootDevice_FlexspiNor or \ self.bootDevice == RTxxx_uidef.kBootDevice_QuadspiNor: if not self.isXspiNorErasedForImage: if not self._eraseXspiNorForImageLoading(): return False if self.secureBootType == RTxxx_uidef.kSecureBootType_PlainUnsigned or \ self.secureBootType == RTxxx_uidef.kSecureBootType_PlainCrc: if not self._programXspiNorConfigBlock(): self.isXspiNorErasedForImage = False self.isFdcbFromSrcApp = False return False imageLoadAddr = self.bootDeviceMemBase + RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM if self.isSbFileEnabledToGen: self._RTxxx_addFlashActionIntoSbAppBdContent( " load " + self.sbAccessBootDeviceMagic + " myBinFile > " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ";\n") status = boot.status.kStatus_Success else: status, results, cmdStr = self.blhost.writeMemory( imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) self.isXspiNorErasedForImage = False self.isFdcbFromSrcApp = False if status != boot.status.kStatus_Success: return False elif self.bootDevice == RTxxx_uidef.kBootDevice_FlexcommSpiNor: memEraseLen = misc.align_up(imageLen, self.comMemEraseUnit) imageLoadAddr = self.bootDeviceMemBase + RTxxx_gendef.kBootImageOffset_NOR_SD_EEPROM if self.isSbFileEnabledToGen: self._RTxxx_addFlashActionIntoSbAppBdContent( " erase " + self.sbAccessBootDeviceMagic + " " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ".." + self.convertLongIntHexText( str(hex(imageLoadAddr + memEraseLen))) + ";\n") self._RTxxx_addFlashActionIntoSbAppBdContent( " load " + self.sbAccessBootDeviceMagic + " myBinFile > " + self.convertLongIntHexText(str(hex(imageLoadAddr))) + ";\n") else: status, results, cmdStr = self.blhost.flashEraseRegion( imageLoadAddr, memEraseLen, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False status, results, cmdStr = self.blhost.writeMemory( imageLoadAddr, self.destAppFilename, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: return False else: pass if self.isConvertedAppUsed: try: os.remove(self.srcAppFilename) except: pass self.isConvertedAppUsed = False return True
def eccWriteBootDeviceMemory(self): if self.bootDeviceMemId != rundef.kBootDeviceMemId_FlexspiNor: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox('ECC-write is only for FlexSPI NOR device!') elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"ECC方式写入仅支持 FlexSPI NOR 设备!") else: pass return if self.tgt.hasFlexspiNorEcc == None or self.tgt.hasFlexspiNorEcc != True: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'ECC-write is not supported by this MCU device!') elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"ECC方式写入在当前 MCU 型号上不被支持!") else: pass return status, memStart, memBinFile, useFlashImageCmd = self._getUserComMemParameters( True) if status: if useFlashImageCmd: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'Image file format can only be binary(.bin) for ECC write!' ) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"ECC方式写入时,程序镜像文件格式仅支持 .bin 格式!") else: pass else: memStart = self._convertComMemStart(memStart) if memStart % memdef.kXeccRegionAlignmentUnit: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'ECC Start Address should be aligned with 0x%x !' % (memdef.kXeccRegionAlignmentUnit)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"ECC写入起始地址应该以 0x%x 对齐!" % (memdef.kXeccRegionAlignmentUnit)) else: pass return eraseMemStart = misc.align_down( memStart, memdef.kXeccRegionAlignmentUnit) eraseMemEnd = misc.align_up( memStart + os.path.getsize(memBinFile), memdef.kXeccRegionAlignmentUnit) status, results, cmdStr = self.blhost.flashEraseRegion( eraseMemStart, (eraseMemEnd - eraseMemStart) * 2, self.bootDeviceMemId) self.printLog(cmdStr) if status != boot.status.kStatus_Success: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'Failed to erase boot device, error code is %d !' % (status)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox(u"擦除启动设备失败,错误的代码是 %d !" % (status)) else: pass return status, results, cmdStr = self.blhost.setProperty( boot.properties.kPropertyTag_FlashXeccWriteState, 1) self.printLog(cmdStr) if (status == boot.status.kStatus_Success): shutil.copy(memBinFile, self.userFilename) status, results, cmdStr = self.blhost.writeMemory( memStart, self.userFilename, self.bootDeviceMemId) try: os.remove(self.userFilename) except: pass self.printLog(cmdStr) if status != boot.status.kStatus_Success: if self.languageIndex == uilang.kLanguageIndex_English: self.popupMsgBox( 'Failed to write boot device, error code is %d, You may forget to erase boot device first!' % (status)) elif self.languageIndex == uilang.kLanguageIndex_Chinese: self.popupMsgBox( u"ECC方式写入启动设备失败,错误的代码是 %d ,请确认是否先擦除了启动设备!" % (status)) else: pass status, results, cmdStr = self.blhost.setProperty( boot.properties.kPropertyTag_FlashXeccWriteState, 0) self.printLog(cmdStr)