EFI_STATUS FatAccessVolumeDirty ( IN FAT_VOLUME *Volume, IN IO_MODE IoMode, IN VOID *DirtyValue ) /*++ Routine Description: Set the volume as dirty or not Arguments: Volume - FAT file system volume. IoMode - The access mode. DirtyValue - Set the volume as dirty or not. Returns: EFI_SUCCESS - Set the new FAT entry value sucessfully. other - An error occurred when operation the FAT entries. --*/ { UINTN WriteCount; WriteCount = Volume->FatEntrySize; return FatDiskIo (Volume, IoMode, Volume->FatPos + WriteCount, WriteCount, DirtyValue); }
/** Detects FAT file system on Disk and set relevant fields of Volume. @param Volume - The volume structure. @retval EFI_SUCCESS - The Fat File System is detected successfully @retval EFI_UNSUPPORTED - The volume is not FAT file system. @retval EFI_VOLUME_CORRUPTED - The volume is corrupted. **/ EFI_STATUS FatOpenDevice ( IN OUT FAT_VOLUME *Volume ) { EFI_STATUS Status; UINT32 BlockSize; UINT32 DirtyMask; EFI_DISK_IO_PROTOCOL *DiskIo; FAT_BOOT_SECTOR FatBs; FAT_VOLUME_TYPE FatType; UINTN RootDirSectors; UINTN FatLba; UINTN RootLba; UINTN FirstClusterLba; UINTN Sectors; UINTN SectorsPerFat; UINT8 SectorsPerClusterAlignment; UINT8 BlockAlignment; // // Read the FAT_BOOT_SECTOR BPB info // This is the only part of FAT code that uses parent DiskIo, // Others use FatDiskIo which utilizes a Cache. // DiskIo = Volume->DiskIo; Status = DiskIo->ReadDisk (DiskIo, Volume->MediaId, 0, sizeof (FatBs), &FatBs); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_INIT, "FatOpenDevice: read of part_lba failed %r\n", Status)); return Status; } FatType = FatUndefined; // // Use LargeSectors if Sectors is 0 // Sectors = FatBs.FatBsb.Sectors; if (Sectors == 0) { Sectors = FatBs.FatBsb.LargeSectors; } SectorsPerFat = FatBs.FatBsb.SectorsPerFat; if (SectorsPerFat == 0) { SectorsPerFat = FatBs.FatBse.Fat32Bse.LargeSectorsPerFat; FatType = Fat32; } // // Is boot sector a fat sector? // (Note that so far we only know if the sector is FAT32 or not, we don't // know if the sector is Fat16 or Fat12 until later when we can compute // the volume size) // if (FatBs.FatBsb.ReservedSectors == 0 || FatBs.FatBsb.NumFats == 0 || Sectors == 0) { return EFI_UNSUPPORTED; } if ((FatBs.FatBsb.SectorSize & (FatBs.FatBsb.SectorSize - 1)) != 0) { return EFI_UNSUPPORTED; } BlockAlignment = (UINT8) HighBitSet32 (FatBs.FatBsb.SectorSize); if (BlockAlignment > MAX_BLOCK_ALIGNMENT || BlockAlignment < MIN_BLOCK_ALIGNMENT) { return EFI_UNSUPPORTED; } if ((FatBs.FatBsb.SectorsPerCluster & (FatBs.FatBsb.SectorsPerCluster - 1)) != 0) { return EFI_UNSUPPORTED; } SectorsPerClusterAlignment = (UINT8) HighBitSet32 (FatBs.FatBsb.SectorsPerCluster); if (SectorsPerClusterAlignment > MAX_SECTORS_PER_CLUSTER_ALIGNMENT) { return EFI_UNSUPPORTED; } if (FatBs.FatBsb.Media <= 0xf7 && FatBs.FatBsb.Media != 0xf0 && FatBs.FatBsb.Media != 0x00 && FatBs.FatBsb.Media != 0x01 ) { return EFI_UNSUPPORTED; } // // Initialize fields the volume information for this FatType // if (FatType != Fat32) { if (FatBs.FatBsb.RootEntries == 0) { return EFI_UNSUPPORTED; } // // Unpack fat12, fat16 info // Volume->RootEntries = FatBs.FatBsb.RootEntries; } else { // // If this is fat32, refuse to mount mirror-disabled volumes // if ((SectorsPerFat == 0 || FatBs.FatBse.Fat32Bse.FsVersion != 0) || (FatBs.FatBse.Fat32Bse.ExtendedFlags & 0x80)) { return EFI_UNSUPPORTED; } // // Unpack fat32 info // Volume->RootCluster = FatBs.FatBse.Fat32Bse.RootDirFirstCluster; } Volume->NumFats = FatBs.FatBsb.NumFats; // // Compute some fat locations // BlockSize = FatBs.FatBsb.SectorSize; RootDirSectors = ((Volume->RootEntries * sizeof (FAT_DIRECTORY_ENTRY)) + (BlockSize - 1)) / BlockSize; FatLba = FatBs.FatBsb.ReservedSectors; RootLba = FatBs.FatBsb.NumFats * SectorsPerFat + FatLba; FirstClusterLba = RootLba + RootDirSectors; Volume->FatPos = FatLba * BlockSize; Volume->FatSize = SectorsPerFat * BlockSize; Volume->VolumeSize = LShiftU64 (Sectors, BlockAlignment); Volume->RootPos = LShiftU64 (RootLba, BlockAlignment); Volume->FirstClusterPos = LShiftU64 (FirstClusterLba, BlockAlignment); Volume->MaxCluster = (Sectors - FirstClusterLba) >> SectorsPerClusterAlignment; Volume->ClusterAlignment = (UINT8)(BlockAlignment + SectorsPerClusterAlignment); Volume->ClusterSize = (UINTN)1 << (Volume->ClusterAlignment); // // If this is not a fat32, determine if it's a fat16 or fat12 // if (FatType != Fat32) { if (Volume->MaxCluster >= FAT_MAX_FAT16_CLUSTER) { return EFI_VOLUME_CORRUPTED; } FatType = Volume->MaxCluster < FAT_MAX_FAT12_CLUSTER ? Fat12 : Fat16; // // fat12 & fat16 fat-entries are 2 bytes // Volume->FatEntrySize = sizeof (UINT16); DirtyMask = FAT16_DIRTY_MASK; } else { if (Volume->MaxCluster < FAT_MAX_FAT16_CLUSTER) { return EFI_VOLUME_CORRUPTED; } // // fat32 fat-entries are 4 bytes // Volume->FatEntrySize = sizeof (UINT32); DirtyMask = FAT32_DIRTY_MASK; } // // Get the DirtyValue and NotDirtyValue // We should keep the initial value as the NotDirtyValue // in case the volume is dirty already // if (FatType != Fat12) { Status = FatAccessVolumeDirty (Volume, ReadDisk, &Volume->NotDirtyValue); if (EFI_ERROR (Status)) { return Status; } Volume->DirtyValue = Volume->NotDirtyValue & DirtyMask; } // // If present, read the fat hint info // if (FatType == Fat32) { Volume->FreeInfoPos = FatBs.FatBse.Fat32Bse.FsInfoSector * BlockSize; if (FatBs.FatBse.Fat32Bse.FsInfoSector != 0) { FatDiskIo (Volume, ReadDisk, Volume->FreeInfoPos, sizeof (FAT_INFO_SECTOR), &Volume->FatInfoSector, NULL); if (Volume->FatInfoSector.Signature == FAT_INFO_SIGNATURE && Volume->FatInfoSector.InfoBeginSignature == FAT_INFO_BEGIN_SIGNATURE && Volume->FatInfoSector.InfoEndSignature == FAT_INFO_END_SIGNATURE && Volume->FatInfoSector.FreeInfo.ClusterCount <= Volume->MaxCluster ) { Volume->FreeInfoValid = TRUE; } } } // // Just make up a FreeInfo.NextCluster for use by allocate cluster // if (FAT_MIN_CLUSTER > Volume->FatInfoSector.FreeInfo.NextCluster || Volume->FatInfoSector.FreeInfo.NextCluster > Volume->MaxCluster + 1 ) { Volume->FatInfoSector.FreeInfo.NextCluster = FAT_MIN_CLUSTER; } // // We are now defining FAT Type // Volume->FatType = FatType; ASSERT (FatType != FatUndefined); return EFI_SUCCESS; }
EFI_STATUS FatAccessOFile ( IN FAT_OFILE *OFile, IN IO_MODE IoMode, IN UINTN Position, IN OUT UINTN *DataBufferSize, IN OUT UINT8 *UserBuffer, IN FAT_TASK *Task ) /*++ Routine Description: This function reads data from a file or writes data to a file. It uses OFile->PosRem to determine how much data can be accessed in one time. Arguments: OFile - The open file. IoMode - Indicate whether the access mode is reading or writing. Position - The position where data will be accessed. DataBufferSize - Size of Buffer. UserBuffer - Buffer containing data. Returns: EFI_SUCCESS - Access the data successfully. other - An error occurred when operating on the disk. --*/ { FAT_VOLUME *Volume; UINTN Len; EFI_STATUS Status; UINTN BufferSize; BufferSize = *DataBufferSize; Volume = OFile->Volume; ASSERT_VOLUME_LOCKED (Volume); Status = EFI_SUCCESS; while (BufferSize > 0) { // // Seek the OFile to the file position // Status = FatOFilePosition (OFile, Position, BufferSize); if (EFI_ERROR (Status)) { break; } // // Clip length to block run // Len = BufferSize > OFile->PosRem ? OFile->PosRem : BufferSize; // // Write the data // Status = FatDiskIo (Volume, IoMode, OFile->PosDisk, Len, UserBuffer, Task); if (EFI_ERROR (Status)) { break; } // // Data was successfully accessed // Position += Len; UserBuffer += Len; BufferSize -= Len; if (IoMode == WRITE_DATA) { OFile->Dirty = TRUE; OFile->Archive = TRUE; } // // Make sure no outbound occurred // ASSERT (Position <= OFile->FileSize); } // // Update the number of bytes accessed // *DataBufferSize -= BufferSize; return Status; }