Module_Upgrade.c 40 KB

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