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;
}
(1)disk_status(fs->drive);擷取裝置的狀态,判斷裝置是否存在;
(2)disk_initialize(fs->drive);它對接口做初始化;
(3)check_fs(fs, bootsect = 0);讀取MBR和分區表,判斷分區是否是FAT或FAT32;