2 * Acess2 FAT12/16/32 Driver
3 * - By John Hodge (thePowersGang)
6 * - FAT Manipulation and Cluster IO
15 * \fn Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
16 * \brief Fetches a value from the FAT
18 Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
22 ENTER("pDisk xCluster", Disk, cluster);
23 Mutex_Acquire( &Disk->lFAT );
25 if( Disk->ClusterCount <= giFAT_MaxCachedClusters )
27 val = Disk->FATCache[cluster];
28 if(Disk->type == FAT12 && val == EOC_FAT12) val = -1;
29 if(Disk->type == FAT16 && val == EOC_FAT16) val = -1;
30 if(Disk->type == FAT32 && val == EOC_FAT32) val = -1;
35 ofs = Disk->bootsect.resvSectCount*512;
36 if(Disk->type == FAT12) {
37 VFS_ReadAt(Disk->fileHandle, ofs+(cluster/2)*3, 3, &val);
38 LOG("3 bytes at 0x%x are (Uint32)0x%x", ofs+(cluster/2)*3, val);
39 val = (cluster & 1) ? (val>>12) : (val & 0xFFF);
40 if(val == EOC_FAT12) val = -1;
41 } else if(Disk->type == FAT16) {
42 VFS_ReadAt(Disk->fileHandle, ofs+cluster*2, 2, &val);
43 if(val == EOC_FAT16) val = -1;
45 VFS_ReadAt(Disk->fileHandle, ofs+cluster*4, 4, &val);
46 if(val == EOC_FAT32) val = -1;
51 Mutex_Release( &Disk->lFAT );
58 * \brief Allocate a new cluster
60 Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous)
65 if( Disk->ClusterCount <= giFAT_MaxCachedClusters )
67 int bFoundCluster = 0;
72 case FAT12: eoc = EOC_FAT12; break;
73 case FAT16: eoc = EOC_FAT16; break;
74 case FAT32: eoc = EOC_FAT32; break;
78 Mutex_Acquire(&Disk->lFAT);
81 for(ret = Previous; ret < Disk->ClusterCount; ret++)
83 if(Disk->FATCache[ret] != 0) {
91 for(ret = 0; ret < Previous; ret++)
93 if(Disk->FATCache[ret] == 0) {
102 Disk->FATCache[ret] = eoc;
104 Disk->FATCache[Previous] = ret;
111 Mutex_Release(&Disk->lFAT);
112 LOG("Allocated cluster %x", ret);
119 Uint32 base = Disk->bootsect.resvSectCount*512;
120 int block = 0, block_ofs = 0;
122 const int block_size = 512*3;
123 const int ents_per_block_12 = block_size * 2 / 3; // 1.5 bytes per entry
124 // const int ents_per_block_16 = block_size / 2; // 2 bytes per entry
125 // const int ents_per_block_32 = block_size / 4; // 4 bytes per entry
126 int block_count_12 = DivUp(Disk->ClusterCount, ents_per_block_12);
127 Uint8 sector_data[block_size+1];
128 sector_data[block_size] = 0;
130 Mutex_Acquire(&Disk->lFAT);
135 block = Previous / ents_per_block_12;
140 // Search within the same block as the previous cluster first
142 VFS_ReadAt(Disk->fileHandle, base + block*block_size, block_size, sector_data);
143 for( block_ofs = 0; block_ofs < ents_per_block_12; block_ofs ++ )
145 Uint32 *valptr = (void*)( sector_data + block_ofs / 2 * 3 );
146 int bitofs = 12 * (block_ofs % 2);
147 // LOG("%i:%i - FAT Ent 0x%03x", block, block_ofs, (*valptr>>bitofs) & 0xFFF);
148 if( ((*valptr >> bitofs) & 0xFFF) == 0 ) {
149 // Found a free cluster
150 *valptr |= EOC_FAT12 << bitofs;
151 ret = block * ents_per_block_12 + block_ofs;
155 // Check for early break from the above loop
156 if( block_ofs != ents_per_block_12 )
161 if( block == block_count_12 )
163 } while( block != first_block );
165 if( ret != 0 ) // TODO: Could cluster 0 be valid?
167 // Write back changes to this part of the FAT
168 VFS_WriteAt(Disk->fileHandle, base + block, block_size, sector_data);
170 // Note the new cluster in the chain
173 LOG("Updating cluster %x to point to %x (offset %x)", Previous, ret,
174 base + (Previous>>1)*3);
175 VFS_ReadAt(Disk->fileHandle, base + (Previous>>1)*3, 3, &val);
184 VFS_WriteAt(Disk->fileHandle, base + (Previous>>1)*3, 3, &val);
189 Log_Warning("FAT", "TODO: Implement cluster allocation with FAT16");
190 // VFS_ReadAt(Disk->fileHandle, ofs+Previous*2, 2, &ret);
191 // VFS_WriteAt(Disk->fileHandle, ofs+ret*2, 2, &eoc);
194 Log_Warning("FAT", "TODO: Implement cluster allocation with FAT32");
195 // VFS_ReadAt(Disk->fileHandle, ofs+Previous*4, 4, &ret);
196 // VFS_WriteAt(Disk->fileHandle, ofs+ret*4, 4, &eoc);
199 Mutex_Release(&Disk->lFAT);
200 LOG("Allocated cluster %x", ret);
208 * \brief Free's a cluster
209 * \return The original contents of the cluster
211 Uint32 FAT_int_FreeCluster(tFAT_VolInfo *Disk, Uint32 Cluster)
215 if( Cluster < 2 || Cluster > Disk->ClusterCount ) // oops?
217 Log_Notice("FAT", "Cluster 0x%x is out of range (2 ... 0x%x)",
218 Cluster, Disk->ClusterCount-1);
222 Mutex_Acquire(&Disk->lFAT);
224 if( Disk->ClusterCount <= giFAT_MaxCachedClusters )
227 ret = Disk->FATCache[Cluster];
228 Disk->FATCache[Cluster] = 0;
234 Uint32 ofs = Disk->bootsect.resvSectCount*512;
238 VFS_ReadAt(Disk->fileHandle, ofs+(Cluster>>1)*3, 3, &val);
239 val = LittleEndian32(val);
241 ret = (val >> 12) & 0xFFF;
248 val = LittleEndian32(val);
249 VFS_WriteAt(Disk->fileHandle, ofs+(Cluster>>1)*3, 3, &val);
252 VFS_ReadAt(Disk->fileHandle, ofs+Cluster*2, 2, &ret);
253 ret = LittleEndian16(ret);
255 VFS_WriteAt(Disk->fileHandle, ofs+Cluster*2, 2, &val);
258 VFS_ReadAt(Disk->fileHandle, ofs+Cluster*4, 4, &ret);
259 ret = LittleEndian32(ret);
261 VFS_WriteAt(Disk->fileHandle, ofs+Cluster*2, 4, &val);
267 Mutex_Release(&Disk->lFAT);
268 LOG("ret = %07x, eoc = %07x", ret, EOC_FAT12);
270 Log_Notice("FAT", "Cluster 0x%x was already free", Cluster);
273 if(Disk->type == FAT12 && ret == EOC_FAT12) ret = -1;
274 if(Disk->type == FAT16 && ret == EOC_FAT16) ret = -1;
275 if(Disk->type == FAT32 && ret == EOC_FAT32) ret = -1;
276 LOG("ret = %07x", ret);
282 * ====================
284 * ====================
287 * \brief Read a cluster
288 * \param Disk Disk (Volume) to read from
289 * \param Length Length to read
290 * \param Buffer Destination for read data
292 void FAT_int_ReadCluster(tFAT_VolInfo *Disk, Uint32 Cluster, int Length, void *Buffer)
294 ENTER("pDisk xCluster iLength pBuffer", Disk, Cluster, Length, Buffer);
297 (Disk->firstDataSect + (Cluster-2)*Disk->bootsect.spc )
298 * Disk->bootsect.bps,
307 * \brief Write a cluster to disk
309 void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, const void *Buffer)
311 ENTER("pDisk xCluster pBuffer", Disk, Cluster, Buffer);
314 (Disk->firstDataSect + (Cluster-2)*Disk->bootsect.spc )
315 * Disk->bootsect.bps,
316 Disk->BytesPerCluster,