Module_Upgrade.c 40 KB

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