Hi Ed
Below is the solution used in the uTasker project. The SDRAM needs either to be configured via DCD or else by initialisation code before the copy/jump. It shows decrypting from encrypted code in SPI flash or plain text, depending on the image.
The image needs just to be linked to the SRDAM address that it will run at.
static void jump_to_application(unsigned long app_link_location)
{
asm(" mov r1, #0");
asm(" msr control, r1");
asm(" ldr sp, [r0,#0]");
asm(" ldr pc, [r0,#4]");
}
static void fnCopyApplicationToRAM(iMX_BOOT_HEADER *ptrHeader, unsigned long ulCodeLength)
{
if ((ptrHeader->usMagicNumber & 0x8000) != 0) {
unsigned long ulBuffer[512];
unsigned long ulCopyLength = sizeof(ulBuffer);
const unsigned char *ptrInput = (const void *)(ptrHeader + 1);
unsigned char *ptrDestination = (unsigned char *)(SDRAM_START_ADDRESS);
fnPrepareDecrypt();
while (ulCodeLength != 0) {
if (ulCodeLength < sizeof(ulBuffer)) {
ulCopyLength = ulCodeLength;
}
uMemcpy(ulBuffer, ptrInput, ulCopyLength);
fnAES_Cipher(AES_COMMAND_AES_DECRYPT, (const unsigned char *)ulBuffer, (unsigned char *)ulBuffer, ulCopyLength);
uMemcpy(ptrDestination, (const void *)ulBuffer, ulCopyLength);
ulCodeLength -= ulCopyLength;
ptrDestination += ulCopyLength;
ptrInput += ulCopyLength;
}
}
else {
uMemcpy((void *)(SDRAM_START_ADDRESS), (const void *)(ptrHeader + 1), ulCodeLength);
}
}
extern void start_application(unsigned long app_link_location)
{
iMX_BOOT_HEADER *ptrHeader = (iMX_BOOT_HEADER *)app_link_location;
unsigned long ulCodeLength = fnCheckValidApplication(ptrHeader);
if (ulCodeLength != 0) {
jump_to_application(SDRAM_START_ADDRESS);
}
}
Regards
Mark
[uTasker project developer for Kinetis and i.MX RT]