123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- /*****************************************************************************
- FileSys.c
- *****************************************************************************/
- #define THIS_FILE_ID 20
- /* Includes ------------------------------------------------------------------*/
- #include "includes.h"
- SUT_FILE_LIST g_sutFilesList;
- /*****************************************************************
- FileSysReadIndex
- ******************************************************************/
- void FileSysReadIndex(void)
- {
- u8 *pBuf;
- pBuf=(u8 *)&g_sutFilesList;
- sFlash_Read(pBuf,FILE_INDEX_ADDR,sizeof(SUT_FILE_LIST));
- }
- /******************************************************************
- FileSysSaveIndex
- *******************************************************************/
- void FileSysSaveIndex(void)
- {
- u8 *pBuf;
- pBuf=(u8 *)&g_sutFilesList;
- sFlash_Write(pBuf,FILE_INDEX_ADDR,sizeof(SUT_FILE_LIST));
- }
- /******************************************************************
- FileSysInit
- 检查是否需要格式化,需要则格式化之,否则不格式化
- return:
- 1--format
- 0--no format
- *******************************************************************/
- int FileSysInit()
- {
- int i,j;
- FileSysReadIndex();//读索引
- if(g_sutFilesList.Mark!=STORAGE_MARK){//需要格式化
- memset(&g_sutFilesList,0,sizeof(SUT_FILE_LIST));
- g_sutFilesList.Mark=STORAGE_MARK;
- printf("Format File System!");
- FileSysSaveIndex();
- return 1;
- }else{
- // //检查文件系统是否有损坏,有损坏则格式化文件系统
- // for(i=0;i<g_sutFilesList.FileCount;i++){
- // FileCheckMD5(i);//if(0==FileCheckMD5(i))break;
- // }
- // if(i<g_sutFilesList.FileCount){//有文件损害,其后的文件均需格式化之
- // printf("File[%d] MD5 Fail!\r\n",i);
- // printf("Format File System!\r\n");
- // for(j=i;j<g_sutFilesList.FileCount;j++){
- // memset(&g_sutFilesList.FileInfo[j],0,sizeof(SUT_FILE_INFO));
- // }
- // g_sutFilesList.FileCount=i;
- // FileSysSaveIndex();
- // }
- //
- return 0;
- }
- }
- void UCharToStrByHex(char *str,unsigned char *data,int len)
- {
- int i;
- for(i=0;i<len;i++){
- sprintf(&str[i*2],"%02X",data[i]);
- }
- }
- int FileCheckMD5(int FileIndex)
- {
- MD5_CTX mdContext;
- unsigned char data[1024];
- int i;
- uint32_t FileLen;
- uint32_t FileAddr;
- uint32_t Addr,len;
-
- FileLen=g_sutFilesList.FileInfo[FileIndex].FileLen;
- FileAddr=g_sutFilesList.FileInfo[FileIndex].FileAddr;
- //读取文件数据并计算MD5
- Addr=FileAddr;
- len=FileLen;
- MD5Init(&mdContext);//初始化MD5
- while(len){
- if(len>1024){
- sFlash_Read(data,Addr,1024);
- MD5Update(&mdContext,data,1024);
- Addr+=1024;
- len-=1024;
- }else{
- sFlash_Read(data,Addr,len);
- MD5Update(&mdContext,data,len);
- len=0;
- }
- }
- MD5Final(&mdContext);
-
- //比较文件数据的MD5计数值是否与文件信息头的MD5相同
- for(i=0;i<16;i++){
- if(g_sutFilesList.FileInfo[FileIndex].FileMD5[i]!=mdContext.digest[i]){
- //不同的部分打印出来
- UCharToStrByHex((char *)data,g_sutFilesList.FileInfo[FileIndex].FileMD5,16);
- printf("Check File[%d] %s <> ",FileIndex,(char *)data);
- UCharToStrByHex((char *)data,mdContext.digest,16);
- printf("%s\r\n",(char *)data);
- return 0;
- }
- }
-
-
- //不同的部分打印出来
- // UCharToStrByHex((char *)data,g_sutFilesList.FileInfo[FileIndex].FileMD5,16);
- // printf("Check File[%d] %s == ",FileIndex,(char *)data);
- // UCharToStrByHex((char *)data,mdContext.digest,16);
- // printf("%s\r\n",(char *)data);
- return 1;
- }
- /***********************************************************
- *获取文件索引
- ************************************************************/
- int GetFileIndex(const char *filename)
- {
- int i;
- for(i=0;i<g_sutFilesList.FileCount;i++){
- if(0==strcmp(g_sutFilesList.FileInfo[i].FileName,filename)){
- return i;
- }
- }
- return -1;
- }
- /***********************************************************
- *获取文件长度
- ************************************************************/
- int GetFileLen(int FileIndex)
- {
- int len;
- if(FileIndex<0)return 0;
- len=g_sutFilesList.FileInfo[FileIndex].FileLen;
- if(len<0 || len>(FILE_DATA_ADDR_END-FILE_DATA_ADDR))return 0;
- else return len;
- }
- /***********************************************************
- 读取文件数据
- 返回实际读取字节数
- ************************************************************/
- int ReadFileData(int FileIndex,u32 startAddr,u32 readLen,unsigned char *pData)
- {
- int len;
- u32 addr;
- if(FileIndex<0)return 0;
- if(FileIndex>=g_sutFilesList.FileCount)return 0;
- if((startAddr+readLen)>g_sutFilesList.FileInfo[FileIndex].FileLen){
- len=g_sutFilesList.FileInfo[FileIndex].FileLen-startAddr;
- }else{
- len=readLen;
- }
- addr=g_sutFilesList.FileInfo[FileIndex].FileAddr + startAddr;
- sFlash_Read(pData,addr,len);
- return len;
- }
- /***************************************************************
- UpdateFileMD5
- *****************************************************************/
- void UpdateFileMD5(int FileIndex)
- {
- MD5_CTX mdContext;
- unsigned char data[1024];
- int i;
- uint32_t FileLen;
- uint32_t FileAddr;
- uint32_t Addr,len;
-
- FileLen=g_sutFilesList.FileInfo[FileIndex].FileLen;
- FileAddr=g_sutFilesList.FileInfo[FileIndex].FileAddr;
- //读取文件数据并计算MD5
- Addr=FileAddr;
- len=FileLen;
- MD5Init(&mdContext);//初始化MD5
- while(len){
- if(len>1024){
- sFlash_Read(data,Addr,1024);
- MD5Update(&mdContext,data,1024);
- Addr+=1024;
- len-=1024;
- }else{
- sFlash_Read(data,Addr,len);
- MD5Update(&mdContext,data,len);
- len=0;
- }
- }
- MD5Final(&mdContext);
- //比较文件数据的MD5计数值是否与文件信息头的MD5相同
- if(g_sutFilesList.FileInfo[FileIndex].FileMD5[FileIndex]!=mdContext.digest[FileIndex]){
- //更新文件索引对应文件的MD5
- for(i=0;i<16;i++){
- g_sutFilesList.FileInfo[FileIndex].FileMD5[i]=mdContext.digest[i];
- }
- FileSysSaveIndex();
- }
- return;
- }
- /*****************************************************************
- 修改文件
- *****************************************************************/
- //ModifyFileData(FileIndex,0,sizeof(SUT_LCD_PARAM),(unsigned char *)&sutLcdParam);
- int ModifyFileData(int FileIndex,u32 startAddr,u32 writeLen,unsigned char *pData)
- {
- int len;
- u32 addr;
-
- if(FileIndex<0)return 0;
- if(FileIndex>=g_sutFilesList.FileCount)return 0;
-
- if((startAddr+writeLen)>g_sutFilesList.FileInfo[FileIndex].FileLen){//防止修改长度越界。
- len=g_sutFilesList.FileInfo[FileIndex].FileLen-startAddr;
- //return;
- }else{
- len=writeLen;
- }
- addr=g_sutFilesList.FileInfo[FileIndex].FileAddr + startAddr;
-
- sFlash_Write(pData,addr,len);
- UpdateFileMD5(FileIndex);
- return len;
- }
- /********************************************
- *串口打印所有资源文件信息
- **********************************************/
- void PrintfAllRFileInfo(void)
- {
- char strMD5[35];
- int i;
- printf("=============================RFile list============================\r\n");
- printf("FilesNumber=%lu\r\n",g_sutFilesList.FileCount);
- printf("FileName Address Length FileMD5\r\n");
- printf("-------------------------------------------------------------------\r\n");
- for(i=0;i<g_sutFilesList.FileCount;i++){
- UCharToStrByHex(strMD5,g_sutFilesList.FileInfo[i].FileMD5,16);
- printf("%-16s0x%06x %-8lu%-s\r\n",g_sutFilesList.FileInfo[i].FileName,\
- (unsigned int)g_sutFilesList.FileInfo[i].FileAddr,g_sutFilesList.FileInfo[i].FileLen,strMD5);
- }
- printf("====================================================================\r\n");
- }
|