Tuesday 5 June 2012

Know YOur HARD DISK



This page describes the typical layout of a modern hard drive. You may have heard of file systems such as NTFS, FAT32 or EXT3, which are used by your operating system. Concepts like files and directories are contained in these file systems, so obviously they are very important. But a single physical hard disk can contain multiple filesystems – each on a separatepartition. And a hard disk that contains a operating system must contain some elements that play a role in the boot sequence.
Obviously a lot more is going on under the hood. Let’s take a look.

An image showing the mechanical components of a hard disk. A typical consumer hard disk will usually have between one and five platters.
Hard disks have been around since the 1950′s, but the design has not changed much. The general hard disk design is quite simple, consisting of only a few moving parts. In the picture above you can see:
  • Platters: Solid disks with a magnetic coating that contains the data. The platters spin at a constant rate when the hard disk is in operation,  typically at 3600, 5200 or 7200 rounds per minute (rpm).
  • Arms: The head stack assembly holds the arms that hold the read/write heads. The stack is rotated by an actuator which is not displayed in the image, causing the arms to position the heads between the hub and the edge of the platter. To achieve great speed and accuracy, the arm and its movement mechanism need to be extremely light and fast. The arm on a typical hard-disk drive can move from hub to edge and back up to 50 times per second
The data is stored on the surface of a platter in areas called tracks and sectors. A sector is displayed in yellow and a track in blue.
Every platter contains many concentric circles – called tracks – that are used to store the data. This radically differs from a CD or DVD, where a single track of data is used, laid out as a spiral. A modern hard disk has tens of thousands of tracks on a platter. The tracks on a hard disk are divided up into smaller segments called sectors. Each sector usually holds 512 bytes of user data, plus as many as a few dozen additional bytes used for internal drive control and for error detection and correction.
The heads that access the platters are locked together on an assembly of head arms. This means that all the heads move in and out together, so each head is always physically located at the same track number. It is not possible to have one head at track 0 and another at track 1,000. Because of this arrangement, often the track location of the heads is not referred to as a track number but rather as a cylinder number. What you should take away from this is that they are essentially the same.
This image shows a cylinder. A cylinder is formed by the tracks that are physically located directly above each other.
In the past, hard disks used to work pretty much exactly as described above. Modern hard disks use methods such as zoned bit recording to improve performance, but these details are only known by the disk controller.

The logical geometry of a hard disk is the logical structure that programs see when communicating with the hard disk’s controller – which is located on a logical board inside the hard disk. In all but the earliest hard disks the physical geometry is a lot more complicated than the logical geometry. Luckily, only the hard disk’s engineers have to deal with the complicated nature of the physical geometry; they are hidden from the operating system and the user.

In the case of early IDE/ATA hard disks the BIOS provides access to the hard disk through an addressing mode called CHS,where “CHS” stands for “cylinder, head, sector”. CHS addressing starts at (0, 0, 1). In old computer system the maximum amount of addressable data was very limited – due to limitations in both the BIOS and the hard disk interface. Some well-known resulting limits are the 502 MB and the 8.4 GB barriers. The CHS addressing mode was declared obsolete in the ATA-5 standard, replacing it with LBA addressing.

Modern hard disks use a recent version of the ATA standard, such as ATA-7. These disks are accessed using a different addressing mode called: logical block addressing or LBA involves a totally new way of addressing sectors. Instead of referring to a cylinder, head and sector number, each sector is instead assigned a unique “sector number”. In essence, the sectors are numbered 0, 1, 2, etc. up to (N-1), where N is the number of sectors on the disk. In order for LBA to work, it must be supported by the disk, the BIOS and operating system. The current 48-bit LBA scheme, introduced in 2003 with the ATA-6 standard, allows addressing up to 144 petabytes (144,000,000 gigabytes)..
The BIOS, working with the system chipset on the motherboard and the system I/O bus, controls which types of modes can be used with the hard disk to actually transfer the data. Originally, systems used the BIOS as an intermediary for every byte of transferred information. Modern operating systems implement direct disk access (Direct Memory Access), and do not use the BIOS subsystems, except at boot load time. A detailed description of the modes of transfer between the hard disk and volatile memory is out of the scope of this page, but the prevalent mode in 2011, UDMA, is a good place to start reading.

When an x86 PC is powered on, the BIOS will select a storage device from which to boot. From this device, the BIOS reads a boot sector -  called the Master Boot Record (MBR) – which contains the primary boot loader. The MBR is a 512-byte sector, located in the first sector on the disk (sector 1 of cylinder 0, head 0). This is equal to LBA 0. After the entire MBR is loaded into RAM, the BIOS yields control to it.
The layout of the MBR, located in the very first sector of the hard disk.

  • The first 446 bytes of the MBR contain executable code – the primary boot loader.
  • The next 64 bytes hold a Partition Table which describes the partitions – or volumes – of a storage device. In this context the boot sector may also be known as a partition sector.
  • The last two bytes of the MBR contain a magic number. If the value is equal to 0xAA55 then the BIOS will assume it is dealing with a valid MBR. If it is not 0xAA55 the BIOS will produce an error message.

The Partition Table contains a maximum of four entries. Each entry specifies a primary partition, but one of the entries can specify an extended patition. Extended partitions are discussed later. The entries have the following format:
Byte OffsetField Length (Bytes)Meaning
001Boot Indicator
Indicates whether a partition is the system partition that contains the operating system. Legal values are:
0×00 = Do not use for booting
0×80 = System partition
Only one entry can be marked as the system partition.
013Starting CHS
These values pinpoint the location of a partition’s first sector, if  it’s within the first 1024 cylinders of a hard disk. Because sectors are 512 bytes, this boils down to a maximum of 8.4 GB. When a sector is beyond that point, the CHS tuples are normally set to their maximum allowed values of 1023, 254, 63; which stand for the 1024th cylinder, 255th head and 63rd sector, due to the fact that cylinder and head counts begin at zero. These values appear on the disk as the three bytes: FE FF FF (in that order).
041Partition typeThis byte defines the type of filesystem that was used to format the volume. An operating system can use this field to determine what file system drivers to load during startup. For example, the NTFS filesystem commonly used by Windows NT has partition type 0×07. A list of well-known types can be found here.
053Ending CHSThe CHS address of the last sector in the volume. The rules apply as for the starting sector field. The start and end sectors of a volume can be calculated using the Relative Sectors and Total Sectors fields.
084Starting Sector (LBA)
This value uniquely identifies the first sector of a partition just as Starting CHS values do. But it does so by using a 4-byte LBA. This means it can locate up to 2.19 TB – or 2 TiB.
When you obtain all 4 bytes of this value (as stored on a little-endian computer), the byte-order must first be reversed. So, if the value in the MBR is 3F 00 00 00, this becomes: 00 00 00 3F. This means that the partition begins at sector 0x3F, which is LBA 63 (or the 64th sector on the disk). This is the first possible boot sector for any drive having 63 sectors per head/track.
124Number of SectorsThis field specifies the exact amount of sectors in the volume. As with the starting sector field, it allows for a size of up to 2.19 TB.
The contents of a typical master boot record are displayed below. Note that the code section also contains some human-readable strings – these strings are used by the machine instructions to display messages depending on the code flow at runtime.

Due to the size of the MBR’s partiton table, there is a limit of 4 primary partitions. To get around this issue, engineers came up with a special partition type: the extended partition. A hard disk may contain a maximum of one extended partition. The extended partition can be subdivided into multiple logical partitions. In DOS/Windows systems, each logical partition may then be assigned an additional drive letter. In the MBR’s Partition Table, extended partition entries usually have a Partition Type of either 0x05 or 0x0Fdepending upon the size of the disk.
When the operating system encounters an extended partition type in the Partition Table entry, it will use the Starting Sectorfield to locate the first sector of the extended partition. In that sector it will look for a structure called the Extended Boot Record(EBR). This is a descriptor for a logical partition.
EBRs have exactly the same structure as the MBR; the only difference is that a some fields in the EBR go unused. The only used fields are the first and second Partion Table entries, along with the mandatory boot record signature (or magic number) of 0xAA55 at the end of the sector. This results in the following layout:
Byte OffsetField Length (Bytes)Meaning
0446Code area. Generally unused and filled with zeroes.
44616Partition table’s first entry. This entry points to the logical partition belonging to this EBR.
Starting Sector = relative offset between this EBR sector and the first sector of the logical partition. Usually 63 sectors.
Number of Sectors = total count of data sectors for this logical partition.
46216Partition table’s second entry. This entry will contain zero-bytes if it’s the last EBR in the extended partition. Otherwise, it points to the next EBR in the EBR chain.
Starting Sector = relative address of next EBR within extended partition.
Number of Sectors = total count of sectors for next logical partition, starting the count at the EBR.
47816Partition table’s third entry. Unused, filled with zeroes.
49416Partition table’s fourth entry. Unused, filled with zeroes.
5102Mandatory boot record signature (magic number): 0xAA55
An example disk layout containing 3 primary partition, followed by 1 extended partition with 2 logical partitions.
Note that every filesystem contains a Boot Sector of its own. This is called the Volume Boot Record (VBR). In the boot sequence the MBR is processed first, and eventually the VBR of the partition that contains the OS is processed.

We conclude this page with some food for thought: the contents of a Master Boot Record. Can you disassemble the code?
Example content of a Master Boot Record. Displayed are: hexadecimal (left) and ASCII (right).

No comments:

Post a Comment

Note: only a member of this blog may post a comment.