• FATFS介绍及相关参数计算


    1.相关概念
    Volume的概念:卷,例如C盘。除开前63个扇区,
    Clusters:文件系统的基本操作单元
    Sector:扇区,flash的操作单元
    MBR 主引导记录(MBR,Master Boot Record)由 446 个字节的引导代码、64 字节的主分区(4 个)表及两个字节的签名值“55 AA”组成

    VBR:卷引导记录VBR除了包含引导启动代码,还包含文件系统的元数据。

    2.MBR,VBR,DIR结构及扇区划分

    格式化的过程,写入分区表
    0扇区写主引导区MBR (boot区)
    MBR表的起始从446开始,
    四个分区,每个分区为16字节信息共64字节,加两字节的数字签名刚好为一个扇区大小512,其他填0
    #define MBR_Table           446     /* MBR: Offset of partition table in the MBR */
    #define SZ_PTE              16      /* MBR: Size of a partition table entry */

    //********************************
    #define PTE_Boot            0       /* MBR PTE: Boot indicator */
    #define PTE_StHead          1       /* MBR PTE: Start head */
    #define PTE_StSec           2       /* MBR PTE: Start sector */
    #define PTE_StCyl           3       /* MBR PTE: Start cylinder */
    #define PTE_System          4       /* MBR PTE: System ID */
    #define PTE_EdHead          5       /* MBR PTE: End head */
    #define PTE_EdSec           6       /* MBR PTE: End sector */
    #define PTE_EdCyl           7       /* MBR PTE: End cylinder */
    #define PTE_StLba           8       /* MBR PTE: Start in LBA */
    #define PTE_SizLba          12      /* MBR PTE: Size in LBA */ word

    //**********************************
    上面为每个分区的16字节含义

    下面为实现的部分源码
            if (!(opt & FM_SFD)) {    /* Create partition table if in FDISK format */
                mem_set(buf, 0, ss);
                st_word(buf + BS_55AA, 0xAA55);        /* MBR signature */
                pte = buf + MBR_Table;                /* Create partition table for single partition in the drive */446
                pte[PTE_Boot] = 0;                    /* Boot indicator */
                pte[PTE_StHead] = 1;                /* Start head */
                pte[PTE_StSec] = 1;                    /* Start sector */
                pte[PTE_StCyl] = 0;                    /* Start cylinder */
                pte[PTE_System] = sys;                /* System type */0
                n = (b_vol + sz_vol) / (63 * 255);    /* (End CHS may be invalid) */
                pte[PTE_EdHead] = 254;                /* End head */
                pte[PTE_EdSec] = (BYTE)(((n >> 2) & 0xC0) | 63);    /* End sector */63
                pte[PTE_EdCyl] = (BYTE)n;            /* End cylinder */0
                st_dword(pte + PTE_StLba, b_vol);    /* Start offset in LBA */63
                st_dword(pte + PTE_SizLba, sz_vol);    /* Size in sectors */数据的扇区3937

    Volbase 1个
    63扇区填引导程序VBR,包含名字,fat类型,卷的大小等

    FAT VBR 分区表结构
       /* FatFs refers the FAT structure as simple byte array instead of structure member
    / because the C structure is not binary compatible between different platforms */

    #define BS_JmpBoot          0       /* x86 jump instruction (3-byte) */
    0x90feeb
    #define BS_OEMName          3       /* OEM name (8-byte) */
    MSD0S5.0
    #define BPB_BytsPerSec      11      /* Sector size [byte] (WORD) */
    0X200  512Byte
    #define BPB_SecPerClus      13      /* Cluster size [sector] (BYTE) */
    0x1 1个扇区
    #define BPB_RsvdSecCnt      14      /* Size of reserved area [sector] (WORD) */ 1个
    #define BPB_NumFATs         16      /* Number of FATs (BYTE) */1
    #define BPB_RootEntCnt      17      /* Size of root directory area for FAT [entry] (WORD) */0x200  /* Number of root directory entries */
    #define BPB_TotSec16        19      /* Volume size (16-bit) [sector] (WORD) */0x5fc1
    #define BPB_Media           21      /* Media descriptor byte (BYTE) */
    #define BPB_FATSz16         22      /* FAT size (16-bit) [sector] (WORD) */96个
    #define BPB_SecPerTrk       24      /* Number of sectors per track for int13h [sector] (WORD) */63个
    #define BPB_NumHeads        26      /* Number of heads for int13h (WORD) */255
    #define BPB_HiddSec         28      /* Volume offset from top of the drive (DWORD) */63个
    #define BPB_TotSec32        32      /* Volume size (32-bit) [sector] (DWORD) */0
    #define BS_DrvNum           36      /* Physical drive number for int13h (BYTE) */128
    #define BS_NTres            37      /* WindowsNT error flag (BYTE) */0
    #define BS_BootSig          38      /* Extended boot signature (BYTE) */41
    #define BS_VolID            39      /* Volume serial number (DWORD) */
    #define BS_VolLab           43      /* Volume label string (8-byte) */
    #define BS_FilSysType       54      /* Filesystem type string (8-byte) */
    #define BS_BootCode         62      /* Boot code (448-byte) */
    #define BS_55AA             510     /* Signature word (WORD) */




    Fatbase 共32扇区,文件系统本身大小  fat12
    64-95 共0x20h 
    st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8);    
    64扇区头填 0x08 0xff 0xff (标记)其他填0
    65-95填0  
    Dirbase:根目录扇区存放文件名,初始全填0
    96-127 
    Database:真正用于存放数据
    128扇区开始
    一个保留扇区,一个fat文件扇区32,512个目录,每个目录32个字节,占32个扇区,

    共65个扇区/* RSV + FAT + DIR */
    /* Number of clusters */Volume-65 = 3937-65=3872可用于操作的单元

    在挂载中
    功能:加载文件系统对象到工作区FATFS
    核心函数:check_fs(fs,bsect)
    用于加载扇区0并检测有无启动程序
    核对跳转代码,数字签名,文件系统类型
    1.    加载boot record signature 55aa
    2.    得到分区的偏移63 
    3.    MBR_TABLE 446
    4.    454存储引导程序的扇区


    根目录的结构

    #define DIR_Name            0       /* Short file name (11-byte) */
    #define DIR_Attr            11      /* Attribute (BYTE) */
    #define DIR_NTres           12      /* Lower case flag (BYTE) */
    #define DIR_CrtTime10       13      /* Created time sub-second (BYTE) */
    #define DIR_CrtTime         14      /* Created time (DWORD) */
    #define DIR_LstAccDate      18      /* Last accessed date (WORD) */
    #define DIR_FstClusHI       20      /* Higher 16-bit of first cluster (WORD) */
    #define DIR_ModTime         22      /* Modified time (DWORD) */
    #define DIR_FstClusLO       26      /* Lower 16-bit of first cluster (WORD) */
    #define DIR_FileSize        28      /* File size (DWORD) */

    Fat文件分配原则,下一可用分配
    NTFS:最优分配

  • 相关阅读:
    分享PPT设计思路,让你的PPT更吸睛
    究竟RPA能做什么?从企业,团队,个人的角度来分析RPA带来的好处
    【Docker 基础教程】Centos7.5安装Docker并配置阿里云镜像
    解锁工业 4.0 元宇宙:AR/VR、AI 和 3D 技术如何为下一次工业革命提供动力
    香,一套逻辑轻松且智能解决PyQt中控件数值验证的问题
    大数据请把这个分离软件推给所有后期~
    NewStarCTF2023week4-RSA Variation II
    代码随想录算法训练营19期第49天
    nasal脚本起源与环境搭建(flightgear开源项目)
    通过 Prometheus 编写 TiDB 巡检脚本(脚本已开源,内附链接)
  • 原文地址:https://blog.csdn.net/blacksharpsss/article/details/126437114