// Upgrade.cpp : implementation file // #include "stdafx.h" #include "udpadmintool.h" #include "Upgrade.h" #include #include // _beginthread, _endthread #include "util.hpp" #include "util_local.h" #include "util_firmware.h" #include "login.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CUpgrade dialog CUpgrade::CUpgrade(CWnd* pParent /*=NULL*/) : CDialog(CUpgrade::IDD, pParent) { //{{AFX_DATA_INIT(CUpgrade) m_bAuto = FALSE; m_edSelectIP = _T(""); //}}AFX_DATA_INIT } void CUpgrade::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CUpgrade) DDX_Control(pDX, IDC_PROGRESS_UP, m_prUpgrade); DDX_Check(pDX, IDC_CHECK_AUTO, m_bAuto); DDX_Text(pDX, IDC_EDIT_SELECT_IP, m_edSelectIP); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CUpgrade, CDialog) //{{AFX_MSG_MAP(CUpgrade) ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind) ON_BN_CLICKED(IDC_BUTTON_UG, OnButtonUg) ON_BN_CLICKED(IDC_RADIO_BOOT, OnRadioBoot) ON_BN_CLICKED(IDC_RADIO_FIRM, OnRadioFirm) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CUpgrade message handlers // not test~~!!! /* Device Name : NVE4000rev50 Æß¿þ¾î ¾ÕÂÊ 40¹ÙÀÌÆ®¿¡ 32¹ø° À§Ä¡ºÎÅÍ Linux image HW1.3 KR423 ¿Í °°Àº Æß¿þ¾î ¹öÁ¯ÀÌ ÀÖÀ½ rev³Ñ¹ö°¡ 30ÀÌ»óÀÌ°í HW1.3À̸é ÀûÇÕ(buf[16] = 3À̸é) rev³Ñ¹ö°¡ 30ÀÌÇÏÀÌ°í HW1.1À̸é ÀûÇÕ(buf[16] = 1À̸é) */ void CUpgrade::OnButtonFind() { // TODO: Add your control notification handler code here char szFilter[30]; // char sFVersion[10]; UpdateData(TRUE); /* // get revision number - move to initdialog() if(strstr(theApp.m_info.Name, "rev")) { sprintf(sFVersion, "%s", strstr(theApp.m_info.Name, "rev")); iFVersion = atoi(&sFVersion[3]); } else { warn("Fail to get firmware information\r\nDo it again (Refresh)"); return; } */ if(m_bAuto && m_iRadio == 1) { _chdir(m_sFilePath); if(!AutoFindFile()) { warn("Not find file"); return; } /* if(m_iRadio) { if(iFVersion<30 && iFVersion>0) sprintf(szFilter, "IMAGE File|KernelT*.*||"); else if(iFVersion >= 30) sprintf(szFilter, "IMAGE File|Kernel16*.*||"); else sprintf(szFilter, "IMAGE File|Kernel*.*||"); } else { sprintf(szFilter, "IMAGE File|*.img||"); } */ } else { if(m_iRadio) { // firmware sprintf(szFilter, "IMAGE File(*.*)|*.*||"); } else { sprintf(szFilter, "IMAGE File(*.img)|*.img||"); } CString path = theApp.get_fw_path(); // registry CFileDialog dlgOpen(TRUE, NULL, path, OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST, szFilter, this); // Open dialog dlgOpen.m_ofn.lpstrTitle = _T("Select files to burn"); // Select and add files if(IDOK == dlgOpen.DoModal()) { CString path = dlgOpen.GetPathName(); set_file(path, 0, TRUE, m_iRadio == 1 ? false:true); // radio == 1 -> firmware // radio == 0 -> bootloader } } } int CUpgrade::set_file(CString path, int version, BOOL bWarning, bool bBootloader) { // ¼±ÅÃµÈ°Ô ¾ø´Â °æ¿ìÀÔ´Ï´Ù. if(path == "") { GetDlgItem(IDC_STATIC_FILE)->SetWindowText("None"); return -1; } // if(bBootloader == FALSE) // { // kernel char type[32]; // char firm_ver[1024]; int hr = get_fw_version(path, type); if(hr < 0) { // fail switch(hr) { // -1 : ÆÄÀÏÀ» ¿­¼ö ¾ø´Ù. // -2 : ÆÄÀÏÀÇ Å©±â°¡ ÀÛÀº °æ¿ì(90¹ÙÀÌÆ®¹Ì¸¸) // -3 : À߸øµÈ ÆÄÀÏÀÌ´Ù(¹öÁ¯Á¤º¸°¡ ¾ø´Â °æ¿ì) case -1: if(bWarning) warn("File open error... %s \n", path); break; case -2: if(bWarning) warn("File read error... %s \n", path); break; // case -3: // if(bWarning) warn("Invalid firmware... %s \n", path); // break; default: if(bWarning) warn("Unknown error at get_fw_version() [%d]\n", hr); } return 0; } if(bBootloader == FALSE) { if(strncmp(type, "Linux image HW", 14) != 0) { if(bWarning) warn("Invalid firmware... %s \n", path); } } else { if(strncmp(type, "ub16m", 5) != 0) { if(bWarning) warn("Invalid bootloader... %s \n", path); } } //warn2("jjajaa %d %s", 3, "8823"); /* ¸®ºñÁ¯ ¹øÈ£·Î Æß¿þ¾î ÀÇÁ¸¼º üũ¸¦ ÇÏÁö ¾Ê½À´Ï´Ù. - cjy, 07_0718 17:04 // check firmware hr = check_valid_upgrade(firm_ver, iFVersion); if(hr < 0) { // m_bFile = FALSE; GetDlgItem(IDC_STATIC_FILE)->SetWindowText("None"); if(bWarning) { warn("bad file"); } return 0; } */ TRACE("find %s, %s\n", path, type); // version check int nCurVer = 0; if(bBootloader == FALSE) { // bootloader didn't have version // ¿©±â¼­´Â get_fw_version_nÀ» ½á¾ß ÇÕ´Ï´Ù - cjy, 08_0723 09:37 nCurVer = get_fw_version_n(type); //nCurVer = get_firm_version(type); if(nCurVer <= version) { return 0; } } // } // success char text[1024]; m_sFilePath = path; sprintf(text, "%s(%s)", m_sFilePath, type); GetDlgItem(IDC_STATIC_FILE)->SetWindowText(text); // m_bFile = TRUE; //CString path = get_parent(dlgOpen.GetPathName()); //theApp.set_fw_path(path); // registry if(bBootloader == FALSE) { theApp.set_fw_path(path); } else { theApp.set_value("PATH", "BootLoader", path); } return nCurVer; } struct __thread_upgrade { CString ip; CString file; int flag; CString id; char pw_hash[16]; int result; }; unsigned int __stdcall ThreadUpgrade(void* data) { __thread_upgrade* tg; int upgrade_flag; tg = (__thread_upgrade*)data; tg->result = -9980; // do // ¹Ýȯ°ªÀÌ ¾ç¼ö¸é ¼º°ø upgrade_flag = UpGrade((char*)((const char*)(tg->ip)), (char*)((const char*)(tg->file)), tg->flag); tg->result = upgrade_flag; return 1; } void CUpgrade::start_prog(int time) { m_nTimerGap = time; SetTimer(0, time, NULL); } void CUpgrade::modify_prog(int time) { m_nTimerGap = time; SetTimer(0, time, NULL); } void CUpgrade::end_prog() { KillTimer(0); } // id, pw_hash Ãß°¡ - cjy, 07_1130 14:37 int CUpgrade::upgrade_thread(char *ip, char *file, int flag, char* id, char* pw_hash) { __thread_upgrade tg; tg.ip = ip; tg.file = file; tg.flag = flag; tg.id = id; memcpy(tg.pw_hash, pw_hash, 16); // thread DWORD dwThreadID; HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadUpgrade, &tg, 0, (unsigned*)&dwThreadID); m_prUpgrade.SetRange(0, 60*5); m_prUpgrade.SetPos(0); //m_ProgressNormalMarquee.SetMarquee(true, 30); m_prUpgrade.SetShowPercent(false); start_prog(350); // loop int result = 0; BOOL bExec = TRUE; while(bExec) { PumpMessages(); DWORD hr = WaitForSingleObject(hThread, 50); switch(hr) { case WAIT_OBJECT_0: // success result = tg.result; // 0º¸´Ù Å©¸é ¼º°ø, 0º¸´Ù ÀÛÀ¸¸é ½ÇÆÐ. if(result < 0) result -= 20; bExec = FALSE; break; case WAIT_TIMEOUT: continue; case WAIT_ABANDONED: case WAIT_FAILED: result = -3; bExec = FALSE; break; } } end_prog(); // update ui if(result >= 0) { // ÇÁ·Î±×·¡½º¹Ù ¿¡¹Ä·¹ÀÌ¼Ç int lo, hi; m_prUpgrade.GetRange(lo, hi); for(int i = m_prUpgrade.GetPos(); i < hi; ++i) { set_progress(i); PumpMessages(); Sleep(10); } m_prUpgrade.SetPos(hi); m_prUpgrade.SetWindowText("Success"); m_prUpgrade.RedrawWindow(); PumpMessages(); } else { // result°¡ -20º¸´Ù ÀÛÀ¸¸é Upgrade()ÂÊ ¿À·ùÄÚµå´Ù CString mm; mm.Format("Fail... [ErrorCode : %d]", result); m_prUpgrade.SetWindowText(mm); m_prUpgrade.RedrawWindow(); PumpMessages(); } return result; } void CUpgrade::EnableControl(BOOL bEn) { GetDlgItem(IDC_BUTTON_UG)->EnableWindow(bEn); GetDlgItem(IDC_BUTTON_FIND)->EnableWindow(bEn); //GetDlgItem(IDCANCEL)->EnableWindow(bEn); GetDlgItem(IDOK)->EnableWindow(bEn); GetDlgItem(IDC_RADIO_BOOT)->EnableWindow(bEn); GetDlgItem(IDC_RADIO_FIRM)->EnableWindow(bEn); GetDlgItem(IDC_CHECK_AUTO)->EnableWindow(bEn); } void CUpgrade::OnButtonUg() { // TODO: Add your control notification handler code here //unsigned char data[30] = { 0x00, 0x00, 0x00, 0x00, 0x00,}; // if(!CtrlDevice(theApp.m_info.sIp, 1, data, 5)) // ÀÏ¹Ý Æß¿þ¾î ¾÷±×·¹À̵å´Â º¸È£¸ðµ¨ Áö¿øÀÌÈĺÎÅÍ Áö¿øµÇÁö ¾Ê½À´Ï´Ù. // °í·Î ¿©±â¿¡´Â ÀÎÁõÀÌ ÇÊ¿äÇÏÁö ¾Ê½À´Ï´Ù. - cjy, 07_1203 17:00 char id[1024] = {0,}; char pw_hash[16] = {0,}; // int hr = proc_login(theApp.m_info.FirmwareVersion, theApp.m_info.MAC, id, pw_hash); // if(hr <= 0) // return ; // check file valid int attr = GetFileAttributes(m_sFilePath); if(attr == -1) { CString msg; //msg.Format("The firmware file does not exist : [%s]", m_sFilePath); msg.Format("Firmware file does not exist : [%s]", m_sFilePath); AfxMessageBox(msg); return; } EnableControl(FALSE); if(m_sFilePath != "") { TRACE("%s\n", m_sFilePath); //GetDlgItem(IDC_STATIC_STATE)->SetWindowText("Image Send & Update....... "); //upgrade_flag = UpGrade(theApp.m_info.sIp, m_sFilePath, 0x50); char path[1024]; sprintf(path, m_sFilePath); int flag = 0x50; if(m_iRadio) flag = 0x50; // firmware else flag = 0x60; // bootloader int upgrade_flag = upgrade_thread(theApp.m_info.sIp, path, flag, id, pw_hash); CString msg; if(upgrade_flag > 0) { if(m_iRadio) GetDlgItem(IDC_STATIC_STATE)->SetWindowText("Completed to firmware Update"); else GetDlgItem(IDC_STATIC_STATE)->SetWindowText("Completed to Boot Loader Update"); //if(AfxMessageBox("You need to press 'OK' for restarting NVE/IPC series to apply new configuration, otherwise press ¡°cancel¡±", MB_ICONEXCLAMATION|MB_OKCANCEL)==1) //if(AfxMessageBox("Press OK to restart NVE/IPC series, press Cancel to abort.\nNew configuration will take effect upon system reboot.", if(AfxMessageBox( //"Press Yes to restart NVE/IPC series, press Cancel to abort.\nNew configuration will take effect upon system reboot.", "Would you like to restart the system to apply the new settings?\n" "(The new settings will not be applied until the system restarts.)", MB_ICONEXCLAMATION|MB_YESNO)==IDYES) { // server reboot unsigned char data[7] = "reboot"; Sleep(1000); int hr = CtrlDeviceEx(theApp.m_info.sIp, id, pw_hash, 1, data, 5); if(hr < 0) { Sleep(1000); CtrlDeviceEx(theApp.m_info.sIp, id, pw_hash, 1, data, 5); } } //GetDlgItem(IDC_BUTTON_UG)->EnableWindow(TRUE); //////////GetDlgItem(IDCANCEL)->EnableWindow(TRUE); -- ¾Ê¾¸ //GetDlgItem(IDOK)->EnableWindow(TRUE); OnOK(); } else if(upgrade_flag < -20) { // Upgrade() ipadmintoll.dllÀÇ ¹Ýȯ°ªÀ¸·Î ¿À·ùÀÏ °æ¿ì. msg.Format("NVE Device Error[Error code:%d]", upgrade_flag); GetDlgItem(IDC_STATIC_STATE)->SetWindowText(msg); AfxMessageBox(msg, MB_ICONEXCLAMATION); } else { if(m_iRadio) { msg.Format("Failed to update firmware with [Error code:%d]", upgrade_flag); } else { msg.Format("Failed to update boot loader with [Error code:%d]", upgrade_flag); } GetDlgItem(IDC_STATIC_STATE)->SetWindowText(msg); AfxMessageBox(msg, MB_ICONEXCLAMATION); } } else { AfxMessageBox("Not file select ", MB_ICONEXCLAMATION); } EnableControl(TRUE); } BOOL CUpgrade::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->wParam == VK_RETURN) { return TRUE; } return CDialog::PreTranslateMessage(pMsg); } void CUpgrade::OnRadioBoot() { // TODO: Add your control notification handler code here m_iRadio = 0; //GetDlgItem(IDC_BUTTON_UG)->EnableWindow(FALSE); m_sFilePath = theApp.get_value("PATH", "BootLoader"); GetDlgItem(IDC_STATIC_FILE)->SetWindowText(m_sFilePath); UpdateData(FALSE); } void CUpgrade::OnRadioFirm() { // TODO: Add your control notification handler code here m_iRadio = 1; //GetDlgItem(IDC_BUTTON_UG)->EnableWindow(TRUE); m_sFilePath = theApp.get_fw_path(); GetDlgItem(IDC_STATIC_FILE)->SetWindowText(m_sFilePath); UpdateData(FALSE); } BOOL CUpgrade::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here //m_bFile = FALSE; m_iRadio = 1; m_bAuto = TRUE; iFVersion = 0; /* -- ¸®ºñÁ¯ Á¤º¸·Î üũ¸¦ ÇÏÁö ¾Ê½À´Ï´Ù. -- - cjy, 07_0718 17:03 // get revision num if(strstr(theApp.m_info.Name, "rev")) { char sver[1024]; sprintf(sver, "%s", strstr(theApp.m_info.Name, "rev")); iFVersion = atoi(&sver[3]); } else { warn("Fail to get firmware information\r\nTry it again after clicking \"Refresh\""); return FALSE; } */ // init m_prUpgrade.SetWindowText("Ready..."); m_prUpgrade.RedrawWindow(); UpdateData(); m_edSelectIP = theApp.GetSelectDeviceName(); UpdateData(FALSE); // ±âº» »óÅ´ °æ·Î »óÅÂÀÔ´Ï´Ù. set_file(theApp.get_fw_path(), 0, TRUE, FALSE); ::CheckRadioButton(this->m_hWnd, IDC_RADIO_BOOT, IDC_RADIO_FIRM, IDC_RADIO_FIRM); UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } bool CUpgrade::AutoFindFile() { BROWSEINFO bi; ZeroMemory(&bi, sizeof(bi)); TCHAR szDisplayName[MAX_PATH]; szDisplayName[0] = '\0'; bi.hwndOwner = NULL; bi.pidlRoot = NULL; bi.pszDisplayName = szDisplayName; bi.lpszTitle = TEXT("Æú´õ¸¦ ¼±ÅÃÇÏ¿© ÁÖ¼¼¿ä."); // bi.ulFlags = BIF_EDITBOX | BIF_VALIDATE ; bi.ulFlags = BIF_VALIDATE ; #ifndef SHOW_PROBLEM bi.lpfn = BrowseCallbackProc; #endif bi.lParam = (LPARAM)(LPSTR)(LPCTSTR)m_sFilePath; // CString path; // GetDlgItem(IDC_STATIC_FILE)->GetWindowText(path); // bi.lParam = (LPARAM)(LPSTR)(LPCTSTR)path; bi.iImage = 0; // do~ LPITEMIDLIST pidl = SHBrowseForFolder(&bi); TCHAR szPath[MAX_PATH] = {0}; if(NULL != pidl) SHGetPathFromIDList(pidl, szPath); if(strlen(szPath) < 1) return FALSE; m_sFilePath = szPath; _chdir(m_sFilePath); if(CheckFile()) { //theApp.set_fw_path(m_sFilePath); //m_bFile = TRUE; return TRUE; } else { // m_bFile = FALSE; GetDlgItem(IDC_STATIC_FILE)->SetWindowText("None"); } return FALSE; } int CUpgrade::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM dwData) { CUpgrade* PDlg = (CUpgrade *)AfxGetMainWnd(); TCHAR* szDir; TCHAR szText[MAX_PATH] = {0}; CString str; szDir = (TCHAR * )(LPSTR)(LPCTSTR) dwData; switch(uMsg) { case BFFM_INITIALIZED: ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir); break; case BFFM_SELCHANGED: SHGetPathFromIDList(reinterpret_cast(lParam), szText); ::SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, reinterpret_cast(szText)); break; case BFFM_VALIDATEFAILED: str.Format("\"%s\" Æú´õ °æ·Î´Â À߸øµÇ¾ú½À´Ï´Ù.", reinterpret_cast(lParam)); AfxMessageBox(str); return 1; } return 0; } bool CUpgrade::CheckFile() { //int cnt=0; //int flag=0; //int readCnt=0; int saveImageVersion = 0, nowImageVersion = 0; //char version[4], buf[1024], fileName[100], imageFileName[100], imagInfo[50]; //char buf[1024], fileName[100], imageFileName[100], imagInfo[50]; //char fileName[100]; //, imageFileName[100], imagInfo[50]; CFileFind finder; //FILE *fifo = NULL; BOOL bWorking = finder.FindFile("*"); bWorking = finder.FindNextFile(); while (bWorking) { //if(bWorking > 0 || flag==0) //{ // if(bWorking == 0) // flag=1; CString fileName = finder.GetFileName(); if(!finder.IsDirectory() && fileName!= "." && fileName!= "..") { /* fifo = NULL; if((fifo = fopen(fileName, "rb"))) { readCnt=0; memset(buf, 0x00, sizeof(buf)); if((readCnt = fread(buf, 1, sizeof(buf), fifo))>90) { sprintf(buf, "%s", &buf[32]); if(!strncmp(buf, "Linux image HW", 14)) { if(iFVersion>=30 && buf[16]=='3') { cnt++; TRACE("find %s, %s\n", fileName, &buf[32]); memcpy(version, &buf[20], 3); nowImageVersion = atoi(version); if(saveImageVersion < nowImageVersion) { sprintf(imageFileName, "%s", fileName); sprintf(imagInfo, "%s", buf); } } else if(iFVersion<30 && buf[16]=='1') { cnt++; TRACE("find %s, %s\n", fileName, &buf[32]); memcpy(version, &buf[20], 3); nowImageVersion = atoi(version); if(saveImageVersion < nowImageVersion) { sprintf(imageFileName, "%s", fileName); sprintf(imagInfo, "%s", buf); } } } } fclose(fifo); } */ // firmware path, check version, show warning CString path = finder.GetFilePath(); nowImageVersion = set_file(path, saveImageVersion, FALSE, FALSE); if(nowImageVersion != 0) { saveImageVersion = nowImageVersion; //m_sFilePath = fileName; } //theApp.get_fw_path(); } //} bWorking = finder.FindNextFile(); } //TRACE("find %d\n", cnt); // if(cnt) // { // sprintf(m_sFilePath, "%s", imageFileName); // sprintf(buf, "%s(%s)", imageFileName, imagInfo); // GetDlgItem(IDC_STATIC_FILE)->SetWindowText(buf); // } // else // return FALSE; if(m_sFilePath == "") return FALSE; return TRUE; } void CUpgrade::set_progress(int pos) { m_prUpgrade.SetPos(pos); // persent int low, hi; m_prUpgrade.GetRange(low, hi); CString ss; ss.Format("Completed [%d%%]", int((100.0/hi)*pos)); m_prUpgrade.SetWindowText(ss); m_prUpgrade.RedrawWindow(); } void CUpgrade::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent == 0) { // ½Ã°£Àº ÅäÅ» 5ºÐÀ¸·Î ÇÏ°í~ // 1~70±îÁö°¡ µü °É¸®°Ô ÇÏ°í // ¸¸¾à 70À» ³Ñ±â¸é À̼ø°£ºÎÅÍ ´À¸®°Ô ÇÏÀÚ(1ÃÊ ´ÜÀ§(3¹è)...) // ¸¸¾à 90À» ³Ñ±â¸é À̼ø°£ºÎÅÍ´Â ´õ ´À¸®°Ô(3ÃÊ ´ÜÀ§(10¹è)...) int pt = m_prUpgrade.GetPos()+1; // ¼Óµµ Á¶Àý int low, hi; m_prUpgrade.GetRange(low, hi); int per = (int)((100.0/(hi-low)) * (pt-low)); if(per > 90) { if(m_nTimerGap <= 3000) { modify_prog(3000); } } else if(per > 70) { if(m_nTimerGap <= 1000) { modify_prog(1000); } } set_progress(pt); } CDialog::OnTimer(nIDEvent); }