The Master Boot Record (MBR), also known as the master boot sector, is the first sector that must be read when the computer accesses the hard disk after it is started, its 3D address on the hard disk is (cylindrical, Head, Sector) = (0, 0, 1 ). When in-depth discussion of the internal structure of the primary Boot Sector, the first 446 bytes of content is sometimes referred to as the "primary Boot Record" (MBR ), it is followed by a 4-Byte "disk partition table" (DPT) and a 2-byte ending sign (55aa ). Therefore, when using the term "primary Boot Record" (MBR), you need to determine whether it refers to the entire primary boot sector or the first 446 bytes of the primary Boot Sector.
The primary Boot Sector records the information about the hard disk and the size and location of each partition of the hard disk, which is an important entry to data information. If it is damaged, the basic data structure information on the hard disk will be lost. The original data can be accessed only after the data structure information is reconstructed in a tedious way. Information in the primary boot sector can be written to the software through any partition tool based on an operating system, but it has no specific relationship with an operating system, that is, if a valid Master Boot Record is created, any operating system can be guided (the operating system is created on an advanced formatted hard disk partition and is associated with a certain file system ).
Composition of the Master Boot Record
| =================================|
| Address description |
| ----------------------------------------- |
| 0x00 code zone 440 |
| ----------------------------------------- |
| 0x01b8 disk flag 4 |
| ---------------------------------------- |
| 0x01bc generally has a null value of 2 |
| ---------------------------------------- |
| 0x01be standard MBR Partition Table 64 |
| Four 16-B primary partition tables |
| ----------------------------------------- |
| 0xfe MBR magic 0x55aa |
| Number |
| =================================|
Hard Disk Partition structure table:
The 64 bytes starting with 0x01be in are 4 primary partition tables. The structure of a partition table is as follows:
| =================================|
| Offset Len info |
| ------------------------------------------------- |
| 0x00 1 Status of this partition |
| ------------------------------------------------- |
| 0x1 1 start head number of this partition |
| ------------------------------------------------- |
| 0x2 1 start fan ID of this partition |
| ------------------------------------------------- |
| 0x3 1 starting column number of this partition |
| ------------------------------------------------- |
| 0x4 1 file system flag |
| ------------------------------------------------- |
| 0x5 1 end head number of this partition |
| ------------------------------------------------- |
| 0x6 1 end fan ID of this partition |
| ------------------------------------------------- |
| 0x7 1 ending column number of this partition |
| ------------------------------------------------- |
| 0x8 4 partition start sector ID |
| ------------------------------------------------- |
| 0xc 4 Total number of sectors in the partition |
| =================================|
How to read the MBR partition content:
First, you need to enable the hard disk device, and then read the content of 1st sectors from the device, that is, the MBR content,
Handle handle;
Disk_geometry DSK;
Bool bresult;
DWORD junk;
// If only the read operation is performed, you can use the generic_read permission to set the read operation permission when you open the device;
/*
Handle = createfilea (path, generic_read,
File_pai_read,
Null,
Open_existing,
0, 0 );
*/
// If you need to read, write, and execute operations, use the generic_all permission word to open the device.
Handle = createfilea (path, generic_all,
File_pai_read,
Null,
Open_existing,
0, 0 );
If (handle = invalid_handle_value)
{
Return handle;
}
// Obtain the number of bytes in a single sector of the device, generally 512 bytes.
Bresult = deviceiocontrol (handle, // device we are querying
Ioctl_disk_get_drive_geometry, // operation to perform
Null, 0, // no input buffer, so pass zero
& DSK, sizeof (DSK), // output buffer
& Junk, // discard count of bytes returned
(Lpoverlapped) null); // synchronous I/O
If ((! Bresult) | (DSK. bytespersector <sector_size ))
* Sect_size = sector_size;
Else
// DSK. bytespersector is the sector size of the current hard disk device.
* Sect_size = DSK. bytespersector;
Use read_disk to read data from one slice.
// HND is the handle of the device, PTR is the pointer to the storage space for storing the data in the first sector. The sector starts from the first sector, and the nsects indicates taking several sectors,
Sectorsize indicates the size of the slice.
Int read_disk (filehandle hnd, void * PTR, loff_t sector, int nsects, int sectorsize)
{
Loff_t offset;
DWORD Rd, Len;
DWORD low;
Long high;
Bool ret;
// Obtain the offset bytes of the read data on the device;
Offset = sector * sectorsize;
Low = (DWORD) (offset & 0 xffffffff );
High = (DWORD) (Offset> 32) & 0 xffffffff );
// Set the read location of the current device handle
Low = setfilepointer (HND, low, & High, file_begin );
// The total length of the slice to be read
Len = nsects * sectorsize;
// Read Len data from the device handle hnd to the bucket pointed to by PTR. RD indicates the length of the data read.
Ret = readfile (HND, PTR, Len, & Rd, null );
If (! RET)
Return-1;
Return RD;
}
Open-source ext2read code-read MBR partition content