Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Direct access to physical drives in Win2k question

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 05-24-04, 01:06 AM Thread Starter   #1
lex57ukr
Registered



Join Date: Feb 2004
Location: Orem, UT

 
Exclamation Direct access to physical drives in Win2k question


Greetings everyone!

I wonder who could help me to understand better the following statement. Probably, I am doing something wrong or with the wrong tools.

STATEMENT
Direct access by a sector level to a hard disk is needed under OS of Win2000 or compatible. Program is designed to be compiled under UNIX platforms as well, thus I have a low level functionality dealing with HDD routines enclosed separately with base types redefined.

REVISION
I use function CreateFileW to obtain a HANDLE to a particular physical device. There is an example of source-code opening a device:
Quote:
DEVICE OpenDevice(wchar_t* devName, int mode, bool fUseException)
{
// detect operation mode needed
DWORD dwOpMode = ( mode&mdRead ? GENERIC_READ : 0 ) |
( mode&mdWrite ? GENERIC_WRITE : 0 );

// opening a device. DEVICE type is a redefined HANDLE
DEVICE dev = CreateFileW(devName, dwOpMode, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);

// check if there are errors opening a device
if ( INVALID_HANDLE_VALUE == dev )
{
// errors detected. To know an extended error code
// use GetLastError
//
if ( fUseException )
// generating exception
//
throw afx_except(EX_DEV_OPEN);

// reflect there is an error
dev = NULL;
}

// here - a device handle
return dev;
}// OpenDevice
Well, lads from Micro$oft say: due the unbuffered way of working with devices, use those restriction, described in their technical articles for unbuffered access to files. There, my buffer preparation routine:
Quote:
unsigned char* alloc_buffer(long cb)
{
// allocate some paging memory. This memory to be sure is aligned to page boundaries
//
unsigned char* pBuff = (unsigned char*)VirtualAlloc(NULL, cb, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);

if ( pBuff )
// guaranty this memory is currently present in our RAM
VirtualLock(pBuff, cb);

return pBuff;
}// alloc_buffer
And finally, I assume I could read and write sectors to my buffer prepared in its special way, using ReadFile and WriteFile functions providing them with my device's handle.

REMARKS
Function OpenDevice works fine, returns a device's handle; alloc_buffer works as well but when I try to ReadFile (WriteFile I have not tried yet), system reads nothing and GetLastError would return 0x00000005 - "Access denied". My program runes with administrative rights, thus I guess, I need to gain an exclusive access to a drive, i.e. \\.\PHYSICALDRIVE0. But how should I do so? Do I need to lock an entire drive and how could that be done?

Last edited by lex57ukr; 05-24-04 at 05:31 AM.
lex57ukr is offline   QUOTE Thanks
Old 05-24-04, 02:44 AM Thread Starter   #2
lex57ukr
Registered



Join Date: Feb 2004
Location: Orem, UT

 
I found the wrong thing and it is not in the code above! The entire code is correct with its logic, but I have missed accounting one valuable parameter from analyze before calling OpenDevice, thus it led to a device opened with forbidden READ ACCESS!

Anyway, probably there are some hints in devices (namely, HDDs) Input and Output that might be interesting for the discussion. Go ahead! Let’s talk for there are lots of underwater stones hidden...

As for the instance:
Quote:
unsigned char* RawRead(DEVICE& dev, __int64 ofst, unsigned char* buff, long cb)
{
DWORD dwcb;

// absolut sector offset
LARGE_INTEGER liOfst; liOfst.QuadPart = ofst;

if ( !SetFilePointerEx(dev, liOfst, NULL, FILE_BEGIN) ||
!ReadFile(dev, buff, cb, &dwcb, NULL) || (DWORD)cb != dwcb )
// failed to read data
//
throw afx_except(EX_DEV_READ);

return buff;
}// RawRead
How about this one? I feel like SetFilePointerEx doesn't do the job the way I want or I ask for one the wrong way. The symptom is that the 0th sector (boot sector) is read perfectly, but when I try to read an LBA Extended Partition's boot sector it reads something else different of the data I need.

Last edited by lex57ukr; 05-24-04 at 02:56 AM.
lex57ukr is offline   QUOTE Thanks
Old 05-24-04, 05:36 AM Thread Starter   #3
lex57ukr
Registered



Join Date: Feb 2004
Location: Orem, UT

 
I found the problem!

Quote:
Original code:
LARGE_INTEGER liOfst; liOfst.QuadPart = ofst;
thus ofst becomes a logical sector and not an absolute one, but I need it vice versa

Quote:
Fixed code:
LARGE_INTEGER liOfst; liOfst.QuadPart = ofst*512;
lex57ukr is offline   QUOTE Thanks
Old 05-25-04, 08:50 AM Thread Starter   #4
lex57ukr
Registered



Join Date: Feb 2004
Location: Orem, UT

 
Done! I wrote the program. It is able of saving, restoring, fixing and comparing layouts with original boot sectors on physical devices in win2000.

No more troubles and secretes with that

Well, I am about to port this project to Linux Red Hat 9.0
lex57ukr is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 02:34 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?