📄 tff.lst
字号:
642
643
644
645
646 /*-----------------------------------------------------------------------*/
647 /* Make sure that the file system is valid */
648 /*-----------------------------------------------------------------------*/
649
650 static
651 FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */
652 const char **path, /* Pointer to pointer to the path name (drive number) */
653 BYTE chk_wp /* !=0: Check media write protection for write access */
654 )
655 {
656 1 BYTE fmt;
657 1 DSTATUS stat;
658 1 DWORD bootsect, fatsize, totalsect, maxclust;
659 1 const char *p = *path;
660 1 FATFS *fs;
661 1
662 1
663 1 while (*p == ' ') p++; /* Strip leading spaces */
664 1 if (*p == '/') p++; /* Strip heading slash */
665 1 *path = p; /* Return pointer to the path name */
666 1
667 1 /* Is the file system object registered? */
668 1 fs = FatFs;
669 1 if (!fs) return FR_NOT_ENABLED;
670 1
671 1 if (fs->fs_type) { /* If the logical drive has been mounted */
672 2 stat = disk_status(0);
673 2 if (!(stat & STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 12
674 3 #if !_FS_READONLY
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
return FR_WRITE_PROTECTED;
#endif
678 3 return FR_OK; /* The file system object is valid */
679 3 }
680 2 }
681 1
682 1 /* The logical drive must be re-mounted. Following code attempts to mount the logical drive */
683 1
684 1 memset(fs, 0, sizeof(FATFS)); /* Clean-up the file system object */
685 1 stat = disk_initialize(0); /* Initialize low level disk I/O layer */
686 1 if (stat & STA_NOINIT) /* Check if the drive is ready */
687 1 return FR_NOT_READY;
688 1 #if !_FS_READONLY
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
return FR_WRITE_PROTECTED;
#endif
692 1
693 1 /* Search FAT partition on the drive */
694 1 fmt = check_fs(bootsect = 0); /* Check sector 0 as an SFD format */
695 1 if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */
696 2 /* Check a partition listed in top of the partition table */
697 2 if (fs->win[MBR_Table+4]) { /* Is the 1st partition existing? */
698 3 bootsect = LD_DWORD(&fs->win[MBR_Table+8]); /* Partition offset in LBA */
699 3 fmt = check_fs(bootsect); /* Check the partition */
700 3 }
701 2 }
702 1 if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != 512U) /* No valid FAT patition is found */
703 1 return FR_NO_FILESYSTEM;
704 1
705 1 /* Initialize the file system object */
706 1 fatsize = LD_WORD(&fs->win[BPB_FATSz16]); /* Number of sectors per FAT */
707 1 if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]);
708 1 fs->sects_fat = (CLUST)fatsize;
709 1 fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
710 1 fatsize *= fs->n_fats; /* (Number of sectors in FAT area) */
711 1 fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]); /* FAT start sector (lba) */
712 1 fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
713 1 fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]); /* Nmuber of root directory entries */
714 1 totalsect = LD_WORD(&fs->win[BPB_TotSec16]); /* Number of sectors on the file system */
715 1 if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]);
716 1 fs->max_clust = maxclust = (totalsect /* max_clust = Last cluster# + 1 */
717 1 - LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / 16
718 1 ) / fs->csize + 2;
719 1
720 1 fmt = FS_FAT12; /* Determine the FAT sub type */
721 1 if (maxclust >= 0xFF7) fmt = FS_FAT16;
722 1 if (maxclust >= 0xFFF7)
723 1 #if !_FAT32
return FR_NO_FILESYSTEM;
#else
726 1 fmt = FS_FAT32;
727 1 if (fmt == FS_FAT32)
728 1 fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]); /* Root directory start cluster */
729 1 else
730 1 #endif
731 1 fs->dirbase = fs->fatbase + fatsize; /* Root directory start sector (lba) */
732 1 fs->database = fs->fatbase + fatsize + fs->n_rootdir / 16; /* Data start sector (lba) */
733 1
734 1 #if !_FS_READONLY
/* Initialize allocation information */
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 13
fs->free_clust = (CLUST)0xFFFFFFFF;
#if _USE_FSINFO
/* Get fsinfo 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
751 1
752 1 fs->fs_type = fmt; /* FAT syb-type */
753 1 fs->id = ++fsid; /* File system mount ID */
754 1 return FR_OK;
755 1 }
*** WARNING C280 IN LINE 653 OF ..\SRC\TFF.C: 'chk_wp': unreferenced local variable
756
757
758
759
760 /*-----------------------------------------------------------------------*/
761 /* Check if the file/dir object is valid or not */
762 /*-----------------------------------------------------------------------*/
763
764 static
765 FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
766 const FATFS *fs, /* Pointer to the file system object */
767 WORD id /* Member id of the target object to be checked */
768 )
769 {
770 1 if (!fs || !fs->fs_type || fs->id != id)
771 1 return FR_INVALID_OBJECT;
772 1 if (disk_status(0) & STA_NOINIT)
773 1 return FR_NOT_READY;
774 1
775 1 return FR_OK;
776 1 }
777
778
779
780
781 /*--------------------------------------------------------------------------
782
783 Public Functions
784
785 --------------------------------------------------------------------------*/
786
787
788 /*-----------------------------------------------------------------------*/
789 /* Mount/Unmount a Locical Drive */
790 /*-----------------------------------------------------------------------*/
791
792 FRESULT f_mount (
793 BYTE drv, /* Logical drive number to be mounted/unmounted */
794 FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
795 )
796 {
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 14
797 1 if (drv) return FR_INVALID_DRIVE;
798 1
799 1 if (FatFs) FatFs->fs_type = 0; /* Clear old object */
800 1
801 1 FatFs = fs; /* Register and clear new object */
802 1 if (fs) fs->fs_type = 0;
803 1
804 1 return FR_OK;
805 1 }
806
807
808
809
810 /*-----------------------------------------------------------------------*/
811 /* Open or Create a File */
812 /*-----------------------------------------------------------------------*/
813
814 FRESULT f_open (
815 FIL *fp, /* Pointer to the blank file object */
816 const char *path, /* Pointer to the file name */
817 BYTE mode /* Access mode and file open mode flags */
818 )
819 {
820 1 FRESULT res;
821 1 DIR dj;
822 1 BYTE *dir;
823 1 char fn[8+3+1];
824 1
825 1
826 1 fp->fs = NULL; /* Clear file object */
827 1 #if !_FS_READONLY
mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW);
res = auto_mount(&path, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)));
#else
831 1 mode &= FA_READ;
832 1 res = auto_mount(&path, 0);
833 1 #endif
834 1 if (res != FR_OK) return res;
835 1 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
836 1
837 1 #if !_FS_READONLY
/* Create or Open a File */
if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) {
CLUST rs;
DWORD dw;
if (res != FR_OK) { /* No file, create new */
if (res != FR_NO_FILE) return res;
res = reserve_direntry(&dj, &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 || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite (R/O or DIR) */
return FR_DENIED;
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero */
#if _FAT32
rs = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -