📄 dosformt.c
字号:
if (partitionParams->lengthType == FL_LENGTH_IN_BYTES)
partitionSize = (partitionSize+FL_SECTOR_MASK) >> FL_SECTOR_SIZE_BITS;
if (partitionParams->lengthType == FL_LENGTH_IN_PERCENTS)
partitionSize = (totalSectorsInVolume * partitionSize)/100;
/* good for volume no more than 16Gbytes */
if (partitionParams->lengthType == FL_LENGTH_IN_CYLINDERS)
partitionSize *= fullCylinderSize;
if ((currentMEBR != 0) && (currentMBRslot == 1)) /* log drive will have EBR overhead */
partitionSize += shiftToActualPartition;
#ifdef FL_FDISK_COMPATIBILITY_MODE
/* in this mode, partition can start only on TRACK boundary, and end on CYLINDER boundary */
if ((freeSpaceStartSector + partitionSize) % fullCylinderSize)
{
unsigned bumpValue = fullCylinderSize - ((freeSpaceStartSector + partitionSize) % fullCylinderSize);
partitionSize += bumpValue;
}
/* now freeSpaceStartSector starts on track boundary */
#endif /* FL_FDISK_COMPATIBILITY_MODE */
/* last partition - length field is ignored, all remaining space taken */
if ((partitionsNumber == 0) || (partitionParams->pType == EX_PARTIT))
{
if (partitionParams->length && (partitionSize != freeSpaceSectorsNumber))
{
DBG_PRINT_WRN(FLZONE_FORMAT,"Warning - given partition size !0 && != free space\r\n");
}
partitionSize = freeSpaceSectorsNumber;
/* in COMPATIBILITY - obviously there is no bump in size for the last partition */
/**** ORIS - BUT WE FORCE IT ANYWAY ****/
}
else
{ /* not the last partition -> check that it fits in the free space */
if (partitionSize > freeSpaceSectorsNumber) {
DBG_PRINT_ERR(FLZONE_FORMAT,"ERROR - logical parition too large\r\n");
return flBadPartitionSize;
}
if (partitionParams->length == 0)
{
DBG_PRINT_ERR(FLZONE_FORMAT,"ERROR - logical parition too small\r\n");
return flBadPartitionSize;
}
} /* not the last partition */
partitionStart = freeSpaceStartSector;
freeSpaceSectorsNumber -= partitionSize;
freeSpaceStartSector += partitionSize;
/* fill the entry */
if (currentMBRslot >= PARTITION_ENTRIES)
{
DBG_PRINT_ERR(FLZONE_FORMAT,"ERROR - more than 4 logical paritions in MBR\r\n");
return flTooManyPartitions;
}
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
ptEntry = &partitionTable.ptEntry[currentMBRslot];
#else
ptEntry = (Partition *) &FL_REF_1(partitionTable, FL_PARTITION_TABLE_PT_ENTRY_OFFSET + currentMBRslot * sizeof(Partition));
#endif
if (partitionParams->flags & FL_MARK_PARTITION_BOOTABLE)
{
if ((bootableMarked) || (currentMEBR != 0) || (partitionParams->pType == EX_PARTIT))
return flBadParameter;
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
ptEntry->activeFlag = 0x80;
#else
FL_REF_1(ptEntry, FL_PARTITION_ACTIVE_FLAG_OFFSET) = 0x80;
#endif
bootableMarked = TRUE;
}
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
ptEntry->type = ((currentMEBR != 0) && (currentMBRslot == 1)) ? EX_PARTIT : partitionParams->pType;
/* EBRs are chained */
toUNAL4(ptEntry->startingSectorOfPartition, partitionStart - mainEBR);
toUNAL4(ptEntry->sectorsInPartition, partitionSize);
#else
FL_REF_1(ptEntry, FL_PARTITION_TYPE_OFFSET) = ((currentMEBR != 0) && (currentMBRslot == 1)) ? EX_PARTIT : partitionParams->pType;
/* EBRs are chained */
FL_SET_UNAL4(ptEntry, FL_PARTITION_STARTING_SECTOR_OF_PARTITION_OFFSET, partitionStart - mainEBR);
FL_SET_UNAL4(ptEntry, FL_PARTITION_SECTORS_IN_PARTITION_OFFSET, partitionSize);
/* CHS are always correct, but starting sectors are relative for secondary partitions */
#endif
/* now write CHS parameters. They are already known in compatibility mode,
but not known in normal mode, so we will calculate them identically for simplicity */
recordCHS(ptEntry, fullCylinderSize, pVol->sectorsPerTrack, partitionStart, TRUE);
recordCHS(ptEntry, fullCylinderSize, pVol->sectorsPerTrack, (partitionStart + partitionSize - 1), FALSE);
currentMBRslot++;
/* special care for extended and secondary partitions */
if (currentMEBR != 0)
{ /* this partition was created inside Extended */
if (partitionParams->pType == EX_PARTIT) /* user should create only outer-most Extended */
return flBadParameter;
if (currentMBRslot == 2)
{ /* second entry in EBR was written => must create next EBR! */
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
toLE2(partitionTable.signature, PARTITION_SIGNATURE);
#else
FL_SET_LE2(partitionTable, FL_PARTITION_TABLE_SIGNATURE_OFFSET, PARTITION_SIGNATURE);
#endif
checkStatus(pVol->writeSector(pVol->rec, currentMEBR, &partitionTable));
currentMEBR = partitionStart;
currentMBRslot = 1;
tffsset(&partitionTable, 0, sizeof partitionTable);
tffscpy(&partitionTable, bootCode, sizeof bootCode);
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
ptEntry = &partitionTable.ptEntry[0];
#else
ptEntry = (Partition *) &FL_REF_1(partitionTable, FL_PARTITION_TABLE_PT_ENTRY_OFFSET);
#endif
recordCHS(ptEntry, fullCylinderSize, pVol->sectorsPerTrack, partitionStart+shiftToActualPartition, TRUE);
recordCHS(ptEntry, fullCylinderSize, pVol->sectorsPerTrack, (partitionStart + partitionSize - 1), FALSE);
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
ptEntry->type = partitionParams->pType;
toUNAL4(ptEntry->startingSectorOfPartition, shiftToActualPartition);
toUNAL4(ptEntry->sectorsInPartition, partitionSize - shiftToActualPartition);
#else
FL_REF_1(ptEntry, FL_PARTITION_TYPE_OFFSET) = partitionParams->pType;
FL_SET_UNAL4(ptEntry, FL_PARTITION_STARTING_SECTOR_OF_PARTITION_OFFSET, shiftToActualPartition);
FL_SET_UNAL4(ptEntry, FL_PARTITION_SECTORS_IN_PARTITION_OFFSET, partitionSize - shiftToActualPartition);
#endif
}
/* the only case in EBR chain when currentMBRslot is not 2 - is in mainEBR
in all others 1st slot is always occupied */
}
else /* this partition was created inside MBR */
{
if (partitionParams->pType == EX_PARTIT)
{ /* save current MBR */
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
toLE2(partitionTable.signature, PARTITION_SIGNATURE);
#else
FL_SET_LE2(partitionTable, FL_PARTITION_TABLE_SIGNATURE_OFFSET, PARTITION_SIGNATURE);
#endif
checkStatus(pVol->writeSector(pVol->rec, 0, &partitionTable));
/* prepare to work with EBR chain */
mainEBR = currentMEBR = partitionStart;
currentMBRslot = 0;
tffsset(&partitionTable, 0, sizeof partitionTable);
tffscpy(&partitionTable, bootCode, sizeof bootCode);
freeSpaceStartSector = partitionStart + 1;
freeSpaceSectorsNumber = partitionSize - 1;
/* free space for the following [secondary] partitions is inside this - extended - one
COMPATIBILITY mode should automatically bump it to cylinder boundary */
}
}
/* done! */
partitionParams++;
}
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
toLE2(partitionTable.signature,PARTITION_SIGNATURE);
#else
FL_SET_LE2(partitionTable, FL_PARTITION_TABLE_SIGNATURE_OFFSET, PARTITION_SIGNATURE);
#endif
return pVol->writeSector(pVol->rec, currentMEBR, &partitionTable);
}
#endif /* FL_CREATE_LOGICAL */
#if defined (FL_LOGICAL_FORMAT_VOLUME) || defined (FS_FORMAT_VOLUME)
/*-----------------------------------------------------------------------*/
/* f s F o r m a t F S */
/* irHandle : Socket number (0, 1, ...) */
/* bits 11-8 - Logical partition in Flash partition (zero based) - if bit 12 is set */
/* bits 7-4 - Partition # (zero based) */
/* bits 3-0 - Socket # (zero based) */
/* irSectorNo : First absolute sector of logical partition */
/* (if bit 13 is set) */
/* irSectorCount : Number of sectors in logical partition */
/* (if bit 13 is set) */
/* irData : Address of the FATFormatParams structure */
/* irFlags : */
/* bit 14 - FL_DO_NOT_UPDATE_MBR */
/* bit 13 - FL_MEDIA_WITHOUT_MBR */
/* bit 12 - FL_MOUNT_ON_GIVEN_RANGE */
/*-----------------------------------------------------------------------*/
static FLStatus formatFS(TL * tl, IOreq FAR2 *ioreq) /* yeah, the name is not great */
{
FLStatus status = flOK;
SectorNo MBR;
int MBRSlot;
SectorNo bootSectorNo, sectorsInVolume;
char logPartition = FL_GET_LOGICAL_PARTITION_FROM_HANDLE(ioreq);
if (ioreq->irFlags & FL_MEDIA_WITHOUT_MBR)
{
if (logPartition != 0)
{
return flBadParameter;
}
bootSectorNo = 0;
sectorsInVolume = tl->sectorsInVolume(tl->rec);
MBR = (SectorNo)-1;
MBRSlot = -1;
}
if (ioreq->irFlags & FL_MOUNT_ON_GIVEN_RANGE)
{
bootSectorNo = ioreq->irSectorNo;
sectorsInVolume = ioreq->irSectorCount;
MBR = (SectorNo)-1;
MBRSlot = -1;
}
if ( !(ioreq->irFlags & (FL_MEDIA_WITHOUT_MBR | FL_MOUNT_ON_GIVEN_RANGE)) &&
!(ioreq->irFlags & FL_DO_NOT_UPDATE_MBR) )
status = findLogicalPartition(tl, logPartition,
&bootSectorNo,
§orsInVolume,
&MBR,
&MBRSlot);
/* we already have bootSectorNo & sectorsInVolume, but we need MBR and MBRslot
can not just checkstatus(): it can blow the lockForIO */
if (status == flOK)
status = flFATFormat(tl, (FATFormatParams FAR0 *)ioreq->irData,
bootSectorNo, sectorsInVolume,
MBR, MBRSlot);
return status;
}
#endif /* FL_LOGICAL_FORMAT_VOLUME */
/*----------------------------------------------------------------------*/
/* f o r m a t C a l l */
/* */
/* Common entry-point to all file-system functions. Macros are */
/* to call individual function, which are separately described below. */
/* */
/* Parameters: */
/* function : file-system function code (listed below) */
/* ioreq : IOreq structure */
/* */
/* Returns: */
/* FLStatus : 0 on success, otherwise failed */
/*----------------------------------------------------------------------*/
TFFS_DLL_API FLStatus NAMING_CONVENTION formatCall(FLFunctionNo functionNo, IOreq FAR2 *ioreq)
{
IOreq ioreq2 = *ioreq;
FLStatus status;
FLByte socket = FL_GET_SOCKET_FROM_HANDLE(ioreq);
FLByte partition = FL_GET_FLASH_PARTITION_FROM_HANDLE(ioreq);
#if defined(FL_FIND_LOGICAL) || defined(FS_FIND_LOGICAL_PARTITION)
FLByte logPartition = FL_GET_LOGICAL_PARTITION_FROM_HANDLE(ioreq);
#endif /*defined(FL_FIND_LOGICAL) || defined(FS_FIND_LOGICAL_PARTITION)*/
TL tl;
/* init values */
tl.socketNo = socket;
tl.partitionNo = partition;
/* Mount the volume */
ioreq2.irHandle = ioreq->irHandle & FL_SOCKET_AND_FLASH_PARTITION_BITS;
ioreq2.irData = NULL; /* callback parameter used in utilities */
checkStatus(bdCall(FL_ABS_MOUNT, &ioreq2));
setTL(socket, partition, &tl); /* copy or set-up tl routines for FS level */
checkStatus(lockForIO(socket, partition, FL_ON /*, FALSE*/)); /* lock for read */
switch (functionNo)
{
#if defined(FL_CREATE_LOGICAL) || defined (FS_CREATE_LOGICAL_PARTITIONS)
case FL_CREATE_LOGICAL_PARTITIONS:
status = createLogicalPartitions(&tl,
(LogicalPartitionParams FAR0 *)ioreq->irData,
ioreq->irCount);
break;
#endif /* FL_CREATE_LOGICAL || FS_CREATE_LOGICAL_PARTITIONS*/
/*Logical format routines*/
#if def
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -