int fatfs_fat_set_cluster(struct fatfs *fs, UINT32 cluster, UINT32 next_cluster) { struct sector_buffer *pbuf; UINT32 fat_sector_offset, position; // Find which sector of FAT table to read if (fs->fat_type == FAT_TYPE_16) fat_sector_offset = cluster / 256; else fat_sector_offset = cluster / 128; // Read FAT sector into buffer pbuf = fatfs_fat_read_sector(fs, fs->fat_begin_lba+fat_sector_offset); if (!pbuf) return 0; if (fs->fat_type == FAT_TYPE_16) { // Find 16 bit entry of current sector relating to cluster number position = (cluster - (fat_sector_offset * 256)) * 2; // Write Next Clusters value to Sector Buffer FAT16_SET_16BIT_WORD(pbuf, (UINT16)position, ((UINT16)next_cluster)); } else { // Find 32 bit entry of current sector relating to cluster number position = (cluster - (fat_sector_offset * 128)) * 4; // Write Next Clusters value to Sector Buffer FAT32_SET_32BIT_WORD(pbuf, (UINT16)position, next_cluster); } return 1; }
//----------------------------------------------------------------------------- // fatfs_set_fs_info_next_free_cluster: Write the next free cluster to the FSINFO table //----------------------------------------------------------------------------- void fatfs_set_fs_info_next_free_cluster(struct fatfs *fs, UINT32 newValue) { if (fs->fat_type == FAT_TYPE_16) ; else { // Load sector to change it struct sector_buffer *pbuf = fatfs_fat_read_sector(fs, fs->lba_begin+fs->fs_info_sector); if (!pbuf) return ; // Change FAT32_SET_32BIT_WORD(pbuf, 492, newValue); fs->next_free_cluster = newValue; } }
//----------------------------------------------------------------------------- // fatfs_set_fs_info_next_free_cluster: Write the next free cluster to the FSINFO table //----------------------------------------------------------------------------- void fatfs_set_fs_info_next_free_cluster(struct fatfs *fs, uint32 newValue) { if (fs->fat_type == FAT_TYPE_16) ; else { // Load sector to change it struct fat_buffer *pbuf = fatfs_fat_read_sector(fs, fs->lba_begin+fs->fs_info_sector); if (!pbuf) return ; // Change FAT32_SET_32BIT_WORD(pbuf, 492, newValue); fs->next_free_cluster = newValue; // Write back FSINFO sector to disk if (fs->disk_io.write_media) fs->disk_io.write_media(pbuf->address, pbuf->sector, 1); // Invalidate cache entry pbuf->address = FAT32_INVALID_CLUSTER; pbuf->dirty = 0; } }