Understanding MTK’s MBR/EBR File Format

JuliusBajaj

Administrator
Staff member
According to wikipedia, MBR is short for Master Boot Record, and EBR means Extended Boot Record. MBR was introduced in 1983 and was previously intended to be used for IBM PC. Aside from partition info, MBR file can also contain bootstrap code, and there are some variants of MBR file format due to the limitation of the original format.

Thank goodness! We don’t have to concern about all the complexity of the MBR file format in our case(the MTK’s MBR), we only focus on the simplest things, no bootstrap code, no extended data fields. Here is a sample MBR file(opened by a hex editor) along with a MTK factory package:


0000000: 0000 0000 0000 0000 0000 0000 0000 0000
0000010: 0000 0000 0000 0000 0000 0000 0000 0000
0000020: 0000 0000 0000 0000 0000 0000 0000 0000
0000030: 0000 0000 0000 0000 0000 0000 0000 0000
0000040: 0000 0000 0000 0000 0000 0000 0000 0000
0000050: 0000 0000 0000 0000 0000 0000 0000 0000
0000060: 0000 0000 0000 0000 0000 0000 0000 0000
0000070: 0000 0000 0000 0000 0000 0000 0000 0000
0000080: 0000 0000 0000 0000 0000 0000 0000 0000
0000090: 0000 0000 0000 0000 0000 0000 0000 0000
00000a0: 0000 0000 0000 0000 0000 0000 0000 0000
00000b0: 0000 0000 0000 0000 0000 0000 0000 0000
00000c0: 0000 0000 0000 0000 0000 0000 0000 0000
00000d0: 0000 0000 0000 0000 0000 0000 0000 0000
00000e0: 0000 0000 0000 0000 0000 0000 0000 0000
00000f0: 0000 0000 0000 0000 0000 0000 0000 0000
0000100: 0000 0000 0000 0000 0000 0000 0000 0000
0000110: 0000 0000 0000 0000 0000 0000 0000 0000
0000120: 0000 0000 0000 0000 0000 0000 0000 0000
0000130: 0000 0000 0000 0000 0000 0000 0000 0000
0000140: 0000 0000 0000 0000 0000 0000 0000 0000
0000150: 0000 0000 0000 0000 0000 0000 0000 0000
0000160: 0000 0000 0000 0000 0000 0000 0000 0000
0000170: 0000 0000 0000 0000 0000 0000 0000 0000
0000180: 0000 0000 0000 0000 0000 0000 0000 0000
0000190: 0000 0000 0000 0000 0000 0000 0000 0000
00001a0: 0000 0000 0000 0000 0000 0000 0000 0000
00001b0: 0000 0000 0000 0000 0000 0000 0000 0000
00001c0: 0000 0500 0000 0004 0000 ffff ffff 0000
00001d0: 0000 8300 0000 0068 0000 0050 0000 0000
00001e0: 0000 8300 0000 00b8 0000 0050 0000 0000
00001f0: 0000 8300 0000 006c 0100 0030 0000 55aa


The size of this MBR file is 512 bytes. As we can see, the file is almost filled by 0, and ends with 0xaa55(little endian). Here’ the file format:

Address Description Size
                                                                       (bytes)
Hex Dec
+000h +0 Bootstrap code area 446
+1BEh +446 Partition entry #1 Partition table 16
+1CEh +462 Partition entry #2 16
+1DEh +478 Partition entry #3 (for primary partitions) 16
+1EEh +494 Partition entry #4 16
+1FEh +510 55h Boot signature 2
+1FFh +511 AAh
Total size: 446 + 4×16 + 2 512


From the table above, we can learn that addresses from 0 to 0x1bd store the bootstrap code, which is empty(all 0s) in our case. Data start from 0x1be to 0x1cd, 16 bytes in total, is partition entry #1; 0x1ce to 0x1dd is entry #2, 0x1de to 0x1ed is #3, and 0x1ee to 0x1fd is #4. Now let’s dig into the detail of a partition entry.

First of all, we must know the representation of these 16 bytes:
Code:
0	1 – 3	4	5 – 7	8 – 11	12 – 15
Boot indicator.	CHS address:	partition type.	CHS address:	start address of	size of this
0x80 for activate,	partition start.	0x05 for MBR/EBR,	partition end.	this partition.	partition.
0x00 otherwise.	 	0x83 for Linux	 	 	 
 	 	filesystem.
Note:
  1. The data order in the MBR file could be big endian or little endian, depends on the endian that the target device uses. The chip is configured to use little endian in my case(MT6589).
  2. The address or size value defined in the MBR is not represented in bytes, it is represented in sectors. For example, the size of the partition #2 is 0x5000(00500000, little endian), its real size is 0x5000*0x200=0xa00000 bytes, which is 10MB. The 0x200 is 512 in decimal, it presents how many bytes in a sector, which it 512 in most cases(but not all).
  3. The value of the partition’s start address(byte 8 to 11) does not represent the absolute offset(in sectors) from 0, it represents the offset(in sectors) from current MBR/EBR file. For example, the fourth partition in the sample MBR is start from 0x16c00, that means this partition is 0x16c00 sectors away from this MBR file, assume the address of this MBR file is sector 0x3000(byte 0x600000), then the real start address of partition #4 is (0x16c00 + 0x3000)*0x200 (bytes), which is byte 0x3380000.
With the byte representation in mind, we’ll look into the partitions in the sample MBR file.
The first partition is a MBR/EBR(EBR1 here, in fact) partition(byte 4 is 0x05). It starts from sector 0x400 and has 0xffffffff sectors.


Code:
00001b0:                                    0000
00001c0: 0000 0500 0000 0004 0000 ffff ffff
TO BE CONTINUE...
 

GSM Nigeria Forum by GSM Nigeria  2009 |  2019 Engr. Dearlex - GSM Palace
Dedicated forum for the GSM community!
- GSM Nigeria Forum.  2004-2007 - 2009-2012 - 2014-2015 - 2019 -  !