当前位置:网站首页>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 :

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 :
- 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 :
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 :
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 .
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 :
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 :
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 ;
- 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 :

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 :

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 :
- Set partition table ;
- FAT32 Of DBR A sector ;
- FSINFO A sector ;
- FAT surface ;
- 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 cardFAT32 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 ,0x29Root 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 sizeFAT 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 itemFSINFO 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 NumberWrite 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
边栏推荐
- How to monitor the log through the easycvr interface to observe the platform streaming?
- How do the TMUX color palette work?
- How to quickly familiarize yourself with the code when you join a new company?
- Specific list of regular and safe domestic stock trading account opening
- Unity C# 网络学习(六)——FTP(一)
- Poj3669 meteor shower (BFS pretreatment)
- LogMiner 数据库日志挖掘
- Abnova CSV magnetic beads description in Chinese and English
- 监听 Markdown 文件并热更新 Next.js 页面
- Unity C # e-learning (VI) -- FTP (II)
猜你喜欢

jwt

Constant current circuit composed of 2 NPN triodes

左手梦想 右手责任 广汽本田不光关注销量 还有儿童安全

Day 04 - file IO

业务与技术双向结合构建银行数据安全管理体系

How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site

創新藥二級市場審餅疲勞:三期臨床成功、產品獲批也不管用了

Numerical scheme simulation of forward stochastic differential equations with Markov Switching

Experience of epidemic prevention and control, home office and online teaching | community essay solicitation

入职一家新公司,如何快速熟悉代码?
随机推荐
你知道你的ABC吗(春季每日一题 1)
Stocking but not completely stocking (daily question 2 in spring)
Basic use of transformers Library
Search two-dimensional matrix [clever use of bisection + record solution different from inserting bisection]
非凸联合创始人李佐凡:将量化作为自己的终身事业
Android物联网应用程序开发(智慧园区)—— 设置传感器阈值对话框界面
2个NPN三极管组成的恒流电路
Smartctl 打开设备遇到 Permission denied 问题排查过程记录
Fatigue liée à l'examen du marché secondaire des médicaments innovants: succès clinique de la phase III et approbation du produit
最长连续序列[扩散法+空间换时间]
[leetcode] 11. Container with the most water
‘distutils‘ has no attribute ‘version
Build and train your own dataset for pig face recognition
Unity C # e-learning (VI) -- FTP (II)
基本布局-QHBoxLayout类、QVBoxLayout类、QGridLayout类
How to get the picture outside the chain - Netease photo album [easy to understand]
An Chaoyun: "one cloud with multiple cores" supports the implementation of the national information innovation government cloud
Chinese and English instructions of trypsin
June 24, 2022: golang multiple choice question, what does the following golang code output? A:1; B:3; C:4; D: Compilation failed. package main import ( “f
左手梦想 右手责任 广汽本田不光关注销量 还有儿童安全