Module_Upgrade.h 29 KB

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