// HSCDlg.cpp : implementation file // #include "stdafx.h" #include "HSC.h" #include "HSCDlg.h" #include "afxdialogex.h" #include #include #include #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 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 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= 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(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(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> 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= 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(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("生成文件成功!"); }