Module_Upgrade.c 38 KB

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