📄 cprm_test.c
字号:
/***************************************************************************
* Copyright Faraday Technology Corp 2002-2003. All rights reserved. *
*--------------------------------------------------------------------------*
* Name:CPRM_test.c *
* Description: CPRM test program *
* Author: W.D.Shih *
****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "fLib.h"
#include "filectrl.h"
#include "command.h"
#include "DMA.h"
#include "sd.h"
#include "cprm.h"
#include "audio.h"
SDCardStruct fLib_SDCard;
SDCardStruct fLib_ProtectAreaCard;
CPRMFileStruct fLib_CPRMAudio;
BOOL CPRM_Order_Change;
extern UINT32 *Buf;
extern INT8 SecretConstant[256];
UINT32 DirCmd(CommandTableStruct_T *CmdTable);
UINT32 ChangeDirCmd(CommandTableStruct_T *CmdTable);
UINT32 DeleteCmd(CommandTableStruct_T *CmdTable);
UINT32 ProtectAreaTestCmd(CommandTableStruct_T *CmdTable);
UINT32 TstDeviceMKBCmd(CommandTableStruct_T *CmdTable);
UINT32 TstSimpleMKBCmd(CommandTableStruct_T *CmdTable);
UINT32 TstAlgorithmCmd(CommandTableStruct_T *CmdTable);
UINT32 EncryptFileCmd(CommandTableStruct_T *CmdTable);
UINT32 DecryptFileCmd(CommandTableStruct_T *CmdTable);
UINT32 TstPLMCmd(CommandTableStruct_T *CmdTable);
UINT32 TstTKMCmd(CommandTableStruct_T *CmdTable);
CommandTableStruct_T SD_CmdTable[] =
{
{{"dir", NULL,}, "dir [directory/file]\n",
0, 0, 1, 0, 0, 0, DirCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "[directory/file] - directory or file\n"},
},
},
{{"cd", NULL,}, "cd [directory]\n",
0, 0, 1, 1, 0, 0, ChangeDirCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "[directory] - directory\n"},
},
},
{{"remove", "del", NULL,}, "remove/del [name] [-d]\n",
0, 0, 1, 1, 1, 0, DeleteCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "[name] - file or directory name\n"},
},
{
{"-d", IS_NULL, {NULL,}, NULL, 0, FALSE, "[-d] - [name] is directory\n"},
},
},
{{"rwtest", NULL,}, "rwtest \n",
0, 0, 1, 0, 0, 0, ProtectAreaTestCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "Protect Area Read/Write MultiBlock Test\n"},
},
},
{{"tstdev", NULL,}, "tstdev \n",
0, 0, 1, 0, 0, 0, TstDeviceMKBCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "Test 4C device-test patterns\n"},
},
},
{{"tstsim", NULL,}, "tstsim \n",
0, 0, 1, 0, 0, 0, TstSimpleMKBCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "Test 4C simple-test patterns\n"},
},
},
{{"tste", NULL,}, "tste \n",
0, 0, 1, 0, 0, 0, TstAlgorithmCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "Test CPRM Algorithm\n"},
},
},
{{"en", "encrypt", NULL,}, "en/encrypt [name]\n",
0, 0, 1, 1, 0, 0, EncryptFileCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "[name] - file\n"},
},
},
{{"de", "decrypt", NULL,}, "de/decrypt [name]\n",
0, 0, 1, 1, 0, 0, DecryptFileCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "[name] - file\n"},
},
},
{{"tstplm", NULL,}, "tstplm \n",
0, 0, 1, 0, 0, 0, TstPLMCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "Parsing PLM file\n"},
},
},
{{"tsttkm", NULL,}, "tsttkm \n",
0, 0, 1, 0, 0, 0, TstTKMCmd, 0, 0,
{
{IS_STR, NULL, 0, FALSE, "Parsing TKM file\n"},
},
},
{NULL,
},
};
#define SD_DMA_CHANNEL 2
int main(int argc, int *argv[])
{
char InputBuf[80];
fLib_CloseInt(IRQ_SDC);
if (!fLib_ConnectInt(IRQ_SDC, fLib_SDCntrInterruptHandler))
return FALSE;
fLib_SetIntTrig(IRQ_SDC,LEVEL,H_ACTIVE);
fLib_EnableInt(IRQ_SDC);
fLib_InitDMA(FALSE, FALSE, 1 << SD_DMA_CHANNEL);
#ifdef Debug_CPRM_Message
printf("Init User Area Disk C!!!\n");
#endif
#ifndef AUTO_CBC_DMAmode
if(!fLib_SDHostInterfaceInit(&fLib_SDCard, CPE_SD_BASE, FALSE, SD_DMA_CHANNEL, 'C', SYS_CLK/2))
#else
if(!fLib_SDHostInterfaceInit(&fLib_SDCard, CPE_SD_BASE, TRUE, SD_DMA_CHANNEL, 'C', SYS_CLK/2))
#endif
{
printf("Can not found SD or MMC card!!!\n");
return FALSE;
}
#ifndef license_4C_Test
if(!fLib_CPRMInit(&fLib_SDCard))
{
printf("Can not init CPRM!!!\n");
return FALSE;
}
#else
#ifdef Debug_CPRM_Message
printf("Init User Area Disk D!!!\n");
#endif
if(!fLib_ProtectedAreaInit(&fLib_SDCard, &fLib_ProtectAreaCard, 'D'))
{
printf("Can not found SD CPRM Protected Area!!!\n");
return FALSE;
}
if(!fLib_ReadTKMG(&fLib_ProtectAreaCard, &fLib_CPRMAudio))
{
printf("Can not found SD CPRM Protected Area!!!\n");
return FALSE;
}
// Change to Disk C
fLib_ChangeDirectory("C:");
#endif
fLib_InsertCommand(SD_CmdTable);
fLib_InitUserSymbol(NULL);
while(1)
{
printf("SD>>>\n");
gets(InputBuf);
fLib_ExecuteCommand(InputBuf);
}
return TRUE;
}
UINT32 DirCmd(CommandTableStruct_T *CmdTable)
{
UINT32 MsgStr;
MsgStr = malloc(0x1000);
if(!fLib_DirDirectory((CmdTable->Argu[0].IsSet ? CmdTable->Argu[0].Str : NULL), MsgStr))
{
free(MsgStr);
return ERROR_COMMAND;
}
printf(MsgStr);
free(MsgStr);
return OK_COMMAND;
}
UINT32 ChangeDirCmd(CommandTableStruct_T *CmdTable)
{
if(fLib_ChangeDirectory(CmdTable->Argu[0].Str))
{
printf("Change to %s directory OK!!!\n", CmdTable->Argu[0].Str);
return OK_COMMAND;
}
else
{
printf("Change to %s directory error!!!\n", CmdTable->Argu[0].Str);
}
return ERROR_COMMAND;
}
UINT32 DeleteCmd(CommandTableStruct_T *CmdTable)
{
if (CmdTable->Opt[0].IsSet) {
if (fLib_DeleteDirectory(CmdTable->Argu[0].Str)) {
printf("Del %s directory OK!!!\n", CmdTable->Argu[0].Str);
return OK_COMMAND;
}
}
else {
if ( fLib_DeleteFile(CmdTable->Argu[0].Str, sizeof(CmdTable->Argu[0].Str)) != -1) {
printf("Del %s file OK!!!\n", CmdTable->Argu[0].Str);
return OK_COMMAND;
}
}
printf("Del %s directory or file error!!!\n", CmdTable->Argu[0].Str);
return ERROR_COMMAND;
}
UINT32 ProtectAreaTestCmd(CommandTableStruct_T *CmdTable)
{
UINT32 *ABuf, *BBuf, *CBuf;
UINT32 StartBlk, BlkCount, Sector_Length, i;
BOOL flag = TRUE;
StartBlk = 4;
BlkCount = 1;
Sector_Length = 512;
//Backup Buffer Data
ABuf = malloc(BlkCount * Sector_Length);
CBuf = malloc(BlkCount * Sector_Length);
for (i = 0; i < BlkCount * Sector_Length / 4; i++)
*(CBuf + i) = *(ABuf + i);
// ABuf will be changed when we call it.
if(!fLib_CPRMWriteProtectArea(&fLib_ProtectAreaCard, StartBlk, BlkCount, Sector_Length, ABuf))
{
free(ABuf);
free(CBuf);
return ERROR_COMMAND;
}
BBuf = malloc(BlkCount * Sector_Length);
if(!fLib_CPRMReadProtectArea(&fLib_ProtectAreaCard, StartBlk, BlkCount, Sector_Length, BBuf))
{
free(ABuf);
free(BBuf);
free(CBuf);
return ERROR_COMMAND;
}
for (i = 0; i < BlkCount * Sector_Length / 4; i++)
{
if(*(CBuf + i) != *(BBuf + i))
{
printf("Compare Fail!, Addr = %0.3x, C = %0.8x, F = %0.8x\n",i*4, *(CBuf + i), *(BBuf + i));
flag = FALSE;
}
}
if(!flag)
printf("Protected Area Read/Write Multi Block Verification fail!\n");
else
printf("Protected Area Read/Write Multi Block Verification Pass!\n");
free(ABuf);
free(BBuf);
free(CBuf);
return OK_COMMAND;
}
UINT32 TstDeviceMKBCmd(CommandTableStruct_T *CmdTable)
{
FILE *stream;
INT32 nbytes;
int length, Tmp;
UINT32 mediaKey[2];
UINT8 *BufPtr, *TmpBuf;
TmpBuf = malloc(0x10000);
//Device Test
if((stream = fopen( "c:\\device-test-mkb.bin", "rb" )) != NULL )
{
BufPtr = TmpBuf;
while(1)
{
nbytes = fread(BufPtr, 1, 4, stream);
if (nbytes > 0)
{
Tmp = *(BufPtr+1);
length = Tmp << 16;
Tmp = *(BufPtr+2);
length = length | (Tmp << 8);
Tmp = *(BufPtr+3);
length |= Tmp; //Record Length
BufPtr += 4;
nbytes = fread(BufPtr, 1, length - 4, stream);
if (nbytes <= 0)
printf("Read File Error\n");
BufPtr = BufPtr + length - 4;
}
else
break;
}
fclose( stream );
}
else
{
printf("File could not be opened\n" );
return FALSE;
}
CPRM_Process_MKB(TmpBuf, &mediaKey);
//printf("mediakey = %0.8x%0.8x\n",*(UINT32 *)mediaKey,*(UINT32 *)(mediaKey + 1));
//MKB Extension data
if((stream = fopen( "c:\\device-test-mkb-extension.bin", "rb" )) != NULL )
{
BufPtr = TmpBuf;
while(1)
{
nbytes = fread(BufPtr, 1, 4, stream);
if (nbytes > 0)
{
Tmp = *(BufPtr+1);
length = Tmp << 16;
Tmp = *(BufPtr+2);
length = length | (Tmp << 8);
Tmp = *(BufPtr+3);
length |= Tmp; //Record Length
BufPtr += 4;
nbytes = fread(BufPtr, 1, length - 4, stream);
if (nbytes <= 0)
printf("Read File Error\n");
BufPtr = BufPtr + length - 4;
}
else
break;
}
fclose( stream );
}
else
printf("File could not be opened\n" );
CPRM_Process_MKB(TmpBuf, &mediaKey);
printf("mediakey = %0.8x%0.8x\n",*(UINT32 *)mediaKey,*(UINT32 *)(mediaKey + 1));
free(TmpBuf);
return OK_COMMAND;
}
UINT32 TstSimpleMKBCmd(CommandTableStruct_T *CmdTable)
{
FILE *stream;
INT32 nbytes;
int length, Tmp;
UINT64 mediaKey;
UINT8 *BufPtr, *TmpBuf;
TmpBuf = malloc(0x10000);
//Simple Test
#if 1
if((stream = fopen( "c:\\simple-test-2b-mkb.bin", "rb" )) != NULL )
#else
if((stream = fopen( "c:\\simple-test-3b-mkb.bin", "rb" )) != NULL )
#endif
{
BufPtr = TmpBuf;
while(1)
{
nbytes = fread(BufPtr, 1, 4, stream);
if (nbytes > 0)
{
Tmp = *(BufPtr+1);
length = Tmp << 16;
Tmp = *(BufPtr+2);
length = length | (Tmp << 8);
Tmp = *(BufPtr+3);
length |= Tmp; //Record Length
BufPtr += 4;
nbytes = fread(BufPtr, 1, length - 4, stream);
if (nbytes <= 0)
printf("Read File Error\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -