WIN32磁盘操作代码
+ -

使用FAT32一键格式化硬盘

2021-09-07 54 0

BOOL CKDiskInitDlg::FormatDisk()
{

    CComboBox* pList =(CComboBox*)GetDlgItem(IDC_COMBO_DISK_LIST);
    int nId = pList->GetCurSel();
    char buff[512];
    sprintf(buff,"\\\\.\\PhysicalDrive%d",nId);
    HANDLE hDevice = CreateFile(buff,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);

    if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive 
    { 
        return FALSE;
    }

#pragma warning(disable:4305)
#pragma warning(disable:4309)
    char mbrbuff[512] = 
    {
            0x33,0xC0,0x8E,0xD0,0xBC,0x00,0x7C,0xFB,0x50,0x07,0x50,0x1F,0xFC,0xBE,0x1B,0x7C,
            0xBF,0x1B,0x06,0x50,0x57,0xB9,0xE5,0x01,0xF3,0xA4,0xCB,0xBD,0xBE,0x07,0xB1,0x04,
            0x38,0x6E,0x00,0x7C,0x09,0x75,0x13,0x83,0xC5,0x10,0xE2,0xF4,0xCD,0x18,0x8B,0xF5,
            0x83,0xC6,0x10,0x49,0x74,0x19,0x38,0x2C,0x74,0xF6,0xA0,0xB5,0x07,0xB4,0x07,0x8B,
            0xF0,0xAC,0x3C,0x00,0x74,0xFC,0xBB,0x07,0x00,0xB4,0x0E,0xCD,0x10,0xEB,0xF2,0x88,
            0x4E,0x10,0xE8,0x46,0x00,0x73,0x2A,0xFE,0x46,0x10,0x80,0x7E,0x04,0x0B,0x74,0x0B,
            0x80,0x7E,0x04,0x0C,0x74,0x05,0xA0,0xB6,0x07,0x75,0xD2,0x80,0x46,0x02,0x06,0x83,
            0x46,0x08,0x06,0x83,0x56,0x0A,0x00,0xE8,0x21,0x00,0x73,0x05,0xA0,0xB6,0x07,0xEB,
            0xBC,0x81,0x3E,0xFE,0x7D,0x55,0xAA,0x74,0x0B,0x80,0x7E,0x10,0x00,0x74,0xC8,0xA0,
            0xB7,0x07,0xEB,0xA9,0x8B,0xFC,0x1E,0x57,0x8B,0xF5,0xCB,0xBF,0x05,0x00,0x8A,0x56,
            0x00,0xB4,0x08,0xCD,0x13,0x72,0x23,0x8A,0xC1,0x24,0x3F,0x98,0x8A,0xDE,0x8A,0xFC,
            0x43,0xF7,0xE3,0x8B,0xD1,0x86,0xD6,0xB1,0x06,0xD2,0xEE,0x42,0xF7,0xE2,0x39,0x56,
            0x0A,0x77,0x23,0x72,0x05,0x39,0x46,0x08,0x73,0x1C,0xB8,0x01,0x02,0xBB,0x00,0x7C,
            0x8B,0x4E,0x02,0x8B,0x56,0x00,0xCD,0x13,0x73,0x51,0x4F,0x74,0x4E,0x32,0xE4,0x8A,
            0x56,0x00,0xCD,0x13,0xEB,0xE4,0x8A,0x56,0x00,0x60,0xBB,0xAA,0x55,0xB4,0x41,0xCD,
            0x13,0x72,0x36,0x81,0xFB,0x55,0xAA,0x75,0x30,0xF6,0xC1,0x01,0x74,0x2B,0x61,0x60,
            0x6A,0x00,0x6A,0x00,0xFF,0x76,0x0A,0xFF,0x76,0x08,0x6A,0x00,0x68,0x00,0x7C,0x6A,
            0x01,0x6A,0x10,0xB4,0x42,0x8B,0xF4,0xCD,0x13,0x61,0x61,0x73,0x0E,0x4F,0x74,0x0B,
            0x32,0xE4,0x8A,0x56,0x00,0xCD,0x13,0xEB,0xD6,0x61,0xF9,0xC3,0x49,0x6E,0x76,0x61,
            0x6C,0x69,0x64,0x20,0x70,0x61,0x72,0x74,0x69,0x74,0x69,0x6F,0x6E,0x20,0x74,0x61,
            0x62,0x6C,0x65,0x00,0x45,0x72,0x72,0x6F,0x72,0x20,0x6C,0x6F,0x61,0x64,0x69,0x6E,
            0x67,0x20,0x6F,0x70,0x65,0x72,0x61,0x74,0x69,0x6E,0x67,0x20,0x73,0x79,0x73,0x74,
            0x65,0x6D,0x00,0x4D,0x69,0x73,0x73,0x69,0x6E,0x67,0x20,0x6F,0x70,0x65,0x72,0x61,
            0x74,0x69,0x6E,0x67,0x20,0x73,0x79,0x73,0x74,0x65,0x6D,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x9F,0x86,0x01,0x00,0x00,0x00,

            0x00,  //0X80  
            0x01,0x01,0x00,
            0x0C,               //0x0b 0xc 均是FAT32
            0xFE,0xFF,0xFF,
            0x3F,0x00,0x00,0x00,
            0x00,0x00,0x80,0x04,

            0x00,
            0xFE,0xFF,0xFF,
            0x0B,
            0xFE,0xFF,0xFF,
            0x3F,0x00,0x80,0x04,
            0x00,0x00,0x00,0x09,

            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x55,0xAA
    };
#pragma warning(default:4305)
#pragma warning(default:4309)

    #pragma pack(1)
    typedef struct _MBR_STRUCT
    {
        UCHAR code[446];
        struct
        {
            UCHAR active;
            UCHAR schl;
            UCHAR shead;
            UCHAR scylinder;
            UCHAR fattype;
            UCHAR echl;
            UCHAR ehead;
            UCHAR ecylinder;
            ULONG startlba;
            ULONG lenlba;
        }fat[4];

        UCHAR signame[2];

    }MBR_STRUCT,*PMBR_STRUCT;
#pragma pack()
    MBR_STRUCT mba;
    memset(&mba,0,sizeof(mba));

    ULONG d;
    DISK_GEOMETRY diskinfo;
    BOOL ok =     DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&diskinfo,sizeof(diskinfo),&d,NULL);
    TRACE("err=%d\n",GetLastError());


    ULONG fat1size = GetDlgItemInt(IDC_STATIC_SIZE1);
    ULONG fat2size = GetDlgItemInt(IDC_STATIC_SIZE2);


    PMBR_STRUCT pMbr = (PMBR_STRUCT)mbrbuff;
    pMbr->fat[0].startlba = 0x3f;
    pMbr->fat[0].lenlba = fat1size * 1024 *1024  / diskinfo.BytesPerSector* 1024;
    pMbr->fat[1].startlba = pMbr->fat[0].startlba  + pMbr->fat[0].lenlba;
    pMbr->fat[1].lenlba =  (ULONG)(GetDiskSize()/diskinfo.BytesPerSector -  pMbr->fat[1].startlba) ;//fat2size * 1024 *1024 / diskinfo.BytesPerSector*1024;

    SetFilePointer(hDevice,0,NULL,FILE_BEGIN);
    WriteFile(hDevice,mbrbuff,512,&d,NULL);


    for(int i=0;i<2;i++)
    {
        FormatPart(hDevice,pMbr->fat[i].startlba,pMbr->fat[i].lenlba,&diskinfo);
    }

    return TRUE;
}

void CKDiskInitDlg::FormatPart(HANDLE h,ULONG nStartSector, ULONG nLenSector,PDISK_GEOMETRY pDiskGeo)
{

    //保留区
    //FAT1 FAT2
    //数据区

    typedef struct _FAT32_STRUCT
    {
        UCHAR buff[512];
    }FAT32_STRUCT,*PFAT32_STRUCT;



    FAT32_STRUCT fat32;
    memset(&fat32,0,sizeof(fat32));


    PCHAR p = (PCHAR)&fat32;


    UCHAR jmpcode[] = {0xEB,0X58,0X90};
    memcpy(&p[0],jmpcode,3);


    char fatname[] ={"MSDOS5.0"};
    memcpy(&p[3],fatname,8);

    //扇区大小 
    USHORT sectorperbytes = (USHORT)(pDiskGeo->BytesPerSector);
    memcpy(&p[0x0b],&sectorperbytes,2);

    //每簇多少个扇区
    UCHAR clusterpersecotr= 0x40; 
    memcpy(&p[0x0d],&clusterpersecotr,1);

    //保留扇区数目
    USHORT reservedsecotr = 0x24;///即到FAT1的扇区个数
    memcpy(&p[0x0e],&reservedsecotr,2);


    //FAT表个数
    UCHAR fattablenum = 2;
    memcpy(&p[0x10],&fattablenum,1);

    //本地磁盘
    UCHAR mediades = 0xf8;
    memcpy(&p[0x15],&mediades,1);

    //每磁道多少个扇区
    USHORT trackpersecotor = (USHORT)(pDiskGeo->SectorsPerTrack);;
    memcpy(&p[0x18],&trackpersecotor,2);

    //磁头数(每柱面多个磁道)
    USHORT headnum = (USHORT)(pDiskGeo->TracksPerCylinder);
    memcpy(&p[0x1a],&headnum,2);

    //分区偏移
    ULONG offsetsector = nStartSector;
    memcpy(&p[0x1c],&offsetsector,4);

    //分区大小
    ULONG fatsyssector = nLenSector;//大小
    memcpy(&p[0x20],&fatsyssector,4);

    //FAT表大小
    ULONG fatsize = nLenSector/2/1024/4; //----------------FAT表大小自适应 1G 256 与[0x0d]0x40有关
    memcpy(&p[0x24],&fatsize,4);

    USHORT workmode = 0x00;
    memcpy(&p[0x28],&workmode,2);


    USHORT version = 0x00;
    memcpy(&p[0x2a],&version,2);


    ULONG startcluster = 2;
    memcpy(&p[0x2c],&startcluster,4);


    USHORT fsinfosector = 0x01;
    memcpy(&p[0x30],&fsinfosector,2);


    USHORT backsecotr = 0x6;//备份位置
    memcpy(&p[0x32],&backsecotr,2);


    UCHAR int13=0x80;
    memcpy(&p[0x40],&int13,1);


    UCHAR bootext=0x29;
    memcpy(&p[0x42],&bootext,1);


    ULONG volumenum = rand();// volume标识
    memcpy(&p[0x43],&volumenum,4);


    UCHAR volumename[11] = {0x4e,0x4f,0x20,0x4e,0x41,0x4d,0x45,0x20,0x20,0x20,0x20};
    memset(volumename,0x20,11);
    memcpy(&p[0x47],volumename,11);

    UCHAR fat[8] = {0x46,0x41,0x54,0x33,0x32,0x20,0x20,0x20};
    memcpy(&p[0x52],fat,8);


    p[0x1fe] = (UCHAR)0x55;
    p[0x1ff] = (UCHAR)0xaa;    


    ULONG d;
    LONGLONG offset = 0;
    //写BOOOT
    offset = (LONGLONG)nStartSector* pDiskGeo->BytesPerSector; //到分区的位置


    LONG high = ((ULONG)(offset>>32));
    SetFilePointer(h,(ULONG)offset,&high,FILE_BEGIN);
    WriteFile(h,p,512,&d,NULL);
    FlushFileBuffers(h);


    //初始化FAT 表内容
    ULONG fatcon[512/4];
    memset(fatcon,0,512);
    fatcon[0] = 0x0FFFFFF8;
    fatcon[1] = 0xFFFFFFFF;
    fatcon[2] = 0x0FFFFFFF;

    //写FAT1
    offset+= reservedsecotr* pDiskGeo->BytesPerSector;
    high = ((ULONG)(offset>>32));
    SetFilePointer(h,(ULONG)offset,&high,FILE_BEGIN);
    WriteFile(h,fatcon,512,&d,NULL);
    FlushFileBuffers(h);

    //写FAT2
    offset+= fatsize*pDiskGeo->BytesPerSector;
    high = ((ULONG)(offset>>32));
    SetFilePointer(h,(ULONG)offset,&high,FILE_BEGIN);
    WriteFile(h,fatcon,512,&d,NULL);
    FlushFileBuffers(h);

}

0 篇笔记 写笔记

使用FAT32一键格式化硬盘
BOOL CKDiskInitDlg::FormatDisk(){ CComboBox* pList =(CComboBox*)GetDlgItem(IDC_COMBO_DISK_LIST); int nId = pList->GetCurSel(); char ......
Windbg 格式化命令.formats
以多种格式显示表达式的值windbg默认为16进制.formats 1000:001> .formats 100Evaluate expression: Hex: 00000000`00000100 Decimal: 256 Octal: 0000000000......
作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!