📄 main.i
字号:
BOOL put_cluster ( /* TRUE: successful, FALSE: failed */
CLUST clust, /* Cluster# to change */
CLUST val /* New value to mark the cluster */
)
{
WORD bc;
BYTE *p;
DWORD fatsect;
FATFS *fs = FatFs;
fatsect = fs->fatbase;
switch (fs->fs_type) {
case 1 :
bc = (WORD)clust * 3 / 2;
if (!move_window(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(fatsect + bc / 512)) return FALSE;
p = &fs->win[bc % 512];
*p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
break;
case 2 :
if (!move_window(fatsect + clust / 256)) return FALSE;
*(volatile BYTE*)(&fs->win[((WORD)clust * 2) % 512])=(BYTE)((WORD)val); *(volatile BYTE*)((&fs->win[((WORD)clust * 2) % 512])+1)=(BYTE)((WORD)((WORD)val)>>8);
break;
case 3 :
if (!move_window(fatsect + clust / 128)) return FALSE;
*(volatile BYTE*)(&fs->win[((WORD)clust * 4) % 512])=(BYTE)(val); *(volatile BYTE*)((&fs->win[((WORD)clust * 4) % 512])+1)=(BYTE)((WORD)(val)>>8); *(volatile BYTE*)((&fs->win[((WORD)clust * 4) % 512])+2)=(BYTE)((DWORD)(val)>>16); *(volatile BYTE*)((&fs->win[((WORD)clust * 4) % 512])+3)=(BYTE)((DWORD)(val)>>24);
break;
default :
return FALSE;
}
fs->winflag = 1;
return TRUE;
}
/*-----------------------------------------------------------------------*/
/* Remove a cluster chain */
/*-----------------------------------------------------------------------*/
static
BOOL remove_chain ( /* TRUE: successful, FALSE: failed */
CLUST clust /* Cluster# to remove chain from */
)
{
CLUST nxt;
FATFS *fs = FatFs;
while (clust >= 2 && clust < fs->max_clust) {
nxt = get_cluster(clust);
if (nxt == 1) return FALSE;
if (!put_cluster(clust, 0)) return FALSE;
if (fs->free_clust != (CLUST)0xFFFFFFFF) {
fs->free_clust++;
fs->fsi_flag = 1;
}
clust = nxt;
}
return TRUE;
}
/*-----------------------------------------------------------------------*/
/* Stretch or create a cluster chain */
/*-----------------------------------------------------------------------*/
static
CLUST create_chain ( /* 0: no free cluster, 1: error, >=2: new cluster number */
CLUST clust /* Cluster# to stretch, 0 means create new */
)
{
CLUST cstat, ncl, scl, mcl;
FATFS *fs = FatFs;
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(clust); /* Check the cluster status */
if (cstat < 2) return 1; /* It is an invalid cluster */
if (cstat < mcl) return cstat; /* It is already followed by next cluster */
scl = clust;
}
ncl = scl; /* Start cluster */
for (;;) {
ncl++; /* Next cluster */
if (ncl >= mcl) { /* Wrap around */
ncl = 2;
if (ncl > scl) return 0; /* No free custer */
}
cstat = get_cluster(ncl); /* Get the cluster status */
if (cstat == 0) break; /* Found a free cluster */
if (cstat == 1) return 1; /* Any error occured */
if (ncl == scl) return 0; /* No free custer */
}
if (!put_cluster(ncl, (CLUST)0x0FFFFFFF)) return 1; /* Mark the new cluster "in use" */
if (clust && !put_cluster(clust, ncl)) return 1; /* Link it to previous one if needed */
fs->last_clust = ncl; /* Update fsinfo */
if (fs->free_clust != (CLUST)0xFFFFFFFF) {
fs->free_clust--;
fs->fsi_flag = 1;
}
return ncl; /* Return new cluster number */
}
/*-----------------------------------------------------------------------*/
/* Get sector# from cluster# */
/*-----------------------------------------------------------------------*/
static
DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */
CLUST clust /* Cluster# to be converted */
)
{
FATFS *fs = FatFs;
clust -= 2;
if (clust >= (fs->max_clust - 2)) return 0; /* Invalid cluster# */
return (DWORD)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 */
)
{
CLUST clust;
WORD idx;
FATFS *fs = FatFs;
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(dirobj->clust); /* Get next cluster */
if (clust < 2 || clust >= fs->max_clust) /* Reached to end of table */
return FALSE;
dirobj->clust = clust; /* Initialize for new cluster */
dirobj->sect = clust2sect(clust);
}
}
}
dirobj->index = idx; /* Lower 4 bit of dirobj->index indicates offset in dirobj->sect */
return TRUE;
}
/*-----------------------------------------------------------------------*/
/* Get file status from directory entry */
/*-----------------------------------------------------------------------*/
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 = 1 ? dir[12] : 0; /* 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 = ((DWORD)(((DWORD)*(volatile BYTE*)((&dir[28])+3)<<24)|((DWORD)*(volatile BYTE*)((&dir[28])+2)<<16)|((WORD)*(volatile BYTE*)((&dir[28])+1)<<8)|*(volatile BYTE*)(&dir[28]))); /* Size */
finfo->fdate = ((WORD)(((WORD)*(volatile BYTE*)((&dir[24])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[24]))); /* Date */
finfo->ftime = ((WORD)(((WORD)*(volatile BYTE*)((&dir[22])+1)<<8)|(WORD)*(volatile BYTE*)(&dir[22]))); /* Time */
}
/*-----------------------------------------------------------------------*/
/* Pick a paragraph and create the name in format of directory entry */
/*-----------------------------------------------------------------------*/
static
char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */
// const char **path, /* Pointer to the file path pointer */
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] = 1 ? (a & b) : 0;
return c;
}
if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */
if (c == '.') {
if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */
n = 8; t = 11; continue;
}
break;
}
if (1 &&
((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 == '"') 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 (1 && c >= 'A' && c <= 'Z')
(t == 8) ? (b &= ~0x08) : (b &= ~0x10);
if (c >= 'a' && c <= 'z') { /* Convert to upper case */
c -= 0x20;
if (1) (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 */
const char *path, /* Full-path string to trace a file or directory */
BYTE **dir /* Directory pointer in Win[] to retutn */
)
{
CLUST clust;
char ds;
BYTE *dptr = 0;
FATFS *fs = FatFs;
/* Initialize directory object */
clust = fs->dirbase;
if (fs->fs_type == 3) {
dirobj->clust = dirobj->sclust = clust;
dirobj->sect = clust2sect(clust);
} else
{
dirobj->clust = dirobj->sclust = 0;
dirobj->sect = clust;
}
dirobj->index = 0;
dirobj->fs = fs;
if (*path == '\0') { /* Null path means the root directory */
*dir = 0; 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(dirobj->sect)) return FR_RW_ERROR;
dptr = &fs->win[(dirobj->index & 15) * 32]; /* Pointer to the directory entry */
if (dptr[0] == 0) /* Has it reached to end of dir? */
return !ds ? FR_NO_FILE : FR_NO_PATH;
if (dptr[0] != 0xE5 /* Matched? */
&& !(dptr[11] & 0x08 )
&& !memcmp(&dptr[0], 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[11] & 0x10 )) return FR_NO_PATH; /* Cannot trace because it is a file */
clust = /* Get cluster# of the directory */
((DWORD)((WORD)(((WORD)*(volatile BYTE*)((&dptr[20])+1)<<8)|(WORD)*(volatile BYTE*)(&dptr[20]))) << 16) |
((WORD)(((WORD)*(volatile BYTE*)((&dptr[26])+1)<<8)|(WORD)*(volatile BYTE*)(&dptr[26])));
dirobj->clust = dirobj->sclust = clust; /* Restart scannig with the new directory */
dirobj->sect = clust2sect(clust);
dirobj->index = 2;
}
}
/*-----------------------------------------------------------------------*/
/* Reserve a directory entry */
/*-----------------------------------------------------------------------*/
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 */
)
{
CLUST clust;
DWORD sector;
BYTE c, n, *dptr;
FATFS *fs = FatFs;
/* Re-initialize directory object */
clust = dirobj->sclust;
if (clust) { /* Dyanmic directory table */
dirobj->clust = clust;
dirobj->sect = clust2sect(clust);
} else { /* Static directory table */
dirobj->sect = fs->dirbase;
}
dirobj->index = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -