Partition Table: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
m (Added a link)
m (→‎MBR: make LBA a link to the page.)
 
(21 intermediate revisions by 13 users not shown)
Line 2: Line 2:


==MBR==
==MBR==
{{Main|MBR (x86)}}
The Master Boot Record is the traditional way of storing partition information about a hard disk, along with some boot code.
The Master Boot Record is the traditional way of storing partition information about a hard disk, along with some boot code.
That is, the Partition Table is contained inside the [[MBR (x86)|MBR]], which is stored in the first sector
That is, the Partition Table is contained inside the [[MBR (x86)|MBR]], which is stored in the first sector
(cylinder 0, head 0, sector 1 -- or, alternately, LBA 0) of the hard drive.
(cylinder 0, head 0, sector 1 -- or, alternately, [[LBA]] 0) of the hard drive.
(See the [[MBR (x86)|MBR article]] for the overall structure and contents of the MBR.)
(See the [[MBR (x86)|MBR article]] for the overall structure and contents of the MBR.)


Almost all PCs still use an MBR for booting hard disks, and for storing partition information on hard disks.
Almost all PCs still use an MBR for booting hard disks, and for storing partition information on hard disks.
Traditional MBRs are nearly obsolete at this time, because the 32 bit design of the LBA fields in the Partition Table begins
Traditional MBRs are nearly '''obsolete''' at this time, because the 32 bit design of the LBA fields in the Partition Table begins
to "overflow" when dealing with disks larger than 2Tb.
to "overflow" when dealing with disks larger than 2Tb.
One possible replacement for the MBR system is GPT (see below). It might also be possible to agree on a new standard for
The successor to the MBR system is [[GPT]] (see below). There were other proposed new standards for
the MBR, but the GPT won the battle.
the MBR, with 48 bit LBA fields for the partitions ([[#"Unofficial" 48 bit LBA Proposed MBR Format|see below]]).


Information about primary partitions and an extended partition is contained in a 64-byte data structure located in the MBR.
Information about primary partitions and an extended partition is contained in a 64-byte data structure located in the MBR.
Line 43: Line 44:


{| {{wikitable}}
{| {{wikitable}}
! Offset
! Element (offset)
! Size
! Size
! Description
! Description
|-
|-
| 0
| 0x00
| byte
| 1 byte
| Boot indicator bit flag: 0 = no, 0x80 = bootable (or "active")
| Boot indicator bit flag: 0 = no, 0x80 = bootable (or "active")
|-
|-
| 1
| 0x01
| byte
| 1 byte
| Starting head
| Starting head
|-
|-
| 2
| 0x02
| 6 bits
| 6 bits
| Starting sector (Bits 6-7 are the upper two bits for the Starting Cylinder field.)
| Starting sector (Bits 6-7 are the upper two bits for the Starting Cylinder field.)
|-
|-
| 3
| 0x03
| 10 bits
| 10 bits
| Starting Cylinder
| Starting Cylinder
|-
|-
| 4
| 0x04
| byte
| 1 byte
| System ID
| System ID
|-
|-
| 5
| 0x05
| byte
| 1 byte
| Ending Head
| Ending Head
|-
|-
| 6
| 0x06
| 6 bits
| 6 bits
| Ending Sector (Bits 6-7 are the upper two bits for the ending cylinder field)
| Ending Sector (Bits 6-7 are the upper two bits for the ending cylinder field)
|-
|-
| 7
| 0x07
| 10 bits
| 10 bits
| Ending Cylinder
| Ending Cylinder
|-
|-
| 8
| 0x08
| dword
| 4 bytes
| Relative Sector (to start of partition -- also equals the partition's starting LBA value)
| Relative Sector (to start of partition -- also equals the partition's starting LBA value)
|-
|-
| 12
| 0x0C
| dword
| 4 bytes
| Total Sectors in partition
| Total Sectors in partition
|}
|}
Line 92: Line 93:
* At most one partition should be active.
* At most one partition should be active.
* The Partition Table entries are <b>not</b> aligned on 4 byte boundaries (if the MBR is itself loaded into memory on a 4 byte boundary).
* The Partition Table entries are <b>not</b> aligned on 4 byte boundaries (if the MBR is itself loaded into memory on a 4 byte boundary).
* Therefore, neither are the two dword LBA entry values -- so the LBA values cannot be copied directly into a register.
* Therefore, neither are the two uint32_t LBA entry values -- so the LBA values cannot be copied directly into a register.
* All the entry values are encoded little-endian. Take note of this if fetching the uint32_t LBA entry values two bytes at a time, even on little-endian systems.
* The Cylinder, Head, Sector fields (taken together) are only 3 bytes (24 bits) long.
* The Cylinder, Head, Sector fields (taken together) are only 3 bytes (24 bits) long.
* Sector values (in the CHS fields) of 0 are illegal.
* Sector values (in the CHS fields) of 0 are illegal.
* CHS fields "max out" on a drive that is approximately 8GB in size -- and are therefore useless on almost all current drives.
* CHS fields "max out" on a drive that is approximately 8GB in size -- and are therefore useless on almost all current drives.
* For drives smaller than 8GB, the LBA fields and the CHS fields must "match" when the values are converted into the other format.
* For drives smaller than 8GB, the LBA fields and the CHS fields must "match" when the values are converted into the other format.
* For drives bigger than 8GB, generally the CHS fields are set to Cylinder = 1023, Head = 255, Sector = 63 -- which is considered an invalid setting.
* For drives bigger than 8GB, generally the CHS fields are set to Cylinder = 1023, Head = 254 or 255, Sector = 63 -- which is considered an invalid setting.
* If a Partition Table entry is unused, then it should be set to all 0.
* If a Partition Table entry is unused, then it should be set to all 0.
* A System ID byte value of 0 is the definitive indicator for an unused entry.
* A System ID byte value of 0 is the definitive indicator for an unused entry.
Line 105: Line 107:
The System ID byte is supposed to indicate what filesystem is contained on the partition (ie. Ext2, ReiserFS, FAT32, NTFS, ...).
The System ID byte is supposed to indicate what filesystem is contained on the partition (ie. Ext2, ReiserFS, FAT32, NTFS, ...).
There was never any standard created for the System ID byte -- which means that Microsoft went and tried to hog almost all of the possible values.
There was never any standard created for the System ID byte -- which means that Microsoft went and tried to hog almost all of the possible values.
See [[#External Links|the links below]] for tables of values of the System ID byte, for filesystems that have been lucky enough to aquire
See [[#External Links|the links below]] for tables of values of the System ID byte, for filesystems that have been lucky enough to acquire their own value by common consensus.
their own value by common consensus.


If you create your own custom filesystem, then you can simply pick a System ID value for your filesystem that seems to be unused.
If you create your own custom filesystem, then you can simply pick a System ID value for your filesystem that seems to be unused.
There is also an attempt to standardize the use of System ID value = 0x7f (by the Alt-OS gang), to cover all custom filesystems that follow the standard -- however, their effort seems to be losing steam.
There is also an attempt to standardize the use of System ID value = 0x7f (by the Alt-OS gang), to cover all custom filesystems that follow the standard -- however, their effort seems to be losing steam.

=="Unofficial" 48 bit LBA Proposed MBR Format==
The two CHS fields are unused in all current drives, leaving only the two 32 bit LBA fields to "do all the work". But there never was any 32 bit
LBA addressing mode -- the current "standard" LBA addressing mode is 48 bits. So it is reasonable to try to redefine the Partition Table to eliminate the
unused CHS fields, and use the extra space to extend the two LBA fields to a full 48 bit size. This would eliminate the impending obsolescence
of the entire MBR scheme.

It seems reasonable to try to preserve (as much as possible) the current Partition Table structure. Therefore, the following alternate
structure for 48 bit LBA Partition Table entries is proposed:

{| {{wikitable}}
! Element (offset)
! Size
! Description
|-
| 0
| byte
| Bitflags field: 1 = not bootable, 0x81 = bootable (or "active")
|-
| 1
| byte
| (unused)
|-
| 2
| word
| Partition Start LBA (high word of 48 bit value)
|-
| 4
| byte
| System ID
|-
| 5
| byte
| (unused)
|-
| 6
| word
| Partition Length (high word of 48 bit value)
|-
| 8
| dword
| Partition Start LBA (low dword)
|-
| 12
| dword
| Partition Length (low dword)
|}

Note: The basic intent is to use bit #0 (value = 1) in the bitflags field as a "48 bit LBA" indicator, preserve the offsets and functions
of the bitflags and System ID fields, and use the two available aligned words to extend the LBA fields.


==Extended Partitions==
==Extended Partitions==


Extended partitions are very simple. However, their definition is very messed up, so it is easy to make mistakes when implementing them.
Extended partitions are a way of adding more than 4 partitions to a partition table.


The partition table may have one and only one entry that has the SystemID 0x5 (or 0xF). This describes an extended partition.
Extended partitions are nested. That is, in the Partition Table of your MBR you are allowed to have one (and only one) partition marked as being "extended". Otherwise, the extended partition entry has exactly the same format as all the other partition table entries.


An extended partition is one physical partition that is subpartitioned into "logical" partitions. So, the partition table entry describing it has a StartLBA and NumSectors that describe the space inside which any number of logical partitions may sit.
To mark a partition as extended, set the "System ID" AKA "type" byte to 0xf (or 5). 0xf is probably safer than 5 -- "Using type 05 for extended partitions beyond 8 GB may lead to data corruption with MSDOS." However, more OSes may recognize 5 than 0xf, and mount the partition properly.


At the start of an extended partition is an extended partition table. This takes exactly the same form as a normal partition table, apart from most of the fields are unused. Only two of the partition entries are used - the first one describes the desired logical partition, and the second one is a link (much like a linked list) that points at another extended partition table. The size should officially include the logical partition that follows with it. The remaining two entries are not used and should be left zeroed out.
Within each extended partition, the first sector contains a fake MBR with a fake partition table. The entire fake MBR is blank, except for two entries in the partition table (the first two), plus the 0xAA55 signature.


The two fake partition table entries may contain one "logical partition" and another nested extended partition.
Note that the StartLBA fields in these extended partition table entries are relative to the start of the extended partition itself.

The "bitflags" and "System ID" bytes of the logical partition are used as normal. The partition <b>can</b> be bootable.

The CHS fields in the two fake partition table entries are undefined, and unused in the standard definition. (Note: This makes it convenient to use the CHS fields for the LBA48 high words, [[#"Unofficial" 48 bit LBA Proposed MBR Format|as suggested above]].)

The "Partition length" dwords in each of the two fake partition table entries are used normally.

Which brings up the biggest confusing thing about extended partitions: the "StartLBA" values in the fake partition tables. The values are "relative LBA"s -- not absolute LBAs, such as the ones in a normal partition table. Even worse: the two StartLBA values of the two fake partition table entries are <b>relative to two different things.</b> The StartLBA value of the logical partition is a relative offset from the beginning of the current extended partition (ie. the LBA that contains the current fake MBR and fake partition table). But the StartLBA of the <b>next</b> nested extended partition is relative to the beginning of the <b>previous</b> extended partition. (Note: of course the "previous" extended partition does not apply to the very first extended partition -- for the first extended partition, both StartLBAs are relative to the same point.)

Specifically, if you are parsing the fake partition table of the third extended partition, the logical partition entry's StartLBA will be a relative offset from the current LBA. The StartLBA entry for the nested <b>fourth</b> extended partition will be relative to the beginning of the <b>second</b> extended partition.


== GPT ==
== GPT ==
{{Main|GPT}}

GPT is an updated Partition Table standard, that has been adopted as the recommended partition mechanism under [[UEFI]]. It does not contain the artificial 24 bit or 32 bit limitations of the MBR Partition Tables. It also contains enhancements to the concept of partition tables, in general, and is significantly more complex than the MBR scheme.
GPT is an updated Partition Table standard, that has been adopted as the recommended partition mechanism under [[UEFI]]. It does not contain the artificial 24 bit or 32 bit limitations of the MBR Partition Tables. It also contains enhancements to the concept of partition tables, in general, and is significantly more complex than the MBR scheme.



==See Also==
==See Also==
[[GPT]]


===External Links===
===External Links===
Line 196: Line 137:
* [http://www.osdever.net/documents/pdf/partitiontypes.pdf Another list of System IDs/Partition Types from osdever (pdf)]
* [http://www.osdever.net/documents/pdf/partitiontypes.pdf Another list of System IDs/Partition Types from osdever (pdf)]
* [http://en.wikipedia.org/wiki/Extended_partition The wikidpedia article on the Extended Boot Record (AKA Extended Partition) has lots of pictures and many more words]
* [http://en.wikipedia.org/wiki/Extended_partition The wikidpedia article on the Extended Boot Record (AKA Extended Partition) has lots of pictures and many more words]
* GPT at [http://en.wikipedia.org/wiki/GUID_Partition_Table Wikipedia]


[[Category:x86]]
[[Category:x86]]

Latest revision as of 02:49, 5 January 2024

The following information about Partition Tables only applies to hard disk drives. Most other storage devices use other partitioning techniques, if they even use partitioning at all.

MBR

Main article: MBR (x86)

The Master Boot Record is the traditional way of storing partition information about a hard disk, along with some boot code. That is, the Partition Table is contained inside the MBR, which is stored in the first sector (cylinder 0, head 0, sector 1 -- or, alternately, LBA 0) of the hard drive. (See the MBR article for the overall structure and contents of the MBR.)

Almost all PCs still use an MBR for booting hard disks, and for storing partition information on hard disks. Traditional MBRs are nearly obsolete at this time, because the 32 bit design of the LBA fields in the Partition Table begins to "overflow" when dealing with disks larger than 2Tb. The successor to the MBR system is GPT (see below). There were other proposed new standards for the MBR, but the GPT won the battle.

Information about primary partitions and an extended partition is contained in a 64-byte data structure located in the MBR. This Partition Table conforms to a standard layout that is independent of the operating system. Each Partition Table entry is 16 bytes long, making a maximum of four entries available. Each entry starts at a predetermined offset from the beginning of the sector, as follows:

Partition number Offset
Partition 1 0x01BE (446)
Partition 2 0x01CE (462)
Partition 3 0x01DE (478)
Partition 4 0x01EE (494)

Note: Naming the partition table entries as "1" through "4" is for convenience only. The partition table entries are not required to be in any kind of order.


Each of the four Partition Table entries contains the following elements, in the following structure:

Offset Size Description
0x00 1 byte Boot indicator bit flag: 0 = no, 0x80 = bootable (or "active")
0x01 1 byte Starting head
0x02 6 bits Starting sector (Bits 6-7 are the upper two bits for the Starting Cylinder field.)
0x03 10 bits Starting Cylinder
0x04 1 byte System ID
0x05 1 byte Ending Head
0x06 6 bits Ending Sector (Bits 6-7 are the upper two bits for the ending cylinder field)
0x07 10 bits Ending Cylinder
0x08 4 bytes Relative Sector (to start of partition -- also equals the partition's starting LBA value)
0x0C 4 bytes Total Sectors in partition

Notes:

  • Any one of the partitions may be "active" (ie. bootable).
  • At most one partition should be active.
  • The Partition Table entries are not aligned on 4 byte boundaries (if the MBR is itself loaded into memory on a 4 byte boundary).
  • Therefore, neither are the two uint32_t LBA entry values -- so the LBA values cannot be copied directly into a register.
  • All the entry values are encoded little-endian. Take note of this if fetching the uint32_t LBA entry values two bytes at a time, even on little-endian systems.
  • The Cylinder, Head, Sector fields (taken together) are only 3 bytes (24 bits) long.
  • Sector values (in the CHS fields) of 0 are illegal.
  • CHS fields "max out" on a drive that is approximately 8GB in size -- and are therefore useless on almost all current drives.
  • For drives smaller than 8GB, the LBA fields and the CHS fields must "match" when the values are converted into the other format.
  • For drives bigger than 8GB, generally the CHS fields are set to Cylinder = 1023, Head = 254 or 255, Sector = 63 -- which is considered an invalid setting.
  • If a Partition Table entry is unused, then it should be set to all 0.
  • A System ID byte value of 0 is the definitive indicator for an unused entry.
  • Any other illegal value (CHS Sector = 0 or Total Sectors = 0) may also indicate an unused entry.

The System ID byte

The System ID byte is supposed to indicate what filesystem is contained on the partition (ie. Ext2, ReiserFS, FAT32, NTFS, ...). There was never any standard created for the System ID byte -- which means that Microsoft went and tried to hog almost all of the possible values. See the links below for tables of values of the System ID byte, for filesystems that have been lucky enough to acquire their own value by common consensus.

If you create your own custom filesystem, then you can simply pick a System ID value for your filesystem that seems to be unused. There is also an attempt to standardize the use of System ID value = 0x7f (by the Alt-OS gang), to cover all custom filesystems that follow the standard -- however, their effort seems to be losing steam.

Extended Partitions

Extended partitions are a way of adding more than 4 partitions to a partition table.

The partition table may have one and only one entry that has the SystemID 0x5 (or 0xF). This describes an extended partition.

An extended partition is one physical partition that is subpartitioned into "logical" partitions. So, the partition table entry describing it has a StartLBA and NumSectors that describe the space inside which any number of logical partitions may sit.

At the start of an extended partition is an extended partition table. This takes exactly the same form as a normal partition table, apart from most of the fields are unused. Only two of the partition entries are used - the first one describes the desired logical partition, and the second one is a link (much like a linked list) that points at another extended partition table. The size should officially include the logical partition that follows with it. The remaining two entries are not used and should be left zeroed out.

Note that the StartLBA fields in these extended partition table entries are relative to the start of the extended partition itself.

GPT

Main article: GPT

GPT is an updated Partition Table standard, that has been adopted as the recommended partition mechanism under UEFI. It does not contain the artificial 24 bit or 32 bit limitations of the MBR Partition Tables. It also contains enhancements to the concept of partition tables, in general, and is significantly more complex than the MBR scheme.

See Also

GPT

External Links