fatfs/src/ff.c
static FRESULT put_fat (	/* FR_OK(0):succeeded, !=0:error */
	FATFS* fs,		/* Corresponding filesystem object */
	DWORD clst,		/* FAT index number (cluster number) to be changed */
	DWORD val		/* New value to be set to the entry */
)
{
	UINT bc;
	BYTE *p;
	FRESULT res = FR_INT_ERR;


	if (clst >= 2 && clst < fs->n_fatent) {	/* Check if in valid range */
		switch (fs->fs_type) {
		case FS_FAT12 :
			bc = (UINT)clst; bc += bc / 2;	/* bc: byte offset of the entry */
			res = move_window(fs, fs->fatbase + (bc / SS(fs)));
			if (res != FR_OK) break;
			p = fs->win + bc++ % SS(fs);
			*p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;		/* Put 1st byte */
			fs->wflag = 1;
			res = move_window(fs, fs->fatbase + (bc / SS(fs)));
			if (res != FR_OK) break;
			p = fs->win + bc % SS(fs);
			*p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));	/* Put 2nd byte */
			fs->wflag = 1;
			break;

		case FS_FAT16 :
			res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
			if (res != FR_OK) break;
			st_word(fs->win + clst * 2 % SS(fs), (WORD)val);	/* Simple WORD array */
			fs->wflag = 1;
			break;

		case FS_FAT32 :
#if FF_FS_EXFAT
		case FS_EXFAT :
#endif
			res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
			if (res != FR_OK) break;
			if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {
				val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000);
			}
			st_dword(fs->win + clst * 4 % SS(fs), val);
			fs->wflag = 1;
			break;
		}
	}
	return res;
}
/*-----------------------------------------------------------------------*/
/* FAT access - Change value of a FAT entry                              */
/*-----------------------------------------------------------------------*/
FAT access - Change value of a FAT entry
FAT access - Change value of a FAT entry
static FRESULT put_fat (	
	FATFS* fs,		
	DWORD clst,		
	DWORD val		
)
{
	UINT bc;
	BYTE *p;
	FRESULT res = FR_INT_ERR;
	if (clst >= 2 && clst < fs->n_fatent) {	
		switch (fs->fs_type) {
		case FS_FAT12 :
			bc = (UINT)clst; bc += bc / 2;	
			res = move_window(fs, fs->fatbase + (bc / SS(fs)));
			if (res != FR_OK) break;
			p = fs->win + bc++ % SS(fs);
			*p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;		
			fs->wflag = 1;
			res = move_window(fs, fs->fatbase + (bc / SS(fs)));
			if (res != FR_OK) break;
			p = fs->win + bc % SS(fs);
			*p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));	
			fs->wflag = 1;
			break;
		case FS_FAT16 :
			res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
			if (res != FR_OK) break;
			st_word(fs->win + clst * 2 % SS(fs), (WORD)val);	
			fs->wflag = 1;
			break;
		case FS_FAT32 :
#if FF_FS_EXFAT
		case FS_EXFAT :
#endif
			res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
			if (res != FR_OK) break;
			if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {
				val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000);
			}
			st_dword(fs->win + clst * 4 % SS(fs), val);
			fs->wflag = 1;
			break;
		}
	}
	return res;
}
static FRESULT put_fat ( FATFS * fs , DWORD clst , DWORD val ) { UINT bc ; BYTE * p ; FRESULT res = FR_INT_ERR ; if ( clst >= 2 && clst < fs -> n_fatent ) { switch ( fs -> fs_type ) { case FS_FAT12 : bc = ( UINT ) clst ; bc += bc / 2 ; res = move_window ( fs , fs -> fatbase + ( bc / SS ( fs ) ) ) ; if ( res != FR_OK ) break ; p = fs -> win + bc ++ % SS ( fs ) ; * p = ( clst & 1 ) ? ( ( * p & 0x0F ) | ( ( BYTE ) val << 4 ) ) : ( BYTE ) val ; fs -> wflag = 1 ; res = move_window ( fs , fs -> fatbase + ( bc / SS ( fs ) ) ) ; if ( res != FR_OK ) break ; p = fs -> win + bc % SS ( fs ) ; * p = ( clst & 1 ) ? ( BYTE ) ( val >> 4 ) : ( ( * p & 0xF0 ) | ( ( BYTE ) ( val >> 8 ) & 0x0F ) ) ; fs -> wflag = 1 ; break ; case FS_FAT16 : res = move_window ( fs , fs -> fatbase + ( clst / ( SS ( fs ) / 2 ) ) ) ; if ( res != FR_OK ) break ; st_word ( fs -> win + clst * 2 % SS ( fs ) , ( WORD ) val ) ; fs -> wflag = 1 ; break ; case FS_FAT32 : #if FF_FS_EXFAT case FS_EXFAT : #endif res = move_window ( fs , fs -> fatbase + ( clst / ( SS ( fs ) / 4 ) ) ) ; if ( res != FR_OK ) break ; if ( ! FF_FS_EXFAT || fs -> fs_type != FS_EXFAT ) { val = ( val & 0x0FFFFFFF ) | ( ld_dword ( fs -> win + clst * 4 % SS ( fs ) ) & 0xF0000000 ) ; } st_dword ( fs -> win + clst * 4 % SS ( fs ) , val ) ; fs -> wflag = 1 ; break ; } } return res ; }
FAT access - Change value of a FAT entry
FAT access - Change value of a FAT entry
/* FR_OK(0):succeeded, !=0:error */ /* Corresponding filesystem object */ /* FAT index number (cluster number) to be changed */ /* New value to be set to the entry */ /* Check if in valid range */ /* bc: byte offset of the entry */ /* Put 1st byte */ /* Put 2nd byte */ /* Simple WORD array */