Module_Upgrade.c 42 KB

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