Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "maingui.h"
- #include "cehelper.h"
- #include "resource.h"
- #define BITMAP_PATH L"\\Storage Card\\images"
- MainGui::MainGui() : DGui()
- {
- #ifdef DEBUG
- this->SetDBitmapPath(BITMAP_PATH);
- #else
- TCHAR bmpath[MAX_PATH];
- CEHelper::GetExePath(bmpath);
- wcscat_s(bmpath, MAX_PATH, _T("\\images"));
- this->SetDBitmapPath(bmpath);
- #endif
- InitFont();
- // picturebox background
- m_pboxBg = new DPictureBox(PB_BACKGROUND, this->LoadDBitmap(IDB_BACKGROUND));
- _tcscpy_s(m_filename, MAX_PATH, _T("custom.bin"));
- // set gui position center
- int x, y;
- x = (int)((800 - m_pboxBg->GetWidth())/2);
- y = (int)((480 - m_pboxBg->GetHeight())/2);
- this->SetPosition(x, y);
- // set gui size from picturebox
- this->SetSize(m_pboxBg->GetWidth(), m_pboxBg->GetHeight());
- // button close
- m_butClose = new DButton(BT_CLOSE);
- m_butClose->SetBitmapNormal(this->LoadDBitmap(IDB_BUTTON_SNORMAL));
- m_butClose->SetBitmapPressed(this->LoadDBitmap(IDB_BUTTON_SPRESSED));
- m_butClose->SetSize(164, 36);
- m_butClose->SetPosition(this->GetWidth() - 10 - m_butClose->GetWidth(),
- this->GetHeight() - 10 - m_butClose->GetHeight());
- m_butClose->SetText(_T("Quitter"), &m_nmhFont, TEXT_WH);
- m_butClose->SetTextAlign(DT_CENTER|DT_VCENTER);
- m_butClose->SetEventMouseUp(this, &MainGui::BtCloseClicked);
- // label about
- m_lblAbout = new DLabel(LBL_ABOUT, &m_lthFont, TEXT_WH);
- m_lblAbout->SetText(_T("Djeman 2017 - www.msieurlolo.fr"));
- m_lblAbout->SetRect(10, this->GetHeight() - 22, 350, this->GetHeight() - 10);
- m_lblAbout->SetTextAlign(DT_BOTTOM);
- // widget anchor
- m_wgAnchor = new DWidget(WG_ANCHOR);
- m_wgAnchor->SetRect(0, 0, 550, 20);
- m_wgAnchor->SetEnabled(TRUE);
- m_wgAnchor->SetEventMouseMove(m_wgAnchor, &MainGui::WgAnchorMoved);
- m_wgAnchor->SetEventMouseUp(this, &MainGui::WgAnchorClicked);
- // label title
- m_lblTitle = new LblTitle(LBL_TITLE, &m_nmhFont);
- m_lblTitle->SetRect(20, 8, 540, 34);
- m_lblTitle->SetText(_T("Flash Boot"));
- // button open file
- m_butOpenFile = new DButton(BT_OPENFILE);
- m_butOpenFile->SetBitmapNormal(this->LoadDBitmap(IDB_BUTTON_NORMAL));
- m_butOpenFile->SetBitmapPressed(this->LoadDBitmap(IDB_BUTTON_PRESSED));
- m_butOpenFile->SetSize(148, 100);
- m_butOpenFile->SetPosition(10, 45);
- m_butOpenFile->SetText(_T("Démarrer"), &m_nmhFont, TEXT_WH);
- m_butOpenFile->SetTextAlign(DT_CENTER|DT_VCENTER);
- m_butOpenFile->SetEventMouseUp(this, &MainGui::BtOpenFileClicked);
- // groupbox right
- RECT rc = {168,45,540,145};
- m_gbRight = new GbRight(GB_RIGHT, &rc, &m_nmhFont);
- this->AddWidget(m_pboxBg);
- this->AddWidget(m_butClose);
- this->AddWidget(m_lblAbout);
- this->AddWidget(m_wgAnchor);
- this->AddWidget(m_lblTitle);
- this->AddWidget(m_gbRight);
- this->AddWidget(m_butOpenFile);
- }
- MainGui::~MainGui()
- {
- if (m_lthFont) DeleteObject(m_lthFont);
- if (m_nmhFont) DeleteObject(m_nmhFont);
- }
- void MainGui::OnCreate(HWND hWnd)
- {
- DGui::OnCreate(hWnd);
- SetForegroundWindow(hWnd);
- }
- void MainGui::OnDraw(HDC hDC, RECT* rcPaint)
- {
- if (m_wgAnchor->IsPressed())
- return;
- DGui::OnDraw(hDC, rcPaint);
- }
- void MainGui::InitFont()
- {
- LOGFONT logfont;
- memset (&logfont, 0, sizeof (logfont));
- logfont.lfHeight = 20;
- logfont.lfWidth = 0;
- logfont.lfEscapement = 0;
- logfont.lfOrientation = 0;
- logfont.lfWeight = FW_NORMAL;
- logfont.lfItalic = FALSE;
- logfont.lfUnderline = FALSE;
- logfont.lfStrikeOut = FALSE;
- logfont.lfCharSet = DEFAULT_CHARSET;
- logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
- logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
- logfont.lfQuality = ANTIALIASED_QUALITY;
- logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
- _tcsncpy_s(logfont.lfFaceName, 64, _T("Arial"), LF_FACESIZE);
- logfont.lfFaceName[LF_FACESIZE-1] = _T('\0'); // Ensure null termination
- m_nmhFont = CreateFontIndirect(&logfont);
- logfont.lfWeight = FW_NORMAL;
- logfont.lfHeight = 12;
- m_lthFont = CreateFontIndirect(&logfont);
- }
- void MainGui::BtCloseClicked(void* obj, HWND hWnd, POINT pt)
- {
- DestroyWindow(hWnd);
- }
- void MainGui::WgAnchorMoved(void* obj, HWND hWnd, POINT pt)
- {
- DWidget* wg = (DWidget*)obj;
- if (wg->IsPressed()) {
- RECT rcMainWin;
- int w, h;
- GetWindowRect(hWnd, &rcMainWin);
- w = rcMainWin.right - rcMainWin.left;
- h = rcMainWin.bottom - rcMainWin.top;
- ClientToScreen(hWnd, &pt);
- MoveWindow(hWnd, pt.x - (w/2), pt.y, w, h, FALSE);
- }
- }
- void MainGui::WgAnchorClicked(void* obj, HWND hWnd, POINT pt)
- {
- ((MainGui*)obj)->Refresh();
- }
- void MainGui::BtOpenFileClicked(void* obj, HWND hWnd, POINT pt)
- {
- if (!(CEHelper::DoFileOpen(((MainGui*)obj)->GetWinHandle(), ((MainGui*)obj)->m_filename,
- _T("Bootloader (*.bin)\0*.bin\0\0"), FILE_OPEN))) {
- ((MainGui*)obj)->Refresh();
- return;
- }
- ((MainGui*)obj)->m_gbRight->InitPg(FALSE);
- ((MainGui*)obj)->SetButtonsEnabled(FALSE);
- ((MainGui*)obj)->SetGbTitle(_T("Processus en cours, veuillez patienter ..."));
- ((MainGui*)obj)->SetGbDesc(_T("J'ai peur :x"));
- ((MainGui*)obj)->Refresh();
- CreateThread(NULL, 0, FlashThread, obj, 0, NULL);
- }
- void MainGui::SetGbTitle(const TCHAR* text)
- {
- m_gbRight->SetTitle(text);
- }
- void MainGui::SetGbDesc(const TCHAR* text, BOOL iserror)
- {
- if (iserror) {
- m_gbRight->SetTitle(_T("Erreur !!!"));
- m_gbRight->InitPg(TRUE);
- }
- m_gbRight->SetDesc(text);
- }
- void MainGui::AddPgValue(int value)
- {
- m_gbRight->AddPgValue(value);
- }
- void MainGui::SetPgMax(int value)
- {
- m_gbRight->SetPgMax(value);
- }
- void MainGui::SetButtonsEnabled(BOOL value)
- {
- m_butOpenFile->SetEnabled(value);
- m_butClose->SetEnabled(value);
- }
- TCHAR* MainGui::GetFileName()
- {
- return m_filename;
- }
- DWORD WINAPI MainGui::FlashThread(LPVOID lpParameter)
- {
- DWORD curAddr, fileSize;
- BOOL res;
- NOR_CONTROL norCtl;
- DWORD bytesRet = 0;
- PBYTE buffer = NULL;
- HANDLE hFile = INVALID_HANDLE_VALUE;
- HANDLE hNor = INVALID_HANDLE_VALUE;
- MainGui *mg = (MainGui*)lpParameter;
- Sleep(100);
- hFile = CreateFile(mg->GetFileName(), GENERIC_READ, FILE_SHARE_READ,
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- mg->SetGbDesc(_T("Fichier introuvable"), TRUE);
- goto END;
- }
- fileSize = GetFileSize(hFile, 0);
- mg->SetPgMax(((fileSize >> 16)*2) + 3);
- mg->AddPgValue(1);
- mg->SetGbDesc(_T("Préparation de la NOR"));
- hNor = CreateFile(_T("PHM1:"), 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hNor == INVALID_HANDLE_VALUE) {
- mg->SetGbDesc(_T("Echec ouverture NOR"), TRUE);
- goto END;
- }
- mg->AddPgValue(1);
- memset(&norCtl, 0, sizeof(NOR_CONTROL));
- norCtl.address = 0xBFC00000;
- norCtl.length = fileSize;
- res = DeviceIoControl(hNor, IOCTL_NOR_ERASE, &norCtl, sizeof(NOR_CONTROL), 0, 0, &bytesRet, NULL);
- if (!res) {
- mg->SetGbDesc(_T("Echec effacement NOR"), TRUE);
- goto END;
- }
- mg->SetGbDesc(_T("Ecriture de l'image"));
- mg->AddPgValue((fileSize >> 16));
- curAddr = 0xBFC00000;
- buffer = new BYTE[0x10000];
- while (fileSize > 0 && curAddr < (0xBFC00000+0x400000)) {
- memset(buffer, 0, sizeof(buffer));
- res = ReadFile(hFile, buffer, sizeof(buffer), &bytesRet, NULL);
- if (!res || bytesRet < 1) {
- mg->SetGbDesc(_T("Echec lecture fichier"), TRUE);
- goto END;
- }
- norCtl.buffer = buffer;
- norCtl.address = curAddr;
- norCtl.length = bytesRet;
- res = DeviceIoControl(hNor, IOCTL_NOR_WRITE, &norCtl, sizeof(NOR_CONTROL), 0, 0, &bytesRet, NULL);
- if (!res || bytesRet < 1) {
- mg->SetGbDesc(_T("Echec écriture NOR"), TRUE);
- goto END;
- }
- fileSize -= norCtl.length;
- curAddr += norCtl.length;
- mg->AddPgValue(1);
- }
- mg->SetGbTitle(_T("Flash terminé"));
- mg->SetGbDesc(_T("Yiiihhhaaa Victoire !!!"));
- mg->AddPgValue(1);
- END:
- if (buffer != NULL)
- delete buffer;
- if (hFile != INVALID_HANDLE_VALUE)
- CloseHandle(hFile);
- if (hNor != INVALID_HANDLE_VALUE)
- CloseHandle(hNor);
- mg->SetButtonsEnabled(TRUE);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement