0

我想将数据写入 SD 卡的扇区 0 (MBR),而不是 SD 卡中的分区。为此,我尝试打开 SD 卡的物理驱动器,这很成功。但是当我调用 WriteFile() 写入 SD 卡时,它返回错误 5 (ERROR_ACCESS_DENIED)。

以下是我使用的示例代码。有人可以帮我解决我在这段代码中遗漏的内容吗?(我也尝试在管理员模式下执行此代码,但徒劳无功。)

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);
}
4

0 回答 0