#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <libintl.h>
#include <sys/dktp/fdisk.h>
#include <sys/fs/pc_fs.h>
#include <sys/fs/pc_dir.h>
#include <sys/fs/pc_label.h>
#include "pcfs_common.h"
#include "fsck_pcfs.h"
extern int32_t BytesPerCluster;
extern int32_t TotalClusters;
extern int32_t LastCluster;
extern off64_t FirstClusterOffset;
extern off64_t PartitionOffset;
extern bpb_t TheBIOSParameterBlock;
extern int ReadOnly;
extern int IsFAT32;
extern int Verbose;
static uchar_t *TheFAT;
static int FATRewriteNeeded = 0;
int32_t FATSize;
short FATEntrySize;
static off64_t
seekFAT(int fd)
{
off64_t seekto;
seekto = TheBIOSParameterBlock.bpb.resv_sectors *
TheBIOSParameterBlock.bpb.bytes_per_sector + PartitionOffset;
return (lseek64(fd, seekto, SEEK_SET));
}
void
getFAT(int fd)
{
ssize_t bytesRead;
if (TheFAT != NULL) {
return;
} else if ((TheFAT = (uchar_t *)malloc(FATSize)) == NULL) {
mountSanityCheckFails();
perror(gettext("No memory for a copy of the FAT"));
(void) close(fd);
exit(7);
}
if (seekFAT(fd) < 0) {
mountSanityCheckFails();
perror(gettext("Cannot seek to FAT"));
(void) close(fd);
exit(7);
}
if (Verbose)
(void) fprintf(stderr,
gettext("Reading FAT\n"));
if ((bytesRead = read(fd, TheFAT, FATSize)) != FATSize) {
mountSanityCheckFails();
if (bytesRead < 0) {
perror(gettext("Cannot read a FAT"));
} else {
(void) fprintf(stderr,
gettext("Short read of FAT."));
}
(void) close(fd);
exit(7);
}
if (Verbose) {
(void) fprintf(stderr,
gettext("Dump of FAT's first 32 bytes.\n"));
header_for_dump();
dump_bytes(TheFAT, 32);
}
}
void
writeFATMods(int fd)
{
ssize_t bytesWritten;
if (TheFAT == NULL) {
(void) fprintf(stderr,
gettext("Internal error: No FAT to write\n"));
(void) close(fd);
exit(11);
}
if (!FATRewriteNeeded) {
if (Verbose) {
(void) fprintf(stderr,
gettext("No FAT changes need to be written.\n"));
}
return;
}
if (ReadOnly)
return;
if (Verbose)
(void) fprintf(stderr, gettext("Writing FAT\n"));
if (seekFAT(fd) < 0) {
perror(gettext("Cannot seek to FAT"));
(void) close(fd);
exit(11);
}
if ((bytesWritten = write(fd, TheFAT, FATSize)) != FATSize) {
if (bytesWritten < 0) {
perror(gettext("Cannot write FAT"));
} else {
(void) fprintf(stderr,
gettext("Short write of FAT."));
}
(void) close(fd);
exit(11);
}
FATRewriteNeeded = 0;
}
int
checkFAT32CleanBit(int fd)
{
getFAT(fd);
return (TheFAT[WIN_SHUTDOWN_STATUS_BYTE] & WIN_SHUTDOWN_BIT_MASK);
}
static uchar_t *
findClusterEntryInFAT(int32_t currentCluster)
{
int32_t idx;
if (FATEntrySize == 32) {
idx = currentCluster * 4;
} else if (FATEntrySize == 16) {
idx = currentCluster * 2;
} else {
idx = currentCluster + currentCluster/2;
}
return (TheFAT + idx);
}
int32_t
readFATEntry(int32_t currentCluster)
{
int32_t value;
uchar_t *ep;
ep = findClusterEntryInFAT(currentCluster);
if (FATEntrySize == 32) {
read_32_bits(ep, (uint32_t *)&value);
} else if (FATEntrySize == 16) {
read_16_bits(ep, (uint32_t *)&value);
if (value >= PCF_RESCLUSTER)
value |= 0xFFF0000;
} else {
value = 0;
if (currentCluster & 1) {
value = (((unsigned int)*ep++ & 0xf0) >> 4);
value += (*ep << 4);
} else {
value = *ep++;
value += ((*ep & 0x0f) << 8);
}
if (value >= PCF_12BCLUSTER)
value |= 0xFFFF000;
}
return (value);
}
void
writeFATEntry(int32_t currentCluster, int32_t value)
{
uchar_t *ep;
FATRewriteNeeded = 1;
ep = findClusterEntryInFAT(currentCluster);
if (FATEntrySize == 32) {
store_32_bits(&ep, value);
} else if (FATEntrySize == 16) {
store_16_bits(&ep, value);
} else {
if (currentCluster & 1) {
*ep = (*ep & 0x0f) | ((value << 4) & 0xf0);
ep++;
*ep = (value >> 4) & 0xff;
} else {
*ep++ = value & 0xff;
*ep = (*ep & 0xf0) | ((value >> 8) & 0x0f);
}
}
}
int
reservedInFAT(int32_t clusterNum)
{
int32_t e;
e = readFATEntry(clusterNum);
return (e >= PCF_RESCLUSTER32 && e < PCF_BADCLUSTER32);
}
int
badInFAT(int32_t clusterNum)
{
return (readFATEntry(clusterNum) == PCF_BADCLUSTER32);
}
int
freeInFAT(int32_t clusterNum)
{
return (readFATEntry(clusterNum) == PCF_FREECLUSTER);
}
int
lastInFAT(int32_t clusterNum)
{
return (readFATEntry(clusterNum) == PCF_LASTCLUSTER32);
}
void
markLastInFAT(int32_t clusterNum)
{
writeFATEntry(clusterNum, PCF_LASTCLUSTER32);
}
void
markFreeInFAT(int32_t clusterNum)
{
writeFATEntry(clusterNum, PCF_FREECLUSTER);
}
void
markBadInFAT(int32_t clusterNum)
{
writeFATEntry(clusterNum, PCF_BADCLUSTER32);
}