210 4418 2416启动卡制作

来源:互联网 发布:linux 内存交换 编辑:程序博客网 时间:2024/06/06 02:41
// IROM_Fusing_ToolDlg.cpp : implementation file
//


#include "stdafx.h"
#include "IROM_Fusing_Tool.h"
#include "IROM_Fusing_ToolDlg.h"
#include <WinIoCtl.h>
#include <Dbt.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


#define EFUSE_RESEVED_SECTOR (2)
//#define SDHC_SHIFT_SECTOR (1024) // it's for SDHC card
#define SDHC_SHIFT_SECTOR (0) // it's for some SD/MMC cards that don't need any sector shift.
#define VOLUMEDISKSIZE (sizeof(VOLUME_DISK_EXTENTS))


// CAboutDlg dialog used for App About


class CAboutDlg : public CDialog
{
public:
CAboutDlg();


// Dialog Data
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()




// CIROM_Fusing_ToolDlg dialog






LRESULT CIROM_Fusing_ToolDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
    switch (message)
    {
        //WM_DEVICECHANGE,系统硬件改变发出的系统消息
        case WM_DEVICECHANGE:
        {
            PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)lParam;
            switch(wParam)
            {
                case WM_DEVICECHANGE:
                    break;
                case DBT_DEVICEARRIVAL://DBT_DEVICEARRIVAL,设备检测结束,并且可以使用
                {
OnStart();
                    
                }
                    break;
                case DBT_DEVICEREMOVECOMPLETE://DBT_DEVICEREMOVECOMPLETE,设备卸载或者拔出
                {
                    if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
                    {
                        PDEV_BROADCAST_VOLUME lpdbv =  (PDEV_BROADCAST_VOLUME)lpdb;
                        switch(lpdbv->dbcv_flags)
                        {
                            case 0:                //U盘
                            {
                                CString decDriver;
//                                decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
  //                              TRACE1("检测到U盘:[%s]拔出!\n",decDriver.GetBuffer(0));
                            }
                                break;
                            case DBTF_MEDIA:    //光盘
                                
                                break;
                        }
                    }
                }
                    break;
            }
        }
        break;
    }
    return CDialog::WindowProc(message,wParam,lParam); 
}


CIROM_Fusing_ToolDlg::CIROM_Fusing_ToolDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIROM_Fusing_ToolDlg::IDD, pParent)
, m_strCombo(_T(""))
, m_dwDriveNumber(0)
, m_strDriveSize(_T(""))
, m_strFilePath(_T(""))
, m_dwTotalSector(0)
, m_pbWriteBuffer(NULL)
, m_dwStartWriteSector(0)
, m_strFromSector(_T(""))
, m_strToSector(_T(""))
, m_strTargetDrive(_T(""))
, m_nPhysicalNumber(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON5);
}


void CIROM_Fusing_ToolDlg::DoDataExchange(CDataExchange* pDX)
{
// DDX_CBString(pDX, IDC_COMBO2, m_strCombo);
DDX_Text(pDX, IDC_EDIT1, m_strFilePath);
DDX_Text(pDX, IDC_EDIT7, m_strFilePath2);
DDX_Control(pDX, IDC_COMBO3, m_combox);
DDX_Control(pDX, IDC_EDIT1, m_string);
DDX_Control(pDX, IDC_EDIT5, m_report);
DDX_Control(pDX, IDC_EDIT6, m_drive);
DDX_Control(pDX, IDC_EDIT7, m_Path2);
DDX_Control(pDX, IDC_BUTTON3, m_Button2);
}


BEGIN_MESSAGE_MAP(CIROM_Fusing_ToolDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT3, &CIROM_Fusing_ToolDlg::OnEnChangeEdit3)
ON_BN_CLICKED(ID_BUTTON_START, &CIROM_Fusing_ToolDlg::OnBnClickedButtonStart)
ON_EN_CHANGE(IDC_EDIT_SIZE, &CIROM_Fusing_ToolDlg::OnEnChangeEditSize)
ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CIROM_Fusing_ToolDlg::OnBnClickedButtonBrowse)

// ON_BN_CLICKED(IDC_BUTTON1, &CIROM_Fusing_ToolDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CIROM_Fusing_ToolDlg::OnStart)
ON_CBN_SELCHANGE(IDC_COMBO3, &CIROM_Fusing_ToolDlg::OnCbnSelchangeCombo3)
ON_BN_CLICKED(IDC_BUTTON3, &CIROM_Fusing_ToolDlg::OnBnClickedButton3)
END_MESSAGE_MAP()




// CIROM_Fusing_ToolDlg message handlers


BOOL CIROM_Fusing_ToolDlg::OnInitDialog()
{
CDialog::OnInitDialog();


// Add "About..." menu item to system menu.


// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);


CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon
m_combox.InsertString(0,_T("icool 210"));
m_combox.InsertString(1,_T("icool 2416"));
m_combox.InsertString(2,_T("icool 6410"));
m_combox.InsertString(3,_T("icool 4418"));
m_combox.SetCurSel(0);
// TODO: Add extra initialization here


return TRUE;  // return TRUE  unless you set the focus to a control
}


void CIROM_Fusing_ToolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}


// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.


void CIROM_Fusing_ToolDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting


SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);


// Center icon in client rectangle
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;


// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}


// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CIROM_Fusing_ToolDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}




void CIROM_Fusing_ToolDlg::OnEnChangeEdit3()
{
// TODO:  If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.


// TODO:  Add your control notification handler code here
}


void CIROM_Fusing_ToolDlg::OnCbnSelchangeComboDriveName()
{


}


int cal=0;
void CIROM_Fusing_ToolDlg::OnBnClickedButtonStart()
{
char *pMBRBuffer = NULL;
char *pw=NULL;
wchar_t temp[6];
wchar_t buff[17];
//wchar_t temp[20];
wchar_t sectors[9];
char *a;
char *Buf;
int BufLen;
CString strMBR;
CString strcurent;
m_combox.GetLBText(m_combox.GetCurSel(),strcurent);
char aa[512]=
{
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x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( char((char)(m_dwDriveNumber)+97) )) == -1)
{
m_report.SetWindowTextW(TEXT("Can't Read Physic Drive Name"));
//MessageBox(TEXT("Can't Read Physic Drive Name"),TEXT("ERROR"),NULL);
return ;
}


pMBRBuffer = ReadSector(0,1); // read MBR sector.
pw=ReadSector(1,1); 
if (pMBRBuffer == NULL )
{
m_report.SetWindowTextW(TEXT("Can't Read BR sector from Drvie"));
//MessageBox(TEXT("Can't Read BR sector from Drvie ")+m_strCombo,TEXT("ERROR"),NULL);
return ;
}
else
{
temp[0] = pMBRBuffer[0x52];
temp[1] = pMBRBuffer[0x53];
temp[2] = pMBRBuffer[0x54];
temp[3] = pMBRBuffer[0x55];
temp[4] = pMBRBuffer[0x56];
temp[5] = '\0';


strMBR = temp;


if ( strMBR != "FAT32")
{
m_report.SetWindowTextW(TEXT("This Drive is not formatted as FAT32"));
//MessageBox(TEXT("This Drive is not formatted as FAT32")+strMBR,TEXT("ERROR"),NULL);
exit(-1);
}


}


if (!GetTotalSector())
{
m_report.SetWindowTextW(TEXT("Can not Get Whole Sector size"));
//MessageBox(TEXT("Can not Get Whole Sector size"),TEXT("ERROR"),NULL);
return ;
}
if (!m_fileImageFile.Open(m_strFilePath,CFile::modeReadWrite|CFile::typeBinary))
{
m_report.SetWindowTextW(TEXT("Can not Open the File1"));
//MessageBox(TEXT("Can not Open the ")+m_strFilePath,TEXT("ERROR"),NULL);
return ;
}
if(strcurent==(_T("icool 4418")))
{
if (!m_fileImageFile1.Open(m_strFilePath2,CFile::modeReadWrite|CFile::typeBinary))
{
m_report.SetWindowTextW(TEXT("Can not Open the File2"));
//MessageBox(TEXT("Can not Open the File2")+m_strFilePath2,TEXT("ERROR"),NULL);
return ;
}
m_fileImageFile1.Seek(0,CFile::begin);
}

m_fileImageFile.Seek(0,CFile::begin);

CButton   *   p   =  (CButton   *)GetDlgItem(IDC_CHECK1) ;
CButton   *   p2  =  (CButton   *)GetDlgItem(IDC_CHECK2) ;
CButton   *   p3  =  (CButton   *)GetDlgItem(IDC_CHECK3) ;

if(m_dwTotalSector>15523840/3) //SDHC
    { 
         m_dwStartWriteSector = m_dwTotalSector - EFUSE_RESEVED_SECTOR - 1024 - ((DWORD)m_fileImageFile.GetLength() / 512);


    else   
    { 
          m_dwStartWriteSector = m_dwTotalSector - EFUSE_RESEVED_SECTOR - 0 - ((DWORD)m_fileImageFile.GetLength() / 512);

    } 

if((strcurent==(_T("icool 210")))&&(m_strFilePath.Right(3)==(_T("nb0"))) )
{
m_dwStartWriteSector =65;//65;96
BufLen=16*1024;
Buf=(char *)malloc(BufLen);
memset(Buf, 0x00, BufLen);
m_fileImageFile.Read(Buf,BufLen);
a = Buf + 16;
unsigned long checksum=0;
for(int i = 0; i < BufLen - 16; i++)
checksum += (0x000000FF) & *a++;
a = Buf + 8;
*( (unsigned int *)a ) = checksum;
a = Buf;
if (!WriteSector(1,32,Buf ))
{
//MessageBox(TEXT("Can not Write image file from 1 sertor block"),TEXT("ERROR"),NULL);
m_report.SetWindowTextW(TEXT("erro Can not Write image file from first block"));
//exit(1) ;
return ;
}
free(Buf);
}
else if((strcurent==(_T("icool 210"))))
{
m_dwStartWriteSector =49;
BufLen=8*1024;
Buf=(char *)malloc(BufLen);
memset(Buf, 0x00, BufLen);
m_fileImageFile.Read(Buf,BufLen);


// CString cs2;
//cs2.Format(_T("%c,%c,%c,%c,%c,%c"),Buf[0],Buf[1],Buf[2],Buf[3],Buf[4],Buf[5]);
//MessageBox(cs2);


a = Buf + 16;
unsigned long checksum=0;
for(int i = 0; i < BufLen - 16; i++)
checksum += (0x000000FF) & *a++;
a = Buf + 8;
*( (unsigned int *)a ) = checksum;
a = Buf;
if (!WriteSector(1,16,Buf ))
{
//MessageBox(TEXT("Can not Write image file from 1 sertor"),TEXT("ERROR"),NULL);
m_report.SetWindowTextW(TEXT("Can not Write image file from 1 sertor"));
//exit(1) ;
return ;
}

free(Buf);


}

 if((strcurent==(_T("icool 4418"))))
{
m_dwStartWriteSector =65;
BufLen=16*1024;
Buf=(char *)malloc(BufLen);
memset(Buf, 0x00, BufLen);
m_fileImageFile1.Read(Buf,BufLen);


if (!WriteSector(1,32,Buf ))
{
m_report.SetWindowTextW(TEXT("Can not Write image file from 1 sertor"));
//MessageBox(TEXT("Can not Write image file from 1 sertor"),TEXT("ERROR"),NULL);
//exit(1) ;
}
free(Buf);
m_fileImageFile1.Close();


WriteSector(64,1,aa );
}


//MessageBox(TEXT("4"));
// m_dwStartWriteSector = m_dwTotalSector - EFUSE_RESEVED_SECTOR - SDHC_SHIFT_SECTOR - ((DWORD)m_fileImageFile.GetLength() / 512);
m_fileImageFile.Seek(0,CFile::begin);
m_pbWriteBuffer = (char *)malloc(8*1024);
m_pbWriteBufferTotle = (char *)malloc((UINT)m_fileImageFile.GetLength());


if (m_pbWriteBufferTotle == NULL)
{
m_report.SetWindowTextW(TEXT("Can not allocate write buffer"));
//MessageBox(TEXT("Can not allocate write buffer"),TEXT("ERROR"),NULL);
return ;
}
m_fileImageFile.Read(m_pbWriteBufferTotle,(UINT)m_fileImageFile.GetLength());
//CString cs11;
//cs11.Format(_T("%d,0x%x,0x%x,0x%x,0x%x,0x%x"),m_dwStartWriteSector,m_pbWriteBufferTotle[1],m_pbWriteBufferTotle[2],m_pbWriteBufferTotle[3],m_pbWriteBufferTotle[4],m_pbWriteBufferTotle[5],(UINT)m_fileImageFile.GetLength());
//MessageBox(cs11);
swprintf(sectors,9,L"%d",m_dwStartWriteSector);
m_strFromSector = sectors;
swprintf(sectors,9,L"%d",m_dwStartWriteSector+((DWORD)m_fileImageFile.GetLength() / 512));
//swprintf(sectors,9,L"%d",((DWORD)m_fileImageFile.GetLength() / 512));
m_strToSector = sectors;
m_strTargetDrive = m_strCombo;
UpdateData(FALSE);

if( (!WriteSector(m_dwStartWriteSector, ((UINT)m_fileImageFile.GetLength())/512,m_pbWriteBufferTotle )))
{m_report.SetWindowTextW(TEXT("Can not Write image file to SD card"));
//MessageBox(TEXT("Can not Write image file to SD card"),TEXT("ERROR"),NULL);
//exit(1) ;
}


m_fileImageFile.Close();
CString jj;
m_report.GetWindowTextW(jj);
//MessageBox(TEXT("烧写完成"),TEXT("NOTICE"),NULL);
m_report.SetWindowTextW(jj+TEXT("Write Succed\r\n"));
//m_report.SetDlgItemTextW(1,TEXT("Write Succed"));
return ;
}




int CIROM_Fusing_ToolDlg::ChangeStringDriveNameToDriveNumber(void)
{
DWORD dwReturnValue = TRUE;


UpdateData(TRUE);
m_strCombo="F";
if ( m_strCombo == "D")
m_dwDriveNumber = 3; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "E")
m_dwDriveNumber = 4; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "F")
m_dwDriveNumber = 5; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "G")
m_dwDriveNumber = 6; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "H")
m_dwDriveNumber = 7; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "I")
m_dwDriveNumber = 8; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "J")
m_dwDriveNumber = 9; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "K")
m_dwDriveNumber = 10; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "L")
m_dwDriveNumber = 11; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "M")
m_dwDriveNumber = 12; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "N")
m_dwDriveNumber = 13; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "O")
m_dwDriveNumber = 14; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "P")
m_dwDriveNumber = 15; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "Q")
m_dwDriveNumber = 16; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "R")
m_dwDriveNumber = 17; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "S")
m_dwDriveNumber = 18; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "T")
m_dwDriveNumber = 19; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "U")
m_dwDriveNumber = 20; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "V")
m_dwDriveNumber = 21; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "W")
m_dwDriveNumber = 22; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "X")
m_dwDriveNumber = 23; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "Y")
m_dwDriveNumber = 24; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_strCombo == "Z")
m_dwDriveNumber = 25; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else
dwReturnValue = FALSE;


return dwReturnValue;
}


char * CIROM_Fusing_ToolDlg::ReadSector(int startsector, int numberofsectors)
{
// All msdos data structures must be packed on a 1 byte boundary
#pragma pack (1)
struct
{
DWORD StartingSector ;
WORD NumberOfSectors ;
DWORD pBuffer;
}ControlBlock;
#pragma pack ()


#pragma pack (1)
typedef struct _DIOC_REGISTERS
{
DWORD reg_EBX;
DWORD reg_EDX;
DWORD reg_ECX;
DWORD reg_EAX;
DWORD reg_EDI;
DWORD reg_ESI;
DWORD reg_Flags;
} DIOC_REGISTERS ;
#pragma pack ()


char* buffer = (char*)malloc (512*numberofsectors);
HANDLE hDevice ;
DIOC_REGISTERS reg ;
BOOL  fResult ;
DWORD cb ;


// Creating handle to vwin32.vxd (win 9x)
hDevice = CreateFile ( TEXT("\\\\.\\vwin32"),
0,
0,
NULL,
0,
FILE_FLAG_DELETE_ON_CLOSE,
NULL );


if ( hDevice == INVALID_HANDLE_VALUE )
{
// win NT/2K/XP code
HANDLE hDevice;
DWORD bytesread;


wchar_t _devicename[] = TEXT("\\\\.\\A:");
_devicename[4] += m_dwDriveNumber;


// Creating a handle to disk drive using CreateFile () function ..
hDevice = CreateFile(_devicename,
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);


if (hDevice == INVALID_HANDLE_VALUE)
{
free(buffer);
buffer = NULL;
goto finish;
}


// Setting the pointer to point to the start of the sector we want to read ..
SetFilePointer (hDevice, (startsector*512), NULL, FILE_BEGIN);


if (!ReadFile (hDevice, buffer, 512*numberofsectors, &bytesread, NULL) )
{
free(buffer);
buffer = NULL;
goto finish;
}


}
else
{
// code for win 95/98
ControlBlock.StartingSector = (DWORD)startsector;
ControlBlock.NumberOfSectors = (WORD)numberofsectors ;
ControlBlock.pBuffer = (DWORD)buffer ;


//-----------------------------------------------------------
// SI contains read/write mode flags
// SI=0h for read and SI=1h for write
// CX must be equal to ffffh for
// int 21h's 7305h extention
// DS:BX -> base addr of the
// control block structure
// DL must contain the drive number
// (01h=A:, 02h=B: etc)
//-----------------------------------------------------------


reg.reg_ESI = 0x00 ;
reg.reg_ECX = -1 ;
reg.reg_EBX = (DWORD)(&ControlBlock);
reg.reg_EDX = m_dwDriveNumber+1;
reg.reg_EAX = 0x7305 ;


//  6 == VWIN32_DIOC_DOS_DRIVEINFO
fResult = DeviceIoControl ( hDevice,
6,
&(reg),
sizeof (reg),
&(reg),
sizeof (reg),
&cb,
0);


if (!fResult || (reg.reg_Flags & 0x0001)) return NULL;
}
finish:
CloseHandle(hDevice);
return buffer;
}






void CIROM_Fusing_ToolDlg::OnEnChangeEditSize()
{
// TODO:  If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.


// TODO:  Add your control notification handler code here
}


void CIROM_Fusing_ToolDlg::OnBnClickedButtonBrowse()
{


CFileDialog dlgFileOpen(TRUE,TEXT("*.*"),NULL,OFN_FILEMUSTEXIST,
TEXT("Image(*.*)|*.*||"),NULL);


if (dlgFileOpen.DoModal() == IDOK)
{
m_strFilePath = dlgFileOpen.GetPathName();
UpdateData(FALSE);
}
}


int CIROM_Fusing_ToolDlg::GetTotalSector(void)
{
struct _DISK_GEOMETRY_EX 
{ DISK_GEOMETRY  Geometry;  
LARGE_INTEGER  DiskSize;  
UCHAR  Data[1];
} DiskEX;


wchar_t temp2[20];
HANDLE hDevice;
DWORD bytesReturned;
DWORD totalSector = 0;
DWORD bRet;


wchar_t _devicename[] = TEXT("\\\\.\\A:");
_devicename[4] += m_dwDriveNumber;


// Creating a handle to disk drive using CreateFile () function ..
hDevice = CreateFile(_devicename,
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);


if (hDevice == INVALID_HANDLE_VALUE)
return FALSE;


bRet = DeviceIoControl(
 hDevice,              // handle to device
 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,    // dwIoControlCode
 NULL,                          // lpInBuffer
 0,                             // nInBufferSize
 (LPVOID) &DiskEX,          // output buffer
 sizeof(DiskEX),        // size of output buffer
 (LPDWORD) &bytesReturned ,     // number of bytes returned
 NULL    // OVERLAPPED structure
);


if (bRet == 0)
{
return FALSE;
}


if ( DiskEX.DiskSize.HighPart != 0 )
{
totalSector = ((DiskEX.DiskSize.HighPart) << 23);
}
totalSector |= (DiskEX.DiskSize.LowPart >> 9);




swprintf(temp2,20,L"%d sectors",totalSector);
m_strDriveSize = temp2;
UpdateData(FALSE);
m_dwTotalSector = totalSector;


return TRUE;
}


int CIROM_Fusing_ToolDlg::WriteSector(int startsector, int numberofsectors, char *buffer)
{
wchar_t temp2[20];
LARGE_INTEGER startbytes;
CString StringTemp;


// All msdos data structures must be packed on a 1 byte boundary
#pragma pack (1)
struct
{
DWORD StartingSector ;
WORD NumberOfSectors ;
DWORD pBuffer;
}ControlBlock;
#pragma pack ()


#pragma pack (1)
typedef struct _DIOC_REGISTERS
{
DWORD reg_EBX;
DWORD reg_EDX;
DWORD reg_ECX;
DWORD reg_EAX;
DWORD reg_EDI;
DWORD reg_ESI;
DWORD reg_Flags;
} DIOC_REGISTERS ;
#pragma pack ()


HANDLE hDevice ;
DIOC_REGISTERS reg ;
BOOL  fResult ;
DWORD cb ;


// Creating handle to vwin32.vxd (win 9x)
/*hDevice = CreateFile ( TEXT("\\\\.\\vwin32"),
0,
0,
NULL,
0,
FILE_FLAG_DELETE_ON_CLOSE,
NULL );*/


if ( 1 )
{
// win NT/2K/XP code
//HANDLE hDevice;
DWORD byteswrite;


wchar_t _devicename[30];


if (m_nPhysicalNumber == 0 )
return FALSE;
//m_nPhysicalNumber=6;
swprintf(_devicename,30,TEXT("\\\\.\\PhysicalDrive%d"), m_nPhysicalNumber);
//MessageBox(_devicename);


// Creating a handle to disk drive using CreateFile () function ..
hDevice = CreateFile(_devicename,
GENERIC_READ | GENERIC_WRITE , FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,0 , NULL);


if (hDevice == INVALID_HANDLE_VALUE)
{
// MessageBox(_T("INVALID_HANDLE_VALUE"));
return FALSE;
}


startbytes.LowPart = ((startsector&0x7fffff) << 9);
if (startsector&0xff800000)
startbytes.HighPart = ((startsector&0xff800000)>>23);
else
startbytes.HighPart = 0;


// Setting the pointer to point to the start of the sector we want to read ..
SetFilePointerEx (hDevice, startbytes, NULL, FILE_BEGIN);
//SetFilePointer (hDevice, ((startsector&0x7fffff) << 9), NULL, FILE_BEGIN);


if (!WriteFile (hDevice, buffer, 512*numberofsectors, &byteswrite, NULL) )
return FALSE;


if (byteswrite != (512*numberofsectors))
{
swprintf(temp2,20,L"%d sectors",byteswrite);
StringTemp = temp2;
MessageBox((TEXT("Only copy ")+StringTemp),TEXT("ERROR"),NULL);
return FALSE;
}
}
else
{
// code for win 95/98
ControlBlock.StartingSector = (DWORD)startsector;
ControlBlock.NumberOfSectors = (WORD)numberofsectors ;
ControlBlock.pBuffer = (DWORD)buffer ;


//-----------------------------------------------------------
// SI contains read/write mode flags
// SI=0h for read and SI=1h for write
// CX must be equal to ffffh for
// int 21h's 7305h extention
// DS:BX -> base addr of the
// control block structure
// DL must contain the drive number
// (01h=A:, 02h=B: etc)
//-----------------------------------------------------------


reg.reg_ESI = 0x00 ;
reg.reg_ECX = -1 ;
reg.reg_EBX = (DWORD)(&ControlBlock);
reg.reg_EDX = m_dwDriveNumber+1;
reg.reg_EAX = 0x7305 ;


//  6 == VWIN32_DIOC_DOS_DRIVEINFO
fResult = DeviceIoControl ( hDevice,
6,
&(reg),
sizeof (reg),
&(reg),
sizeof (reg),
&cb,
0);


if (!fResult || (reg.reg_Flags & 0x0001)) return FALSE;
}


CloseHandle(hDevice);
return TRUE;
}


void CIROM_Fusing_ToolDlg::InitMemberVariables(void)
{
m_dwDriveNumber = 0x100; 
m_dwStartWriteSector = 0x0;
m_dwTotalSector = 0;
if (m_pbWriteBuffer != NULL)
{
free(m_pbWriteBuffer);
m_pbWriteBuffer = NULL;
}
m_strCombo.Empty();
m_strDriveSize.Empty();
m_strFilePath.Empty();
m_fileImageFile.Close();
}








int CIROM_Fusing_ToolDlg::GetPhysicalDriveNumber( char cDriveName )
{
    HANDLE hDevice;
    DWORD dwOut;
    BOOL bRet;
    wchar_t vcDriveName[ 40 ];
    VOLUME_DISK_EXTENTS* pstVolumeData;
    int iDiskNumber; 


    // alloc memory
    pstVolumeData = ( VOLUME_DISK_EXTENTS * ) malloc( VOLUMEDISKSIZE );
    if( pstVolumeData == NULL )
    {
        return -1;
    } 
    // 
    swprintf( vcDriveName,7,TEXT("\\\\?\\%c:"), cDriveName );
    // Open Device
    hDevice = CreateFile( vcDriveName, GENERIC_READ, FILE_SHARE_READ |
        FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
    if( hDevice == INVALID_HANDLE_VALUE )
    {
        return -1;
    } 
    // Call Device Io Control
    bRet = DeviceIoControl( hDevice, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
            NULL, 0, pstVolumeData, VOLUMEDISKSIZE, &dwOut, NULL );
    if( bRet == FALSE )
    {
        free( pstVolumeData );
        return -1;
    }
    CloseHandle( hDevice ); 
    // Disk number is lower than 1 is failed.
    if( pstVolumeData->NumberOfDiskExtents < 1 )
    {
        free( pstVolumeData );
        return -1;
    } 
    iDiskNumber = pstVolumeData->Extents[ 0 ].DiskNumber;
    free( pstVolumeData ); 
    return iDiskNumber;



int CIROM_Fusing_ToolDlg::GetSettingFromINIFile(void)
{
HANDLE hFile;
wchar_t buffer[1024];
// wchar_t filename[868];
// wchar_t drivename;
// wchar_t temp2[868];
DWORD bytesread;


hFile = CreateFile( TEXT("irom.ini"), GENERIC_READ, FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );

if (hFile == INVALID_HANDLE_VALUE)
{
if (!CreateINIFile())
{
CloseHandle(hFile);
return FALSE;
}
hFile = CreateFile( TEXT("irom.ini"), GENERIC_READ, FILE_SHARE_READ |
       FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );


if (hFile == INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
return FALSE;
}
}


if(!ReadFile (hFile, buffer, 1024 * sizeof(wchar_t), &bytesread, NULL))
{
CloseHandle(hFile);
return FALSE;
}


if ( buffer[0] != 0xaa || 
buffer[1] != 0xbb || 
buffer[2] != 0xcc || 
buffer[3] != 0xdd || 
buffer[4] != 0xee )
{
CloseHandle(hFile);
return FALSE;
}


if ( buffer[6] == TEXT('\0'))
{
return FALSE;
}


return TRUE;
}


int CIROM_Fusing_ToolDlg::CreateINIFile(void)
{
wchar_t buffer[1024] = {TEXT('\0'),};
DWORD byteswrite;
HANDLE hFile;


buffer[0] = 0xaa;
buffer[1] = 0xbb;
buffer[2] = 0xcc;
buffer[3] = 0xdd;
buffer[4] = 0xff;

hFile = CreateFile( TEXT("irom.ini"), GENERIC_WRITE, FILE_SHARE_READ |
        FILE_SHARE_WRITE, NULL, CREATE_NEW, 0, NULL );


if (hFile == INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
return FALSE;
}


if(!WriteFile (hFile, buffer, 1024 * sizeof(wchar_t), &byteswrite, NULL))
{
CloseHandle(hFile);
return FALSE;
}


CloseHandle(hFile);


return TRUE;
}




void CIROM_Fusing_ToolDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE hPtoken = NULL ;    
//INTER_GetExplorerToken( &hPtoken ) ;   
PROCESS_INFORMATION pi;    
STARTUPINFO si = { sizeof(STARTUPINFO),NULL,_T(""),NULL,0,0,0,0,0,0,0,STARTF_USESHOWWINDOW,0,0,NULL,0,0,0};      
si.wShowWindow = SW_SHOW;   
si.lpDesktop = NULL;   
ZeroMemory( &pi, sizeof(pi) );    
CreateProcessAsUser( hPtoken ,_T("E:\\1\\IROM_Fusing_Tool_src\Release\\IROM_Fusing_Tool.exe"),NULL ,     
NULL,NULL,FALSE , NORMAL_PRIORITY_CLASS| CREATE_NEW_CONSOLE ,NULL,NULL,&si,&pi ) ;   




Sleep(1000);   


CloseHandle( pi.hProcess );   


CloseHandle( pi.hThread );  
}


void CIROM_Fusing_ToolDlg::OnStart()
{
// TODO: 在此添加控件通知处理程序代码
CString a,b;
int i,ii,iii;
char ab='D';
int m_panfu;
CString cs;
for( m_panfu=3;m_panfu<25;m_panfu++)
{
b.Format(_T("%c:\\"),ab);
ii=GetDriveType(b);
if(ii==2)
{
break;
}
ab++;
}


m_dwDriveNumber=m_panfu;


// MessageBox(b);
if ( m_panfu == 3)
a = "D"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu ==4 )
a = "E"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 5)
a = "F"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 6)
a = "G"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu ==7)
a = "H"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 8)
a = "I"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 9)
a = "J"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 10)
a = "K";// driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 11)
a = "L";// driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 12)
a = "M"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 13)
a = "N"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 14)
a = "O";// driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 15)
a = "P"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 16)
a = "Q"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 17)
a = "R"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 18)
a = "S"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 19)
a = "T"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 20)
a = "U"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 21)
a = "V"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 22)
a = "W"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 23)
a = "X"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 24)
a = "Y"; // driver number [0=A, 1=B, 2=C, 3=D ....] 
else if ( m_panfu == 25)
a = "Z";// driver number [0=A, 1=B, 2=C, 3=D ....] 
else
a = "";
cs.Format(_T("%c",m_panfu));

m_drive.SetWindowTextW(b.Left(3));


}


void CIROM_Fusing_ToolDlg::OnCbnSelchangeCombo3()
{
CString strcurent;
m_combox.GetLBText(m_combox.GetCurSel(),strcurent);
if(strcurent==(_T("icool 4418")))
{
// TODO: 在此添加控件通知处理程序代码
GetDlgItem(IDC_EDIT7)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_Nboot)->ShowWindow(SW_SHOW);
}
else
{
GetDlgItem(IDC_EDIT7)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_Nboot)->ShowWindow(SW_HIDE);
}
}


void CIROM_Fusing_ToolDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog dlgFileOpen(TRUE,TEXT("*.*"),NULL,OFN_FILEMUSTEXIST,
TEXT("Image(*.*)|*.*||"),NULL);


if (dlgFileOpen.DoModal() == IDOK)
{
m_strFilePath2 = dlgFileOpen.GetPathName();
UpdateData(FALSE);
}
}
0 0
原创粉丝点击