3 Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Initialization routines
28 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
29 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
35 Allocates volume structure, detects FAT file system, installs protocol,
40 Handle - The handle of parent device.
41 DiskIo - The DiskIo of parent device.
42 BlockIo - The BlockIo of parent devicel
46 EFI_SUCCESS - Allocate a new volume successfully.
47 EFI_OUT_OF_RESOURCES - Can not allocate the memory.
48 Others - Allocating a new volume failed.
56 // Allocate a volume structure
58 Volume
= AllocateZeroPool (sizeof (FAT_VOLUME
));
60 return EFI_OUT_OF_RESOURCES
;
64 // Initialize the structure
66 Volume
->Signature
= FAT_VOLUME_SIGNATURE
;
67 Volume
->Handle
= Handle
;
68 Volume
->DiskIo
= DiskIo
;
69 Volume
->BlockIo
= BlockIo
;
70 Volume
->MediaId
= BlockIo
->Media
->MediaId
;
71 Volume
->ReadOnly
= BlockIo
->Media
->ReadOnly
;
72 Volume
->VolumeInterface
.Revision
= EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
;
73 Volume
->VolumeInterface
.OpenVolume
= FatOpenVolume
;
74 InitializeListHead (&Volume
->CheckRef
);
75 InitializeListHead (&Volume
->DirCacheList
);
77 // Initialize Root Directory entry
79 Volume
->RootDirEnt
.FileString
= Volume
->RootFileString
;
80 Volume
->RootDirEnt
.Entry
.Attributes
= FAT_ATTRIBUTE_DIRECTORY
;
82 // Check to see if there's a file system on the volume
84 Status
= FatOpenDevice (Volume
);
85 if (EFI_ERROR (Status
)) {
91 Status
= FatInitializeDiskCache (Volume
);
92 if (EFI_ERROR (Status
)) {
96 // Install our protocol interfaces on the device's handle
98 Status
= gBS
->InstallMultipleProtocolInterfaces (
100 &gEfiSimpleFileSystemProtocolGuid
,
101 &Volume
->VolumeInterface
,
104 if (EFI_ERROR (Status
)) {
110 DEBUG ((EFI_D_INIT
, "Installed Fat filesystem on %p\n", Handle
));
111 Volume
->Valid
= TRUE
;
114 if (EFI_ERROR (Status
)) {
115 FatFreeVolume (Volume
);
123 IN FAT_VOLUME
*Volume
129 Called by FatDriverBindingStop(), Abandon the volume.
133 Volume - The volume to be abandoned.
137 EFI_SUCCESS - Abandoned the volume successfully.
138 Others - Can not uninstall the protocol interfaces.
146 // Uninstall the protocol interface.
148 if (Volume
->Handle
!= NULL
) {
149 Status
= gBS
->UninstallMultipleProtocolInterfaces (
151 &gEfiSimpleFileSystemProtocolGuid
,
152 &Volume
->VolumeInterface
,
155 if (EFI_ERROR (Status
)) {
164 // If the caller has already acquired the lock (which
165 // means we are in the process of some Fat operation),
166 // we can not acquire again.
168 Status
= FatAcquireLockOrFail ();
169 if (!EFI_ERROR (Status
)) {
173 // The volume is still being used. Hence, set error flag for all OFiles still in
174 // use. In two cases, we could get here. One is EFI_MEDIA_CHANGED, the other is
177 if (Volume
->Root
!= NULL
) {
180 Volume
->BlockIo
->Media
->MediaPresent
? EFI_MEDIA_CHANGED
: EFI_NO_MEDIA
184 Volume
->Valid
= FALSE
;
188 // If locked by me, this means DriverBindingStop is NOT
189 // called within an on-going Fat operation, so we should
190 // take responsibility to cleanup and free the volume.
191 // Otherwise, the DriverBindingStop is called within an on-going
192 // Fat operation, we shouldn't check reference, so just let outer
193 // FatCleanupVolume do the task.
196 FatCleanupVolume (Volume
, NULL
, EFI_SUCCESS
);
205 IN OUT FAT_VOLUME
*Volume
211 Detects FAT file system on Disk and set relevant fields of Volume
215 Volume - The volume structure.
219 EFI_SUCCESS - The Fat File System is detected successfully
220 EFI_UNSUPPORTED - The volume is not FAT file system.
221 EFI_VOLUME_CORRUPTED - The volume is corrupted.
228 EFI_DISK_IO_PROTOCOL
*DiskIo
;
229 FAT_BOOT_SECTOR FatBs
;
230 FAT_VOLUME_TYPE FatType
;
231 UINTN RootDirSectors
;
234 UINTN FirstClusterLba
;
237 UINT8 SectorsPerClusterAlignment
;
238 UINT8 BlockAlignment
;
241 // Read the FAT_BOOT_SECTOR BPB info
242 // This is the only part of FAT code that uses parent DiskIo,
243 // Others use FatDiskIo which utilizes a Cache.
245 DiskIo
= Volume
->DiskIo
;
246 Status
= DiskIo
->ReadDisk (DiskIo
, Volume
->MediaId
, 0, sizeof (FatBs
), &FatBs
);
248 if (EFI_ERROR (Status
)) {
249 DEBUG ((EFI_D_INIT
, "FatOpenDevice: read of part_lba failed %r\n", Status
));
253 FatType
= FatUndefined
;
256 // Use LargeSectors if Sectors is 0
258 Sectors
= FatBs
.FatBsb
.Sectors
;
260 Sectors
= FatBs
.FatBsb
.LargeSectors
;
263 SectorsPerFat
= FatBs
.FatBsb
.SectorsPerFat
;
264 if (SectorsPerFat
== 0) {
265 SectorsPerFat
= FatBs
.FatBse
.Fat32Bse
.LargeSectorsPerFat
;
269 // Is boot sector a fat sector?
270 // (Note that so far we only know if the sector is FAT32 or not, we don't
271 // know if the sector is Fat16 or Fat12 until later when we can compute
274 if (FatBs
.FatBsb
.ReservedSectors
== 0 || FatBs
.FatBsb
.NumFats
== 0 || Sectors
== 0) {
275 return EFI_UNSUPPORTED
;
278 if ((FatBs
.FatBsb
.SectorSize
& (FatBs
.FatBsb
.SectorSize
- 1)) != 0) {
279 return EFI_UNSUPPORTED
;
282 BlockAlignment
= (UINT8
) HighBitSet32 (FatBs
.FatBsb
.SectorSize
);
283 if (BlockAlignment
> MAX_BLOCK_ALIGNMENT
|| BlockAlignment
< MIN_BLOCK_ALIGNMENT
) {
284 return EFI_UNSUPPORTED
;
287 if ((FatBs
.FatBsb
.SectorsPerCluster
& (FatBs
.FatBsb
.SectorsPerCluster
- 1)) != 0) {
288 return EFI_UNSUPPORTED
;
291 SectorsPerClusterAlignment
= (UINT8
) HighBitSet32 (FatBs
.FatBsb
.SectorsPerCluster
);
292 if (SectorsPerClusterAlignment
> MAX_SECTORS_PER_CLUSTER_ALIGNMENT
) {
293 return EFI_UNSUPPORTED
;
296 if (FatBs
.FatBsb
.Media
<= 0xf7 &&
297 FatBs
.FatBsb
.Media
!= 0xf0 &&
298 FatBs
.FatBsb
.Media
!= 0x00 &&
299 FatBs
.FatBsb
.Media
!= 0x01
301 return EFI_UNSUPPORTED
;
304 // Initialize fields the volume information for this FatType
306 if (FatType
!= FAT32
) {
307 if (FatBs
.FatBsb
.RootEntries
== 0) {
308 return EFI_UNSUPPORTED
;
311 // Unpack fat12, fat16 info
313 Volume
->RootEntries
= FatBs
.FatBsb
.RootEntries
;
316 // If this is fat32, refuse to mount mirror-disabled volumes
318 if ((SectorsPerFat
== 0 || FatBs
.FatBse
.Fat32Bse
.FsVersion
!= 0) || (FatBs
.FatBse
.Fat32Bse
.ExtendedFlags
& 0x80)) {
319 return EFI_UNSUPPORTED
;
324 Volume
->RootCluster
= FatBs
.FatBse
.Fat32Bse
.RootDirFirstCluster
;
327 Volume
->NumFats
= FatBs
.FatBsb
.NumFats
;
329 // Compute some fat locations
331 BlockSize
= FatBs
.FatBsb
.SectorSize
;
332 RootDirSectors
= ((Volume
->RootEntries
* sizeof (FAT_DIRECTORY_ENTRY
)) + (BlockSize
- 1)) / BlockSize
;
334 FatLba
= FatBs
.FatBsb
.ReservedSectors
;
335 RootLba
= FatBs
.FatBsb
.NumFats
* SectorsPerFat
+ FatLba
;
336 FirstClusterLba
= RootLba
+ RootDirSectors
;
338 Volume
->FatPos
= FatLba
* BlockSize
;
339 Volume
->FatSize
= SectorsPerFat
* BlockSize
;
341 Volume
->VolumeSize
= LShiftU64 (Sectors
, BlockAlignment
);
342 Volume
->RootPos
= LShiftU64 (RootLba
, BlockAlignment
);
343 Volume
->FirstClusterPos
= LShiftU64 (FirstClusterLba
, BlockAlignment
);
344 Volume
->MaxCluster
= (Sectors
- FirstClusterLba
) >> SectorsPerClusterAlignment
;
345 Volume
->ClusterAlignment
= (UINT8
)(BlockAlignment
+ SectorsPerClusterAlignment
);
346 Volume
->ClusterSize
= (UINTN
)1 << (Volume
->ClusterAlignment
);
349 // If this is not a fat32, determine if it's a fat16 or fat12
351 if (FatType
!= FAT32
) {
352 if (Volume
->MaxCluster
>= FAT_MAX_FAT16_CLUSTER
) {
353 return EFI_VOLUME_CORRUPTED
;
356 FatType
= Volume
->MaxCluster
< FAT_MAX_FAT12_CLUSTER
? FAT12
: FAT16
;
358 // fat12 & fat16 fat-entries are 2 bytes
360 Volume
->FatEntrySize
= sizeof (UINT16
);
361 DirtyMask
= FAT16_DIRTY_MASK
;
363 if (Volume
->MaxCluster
< FAT_MAX_FAT16_CLUSTER
) {
364 return EFI_VOLUME_CORRUPTED
;
367 // fat32 fat-entries are 4 bytes
369 Volume
->FatEntrySize
= sizeof (UINT32
);
370 DirtyMask
= FAT32_DIRTY_MASK
;
373 // Get the DirtyValue and NotDirtyValue
374 // We should keep the initial value as the NotDirtyValue
375 // in case the volume is dirty already
377 if (FatType
!= FAT12
) {
378 Status
= FatAccessVolumeDirty (Volume
, READ_DISK
, &Volume
->NotDirtyValue
);
379 if (EFI_ERROR (Status
)) {
383 Volume
->DirtyValue
= Volume
->NotDirtyValue
& DirtyMask
;
386 // If present, read the fat hint info
388 if (FatType
== FAT32
) {
389 Volume
->FreeInfoPos
= FatBs
.FatBse
.Fat32Bse
.FsInfoSector
* BlockSize
;
390 if (FatBs
.FatBse
.Fat32Bse
.FsInfoSector
!= 0) {
391 FatDiskIo (Volume
, READ_DISK
, Volume
->FreeInfoPos
, sizeof (FAT_INFO_SECTOR
), &Volume
->FatInfoSector
);
392 if (Volume
->FatInfoSector
.Signature
== FAT_INFO_SIGNATURE
&&
393 Volume
->FatInfoSector
.InfoBeginSignature
== FAT_INFO_BEGIN_SIGNATURE
&&
394 Volume
->FatInfoSector
.InfoEndSignature
== FAT_INFO_END_SIGNATURE
&&
395 Volume
->FatInfoSector
.FreeInfo
.ClusterCount
<= Volume
->MaxCluster
397 Volume
->FreeInfoValid
= TRUE
;
402 // Just make up a FreeInfo.NextCluster for use by allocate cluster
404 if (FAT_MIN_CLUSTER
> Volume
->FatInfoSector
.FreeInfo
.NextCluster
||
405 Volume
->FatInfoSector
.FreeInfo
.NextCluster
> Volume
->MaxCluster
+ 1
407 Volume
->FatInfoSector
.FreeInfo
.NextCluster
= FAT_MIN_CLUSTER
;
410 // We are now defining FAT Type
412 Volume
->FatType
= FatType
;
413 ASSERT (FatType
!= FatUndefined
);