Module_Upgrade.c 40 KB

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