72 lines
1.3 KiB
C
72 lines
1.3 KiB
C
|
||
#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;
|
||
}
|
||
|
||
|