cache.c
来自「Next BIOS Source code : Extensible Firmw」· C语言 代码 · 共 614 行 · 第 1/2 页
C
614 行
/*++
Copyright (c) 1999 - 2003 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
Module Name:
cache.c
Abstract:
Cache implementation for EFI FAT File system driver
Revision History
--*/
#include "fat.h"
EFI_STATUS
FatGetCacheBuffer (
IN FAT_VOLUME *Vol,
IN UINTN PageNo,
OUT CACHE_BUFFER **CacheEntry
);
EFI_STATUS
FatCacheFlushRange (
IN FAT_VOLUME *Vol,
IN UINTN StartPageNo,
IN UINTN EndPageNo
);
EFI_STATUS
FatGetCacheBuffer (
IN FAT_VOLUME *Vol,
IN UINTN PageNo,
OUT CACHE_BUFFER **CacheEntry
)
/*++
Routine Description:
Get one cache entry by specified PageNo.
If PageNo is in the cache return it in CacheEntry. If PageNo is not
in the cache allocate a new entry and read the page pointed by PageNo
into it. If the read fails return an EFI_ERROR().
1. If there is invalid entry left, a new CacheEntry will use this invalid
entry.
2. If there is no invalid entries existing, the Least Recently Used valid
entry is invalidated and returned. Before invalidating this entry, if this
entry is dirty need to write this entry back to disk first.
Arguments:
Vol - FAT file system volume.
PageNo - PageNo to match with the cache.
CacheEntry - Last PageNo to invalidate in the cache.
Returns:
EFI_SUCCESS - Return CacheEntry buffer that matches PageNo
other - An error occurred reading the data
--*/
{
EFI_STATUS Status;
UINTN Index;
UINTN MaxLru;
UINTN MinLru;
UINTN MinLruIndex;
UINTN FreeIndex;
UINTN CacheIndex;
EFI_TPL EntryTpl;
CACHE_BUFFER *Cache;
FreeIndex = (UINTN) -1;
MinLru = (UINTN) -1;
MinLruIndex = 0;
MaxLru = 0;
for (Index = 0; Index < FAT_CACHE_SIZE; Index++) {
if (Vol->Cache[Index].Valid) {
if (Vol->Cache[Index].PageNo == PageNo) {
//
// Hit in the cache
//
Vol->Cache[Index].Lru++;
*CacheEntry = &Vol->Cache[Index];
return EFI_SUCCESS;
}
if (Vol->Cache[Index].Lru > MaxLru) {
//
// Tally the MaxLru value in case we need to allocate a new entry
//
MaxLru = Vol->Cache[Index].Lru;
}
if (Vol->Cache[Index].Lru < MinLru) {
MinLru = Vol->Cache[Index].Lru;
MinLruIndex = Index;
}
} else {
FreeIndex = Index;
}
}
if (FreeIndex == -1) {
//
// Replace the Least Recently Used entry
//
CacheIndex = MinLruIndex;
} else {
//
// Fill an invalid entry
//
CacheIndex = FreeIndex;
}
//
// Mark Cache Update a critical section
//
EntryTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY);
Cache = &Vol->Cache[CacheIndex];
//
// If the selected cache entry is not from invalid entry
// (means using LRU mechanism) and it is dirty, then write back
// to disk before doing read
//
if (Cache->Valid == TRUE && Cache->Dirty == TRUE) {
//
// If write failed, set the cache as invalid
// Not returning here on error condition in case the media is readonly
//
Status = Vol->DiskIo->WriteDisk (
Vol->DiskIo,
Vol->MediaId,
DriverLibMultU64x32 (Cache->PageNo, Vol->CachePageSize),
Vol->CachePageSize,
Cache->Data
);
if (EFI_ERROR(Status)) {
Cache->Valid = FALSE;
}
}
if (Vol->Valid) {
Status = Vol->DiskIo->ReadDisk (
Vol->DiskIo,
Vol->MediaId,
DriverLibMultU64x32 (PageNo, Vol->CachePageSize),
Vol->CachePageSize,
Cache->Data
);
} else {
Status = EFI_MEDIA_CHANGED;
}
if (EFI_ERROR (Status)) {
gBS->RestoreTPL (EntryTpl);
return Status;
}
Cache->Valid = TRUE;
Cache->Dirty = FALSE;
Cache->PageNo = PageNo;
Cache->Lru = MaxLru;
*CacheEntry = Cache;
//
// Exit Cache Update critical section
//
gBS->RestoreTPL (EntryTpl);
return Status;
}
EFI_STATUS
FatCacheFlushRange (
IN FAT_VOLUME *Vol,
IN UINTN StartPageNo,
IN UINTN EndPageNo
)
/*++
Routine Description:
Flush any cache entry for StartPageNo through EndPageNo.
Make all entries in this range invalid.
Arguments:
Vol - FAT file system volume.
StartPageNo - First PageNo to invalidate in the cache.
EndPageNo - Last PageNo to invalidate in the cache.
Returns:
None
--*/
{
EFI_STATUS Status;
UINTN Index;
EFI_TPL EntryTpl;
Status = EFI_SUCCESS;
//
// Make Cache Update a critical section
//
EntryTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY);
for (Index = 0; Index < FAT_CACHE_SIZE; Index++) {
if (Vol->Cache[Index].PageNo >= StartPageNo &&
Vol->Cache[Index].PageNo <= EndPageNo) {
if (Vol->Cache[Index].Valid == TRUE) {
//
// Make all valid entries in this range invalid.
//
Vol->Cache[Index].Valid = FALSE;
}
}
}
//
// Exit Cache Update critical section
//
gBS->RestoreTPL (EntryTpl);
return Status;
}
EFI_STATUS
FatDiskIoReadVolume (
IN FAT_VOLUME *Vol,
IN UINT64 Offset,
IN UINTN BufferSize,
OUT VOID *Buffer
)
/*++
Routine Description:
Read BufferSize bytes from Offset into Buffer.
Reads may require a read cache to support reads that are not aligned on
page boundaries. There are three cases:
UnderRun - The first byte is not on a page boundary or the read request is
less than a page in length.
Aligned - A read of N contiguous pages.
OverRun - The last byte is not on a page boundary.
Arguments:
Vol - FAT file system volume.
Offset - The starting byte offset to read from.
BufferSize - Size of Buffer.
Buffer - Buffer containing read data.
Returns:
EFI_SUCCESS - The data was read correctly from the device.
EFI_DEVICE_ERROR - The device reported an error while performing the
read.
EFI_NO_MEDIA - There is no media in the device.
EFI_MEDIA_CHNAGED - The MediaId does not matched the current device.
EFI_INVALID_PARAMETER - The read request contains device addresses that are
not valid for the device.
--*/
{
EFI_STATUS Status;
UINTN PageNo;
UINTN OverRunPageNo;
UINTN UnderRun;
UINTN OverRun;
UINTN WorkingBufferSize;
UINT8 *WorkingBuffer;
UINTN Length;
CACHE_BUFFER *Cache;
if (Vol->BlkIo->Media->MediaId != Vol->MediaId) {
return EFI_MEDIA_CHANGED;
}
if (!Vol->Valid) {
return EFI_MEDIA_CHANGED;
}
WorkingBuffer = Buffer;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?