Popular White Paper On This Topic

Is this a copy/paste please? If so you have typedef sturct and not struct
I also think you need "int main( int argc ..."
I think fdopen() returns a file descriptor which you then use on subsequent fread() calls

Why do you presume that the FILE * returned by fdopen(fd, "r"); is the
first 512 bytes(MBR) of the disk?
Why don't you try doing a read(fd, &MBR ,512)) from offset 0 that is 512
bytes long instead of the fdopen call?
Get rid of the typedef and spell struct correctly.

No this is not a copy paste. I wrote this on my own.
I have not used "int main(argc,argv)" because I dont want command line arguments.
fdopen is prototyped as follows in the man page:
FILE *fdopen(int fildes, const char *mode);

Opps! my mistake I have modified my program since this post. I have corrected my errorrs and posting the corrected Program.
bobdobs quoted
--------
Why do you presume that the FILE * returned by fdopen(fd, "r"); is the
first 512 bytes(MBR) of the disk?
Why don't you try doing a read(fd, &MBR ,512)) from offset 0 that is 512
bytes long instead of the fdopen call?
Get rid of the typedef and spell struct correctly. ...
----------
thanks for the suggestions. I have made the corrections and posting the code.

------
What is the value of res after the read? Did you in fact read 512 bytes?
-------
res = 512
------
Did you in fact read 512 bytes?
-------
Yes I did. I got the MBR signature at byte numbers 510(0x55) and 511 (0xAA).
----
If none of that helps do a read of the 1st 4k of the disk and see what
you get. ...
----
I'll do it now. My intention is to write a program which reads the Boot record and prints the size and partition numbers; A kind of fdisk implementation.
I'll keep the community posted.
cheers,
Sarma

Hi there,
I have implemented the program to read the MBR and print the information on stdout. I am posting the program for review. I'll glad to have comments on the coding style and any suggestions to better the code.
mbr.h :- this defines the structure containing partition table , MBR elements.

-------
shouldn't chs_ptr->head = ptr[0]; chs_ptr->sector = (ptr[1] &
0x3F); chs_ptr->cylinder = ptr[2];
actually be chs_ptr->head = ptr[1]; chs_ptr->sector = (ptr[2] &
0x3F); chs_ptr->cylinder = ptr[3];
because ptr[0] actually points to mbr.primary[i].bi
-------
yes! this is a bug. Thanks for spotting it.
--------
How can you be certain that only 3 bytes are
being allocated instead of 4? I know that you only specify 24 bits but
the memory allocation unit may be 4 bytes.
--------
Yes this was the bug which delayed the task for so long. *__attribute__* saved the day. More on this can be had from this reference link:
http://docs.freebsd.org/info/gcc/gcc.info.Type_Att ributes.htmlI've checked the sizes using *sizeof*.
I couldn't check this code with different cases as I have only one hard disk setup here. I'll be glad if some one can run the program on their systems.
You don't have to test it on the hard disk directly. you can try to create a dummy image with a size of 4K as follows[with superuser permissions]:
dd -bs=512 -count=8 -if=/dev/<your_hard_disk_file_name> -of=<some_temp_file>
cheers,

Copyright 1998-2015 Ziff Davis, LLC (Toolbox.com). All rights reserved. All product names are trademarks of their respective companies. Toolbox.com is not
affiliated with or endorsed by any company listed at this site.