#include "fsctl/fugue/fat.h" #include "fsctl/fugue/sector.h" #include "fsctl/fugue/utils.h" //--- // Public //--- /* fugue_fat_is_vaild() : Check the complet FATs validity */ int fugue_fat_is_vaild(fugue_fs_t *fs) { uint16_t *cluster_map; uint32_t *sector; uint16_t cluster; /* check FATID and root value */ sector = (void *)fs->_private.fat0; if (((uint8_t*)sector)[0] != 0xf8 || ((uint8_t*)sector)[1] != 0xff) return -1; if (((uint8_t*)sector)[2] != 0xff || ((uint8_t*)sector)[3] != 0xff) return -1; sector = (void *)fs->_private.fat1; if (((uint8_t*)sector)[0] != 0xf8 || ((uint8_t*)sector)[1] != 0xff) return -1; if (((uint8_t*)sector)[2] != 0xff || ((uint8_t*)sector)[3] != 0xff) return -1; /* check complet valid FAT * @note * - full Fugue FAT area take 5120 bytes -> 10x512 -> 10 sectors * - also check the root reserved sectors */ sector = (void*)fs->_private.fat0; for (int i = 0 ; i < 10 + fs->props.sector_root_nb ; i++) { if (fugue_sector_is_invalid(sector)) return -1; sector = (void *)((uintptr_t)sector + 512); } /* follows each cluster and check that all sector are used */ cluster_map = (void *)fs->_private.fat1; fs->props.cluster_resv = 0; fs->props.cluster_free = 0; fs->props.cluster_used = 0; fs->props.cluster_dead = 0; fs->props.cluster_error = 0; for (int i = 1 ; i < 256 ; i++) { cluster = FAT_WORD(cluster_map[i]); if (cluster == 0x0000) { fs->props.cluster_free += 1; continue; } if (cluster == 0x0001) { fs->props.cluster_resv += 1; continue; } if (cluster == 0xfff7) { fs->props.cluster_dead += 1; continue; } if (cluster >= 0xfff8) { fs->props.cluster_used += 1; continue; } if (cluster < (fs->props.cluster_nb + 1)) { fs->props.cluster_used += 1; continue; } fs->props.cluster_error += 1; fs->props.test = cluster; } return 0; }