2 Cache implementation for EFI FAT File system driver.
4 Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 This function is used by the Data Cache.
15 When this function is called by write command, all entries in this range
16 are older than the contents in disk, so they are invalid; just mark them invalid.
18 When this function is called by read command, if any entry in this range
19 is dirty, it means that the relative info directly read from media is older than
20 than the info in the cache; So need to update the relative info in the Buffer.
22 @param Volume - FAT file system volume.
23 @param IoMode - This function is called by read command or write command
24 @param StartPageNo - First PageNo to be checked in the cache.
25 @param EndPageNo - Last PageNo to be checked in the cache.
26 @param Buffer - The user buffer need to update. Only when doing the read command
27 and there is dirty cache in the cache range, this parameter will be used.
32 FatFlushDataCacheRange (
33 IN FAT_VOLUME
*Volume
,
45 DISK_CACHE
*DiskCache
;
49 DiskCache
= &Volume
->DiskCache
[CacheData
];
50 BaseAddress
= DiskCache
->CacheBase
;
51 GroupMask
= DiskCache
->GroupMask
;
52 PageAlignment
= DiskCache
->PageAlignment
;
53 PageSize
= (UINTN
)1 << PageAlignment
;
55 for (PageNo
= StartPageNo
; PageNo
< EndPageNo
; PageNo
++) {
56 GroupNo
= PageNo
& GroupMask
;
57 CacheTag
= &DiskCache
->CacheTag
[GroupNo
];
58 if ((CacheTag
->RealSize
> 0) && (CacheTag
->PageNo
== PageNo
)) {
60 // When reading data form disk directly, if some dirty data
61 // in cache is in this rang, this data in the Buffer need to
62 // be updated with the cache's dirty data.
64 if (IoMode
== ReadDisk
) {
65 if (CacheTag
->Dirty
) {
67 Buffer
+ ((PageNo
- StartPageNo
) << PageAlignment
),
68 BaseAddress
+ (GroupNo
<< PageAlignment
),
74 // Make all valid entries in this range invalid.
76 CacheTag
->RealSize
= 0;
84 Exchange the cache page with the image on the disk
86 @param Volume - FAT file system volume.
87 @param DataType - Indicate the cache type.
88 @param IoMode - Indicate whether to load this page from disk or store this page to disk.
89 @param CacheTag - The Cache Tag for the current cache page.
90 @param Task point to task instance.
92 @retval EFI_SUCCESS - Cache page exchanged successfully.
93 @return Others - An error occurred when exchanging cache page.
98 FatExchangeCachePage (
99 IN FAT_VOLUME
*Volume
,
100 IN CACHE_DATA_TYPE DataType
,
102 IN CACHE_TAG
*CacheTag
,
113 DISK_CACHE
*DiskCache
;
117 DiskCache
= &Volume
->DiskCache
[DataType
];
118 PageNo
= CacheTag
->PageNo
;
119 GroupNo
= PageNo
& DiskCache
->GroupMask
;
120 PageAlignment
= DiskCache
->PageAlignment
;
121 PageAddress
= DiskCache
->CacheBase
+ (GroupNo
<< PageAlignment
);
122 EntryPos
= DiskCache
->BaseAddress
+ LShiftU64 (PageNo
, PageAlignment
);
123 RealSize
= CacheTag
->RealSize
;
124 if (IoMode
== ReadDisk
) {
125 RealSize
= (UINTN
)1 << PageAlignment
;
126 MaxSize
= DiskCache
->LimitAddress
- EntryPos
;
127 if (MaxSize
< RealSize
) {
128 DEBUG ((DEBUG_INFO
, "FatDiskIo: Cache Page OutBound occurred! \n"));
129 RealSize
= (UINTN
)MaxSize
;
134 if ((DataType
== CacheFat
) && (IoMode
== WriteDisk
)) {
135 WriteCount
= Volume
->NumFats
;
140 // Only fat table writing will execute more than once
142 Status
= FatDiskIo (Volume
, IoMode
, EntryPos
, RealSize
, PageAddress
, Task
);
143 if (EFI_ERROR (Status
)) {
147 EntryPos
+= Volume
->FatSize
;
148 } while (--WriteCount
> 0);
150 CacheTag
->Dirty
= FALSE
;
151 CacheTag
->RealSize
= RealSize
;
157 Get one cache page by specified PageNo.
159 @param Volume - FAT file system volume.
160 @param CacheDataType - The cache type: CACHE_FAT or CACHE_DATA.
161 @param PageNo - PageNo to match with the cache.
162 @param CacheTag - The Cache Tag for the current cache page.
164 @retval EFI_SUCCESS - Get the cache page successfully.
165 @return other - An error occurred when accessing data.
171 IN FAT_VOLUME
*Volume
,
172 IN CACHE_DATA_TYPE CacheDataType
,
174 IN CACHE_TAG
*CacheTag
180 OldPageNo
= CacheTag
->PageNo
;
181 if ((CacheTag
->RealSize
> 0) && (OldPageNo
== PageNo
)) {
183 // Cache Hit occurred
189 // Write dirty cache page back to disk
191 if ((CacheTag
->RealSize
> 0) && CacheTag
->Dirty
) {
192 Status
= FatExchangeCachePage (Volume
, CacheDataType
, WriteDisk
, CacheTag
, NULL
);
193 if (EFI_ERROR (Status
)) {
199 // Load new data from disk;
201 CacheTag
->PageNo
= PageNo
;
202 Status
= FatExchangeCachePage (Volume
, CacheDataType
, ReadDisk
, CacheTag
, NULL
);
209 Read Length bytes from the position of Offset into Buffer, or
210 write Length bytes from Buffer into the position of Offset.
212 @param Volume - FAT file system volume.
213 @param CacheDataType - The type of cache: CACHE_DATA or CACHE_FAT.
214 @param IoMode - Indicate the type of disk access.
215 @param PageNo - The number of unaligned cache page.
216 @param Offset - The starting byte of cache page.
217 @param Length - The number of bytes that is read or written
218 @param Buffer - Buffer containing cache data.
220 @retval EFI_SUCCESS - The data was accessed correctly.
221 @return Others - An error occurred when accessing unaligned cache page.
226 FatAccessUnalignedCachePage (
227 IN FAT_VOLUME
*Volume
,
228 IN CACHE_DATA_TYPE CacheDataType
,
239 DISK_CACHE
*DiskCache
;
243 DiskCache
= &Volume
->DiskCache
[CacheDataType
];
244 GroupNo
= PageNo
& DiskCache
->GroupMask
;
245 CacheTag
= &DiskCache
->CacheTag
[GroupNo
];
246 Status
= FatGetCachePage (Volume
, CacheDataType
, PageNo
, CacheTag
);
247 if (!EFI_ERROR (Status
)) {
248 Source
= DiskCache
->CacheBase
+ (GroupNo
<< DiskCache
->PageAlignment
) + Offset
;
249 Destination
= Buffer
;
250 if (IoMode
!= ReadDisk
) {
251 CacheTag
->Dirty
= TRUE
;
252 DiskCache
->Dirty
= TRUE
;
253 Destination
= Source
;
257 CopyMem (Destination
, Source
, Length
);
265 Read BufferSize bytes from the position of Offset into Buffer,
266 or write BufferSize bytes from Buffer into the position of Offset.
268 Base on the parameter of CACHE_DATA_TYPE, the data access will be divided into
269 the access of FAT cache (CACHE_FAT) and the access of Data cache (CACHE_DATA):
271 1. Access of FAT cache (CACHE_FAT): Access the data in the FAT cache, if there is cache
272 page hit, just return the cache page; else update the related cache page and return
273 the right cache page.
274 2. Access of Data cache (CACHE_DATA):
275 The access data will be divided into UnderRun data, Aligned data and OverRun data;
276 The UnderRun data and OverRun data will be accessed by the Data cache,
277 but the Aligned data will be accessed with disk directly.
279 @param Volume - FAT file system volume.
280 @param CacheDataType - The type of cache: CACHE_DATA or CACHE_FAT.
281 @param IoMode - Indicate the type of disk access.
282 @param Offset - The starting byte offset to read from.
283 @param BufferSize - Size of Buffer.
284 @param Buffer - Buffer containing cache data.
285 @param Task point to task instance.
287 @retval EFI_SUCCESS - The data was accessed correctly.
288 @retval EFI_MEDIA_CHANGED - The MediaId does not match the current device.
289 @return Others - An error occurred when accessing cache.
294 IN FAT_VOLUME
*Volume
,
295 IN CACHE_DATA_TYPE CacheDataType
,
299 IN OUT UINT8
*Buffer
,
310 UINTN AlignedPageCount
;
312 DISK_CACHE
*DiskCache
;
316 ASSERT (Volume
->CacheBuffer
!= NULL
);
318 Status
= EFI_SUCCESS
;
319 DiskCache
= &Volume
->DiskCache
[CacheDataType
];
320 EntryPos
= Offset
- DiskCache
->BaseAddress
;
321 PageAlignment
= DiskCache
->PageAlignment
;
322 PageSize
= (UINTN
)1 << PageAlignment
;
323 PageNo
= (UINTN
)RShiftU64 (EntryPos
, PageAlignment
);
324 UnderRun
= ((UINTN
)EntryPos
) & (PageSize
- 1);
327 Length
= PageSize
- UnderRun
;
328 if (Length
> BufferSize
) {
332 Status
= FatAccessUnalignedCachePage (Volume
, CacheDataType
, IoMode
, PageNo
, UnderRun
, Length
, Buffer
);
333 if (EFI_ERROR (Status
)) {
338 BufferSize
-= Length
;
342 AlignedPageCount
= BufferSize
>> PageAlignment
;
343 OverRunPageNo
= PageNo
+ AlignedPageCount
;
345 // The access of the Aligned data
347 if (AlignedPageCount
> 0) {
349 // Accessing fat table cannot have alignment data
351 ASSERT (CacheDataType
== CacheData
);
353 EntryPos
= Volume
->RootPos
+ LShiftU64 (PageNo
, PageAlignment
);
354 AlignedSize
= AlignedPageCount
<< PageAlignment
;
355 Status
= FatDiskIo (Volume
, IoMode
, EntryPos
, AlignedSize
, Buffer
, Task
);
356 if (EFI_ERROR (Status
)) {
361 // If these access data over laps the relative cache range, these cache pages need
364 FatFlushDataCacheRange (Volume
, IoMode
, PageNo
, OverRunPageNo
, Buffer
);
365 Buffer
+= AlignedSize
;
366 BufferSize
-= AlignedSize
;
370 // The access of the OverRun data
372 OverRun
= BufferSize
;
375 // Last read is not a complete page
377 Status
= FatAccessUnalignedCachePage (Volume
, CacheDataType
, IoMode
, OverRunPageNo
, 0, OverRun
, Buffer
);
385 Flush all the dirty cache back, include the FAT cache and the Data cache.
387 @param Volume - FAT file system volume.
388 @param Task point to task instance.
390 @retval EFI_SUCCESS - Flush all the dirty cache back successfully
391 @return other - An error occurred when writing the data into the disk
395 FatVolumeFlushCache (
396 IN FAT_VOLUME
*Volume
,
401 CACHE_DATA_TYPE CacheDataType
;
404 DISK_CACHE
*DiskCache
;
407 for (CacheDataType
= (CACHE_DATA_TYPE
)0; CacheDataType
< CacheMaxType
; CacheDataType
++) {
408 DiskCache
= &Volume
->DiskCache
[CacheDataType
];
409 if (DiskCache
->Dirty
) {
411 // Data cache or fat cache is dirty, write the dirty data back
413 GroupMask
= DiskCache
->GroupMask
;
414 for (GroupIndex
= 0; GroupIndex
<= GroupMask
; GroupIndex
++) {
415 CacheTag
= &DiskCache
->CacheTag
[GroupIndex
];
416 if ((CacheTag
->RealSize
> 0) && CacheTag
->Dirty
) {
418 // Write back all Dirty Data Cache Page to disk
420 Status
= FatExchangeCachePage (Volume
, CacheDataType
, WriteDisk
, CacheTag
, Task
);
421 if (EFI_ERROR (Status
)) {
427 DiskCache
->Dirty
= FALSE
;
432 // Flush the block device.
434 Status
= Volume
->BlockIo
->FlushBlocks (Volume
->BlockIo
);
440 Initialize the disk cache according to Volume's FatType.
442 @param Volume - FAT file system volume.
444 @retval EFI_SUCCESS - The disk cache is successfully initialized.
445 @retval EFI_OUT_OF_RESOURCES - Not enough memory to allocate disk cache.
449 FatInitializeDiskCache (
450 IN FAT_VOLUME
*Volume
453 DISK_CACHE
*DiskCache
;
454 UINTN FatCacheGroupCount
;
459 DiskCache
= Volume
->DiskCache
;
461 // Configure the parameters of disk cache
463 if (Volume
->FatType
== Fat12
) {
464 FatCacheGroupCount
= FAT_FATCACHE_GROUP_MIN_COUNT
;
465 DiskCache
[CacheFat
].PageAlignment
= FAT_FATCACHE_PAGE_MIN_ALIGNMENT
;
466 DiskCache
[CacheData
].PageAlignment
= FAT_DATACACHE_PAGE_MIN_ALIGNMENT
;
468 FatCacheGroupCount
= FAT_FATCACHE_GROUP_MAX_COUNT
;
469 DiskCache
[CacheFat
].PageAlignment
= FAT_FATCACHE_PAGE_MAX_ALIGNMENT
;
470 DiskCache
[CacheData
].PageAlignment
= FAT_DATACACHE_PAGE_MAX_ALIGNMENT
;
473 DiskCache
[CacheData
].GroupMask
= FAT_DATACACHE_GROUP_COUNT
- 1;
474 DiskCache
[CacheData
].BaseAddress
= Volume
->RootPos
;
475 DiskCache
[CacheData
].LimitAddress
= Volume
->VolumeSize
;
476 DiskCache
[CacheFat
].GroupMask
= FatCacheGroupCount
- 1;
477 DiskCache
[CacheFat
].BaseAddress
= Volume
->FatPos
;
478 DiskCache
[CacheFat
].LimitAddress
= Volume
->FatPos
+ Volume
->FatSize
;
479 FatCacheSize
= FatCacheGroupCount
<< DiskCache
[CacheFat
].PageAlignment
;
480 DataCacheSize
= FAT_DATACACHE_GROUP_COUNT
<< DiskCache
[CacheData
].PageAlignment
;
482 // Allocate the Fat Cache buffer
484 CacheBuffer
= AllocateZeroPool (FatCacheSize
+ DataCacheSize
);
485 if (CacheBuffer
== NULL
) {
486 return EFI_OUT_OF_RESOURCES
;
489 Volume
->CacheBuffer
= CacheBuffer
;
490 DiskCache
[CacheFat
].CacheBase
= CacheBuffer
;
491 DiskCache
[CacheData
].CacheBase
= CacheBuffer
+ FatCacheSize
;