Module_Upgrade.c 38 KB

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