The format of the MBR can be found at
Wikipedia[
^]
In code these look like
typedef packed struct _Partition {
BYTE status; BYTE StartAddrHead; WORD StartAddrCylSec; BYTE PartType;
BYTE EndAddrHead; WORD EndAddrCylSec; DWORD StartLBA; DWORD EndLBA; } Partition;
typedef packed struct _MBR {
BYTE Code[440];
DWORD DiskSig; WORD Reserved; Partition PartTable[4];
BYTE BootSignature[2]; } MBR;
Both of these structures must be packed. That is there is no padding added by the compiler for optimisations. I think the keyword
packed
before
struct
does this on the Turbo compiler.
So, just open the drive and read/write.
To open the whole harddrive for writing use the following filename:
Windows:
\\.\PhysicalDriveX
where
X is the drive number, starting from
0
(rember that to get a \ in code, you need 2 \, so this is
\\\\.\\PhysicalDriveX
in the source code).
Linux/UNIX/OSX:
/dev/sdX
where
X is the drive letter, starting from
a
.