123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- #include "includes.h"
- struct COPYFILE_DEF copyFilePara;
- static void CopyBinToUFlash(SUT_FILE_LIST *,uint8_t);
- static int CopyWriteFileData(uint32_t ,uint32_t ,uint8_t *);
- static void CopyDataToBlockOne(SUT_FILE_LIST *);
- void CopyFileInit(void)
- {
- return ;
- copyFilePara.runFlag = 2;
- copyFilePara.userBuf = IapRxBuf;
- }
- void CopyFileHandle(void)
- {
- SUT_FILE_LIST sutFileList;
- uint8_t *pBuf;
- uint8_t time;
- int index;
- int i;
- return;
- if(copyFilePara.runFlag != 2) return;
-
- //1 check mark of block 2
- pBuf = (uint8_t *) &sutFileList;
- sFlash_Read(pBuf, APP_FILE_DATA_ADDR, sizeof(SUT_FILE_LIST));
- if(sutFileList.Mark == GOOD_UPDATE_STORAGE) // need to update app & res
- {
- index = GetFileIndexIAP(&sutFileList,"HS15\r\n", 0);
- if(index == -1) {
- //IapTrace("No xx.bin\r\n");
- return;
- }
- CopyBinToUFlash(&sutFileList,index);
- CopyDataToBlockOne(&sutFileList);
- ///////////////fix offset address//////////////////////
- for(i=0;i<sutFileList.FileCount;i++)
- sutFileList.FileInfo[i].FileAddr += 0x1000;
- sFlash_Write(pBuf,FILE_INDEX_ADDR,sizeof(SUT_FILE_LIST));
- /////////////////////////////////////
- //set mark of block 2 to UPDATE_STORAGE_ALREADY
- sutFileList.Mark = UPDATE_ALREADY;
- sFlash_Write(pBuf, APP_FILE_DATA_ADDR, 4);
- //set mark of block 1 to STORAGE_MARK
- sutFileList.Mark = STORAGE_MARK;
- sFlash_Write(pBuf, FILE_INDEX_ADDR, 4);
- //IapTrace("Fota OK!\r\n");
- }else
- {//IapTrace("no copy\r\n");
- }//do nothing
- }
- //type 0 找到.bin结尾的文件
- //type 1 正常查找
- int GetFileIndexIAP(SUT_FILE_LIST *sutfileList ,const char *filename,uint8_t type)
- {
- uint8_t i,j;
-
- for(i=0;i<sutfileList->FileCount;i++)
- {
- if(type){
- if(0==strcmp(sutfileList->FileInfo->FileName+i, filename)) return i;
- }
- else{
- //if(0 == strncmp(filename, sutfileList->FileInfo[i].FileName, 4)) return i;
- for(j=0;j<FILE_NAME_LEN_MAX;j++)
- {
- if(sutfileList->FileInfo[i].FileName[j] == '.')
- {
- if((FILE_NAME_LEN_MAX-j)>=3)
- {
- if((sutfileList->FileInfo[i].FileName[j+1] == 'B' || sutfileList->FileInfo[i].FileName[j+1] == 'b') &&
- (sutfileList->FileInfo[i].FileName[j+2] == 'I' || sutfileList->FileInfo[i].FileName[j+2] == 'i') &&
- (sutfileList->FileInfo[i].FileName[j+3] == 'N' || sutfileList->FileInfo[i].FileName[j+3] == 'n'))
- return i;
- }else break;
- }
- }
- }
- }
- return -1;
- }
- static void CopyBinToUFlash(SUT_FILE_LIST *sutfileList, uint8_t index)
- {
- uint32_t totalLen,addr,offset,copyLen;
- uint16_t copysize;
- uint8_t finished;
-
- totalLen = sutfileList->FileInfo[index].FileLen;
- addr = sutfileList->FileInfo[index].FileAddr;
- offset = 0;
- copyLen = 0;
- finished = 0;
- while(1)
- {
- if(totalLen - copyLen >= 2048)
- copysize = 2048;
- else{
- copysize = totalLen - copyLen;
- finished ++;
- }
-
- sFlash_Read(copyFilePara.userBuf, APP_FILE_DATA_ADDR+FILE_DATA_ADDR+addr+offset, copysize);//read
- CopyWriteFileData(offset, copysize, copyFilePara.userBuf);//write
-
- if(finished) return;
-
- copyLen += copysize;
- offset += copysize;
- }
- }
- static
- int CopyWriteFileData(uint32_t Offset,uint32_t Length,uint8_t *pData)
- {
- uint16_t i,temp;
- uint16_t len=0;
- if(0!=(Offset%2))return 0;
- WritePageData(FLASH_APP1_ADDR+Offset,pData,Length);
- return 1;
- }
- static void CopyDataToBlockOne(SUT_FILE_LIST *sutfileList)
- {
- uint32_t totalLen,offset,copyLen;
- uint16_t copysize;
- uint8_t i,finished;
- char buf[10];
- uint8_t persent,temp;
-
- totalLen = 0x1000;//4K
- for(i=0;i<sutfileList->FileCount;i++)
- totalLen += sutfileList->FileInfo[i].FileLen;
-
- offset = 0;
- copyLen = 0;
- finished = 0;
-
- while(1)
- {
- if(totalLen - copyLen >= 2048)
- copysize = 2048;
- else{
- copysize = totalLen - copyLen;
- finished ++;
- }
- sFlash_Read(copyFilePara.userBuf, offset+APP_FILE_DATA_ADDR, copysize);
- sFlash_Write(copyFilePara.userBuf, offset+FILE_INDEX_ADDR, copysize);
-
- offset += copysize;
- copyLen += copysize;
- temp = copyLen*100/totalLen;
- if(persent != temp)
- {
- persent = temp;
- sprintf(buf, "Copy %d \r\n", copyLen*100/totalLen);
- IapTrace(buf);
- }
-
- if(finished)
- break;
- }
- }
|