Module_Upgrade.c 37 KB

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