BOOL CALLBACK FormatDriveDlg(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { PFORMAT_DRIVE_CONTEXT pContext; switch(uMsg) { case WM_INITDIALOG: InitializeFormatDriveDlg(hwndDlg, (PFORMAT_DRIVE_CONTEXT)lParam); SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)lParam); return TRUE; case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: pContext = (PFORMAT_DRIVE_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER); FormatDrive(hwndDlg, pContext); break; case IDCANCEL: pContext = (PFORMAT_DRIVE_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER); EndDialog(hwndDlg, pContext->Result); break; case 28677: // filesystem combo if (HIWORD(wParam) == CBN_SELENDOK) { pContext = (PFORMAT_DRIVE_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER); InsertDefaultClusterSizeForFs(hwndDlg, pContext); } break; } } return FALSE; }
BOOL DLL_EXPORT DoFormat(FORMAT_INFO & FormatInfo) { BYTE DriveLetter = RetrieveDriveLttr(FormatInfo.btDriveNum,0,FormatInfo.dwStartSec); TCHAR szLabel[12]; memcpy(szLabel,&FormatInfo.Label,11); szLabel[11] = '\0'; if( !memcmp(szLabel,"NO NAME",7)) szLabel[0] = '\0'; return FormatDrive(DriveLetter,szLabel,FormatInfo.dwType,FormatInfo.hWnd); }
BOOL DLL_EXPORT CreatePartition(PPARTITION_ENTRY ppeParEntry, BYTE btHardDisk, DWORD dwFlag, BOOL blIsFormat, PBYTE pLabel, HWND hWnd, PINT pnError) { int nErrorCode; PARTITION_INFORMATION pi,piExtend; DRIVE_LAYOUT DriveLayout; DISK_GEOMETRY dg; int nIndex; BYTE btPartitionType; BOOL bExistExtend; BOOL bNeedResize,bResizeSuccess; LARGE_INTEGER lnExtendStart,lnExtendEnd; char chDriveLetter; char szLabel[12]; // int nLogicalNumber; btHardDisk &= 0x7f; //Get disk geomtry and layout information *pnError = nErrorCode = ERR_PARMAN_PARAM; // 102 indicate false if ( GetDiskGeometry(btHardDisk,&dg)) return FALSE; if ( GetDriveLayout(btHardDisk, &DriveLayout)) return FALSE; //check if usb sigle partition if((DriveLayout.dli.PartitionCount % 4) != 0 ) return FALSE; //translate to partition information memset(&pi,0,sizeof(PARTITION_INFORMATION)); pi.StartingOffset.QuadPart = (__int64)ppeParEntry->StartSector * dg.BytesPerSector; pi.PartitionLength.QuadPart = (__int64)ppeParEntry->SectorsInPartition * dg.BytesPerSector; pi.BootIndicator = ppeParEntry->BootFlag ? 1:0; pi.PartitionType = ppeParEntry->SystemFlag; *pnError = ERR_PARMAN_PARAM; //stat logical drive numbers // nLogicalNumber = 0; // for( nIndex = 4 ; nIndex < DriveLayout.dli.PartitionCount ; nIndex +=4 ) // { // if ( GetPartitionType( &DriveLayout,nIndex) != DISKMAN_PAR_UNUSED ) // nLogicalNumber++; // } //detect if exist extend partition bExistExtend = FALSE; for( nIndex = 0 ; nIndex < 4 ; nIndex++ ) { if ( GetPartitionType( &DriveLayout,nIndex) == DISKMAN_PAR_EXTENDED_MBR ) { bExistExtend = TRUE; memcpy(&piExtend,&DriveLayout.dli.PartitionEntry[nIndex], sizeof(PARTITION_INFORMATION)); lnExtendStart.QuadPart = piExtend.StartingOffset.QuadPart; lnExtendEnd.QuadPart = piExtend.PartitionLength.QuadPart; lnExtendEnd.QuadPart += lnExtendStart.QuadPart - 1; break; } } //do alignment if ( dwFlag == LOGICAL ) { LegalizePartition(&pi,&dg,DISKMAN_PAR_LOGICAL_EMBR); }else LegalizePartition(&pi,&dg,DISKMAN_PAR_PRIMARY_MBR); //create partition bNeedResize = FALSE; bResizeSuccess = TRUE; if( dwFlag == LOGICAL ) { if( bExistExtend ) { //beyond the extend low boundary if( pi.StartingOffset.QuadPart < lnExtendStart.QuadPart ) { //justify the extend head piExtend.StartingOffset.QuadPart = pi.StartingOffset.QuadPart; piExtend.PartitionLength.QuadPart = lnExtendEnd.QuadPart - piExtend.StartingOffset.QuadPart + 1; bNeedResize = TRUE; } //beyond the extend high boundary if( pi.StartingOffset.QuadPart + pi.PartitionLength.QuadPart > lnExtendEnd.QuadPart + 1 ) { //justify the extend end //piExtend.StartingOffset has modified; piExtend.PartitionLength.QuadPart = pi.StartingOffset.QuadPart + pi.PartitionLength.QuadPart - piExtend.StartingOffset.QuadPart; bNeedResize = TRUE; } //if there existing only one logical drive is required to //move to the free space acrross a primary partition, parman //should move the extend with the logical // if( nLogicalNumber == 1 ) // { // if( pi.StartingOffset.QuadPart // < // lnExtendStart.QuadPart ) // // if( pi.StartingOffset.QuadPart + pi.PartitionLength.QuadPart // > // lnExtendEnd.QuadPart + 1 ) // } if ( bNeedResize ) { nErrorCode = ResizePrimary(&DriveLayout,&piExtend,&dg,nIndex); bResizeSuccess = ! nErrorCode; } //resize success create logical if( bResizeSuccess ) nErrorCode = CreateLogical(&DriveLayout,&pi,&dg); }else { //no extend partition, should create one //Backup partition type; btPartitionType = pi.PartitionType; pi.PartitionType = PARTITION_EXTENDED; nErrorCode = CreatePrimary(&DriveLayout,&pi,&dg); if( !nErrorCode ) { pi.PartitionType = btPartitionType; nErrorCode = CreateLogical(&DriveLayout,&pi,&dg); } } }else { // if primary overlap the extend then resize extend partition if( bExistExtend ) { if( IsOverlap(&piExtend,&pi) ) { //try to resize extend partition forward piExtend.PartitionLength.QuadPart = pi.StartingOffset.QuadPart - lnExtendStart.QuadPart; //if length < 0 then length = 0 if( piExtend.PartitionLength.QuadPart < 0 ) piExtend.PartitionLength.QuadPart = 0; //resize forward nErrorCode = ResizePrimary(&DriveLayout,&piExtend,&dg,nIndex); if( nErrorCode != 0 ) { //try to resize backward piExtend.StartingOffset.QuadPart = pi.StartingOffset.QuadPart + pi.PartitionLength.QuadPart; piExtend.PartitionLength.QuadPart = lnExtendEnd.QuadPart - piExtend.StartingOffset.QuadPart + 1; nErrorCode = ResizePrimary(&DriveLayout,&piExtend,&dg,nIndex); bResizeSuccess = ! nErrorCode; } } } if( bResizeSuccess ) nErrorCode = CreatePrimary(&DriveLayout,&pi,&dg); } //create success, write back if( !nErrorCode ) nErrorCode = SetDriveLayout(btHardDisk,&DriveLayout); //format partition if(( blIsFormat )&&( !nErrorCode )) { for( nIndex = 0; nIndex < 100 ; nIndex++ ) { Sleep(500); chDriveLetter = RetrieveDriveLttr(btHardDisk, dwFlag, (DWORD) (pi.StartingOffset.QuadPart / dg.BytesPerSector)); if( (BYTE)chDriveLetter != 0xff ) { chDriveLetter += 0x40; break; } } if ( chDriveLetter >= 'A' && chDriveLetter <= 'Z' ) { memcpy(szLabel,pLabel,11); szLabel[11] = '\0'; //if no name if( !memcmp(szLabel,"NO NAME",7)) szLabel[0] = '\0'; switch( pi.PartitionType & 0x0f) { case 0x01://fat12 FormatDrive(chDriveLetter, szLabel,FORMAT_FAT_16,hWnd); break; case 0x04://fat16(old) case 0x06://fat16(big dos) case 0x0e://fat16(eint13) case 0x0f://fat16(int13) FormatDrive(chDriveLetter, szLabel,FORMAT_FAT_16,hWnd) ||(nErrorCode=ERR_PARMAN_FORMATDRIVE); break; case 0x07://ntfs FormatDrive(chDriveLetter, szLabel,FORMAT_NTFS,hWnd) ||(nErrorCode=ERR_PARMAN_FORMATDRIVE); break; case 0x0b://fat32(int13) case 0x0c://fat32(eint13) FormatDrive(chDriveLetter, szLabel,FORMAT_FAT_32,hWnd) ||(nErrorCode=ERR_PARMAN_FORMATDRIVE); break; default: //unknow type break; } } } *pnError = nErrorCode; return !nErrorCode; }