Module_Upgrade.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /*
  2. * Module_Upgrade.c
  3. *
  4. * Created on: 2020-01-21
  5. * Author: Jerry Wang
  6. * Version: D0.03
  7. */
  8. #include "Module_Upgrade.h"
  9. //==================================
  10. // PRINT OUT LOG FORMAT
  11. //==================================
  12. #define DEBUG_INFO(format, args...) storeLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  13. #define DEBUG_WARN(format, args...) storeLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  14. #define DEBUG_ERROR(format, args...) storeLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  15. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  16. #define PASS 1
  17. #define FAIL -1
  18. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  19. struct StatusCodeData *ShmStatusCodeData;
  20. struct FanModuleData *ShmFanModuleData;
  21. int storeLogMsg(const char *fmt, ...)
  22. {
  23. char Buf[4096+256];
  24. char buffer[4096];
  25. time_t CurrentTime;
  26. struct tm *tm;
  27. va_list args;
  28. va_start(args, fmt);
  29. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  30. va_end(args);
  31. memset(Buf,0,sizeof(Buf));
  32. CurrentTime = time(NULL);
  33. tm=localtime(&CurrentTime);
  34. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Upgrade_SystemLog",
  35. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  36. buffer,
  37. tm->tm_year+1900,tm->tm_mon+1);
  38. #ifdef SystemLogMessage
  39. system(Buf);
  40. #endif
  41. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  42. return rc;
  43. }
  44. int DiffTimebByUpgrade(struct timeb ST, struct timeb ET)
  45. {
  46. //return milli-second
  47. unsigned int StartTime,StopTime;
  48. StartTime=(unsigned int)ST.time;
  49. StopTime=(unsigned int)ET.time;
  50. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  51. }
  52. unsigned char *memcat(unsigned char *dest, unsigned int dest_len, unsigned char *src, unsigned int src_len)
  53. {
  54. memcpy(dest+dest_len, src, src_len);
  55. return dest;
  56. }
  57. uint32_t crc32(uint8_t *data, unsigned int length)
  58. {
  59. uint8_t i;
  60. uint32_t cnt = 0;
  61. uint32_t crc = 0xffffffff; // Initial value
  62. while(length--)
  63. {
  64. if(cnt>33 && cnt<48) {
  65. data++;
  66. }else {
  67. crc ^= *data++; // crc ^= *data; data++;
  68. for (i = 0; i < 8; ++i)
  69. {
  70. if (crc & 1)
  71. crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
  72. else
  73. crc = (crc >> 1);
  74. }
  75. }
  76. cnt++;
  77. }
  78. return ~crc;
  79. }
  80. int Upgrade_Flash(unsigned int Type,char *SourcePath,char *ModelName)
  81. {
  82. int result = FAIL;
  83. long int MaxLen=48*1024*1024, ImageLen=0;
  84. unsigned int ImageCRC=0, DataLength=0;
  85. int wrd,fd;
  86. // space max size set
  87. switch(Type)
  88. {
  89. case CSU_BOOTLOADER:
  90. MaxLen = 1*1024*1024;
  91. //DEBUG_INFO("Image type: U-Boot\r\n");
  92. break;
  93. case CSU_KERNEL_CONFIGURATION:
  94. MaxLen = 0.5*1024*1024;
  95. DEBUG_INFO("Image type: DTB\r\n");
  96. break;
  97. case CSU_KERNEL_IMAGE:
  98. MaxLen = 10*1024*1024;
  99. DEBUG_INFO("Image type: Kernel\r\n");
  100. break;
  101. case CSU_ROOT_FILE_SYSTEM:
  102. MaxLen = 48*1024*1024;
  103. DEBUG_INFO("Image type: Root fs\r\n");
  104. break;
  105. case CSU_USER_CONFIGURATION:
  106. MaxLen = 6*1024*1024+48;
  107. DEBUG_INFO("Image type: Config\r\n");
  108. break;
  109. default:
  110. break;
  111. }
  112. fd = open(SourcePath, O_RDONLY);
  113. if(fd < 0)
  114. {
  115. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  116. return result;
  117. }
  118. unsigned char *ptr = malloc(MaxLen);
  119. memset(ptr,0xFF,MaxLen);
  120. //get the image length
  121. ImageLen = read(fd,ptr,MaxLen);
  122. close(fd);
  123. //read out the header
  124. int i;
  125. int isModelNameOK = PASS;
  126. for(i=0;i<16;i++)
  127. {
  128. if(ModelName[i] != ptr[i])
  129. {
  130. isModelNameOK = FAIL;
  131. }
  132. }
  133. if(isModelNameOK == FAIL)
  134. {
  135. DEBUG_ERROR("Model name mismatch.\r\n");
  136. }
  137. else
  138. {
  139. // check if the firmware type is correct
  140. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  141. {
  142. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  143. {
  144. DataLength = ImageLen-48;
  145. // get CRC in the header
  146. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  147. // calculate the image CRC
  148. DEBUG_INFO("CRC32 in image: 0x%08X\r\n",ImageCRC);
  149. DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
  150. if(crc32(ptr,ImageLen) == ImageCRC)
  151. {
  152. // Write image to target flash block
  153. switch(Type)
  154. {
  155. case FLASH_IMAGE_TYPE_SPL:
  156. fd = open("/dev/mtdblock0", O_RDWR);
  157. if (fd < 0)
  158. {
  159. DEBUG_ERROR("Can not open mtdblock0\r\n");
  160. result = FAIL;
  161. }
  162. else
  163. {
  164. // Write image to flash
  165. DEBUG_INFO("Writing image to mtdblock0...\n");
  166. wrd=write(fd, ptr+48, DataLength);
  167. close(fd);
  168. DEBUG_INFO(">> mtdblock0 Written length: 0x%x\r\n", wrd);
  169. if(wrd != DataLength)
  170. {
  171. result = FAIL;
  172. }
  173. else
  174. {
  175. result = PASS;
  176. }
  177. }
  178. break;
  179. case CSU_BOOTLOADER:
  180. fd = open("/dev/mtdblock1", O_RDWR);
  181. if (fd < 0)
  182. {
  183. DEBUG_ERROR("Can not open mtdblock1\r\n");
  184. result = FAIL;
  185. }
  186. else
  187. {
  188. // Write image to flash
  189. DEBUG_INFO("Writing image to mtdblock1...\n");
  190. wrd=write(fd, ptr+48, DataLength);
  191. close(fd);
  192. DEBUG_INFO(">> mtdblock1 written length: 0x%x\r\n", wrd);
  193. if(wrd != DataLength)
  194. {
  195. result = FAIL;
  196. }
  197. else
  198. {
  199. // Open flash target mtdblock
  200. fd = open("/dev/mtdblock3", O_RDWR);
  201. if (fd < 0)
  202. {
  203. DEBUG_ERROR("Can not open mtdblock3\r\n");
  204. result = FAIL;
  205. }
  206. else
  207. {
  208. // Write image to flash
  209. DEBUG_INFO("Writing image to mtdblock3...\n");
  210. wrd=write(fd, ptr+48, DataLength);
  211. close(fd);
  212. DEBUG_INFO(">> mtdblock3 written length: 0x%x\r\n", wrd);
  213. if(wrd != DataLength)
  214. {
  215. result = FAIL;
  216. }
  217. else
  218. {
  219. result = PASS;
  220. }
  221. }
  222. }
  223. }
  224. break;
  225. case CSU_KERNEL_CONFIGURATION:
  226. fd = open("/dev/mtdblock4", O_RDWR);
  227. if (fd < 0)
  228. {
  229. DEBUG_ERROR("Can not open mtdblock4\r\n");
  230. result = FAIL;
  231. }
  232. else
  233. {
  234. // Write image to flash
  235. DEBUG_INFO("Writing image to mtdblock4...\n");
  236. wrd=write(fd, ptr+48, DataLength);
  237. close(fd);
  238. DEBUG_INFO(">> mtdblock4 written length: 0x%x\r\n", wrd);
  239. if(wrd != DataLength)
  240. {
  241. result = FAIL;
  242. }
  243. else
  244. {
  245. // Open flash target mtdblock
  246. fd = open("/dev/mtdblock5", O_RDWR);
  247. if (fd < 0)
  248. {
  249. DEBUG_ERROR("Can not open mtdblock5\r\n");
  250. result = FAIL;
  251. }
  252. else
  253. {
  254. // Write image to flash
  255. DEBUG_INFO("Writing image to mtdblock5...\n");
  256. wrd=write(fd, ptr+48, DataLength);
  257. close(fd);
  258. DEBUG_INFO(">> mtdblock5 written length: 0x%x\r\n", wrd);
  259. if(wrd != DataLength)
  260. {
  261. result = FAIL;
  262. }
  263. else
  264. {
  265. result = PASS;
  266. }
  267. }
  268. }
  269. }
  270. break;
  271. case CSU_KERNEL_IMAGE:
  272. fd = open("/dev/mtdblock6", O_RDWR);
  273. if (fd < 0)
  274. {
  275. DEBUG_ERROR("Can not open mtdblock6\r\n");
  276. result = FAIL;
  277. }
  278. else
  279. {
  280. // Write image to flash
  281. DEBUG_INFO("Writing image to mtdblock6...\n");
  282. wrd=write(fd, ptr+48, DataLength);
  283. close(fd);
  284. DEBUG_INFO(">> mtdblock6 written length: 0x%x\r\n", wrd);
  285. if(wrd != DataLength)
  286. {
  287. result = FAIL;
  288. }
  289. else
  290. {
  291. // Open flash target mtdblock
  292. fd = open("/dev/mtdblock7", O_RDWR);
  293. if (fd < 0)
  294. {
  295. DEBUG_ERROR("Can not open mtdblock7\r\n");
  296. result = FAIL;
  297. }
  298. else
  299. {
  300. // Write image to flash
  301. DEBUG_INFO("Writing image to mtdblock7...\n");
  302. wrd=write(fd, ptr+48, DataLength);
  303. close(fd);
  304. DEBUG_INFO(">> mtdblock7 written length: 0x%x\r\n", wrd);
  305. if(wrd != DataLength)
  306. {
  307. result = FAIL;
  308. }
  309. else
  310. {
  311. result = PASS;
  312. }
  313. }
  314. }
  315. }
  316. break;
  317. case CSU_ROOT_FILE_SYSTEM:
  318. fd = open("/dev/mtdblock8", O_RDWR);
  319. if(fd < 0)
  320. {
  321. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  322. result = FAIL;
  323. }
  324. else
  325. {
  326. DEBUG_INFO("Writing image to mtdblock8...\n");
  327. wrd=write(fd, ptr+48, DataLength);
  328. close(fd);
  329. DEBUG_INFO(">> mtdblock8 written length: 0x%x\r\n", wrd);
  330. if(wrd!=DataLength)
  331. {
  332. result = FAIL;
  333. }
  334. else
  335. {
  336. fd = open("/dev/mtdblock9", O_RDWR);
  337. if(fd < 0)
  338. {
  339. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  340. result = FAIL;
  341. }
  342. DEBUG_INFO("Writing image to mtdblock9...\n");
  343. wrd=write(fd, ptr+48, DataLength);
  344. close(fd);
  345. DEBUG_INFO(">> mtdblock9 written length: 0x%x\r\n", wrd);
  346. if(wrd!=DataLength)
  347. {
  348. result = FAIL;
  349. }
  350. else
  351. {
  352. result = PASS;
  353. }
  354. }
  355. }
  356. break;
  357. case CSU_USER_CONFIGURATION:
  358. // Open flash target mtdblock
  359. fd = open("/dev/mtdblock10", O_RDWR);
  360. if (fd < 0)
  361. {
  362. DEBUG_ERROR("Can not open mtdblock10\r\n");
  363. result = FAIL;
  364. }
  365. else
  366. {
  367. // Write image to flash
  368. DEBUG_INFO("Writing image to mtdblock10...\n");
  369. wrd=write(fd, ptr+48, DataLength);
  370. close(fd);
  371. DEBUG_INFO(">> mtdblock10 written length: 0x%x\r\n", wrd);
  372. if(wrd != DataLength)
  373. {
  374. result = FAIL;
  375. }
  376. else
  377. {
  378. // Open flash target mtdblock
  379. fd = open("/dev/mtdblock11", O_RDWR);
  380. if (fd < 0)
  381. {
  382. DEBUG_ERROR("Can not open mtdblock11\r\n");
  383. result = FAIL;
  384. }
  385. else
  386. {
  387. // Write image to flash
  388. DEBUG_INFO("Writing image to mtdblock11...\n");
  389. wrd=write(fd, ptr+48, DataLength);
  390. close(fd);
  391. DEBUG_INFO(">> mtdblock11 written length: 0x%x\r\n", wrd);
  392. if(wrd != DataLength)
  393. {
  394. result = FAIL;
  395. }
  396. else
  397. {
  398. result = PASS;
  399. }
  400. }
  401. }
  402. }
  403. break;
  404. default:
  405. break;
  406. }
  407. }
  408. else
  409. DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
  410. }
  411. else
  412. DEBUG_ERROR("Firmware image length mismatch.\r\n");
  413. }
  414. else
  415. DEBUG_ERROR("Firmware image type mismatch.\r\n");
  416. }
  417. free(ptr);
  418. if(result == PASS)
  419. DEBUG_INFO("Update image success\r\n");
  420. else
  421. DEBUG_ERROR("Update image fail\r\n");
  422. return result;
  423. }
  424. //================================================
  425. // UART update function
  426. //================================================
  427. int uart_tranceive(int fd, unsigned char* cmd, unsigned char* rx, int len, unsigned char needErase)
  428. {
  429. tcflush(fd,TCIOFLUSH);
  430. if(write(fd, cmd, len) >= len)
  431. {
  432. len = 0;
  433. if (needErase == 0x01)
  434. sleep(5);
  435. else
  436. usleep(500000);
  437. len = read(fd, rx, 512);
  438. }
  439. else
  440. {
  441. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  442. }
  443. return len;
  444. }
  445. unsigned char uart_update_start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  446. {
  447. unsigned char result = FAIL;
  448. 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};
  449. unsigned char rx[512];
  450. unsigned char chksum = 0x00;
  451. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  452. chksum ^= tx[6+idx];
  453. tx[10] = chksum;
  454. if(uart_tranceive(fd, tx, rx, 11, 0x01) >0)
  455. {
  456. chksum = 0x00;
  457. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  458. {
  459. chksum ^= rx[6+idx];
  460. }
  461. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  462. (rx[2] == tx[1]) &&
  463. (rx[1] == tx[2]) &&
  464. (rx[3] == tx[3]) &&
  465. (rx[6] == 0x01))
  466. {
  467. result = PASS;
  468. DEBUG_INFO("UART target is ready for upgrade.\n");
  469. }
  470. else
  471. {
  472. DEBUG_INFO("UART target is not ready...\n");
  473. }
  474. }
  475. else
  476. {
  477. DEBUG_ERROR("UART receiving update start ack failed...\n");
  478. }
  479. return result;
  480. }
  481. unsigned char uart_update_abord(unsigned char fd, unsigned char targetAddr)
  482. {
  483. unsigned char result = FAIL;
  484. unsigned char tx[7] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_ABORD, 0x00, 0x00, 0x00};
  485. unsigned char rx[512];
  486. unsigned char chksum = 0x00;
  487. if(uart_tranceive(fd, tx, rx, 7, 0x00) >0)
  488. {
  489. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  490. {
  491. chksum ^= rx[6+idx];
  492. }
  493. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  494. (rx[2] == tx[1]) &&
  495. (rx[1] == tx[2]) &&
  496. (rx[3] == tx[3]) &&
  497. (rx[6] == 0x01))
  498. {
  499. result = PASS;
  500. DEBUG_INFO("UART target abord update OK.\n");
  501. }
  502. else
  503. {
  504. DEBUG_ERROR("UART target abord update failed.\n");
  505. }
  506. }
  507. else
  508. {
  509. DEBUG_ERROR("UART receiving update abord ack failed...\n");
  510. }
  511. return result;
  512. }
  513. unsigned char uart_update_transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  514. {
  515. unsigned char result = FAIL;
  516. unsigned char tx[11 + length];
  517. unsigned char rx[512];
  518. unsigned char chksum = 0x00;
  519. tx[0] = 0xaa;
  520. tx[1] = 0x00;
  521. tx[2] = targetAddr;
  522. tx[3] = UART_CMD_UPDATE_TRANSFER;
  523. tx[4] = (4 + length) & 0xff;
  524. tx[5] = ((4 + length)>>8) & 0xff;
  525. tx[6] = (startAddr>>0) & 0xff;
  526. tx[7] = (startAddr>>8) & 0xff;
  527. tx[8] = (startAddr>>16) & 0xff;
  528. tx[9] = (startAddr>>24) & 0xff;
  529. memcpy(tx+10, data, length);
  530. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  531. chksum ^= tx[6+idx];
  532. tx[sizeof(tx)-1] = chksum;
  533. if(uart_tranceive(fd, tx, rx, 11 + length,0x00) >0)
  534. {
  535. chksum = 0;
  536. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  537. {
  538. chksum ^= rx[6+idx];
  539. }
  540. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  541. (rx[2] == tx[1]) &&
  542. (rx[1] == tx[2]) &&
  543. (rx[3] == tx[3]) &&
  544. (rx[6] == 0x01))
  545. {
  546. result = PASS;
  547. }
  548. }
  549. else
  550. {
  551. DEBUG_ERROR("UART receiving update transfer ack failed...\n");
  552. }
  553. return result;
  554. }
  555. unsigned char uart_update_finish(unsigned char fd, unsigned char targetAddr)
  556. {
  557. unsigned char result = FAIL;
  558. unsigned char tx[7] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_FINISH, 0x00, 0x00, 0x00};
  559. unsigned char rx[512];
  560. unsigned char chksum = 0x00;
  561. if(uart_tranceive(fd, tx, rx, 7,0x00) >0)
  562. {
  563. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  564. {
  565. chksum ^= rx[6+idx];
  566. }
  567. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  568. (rx[2] == tx[1]) &&
  569. (rx[1] == tx[2]) &&
  570. (rx[3] == tx[3]) &&
  571. (rx[6] == 0x01))
  572. {
  573. result = PASS;
  574. DEBUG_INFO("UART update finish check OK...\n");
  575. }
  576. else
  577. {
  578. DEBUG_ERROR("UART update finish check failed...\n");
  579. }
  580. }
  581. else
  582. {
  583. DEBUG_ERROR("UART receiving update finish ack failed...\n");
  584. }
  585. return result;
  586. }
  587. int Upgrade_UART(unsigned char uartfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
  588. {
  589. int result = FAIL;
  590. long int MaxLen=48*1024*1024, ImageLen=0;
  591. unsigned int ImageCRC=0, DataLength=0;
  592. int fd;
  593. fd = open(SourcePath, O_RDONLY);
  594. if(fd < 0)
  595. {
  596. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  597. return result;
  598. }
  599. unsigned char *ptr = malloc(MaxLen);
  600. memset(ptr,0xFF,MaxLen);
  601. //get the image length
  602. ImageLen = read(fd,ptr,MaxLen);
  603. close(fd);
  604. //read out the header
  605. int i;
  606. int isModelNameOK = PASS;
  607. for(i=0;i<16;i++)
  608. {
  609. if(ModelName[i] != ptr[i])
  610. {
  611. isModelNameOK = FAIL;
  612. }
  613. }
  614. if(isModelNameOK == FAIL)
  615. {
  616. DEBUG_ERROR("Model name mismatch...\n");
  617. }
  618. else
  619. {
  620. // check if the firmware type is correct
  621. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  622. {
  623. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  624. {
  625. DataLength = ImageLen-48;
  626. // get CRC in the header
  627. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  628. // calculate the image CRC
  629. DEBUG_INFO("CRC32 in image: 0x%08X\r\n",ImageCRC);
  630. DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
  631. if(crc32(ptr,ImageLen) == ImageCRC)
  632. {
  633. if(uart_update_start(uartfd, TargetAddr, crc32(ptr+48,DataLength))==PASS)
  634. {
  635. int CNT_Fail = 0;
  636. int CNT_Trans = 0;
  637. do
  638. {
  639. if(uart_update_transfer(uartfd, TargetAddr, CNT_Trans*1024, ptr+48+(CNT_Trans*1024), 1024)==PASS)
  640. {
  641. CNT_Fail = 0;
  642. CNT_Trans++;
  643. DEBUG_INFO("Upgrade progress:%.2f%%\r\n", ((float)(CNT_Trans*1024))/(DataLength)*100);
  644. }
  645. else
  646. {
  647. DEBUG_WARN("Data transfer fail, retry %d \r\n", ++CNT_Fail);
  648. }
  649. }while(DataLength-(CNT_Trans*1024)>0 && CNT_Fail<3);
  650. if(CNT_Fail>=3)
  651. {
  652. uart_update_abord(uartfd, TargetAddr);
  653. DEBUG_ERROR("UART upgrade retry > limits, aboard upgrade.\r\n");
  654. }
  655. else if(uart_update_finish(uartfd, TargetAddr)==PASS)
  656. {
  657. result = PASS;
  658. }
  659. }
  660. else
  661. DEBUG_ERROR("UART upgrade request failed.\n");
  662. }
  663. else
  664. DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
  665. }
  666. else
  667. DEBUG_ERROR("Firmware image length mismatch.\r\n");
  668. }
  669. else
  670. DEBUG_ERROR("Firmware image type mismatch.\r\n");
  671. }
  672. free(ptr);
  673. return result;
  674. }
  675. //================================================
  676. // CANBUS update function
  677. //================================================
  678. unsigned long getTimeoutValue(struct timeval _sour_time)
  679. {
  680. struct timeval _end_time;
  681. gettimeofday(&_end_time, NULL);
  682. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  683. }
  684. int CAN_Download_REQ(int canfd,unsigned int Slave_Addr, unsigned int imageSize)
  685. {
  686. struct can_frame frame;
  687. frame.can_id = (0x00000E00 + Slave_Addr) | 0x80000000; //extended frame
  688. frame.can_dlc = 0x07;
  689. 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
  690. frame.data[1] = (imageSize>>0)&0xff; //Total 384 KBytes
  691. frame.data[2] = (imageSize>>8)&0xff; //Total 384 KBytes
  692. frame.data[3] = (imageSize>>16)&0xff; //Total 384 KBytes
  693. frame.data[4] = (imageSize>>24)&0xff; //Total 384 KBytes
  694. frame.data[5] = 0x10; //16 blocks
  695. frame.data[6] = 0x18; //24 KBytes
  696. DEBUG_INFO( "File size = %x, %d \n", imageSize, imageSize);
  697. write(canfd, &frame, sizeof(struct can_frame));
  698. if (canfd > 0)
  699. {
  700. struct timeval timer;
  701. gettimeofday(&timer, NULL);
  702. while (getTimeoutValue(timer) < 5000000)
  703. {
  704. struct can_frame frame;
  705. int len;
  706. len = read(canfd, &frame, sizeof(struct can_frame));
  707. if (len >= 0)
  708. {
  709. DEBUG_INFO( "*****************************CAN_Download_REQ Get***************************** \n");
  710. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK);
  711. if (((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000E00) && frame.data[0] == 1)
  712. {
  713. DEBUG_INFO("PASS \n");
  714. return PASS;
  715. }
  716. }
  717. }
  718. }
  719. return FAIL;
  720. }
  721. int CAN_Start_BLK_Trans(int canfd,unsigned int Slave_Addr,unsigned int Block_No,unsigned int Block_Checksum)
  722. {
  723. struct can_frame frame;
  724. frame.can_id = (0x00000F00 + Slave_Addr) | 0x80000000; //extended frame
  725. frame.can_dlc = 0x02;
  726. frame.data[0] = Block_No;
  727. frame.data[1] = Block_Checksum;
  728. DEBUG_INFO("Block_No = %x, Block_Checksum = %x \n", Block_No, Block_Checksum);
  729. write(canfd, &frame, sizeof(struct can_frame));
  730. usleep(100000);
  731. if (canfd > 0)
  732. {
  733. struct timeval timer;
  734. gettimeofday(&timer, NULL);
  735. while (getTimeoutValue(timer) < 1000000)
  736. {
  737. struct can_frame frame;
  738. int len;
  739. len = read(canfd, &frame, sizeof(struct can_frame));
  740. if(len >= 0)
  741. {
  742. DEBUG_INFO("*****************************CAN_Start_BLK_Trans Get***************************** \n");
  743. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame CAN_EFF_MASK
  744. if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000F00) &&frame.data[0] == 1)
  745. {
  746. DEBUG_INFO("CAN_Start_BLK_Trans PASS \n");
  747. return PASS;
  748. }
  749. }
  750. }
  751. }
  752. return FAIL;
  753. }
  754. void CAN_Data_Trans(int canfd,unsigned int Slave_Addr,long Data_num,unsigned char Data[])
  755. {
  756. struct can_frame frame;
  757. frame.can_id = (0x00001000 + Slave_Addr) | 0x80000000; //extended frame
  758. frame.can_dlc = 0x08;
  759. frame.data[0] = Data[Data_num+0];
  760. frame.data[1] = Data[Data_num+1];
  761. frame.data[2] = Data[Data_num+2];
  762. frame.data[3] = Data[Data_num+3];
  763. frame.data[4] = Data[Data_num+4];
  764. frame.data[5] = Data[Data_num+5];
  765. frame.data[6] = Data[Data_num+6];
  766. frame.data[7] = Data[Data_num+7];
  767. // DEBUG_INFO("%02x %02x %02x %02x %02x %02x %02x %02x \n", frame.data[0], frame.data[1], frame.data[2], frame.data[3],
  768. // frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
  769. write(canfd, &frame, sizeof(struct can_frame));
  770. usleep(2000);
  771. }
  772. int CAN_Download_FIN(int canfd,unsigned int Slave_Addr)
  773. {
  774. struct can_frame frame;
  775. frame.can_id = (0x00001100 + Slave_Addr) | 0x80000000; //extended frame
  776. frame.can_dlc = 0x00;
  777. write(canfd, &frame, sizeof(struct can_frame));
  778. usleep(10000);
  779. if (canfd > 0)
  780. {
  781. struct timeval timer;
  782. gettimeofday(&timer, NULL);
  783. while (getTimeoutValue(timer) < 1000000)
  784. {
  785. struct can_frame frame;
  786. int len;
  787. len = read(canfd, &frame, sizeof(struct can_frame));
  788. if(len >= 0)
  789. {
  790. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame
  791. if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08001100) && frame.data[0] == 1)
  792. {
  793. DEBUG_INFO("CAN_Download_FIN PASS \n");
  794. return PASS;
  795. }
  796. }
  797. }
  798. }
  799. return FAIL;
  800. }
  801. int Checksum_Cal(unsigned int StartAdress,unsigned int length, unsigned char Data[])
  802. {
  803. unsigned char checksum = 0x00;
  804. for(unsigned int i = 0; i < length; i++)
  805. {
  806. //DEBUG_INFO("value = %x \n", Data[StartAdress + i]);
  807. checksum ^= Data[StartAdress + i];
  808. //DEBUG_INFO("checksum = %x \n", checksum);
  809. }
  810. return checksum;
  811. }
  812. int Upgrade_CAN(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
  813. {
  814. int result = FAIL;
  815. long int MaxLen=48*1024*1024, ImageLen=0;
  816. unsigned int ImageCRC=0, DataLength=0;
  817. int fd;
  818. fd = open(SourcePath, O_RDONLY);
  819. if(fd < 0)
  820. {
  821. DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
  822. return result;
  823. }
  824. unsigned char *ptr = malloc(MaxLen);
  825. memset(ptr,0xFF,MaxLen);
  826. //get the image length
  827. ImageLen = read(fd,ptr,MaxLen);
  828. close(fd);
  829. //read out the header
  830. int i;
  831. int isModelNameOK = PASS;
  832. for(i=0;i<16;i++) {
  833. if(ModelName[i] != ptr[i]){
  834. isModelNameOK = FAIL;
  835. }
  836. }
  837. if(isModelNameOK == FAIL)
  838. {
  839. DEBUG_ERROR("Model name mismatch...\n");
  840. return result;
  841. }
  842. else
  843. {
  844. // check if the firmware type is correct
  845. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  846. {
  847. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  848. {
  849. DataLength = ImageLen-48;
  850. // get CRC in the header
  851. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  852. // calculate the image CRC
  853. DEBUG_INFO("CRC32 in image: 0x%08X\r\n",ImageCRC);
  854. DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
  855. if(crc32(ptr,ImageLen) == ImageCRC)
  856. {
  857. unsigned int Checksum[16];
  858. for(int i=0;i<16;i++)
  859. {
  860. Checksum[i] = Checksum_Cal(i * 24576, 24576, ptr + 48);
  861. }
  862. if(CAN_Download_REQ(canfd, TargetAddr, DataLength) == PASS)
  863. {
  864. for(int block = 1; block <= 16; block++)
  865. {
  866. if(CAN_Start_BLK_Trans(canfd, TargetAddr, block, Checksum[block - 1]) == PASS)
  867. {
  868. for(int times = 0; times < 3072; times++)
  869. {
  870. CAN_Data_Trans(canfd, TargetAddr, ((block - 1) * 24576 + times * 8), ptr + 48);
  871. }
  872. DEBUG_INFO(" \r\n\r\n");
  873. }
  874. else
  875. {
  876. free(ptr);
  877. return result;
  878. }
  879. }
  880. if (CAN_Download_FIN(canfd, TargetAddr) == PASS)
  881. result = PASS;
  882. }
  883. else
  884. DEBUG_ERROR("CANBUS upgrade request failed.\n");
  885. }
  886. else
  887. DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
  888. }
  889. else
  890. DEBUG_ERROR("Firmware image length mismatch.\r\n");
  891. }
  892. else
  893. DEBUG_ERROR("Firmware image type mismatch.\r\n");
  894. }
  895. free(ptr);
  896. return result;
  897. }
  898. //================================================
  899. // CCS update function
  900. //================================================
  901. int Check_CCS_image_header(unsigned int Type,char *SourcePath,char *ModelName)
  902. {
  903. long int MaxLen=48*1024*1024, ImageLen=0;
  904. unsigned int ImageCRC=0;
  905. int fd;
  906. // space max size set
  907. fd = open(SourcePath, O_RDONLY);
  908. if(fd < 0)
  909. {
  910. DEBUG_ERROR("Update CCS NG - can not open upgrade image\n");
  911. return FAIL;
  912. }
  913. switch(Type)
  914. {
  915. case CCS_BOARD_BOOTLOADER:
  916. MaxLen = 1*1024*1024;
  917. DEBUG_INFO("Prepare to upgrade CCS BOOTLOADER\n");
  918. break;
  919. case CCS_BOARD_KERNEL_CONFIGURATION:
  920. MaxLen = 0.5*1024*1024;
  921. DEBUG_INFO("Prepare to upgrade CCS KERNEL CONFIGURATION\n");
  922. break;
  923. case CCS_BOARD_KERNEL_IMAGE:
  924. MaxLen = 10*1024*1024;
  925. DEBUG_INFO("Prepare to upgrade CCS KERNEL\n");
  926. break;
  927. case CCS_BOARD_FILE_SYSTEM:
  928. MaxLen = 48*1024*1024;
  929. DEBUG_INFO("Prepare to upgrade CCS FILE SYSTEM\n");
  930. break;
  931. default:
  932. DEBUG_ERROR("Wrong image type for CCS upgrade\n");
  933. return FAIL;
  934. break;
  935. }
  936. unsigned char *ptr = malloc(MaxLen);
  937. memset(ptr,0xFF,MaxLen);
  938. //get the image length
  939. ImageLen = read(fd,ptr,MaxLen);
  940. close(fd);
  941. //read out the header
  942. int i;
  943. for(i=0;i<16;i++)
  944. {
  945. if(ModelName[i] != ptr[i])
  946. {
  947. DEBUG_ERROR("Model name mismatch.\r\n");
  948. return FAIL;
  949. }
  950. }
  951. // check if the firmware type is correct
  952. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  953. {
  954. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  955. {
  956. // get CRC in the header
  957. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  958. // calculate the image CRC
  959. DEBUG_INFO("CRC32 in CCS image: 0x%08X\r\n",ImageCRC);
  960. DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
  961. if(crc32(ptr,ImageLen) == ImageCRC)
  962. {
  963. return PASS;
  964. }
  965. else
  966. {
  967. DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
  968. return FAIL;
  969. }
  970. }
  971. else
  972. {
  973. DEBUG_ERROR("Firmware image length mismatch.\r\n");
  974. return FAIL;
  975. }
  976. }
  977. else
  978. {
  979. DEBUG_ERROR("Firmware image type mismatch.\r\n");
  980. return FAIL;
  981. }
  982. }
  983. int Put_CCS_image(char *SourcePath, unsigned char TargetAddr)
  984. {
  985. unsigned char ftpcmdbuf[256];
  986. unsigned char CCSIpAddress[16];
  987. //If ID of target EV board is 1, the IP address will be 192.168.0.21,
  988. //if ID of target EV board is 2, the IP address will be 192.168.0.22.
  989. sprintf((char*)CCSIpAddress,"192.168.0.2%d", TargetAddr);
  990. //Using ftpput command to transfer CCS upgrade image,
  991. //User name : root
  992. //User password : y42j/4cj84
  993. //Destination : /root/ccs.image
  994. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s /root/ccs.image %s",
  995. CCSIpAddress, SourcePath);
  996. if(system((char*)ftpcmdbuf) != 0)
  997. {
  998. DEBUG_ERROR("Update CCS NG - FTP put CCS upgrade image to CCS board %d fail\n", TargetAddr);
  999. return FAIL;
  1000. }
  1001. else
  1002. {
  1003. DEBUG_INFO("FTP put %s to CCS board %d finish\n", SourcePath, TargetAddr);
  1004. return PASS;
  1005. }
  1006. }
  1007. int Send_CCS_download_finish(int canfd,unsigned int Slave_Addr)
  1008. {
  1009. if (canfd > 0)
  1010. {
  1011. struct can_frame frame;
  1012. frame.can_id = (CANBUS_MESSAGE_ID_UPGRADE_FINISH + Slave_Addr) | 0x80000000; //extended frame
  1013. frame.can_dlc = 0x00;
  1014. write(canfd, &frame, sizeof(struct can_frame));
  1015. usleep(10000);
  1016. struct timeval timer;
  1017. gettimeofday(&timer, NULL);
  1018. unsigned long ack_timeout = 30 * 60 * 1000 * 1000; //30 minutes
  1019. while (getTimeoutValue(timer) < ack_timeout)
  1020. {
  1021. struct can_frame frame;
  1022. int len;
  1023. len = read(canfd, &frame, sizeof(struct can_frame));
  1024. if(len >= 0)
  1025. {
  1026. if(((int)(frame.can_id & CAN_EFF_MASK) == (CANBUS_MESSAGE_ID_UPGRADE_FINISH | Slave_Addr | 0x08000000)) && frame.data[0] == 1)
  1027. {
  1028. return PASS;
  1029. }
  1030. }
  1031. }
  1032. DEBUG_ERROR("Wait for download finish ack from CCS %d timeout\n", Slave_Addr);
  1033. return FAIL;
  1034. }
  1035. else
  1036. {
  1037. DEBUG_ERROR("Send CCS download finish command fail, CAN fd is null\n");
  1038. return FAIL;
  1039. }
  1040. }
  1041. int Upgrade_CCS(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
  1042. {
  1043. if(Check_CCS_image_header(Type, SourcePath, ModelName) == FAIL)
  1044. {
  1045. return FAIL;
  1046. }
  1047. if(Put_CCS_image(SourcePath, TargetAddr) == FAIL)
  1048. {
  1049. return FAIL;
  1050. }
  1051. if(Send_CCS_download_finish(canfd, TargetAddr) == FAIL)
  1052. {
  1053. return FAIL;
  1054. }
  1055. DEBUG_INFO("Upgrade CCS board %d complete.\n", TargetAddr);
  1056. return PASS;
  1057. }