fsctl/src/fugue/core/sector.c

90 lines
2.4 KiB
C

#include <string.h>
#include "fsctl/fugue/sector.h"
#include "fsctl/fugue/utils.h"
//---
// Public
//---
/* fugue_sector_is_vbr() : check if the given address is a potential VBR */
int fugue_sector_is_vbr(struct fugue_fat_vbr *vbr)
{
static const struct fugue_fat_vbr fugue_fat_vbr = {
/* common boot information */
.BS_jmpBoot = { 0xeb, 0x3c, 0x90 },
.BS_OEMName = "MSDOS5.0",
/* BIOS Parameter Block */
.BPB_BytsPerSec = 512,
.BPB_SecPerClus = 8, // <--- not hardcoded by Casio
.BPB_RsvdSecCnt = 0, // <--- not hardcoded by Casio
.BPB_NumFATs = 2,
.BPB_RootEntCnt = 0, // <--- not hardcoded by Casio
.BPB_TotSec16 = 0, // <--- not hardcoded
.BPB_Media = 0xf8,
.BPB_FATSz16 = 0, // <--- not hardcoded
.BPB_SecPerTrk = 0, // <--- not hardcoded
.BPB_HiddSec = 0, // <--- not hardcoded
.BPB_TotSec32 = 0, // <--- not hardcoded
/* Extended BIOS Parameter Block */
.BS_DrvNum = 0x80,
.BS_Reserved1 = 0,
.BS_BootSig = 0x29,
.BS_VolID = {0x00, 0x00, 0x00, 0x00},
.BS_VolLab = "CASIO ",
.BS_FilSysType = "FAT16 ",
/* signature */
.Signature_word = {0x55, 0xaa}
};
//---
// VBR global hardcoded value check
//---
/* check common boot information */
if (memcmp(vbr, &fugue_fat_vbr, 11) != 0)
return -1;
/* check BIOS Parameter Block */
if (FAT_WORD(vbr->BPB_BytsPerSec) != fugue_fat_vbr.BPB_BytsPerSec)
return -1;
if (vbr->BPB_NumFATs != fugue_fat_vbr.BPB_NumFATs)
return -1;
if (vbr->BPB_Media != fugue_fat_vbr.BPB_Media)
return -1;
/* check Extended BIOS Parameter Block */
if (memcmp(&(vbr->BS_DrvNum), &(fugue_fat_vbr.BS_DrvNum), 474) != 0)
return -1;
/* check signature */
if (vbr->Signature_word[0] != fugue_fat_vbr.Signature_word[0])
return -1;
if (vbr->Signature_word[1] != fugue_fat_vbr.Signature_word[1])
return -1;
//---
// FAT validity check
//---
return 0;
}
/* fugue_sector_is_invalid() : check if the sector is invalide */
int fugue_sector_is_invalid(void *sector)
{
int x;
for (x = 0 ; x < 512 / 2 ; x++)
{
if (((uint32_t *)sector)[x] != 0xffffffff)
break;
}
if (x >= (512 / 4))
return -1;
return 0;
}