-
May 4th, 2015, 02:38 PM
#1
Write Raw data to hdd
Hello,
I can access a physical device to read a specific sector in a USB hard drive.
But my question is how can I Write to a specific sector?
Lets say I want to replace what is in a specific sector of a hard drive with a new data.
Can anyone help me get started, tips, hints, where to look?
Thanks.
-
May 5th, 2015, 12:19 AM
#2
Re: Write Raw data to hdd
Here is what I have. The reading part is working by selecting what sector to read, but if I want to write all 0's to one specific sector how do I do that?
I have tried so much but cant figure it out, any tips/help is greatly appreciated.
Thanks.
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
#define FILE_SHARE_VALID_FLAGS 0x00000007
short Sect
(const char *_dsk, // disk to access
char *&_buff, // buffer where sector will be stored
unsigned int _nsect // sector number, starting with 0
)
{
DWORD dwRead;
DWORD dwWrite;
HANDLE hDisk=CreateFile(_dsk,GENERIC_READ,FILE_SHARE_VALID_FLAGS,0,OPEN_EXISTING,0,0);
if(hDisk==INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
{
CloseHandle(hDisk);
cout<<"Incorrect File Handle"<<endl;
return 1;
}
SetFilePointer(hDisk,_nsect*512LL,0,FILE_BEGIN); // which sector to read
ReadFile(hDisk,_buff,512LL,&dwRead,0); // read sector
cout<<"correct File Handle Read"<<endl;
CloseHandle(hDisk);
hDisk=CreateFile(_dsk,GENERIC_WRITE,FILE_SHARE_VALID_FLAGS,0,OPEN_EXISTING,0,0);
if(hDisk==INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
{
CloseHandle(hDisk);
cout<<"Incorrect File Handle"<<endl;
return 1;
}
SetFilePointer(hDisk,2*512LL,0,FILE_BEGIN); // which sector to write
WriteFile(hDisk,_buff,512LL,&dwWrite,NULL); // write sector
cout<<"correct File Handle Write"<<endl;
CloseHandle(hDisk);
return 0;
}
int main()
{
char *dsk="\\\\.\\PhysicalDrive1";
int sector=280; //Sector to read/write
char *buff=new char[512LL];
Sect(dsk,buff,sector);
cout << buff << endl; //To see what is inside the buffer
getchar();
}
-
May 5th, 2015, 01:01 AM
#3
Re: Write Raw data to hdd
Please, use Code tags, not the QUOTE ones!
Victor Nijegorodov
-
May 5th, 2015, 04:18 AM
#4
Re: Write Raw data to hdd
Code:
SetFilePointer(hDisk,2*512LL,0,FILE_BEGIN); // which sector to write
WriteFile(hDisk,_buff,512LL,&dwWrite,NULL); // write sector
Isn't all you need here is to set the contents of _buff to all 0's first? eg
Code:
memset(_buff, 0, 512);
Also you are not checking API return values for failure. ie WriteFile() returns 0 on failure for synchronous write. See https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 5th, 2015, 07:50 AM
#5
Re: Write Raw data to hdd
Can't do this while the disk is mounted. Direct low level write access to disks requires them to be unmounted.
This may or may not work on special volumes like raid stacks.
For "moving" data around for a disk optimizer, Windows provides API's to do this while the disk is in use (though limited to not allowing to move clusters involved with directories, the MFT and the paging file (there's a few other special cases I can't recall off the top of my head)).
Note that depending on the driver, you may have to make sure the buffer you pass is aligned to a proper address (this could require an address multiple of 512, but could also require page (4k) or larger alignment.
Look up the information on the driver.
For regular harddisks, you typically need page (4k) aligned buffers.
Note that simply writing sectors without knowing what you're doing could destroy the hardisk (or rather make it permanently inaccessible from windows) or really mess up the file system.
also zeroing the disk may be totally pointless on a SSD because here the internal hardware of the SSD will reorganise sectors to reduce wear on flash memory, and writing a sector may not actually erase whatver was there, rather, merely move the allocation to another sector leaving the actual data still available (though not directly accessible via the OS) when using specialised tools.
-
May 6th, 2015, 11:43 PM
#6
Re: Write Raw data to hdd
This might be a noobish question, but how can I unmount the drive/disk.
-
May 7th, 2015, 06:52 AM
#7
Re: Write Raw data to hdd
to unmount D, open a command prompt as administrator
MountVol D: /p
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|