12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901 |
- // HSCDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "HSC.h"
- #include "HSCDlg.h"
- #include "afxdialogex.h"
- #include <string.h>
- #include <string>
- #include <vector>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #pragma warning(disable:4996);
- #define MAXSIZE 512*1024
- // CHSCDlg dialog
- CString _I2T(int nValue)
- {
- CString szValue;
- char ch[20] = { 0 };
- itoa(nValue, ch, 10);
- szValue = ch;
- return szValue;
- }
- CString _I2T(unsigned int nValue)
- {
- CString szValue;
- char ch[20] = { 0 };
- itoa(nValue, ch, 10);
- szValue = ch;
- return szValue;
- }
- template<size_t _size>
- void get_current_time(char(&dest)[_size])
- {
- SYSTEMTIME tm = { 0 };
- GetLocalTime(&tm);
- sprintf_s(dest, "%d%.2d%.2d%.2d%.2d%.2d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
- }
- template<size_t _size>
- void get_current_time_2(char(&dest)[_size])
- {
- SYSTEMTIME tm = { 0 };
- GetLocalTime(&tm);
- sprintf_s(dest, "%d-%02d-%02d %02d:%02d:%02d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
- }
- CHSCDlg::CHSCDlg(CWnd* pParent /*=NULL*/)
- : CDialogEx(CHSCDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CHSCDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_LIST1, m_listctrl);
- DDX_Control(pDX, IDC_EDIT2, ED_CURNAME);
- DDX_Control(pDX, IDC_EDIT1, ED_CURVER);
- }
- BEGIN_MESSAGE_MAP(CHSCDlg, CDialogEx)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, &CHSCDlg::ChooseA)
- ON_BN_CLICKED(IDC_BUTTON2, &CHSCDlg::ChooseB)
- ON_WM_DROPFILES()
- ON_BN_CLICKED(IDC_BUTTON3, &CHSCDlg::Generate)
- ON_BN_CLICKED(IDC_BUTTON4, &CHSCDlg::ChooseC)
- ON_NOTIFY(NM_CLICK, IDC_LIST1, &CHSCDlg::OnNMClickList1)
- ON_BN_CLICKED(IDC_UPDATE, &CHSCDlg::OnBnClickedUpdate)
- ON_BN_CLICKED(IDC_BUTTON6, &CHSCDlg::OnBnClickedButton6)
- END_MESSAGE_MAP()
- // CHSCDlg message handlers
- //单个字符异或运算
- char MakecodeChar(char c, int key){
- return c = c^key;
- }
- //单个字符解密
- char CutcodeChar(char c, int key){
- return c^key;
- }
- //加密
- void Makecode(char *pstr, int *pkey){
- int len = strlen(pstr);//获取长度
- for (int i = 0; i<len; i++)
- *(pstr + i) = MakecodeChar(*(pstr + i), pkey[i % 5]);
- }
- //解密
- void Cutecode(char *pstr, int *pkey){
- int len = strlen(pstr);
- for (int i = 0; i<len; i++)
- *(pstr + i) = CutcodeChar(*(pstr + i), pkey[i % 5]);
- }
- CString Makecode_String(CString str)
- {
- int key[] = { 1, 2, 3, 4, 5 };//加密字符
- char c[100] = { 0 };
- memcpy(c, str.GetString(), str.GetLength());
- Makecode(c, key);//加密
- CString strcode = c;
- return strcode;
- }
- void writehex(FILE* fw,CString str)
- {
- int len = str.GetLength();
- for (int i = 0; i < len;)
- {
- CString s = str.Mid(i, 2);
- char c[5];
- memcpy_s(c, 4, s.GetString(), 4);
- char d[5] = { 0 };
- sscanf_s(c, "%x", d);
- CString ds = CString(d);
- fwrite(d, 1, 1, fw);
- i += 2;
- }
- }
- void writehex_xor(FILE* fw, CString str,int or)
- {
- int len = str.GetLength();
- std::string s = "";// = str;
- for (int i = 0; i < len;i++)
- {
- char c = str.GetAt(i);
- c = or^ c;
- char c2[3];
- itoa(c, c2, 10);
- s += c2;
- }
- str = s.c_str();
- for (int i = 0; i < len;)
- {
- CString s = str.Mid(i, 2);
- char c[5];
- memcpy_s(c, 4, s.GetString(), 4);
- char d[5] = { 0 };
- sscanf_s(c, "%x", d);
- CString ds = CString(d);
-
- fwrite(d, 1, 1, fw);
- i += 2;
- }
- }
- void writehex_little_end(FILE* fw, CString str)
- {
- int len = str.GetLength();
- for (int i = len-2; i >= 0;)
- {
- CString s = str.Mid(i, 2);
- char c[5];
- memcpy_s(c, 4, s.GetString(), 4);
- char d[5] = { 0 };
- sscanf_s(c, "%x", d);
- CString ds = CString(d);
- fwrite(d, 1, 1, fw);
- i -= 2;
- }
- }
- void writestr(FILE* fw, CString str)
- {
- int len = str.GetLength();
- for (int i = 0; i < len;)
- {
- CString s = str.Mid(i, 1);
- char c[3];
- memcpy_s(c, 2, s.GetString(), 2);
- char d[3] = { 0 };
- sscanf_s(c, "%c", d);
- CString ds = CString(d);
- fwrite(d, 1, 1, fw);
- i += 1;
- }
- }
- CString Int2HexString(int n)
- {
- CString str = "";
- while (n / 256 > 0)
- {
- CString s = "";
- s.Format("%02x", n % 256);
- str = s + str;
- n = n / 256;
- }
- if (n % 256 != 0)
- {
- CString s = "";
- s.Format("%02x", n % 256);
- str = s + str;
- }
- return str;
- }
- CString Int2HexString(int n,int bytenum)
- {
- CString str = "";
- while (n / 256 > 0)
- {
- CString s = "";
- s.Format("%02x", n % 256);
- str = s + str;
- n = n / 256;
- }
- if (n % 256 != 0)
- {
- CString s = "";
- s.Format("%02x", n % 256);
- str = s + str;
- }
- int diff = bytenum - str.GetLength() / 2;
- while (diff > 0)
- {
- str = "00" + str;
- diff--;
- }
- return str;
- }
- CString get_computer_name()
- {
- char buffer[512];
- DWORD name_len = 512;
- CString name;
- GetComputerName((LPSTR)buffer, &name_len);
- name =buffer;
- return name;
- }
- CString get_user_name()
- {
- char buffer[512];
- DWORD name_len = 512;
- CString name;
- GetUserName((LPSTR)buffer, &name_len);
- name = buffer;
- return name;
- }
- BOOL CHSCDlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- //CString str = "20200115cs.bin";
- //BYTE tmp[500] = { 0 };
- //memcpy_s(tmp, str.GetLength(), str.GetString(), str.GetLength());
- //int len = str.GetLength();
- CString name = get_computer_name();
- CString user = get_user_name();
- GetDlgItem(USER_MAIN)->SetWindowText(user);
- m_username = user;
- DWORD style = m_listctrl.GetExtendedStyle();
- m_listctrl.SetExtendedStyle(style | LVS_EX_FLATSB | LVS_EX_FULLROWSELECT);
-
- m_listctrl.SetBkColor(RGB(255, 255, 255));//m_DetailLstCtrl.SetBkColor( RGB(87,91,92) );
- m_listctrl.SetTextBkColor(RGB(255, 255, 255));//m_DetailLstCtrl.SetTextBkColor( RGB(87,91,92) );
- int index = 0;
- m_listctrl.InsertColumn(index++, "名称", LVCFMT_LEFT, 150);
- m_listctrl.InsertColumn(index++, "固件版本号", LVCFMT_LEFT, 100);
- m_listctrl.InsertColumn(index++, "适配版本号", LVCFMT_LEFT, 100);
- m_listctrl.InsertColumn(index++, "生成时间", LVCFMT_LEFT, 120);
- m_listctrl.InsertColumn(index++, "长度", LVCFMT_LEFT, 100);
- m_listctrl.InsertColumn(index++, "crc", LVCFMT_LEFT, 100);
- m_listctrl.InsertColumn(index++, "起始地址", LVCFMT_LEFT, 100);
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CHSCDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialogEx::OnPaint();
- }
- }
- // The system calls this function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CHSCDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- char *strlowr(char *str)
- {
- char *orign = str;
- for (; *str != '\0'; str++)
- *str = tolower(*str);
- return orign;
- }
- void CHSCDlg::ChooseA()
- {
-
- CString strFile = _T("");
- CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);
- if (dlgFile.DoModal())
- {
- m_path_A = dlgFile.GetPathName();
- m_name_A = dlgFile.GetFileName();
-
- //if (m_name_A !="" && m_name_A == m_name_B)
- //{
- // AfxMessageBox("与另一固件同名,请自行检查!");
- //}
- char c[100] = { 0 };
- memcpy(c, m_name_A, m_name_A.GetLength());
- CString strlow = strlowr(c);
- if (strlow.Find("apps") == -1)
- {
- AfxMessageBox("请选择包含APPS字符串的文件名");
- return;
- }
- GetInfo_A();
-
- }
- }
- void CHSCDlg::ChooseB()
- {
- CString strFile = _T("");
- CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);
- if (dlgFile.DoModal())
- {
- m_path_B = dlgFile.GetPathName();
- m_name_B = dlgFile.GetFileName();
- //if (m_name_A != "" && m_name_A == m_name_B)
- //{
- // AfxMessageBox("与另一固件同名,请自行检查!");
- //}
- char c[100] = { 0 };
- memcpy(c, m_name_B, m_name_B.GetLength());
- CString strlow = strlowr(c);
- if (strlow.Find("uiic") == -1)
- {
- AfxMessageBox("请选择包含UIIC字符串的文件名!");
- return;
- }
- GetInfo_B();
- }
- }
- SYSTEMTIME OnTime64toSystemTime(__time64_t& itime)
- {
- struct tm *temptm = _localtime64(&itime);
- SYSTEMTIME st = { 1900 + temptm->tm_year,
- 1 + temptm->tm_mon,
- temptm->tm_wday,
- temptm->tm_mday,
- temptm->tm_hour,
- temptm->tm_min,
- temptm->tm_sec,
- 0 };
- return st;
- }
- void GetModifyDateTime(const wchar_t* strFilename, SYSTEMTIME& stLocal)
- {
- struct _stat64i32 statbuf;
- _wstat64i32(strFilename, &statbuf);
- stLocal = OnTime64toSystemTime(statbuf.st_mtime);
- }
- unsigned short crc16(unsigned char *pucData, long long nLen)
- {
- unsigned char CRC16Lo, CRC16Hi, CH, CL, SaveHi, SaveLo, j;
- long long i;
- CRC16Lo = 0;
- CRC16Hi = 0;
- CH = 0XA0;
- CL = 0X01;
- for (i = 0; i<nLen; i++) {
- CRC16Lo = CRC16Lo ^ (*(pucData + i));
- for (j = 0; j<8; j++) {
- SaveHi = CRC16Hi;
- SaveLo = CRC16Lo;
- CRC16Hi = CRC16Hi >> 1;
- CRC16Lo = CRC16Lo >> 1;
- if ((SaveHi & 0x01) == 0x01)
- CRC16Lo = CRC16Lo | 0x80;
- if ((SaveLo & 0x01) == 0x01) {
- CRC16Hi = CRC16Hi ^ CH;
- CRC16Lo = CRC16Lo ^ CL;
- }
- }
- }
- return CRC16Hi * 256 + CRC16Lo;
- }
- void CHSCDlg::GetInfo_A()
- {
- CString name = m_name_A;//dif
- CString path = m_path_A;//dif
-
- //version
- CString strversion;
- char c[100] = { 0 };
- memcpy(c, name, name.GetLength());
- CString strlow = strlowr(c);
- int pos = strlow.Find("_v");
- if (pos == -1)
- {
- AfxMessageBox("文件名格式不对!");
- return;
- }
- CString vname = name.Right(name.GetLength() - pos - 2);
- int pos2 = vname.Find(".");
- vname = vname.Left(pos2);
- if (vname.GetLength() < 8)
- {
- AfxMessageBox("文件名格式不对!");
- return;
- }
- int count = 1;
- strversion += CString(vname[0]) + CString(vname[1]);
- for (int i = 2; i < vname.GetLength() - 2; i++)
- {
- if (vname[i] == '_')
- {
- count++;
- strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
- }
- }
- if (count != 3 && count != 4)
- {
- AfxMessageBox("文件名版本号数目不对!");
- return;
- }
- GetDlgItem(NAME_A)->SetWindowText(name);//dif
- BYTE* v = new BYTE[MAXSIZE];
- int i = 0;
- //open
- FILE *fr;
- fopen_s(&fr, path, "rb");
- if (fr == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- //size
- fseek(fr, 0, SEEK_END);
- long int size = ftell(fr);
- fseek(fr, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- //scan
- while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- fclose(fr);
- //crc
- UINT crc = crc16(v,size);
-
-
- //time
- CStringW namew;
- namew = path;
- SYSTEMTIME stLocal;
- GetModifyDateTime(namew, stLocal);
- char time[30] = { 0 };
- //get_current_time_2(time);
- sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
- stLocal.wYear, stLocal.wMonth, stLocal.wDay,
- stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
- //起始地址
- int addr = HEAD_LEN; //dif
- //showtext
- GetDlgItem(VERSION_A)->SetWindowText(strversion); //dif
- GetDlgItem(TIME_A)->SetWindowText(time); //dif
- GetDlgItem(LEN_A)->SetWindowText(_I2T(size)); //dif
- GetDlgItem(CRC_A)->SetWindowText("0x"+Int2HexString(crc)); //dif
- GetDlgItem(ADDR_A)->SetWindowText("0x" + Int2HexString(addr)); //dif
- m_version_A = strversion; //dif
- m_time_A = time; //dif
- m_len_A = _I2T(size); //dif
- m_crc_A = _I2T(crc); //dif
- m_addr_A = _I2T(addr); //dif
- m_nLen_A = size; //dif
- m_nCrc_A = crc; //dif
- m_nAddr_A = addr; //dif
- delete v;
- //A特有 在A改变后再次计算B和C的起始地址
- int addrb = HEAD_LEN + m_nLen_A; //dif
- GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addrb)); //dif
- m_addr_B = _I2T(addrb); //dif
- m_nAddr_B = addrb; //dif
- int addrc = HEAD_LEN + m_nLen_A+ m_nLen_B; //dif
- GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc)); //dif
- m_addr_C = _I2T(addrc); //dif
- m_nAddr_C = addrc; //dif
- }
- void CHSCDlg::GetInfo_B()
- {
- CString name = m_name_B;//dif
- CString path = m_path_B;//dif
- //version
- CString strversion;
- char c[100] = { 0 };
- memcpy(c, name, name.GetLength());
- CString strlow = strlowr(c);
- int pos = strlow.Find("_v");
- if (pos == -1)
- {
- AfxMessageBox("文件名格式不对!");
- return;
- }
- CString vname = name.Right(name.GetLength() - pos - 2);
- int pos2 = vname.Find(".");
- vname = vname.Left(pos2);
- if (vname.GetLength() < 8)
- {
- AfxMessageBox("文件名格式不对!");
- return;
- }
- int count = 1;
- strversion += CString(vname[0]) + CString(vname[1]);
- for (int i = 2; i < vname.GetLength() - 2; i++)
- {
- if (vname[i] == '_')
- {
- count++;
- strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
- }
- }
- if (count != 3 && count != 4)
- {
- AfxMessageBox("文件名版本号数目不对!");
- return;
- }
- GetDlgItem(NAME_B)->SetWindowText(name);//dif
- BYTE* v = new BYTE[MAXSIZE];
- int i = 0;
- //open
- FILE *fr;
- fopen_s(&fr, path, "rb");
- if (fr == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- //size
- fseek(fr, 0, SEEK_END);
- long int size = ftell(fr);
- fseek(fr, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- //scan
- while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- fclose(fr);
- //crc
- UINT crc = crc16(v, size);
- //time
- CStringW namew;
- namew = path;
- SYSTEMTIME stLocal;
- GetModifyDateTime(namew, stLocal);
- char time[30] = { 0 };
- //get_current_time_2(time);
- sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
- stLocal.wYear, stLocal.wMonth, stLocal.wDay,
- stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
- //起始地址
- int addr = HEAD_LEN + m_nLen_A; //dif
- //showtext
- GetDlgItem(VERSION_B)->SetWindowText(strversion); //dif
- GetDlgItem(TIME_B)->SetWindowText(time); //dif
- GetDlgItem(LEN_B)->SetWindowText(_I2T(size)); //dif
- GetDlgItem(CRC_B)->SetWindowText("0x" + Int2HexString(crc)); //dif
- GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addr)); //dif
- m_version_B = strversion; //dif
- m_time_B = time; //dif
- m_len_B = _I2T(size); //dif
- m_crc_B = _I2T(crc); //dif
- m_addr_B = _I2T(addr); //dif
- m_nLen_B = size; //dif
- m_nCrc_B = crc; //dif
- m_nAddr_B = addr; //dif
- delete v;
- //B特有 在B改变后再次计算C的起始地址
- int addrc = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
- GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc)); //dif
- m_addr_C = _I2T(addrc); //dif
- m_nAddr_C = addrc; //dif
- }
- void CHSCDlg::GetInfo_C()
- {
- CString name = m_name_C;//dif
- CString path = m_path_C;//dif
- //version
- CString strversion;
- char c[100] = { 0 };
- memcpy(c, name, name.GetLength());
- CString strlow = strlowr(c);
- int pos = strlow.Find("_v");
- if (pos == -1)
- {
- AfxMessageBox("文件名格式不对!");
- return;
- }
- CString vname = name.Right(name.GetLength() - pos - 2);
- int pos2 = vname.Find(".");
- vname = vname.Left(pos2);
- if (vname.GetLength() < 8)
- {
- AfxMessageBox("文件名格式不对!");
- return;
- }
- int count = 1;
- strversion += CString(vname[0]) + CString(vname[1]);
- for (int i = 2; i < vname.GetLength() - 2; i++)
- {
- if (vname[i] == '_')
- {
- count++;
- strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
- }
- }
- if (count != 3 && count != 4)
- {
- AfxMessageBox("文件名版本号数目不对!");
- return;
- }
- GetDlgItem(NAME_C)->SetWindowText(name);//dif
- BYTE* v = new BYTE[MAXSIZE];
- int i = 0;
- //open
- FILE *fr;
- fopen_s(&fr, path, "rb");
- if (fr == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- //size
- fseek(fr, 0, SEEK_END);
- long int size = ftell(fr);
- fseek(fr, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- //scan
- while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- fclose(fr);
- //crc
- UINT crc = crc16(v, size);
- //time
- CStringW namew;
- namew = path;
- SYSTEMTIME stLocal;
- GetModifyDateTime(namew, stLocal);
- char time[30] = { 0 };
- //get_current_time_2(time);
- sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
- stLocal.wYear, stLocal.wMonth, stLocal.wDay,
- stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
- //起始地址
- int addr = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
- //showtext
- GetDlgItem(VERSION_C)->SetWindowText(strversion); //dif
- GetDlgItem(TIME_C)->SetWindowText(time); //dif
- GetDlgItem(LEN_C)->SetWindowText(_I2T(size)); //dif
- GetDlgItem(CRC_C)->SetWindowText("0x" + Int2HexString(crc)); //dif
- GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addr)); //dif
- m_version_C = strversion; //dif
- m_time_C = time; //dif
- m_len_C = _I2T(size); //dif
- m_crc_C = _I2T(crc); //dif
- m_addr_C = _I2T(addr); //dif
- m_nLen_C = size; //dif
- m_nCrc_C = crc; //dif
- m_nAddr_C = addr; //dif
- delete v;
- }
- char *GetFilename(char *p)
- {
- int x = strlen(p);
- char ch = '\\';
- char *q = strrchr(p, ch) + 1;
- return q;
- }
- void CHSCDlg::OnDropFiles(HDROP hDropInfo)
- {
-
- UINT count;
- TCHAR filePath[MAX_PATH] = { 0 };
- count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);//从成功的拖放操作中检索文件的名称。并取代被拖拽文件的数目
- /*
- if (count == 1)//如果只拖拽一个文件夹
- {
- DragQueryFile(hDropInfo, 0, filePath, sizeof(filePath));//获得拖拽的文件名
- CString m_szPath = filePath;
- char ch[300] = { 0 };
- memcpy(ch, m_szPath, m_szPath.GetLength());
- CString name = GetFilename(ch);
- char c[100] = { 0 };
- memcpy(c, name, name.GetLength());
- CString strlow = strlowr(c);
- if (strlow.Find("apps") != -1)
- {
- m_path_A = m_szPath;
- m_name_A = name;
- GetInfo_A();
- }
- else if (strlow.Find("uiic") != -1)
- {
- m_path_B = m_szPath;
- m_name_B = name;
- GetInfo_B();
- }
- else if (strlow.Find("com") != -1)
- {
- m_path_C = m_szPath;
- m_name_C = name;
- GetInfo_C();
- }
- else
- {
- AfxMessageBox("没找到包含APPS、UIIC或COM字符串的文件!");
- }
- UpdateData(FALSE);
- DragFinish(hDropInfo);//拖放成功后,释放内存
- CDialog::OnDropFiles(hDropInfo);
- return;
- }
- else//如果拖拽多个文件夹
- */
- {
- //m_vectorFile.clear();
- int bfind = 0;
- for (UINT i = 0; i<count; i++)
- {
- int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
- CString m_szPath = filePath;
- m_szPath = filePath;
- char ch[300] = { 0 };
- memcpy(ch, m_szPath, m_szPath.GetLength());
- CString name = GetFilename(ch);
- char c[100] = { 0 };
- memcpy(c, name, name.GetLength());
- CString strlow = strlowr(c);
- HSCDATA data;
-
- if (GetInfo(name, m_szPath, data))
- {
- dataset.insertdata(data);
- }
- if (strlow.Find("apps") != -1)
- {
- m_path_A = m_szPath;
- m_name_A = name;
- GetInfo_A();
- bfind = 1;
- }
- else if (strlow.Find("uiic") != -1)
- {
- m_path_B = m_szPath;
- m_name_B = name;
- GetInfo_B();
- bfind = 1;
- }
- else if (strlow.Find("com") != -1)
- {
- m_path_C = m_szPath;
- m_name_C = name;
- GetInfo_C();
- bfind = 1;
- }
-
- //m_vectorFile.push_back(filePath);
- //break;
- }
- if (!bfind)
- {
- AfxMessageBox("没找到包含APPS、UIIC或COM字符串的文件!");
- }
- UpdateData(FALSE);
- DragFinish(hDropInfo);
- }
- updatelistctrl();
- CDialogEx::OnDropFiles(hDropInfo);
- }
- void CHSCDlg::Generate()
- {
- if (m_name_A == "" || m_name_B == "" || m_name_C == "")
- {
- AfxMessageBox("请选择三个固件!");
- return;
- }
- //1
- BYTE* v1 = new BYTE[MAXSIZE];
- int i = 0;
- FILE *fr1;
- fopen_s(&fr1, m_path_A, "rb");
- if (fr1 == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- fseek(fr1, 0, SEEK_END);
- long int size = ftell(fr1);
- fseek(fr1, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- while (fscanf(fr1, "%c", &v1[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- int count1 = i;
- fclose(fr1);
- //2
- char* v2 = new char[MAXSIZE];
- i = 0;
- FILE *fr2;
- fopen_s(&fr2, m_path_B, "rb");
- if (fr2 == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- fseek(fr2, 0, SEEK_END);
- size = ftell(fr2);
- fseek(fr2, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- while (fscanf(fr2, "%c", &v2[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- int count2 = i;
- fclose(fr2);
- //3
- char* v3 = new char[MAXSIZE];
- i = 0;
- FILE *fr3;
- fopen_s(&fr3, m_path_C, "rb");
- if (fr3 == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- fseek(fr3, 0, SEEK_END);
- size = ftell(fr3);
- fseek(fr3, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- while (fscanf(fr3, "%c", &v3[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- int count3 = i;
- fclose(fr3);
- //
- CString strV;
- GetDlgItem(VERSION_MAIN)->GetWindowText(strV);
- if (strV.GetLength() != 8)
- {
- AfxMessageBox("请输入8位的主版本号!");
- return;
- }
- CString strVA;
- GetDlgItem(VERSION2_A)->GetWindowText(strVA);
- if (strVA.GetLength() != 8)
- {
- AfxMessageBox("请输入8位的主板固件版本号!");
- return;
- }
- CString strVB;
- GetDlgItem(VERSION2_B)->GetWindowText(strVB);
- if (strVB.GetLength() != 8)
- {
- AfxMessageBox("请输入8位的UI固件版本号!");
- return;
- }
- CString strVC;
- GetDlgItem(VERSION2_C)->GetWindowText(strVC);
- if (strVC.GetLength() != 8)
- {
- AfxMessageBox("请输入8位的COM固件版本号!");
- return;
- }
- CString firmname = "firmware_" + strV.Mid(0, 2) + "_" + strV.Mid(2, 2)
- + "_" + strV.Mid(4, 2) + "_" + strV.Mid(6, 2) + ".hsc";
- FILE *fini;
- fopen_s(&fini, "HSC.ini", "wb");
- fwrite(firmname, firmname.GetLength(),1, fini);
- fclose(fini);
- FILE *fw;
- fopen_s(&fw, firmname, "wb");
- //writehex(fw, "00012345");
- //writehex_little_end(fw, "00012345");
- //烧录文件主版本号
- writehex(fw, strV);
- //烧录文件生成时间
- char time[15];
- get_current_time(time);
- GetDlgItem(TIME_MAIN)->SetWindowText(time);
- CString strtime = "00" + CString(time);
- writehex(fw, strtime);
- //烧录文件生成软件所在电脑用户名
- CString struser = m_username;
- if (struser.GetLength() > 32)
- {
- struser = struser.Left(32);
- }
- int nullnum = 32 - struser.GetLength();
- for (auto i = 0; i < nullnum; i++)
- {
- writehex(fw, "00");
- }
-
- writestr(fw, struser);
-
- //固件A版本号
- if (m_version_A.GetLength() == 6)
- {
- writehex(fw, "00");
- }
- writehex(fw, m_version_A);
- //适配硬件A版本号
- writehex(fw, strVA);
- //固件A生成时间
- CString stime = m_time_A;
- 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);
- writehex(fw, strtimeA);
- //固件A长度
- CString strLenA = Int2HexString(m_nLen_A,4);
- writehex_little_end(fw, strLenA);
- //固件A的CRC16
- CString strCrcA = Int2HexString(m_nCrc_A,4);
- writehex_little_end(fw, strCrcA);
- //固件A在文件中的起始地址
- CString strAddrA = Int2HexString(m_nAddr_A,4);
- writehex_little_end(fw, strAddrA);
- //固件B版本号
- if (m_version_B.GetLength() == 6)
- {
- writehex(fw, "00");
- }
- writehex(fw, m_version_B);
- //适配硬件B版本号
- writehex(fw, strVB);
- //固件B生成时间
- stime = m_time_B;
- 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);
- writehex(fw, strtimeB);
- //固件B长度
- CString strLenB = Int2HexString(m_nLen_B, 4);
- writehex_little_end(fw, strLenB);
- //固件B的CRC16
- CString strCrcB = Int2HexString(m_nCrc_B, 4);
- writehex_little_end(fw, strCrcB);
- //固件B在文件中的起始地址
- CString strAddrB = Int2HexString(m_nAddr_B, 4);
- writehex_little_end(fw, strAddrB);
- //固件C版本号
- if (m_version_C.GetLength() == 6)
- {
- writehex(fw, "00");
- }
- writehex(fw, m_version_C);
- //适配硬件C版本号
- writehex(fw, strVC);
- //固件C生成时间
- stime = m_time_C;
- 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);
- writehex(fw, strtimeC);
- //固件C长度
- CString strLenC = Int2HexString(m_nLen_C, 4);
- writehex_little_end(fw, strLenC);
- //固件C的CRC16
- CString strCrcC = Int2HexString(m_nCrc_C, 4);
- writehex_little_end(fw, strCrcC);
- //固件C在文件中的起始地址
- CString strAddrC = Int2HexString(m_nAddr_C, 4);
- writehex_little_end(fw, strAddrC);
- /*固件的CRC16数据使用初始源文件计算结果,计算完CRC16后,
- 覆盖写入到对应固件起点偏移地址为0x1C的4个字节上*/
- int crc_beg = 0x1c;
- v1[crc_beg] = m_nCrc_A % 256;
- v1[crc_beg + 1] = m_nCrc_A / 256;
- v2[crc_beg] = m_nCrc_B % 256;
- v2[crc_beg + 1] = m_nCrc_B / 256;
- v3[crc_beg] = m_nCrc_C % 256;
- v3[crc_beg + 1] = m_nCrc_C / 256;
- /*固件加密方式是使用文件长度(取最低的一字节)对源文件数据进行异或运算,
- 对前200个字节依次进行异或,然后修改生成新的固件数据再放入烧录文件主体中。
- 例如:文件长度为0x7452(29778bytes)。取0x52对源文件数据的前200字节依次进行异或运算,
- 再覆盖到原来字节的位置上,生成的文件即是加密后的数据块*/
- //加密处理后的固件A数据块
- int lowbyte = count1 & 0xff;
- for (int i = 0; i < 200 && i < count1; i++)
- {
- v1[i] = lowbyte ^ v1[i];
- }
- for (int i = 0; i < count1; i++)
- {
- fwrite(&v1[i], 1, 1, fw);
- }
- //加密处理后的固件B数据块
- lowbyte = count2 & 0xff;
- for (int i = 0; i < 200 && i < count2; i++)
- {
- v2[i] = lowbyte ^ v2[i];
- }
- for (int i = 0; i < count2; i++)
- {
- fwrite(&v2[i], 1, 1, fw);
- }
- //加密处理后的固件C数据块
- lowbyte = count3 & 0xff;
- for (int i = 0; i < 200 && i < count3; i++)
- {
- v3[i] = lowbyte ^ v3[i];
- }
- for (int i = 0; i < count3; i++)
- {
- fwrite(&v3[i], 1, 1, fw);
- }
- fclose(fw);
- delete v1;
- delete v2;
- delete v3;
- AfxMessageBox("生成文件成功!");
- }
- void CHSCDlg::ChooseC()
- {
- CString strFile = _T("");
- CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);
- if (dlgFile.DoModal())
- {
- m_path_C = dlgFile.GetPathName();
- m_name_C = dlgFile.GetFileName();
- //if (m_name_A != "" && m_name_A == m_name_B)
- //{
- // AfxMessageBox("与另一固件同名,请自行检查!");
- //}
- char c[100] = { 0 };
- memcpy(c, m_name_C, m_name_C.GetLength());
- CString strlow = strlowr(c);
- if (strlow.Find("com") == -1)
- {
- AfxMessageBox("请选择包含COM字符串的文件名!");
- return;
- }
- GetInfo_C();
- }
- }
- int CHSCDlg::GetInfo(CString name, CString path, HSCDATA& data)
- {
- data.name = name;
- data.path = path;
- //version
- CString strversion;
- char c[100] = { 0 };
- memcpy(c, name, name.GetLength());
- CString strlow = strlowr(c);
- int pos = strlow.Find("_v");
- if (pos == -1)
- {
- AfxMessageBox("文件名格式不对!");
- return 0;
- }
- CString vname = name.Right(name.GetLength() - pos - 2);
- int pos2 = vname.Find(".");
- vname = vname.Left(pos2);
- if (vname.GetLength() < 8)
- {
- AfxMessageBox("文件名格式不对!");
- return 0;
- }
- int count = 1;
- strversion += CString(vname[0]) + CString(vname[1]);
- for (int i = 2; i < vname.GetLength() - 2; i++)
- {
- if (vname[i] == '_')
- {
- count++;
- strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
- }
- }
- if (count != 3 && count != 4)
- {
- AfxMessageBox("文件名版本号数目不对!");
- return 0;
- }
- //GetDlgItem(NAME_A)->SetWindowText(name);//dif
- BYTE* v = new BYTE[MAXSIZE];
- int i = 0;
- //open
- FILE *fr;
- fopen_s(&fr, path, "rb");
- if (fr == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return 0;
- }
- //size
- fseek(fr, 0, SEEK_END);
- long int size = ftell(fr);
- fseek(fr, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return 0;
- }
- //scan
- while (fscanf_s(fr, "%c", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- fclose(fr);
- //crc
- UINT crc = crc16(v, size);
- //time
- CStringW namew;
- namew = path;
- SYSTEMTIME stLocal;
- GetModifyDateTime(namew, stLocal);
- char time[30] = { 0 };
- //get_current_time_2(time);
- sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
- stLocal.wYear, stLocal.wMonth, stLocal.wDay,
- stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
- //起始地址
- //int addr = dataset.getbeginaddr();
- //showtext
- //GetDlgItem(VERSION_A)->SetWindowText(strversion); //dif
- //GetDlgItem(TIME_A)->SetWindowText(time); //dif
- //GetDlgItem(LEN_A)->SetWindowText(_I2T(size)); //dif
- //GetDlgItem(CRC_A)->SetWindowText("0x" + Int2HexString(crc)); //dif
- //GetDlgItem(ADDR_A)->SetWindowText("0x" + Int2HexString(addr)); //dif
- data.version = strversion;
- data.time = time;
- data.len = _I2T(size);
- data.crc = _I2T(crc);
- //data.addr = _I2T(addr);
- data.nLen = size;
- data.nCrc = crc;
- //data.nAddr = addr;
- delete v;
- return 1;
- ////A特有 在A改变后再次计算B和C的起始地址
- //int addrb = HEAD_LEN + m_nLen_A; //dif
- //GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addrb)); //dif
- //m_addr_B = _I2T(addrb); //dif
- //m_nAddr_B = addrb; //dif
- //int addrc = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
- //GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc)); //dif
- //m_addr_C = _I2T(addrc); //dif
- //m_nAddr_C = addrc; //dif
- }
- void CHSCDlg::updatelistctrl()
- {
- m_listctrl.DeleteAllItems();
- int index = 0;
- for (auto data:dataset.getdatas())
- {
- m_listctrl.InsertItem(index, _T(""));
- m_listctrl.SetItemText(index, 0, data.name);
- m_listctrl.SetItemText(index, 1, data.version);
- m_listctrl.SetItemText(index, 2, data.version_add);
- m_listctrl.SetItemText(index, 3, data.time);
- m_listctrl.SetItemText(index, 4, data.len);
-
- m_listctrl.SetItemText(index, 5, "0x" + Int2HexString(data.nCrc));
- m_listctrl.SetItemText(index, 6, "0x" + Int2HexString(data.nAddr));
- index++;
- }
- }
- int g_cursel = -1;
- void CHSCDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
- // TODO: Add your control notification handler code here
-
-
- CString str;
- g_cursel = m_listctrl.GetSelectionMark();
- if (g_cursel >= 0 && g_cursel < dataset.getcount())
- {
- HSCDATA data = dataset.m_datas[g_cursel];
- ED_CURNAME.SetWindowText(data.name);
- ED_CURVER.SetWindowText(data.version_add);
- }
-
-
-
-
- *pResult = 0;
- }
- void CHSCDlg::OnBnClickedUpdate()
- {
- if (g_cursel >= 0 && g_cursel < dataset.getcount())
- {
- CString strV;
- ED_CURVER.GetWindowText(strV);
- if (strV.GetLength() != 8)
- {
- AfxMessageBox("请输入8位的固件版本号!");
- return;
- }
- dataset.m_datas[g_cursel].version_add = strV;
- updatelistctrl();
- }
- else
- {
- AfxMessageBox("没选中!");
- }
- }
- void CHSCDlg::OnBnClickedButton6()
- {
-
- for (auto data : dataset.m_datas)
- {
- if (data.version_add.GetLength() != 8)
- {
- AfxMessageBox("请输入8位的固件版本号!");
- return;
- }
- }
- //
- CString strV;
- GetDlgItem(VERSION_MAIN)->GetWindowText(strV);
- if (strV.GetLength() != 8)
- {
- AfxMessageBox("请输入8位的主版本号!");
- return;
- }
- CString firmname = "firmware_" + strV.Mid(0, 2) + "_" + strV.Mid(2, 2)
- + "_" + strV.Mid(4, 2) + "_" + strV.Mid(6, 2) + ".hsc";
- FILE *fini;
- fopen_s(&fini, "HSC.ini", "wb");
- fwrite(firmname, firmname.GetLength(), 1, fini);
- fclose(fini);
- FILE *fw;
- fopen_s(&fw, firmname, "wb");
- //烧录文件主版本号
- writehex(fw, strV);
- //烧录文件生成时间
- char time[15];
- get_current_time(time);
- GetDlgItem(TIME_MAIN)->SetWindowText(time);
- CString strtime = "00" + CString(time);
- writehex(fw, strtime);
- //烧录文件生成软件所在电脑用户名
- CString struser = m_username;
- if (struser.GetLength() > 32)
- {
- struser = struser.Left(32);
- }
- int nullnum = 32 - struser.GetLength();
- for (auto i = 0; i < nullnum; i++)
- {
- writehex(fw, "00");
- }
- writestr(fw, struser);
- //算新crc
- for (auto &data : dataset.m_datas)
- {
- BYTE* v1 = new BYTE[MAXSIZE];
- int i = 0;
- FILE *fr1;
- fopen_s(&fr1, data.path, "rb");
- if (fr1 == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- fseek(fr1, 0, SEEK_END);
- long int size = ftell(fr1);
- fseek(fr1, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- while (fscanf(fr1, "%c", &v1[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- data.count = i;
- fclose(fr1);
- int crc_beg = 0x1c;
- v1[crc_beg] = data.nCrc % 256;
- v1[crc_beg + 1] = data.nCrc / 256;
- data.newcrc = crc16(v1, size);
- delete v1;
- }
- for (auto data : dataset.m_datas)
- {
- //固件版本号
- if (data.version.GetLength() == 6)
- {
- writehex(fw, "00");
- }
- writehex(fw, data.version);
- //适配硬件版本号
- writehex(fw, data.version_add);
- //固件生成时间
- CString stime = data.time;
- 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);
- writehex(fw, strtimeA);
- //固件长度
- CString strLenA = Int2HexString(data.nLen, 4);
- writehex_little_end(fw, strLenA);
- //固件的CRC16
- //CString strCrcA = Int2HexString(data.nCrc, 4);
- CString strCrcA = Int2HexString(data.newcrc, 4);//新crc
- writehex_little_end(fw, strCrcA);
- //固件在文件中的起始地址
- CString strAddrA = Int2HexString(data.nAddr, 4);
- writehex_little_end(fw, strAddrA);
- }
- for (auto data : dataset.m_datas)
- {
- BYTE* v1 = new BYTE[MAXSIZE];
- int i = 0;
- FILE *fr1;
- fopen_s(&fr1, data.path, "rb");
- if (fr1 == NULL) //打开文件出错。
- {
- AfxMessageBox("打开固件失败!");
- return;
- }
- fseek(fr1, 0, SEEK_END);
- long int size = ftell(fr1);
- fseek(fr1, 0, SEEK_SET);
- if (size >= MAXSIZE)
- {
- AfxMessageBox("文件太大!");
- return;
- }
- while (fscanf(fr1, "%c", &v1[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
- {
- i++;
- }
- data.count = i;
- fclose(fr1);
- int crc_beg = 0x1c;
- v1[crc_beg] = data.nCrc % 256;
- v1[crc_beg + 1] = data.nCrc / 256;
- int lowbyte = data.count & 0xff;
- for (int i = 0; i < 200 && i < data.count; i++)
- {
- v1[i] = lowbyte ^ v1[i];
- }
- //
- UINT newcrc = crc16(v1, size);
- for (int i = 0; i < data.count; i++)
- {
- fwrite(&v1[i], 1, 1, fw);
- }
- delete v1;
- }
- fclose(fw);
- AfxMessageBox("生成文件成功!");
- }
|