104 lines
2.6 KiB
C
104 lines
2.6 KiB
C
|
#include <string.h>
|
||
|
|
||
|
#include "fsctl/fugue.h"
|
||
|
#include "fsctl/fugue/sector.h"
|
||
|
#include "fsctl/fugue/utils.h"
|
||
|
#include "fsctl/fugue/fat.h"
|
||
|
|
||
|
//---
|
||
|
// Public
|
||
|
//---
|
||
|
|
||
|
/* fugue_fs_mount() : try to initialize the FS */
|
||
|
int fugue_fs_mount(fugue_fs_t *fsinfo, void *addr)
|
||
|
{
|
||
|
fugue_fs_t fs;
|
||
|
struct fugue_fat_vbr *vbr;
|
||
|
uintptr_t FAT0_addr;
|
||
|
uintptr_t FAT1_addr;
|
||
|
int CountofClusters;
|
||
|
int RootDirSectors;
|
||
|
int TotCapacity;
|
||
|
int DataSec;
|
||
|
void *type;
|
||
|
int FATz;
|
||
|
|
||
|
if (fugue_sector_is_vbr(addr) != 0)
|
||
|
return -1;
|
||
|
vbr = addr;
|
||
|
|
||
|
//---
|
||
|
// determine the number of cluster
|
||
|
// @note
|
||
|
// - hardcoded for FAT12 and FAT16
|
||
|
//---
|
||
|
|
||
|
RootDirSectors = FAT_WORD(vbr->BPB_RootEntCnt) * 32;
|
||
|
RootDirSectors += FAT_WORD(vbr->BPB_BytsPerSec) - 1;
|
||
|
RootDirSectors /= FAT_WORD(vbr->BPB_BytsPerSec);
|
||
|
|
||
|
DataSec = FAT_WORD(vbr->BPB_RsvdSecCnt);
|
||
|
DataSec += FAT_WORD(vbr->BPB_FATSz16) * vbr->BPB_NumFATs;
|
||
|
DataSec += RootDirSectors;
|
||
|
DataSec = FAT_WORD(vbr->BPB_TotSec16) - DataSec;
|
||
|
|
||
|
CountofClusters = DataSec;
|
||
|
CountofClusters /= vbr->BPB_SecPerClus;
|
||
|
|
||
|
TotCapacity = DataSec;
|
||
|
TotCapacity *= FAT_WORD(vbr->BPB_BytsPerSec);
|
||
|
|
||
|
if (CountofClusters < 4085) {
|
||
|
type = "FAT12";
|
||
|
} else if (CountofClusters < 65525) {
|
||
|
type = "FAT16";
|
||
|
} else {
|
||
|
type = "FAT32";
|
||
|
}
|
||
|
|
||
|
//---
|
||
|
// Analysing FAT intergrity
|
||
|
// @note
|
||
|
// - hardcoded for two Fugue
|
||
|
// - Fugue seems use a static offset of its FAT : 5120 (10 sectors)
|
||
|
// - Fugue doesn't have same sized FAT (FAT0=4608 && FAT1=512)
|
||
|
//---
|
||
|
|
||
|
//FATz = FAT_WORD(vbr->BPB_FATSz16);
|
||
|
//FATz *= FAT_WORD(vbr->BPB_BytsPerSec);
|
||
|
//FATz *= vbr->BPB_NumFATs;
|
||
|
FATz = 5120;
|
||
|
|
||
|
FAT0_addr = FAT_WORD(vbr->BPB_RsvdSecCnt);
|
||
|
FAT0_addr *= FAT_WORD(vbr->BPB_BytsPerSec);
|
||
|
FAT0_addr += (uintptr_t)vbr;
|
||
|
|
||
|
FAT1_addr = 4608;
|
||
|
FAT1_addr += FAT0_addr;
|
||
|
|
||
|
//---
|
||
|
// Save calculated information
|
||
|
//---
|
||
|
|
||
|
memset(&fs, 0x00, sizeof(fugue_fs_t));
|
||
|
fs._private.vbr = addr;
|
||
|
fs.props.type = type;
|
||
|
fs.props.sector_data_nb = DataSec;
|
||
|
fs.props.sector_root_nb = RootDirSectors;
|
||
|
fs.props.cluster_nb = CountofClusters;
|
||
|
fs.props.cluster_size = vbr->BPB_SecPerClus * 512;
|
||
|
fs.props.cluster_nb_sector = vbr->BPB_SecPerClus;
|
||
|
fs.props.capacity = TotCapacity;
|
||
|
fs.props.fats_size = FATz;
|
||
|
fs.props.fat0_size = 4608;
|
||
|
fs.props.fat1_size = 512;
|
||
|
fs._private.fat0 = (void*)FAT0_addr;
|
||
|
fs._private.fat1 = (void*)FAT1_addr;
|
||
|
fs._private.root = (void*)(FAT0_addr + FATz);
|
||
|
|
||
|
if (fugue_fat_is_vaild(&fs) != 0)
|
||
|
return -1;
|
||
|
memcpy(fsinfo, &fs, sizeof(fugue_fs_t));
|
||
|
return 0;
|
||
|
}
|