Module_Upgrade.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*
  2. * Module_RFID.c
  3. *
  4. * Created on: 2019-12-24
  5. * Author: Jerry Wang
  6. * Version: D0.01
  7. */
  8. #include "main.h"
  9. #include "Module_Upgrade.h"
  10. //==================================
  11. // PRINT OUT LOG FORMAT
  12. //==================================
  13. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  14. #define PASS 1
  15. #define FAIL -1
  16. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  17. struct StatusCodeData *ShmStatusCodeData;
  18. struct FanModuleData *ShmFanModuleData;
  19. int DiffTimebByUpgrade(struct timeb ST, struct timeb ET)
  20. {
  21. //return milli-second
  22. unsigned int StartTime,StopTime;
  23. StartTime=(unsigned int)ST.time;
  24. StopTime=(unsigned int)ET.time;
  25. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  26. }
  27. unsigned char *memcat(unsigned char *dest, unsigned int dest_len, unsigned char *src, unsigned int src_len)
  28. {
  29. memcpy(dest+dest_len, src, src_len);
  30. return dest;
  31. }
  32. uint32_t crc32(uint8_t *data, unsigned int length)
  33. {
  34. uint8_t i;
  35. uint32_t cnt = 0;
  36. uint32_t crc = 0xffffffff; // Initial value
  37. while(length--)
  38. {
  39. if(cnt>33 && cnt<48) {
  40. data++;
  41. }else {
  42. crc ^= *data++; // crc ^= *data; data++;
  43. for (i = 0; i < 8; ++i)
  44. {
  45. if (crc & 1)
  46. crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
  47. else
  48. crc = (crc >> 1);
  49. }
  50. }
  51. cnt++;
  52. }
  53. return ~crc;
  54. }
  55. int Upgrade_Flash(unsigned int Type,char *SourcePath,char *ModelName)
  56. {
  57. int result = FAIL;
  58. long int MaxLen=48*1024*1024, ImageLen=0;
  59. unsigned int ImageCRC=0, DataLength=0;
  60. int wrd,fd;
  61. // space max size set
  62. switch(Type)
  63. {
  64. case CSU_BOOTLOADER:
  65. MaxLen = 1*1024*1024;
  66. DEBUG_INFO("Image type: U-Boot\r\n");
  67. break;
  68. case CSU_KERNEL_CONFIGURATION:
  69. MaxLen = 0.5*1024*1024;
  70. DEBUG_INFO("Image type: DTB\r\n");
  71. break;
  72. case CSU_KERNEL_IMAGE:
  73. MaxLen = 10*1024*1024;
  74. DEBUG_INFO("Image type: Kernel\r\n");
  75. break;
  76. case CSU_ROOT_FILE_SYSTEM:
  77. MaxLen = 48*1024*1024;
  78. DEBUG_INFO("Image type: Root fs\r\n");
  79. break;
  80. case CSU_USER_CONFIGURATION:
  81. MaxLen = 6*1024*1024;
  82. DEBUG_INFO("Image type: Config\r\n");
  83. break;
  84. default:
  85. break;
  86. }
  87. fd = open(SourcePath, O_RDONLY);
  88. if(fd < 0)
  89. {
  90. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  91. return result;
  92. }
  93. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  94. memset(ptr,0xFF,MaxLen); //-48 is take out the header
  95. //get the image length
  96. ImageLen = read(fd,ptr,MaxLen);
  97. close(fd);
  98. //read out the header
  99. int i;
  100. int isModelNameOK = PASS;
  101. for(i=0;i<16;i++)
  102. {
  103. if(ModelName[i] != ptr[i])
  104. {
  105. isModelNameOK = FAIL;
  106. }
  107. }
  108. if(isModelNameOK == FAIL)
  109. {
  110. DEBUG_ERROR("Model name mismatch.\r\n");
  111. }
  112. else
  113. {
  114. // check if the firmware type is correct
  115. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  116. {
  117. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  118. {
  119. DataLength = ImageLen-48;
  120. // get CRC in the header
  121. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  122. // calculate the image CRC
  123. DEBUG_INFO("CRC32 in image: 0x%08X\r\n",ImageCRC);
  124. DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
  125. if(crc32(ptr,ImageLen/*34+DataLength*/) == ImageCRC)
  126. {
  127. // Write image to target flash block
  128. switch(Type)
  129. {
  130. case FLASH_IMAGE_TYPE_SPL:
  131. fd = open("/dev/mtdblock0", O_RDWR);
  132. if (fd < 0)
  133. {
  134. DEBUG_ERROR("Can not open mtdblock0\r\n");
  135. result = FAIL;
  136. }
  137. else
  138. {
  139. // Write image to flash
  140. DEBUG_INFO("Writing image to mtdblock0...\n");
  141. wrd=write(fd, ptr, DataLength);
  142. close(fd);
  143. DEBUG_INFO(">> mtdblock0 Written length: 0x%x\r\n", wrd);
  144. if(wrd != DataLength)
  145. {
  146. result = FAIL;
  147. }
  148. else
  149. {
  150. result = PASS;
  151. }
  152. }
  153. break;
  154. case CSU_BOOTLOADER:
  155. fd = open("/dev/mtdblock1", O_RDWR);
  156. if (fd < 0)
  157. {
  158. DEBUG_ERROR("Can not open mtdblock1\r\n");
  159. result = FAIL;
  160. }
  161. else
  162. {
  163. // Write image to flash
  164. DEBUG_INFO("Writing image to mtdblock1...\n");
  165. wrd=write(fd, ptr+48, DataLength);
  166. close(fd);
  167. DEBUG_INFO(">> mtdblock1 written length: 0x%x\r\n", wrd);
  168. if(wrd != DataLength)
  169. {
  170. result = FAIL;
  171. }
  172. else
  173. {
  174. // Open flash target mtdblock
  175. fd = open("/dev/mtdblock3", O_RDWR);
  176. if (fd < 0)
  177. {
  178. DEBUG_ERROR("Can not open mtdblock3\r\n");
  179. result = FAIL;
  180. }
  181. else
  182. {
  183. // Write image to flash
  184. DEBUG_INFO("Writing image to mtdblock3...\n");
  185. wrd=write(fd, ptr, DataLength);
  186. close(fd);
  187. DEBUG_INFO(">> mtdblock3 written length: 0x%x\r\n", wrd);
  188. if(wrd != DataLength)
  189. {
  190. result = FAIL;
  191. }
  192. else
  193. {
  194. result = PASS;
  195. }
  196. }
  197. }
  198. }
  199. break;
  200. case CSU_KERNEL_CONFIGURATION:
  201. fd = open("/dev/mtdblock4", O_RDWR);
  202. if (fd < 0)
  203. {
  204. DEBUG_ERROR("Can not open mtdblock4\r\n");
  205. result = FAIL;
  206. }
  207. else
  208. {
  209. // Write image to flash
  210. DEBUG_INFO("Writing image to mtdblock4...\n");
  211. wrd=write(fd, ptr+48, DataLength);
  212. close(fd);
  213. DEBUG_INFO(">> mtdblock4 written length: 0x%x\r\n", wrd);
  214. if(wrd != DataLength)
  215. {
  216. result = FAIL;
  217. }
  218. else
  219. {
  220. // Open flash target mtdblock
  221. fd = open("/dev/mtdblock5", O_RDWR);
  222. if (fd < 0)
  223. {
  224. DEBUG_ERROR("Can not open mtdblock5\r\n");
  225. result = FAIL;
  226. }
  227. else
  228. {
  229. // Write image to flash
  230. DEBUG_INFO("Writing image to mtdblock5...\n");
  231. wrd=write(fd, ptr+48, DataLength);
  232. close(fd);
  233. DEBUG_INFO(">> mtdblock5 written length: 0x%x\r\n", wrd);
  234. if(wrd != DataLength)
  235. {
  236. result = FAIL;
  237. }
  238. else
  239. {
  240. result = PASS;
  241. }
  242. }
  243. }
  244. }
  245. break;
  246. case CSU_KERNEL_IMAGE:
  247. fd = open("/dev/mtdblock6", O_RDWR);
  248. if (fd < 0)
  249. {
  250. DEBUG_ERROR("Can not open mtdblock6\r\n");
  251. result = FAIL;
  252. }
  253. else
  254. {
  255. // Write image to flash
  256. DEBUG_INFO("Writing image to mtdblock6...\n");
  257. wrd=write(fd, ptr+48, DataLength);
  258. close(fd);
  259. DEBUG_INFO(">> mtdblock6 written length: 0x%x\r\n", wrd);
  260. if(wrd != DataLength)
  261. {
  262. result = FAIL;
  263. }
  264. else
  265. {
  266. // Open flash target mtdblock
  267. fd = open("/dev/mtdblock7", O_RDWR);
  268. if (fd < 0)
  269. {
  270. DEBUG_ERROR("Can not open mtdblock7\r\n");
  271. result = FAIL;
  272. }
  273. else
  274. {
  275. // Write image to flash
  276. DEBUG_INFO("Writing image to mtdblock7...\n");
  277. wrd=write(fd, ptr+48, DataLength);
  278. close(fd);
  279. DEBUG_INFO(">> mtdblock7 written length: 0x%x\r\n", wrd);
  280. if(wrd != DataLength)
  281. {
  282. result = FAIL;
  283. }
  284. else
  285. {
  286. result = PASS;
  287. }
  288. }
  289. }
  290. }
  291. break;
  292. case CSU_ROOT_FILE_SYSTEM:
  293. fd = open("/dev/mtdblock8", O_RDWR);
  294. if(fd < 0)
  295. {
  296. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  297. result = FAIL;
  298. }
  299. else
  300. {
  301. DEBUG_INFO("Writing image to mtdblock8...\n");
  302. wrd=write(fd, ptr+48, DataLength);
  303. close(fd);
  304. DEBUG_INFO(">> mtdblock8 written length: 0x%x\r\n", wrd);
  305. if(wrd!=DataLength)
  306. {
  307. result = FAIL;
  308. }
  309. else
  310. {
  311. fd = open("/dev/mtdblock9", O_RDWR);
  312. if(fd < 0)
  313. {
  314. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  315. result = FAIL;
  316. }
  317. DEBUG_INFO("Writing image to mtdblock9...\n");
  318. wrd=write(fd, ptr+48, DataLength);
  319. close(fd);
  320. DEBUG_INFO(">> mtdblock9 written length: 0x%x\r\n", wrd);
  321. if(wrd!=DataLength)
  322. {
  323. result = FAIL;
  324. }
  325. else
  326. {
  327. result = PASS;
  328. }
  329. }
  330. }
  331. break;
  332. case CSU_USER_CONFIGURATION:
  333. // Open flash target mtdblock
  334. fd = open("/dev/mtdblock10", O_RDWR);
  335. if (fd < 0)
  336. {
  337. DEBUG_ERROR("Can not open mtdblock10\r\n");
  338. result = FAIL;
  339. }
  340. else
  341. {
  342. // Write image to flash
  343. DEBUG_INFO("Writing image to mtdblock10...\n");
  344. wrd=write(fd, ptr+48, DataLength);
  345. close(fd);
  346. DEBUG_INFO(">> mtdblock10 written length: 0x%x\r\n", wrd);
  347. if(wrd != DataLength)
  348. {
  349. result = FAIL;
  350. }
  351. else
  352. {
  353. // Open flash target mtdblock
  354. fd = open("/dev/mtdblock11", O_RDWR);
  355. if (fd < 0)
  356. {
  357. DEBUG_ERROR("Can not open mtdblock11\r\n");
  358. result = FAIL;
  359. }
  360. else
  361. {
  362. // Write image to flash
  363. DEBUG_INFO("Writing image to mtdblock11...\n");
  364. wrd=write(fd, ptr+48, DataLength);
  365. close(fd);
  366. DEBUG_INFO(">> mtdblock11 written length: 0x%x\r\n", wrd);
  367. if(wrd != DataLength)
  368. {
  369. result = FAIL;
  370. }
  371. else
  372. {
  373. result = PASS;
  374. }
  375. }
  376. }
  377. }
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. else
  384. DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
  385. }
  386. else
  387. DEBUG_ERROR("Firmware image length mismatch.\r\n");
  388. }
  389. else
  390. DEBUG_ERROR("Firmware image type mismatch.\r\n");
  391. }
  392. free(ptr);
  393. if(result == PASS)
  394. DEBUG_INFO("Update image success\r\n");
  395. else
  396. DEBUG_ERROR("Update image fail\r\n");
  397. return result;
  398. }
  399. //================================================
  400. // UART update function
  401. //================================================
  402. int uart_tranceive(int fd, unsigned char* cmd, unsigned char* rx, int len, unsigned char needErase)
  403. {
  404. tcflush(fd,TCIOFLUSH);
  405. if(write(fd, cmd, len) >= len)
  406. {
  407. len = 0;
  408. if (needErase == 0x01)
  409. sleep(5);
  410. else
  411. usleep(500000);
  412. len = read(fd, rx, 512);
  413. }
  414. else
  415. {
  416. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  417. }
  418. return len;
  419. }
  420. unsigned char uart_update_start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  421. {
  422. unsigned char result = FAIL;
  423. unsigned char tx[11] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_START, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
  424. unsigned char rx[512];
  425. unsigned char chksum = 0x00;
  426. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  427. chksum ^= tx[6+idx];
  428. tx[10] = chksum;
  429. if(uart_tranceive(fd, tx, rx, 11, 0x01) >0)
  430. {
  431. chksum = 0x00;
  432. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  433. {
  434. chksum ^= rx[6+idx];
  435. }
  436. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  437. (rx[2] == tx[1]) &&
  438. (rx[1] == tx[2]) &&
  439. (rx[3] == tx[3]) &&
  440. (rx[6] == 0x01))
  441. {
  442. result = PASS;
  443. DEBUG_INFO("UART target is ready for upgrade.\n");
  444. }
  445. else
  446. {
  447. DEBUG_INFO("UART target is not ready...\n");
  448. }
  449. }
  450. else
  451. {
  452. DEBUG_ERROR("UART receiving update start ack failed...\n");
  453. }
  454. return result;
  455. }
  456. unsigned char uart_update_abord(unsigned char fd, unsigned char targetAddr)
  457. {
  458. unsigned char result = FAIL;
  459. unsigned char tx[7] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_ABORD, 0x04, 0x00, 0x00};
  460. unsigned char rx[512];
  461. unsigned char chksum = 0x00;
  462. if(uart_tranceive(fd, tx, rx, 7, 0x00) >0)
  463. {
  464. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  465. {
  466. chksum ^= rx[6+idx];
  467. }
  468. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  469. (rx[2] == tx[1]) &&
  470. (rx[1] == tx[2]) &&
  471. (rx[3] == tx[3]) &&
  472. (rx[6] == 0x01))
  473. {
  474. result = PASS;
  475. DEBUG_INFO("UART target abord update OK.\n");
  476. }
  477. else
  478. {
  479. DEBUG_ERROR("UART target abord update failed.\n");
  480. }
  481. }
  482. else
  483. {
  484. DEBUG_ERROR("UART receiving update abord ack failed...\n");
  485. }
  486. return result;
  487. }
  488. unsigned char uart_update_transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  489. {
  490. unsigned char result = FAIL;
  491. unsigned char tx[11 + length];
  492. unsigned char rx[512];
  493. unsigned char chksum = 0x00;
  494. tx[0] = 0xaa;
  495. tx[1] = 0x00;
  496. tx[2] = targetAddr;
  497. tx[3] = UART_CMD_UPDATE_TRANSFER;
  498. tx[4] = (4 + length) & 0xff;
  499. tx[5] = ((4 + length)>>8) & 0xff;
  500. tx[6] = (startAddr>>0) & 0xff;
  501. tx[7] = (startAddr>>8) & 0xff;
  502. tx[8] = (startAddr>>16) & 0xff;
  503. tx[9] = (startAddr>>24) & 0xff;
  504. memcpy(tx+10, data, length);
  505. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  506. chksum ^= tx[6+idx];
  507. tx[sizeof(tx)-1] = chksum;
  508. if(uart_tranceive(fd, tx, rx, 11 + length,0x00) >0)
  509. {
  510. chksum = 0;
  511. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  512. {
  513. chksum ^= rx[6+idx];
  514. }
  515. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  516. (rx[2] == tx[1]) &&
  517. (rx[1] == tx[2]) &&
  518. (rx[3] == tx[3]) &&
  519. (rx[6] == 0x01))
  520. {
  521. result = PASS;
  522. }
  523. }
  524. else
  525. {
  526. DEBUG_ERROR("UART receiving update transfer ack failed...\n");
  527. }
  528. return result;
  529. }
  530. unsigned char uart_update_finish(unsigned char fd, unsigned char targetAddr)
  531. {
  532. unsigned char result = FAIL;
  533. unsigned char tx[7] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_FINISH, 0x04, 0x00, 0x00};
  534. unsigned char rx[512];
  535. unsigned char chksum = 0x00;
  536. if(uart_tranceive(fd, tx, rx, 7,0x00) >0)
  537. {
  538. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  539. {
  540. chksum ^= rx[6+idx];
  541. }
  542. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  543. (rx[2] == tx[1]) &&
  544. (rx[1] == tx[2]) &&
  545. (rx[3] == tx[3]) &&
  546. (rx[6] == 0x01))
  547. {
  548. result = PASS;
  549. DEBUG_INFO("UART update finish check OK...\n");
  550. }
  551. else
  552. {
  553. DEBUG_ERROR("UART update finish check failed...\n");
  554. }
  555. }
  556. else
  557. {
  558. DEBUG_ERROR("UART receiving update finish ack failed...\n");
  559. }
  560. return result;
  561. }
  562. int Upgrade_UART(unsigned char uartfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
  563. {
  564. int result = FAIL;
  565. long int MaxLen=48*1024*1024, ImageLen=0;
  566. unsigned int ImageCRC=0, DataLength=0;
  567. int fd;
  568. fd = open(SourcePath, O_RDONLY);
  569. if(fd < 0)
  570. {
  571. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  572. return result;
  573. }
  574. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  575. memset(ptr,0xFF,MaxLen); //-48 is take out the header
  576. //get the image length
  577. ImageLen = read(fd,ptr,MaxLen);
  578. close(fd);
  579. //read out the header
  580. int i;
  581. int isModelNameOK = PASS;
  582. for(i=0;i<16;i++)
  583. {
  584. if(ModelName[i] != ptr[i])
  585. {
  586. isModelNameOK = FAIL;
  587. }
  588. }
  589. if(isModelNameOK == FAIL)
  590. {
  591. DEBUG_ERROR("Model name mismatch...\n");
  592. }
  593. else
  594. {
  595. // check if the firmware type is correct
  596. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  597. {
  598. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  599. {
  600. DataLength = ImageLen-48;
  601. // get CRC in the header
  602. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  603. // calculate the image CRC
  604. DEBUG_INFO("CRC32 in image: 0x%08X\r\n",ImageCRC);
  605. DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
  606. if(crc32(ptr,ImageLen/*34+DataLength*/) == ImageCRC)
  607. {
  608. if(uart_update_start(uartfd, TargetAddr, crc32(ptr+48,DataLength))==PASS)
  609. {
  610. int CNT_Fail = 0;
  611. int CNT_Trans = 0;
  612. do
  613. {
  614. if(uart_update_transfer(uartfd, TargetAddr, CNT_Trans*1024, ptr+48+(CNT_Trans*1024), 1024)==PASS)
  615. {
  616. CNT_Fail = 0;
  617. CNT_Trans++;
  618. DEBUG_INFO("Upgrade progress:%.2f%%\r\n", ((float)(CNT_Trans*1024))/(DataLength)*100);
  619. }
  620. else
  621. {
  622. DEBUG_WARN("Data transfer fail, retry %d \r\n", ++CNT_Fail);
  623. }
  624. }while(DataLength-(CNT_Trans*1024)>0 && CNT_Fail<3);
  625. if(CNT_Fail>=3)
  626. {
  627. uart_update_abord(uartfd, TargetAddr);
  628. DEBUG_ERROR("UART upgrade retry > limits, aboard upgrade.\r\n");
  629. }
  630. else if(uart_update_finish(uartfd, TargetAddr)==PASS)
  631. {
  632. result = PASS;
  633. }
  634. }
  635. else
  636. DEBUG_ERROR("UART upgrade request failed.\n");
  637. }
  638. else
  639. DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
  640. }
  641. else
  642. DEBUG_ERROR("Firmware image length mismatch.\r\n");
  643. }
  644. else
  645. DEBUG_ERROR("Firmware image type mismatch.\r\n");
  646. }
  647. free(ptr);
  648. return result;
  649. }
  650. //================================================
  651. // CANBUS update function
  652. //================================================
  653. unsigned long GetTimeoutValue(struct timeval _sour_time)
  654. {
  655. struct timeval _end_time;
  656. gettimeofday(&_end_time, NULL);
  657. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  658. }
  659. int CAN_Download_REQ(int canfd,unsigned int Slave_Addr, unsigned int imageSize)
  660. {
  661. struct can_frame frame;
  662. frame.can_id = (0x00000E00 + Slave_Addr) | 0x80000000; //extended frame : �� 31 ��n�� 1
  663. frame.can_dlc = 0x07;
  664. frame.data[0] = 0x04; //0x01:Configuration file, 0x02:Bootloader of primary side MCU, 0x03:Firmware (main code) of primary side MCU, 0x04:Bootloader of secondary side MCU, 0x05:Firmware (main code) of secondary side MCU
  665. frame.data[1] = (imageSize>>0)&0xff; //Total 384 KBytes
  666. frame.data[2] = (imageSize>>8)&0xff; //Total 384 KBytes
  667. frame.data[3] = (imageSize>>16)&0xff; //Total 384 KBytes
  668. frame.data[4] = (imageSize>>24)&0xff; //Total 384 KBytes
  669. frame.data[5] = 0x10; //16 blocks
  670. frame.data[6] = 0x18; //24 KBytes
  671. DEBUG_INFO( "File size = %x, %d \n", imageSize, imageSize);
  672. write(canfd, &frame, sizeof(struct can_frame));
  673. if (canfd > 0)
  674. {
  675. struct timeval timer;
  676. gettimeofday(&timer, NULL);
  677. while (GetTimeoutValue(timer) < 5000000)
  678. {
  679. struct can_frame frame;
  680. int len;
  681. len = read(canfd, &frame, sizeof(struct can_frame));
  682. if (len >= 0)
  683. {
  684. DEBUG_INFO( "*****************************CAN_Download_REQ Get***************************** \n");
  685. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK);
  686. if (((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000E00) && frame.data[0] == 1)
  687. {
  688. DEBUG_INFO("PASS \n");
  689. return PASS;
  690. }
  691. }
  692. }
  693. }
  694. return FAIL;
  695. }
  696. int CAN_Start_BLK_Trans(int canfd,unsigned int Slave_Addr,unsigned int Block_No,unsigned int Block_Checksum)
  697. {
  698. struct can_frame frame;
  699. frame.can_id = (0x00000F00 + Slave_Addr) | 0x80000000; //extended frame : �� 31 ��n�� 1
  700. frame.can_dlc = 0x02;
  701. frame.data[0] = Block_No;
  702. frame.data[1] = Block_Checksum;
  703. DEBUG_INFO("Block_No = %x, Block_Checksum = %x \n", Block_No, Block_Checksum);
  704. write(canfd, &frame, sizeof(struct can_frame));
  705. usleep(100000);
  706. if (canfd > 0)
  707. {
  708. struct timeval timer;
  709. gettimeofday(&timer, NULL);
  710. while (GetTimeoutValue(timer) < 1000000)
  711. {
  712. struct can_frame frame;
  713. int len;
  714. len = read(canfd, &frame, sizeof(struct can_frame));
  715. if(len >= 0)
  716. {
  717. DEBUG_INFO("*****************************CAN_Start_BLK_Trans Get***************************** \n");
  718. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame ����T�ݭn���z�L CAN_EFF_MASK �~�|����u������T
  719. if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000F00) &&frame.data[0] == 1)
  720. {
  721. DEBUG_INFO("CAN_Start_BLK_Trans PASS \n");
  722. return PASS;
  723. }
  724. }
  725. }
  726. }
  727. return FAIL;
  728. }
  729. void CAN_Data_Trans(int canfd,unsigned int Slave_Addr,long Data_num,unsigned char Data[])
  730. {
  731. struct can_frame frame;
  732. frame.can_id = (0x00001000 + Slave_Addr) | 0x80000000; //extended frame : �� 31 ��n�� 1
  733. frame.can_dlc = 0x08;
  734. frame.data[0] = Data[Data_num+0];
  735. frame.data[1] = Data[Data_num+1];
  736. frame.data[2] = Data[Data_num+2];
  737. frame.data[3] = Data[Data_num+3];
  738. frame.data[4] = Data[Data_num+4];
  739. frame.data[5] = Data[Data_num+5];
  740. frame.data[6] = Data[Data_num+6];
  741. frame.data[7] = Data[Data_num+7];
  742. // DEBUG_INFO("%02x %02x %02x %02x %02x %02x %02x %02x \n", frame.data[0], frame.data[1], frame.data[2], frame.data[3],
  743. // frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
  744. write(canfd, &frame, sizeof(struct can_frame));
  745. usleep(2000);
  746. }
  747. int CAN_Download_FIN(int canfd,unsigned int Slave_Addr)
  748. {
  749. struct can_frame frame;
  750. frame.can_id = (0x00001100 + Slave_Addr) | 0x80000000; //extended frame
  751. frame.can_dlc = 0x00;
  752. write(canfd, &frame, sizeof(struct can_frame));
  753. usleep(10000);
  754. if (canfd > 0)
  755. {
  756. struct timeval timer;
  757. gettimeofday(&timer, NULL);
  758. while (GetTimeoutValue(timer) < 1000000)
  759. {
  760. struct can_frame frame;
  761. int len;
  762. len = read(canfd, &frame, sizeof(struct can_frame));
  763. if(len >= 0)
  764. {
  765. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame
  766. if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08001100) && frame.data[0] == 1)
  767. {
  768. DEBUG_INFO("CAN_Download_FIN PASS \n");
  769. return PASS;
  770. }
  771. }
  772. }
  773. }
  774. return FAIL;
  775. }
  776. int Checksum_Cal(unsigned int StartAdress,unsigned int length, unsigned char Data[])
  777. {
  778. unsigned char checksum = 0x00;
  779. for(unsigned int i = 0; i < length; i++)
  780. {
  781. //DEBUG_INFO("value = %x \n", Data[StartAdress + i]);
  782. checksum ^= Data[StartAdress + i];
  783. //DEBUG_INFO("checksum = %x \n", checksum);
  784. }
  785. return checksum;
  786. }
  787. int Upgrade_CAN(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
  788. {
  789. int result = FAIL;
  790. long int MaxLen=48*1024*1024, ImageLen=0;
  791. unsigned int ImageCRC=0, DataLength=0;
  792. int fd;
  793. fd = open(SourcePath, O_RDONLY);
  794. if(fd < 0)
  795. {
  796. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  797. return result;
  798. }
  799. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  800. memset(ptr,0xFF,MaxLen); //-48 is take out the header
  801. //get the image length
  802. ImageLen = read(fd,ptr,MaxLen);
  803. close(fd);
  804. //read out the header
  805. int i;
  806. int isModelNameOK = PASS;
  807. for(i=0;i<16;i++) {
  808. if(ModelName[i] != ptr[i]){
  809. isModelNameOK = FAIL;
  810. }
  811. }
  812. if(isModelNameOK == FAIL)
  813. {
  814. DEBUG_ERROR("The model name is wrong...\n");
  815. return result;
  816. }
  817. else
  818. {
  819. // check if the firmware type is correct
  820. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  821. {
  822. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  823. {
  824. DataLength = ImageLen-48;
  825. // get CRC in the header
  826. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  827. // calculate the image CRC
  828. DEBUG_INFO("CRC32 in image: 0x%08X\r\n",ImageCRC);
  829. DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
  830. if(crc32(ptr,ImageLen/*34+DataLength*/) == ImageCRC)
  831. {
  832. unsigned int Checksum[16];
  833. for(int i=0;i<16;i++)
  834. {
  835. Checksum[i] = Checksum_Cal(i * 24576, 24576, ptr + 48);
  836. }
  837. if(CAN_Download_REQ(canfd, TargetAddr, DataLength) == PASS)
  838. {
  839. for(int block = 1; block <= 16; block++)
  840. {
  841. if(CAN_Start_BLK_Trans(canfd, TargetAddr, block, Checksum[block - 1]) == PASS)
  842. {
  843. for(int times = 0; times < 3072; times++)
  844. {
  845. CAN_Data_Trans(canfd, TargetAddr, ((block - 1) * 24576 + times * 8), ptr + 48);
  846. }
  847. DEBUG_INFO(" \r\n\r\n");
  848. }
  849. else
  850. {
  851. free(ptr);
  852. return result;
  853. }
  854. }
  855. if (CAN_Download_FIN(canfd, TargetAddr) == PASS)
  856. result = PASS;
  857. }
  858. else
  859. DEBUG_ERROR("CANBUS upgrade request failed.\n");
  860. }
  861. else
  862. DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
  863. }
  864. else
  865. DEBUG_ERROR("Firmware image length mismatch.\r\n");
  866. }
  867. else
  868. DEBUG_ERROR("Firmware image type mismatch.\r\n");
  869. }
  870. free(ptr);
  871. return result;
  872. }