MBIC_Bootloader.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * MBIC_Bootloader.c
  3. *
  4. * Created on: 2020. 5. 18.
  5. * Author: parkyj
  6. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include "main.h"
  11. #include "MBIC_Bootloader.h"
  12. #include "flash.h"
  13. #include "CRC.h"
  14. #include "uart.h"
  15. extern __IO bool EraseInit;
  16. extern uint8_t Bank_Flash_write(uint8_t* data,uint32_t StartBankAddress);
  17. BootLoaderDataCheck_st MBIC_FileDataCheck(uint8_t* data){
  18. BootLoaderDataCheck_st ret = {0,0,false};
  19. int8_t MBIC_Mark[9] = "JT-NRDAS ";
  20. int8_t MBIC_FileName1[] = "jhfs-mbic-nrdas-v";
  21. int8_t MBIC_FileName2[] = "v00.00.01.mbc";
  22. uint8_t MBIC_type = 0x20;
  23. uint8_t MBIC_FileType = 0x00;
  24. int i = 0;
  25. /***
  26. MARK Check
  27. ***/
  28. for(i = MARK_START_POSITION; i < TYPE_START_POSITION; i++){
  29. if(MBIC_Mark[i] != data[i]){
  30. printf("Data Diff \r\n");
  31. return ret;
  32. }else{
  33. printf("MARK Data Success \r\n");
  34. }
  35. }
  36. /***
  37. TYPE Check
  38. ***/
  39. for(i = TYPE_START_POSITION; i < FILE_TYPE_START_POSITION; i++){
  40. if(MBIC_type != data[i]){
  41. printf("Data Diff \r\n");
  42. return ret;
  43. }
  44. else
  45. printf("Type Data Success \r\n");
  46. }
  47. /***
  48. File Type Check
  49. ***/
  50. for(i = FILE_TYPE_START_POSITION; i < VERSION_START_POSITION; i++){
  51. if(MBIC_FileType != data[i]){
  52. printf("Data Diff \r\n");
  53. return ret;
  54. }
  55. else
  56. printf("File Type Data Success \r\n");
  57. }
  58. /***
  59. Version Check
  60. ***/
  61. for(i = VERSION_START_POSITION; i < FILENAME_START_POSITION; i++){
  62. printf("Version Data Success Version %x\r\n",data[i]);
  63. }
  64. /***
  65. File Name Check
  66. ***/
  67. for(i = FILENAME_START_POSITION; i < 30; i++){
  68. if(MBIC_FileName1[i-14] != data[i]){
  69. printf("Data Diff \r\n");
  70. printf("MBIC_FileName1[%d] : %x, data[%d] : %x\r\n",i - 14,MBIC_FileName1[i - 14],i,data[i]);
  71. return ret;
  72. }
  73. else
  74. printf("File Name Data Success %c\r\n",data[i]);
  75. }
  76. for(i = i; i < 43; i++){
  77. if(MBIC_FileName2[i-30] != data[i]){
  78. // printf("Data Diff %c\r\n",data[i]);
  79. // printf("MBIC_FileName2[%d] : %x, data[%d] : %x\r\n",i - 30,MBIC_FileName2[i - 30],i,data[i]);
  80. // return ret;
  81. }
  82. else
  83. printf("File Name Data Success %c\r\n",data[i]);
  84. }
  85. for(i = i; i < 49; i++){
  86. printf("Creation Success %x\r\n",data[i]);
  87. }
  88. for(i = i; i < 55; i++){
  89. printf("Creation Success data[%d] : %x\r\n",i,data[i]);
  90. }
  91. printf(" %d",data[i++]);
  92. printf(" -%02d",data[i++]);
  93. printf(" -%02d",data[i++]);
  94. printf(" -%02d",data[i++]);
  95. printf(" -%02d",data[i++]);
  96. printf(" -%02d\r\n",data[i++]);
  97. ret.Length = ((data[i++] << 8) & 0xFF00);
  98. ret.Length += (data[i++]);
  99. printf("data[%d] : %d\r\n",i - 1,ret.Length);
  100. printf("data[%d] : %d\r\n",i - 1,ret.Length);
  101. ret.Crcchk = ((data[i++] << 8) & 0xFF00);
  102. ret.Crcchk += (data[i++]);
  103. /*ONLY DATA CRC*/
  104. printf("CRC_H[%d] : %x\r\n",i,ret.Crcchk);
  105. printf("CRC_L[%d] : %x\r\n",i,ret.Crcchk);
  106. ret.FileChk = true;
  107. return ret;
  108. }
  109. /*
  110. MBIC Basic Header merge function
  111. Data : Response Data
  112. Length : Response Data Length
  113. CRCINDEX : CRC INDEX Number
  114. */
  115. uint8_t* MBIC_HeaderMergeFunction(uint8_t* data,uint16_t Length )
  116. {
  117. uint8_t ret[Length + 22 + 3];/*Data Length + Header Length + Tail Length*/
  118. uint16_t CRCData = CRC16_Generate(data,Length);
  119. /*CRC Create*/
  120. ret[MBIC_PAYLOADSTART + Length + 0] = ((CRCData & 0xFF00) >> 8);
  121. ret[MBIC_PAYLOADSTART + Length + 1] = ((CRCData & 0x00FF));
  122. ret[MBIC_PAYLOADSTART + Length + 2] = 0x03;
  123. /*Data Mark Create*/
  124. ret[MBIC_PREAMBLE_0] = MBIC_PREAMBLE0;
  125. ret[MBIC_PREAMBLE_1] = MBIC_PREAMBLE1;
  126. ret[MBIC_PREAMBLE_2] = MBIC_PREAMBLE2;
  127. ret[MBIC_PREAMBLE_3] = MBIC_PREAMBLE3;
  128. /*Data Subid Create*/
  129. ret[MBIC_SUBUID_0] = MBIC_SUBUID0;
  130. ret[MBIC_SUBUID_1] = MBIC_SUBUID1;
  131. ret[MBIC_RCODE_0] = data[MBIC_RCODE_0];
  132. ret[MBIC_TRID_0] = data[MBIC_TRID_0];
  133. ret[MBIC_TRID_1] = data[MBIC_TRID_1];
  134. ret[MBIC_SEQSUM_0] = data[MBIC_SEQSUM_0];
  135. ret[MBIC_TTL_0] = data[MBIC_TTL_0];
  136. ret[MBIC_TIME_0] = data[MBIC_TIME_0];
  137. ret[MBIC_TIME_1] = data[MBIC_TIME_1];
  138. ret[MBIC_TIME_2] = data[MBIC_TIME_2];
  139. ret[MBIC_TIME_3] = data[MBIC_TIME_3];
  140. ret[MBIC_TIME_4] = data[MBIC_TIME_4];
  141. ret[MBIC_TIME_5] = data[MBIC_TIME_5];
  142. ret[MBIC_ERRRESPONSE_0] = MBIC_ERRRESPONSE;
  143. ret[MBIC_LENGTH_0] = (Length & 0xFF00) >> 8;
  144. ret[MBIC_LENGTH_1] = Length & 0x00FF;
  145. ret[MBIC_HEADERCHECKSUM_0] = Chksum_Create(ret);
  146. /*Data Move*/
  147. // for(int i = 0; i < Length; i++){
  148. // data[MBIC_PAYLOADSTART + i] = data[i];
  149. // }
  150. /*
  151. MBIC Header Data input
  152. */
  153. for(int i = 0; i < MBIC_HEADER_SIZE; i++){
  154. if(i == MBIC_CMD_0) /*cmd exception*/
  155. continue;
  156. data[i] = ret[i];
  157. }
  158. /*
  159. MBIC Tail Data input
  160. */
  161. for(int i = MBIC_HEADER_SIZE + Length; i < MBIC_HEADER_SIZE + MBIC_TAIL_SIZE + Length; i++){
  162. data[i] = ret[i];
  163. }
  164. // for(uint16_t i = 0; i < Length; i ++)
  165. // ret[MBIC_PAYLOADSTART + i] = data[i];
  166. // for(int i = 0; i < Length; i++)
  167. // printf("MBIC : %x \r\n",data[i]);
  168. return data;
  169. }
  170. void MBIC_Bootloader_FirmwareUpdate(uint8_t* data){
  171. uint8_t MBIC_DownLoadData[0xFFFF];
  172. uint8_t cmd = data[MBIC_CMD_0];
  173. static uint8_t Download_Option = 0;
  174. uint16_t index = 0;
  175. static uint32_t Curr_Download_DataIndex = 0;
  176. static uint32_t Prev_Download_DataIndex = 0;
  177. uint32_t TotalFrame = 0;
  178. uint32_t CurrFrame = 0;
  179. uint32_t i = 0 ;
  180. #if 0 // PYJ.2020.06.04_BEGIN --
  181. uint8_t dataTest[1024] = {
  182. 0x4A,0x54,0x2D,0x4E,0x52,0x44,0x41,0x53,0x20,0x20,0x00,0x00,
  183. 0x00,0x01,0x6A,0x68,0x66,0x73,0x2D,0x6D,0x62,0x69,0x63,0x2D,
  184. 0x6E,0x72,0x64,0x61,0x73,0x2D,0x76,0x30,0x30,0x2E,0x30,0x30,0x2E,
  185. 0x30,0x31,0x2E,0x6D,0x62,0x63,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  186. 0x20,0x20,0x20,0x20,0x20,0x14,0x06,0x03,0x10,0x1F,0xC4,0x3C,0x49,
  187. 0x42,0x98,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  188. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  189. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  190. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  191. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  192. };
  193. #endif // PYJ.2020.06.04_END --
  194. // printf("RX");
  195. // for(int i = 0; i < 128; i++)
  196. // printf("%c",*data++);
  197. switch(cmd){
  198. case MBIC_Notice_REQ:
  199. cmd = MBIC_Notice_RSP;
  200. /*TOTAL FRAME NUMBER*/
  201. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
  202. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];
  203. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
  204. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];
  205. /*DOWNLOAD OPTION*/
  206. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
  207. Download_Option = data[MBIC_PAYLOADSTART + 4];
  208. /*DOWNLOAD DELAY REQUEST*/
  209. data[MBIC_PAYLOADSTART + index++] = 3;
  210. /*DOWNLOAD Reserve*/
  211. data[MBIC_PAYLOADSTART + index++] = 0;
  212. data[MBIC_PAYLOADSTART + index++] = 0;
  213. data[MBIC_PAYLOADSTART + index++] = 0;
  214. data[MBIC_PAYLOADSTART + index++] = 0;
  215. data[MBIC_PAYLOADSTART + index++] = 0;
  216. data[MBIC_PAYLOADSTART + index++] = 0;
  217. // printf("ccc\r\n");
  218. break;
  219. case MBIC_Download_DATA_REQ:
  220. TotalFrame = data[MBIC_PAYLOADSTART + 0] << 24
  221. |data[MBIC_PAYLOADSTART + 1] << 16
  222. |data[MBIC_PAYLOADSTART + 2] << 8
  223. |data[MBIC_PAYLOADSTART + 3] << 0;
  224. // MBIC_FileDataCheck(data);
  225. cmd = MBIC_Download_DATA_RSP;
  226. /*TOTAL FRAME NUMBER*/
  227. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
  228. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];
  229. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
  230. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];
  231. /*Current Download Frame Number*/
  232. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
  233. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 5];
  234. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 6];
  235. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 7];
  236. Curr_Download_DataIndex = data[MBIC_PAYLOADSTART + 4] << 24;
  237. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 5] << 16;
  238. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 6] << 8;
  239. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 7];
  240. /*DOWNLOAD Reserve*/
  241. data[MBIC_PAYLOADSTART + index++] = 0;
  242. data[MBIC_PAYLOADSTART + index++] = 0;
  243. data[MBIC_PAYLOADSTART + index++] = 0;
  244. data[MBIC_PAYLOADSTART + index++] = 0;
  245. for(i = 0; i < Curr_Download_DataIndex - Prev_Download_DataIndex; i++){
  246. MBIC_DownLoadData[i] = data[MBIC_PAYLOADSTART + 12 +i];
  247. // printf("%02x ",MBIC_DownLoadData[i]);
  248. }
  249. // printf("Data End\r\n");
  250. Bank_Flash_write(data,FLASH_USER_BANK1_START_ADDR);
  251. // HAL_Delay(100);
  252. Prev_Download_DataIndex = Curr_Download_DataIndex + 1;
  253. break;
  254. case MBIC_Download_Confirm_REQ:
  255. EraseInit = true;
  256. cmd = MBIC_Download_Confirm_RSP;
  257. /*TOTAL FRAME NUMBER*/
  258. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
  259. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];
  260. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
  261. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];
  262. /*DOWNLOAD OPTION*/
  263. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
  264. Download_Option = data[MBIC_PAYLOADSTART + 4];
  265. /*DOWNLOAD DELAY REQUEST*/
  266. data[MBIC_PAYLOADSTART + index++] = 3;
  267. /*DOWNLOAD Reserve*/
  268. data[MBIC_PAYLOADSTART + index++] = 0;
  269. data[MBIC_PAYLOADSTART + index++] = 0;
  270. data[MBIC_PAYLOADSTART + index++] = 0;
  271. data[MBIC_PAYLOADSTART + index++] = 0;
  272. data[MBIC_PAYLOADSTART + index++] = 0;
  273. data[MBIC_PAYLOADSTART + index++] = 0;
  274. break;
  275. case MBIC_Complete_Notice_REQ_REQ:
  276. cmd = MBIC_Complete_Notice_RSP;
  277. /*TOTAL FRAME NUMBER*/
  278. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
  279. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];
  280. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
  281. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];
  282. /*DOWNLOAD OPTION*/
  283. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
  284. Download_Option = data[MBIC_PAYLOADSTART + 4];
  285. /*DOWNLOAD DELAY REQUEST*/
  286. data[MBIC_PAYLOADSTART + index++] = 3;
  287. /*DOWNLOAD Reserve*/
  288. data[MBIC_PAYLOADSTART + index++] = 0;
  289. data[MBIC_PAYLOADSTART + index++] = 0;
  290. data[MBIC_PAYLOADSTART + index++] = 0;
  291. data[MBIC_PAYLOADSTART + index++] = 0;
  292. data[MBIC_PAYLOADSTART + index++] = 0;
  293. data[MBIC_PAYLOADSTART + index++] = 0;
  294. break;
  295. case MBIC_Reboot_Notice_REQ:
  296. cmd = MBIC_Reboot_Notice_RSP;
  297. /*TOTAL FRAME NUMBER*/
  298. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 0];
  299. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 1];
  300. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 2];
  301. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 3];
  302. /*DOWNLOAD OPTION*/
  303. data[MBIC_PAYLOADSTART + index++] = data[MBIC_PAYLOADSTART + 4];
  304. Download_Option = data[MBIC_PAYLOADSTART + 4];
  305. /*DOWNLOAD DELAY REQUEST*/
  306. data[MBIC_PAYLOADSTART + index++] = 3;
  307. /*DOWNLOAD Reserve*/
  308. data[MBIC_PAYLOADSTART + index++] = 0;
  309. data[MBIC_PAYLOADSTART + index++] = 0;
  310. data[MBIC_PAYLOADSTART + index++] = 0;
  311. data[MBIC_PAYLOADSTART + index++] = 0;
  312. data[MBIC_PAYLOADSTART + index++] = 0;
  313. data[MBIC_PAYLOADSTART + index++] = 0;
  314. break;
  315. default:
  316. return;
  317. }
  318. data[MBIC_CMD_0] = cmd;
  319. data = MBIC_HeaderMergeFunction(data,index); // reponse
  320. // HAL_UART_Transmit_DMA(&huart1, data,22 + 3 + index);
  321. Uart1_Data_Send(data ,22 + 3 + index);
  322. }