// DlgProtectUpdate.cpp : implementation file // #include "stdafx.h" #include "udpadmintool.h" #include "DlgProtectUpdate.h" #include "util_auto.hpp" #include // _beginthread, _endthread #include "util.hpp" #include "login.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "util_firmware.h" ///////////////////////////////////////////////////////////////////////////// // CDlgProtectUpdate dialog CDlgProtectUpdate::CDlgProtectUpdate(CWnd* pParent /*=NULL*/) : CDialog(CDlgProtectUpdate::IDD, pParent) { //{{AFX_DATA_INIT(CDlgProtectUpdate) m_sEdSelectDevice = _T(""); m_sEdTypeMajor = _T(""); m_sEdTypeMinor = _T(""); m_sEdBrowse = _T(""); m_sEdVersion = _T(""); m_sEdCompanyName = _T(""); //}}AFX_DATA_INIT } void CDlgProtectUpdate::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgProtectUpdate) DDX_Control(pDX, IDC_LIST_PACKAGE, m_ctrListPackage); DDX_Control(pDX, IDC_PROGRESS_UP, m_prUpgrade); DDX_Text(pDX, IDC_EDIT_SELECT_DEVICE, m_sEdSelectDevice); DDX_Text(pDX, IDC_EDIT_TYPE_MAJOR, m_sEdTypeMajor); DDX_Text(pDX, IDC_EDIT_TYPE_MINOR, m_sEdTypeMinor); DDX_Text(pDX, IDC_EDIT_BROWSE, m_sEdBrowse); DDX_Text(pDX, IDC_EDIT_VERSION, m_sEdVersion); DDX_Text(pDX, IDC_EDIT_COMPANY_NAME, m_sEdCompanyName); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlgProtectUpdate, CDialog) //{{AFX_MSG_MAP(CDlgProtectUpdate) ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose) ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse) ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgProtectUpdate message handlers void CDlgProtectUpdate::OnButtonClose() { // TODO: Add your control notification handler code here OnOK(); } void CDlgProtectUpdate::OnButtonBrowse() { // TODO: Add your control notification handler code here CString path; GetDlgItem(IDC_EDIT_BROWSE)->GetWindowText(path); char szFilter[200]; sprintf(szFilter, "Protected File(*.enc;*.pkg)|*.enc; *.pkg||"); CFileDialog dlgOpen(TRUE, NULL, path, OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST, szFilter,this); // Open dialog dlgOpen.m_ofn.lpstrTitle = _T("Select file"); // Select and add files if(IDOK == dlgOpen.DoModal()) { CString dlg_path = dlgOpen.GetPathName(); //theApp.set_dome_path(dlg_path); GetDlgItem(IDC_EDIT_BROWSE)->SetWindowText(dlg_path); UpdateData(TRUE); // update information GetDlgItem(IDC_EDIT_BROWSE)->GetWindowText(path); UpdateInfo(path); refresh_package_information(path); } } #include "protect_info.h" void CDlgProtectUpdate::UpdateInfo(CString path) { // todo: #if 0 char major[4+1]; char minor[4+1]; char detail_type[4+1]; // add - cjy, 07_1116 15:20 char version[8+12+1]; // 8ÀÚ¸®¿¡¼­ 20ÀÚ¸®·Î º¯°æ - cjy, 08_0407 17:05 char company_name[16+1]; char dummy[1024]; memset(major, 0, sizeof(major)); memset(minor, 0, sizeof(minor)); memset(detail_type, 0, sizeof(detail_type)); memset(version, 0, sizeof(version)); memset(company_name, 0, sizeof(company_name)); memset(dummy, 0, sizeof(dummy)); FILE* fp = fopen(path, "rb"); if(fp == NULL) { AfxMessageBox("Fail"); return; } auto_file_close _fp(fp); fread(major, 1, 4, fp); fread(minor, 1, 4, fp); fread(detail_type, 1, 4, fp); fread(version, 1, 8, fp); // version field fread(company_name, 1, 16, fp); fread(dummy, 1, 16, fp); // reserved area fread(version+8, 1, 12, fp); // version2 field - cjy, 08_0407 17:06 //major[4] = 0; //minor[4] = 0; //detail_type[4] = 0; //version[8+12] = 0; //company_name[16] = 0; // show UpdateData(); m_sEdTypeMajor = major; m_sEdTypeMinor = minor; m_sEdVersion = version; m_sEdCompanyName = company_name; UpdateData(FALSE); #else CString msg; enc_info info; int hr = get_file_information(path, &info); if(hr <= 0) { msg.Format("Failed to get information from file[%s] with error[%d]", path, hr); AfxMessageBox(msg); return; } UpdateData(); m_sEdTypeMajor = info.major; m_sEdTypeMinor = info.minor; m_sEdVersion = info.version; m_sEdCompanyName = info.company_name; UpdateData(FALSE); #endif // description } struct __thread_protect_upgrade { CString ip; CString file; int flag; // - cjy, 07_1130 17:21 CString id; char pw_hash[16]; int result; }; CDlgProtectUpdate* gdlg; void progress_status(const char* ip, int cur, int total) { TRACE2("Progress %d / %d\n", cur, total); gdlg->set_progress(cur); } unsigned int __stdcall ThreadProtectUpgrade(void* data) { __thread_protect_upgrade* tg; tg = (__thread_protect_upgrade*)data; tg->result = -9980; // do // ¹Ýȯ °ªÀÌ ¾ç¼ö¸é ¼º°ø SetUpgradeCallback(progress_status); int upgrade_flag = UpGradeEx((char*)((const char*)(tg->ip)), tg->id, tg->pw_hash, (char*)((const char*)(tg->file)), tg->flag); SetUpgradeCallback(NULL); tg->result = upgrade_flag; return 1; } void CDlgProtectUpdate::EnableControl(BOOL bEn) { GetDlgItem(IDC_BUTTON_UPDATE)->EnableWindow(bEn); GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(bEn); GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(bEn); GetDlgItem(IDC_CHECK_FORCE)->EnableWindow(bEn); GetDlgItem(IDC_CHECK_FD)->EnableWindow(bEn); // - cjy, 08_0616 14:42 } int CDlgProtectUpdate::upgrade_thread(const char *ip, const char *file, int flag, char* id, char* pw_hash) { __thread_protect_upgrade tg; tg.ip = ip; tg.file = file; tg.flag = flag; tg.id = id; memcpy(tg.pw_hash, pw_hash, 16); // for progress bar gdlg = this; // thread DWORD dwThreadID; HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadProtectUpgrade, &tg, 0, (unsigned*)&dwThreadID); // init progress bar m_prUpgrade.SetRange(0, 100); m_prUpgrade.SetPos(0); //m_prUpgrade.SetShowPercent(false); m_prUpgrade.SetWindowText("Ready..."); m_prUpgrade.RedrawWindow(); PumpMessages(); // 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; } } // update ui CString mm; if(result == 2) { // skipµÈ °æ¿ì´Â ÇÁ·Î±×·¡½º¸¦ ÁøÇà½ÃÅ°Áö ¾Ê´Â´Ù. - cjy, 08_0415 16:52 mm.Format("Skip"); m_prUpgrade.SetWindowText(mm); m_prUpgrade.RedrawWindow(); PumpMessages(); } else 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()ÂÊ ¿À·ùÄÚµå´Ù mm.Format("Fail... [ErrorCode : %d]", result); m_prUpgrade.SetWindowText(mm); m_prUpgrade.RedrawWindow(); PumpMessages(); } return result; } void CDlgProtectUpdate::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(); } #include "message.h" void CDlgProtectUpdate::OnButtonUpdate() { // TODO: Add your control notification handler code here UpdateData(); BOOL bRetry = true; while(bRetry) { bRetry = act_update(); } } BOOL CDlgProtectUpdate::act_update() { // protect update CString ip; CString file; ip = theApp.m_info.sIp; file = m_sEdBrowse; //GetDlgItem(IDC_STATIC_FILE)->GetWindowText(file); if(file == "") { CString msg; msg.Format("Don't exists file name[%s]", file); AfxMessageBox(msg); return FALSE; } // 0xb0 + flag(bits) // 0000 0001 : force update // 0000 0010 : with soft factory default int cmd = 0xb0; // protect_update command int chk_force = ((CButton*)GetDlgItem(IDC_CHECK_FORCE))->GetCheck(); if(chk_force == 1) { cmd = 0xb1; // forced protect update } // ¾÷µ¥ÀÌÆ® ÇÒ¶§ factory default¸¦ °°ÀÌ ÇÑ´Ù. int chk_fd = ((CButton*)GetDlgItem(IDC_CHECK_FD))->GetCheck(); if(chk_fd == 1) { cmd = 0xb2; if(chk_force == 1) { cmd = 0xb3; } } int login = 0; for(login = 0; login <= 3; ++login) { char id[1024]={0,}; char pw_hash[16]={0,}; char out_mac[1024]; // out_macÀº macÁ¤º¸°¡ º¹»çµÇ¾îÁø´Ù. ¸¸¾à infoÀÇ MACÀÌ ºñ¾î ÀÕ´Â °æ¿ì, ÀÎÁõ Áö¿øÄ¿³ÎÀε¥ TCP°¡ MAC Á¤º¸¸¦ Áö¿øÇÏÁö ¾Ê¾Æ¼­ // MAC°ªÀÌ ºñ¾îÀÖ´Â °æ¿ì(K620 ~ K640) proc_login()³»ºÎ¿¡¼­ MAC°ªÀ» ÀÔ·Â&Äõ¸®¸¦ ÇÑ´Ù. ÀÌ°ªÀÌ out_mac¿¡ ´ã°Ü¿Â´Ù. // ÀÌ °ªÀº ¾÷µ¥ÀÌÆ®°¡ ¼º°øÇÏ´Â °æ¿ì infoÀÇ MAC¿¡ ä¿öÁø´Ù. // (ÀÌ ÀýÂ÷´Â ¹öÁ¯ÀÌ °°¾Æ¼­ ½ºÅµµÇ´Â °æ¿ì ´Ù½Ã ½ÃµµÇÒ¶§ ¶Ç MAC°ªÀ» ÀÔ·ÂÇÏÁö ¾Êµµ·Ï Çϱâ À§Çؼ­ ÀúÀåÇسõ´Â´Ù.) int hr = proc_login(theApp.m_info.FirmwareVersion, theApp.m_info.MAC, theApp.GetSelectDeviceName(), theApp.m_info.sIp, id, pw_hash, out_mac); if(hr <= 0) { return FALSE; } // do // ¹Ýȯ°ªÀÌ ¾ç¼ö¸é ¼º°ø EnableControl(false); // add id, pw_hash - cjy, 07_1130 14:42 hr = upgrade_thread(ip, file, cmd, id, pw_hash); EnableControl(true); switch(hr) { case -1042: // access level error AfxMessageBox(MSG_LOGIN_ACCESS_LEVEL); theApp.clear_login_info(); continue; case -1043: // not match pw AfxMessageBox(MSG_LOGIN_NOT_MATCH_PW); theApp.clear_login_info(); continue; case -1061: // not exists user id AfxMessageBox(MSG_LOGIN_NOT_EXISTS_USER); theApp.clear_login_info(); continue; } // log show CString msg; if(hr <= 0) { switch(hr) { case -9: msg.Format("The device doesn't support to protect update"); break; default: msg.Format("Failed to protect update with [Error code:%d]", hr); break; } AfxMessageBox(msg, MB_OK | MB_ICONEXCLAMATION); } else { // ¼º°øÇÑ °æ¿ì out_macÀº ¹«Á¶°Ç ºó ¹®ÀÚ¿­ÀÌ ¾Æ´Ï´Ù. - cjy, 08_0506 11:40 ASSERT(strcmp(out_mac, "") != 0); if(strcmp(theApp.m_info.MAC, "") == 0) { // ¹öÁ¯ÀÌ °°¾Æ¼­ ´Ù½Ã ½ÃµµÇÏ´Â °æ¿ì ¹®Á¦°¡ ¾øµµ·Ï Çϱâ À§Çؼ­ ÀúÀåÇسõ´Â´Ù. strcpy(theApp.m_info.MAC, out_mac); } // other if(hr == 1) { // Á¤»óÀûÀÎ ¼öÇà msg.Format("Protect update completed"); AfxMessageBox(msg, MB_OK | MB_ICONINFORMATION); } else if(hr == 2) { //msg.Format("As the same or higher version are already installed, update has failed.\n" // "If you still proceed to update, check the [Force update] box and retry."); msg.Format("Same version or the higher version is already installed. Do you want to update?"); int ret = AfxMessageBox(msg, MB_YESNO | MB_ICONINFORMATION); if(ret == IDYES) { ((CButton*)GetDlgItem(IDC_CHECK_FORCE))->SetCheck(1); return TRUE; // ´Ù½Ã ½ÃµµÇϵµ·Ï ÇÑ´Ù. } } else { // generally success msg.Format("Protect update completed with [%d] code", hr); AfxMessageBox(msg, MB_OK | MB_ICONINFORMATION); } // ÀÌÁ¦´Â ¹«Á¶°Ç ÀçºÎÆà ¿©ºÎ¸¦ ¹¯µµ·Ï ÇÕ´Ï´Ù. // if(m_sEdTypeMajor == "FRM0" || m_sEdTypeMajor == "WEB0" || m_sEdTypeMajor == "PKG0") { if(AfxMessageBox( "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); } } // ⠴ݱâ OnOK(); } } // success break; } // login loop //m_richLog. return FALSE; } BOOL CDlgProtectUpdate::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here UpdateData(); m_sEdSelectDevice = theApp.GetSelectDeviceName(); UpdateData(FALSE); // init progress bar m_prUpgrade.SetRange(0, 100); m_prUpgrade.SetPos(0); m_prUpgrade.SetShowPercent(false); m_prUpgrade.SetWindowText("Ready..."); m_prUpgrade.RedrawWindow(); // ctrl list package init_package_info_column(); // remove x button, - cjy, 08_0410 15:42 HMENU xbtn; xbtn = ::GetSystemMenu(this->m_hWnd, FALSE); ::RemoveMenu(xbtn, SC_CLOSE, MF_BYCOMMAND); // Kernel ¹öÁ¯ÀÌ 641ÀÌ»óÀ̸é Áö¿øÇÑ´Ù. - cjy, 08_0613 15:34 m_bDisableFd = FALSE; int kernel_ver = get_firm_version(theApp.m_info.FirmwareVersion); if(kernel_ver < 641) { m_tip_fd.Create(this); m_tip_fd.AddTool(GetDlgItem(IDC_CHECK_FD), _T("This function is supported since K641 version"), NULL); GetDlgItem(IDC_CHECK_FD)->EnableWindow(FALSE); m_bDisableFd = TRUE; } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL CDlgProtectUpdate::PreTranslateMessage(MSG* pMsg) { if(m_bDisableFd) { m_tip_fd.RelayEvent(pMsg); } return CDialog::PreTranslateMessage(pMsg); } int CDlgProtectUpdate::init_package_info_column() { LV_COLUMN lvcolumn; int i; for(i = 0; i < 3; i++) { lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER; lvcolumn.fmt = LVCFMT_LEFT; lvcolumn.iSubItem = i; lvcolumn.iOrder = i; lvcolumn.pszText = ""; lvcolumn.cx = 50; switch(i) { case 0: lvcolumn.cx = 70; lvcolumn.pszText = "Type"; break; case 1: lvcolumn.cx = 70; lvcolumn.pszText = "Attribute"; break; case 2: lvcolumn.cx = 130; lvcolumn.pszText = "Version"; break; default: ASSERT(!"Invalid column number"); } m_ctrListPackage.InsertColumn(i, &lvcolumn); } return 1; } int CDlgProtectUpdate::enable_package_info(BOOL bEnable) { m_ctrListPackage.EnableWindow(bEnable); return 1; } // 0: not package file int CDlgProtectUpdate::refresh_package_information(const char* path) { // clear m_ctrListPackage.DeleteAllItems(); // is package file? int len = strlen(path); if(len <= 4) return 0; if(strcmp(path+len-4, ".pkg") != 0) return 0; // get package file information // load file FILE* fp = fopen(path, "rb"); fseek(fp, 0, SEEK_END); int file_size = ftell(fp); fseek(fp, 0, SEEK_SET); char* file_data = new char[file_size]; fread(file_data, 1, file_size, fp); fclose(fp); enc_info info; // seek main information area int hr; int pp = 0; pp += 4+4+128+4; // major, minor, information, size int full_data_size = *((int*)(file_data+pp)); pp += 4; int file_count = *((int*)(file_data+pp)); pp += 4; int file_num = 0; for(; pp < file_size && file_num < file_count; file_num++) { int data_size = *((int*)(file_data+pp)); pp += 4; hr = get_file_information_from_memory(file_data+pp, &info); add_sub_data(&info); pp += data_size; } ASSERT(pp == file_size-16-16-128); // plain_hash(16), vid(128), enc_hash(16) ASSERT(file_num == file_count); return 1; } void CDlgProtectUpdate::add_sub_data(enc_info* info) { // add item LV_ITEM lvitem; lvitem.iItem = m_ctrListPackage.GetItemCount(); lvitem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_INDENT|LVIF_PARAM; lvitem.iSubItem = 0; lvitem.iImage = -1; lvitem.stateMask = LVIS_STATEIMAGEMASK; lvitem.state = INDEXTOSTATEIMAGEMASK(1); // content lvitem.lParam = NULL; //(long)(newItem); // save index - cjy, 07_0402 16:16 lvitem.pszText = (LPSTR)(LPCTSTR)info->major; // add m_ctrListPackage.InsertItem(&lvitem); m_ctrListPackage.SetItemText(lvitem.iItem, 1, info->minor); m_ctrListPackage.SetItemText(lvitem.iItem, 2, info->version); }