Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Turbo
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 21:12pm
Comments
johny10151981 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 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)

  Print Answers RSS
0 Maciej Los 424
1 ridoy 400
2 DamithSL 230
3 OriginalGriff 179
4 Suvendu Shekhar Giri 150
0 OriginalGriff 8,033
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,538
3 Maciej Los 5,508
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 7 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100