📄 dosformt.c
字号:
#else
FL_SET_LE2(bpb, FL_BPB_NO_OF_HEADS_OFFSET, pVol->heads);
FL_SET_LE2(bpb, FL_BPB_SECTORS_PER_TRACK_OFFSET, pVol->sectorsPerTrack);
FL_SET_LE4(bpb, FL_BPB_NO_OF_HIDDEN_SECTORS_OFFSET, bootSectorNo);
FL_SET_UNAL2(bpb, FL_BPB_BYTES_PER_SECTOR_OFFSET, FL_SECTOR_SIZE);
FL_REF_1(bpb, FL_BPB_NO_OF_FATS_OFFSET) = formatParams->noOfFATs;
FL_REF_1(bpb, FL_BPB_MEDIA_DESCRIPTOR_OFFSET) = 0xf8; /* hard disk */
#endif
/* use flash-specific geometry parameters */
if (pVol->normalArea_SectorsInUnit_Bits && pVol->recommendedSectorsInCluster)
{
if (pVol->fastAreaSectors > bootSectorNo)
sectorAlignment = 1L << (pVol->fastArea_SectorsInUnit_Bits + FL_SECTOR_SIZE_BITS);
else
sectorAlignment = 1L << (pVol->normalArea_SectorsInUnit_Bits + FL_SECTOR_SIZE_BITS);
}
else
{
#ifdef FL_BOOT_SECTOR_ALIGNMENT
sectorAlignment = FL_BOOT_SECTOR_ALIGNMENT;
#endif
}
if (!clusterSize)
{
DiskParamsTable* table;
if (fatType == FAT12_PARTIT)
{
table = FAT12Params;
}
if ((fatType == FAT16_PARTIT) || (fatType == DOS4_PARTIT))
{
table = FAT16Params;
}
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
{
table = FAT32Params;
}
#endif /*FL_SUPPORT_FAT32_FORMAT*/
for (i = 0 ; 1 ; i++)
{
if (totalSectorsInPartition < table[i].size)
{
clusterSize = table[i].clusterSize;
break;
}
}
if (clusterSize == 0)
{
DBG_PRINT_ERR(FLZONE_FORMAT,"ERROR - Illegal FAT type for partition size.\r\n");
return flBadParameter;
}
}
#ifdef MIN_CLUSTER_SIZE
if (clusterSize < MIN_CLUSTER_SIZE)
clusterSize = MIN_CLUSTER_SIZE;
#endif
/* ---- total sectors ---------- */
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
toLE4(bpb->totalSectorsInVolume, totalSectorsInPartition);
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
toUNAL2(bpb->totalSectorsInVolumeDOS3, 0);
else
#endif /*FL_SUPPORT_FAT32_FORMAT*/
toUNAL2(bpb->totalSectorsInVolumeDOS3,
(FLWord)(totalSectorsInPartition > 65535l ? 0 : totalSectorsInPartition));
#else /*FL_NO_PACKED_STRUCTS_SUPPORTED*/
FL_SET_LE4(bpb, FL_BPB_TOTAL_SECTORS_IN_VOL_OFFSET, totalSectorsInPartition);
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
FL_SET_UNAL2(bpb, FL_BPB_TOTAL_SECTORS_IN_VOL_DOS3_OFFSET, 0);
else
#endif /*FL_SUPPORT_FAT32_FORMAT*/
FL_SET_UNAL2(bpb, FL_BPB_TOTAL_SECTORS_IN_VOL_DOS3_OFFSET,
(FLWord)(totalSectorsInPartition > 65535l ? 0 : totalSectorsInPartition));
#endif /*FL_NO_PACKED_STRUCTS_SUPPORTED*/
if ( totalSectorsInPartition < ( sectorAlignment>>FL_SECTOR_SIZE_BITS ) )
{
DBG_PRINT_ERR(FLZONE_FORMAT,"ERROR - logical parition too small\r\n");
return flBadPartitionSize; /* partition is too small */
}
/*---- now root dir entries -----*/
if (formatParams->noOfRootEntries == 0)
{ /* default calculation */
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
rootDirectorySectors = 0;
else
#endif /*FL_SUPPORT_FAT32_FORMAT*/
rootDirectorySectors = FL_ROOT_DIR_ENTRIES / DIRECTORY_ENTRIES_PER_SECTOR;
}
else
{ /* user-provided number of root entries */
rootDirectorySectors = (FLWord)((formatParams->noOfRootEntries - 1) / DIRECTORY_ENTRIES_PER_SECTOR + 1);
/* if value written is not a multiple of 16, FATLITE will use whole number of sectors anyway */
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
rootDirectorySectors = 0;
else
#endif /*FL_SUPPORT_FAT32_FORMAT*/
if (rootDirectorySectors > MAX_ROOT_DIR_SECTORS)
rootDirectorySectors = MAX_ROOT_DIR_SECTORS;
}
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
toUNAL2(bpb->rootDirectoryEntries, (FLWord)(rootDirectorySectors * DIRECTORY_ENTRIES_PER_SECTOR));
toLE2(bpb->reservedSectors,1);
#else
FL_SET_UNAL2(bpb, FL_BPB_ROOT_DIR_ENTRIES_OFFSET,
(FLWord)(rootDirectorySectors * DIRECTORY_ENTRIES_PER_SECTOR));
FL_SET_LE2(bpb, FL_BPB_RESERVED_SECTORS_OFFSET, 1);
#endif
/* ---- now-iterative calculation of number of clusters ----- */
minNonDataSectors = 1 + formatParams->noOfFATs + rootDirectorySectors; /* reserved, FAT, root */
noOfClusters = ( totalSectorsInPartition - minNonDataSectors ) / clusterSize;
/* not a final number; this one is needed for select FAT12 or 16 and get FAT size.
Setting FAT size too large is not a problem: it will be recorded in BPB, not re-calculated */
/* --- now estimate the MINIMUM number of FAT sectors that we will need --- */
if ((fatType == FAT16_PARTIT) || (fatType == DOS4_PARTIT))
{
sectorsPerFAT = (FLWord) (((noOfClusters + 2L) * 2 - 1) / FL_SECTOR_SIZE);
if (sectorsPerFAT > 127)
sectorsPerFAT = 127; /* size for 32K clusters */
}
#ifdef FL_SUPPORT_FAT32_FORMAT
else if (fatType == FAT32_PARTIT)
{
sectorsPerFAT = (FLWord) (((noOfClusters + 2L) * 4 - 1) / FL_SECTOR_SIZE);
}
#endif /*FL_SUPPORT_FAT32_FORMAT*/
else /*FAT12*/
{
sectorsPerFAT = (FLWord) ((((noOfClusters + 2L) * 3 + 1) / 2 - 1) / FL_SECTOR_SIZE);
if (sectorsPerFAT == 0)
sectorsPerFAT = 1;
}
/* this calculation is corresponding to size -1; because noOfClusters below will be smaller
=> new number of sectorsPerFAT could otherwise be smaller than above.
The sectorsPerFAT above is MINIMUM possible => noOfClusters above is maximum possible
=> they will fit into number of FAT sectors calculated below */
/* ---- now- more precise calculation, again the upper limit ----- */
minNonDataSectors = 1 + sectorsPerFAT*formatParams->noOfFATs + rootDirectorySectors; /* reserved, FAT, root */
noOfClusters = ( totalSectorsInPartition - minNonDataSectors ) / clusterSize;
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
bpb->sectorsPerCluster = (FLByte)clusterSize;
#else
FL_REF_1(bpb, FL_BPB_SECTORS_PER_CLUSTER_OFFSET) = (FLByte)clusterSize;
#endif
/* we will not fail format on FAT12; format can always be performed
mount will fail if FAT12 support is not enabled */
if (formatParams->pType == FAT12_PARTIT)
sectorsPerFAT = (FLWord) ((((noOfClusters + 2L) * 3 + 1) / 2 - 1) / FL_SECTOR_SIZE + 1);
else if ((formatParams->pType == FAT16_PARTIT) || (fatType == DOS4_PARTIT))
sectorsPerFAT = (FLWord) (((noOfClusters + 2L) * 2 - 1) / FL_SECTOR_SIZE + 1);
else
sectorsPerFAT = 0;
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
toLE2(bpb->sectorsPerFAT, sectorsPerFAT);
#else
FL_SET_LE2(bpb, FL_BPB_SECTORS_PER_FAT_OFFSET, sectorsPerFAT);
#endif
/* ---- now alignment --------- */
firstDataSector = bootSectorNo + 1 + (FLWord)formatParams->noOfFATs * sectorsPerFAT + rootDirectorySectors;
/* 1 for current value of reservedSectors */
if (sectorAlignment) /* either from recommendedClusterInfo or FL_BOOT_SECTOR_ALIGNMENT */
{
FLDword sectorsInBlock;
if (pVol->normalArea_SectorsInUnit_Bits)
{ /* again; we need 1st data cluster, not bootSector */
if (pVol->fastAreaSectors >= firstDataSector)
sectorsInBlock = 1L << (pVol->fastArea_SectorsInUnit_Bits);
else
sectorsInBlock = 1L << (pVol->normalArea_SectorsInUnit_Bits);
}
else
sectorsInBlock = sectorAlignment >> FL_SECTOR_SIZE_BITS;
if( ( firstDataSector % sectorsInBlock ) != 0 )
{
/* aligned first DataSector to unit size */
FLDword reserved = 1 + sectorsInBlock - ( firstDataSector % sectorsInBlock );
/* 1 for current value of reservedSectors */
/* Should never happen, but please check if reserved sectors > 16 bits */
if ((FLWord)reserved == 0)
reserved = 1;
/* in case eraseble block is 32MB or larger */
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
toLE2(bpb->reservedSectors, (FLWord)reserved);
#else
FL_SET_LE2(bpb, FL_BPB_RESERVED_SECTORS_OFFSET, (FLWord)reserved);
#endif
firstDataSector += reserved - 1;
}
}
/* check that we have at least 1 cluster in the logical partition! */
if (((firstDataSector - bootSectorNo) + clusterSize) > totalSectorsInPartition )
{
DBG_PRINT_ERR(FLZONE_FORMAT,"ERROR - logical parition too small\r\n");
return flBadPartitionSize; /* partition is too small */
}
return flOK;
}
/*----------------------------------------------------------------------*/
/* c r e a t e D O S B o o t S e c t o r */
/* */
/* Creates the DOS boot sector */
/* */
/* Parameters: */
/* */
/* pVol : Pointer identifying drive */
/* bpb : volume BIOS parameter block */
/* volumeId : 32-bit volume id */
/* volumeLabel : volume label */
/* fatType :Specify the fat type of the partition. */
/* */
/* Returns: */
/* FLStatus : 0 on success, failed otherwise */
/*----------------------------------------------------------------------*/
static FLStatus createDOSbootSector(TL * pVol,
BPB *bpb,
const FLByte FAR1 *volumeId,
const FLByte FAR1 *volumeLabel,
FLByte fatType, FLByte flags)
{
FLByte bootSectBuf[FL_SECTOR_SIZE];
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
{
FAT32BootSector *bootSector = (FAT32BootSector *)bootSectBuf;
FAT32FSInfo *infoSector = (FAT32FSInfo *)bootSectBuf;
FLDword noOfClusters;
tffsset(bootSector,0,sizeof bootSectBuf);
bootSector->bpb = *bpb;
noOfClusters = bootSector->bpb.totalSectorsInVolume / bootSector->bpb.sectorsPerCluster;
toLE4(bootSector->FAT32Size, (((noOfClusters + 2L) * 4 - 1) / FL_SECTOR_SIZE + 1));
toLE2(bootSector->extFlags, 0);
toLE2(bootSector->FSVer, 0);
bootSector->RootClus = 2;
toLE2(bootSector->FSInfo, 1);
bootSector->bckUpBootSect = BACKUP_OF_BOOT_SECTOR;
bootSector->physicalDriveNo = DRIVE_NUMBER;
bootSector->extendedBootSignature = EXTENDED_BOOT_SIGNATURE;
if ((volumeId[0] == 0) && (volumeId[1] == 0) &&
(volumeId[2] == 0) && (volumeId[3] == 0))
{
FLWord curDate = (FLWord)flCurrentDate();
FLWord curTime = (FLWord)flCurrentTime();
bootSector->volumeId[0] = (FLSByte)curDate;
bootSector->volumeId[1] = (FLSByte)(curDate >> 8);
bootSector->volumeId[2] = (FLSByte)curTime;
bootSector->volumeId[3] = (FLSByte)(curTime >> 8);
}
if (volumeLabel)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -