FATFS文件系统剖析2_fatfs文件系统的移植

2020-02-27 其他范文 下载本文

FATFS文件系统剖析2由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“fatfs文件系统的移植”。

FATFS文件系统剖析2: 分析下ff.h和ff.c两个文件。先来分析ff.h中定义的几个结构体: typedef struct { BYTE fs_type;// 系统类型,为0时系统没有被挂载 BYTE drv;// 物理磁盘驱动号

BYTE csize;// 每簇的扇区数目,簇是文件数据分配的基本单位 BYTE n_fats;// 文件分配表的数目,一般为两个(一个备份fat表)//Fatfs文件系统应该是:引导扇区,文件分配表2个,根目录区,和数据区

BYTE wflag;// 文件活动窗体是否改动标志,为1要回写 BYTE fsi_flag;// 文件信息回写标志 WORD id;// 文件系统加载id号

WORD n_rootdir;// 根目录区目录项数目(针对FAT12/16,FAT32不使用)#if _MAX_SS!= 512 WORD ize;// 每扇区多少字节 #endif #if _FS_REENTRANT _SYNC_t sobj;// 允许重入,则定义同步对象 #endif #if!_FS_READONLY DWORD last_clust;// 最新分配的簇 DWORD free_clust;// 空闲簇

DWORD fsi_sector;// 文件信息扇区(仅用于FAT32)#endif #if _FS_RPATH DWORD cdir;//使用相对路径,文件系统的当前起始路径 0(root 路径)#endif DWORD n_fatent;//文件分配表占用的扇区 n_fatent=数据簇数目+2 DWORD fsize;//每FAT表有多少个扇区 DWORD fatbase;//文件分配表开始扇区

DWORD dirbase;// 如果是FAT32,根目录开始扇区需要首先得到 DWORD database;// 数据起始扇区 DWORD winsect;// win中当前指定的扇区 BYTE win[_MAX_SS];//扇区操作缓存 } FATFS;typedef struct { FATFS* fs;// 指向的文件系统

WORD id;// 自身文件系统挂载id号 即 fs->id BYTE flag;// 文件状态

BYTE pad1;//文件在簇里面扇区偏移(0--fs->csize)DWORD fptr;//文件当前读写指针位置,当文件打开时为0 DWORD fsize;//文件大小(按字节计算)

DWORD org_clust;// 文件起始簇(0 when fsize==0)DWORD curr_clust;// 文件当前操作簇 DWORD dsect;// 文件当前操作扇区

#if!_FS_READONLY DWORD dir_sect;// 包含路径入口的扇区号 BYTE* dir_ptr;// 目录入口指针 #endif #if _USE_FASTSEEK DWORD* cltbl;//指向查找映射表的簇(null on file open)#endif #if _FS_SHARE UINT lockid;// 文件锁 ID号(index of file semaphore table)#endif #if!_FS_TINY BYTE buf[_MAX_SS];// 文件读写缓存

#endif } FIL;

typedef struct { FATFS* fs;// 对应的文件系统

WORD id;// 自身文件系统挂载id号 即 fs->id WORD index;// 目前读写索引号 /* Current read/write index number */ DWORD sclust;// 目录表起始簇(0:Root dir)DWORD clust;// 目前处理的簇 DWORD sect;// 目前簇里对应的扇区

BYTE* dir;//指向当前在win[]中的短文件名入口项/* Pointer to the current SFN entry in the win[] BYTE* fn;//指向短文件名(in/out){file[8],ext[3],status[1]} #if _USE_LFN WCHAR* lfn;//指向长文件名缓冲 /* Pointer to the LFN working buffer */ WORD lfn_idx;/* Last matched LFN index number(0xFFFF:No LFN)*/ #endif } DIR;

typedef struct { //文件目录表项 大小=4+2+2+1+13 DWORD fsize;/* File size */ WORD fdate;/* Last modified date */ WORD ftime;/* Last modified time */ BYTE fattrib;/* Attribute */ // 文件属性

TCHAR fname[13];/* Short file name(8.3 format)*/ #if _USE_LFN //长文件名支持

TCHAR* lfname;/* Pointer to the LFN buffer */ UINT lfsize;/* Size of LFN buffer in TCHAR */ #endif } FILINFO;

结构是搞清楚了,但其里面的具体联系怎么也还收理不清楚。只有看ff.c来疏通了!里面东西还是蛮多的,咋一看,3000多行类(太多,在这里就根据我的配置,进行逐个分析吧),从头到尾,一个一个来。

首先是三个内存操作和以个字符查找处理函数,不说不解释。

然后是: static FRESULT move_window(FATFS *fs, /* File system object */ DWORD sector /* Sector number to make appearance in the fs->win[] */)/* Move to zero only writes back dirty window */ 该函数就是把指定扇区sector中的数据读到fs->win[]里面

DWORD clust2sect(/*!=0: Sector number, 0: Failed-invalid cluster# */ FATFS *fs, /* File system object */ DWORD clst /* Cluster# to be converted */)计算簇clst在对应文件系统fs里面的扇区号 DWORD get_fat(/* 0xFFFFFFFF:Disk error, 1:Internal error, Else:Cluster status */ FATFS *fs, /* File system object */ DWORD clst /* Cluster# to get the link information */)获取簇clst在文件系统fs中FAT表里面fat入口

static FRESULT dir_sdi(DIR *dj, /* Pointer to directory object */ WORD idx /* Directory index number */)根据根目录索引号idx获取相应的目录信息存储到dj结构里面

static FRESULT dir_next(/* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not stretch */ DIR *dj, /* Pointer to directory object */ int stretch /* 0: Do not stretch table, 1: Stretch table if needed */)获取当前目录项的索引值+1,对应的目录项信息

static FRESULT dir_find(DIR *dj /* Pointer to the directory object linked to the file name */)在目录表中查找与dj->fn相同文件名的目录项 static FRESULT create_name(DIR *dj, /* Pointer to the directory object */ const TCHAR **path /* Pointer to pointer to the segment in the path string */)

创建一个文件名为 path指向的 dj目录项

static FRESULT follow_path(/* FR_OK(0): succeful,!=0: error code */ DIR *dj, /* Directory object to return last directory and found object */ const TCHAR *path /* Full-path string to find a file or directory */)

获取文件路径path对应的目录项填入dj里面

static BYTE check_fs(/* 0:The FAT BR, 1:Valid BR but not an FAT, 2:Not a BR, 3:Disk error */ FATFS *fs, /* File system object */ DWORD sect /* Sector#(lba)to check if it is an FAT boot record or not */)

读取文件系统fs的一号扇区,进行MBR 检查,文件系统类型区分

static FRESULT chk_mounted(/* FR_OK(0): succeful,!=0: any error occurred */ const TCHAR **path, /* Pointer to pointer to the path name(drive number)*/ FATFS **rfs, /* Pointer to pointer to the found file system object */ BYTE chk_wp /*!=0: Check media write protection for write acce */)

测试文件系统是否已挂在,如没有,就进行挂载,文件系统结构初始化 static FRESULT validate(/* FR_OK(0): The object is valid,!=0: Invalid */ FATFS *fs, /* Pointer to the file system object */ WORD id /* Member id of the target object to be checked */)

检测文件系统是否可用

如下是现配置好的文件系统引出的四个接口函数:

FRESULT f_mount(//挂在一个逻辑的文件系统

BYTE vol, /* Logical drive number to be mounted/unmounted */ FATFS *fs /* Pointer to new file system object(NULL for unmount)*/)系统挂载

FRESULT f_open(FIL *fp, /* Pointer to the blank file object */ const TCHAR *path, /* Pointer to the file name */ BYTE mode /* Acce mode and file open mode flags */)

文件打开,包括(真正的文件系统初始化,系统检测)

FRESULT f_read(FIL *fp, /* Pointer to the file object */ void *buff, /* Pointer to data buffer */ UINT btr, /* Number of bytes to read */ UINT *br /* Pointer to number of bytes read */)文件读

FRESULT f_close(FIL *fp /* Pointer to the file object to be closed */)文件关闭

转自博客,供大家相互交流!

《FATFS文件系统剖析2.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
FATFS文件系统剖析2
点击下载文档
相关专题 fatfs文件系统的移植 文件系统 FATFS fatfs文件系统的移植 文件系统 FATFS
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文