2 General purpose supporting routines for FAT recovery PEIM
4 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "FatLitePeim.h"
19 #define CHAR_FAT_VALID 0x01
23 Converts a union code character to upper case.
24 This functions converts a unicode character to upper case.
25 If the input Letter is not a lower-cased letter,
26 the original value is returned.
28 @param Letter The input unicode character.
30 @return The upper cased letter.
38 if ('a' <= Letter
&& Letter
<= 'z') {
39 Letter
= (CHAR16
) (Letter
- 0x20);
47 Reads a block of data from the block device by calling
48 underlying Block I/O service.
50 @param PrivateData Global memory map for accessing global variables
51 @param BlockDeviceNo The index for the block device number.
52 @param Lba The logic block address to read data from.
53 @param BufferSize The size of data in byte to read.
54 @param Buffer The buffer of the
56 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
58 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
64 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
65 IN UINTN BlockDeviceNo
,
72 PEI_FAT_BLOCK_DEVICE
*BlockDev
;
74 if (BlockDeviceNo
> PEI_FAT_MAX_BLOCK_DEVICE
- 1) {
75 return EFI_DEVICE_ERROR
;
79 BlockDev
= &(PrivateData
->BlockDevice
[BlockDeviceNo
]);
81 if (BufferSize
> MultU64x32 (BlockDev
->LastBlock
- Lba
+ 1, BlockDev
->BlockSize
)) {
82 return EFI_DEVICE_ERROR
;
85 if (!BlockDev
->Logical
) {
87 // Status = BlockDev->ReadFunc
88 // (PrivateData->PeiServices, BlockDev->PhysicalDevNo, Lba, BufferSize, Buffer);
90 if (BlockDev
->BlockIo2
!= NULL
) {
91 Status
= BlockDev
->BlockIo2
->ReadBlocks (
92 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer (),
94 BlockDev
->PhysicalDevNo
,
100 Status
= BlockDev
->BlockIo
->ReadBlocks (
101 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer (),
103 BlockDev
->PhysicalDevNo
,
111 Status
= FatReadDisk (
113 BlockDev
->ParentDevNo
,
114 BlockDev
->StartingPos
+ MultU64x32 (Lba
, BlockDev
->BlockSize
),
125 Find a cache block designated to specific Block device and Lba.
126 If not found, invalidate an oldest one and use it. (LRU cache)
128 @param PrivateData the global memory map.
129 @param BlockDeviceNo the Block device.
130 @param Lba the Logical Block Address
131 @param CachePtr Ptr to the starting address of the memory holding the
134 @retval EFI_SUCCESS The function completed successfully.
135 @retval EFI_DEVICE_ERROR Something error while accessing media.
140 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
141 IN UINTN BlockDeviceNo
,
147 PEI_FAT_CACHE_BUFFER
*CacheBuffer
;
151 Status
= EFI_SUCCESS
;
155 // go through existing cache buffers
157 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
158 CacheBuffer
= &(PrivateData
->CacheBuffer
[Index
]);
159 if (CacheBuffer
->Valid
&& CacheBuffer
->BlockDeviceNo
== BlockDeviceNo
&& CacheBuffer
->Lba
== Lba
) {
164 if (Index
< PEI_FAT_CACHE_SIZE
) {
165 *CachePtr
= (CHAR8
*) CacheBuffer
->Buffer
;
169 // We have to find an invalid cache buffer
171 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
172 if (!PrivateData
->CacheBuffer
[Index
].Valid
) {
177 // Use the cache buffer
179 if (Index
== PEI_FAT_CACHE_SIZE
) {
180 Index
= (Seed
++) % PEI_FAT_CACHE_SIZE
;
184 // Current device ID should be less than maximum device ID.
186 if (BlockDeviceNo
>= PEI_FAT_MAX_BLOCK_DEVICE
) {
187 return EFI_DEVICE_ERROR
;
190 CacheBuffer
= &(PrivateData
->CacheBuffer
[Index
]);
192 CacheBuffer
->BlockDeviceNo
= BlockDeviceNo
;
193 CacheBuffer
->Lba
= Lba
;
194 CacheBuffer
->Size
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
199 Status
= FatReadBlock (
206 if (EFI_ERROR (Status
)) {
207 return EFI_DEVICE_ERROR
;
210 CacheBuffer
->Valid
= TRUE
;
211 *CachePtr
= (CHAR8
*) CacheBuffer
->Buffer
;
220 @param PrivateData the global memory map;
221 @param BlockDeviceNo the block device to read;
222 @param StartingAddress the starting address.
223 @param Size the amount of data to read.
224 @param Buffer the buffer holding the data
226 @retval EFI_SUCCESS The function completed successfully.
227 @retval EFI_DEVICE_ERROR Something error.
232 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
233 IN UINTN BlockDeviceNo
,
234 IN UINT64 StartingAddress
,
248 Status
= EFI_SUCCESS
;
250 BlockSize
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
255 Lba
= DivU64x32Remainder (StartingAddress
, BlockSize
, &Offset
);
256 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, Lba
, &CachePtr
);
257 if (EFI_ERROR (Status
)) {
258 return EFI_DEVICE_ERROR
;
261 Amount
= Size
< (BlockSize
- Offset
) ? Size
: (BlockSize
- Offset
);
262 CopyMem (BufferPtr
, CachePtr
+ Offset
, Amount
);
264 if (Size
== Amount
) {
270 StartingAddress
+= Amount
;
274 // Read aligned parts
276 OverRunLba
= Lba
+ DivU64x32Remainder (Size
, BlockSize
, &Offset
);
279 Status
= FatReadBlock (PrivateData
, BlockDeviceNo
, Lba
, Size
, BufferPtr
);
280 if (EFI_ERROR (Status
)) {
281 return EFI_DEVICE_ERROR
;
290 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, OverRunLba
, &CachePtr
);
291 if (EFI_ERROR (Status
)) {
292 return EFI_DEVICE_ERROR
;
295 CopyMem (BufferPtr
, CachePtr
, Offset
);
303 This version is different from the version in Unicode collation
304 protocol in that this version strips off trailing blanks.
305 Converts an 8.3 FAT file name using an OEM character set
306 to a Null-terminated Unicode string.
307 Here does not expand DBCS FAT chars.
309 @param FatSize The size of the string Fat in bytes.
310 @param Fat A pointer to a Null-terminated string that contains
311 an 8.3 file name using an OEM character set.
312 @param Str A pointer to a Null-terminated Unicode string. The
313 string must be allocated in advance to hold FatSize
328 // No DBCS issues, just expand and add null terminate to end of string
330 while (*Fat
!= 0 && FatSize
!= 0) {
345 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
347 @param PrivateData Global memory map for accessing global variables
348 @param Str1 First string to perform case insensitive comparison.
349 @param Str2 Second string to perform case insensitive comparison.
354 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
362 UpperS1
= ToUpper (*Str1
);
363 UpperS2
= ToUpper (*Str2
);
365 if (UpperS1
!= UpperS2
) {
371 UpperS1
= ToUpper (*Str1
);
372 UpperS2
= ToUpper (*Str2
);
375 return (BOOLEAN
) ((*Str2
!= 0) ? FALSE
: TRUE
);