Module_Upgrade.c 40 KB

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