Master Boot Record
As explain before ,The Master Boot Record is the same for pretty much all Operating Systems. It is located on the first Sector of the Hard Drive, at Cylinder 0, Head 0, Sector 1. It also contains the partition table, which defines the different sections of your hard drive. Basically if anything happens to this little 512 byte section, your hard drive is brain dead. In this article I will explain about FAT 32 structure that we use to make a 32 bit FAT 32 boot loader.
FAT32 Boot Record
This information is located in the first sector of every partition.
Offset | Description | Size |
00h | Jump Code + NOP | 3 Bytes |
03h | OEM Name | 8 Bytes |
0Bh | Bytes Per Sector | 1 Word |
0Dh | Sectors Per Cluster | 1 Byte |
0Eh | Reserved Sectors | 1 Word |
10h | Number of Copies of FAT | 1 Byte |
11h | Maximum Root DirectoryEntries (N/A for FAT32) | 1 Word |
13h | Number of Sectors inPartition Smaller than 32MB (N/A for FAT32) | 1 Word |
15h | Media Descriptor (F8h forHard Disks) | 1 Byte |
16h | Sectors Per FAT in Older FAT Systems (N/A for FAT32) | 1 Word |
18h | Sectors Per Track | 1 Word |
1Ah | Number of Heads | 1 Word |
1Ch | Number of Hidden Sectors in Partition | 1 Double Word |
20h | Number of Sectors in Partition | 1 Double Word |
24h | Number of Sectors Per FAT | 1 Double Word |
28h | Flags (Bits 0-4 IndicateActive FAT Copy) (Bit 7 Indicates whether FAT Mirroringis Enabled or Disabled <Clear is Enabled>) (If FAT Mirroring is Disabled, the FAT Information is only written to the copy indicated by bits 0-4) | 1 Word |
2Ah | Version of FAT32 Drive (HighByte = Major Version, Low Byte = Minor Version) | 1 Word |
2Ch | Cluster Number of the Start of the Root Directory | 1 Double Word |
30h | Sector Number of the File System Information Sector (See Structure Below)(Referenced from the Start of the Partition) | 1 Word |
32h | Sector Number of the Backup Boot Sector (Referenced from the Start of the Partition) | 1 Word |
34h | Reserved | 12 Bytes |
40h | Logical Drive Number of Partition | 1 Byte |
41h | Unused (Could be High Byte of Previous Entry) | 1 Byte |
42h | Extended Signature (29h) | 1 Byte |
43h | Serial Number of Partition | 1 Double Word |
47h | Volume Name of Partition | 11 Bytes |
52h | FAT Name (FAT32) | 8 Bytes |
5Ah | Executable Code | 420 Bytes |
1FEh | Boot Record Signature (55hAAh) | 2 Bytes |
File System Information Sector
Usually this is the Second Sector of the partition, although since there is a reference in the Boot Sector to it, I'm assuming it can be moved around. I never got a complete picture of this one. Although I do know where the important fields are at.
Offset | Description | Size |
00h | First Signature (52h 52h 61h41h) | 1 Double Word |
04h | Unknown, Currently (Might just be Null) | 480 Bytes |
1E4h | Signature of FS Info Sector (72h 72h 41h 61h) | 1 Double Word |
1E8h | Number of Free Clusters (Set to -1 if Unknown) | 1 Double Word |
1ECh | Cluster Number of Cluster that was Most Recently Allocated. | 1 Double Word |
1F0h | Reserved | 12 Bytes |
1FCh | Unknown or Null | 2 Bytes |
1FEh | Boot Record Signature (55hAAh) | 2 Bytes |
FAT32 Drive Layout
FAT 32 Drive layout is use when you need to load your kernel or second stage boot loader. FAT 32 Drive lay out makes us know where the kernel located. First you need to load FAT to memory so the first thing we do is compute the sector where FAT located. Second thing you do is to load root directory to memory and we must to compute sector of root directory. From the FAT and root directory you can locate the kernel and load it to memory.
Offset | Description |
Start of Partition | Boot Sector |
Start + # of ReservedSectors | Fat Tables |
Start + # of Reserved + (#of Sectors Per FAT * 2) <Assuming that FAT Mirroring is Enabled, I personally haven't seen a case where it wasn't, but I guess there is always the possibility> | Data Area (Starts with Cluster #2) |
Cluster Meaning
A Cluster is a Group of Sectors on the Hard Drive that have information in them. A 4K Cluster has 8 Sectors in it (512*8=4096). Each Cluster is given a spot in the FAT Table. When you look at an Entry in the FAT, the number there tells you whether or not that cluster has data in it, and if so, if it is the end of the data or there is another cluster after it. All Data on a Partition starts with Cluster #2. If the FAT Entry is 0, then there is no data in that cluster. If the FAT Entry is 0FFFFFFFh, then it is the last entry in the chain.
This is one of my biggest holes in my information. I am unable to find anyplace that shows what numbers mean what when it comes to the FAT table. I was able to tell the end of the chain just by looking at a FAT32 Drive, but I don't know what stands for a BAD Cluster or what the maximum valid number for showing data is.
For now, you can calculate the maximum valid cluster in a partition with this formula:
( (# of Sectors in Partition) - (# of Sectors per Fat * 2) - (# of Reserved Sectors) ) / (# of Sectors per Cluster)
If there is any remainder in the answer to that formula, it just means that there were a few extra clusters at the end of the partition (probably not enough to make another cluster), so you can just get rid of anything after the decimal point.
Directory Table
Another aspect when looking at a File System at Low Level is the Directory Table. The Directory Table is what stores all of the File and Directory Entries. Basically there is only one difference between the Directory Table of FAT16 and FAT32. The Difference is : the Reserved OS/2 Byte (Offset 20 [14h]) in the Short Filename Structure is replaced with the High Word of the Cluster Number (since it's now 4 bytes instead of 2).
This is the example of my FAT 32 BPB on 2 GB flash disk. I don't develop the boot code yet because is very hard to debug it.
org 7c00h
jmp start
nop
bs_OEM db "MSDOS5.0"
A_BF_BPB_BytesPerSector DW 200h
A_BF_BPB_SectorsPerCluster DB 8h
A_BF_BPB_ReservedSectors DW 01f0h
A_BF_BPB_NumberOfFATs DB 02
A_BF_BPB_RootEntries DW 0
A_BF_BPB_TotalSectors DW 0
A_BF_BPB_MediaDescriptor DB 0f8h
A_BF_BPB_SectorsPerFAT DW 0
A_BF_BPB_SectorsPerTrack DW 03fh
A_BF_BPB_Heads DW 0ffh
A_BF_BPB_HiddenSectors DW 080h
A_BF_BPB_HiddenSectorsHigh DW 0h
A_BF_BPB_BigTotalSectors DW 3f00h
A_BF_BPB_BigTotalSectorsHigh DW 03ch
A_BF_BPB_BigSectorsPerFat DW 0f08h
A_BF_BPB_BigSectorsPerFatHi DW 0h
A_BF_BPB_ExtFlags DW 0
A_BF_BPB_FS_Version DW 0
A_BF_BPB_RootDirStrtClus DW 2
A_BF_BPB_RootDirStrtClusHi DW 0
A_BF_BPB_FSInfoSec DW 1h
A_BF_BPB_BkUpBootSec DW 6h
A_BF_BPB_Reserved DW 6 DUP (?)
BS_physical db 80h
db 00
BS_bootrecord db 29h
volumeid dd 0AE6DA6B2h
volumelabel db "No Name "
filesystem db "FAT32 "
source
http://www.easeus.com/resource/fat32-disk-structure.htm