Dear sir,
This is my code flow, please see the comment.
Set backdoor key and secure the device if the device is unsecured. And if device is secured, set KEYACC and write backdoor key to unsecure the device.
//If the device is not secured, I set backdoor key and KEYEN to 1, and then secure device.
if((FOPT&0x03)==0x02){
FlashSectorBackup(0xFE00);
vfnFlashErase(0xFFB0);
au8BackDoorKey[0] = 0x70;
au8BackDoorKey[1] = 0x61;
au8BackDoorKey[2] = 0x52;
au8BackDoorKey[3] = 0x43;
au8BackDoorKey[4] = 0x34;
au8BackDoorKey[5] = 0x25;
au8BackDoorKey[6] = 0x16;
au8BackDoorKey[7] = 0x07;
vfnFlashWrite(0xFFB0, (const UINT8*)au8BackDoorKey, N_ELEMENTS(au8BackDoorKey));
u8NewNVOPT = 0xC0;
vfnFlashWrite(0xFFBF, (const UINT8*)&u8NewNVOPT, 1u);
FlashSectorRestore(0xFE00);
}
else{
//After reset, the device is secured, then unsecure the device by set KEYACC to 1 and write backbook key, then set KEYACC to 0.
vfnCopyInRAM();
vfnUnsecure();
FlashSectorBackup(0xFE00);
vfnFlashErase(0xFFBF);
//If unsecured is successful, then set NVOPT to 0x82 to make device be always unsecured.
u8NewNVOPT=0x82;
vfnFlashWrite(0xFFBF, (const UINT8*)&u8NewNVOPT, 1u);
vfnFlashWrite(0xFFB0, (const UINT8*)au8BackDoorKey, N_ELEMENTS(au8BackDoorKey));
FlashSectorRestore(0xFE00);
}
The code can secure the device successfully, but how I could implement the unsecured code in secured situation during debug mode ?
I followed your suggestion from the link like below, and make vfnUnsecure() routine to unsecure device.
https://community.nxp.com/message/653031?commentID=653031#comment-653031
This code is right? And it runs in RAM(vfnCopyInRAM)
#pragma CODE_SEG FLASH_ROUTINES
void vfnUnsecure(void)
{
DisableInterrupts;
FCNFG_KEYACC=1;
*(byte*)0xFFB0 = 0x70;
*(byte*)0xFFB1 = 0x61;
*(byte*)0xFFB2 = 0x52;
*(byte*)0xFFB3 = 0x43;
*(byte*)0xFFB4 = 0x34;
*(byte*)0xFFB5 = 0x25;
*(byte*)0xFFB6 = 0x16;
*(byte*)0xFFB7 = 0x07;
FCNFG_KEYACC=0;
EnableInterrupts;
return;
}
#pragma CODE_SEG DEFAULT
void vfnCopyInRAM(void)
{
unsigned int Size = (unsigned int)Size_Copy_In_RAM;
unsigned char *SourcePtr;
unsigned char *DestinationPtr;
SourcePtr= (unsigned char*)Start_Copy_In_RAM;
DestinationPtr = (unsigned char*)&vfnUnsecure;
while (Size--)
{
*DestinationPtr = *SourcePtr;
DestinationPtr++;
SourcePtr++;
}
}
Could you please spend some time to check the code, and teach me how to implement the code in debug mode?
Thanks.
BR,
Sean Wu