Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "maingui.h"
  3. #include "cehelper.h"
  4. #include "resource.h"
  5.  
  6. #define BITMAP_PATH     L"\\Storage Card\\images"
  7.  
  8. MainGui::MainGui() : DGui()
  9. {
  10. #ifdef DEBUG
  11.     this->SetDBitmapPath(BITMAP_PATH);
  12. #else
  13.     TCHAR bmpath[MAX_PATH];
  14.     CEHelper::GetExePath(bmpath);
  15.     wcscat_s(bmpath, MAX_PATH, _T("\\images"));
  16.     this->SetDBitmapPath(bmpath);
  17. #endif
  18.  
  19.     InitFont();
  20.  
  21.     // picturebox background
  22.     m_pboxBg = new DPictureBox(PB_BACKGROUND, this->LoadDBitmap(IDB_BACKGROUND));
  23.  
  24.     _tcscpy_s(m_filename, MAX_PATH, _T("custom.bin"));
  25.  
  26.     // set gui position center
  27.     int x, y;
  28.     x = (int)((800 - m_pboxBg->GetWidth())/2);
  29.     y = (int)((480 - m_pboxBg->GetHeight())/2);
  30.     this->SetPosition(x, y);
  31.  
  32.     // set gui size from picturebox
  33.     this->SetSize(m_pboxBg->GetWidth(), m_pboxBg->GetHeight());
  34.  
  35.     // button close
  36.     m_butClose = new DButton(BT_CLOSE);
  37.     m_butClose->SetBitmapNormal(this->LoadDBitmap(IDB_BUTTON_SNORMAL));
  38.     m_butClose->SetBitmapPressed(this->LoadDBitmap(IDB_BUTTON_SPRESSED));
  39.     m_butClose->SetSize(164, 36);
  40.     m_butClose->SetPosition(this->GetWidth() - 10 - m_butClose->GetWidth(),
  41.         this->GetHeight() - 10 - m_butClose->GetHeight());
  42.     m_butClose->SetText(_T("Quitter"), &m_nmhFont, TEXT_WH);
  43.     m_butClose->SetTextAlign(DT_CENTER|DT_VCENTER);
  44.     m_butClose->SetEventMouseUp(this, &MainGui::BtCloseClicked);
  45.  
  46.     // label about
  47.     m_lblAbout = new DLabel(LBL_ABOUT, &m_lthFont, TEXT_WH);
  48.     m_lblAbout->SetText(_T("Djeman 2017 - www.msieurlolo.fr"));
  49.     m_lblAbout->SetRect(10, this->GetHeight() - 22, 350, this->GetHeight() - 10);
  50.     m_lblAbout->SetTextAlign(DT_BOTTOM);
  51.  
  52.     // widget anchor
  53.     m_wgAnchor = new DWidget(WG_ANCHOR);
  54.     m_wgAnchor->SetRect(0, 0, 550, 20);
  55.     m_wgAnchor->SetEnabled(TRUE);
  56.     m_wgAnchor->SetEventMouseMove(m_wgAnchor, &MainGui::WgAnchorMoved);
  57.     m_wgAnchor->SetEventMouseUp(this, &MainGui::WgAnchorClicked);
  58.  
  59.     // label title
  60.     m_lblTitle = new LblTitle(LBL_TITLE, &m_nmhFont);
  61.     m_lblTitle->SetRect(20, 8, 540, 34);
  62.     m_lblTitle->SetText(_T("Flash Boot"));
  63.  
  64.     // button open file
  65.     m_butOpenFile = new DButton(BT_OPENFILE);
  66.     m_butOpenFile->SetBitmapNormal(this->LoadDBitmap(IDB_BUTTON_NORMAL));
  67.     m_butOpenFile->SetBitmapPressed(this->LoadDBitmap(IDB_BUTTON_PRESSED));
  68.     m_butOpenFile->SetSize(148, 100);
  69.     m_butOpenFile->SetPosition(10, 45);
  70.     m_butOpenFile->SetText(_T("Démarrer"), &m_nmhFont, TEXT_WH);
  71.     m_butOpenFile->SetTextAlign(DT_CENTER|DT_VCENTER);
  72.     m_butOpenFile->SetEventMouseUp(this, &MainGui::BtOpenFileClicked);
  73.  
  74.     // groupbox right
  75.     RECT rc = {168,45,540,145};
  76.     m_gbRight = new GbRight(GB_RIGHT, &rc, &m_nmhFont);
  77.  
  78.     this->AddWidget(m_pboxBg);
  79.     this->AddWidget(m_butClose);
  80.     this->AddWidget(m_lblAbout);
  81.     this->AddWidget(m_wgAnchor);
  82.     this->AddWidget(m_lblTitle);
  83.     this->AddWidget(m_gbRight);
  84.  
  85.     this->AddWidget(m_butOpenFile);
  86. }
  87.  
  88. MainGui::~MainGui()
  89. {
  90.     if (m_lthFont) DeleteObject(m_lthFont);
  91.     if (m_nmhFont) DeleteObject(m_nmhFont);
  92. }
  93.  
  94. void MainGui::OnCreate(HWND hWnd)
  95. {
  96.     DGui::OnCreate(hWnd);
  97.  
  98.     SetForegroundWindow(hWnd);
  99. }
  100.  
  101. void MainGui::OnDraw(HDC hDC, RECT* rcPaint)
  102. {
  103.     if (m_wgAnchor->IsPressed())
  104.         return;
  105.  
  106.     DGui::OnDraw(hDC, rcPaint);
  107. }
  108.  
  109. void MainGui::InitFont()
  110. {
  111.     LOGFONT logfont;
  112.     memset (&logfont, 0, sizeof (logfont));
  113.     logfont.lfHeight = 20;
  114.     logfont.lfWidth = 0;
  115.     logfont.lfEscapement = 0;
  116.     logfont.lfOrientation = 0;
  117.     logfont.lfWeight = FW_NORMAL;
  118.     logfont.lfItalic = FALSE;
  119.     logfont.lfUnderline = FALSE;
  120.     logfont.lfStrikeOut = FALSE;
  121.     logfont.lfCharSet = DEFAULT_CHARSET;
  122.     logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  123.     logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  124.     logfont.lfQuality = ANTIALIASED_QUALITY;
  125.     logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  126.     _tcsncpy_s(logfont.lfFaceName, 64, _T("Arial"), LF_FACESIZE);
  127.     logfont.lfFaceName[LF_FACESIZE-1] = _T('\0');  // Ensure null termination
  128.     m_nmhFont = CreateFontIndirect(&logfont);
  129.  
  130.     logfont.lfWeight = FW_NORMAL;
  131.     logfont.lfHeight = 12;
  132.     m_lthFont = CreateFontIndirect(&logfont);
  133. }
  134.  
  135. void MainGui::BtCloseClicked(void* obj, HWND hWnd, POINT pt)
  136. {
  137.     DestroyWindow(hWnd);
  138. }
  139.  
  140. void MainGui::WgAnchorMoved(void* obj, HWND hWnd, POINT pt)
  141. {
  142.     DWidget* wg = (DWidget*)obj;
  143.     if (wg->IsPressed()) {
  144.         RECT rcMainWin;
  145.         int w, h;
  146.  
  147.         GetWindowRect(hWnd, &rcMainWin);
  148.         w = rcMainWin.right - rcMainWin.left;
  149.         h = rcMainWin.bottom - rcMainWin.top;
  150.  
  151.         ClientToScreen(hWnd, &pt);
  152.         MoveWindow(hWnd, pt.x - (w/2), pt.y, w, h, FALSE);
  153.     }
  154. }
  155.  
  156. void MainGui::WgAnchorClicked(void* obj, HWND hWnd, POINT pt)
  157. {
  158.     ((MainGui*)obj)->Refresh();
  159. }
  160.  
  161. void MainGui::BtOpenFileClicked(void* obj, HWND hWnd, POINT pt)
  162. {
  163.     if (!(CEHelper::DoFileOpen(((MainGui*)obj)->GetWinHandle(), ((MainGui*)obj)->m_filename,
  164.         _T("Bootloader (*.bin)\0*.bin\0\0"), FILE_OPEN))) {
  165.         ((MainGui*)obj)->Refresh();
  166.         return;
  167.     }
  168.  
  169.     ((MainGui*)obj)->m_gbRight->InitPg(FALSE);
  170.     ((MainGui*)obj)->SetButtonsEnabled(FALSE);
  171.     ((MainGui*)obj)->SetGbTitle(_T("Processus en cours, veuillez patienter ..."));
  172.     ((MainGui*)obj)->SetGbDesc(_T("J'ai peur :x"));
  173.     ((MainGui*)obj)->Refresh();
  174.  
  175.     CreateThread(NULL, 0, FlashThread, obj, 0, NULL);
  176. }
  177.  
  178. void MainGui::SetGbTitle(const TCHAR* text)
  179. {
  180.     m_gbRight->SetTitle(text);
  181. }
  182.  
  183. void MainGui::SetGbDesc(const TCHAR* text, BOOL iserror)
  184. {
  185.     if (iserror) {
  186.         m_gbRight->SetTitle(_T("Erreur !!!"));
  187.         m_gbRight->InitPg(TRUE);
  188.     }
  189.  
  190.     m_gbRight->SetDesc(text);
  191. }
  192.  
  193. void MainGui::AddPgValue(int value)
  194. {
  195.     m_gbRight->AddPgValue(value);
  196. }
  197.  
  198. void MainGui::SetPgMax(int value)
  199. {
  200.     m_gbRight->SetPgMax(value);
  201. }
  202.  
  203. void MainGui::SetButtonsEnabled(BOOL value)
  204. {
  205.     m_butOpenFile->SetEnabled(value);
  206.     m_butClose->SetEnabled(value);
  207. }
  208.  
  209. TCHAR* MainGui::GetFileName()
  210. {
  211.     return m_filename;
  212. }
  213.  
  214. DWORD WINAPI MainGui::FlashThread(LPVOID lpParameter)
  215. {
  216.     DWORD curAddr, fileSize;
  217.     BOOL res;
  218.     NOR_CONTROL norCtl;
  219.  
  220.     DWORD bytesRet = 0;
  221.     PBYTE buffer = NULL;
  222.     HANDLE hFile = INVALID_HANDLE_VALUE;
  223.     HANDLE hNor = INVALID_HANDLE_VALUE;
  224.  
  225.     MainGui *mg = (MainGui*)lpParameter;
  226.  
  227.     Sleep(100);
  228.  
  229.     hFile = CreateFile(mg->GetFileName(), GENERIC_READ, FILE_SHARE_READ,
  230.         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  231.     if (hFile == INVALID_HANDLE_VALUE) {
  232.         mg->SetGbDesc(_T("Fichier introuvable"), TRUE);
  233.         goto END;
  234.     }
  235.  
  236.     fileSize = GetFileSize(hFile, 0);
  237.    
  238.     mg->SetPgMax(((fileSize >> 16)*2) + 3);
  239.     mg->AddPgValue(1);
  240.     mg->SetGbDesc(_T("Préparation de la NOR"));
  241.  
  242.     hNor = CreateFile(_T("PHM1:"), 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  243.     if (hNor == INVALID_HANDLE_VALUE) {
  244.         mg->SetGbDesc(_T("Echec ouverture NOR"), TRUE);
  245.         goto END;
  246.     }
  247.  
  248.     mg->AddPgValue(1);
  249.  
  250.     memset(&norCtl, 0, sizeof(NOR_CONTROL));
  251.     norCtl.address = 0xBFC00000;
  252.     norCtl.length = fileSize;
  253.  
  254.     res = DeviceIoControl(hNor, IOCTL_NOR_ERASE, &norCtl, sizeof(NOR_CONTROL), 0, 0, &bytesRet, NULL);
  255.     if (!res) {
  256.         mg->SetGbDesc(_T("Echec effacement NOR"), TRUE);
  257.         goto END;
  258.     }
  259.  
  260.     mg->SetGbDesc(_T("Ecriture de l'image"));
  261.     mg->AddPgValue((fileSize >> 16));
  262.  
  263.     curAddr = 0xBFC00000;
  264.     buffer = new BYTE[0x10000];
  265.  
  266.     while (fileSize > 0 && curAddr < (0xBFC00000+0x400000)) {
  267.         memset(buffer, 0, sizeof(buffer));
  268.         res = ReadFile(hFile, buffer, sizeof(buffer), &bytesRet, NULL);
  269.         if (!res || bytesRet < 1) {
  270.             mg->SetGbDesc(_T("Echec lecture fichier"), TRUE);
  271.             goto END;
  272.         }
  273.  
  274.         norCtl.buffer = buffer;
  275.         norCtl.address = curAddr;
  276.         norCtl.length = bytesRet;
  277.  
  278.         res = DeviceIoControl(hNor, IOCTL_NOR_WRITE, &norCtl, sizeof(NOR_CONTROL), 0, 0, &bytesRet, NULL);
  279.         if (!res || bytesRet < 1) {
  280.             mg->SetGbDesc(_T("Echec écriture NOR"), TRUE);
  281.             goto END;
  282.         }
  283.  
  284.         fileSize -= norCtl.length;
  285.         curAddr += norCtl.length;
  286.  
  287.         mg->AddPgValue(1);
  288.     }
  289.  
  290.     mg->SetGbTitle(_T("Flash terminé"));
  291.     mg->SetGbDesc(_T("Yiiihhhaaa Victoire !!!"));
  292.     mg->AddPgValue(1);
  293.  
  294. END:
  295.     if (buffer != NULL)
  296.         delete buffer;
  297.     if (hFile != INVALID_HANDLE_VALUE)
  298.         CloseHandle(hFile);
  299.     if (hNor != INVALID_HANDLE_VALUE)
  300.         CloseHandle(hNor);
  301.  
  302.     mg->SetButtonsEnabled(TRUE);
  303.  
  304.     return 0;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement