📄 ff.c
字号:
FRESULT f_mkdir ( const char *path /* Pointer to the directory path */ ){ U8 *dir, *fw, n; char fn[8+3+1]; U32 sect, dsect, dclust, pclust, tim; FRESULT res; DIR dirobj; FATFS *fs; res = auto_mount(&path, &fs, 1); if (res != FR_OK) return res; dirobj.fs = fs; res = trace_path(&dirobj, fn, path, &dir); /* Trace the file path */ if (res == FR_OK) return FR_EXIST; /* Any file or directory is already existing */ if (res != FR_NO_FILE) return res; res = reserve_direntry(&dirobj, &dir); /* Reserve a directory entry */ if (res != FR_OK) return res; sect = fs->winsect; dclust = create_chain(fs, 0); /* Allocate a cluster for new directory table */ if (dclust == 1) return FR_RW_ERROR; dsect = clust2sect(fs, dclust); if (!dsect) return FR_DENIED; if (!move_window(fs, dsect)) return FR_RW_ERROR; fw = fs->win; memset(fw, 0, S_SIZ); /* Clear the new directory table */ for (n = 1; n < fs->sects_clust; n++) { if (diskWrite(fs->drive, fw, ++dsect, 1) != DRESULT_OK) return FR_RW_ERROR; } memset(&fw[DIR_Name], ' ', 8+3); /* Create "." entry */ fw[DIR_Name] = '.'; fw[DIR_Attr] = AM_DIR; tim = get_fattime(); ST_U32(&fw[DIR_WrtTime], tim); memcpy(&fw[32], &fw[0], 32); fw[33] = '.'; /* Create ".." entry */ pclust = dirobj.sclust;#if _FAT32 ST_U16(&fw[ DIR_FstClusHI], dclust >> 16); if (fs->fs_type == FS_FAT32 && pclust == fs->dirbase) pclust = 0; ST_U16(&fw[32+DIR_FstClusHI], pclust >> 16);#endif ST_U16(&fw[ DIR_FstClusLO], dclust); ST_U16(&fw[32+DIR_FstClusLO], pclust); fs->winflag = 1; if (!move_window(fs, sect)) return FR_RW_ERROR; memset(&dir[0], 0, 32); /* Initialize the new entry */ memcpy(&dir[DIR_Name], fn, 8+3); /* Name */ dir[DIR_NTres] = fn[11]; dir[DIR_Attr] = AM_DIR; /* Attribute */ ST_U32(&dir[DIR_WrtTime], tim); /* Crated time */ ST_U16(&dir[DIR_FstClusLO], dclust); /* Table start cluster */ ST_U16(&dir[DIR_FstClusHI], dclust >> 16); return sync(fs);}/*-----------------------------------------------------------------------*//* Change File Attribute *//*-----------------------------------------------------------------------*/FRESULT f_chmod ( const char *path, /* Pointer to the file path */ U8 value, /* Attribute bits */ U8 mask /* Attribute mask to change */ ){ FRESULT res; U8 *dir; DIR dirobj; char fn[8+3+1]; FATFS *fs; res = auto_mount(&path, &fs, 1); if (res == FR_OK) { dirobj.fs = fs; res = trace_path(&dirobj, fn, path, &dir); /* Trace the file path */ if (res == FR_OK) { /* Trace completed */ if (dir == NULL) { res = FR_INVALID_NAME; } else { mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */ dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (U8)~mask); /* Apply attribute change */ res = sync(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; U32 sect_old; U8 *dir_old, *dir_new, direntry[32-11]; DIR dirobj; char fn[8+3+1]; FATFS *fs; res = auto_mount(&path_old, &fs, 1); if (res != FR_OK) return res; dirobj.fs = fs; res = trace_path(&dirobj, 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 = fs->winsect; /* Save the object information */ memcpy(direntry, &dir_old[DIR_Attr], 32-11); res = trace_path(&dirobj, 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(&dirobj, &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]; fs->winflag = 1; if (!move_window(fs, sect_old)) return FR_RW_ERROR; /* Remove old entry */ dir_old[DIR_Name] = 0xE5; return sync(fs);}#if _USE_MKFS/*-----------------------------------------------------------------------*//* Create File System on the Drive *//*-----------------------------------------------------------------------*/#define N_ROOTDIR 512#define N_FATS 1#define MAX_SECTOR 64000000UL#define MIN_SECTOR 2000UL#define ERASE_BLK 32FRESULT f_mkfs ( U8 drv, /* Logical drive number */ U8 partition, /* Partitioning rule 0:FDISK, 1:SFD */ U8 allocsize /* Allocation unit size [sectors] */ ){ U8 fmt, m, *tbl; U32 b_part, b_fat, b_dir, b_data; /* Area offset (LBA) */ U32 n_part, n_rsv, n_fat, n_dir; /* Area size */ U32 n_clust, n; FATFS *fs; DSTATUS stat; /* Check and mounted drive and clear work area */ if (drv >= _DRIVES) return FR_INVALID_DRIVE; if (!(fs = FatFs [drv])) return FR_NOT_ENABLED; memset (fs, 0, sizeof(FATFS)); drv = LD2PD(drv); /* Check validity of the parameters */ for (n = 1; n <= 64 && allocsize != n; n <<= 1) ; if (n > 64 || partition >= 2) { // ### // printf ("line %d: n > 64 || partition >= 2. n=%d, partition=%d\n", __LINE__, n, partition); // ### return FR_MKFS_ABORTED; } // ### /* Get disk statics */ stat = diskInitialize (drv); if (stat & DSTATUS_NOINIT) return FR_NOT_READY; if (stat & DSTATUS_PROTECT) return FR_WRITE_PROTECTED; { // ### DRESULT dres; // ### if ((dres = diskIoctl (drv, IOCTL_GET_SECTOR_COUNT, &n_part)) != DRESULT_OK || n_part < MIN_SECTOR) { // ### // printf ("line %d: ioctl returned %d. n_part=%d, MIN_SECTOR=%d\n", __LINE__, dres, n_part, MIN_SECTOR); // ### return FR_MKFS_ABORTED; } // ### } // ### if (n_part > MAX_SECTOR) n_part = MAX_SECTOR; b_part = (!partition) ? 63 : 0; n_part -= b_part;#if S_MAX_SIZ > 512 /* Check disk sector size */ if (diskIoctl(drv, IOCTL_GET_SECTOR_SIZE, &S_SIZ) != DRESULT_OK || S_SIZ > S_MAX_SIZ || (U32)S_SIZ * allocsize > 32768U) return FR_MKFS_ABORTED;#endif /* Pre-compute number of clusters and FAT type */ n_clust = n_part / allocsize; fmt = FS_FAT12; if (n_clust >= 0xFF7) fmt = FS_FAT16; if (n_clust >= 0xFFF7) fmt = FS_FAT32; switch (fmt) { case FS_FAT12: n_fat = ((n_clust * 3 + 1) / 2 + 3 + S_SIZ - 1) / S_SIZ; n_rsv = 1 + partition; n_dir = N_ROOTDIR * 32 / S_SIZ; break; case FS_FAT16: n_fat = ((n_clust * 2) + 4 + S_SIZ - 1) / S_SIZ; n_rsv = 1 + partition; n_dir = N_ROOTDIR * 32 / S_SIZ; break; default: n_fat = ((n_clust * 4) + 8 + S_SIZ - 1) / S_SIZ; 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 */#ifdef ERASE_BLK /* Round up data start sector to erase block boundary */ n = (b_data + ERASE_BLK - 1) & ~(ERASE_BLK - 1); b_dir += n - b_data; n_fat += (n - b_data) / N_FATS;#endif /* Determine number of cluster and final check of validity of the FAT type */ n_clust = (n_part - n_rsv - n_fat * 2 - n_dir) / allocsize; if ( (fmt == FS_FAT16 && n_clust < 0xFF7) || (fmt == FS_FAT32 && n_clust < 0xFFF7)) { // ### // printf ("line %d: fmt=%d, FS_FAT16=%d, FS_FAT32=%d, n_clust=0x%x\n", __LINE__, fmt, FS_FAT16, FS_FAT32, n_clust); // ### return FR_MKFS_ABORTED; } // ### /* Create partition table if needed */ if (!partition) { U32 n_disk = b_part + n_part; tbl = &fs->win[MBR_Table]; ST_U32(&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] = (U8)n_disk; tbl[6] = (U8)((n_disk >> 2) | 63); } else { ST_U16(&tbl[6], 0xFFFF); } tbl[5] = 254; if (fmt != FS_FAT32) /* System ID */ tbl[4] = (n_part < 0x10000) ? 0x04 : 0x06; else tbl[4] = 0x0c; ST_U32(&tbl[8], 63); /* Partition start in LBA */ ST_U32(&tbl[12], n_part); /* Partition size in LBA */ ST_U16(&tbl[64], 0xAA55); /* Signature */ if (diskWrite(drv, fs->win, 0, 1) != DRESULT_OK) return FR_RW_ERROR; } /* Create boot record */ memset(tbl = fs->win, 0, S_SIZ); ST_U32(&tbl[BS_jmpBoot], 0x90FEEB); /* Boot code (jmp $, nop) */ ST_U16(&tbl[BPB_BytsPerSec], S_SIZ); /* Sector size */ tbl[BPB_SecPerClus] = (U8)allocsize; /* Sectors per cluster */ ST_U16(&tbl[BPB_RsvdSecCnt], n_rsv); /* Reserved sectors */ tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */ ST_U16(&tbl[BPB_RootEntCnt], S_SIZ / 32 * n_dir); /* Number of rootdir entries */ if (n_part < 0x10000) { /* Number of total sectors */ ST_U16(&tbl[BPB_TotSec16], n_part); } else { ST_U32(&tbl[BPB_TotSec32], n_part); } tbl[BPB_Media] = 0xF8; /* Media descripter */ ST_U16(&tbl[BPB_SecPerTrk], 63); /* Number of sectors per track */ ST_U16(&tbl[BPB_NumHeads], 255); /* Number of heads */ ST_U32(&tbl[BPB_HiddSec], b_part); /* Hidden sectors */ if (fmt != FS_FAT32) { ST_U16(&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_U32(&tbl[BPB_FATSz32], n_fat); /* Number of secters per FAT */ ST_U32(&tbl[BPB_RootClus], 2); /* Root directory cluster (2) */ ST_U16(&tbl[BPB_FSInfo], 1); /* FSInfo record (bs+1) */ ST_U16(&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_U16(&tbl[BS_55AA], 0xAA55); /* Signature */ if (diskWrite(drv, tbl, b_part+0, 1) != DRESULT_OK) return FR_RW_ERROR; if (fmt == FS_FAT32) diskWrite(drv, tbl, b_part+6, 1); /* Initialize FAT area */ for (m = 0; m < N_FATS; m++) { memset(tbl, 0, S_SIZ); /* 1st sector of the FAT */ if (fmt != FS_FAT32) { n = (fmt == FS_FAT12) ? 0x00FFFFF8 : 0xFFFFFFF8; ST_U32(&tbl[0], n); /* Reserve cluster #0-1 (FAT12/16) */ } else { ST_U32(&tbl[0], 0xFFFFFFF8); /* Reserve cluster #0-1 (FAT32) */ ST_U32(&tbl[4], 0xFFFFFFFF); ST_U32(&tbl[8], 0x0FFFFFFF); /* Reserve cluster #2 for root dir */ } if (diskWrite(drv, tbl, b_fat++, 1) != DRESULT_OK) return FR_RW_ERROR; memset(tbl, 0, S_SIZ); /* Following FAT entries are filled by zero */ for (n = 1; n < n_fat; n++) { if (diskWrite(drv, tbl, b_fat++, 1) != DRESULT_OK) return FR_RW_ERROR; } } /* Initialize Root directory */ for (m = 0; m < 64; m++) { if (diskWrite(drv, tbl, b_fat++, 1) != DRESULT_OK) return FR_RW_ERROR; } /* Create FSInfo record if needed */ if (fmt == FS_FAT32) { ST_U16(&tbl[BS_55AA], 0xAA55); ST_U32(&tbl[FSI_LeadSig], 0x41615252); ST_U32(&tbl[FSI_StrucSig], 0x61417272); ST_U32(&tbl[FSI_Free_Count], n_clust - 1); ST_U32(&tbl[FSI_Nxt_Free], 0xFFFFFFFF); diskWrite(drv, tbl, b_part+1, 1); diskWrite(drv, tbl, b_part+7, 1); } return (diskIoctl(drv, IOCTL_CTRL_SYNC, NULL) == DRESULT_OK) ? FR_OK : FR_RW_ERROR;}#endif /* _USE_MKFS */#endif /* !_FS_READONLY */#endif /* _FS_MINIMIZE == 0 */#endif /* _FS_MINIMIZE <= 1 */#endif /* _FS_MINIMIZE <= 2 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -