]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/FatPei/FatLiteLib.c
BaseTools/header.makefile: add "-Wno-stringop-truncation"
[mirror_edk2.git] / FatPkg / FatPei / FatLiteLib.c
CommitLineData
2f4dfa84
JJ
1/** @file\r
2 General purpose supporting routines for FAT recovery PEIM\r
3\r
8a467be1 4Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
2f4dfa84
JJ
5\r
6This program and the accompanying materials are licensed and made available\r
7under the terms and conditions of the BSD License which accompanies this\r
8distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "FatLitePeim.h"\r
17\r
18\r
19#define CHAR_FAT_VALID 0x01\r
20\r
21\r
22/**\r
23 Converts a union code character to upper case.\r
24 This functions converts a unicode character to upper case.\r
25 If the input Letter is not a lower-cased letter,\r
26 the original value is returned.\r
27\r
28 @param Letter The input unicode character. \r
29\r
30 @return The upper cased letter.\r
31\r
32**/\r
33CHAR16\r
34ToUpper (\r
35 IN CHAR16 Letter\r
36 )\r
37{\r
38 if ('a' <= Letter && Letter <= 'z') {\r
39 Letter = (CHAR16) (Letter - 0x20);\r
40 }\r
41\r
42 return Letter;\r
43}\r
44\r
45\r
46/**\r
47 Reads a block of data from the block device by calling\r
48 underlying Block I/O service.\r
49\r
50 @param PrivateData Global memory map for accessing global variables \r
51 @param BlockDeviceNo The index for the block device number. \r
52 @param Lba The logic block address to read data from. \r
53 @param BufferSize The size of data in byte to read. \r
54 @param Buffer The buffer of the \r
55\r
56 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum \r
57 device number. \r
58 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address \r
59 of the block device.\r
60\r
61**/\r
62EFI_STATUS\r
63FatReadBlock (\r
64 IN PEI_FAT_PRIVATE_DATA *PrivateData,\r
65 IN UINTN BlockDeviceNo,\r
66 IN EFI_PEI_LBA Lba,\r
67 IN UINTN BufferSize,\r
68 OUT VOID *Buffer\r
69 )\r
70{\r
71 EFI_STATUS Status;\r
72 PEI_FAT_BLOCK_DEVICE *BlockDev;\r
73\r
74 if (BlockDeviceNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) {\r
75 return EFI_DEVICE_ERROR;\r
76 }\r
77\r
78 Status = EFI_SUCCESS;\r
79 BlockDev = &(PrivateData->BlockDevice[BlockDeviceNo]);\r
80\r
81 if (BufferSize > MultU64x32 (BlockDev->LastBlock - Lba + 1, BlockDev->BlockSize)) {\r
82 return EFI_DEVICE_ERROR;\r
83 }\r
84\r
85 if (!BlockDev->Logical) {\r
86 //\r
87 // Status = BlockDev->ReadFunc\r
88 // (PrivateData->PeiServices, BlockDev->PhysicalDevNo, Lba, BufferSize, Buffer);\r
89 //\r
8a467be1
FT
90 if (BlockDev->BlockIo2 != NULL) {\r
91 Status = BlockDev->BlockIo2->ReadBlocks (\r
92 (EFI_PEI_SERVICES **) GetPeiServicesTablePointer (),\r
93 BlockDev->BlockIo2,\r
94 BlockDev->PhysicalDevNo,\r
95 Lba,\r
96 BufferSize,\r
97 Buffer\r
98 );\r
99 } else {\r
100 Status = BlockDev->BlockIo->ReadBlocks (\r
2f4dfa84
JJ
101 (EFI_PEI_SERVICES **) GetPeiServicesTablePointer (),\r
102 BlockDev->BlockIo,\r
103 BlockDev->PhysicalDevNo,\r
104 Lba,\r
105 BufferSize,\r
106 Buffer\r
107 );\r
8a467be1 108 }\r
2f4dfa84
JJ
109\r
110 } else {\r
111 Status = FatReadDisk (\r
112 PrivateData,\r
113 BlockDev->ParentDevNo,\r
114 BlockDev->StartingPos + MultU64x32 (Lba, BlockDev->BlockSize),\r
115 BufferSize,\r
116 Buffer\r
117 );\r
118 }\r
119\r
120 return Status;\r
121}\r
122\r
123\r
124/**\r
125 Find a cache block designated to specific Block device and Lba.\r
126 If not found, invalidate an oldest one and use it. (LRU cache)\r
127\r
128 @param PrivateData the global memory map. \r
129 @param BlockDeviceNo the Block device. \r
130 @param Lba the Logical Block Address \r
131 @param CachePtr Ptr to the starting address of the memory holding the \r
132 data; \r
133\r
134 @retval EFI_SUCCESS The function completed successfully.\r
135 @retval EFI_DEVICE_ERROR Something error while accessing media.\r
136\r
137**/\r
138EFI_STATUS\r
139FatGetCacheBlock (\r
140 IN PEI_FAT_PRIVATE_DATA *PrivateData,\r
141 IN UINTN BlockDeviceNo,\r
142 IN UINT64 Lba,\r
143 OUT CHAR8 **CachePtr\r
144 )\r
145{\r
146 EFI_STATUS Status;\r
147 PEI_FAT_CACHE_BUFFER *CacheBuffer;\r
148 INTN Index;\r
149 STATIC UINT8 Seed;\r
150\r
151 Status = EFI_SUCCESS;\r
152 CacheBuffer = NULL;\r
153\r
154 //\r
155 // go through existing cache buffers\r
156 //\r
157 for (Index = 0; Index < PEI_FAT_CACHE_SIZE; Index++) {\r
158 CacheBuffer = &(PrivateData->CacheBuffer[Index]);\r
159 if (CacheBuffer->Valid && CacheBuffer->BlockDeviceNo == BlockDeviceNo && CacheBuffer->Lba == Lba) {\r
160 break;\r
161 }\r
162 }\r
163\r
164 if (Index < PEI_FAT_CACHE_SIZE) {\r
165 *CachePtr = (CHAR8 *) CacheBuffer->Buffer;\r
166 return EFI_SUCCESS;\r
167 }\r
168 //\r
169 // We have to find an invalid cache buffer\r
170 //\r
171 for (Index = 0; Index < PEI_FAT_CACHE_SIZE; Index++) {\r
172 if (!PrivateData->CacheBuffer[Index].Valid) {\r
173 break;\r
174 }\r
175 }\r
176 //\r
177 // Use the cache buffer\r
178 //\r
179 if (Index == PEI_FAT_CACHE_SIZE) {\r
180 Index = (Seed++) % PEI_FAT_CACHE_SIZE;\r
181 }\r
182 \r
183 //\r
184 // Current device ID should be less than maximum device ID. \r
185 //\r
186 if (BlockDeviceNo >= PEI_FAT_MAX_BLOCK_DEVICE) {\r
187 return EFI_DEVICE_ERROR;\r
188 }\r
189\r
190 CacheBuffer = &(PrivateData->CacheBuffer[Index]);\r
191\r
192 CacheBuffer->BlockDeviceNo = BlockDeviceNo;\r
193 CacheBuffer->Lba = Lba;\r
194 CacheBuffer->Size = PrivateData->BlockDevice[BlockDeviceNo].BlockSize;\r
195\r
196 //\r
197 // Read in the data\r
198 //\r
199 Status = FatReadBlock (\r
200 PrivateData,\r
201 BlockDeviceNo,\r
202 Lba,\r
203 CacheBuffer->Size,\r
204 CacheBuffer->Buffer\r
205 );\r
206 if (EFI_ERROR (Status)) {\r
207 return EFI_DEVICE_ERROR;\r
208 }\r
209\r
210 CacheBuffer->Valid = TRUE;\r
211 *CachePtr = (CHAR8 *) CacheBuffer->Buffer;\r
212\r
213 return Status;\r
214}\r
215\r
216\r
217/**\r
218 Disk reading.\r
219\r
220 @param PrivateData the global memory map; \r
221 @param BlockDeviceNo the block device to read; \r
222 @param StartingAddress the starting address. \r
223 @param Size the amount of data to read. \r
224 @param Buffer the buffer holding the data \r
225\r
226 @retval EFI_SUCCESS The function completed successfully.\r
227 @retval EFI_DEVICE_ERROR Something error.\r
228\r
229**/\r
230EFI_STATUS\r
231FatReadDisk (\r
232 IN PEI_FAT_PRIVATE_DATA *PrivateData,\r
233 IN UINTN BlockDeviceNo,\r
234 IN UINT64 StartingAddress,\r
235 IN UINTN Size,\r
236 OUT VOID *Buffer\r
237 )\r
238{\r
239 EFI_STATUS Status;\r
240 UINT32 BlockSize;\r
241 CHAR8 *BufferPtr;\r
242 CHAR8 *CachePtr;\r
243 UINT32 Offset;\r
244 UINT64 Lba;\r
245 UINT64 OverRunLba;\r
246 UINTN Amount;\r
247\r
248 Status = EFI_SUCCESS;\r
249 BufferPtr = Buffer;\r
250 BlockSize = PrivateData->BlockDevice[BlockDeviceNo].BlockSize;\r
251\r
252 //\r
253 // Read underrun\r
254 //\r
255 Lba = DivU64x32Remainder (StartingAddress, BlockSize, &Offset);\r
256 Status = FatGetCacheBlock (PrivateData, BlockDeviceNo, Lba, &CachePtr);\r
257 if (EFI_ERROR (Status)) {\r
258 return EFI_DEVICE_ERROR;\r
259 }\r
260\r
261 Amount = Size < (BlockSize - Offset) ? Size : (BlockSize - Offset);\r
262 CopyMem (BufferPtr, CachePtr + Offset, Amount);\r
263\r
264 if (Size == Amount) {\r
265 return EFI_SUCCESS;\r
266 }\r
267\r
268 Size -= Amount;\r
269 BufferPtr += Amount;\r
270 StartingAddress += Amount;\r
271 Lba += 1;\r
272\r
273 //\r
274 // Read aligned parts\r
275 //\r
276 OverRunLba = Lba + DivU64x32Remainder (Size, BlockSize, &Offset);\r
277\r
278 Size -= Offset;\r
279 Status = FatReadBlock (PrivateData, BlockDeviceNo, Lba, Size, BufferPtr);\r
280 if (EFI_ERROR (Status)) {\r
281 return EFI_DEVICE_ERROR;\r
282 }\r
283\r
284 BufferPtr += Size;\r
285\r
286 //\r
287 // Read overrun\r
288 //\r
289 if (Offset != 0) {\r
290 Status = FatGetCacheBlock (PrivateData, BlockDeviceNo, OverRunLba, &CachePtr);\r
291 if (EFI_ERROR (Status)) {\r
292 return EFI_DEVICE_ERROR;\r
293 }\r
294\r
295 CopyMem (BufferPtr, CachePtr, Offset);\r
296 }\r
297\r
298 return Status;\r
299}\r
300\r
301\r
302/**\r
303 This version is different from the version in Unicode collation\r
304 protocol in that this version strips off trailing blanks.\r
305 Converts an 8.3 FAT file name using an OEM character set\r
306 to a Null-terminated Unicode string.\r
307 Here does not expand DBCS FAT chars.\r
308\r
309 @param FatSize The size of the string Fat in bytes. \r
310 @param Fat A pointer to a Null-terminated string that contains \r
311 an 8.3 file name using an OEM character set. \r
312 @param Str A pointer to a Null-terminated Unicode string. The \r
313 string must be allocated in advance to hold FatSize \r
314 Unicode characters\r
315\r
316**/\r
317VOID\r
318EngFatToStr (\r
319 IN UINTN FatSize,\r
320 IN CHAR8 *Fat,\r
321 OUT CHAR16 *Str\r
322 )\r
323{\r
324 CHAR16 *String;\r
325\r
326 String = Str;\r
327 //\r
328 // No DBCS issues, just expand and add null terminate to end of string\r
329 //\r
330 while (*Fat != 0 && FatSize != 0) {\r
3ba5368d
SZ
331 if (*Fat == ' ') {\r
332 break;\r
333 }\r
2f4dfa84
JJ
334 *String = *Fat;\r
335 String += 1;\r
336 Fat += 1;\r
337 FatSize -= 1;\r
2f4dfa84 338 }\r
3ba5368d
SZ
339\r
340 *String = 0;\r
2f4dfa84
JJ
341}\r
342\r
343\r
344/**\r
345 Performs a case-insensitive comparison of two Null-terminated Unicode strings.\r
346\r
347 @param PrivateData Global memory map for accessing global variables \r
348 @param Str1 First string to perform case insensitive comparison. \r
349 @param Str2 Second string to perform case insensitive comparison.\r
350\r
351**/\r
352BOOLEAN\r
353EngStriColl (\r
354 IN PEI_FAT_PRIVATE_DATA *PrivateData,\r
355 IN CHAR16 *Str1,\r
356 IN CHAR16 *Str2\r
357 )\r
358{\r
359 CHAR16 UpperS1;\r
360 CHAR16 UpperS2;\r
361\r
362 UpperS1 = ToUpper (*Str1);\r
363 UpperS2 = ToUpper (*Str2);\r
364 while (*Str1 != 0) {\r
365 if (UpperS1 != UpperS2) {\r
366 return FALSE;\r
367 }\r
368\r
369 Str1++;\r
370 Str2++;\r
371 UpperS1 = ToUpper (*Str1);\r
372 UpperS2 = ToUpper (*Str2);\r
373 }\r
374\r
375 return (BOOLEAN) ((*Str2 != 0) ? FALSE : TRUE);\r
376}\r