搜索
bottom↓
回复: 0

关于金士顿SD卡,FATFS f_mount失败问题求解决方法

[复制链接]

出0入0汤圆

发表于 2021-1-20 18:41:13 | 显示全部楼层 |阅读模式
本帖最后由 raymon 于 2021-1-20 18:43 编辑

static
FRESULT find_volume (        /* FR_OK(0): successful, !=0: any error occurred */
        FATFS** rfs,                /* Pointer to pointer to the found file system object */
        const TCHAR** path,        /* Pointer to pointer to the path name (drive number) */
        BYTE wmode                        /* !=0: Check write protection for write access */
)
{
        BYTE fmt;
        int vol;
        DSTATUS stat;
        DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
        WORD nrsv;
        FATFS *fs;


        /* Get logical drive number from the path name */
        *rfs = 0;
        vol = get_ldnumber(path);
        if (vol < 0) return FR_INVALID_DRIVE;

        /* Check if the file system object is valid or not */
        fs = FatFs[vol];                                        /* Get pointer to the file system object */
        if (!fs) return FR_NOT_ENABLED;                /* Is the file system object available? */

        ENTER_FF(fs);                                                /* Lock the volume */
        *rfs = fs;                                                        /* Return pointer to the file system object */

        if (fs->fs_type) {                                        /* If the volume has been mounted */
                stat = disk_status(fs->drv);
                if (!(stat & STA_NOINIT)) {                /* and the physical drive is kept initialized */
                        if (!_FS_READONLY && wmode && (stat & STA_PROTECT))        /* Check write protection if needed */
                                return FR_WRITE_PROTECTED;
                        return FR_OK;                                /* The file system object is valid */
                }
        }

        /* The file system object is not valid. */
        /* Following code attempts to mount the volume. (analyze BPB and initialize the fs object) */

        fs->fs_type = 0;                                        /* Clear the file system object */
        fs->drv = LD2PD(vol);                                /* Bind the logical drive and a physical drive */
        stat = disk_initialize(fs->drv);        /* Initialize the physical drive */
        if (stat & STA_NOINIT)                                /* Check if the initialization succeeded */
                return FR_NOT_READY;                        /* Failed to initialize due to no medium or hard error */
        if (!_FS_READONLY && wmode && (stat & STA_PROTECT))        /* Check disk write protection if needed */
                return FR_WRITE_PROTECTED;
#if _MAX_SS != _MIN_SS                                                /* Get sector size (multiple sector size cfg only) */
        if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
                || SS(fs) < _MIN_SS || SS(fs) > _MAX_SS) return FR_DISK_ERR;
#endif
        /* Find an FAT partition on the drive. Supports only generic partitioning, FDISK and SFD. */
        bsect = 0;
        fmt = check_fs(fs, bsect);                                        /* Load sector 0 and check if it is an FAT boot sector as SFD */
        if (fmt == 1 || (!fmt && (LD2PT(vol)))) {        /* Not an FAT boot sector or forced partition number */
                UINT i;
                DWORD br[4];

                for (i = 0; i < 4; i++) {                        /* Get partition offset */
                        BYTE *pt = fs->win+MBR_Table + i * SZ_PTE;
                        br = pt[4] ? LD_DWORD(&pt[8]) : 0;
                }
                i = LD2PT(vol);                                                /* Partition number: 0:auto, 1-4:forced */
                if (i) i--;
                do {                                                                /* Find an FAT volume */
                        bsect = br;
                        fmt = bsect ? check_fs(fs, bsect) : 2;        /* Check the partition */
                } while (!LD2PT(vol) && fmt && ++i < 4);
        }
        if (fmt == 3) return FR_DISK_ERR;                /* An error occured in the disk I/O layer */
        if (fmt) return FR_NO_FILESYSTEM;                /* No FAT volume is found */

        /* An FAT volume is found. Following code initializes the file system object */

        if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs))                /* (BPB_BytsPerSec must be equal to the physical sector size) */
                return FR_NO_FILESYSTEM;

        fasize = LD_WORD(fs->win+BPB_FATSz16);                                /* Number of sectors per FAT */
        if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
        fs->fsize = fasize;

        fs->n_fats = fs->win[BPB_NumFATs];                                        /* Number of FAT copies */
        if (fs->n_fats != 1 && fs->n_fats != 2)                                /* (Must be 1 or 2) */
                return FR_NO_FILESYSTEM;
        fasize *= fs->n_fats;                                                                /* Number of sectors for FAT area */

        fs->csize = fs->win[BPB_SecPerClus];                                /* Number of sectors per cluster */
        if (!fs->csize || (fs->csize & (fs->csize - 1)))        /* (Must be power of 2) */
                return FR_NO_FILESYSTEM;

        fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt);        /* Number of root directory entries */
        if (fs->n_rootdir % (SS(fs) / SZ_DIR))                                /* (Must be sector aligned) */
                return FR_NO_FILESYSTEM;

        tsect = LD_WORD(fs->win+BPB_TotSec16);                                /* Number of sectors on the volume */
        if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);

        nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt);                                /* Number of reserved sectors */
        if (!nrsv) return FR_NO_FILESYSTEM;                                        /* (Must not be 0) */

        /* Determine the FAT sub type */
        sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR);        /* RSV+FAT+DIR */
        if (tsect < sysect) return FR_NO_FILESYSTEM;                /* (Invalid volume size) */
        nclst = (tsect - sysect) / fs->csize;                                /* Number of clusters */
        if (!nclst) return FR_NO_FILESYSTEM;                                /* (Invalid volume size) */
        fmt = FS_FAT12;
        if (nclst >= MIN_FAT16) fmt = FS_FAT16;
        if (nclst >= MIN_FAT32) fmt = FS_FAT32;

        /* Boundaries and Limits */
        fs->n_fatent = nclst + 2;                                                        /* Number of FAT entries */
        fs->volbase = bsect;                                                                /* Volume start sector */
        fs->fatbase = bsect + nrsv;                                                 /* FAT start sector */
        fs->database = bsect + sysect;                                                /* Data start sector */
        if (fmt == FS_FAT32) {
                if (fs->n_rootdir) return FR_NO_FILESYSTEM;                /* (BPB_RootEntCnt must be 0) */
                fs->dirbase = LD_DWORD(fs->win+BPB_RootClus);        /* Root directory start cluster */
                szbfat = fs->n_fatent * 4;                                                /* (Needed FAT size) */
        } else {
                if (!fs->n_rootdir)        return FR_NO_FILESYSTEM;        /* (BPB_RootEntCnt must not be 0) */
                fs->dirbase = fs->fatbase + fasize;                                /* Root directory start sector */
                szbfat = (fmt == FS_FAT16) ?                                        /* (Needed FAT size) */
                        fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
        }
        if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs))        /* (BPB_FATSz must not be less than needed) */
                return FR_NO_FILESYSTEM;
  
//此处出现错误
#if !_FS_READONLY
        /* Initialize cluster allocation information */
        fs->last_clust = fs->free_clust = 0xFFFFFFFF;

        /* Get fsinfo if available */
        fs->fsi_flag = 0x80;
#if (_FS_NOFSINFO & 3) != 3
        if (fmt == FS_FAT32                                /* Enable FSINFO only if FAT32 and BPB_FSInfo is 1 */
                && LD_WORD(fs->win+BPB_FSInfo) == 1
                && move_window(fs, bsect + 1) == FR_OK)
        {
                fs->fsi_flag = 0;
                if (LD_WORD(fs->win+BS_55AA) == 0xAA55        /* Load FSINFO data if available */
                        && LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252
                        && LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272)
                {
#if (_FS_NOFSINFO & 1) == 0
                        fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
#endif
#if (_FS_NOFSINFO & 2) == 0
                        fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
#endif
                }
        }
#endif
#endif
        fs->fs_type = fmt;        /* FAT sub-type */
        fs->id = ++Fsid;        /* File system mount ID */
#if _FS_RPATH
        fs->cdir = 0;                /* Set current directory to root */
#endif
#if _FS_LOCK                        /* Clear file lock semaphores */
        clear_lock(fs);
#endif

        return FR_OK;
}

有没有大神遇见f->fsize 和 fs->n_fatent 两个不匹配的情况    ,程序红色部分。新卡存在这个问题,SD卡重新格式化下就好了。这句话屏蔽掉,系统也正常,就是不知道稳定性咋样。

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-16 03:00

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表