From: JY on
Hi,

I'm trying to use _read to get the MBR information from the 1st sector of a
disk.
The code is shown below:

int main( void )
{
char *pDisk = "\\\\.\\PhysicalDrive1";

int fdDisk0 = _open(pDisk, _O_RDONLY | _O_BINARY);

if (fdDisk0 != -1)
{
//MasterBootRecord is a struct in a header.
MasterBootRecord mbrDisk0;
memset(&mbrDisk0, '\0', sizeof(MasterBootRecord));
int nReadBytes = 0;
nReadBytes = _read(fdDisk0, &mbrDisk0, sizeof(MasterBootRecord));

_close(fdDisk0);
}

return 0;
}

Somehow, nReadBytes returns -1. I'm not sure what is wrong, could someone
please help.

Thanks,
JY
From: Barry Schwarz on
On Mon, 2 Jun 2008 01:21:00 -0700, JY <sd(a)nospamgroup.com> wrote:

>Hi,
>
>I'm trying to use _read to get the MBR information from the 1st sector of a
>disk.
>The code is shown below:
>
>int main( void )
>{
> char *pDisk = "\\\\.\\PhysicalDrive1";
>
> int fdDisk0 = _open(pDisk, _O_RDONLY | _O_BINARY);
>
> if (fdDisk0 != -1)
> {
> //MasterBootRecord is a struct in a header.
> MasterBootRecord mbrDisk0;
> memset(&mbrDisk0, '\0', sizeof(MasterBootRecord));
> int nReadBytes = 0;
> nReadBytes = _read(fdDisk0, &mbrDisk0, sizeof(MasterBootRecord));
>
> _close(fdDisk0);
> }
>
> return 0;
>}
>
>Somehow, nReadBytes returns -1. I'm not sure what is wrong, could someone
>please help.

The help for _read says it returns -1 when the handle is invalid, the
file is not open for reading, or the file is locked. You check the
handle so it should be valid. You specified read in the call to
_open. That leaves the file is locked. Is the MBR protected? Do you
need administrator authority to read it?


Remove del for email
From: Alex Blekhman on
"JY" wrote:
> Hi,
>
> I'm trying to use _read to get the MBR information from the 1st
> sector of a
> disk.
> The code is shown below:
>
> MasterBootRecord mbrDisk0;
> memset(&mbrDisk0, '\0', sizeof(MasterBootRecord));
> int nReadBytes = 0;
> nReadBytes = _read(fdDisk0, &mbrDisk0,
> sizeof(MasterBootRecord));
>
> Somehow, nReadBytes returns -1. I'm not sure what is wrong,
> could someone
> please help.

I think you should read it in multiplies of 4K (mem page size
under WinNT) or multiplies of disc clusters. Read `CreateFile'
documentation for more details.

HTH
Alex


From: JY on
> I think you should read it in multiplies of 4K (mem page size
> under WinNT) or multiplies of disc clusters. Read `CreateFile'
> documentation for more details.
>
I just found out that the read should happen in multiples of 512 bytes, else
it returns -1.

So the struct that I'm trying to use to represent the MBR should also be of
size 512.
In spite of using #pragma pack(1), I see that the packing is not taking
place as expected.

The structs are:

#pragma pack(1)
struct PartitionTableEntry {
unsigned char bootIndicator;
unsigned char startCHSone;
unsigned char startCHStwo;
unsigned char startCHSthree;
unsigned char partitionTypeDescriptor;
unsigned char endCHSone;
unsigned char endCHStwo;
unsigned char endCHSthree;
unsigned int startSectorLBA;
unsigned int partitionSize;
};

#pragma pack(1)
struct MasterBootRecord {
unsigned int CodeArea[446];
PartitionTableEntry partitionEntry[4];
unsigned short BootRecordSignature;
};

I see that sizeof(MasterBootRecord) retruns 1850 instead of 512. How do I
pack it correctly?

Thanks,
JY
From: Igor Tandetnik on
"JY" <sd(a)nospamgroup.com> wrote in message
news:F5FE2BD2-BB4E-4D2E-8FFF-C647CD2E69BB(a)microsoft.com
> I just found out that the read should happen in multiples of 512
> bytes, else it returns -1.
>
> So the struct that I'm trying to use to represent the MBR should also
> be of size 512.

Not necessarily: it should be a _multiple_ of 512.

> struct MasterBootRecord {
> unsigned int CodeArea[446];
> PartitionTableEntry partitionEntry[4];
> unsigned short BootRecordSignature;
> };
>
> I see that sizeof(MasterBootRecord) retruns 1850 instead of 512. How
> do I pack it correctly?

446 ints is 1784 bytes already. You can't put a lake into a thimble no
matter how hard you "pack" it.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925