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"
12 #define CHAR_FAT_VALID 0x01
15 Converts a union code character to upper case.
16 This functions converts a unicode character to upper case.
17 If the input Letter is not a lower-cased letter,
18 the original value is returned.
20 @param Letter The input unicode character.
22 @return The upper cased letter.
30 if (('a' <= Letter
) && (Letter
<= 'z')) {
31 Letter
= (CHAR16
)(Letter
- 0x20);
38 Reads a block of data from the block device by calling
39 underlying Block I/O service.
41 @param PrivateData Global memory map for accessing global variables
42 @param BlockDeviceNo The index for the block device number.
43 @param Lba The logic block address to read data from.
44 @param BufferSize The size of data in byte to read.
45 @param Buffer The buffer of the
47 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
49 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
55 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
56 IN UINTN BlockDeviceNo
,
63 PEI_FAT_BLOCK_DEVICE
*BlockDev
;
65 if (BlockDeviceNo
> PEI_FAT_MAX_BLOCK_DEVICE
- 1) {
66 return EFI_DEVICE_ERROR
;
70 BlockDev
= &(PrivateData
->BlockDevice
[BlockDeviceNo
]);
72 if (BufferSize
> MultU64x32 (BlockDev
->LastBlock
- Lba
+ 1, BlockDev
->BlockSize
)) {
73 return EFI_DEVICE_ERROR
;
76 if (!BlockDev
->Logical
) {
78 // Status = BlockDev->ReadFunc
79 // (PrivateData->PeiServices, BlockDev->PhysicalDevNo, Lba, BufferSize, Buffer);
81 if (BlockDev
->BlockIo2
!= NULL
) {
82 Status
= BlockDev
->BlockIo2
->ReadBlocks (
83 (EFI_PEI_SERVICES
**)GetPeiServicesTablePointer (),
85 BlockDev
->PhysicalDevNo
,
91 Status
= BlockDev
->BlockIo
->ReadBlocks (
92 (EFI_PEI_SERVICES
**)GetPeiServicesTablePointer (),
94 BlockDev
->PhysicalDevNo
,
101 Status
= FatReadDisk (
103 BlockDev
->ParentDevNo
,
104 BlockDev
->StartingPos
+ MultU64x32 (Lba
, BlockDev
->BlockSize
),
114 Find a cache block designated to specific Block device and Lba.
115 If not found, invalidate an oldest one and use it. (LRU cache)
117 @param PrivateData the global memory map.
118 @param BlockDeviceNo the Block device.
119 @param Lba the Logical Block Address
120 @param CachePtr Ptr to the starting address of the memory holding the
123 @retval EFI_SUCCESS The function completed successfully.
124 @retval EFI_DEVICE_ERROR Something error while accessing media.
129 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
130 IN UINTN BlockDeviceNo
,
136 PEI_FAT_CACHE_BUFFER
*CacheBuffer
;
140 Status
= EFI_SUCCESS
;
144 // go through existing cache buffers
146 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
147 CacheBuffer
= &(PrivateData
->CacheBuffer
[Index
]);
148 if (CacheBuffer
->Valid
&& (CacheBuffer
->BlockDeviceNo
== BlockDeviceNo
) && (CacheBuffer
->Lba
== Lba
)) {
153 if (Index
< PEI_FAT_CACHE_SIZE
) {
154 *CachePtr
= (CHAR8
*)CacheBuffer
->Buffer
;
159 // We have to find an invalid cache buffer
161 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
162 if (!PrivateData
->CacheBuffer
[Index
].Valid
) {
168 // Use the cache buffer
170 if (Index
== PEI_FAT_CACHE_SIZE
) {
171 Index
= (Seed
++) % PEI_FAT_CACHE_SIZE
;
175 // Current device ID should be less than maximum device ID.
177 if (BlockDeviceNo
>= PEI_FAT_MAX_BLOCK_DEVICE
) {
178 return EFI_DEVICE_ERROR
;
181 CacheBuffer
= &(PrivateData
->CacheBuffer
[Index
]);
183 CacheBuffer
->BlockDeviceNo
= BlockDeviceNo
;
184 CacheBuffer
->Lba
= Lba
;
185 CacheBuffer
->Size
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
190 Status
= FatReadBlock (
197 if (EFI_ERROR (Status
)) {
198 return EFI_DEVICE_ERROR
;
201 CacheBuffer
->Valid
= TRUE
;
202 *CachePtr
= (CHAR8
*)CacheBuffer
->Buffer
;
210 @param PrivateData the global memory map;
211 @param BlockDeviceNo the block device to read;
212 @param StartingAddress the starting address.
213 @param Size the amount of data to read.
214 @param Buffer the buffer holding the data
216 @retval EFI_SUCCESS The function completed successfully.
217 @retval EFI_DEVICE_ERROR Something error.
222 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
223 IN UINTN BlockDeviceNo
,
224 IN UINT64 StartingAddress
,
238 Status
= EFI_SUCCESS
;
240 BlockSize
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
245 Lba
= DivU64x32Remainder (StartingAddress
, BlockSize
, &Offset
);
246 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, Lba
, &CachePtr
);
247 if (EFI_ERROR (Status
)) {
248 return EFI_DEVICE_ERROR
;
251 Amount
= Size
< (BlockSize
- Offset
) ? Size
: (BlockSize
- Offset
);
252 CopyMem (BufferPtr
, CachePtr
+ Offset
, Amount
);
254 if (Size
== Amount
) {
260 StartingAddress
+= Amount
;
264 // Read aligned parts
266 OverRunLba
= Lba
+ DivU64x32Remainder (Size
, BlockSize
, &Offset
);
269 Status
= FatReadBlock (PrivateData
, BlockDeviceNo
, Lba
, Size
, BufferPtr
);
270 if (EFI_ERROR (Status
)) {
271 return EFI_DEVICE_ERROR
;
280 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, OverRunLba
, &CachePtr
);
281 if (EFI_ERROR (Status
)) {
282 return EFI_DEVICE_ERROR
;
285 CopyMem (BufferPtr
, CachePtr
, Offset
);
292 This version is different from the version in Unicode collation
293 protocol in that this version strips off trailing blanks.
294 Converts an 8.3 FAT file name using an OEM character set
295 to a Null-terminated Unicode string.
296 Here does not expand DBCS FAT chars.
298 @param FatSize The size of the string Fat in bytes.
299 @param Fat A pointer to a Null-terminated string that contains
300 an 8.3 file name using an OEM character set.
301 @param Str A pointer to a Null-terminated Unicode string. The
302 string must be allocated in advance to hold FatSize
317 // No DBCS issues, just expand and add null terminate to end of string
319 while (*Fat
!= 0 && FatSize
!= 0) {
334 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
336 @param PrivateData Global memory map for accessing global variables
337 @param Str1 First string to perform case insensitive comparison.
338 @param Str2 Second string to perform case insensitive comparison.
343 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
351 UpperS1
= ToUpper (*Str1
);
352 UpperS2
= ToUpper (*Str2
);
354 if (UpperS1
!= UpperS2
) {
360 UpperS1
= ToUpper (*Str1
);
361 UpperS2
= ToUpper (*Str2
);
364 return (BOOLEAN
)((*Str2
!= 0) ? FALSE
: TRUE
);