📄 ff.c
字号:
/*--------------------------------------------------------------------------/
/ FatFs - FAT file system module R0.04 (C)ChaN, 2007
/---------------------------------------------------------------------------/
/ FatFs module is an experimenal project to implement FAT file system to
/ cheap microcontrollers. This is a free software and is opened for education,
/ research and development under license policy of following trems.
/
/ Copyright (C) 2007, ChaN, all right reserved.
/
/ * The FatFs module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/ profit use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/---------------------------------------------------------------------------/
/ Feb 26, 2006 R0.00 Prototype.
/ Apr 29, 2006 R0.01 First stable version.
/ Jun 01, 2006 R0.02 Added FAT12. Removed unbuffered mode.
/ Fixed a problem on small (<32M) patition.
/ Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM).
/ Sep 22, 2006 R0.03 Added f_rename().
/ Changed option _FS_MINIMUM to _FS_MINIMIZE.
/ Dec 11, 2006 R0.03a Improved cluster scan algolithm to write files fast.
/ Fixed f_mkdir() creates incorrect directory on FAT32.
/ Feb 04, 2007 R0.04 Supported multiple drive system.
/ Changed some interfaces for multiple drive system.
/ Added f_mkfs().
/---------------------------------------------------------------------------*/
#include <string.h>
#include "ff.h" /* FatFs declarations */
#include "diskio.h" /* Include file for user provided disk functions */
#define ld2pd(drv) (drv) /* Logical drive# to Physical drive# conversion */
/*--------------------------------------------------------------------------
Module Private Functions
---------------------------------------------------------------------------*/
static
FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */
static
WORD fsid; /* File system mount ID */
/*-----------------------------------------------------------------------*/
/* Change Window Offset */
/*-----------------------------------------------------------------------*/
static
BOOL move_window ( /* TRUE: successful, FALSE: failed */
FATFS *fs, /* File system object */
DWORD sector /* Sector number to make apperance in the fs->win[] */
) /* Move to zero only writes back dirty window */
{
DWORD wsect;
wsect = fs->winsect;
if (wsect != sector) { /* Changed current window */
#if _FS_READONLY == 0
BYTE n;
if (fs->winflag) { /* Write back dirty window if needed */
if (disk_write(fs->drive, fs->win, wsect, 1) != RES_OK)
return FALSE;
fs->winflag = 0;
if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */
for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to FAT copy */
wsect += fs->sects_fat;
if (disk_write(fs->drive, fs->win, wsect, 1) != RES_OK)
break;
}
}
}
#endif
if (sector) {
if (disk_read(fs->drive, fs->win, sector, 1) != RES_OK)
return FALSE;
fs->winsect = sector;
}
}
return TRUE;
}
/*-----------------------------------------------------------------------*/
/* Get a Cluster Status */
/*-----------------------------------------------------------------------*/
static
DWORD get_cluster ( /* 0,2..: successful, 1: failed */
FATFS *fs, /* File system object */
DWORD clust /* Cluster# to get the link information */
)
{
WORD wc, bc;
DWORD fatsect;
if (clust >= 2 && clust < fs->max_clust) { /* Valid cluster# */
fatsect = fs->fatbase;
switch (fs->fs_type) {
case FS_FAT12 :
bc = (WORD)clust * 3 / 2;
if (!move_window(fs, fatsect + bc / 512)) break;
wc = fs->win[bc % 512]; bc++;
if (!move_window(fs, fatsect + bc / 512)) break;
wc |= (WORD)fs->win[bc % 512] << 8;
return (clust & 1) ? (wc >> 4) : (wc & 0xFFF);
case FS_FAT16 :
if (!move_window(fs, fatsect + clust / 256)) break;
return LD_WORD(&fs->win[((WORD)clust * 2) % 512]);
case FS_FAT32 :
if (!move_window(fs, fatsect + clust / 128)) break;
return LD_DWORD(&fs->win[((WORD)clust * 4) % 512]) & 0x0FFFFFFF;
}
}
return 1; /* There is no cluster information, or an error occured */
}
/*-----------------------------------------------------------------------*/
/* Change a Cluster Status */
/*-----------------------------------------------------------------------*/
#if _FS_READONLY == 0
static
BOOL put_cluster ( /* TRUE: successful, FALSE: failed */
FATFS *fs, /* File system object */
DWORD clust, /* Cluster# to change */
DWORD val /* New value to mark the cluster */
)
{
WORD bc;
BYTE *p;
DWORD fatsect;
fatsect = fs->fatbase;
switch (fs->fs_type) {
case FS_FAT12 :
bc = (WORD)clust * 3 / 2;
if (!move_window(fs, fatsect + bc / 512)) return FALSE;
p = &fs->win[bc % 512];
*p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
bc++;
fs->winflag = 1;
if (!move_window(fs, fatsect + bc / 512)) return FALSE;
p = &fs->win[bc % 512];
*p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
break;
case FS_FAT16 :
if (!move_window(fs, fatsect + clust / 256)) return FALSE;
ST_WORD(&fs->win[((WORD)clust * 2) % 512], (WORD)val);
break;
case FS_FAT32 :
if (!move_window(fs, fatsect + clust / 128)) return FALSE;
ST_DWORD(&fs->win[((WORD)clust * 4) % 512], val);
break;
default :
return FALSE;
}
fs->winflag = 1;
return TRUE;
}
#endif /* _FS_READONLY */
/*-----------------------------------------------------------------------*/
/* Remove a Cluster Chain */
/*-----------------------------------------------------------------------*/
#if _FS_READONLY == 0
static
BOOL remove_chain ( /* TRUE: successful, FALSE: failed */
FATFS *fs, /* File system object */
DWORD clust /* Cluster# to remove chain from */
)
{
DWORD nxt;
if (clust) {
while ((nxt = get_cluster(fs, clust)) >= 2) {
if (!put_cluster(fs, clust, 0)) return FALSE;
clust = nxt;
}
}
return TRUE;
}
#endif
/*-----------------------------------------------------------------------*/
/* Stretch or Create a Cluster Chain */
/*-----------------------------------------------------------------------*/
#if _FS_READONLY == 0
static
DWORD create_chain ( /* !=0: new cluster number, 0: failed */
FATFS *fs, /* File system object */
DWORD clust /* Cluster# to stretch, 0 means create new */
)
{
DWORD cstat, ncl, scl, mcl;
mcl = fs->max_clust;
if (clust == 0) { /* Create new chain */
scl = fs->last_clust; /* Get last allocated cluster */
if (scl < 2 || scl >= mcl) scl = 1;
}
else { /* Stretch existing chain */
cstat = get_cluster(fs, clust); /* Check the cluster status */
if (cstat < 2) return 0; /* It is an invalid cluster */
if (cstat < mcl) return cstat; /* It is already followed by next cluster */
scl = clust;
}
ncl = scl; /* Start cluster */
do {
ncl++; /* Next cluster */
if (ncl >= mcl) { /* Wrap around */
ncl = 2;
if (scl == 1) return 0; /* No free custer was found */
}
if (ncl == scl) return 0; /* No free custer was found */
cstat = get_cluster(fs, ncl); /* Get the cluster status */
if (cstat == 1) return 0; /* Any error occured */
} while (cstat); /* Repeat until find a free cluster */
if (!put_cluster(fs, ncl, 0x0FFFFFFF)) return 0; /* Mark the new cluster "in use" */
if (clust && !put_cluster(fs, clust, ncl)) return 0; /* Link it to previous one if needed */
fs->last_clust = ncl;
return ncl; /* Return new cluster number */
}
#endif /* _FS_READONLY */
/*-----------------------------------------------------------------------*/
/* Get Sector# from Cluster# */
/*-----------------------------------------------------------------------*/
static
DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */
FATFS *fs, /* File system object */
DWORD clust /* Cluster# to be converted */
)
{
clust -= 2;
if (clust >= fs->max_clust) return 0; /* Invalid cluster# */
return clust * fs->sects_clust + fs->database;
}
/*-----------------------------------------------------------------------*/
/* Move Directory Pointer to Next */
/*-----------------------------------------------------------------------*/
static
BOOL next_dir_entry ( /* TRUE: successful, FALSE: could not move next */
DIR *dirobj /* Pointer to directory object */
)
{
DWORD clust;
WORD idx;
FATFS *fs = dirobj->fs;
idx = dirobj->index + 1;
if ((idx & 15) == 0) { /* Table sector changed? */
dirobj->sect++; /* Next sector */
if (!dirobj->clust) { /* In static table */
if (idx >= fs->n_rootdir) return FALSE; /* Reached to end of table */
} else { /* In dynamic table */
if (((idx / 16) & (fs->sects_clust - 1)) == 0) { /* Cluster changed? */
clust = get_cluster(fs, dirobj->clust); /* Get next cluster */
if (clust >= fs->max_clust || clust < 2) /* Reached to end of table */
return FALSE;
dirobj->clust = clust; /* Initialize for new cluster */
dirobj->sect = clust2sect(fs, clust);
}
}
}
dirobj->index = idx; /* Lower 4 bit of dirobj->index indicates offset in dirobj->sect */
return TRUE;
}
/*-----------------------------------------------------------------------*/
/* Get File Status from Directory Entry */
/*-----------------------------------------------------------------------*/
#if _FS_MINIMIZE <= 1
static
void get_fileinfo ( /* No return code */
FILINFO *finfo, /* Ptr to store the file information */
const BYTE *dir /* Ptr to the directory entry */
)
{
BYTE n, c, a;
char *p;
p = &finfo->fname[0];
a = *(dir+12); /* NT flag */
for (n = 0; n < 8; n++) { /* Convert file name (body) */
c = *(dir+n);
if (c == ' ') break;
if (c == 0x05) c = 0xE5;
if (a & 0x08 && c >= 'A' && c <= 'Z') c += 0x20;
*p++ = c;
}
if (*(dir+8) != ' ') { /* Convert file name (extension) */
*p++ = '.';
for (n = 8; n < 11; n++) {
c = *(dir+n);
if (c == ' ') break;
if (a & 0x10 && c >= 'A' && c <= 'Z') c += 0x20;
*p++ = c;
}
}
*p = '\0';
finfo->fattrib = *(dir+11); /* Attribute */
finfo->fsize = LD_DWORD(dir+28); /* Size */
finfo->fdate = LD_WORD(dir+24); /* Date */
finfo->ftime = LD_WORD(dir+22); /* Time */
}
#endif /* _FS_MINIMIZE <= 1 */
/*-----------------------------------------------------------------------*/
/* Pick a Paragraph and Create the Name in Format of Directory Entry */
/*-----------------------------------------------------------------------*/
static
char make_dirfile ( /* 1: error - detected an invalid format, '\0'|'/': next character */
const char **path, /* Pointer to the file path pointer */
char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
)
{
BYTE n, t, c, a, b;
memset(dirname, ' ', 8+3); /* Fill buffer with spaces */
a = 0; b = 0x18; /* NT flag */
n = 0; t = 8;
for (;;) {
c = *(*path)++;
if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */
if (n == 0) break;
dirname[11] = a & b; return c;
}
if (c <= ' ') break; /* Reject invisible chars */
if (c == '.') {
if(!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */
n = 8; t = 11; continue;
}
break;
}
#ifdef _USE_SJIS
if ((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */
(c >= 0xE0 && c <= 0xFC)) {
if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */
c = 0x05;
a ^= 1; goto md_l2;
}
if (c >= 0x7F && c <= 0x80) break; /* Reject \x7F \x80 */
#else
if (c >= 0x7F) goto md_l1; /* Accept \x7F-0xFF */
#endif
if (c == '"') break; /* Reject " */
if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */
if (c <= ',') break; /* Reject * + , */
if (c <= '9') goto md_l1; /* Accept - 0-9 */
if (c <= '?') break; /* Reject : ; < = > ? */
if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */
if (c == '|') break; /* Reject | */
if (c >= '[' && c <= ']') break;/* Reject [ \ ] */
if (c >= 'A' && c <= 'Z')
(t == 8) ? (b &= ~0x08) : (b &= ~0x10);
if (c >= 'a' && c <= 'z') { /* Convert to upper case */
c -= 0x20;
(t == 8) ? (a |= 0x08) : (a |= 0x10);
}
}
md_l1:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -