How to write to sector 0 (MBR) of SD card?

SkyKid 1 Reputation point
2021-03-07T18:13:54.467+00:00

I want to write data to sector 0 (MBR) of the SD card, not a partition in the SD card.
To do that, I tried to open the physical drive for the SD card, which was successful.
But when I called WriteFile() to write to the SD card, it returned Error 5 (ERROR_ACCESS_DENIED).

The following is the sample code that I used.
Can somebody help me with what I missed in this code?

int sd_card_open(char *drive_name) 
{
    char   dev_name[64];
    BOOL   bResult;
    HANDLE hDevice;
    DWORD  BytesReturned;
    VOLUME_DISK_EXTENTS vde;

    // drive_name example : "D:"

    sprintf(dev_name, "\\\\.\\%s", drive_name);
    hDevice = CreateFile(dev_name,                              // Drive to open
                         GENERIC_READ | GENERIC_WRITE,          // Access to the drive
                         FILE_SHARE_READ | FILE_SHARE_WRITE,    // Share mode
                         NULL,                                  // Security
                         OPEN_EXISTING,                         // Disposition
                         0,                                     // no file attributes
                         NULL);

    if (hDevice == INVALID_HANDLE_VALUE) {
        printf("Can't open the drive %s (err = %d).\n", drive_name, GetLastError());
        return(-1);
    }

    bResult = DeviceIoControl(hDevice, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, 
                              NULL, 0, &vde, sizeof(vde), &BytesReturned, NULL);
    if (!bResult) {
        printf("Failed to get physical disk name (err = %d).\n", GetLastError());
        return(-1);
    }

    CloseHandle(hDevice);
    sprintf(dev_name, "\\\\.\\PhysicalDrive%d", vde.Extents[0].DiskNumber);

    hDevice = CreateFile(dev_name,                              // Drive to open
                         GENERIC_READ | GENERIC_WRITE,          // Access to the drive
                         FILE_SHARE_READ | FILE_SHARE_WRITE,    // Share mode
                         NULL,                                  // Security
                         OPEN_EXISTING,                         // Disposition
                         0,                                     // no file attributes
                         NULL);

    if (hDevice == INVALID_HANDLE_VALUE) {
        printf("Can't open the disk %s (err = %d).\n", dev_name, GetLastError());
        return(-1);
    }

    bResult = DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, 
                              NULL, 0, NULL, 0, &BytesReturned, NULL);
    if (!bResult) {
        printf("Failed to lock volume (err = %d).\n", GetLastError());
        return(-1);
    }

    bResult = DeviceIoControl(hDevice, FSCTL_DISMOUNT_VOLUME, 
                              NULL, 0, NULL, 0, &BytesReturned, NULL);
    if (!bResult) {
        printf("Failed to dismount volume (err = %d).\n", GetLastError());
        return(-1);
    }

    return(0);
}

int sd_card_write(unsigned int sec, unsigned int num_secs, unsigned char *buf)
{
    LARGE_INTEGER pos;
    DWORD         cnt, wcnt;
    BOOL          bResult;

    pos.QuadPart = ((LONGLONG) sec)   << 9;     // assume 512-byte sector
    cnt          = ((DWORD) num_secs) << 9;

    bResult = SetFilePointerEx(hDevice, pos, NULL, FILE_BEGIN);
    if (!bResult) {
        printf("SetFilePointerEx() failed (err = %d).\n", GetLastError());
        return(-1);
    }

    bResult = WriteFile(hDevice, buf, cnt, &wcnt, NULL);
    if (!bResult || cnt != wcnt) {
        printf("WriteFile() failed (err = %d).\n", GetLastError());
        return(-1);
    }

    return(0);
}
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,428 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Song Zhu - MSFT 906 Reputation points
    2021-03-08T03:25:12.2+00:00

    I use this code to work normally, I think it is your disk settings that caused you to have this error, you can refer to: How to WriteFile to a PhysicalDrive (Windows 7) without getting ERROR_ACCESS_DENIED? and Writing USB Disk Sectors Results in Permission Error 5

    0 comments No comments