views:

292

answers:

1

I wrote a disk utility that allowed you to erase whole physical drives. it uses the windows file api, calling :

destFile = CreateFile("\\\\.\\PhysicalDrive1",
    GENERIC_WRITE,  FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, OPEN_EXISTING,createflags, NULL);

and then just calling WriteFile, and making sure you write in multiples of sectors, i.e. 512 bytes.

this worked fine in the past, on XP, and even on the Win7 RC, all you have to do is make sure you are running it as an administrator.

but now I have retail Win7 professional, it doesn't work anymore! the drives still open fine for writing, but calling WriteFile on the successfully opened Drive now fails!

does anyone know why this might be? could it have something to do with opening it with shared flags? this is always what I have done before, and its worked. could it be that something is now sharing the drive? blocking the writes? is there some way to properly "unmount" a drive, or at least the partitions on it so that I would have exclusive access to it?

some other tools that used to work don't any more either, but some do, like the WD Diagnostic's erase functionality. and after it has erased the drive, my tool then works on it too! leading me to believe there is some "unmount" process I need to be doing to the drive first, to free up permission to write to it.

Any ideas?

Update:

the error code returned from WriteFile is '5', ERROR_ACCESS_DENIED but again, if I 'erase' the drive first using WD Diag, I can then access and write to the drive fine. when I initialize the drive again, and give it a partition, I go back to getting the ERROR_ACCESS_DENIED error.

+1  A: 

This is almost certainly related to preventing the attack found against driver signing by changing sectors in the page file. It will prevent writing to the areas of the disk containing partitions.

See http://msdn.microsoft.com/en-us/library/aa365747%28VS.85%29.aspx and look at the information starting with the text "If you write directly to a volume that has a mounted file system", basically you have to lock the disk's volumes in order to write to their sectors.

tyranid
this looks promising, I assume you can't call FSCTL_DISMOUNT_VOLUME on a physical drive, and have to enumerate the Partitions on the drive and call FSCTL_DISMOUNT_VOLUME on all of them. I'm not sure how to get the partitions on a physical drive but I will look into it and try it tomorrow, Thanks.
matt
You can always enumerate all volumes on the system using FindFirstVolume/FindNextVolume, call IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS on each and then check if the volume has any backing storage on the disk in question. (There are some trickier cases in which volumes can be spread across multiple disks, etc.)
Reuben

related questions