Module_Upgrade.c 38 KB

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