⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdfs_out.lst

📁 基于SD卡的FAT文件系统
💻 LST
📖 第 1 页 / 共 5 页
字号:
		return 0;
#if _FAT32
	if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
		return 0;
#endif
	return 1;
}




/*-----------------------------------------------------------------------*/
/* Make sure that the file system is valid                               */
/*-----------------------------------------------------------------------*/

static
FRESULT auto_mount (		/* FR_OK(0): successful, !=0: any error occured */
	const char **path,		/* Pointer to pointer to the path name (drive number) */
	BYTE chk_wp				/* !=0: Check media write protection for write access */
)
{
	BYTE fmt;
	DSTATUS stat;
	DWORD bootsect, fatsize, totalsect, maxclust;
	const char *p = *path;
	FATFS *fs = FatFs;



	while (*p == ' ') p++;	/* Strip leading spaces */
	if (*p == '/') p++;		/* Strip heading slash */
	*path = p;				/* Return pointer to the path name */

	/* Is the file system object registered? */
	if (!fs) return FR_NOT_ENABLED;

	/* Chekck if the logical drive has been mounted or not */
	if (fs->fs_type) {
		stat = disk_status(0);
		if (!(stat & STA_NOINIT)) {				/* If the physical drive is kept initialized */
#if !_FS_READONLY
			if (chk_wp && (stat & STA_PROTECT))	/* Check write protection if needed */
				return FR_WRITE_PROTECTED;
#endif
			return FR_OK;						/* The file system object is valid */
		}
	}

	/* The logical drive has not been mounted, following code attempts to mount the logical drive */

	memset(fs, 0, sizeof(FATFS));		/* Clean-up the file system object */
	stat = disk_initialize(0);			/* Initialize low level disk I/O layer */
	if (stat & STA_NOINIT)				/* Check if the drive is ready */
		return FR_NOT_READY;
#if !_FS_READONLY
	if (chk_wp && (stat & STA_PROTECT))	/* Check write protection if needed */
		return FR_WRITE_PROTECTED;
#endif

	/* Search FAT partition on the drive */
	fmt = check_fs(bootsect = 0);		/* Check sector 0 as an SFD format */
	if (fmt == 1) {						/* Not a FAT boot record, it may be patitioned */
		/* Check a partition listed in top of the partition table */
		if (fs->win[MBR_Table+4]) {					/* Is the 1st partition existing? */
			bootsect = LD_DWORD(&fs->win[MBR_Table+8]);	/* Partition offset in LBA */
			fmt = check_fs(bootsect);				/* Check the partition */
		}
	}
	if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != 512)	/* No valid FAT patition is found */
		return FR_NO_FILESYSTEM;

	/* Initialize the file system object */
	fatsize = LD_WORD(&fs->win[BPB_FATSz16]);			/* Number of sectors per FAT */
	if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]);
	fs->sects_fat = (CLUST)fatsize;
	fs->n_fats = fs->win[BPB_NumFATs];					/* Number of FAT copies */
	fatsize *= fs->n_fats;								/* (Number of sectors in FAT area) */
	fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]);	/* FAT start sector (lba) */
	fs->sects_clust = fs->win[BPB_SecPerClus];			/* Number of sectors per cluster */
	fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]);	/* Nmuber of root directory entries */
	totalsect = LD_WORD(&fs->win[BPB_TotSec16]);		/* Number of sectors on the file system */
	if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]);
	fs->max_clust = maxclust = (totalsect				/* Last cluster# + 1 */
		- LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / 16
		) / fs->sects_clust + 2;

	fmt = FS_FAT12;										/* Determine the FAT sub type */
	if (maxclust > 0xFF7) fmt = FS_FAT16;
	if (maxclust > 0xFFF7)
#if !_FAT32
		return FR_NO_FILESYSTEM;
#else
		fmt = FS_FAT32;
	if (fmt == FS_FAT32)
		fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]);	/* Root directory start cluster */
	else
#endif
		fs->dirbase = fs->fatbase + fatsize;			/* Root directory start sector (lba) */
	fs->database = fs->fatbase + fatsize + fs->n_rootdir / 16;	/* Data start sector (lba) */
	fs->fs_type = fmt;									/* FAT sub-type */

#if !_FS_READONLY
	fs->free_clust = (CLUST)0xFFFFFFFF;
#if _USE_FSINFO
	/* Load fsinfo sector 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
	fs->id = ++fsid;									/* File system mount ID */
	return FR_OK;
}




/*-----------------------------------------------------------------------*/
/* Check if the file/dir object is valid or not                          */
/*-----------------------------------------------------------------------*/

static
FRESULT validate (		/* FR_OK(0): The id is valid, !=0: Not valid */
	const FATFS *fs,	/* Pointer to the file system object */
	WORD id				/* id member of the target object to be checked */
)
{
     266:	fc 01       	movw	r30, r24
	if (!fs || fs->id != id)
     268:	89 2b       	or	r24, r25
     26a:	61 f0       	breq	.+24     	; 0x284 <validate+0x1e>
     26c:	80 81       	ld	r24, Z
     26e:	91 81       	ldd	r25, Z+1	; 0x01
     270:	86 17       	cp	r24, r22
     272:	97 07       	cpc	r25, r23
     274:	39 f4       	brne	.+14     	; 0x284 <validate+0x1e>
		return FR_INVALID_OBJECT;
	if (disk_status(0) & STA_NOINIT)
     276:	80 e0       	ldi	r24, 0x00	; 0
     278:	0e 94 87 07 	call	0xf0e	; 0xf0e <disk_status>
     27c:	99 27       	eor	r25, r25
     27e:	81 70       	andi	r24, 0x01	; 1
     280:	90 70       	andi	r25, 0x00	; 0
     282:	08 95       	ret
     284:	8c e0       	ldi	r24, 0x0C	; 12
     286:	90 e0       	ldi	r25, 0x00	; 0
		return FR_NOT_READY;

	return FR_OK;
}
     288:	08 95       	ret

0000028a <f_close>:




/*--------------------------------------------------------------------------

   Public Functions

--------------------------------------------------------------------------*/


/*-----------------------------------------------------------------------*/
/* Mount/Unmount a Locical Drive                                         */
/*-----------------------------------------------------------------------*/

FRESULT f_mount (
	BYTE drv,		/* Logical drive number to be mounted/unmounted */
	FATFS *fs		/* Pointer to new file system object (NULL for unmount)*/
)
{
	FATFS *fsobj;


	if (drv) return FR_INVALID_DRIVE;
	fsobj = FatFs;
	FatFs = fs;
	if (fsobj) memset(fsobj, 0, sizeof(FATFS));
	if (fs) memset(fs, 0, sizeof(FATFS));

	return FR_OK;
}




/*-----------------------------------------------------------------------*/
/* Open or Create a File                                                 */
/*-----------------------------------------------------------------------*/

FRESULT f_open (
	FIL *fp,			/* Pointer to the blank file object */
	const char *path,	/* Pointer to the file name */
	BYTE mode			/* Access mode and file open mode flags */
)
{
	FRESULT res;
	BYTE *dir;
	DIR dirobj;
	char fn[8+3+1];
	FATFS *fs = FatFs;


	fp->fs = NULL;
#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
	mode &= FA_READ;
	res = auto_mount(&path, 0);
#endif
	if (res != FR_OK) return res;

	/* Trace the file path */
	res = trace_path(&dirobj, fn, path, &dir);	/* Trace the file path */

#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(&dirobj, &dir);
			if (res != FR_OK) return res;
			memset(dir, 0, 32);		/* Initialize the new entry */
			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 == NULL || (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]);
				ST_WORD(&dir[DIR_FstClusHI], 0);
#else
				rs = LD_WORD(&dir[DIR_FstClusLO]);
#endif
				ST_WORD(&dir[DIR_FstClusLO], 0);	/* cluster = 0 */
				ST_DWORD(&dir[DIR_FileSize], 0);	/* size = 0 */
				fs->winflag = 1;
				dw = fs->winsect;				/* Remove the cluster chain */
				if (!remove_chain(rs) || !move_window(dw))
					return FR_RW_ERROR;
				fs->last_clust = rs - 1;		/* Reuse the cluster hole */
			}
		}
		if (mode & FA_CREATE_ALWAYS) {
			dir[DIR_Attr] = AM_ARC;		/* New attribute */
			dw = get_fattime();
			ST_DWORD(&dir[DIR_WrtTime], dw);	/* Updated time */
			ST_DWORD(&dir[DIR_CrtTime], dw);	/* Created time */
			fs->winflag = 1;
		}
	}
	/* Open a File */
	else {
#endif /* !_FS_READONLY */
		if (res != FR_OK) return res;		/* Trace failed */
		if (dir == NULL || (dir[DIR_Attr] & AM_DIR))	/* It is a directory */
			return FR_NO_FILE;
#if !_FS_READONLY
		if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
			return FR_DENIED;
	}

	fp->dir_sect = fs->winsect;			/* Pointer to the directory entry */
	fp->dir_ptr = dir;
#endif
	fp->flag = mode;							/* File access mode */
	fp->org_clust =								/* File start cluster */
#if _FAT32
		((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) |
#endif
		LD_WORD(&dir[DIR_FstClusLO]);
	fp->fsize = LD_DWORD(&dir[DIR_FileSize]);	/* File size */
	fp->fptr = 0;								/* File ptr */
	fp->sect_clust = 1;							/* Sector counter */
	fp->fs = fs; fp->id = fs->id;				/* Owner file system object of the file */

	return FR_OK;
}




/*-----------------------------------------------------------------------*/
/* Read File                                                             */
/*-----------------------------------------------------------------------*/

FRESULT f_read (
	FIL *fp, 		/* Pointer to the file object */
	void *buff,		/* Pointer to data buffer */
	UINT btr,		/* Number of bytes to read */
	UINT *br		/* Pointer to number of bytes read */
)
{
	DWORD sect, remain;
	UINT rcnt, cc;
	CLUST clust;
	BYTE *rbuff = buff;
	FRESULT res;
	FATFS *fs = fp->fs;


	*br = 0;
	res = validate(fs, fp->id);						/* Check validity of the object */
	if (res != FR_OK) return res;
	if (fp->flag & FA__ERROR) return FR_RW_ERROR;	/* Check error flag */
	if (!(fp->flag & FA_READ)) return FR_DENIED;	/* Check access mode */
	remain = fp->fsize - fp->fptr;
	if (btr > remain) btr = (WORD)remain;			/* Truncate read count by number of bytes left */

	for ( ;  btr;									/* Repeat until all data transferred */
		rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
		if ((fp->fptr % 512) == 0) {				/* On the sector boundary */
			if (--fp->sect_clust) {					/* Decrement left sector counter */
				sect = fp->curr_sect + 1;			/* Get current sector */
			} else {								/* On the cluster boundary, get next cluster */
				clust = (fp->fptr == 0) ?
					fp->org_clust : get_cluster(fp->curr_clust);
				if (clust < 2 || clust >= fs->max_clust)
					goto fr_error;
				fp->curr_clust = clust;				/* Current cluster */
				sect = clust2sect(clust);			/* Get current sector */
				fp->sect_clust = fs->sects_clust;	/* Re-initialize the left sector counter */
			}
			fp->curr_sect = sect;					/* Update current sector */
			cc = btr / 512;							/* When left bytes >= 512, */
			if (cc) {								/* Read maximum contiguous sectors directly */
				if (cc > fp->sect_clust) cc = fp->sect_clust;
				if (disk_read(0, rbuff, sect, (BYTE)cc) != RES_OK)
					goto fr_error;
				fp->sect_clust -= (BYTE)(cc - 1);
				fp->curr_sect += cc - 1;
				rcnt = cc * 512;
				continue;
			}
		}
		if (!move_window(fp->curr_sect)) goto fr_error;	/* Move sector window */
		rcnt = 512 - (WORD)(fp->fptr % 512);			/* Copy fractional bytes from sector window */
		if (rcnt > btr) rcnt = btr;
		memcpy(rbuff, &fs->win[(WORD)fp->fptr % 512], rcnt);
	}

	return FR_OK;

fr_error:	/* Abort this function due to an unrecoverable error */
	fp->flag |= FA__ERROR;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -