2 General purpose supporting routines for FAT recovery PEIM
4 Copyright (c) 2006 - 2013, 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 Status
= BlockDev
->BlockIo
->ReadBlocks (
91 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer (),
93 BlockDev
->PhysicalDevNo
,
100 Status
= FatReadDisk (
102 BlockDev
->ParentDevNo
,
103 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
;
158 // We have to find an invalid cache buffer
160 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
161 if (!PrivateData
->CacheBuffer
[Index
].Valid
) {
166 // Use the cache buffer
168 if (Index
== PEI_FAT_CACHE_SIZE
) {
169 Index
= (Seed
++) % PEI_FAT_CACHE_SIZE
;
173 // Current device ID should be less than maximum device ID.
175 if (BlockDeviceNo
>= PEI_FAT_MAX_BLOCK_DEVICE
) {
176 return EFI_DEVICE_ERROR
;
179 CacheBuffer
= &(PrivateData
->CacheBuffer
[Index
]);
181 CacheBuffer
->BlockDeviceNo
= BlockDeviceNo
;
182 CacheBuffer
->Lba
= Lba
;
183 CacheBuffer
->Size
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
188 Status
= FatReadBlock (
195 if (EFI_ERROR (Status
)) {
196 return EFI_DEVICE_ERROR
;
199 CacheBuffer
->Valid
= TRUE
;
200 *CachePtr
= (CHAR8
*) CacheBuffer
->Buffer
;
209 @param PrivateData the global memory map;
210 @param BlockDeviceNo the block device to read;
211 @param StartingAddress the starting address.
212 @param Size the amount of data to read.
213 @param Buffer the buffer holding the data
215 @retval EFI_SUCCESS The function completed successfully.
216 @retval EFI_DEVICE_ERROR Something error.
221 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
222 IN UINTN BlockDeviceNo
,
223 IN UINT64 StartingAddress
,
237 Status
= EFI_SUCCESS
;
239 BlockSize
= PrivateData
->BlockDevice
[BlockDeviceNo
].BlockSize
;
244 Lba
= DivU64x32Remainder (StartingAddress
, BlockSize
, &Offset
);
245 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, Lba
, &CachePtr
);
246 if (EFI_ERROR (Status
)) {
247 return EFI_DEVICE_ERROR
;
250 Amount
= Size
< (BlockSize
- Offset
) ? Size
: (BlockSize
- Offset
);
251 CopyMem (BufferPtr
, CachePtr
+ Offset
, Amount
);
253 if (Size
== Amount
) {
259 StartingAddress
+= Amount
;
263 // Read aligned parts
265 OverRunLba
= Lba
+ DivU64x32Remainder (Size
, BlockSize
, &Offset
);
268 Status
= FatReadBlock (PrivateData
, BlockDeviceNo
, Lba
, Size
, BufferPtr
);
269 if (EFI_ERROR (Status
)) {
270 return EFI_DEVICE_ERROR
;
279 Status
= FatGetCacheBlock (PrivateData
, BlockDeviceNo
, OverRunLba
, &CachePtr
);
280 if (EFI_ERROR (Status
)) {
281 return EFI_DEVICE_ERROR
;
284 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
);