// FirmwareToolDlg.cpp: 实现文件 // #include "pch.h" #include "framework.h" #include "FirmwareTool.h" #include "FirmwareToolDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif CString sBtldrFileName; CString sAppFileName; CString sAppFileTitle; unsigned char btldrbuf[131072]; unsigned char Appfilebuf[131072]; unsigned char firmwarebuf[262144]; int nBtldrInfoOffset = 256; int AppOffset = 4096; int nAppInfoOffset = 256; int btldrLength = 0; int AppFileLength = 0; int FirmwareSize = 0; unsigned char AppVerMajor = 0; unsigned char AppVerMinor = 0; bool btldrfileLoaded = false; bool appfileLoaded = false; bool combinefileSaved = false; // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialogEx { public: CAboutDlg(); // 对话框数据 #ifdef AFX_DESIGN_TIME enum { IDD = IDD_ABOUTBOX }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CFirmwareToolDlg 对话框 CFirmwareToolDlg::CFirmwareToolDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_FIRMWARETOOL_DIALOG, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CFirmwareToolDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CFirmwareToolDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_OPEN_BTLDR, &CFirmwareToolDlg::OnBnClickedButtonOpenBtldr) ON_BN_CLICKED(IDC_BUTTON_OPEN_APP, &CFirmwareToolDlg::OnBnClickedButtonOpenApp) ON_BN_CLICKED(IDC_BUTTON_MAKE, &CFirmwareToolDlg::OnBnClickedButtonMake) ON_WM_TIMER() ON_WM_CLOSE() END_MESSAGE_MAP() // CFirmwareToolDlg 消息处理程序 BOOL CFirmwareToolDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != nullptr) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 myLogger1.AttachWnd(GetDlgItem(IDC_EDIT_LOG)->m_hWnd); myLogger1.bShowLog[0] = 1; myLogger1.bSaveLog[0] = 1; myLogger1.SetLogPathName(_T("D:/logs/FirmWareTool"), _T("RunLog")); myLogger1.bShowThreadId = 0; myLogger1.bShowChannel = 0; SysLog(_T("Program Start \r\n")); SetTimer(0, 100, NULL);//delayinit SetTimer(2, 100, NULL);//display LoadConfig(); ShowParams(); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CFirmwareToolDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CFirmwareToolDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CFirmwareToolDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } BOOL CFirmwareToolDlg::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 CString s1; if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) { return TRUE; } if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) { return FALSE; } if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_BACK) { return FALSE; } try { // if (m_tooltip1.GetSafeHwnd() != NULL) m_tooltip1.RelayEvent(pMsg); return CDialogEx::PreTranslateMessage(pMsg); } catch (CMemoryException* e) { CString Serror; e->GetErrorMessage(Serror.GetBuffer(256), 256); Serror.ReleaseBuffer(); s1.Format(_T("程序 发生 内存 错误 %s \r\n"), Serror); PopupMessage(s1); } catch (CFileException* e) { CString Serror; e->GetErrorMessage(Serror.GetBuffer(256), 256); Serror.ReleaseBuffer(); s1.Format(_T("程序 发生 文件 错误 %s \r\n"), Serror); PopupMessage(s1); } /* catch (SE_Exception& e) { s1.Format(_T("程序 发生 SE 错误 Code %08X Addr %08X \r\n%s\r\n"), e.getSeNumber(), e.getAddress(), e.getInfoStr()); PopupMessage(s1); } */ catch (CException* e) { CString Serror; e->GetErrorMessage(Serror.GetBuffer(256), 256); Serror.ReleaseBuffer(); s1.Format(_T("程序 发生 C++ 错误 %s \r\n"), Serror); PopupMessage(s1); } catch (...) { s1.Format(_T("程序 发生未知错误 \r\n")); PopupMessage(s1); } return TRUE; return CDialogEx::PreTranslateMessage(pMsg); } int CFirmwareToolDlg::DelayInit() { // TODO: 在此处添加实现代码. // InitCamera(); // InitPlcCon(); // InitNetCon(); SetTimer(1, 50, NULL);//plc // SetTimer(3, 100, NULL); return 0; } int CFirmwareToolDlg::ShowParams() { // TODO: 在此处添加实现代码. CString s1; s1.Format(_T("%d"), AppOffset); SetDlgItemText(IDC_EDIT_OFFSET, s1); return 0; } int CFirmwareToolDlg::GetParams() { // TODO: 在此处添加实现代码. CString s1; GetDlgItemText(IDC_EDIT_OFFSET, s1); AppOffset = _tstoi(s1); // s1.Format(_T("%d"), AppOffset); return 0; } int CFirmwareToolDlg::LoadConfig() { // TODO: 在此处添加实现代码. return 0; } int CFirmwareToolDlg::SaveConfig() { // TODO: 在此处添加实现代码. return 0; } void CFirmwareToolDlg::OnClose() { // TODO: 在此添加消息处理程序代码和/或调用默认值 CDialogEx::OnClose(); } void CFirmwareToolDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if (nIDEvent == 0) { KillTimer(0); DelayInit(); } else if (nIDEvent == 1) { //ProcessPLC(); } else if (nIDEvent == 2) { // ProcessInput(); //DrawPic1(); myLogger1.UpdateLogDisplay(0); } else if (nIDEvent == 3) { } else { } CDialogEx::OnTimer(nIDEvent); } const uint16_t crctalbeabs[] = { 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 }; uint16_t crc16tablefast(uint8_t* ptr, uint16_t len) { uint16_t crc = 0xffff; uint16_t i; uint8_t ch; for (i = 0; i < len; i++) { ch = *ptr++; crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4); crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4); } return crc; } // 载入BootLoader文件 int CFirmwareToolDlg::LoadBtldrFile(CString sFilePathName) { // TODO: 在此处添加实现代码. CString s1; CFile file1; CFileException e; bool r = file1.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e); if (r) { s1.Format(_T("打开 File %s = 成功"), sFilePathName); } else { s1.Format(_T("打开 File %s = 失败"), sFilePathName); } DbgLog(s1); if (r) { int len1 = (int)file1.GetLength(); if (len1 < 65536) { file1.Read(btldrbuf, len1); } else { file1.Read(btldrbuf, 65536); } file1.Close(); unsigned short crc2 = crc16tablefast(btldrbuf, len1); unsigned char* buf2 = btldrbuf + nBtldrInfoOffset; ; //s1.Format(_T("GetInfo From File = %d %d \r\n"), j, len1); unsigned int nBlkType = 0; unsigned int nBlkVer = 0; s1.Empty(); s1.Format(_T("文件信息\r\n")); SetDlgItemText(IDC_STATIC_BTLDR_INFO, s1); pBtLdrInfoBlock pinfob = (pBtLdrInfoBlock)buf2; if (pinfob->Hdr.nBlkSign == 0xaa55 ) { s1.Empty(); s1.Format(_T("文件信息 ")); // s1.AppendFormat(_T("启动文件 %s \r\n"), sFilePathName); s1.AppendFormat(_T("文件大小: %d \r\n"), len1); s1.AppendFormat(_T("信息块位置: 0x%04X 信息块大小: %d \r\n"), nBtldrInfoOffset, pinfob->Hdr.nBlkSize); s1.AppendFormat(_T("信息块类型: 0x%04X "), pinfob->Hdr.nBlkTypeVer); nBlkType = (pinfob->Hdr.nBlkTypeVer) >> 8; nBlkVer = pinfob->Hdr.nBlkTypeVer & 0xff; if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "),nBlkVer); } else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); } else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); } else { s1.AppendFormat(_T(" (未知类型) ")); } s1.AppendFormat(_T("\r\n")); s1.AppendFormat(_T("启动区版本: 0x%04X 启动器对应芯片: 0x%04X\r\n"), pinfob->nBtldrVer, pinfob->nBtldrDevice); s1.AppendFormat(_T("启动器大小: 0x%04X 实际代码大小: 0x%04X \r\n"), pinfob->nBtldrSize, pinfob->nBtldrDataSize); s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pinfob->nBtldr_AppAddr, pinfob->nBtldr_AppAddr -0x08000000); s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pinfob->nBtldr_NewAppInfoAddr, pinfob->nBtldr_NewAppAddr); SetDlgItemText(IDC_STATIC_BTLDR_INFO, s1); if (nBlkType == 0x01 || nBlkType == 0x02) { btldrLength = len1; btldrfileLoaded = true; pAppInfoBlock pAppinfo2 = (pAppInfoBlock)(btldrbuf + AppOffset + nAppInfoOffset); s1.Append(_T("\r\n")); s1.AppendFormat(_T("信息块2位置: 0x%04X 信息块2大小: %d \r\n"), AppOffset + nAppInfoOffset, pAppinfo2->Hdr.nBlkSize); s1.AppendFormat(_T("信息块2类型: 0x%04X "), pAppinfo2->Hdr.nBlkTypeVer); nBlkType = (pAppinfo2->Hdr.nBlkTypeVer) >> 8; nBlkVer = pAppinfo2->Hdr.nBlkTypeVer & 0xff; if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); } else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); } else { s1.AppendFormat(_T(" (未知类型) ")); } s1.AppendFormat(_T("\r\n")); s1.AppendFormat(_T("对应模块类型: %04X %s\r\n"), pAppinfo2->nAppDevice, DeviceTypeToStr(pAppinfo2->nAppDevice)); s1.AppendFormat(_T("应用区版本: %d.%02d\r\n"), pAppinfo2->nAppVerMajor,pAppinfo2->nAppVerMinor); s1.AppendFormat(_T("应用区大小: 0x%04X 实际代码大小: 0x%04X \r\n"), pAppinfo2->nAppSize, pAppinfo2->nAppDataSize); s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pAppinfo2->nAppStartAddr, pAppinfo2->nAppStartAddr - 0x08000000); s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pAppinfo2->nAppStartOffset, pAppinfo2->nApp); //s2 += s1; if ((pAppinfo2->Hdr.nBlkSign == 0xaa55)) { SetDlgItemText(IDC_STATIC_BTLDR_INFO, s1); } } else { s1.Format(_T("文件信息块中不包含 BootLoader 内容, 是否继续 ")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { btldrLength = len1; btldrfileLoaded = true; } } // s1.Format(_T(" BootLoader启动文件 版本 %04X 硬件类型 %04X 占用空间 %04X, %d Bytes\r\n 是否继续"), pinfob->nBtldrVer, pinfob->nBtldrDevice, pinfob->nBtldrSize, len1); } else { s1.Format(_T("BootLoader启动文件中未找到信息块, 文件 %d Bytes\r\n 是否继续"), len1); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { btldrLength = len1; btldrfileLoaded = true; } } } return 0; } // 载入APP应用程序文件 int CFirmwareToolDlg::LoadAppFile(CString sFilePathName) { // TODO: 在此处添加实现代码. CString s1; CString s2; CFile file1; CFileException e; bool r = file1.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e); if (r) { s1.Format(_T("打开 File %s = 成功"), sFilePathName); } else { s1.Format(_T("打开 File %s = 失败"), sFilePathName); } DbgLog(s1); if (r) { int len1 = (int)file1.GetLength(); if (len1 < 262144) { file1.Read(Appfilebuf, len1); } else { file1.Read(Appfilebuf, len1); } file1.Close(); unsigned short crc2 = crc16tablefast(Appfilebuf, len1); unsigned int nBlkType = 0; unsigned int nBlkVer = 0; // 查找 Appinfo 和 KMInfo Block pAppInfoBlock pAppinfo = (pAppInfoBlock)(Appfilebuf + nAppInfoOffset); s1.Empty(); s1.Format(_T("文件信息 \r\n")); SetDlgItemText(IDC_STATIC_APP_INFO, s1); if (pAppinfo->Hdr.nBlkSign == 0xaa55) { s1.Empty(); s1.Format(_T("文件信息 ")); // s1.AppendFormat(_T("启动文件 %s \r\n"), sFilePathName); s1.AppendFormat(_T("文件大小: %d \r\n"), len1); s1.AppendFormat(_T("信息块1位置: 0x%04X 信息块大小: %d \r\n"), nAppInfoOffset, pAppinfo->Hdr.nBlkSize); s1.AppendFormat(_T("信息块类型: 0x%04X "), pAppinfo->Hdr.nBlkTypeVer); nBlkType = (pAppinfo->Hdr.nBlkTypeVer) >> 8; nBlkVer = pAppinfo->Hdr.nBlkTypeVer & 0xff; if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); } else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); } else { s1.AppendFormat(_T(" (未知类型) ")); } s1.AppendFormat(_T("\r\n")); if (nBlkType == 0x01 || nBlkType == 0x02) { pBtLdrInfoBlock pinfob = (pBtLdrInfoBlock)pAppinfo; s1.AppendFormat(_T("启动区版本: 0x%04X 启动器对应芯片: 0x%04X\r\n"), pinfob->nBtldrVer, pinfob->nBtldrDevice); s1.AppendFormat(_T("启动器大小: 0x%04X 实际代码大小: 0x%04X \r\n"), pinfob->nBtldrSize, pinfob->nBtldrDataSize); s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pinfob->nBtldr_AppAddr, pinfob->nBtldr_AppAddr - 0x08000000); s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pinfob->nBtldr_NewAppInfoAddr, pinfob->nBtldr_NewAppAddr); } else if (nBlkType == 0x03 || nBlkType == 0x04) { s1.AppendFormat(_T("对应模块类型: %04X %s\r\n"), pAppinfo->nAppDevice, DeviceTypeToStr(pAppinfo->nAppDevice)); s1.AppendFormat(_T("应用区版本: %d.%02d \r\n"), pAppinfo->nAppVerMajor, pAppinfo->nAppVerMinor); s1.AppendFormat(_T("应用区大小: 0x%04X 实际代码大小: 0x%04X \r\n"), pAppinfo->nAppSize, pAppinfo->nAppDataSize); s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pAppinfo->nAppStartAddr, pAppinfo->nAppStartAddr - 0x08000000); s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pAppinfo->nAppStartOffset, pAppinfo->nApp); } s2 = s1; SetDlgItemText(IDC_STATIC_APP_INFO, s1); if (nBlkType == 0x03 || nBlkType == 0x04) { appfileLoaded = true; AppFileLength = len1; AppVerMajor = pAppinfo->nAppVerMajor; AppVerMinor = pAppinfo->nAppVerMinor; } else { if ((nBlkType == 0x01 || nBlkType == 0x02) && len1 >= AppOffset + nAppInfoOffset + sizeof(stAppInfoBlock) ) { pAppInfoBlock pAppinfo2 = (pAppInfoBlock)(Appfilebuf + AppOffset + nAppInfoOffset); s1.Append(_T("\r\n")); s1.AppendFormat(_T("信息块2位置: 0x%04X 信息块2大小: %d \r\n"), AppOffset + nAppInfoOffset, pAppinfo2->Hdr.nBlkSize); s1.AppendFormat(_T("信息块2类型: 0x%04X "), pAppinfo2->Hdr.nBlkTypeVer); nBlkType = (pAppinfo2->Hdr.nBlkTypeVer) >> 8; nBlkVer = pAppinfo2->Hdr.nBlkTypeVer & 0xff; if (nBlkType == 0x01) { s1.AppendFormat(_T(" (BootLoader 类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x02) { s1.AppendFormat(_T(" (BootLoader 类型2 版本 %d) "), nBlkVer); } else if (nBlkType == 0x03) { s1.AppendFormat(_T(" (App类型1 版本 %d) "), nBlkVer); } else if (nBlkType == 0x04) { s1.AppendFormat(_T(" (App类型2 版本 %d) "), nBlkVer); } else { s1.AppendFormat(_T(" (未知类型) ")); } s1.AppendFormat(_T("\r\n")); s1.AppendFormat(_T("对应模块类型: %04X %s\r\n"), pAppinfo2->nAppDevice, DeviceTypeToStr(pAppinfo2->nAppDevice)); s1.AppendFormat(_T("应用区版本: %d.%02d\r\n"), pAppinfo2->nAppVerMajor,pAppinfo2->nAppVerMinor); s1.AppendFormat(_T("应用区大小: 0x%04X 实际代码大小: 0x%04X \r\n"), pAppinfo2->nAppSize, pAppinfo2->nAppDataSize); s1.AppendFormat(_T("应用区地址: 0x%08X (偏移 %d )\r\n"), pAppinfo2->nAppStartAddr, pAppinfo2->nAppStartAddr - 0x08000000); s1.AppendFormat(_T("信息暂存区: 0x%08X \r\n应用暂存区: 0x%08X \r\n"), pAppinfo2->nAppStartOffset, pAppinfo2->nApp); //s2 += s1; if ((pAppinfo2->Hdr.nBlkSign == 0xaa55) && pAppinfo2->Hdr.nBlkTypeVer == 0x0301) { SetDlgItemText(IDC_STATIC_APP_INFO, s1); s1.Format(_T("文件已经是bootloader和App合成的文件 , 是否使用文件中App?")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { memmove(Appfilebuf, Appfilebuf + AppOffset, len1 - AppOffset); appfileLoaded = true; AppFileLength = len1-AppOffset; AppVerMajor = pAppinfo2->nAppVerMajor; AppVerMinor = pAppinfo2->nAppVerMinor; } } else { s1.Format(_T("文件 不是App文件 , 是否继续 ")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { appfileLoaded = true; AppFileLength = len1; AppVerMajor = 0; AppVerMinor = 0; } } } else { s1.Format(_T("文件 不是App文件 , 是否继续 ")); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { appfileLoaded = true; AppFileLength = len1; AppVerMajor = 0; AppVerMinor = 0; } } } // s1.Format(_T(" BootLoader启动文件 版本 %04X 硬件类型 %04X 占用空间 %04X, %d Bytes\r\n 是否继续"), pinfob->nBtldrVer, pinfob->nBtldrDevice, pinfob->nBtldrSize, len1); } else { s1.Format(_T("文件中未找到信息块, 文件大小 %d Bytes\r\n 是否继续"), len1); int r3 = AfxMessageBox(s1, MB_YESNO); if (r3 == IDYES) { appfileLoaded = true; AppFileLength = len1; AppVerMajor = 0; AppVerMinor = 0; } } } return 0; } int CFirmwareToolDlg::MakeFirmware() { // TODO: 在此处添加实现代码. CString s1; s1.Format(_T("Btldr File Size %d \r\n"), btldrLength); s1.AppendFormat(_T("App File Size %d \r\n"), AppFileLength); DbgLog(s1); if (btldrfileLoaded && appfileLoaded) { memcpy(firmwarebuf, btldrbuf, AppOffset); memcpy(firmwarebuf + AppOffset, Appfilebuf, AppFileLength); FirmwareSize = AppOffset + AppFileLength; s1.Format(_T("Firmware File Size %d \r\n"), FirmwareSize); DbgLog(s1); } return 0; } void CFirmwareToolDlg::OnBnClickedButtonOpenBtldr() { // TODO: 在此添加控件通知处理程序代码 CString s1; CFileDialog dialog1(true); INT_PTR r = dialog1.DoModal(); if (r == IDOK) { CString sFilePathName = dialog1.GetPathName(); SetDlgItemText(IDC_EDIT_BTLDR_NAME, sFilePathName); //OpenFile LoadBtldrFile(sFilePathName); } } void CFirmwareToolDlg::OnBnClickedButtonOpenApp() { // TODO: 在此添加控件通知处理程序代码 CString s1; CFileDialog dialog1(true); INT_PTR r = dialog1.DoModal(); if (r == IDOK) { CString sFilePathName = dialog1.GetPathName(); sAppFileName = dialog1.GetFileName(); sAppFileTitle = dialog1.GetFileTitle(); SetDlgItemText(IDC_EDIT_APP_NAME, sFilePathName); //OpenFile LoadAppFile(sFilePathName); } } void CFirmwareToolDlg::OnBnClickedButtonMake() { // TODO: 在此添加控件通知处理程序代码 CString s1; int r = MakeFirmware(); CString sFileName; if (AppVerMajor > 0) { s1.Format(_T("%d.%02d"), AppVerMajor, AppVerMinor); sFileName = sAppFileTitle + _T("_") + s1; } else { sFileName = sAppFileTitle + _T("_all"); } CFileDialog dialog1(false,_T("bin"),sFileName); INT_PTR r2 = dialog1.DoModal(); if (r2 == IDOK) { CString sFilePathName = dialog1.GetPathName(); SetDlgItemText(IDC_EDIT_FIRMWARE_NAME, sFilePathName); //OpenFile SaveFirmwareAsFile(sFilePathName); } } int CFirmwareToolDlg::SaveFirmwareAsFile(CString sFilePathName) { // TODO: 在此处添加实现代码. CString s1; CFile file1; CFileException e; bool r = file1.Open(sFilePathName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, &e); if (r) { s1.Format(_T("创建 File %s = 成功"), sFilePathName); } else { s1.Format(_T("创建 File %s = 失败"), sFilePathName); } DbgLog(s1); if (r) { int len1 = FirmwareSize; file1.Write(firmwarebuf, len1); file1.Close(); s1.Format(_T("保存 File %s = 完成"), sFilePathName); DbgLog(s1); } return 0; }