/*
 * crypto.c
 *
 *  Created on: 2023年10月17日
 *      Author: mglsq
 */
#include "platform.h"

__attribute__((section (".preinit"), aligned(4)))
void crypto(void)
{
	uint32_t flag_encrypted = 0;
	if(DEV_CHIP->OTP_DATA0 & (1u << 17))	//SEED_LOCK set?
	{
		flag_encrypted = 1;
	}
	if(flag_encrypted)
	{
		DEV_NORC->ENCRY_SEED = DEV_CHIP->OTP_DATA5;
		DEV_NORC->ENCRY_BASE0 = FW_HEADER->ENCRY_ADDR;
		uint32_t end_addr = FW_HEADER->ENCRY_ADDR + FW_HEADER->ENCRY_SIZE;
		//aligned to 4K
		if(end_addr & 0xfffu)
		{
			end_addr &= 0xfffff000u;
			end_addr += 0x1000u;
		}
		DEV_NORC->ENCRY_END0 = end_addr;
		DEV_NORC->ENCRY_CTL = DEV_NORC_ENCRY_CTL_ALG_ROUNDXOR | DEV_NORC_ENCRY_CTL_EN0;
	}
}

