?? ff.c
字號(hào):
/*-----------------------------------------------------------------------*/
/* Change Timestamp */
/*-----------------------------------------------------------------------*/
FRESULT f_utime (
const char *path, /* Pointer to the file/directory name */
const FILINFO *finfo /* Pointer to the timestamp to be set */
)
{
FRESULT res;
DIR dj;
BYTE *dir;
char fn[8+3+1];
res = auto_mount(&path, &dj.fs, 1);
if (res == FR_OK) {
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
if (res == FR_OK) { /* Trace completed */
if (!dir) {
res = FR_INVALID_NAME; /* Root directory */
} else {
ST_WORD(&dir[DIR_WrtTime], finfo->ftime);
ST_WORD(&dir[DIR_WrtDate], finfo->fdate);
res = sync(dj.fs);
}
}
}
return res;
}
/*-----------------------------------------------------------------------*/
/* Rename File/Directory */
/*-----------------------------------------------------------------------*/
FRESULT f_rename (
const char *path_old, /* Pointer to the old name */
const char *path_new /* Pointer to the new name */
)
{
FRESULT res;
DIR dj;
DWORD sect_old;
BYTE *dir_old, *dir_new, direntry[32-11];
char fn[8+3+1];
res = auto_mount(&path_old, &dj.fs, 1);
if (res != FR_OK) return res;
res = trace_path(&dj, fn, path_old, &dir_old); /* Check old object */
if (res != FR_OK) return res; /* The old object is not found */
if (!dir_old) return FR_NO_FILE;
sect_old = dj.fs->winsect; /* Save the object information */
memcpy(direntry, &dir_old[DIR_Attr], 32-11);
res = trace_path(&dj, fn, path_new, &dir_new); /* Check new object */
if (res == FR_OK) return FR_EXIST; /* The new object name is already existing */
if (res != FR_NO_FILE) return res; /* Is there no old name? */
res = reserve_direntry(&dj, &dir_new); /* Reserve a directory entry */
if (res != FR_OK) return res;
memcpy(&dir_new[DIR_Attr], direntry, 32-11); /* Create new entry */
memcpy(&dir_new[DIR_Name], fn, 8+3);
dir_new[DIR_NTres] = fn[11];
dj.fs->winflag = 1;
if (!move_window(dj.fs, sect_old)) return FR_RW_ERROR; /* Delete old entry */
dir_old[DIR_Name] = 0xE5;
return sync(dj.fs);
}
#endif /* !_FS_READONLY */
#endif /* _FS_MINIMIZE == 0 */
#endif /* _FS_MINIMIZE <= 1 */
#endif /* _FS_MINIMIZE <= 2 */
#if _USE_MKFS && !_FS_READONLY
/*-----------------------------------------------------------------------*/
/* Create File System on the Drive */
/*-----------------------------------------------------------------------*/
#define N_ROOTDIR 512 /* Multiple of 32 and <= 2048 */
#define N_FATS 1 /* 1 or 2 */
#define MAX_SECTOR 64000000UL /* Maximum partition size */
#define MIN_SECTOR 2000UL /* Minimum partition size */
FRESULT f_mkfs (
BYTE drv, /* Logical drive number */
BYTE partition, /* Partitioning rule 0:FDISK, 1:SFD */
WORD allocsize /* Allocation unit size [bytes] */
)
{
BYTE fmt, m, *tbl;
DWORD b_part, b_fat, b_dir, b_data; /* Area offset (LBA) */
DWORD n_part, n_rsv, n_fat, n_dir; /* Area size */
DWORD n_clust, n;
FATFS *fs;
DSTATUS stat;
/* Check validity of the parameters */
if (drv >= _DRIVES) return FR_INVALID_DRIVE;
if (partition >= 2) return FR_MKFS_ABORTED;
for (n = 512; n <= 32768U && n != allocsize; n <<= 1);
if (n != allocsize) return FR_MKFS_ABORTED;
/* Check mounted drive and clear work area */
fs = FatFs[drv];
if (!fs) return FR_NOT_ENABLED;
fs->fs_type = 0;
drv = LD2PD(drv);
/* Get disk statics */
stat = disk_initialize(drv);
if (stat & STA_NOINIT) return FR_NOT_READY;
if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
if (disk_ioctl(drv, GET_SECTOR_COUNT, &n_part) != RES_OK || n_part < MIN_SECTOR)
return FR_MKFS_ABORTED;
if (n_part > MAX_SECTOR) n_part = MAX_SECTOR;
b_part = (!partition) ? 63 : 0; /* Boot sector */
n_part -= b_part;
#if S_MAX_SIZ > 512 /* Check disk sector size */
if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
|| SS(fs) > S_MAX_SIZ
|| SS(fs) > allocsize)
return FR_MKFS_ABORTED;
#endif
allocsize /= SS(fs); /* Number of sectors per cluster */
/* Pre-compute number of clusters and FAT type */
n_clust = n_part / allocsize;
fmt = FS_FAT12;
if (n_clust >= 0xFF5) fmt = FS_FAT16;
if (n_clust >= 0xFFF5) fmt = FS_FAT32;
/* Determine offset and size of FAT structure */
switch (fmt) {
case FS_FAT12:
n_fat = ((n_clust * 3 + 1) / 2 + 3 + SS(fs) - 1) / SS(fs);
n_rsv = 1 + partition;
n_dir = N_ROOTDIR * 32 / SS(fs);
break;
case FS_FAT16:
n_fat = ((n_clust * 2) + 4 + SS(fs) - 1) / SS(fs);
n_rsv = 1 + partition;
n_dir = N_ROOTDIR * 32 / SS(fs);
break;
default:
n_fat = ((n_clust * 4) + 8 + SS(fs) - 1) / SS(fs);
n_rsv = 33 - partition;
n_dir = 0;
}
b_fat = b_part + n_rsv; /* FATs start sector */
b_dir = b_fat + n_fat * N_FATS; /* Directory start sector */
b_data = b_dir + n_dir; /* Data start sector */
/* Align data start sector to erase block boundary (for flash memory media) */
if (disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK) return FR_MKFS_ABORTED;
n = (b_data + n - 1) & ~(n - 1);
n_fat += (n - b_data) / N_FATS;
/* b_dir and b_data are no longer used below */
/* Determine number of cluster and final check of validity of the FAT type */
n_clust = (n_part - n_rsv - n_fat * N_FATS - n_dir) / allocsize;
if ( (fmt == FS_FAT16 && n_clust < 0xFF5)
|| (fmt == FS_FAT32 && n_clust < 0xFFF5))
return FR_MKFS_ABORTED;
/* Create partition table if needed */
if (!partition) {
DWORD n_disk = b_part + n_part;
tbl = &fs->win[MBR_Table];
ST_DWORD(&tbl[0], 0x00010180); /* Partition start in CHS */
if (n_disk < 63UL * 255 * 1024) { /* Partition end in CHS */
n_disk = n_disk / 63 / 255;
tbl[7] = (BYTE)n_disk;
tbl[6] = (BYTE)((n_disk >> 2) | 63);
} else {
ST_WORD(&tbl[6], 0xFFFF);
}
tbl[5] = 254;
if (fmt != FS_FAT32) /* System ID */
tbl[4] = (n_part < 0x10000) ? 0x04 : 0x06;
else
tbl[4] = 0x0c;
ST_DWORD(&tbl[8], 63); /* Partition start in LBA */
ST_DWORD(&tbl[12], n_part); /* Partition size in LBA */
ST_WORD(&tbl[64], 0xAA55); /* Signature */
if (disk_write(drv, fs->win, 0, 1) != RES_OK)
return FR_RW_ERROR;
}
/* Create boot record */
tbl = fs->win; /* Clear buffer */
memset(tbl, 0, SS(fs));
ST_DWORD(&tbl[BS_jmpBoot], 0x90FEEB); /* Boot code (jmp $, nop) */
ST_WORD(&tbl[BPB_BytsPerSec], SS(fs)); /* Sector size */
tbl[BPB_SecPerClus] = (BYTE)allocsize; /* Sectors per cluster */
ST_WORD(&tbl[BPB_RsvdSecCnt], n_rsv); /* Reserved sectors */
tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
ST_WORD(&tbl[BPB_RootEntCnt], SS(fs) / 32 * n_dir); /* Number of rootdir entries */
if (n_part < 0x10000) { /* Number of total sectors */
ST_WORD(&tbl[BPB_TotSec16], n_part);
} else {
ST_DWORD(&tbl[BPB_TotSec32], n_part);
}
tbl[BPB_Media] = 0xF8; /* Media descripter */
ST_WORD(&tbl[BPB_SecPerTrk], 63); /* Number of sectors per track */
ST_WORD(&tbl[BPB_NumHeads], 255); /* Number of heads */
ST_DWORD(&tbl[BPB_HiddSec], b_part); /* Hidden sectors */
n = get_fattime(); /* Use current time as a VSN */
if (fmt != FS_FAT32) {
ST_DWORD(&tbl[BS_VolID], n); /* Volume serial number */
ST_WORD(&tbl[BPB_FATSz16], n_fat); /* Number of secters per FAT */
tbl[BS_DrvNum] = 0x80; /* Drive number */
tbl[BS_BootSig] = 0x29; /* Extended boot signature */
memcpy(&tbl[BS_VolLab], "NO NAME FAT ", 19); /* Volume lavel, FAT signature */
} else {
ST_DWORD(&tbl[BS_VolID32], n); /* Volume serial number */
ST_DWORD(&tbl[BPB_FATSz32], n_fat); /* Number of secters per FAT */
ST_DWORD(&tbl[BPB_RootClus], 2); /* Root directory cluster (2) */
ST_WORD(&tbl[BPB_FSInfo], 1); /* FSInfo record (bs+1) */
ST_WORD(&tbl[BPB_BkBootSec], 6); /* Backup boot record (bs+6) */
tbl[BS_DrvNum32] = 0x80; /* Drive number */
tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
memcpy(&tbl[BS_VolLab32], "NO NAME FAT32 ", 19); /* Volume lavel, FAT signature */
}
ST_WORD(&tbl[BS_55AA], 0xAA55); /* Signature */
if (disk_write(drv, tbl, b_part+0, 1) != RES_OK)
return FR_RW_ERROR;
if (fmt == FS_FAT32)
disk_write(drv, tbl, b_part+6, 1);
/* Initialize FAT area */
for (m = 0; m < N_FATS; m++) {
memset(tbl, 0, SS(fs)); /* 1st sector of the FAT */
if (fmt != FS_FAT32) {
n = (fmt == FS_FAT12) ? 0x00FFFFF8 : 0xFFFFFFF8;
ST_DWORD(&tbl[0], n); /* Reserve cluster #0-1 (FAT12/16) */
} else {
ST_DWORD(&tbl[0], 0xFFFFFFF8); /* Reserve cluster #0-1 (FAT32) */
ST_DWORD(&tbl[4], 0xFFFFFFFF);
ST_DWORD(&tbl[8], 0x0FFFFFFF); /* Reserve cluster #2 for root dir */
}
if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
return FR_RW_ERROR;
memset(tbl, 0, SS(fs)); /* Following FAT entries are filled by zero */
for (n = 1; n < n_fat; n++) {
if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
return FR_RW_ERROR;
}
}
/* Initialize Root directory */
m = (BYTE)((fmt == FS_FAT32) ? allocsize : n_dir);
do {
if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
return FR_RW_ERROR;
} while (--m);
/* Create FSInfo record if needed */
if (fmt == FS_FAT32) {
ST_WORD(&tbl[BS_55AA], 0xAA55);
ST_DWORD(&tbl[FSI_LeadSig], 0x41615252);
ST_DWORD(&tbl[FSI_StrucSig], 0x61417272);
ST_DWORD(&tbl[FSI_Free_Count], n_clust - 1);
ST_DWORD(&tbl[FSI_Nxt_Free], 0xFFFFFFFF);
disk_write(drv, tbl, b_part+1, 1);
disk_write(drv, tbl, b_part+7, 1);
}
return (disk_ioctl(drv, CTRL_SYNC, NULL) == RES_OK) ? FR_OK : FR_RW_ERROR;
}
#endif /* _USE_MKFS && !_FS_READONLY */
#if _USE_STRFUNC >= 1
/*-----------------------------------------------------------------------*/
/* Get a string from the file */
/*-----------------------------------------------------------------------*/
char* fgets (
char* buff, /* Pointer to the string buffer to read */
int len, /* Size of string buffer */
FIL* fil /* Pointer to the file object */
)
{
int i = 0;
char *p = buff;
UINT rc;
while (i < len - 1) { /* Read bytes until buffer gets filled */
f_read(fil, p, 1, &rc);
if (rc != 1) break; /* Break when no data to read */
#if _USE_STRFUNC >= 2
if (*p == '\r') continue; /* Strip '\r' */
#endif
i++;
if (*p++ == '\n') break; /* Break when reached end of line */
}
*p = 0;
return i ? buff : 0; /* When no data read (eof or error), return with error. */
}
#if !_FS_READONLY
#include <stdarg.h>
/*-----------------------------------------------------------------------*/
/* Put a character to the file */
/*-----------------------------------------------------------------------*/
int fputc (
int chr, /* A character to be output */
FIL* fil /* Ponter to the file object */
)
{
UINT bw;
char c;
#if _USE_STRFUNC >= 2
if (chr == '\n') fputc ('\r', fil); /* LF -> CRLF conversion */
#endif
if (!fil) { /* Special value may be used to switch the destination to any other device */
/* put_console(chr); */
return chr;
}
c = (char)chr;
f_write(fil, &c, 1, &bw); /* Write a byte to the file */
return bw ? chr : EOF; /* Return the resulut */
}
/*-----------------------------------------------------------------------*/
/* Put a string to the file */
/*-----------------------------------------------------------------------*/
int fputs (
const char* str, /* Pointer to the string to be output */
FIL* fil /* Pointer to the file object */
)
{
int n;
for (n = 0; *str; str++, n++) {
if (fputc(*str, fil) == EOF) return EOF;
}
return n;
}
/*-----------------------------------------------------------------------*/
/* Put a formatted string to the file */
/*-----------------------------------------------------------------------*/
int fprintf (
FIL* fil, /* Pointer to the file object */
const char* str, /* Pointer to the format string */
... /* Optional arguments... */
)
{
va_list arp;
UCHAR c, f, r;
ULONG val;
char s[16];
int i, w, res, cc;
va_start(arp, str);
for (cc = res = 0; cc != EOF; res += cc) {
c = *str++;
if (c == 0) break; /* End of string */
if (c != '%') { /* Non escape cahracter */
cc = fputc(c, fil);
if (cc != EO
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -