TianoCore EDK2 master
Loading...
Searching...
No Matches
FatLiteLib.c
Go to the documentation of this file.
1
10#include "FatLitePeim.h"
11
12#define CHAR_FAT_VALID 0x01
13
25CHAR16
27 IN CHAR16 Letter
28 )
29{
30 if (('a' <= Letter) && (Letter <= 'z')) {
31 Letter = (CHAR16)(Letter - 0x20);
32 }
33
34 return Letter;
35}
36
55 IN PEI_FAT_PRIVATE_DATA *PrivateData,
56 IN UINTN BlockDeviceNo,
57 IN EFI_PEI_LBA Lba,
58 IN UINTN BufferSize,
59 OUT VOID *Buffer
60 )
61{
62 EFI_STATUS Status;
63 PEI_FAT_BLOCK_DEVICE *BlockDev;
64
65 if (BlockDeviceNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) {
66 return EFI_DEVICE_ERROR;
67 }
68
69 Status = EFI_SUCCESS;
70 BlockDev = &(PrivateData->BlockDevice[BlockDeviceNo]);
71
72 if (BufferSize > MultU64x32 (BlockDev->LastBlock - Lba + 1, BlockDev->BlockSize)) {
73 return EFI_DEVICE_ERROR;
74 }
75
76 if (!BlockDev->Logical) {
77 //
78 // Status = BlockDev->ReadFunc
79 // (PrivateData->PeiServices, BlockDev->PhysicalDevNo, Lba, BufferSize, Buffer);
80 //
81 if (BlockDev->BlockIo2 != NULL) {
82 Status = BlockDev->BlockIo2->ReadBlocks (
84 BlockDev->BlockIo2,
85 BlockDev->PhysicalDevNo,
86 Lba,
87 BufferSize,
88 Buffer
89 );
90 } else {
91 Status = BlockDev->BlockIo->ReadBlocks (
93 BlockDev->BlockIo,
94 BlockDev->PhysicalDevNo,
95 Lba,
96 BufferSize,
97 Buffer
98 );
99 }
100 } else {
101 Status = FatReadDisk (
102 PrivateData,
103 BlockDev->ParentDevNo,
104 BlockDev->StartingPos + MultU64x32 (Lba, BlockDev->BlockSize),
105 BufferSize,
106 Buffer
107 );
108 }
109
110 return Status;
111}
112
129 IN PEI_FAT_PRIVATE_DATA *PrivateData,
130 IN UINTN BlockDeviceNo,
131 IN UINT64 Lba,
132 OUT CHAR8 **CachePtr
133 )
134{
135 EFI_STATUS Status;
136 PEI_FAT_CACHE_BUFFER *CacheBuffer;
137 INTN Index;
138 STATIC UINT8 Seed;
139
140 Status = EFI_SUCCESS;
141 CacheBuffer = NULL;
142
143 //
144 // go through existing cache buffers
145 //
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)) {
149 break;
150 }
151 }
152
153 if (Index < PEI_FAT_CACHE_SIZE) {
154 *CachePtr = (CHAR8 *)CacheBuffer->Buffer;
155 return EFI_SUCCESS;
156 }
157
158 //
159 // We have to find an invalid cache buffer
160 //
161 for (Index = 0; Index < PEI_FAT_CACHE_SIZE; Index++) {
162 if (!PrivateData->CacheBuffer[Index].Valid) {
163 break;
164 }
165 }
166
167 //
168 // Use the cache buffer
169 //
170 if (Index == PEI_FAT_CACHE_SIZE) {
171 Index = (Seed++) % PEI_FAT_CACHE_SIZE;
172 }
173
174 //
175 // Current device ID should be less than maximum device ID.
176 //
177 if (BlockDeviceNo >= PEI_FAT_MAX_BLOCK_DEVICE) {
178 return EFI_DEVICE_ERROR;
179 }
180
181 CacheBuffer = &(PrivateData->CacheBuffer[Index]);
182
183 CacheBuffer->BlockDeviceNo = BlockDeviceNo;
184 CacheBuffer->Lba = Lba;
185 CacheBuffer->Size = PrivateData->BlockDevice[BlockDeviceNo].BlockSize;
186
187 //
188 // Read in the data
189 //
190 Status = FatReadBlock (
191 PrivateData,
192 BlockDeviceNo,
193 Lba,
194 CacheBuffer->Size,
195 CacheBuffer->Buffer
196 );
197 if (EFI_ERROR (Status)) {
198 return EFI_DEVICE_ERROR;
199 }
200
201 CacheBuffer->Valid = TRUE;
202 *CachePtr = (CHAR8 *)CacheBuffer->Buffer;
203
204 return Status;
205}
206
222 IN PEI_FAT_PRIVATE_DATA *PrivateData,
223 IN UINTN BlockDeviceNo,
224 IN UINT64 StartingAddress,
225 IN UINTN Size,
226 OUT VOID *Buffer
227 )
228{
229 EFI_STATUS Status;
230 UINT32 BlockSize;
231 CHAR8 *BufferPtr;
232 CHAR8 *CachePtr;
233 UINT32 Offset;
234 UINT64 Lba;
235 UINT64 OverRunLba;
236 UINTN Amount;
237
238 Status = EFI_SUCCESS;
239 BufferPtr = Buffer;
240 BlockSize = PrivateData->BlockDevice[BlockDeviceNo].BlockSize;
241
242 //
243 // Read underrun
244 //
245 Lba = DivU64x32Remainder (StartingAddress, BlockSize, &Offset);
246 Status = FatGetCacheBlock (PrivateData, BlockDeviceNo, Lba, &CachePtr);
247 if (EFI_ERROR (Status)) {
248 return EFI_DEVICE_ERROR;
249 }
250
251 Amount = Size < (BlockSize - Offset) ? Size : (BlockSize - Offset);
252 CopyMem (BufferPtr, CachePtr + Offset, Amount);
253
254 if (Size == Amount) {
255 return EFI_SUCCESS;
256 }
257
258 Size -= Amount;
259 BufferPtr += Amount;
260 StartingAddress += Amount;
261 Lba += 1;
262
263 //
264 // Read aligned parts
265 //
266 OverRunLba = Lba + DivU64x32Remainder (Size, BlockSize, &Offset);
267
268 Size -= Offset;
269 Status = FatReadBlock (PrivateData, BlockDeviceNo, Lba, Size, BufferPtr);
270 if (EFI_ERROR (Status)) {
271 return EFI_DEVICE_ERROR;
272 }
273
274 BufferPtr += Size;
275
276 //
277 // Read overrun
278 //
279 if (Offset != 0) {
280 Status = FatGetCacheBlock (PrivateData, BlockDeviceNo, OverRunLba, &CachePtr);
281 if (EFI_ERROR (Status)) {
282 return EFI_DEVICE_ERROR;
283 }
284
285 CopyMem (BufferPtr, CachePtr, Offset);
286 }
287
288 return Status;
289}
290
306VOID
308 IN UINTN FatSize,
309 IN CHAR8 *Fat,
310 OUT CHAR16 *Str
311 )
312{
313 CHAR16 *String;
314
315 String = Str;
316 //
317 // No DBCS issues, just expand and add null terminate to end of string
318 //
319 while (*Fat != 0 && FatSize != 0) {
320 if (*Fat == ' ') {
321 break;
322 }
323
324 *String = *Fat;
325 String += 1;
326 Fat += 1;
327 FatSize -= 1;
328 }
329
330 *String = 0;
331}
332
341BOOLEAN
343 IN PEI_FAT_PRIVATE_DATA *PrivateData,
344 IN CHAR16 *Str1,
345 IN CHAR16 *Str2
346 )
347{
348 CHAR16 UpperS1;
349 CHAR16 UpperS2;
350
351 UpperS1 = ToUpper (*Str1);
352 UpperS2 = ToUpper (*Str2);
353 while (*Str1 != 0) {
354 if (UpperS1 != UpperS2) {
355 return FALSE;
356 }
357
358 Str1++;
359 Str2++;
360 UpperS1 = ToUpper (*Str1);
361 UpperS2 = ToUpper (*Str2);
362 }
363
364 return (BOOLEAN)((*Str2 != 0) ? FALSE : TRUE);
365}
UINT64 UINTN
INT64 INTN
CONST EFI_PEI_SERVICES **EFIAPI GetPeiServicesTablePointer(VOID)
UINT64 EFIAPI MultU64x32(IN UINT64 Multiplicand, IN UINT32 Multiplier)
Definition: MultU64x32.c:27
UINT64 EFIAPI DivU64x32Remainder(IN UINT64 Dividend, IN UINT32 Divisor, OUT UINT32 *Remainder OPTIONAL)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID EngFatToStr(IN UINTN FatSize, IN CHAR8 *Fat, OUT CHAR16 *Str)
Definition: FatLiteLib.c:307
EFI_STATUS FatReadBlock(IN PEI_FAT_PRIVATE_DATA *PrivateData, IN UINTN BlockDeviceNo, IN EFI_PEI_LBA Lba, IN UINTN BufferSize, OUT VOID *Buffer)
Definition: FatLiteLib.c:54
CHAR16 ToUpper(IN CHAR16 Letter)
Definition: FatLiteLib.c:26
EFI_STATUS FatGetCacheBlock(IN PEI_FAT_PRIVATE_DATA *PrivateData, IN UINTN BlockDeviceNo, IN UINT64 Lba, OUT CHAR8 **CachePtr)
Definition: FatLiteLib.c:128
BOOLEAN EngStriColl(IN PEI_FAT_PRIVATE_DATA *PrivateData, IN CHAR16 *Str1, IN CHAR16 *Str2)
Definition: FatLiteLib.c:342
EFI_STATUS FatReadDisk(IN PEI_FAT_PRIVATE_DATA *PrivateData, IN UINTN BlockDeviceNo, IN UINT64 StartingAddress, IN UINTN Size, OUT VOID *Buffer)
Definition: FatLiteLib.c:221
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
UINT64 EFI_PEI_LBA
Definition: BlockIo.h:41
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_PEI_READ_BLOCKS2 ReadBlocks
Definition: BlockIo2.h:212
EFI_PEI_READ_BLOCKS ReadBlocks
Definition: BlockIo.h:227