Module_Upgrade.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. long int MaxLen=48*1024*1024, ImageLen=0;
  587. unsigned int ImageCRC=0, DataLength=0;
  588. int fd;
  589. int uartfd = uart_config_timeout(uartfdOrd);
  590. fd = open(SourcePath, O_RDONLY);
  591. if(fd < 0)
  592. {
  593. DEBUG_ERROR("UpdateRootfs NG - can not open image file %s\n", SourcePath);
  594. return result;
  595. }
  596. unsigned char *ptr = malloc(MaxLen);
  597. memset(ptr,0xFF,MaxLen);
  598. //get the image length
  599. ImageLen = read(fd,ptr,MaxLen);
  600. close(fd);
  601. //read out the header
  602. int i;
  603. int isModelNameOK = PASS;
  604. for(i=0;i<16;i++)
  605. {
  606. if(ModelName[i] != ptr[i])
  607. {
  608. isModelNameOK = FAIL;
  609. }
  610. }
  611. if(isModelNameOK == FAIL)
  612. {
  613. DEBUG_ERROR("Model name mismatch...\n");
  614. }
  615. else
  616. {
  617. // check if the firmware type is correct
  618. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  619. {
  620. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  621. {
  622. DataLength = ImageLen-48;
  623. // get CRC in the header
  624. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  625. // calculate the image CRC
  626. DEBUG_INFO("CRC32 in image: 0x%08X\n",ImageCRC);
  627. DEBUG_INFO("CRC32 by calculation: 0x%08X\n",crc32(ptr,ImageLen));
  628. if(crc32(ptr,ImageLen) == ImageCRC)
  629. {
  630. if(YES)
  631. {
  632. int CNT_Fail = 0;
  633. int CNT_Trans = 0;
  634. do
  635. {
  636. if(uart_update_start(uartfd, TargetAddr, crc32(ptr+48,DataLength))==PASS)
  637. break;
  638. else
  639. DEBUG_WARN("Upgrade start fail, retry %d \n", ++CNT_Fail);
  640. }while(CNT_Fail<10);
  641. if(CNT_Fail>=10)
  642. {
  643. uart_update_abord(uartfd, TargetAddr);
  644. DEBUG_ERROR("UART upgrade start retry > limits, aboard upgrade.\n");
  645. }
  646. else
  647. {
  648. CNT_Fail = 0;
  649. do
  650. {
  651. if(uart_update_transfer(uartfd, TargetAddr, CNT_Trans*1024, ptr+48+(CNT_Trans*1024), 1024)==PASS)
  652. {
  653. CNT_Fail = 0;
  654. CNT_Trans++;
  655. DEBUG_INFO("Upgrade progress:%.2f%%\n", ((float)(CNT_Trans*1024))/(DataLength)*100);
  656. }
  657. else
  658. {
  659. DEBUG_WARN("Data transfer fail, retry %d \n", ++CNT_Fail);
  660. sleep(1);
  661. }
  662. }while(DataLength-(CNT_Trans*1024)>0 && CNT_Fail<10);
  663. if(CNT_Fail>=10)
  664. {
  665. uart_update_abord(uartfd, TargetAddr);
  666. DEBUG_ERROR("UART upgrade transfer retry > limits, aboard upgrade.\n");
  667. }
  668. else
  669. {
  670. CNT_Fail = 0;
  671. do
  672. {
  673. if(uart_update_finish(uartfd, TargetAddr)==PASS)
  674. break;
  675. else
  676. DEBUG_WARN("Upgrade finish fail, retry %d \n", ++CNT_Fail);
  677. }while(CNT_Fail<10);
  678. if(CNT_Fail>=10)
  679. {
  680. uart_update_abord(uartfd, TargetAddr);
  681. DEBUG_ERROR("UART upgrade finish retry > limits, aboard upgrade.\n");
  682. }
  683. else
  684. {
  685. result = PASS;
  686. printf("UART upgrade success.\n");
  687. }
  688. }
  689. }
  690. }
  691. else
  692. DEBUG_ERROR("UART upgrade request failed.\n");
  693. }
  694. else
  695. DEBUG_ERROR("Firmware image CRC32 mismatch.\n");
  696. }
  697. else
  698. DEBUG_ERROR("Firmware image length mismatch.\n");
  699. }
  700. else
  701. DEBUG_ERROR("Firmware image type mismatch.\n");
  702. }
  703. free(ptr);
  704. return result;
  705. }
  706. //================================================
  707. // CANBUS update function
  708. //================================================
  709. unsigned long getTimeoutValue(struct timeval _sour_time)
  710. {
  711. struct timeval _end_time;
  712. gettimeofday(&_end_time, NULL);
  713. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  714. }
  715. int CAN_Download_REQ(int canfd,unsigned int Slave_Addr, unsigned int imageSize)
  716. {
  717. struct can_frame frame;
  718. frame.can_id = (0x00000E00 + Slave_Addr) | 0x80000000; //extended frame
  719. frame.can_dlc = 0x07;
  720. 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
  721. frame.data[1] = (imageSize>>0)&0xff; //Total 384 KBytes
  722. frame.data[2] = (imageSize>>8)&0xff; //Total 384 KBytes
  723. frame.data[3] = (imageSize>>16)&0xff; //Total 384 KBytes
  724. frame.data[4] = (imageSize>>24)&0xff; //Total 384 KBytes
  725. frame.data[5] = 0x10; //16 blocks
  726. frame.data[6] = 0x18; //24 KBytes
  727. DEBUG_INFO( "File size = %x, %d \n", imageSize, imageSize);
  728. write(canfd, &frame, sizeof(struct can_frame));
  729. if (canfd > 0)
  730. {
  731. struct timeval timer;
  732. gettimeofday(&timer, NULL);
  733. while (getTimeoutValue(timer) < 5000000)
  734. {
  735. struct can_frame frame;
  736. int len;
  737. len = read(canfd, &frame, sizeof(struct can_frame));
  738. if (len >= 0)
  739. {
  740. DEBUG_INFO( "*****************************CAN_Download_REQ Get***************************** \n");
  741. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK);
  742. if (((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000E00) && frame.data[0] == 1)
  743. {
  744. DEBUG_INFO("PASS \n");
  745. return PASS;
  746. }
  747. }
  748. }
  749. }
  750. return FAIL;
  751. }
  752. int CAN_Start_BLK_Trans(int canfd,unsigned int Slave_Addr,unsigned int Block_No,unsigned int Block_Checksum)
  753. {
  754. struct can_frame frame;
  755. frame.can_id = (0x00000F00 + Slave_Addr) | 0x80000000; //extended frame
  756. frame.can_dlc = 0x02;
  757. frame.data[0] = Block_No;
  758. frame.data[1] = Block_Checksum;
  759. DEBUG_INFO("Block_No = %x, Block_Checksum = %x \n", Block_No, Block_Checksum);
  760. write(canfd, &frame, sizeof(struct can_frame));
  761. usleep(100000);
  762. if (canfd > 0)
  763. {
  764. struct timeval timer;
  765. gettimeofday(&timer, NULL);
  766. while (getTimeoutValue(timer) < 1000000)
  767. {
  768. struct can_frame frame;
  769. int len;
  770. len = read(canfd, &frame, sizeof(struct can_frame));
  771. if(len >= 0)
  772. {
  773. DEBUG_INFO("*****************************CAN_Start_BLK_Trans Get***************************** \n");
  774. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame CAN_EFF_MASK
  775. if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000F00) &&frame.data[0] == 1)
  776. {
  777. DEBUG_INFO("CAN_Start_BLK_Trans PASS \n");
  778. return PASS;
  779. }
  780. }
  781. }
  782. }
  783. return FAIL;
  784. }
  785. void CAN_Data_Trans(int canfd,unsigned int Slave_Addr,long Data_num,unsigned char Data[])
  786. {
  787. struct can_frame frame;
  788. frame.can_id = (0x00001000 + Slave_Addr) | 0x80000000; //extended frame
  789. frame.can_dlc = 0x08;
  790. frame.data[0] = Data[Data_num+0];
  791. frame.data[1] = Data[Data_num+1];
  792. frame.data[2] = Data[Data_num+2];
  793. frame.data[3] = Data[Data_num+3];
  794. frame.data[4] = Data[Data_num+4];
  795. frame.data[5] = Data[Data_num+5];
  796. frame.data[6] = Data[Data_num+6];
  797. frame.data[7] = Data[Data_num+7];
  798. // DEBUG_INFO("%02x %02x %02x %02x %02x %02x %02x %02x \n", frame.data[0], frame.data[1], frame.data[2], frame.data[3],
  799. // frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
  800. write(canfd, &frame, sizeof(struct can_frame));
  801. usleep(2000);
  802. }
  803. int CAN_Download_FIN(int canfd,unsigned int Slave_Addr)
  804. {
  805. struct can_frame frame;
  806. frame.can_id = (0x00001100 + Slave_Addr) | 0x80000000; //extended frame
  807. frame.can_dlc = 0x00;
  808. write(canfd, &frame, sizeof(struct can_frame));
  809. usleep(10000);
  810. if (canfd > 0)
  811. {
  812. struct timeval timer;
  813. gettimeofday(&timer, NULL);
  814. while (getTimeoutValue(timer) < 1000000)
  815. {
  816. struct can_frame frame;
  817. int len;
  818. len = read(canfd, &frame, sizeof(struct can_frame));
  819. if(len >= 0)
  820. {
  821. DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame
  822. if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08001100) && frame.data[0] == 1)
  823. {
  824. DEBUG_INFO("CAN_Download_FIN PASS \n");
  825. return PASS;
  826. }
  827. }
  828. }
  829. }
  830. return FAIL;
  831. }
  832. int Checksum_Cal(unsigned int StartAdress,unsigned int length, unsigned char Data[])
  833. {
  834. unsigned char checksum = 0x00;
  835. for(unsigned int i = 0; i < length; i++)
  836. {
  837. //DEBUG_INFO("value = %x \n", Data[StartAdress + i]);
  838. checksum ^= Data[StartAdress + i];
  839. //DEBUG_INFO("checksum = %x \n", checksum);
  840. }
  841. return checksum;
  842. }
  843. int Upgrade_CAN(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
  844. {
  845. int result = FAIL;
  846. long int MaxLen=48*1024*1024, ImageLen=0;
  847. unsigned int ImageCRC=0, DataLength=0;
  848. int fd;
  849. fd = open(SourcePath, O_RDONLY);
  850. if(fd < 0)
  851. {
  852. DEBUG_ERROR("UpdateRootfs NG - can not open image file %s\n", SourcePath);
  853. return result;
  854. }
  855. unsigned char *ptr = malloc(MaxLen);
  856. memset(ptr,0xFF,MaxLen);
  857. //get the image length
  858. ImageLen = read(fd,ptr,MaxLen);
  859. close(fd);
  860. //read out the header
  861. int i;
  862. int isModelNameOK = PASS;
  863. for(i=0;i<16;i++) {
  864. if(ModelName[i] != ptr[i]){
  865. isModelNameOK = FAIL;
  866. }
  867. }
  868. if(isModelNameOK == FAIL)
  869. {
  870. DEBUG_ERROR("Model name mismatch...\n");
  871. return result;
  872. }
  873. else
  874. {
  875. // check if the firmware type is correct
  876. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  877. {
  878. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  879. {
  880. DataLength = ImageLen-48;
  881. // get CRC in the header
  882. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  883. // calculate the image CRC
  884. DEBUG_INFO("CRC32 in image: 0x%08X\n",ImageCRC);
  885. DEBUG_INFO("CRC32 by calculation: 0x%08X\n",crc32(ptr,ImageLen));
  886. if(crc32(ptr,ImageLen) == ImageCRC)
  887. {
  888. unsigned int Checksum[16];
  889. for(int i=0;i<16;i++)
  890. {
  891. Checksum[i] = Checksum_Cal(i * 24576, 24576, ptr + 48);
  892. }
  893. if(CAN_Download_REQ(canfd, TargetAddr, DataLength) == PASS)
  894. {
  895. for(int block = 1; block <= 16; block++)
  896. {
  897. if(CAN_Start_BLK_Trans(canfd, TargetAddr, block, Checksum[block - 1]) == PASS)
  898. {
  899. for(int times = 0; times < 3072; times++)
  900. {
  901. CAN_Data_Trans(canfd, TargetAddr, ((block - 1) * 24576 + times * 8), ptr + 48);
  902. }
  903. DEBUG_INFO(" \n\n");
  904. }
  905. else
  906. {
  907. free(ptr);
  908. return result;
  909. }
  910. }
  911. if (CAN_Download_FIN(canfd, TargetAddr) == PASS)
  912. result = PASS;
  913. }
  914. else
  915. DEBUG_ERROR("CANBUS upgrade request failed.\n");
  916. }
  917. else
  918. DEBUG_ERROR("Firmware image CRC32 mismatch.\n");
  919. }
  920. else
  921. DEBUG_ERROR("Firmware image length mismatch.\n");
  922. }
  923. else
  924. DEBUG_ERROR("Firmware image type mismatch.\n");
  925. }
  926. free(ptr);
  927. return result;
  928. }
  929. //================================================
  930. // CCS update function
  931. //================================================
  932. int Check_CCS_image_header(unsigned int Type,char *SourcePath,char *ModelName)
  933. {
  934. int result = FAIL;
  935. long int MaxLen=48*1024*1024, ImageLen=0;
  936. unsigned int ImageCRC=0;
  937. int fd;
  938. // space max size set
  939. fd = open(SourcePath, O_RDONLY);
  940. if(fd < 0)
  941. {
  942. DEBUG_ERROR("Update CCS NG - can not open upgrade image %s\n", SourcePath);
  943. return FAIL;
  944. }
  945. switch(Type)
  946. {
  947. case CCS_BOARD_BOOTLOADER:
  948. MaxLen = 1*1024*1024;
  949. DEBUG_INFO("Prepare to upgrade CCS BOOTLOADER\n");
  950. break;
  951. case CCS_BOARD_KERNEL_CONFIGURATION:
  952. MaxLen = 0.5*1024*1024;
  953. DEBUG_INFO("Prepare to upgrade CCS KERNEL CONFIGURATION\n");
  954. break;
  955. case CCS_BOARD_KERNEL_IMAGE:
  956. MaxLen = 10*1024*1024;
  957. DEBUG_INFO("Prepare to upgrade CCS KERNEL\n");
  958. break;
  959. case CCS_BOARD_FILE_SYSTEM:
  960. MaxLen = 48*1024*1024;
  961. DEBUG_INFO("Prepare to upgrade CCS FILE SYSTEM\n");
  962. break;
  963. default:
  964. DEBUG_ERROR("Wrong image type for CCS upgrade\n");
  965. return FAIL;
  966. break;
  967. }
  968. unsigned char *ptr = malloc(MaxLen);
  969. memset(ptr,0xFF,MaxLen);
  970. //get the image length
  971. ImageLen = read(fd,ptr,MaxLen);
  972. close(fd);
  973. //read out the header
  974. int i;
  975. int isModelNameOK = PASS;
  976. for(i=0;i<16;i++)
  977. {
  978. if(ModelName[i] != ptr[i])
  979. {
  980. isModelNameOK = FAIL;
  981. }
  982. }
  983. if(isModelNameOK == FAIL)
  984. {
  985. DEBUG_ERROR("Model name mismatch.\n");
  986. }
  987. else
  988. {
  989. // check if the firmware type is correct
  990. if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
  991. {
  992. if((ImageLen-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
  993. {
  994. // get CRC in the header
  995. ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
  996. // calculate the image CRC
  997. DEBUG_INFO("CRC32 in CCS image: 0x%08X\n",ImageCRC);
  998. DEBUG_INFO("CRC32 by calculation: 0x%08X\n",crc32(ptr,ImageLen));
  999. if(crc32(ptr,ImageLen) == ImageCRC)
  1000. {
  1001. result = PASS;
  1002. }
  1003. else
  1004. {
  1005. DEBUG_ERROR("Firmware image CRC32 mismatch.\n");
  1006. }
  1007. }
  1008. else
  1009. {
  1010. DEBUG_ERROR("Firmware image length mismatch.\n");
  1011. }
  1012. }
  1013. else
  1014. {
  1015. DEBUG_ERROR("Firmware image type mismatch.\n");
  1016. }
  1017. }
  1018. free(ptr);
  1019. return result;
  1020. }
  1021. int Put_CCS_image(char *SourcePath, unsigned char TargetAddr)
  1022. {
  1023. unsigned char ftpcmdbuf[256];
  1024. unsigned char CCSIpAddress[16];
  1025. //If ID of target EV board is 1, the IP address will be 192.168.0.21,
  1026. //if ID of target EV board is 2, the IP address will be 192.168.0.22.
  1027. sprintf((char*)CCSIpAddress,"192.168.0.2%d", TargetAddr);
  1028. //Using ftpput command to transfer CCS upgrade image,
  1029. //User name : root
  1030. //User password : y42j/4cj84
  1031. //Destination : /root/ccs.image
  1032. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s /root/ccs.image %s",
  1033. CCSIpAddress, SourcePath);
  1034. if(system((char*)ftpcmdbuf) != 0)
  1035. {
  1036. DEBUG_ERROR("Update CCS NG - FTP put CCS upgrade image to CCS board %d fail\n", TargetAddr);
  1037. return FAIL;
  1038. }
  1039. else
  1040. {
  1041. DEBUG_INFO("FTP put %s to CCS board %d finish\n", SourcePath, TargetAddr);
  1042. return PASS;
  1043. }
  1044. }
  1045. int Send_CCS_download_finish(int canfd,unsigned int Slave_Addr)
  1046. {
  1047. if (canfd > 0)
  1048. {
  1049. struct can_frame frame;
  1050. frame.can_id = (CANBUS_MESSAGE_ID_UPGRADE_FINISH + Slave_Addr) | 0x80000000; //extended frame
  1051. frame.can_dlc = 0x00;
  1052. write(canfd, &frame, sizeof(struct can_frame));
  1053. usleep(10000);
  1054. struct timeval timer;
  1055. gettimeofday(&timer, NULL);
  1056. unsigned long ack_timeout = 5 * 60 * 1000 * 1000; //5 minutes
  1057. while (getTimeoutValue(timer) < ack_timeout)
  1058. {
  1059. struct can_frame frame;
  1060. int len;
  1061. len = read(canfd, &frame, sizeof(struct can_frame));
  1062. if(len >= 0)
  1063. {
  1064. if(((int)(frame.can_id & CAN_EFF_MASK) == (CANBUS_MESSAGE_ID_UPGRADE_FINISH | Slave_Addr | 0x08000000)) && frame.data[0] == 1)
  1065. {
  1066. return PASS;
  1067. }
  1068. }
  1069. }
  1070. DEBUG_ERROR("Wait for download finish ack from CCS %d timeout\n", Slave_Addr);
  1071. return FAIL;
  1072. }
  1073. else
  1074. {
  1075. DEBUG_ERROR("Send CCS download finish command fail, CAN fd is null\n");
  1076. return FAIL;
  1077. }
  1078. }
  1079. int Upgrade_CCS(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
  1080. {
  1081. if(Check_CCS_image_header(Type, SourcePath, ModelName) == FAIL)
  1082. {
  1083. return FAIL;
  1084. }
  1085. if(Put_CCS_image(SourcePath, TargetAddr) == FAIL)
  1086. {
  1087. return FAIL;
  1088. }
  1089. if(Send_CCS_download_finish(canfd, TargetAddr) == FAIL)
  1090. {
  1091. return FAIL;
  1092. }
  1093. DEBUG_INFO("Upgrade CCS board %d complete.\n", TargetAddr);
  1094. return PASS;
  1095. }