Implementation of FAT32 file system boot Sector

Source: Internet
Author: User
Tags relative reserved valid
Read Yu Yu wrote "Self-write operating system", want to start the code from the floppy disk FAT12 file system to the hard disk FAT32 file system, after all, the current floppy disk is almost no longer used, and hard disk and USB stick is the mainstream. The basic idea is to use the assembly code in real mode, read the hard disk MBR, analyze the hard disk partition table, find the first FAT32 active partition, analyze the FAT32 partition, Locate the Osloader.bin file in the partition, load the Osloader.bin file to the memory specified address, and give the execution to Osloader.bin.
Environment: Ubuntu 14.04 Bochs
Background: MBR, the hard disk master boot record, is the first sector of the hard disk, MBR sector total 512 bytes, the first 446 bytes is the boot code, then 64 bytes is the partition table entry, the last two bytes is a valid flag 0xaa55, that is, the MBR structure is as follows: 0x0000~0x01bd: Master Bootstrapper 0X01BE~0X01FD: Primary partition Table 0x01fe~0x01ff: valid tag, 0xaa55
The primary partition table consists of 4 items, each of 16 bytes, a total of 64 bytes, each describing the basic information of a partition. 16-byte partition table entry structure: 0x00: The boot indicator indicates whether the partition is active partition 0x01: Start head 0x02: Start sector, use only 0~5 bit, the following 2 bits are used by the starting cylinder 0x03: Starting cylinder, using the last 2 bits of the start sector, total 10 bits, maximum value 1023 0X04: System ID, defines the type of partition 0x05: End-of-head 0x06: End sector, using only the 0~5 bit, the last 2 bits of the end of the cylinder used 0x07: End cylinder, using the last 2 bits of the end sector, a total of 10 bits, the maximum value is 1023 0X08: The amount of displacement from the beginning of the disk to the beginning of the partition, in sectors, that is, the relative number of sectors 0x0C: The total number of sectors of the partition
FAT32 file system in the first sector of the FAT32 partition, the relevant parameters of the FAT32 partition are recorded, such as the number of reserved sectors before the FAT table, the number of fat tables, how many sectors the fat table occupies, the number of sectors occupied by each cluster, the cluster number where the root is located, and so on. Its structure is as follows: 0x00~0x02:3 byte, jump instruction 0x03~0x0a:8 byte, file system flag and version number 0x0b~0x0c:2 bytes, bytes per sector 0x0d~0x0d:1 bytes, number of sectors per cluster 0x0e~0x0f:2 bytes, reserved sector number, That is, FAT1 the number of sectors 0x10~0x10:1 bytes, fat tables, usually 2 0x11~0x12:2 bytes relative to the partition start address, FAT32 must be equal to 0,fat12/fat16 as the number of directories in the root directory 0x13~0x14:2 bytes, FAT32 must be equal to 0,fat12/fat16 for the total number of sectors 0x15~0x15:1 bytes, which storage medium, 0xf8 standard value, Removable storage media, common  0xf0 0x16~0x17:2 bytes, FAT32 must be 0,fat12/ FAT16 is the number of sectors that a fat  table occupies. 0x18~0x19:2 bytes, number of sectors per track, only valid 0x1a~0x1b:2 bytes for storage medium with "special shape" (by the head and cylinder per partition of several tracks), number of heads, only valid for special media 0x1c~0x1f:4 bytes, number of sectors hidden before EBR partition 0x20~0x23:4 bytes, the total number of file system sectors 0x24~0x27:4 bytes, each fat table occupies the number of sectors 0x28~0x29:2 bytes, tags, this domain fat32  unique 0x2a~0x2b:2 bytes, FAT32 version number 0.0,fat32 a unique 0x2c~0x2f:4 byte, the cluster number of the first cluster in which the root is located, usually starting at 2nd cluster 0x30~0x31:2 bytes, fsinfo (file System Information Sector) sector code 1, This sector provides the operating system with information about the total number of empty clusters and the next available cluster 0x32~0x33:2 bytes, where the boot sector is backed up. The backup boot sector is always located in the file system's 6th sector 0x34~0x3f:12 bytes, used for later fat  extensions to use 0x40~0x40:1 bytes, the same as the fat12/16  definition, except that they are located in a different boot sector location 0x41~ The 0x41:1 byte is the same as the definition of fat12/16 , except that the two are in different positions in the boot sector 0x42~0x42:1 bytes, extended boot flags, 0x29. and FAT12/1The 6  definition is the same, except that the two are in different positions in the boot sector 0x43~0x46:4 Byte, volume serial number. Typically a random value of 0x47~0x51:11 bytes, volume label (ASCII code), if the file system is established when the volume label is specified, will be saved in this 0x52~0x59:8 byte, file system format ASCII code, FAT32 0x5a~0x1fd:90~ 509 total 410 bytes, not used. This section does not have a clear purpose 0x1fe~0x1ff: Signature sign 0xaa55
Fat table Fat table records whether a cluster is used, and each table entry occupies 4 bytes. Because the cluster number starts at number 2nd, the No. 0 and 1th table entries of the FAT table entries do not correspond to any clusters. FAT32 the value of table No. 0 is always "f8ffff0f", and the 1th table entry may be used to record dirty flags to indicate that the file system has not been unloaded properly or that there is an error on the disk surface. But this value doesn't matter. Under normal conditions, the value of table 1th is "FFFFFFFF" or "ffffff0f". If a cluster is not allocated for use, its corresponding FAT table entry content is 0, and when a cluster is already allocated, the FAT table entry value within its corresponding FAT table entry is the cluster number of the next storage location for the file. If the file ends in the cluster, a file end tag is recorded in its fat table entry, and for FAT32, the Fat table entry value for the end of the file is 0x0fffffff. If a cluster has bad sectors, the entire cluster is marked as a bad cluster with 0XFFFFFF7, and the bad cluster tag is recorded in its corresponding fat table entry.
FAT32 root zone, each catalog table entry occupies 32 bytes, for short filenames, 32 bytes is sufficient, for long filenames will use more than one catalog table entry, specifically refer to other articles. FAT32 Short file directory structure:


The above is a brief introduction of MBR, primary partition table and FAT32 partition information, with the above information can be used in assembly language to read the FAT32 files in memory, and jump to the file to execute.
Set up an experimental environment: Use the Bochs tool in Ubuntu system Bximage generate a 100M virtual hard disk file c.img partition The c.img, this is just one area. Format the partition for c.img, use Mkfs.vfat to mount the FAT32 partition as a FAT32 file system, and copy the Osloader.bin file to the partition

We use the assembly to implement the FAT32 boot sector, the main process is:
The computer starts, loading the MBR sector to the memory address 0x7c00, where the first 446 bytes of the MBR sector are the assembly code we write. First call INT 13 to get the disk parameters, then analyze the primary partition table, find the first active partition, the partition table entry the first byte of the 0x80 partition, read the first sector of the partition through INT 13, get the FAT32 partition parameter, and then retrieve the FAT32 root directory. Look for the cluster occupied by the Osloader.bin file, read the file to the memory specified address, jump to the address to continue execution.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.