### Physical Structure of the Hard Disk:

The storage of data on a hard disk is realized based on the principle of electrical-magnetic conversion. A hard disk consists of one or several disk platters coated with magnetic material on the surface, as well as read/write heads installed on both sides of the platters and corresponding control circuits (Figure 1). The platters and heads are sealed in a dust-free metal case.
When the hard disk is in operation, the platters rotate at a high speed according to the designed rotational speed. The read/write heads set on the surface of the platters move radially to the specified position under the control of the circuit and then store or read data. When the system writes data to the hard disk, the "write data" current in the heads generates a magnetic field, causing a change in the state of the magnetic material on the surface of the platter. And it can remain after the write current magnetic field disappears, so that the data is stored. When the system reads data from the hard disk, the heads pass through the specified area of the platter. The magnetic field on the surface of the platter causes an induced current in the heads or a change in the impedance of the coil. After being processed by the relevant circuit, the data is restored. Therefore, as long as the surface of the platter is made smoother, the heads are designed more precisely, and the rotational speed of the platter is increased as much as possible, a hard disk with a larger capacity and faster data reading/writing speed can be manufactured. This is because the smoother the surface of the platter and the faster the rotational speed, the closer the heads can be to the surface of the platter, improving the reading and writing sensitivity and speed. The smaller and more precise the heads are designed, the smaller the space occupied by the heads on the platter, allowing the heads to establish more tracks on one platter to store more data.
### Logical Structure of the Hard Disk.
A hard disk is composed of many platters (platter). Each surface of each platter has a read/write head. If there are N platters, there are 2N surfaces, corresponding to 2N heads (Heads), numbered starting from 0, 1, 2. Each platter is divided into several concentric circular tracks (logically, it is invisible). The division rules of each platter are usually the same. In this way, the concentric circles with a fixed radius R for each platter logically form a cylinder (Cylinders) with the motor spindle as the axis, numbered from 0, 1, 2... from the outside to the inside. Each track on each platter is further divided into dozens of sectors (Sector), usually with a capacity of 512 bytes, and numbered as 1, 2, 3... according to a certain rule to form Cylinders × Heads × Sector sectors. These three parameters are the physical parameters of the hard disk. Many of our subsequent practices need to deeply understand the meanings of these three parameters.
### Disk Booting Principle.
#### 3.1 MBR (Master Boot Record) Sector:
After the computer presses the power key, it starts to execute the motherboard BIOS program. After a series of inspections and configurations, it starts to boot the system according to the system boot sequence set in the BIOS. Assume it is the hard disk now. After the BIOS executes its own program, how does it hand over the execution power to the hard disk? After handing over to the hard disk, which program stored where is executed? In fact, a section of code called MBR plays a crucial role. MBR (Master Boot Record), that is, the master boot record, is sometimes also called the master boot sector. It is located in the 0 cylinder 0 head 1 sector of the entire hard disk (which can be regarded as the first sector of the hard disk). After the BIOS executes its own inherent program, it will jump to the first instruction in the MBR and hand over the control of the system to the MBR for execution. In the master boot record with a total of 512 bytes, the boot program of the MBR occupies the first 446 bytes (offset 0H ~ offset 1BDH). The subsequent 64 bytes (offset 1BEH ~ offset 1FDH) are the DPT (Disk Partition Table, hard disk partition table). The last two bytes "55 AA" (offset 1FEH ~ offset 1FFH) are the partition valid end mark.
The MBR is not different with different operating systems, that is, different operating systems may have the same MBR. Even if they are different, the MBR will not carry the nature of the operating system. It has the characteristic of public booting.
Let's analyze a section of MBR. The following is the MBR of a Seagate 120GB hard disk viewed with WinHex.

The MBR boot code of your hard disk may not be like this. However, even if it is different, the functions performed are generally the same. Here, find the decompilation of disk MBR by wowocock, which has been added with detailed comments. If you are interested, you can study it carefully.
Let's look at the DPT part. In order to facilitate users' management of the disk, the operating system has added the concept of disk partitioning, that is, dividing a disk logically into several parts. The number of disk partitions is only limited by the number of English letters from C to Z. How to represent the attributes of multiple partitions in the 64 bytes of the DPT in the above figure? Microsoft solves this problem by the link method. In the 64 bytes of the DPT, a partition's attributes are described with 16 bytes as a partition table entry unit. That is, the first partition table entry describes the attributes of a partition, generally the primary partition. The second partition table entry describes the remaining space except the primary partition, generally speaking, it is the extended partition we call. The general description of this part is shown in Table 1.

Note: The data exceeding 1 byte in the above table is displayed as the actual data, that is, displayed in the way from high bit to low bit. When storing, it is stored from low bit to high bit. The two are different, please see clearly. The same applies to the tables and figures that appear later.
You can also see the meaning of these parameters in WinHex:

Explanation: Each partition table entry occupies 16 bytes. Assume that the offset address starts from 0. For example, partition table entry 3 in Figure 3. Partition table entry 4 is the same as partition table entry 3.
1. The offset 0H is the active partition flag, which can only be 00H and 80H. 80H is active, and 00H is inactive. Other values are illegal values for Microsoft.
2. Re-explain (this is very important): Numbers greater than 1 byte are stored in the little endian format, also known as the reverse byte order. The little endian format is a way to store numbers, so that the lowest byte appears first in the hexadecimal number symbol. For example, the value of the relative sector number field 0x3F000000 is represented as 0x0000003F in the little endian format. The decimal number of this little endian format number is 63.
3. When the system partitions, each partition is not allowed to cross cylinders, that is, all are in units of cylinders. This is the so-called partition granularity. Sometimes when we partition, we enter the partition size as 7000M, but it turns out to be 6997M. This is the reason. In the sector and cylinder parameters of offset 2H and offset 6H, the sector occupies 6 bits (bit), and the cylinder occupies 10 bits (bit). Taking offset 6H as an example, its low 6 bits are used as the binary representation of the number of sectors. Its high two bits are used as the high two bits of the 10-bit cylinder number, and the 8 bits composed of offset 7H are used as the low 8 bits of the 10-bit cylinder number. From this, it can be seen that the partition capacity represented in this way is limited. The cylinder and head are numbered starting from 0, and the sector is numbered starting from 1. So the maximum can only represent 1024 cylinders × 63 sectors × 256 heads × 512 bytes = 8455716864 bytes. That is, the usual 8.4GB (actually it should be about 7.8GB) limit. In fact, the number of heads is usually only used up to 255 (determined by the addressing register of the assembly language). Even if these 3 bytes are linearly addressed, it is still insufficient. In later operating systems, partitions exceeding 8.4GB are no longer addressed by the C/H/S method. Instead, the 4-byte 32-bit linear sector address from offset CH ~ offset FH is used to represent the total number of sectors occupied by the partition. It can be seen that 2^32 sectors can be represented by 4 bytes, that is, 2TB = 2048GB, which is an astronomical number for most computers at present. On partitions not exceeding 8.4GB, the representation methods of C/H/S and linear sectors represent the same partition size. That is, the two representation methods are coordinated. Even if they are not coordinated, the linear addressing is used as the standard. (There may be errors prompted in some systems). The end C/H/S of partitions exceeding 8.4GB is generally filled with FEH FFH FFH. That is, the maximum value that C/H/S can represent. Sometimes it is also filled with the modulo of the cylinder to 1024. However, what these bytes are actually is irrelevant.
Although current systems all use the linear addressing method to handle the partition size, the principle of not crossing cylinders remains unchanged. The total number of sectors of this partition plus the number of reserved sectors between it and the previous partition must still be an integer multiple of the cylinder capacity. (The first sector in the reserved sectors is the sector where the partition table is stored, MBR or virtual MBR. The total number of sectors of the partition is not counted in the reserved sectors in the linear representation method. If it is the first partition, the reserved sectors are all sectors before this partition.
Attachment: Partition table type flag is shown in Figure 4

#### 3.2 Extended Partition
Each logical drive in the extended partition has an extended boot record (Extended Boot Record, EBR) similar to the MBR, which is also called virtual MBR or extended MBR, meaning the same. The extended boot record includes an extended partition table and the label of this sector. The extended boot record will record the information of the first surface of the first cylinder of each logical drive in the extended partition. The boot sector in a logical drive is generally located at relative sector 32 or 63. However, if there is no extended partition on the disk, there will be no extended boot record and logical drives. The first item in the extended partition table of the first logical drive points to its own boot sector. The second item points to the EBR of the next logical drive. If there are no further logical drives, the second item will not be used and is recorded as a series of zeros. If there are additional logical drives, then the first item of the extended partition table of the second logical drive will point to its own boot sector. The second item of the extended partition table of the second logical drive points to the EBR of the next logical drive. The third and fourth items of the extended partition table are never used.
Through a disk structure diagram of 4 partitions, the general organization form of the disk can be seen. As shown in Figure 5:

Regarding the extended partition, as shown in Figure 6, the extended boot record of the logical drive in the extended partition is a connection table. This figure shows three logical drives on an extended partition, illustrating the differences in the extended partition table between the previous logical drive and the last logical drive.

Except for the last logical drive on the extended partition, the format of the extended partition table described in Table 2 is repeated in each logical drive: the first item identifies the boot sector of the logical drive itself, and the second item identifies the EBR of the next logical drive. The extended partition table of the last logical drive will only list its own partition item. The second to fourth items of the last extended partition table are used.

The relative sector number field in the extended partition table entry shows the number of bytes of the displacement from the start of the extended partition to the first sector in the logical drive. The number in the total sector number field refers to the number of sectors composing this logical drive. The value of the total sector number field is equal to the number of sectors from the boot sector defined by the extended partition table entry to the end of the logical drive.
Sometimes there will be remaining space at the end of the disk. What is the remaining space? We mentioned earlier that the partition takes the capacity of 1 cylinder as the partition granularity. So if the total space of the disk is not an integer number of cylinders, the remaining space that is less than one cylinder is the remaining space. This part of the space is not involved in partitioning, so it is generally not available. According to reason, the physical mode of the disk determines that the total capacity of the disk should be exactly an integer number of cylinder capacities. Why is there space less than one cylinder? In my understanding, originally, in order to make better use of space, the current disks are generally not managed in the way that the sectors on the outer circle are larger than those on the inner circle at present. It is just CHS abstracted to be compatible with the operating system. Maybe its actual space capacity is not exactly an integer number of cylinder capacities. Regarding this point, if you have any opinions, please inform http://www.sjhf.net or zymail@vip.sina.com

The storage of data on a hard disk is realized based on the principle of electrical-magnetic conversion. A hard disk consists of one or several disk platters coated with magnetic material on the surface, as well as read/write heads installed on both sides of the platters and corresponding control circuits (Figure 1). The platters and heads are sealed in a dust-free metal case.
When the hard disk is in operation, the platters rotate at a high speed according to the designed rotational speed. The read/write heads set on the surface of the platters move radially to the specified position under the control of the circuit and then store or read data. When the system writes data to the hard disk, the "write data" current in the heads generates a magnetic field, causing a change in the state of the magnetic material on the surface of the platter. And it can remain after the write current magnetic field disappears, so that the data is stored. When the system reads data from the hard disk, the heads pass through the specified area of the platter. The magnetic field on the surface of the platter causes an induced current in the heads or a change in the impedance of the coil. After being processed by the relevant circuit, the data is restored. Therefore, as long as the surface of the platter is made smoother, the heads are designed more precisely, and the rotational speed of the platter is increased as much as possible, a hard disk with a larger capacity and faster data reading/writing speed can be manufactured. This is because the smoother the surface of the platter and the faster the rotational speed, the closer the heads can be to the surface of the platter, improving the reading and writing sensitivity and speed. The smaller and more precise the heads are designed, the smaller the space occupied by the heads on the platter, allowing the heads to establish more tracks on one platter to store more data.
### Logical Structure of the Hard Disk.
A hard disk is composed of many platters (platter). Each surface of each platter has a read/write head. If there are N platters, there are 2N surfaces, corresponding to 2N heads (Heads), numbered starting from 0, 1, 2. Each platter is divided into several concentric circular tracks (logically, it is invisible). The division rules of each platter are usually the same. In this way, the concentric circles with a fixed radius R for each platter logically form a cylinder (Cylinders) with the motor spindle as the axis, numbered from 0, 1, 2... from the outside to the inside. Each track on each platter is further divided into dozens of sectors (Sector), usually with a capacity of 512 bytes, and numbered as 1, 2, 3... according to a certain rule to form Cylinders × Heads × Sector sectors. These three parameters are the physical parameters of the hard disk. Many of our subsequent practices need to deeply understand the meanings of these three parameters.
### Disk Booting Principle.
#### 3.1 MBR (Master Boot Record) Sector:
After the computer presses the power key, it starts to execute the motherboard BIOS program. After a series of inspections and configurations, it starts to boot the system according to the system boot sequence set in the BIOS. Assume it is the hard disk now. After the BIOS executes its own program, how does it hand over the execution power to the hard disk? After handing over to the hard disk, which program stored where is executed? In fact, a section of code called MBR plays a crucial role. MBR (Master Boot Record), that is, the master boot record, is sometimes also called the master boot sector. It is located in the 0 cylinder 0 head 1 sector of the entire hard disk (which can be regarded as the first sector of the hard disk). After the BIOS executes its own inherent program, it will jump to the first instruction in the MBR and hand over the control of the system to the MBR for execution. In the master boot record with a total of 512 bytes, the boot program of the MBR occupies the first 446 bytes (offset 0H ~ offset 1BDH). The subsequent 64 bytes (offset 1BEH ~ offset 1FDH) are the DPT (Disk Partition Table, hard disk partition table). The last two bytes "55 AA" (offset 1FEH ~ offset 1FFH) are the partition valid end mark.
The MBR is not different with different operating systems, that is, different operating systems may have the same MBR. Even if they are different, the MBR will not carry the nature of the operating system. It has the characteristic of public booting.
Let's analyze a section of MBR. The following is the MBR of a Seagate 120GB hard disk viewed with WinHex.

The MBR boot code of your hard disk may not be like this. However, even if it is different, the functions performed are generally the same. Here, find the decompilation of disk MBR by wowocock, which has been added with detailed comments. If you are interested, you can study it carefully.
Let's look at the DPT part. In order to facilitate users' management of the disk, the operating system has added the concept of disk partitioning, that is, dividing a disk logically into several parts. The number of disk partitions is only limited by the number of English letters from C to Z. How to represent the attributes of multiple partitions in the 64 bytes of the DPT in the above figure? Microsoft solves this problem by the link method. In the 64 bytes of the DPT, a partition's attributes are described with 16 bytes as a partition table entry unit. That is, the first partition table entry describes the attributes of a partition, generally the primary partition. The second partition table entry describes the remaining space except the primary partition, generally speaking, it is the extended partition we call. The general description of this part is shown in Table 1.

Note: The data exceeding 1 byte in the above table is displayed as the actual data, that is, displayed in the way from high bit to low bit. When storing, it is stored from low bit to high bit. The two are different, please see clearly. The same applies to the tables and figures that appear later.
You can also see the meaning of these parameters in WinHex:

Explanation: Each partition table entry occupies 16 bytes. Assume that the offset address starts from 0. For example, partition table entry 3 in Figure 3. Partition table entry 4 is the same as partition table entry 3.
1. The offset 0H is the active partition flag, which can only be 00H and 80H. 80H is active, and 00H is inactive. Other values are illegal values for Microsoft.
2. Re-explain (this is very important): Numbers greater than 1 byte are stored in the little endian format, also known as the reverse byte order. The little endian format is a way to store numbers, so that the lowest byte appears first in the hexadecimal number symbol. For example, the value of the relative sector number field 0x3F000000 is represented as 0x0000003F in the little endian format. The decimal number of this little endian format number is 63.
3. When the system partitions, each partition is not allowed to cross cylinders, that is, all are in units of cylinders. This is the so-called partition granularity. Sometimes when we partition, we enter the partition size as 7000M, but it turns out to be 6997M. This is the reason. In the sector and cylinder parameters of offset 2H and offset 6H, the sector occupies 6 bits (bit), and the cylinder occupies 10 bits (bit). Taking offset 6H as an example, its low 6 bits are used as the binary representation of the number of sectors. Its high two bits are used as the high two bits of the 10-bit cylinder number, and the 8 bits composed of offset 7H are used as the low 8 bits of the 10-bit cylinder number. From this, it can be seen that the partition capacity represented in this way is limited. The cylinder and head are numbered starting from 0, and the sector is numbered starting from 1. So the maximum can only represent 1024 cylinders × 63 sectors × 256 heads × 512 bytes = 8455716864 bytes. That is, the usual 8.4GB (actually it should be about 7.8GB) limit. In fact, the number of heads is usually only used up to 255 (determined by the addressing register of the assembly language). Even if these 3 bytes are linearly addressed, it is still insufficient. In later operating systems, partitions exceeding 8.4GB are no longer addressed by the C/H/S method. Instead, the 4-byte 32-bit linear sector address from offset CH ~ offset FH is used to represent the total number of sectors occupied by the partition. It can be seen that 2^32 sectors can be represented by 4 bytes, that is, 2TB = 2048GB, which is an astronomical number for most computers at present. On partitions not exceeding 8.4GB, the representation methods of C/H/S and linear sectors represent the same partition size. That is, the two representation methods are coordinated. Even if they are not coordinated, the linear addressing is used as the standard. (There may be errors prompted in some systems). The end C/H/S of partitions exceeding 8.4GB is generally filled with FEH FFH FFH. That is, the maximum value that C/H/S can represent. Sometimes it is also filled with the modulo of the cylinder to 1024. However, what these bytes are actually is irrelevant.
Although current systems all use the linear addressing method to handle the partition size, the principle of not crossing cylinders remains unchanged. The total number of sectors of this partition plus the number of reserved sectors between it and the previous partition must still be an integer multiple of the cylinder capacity. (The first sector in the reserved sectors is the sector where the partition table is stored, MBR or virtual MBR. The total number of sectors of the partition is not counted in the reserved sectors in the linear representation method. If it is the first partition, the reserved sectors are all sectors before this partition.
Attachment: Partition table type flag is shown in Figure 4

#### 3.2 Extended Partition
Each logical drive in the extended partition has an extended boot record (Extended Boot Record, EBR) similar to the MBR, which is also called virtual MBR or extended MBR, meaning the same. The extended boot record includes an extended partition table and the label of this sector. The extended boot record will record the information of the first surface of the first cylinder of each logical drive in the extended partition. The boot sector in a logical drive is generally located at relative sector 32 or 63. However, if there is no extended partition on the disk, there will be no extended boot record and logical drives. The first item in the extended partition table of the first logical drive points to its own boot sector. The second item points to the EBR of the next logical drive. If there are no further logical drives, the second item will not be used and is recorded as a series of zeros. If there are additional logical drives, then the first item of the extended partition table of the second logical drive will point to its own boot sector. The second item of the extended partition table of the second logical drive points to the EBR of the next logical drive. The third and fourth items of the extended partition table are never used.
Through a disk structure diagram of 4 partitions, the general organization form of the disk can be seen. As shown in Figure 5:

Regarding the extended partition, as shown in Figure 6, the extended boot record of the logical drive in the extended partition is a connection table. This figure shows three logical drives on an extended partition, illustrating the differences in the extended partition table between the previous logical drive and the last logical drive.

Except for the last logical drive on the extended partition, the format of the extended partition table described in Table 2 is repeated in each logical drive: the first item identifies the boot sector of the logical drive itself, and the second item identifies the EBR of the next logical drive. The extended partition table of the last logical drive will only list its own partition item. The second to fourth items of the last extended partition table are used.

The relative sector number field in the extended partition table entry shows the number of bytes of the displacement from the start of the extended partition to the first sector in the logical drive. The number in the total sector number field refers to the number of sectors composing this logical drive. The value of the total sector number field is equal to the number of sectors from the boot sector defined by the extended partition table entry to the end of the logical drive.
Sometimes there will be remaining space at the end of the disk. What is the remaining space? We mentioned earlier that the partition takes the capacity of 1 cylinder as the partition granularity. So if the total space of the disk is not an integer number of cylinders, the remaining space that is less than one cylinder is the remaining space. This part of the space is not involved in partitioning, so it is generally not available. According to reason, the physical mode of the disk determines that the total capacity of the disk should be exactly an integer number of cylinder capacities. Why is there space less than one cylinder? In my understanding, originally, in order to make better use of space, the current disks are generally not managed in the way that the sectors on the outer circle are larger than those on the inner circle at present. It is just CHS abstracted to be compatible with the operating system. Maybe its actual space capacity is not exactly an integer number of cylinder capacities. Regarding this point, if you have any opinions, please inform http://www.sjhf.net or zymail@vip.sina.com

DigestIII


























