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