Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any one tell me the code to access and amend the MBR with Turbo C. Pls give me a brief for that.
Posted
Comments
Mohibur Rashid 7-Nov-11 4:39am    
Probably this is the most common advice I see on all forums,
Stop using Turbo C, Its even older than dinosaur. Use latest Software like Microsoft visual studio, if it is about windows you would also need DDK

The format of the MBR can be found at Wikipedia[^]

In code these look like
C++
//These structures must be packed with 0 bytes padding.
//I think the keyword "packed" does this on the turbo compiler
typedef packed struct _Partition {
	BYTE status; //0x80 for bootable, 0x00 for not bootable, anything else for invalid
	BYTE StartAddrHead; //head address of start of partition
	WORD StartAddrCylSec; //(AddrCylSec & 0x3F) for sector,  (AddrCylSec & 0x3FF) for cylendar
	BYTE PartType;
	BYTE EndAddrHead; //head address of start of partition
	WORD EndAddrCylSec; //(AddrCylSec & 0x3F) for sector,  (AddrCylSec & 0x3FF) for cylendar
	DWORD StartLBA; //linear address of first sector in partition. Multiply by sector size (usually 512) for real offset
	DWORD EndLBA;   //linear address of last sector in partition. Multiply by sector size (usually 512) for real offset
} Partition;

typedef packed struct _MBR {
	BYTE Code[440];
	DWORD DiskSig; //This is optional
	WORD Reserved; //Usually 0x0000
	Partition PartTable[4];
	BYTE BootSignature[2]; //0x55 0xAA for bootable
} 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.
 
Share this answer
 
Comments
Richard MacCutchan 7-Nov-11 9:56am    
A 5 for this also (forgot earlier). I think the basic warning I gave is about as much as can be said. If people want to mess about here then it's really up to them.
Do not write into the MBR unless you are absolutely certain that you know what you are doing. The chances of making your system unusable are quite high.
 
Share this answer
 
Comments
Andrew Brock 7-Nov-11 7:49am    
5'd. Forgot to mention that bit. Mention of a backup wouldn't go astray either.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900