当前位置:网站首页>File system - basic knowledge of disk and detailed introduction to FAT32 file system

File system - basic knowledge of disk and detailed introduction to FAT32 file system

2022-06-25 02:06:00 jimte_ pro

1、 brief introduction

Due to the recent development of video storage function , So you need to understand the file system , This section first introduces the basic knowledge of disk .

2、 Mechanical drive

2.1 Physical structure of mechanical hard disk

Mechanical hard disk is mainly composed of disks , head , Disc shaft and control motor , Head controller , Data converter , Interface , Cache and other parts . As shown in the figure below :

 Insert picture description here

2.2 Logical structure of mechanical hard disk

A mechanical hard disk may consist of multiple disks , Each disc contains two faces , Each disk has a corresponding read / Write head . The mechanical hard disk logically divides the space into sectors 、 Magnetic track 、 Cylinder for management . The specific diagram is as follows :
 Insert picture description here

  • A sector : The internal circular metal disk of the hard disk is divided into several sector areas by the magnetic track , To store data , Read and write hard disk to sector as the basic unit .
  • block / cluster : Disk block / cluster ( Virtual ). Block is the smallest logical storage unit in the operating system . The smallest unit that the operating system deals with disk is disk block .

storage capacity = Number of heads × Magnetic track ( cylinder ) Count × Sectors per channel × Bytes per sector ;

The following command looks at SD Information about

# fdisk -l /dev/mmcblk0

Disk /dev/mmcblk0: 31.9 GB, 31927042048 bytes
4 heads, 16 sectors/track, 974336 cylinders
Units = cylinders of 64 * 512 = 32768 bytes
 Device Boot      Start         End      Blocks        Id System
/dev/mmcblk0p1      1          974336    31178744   c Win95 FAT32 (LBA)

3、FAT32 file system

3.1 FAT32 brief introduction

  • file system : The operating system is used to identify storage devices ( What's common is the disk , It's also based on NAND Flash Solid state drive ) Or the method and data structure of the file on the partition , That is, the method of organizing files on the storage device . The software organization responsible for managing and storing file information in the operating system is called file management system , File system for short .

  • FAT32 file system : FAT32 It refers to that the document allocation table adopts 32 Disk file management mode of bit binary number record management , because FAT The core of class file system is file allocation table , The name comes from .FAT32 It's from FAT and FAT16 Developed from , The advantage is good stability and compatibility , Fully compatible with Win 9X And previous versions , And easy to maintain . The disadvantage is poor security , And can only support 2T Partition , A single file can only support the maximum 4GB.

3.2 FAT32 Detailed explanation of memory data distribution structure

FAT32 The file system storage data distribution structure is as follows :

  • MBR A sector ;
  • Keep sectors ;
  • DBR A sector is a file system 0 A sector ;
  • FSINFO A sector , file system 1 A sector ;
  • Keep sectors ;
  • FAT surface ;
  • root directory ;
  • Data area ;

The specific schematic diagram is as follows :
 Insert picture description here
The following describes the above contents respectively :

  • MBR A sector :
    The first sector of the hard disk , front 446 Byte is the main boot record of the hard disk ( namely Master Boot Record,MBR), then 64 Bytes are partition tables , Last 2 Bytes are the end code ( Fixed for 55 AA). The following figure shows the partition table :
     Insert picture description here
    Partition table , Every time 16 Byte is a partition table entry ,64 Bytes can be stored in total 4 Partition table entries , The partition table data structure is as follows .
     Insert picture description here
    The data structure is as follows :
struct partition {
    
	uint8_t boot_ind;
	uint8_t head;
	uint8_t sector;
	uint8_t cyl;
	uint8_t sys_ind;
	uint8_t end_head;
	uint8_t end_sector;
	uint8_t end_cyl;
	uint8_t start[4];
	uint8_t size[4];
}__attribute__ ((__packed__));
  • DBR A sector :
    DBR The detailed structure of the sector is shown in the following figure :
     Insert picture description here
    The data structure is as follows :
struct msdos_boot_sector
{
    
	uint8_t boot_jump[3];
	uint8_t system_id[8];
	uint8_t sector_size[3];
	uint8_t cluster_size;
	uint16_t res; 
	uint8_t fats;
	uint8_t dir_entries[2];
	uint8_t sectors[2];
	uint8_t media;
	uint16_t fat_lenght;
	uint16_t secs_track;  
	uint16_t heads;
	uint32_t hidden;
	uint32_t total_sect;
    union 
    {
    
    	struct 
    	{
    
    		struct msdos_volume_info vi;
    		uint8_t boot_code[BOOTCODE_SIZE];
		}__attribute__ ((packed)) _oldfat;
		
		struct 
		{
    
			uint32_t fat32_lenght;
			uint16_t flags;
			uint8_t ver[2];
			uint32_t root_cluster;
			uint16_t info_sector;
			uint16_t backup_root;
			uint16_t res[2];
		}__attribute__ ((packed)) _fat32;
	}__attribute__ ((packed)) fstype;
	uint16_t boot_sign;
}
  • FSINFO A sector :
    FSINFO The sectors are as follows :
     Insert picture description here
    The specific data structure is shown in the figure below :
struct fat32_fsinfo {
    
	uint32_t res;
	uint32_t signature;
	uint32_t free_clusters;
	uint32_t next_cluster;
	uint32_t res[4];
}
  • FAT surface :
    The file system allocates disk space by cluster . For large documents , Multiple clusters need to be allocated . The data of the same file may not be completely stored in a continuous region of the disk , It is often divided into several paragraphs , Store like a chain . This storage method is called chain storage of files . In order to realize the chain storage of files , The file system must accurately record which clusters have been occupied by files , You must also specify the cluster number of the next cluster to store for each cluster that has been occupied , For the last cluster of files , It is necessary to indicate that there is no subsequent cluster in this cluster . These are all from FAT Watch to keep ,FAT The table entry records the information about the cluster it represents : Such as empty , Is it a bad cluster , Whether it is the tail cluster of a file .

The cluster address of each cluster is 32bit(4 Bytes ),FAT All byte positions in the table are marked with 4 Divided in bytes .

  • special :0x0FFFFFF8
  • end :0x0FFFFFFF
  • Bad cluster :0x0FFFFFF7
    among FAT surface 0、1 Fixed for 0x0FFFFFF8、 0x0FFFFFFF ,2 Cluster number is the root directory ;
     Insert picture description here
  • Catalog items :
    The sector where the directory is located , It's all about 32 Bytes Divide into one unit , Each unit is called a catalog entry , That is, the length of each directory entry is 32 Bytes . The root directory consists of several directory entries , A directory entry occupies 32 Bytes , It can be a long directory entry 、 Short directory entries 、“.” Catalog entries and “…” Catalog items, etc . Each file corresponds to a directory entry .
  • Short directory entries :
     Insert picture description here
    Its data structure :
typedef struct {
    
	uint8_t name[8];
	uint8_t ext[3];
	uint8_t lcase;
	uint8_t ctime_ms;
	uint16_t ctime;
	uint16_t cdate;
	uint16_t adate;
	uint16_t starthi;
	uint16_t time;
	uint16_t date;
	uint16_t start;
	uint32_t size;
} msdos_dir_entry;
  • Long catalog entry :
     Insert picture description here
    Its data structure :
typedef struct 
{
    
	uint8_t id;
	uint8_t name0_4[10];
	uint8_t attr;
	uint8_t res;
	uint8_t alias_checksum;
	uint8_t name5_10[12];
	uint16_t start;
	uint8_t name11_12[4]; 
} msdos_dir_slot;

3.3 FAT32 File system formatting process

FAT32 Formatting is mainly divided into 5 part :

  1. Set partition table ;
  2. FAT32 Of DBR A sector ;
  3. FSINFO A sector ;
  4. FAT surface ;
  5. Root directory initialization ;
  • Set partition table :
    –prepare_for_createfile
    ----getTotalSectors obtain SD Card total sector
    ----usrFdiskPartCreate according to SD Card size for partitioning
    ------add_partition_auto Fill in the partition table
    --------set_partition Set each partition table
    ----------set_start_sect Fill in the starting sector of the partition
    ----------set_nr_sects Fill in the total number of sectors in the partition
    ------write_part_table_flag Write the end code (0xAA55)
    ------write_sector Write memory data to SD card

  • FAT32 Of DBR Sector initialization :
    prepare_for_createfile Format the general interface
    –dosFsCreate format partition , Passable sector size 、 Cluster size and other parameters
    ----mkfs_main Format main function
    ------getopt Analytical parameters , At present, no parameters are brought in
    ------count_blocks Count the total number of device partitions
    ------open Open partition file , All formatting is to read and write the partition file
    ------establish_params Create a parameter
    --------media Set media media 0XF8
    --------cluster_size Set cluster size ,fs size >= 16G: 16k clusters
    --------setup_tables Set table parameters
    --------system_id File system flag and version number ,mkdosfs
    --------volume_id Volume serial number , Format time
    --------volume_label Volume label ,11 A space
    --------boot_jump Jump instruction
    --------fat32.boot_code Pilot code ,420 byte
    --------boot_sign End code ,0xAA55
    --------reserved Number of reserved sectors
    --------fats,FAT Number of tables ,2 individual
    --------hidden The number of hidden sectors , not used
    --------fat32.fat32_length,FAT Watch size
    --------fat32.flags sign ,0
    --------fat32.version Version number ,0
    --------fat32.root_cluster Root cluster number , Cluster number 2
    --------fat32.info_sector,FSINFO Fan area code , A sector 1
    --------fat32.backup_boot,DBR Sector backup sector number , A sector 6
    --------total_sect Total number of sectors in the file system
    --------ext_boot_sign Extended boot flag ,0x29

  • Root directory initialization :
    prepare_for_createfile
    –dosFsCreate format partition , Passable sector size 、 Cluster size and other parameters
    ----mkfs_main Format main function
    ------setup_tables Set table parameters
    --------de->name file name
    --------de-> ext Extension
    --------de->attr File attribute ATTR_VOLUME Volume label
    --------de->time Time
    --------de->date date
    --------de->starthi The starting cluster number of the file is high 2 position
    --------de->start The starting cluster number of the file is low 2 position
    --------de->size file size

  • FAT Table initialization :
    prepare_for_createfile
    –dosFsCreate format partition , Passable sector size 、 Cluster size and other parameters
    ----mkfs_main Format main function
    ------setup_tables Set table parameters
    --------mark_FAT_cluster (0, 0xffffffff) Set the first FAT Table item 0x0FFFFFF8
    --------mark_FAT_cluster (1, 0xffffffff) Set the first FAT Table item 0x0FFFFFFF
    --------mark_FAT_cluster (2, FAT_EOF) Set the root cluster number FAT Table item

  • FSINFO A sector :
    prepare_for_createfile
    –dosFsCreate format partition , Passable sector size 、 Cluster size and other parameters
    ----mkfs_main Format main function
    ------setup_tables Set table parameters
    -------- Extended boot flag 4 byte , character string “RRaA”
    -------- Unused area ,420 byte , Zeroing
    --------info->signature 4 byte , Signature 0x72724161
    --------info->free_clusters 4 byte , Number of empty clusters in the file system
    --------info->next_cluster 4 byte Next available cluster number ,2 Number
    -------info->next_cluster 4 byte Next available cluster number ,2 Number

  • Write to SD card :
    prepare_for_createfile
    –dosFsCreate format partition , Passable sector size 、 Cluster size and other parameters
    ----mkfs_main Format main function
    ------write_tables Write the data initialized in memory to SD card
    --------seekto Go to the offset address
    --------writebuf Write data

Reference material :
https://baike.baidu.com/item/FAT32/827339?fr=aladdin
https://blog.csdn.net/li_wen01/article/details/79929730/
https://blog.csdn.net/z1026544682/article/details/99759500

原网站

版权声明
本文为[jimte_ pro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242212167809.html