optical/NxBase/encrypt.c
2025-09-04 09:45:08 +08:00

72 lines
1.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "encrypt.h"
#include "cpuid.h"
/*
文件加密管理
目的:防止从芯片拷贝目标代码下载到其他板卡使用
使用方法:
1, 在.c文件中定义 const static u16 的变量
2. 用 AddToEncrypt 添加到加密管理中
3. 在合适的时候调用 CheckEncrypt 来检测是否符合加密规则
4. 如果不符合,做...操作
*/
#define MAX_ENCRYPT_NUM 32 // 最大支持的加密字个数
int g_encryptNum = 0;
const u16 * g_encryptList[MAX_ENCRYPT_NUM];
// 添加加密字到加密管理(必须是用 const static 定义的)
int AddToEncrypt(const u16 * pEncryptWord)
{
if (g_encryptNum < MAX_ENCRYPT_NUM)
{
g_encryptList[g_encryptNum] = pEncryptWord;
g_encryptNum++;
}
return g_encryptNum;
}
// 检查加密信息结果不为0非法
int CheckEncrypt(void)
{
int rslt = 0;
int i, j;
const u16 * pEncrype;
int idnum = sizeof(CpuId) / sizeof(u16);
CpuId id;
GetCpuID(&id);
for (i = 0, j = 0; i < g_encryptNum; i++, j++)
{
pEncrype = g_encryptList[i];
j %= idnum;
if (id.wbuff[j] != *pEncrype)
{
if (*pEncrype == 0xffff) // 第一次烧写将id写入flash
{
/*
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);
FLASH_ProgramWord((u32)pEncrype, id.wbuff[j]);
FLASH_Lock();
*/
}
else
{
rslt = i+1;
break;
}
}
}
return rslt;
}