IAP.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**********************************************************************************
  2. * File Name: IAP.c
  3. * Function Describe:In application program
  4. * Relate Module:
  5. * Explain:
  6. * Writer: ShiLiangWen
  7. * Date: 2015.6.9
  8. *********************************************************************************************/
  9. #include "includes.h"
  10. update_hdr_t sutHeader;
  11. unsigned short g_usTid=0;
  12. static unsigned char FileMD5[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  13. int g_ulFileLen=0;//文件长度
  14. int g_ulDFileLen=0;//已下载文件长度
  15. int g_ulOffset=0;
  16. int g_ulLength=0;
  17. uint8_t IapRxBuf[IAP_RX_BUF];
  18. uint16_t IapRxLen;
  19. /*****************************************************************************************
  20. IapCheckFileMD5
  21. 成功返回1
  22. 失败返回0
  23. ******************************************************************************************/
  24. int IapCheckFileMD5(uint32_t FileLen,uint8_t MD5[])
  25. {
  26. MD5_CTX mdContext;
  27. unsigned short temp;
  28. unsigned char data[2048];
  29. char tembuf[5];
  30. unsigned char RMD5[16];
  31. uint32_t offset=0;
  32. uint32_t len;
  33. int i,j;
  34. //计算MD5
  35. MD5Init(&mdContext);
  36. j=0;
  37. for(i=0;i<FileLen;i+=2){
  38. temp=STMFLASH_ReadHalfWord((FLASH_APP1_ADDR+i));
  39. data[j++]=(unsigned char)(temp&0xff);
  40. data[j++]=(temp>>8)&0xff;
  41. if(j==2048){
  42. MD5Update(&mdContext,data,2048);
  43. j=0;
  44. }
  45. }
  46. if(j!=0){
  47. MD5Update(&mdContext,data,j);
  48. }
  49. MD5Final(&mdContext);
  50. #if 1 //debug trace md5
  51. sprintf((char *)data,"FileSize=%d MD5=",FileLen);
  52. for(i=0;i<16;i++){
  53. sprintf(tembuf,"%02X",mdContext.digest[i]);
  54. strcat((char *)data,tembuf);
  55. }
  56. strcat((char *)data,"\r\n");
  57. IapTrace((char *)data);
  58. #endif
  59. //check
  60. for(i=0;i<16;i++){
  61. if(MD5[i]!=mdContext.digest[i])break;
  62. }
  63. if(i<16)return 0;
  64. else return 1;
  65. }
  66. /*****************************************************************************************
  67. IapWriteFileData
  68. ******************************************************************************************/
  69. int IapWriteFileData(uint32_t Offset,uint32_t Length,uint8_t *pData)
  70. {
  71. //unsigned short iapbuf[2048];
  72. uint16_t i,temp;
  73. uint16_t len=0;
  74. if(0!=(Offset%2))return 0;
  75. WritePageData(FLASH_APP1_ADDR+Offset,pData,Length);
  76. // for(i = 0; i < Length; i+=2)
  77. // {
  78. // //数据8位融合为16位
  79. // temp = (((uint16_t)pData[i+1])<<8) + ((uint16_t)pData[i]);
  80. // iapbuf[len] = temp;
  81. // len++;//完成之后receiveDataCur++;
  82. // }
  83. //
  84. // STMFLASH_Write((FLASH_APP1_ADDR+Offset),iapbuf,len);
  85. //
  86. return 1;
  87. }
  88. ///*****************************************************************************************
  89. //MakeFoTaVersionReq
  90. //******************************************************************************************/
  91. //int MakeFoTaVersionReq(char *txbuf)
  92. //{
  93. // update_version_req_t *pUpdateVersionReq;
  94. // pUpdateVersionReq=(update_version_req_t *)txbuf;
  95. // memset(pUpdateVersionReq,0,sizeof(update_version_req_t));
  96. // pUpdateVersionReq->hdr.magic=FOTA_MAGIC;
  97. // pUpdateVersionReq->hdr.protocol=UPDATE_PROTOCOL;
  98. // pUpdateVersionReq->hdr.code=UPDATE_CODE_VERSION_REQ;
  99. // sprintf(pUpdateVersionReq->hdr.dev_id,"%lu",sutProductPara.PSN);
  100. // pUpdateVersionReq->hdr.tid=++g_usTid;
  101. // pUpdateVersionReq->hdr.count=sizeof(update_version_req_t)-sizeof(update_hdr_t);
  102. // strcpy(pUpdateVersionReq->product,sutProductPara.ProductName);
  103. // pUpdateVersionReq->version=sutProductPara.ProductVersion;
  104. // return sizeof(update_version_req_t);
  105. //}
  106. ///*****************************************************************************************
  107. //MakeFoTaDataReq
  108. //******************************************************************************************/
  109. //int MakeFoTaDataReq(char *txbuf,int offset,int length)
  110. //{
  111. // update_data_req_t *pUpdateDataReq;
  112. // pUpdateDataReq=(update_data_req_t *)txbuf;
  113. // memset(pUpdateDataReq,0,sizeof(update_data_req_t));
  114. // pUpdateDataReq->hdr.magic=FOTA_MAGIC;
  115. // pUpdateDataReq->hdr.protocol=UPDATE_PROTOCOL;
  116. // pUpdateDataReq->hdr.code=UPDATE_CODE_DATA_WITH_CRC_REQ;
  117. // sprintf(pUpdateDataReq->hdr.dev_id,"%lu",sutProductPara.PSN);
  118. // pUpdateDataReq->hdr.tid=++g_usTid;
  119. // pUpdateDataReq->hdr.count=sizeof(update_data_req_t)-sizeof(update_hdr_t);
  120. // memcpy(pUpdateDataReq->version_md5,FileMD5,16);
  121. // pUpdateDataReq->offset=offset;
  122. // pUpdateDataReq->length=length;
  123. // return sizeof(update_data_req_t);
  124. //}
  125. /*****************************************************************************************
  126. MakePcTaVersionReq
  127. ******************************************************************************************/
  128. int MakePcTaVersionReq(char *txbuf)
  129. {
  130. update_version_req_t *pUpdateVersionReq;
  131. pUpdateVersionReq=(update_version_req_t *)txbuf;
  132. memset(pUpdateVersionReq,0,sizeof(update_version_req_t));
  133. pUpdateVersionReq->hdr.magic=PCTA_MAGIC;
  134. pUpdateVersionReq->hdr.protocol=UPDATE_PROTOCOL_APP;
  135. pUpdateVersionReq->hdr.code=UPDATE_CODE_VERSION_REQ;
  136. sprintf(pUpdateVersionReq->hdr.dev_id,"0");
  137. pUpdateVersionReq->hdr.tid=g_ucRand;//对于PC升级,传输伪随机数用于加密
  138. pUpdateVersionReq->hdr.count=sizeof(update_version_req_t)-sizeof(update_hdr_t);
  139. strcpy(pUpdateVersionReq->product,"NULL");
  140. pUpdateVersionReq->version=0;
  141. g_usTid=0;
  142. return sizeof(update_version_req_t);
  143. }
  144. /*****************************************************************************************
  145. MakePcTaDataReq
  146. ******************************************************************************************/
  147. int MakePcTaDataReq(char *txbuf,int offset,int length)
  148. {
  149. update_data_req_t *pUpdateDataReq;
  150. pUpdateDataReq=(update_data_req_t *)txbuf;
  151. memset(pUpdateDataReq,0,sizeof(update_data_req_t));
  152. pUpdateDataReq->hdr.magic=PCTA_MAGIC;
  153. pUpdateDataReq->hdr.protocol=UPDATE_PROTOCOL_APP;
  154. pUpdateDataReq->hdr.code=UPDATE_CODE_DATA_WITH_CRC_REQ;
  155. sprintf(pUpdateDataReq->hdr.dev_id,"0");
  156. pUpdateDataReq->hdr.tid=++g_usTid;
  157. pUpdateDataReq->hdr.count=sizeof(update_data_req_t)-sizeof(update_hdr_t);
  158. memcpy(pUpdateDataReq->version_md5,FileMD5,16);
  159. pUpdateDataReq->offset=offset;
  160. pUpdateDataReq->length=length;
  161. return sizeof(update_data_req_t);
  162. }
  163. /*****************************************************************************************
  164. IapGetUpdateData
  165. ******************************************************************************************/
  166. void IapGetUpdateData(uint32_t magic,uint32_t offset)
  167. {
  168. int len;
  169. if(PCTA_MAGIC==magic){
  170. len=MakePcTaDataReq((char *)TxBuffer1,offset,2048);
  171. Uart1Send((char *)TxBuffer1,len);
  172. }
  173. }
  174. /*****************************************************************************************
  175. IapGetUpdateVersion
  176. ******************************************************************************************/
  177. void IapGetUpdateVersion(uint32_t magic)
  178. {
  179. int len;
  180. if(PCTA_MAGIC==magic){
  181. len=MakePcTaVersionReq((char *)TxBuffer1);
  182. Uart1Send((char *)TxBuffer1,len);
  183. }
  184. }
  185. /************************************************************************************
  186. IapRecvMsgHandle
  187. 处理接收到的数据
  188. *************************************************************************************/
  189. int IapRecvMsgHandle(unsigned char *pData, unsigned short DataLen)
  190. {
  191. int i;
  192. char id[16];
  193. unsigned short crc,CRC16;
  194. unsigned short ReadBuf[4];
  195. char buf[100];
  196. uint32_t len,offset;
  197. unsigned char code;
  198. unsigned short count;
  199. update_hdr_t *pHeader;
  200. update_version_rsp_t *pVersionRsp;
  201. update_data_rsp_t *pDataRsp;
  202. pHeader=(update_hdr_t *)pData;
  203. if(pHeader->magic != FOTA_MAGIC && pHeader->magic != PCTA_MAGIC)return -1;
  204. if(pHeader->protocol != UPDATE_PROTOCOL_APP )return -2;
  205. sprintf(id,"0");//PSN
  206. if(0!=strcmp(id,pHeader->dev_id))return -3;
  207. code= pHeader->code;
  208. count= pHeader->count;
  209. if(code==UPDATE_CODE_VERSION_REQ){
  210. if(count!=(sizeof(update_version_rsp_t)-sizeof(update_hdr_t)))return -4;
  211. pVersionRsp= (update_version_rsp_t *)pData;
  212. memcpy(FileMD5,pVersionRsp->version_md5,16);
  213. g_ulFileLen=pVersionRsp->size;
  214. if(0==g_ulFileLen)return -5;
  215. g_ulOffset=0;
  216. g_ulLength=0;
  217. g_ulDFileLen=0;
  218. IapGetUpdateData(pHeader->magic,g_ulOffset);
  219. return 1;
  220. }else if(code == UPDATE_CODE_DATA_WITH_CRC_REQ){
  221. pDataRsp = (update_data_rsp_t*)pData;
  222. //判断MD5是否被更新
  223. for(i=0;i<16;i++){
  224. if(FileMD5[i]!=pDataRsp->version_md5[i])break;
  225. }
  226. if(i<16){//MD5被更新,申请重新下载
  227. IapTrace("MD5 Modify!ReDO!\r\n");
  228. IapGetUpdateVersion(pHeader->magic);
  229. return -6;
  230. }
  231. len=pDataRsp->length;
  232. offset=pDataRsp->offset;
  233. //length和offset不对,申请重新下载
  234. if(len<2 || offset!=g_ulOffset || (len+g_ulOffset)>(g_ulFileLen+2)){
  235. IapTrace("Length or Offset error!ReDO!\r\n");
  236. IapGetUpdateVersion(pHeader->magic);
  237. return -7;
  238. }
  239. len-=2;//实际数据长度应去掉2字节的CRC
  240. //CRC校验
  241. CRC16=crc16_calc(0,pDataRsp->data,len);
  242. crc=((unsigned short)pDataRsp->data[len+1]&0xff)<<8;
  243. crc+=(unsigned short)pDataRsp->data[len]&0xff;
  244. if(crc!=CRC16){
  245. //校验错误,申请重新下载
  246. IapTrace("Check CRC error!ReDO!\r\n");
  247. IapGetUpdateData(pHeader->magic,g_ulOffset);
  248. return -8;
  249. }
  250. //==CRC校验成功,更新FLASH ===
  251. //第一次更新,写修改IAP标志位
  252. if(0==g_ulOffset){
  253. // sutProductPara.IapFlag=0x00;
  254. // SaveProductParaToFlash();
  255. }
  256. //对于PC传输,需要先解密
  257. if(PCTA_MAGIC==pHeader->magic){
  258. for(i=0;i<len;i++)pDataRsp->data[i]=pDataRsp->data[i] ^ g_ucRand;
  259. }
  260. IapWriteFileData(g_ulOffset,len,pDataRsp->data);
  261. g_ulDFileLen+=len;
  262. g_ulOffset+=len;
  263. //判断是否结束?
  264. if(g_ulDFileLen>=g_ulFileLen){
  265. //下载结束,检查MD5
  266. if(0==IapCheckFileMD5(g_ulFileLen,FileMD5)){
  267. //校验MD5错误,重新下载
  268. IapTrace("Check MD5 error!ReDO!\r\n");
  269. IapGetUpdateVersion(pHeader->magic);
  270. return -9;
  271. }
  272. //---校验成功,下载完成---
  273. //修改标志位,以便可以运行APP
  274. // sutProductPara.IapFlag=0x88;
  275. // SaveProductParaToFlash();
  276. return 0;
  277. }
  278. //未结束,启动下一包下载
  279. sprintf(buf,"HBD=%d %d%%\r\n",g_ulOffset, 100*g_ulOffset/g_ulFileLen);//Has been downloaded
  280. IapTrace(buf);
  281. IapGetUpdateData(pHeader->magic,g_ulOffset);
  282. return 2;
  283. }
  284. }
  285. typedef void (*iapfun)(void); //定义一个函数类型的参数.
  286. iapfun jump2app;
  287. //设置栈顶地址
  288. //addr:栈顶地址
  289. __asm void MSR_MSP(u32 addr)
  290. {
  291. MSR MSP, r0 //set Main Stack value
  292. BX r14
  293. }
  294. //跳转到应用程序段
  295. //appxaddr:用户代码起始地址.
  296. void iap_load_app(u32 appxaddr)
  297. {
  298. //static int cnt;
  299. if(((*(vu32*)appxaddr)&0x2FFE0000)==0x20000000) //检查栈顶地址是否合法.0x20000000是sram的起始地址,也是程序的栈顶地址
  300. {
  301. //关闭UART1终端
  302. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  303. USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE); //使能USART1的接收DMA请求
  304. //关闭SysTick中断
  305. SysTick->CTRL = 0;
  306. jump2app=(iapfun)*(vu32*)(appxaddr+4); //用户代码区第二个字为程序开始地址(复位地址)
  307. MSR_MSP(*(vu32*)appxaddr); //初始化APP堆栈指针(用户代码区的第一个字用于存放栈顶地址)
  308. jump2app(); //跳转到APP.
  309. }
  310. else
  311. {
  312. while(1)
  313. {
  314. IapTrace("No App\r\n");
  315. DelayMs(1000);
  316. // if(++cnt >= 30000)
  317. // {
  318. // cnt=0;
  319. // IapTrace("No App\r\n");
  320. // }
  321. }
  322. }
  323. }
  324. /*******************************************************************
  325. 跳转到app区域运行
  326. ********************************************************************/
  327. void iap_jump_app_s(void)
  328. {
  329. iap_load_app(FLASH_APP1_ADDR);//跳转到app的复位向量地址
  330. }
  331. //设置app固化配置
  332. void iap_set_flag_s(void)
  333. {
  334. // Test_Write(APP_CONFIG_ADDR,APP_CONFIG_SET_VALUE);
  335. // printf("ok\r\n");
  336. }
  337. //清除app固化配置
  338. void iap_clear_flag(void)
  339. {
  340. // Test_Write(APP_CONFIG_ADDR,APP_CONFIG_CLEAR_VALUE);
  341. // printf("ok\r\n");
  342. }
  343. /***********************************************************************************/