SD卡与fatfs文件系统(4)
来源:互联网 发布:艾瑞咨询数据 编辑:程序博客网 时间:2024/05/16 07:53
FatFs的初始化和加载的操作是在函数auto_mount中进行的。
- static
- FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */
- const char **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 access */
- )
- {
- BYTE drv, fmt, *tbl;
- DSTATUS stat;
- DWORD bootsect, fatsize, totalsect, maxclust;
- const char *p = *path;
- FATFS *fs;
- //分析路径字符串
- /* Get drive number from the path name */
- while (*p == ‘ ’) p++; /* Strip leading spaces */
- drv = p[0] - ’0’; /* Is there a drive number? */
- if (drv <= 9 && p[1] == ‘:’)
- p += 2; /* Found a drive number, get and strip it */
- else
- drv = 0; /* No drive number is given, use drive number 0 as default */
- if (*p == ‘/’) p++; /* Strip heading slash */
- *path = p; /* Return pointer to the path name */
- /* Check if the drive number is valid or not */
- if (drv >= _DRIVES) return FR_INVALID_DRIVE; /* Is the drive number valid? */
- *rfs = fs = FatFs[drv]; /* Returen pointer to the corresponding file system object */
- if (!fs) return FR_NOT_ENABLED; /* Is the file system object registered? */
- if (fs->fs_type) { /* If the logical drive has been mounted */
- stat = disk_status(fs->drive);
- if (!(stat & STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */
- #if !_FS_READONLY
- if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
- return FR_WRITE_PROTECTED;
- #endif
- return FR_OK; /* The file system object is valid */
- }
- }
- /* The logical drive must be re-mounted. Following code attempts to mount the logical drive */
- memset(fs, 0, sizeof(FATFS)); /* Clean-up the file system object */
- fs->drive = LD2PD(drv); /* Bind the logical drive and a physical drive */
- stat = disk_initialize(fs->drive); /* Initialize low level disk I/O layer */
- if (stat & STA_NOINIT) /* Check if the drive is ready */
- return FR_NOT_READY;
- #if S_MAX_SIZ > 512 /* Get disk sector size if needed */
- if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > S_MAX_SIZ)
- return FR_NO_FILESYSTEM;
- #endif
- #if !_FS_READONLY
- if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
- return FR_WRITE_PROTECTED;
- #endif
- /* Search FAT partition on the drive */
- fmt = check_fs(fs, bootsect = 0); /* Check sector 0 as an SFD format */ 读取MBR和分区表
- if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */
- /* Check a partition listed in top of the partition table */
- tbl = &fs->win[MBR_Table + LD2PT(drv) * 16]; /* Partition table */
- if (tbl[4]) { /* Is the partition existing? */
- bootsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
- fmt = check_fs(fs, bootsect); /* Check the partition */ 读取分区DBR信息
- }
- }
- if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != SS(fs)) /* No valid FAT patition is found */
- return FR_NO_FILESYSTEM;
- /* Initialize the file system object */ 从读取的数据中初始化fs结构体
- fatsize = LD_WORD(&fs->win[BPB_FATSz16]); /* Number of sectors per FAT */
- if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]);
- fs->sects_fat = fatsize;
- fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
- fatsize *= fs->n_fats; /* (Number of sectors in FAT area) */
- fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]); /* FAT start sector (lba) */
- fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
- fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]); /* Nmuber of root directory entries */
- totalsect = LD_WORD(&fs->win[BPB_TotSec16]); /* Number of sectors on the file system */
- if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]);
- fs->max_clust = maxclust = (totalsect /* max_clust = Last cluster# + 1 */
- - LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / (SS(fs)/32)
- ) / fs->csize + 2;
- fmt = FS_FAT12; /* Determine the FAT sub type */
- if (maxclust >= 0xFF7) fmt = FS_FAT16;
- if (maxclust >= 0xFFF7) fmt = FS_FAT32;
- if (fmt == FS_FAT32)
- fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]); /* Root directory start cluster */
- else
- fs->dirbase = fs->fatbase + fatsize; /* Root directory start sector (lba) */
- fs->database = fs->fatbase + fatsize + fs->n_rootdir / (SS(fs)/32); /* Data start sector (lba) */
- #if !_FS_READONLY
- /* Initialize allocation information */
- fs->free_clust = 0xFFFFFFFF;
- #if _USE_FSINFO
- /* Get fsinfo if needed */
- if (fmt == FS_FAT32) { //读取额外的FAT32信息
- fs->fsi_sector = bootsect + LD_WORD(&fs->win[BPB_FSInfo]);
- if (disk_read(fs->drive, fs->win, fs->fsi_sector, 1) == RES_OK &&
- LD_WORD(&fs->win[BS_55AA]) == 0xAA55 &&
- LD_DWORD(&fs->win[FSI_LeadSig]) == 0x41615252 &&
- LD_DWORD(&fs->win[FSI_StrucSig]) == 0x61417272) {
- fs->last_clust = LD_DWORD(&fs->win[FSI_Nxt_Free]);
- fs->free_clust = LD_DWORD(&fs->win[FSI_Free_Count]);
- }
- }
- #endif
- #endif
- fs->fs_type = fmt; /* FAT syb-type */
- fs->id = ++fsid; /* File system mount ID */
- return FR_OK;
- }
staticFRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */ const char **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 access */){ BYTE drv, fmt, *tbl; DSTATUS stat; DWORD bootsect, fatsize, totalsect, maxclust; const char *p = *path; FATFS *fs; //分析路径字符串 /* Get drive number from the path name */ while (*p == ' ') p++; /* Strip leading spaces */ drv = p[0] - '0'; /* Is there a drive number? */ if (drv <= 9 && p[1] == ':') p += 2; /* Found a drive number, get and strip it */ else drv = 0; /* No drive number is given, use drive number 0 as default */ if (*p == '/') p++; /* Strip heading slash */ *path = p; /* Return pointer to the path name */ /* Check if the drive number is valid or not */ if (drv >= _DRIVES) return FR_INVALID_DRIVE; /* Is the drive number valid? */ *rfs = fs = FatFs[drv]; /* Returen pointer to the corresponding file system object */ if (!fs) return FR_NOT_ENABLED; /* Is the file system object registered? */ if (fs->fs_type) { /* If the logical drive has been mounted */ stat = disk_status(fs->drive); if (!(stat & STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */
(1)disk_status(fs->drive);获取设备的状态,判断设备是否存在;
(2)disk_initialize(fs->drive);它对接口做初始化;
(3)check_fs(fs, bootsect = 0);读取MBR和分区表,判断分区是否是FAT或FAT32;
阅读全文
0 0
- SD卡与fatfs文件系统(4)
- SD卡与fatfs文件系统(4)
- SD卡与fatfs文件系统(1)
- SD卡与fatfs文件系统(2)
- SD卡与fatfs文件系统(3)
- SD卡与fatfs文件系统(1)
- SD卡与fatfs文件系统(2)
- SD卡与fatfs文件系统(3)
- SD卡与fatfs文件系统(5)-在fatfs中使用多个SD卡分区
- SD卡的读写和FatFS文件系统
- SD卡的读写和FatFS文件系统
- SD 移植fatfs文件系统
- 连接fatfs与SD卡
- SD卡SPI模式下FatFs与znFat文件系统速度比较
- MSP430 SD卡SPI读写操作(4) —— FatFs文件系统实现(以MSP430F5438A为例)
- lpc1768 SD卡基于ucos的fatfs文件系统
- LPC1768 SPI模式下SD卡FatFs文件系统移植
- 基于STM32的SD卡FATFS文件系统学习笔记
- JavaScript原型
- 给一副扑克牌,求最长的顺子
- HDU-3397:Sequence operation(区间更新之合集?)
- Spring 动态代理(一)
- oracle递归树形结构sql
- SD卡与fatfs文件系统(4)
- Ubuntu16.04安装mediawiki
- poj 3398 Perfect Service
- Passward
- java:封装的概念和代码
- HDU 4565矩阵快速幂—— So Easy!
- (矩阵快速幂)hdu1575 Tr A
- TensorFlow初入
- mysql的延迟加载