44 lines
1.1 KiB
C
44 lines
1.1 KiB
C
|
#include "fsctl/fugue/cluster.h"
|
||
|
#include "fsctl/fugue/sector.h"
|
||
|
#include "fsctl/fugue/utils.h"
|
||
|
|
||
|
//---
|
||
|
// Public
|
||
|
//---
|
||
|
|
||
|
/* fugue_cluster_is_valid() : check cluster validity */
|
||
|
//TODO : support FAT12 / FAT32
|
||
|
int fugue_cluster_is_valid(fugue_fs_t *fs, int cluster_idx)
|
||
|
{
|
||
|
void *sector;
|
||
|
uint16_t id;
|
||
|
|
||
|
id = FAT_WORD(((uint16_t *)fs->_private.fat0)[cluster_idx]);
|
||
|
if (id == 0x0000)
|
||
|
return 0;
|
||
|
if (cluster_idx == 0)
|
||
|
return 0;
|
||
|
|
||
|
/* special check for the root fake cluster */
|
||
|
if (cluster_idx == 1)
|
||
|
{
|
||
|
if (id != 0xffff)
|
||
|
return -1;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
cluster_idx -= 2;
|
||
|
sector = (void*)((uintptr_t)(fs->props.cluster_nb_sector * cluster_idx));
|
||
|
sector = (void*)((uintptr_t)sector + fs->props.sector_root_nb);
|
||
|
sector = (void*)((uintptr_t)sector * 512);
|
||
|
sector = (void*)((uintptr_t)sector + (uintptr_t)fs->_private.root);
|
||
|
|
||
|
for (int i = 0 ; i < fs->props.cluster_nb_sector ; i++)
|
||
|
{
|
||
|
if (fugue_sector_is_invalid(sector))
|
||
|
return -2;
|
||
|
sector = (void *)((uintptr_t)sector + 512);
|
||
|
}
|
||
|
return 0;
|
||
|
}
|