CopyFile.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "includes.h"
  2. struct COPYFILE_DEF copyFilePara;
  3. static void CopyBinToUFlash(SUT_FILE_LIST *,uint8_t);
  4. static int CopyWriteFileData(uint32_t ,uint32_t ,uint8_t *);
  5. static void CopyDataToBlockOne(SUT_FILE_LIST *);
  6. void CopyFileInit(void)
  7. {
  8. return ;
  9. copyFilePara.runFlag = 2;
  10. copyFilePara.userBuf = IapRxBuf;
  11. }
  12. void CopyFileHandle(void)
  13. {
  14. SUT_FILE_LIST sutFileList;
  15. uint8_t *pBuf;
  16. uint8_t time;
  17. int index;
  18. int i;
  19. return;
  20. if(copyFilePara.runFlag != 2) return;
  21. //1 check mark of block 2
  22. pBuf = (uint8_t *) &sutFileList;
  23. sFlash_Read(pBuf, APP_FILE_DATA_ADDR, sizeof(SUT_FILE_LIST));
  24. if(sutFileList.Mark == GOOD_UPDATE_STORAGE) // need to update app & res
  25. {
  26. index = GetFileIndexIAP(&sutFileList,"HS15\r\n", 0);
  27. if(index == -1) {
  28. //IapTrace("No xx.bin\r\n");
  29. return;
  30. }
  31. CopyBinToUFlash(&sutFileList,index);
  32. CopyDataToBlockOne(&sutFileList);
  33. ///////////////fix offset address//////////////////////
  34. for(i=0;i<sutFileList.FileCount;i++)
  35. sutFileList.FileInfo[i].FileAddr += 0x1000;
  36. sFlash_Write(pBuf,FILE_INDEX_ADDR,sizeof(SUT_FILE_LIST));
  37. /////////////////////////////////////
  38. //set mark of block 2 to UPDATE_STORAGE_ALREADY
  39. sutFileList.Mark = UPDATE_ALREADY;
  40. sFlash_Write(pBuf, APP_FILE_DATA_ADDR, 4);
  41. //set mark of block 1 to STORAGE_MARK
  42. sutFileList.Mark = STORAGE_MARK;
  43. sFlash_Write(pBuf, FILE_INDEX_ADDR, 4);
  44. //IapTrace("Fota OK!\r\n");
  45. }else
  46. {//IapTrace("no copy\r\n");
  47. }//do nothing
  48. }
  49. //type 0 找到.bin结尾的文件
  50. //type 1 正常查找
  51. int GetFileIndexIAP(SUT_FILE_LIST *sutfileList ,const char *filename,uint8_t type)
  52. {
  53. uint8_t i,j;
  54. for(i=0;i<sutfileList->FileCount;i++)
  55. {
  56. if(type){
  57. if(0==strcmp(sutfileList->FileInfo->FileName+i, filename)) return i;
  58. }
  59. else{
  60. //if(0 == strncmp(filename, sutfileList->FileInfo[i].FileName, 4)) return i;
  61. for(j=0;j<FILE_NAME_LEN_MAX;j++)
  62. {
  63. if(sutfileList->FileInfo[i].FileName[j] == '.')
  64. {
  65. if((FILE_NAME_LEN_MAX-j)>=3)
  66. {
  67. if((sutfileList->FileInfo[i].FileName[j+1] == 'B' || sutfileList->FileInfo[i].FileName[j+1] == 'b') &&
  68. (sutfileList->FileInfo[i].FileName[j+2] == 'I' || sutfileList->FileInfo[i].FileName[j+2] == 'i') &&
  69. (sutfileList->FileInfo[i].FileName[j+3] == 'N' || sutfileList->FileInfo[i].FileName[j+3] == 'n'))
  70. return i;
  71. }else break;
  72. }
  73. }
  74. }
  75. }
  76. return -1;
  77. }
  78. static void CopyBinToUFlash(SUT_FILE_LIST *sutfileList, uint8_t index)
  79. {
  80. uint32_t totalLen,addr,offset,copyLen;
  81. uint16_t copysize;
  82. uint8_t finished;
  83. totalLen = sutfileList->FileInfo[index].FileLen;
  84. addr = sutfileList->FileInfo[index].FileAddr;
  85. offset = 0;
  86. copyLen = 0;
  87. finished = 0;
  88. while(1)
  89. {
  90. if(totalLen - copyLen >= 2048)
  91. copysize = 2048;
  92. else{
  93. copysize = totalLen - copyLen;
  94. finished ++;
  95. }
  96. sFlash_Read(copyFilePara.userBuf, APP_FILE_DATA_ADDR+FILE_DATA_ADDR+addr+offset, copysize);//read
  97. CopyWriteFileData(offset, copysize, copyFilePara.userBuf);//write
  98. if(finished) return;
  99. copyLen += copysize;
  100. offset += copysize;
  101. }
  102. }
  103. static
  104. int CopyWriteFileData(uint32_t Offset,uint32_t Length,uint8_t *pData)
  105. {
  106. uint16_t i,temp;
  107. uint16_t len=0;
  108. if(0!=(Offset%2))return 0;
  109. WritePageData(FLASH_APP1_ADDR+Offset,pData,Length);
  110. return 1;
  111. }
  112. static void CopyDataToBlockOne(SUT_FILE_LIST *sutfileList)
  113. {
  114. uint32_t totalLen,offset,copyLen;
  115. uint16_t copysize;
  116. uint8_t i,finished;
  117. char buf[10];
  118. uint8_t persent,temp;
  119. totalLen = 0x1000;//4K
  120. for(i=0;i<sutfileList->FileCount;i++)
  121. totalLen += sutfileList->FileInfo[i].FileLen;
  122. offset = 0;
  123. copyLen = 0;
  124. finished = 0;
  125. while(1)
  126. {
  127. if(totalLen - copyLen >= 2048)
  128. copysize = 2048;
  129. else{
  130. copysize = totalLen - copyLen;
  131. finished ++;
  132. }
  133. sFlash_Read(copyFilePara.userBuf, offset+APP_FILE_DATA_ADDR, copysize);
  134. sFlash_Write(copyFilePara.userBuf, offset+FILE_INDEX_ADDR, copysize);
  135. offset += copysize;
  136. copyLen += copysize;
  137. temp = copyLen*100/totalLen;
  138. if(persent != temp)
  139. {
  140. persent = temp;
  141. sprintf(buf, "Copy %d \r\n", copyLen*100/totalLen);
  142. IapTrace(buf);
  143. }
  144. if(finished)
  145. break;
  146. }
  147. }