?? ff.c
字號:
if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */
c = 0x05;
a ^= 1; goto md_l2;
}
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 (_USE_NTFLAG && c >= 'A' && c <= 'Z')
(t == 8) ? (b &= ~0x08) : (b &= ~0x10);
if (c >= 'a' && c <= 'z') { /* Convert to upper case */
c -= 0x20;
if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10);
}
}
md_l1:
a &= ~1;
md_l2:
if (n >= t) break;
dirname[n++] = c;
}
return 1;
}
/*-----------------------------------------------------------------------*/
/* Trace a file path */
/*-----------------------------------------------------------------------*/
static
FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */
DIR *dirobj, /* Pointer to directory object to return last directory */
char *fn, /* Pointer to last segment name to return {file(8),ext(3),attr(1)} */
const char *path, /* Full-path string to trace a file or directory */
BYTE **dir /* Directory pointer in Win[] to retutn */
)
{
DWORD clust;
char ds;
BYTE *dptr = NULL;
FATFS *fs = dirobj->fs; /* Get logical drive from the given DIR structure */
/* Initialize directory object */
clust = fs->dirbase;
if (fs->fs_type == FS_FAT32) {
dirobj->clust = dirobj->sclust = clust;
dirobj->sect = clust2sect(fs, clust);
} else {
dirobj->clust = dirobj->sclust = 0;
dirobj->sect = clust;
}
dirobj->index = 0;
if (*path == '\0') { /* 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(fs, dirobj->sect)) return FR_RW_ERROR;
dptr = &fs->win[(dirobj->index & ((S_SIZ - 1) / 32)) * 32]; /* Pointer to the directory entry */
if (dptr[DIR_Name] == 0) /* Has it reached to end of dir? */
return !ds ? FR_NO_FILE : FR_NO_PATH;
if (dptr[DIR_Name] != 0xE5 /* Matched? */
&& !(dptr[DIR_Attr] & AM_VOL)
&& !memcmp(&dptr[DIR_Name], fn, 8+3) ) break;
if (!next_dir_entry(dirobj)) /* 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[DIR_Attr] & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */
clust = ((DWORD)LD_WORD(&dptr[DIR_FstClusHI]) << 16) | LD_WORD(&dptr[DIR_FstClusLO]); /* Get cluster# of the directory */
dirobj->clust = dirobj->sclust = clust; /* Restart scanning at the new directory */
dirobj->sect = clust2sect(fs, clust);
dirobj->index = 2;
}
}
/*-----------------------------------------------------------------------*/
/* Reserve a directory entry */
/*-----------------------------------------------------------------------*/
#if !_FS_READONLY
static
FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */
DIR *dirobj, /* Target directory to create new entry */
BYTE **dir /* Pointer to pointer to created entry to retutn */
)
{
DWORD clust, sector;
BYTE c, n, *dptr;
FATFS *fs = dirobj->fs;
/* Re-initialize directory object */
clust = dirobj->sclust;
if (clust) { /* Dyanmic directory table */
dirobj->clust = clust;
dirobj->sect = clust2sect(fs, clust);
} else { /* Static directory table */
dirobj->sect = fs->dirbase;
}
dirobj->index = 0;
do {
if (!move_window(fs, dirobj->sect)) return FR_RW_ERROR;
dptr = &fs->win[(dirobj->index & ((S_SIZ - 1) / 32)) * 32]; /* Pointer to the directory entry */
c = dptr[DIR_Name];
if (c == 0 || c == 0xE5) { /* Found an empty entry! */
*dir = dptr; return FR_OK;
}
} while (next_dir_entry(dirobj)); /* 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(fs, dirobj->clust))) return FR_DENIED;
if (clust == 1 || !move_window(fs, 0)) return FR_RW_ERROR;
fs->winsect = sector = clust2sect(fs, clust); /* Cleanup the expanded table */
memset(fs->win, 0, S_SIZ);
for (n = fs->sects_clust; n; n--) {
if (disk_write(fs->drive, fs->win, sector, 1) != RES_OK)
return FR_RW_ERROR;
sector++;
}
fs->winflag = 1;
*dir = fs->win;
return FR_OK;
}
#endif /* !_FS_READONLY */
/*-----------------------------------------------------------------------*/
/* Load boot record and check if it is a FAT boot record */
/*-----------------------------------------------------------------------*/
static
BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error */
FATFS *fs, /* File system object */
DWORD sect /* Sector# (lba) to check if it is a FAT boot record or not */
)
{
if (disk_read(fs->drive, fs->win, sect, 1) != RES_OK) /* Load boot record */
return 2;
if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always offset 510) */
return 2;
if (!memcmp(&fs->win[BS_FilSysType], "FAT", 3)) /* Check FAT signature */
return 0;
if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
return 0;
return 1;
}
/*-----------------------------------------------------------------------*/
/* Make sure that the file system is valid */
/*-----------------------------------------------------------------------*/
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 wrinting fuctions */
)
{
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, select drive 0 in 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? */
if (!(fs = FatFs[drv])) return FR_NOT_ENABLED; /* Is the file system object registered? */
*rfs = fs; /* Returen pointer to the corresponding file system object */
/* Check if the logical drive has been mounted or not */
if (fs->fs_type) {
stat = disk_status(fs->drive);
if (!(stat & STA_NOINIT)) { /* If the physical drive is kept initialized */
#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 has not been 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 /* Check disk sector size */
if (disk_ioctl(drv, GET_SECTOR_SIZE, &S_SIZ) != RES_OK || S_SIZ > 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 */
if (fmt == 1) { /* Not a 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 */
}
}
if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != S_SIZ) /* No valid FAT patition is found */
return FR_NO_FILESYSTEM;
/* Initialize the file system object */
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->sects_clust = 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 /* Last cluster# + 1 */
- LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / (S_SIZ/32)
) / fs->sects_clust + 2;
fmt = FS_FAT12; /* Determine the FAT sub type */
if (maxclust > 0xFF7) fmt = FS_FAT16;
if (maxclust > 0xFFF7) fmt = FS_FAT32;
fs->fs_type = fmt;
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 / (S_SIZ/32); /* Data start sector (lba) */
#if !_FS_READONLY
fs->free_clust = 0xFFFFFFFF;
#if _USE_FSINFO
/* Load fsinfo sector if needed */
if (fmt == FS_FAT32) {
fs->fsi_sector = bootsect + LD_WORD(&fs->win[BPB_FSInfo]);
if (disk_read(0, 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->id = ++fsid; /* File system mount ID */
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Check if the file/dir object is valid or not */
/*-----------------------------------------------------------------------*/
static
FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Not valid */
const FATFS *fs, /* Pointer to the file system object */
WORD id /* id member of the target object to be checked */
)
{
if (!fs || fs->id != id)
return FR_INVALID_OBJECT;
if (disk_status(fs->drive) & STA_NOINIT)
return FR_NOT_READY;
return FR_OK;
}
/*--------------------------------------------------------------------------
Public Functions
--------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/* Mount/Unmount a Locical Drive */
/*-----------------------------------------------------------------------*/
FRESULT f_mount (
BYTE drv, /* Logical drive number to be mounted/unmounted */
FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
)
{
FATFS *fsobj;
if (drv >= _DRIVES) return FR_INVALID_DRIVE;
fsobj = FatFs[drv];
FatFs[drv] = fs;
if (fsobj) memset(fsobj, 0, sizeof(FATFS));
if (fs) memset(fs, 0, sizeof(FATFS));
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Open or Create a File */
/*-----------------------------------------------------------------------*/
FRESULT f_open (
FIL *fp, /* Pointer to the blank file object */
const char *path, /* Pointer to the file name */
BYTE mode /* Access mode and file open mode flags */
)
{
FRESULT res;
BYTE *dir;
DIR dirobj;
char fn[8+3+1];
FATFS *fs;
fp->fs = NULL;
#if !_FS_READONLY
mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW);
res = auto_mount(&path, &fs, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)));
#else
mode &= FA_READ;
res = auto_mount(&path, &fs, 0);
#endif
if (res != FR_OK) return res;
dirobj.fs = fs;
/* Trace the file path */
res = trace_path(&dirobj, fn, path, &dir);
#if !_FS_READONLY
/* Create or Open a file */
if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) {
DWORD ps, rs;
if (res != FR_OK) { /* No file, create new */
if (res != FR_NO_FILE) return res;
res = reserve_direntry(&dirobj, &dir);
if (res != FR_OK) return res;
memset(dir, 0, 32); /* Initialize the new entry with open name */
memcpy(&dir[DIR_Name], fn, 8+3);
dir[DIR_NTres] = fn[11];
mode |= FA_CREATE_ALWAYS;
}
else { /* Any object is already existing */
if (mode & FA_CREATE_NEW) /* Cannot create new */
return FR_EXIST;
if (dir == NULL || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite it (R/O or DIR) */
return FR_DENIED;
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero if needed */
rs = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); /* Get start cluster */
ST_WORD(&dir[DIR_FstClusHI], 0); /* cluster = 0 */
ST_WORD(&dir[DIR_FstClusLO], 0);
ST_DWORD(&dir[DIR_FileSize], 0); /* size = 0 */
fs->winflag = 1;
ps = fs->winsect; /* Remove the cluster chain */
if (!remove_chain(fs, rs) || !move_window(fs, ps))
return FR_RW_ERROR;
fs->last_clust = rs - 1; /* Reuse the cluster hole */
}
}
if (mode & FA_CREATE_ALWAYS) {
dir[DIR_Attr] = AM_ARC; /* New attribute */
ps = get_fattime();
ST_DWORD(&dir[DIR_WrtTime], ps); /* Updated time */
ST_DWORD(&dir[DIR_CrtTime], ps); /* Created time */
fs->winflag = 1;
}
}
/* Open an existing file */
else {
#endif /* !_FS_READONLY */
if (res != FR_OK) return res; /* Trace failed */
if (dir == NULL || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
return FR_NO_FILE;
#if !_FS_READONLY
if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
return FR_DENIED;
}
fp->dir_sect = fs->winsect; /* Pointer to the directory entry */
fp->dir_ptr = dir;
#endif
fp->flag = mode; /* File access mode */
fp->org_clust = /* File start cluster */
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */
fp->fptr = 0; /* File ptr */
fp->sect_clust = 1; /* Sector counter */
fp->fs = fs; fp->id = fs->id; /* Owner file system object of the file */
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Read File */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -