HSCDlg.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. // HSCDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HSC.h"
  5. #include "HSCDlg.h"
  6. #include "afxdialogex.h"
  7. #include <string.h>
  8. #include <string>
  9. #include <vector>
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #endif
  13. #pragma warning(disable:4996);
  14. #define MAXSIZE 512*1024
  15. // CHSCDlg dialog
  16. CString _I2T(int nValue)
  17. {
  18. CString szValue;
  19. char ch[20] = { 0 };
  20. itoa(nValue, ch, 10);
  21. szValue = ch;
  22. return szValue;
  23. }
  24. CString _I2T(unsigned int nValue)
  25. {
  26. CString szValue;
  27. char ch[20] = { 0 };
  28. itoa(nValue, ch, 10);
  29. szValue = ch;
  30. return szValue;
  31. }
  32. template<size_t _size>
  33. void get_current_time(char(&dest)[_size])
  34. {
  35. SYSTEMTIME tm = { 0 };
  36. GetLocalTime(&tm);
  37. sprintf_s(dest, "%d%.2d%.2d%.2d%.2d%.2d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
  38. }
  39. template<size_t _size>
  40. void get_current_time_2(char(&dest)[_size])
  41. {
  42. SYSTEMTIME tm = { 0 };
  43. GetLocalTime(&tm);
  44. sprintf_s(dest, "%d-%02d-%02d %02d:%02d:%02d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
  45. }
  46. CHSCDlg::CHSCDlg(CWnd* pParent /*=NULL*/)
  47. : CDialogEx(CHSCDlg::IDD, pParent)
  48. {
  49. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  50. }
  51. void CHSCDlg::DoDataExchange(CDataExchange* pDX)
  52. {
  53. CDialogEx::DoDataExchange(pDX);
  54. DDX_Control(pDX, IDC_LIST1, m_listctrl);
  55. DDX_Control(pDX, IDC_EDIT2, ED_CURNAME);
  56. DDX_Control(pDX, IDC_EDIT1, ED_CURVER);
  57. }
  58. BEGIN_MESSAGE_MAP(CHSCDlg, CDialogEx)
  59. ON_WM_PAINT()
  60. ON_WM_QUERYDRAGICON()
  61. ON_BN_CLICKED(IDC_BUTTON1, &CHSCDlg::ChooseA)
  62. ON_BN_CLICKED(IDC_BUTTON2, &CHSCDlg::ChooseB)
  63. ON_WM_DROPFILES()
  64. ON_BN_CLICKED(IDC_BUTTON3, &CHSCDlg::Generate)
  65. ON_BN_CLICKED(IDC_BUTTON4, &CHSCDlg::ChooseC)
  66. ON_NOTIFY(NM_CLICK, IDC_LIST1, &CHSCDlg::OnNMClickList1)
  67. ON_BN_CLICKED(IDC_UPDATE, &CHSCDlg::OnBnClickedUpdate)
  68. ON_BN_CLICKED(IDC_BUTTON6, &CHSCDlg::OnBnClickedButton6)
  69. END_MESSAGE_MAP()
  70. // CHSCDlg message handlers
  71. //单个字符异或运算
  72. char MakecodeChar(char c, int key){
  73. return c = c^key;
  74. }
  75. //单个字符解密
  76. char CutcodeChar(char c, int key){
  77. return c^key;
  78. }
  79. //加密
  80. void Makecode(char *pstr, int *pkey){
  81. int len = strlen(pstr);//获取长度
  82. for (int i = 0; i<len; i++)
  83. *(pstr + i) = MakecodeChar(*(pstr + i), pkey[i % 5]);
  84. }
  85. //解密
  86. void Cutecode(char *pstr, int *pkey){
  87. int len = strlen(pstr);
  88. for (int i = 0; i<len; i++)
  89. *(pstr + i) = CutcodeChar(*(pstr + i), pkey[i % 5]);
  90. }
  91. CString Makecode_String(CString str)
  92. {
  93. int key[] = { 1, 2, 3, 4, 5 };//加密字符
  94. char c[100] = { 0 };
  95. memcpy(c, str.GetString(), str.GetLength());
  96. Makecode(c, key);//加密
  97. CString strcode = c;
  98. return strcode;
  99. }
  100. void writehex(FILE* fw,CString str)
  101. {
  102. int len = str.GetLength();
  103. for (int i = 0; i < len;)
  104. {
  105. CString s = str.Mid(i, 2);
  106. char c[5];
  107. memcpy_s(c, 4, s.GetString(), 4);
  108. char d[5] = { 0 };
  109. sscanf_s(c, "%x", d);
  110. CString ds = CString(d);
  111. fwrite(d, 1, 1, fw);
  112. i += 2;
  113. }
  114. }
  115. void writehex_xor(FILE* fw, CString str,int or)
  116. {
  117. int len = str.GetLength();
  118. std::string s = "";// = str;
  119. for (int i = 0; i < len;i++)
  120. {
  121. char c = str.GetAt(i);
  122. c = or^ c;
  123. char c2[3];
  124. itoa(c, c2, 10);
  125. s += c2;
  126. }
  127. str = s.c_str();
  128. for (int i = 0; i < len;)
  129. {
  130. CString s = str.Mid(i, 2);
  131. char c[5];
  132. memcpy_s(c, 4, s.GetString(), 4);
  133. char d[5] = { 0 };
  134. sscanf_s(c, "%x", d);
  135. CString ds = CString(d);
  136. fwrite(d, 1, 1, fw);
  137. i += 2;
  138. }
  139. }
  140. void writehex_little_end(FILE* fw, CString str)
  141. {
  142. int len = str.GetLength();
  143. for (int i = len-2; i >= 0;)
  144. {
  145. CString s = str.Mid(i, 2);
  146. char c[5];
  147. memcpy_s(c, 4, s.GetString(), 4);
  148. char d[5] = { 0 };
  149. sscanf_s(c, "%x", d);
  150. CString ds = CString(d);
  151. fwrite(d, 1, 1, fw);
  152. i -= 2;
  153. }
  154. }
  155. void writestr(FILE* fw, CString str)
  156. {
  157. int len = str.GetLength();
  158. for (int i = 0; i < len;)
  159. {
  160. CString s = str.Mid(i, 1);
  161. char c[3];
  162. memcpy_s(c, 2, s.GetString(), 2);
  163. char d[3] = { 0 };
  164. sscanf_s(c, "%c", d);
  165. CString ds = CString(d);
  166. fwrite(d, 1, 1, fw);
  167. i += 1;
  168. }
  169. }
  170. CString Int2HexString(int n)
  171. {
  172. CString str = "";
  173. while (n / 256 > 0)
  174. {
  175. CString s = "";
  176. s.Format("%02x", n % 256);
  177. str = s + str;
  178. n = n / 256;
  179. }
  180. if (n % 256 != 0)
  181. {
  182. CString s = "";
  183. s.Format("%02x", n % 256);
  184. str = s + str;
  185. }
  186. return str;
  187. }
  188. CString Int2HexString(int n,int bytenum)
  189. {
  190. CString str = "";
  191. while (n / 256 > 0)
  192. {
  193. CString s = "";
  194. s.Format("%02x", n % 256);
  195. str = s + str;
  196. n = n / 256;
  197. }
  198. if (n % 256 != 0)
  199. {
  200. CString s = "";
  201. s.Format("%02x", n % 256);
  202. str = s + str;
  203. }
  204. int diff = bytenum - str.GetLength() / 2;
  205. while (diff > 0)
  206. {
  207. str = "00" + str;
  208. diff--;
  209. }
  210. return str;
  211. }
  212. CString get_computer_name()
  213. {
  214. char buffer[512];
  215. DWORD name_len = 512;
  216. CString name;
  217. GetComputerName((LPSTR)buffer, &name_len);
  218. name =buffer;
  219. return name;
  220. }
  221. CString get_user_name()
  222. {
  223. char buffer[512];
  224. DWORD name_len = 512;
  225. CString name;
  226. GetUserName((LPSTR)buffer, &name_len);
  227. name = buffer;
  228. return name;
  229. }
  230. BOOL CHSCDlg::OnInitDialog()
  231. {
  232. CDialogEx::OnInitDialog();
  233. // Set the icon for this dialog. The framework does this automatically
  234. // when the application's main window is not a dialog
  235. SetIcon(m_hIcon, TRUE); // Set big icon
  236. SetIcon(m_hIcon, FALSE); // Set small icon
  237. // TODO: Add extra initialization here
  238. //CString str = "20200115cs.bin";
  239. //BYTE tmp[500] = { 0 };
  240. //memcpy_s(tmp, str.GetLength(), str.GetString(), str.GetLength());
  241. //int len = str.GetLength();
  242. CString name = get_computer_name();
  243. CString user = get_user_name();
  244. GetDlgItem(USER_MAIN)->SetWindowText(user);
  245. m_username = user;
  246. DWORD style = m_listctrl.GetExtendedStyle();
  247. m_listctrl.SetExtendedStyle(style | LVS_EX_FLATSB | LVS_EX_FULLROWSELECT);
  248. m_listctrl.SetBkColor(RGB(255, 255, 255));//m_DetailLstCtrl.SetBkColor( RGB(87,91,92) );
  249. m_listctrl.SetTextBkColor(RGB(255, 255, 255));//m_DetailLstCtrl.SetTextBkColor( RGB(87,91,92) );
  250. int index = 0;
  251. m_listctrl.InsertColumn(index++, "名称", LVCFMT_LEFT, 150);
  252. m_listctrl.InsertColumn(index++, "固件版本号", LVCFMT_LEFT, 100);
  253. m_listctrl.InsertColumn(index++, "适配版本号", LVCFMT_LEFT, 100);
  254. m_listctrl.InsertColumn(index++, "生成时间", LVCFMT_LEFT, 120);
  255. m_listctrl.InsertColumn(index++, "长度", LVCFMT_LEFT, 100);
  256. m_listctrl.InsertColumn(index++, "crc", LVCFMT_LEFT, 100);
  257. m_listctrl.InsertColumn(index++, "起始地址", LVCFMT_LEFT, 100);
  258. return TRUE; // return TRUE unless you set the focus to a control
  259. }
  260. // If you add a minimize button to your dialog, you will need the code below
  261. // to draw the icon. For MFC applications using the document/view model,
  262. // this is automatically done for you by the framework.
  263. void CHSCDlg::OnPaint()
  264. {
  265. if (IsIconic())
  266. {
  267. CPaintDC dc(this); // device context for painting
  268. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  269. // Center icon in client rectangle
  270. int cxIcon = GetSystemMetrics(SM_CXICON);
  271. int cyIcon = GetSystemMetrics(SM_CYICON);
  272. CRect rect;
  273. GetClientRect(&rect);
  274. int x = (rect.Width() - cxIcon + 1) / 2;
  275. int y = (rect.Height() - cyIcon + 1) / 2;
  276. // Draw the icon
  277. dc.DrawIcon(x, y, m_hIcon);
  278. }
  279. else
  280. {
  281. CDialogEx::OnPaint();
  282. }
  283. }
  284. // The system calls this function to obtain the cursor to display while the user drags
  285. // the minimized window.
  286. HCURSOR CHSCDlg::OnQueryDragIcon()
  287. {
  288. return static_cast<HCURSOR>(m_hIcon);
  289. }
  290. char *strlowr(char *str)
  291. {
  292. char *orign = str;
  293. for (; *str != '\0'; str++)
  294. *str = tolower(*str);
  295. return orign;
  296. }
  297. void CHSCDlg::ChooseA()
  298. {
  299. CString strFile = _T("");
  300. CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);
  301. if (dlgFile.DoModal())
  302. {
  303. m_path_A = dlgFile.GetPathName();
  304. m_name_A = dlgFile.GetFileName();
  305. //if (m_name_A !="" && m_name_A == m_name_B)
  306. //{
  307. // AfxMessageBox("与另一固件同名,请自行检查!");
  308. //}
  309. char c[100] = { 0 };
  310. memcpy(c, m_name_A, m_name_A.GetLength());
  311. CString strlow = strlowr(c);
  312. if (strlow.Find("apps") == -1)
  313. {
  314. AfxMessageBox("请选择包含APPS字符串的文件名");
  315. return;
  316. }
  317. GetInfo_A();
  318. }
  319. }
  320. void CHSCDlg::ChooseB()
  321. {
  322. CString strFile = _T("");
  323. CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);
  324. if (dlgFile.DoModal())
  325. {
  326. m_path_B = dlgFile.GetPathName();
  327. m_name_B = dlgFile.GetFileName();
  328. //if (m_name_A != "" && m_name_A == m_name_B)
  329. //{
  330. // AfxMessageBox("与另一固件同名,请自行检查!");
  331. //}
  332. char c[100] = { 0 };
  333. memcpy(c, m_name_B, m_name_B.GetLength());
  334. CString strlow = strlowr(c);
  335. if (strlow.Find("uiic") == -1)
  336. {
  337. AfxMessageBox("请选择包含UIIC字符串的文件名!");
  338. return;
  339. }
  340. GetInfo_B();
  341. }
  342. }
  343. SYSTEMTIME OnTime64toSystemTime(__time64_t& itime)
  344. {
  345. struct tm *temptm = _localtime64(&itime);
  346. SYSTEMTIME st = { 1900 + temptm->tm_year,
  347. 1 + temptm->tm_mon,
  348. temptm->tm_wday,
  349. temptm->tm_mday,
  350. temptm->tm_hour,
  351. temptm->tm_min,
  352. temptm->tm_sec,
  353. 0 };
  354. return st;
  355. }
  356. void GetModifyDateTime(const wchar_t* strFilename, SYSTEMTIME& stLocal)
  357. {
  358. struct _stat64i32 statbuf;
  359. _wstat64i32(strFilename, &statbuf);
  360. stLocal = OnTime64toSystemTime(statbuf.st_mtime);
  361. }
  362. unsigned short crc16(unsigned char *pucData, long long nLen)
  363. {
  364. unsigned char CRC16Lo, CRC16Hi, CH, CL, SaveHi, SaveLo, j;
  365. long long i;
  366. CRC16Lo = 0;
  367. CRC16Hi = 0;
  368. CH = 0XA0;
  369. CL = 0X01;
  370. for (i = 0; i<nLen; i++) {
  371. CRC16Lo = CRC16Lo ^ (*(pucData + i));
  372. for (j = 0; j<8; j++) {
  373. SaveHi = CRC16Hi;
  374. SaveLo = CRC16Lo;
  375. CRC16Hi = CRC16Hi >> 1;
  376. CRC16Lo = CRC16Lo >> 1;
  377. if ((SaveHi & 0x01) == 0x01)
  378. CRC16Lo = CRC16Lo | 0x80;
  379. if ((SaveLo & 0x01) == 0x01) {
  380. CRC16Hi = CRC16Hi ^ CH;
  381. CRC16Lo = CRC16Lo ^ CL;
  382. }
  383. }
  384. }
  385. return CRC16Hi * 256 + CRC16Lo;
  386. }
  387. void CHSCDlg::GetInfo_A()
  388. {
  389. CString name = m_name_A;//dif
  390. CString path = m_path_A;//dif
  391. //version
  392. CString strversion;
  393. char c[100] = { 0 };
  394. memcpy(c, name, name.GetLength());
  395. CString strlow = strlowr(c);
  396. int pos = strlow.Find("_v");
  397. if (pos == -1)
  398. {
  399. AfxMessageBox("文件名格式不对!");
  400. return;
  401. }
  402. CString vname = name.Right(name.GetLength() - pos - 2);
  403. int pos2 = vname.Find(".");
  404. vname = vname.Left(pos2);
  405. if (vname.GetLength() < 8)
  406. {
  407. AfxMessageBox("文件名格式不对!");
  408. return;
  409. }
  410. int count = 1;
  411. strversion += CString(vname[0]) + CString(vname[1]);
  412. for (int i = 2; i < vname.GetLength() - 2; i++)
  413. {
  414. if (vname[i] == '_')
  415. {
  416. count++;
  417. strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
  418. }
  419. }
  420. if (count != 3 && count != 4)
  421. {
  422. AfxMessageBox("文件名版本号数目不对!");
  423. return;
  424. }
  425. GetDlgItem(NAME_A)->SetWindowText(name);//dif
  426. BYTE* v = new BYTE[MAXSIZE];
  427. int i = 0;
  428. //open
  429. FILE *fr;
  430. fopen_s(&fr, path, "rb");
  431. if (fr == NULL) //打开文件出错。
  432. {
  433. AfxMessageBox("打开固件失败!");
  434. return;
  435. }
  436. //size
  437. fseek(fr, 0, SEEK_END);
  438. long int size = ftell(fr);
  439. fseek(fr, 0, SEEK_SET);
  440. if (size >= MAXSIZE)
  441. {
  442. AfxMessageBox("文件太大!");
  443. return;
  444. }
  445. //scan
  446. while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  447. {
  448. i++;
  449. }
  450. fclose(fr);
  451. //crc
  452. UINT crc = crc16(v,size);
  453. //time
  454. CStringW namew;
  455. namew = path;
  456. SYSTEMTIME stLocal;
  457. GetModifyDateTime(namew, stLocal);
  458. char time[30] = { 0 };
  459. //get_current_time_2(time);
  460. sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
  461. stLocal.wYear, stLocal.wMonth, stLocal.wDay,
  462. stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
  463. //起始地址
  464. int addr = HEAD_LEN; //dif
  465. //showtext
  466. GetDlgItem(VERSION_A)->SetWindowText(strversion); //dif
  467. GetDlgItem(TIME_A)->SetWindowText(time); //dif
  468. GetDlgItem(LEN_A)->SetWindowText(_I2T(size)); //dif
  469. GetDlgItem(CRC_A)->SetWindowText("0x"+Int2HexString(crc)); //dif
  470. GetDlgItem(ADDR_A)->SetWindowText("0x" + Int2HexString(addr)); //dif
  471. m_version_A = strversion; //dif
  472. m_time_A = time; //dif
  473. m_len_A = _I2T(size); //dif
  474. m_crc_A = _I2T(crc); //dif
  475. m_addr_A = _I2T(addr); //dif
  476. m_nLen_A = size; //dif
  477. m_nCrc_A = crc; //dif
  478. m_nAddr_A = addr; //dif
  479. delete v;
  480. //A特有 在A改变后再次计算B和C的起始地址
  481. int addrb = HEAD_LEN + m_nLen_A; //dif
  482. GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addrb)); //dif
  483. m_addr_B = _I2T(addrb); //dif
  484. m_nAddr_B = addrb; //dif
  485. int addrc = HEAD_LEN + m_nLen_A+ m_nLen_B; //dif
  486. GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc)); //dif
  487. m_addr_C = _I2T(addrc); //dif
  488. m_nAddr_C = addrc; //dif
  489. }
  490. void CHSCDlg::GetInfo_B()
  491. {
  492. CString name = m_name_B;//dif
  493. CString path = m_path_B;//dif
  494. //version
  495. CString strversion;
  496. char c[100] = { 0 };
  497. memcpy(c, name, name.GetLength());
  498. CString strlow = strlowr(c);
  499. int pos = strlow.Find("_v");
  500. if (pos == -1)
  501. {
  502. AfxMessageBox("文件名格式不对!");
  503. return;
  504. }
  505. CString vname = name.Right(name.GetLength() - pos - 2);
  506. int pos2 = vname.Find(".");
  507. vname = vname.Left(pos2);
  508. if (vname.GetLength() < 8)
  509. {
  510. AfxMessageBox("文件名格式不对!");
  511. return;
  512. }
  513. int count = 1;
  514. strversion += CString(vname[0]) + CString(vname[1]);
  515. for (int i = 2; i < vname.GetLength() - 2; i++)
  516. {
  517. if (vname[i] == '_')
  518. {
  519. count++;
  520. strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
  521. }
  522. }
  523. if (count != 3 && count != 4)
  524. {
  525. AfxMessageBox("文件名版本号数目不对!");
  526. return;
  527. }
  528. GetDlgItem(NAME_B)->SetWindowText(name);//dif
  529. BYTE* v = new BYTE[MAXSIZE];
  530. int i = 0;
  531. //open
  532. FILE *fr;
  533. fopen_s(&fr, path, "rb");
  534. if (fr == NULL) //打开文件出错。
  535. {
  536. AfxMessageBox("打开固件失败!");
  537. return;
  538. }
  539. //size
  540. fseek(fr, 0, SEEK_END);
  541. long int size = ftell(fr);
  542. fseek(fr, 0, SEEK_SET);
  543. if (size >= MAXSIZE)
  544. {
  545. AfxMessageBox("文件太大!");
  546. return;
  547. }
  548. //scan
  549. while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  550. {
  551. i++;
  552. }
  553. fclose(fr);
  554. //crc
  555. UINT crc = crc16(v, size);
  556. //time
  557. CStringW namew;
  558. namew = path;
  559. SYSTEMTIME stLocal;
  560. GetModifyDateTime(namew, stLocal);
  561. char time[30] = { 0 };
  562. //get_current_time_2(time);
  563. sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
  564. stLocal.wYear, stLocal.wMonth, stLocal.wDay,
  565. stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
  566. //起始地址
  567. int addr = HEAD_LEN + m_nLen_A; //dif
  568. //showtext
  569. GetDlgItem(VERSION_B)->SetWindowText(strversion); //dif
  570. GetDlgItem(TIME_B)->SetWindowText(time); //dif
  571. GetDlgItem(LEN_B)->SetWindowText(_I2T(size)); //dif
  572. GetDlgItem(CRC_B)->SetWindowText("0x" + Int2HexString(crc)); //dif
  573. GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addr)); //dif
  574. m_version_B = strversion; //dif
  575. m_time_B = time; //dif
  576. m_len_B = _I2T(size); //dif
  577. m_crc_B = _I2T(crc); //dif
  578. m_addr_B = _I2T(addr); //dif
  579. m_nLen_B = size; //dif
  580. m_nCrc_B = crc; //dif
  581. m_nAddr_B = addr; //dif
  582. delete v;
  583. //B特有 在B改变后再次计算C的起始地址
  584. int addrc = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
  585. GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc)); //dif
  586. m_addr_C = _I2T(addrc); //dif
  587. m_nAddr_C = addrc; //dif
  588. }
  589. void CHSCDlg::GetInfo_C()
  590. {
  591. CString name = m_name_C;//dif
  592. CString path = m_path_C;//dif
  593. //version
  594. CString strversion;
  595. char c[100] = { 0 };
  596. memcpy(c, name, name.GetLength());
  597. CString strlow = strlowr(c);
  598. int pos = strlow.Find("_v");
  599. if (pos == -1)
  600. {
  601. AfxMessageBox("文件名格式不对!");
  602. return;
  603. }
  604. CString vname = name.Right(name.GetLength() - pos - 2);
  605. int pos2 = vname.Find(".");
  606. vname = vname.Left(pos2);
  607. if (vname.GetLength() < 8)
  608. {
  609. AfxMessageBox("文件名格式不对!");
  610. return;
  611. }
  612. int count = 1;
  613. strversion += CString(vname[0]) + CString(vname[1]);
  614. for (int i = 2; i < vname.GetLength() - 2; i++)
  615. {
  616. if (vname[i] == '_')
  617. {
  618. count++;
  619. strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
  620. }
  621. }
  622. if (count != 3 && count != 4)
  623. {
  624. AfxMessageBox("文件名版本号数目不对!");
  625. return;
  626. }
  627. GetDlgItem(NAME_C)->SetWindowText(name);//dif
  628. BYTE* v = new BYTE[MAXSIZE];
  629. int i = 0;
  630. //open
  631. FILE *fr;
  632. fopen_s(&fr, path, "rb");
  633. if (fr == NULL) //打开文件出错。
  634. {
  635. AfxMessageBox("打开固件失败!");
  636. return;
  637. }
  638. //size
  639. fseek(fr, 0, SEEK_END);
  640. long int size = ftell(fr);
  641. fseek(fr, 0, SEEK_SET);
  642. if (size >= MAXSIZE)
  643. {
  644. AfxMessageBox("文件太大!");
  645. return;
  646. }
  647. //scan
  648. while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  649. {
  650. i++;
  651. }
  652. fclose(fr);
  653. //crc
  654. UINT crc = crc16(v, size);
  655. //time
  656. CStringW namew;
  657. namew = path;
  658. SYSTEMTIME stLocal;
  659. GetModifyDateTime(namew, stLocal);
  660. char time[30] = { 0 };
  661. //get_current_time_2(time);
  662. sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
  663. stLocal.wYear, stLocal.wMonth, stLocal.wDay,
  664. stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
  665. //起始地址
  666. int addr = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
  667. //showtext
  668. GetDlgItem(VERSION_C)->SetWindowText(strversion); //dif
  669. GetDlgItem(TIME_C)->SetWindowText(time); //dif
  670. GetDlgItem(LEN_C)->SetWindowText(_I2T(size)); //dif
  671. GetDlgItem(CRC_C)->SetWindowText("0x" + Int2HexString(crc)); //dif
  672. GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addr)); //dif
  673. m_version_C = strversion; //dif
  674. m_time_C = time; //dif
  675. m_len_C = _I2T(size); //dif
  676. m_crc_C = _I2T(crc); //dif
  677. m_addr_C = _I2T(addr); //dif
  678. m_nLen_C = size; //dif
  679. m_nCrc_C = crc; //dif
  680. m_nAddr_C = addr; //dif
  681. delete v;
  682. }
  683. char *GetFilename(char *p)
  684. {
  685. int x = strlen(p);
  686. char ch = '\\';
  687. char *q = strrchr(p, ch) + 1;
  688. return q;
  689. }
  690. void CHSCDlg::OnDropFiles(HDROP hDropInfo)
  691. {
  692. UINT count;
  693. TCHAR filePath[MAX_PATH] = { 0 };
  694. count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);//从成功的拖放操作中检索文件的名称。并取代被拖拽文件的数目
  695. /*
  696. if (count == 1)//如果只拖拽一个文件夹
  697. {
  698. DragQueryFile(hDropInfo, 0, filePath, sizeof(filePath));//获得拖拽的文件名
  699. CString m_szPath = filePath;
  700. char ch[300] = { 0 };
  701. memcpy(ch, m_szPath, m_szPath.GetLength());
  702. CString name = GetFilename(ch);
  703. char c[100] = { 0 };
  704. memcpy(c, name, name.GetLength());
  705. CString strlow = strlowr(c);
  706. if (strlow.Find("apps") != -1)
  707. {
  708. m_path_A = m_szPath;
  709. m_name_A = name;
  710. GetInfo_A();
  711. }
  712. else if (strlow.Find("uiic") != -1)
  713. {
  714. m_path_B = m_szPath;
  715. m_name_B = name;
  716. GetInfo_B();
  717. }
  718. else if (strlow.Find("com") != -1)
  719. {
  720. m_path_C = m_szPath;
  721. m_name_C = name;
  722. GetInfo_C();
  723. }
  724. else
  725. {
  726. AfxMessageBox("没找到包含APPS、UIIC或COM字符串的文件!");
  727. }
  728. UpdateData(FALSE);
  729. DragFinish(hDropInfo);//拖放成功后,释放内存
  730. CDialog::OnDropFiles(hDropInfo);
  731. return;
  732. }
  733. else//如果拖拽多个文件夹
  734. */
  735. {
  736. //m_vectorFile.clear();
  737. int bfind = 0;
  738. for (UINT i = 0; i<count; i++)
  739. {
  740. int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
  741. CString m_szPath = filePath;
  742. m_szPath = filePath;
  743. char ch[300] = { 0 };
  744. memcpy(ch, m_szPath, m_szPath.GetLength());
  745. CString name = GetFilename(ch);
  746. char c[100] = { 0 };
  747. memcpy(c, name, name.GetLength());
  748. CString strlow = strlowr(c);
  749. HSCDATA data;
  750. if (GetInfo(name, m_szPath, data))
  751. {
  752. dataset.insertdata(data);
  753. }
  754. if (strlow.Find("apps") != -1)
  755. {
  756. m_path_A = m_szPath;
  757. m_name_A = name;
  758. GetInfo_A();
  759. bfind = 1;
  760. }
  761. else if (strlow.Find("uiic") != -1)
  762. {
  763. m_path_B = m_szPath;
  764. m_name_B = name;
  765. GetInfo_B();
  766. bfind = 1;
  767. }
  768. else if (strlow.Find("com") != -1)
  769. {
  770. m_path_C = m_szPath;
  771. m_name_C = name;
  772. GetInfo_C();
  773. bfind = 1;
  774. }
  775. //m_vectorFile.push_back(filePath);
  776. //break;
  777. }
  778. if (!bfind)
  779. {
  780. AfxMessageBox("没找到包含APPS、UIIC或COM字符串的文件!");
  781. }
  782. UpdateData(FALSE);
  783. DragFinish(hDropInfo);
  784. }
  785. updatelistctrl();
  786. CDialogEx::OnDropFiles(hDropInfo);
  787. }
  788. void CHSCDlg::Generate()
  789. {
  790. if (m_name_A == "" || m_name_B == "" || m_name_C == "")
  791. {
  792. AfxMessageBox("请选择三个固件!");
  793. return;
  794. }
  795. //1
  796. BYTE* v1 = new BYTE[MAXSIZE];
  797. int i = 0;
  798. FILE *fr1;
  799. fopen_s(&fr1, m_path_A, "rb");
  800. if (fr1 == NULL) //打开文件出错。
  801. {
  802. AfxMessageBox("打开固件失败!");
  803. return;
  804. }
  805. fseek(fr1, 0, SEEK_END);
  806. long int size = ftell(fr1);
  807. fseek(fr1, 0, SEEK_SET);
  808. if (size >= MAXSIZE)
  809. {
  810. AfxMessageBox("文件太大!");
  811. return;
  812. }
  813. while (fscanf(fr1, "%c", &v1[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  814. {
  815. i++;
  816. }
  817. int count1 = i;
  818. fclose(fr1);
  819. //2
  820. char* v2 = new char[MAXSIZE];
  821. i = 0;
  822. FILE *fr2;
  823. fopen_s(&fr2, m_path_B, "rb");
  824. if (fr2 == NULL) //打开文件出错。
  825. {
  826. AfxMessageBox("打开固件失败!");
  827. return;
  828. }
  829. fseek(fr2, 0, SEEK_END);
  830. size = ftell(fr2);
  831. fseek(fr2, 0, SEEK_SET);
  832. if (size >= MAXSIZE)
  833. {
  834. AfxMessageBox("文件太大!");
  835. return;
  836. }
  837. while (fscanf(fr2, "%c", &v2[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  838. {
  839. i++;
  840. }
  841. int count2 = i;
  842. fclose(fr2);
  843. //3
  844. char* v3 = new char[MAXSIZE];
  845. i = 0;
  846. FILE *fr3;
  847. fopen_s(&fr3, m_path_C, "rb");
  848. if (fr3 == NULL) //打开文件出错。
  849. {
  850. AfxMessageBox("打开固件失败!");
  851. return;
  852. }
  853. fseek(fr3, 0, SEEK_END);
  854. size = ftell(fr3);
  855. fseek(fr3, 0, SEEK_SET);
  856. if (size >= MAXSIZE)
  857. {
  858. AfxMessageBox("文件太大!");
  859. return;
  860. }
  861. while (fscanf(fr3, "%c", &v3[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  862. {
  863. i++;
  864. }
  865. int count3 = i;
  866. fclose(fr3);
  867. //
  868. CString strV;
  869. GetDlgItem(VERSION_MAIN)->GetWindowText(strV);
  870. if (strV.GetLength() != 8)
  871. {
  872. AfxMessageBox("请输入8位的主版本号!");
  873. return;
  874. }
  875. CString strVA;
  876. GetDlgItem(VERSION2_A)->GetWindowText(strVA);
  877. if (strVA.GetLength() != 8)
  878. {
  879. AfxMessageBox("请输入8位的主板固件版本号!");
  880. return;
  881. }
  882. CString strVB;
  883. GetDlgItem(VERSION2_B)->GetWindowText(strVB);
  884. if (strVB.GetLength() != 8)
  885. {
  886. AfxMessageBox("请输入8位的UI固件版本号!");
  887. return;
  888. }
  889. CString strVC;
  890. GetDlgItem(VERSION2_C)->GetWindowText(strVC);
  891. if (strVC.GetLength() != 8)
  892. {
  893. AfxMessageBox("请输入8位的COM固件版本号!");
  894. return;
  895. }
  896. CString firmname = "firmware_" + strV.Mid(0, 2) + "_" + strV.Mid(2, 2)
  897. + "_" + strV.Mid(4, 2) + "_" + strV.Mid(6, 2) + ".hsc";
  898. FILE *fini;
  899. fopen_s(&fini, "HSC.ini", "wb");
  900. fwrite(firmname, firmname.GetLength(),1, fini);
  901. fclose(fini);
  902. FILE *fw;
  903. fopen_s(&fw, firmname, "wb");
  904. //writehex(fw, "00012345");
  905. //writehex_little_end(fw, "00012345");
  906. //烧录文件主版本号
  907. writehex(fw, strV);
  908. //烧录文件生成时间
  909. char time[15];
  910. get_current_time(time);
  911. GetDlgItem(TIME_MAIN)->SetWindowText(time);
  912. CString strtime = "00" + CString(time);
  913. writehex(fw, strtime);
  914. //烧录文件生成软件所在电脑用户名
  915. CString struser = m_username;
  916. if (struser.GetLength() > 32)
  917. {
  918. struser = struser.Left(32);
  919. }
  920. int nullnum = 32 - struser.GetLength();
  921. for (auto i = 0; i < nullnum; i++)
  922. {
  923. writehex(fw, "00");
  924. }
  925. writestr(fw, struser);
  926. //固件A版本号
  927. if (m_version_A.GetLength() == 6)
  928. {
  929. writehex(fw, "00");
  930. }
  931. writehex(fw, m_version_A);
  932. //适配硬件A版本号
  933. writehex(fw, strVA);
  934. //固件A生成时间
  935. CString stime = m_time_A;
  936. CString strtimeA = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
  937. writehex(fw, strtimeA);
  938. //固件A长度
  939. CString strLenA = Int2HexString(m_nLen_A,4);
  940. writehex_little_end(fw, strLenA);
  941. //固件A的CRC16
  942. CString strCrcA = Int2HexString(m_nCrc_A,4);
  943. writehex_little_end(fw, strCrcA);
  944. //固件A在文件中的起始地址
  945. CString strAddrA = Int2HexString(m_nAddr_A,4);
  946. writehex_little_end(fw, strAddrA);
  947. //固件B版本号
  948. if (m_version_B.GetLength() == 6)
  949. {
  950. writehex(fw, "00");
  951. }
  952. writehex(fw, m_version_B);
  953. //适配硬件B版本号
  954. writehex(fw, strVB);
  955. //固件B生成时间
  956. stime = m_time_B;
  957. CString strtimeB = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
  958. writehex(fw, strtimeB);
  959. //固件B长度
  960. CString strLenB = Int2HexString(m_nLen_B, 4);
  961. writehex_little_end(fw, strLenB);
  962. //固件B的CRC16
  963. CString strCrcB = Int2HexString(m_nCrc_B, 4);
  964. writehex_little_end(fw, strCrcB);
  965. //固件B在文件中的起始地址
  966. CString strAddrB = Int2HexString(m_nAddr_B, 4);
  967. writehex_little_end(fw, strAddrB);
  968. //固件C版本号
  969. if (m_version_C.GetLength() == 6)
  970. {
  971. writehex(fw, "00");
  972. }
  973. writehex(fw, m_version_C);
  974. //适配硬件C版本号
  975. writehex(fw, strVC);
  976. //固件C生成时间
  977. stime = m_time_C;
  978. CString strtimeC = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
  979. writehex(fw, strtimeC);
  980. //固件C长度
  981. CString strLenC = Int2HexString(m_nLen_C, 4);
  982. writehex_little_end(fw, strLenC);
  983. //固件C的CRC16
  984. CString strCrcC = Int2HexString(m_nCrc_C, 4);
  985. writehex_little_end(fw, strCrcC);
  986. //固件C在文件中的起始地址
  987. CString strAddrC = Int2HexString(m_nAddr_C, 4);
  988. writehex_little_end(fw, strAddrC);
  989. /*固件的CRC16数据使用初始源文件计算结果,计算完CRC16后,
  990. 覆盖写入到对应固件起点偏移地址为0x1C的4个字节上*/
  991. int crc_beg = 0x1c;
  992. v1[crc_beg] = m_nCrc_A % 256;
  993. v1[crc_beg + 1] = m_nCrc_A / 256;
  994. v2[crc_beg] = m_nCrc_B % 256;
  995. v2[crc_beg + 1] = m_nCrc_B / 256;
  996. v3[crc_beg] = m_nCrc_C % 256;
  997. v3[crc_beg + 1] = m_nCrc_C / 256;
  998. /*固件加密方式是使用文件长度(取最低的一字节)对源文件数据进行异或运算,
  999. 对前200个字节依次进行异或,然后修改生成新的固件数据再放入烧录文件主体中。
  1000. 例如:文件长度为0x7452(29778bytes)。取0x52对源文件数据的前200字节依次进行异或运算,
  1001. 再覆盖到原来字节的位置上,生成的文件即是加密后的数据块*/
  1002. //加密处理后的固件A数据块
  1003. int lowbyte = count1 & 0xff;
  1004. for (int i = 0; i < 200 && i < count1; i++)
  1005. {
  1006. v1[i] = lowbyte ^ v1[i];
  1007. }
  1008. for (int i = 0; i < count1; i++)
  1009. {
  1010. fwrite(&v1[i], 1, 1, fw);
  1011. }
  1012. //加密处理后的固件B数据块
  1013. lowbyte = count2 & 0xff;
  1014. for (int i = 0; i < 200 && i < count2; i++)
  1015. {
  1016. v2[i] = lowbyte ^ v2[i];
  1017. }
  1018. for (int i = 0; i < count2; i++)
  1019. {
  1020. fwrite(&v2[i], 1, 1, fw);
  1021. }
  1022. //加密处理后的固件C数据块
  1023. lowbyte = count3 & 0xff;
  1024. for (int i = 0; i < 200 && i < count3; i++)
  1025. {
  1026. v3[i] = lowbyte ^ v3[i];
  1027. }
  1028. for (int i = 0; i < count3; i++)
  1029. {
  1030. fwrite(&v3[i], 1, 1, fw);
  1031. }
  1032. fclose(fw);
  1033. delete v1;
  1034. delete v2;
  1035. delete v3;
  1036. AfxMessageBox("生成文件成功!");
  1037. }
  1038. void CHSCDlg::ChooseC()
  1039. {
  1040. CString strFile = _T("");
  1041. CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);
  1042. if (dlgFile.DoModal())
  1043. {
  1044. m_path_C = dlgFile.GetPathName();
  1045. m_name_C = dlgFile.GetFileName();
  1046. //if (m_name_A != "" && m_name_A == m_name_B)
  1047. //{
  1048. // AfxMessageBox("与另一固件同名,请自行检查!");
  1049. //}
  1050. char c[100] = { 0 };
  1051. memcpy(c, m_name_C, m_name_C.GetLength());
  1052. CString strlow = strlowr(c);
  1053. if (strlow.Find("com") == -1)
  1054. {
  1055. AfxMessageBox("请选择包含COM字符串的文件名!");
  1056. return;
  1057. }
  1058. GetInfo_C();
  1059. }
  1060. }
  1061. int CHSCDlg::GetInfo(CString name, CString path, HSCDATA& data)
  1062. {
  1063. data.name = name;
  1064. data.path = path;
  1065. //version
  1066. CString strversion;
  1067. char c[100] = { 0 };
  1068. memcpy(c, name, name.GetLength());
  1069. CString strlow = strlowr(c);
  1070. int pos = strlow.Find("_v");
  1071. if (pos == -1)
  1072. {
  1073. AfxMessageBox("文件名格式不对!");
  1074. return 0;
  1075. }
  1076. CString vname = name.Right(name.GetLength() - pos - 2);
  1077. int pos2 = vname.Find(".");
  1078. vname = vname.Left(pos2);
  1079. if (vname.GetLength() < 8)
  1080. {
  1081. AfxMessageBox("文件名格式不对!");
  1082. return 0;
  1083. }
  1084. int count = 1;
  1085. strversion += CString(vname[0]) + CString(vname[1]);
  1086. for (int i = 2; i < vname.GetLength() - 2; i++)
  1087. {
  1088. if (vname[i] == '_')
  1089. {
  1090. count++;
  1091. strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
  1092. }
  1093. }
  1094. if (count != 3 && count != 4)
  1095. {
  1096. AfxMessageBox("文件名版本号数目不对!");
  1097. return 0;
  1098. }
  1099. //GetDlgItem(NAME_A)->SetWindowText(name);//dif
  1100. BYTE* v = new BYTE[MAXSIZE];
  1101. int i = 0;
  1102. //open
  1103. FILE *fr;
  1104. fopen_s(&fr, path, "rb");
  1105. if (fr == NULL) //打开文件出错。
  1106. {
  1107. AfxMessageBox("打开固件失败!");
  1108. return 0;
  1109. }
  1110. //size
  1111. fseek(fr, 0, SEEK_END);
  1112. long int size = ftell(fr);
  1113. fseek(fr, 0, SEEK_SET);
  1114. if (size >= MAXSIZE)
  1115. {
  1116. AfxMessageBox("文件太大!");
  1117. return 0;
  1118. }
  1119. //scan
  1120. while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  1121. {
  1122. i++;
  1123. }
  1124. fclose(fr);
  1125. //crc
  1126. UINT crc = crc16(v, size);
  1127. //time
  1128. CStringW namew;
  1129. namew = path;
  1130. SYSTEMTIME stLocal;
  1131. GetModifyDateTime(namew, stLocal);
  1132. char time[30] = { 0 };
  1133. //get_current_time_2(time);
  1134. sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
  1135. stLocal.wYear, stLocal.wMonth, stLocal.wDay,
  1136. stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
  1137. //起始地址
  1138. //int addr = dataset.getbeginaddr();
  1139. //showtext
  1140. //GetDlgItem(VERSION_A)->SetWindowText(strversion); //dif
  1141. //GetDlgItem(TIME_A)->SetWindowText(time); //dif
  1142. //GetDlgItem(LEN_A)->SetWindowText(_I2T(size)); //dif
  1143. //GetDlgItem(CRC_A)->SetWindowText("0x" + Int2HexString(crc)); //dif
  1144. //GetDlgItem(ADDR_A)->SetWindowText("0x" + Int2HexString(addr)); //dif
  1145. data.version = strversion;
  1146. data.time = time;
  1147. data.len = _I2T(size);
  1148. data.crc = _I2T(crc);
  1149. //data.addr = _I2T(addr);
  1150. data.nLen = size;
  1151. data.nCrc = crc;
  1152. //data.nAddr = addr;
  1153. delete v;
  1154. return 1;
  1155. ////A特有 在A改变后再次计算B和C的起始地址
  1156. //int addrb = HEAD_LEN + m_nLen_A; //dif
  1157. //GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addrb)); //dif
  1158. //m_addr_B = _I2T(addrb); //dif
  1159. //m_nAddr_B = addrb; //dif
  1160. //int addrc = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
  1161. //GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc)); //dif
  1162. //m_addr_C = _I2T(addrc); //dif
  1163. //m_nAddr_C = addrc; //dif
  1164. }
  1165. void CHSCDlg::updatelistctrl()
  1166. {
  1167. m_listctrl.DeleteAllItems();
  1168. int index = 0;
  1169. for (auto data:dataset.getdatas())
  1170. {
  1171. m_listctrl.InsertItem(index, _T(""));
  1172. m_listctrl.SetItemText(index, 0, data.name);
  1173. m_listctrl.SetItemText(index, 1, data.version);
  1174. m_listctrl.SetItemText(index, 2, data.version_add);
  1175. m_listctrl.SetItemText(index, 3, data.time);
  1176. m_listctrl.SetItemText(index, 4, data.len);
  1177. m_listctrl.SetItemText(index, 5, "0x" + Int2HexString(data.nCrc));
  1178. m_listctrl.SetItemText(index, 6, "0x" + Int2HexString(data.nAddr));
  1179. index++;
  1180. }
  1181. }
  1182. int g_cursel = -1;
  1183. void CHSCDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
  1184. {
  1185. LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
  1186. // TODO: Add your control notification handler code here
  1187. CString str;
  1188. g_cursel = m_listctrl.GetSelectionMark();
  1189. if (g_cursel >= 0 && g_cursel < dataset.getcount())
  1190. {
  1191. HSCDATA data = dataset.m_datas[g_cursel];
  1192. ED_CURNAME.SetWindowText(data.name);
  1193. ED_CURVER.SetWindowText(data.version_add);
  1194. }
  1195. *pResult = 0;
  1196. }
  1197. void CHSCDlg::OnBnClickedUpdate()
  1198. {
  1199. if (g_cursel >= 0 && g_cursel < dataset.getcount())
  1200. {
  1201. CString strV;
  1202. ED_CURVER.GetWindowText(strV);
  1203. if (strV.GetLength() != 8)
  1204. {
  1205. AfxMessageBox("请输入8位的固件版本号!");
  1206. return;
  1207. }
  1208. dataset.m_datas[g_cursel].version_add = strV;
  1209. updatelistctrl();
  1210. }
  1211. else
  1212. {
  1213. AfxMessageBox("没选中!");
  1214. }
  1215. }
  1216. void CHSCDlg::OnBnClickedButton6()
  1217. {
  1218. for (auto data : dataset.m_datas)
  1219. {
  1220. if (data.version_add.GetLength() != 8)
  1221. {
  1222. AfxMessageBox("请输入8位的固件版本号!");
  1223. return;
  1224. }
  1225. }
  1226. //
  1227. CString strV;
  1228. GetDlgItem(VERSION_MAIN)->GetWindowText(strV);
  1229. if (strV.GetLength() != 8)
  1230. {
  1231. AfxMessageBox("请输入8位的主版本号!");
  1232. return;
  1233. }
  1234. CString firmname = "firmware_" + strV.Mid(0, 2) + "_" + strV.Mid(2, 2)
  1235. + "_" + strV.Mid(4, 2) + "_" + strV.Mid(6, 2) + ".hsc";
  1236. FILE *fini;
  1237. fopen_s(&fini, "HSC.ini", "wb");
  1238. fwrite(firmname, firmname.GetLength(), 1, fini);
  1239. fclose(fini);
  1240. FILE *fw;
  1241. fopen_s(&fw, firmname, "wb");
  1242. //烧录文件主版本号
  1243. writehex(fw, strV);
  1244. //烧录文件生成时间
  1245. char time[15];
  1246. get_current_time(time);
  1247. GetDlgItem(TIME_MAIN)->SetWindowText(time);
  1248. CString strtime = "00" + CString(time);
  1249. writehex(fw, strtime);
  1250. //烧录文件生成软件所在电脑用户名
  1251. CString struser = m_username;
  1252. if (struser.GetLength() > 32)
  1253. {
  1254. struser = struser.Left(32);
  1255. }
  1256. int nullnum = 32 - struser.GetLength();
  1257. for (auto i = 0; i < nullnum; i++)
  1258. {
  1259. writehex(fw, "00");
  1260. }
  1261. writestr(fw, struser);
  1262. //算新crc
  1263. for (auto &data : dataset.m_datas)
  1264. {
  1265. BYTE* v1 = new BYTE[MAXSIZE];
  1266. int i = 0;
  1267. FILE *fr1;
  1268. fopen_s(&fr1, data.path, "rb");
  1269. if (fr1 == NULL) //打开文件出错。
  1270. {
  1271. AfxMessageBox("打开固件失败!");
  1272. return;
  1273. }
  1274. fseek(fr1, 0, SEEK_END);
  1275. long int size = ftell(fr1);
  1276. fseek(fr1, 0, SEEK_SET);
  1277. if (size >= MAXSIZE)
  1278. {
  1279. AfxMessageBox("文件太大!");
  1280. return;
  1281. }
  1282. while (fscanf(fr1, "%c", &v1[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  1283. {
  1284. i++;
  1285. }
  1286. data.count = i;
  1287. fclose(fr1);
  1288. int crc_beg = 0x1c;
  1289. v1[crc_beg] = data.nCrc % 256;
  1290. v1[crc_beg + 1] = data.nCrc / 256;
  1291. data.newcrc = crc16(v1, size);
  1292. delete v1;
  1293. }
  1294. for (auto data : dataset.m_datas)
  1295. {
  1296. //固件版本号
  1297. if (data.version.GetLength() == 6)
  1298. {
  1299. writehex(fw, "00");
  1300. }
  1301. writehex(fw, data.version);
  1302. //适配硬件版本号
  1303. writehex(fw, data.version_add);
  1304. //固件生成时间
  1305. CString stime = data.time;
  1306. CString strtimeA = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
  1307. writehex(fw, strtimeA);
  1308. //固件长度
  1309. CString strLenA = Int2HexString(data.nLen, 4);
  1310. writehex_little_end(fw, strLenA);
  1311. //固件的CRC16
  1312. //CString strCrcA = Int2HexString(data.nCrc, 4);
  1313. CString strCrcA = Int2HexString(data.newcrc, 4);//新crc
  1314. writehex_little_end(fw, strCrcA);
  1315. //固件在文件中的起始地址
  1316. CString strAddrA = Int2HexString(data.nAddr, 4);
  1317. writehex_little_end(fw, strAddrA);
  1318. }
  1319. for (auto data : dataset.m_datas)
  1320. {
  1321. BYTE* v1 = new BYTE[MAXSIZE];
  1322. int i = 0;
  1323. FILE *fr1;
  1324. fopen_s(&fr1, data.path, "rb");
  1325. if (fr1 == NULL) //打开文件出错。
  1326. {
  1327. AfxMessageBox("打开固件失败!");
  1328. return;
  1329. }
  1330. fseek(fr1, 0, SEEK_END);
  1331. long int size = ftell(fr1);
  1332. fseek(fr1, 0, SEEK_SET);
  1333. if (size >= MAXSIZE)
  1334. {
  1335. AfxMessageBox("文件太大!");
  1336. return;
  1337. }
  1338. while (fscanf(fr1, "%c", &v1[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
  1339. {
  1340. i++;
  1341. }
  1342. data.count = i;
  1343. fclose(fr1);
  1344. int crc_beg = 0x1c;
  1345. v1[crc_beg] = data.nCrc % 256;
  1346. v1[crc_beg + 1] = data.nCrc / 256;
  1347. int lowbyte = data.count & 0xff;
  1348. for (int i = 0; i < 200 && i < data.count; i++)
  1349. {
  1350. v1[i] = lowbyte ^ v1[i];
  1351. }
  1352. //
  1353. UINT newcrc = crc16(v1, size);
  1354. for (int i = 0; i < data.count; i++)
  1355. {
  1356. fwrite(&v1[i], 1, 1, fw);
  1357. }
  1358. delete v1;
  1359. }
  1360. fclose(fw);
  1361. AfxMessageBox("生成文件成功!");
  1362. }