IAP.c 11 KB

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