123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- /*****************************************************************************
- FileSys.c
- *****************************************************************************/
- #define THIS_FILE_ID 20
- /* Includes ------------------------------------------------------------------*/
- #include "includes.h"
- SUT_FILE_LIST g_sutFilesList;
- unsigned char tempBuf[1024];
- void BackupFileToTemp(SUT_FILE_INFO *fileinfo, char type);
- void UpdateBackFileMD5(SUT_FILE_INFO *fileinfo);
- unsigned char isFileFileGood(unsigned int addr)
- {
- SUT_FILE_PAGE_HEAD pageInfo;
- MD5_CTX mdContext;
- unsigned int RAddr;
- unsigned int len;
- unsigned char data[1024];
- sFlash_Read((uint8_t *)&pageInfo, addr, sizeof(SUT_FILE_PAGE_HEAD));
- if(pageInfo.mark != FILE_HEAD_MARK) return 1;//文件非法
- if(pageInfo.fLength > 4096) return 2;//文件也算非法
- //计算md5
- RAddr=addr+4096;
- len=pageInfo.fLength;
- MD5Init(&mdContext);//初始化MD5
- while(len){
- if(len>1024){
- sFlash_Read(data,RAddr,1024);
- MD5Update(&mdContext,data,1024);
- RAddr+=1024;
- len-=1024;
- }else{
- sFlash_Read(data,RAddr,len);
- MD5Update(&mdContext,data,len);
- len=0;
- }
- }
- MD5Final(&mdContext);
- if(0 != memcmp(mdContext.digest, pageInfo.md5, 16)) return 3;//文件内容与MD5对不上
- else return 0;//正常
- }
- /*****************************************************************
- FileSysReadIndex
- ******************************************************************/
- int FileSysReadIndex(void)
- {
- #if((FILE_INDEX_ADDR%4096)||(FILE_DATA_ADDR_END+1)%4096)
- #error "文件列表存储区首地址必须为页首(4K的整数倍)"
- #endif
- /*
- 因为涉及到PC工具,这里不对PC工具作修改,因此,只修改IAP(后续APP也要修改),PC传输协议不变更;
- 以前是先从存储区读出文件列表信息,同时存储区也是有实际的文件列表信息区域的;
- 变更后,存储区没有总体的文件列表信息区域了,这里读取出来的文件信息区域则是能过扫描整个文件列表区来获取并整理出文件列表信息结构体;
- 具体新的文件存储结构则是N个文件依次存储起来,每个文件的存储格式如下:
- 每个文件分页头(PAGE HEAD)及页身(PAGE BODY),页头只有一个,页身有N个,每个页占4K;
- 页头:
- unsigned char fName[];本页头所描述的文件名字
- unsigned char md5[16];本页头所描述的文件内容的md5
- unsigned int fLength;//本页所描述的文件内容共占多少个字节数
- unsigned int nextFileHeadAddr;//如果本页为有效文件的页头,且本值不为0时,则指向的是一下个文件的页头地址
- unsigned int mark; 表示此页是否是合法的页头信息,否则为数据页或无效页
- */
- unsigned char sysFileStatus,backupFileStatus;
- unsigned int addr;
- int i,fnum,tmp;
- char tempFileExist=0;//0 备份文件不存在,即无效 1 备份文件存在且合法的 2 备份文件在文件使用区中已完整存在,并未丢失 3 备份文件在文件使用区中匹配到,且不一致,需要恢复
- SUT_FILE_PAGE_HEAD pageInfo;
- //检测备份区是否有正确的备份文件
- memset(&g_sutFilesList,0,sizeof(SUT_FILE_LIST));
- fnum=0;
- for(i=FILE_INDEX_ADDR;i<FILE_DATA_ADDR_END;){
- sFlash_Read((uint8_t *)&pageInfo, i, sizeof(SUT_FILE_PAGE_HEAD));
- if(pageInfo.mark == FILE_HEAD_MARK){//这是一个有效的文件页
- memcpy(g_sutFilesList.FileInfo[fnum].FileName, pageInfo.fName, sizeof(g_sutFilesList.FileInfo[fnum].FileName));
- memcpy(g_sutFilesList.FileInfo[fnum].FileMD5, pageInfo.md5, sizeof(g_sutFilesList.FileInfo[fnum].FileMD5));
- g_sutFilesList.FileInfo[fnum].FileLen=pageInfo.fLength;
- g_sutFilesList.FileInfo[fnum].FileAddr=i;
- fnum++;
- if(pageInfo.nextFileHeadAddr != 0) i=pageInfo.nextFileHeadAddr;
- else break;
- }else if(pageInfo.mark == FILE_HEAD_END){
- break;
- }else{//此页可能是损坏的文件页,有可能是最后的未使用页
- i += 4096;
- }
- }
- g_sutFilesList.FileCount=fnum;
- if(fnum<1){//没有检测到资源文件
- printf("No res files\r\n");
- return 1;
- }
- g_sutFilesList.Mark=STORAGE_MARK;
- //sys.ini文件是否存在
- sysFileStatus=4;//0 文件正常, 1/2/3 文件存在但非法或MD5错误 4 文件丢失
- if(0==strcmp(g_sutFilesList.FileInfo[g_sutFilesList.FileCount-1].FileName, SYS_CONFIG)){
- sysFileStatus=isFileFileGood(g_sutFilesList.FileInfo[g_sutFilesList.FileCount-1].FileAddr);
- }
- //检查备份文件是否正确
- backupFileStatus=4;//0 文件正常, 1/2/3 文件存在但非法或MD5错误 4 文件丢失
- backupFileStatus=isFileFileGood(FILE_DATA_ADDR_END+1);
- if(sysFileStatus ==0){//文件正常
- printf("%s ok\r\n", SYS_CONFIG);
- return 0;
- }else if(backupFileStatus==0){//文件不正常,但备份文件正常,恢复之
- if(sysFileStatus==4){//文件丢失了,当前文件列表中没有此文件,恢复时需要在最后添加一个文件
- printf("%s lost, recover\r\n", SYS_CONFIG);
- i=0;
- }else{//文件损坏,当前文件列表中有此文件,恢复时不需要添加文件
- printf("%s invalid, recover\r\n", SYS_CONFIG);
- i=1;
- }
- sFlash_Read((uint8_t *)&pageInfo, FILE_DATA_ADDR_END+1, sizeof(SUT_FILE_PAGE_HEAD));
- memcpy(g_sutFilesList.FileInfo[fnum].FileName, pageInfo.fName, sizeof(g_sutFilesList.FileInfo[fnum].FileName));
- memcpy(g_sutFilesList.FileInfo[fnum].FileMD5, pageInfo.md5, sizeof(g_sutFilesList.FileInfo[fnum].FileMD5));
- g_sutFilesList.FileInfo[fnum].FileLen=pageInfo.fLength;
- sFlash_Read((uint8_t *)&pageInfo, g_sutFilesList.FileInfo[fnum-1-i].FileAddr, sizeof(SUT_FILE_PAGE_HEAD));
- g_sutFilesList.FileInfo[fnum].FileAddr=pageInfo.nextFileHeadAddr;
- BackupFileToTemp(&g_sutFilesList.FileInfo[fnum-i],1);
-
- if(sysFileStatus==4) g_sutFilesList.FileCount++;
- return 0;
- }else{//文件与备份文件都不正常,按设计是不会存在,也不会跑到这里
- printf("%s error, status:%d,%d\r\n", SYS_CONFIG,sysFileStatus,backupFileStatus);
- return 2;
- }
- }
- /******************************************************************
- 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()
- {
- return FileSysReadIndex();//读索引
- }
- 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;
- addr += 4096;
- 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;
- }
- //修改备份区内容
- void ModifyTempFile(SUT_FILE_INFO *fileinfo, u32 startAddr,u32 writeLen,unsigned char *pData)
- {
- u32 addr;
-
- addr=FILE_DATA_ADDR_END+1+4096+startAddr;
- sFlash_Write(pData, addr, writeLen);
- }
- /*****************************************************************
- 修改文件
- *****************************************************************/
- int ModifyConfigureFile(char showInfo)
- {//此版本中的此函数不能随便调用了,基本只要修改sysini文件才调
- int FileIndex;
- u32 startAddr,writeLen;
- unsigned char *pData;
- int len=0;
- u32 addr;
-
- FileIndex=newSysiniIndex;
- startAddr=0;
- writeLen=sizeof(SYSINI_HS191X_DEF);
- pData=(unsigned char *)&newPara;
-
- PWR_EN_HIGH;//锁住电源
- if(FileIndex<0) goto M_END;
- if(FileIndex>=g_sutFilesList.FileCount)goto M_END;
- if(showInfo != 0) speciallShow(0);
- if((startAddr+writeLen)>g_sutFilesList.FileInfo[FileIndex].FileLen){
- len=g_sutFilesList.FileInfo[FileIndex].FileLen-startAddr;
- }else len=writeLen;
- IWDG_ReloadCounter();
- //1、写之前先将当前文件拷贝到备份区(注意最好设计成可以修改的文件不在太大,尽量在4K之内,否则操作时间可能比较久)
- BackupFileToTemp(&g_sutFilesList.FileInfo[FileIndex],0);
- //2、然后修改备份区
- ModifyTempFile(&g_sutFilesList.FileInfo[FileIndex], startAddr, len, pData);
- UpdateBackFileMD5(&g_sutFilesList.FileInfo[FileIndex]);
- //3、拷贝备份区回使用区
- BackupFileToTemp(&g_sutFilesList.FileInfo[FileIndex],1);
- if(showInfo != 0) speciallShow(1);
- M_END:
- //PWR_EN_LOW;//释放电源
- return len;
- }
- //文件与备份区相互拷贝
- void BackupFileToTemp(SUT_FILE_INFO *fileinfo, char type)
- {
- unsigned int sourceAddr, desAddr, copyLen;
- unsigned char *tempBufx=tempBuf,i;
-
- if(type==0){//从文件拷贝到备份区
- //先拷贝数据页,再拷贝文件头页,其实从文件到备份区谁先后无所谓
- sourceAddr=fileinfo->FileAddr+4096;
- desAddr=FILE_DATA_ADDR_END+1+4096;
- for(i=0;i<4;i++){
- sFlash_Read(tempBufx, sourceAddr, 1024);
- sFlash_Write(tempBufx, desAddr, 1024);
- sourceAddr += 1024;
- desAddr += 1024;
- }
- sourceAddr=fileinfo->FileAddr;
- desAddr=FILE_DATA_ADDR_END+1;
- for(i=0;i<4;i++){
- sFlash_Read(tempBufx, sourceAddr, 1024);
- sFlash_Write(tempBufx, desAddr, 1024);
- sourceAddr += 1024;
- desAddr += 1024;
- }
- }else{//从备份区拷贝回文件区
- //先拷贝数据页,再拷贝文件头页
- sourceAddr=FILE_DATA_ADDR_END+1+4096;
- desAddr=fileinfo->FileAddr+4096;
- for(i=0;i<4;i++){
- sFlash_Read(tempBufx, sourceAddr, 1024);
- sFlash_Write(tempBufx, desAddr, 1024);
- sourceAddr += 1024;
- desAddr += 1024;
- }
- sourceAddr=FILE_DATA_ADDR_END+1;
- desAddr=fileinfo->FileAddr;
- for(i=0;i<4;i++){
- sFlash_Read(tempBufx, sourceAddr, 1024);
- sFlash_Write(tempBufx, desAddr, 1024);
- sourceAddr += 1024;
- desAddr += 1024;
- }
- }
- }
- void UpdateBackFileMD5(SUT_FILE_INFO *fileinfo)
- {
- MD5_CTX mdContext;
- unsigned char *data=tempBuf;
- uint32_t Addr,len;
- SUT_FILE_PAGE_HEAD *pageInfo;
- //读取文件数据并计算MD5
- Addr=FILE_DATA_ADDR_END+1+4096;
- len=fileinfo->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相同
- pageInfo=(SUT_FILE_PAGE_HEAD *)data;
- sFlash_Read(data, FILE_DATA_ADDR_END+1, sizeof(SUT_FILE_PAGE_HEAD));
- if(0!=memcmp(pageInfo->md5, mdContext.digest, 16)){
- memcpy(pageInfo->md5, mdContext.digest, sizeof(pageInfo->md5));
- sFlash_Write(data, FILE_DATA_ADDR_END+1, sizeof(SUT_FILE_PAGE_HEAD));
- }
- memcpy(fileinfo->FileMD5, mdContext.digest, sizeof(fileinfo->FileMD5));
- return;
- }
- /********************************************
- *串口打印所有资源文件信息
- **********************************************/
- 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");
- }
|