Module_Upgrade.c 38 KB

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