📄 dosformt.c
字号:
tffscpy(bootSector->volumeLabel,volumeLabel,sizeof(bootSector->volumeLabel));
else
tffsset(bootSector->volumeLabel,' ', sizeof(bootSector->volumeLabel));
if (flags & FL_TFAT_MODE)
tffscpy(bootSector->systemId, "TFAT32 ", sizeof(bootSector->systemId));
else
tffscpy(bootSector->systemId, "FAT32 ", sizeof(bootSector->systemId));
bootSector->bpb.jumpInstruction[0] = 0xe9;
tffscpy(bootSector->bpb.OEMname,"MSystems",sizeof(bootSector->bpb.OEMname));
toLE2(bootSector->signature,PARTITION_SIGNATURE);
checkStatus(pVol->writeSector(pVol->rec, (SectorNo) LE4(bootSector->bpb.noOfHiddenSectors), bootSector));
checkStatus(pVol->writeSector(pVol->rec,
(SectorNo) LE4(bpb->noOfHiddenSectors) + BACKUP_OF_BOOT_SECTOR, bootSector));
tffsset(infoSector, 0, sizeof bootSectBuf);
toLE4(infoSector->FSI_LeadSig, FSI_LEADING_SIGNATURE);
toLE4(infoSector->FSI_StructSig, FSI_STRUCTURE_SIGNATURE);
toLE4(infoSector->FSI_Free_Count, (FLDword)(-1));
toLE4(infoSector->FSI_Nxt_Free, (FLDword)(-1));
toLE2(infoSector->signature,PARTITION_SIGNATURE);
return (pVol->writeSector(pVol->rec,
(SectorNo) LE4(bpb->noOfHiddenSectors) + 1, infoSector));
}
else
#endif /*FL_SUPPORT_FAT32_FORMAT*/
{
DOSBootSector* bootSector = (DOSBootSector*)bootSectBuf;
tffsset(bootSector,0,sizeof(bootSectBuf));
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
bootSector->physicalDriveNo = DRIVE_NUMBER;
bootSector->extendedBootSignature = EXTENDED_BOOT_SIGNATURE;
#else
FL_REF_1(bootSector, FL_DOSBOOTSECTOR_PHYSICAL_DRIVE_NO_OFFSET) = 0x80;
FL_REF_1(bootSector, FL_DOSBOOTSECTOR_EXTENDED_BOOT_SIGNATURE_OFFSET) = 0x29;
#endif
if ((volumeId[0] == 0) && (volumeId[1] == 0) &&
(volumeId[2] == 0) && (volumeId[3] == 0) )
{
FLWord curDate = (FLWord)flCurrentDate();
FLWord curTime = (FLWord)flCurrentTime();
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
bootSector->volumeId[0] = (FLSByte)curDate;
bootSector->volumeId[1] = (FLSByte)(curDate >> 8);
bootSector->volumeId[2] = (FLSByte)curTime;
bootSector->volumeId[3] = (FLSByte)(curTime >> 8);
#else
FL_SET_UNAL2(bootSector, FL_DOSBOOTSECTOR_VOLUME_ID_OFFSET, curDate);
FL_SET_UNAL2(bootSector, FL_DOSBOOTSECTOR_VOLUME_ID_OFFSET + 2, curTime);
#endif
}
else
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
tffscpy(bootSector->volumeId, volumeId, sizeof(bootSector->volumeId));
#else
tffscpy(&FL_REF_1(bootSector, FL_DOSBOOTSECTOR_VOLUME_ID_OFFSET), volumeId, 4);
#endif
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
if (volumeLabel)
tffscpy(bootSector->volumeLabel,volumeLabel,sizeof(bootSector->volumeLabel));
else
tffsset(bootSector->volumeLabel,' ',sizeof(bootSector->volumeLabel));
if (flags & FL_TFAT_MODE)
tffscpy(bootSector->systemId, (fatType == FAT12_PARTIT) ?
"TFAT12 " : "TFAT16 ", sizeof(bootSector->systemId));
else
tffscpy(bootSector->systemId, (fatType == FAT12_PARTIT) ?
"FAT12 " : "FAT16 ", sizeof(bootSector->systemId));
bootSector->bpb = *bpb;
bootSector->bpb.jumpInstruction[0] = 0xe9;
tffscpy(bootSector->bpb.OEMname,"MSystems",sizeof(bootSector->bpb.OEMname));
toLE2(bootSector->
signature,PARTITION_SIGNATURE);
#else
if (volumeLabel)
tffscpy(&FL_REF_1(bootSector, FL_DOSBOOTSECTOR_VOLUME_LABEL_OFFSET), volumeLabel, 11);
else
tffsset(&FL_REF_1(bootSector, FL_DOSBOOTSECTOR_VOLUME_LABEL_OFFSET), ' ', 11);
tffscpy(&FL_REF_1(bootSector, FL_DOSBOOTSECTOR_SYSTEM_ID_OFFSET),(fatType == FAT12_PARTIT) ?
"FAT12 " : "FAT16 ", 8);
tffscpy(&FL_REF_1(bootSector, FL_DOSBOOTSECTOR_BPB_OFFSET), bpb, sizeof(BPB));
FL_REF_1(bootSector, FL_BPB_JUMP_INSTRUCTION_OFFSET) = 0xe9;
tffscpy(&FL_REF_1(bootSector, FL_BPB_OEM_NAME_OFFSET), "MSystems", 8);
FL_SET_LE2(bootSector, FL_DOSBOOTSECTOR_SIGNATURE_OFFSET, PARTITION_SIGNATURE);
#endif
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
return pVol->writeSector(pVol->rec, (SectorNo) LE4(bpb->noOfHiddenSectors), bootSector);
#else
return pVol->writeSector(pVol->rec, (SectorNo) FL_GET_LE4(bpb, FL_BPB_NO_OF_HIDDEN_SECTORS_OFFSET),
bootSector);
#endif
}
}
/*----------------------------------------------------------------------*/
/* c r e a t e F A T s */
/* */
/* Creates the FAT tables. The function assumes that the boot sector is*/
/* already written with updated partition parameters and gets the input */
/* parameter from the sector data. */
/* */
/* Parameters: */
/* pVol : Pointer identifying drive */
/* bootSectorNo : Sector number where boot sector is */
/* written. */
/* fatType : Type of partition FAT. */
/* */
/* Returns: */
/* FLStatus : 0 on success, failed otherwise */
/*----------------------------------------------------------------------*/
static FLStatus createFATs(TL * pVol, SectorNo bootSectorNo, FLByte fatType)
{
FLWord iFAT;
FLByte bootSectBuf[FL_SECTOR_SIZE];
SectorNo sectorNo;
FLWord noOfFATS;
FLDword sectorsPerFAT;
FLByte mediaDescriptor;
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
{
FAT32BootSector *bootSector = (FAT32BootSector *)bootSectBuf;
checkStatus(pVol->readSectors(pVol->rec, bootSectorNo, bootSector, 1));
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
sectorNo = (SectorNo) bootSectorNo + LE2(bootSector->bpb.reservedSectors);
noOfFATS = bootSector->bpb.noOfFATS;
sectorsPerFAT = LE4(bootSector->FAT32Size);
mediaDescriptor = bootSector->bpb.mediaDescriptor;
#else
sectorNo = (SectorNo) (bootSectorNo +
FL_GET_LE2(bootSector, FL_FAT32_RESERVED_SECTORS_OFFSET));
noOfFATS = FL_REF_1(bootSector, FL_FAT32_NO_OF_FATS_OFFSET);
sectorsPerFAT = FL_GET_LE2(bootSector, FL_FAT32_SECTORS_PER_FAT_OFFSET);
mediaDescriptor = bootSector->bpb.mediaDescriptor;
#endif
}
else
#endif /*FL_SUPPORT_FAT32_FORMAT*/
{
DOSBootSector *bootSector = (DOSBootSector *)bootSectBuf;
checkStatus(pVol->readSectors(pVol->rec, bootSectorNo, bootSector, 1));
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
sectorNo = (SectorNo) (bootSectorNo + LE2(bootSector->bpb.reservedSectors));
noOfFATS = bootSector->bpb.noOfFATS;
sectorsPerFAT = LE2(bootSector->bpb.sectorsPerFAT);
mediaDescriptor = bootSector->bpb.mediaDescriptor;
#else
sectorNo = (SectorNo) (bootSectorNo +
FL_GET_LE2(bootSector, FL_BPB_RESERVED_SECTORS_OFFSET));
noOfFATS = FL_REF_1(bootSector, FL_BPB_NO_OF_FATS_OFFSET);
sectorsPerFAT = FL_GET_LE2(bootSector, FL_BPB_SECTORS_PER_FAT_OFFSET);
mediaDescriptor = FL_REF_1(bootSector, FL_BPB_MEDIA_DESCRIPTOR_OFFSET);
#endif
}
/* create the FATs */
for (iFAT = 0; iFAT < noOfFATS; iFAT++)
{
FLDword iSector;
FLByte FATEntry[FL_SECTOR_SIZE];
for (iSector = 0; iSector < sectorsPerFAT; iSector++)
{
tffsset(FATEntry,0,FL_SECTOR_SIZE);
if (iSector == 0)
{ /* write the reserved FAT entries */
FATEntry[0] = mediaDescriptor;
FATEntry[1] = 0xff;
FATEntry[2] = 0xff;
if ((fatType == FAT16_PARTIT) || (fatType == DOS4_PARTIT))
FATEntry[3] = 0xff;
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
{ /*Also cluster 2 is taken by the root for FAT32*/
tffsset(&FATEntry[4], 0xff, 8); /*Need also a cluster for the root*/
FATEntry[3] = 0x0f; /*Last 4 bits of each cluster should be zero.*/
FATEntry[7] = 0x0f;
FATEntry[0xb] = 0x0f;
}
#endif /*FL_SUPPORT_FAT32_FORMAT*/
}
checkStatus(pVol->writeSector(pVol->rec,sectorNo++,FATEntry));
}
}
return flOK;
}
/*----------------------------------------------------------------------*/
/* c r e a t e R o o t D i r e c t o r y */
/* */
/* Creates the root directory */
/* */
/* Parameters: */
/* vol : Pointer identifying drive */
/* bpb : volume BIOS parameter block */
/* volumeLabel : volume label */
/* */
/* Returns: */
/* FLStatus : 0 on success, failed otherwise */
/*----------------------------------------------------------------------*/
static FLStatus createRootDirectory(TL * pVol,
SectorNo bootSectorNo,
const FLByte FAR1 *volumeLabel,
FLByte fatType)
{
int iEntry;
FLByte bootSectBuf[FL_SECTOR_SIZE];
SectorNo sectorNo;
FLWord rootDirEntries;
#ifdef FL_SUPPORT_FAT32_FORMAT
if (fatType == FAT32_PARTIT)
{
FAT32BootSector *bootSector = (FAT32BootSector *)bootSectBuf;
checkStatus(pVol->readSectors(pVol->rec, bootSectorNo, bootSector, 1));
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
sectorNo = (SectorNo) bootSectorNo +
LE2(bootSector->bpb.reservedSectors) +
bootSector->bpb.noOfFATS * LE2(bootSector->FAT32Size);
rootDirEntries = (FLWord)(LE2(bootSector->bpb.sectorsPerCluster) * (FL_SECTOR_SIZE / sizeof(DirectoryEntry)));
#else
sectorNo = (SectorNo) (FL_GET_LE4(bpb, FL_BPB_NO_OF_HIDDEN_SECTORS_OFFSET) +
FL_GET_LE2(bpb, FL_BPB_RESERVED_SECTORS_OFFSET) +
FL_GET_LE2(bpb, FL_BPB_SECTORS_PER_FAT_OFFSET) * FL_REF_1(bpb, FL_BPB_NO_OF_FATS_OFFSET));
rootDirEntries = FL_GET_UNAL2(bpb, FL_BPB_ROOT_DIR_ENTRIES_OFFSET);
#endif
}
else
#endif /*FL_SUPPORT_FAT32_FORMAT*/
{
DOSBootSector *bootSector = (DOSBootSector *)bootSectBuf;
checkStatus(pVol->readSectors(pVol->rec, bootSectorNo, bootSector, 1));
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
sectorNo = (SectorNo)bootSectorNo + LE2(bootSector->bpb.reservedSectors) +
bootSector->bpb.noOfFATS * LE2(bootSector->bpb.sectorsPerFAT);
rootDirEntries = UNAL2(bootSector->bpb.rootDirectoryEntries);
#else
sectorNo = (SectorNo) bootSectorNo +
FL_GET_LE2(bootSector, FL_BPB_RESERVED_SECTORS_OFFSET) +
FL_GET_LE2(bootSector, (FL_BPB_SECTORS_PER_FAT_OFFSET) *
FL_REF_1(bootSector, FL_BPB_NO_OF_FATS_OFFSET));
rootDirEntries = FL_GET_UNAL2(bootSector, FL_BPB_ROOT_DIR_ENTRIES_OFFSET);
#endif
}
/* create the root directory */
for (iEntry = 0; iEntry < rootDirEntries; iEntry += (FL_SECTOR_SIZE / sizeof(DirectoryEntry)))
{
DirectoryEntry rootDirectorySector[FL_SECTOR_SIZE / sizeof(DirectoryEntry)];
tffsset(rootDirectorySector, 0, FL_SECTOR_SIZE);
if (iEntry == 0 && volumeLabel)
{
#ifndef FL_NO_PACKED_STRUCTS_SUPPORTED
tffscpy(rootDirectorySector[0].name,volumeLabel,sizeof rootDirectorySector[0].name);
rootDirectorySector[0].attributes = 0x28; /* VOL + ARC */
toLE2(rootDirectorySector[0].updateTime,flCurrentTime());
toLE2(rootDirectorySector[0].updateDate,flCurrentDate());
#else
tffscpy(&FL_REF_1(rootDirectorySector, FL_DIRECTORY_ENTRY_NAME_OFFSET), volumeLabel, 11);
FL_REF_1(rootDirectorySector, FL_DIRECTORY_ENTRY_ATTRIBUTES_OFFSET) = 0x28; /* VOL + ARC */
FL_SET_LE2(rootDirectorySector, FL_DIRECTORY_ENTRY_UPDATE_TIME_OFFSET, flCurrentTime());
FL_SET_LE2(rootDirectorySector, FL_DIRECTORY_ENTRY_UPDATE_DATE_OFFSET, flCurrentDate());
#endif
}
checkStatus(pVol->writeSector(pVol->rec, sectorNo++, rootDirectorySector));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -