2 General purpose supporting routines for FAT recovery PEIM
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "FatLitePeim.h"
13 #define CHAR_FAT_VALID 0x01
17 Converts a union code character to upper case.
18 This functions converts a unicode character to upper case.
19 If the input Letter is not a lower-cased letter,
20 the original value is returned.
22 @param Letter The input unicode character.
24 @return The upper cased letter.
32 if ('a' <= Letter
&& Letter
<= 'z') {
33 Letter
= (CHAR16
) (Letter
- 0x20);
41 Reads a block of data from the block device by calling
42 underlying Block I/O service.
44 @param PrivateData Global memory map for accessing global variables
45 @param BlockDeviceNo The index for the block device number.
46 @param Lba The logic block address to read data from.
47 @param BufferSize The size of data in byte to read.
48 @param Buffer The buffer of the
50 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
52 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
58 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
59 IN UINTN BlockDeviceNo
,
66 PEI_FAT_BLOCK_DEVICE
*BlockDev
;
68 if (BlockDeviceNo
> PEI_FAT_MAX_BLOCK_DEVICE
- 1) {
69 return EFI_DEVICE_ERROR
;
73 BlockDev
= &(PrivateData
->BlockDevice
[BlockDeviceNo
]);
75 if (BufferSize
> MultU64x32 (BlockDev
->LastBlock
- Lba
+ 1, BlockDev
->BlockSize
)) {
76 return EFI_DEVICE_ERROR
;
79 if (!BlockDev
->Logical
) {
81 // Status = BlockDev->ReadFunc
82 // (PrivateData->PeiServices, BlockDev->PhysicalDevNo, Lba, BufferSize, Buffer);
84 if (BlockDev
->BlockIo2
!= NULL
) {
85 Status
= BlockDev
->BlockIo2
->ReadBlocks (
86 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer (),
88 BlockDev
->PhysicalDevNo
,
94 Status
= BlockDev
->BlockIo
->ReadBlocks (
95 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer (),
97 BlockDev
->PhysicalDevNo
,
105 Status
= FatReadDisk (
107 BlockDev
->ParentDevNo
,
108 BlockDev
->StartingPos
+ MultU64x32 (Lba
, BlockDev
->BlockSize
),
119 Find a cache block designated to specific Block device and Lba.
120 If not found, invalidate an oldest one and use it. (LRU cache)
122 @param PrivateData the global memory map.
123 @param BlockDeviceNo the Block device.
124 @param Lba the Logical Block Address
125 @param CachePtr Ptr to the starting address of the memory holding the
128 @retval EFI_SUCCESS The function completed successfully.
129 @retval EFI_DEVICE_ERROR Something error while accessing media.
134 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
135 IN UINTN BlockDeviceNo
,
141 PEI_FAT_CACHE_BUFFER
*CacheBuffer
;
145 Status
= EFI_SUCCESS
;
149 // go through existing cache buffers
151 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
152 CacheBuffer
= &(PrivateData
->CacheBuffer
[Index
]);
153 if (CacheBuffer
->Valid
&& CacheBuffer
->BlockDeviceNo
== BlockDeviceNo
&& CacheBuffer
->Lba
== Lba
) {
158 if (Index
< PEI_FAT_CACHE_SIZE
) {
159 *CachePtr
= (CHAR8
*) CacheBuffer
->Buffer
;
163 // We have to find an invalid cache buffer
165 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
166 if (!PrivateData
->CacheBuffer
[Index
].Valid
) {
171 // Use the cache buffer
173 if (Index
== PEI_FAT_CACHE_SIZE
) {
174 Index
= (Seed
++) % PEI_FAT_CACHE_SIZE
;
178 // Current device ID should be less than maximum device ID.
180 if (BlockDeviceNo
>= PEI_FAT_MAX_BLOCK_DEVICE
) {
181 return EFI_DEVICE_ERROR
;
184 CacheBuffer
= &(PrivateData
->CacheBuffer
[Index
]);
186 CacheBuffer
->BlockDeviceNo
= BlockDeviceNo
;
187 CacheBuffer
->Lba
= Lba
;
188 CacheBuffer
->Size
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
193 Status
= FatReadBlock (
200 if (EFI_ERROR (Status
)) {
201 return EFI_DEVICE_ERROR
;
204 CacheBuffer
->Valid
= TRUE
;
205 *CachePtr
= (CHAR8
*) CacheBuffer
->Buffer
;
214 @param PrivateData the global memory map;
215 @param BlockDeviceNo the block device to read;
216 @param StartingAddress the starting address.
217 @param Size the amount of data to read.
218 @param Buffer the buffer holding the data
220 @retval EFI_SUCCESS The function completed successfully.
221 @retval EFI_DEVICE_ERROR Something error.
226 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
227 IN UINTN BlockDeviceNo
,
228 IN UINT64 StartingAddress
,
242 Status
= EFI_SUCCESS
;
244 BlockSize
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
249 Lba
= DivU64x32Remainder (StartingAddress
, BlockSize
, &Offset
);
250 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, Lba
, &CachePtr
);
251 if (EFI_ERROR (Status
)) {
252 return EFI_DEVICE_ERROR
;
255 Amount
= Size
< (BlockSize
- Offset
) ? Size
: (BlockSize
- Offset
);
256 CopyMem (BufferPtr
, CachePtr
+ Offset
, Amount
);
258 if (Size
== Amount
) {
264 StartingAddress
+= Amount
;
268 // Read aligned parts
270 OverRunLba
= Lba
+ DivU64x32Remainder (Size
, BlockSize
, &Offset
);
273 Status
= FatReadBlock (PrivateData
, BlockDeviceNo
, Lba
, Size
, BufferPtr
);
274 if (EFI_ERROR (Status
)) {
275 return EFI_DEVICE_ERROR
;
284 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, OverRunLba
, &CachePtr
);
285 if (EFI_ERROR (Status
)) {
286 return EFI_DEVICE_ERROR
;
289 CopyMem (BufferPtr
, CachePtr
, Offset
);
297 This version is different from the version in Unicode collation
298 protocol in that this version strips off trailing blanks.
299 Converts an 8.3 FAT file name using an OEM character set
300 to a Null-terminated Unicode string.
301 Here does not expand DBCS FAT chars.
303 @param FatSize The size of the string Fat in bytes.
304 @param Fat A pointer to a Null-terminated string that contains
305 an 8.3 file name using an OEM character set.
306 @param Str A pointer to a Null-terminated Unicode string. The
307 string must be allocated in advance to hold FatSize
322 // No DBCS issues, just expand and add null terminate to end of string
324 while (*Fat
!= 0 && FatSize
!= 0) {
339 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
341 @param PrivateData Global memory map for accessing global variables
342 @param Str1 First string to perform case insensitive comparison.
343 @param Str2 Second string to perform case insensitive comparison.
348 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
356 UpperS1
= ToUpper (*Str1
);
357 UpperS2
= ToUpper (*Str2
);
359 if (UpperS1
!= UpperS2
) {
365 UpperS1
= ToUpper (*Str1
);
366 UpperS2
= ToUpper (*Str2
);
369 return (BOOLEAN
) ((*Str2
!= 0) ? FALSE
: TRUE
);