Module_Upgrade.c 38 KB

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