Click here to Skip to main content
Sign Up to vote bad
good
See more: CTurbo
Can any one tell me the code to access and amend the MBR with Turbo C. Pls give me a brief for that.
Posted 6 Nov '11 - 20:12

Comments
johny10151981 - 7 Nov '11 - 4:39
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

2 solutions

The format of the MBR can be found at Wikipedia[^]
 
In code these look like
//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.
  Permalink  
Comments
Richard MacCutchan - 7 Nov '11 - 9:56
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.
  Permalink  
Comments
Andrew Brock - 7 Nov '11 - 7:49
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 414
1 Arun Vasu 253
2 OriginalGriff 190
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 7 Nov 2011
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid