📄 ff.c
字号:
{
DWORD clust;
char ds;
BYTE *dptr = NULL;
FATFS *fs = FatFs;
/* Initialize directory object */
clust = fs->dirbase;
if (fs->fs_type == FS_FAT32) {
scan->clust = scan->sclust = clust;
scan->sect = clust2sect(clust);
} else {
scan->clust = scan->sclust = 0;
scan->sect = clust;
}
scan->index = 0;
while ((*path == ' ') || (*path == '/')) path++; /* Skip leading spaces */
if ((BYTE)*path < ' ') { /* Null path means the root directory */
*dir = NULL; return FR_OK;
}
for (;;) {
ds = make_dirfile(&path, fn); /* Get a paragraph into fn[] */
if (ds == 1) return FR_INVALID_NAME;
for (;;) {
if (!move_window(scan->sect)) return FR_RW_ERROR;
dptr = &(fs->win[(scan->index & 15) * 32]); /* Pointer to the directory entry */
if (*dptr == 0) /* Has it reached to end of dir? */
return !ds ? FR_NO_FILE : FR_NO_PATH;
if ( (*dptr != 0xE5) /* Matched? */
&& !(*(dptr+11) & AM_VOL)
&& !memcmp(dptr, fn, 8+3) ) break;
if (!next_dir_entry(scan)) /* Next directory pointer */
return !ds ? FR_NO_FILE : FR_NO_PATH;
}
if (!ds) { *dir = dptr; return FR_OK; } /* Matched with end of path */
if (!(*(dptr+11) & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */
clust = ((DWORD)LD_WORD(dptr+20) << 16) | LD_WORD(dptr+26); /* Get cluster# of the directory */
scan->clust = scan->sclust = clust; /* Restart scan with the new directory */
scan->sect = clust2sect(clust);
scan->index = 0;
}
}
/*---------------------------*/
/* Reserve a Directory Entry */
#ifndef _FS_READONLY
static
BYTE* reserve_direntry (
DIR *scan /* Target directory to create new entry */
)
{
DWORD clust, sector;
BYTE c, n, *dptr;
FATFS *fs = FatFs;
/* Re-initialize directory object */
clust = scan->sclust;
if (clust) { /* Dyanmic directory table */
scan->clust = clust;
scan->sect = clust2sect(clust);
} else { /* Static directory table */
scan->sect = fs->dirbase;
}
scan->index = 0;
do {
if (!move_window(scan->sect)) return NULL;
dptr = &(fs->win[(scan->index & 15) * 32]); /* Pointer to the directory entry */
c = *dptr;
if ((c == 0) || (c == 0xE5)) return dptr; /* Found an empty entry! */
} while (next_dir_entry(scan)); /* Next directory pointer */
/* Reached to end of the directory table */
/* Abort when static table or could not stretch dynamic table */
if ((!clust) || !(clust = create_chain(scan->clust))) return NULL;
if (!move_window(0)) return 0;
fs->winsect = sector = clust2sect(clust); /* Cleanup the expanded table */
memset(fs->win, 0, 512);
for (n = fs->sects_clust; n; n--) {
if (disk_write(fs->win, sector, 1) != RES_OK) return NULL;
sector++;
}
fs->winflag = 1;
return fs->win;
}
#endif /* _FS_READONLY */
/*-----------------------------------------*/
/* Make Sure that the File System is Valid */
static
FRESULT check_mounted ()
{
FATFS *fs = FatFs;
if (!fs) return FR_NOT_ENABLED; /* Has the FatFs been enabled? */
if (disk_status() & STA_NOINIT) { /* The drive has not been initialized */
if (fs->files) /* Drive was uninitialized with any file left opend */
return FR_INCORRECT_DISK_CHANGE;
else
return f_mountdrv();; /* Initialize file system and return resulut */
} else { /* The drive has been initialized */
if (!fs->fs_type) /* But the file system has not been initialized */
return f_mountdrv(); /* Initialize file system and return resulut */
}
return FR_OK; /* File system is valid */
}
/*--------------------------------------------------------------------------*/
/* Public Funciotns */
/*--------------------------------------------------------------------------*/
/*----------------------------------------------------------*/
/* Load File System Information and Initialize FatFs Module */
FRESULT f_mountdrv ()
{
BYTE fat;
DWORD sect, fatend, maxsect;
FATFS *fs = FatFs;
if (!fs) return FR_NOT_ENABLED;
/* Initialize file system object */
memset(fs, 0, sizeof(FATFS));
/* Initialize disk drive */
if (disk_initialize() & STA_NOINIT) return FR_NOT_READY;
/* Search FAT partition */
fat = check_fs(sect = 0); /* Check sector 0 as an SFD format */
if (!fat) { /* Not a FAT boot record, it will be an FDISK format */
/* Check a partition listed in top of the partition table */
if (fs->win[0x1C2]) { /* Is the partition existing? */
sect = LD_DWORD(&(fs->win[0x1C6])); /* Partition offset in LBA */
fat = check_fs(sect); /* Check the partition */
}
}
if (!fat) return FR_NO_FILESYSTEM; /* No FAT patition */
/* Initialize file system object */
fs->fs_type = fat; /* FAT type */
fs->sects_fat = /* Sectors per FAT */
(fat == FS_FAT32) ? LD_DWORD(&(fs->win[0x24])) : LD_WORD(&(fs->win[0x16]));
fs->sects_clust = fs->win[0x0D]; /* Sectors per cluster */
fs->n_fats = fs->win[0x10]; /* Number of FAT copies */
fs->fatbase = sect + LD_WORD(&(fs->win[0x0E])); /* FAT start sector (physical) */
fs->n_rootdir = LD_WORD(&(fs->win[0x11])); /* Nmuber of root directory entries */
fatend = fs->sects_fat * fs->n_fats + fs->fatbase;
if (fat == FS_FAT32) {
fs->dirbase = LD_DWORD(&(fs->win[0x2C])); /* FAT32: Directory start cluster */
fs->database = fatend; /* FAT32: Data start sector (physical) */
} else {
fs->dirbase = fatend; /* Directory start sector (physical) */
fs->database = fs->n_rootdir / 16 + fatend; /* Data start sector (physical) */
}
maxsect = LD_DWORD(&(fs->win[0x20])); /* Calculate maximum cluster number */
if (!maxsect) maxsect = LD_WORD(&(fs->win[0x13]));
fs->max_clust = (maxsect - fs->database + sect) / fs->sects_clust + 2;
return FR_OK;
}
/*-----------------------------*/
/* Get Number of Free Clusters */
FRESULT f_getfree (
DWORD *nclust /* Pointer to the double word to return number of free clusters */
)
{
DWORD n, clust, sect;
BYTE fat, f, *p;
FRESULT res;
FATFS *fs = FatFs;
if ((res = check_mounted()) != FR_OK) return res;
/* Count number of free clusters */
fat = fs->fs_type;
n = 0;
if (fat == FS_FAT12) {
clust = 2;
do {
if ((WORD)get_cluster(clust) == 0) n++;
} while (++clust < fs->max_clust);
} else {
clust = fs->max_clust;
sect = fs->fatbase;
f = 0; p = 0;
do {
if (!f) {
if (!move_window(sect++)) return FR_RW_ERROR;
p = fs->win;
}
if (fat == FS_FAT16) {
if (LD_WORD(p) == 0) n++;
p += 2; f += 1;
} else {
if (LD_DWORD(p) == 0) n++;
p += 4; f += 2;
}
} while (--clust);
}
*nclust = n;
return FR_OK;
}
/*-----------------------*/
/* Open or Create a File */
FRESULT f_open (
FIL *fp, /* Pointer to the buffer of new file object to create */
const char *path, /* Pointer to the file path */
BYTE mode /* Access mode and file open mode flags */
)
{
FRESULT res;
BYTE *dir;
DIR dirscan;
char fn[8+3+1];
FATFS *fs = FatFs;
if ((res = check_mounted()) != FR_OK) return res;
#ifndef _FS_READONLY
if ((mode & (FA_WRITE|FA_CREATE_ALWAYS)) && (disk_status() & STA_PROTECT))
return FR_WRITE_PROTECTED;
#endif
res = trace_path(&dirscan, fn, path, &dir); /* Trace the file path */
#ifndef _FS_READONLY
/* Create or Open a File */
if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS)) {
DWORD dw;
if (res != FR_OK) { /* No file, create new */
mode |= FA_CREATE_ALWAYS;
if (res != FR_NO_FILE) return res;
dir = reserve_direntry(&dirscan); /* Reserve a directory entry */
if (dir == NULL) return FR_DENIED;
memcpy(dir, fn, 8+3); /* Initialize the new entry */
*(dir+12) = fn[11];
memset(dir+13, 0, 32-13);
} else { /* File already exists */
if ((dir == NULL) || (*(dir+11) & (AM_RDO|AM_DIR))) /* Could not overwrite (R/O or DIR) */
return FR_DENIED;
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero */
dw = fs->winsect; /* Remove the cluster chain */
if (!remove_chain(((DWORD)LD_WORD(dir+20) << 16) | LD_WORD(dir+26))
|| !move_window(dw) )
return FR_RW_ERROR;
ST_WORD(dir+20, 0); ST_WORD(dir+26, 0); /* cluster = 0 */
ST_DWORD(dir+28, 0); /* size = 0 */
}
}
if (mode & FA_CREATE_ALWAYS) {
*(dir+11) = AM_ARC;
dw = get_fattime();
ST_DWORD(dir+14, dw); /* Created time */
ST_DWORD(dir+22, dw); /* Updated time */
fs->winflag = 1;
}
}
/* Open a File */
else {
#endif /* _FS_READONLY */
if (res != FR_OK) return res; /* Trace failed */
if ((dir == NULL) || (*(dir+11) & AM_DIR)) /* It is a directory */
return FR_NO_FILE;
#ifndef _FS_READONLY
if ((mode & FA_WRITE) && (*(dir+11) & AM_RDO)) /* R/O violation */
return FR_DENIED;
}
#endif
#ifdef _FS_READONLY
fp->flag = mode & FA_READ;
#else
fp->flag = mode & (FA_WRITE|FA_READ);
fp->dir_sect = fs->winsect; /* Pointer to the directory entry */
fp->dir_ptr = dir;
#endif
fp->org_clust = ((DWORD)LD_WORD(dir+20) << 16) | LD_WORD(dir+26); /* File start cluster */
fp->fsize = LD_DWORD(dir+28); /* File size */
fp->fptr = 0; /* File ptr */
fp->sect_clust = 1; /* Sector counter */
fs->files++;
return FR_OK;
}
/*-----------*/
/* Read File */
FRESULT f_read (
FIL *fp, /* Pointer to the file object */
void *buff, /* Pointer to data buffer */
WORD btr, /* Number of bytes to read */
WORD *br /* Pointer to number of bytes read */
)
{
DWORD clust, sect, ln;
WORD rcnt;
BYTE cc, *rbuff = buff;
FATFS *fs = FatFs;
*br = 0;
if (!fs) return FR_NOT_ENABLED;
if ((disk_status() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY; /* Check disk ready */
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
ln = fp->fsize - fp->fptr;
if (btr > ln) btr = ln; /* Truncate read count by number of bytes left */
for ( ; btr; /* Repeat until all data transferred */
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
if ((fp->fptr % 512) == 0) { /* On the sector boundary */
if (--(fp->sect_clust)) { /* Decrement sector counter */
sect = fp->curr_sect + 1; /* Next sector */
} else { /* Next cluster */
clust = (fp->fptr == 0) ? fp->org_clust : get_cluster(fp->curr_clust);
if ((clust < 2) || (clust >= fs->max_clust)) goto fr_error;
fp->curr_clust = clust; /* Current cluster */
sect = clust2sect(clust); /* Current sector */
fp->sect_clust = fs->sects_clust; /* Re-initialize the sector counter */
}
#ifndef _FS_READONLY
if (fp->flag & FA__DIRTY) { /* Flush file I/O buffer if needed */
if (disk_write(fp->buffer, fp->curr_sect, 1) != RES_OK) goto fr_error;
fp->flag &= ~FA__DIRTY;
}
#endif
fp->curr_sect = sect; /* Update current sector */
cc = btr / 512; /* When left bytes >= 512 */
if (cc) { /* Read maximum contiguous sectors */
if (cc > fp->sect_clust) cc = fp->sect_clust;
if (disk_read(rbuff, sect, cc) != RES_OK) goto fr_error;
fp->sect_clust -= cc - 1;
fp->curr_sect += cc - 1;
rcnt = cc * 512; continue;
}
if (disk_read(fp->buffer, sect, 1) != RES_OK) /* Load the sector into file I/O buffer */
goto fr_error;
}
rcnt = 512 - (fp->fptr % 512); /* Copy fractional bytes from file I/O buffer */
if (rcnt > btr) rcnt = btr;
memcpy(rbuff, &fp->buffer[fp->fptr % 512], rcnt);
}
return FR_OK;
fr_error: /* Abort this file due to an unrecoverable error */
fp->flag |= FA__ERROR;
return FR_RW_ERROR;
}
/*------------*/
/* Write File */
#ifndef _FS_READONLY
FRESULT f_write (
FIL *fp, /* Pointer to the file object */
const void *buff, /* Pointer to the data to be written */
WORD btw, /* Number of bytes to write */
WORD *bw /* Pointer to number of bytes written */
)
{
DWORD clust, sect;
WORD wcnt;
BYTE cc;
const BYTE *wbuff = buff;
FATFS *fs = FatFs;
*bw = 0;
if (!fs) return FR_NOT_ENABLED;
if ((disk_status() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY;
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
if (fp->fsize + btw < fp->fsize) btw = 0; /* File size cannot reach 4GB */
for ( ; btw; /* Repeat until all data transferred */
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
if ((fp->fptr % 512) == 0) { /* On the sector boundary */
if (--(fp->sect_clust)) { /* Decrement sector counter */
sect = fp->curr_sect + 1; /* Next sector */
} else { /* Next cluster */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -