]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwMisc.c
Use #include "XXX.h" for module internal header files.
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / FaultTolerantWriteDxe / FtwMisc.c
CommitLineData
6cc9ca32
LG
1/** @file\r
2\r
3 Internal generic functions to support fault tolerant write.\r
d7dec593 4\r
6cc9ca32 5Copyright (c) 2006 - 2008, Intel Corporation \r
d7dec593 6All rights reserved. This program and the accompanying materials \r
7are licensed and made available under the terms and conditions of the BSD License \r
8which accompanies this distribution. 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
6cc9ca32 14**/\r
d7dec593 15\r
e87a334f 16#include "FtwLite.h"\r
d7dec593 17\r
6aab8214 18/**\r
d7dec593 19\r
20 Check whether a flash buffer is erased.\r
21\r
d7dec593 22\r
6aab8214 23 @param Polarity All 1 or all 0\r
24 @param Buffer Buffer to check\r
25 @param BufferSize Size of the buffer\r
d7dec593 26\r
6aab8214 27 @return A BOOLEAN value indicating erased or not.\r
d7dec593 28\r
6aab8214 29**/\r
30BOOLEAN\r
31IsErasedFlashBuffer (\r
32 IN BOOLEAN Polarity,\r
33 IN UINT8 *Buffer,\r
34 IN UINTN BufferSize\r
35 )\r
d7dec593 36{\r
37 UINT8 ErasedValue;\r
38 UINT8 *Ptr;\r
39\r
40 if (Polarity) {\r
41 ErasedValue = 0xFF;\r
42 } else {\r
43 ErasedValue = 0;\r
44 }\r
45\r
46 Ptr = Buffer;\r
6aab8214 47 while ((BufferSize--) != 0) {\r
d7dec593 48 if (*Ptr++ != ErasedValue) {\r
49 return FALSE;\r
50 }\r
51 }\r
52\r
53 return TRUE;\r
54}\r
55\r
6aab8214 56/**\r
57 To Erase one block. The size is FTW_BLOCK_SIZE\r
58\r
59\r
60 @param FtwLiteDevice Calling context\r
61 @param FvBlock FVB Protocol interface\r
62 @param Lba Lba of the firmware block\r
63\r
64 @retval EFI_SUCCESS Block LBA is Erased successfully\r
65 @retval Others Error occurs\r
66\r
67**/\r
d7dec593 68EFI_STATUS\r
69FtwEraseBlock (\r
70 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
71 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,\r
72 EFI_LBA Lba\r
73 )\r
d7dec593 74{\r
75 return FvBlock->EraseBlocks (\r
76 FvBlock,\r
77 Lba,\r
78 FtwLiteDevice->NumberOfSpareBlock,\r
79 EFI_LBA_LIST_TERMINATOR\r
80 );\r
81}\r
82\r
6aab8214 83/**\r
d7dec593 84\r
85 Erase spare block.\r
86\r
d7dec593 87\r
6aab8214 88 @param FtwLiteDevice Calling context\r
d7dec593 89\r
6aab8214 90 @retval EFI_SUCCESS The erase request was successfully\r
91 completed.\r
92 \r
93 @retval EFI_ACCESS_DENIED The firmware volume is in the\r
94 WriteDisabled state.\r
95 @retval EFI_DEVICE_ERROR The block device is not functioning\r
96 correctly and could not be written.\r
97 The firmware device may have been\r
98 partially erased.\r
99 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed\r
100 in the variable argument list do\r
101 not exist in the firmware volume. \r
d7dec593 102\r
d7dec593 103\r
6aab8214 104**/\r
105EFI_STATUS\r
106FtwEraseSpareBlock (\r
107 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice\r
108 )\r
d7dec593 109{\r
110 return FtwLiteDevice->FtwBackupFvb->EraseBlocks (\r
111 FtwLiteDevice->FtwBackupFvb,\r
112 FtwLiteDevice->FtwSpareLba,\r
113 FtwLiteDevice->NumberOfSpareBlock,\r
114 EFI_LBA_LIST_TERMINATOR\r
115 );\r
116}\r
117\r
6aab8214 118/**\r
119 Retrive the proper FVB protocol interface by HANDLE.\r
120\r
121\r
122 @param FvBlockHandle The handle of FVB protocol that provides services for\r
123 reading, writing, and erasing the target block.\r
124 @param FvBlock The interface of FVB protocol\r
125\r
126 @retval EFI_SUCCESS The function completed successfully\r
127 @retval EFI_ABORTED The function could not complete successfully\r
128\r
129**/\r
d7dec593 130EFI_STATUS\r
131FtwGetFvbByHandle (\r
132 IN EFI_HANDLE FvBlockHandle,\r
133 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
134 )\r
d7dec593 135{\r
136 //\r
137 // To get the FVB protocol interface on the handle\r
138 //\r
139 return gBS->HandleProtocol (\r
140 FvBlockHandle,\r
141 &gEfiFirmwareVolumeBlockProtocolGuid,\r
142 (VOID **) FvBlock\r
143 );\r
144}\r
145\r
6aab8214 146/**\r
d7dec593 147\r
148 Get firmware block by address.\r
149\r
d7dec593 150\r
6aab8214 151 @param Address Address specified the block\r
152 @param FvBlock The block caller wanted\r
d7dec593 153\r
6aab8214 154 @retval EFI_SUCCESS The protocol instance if found.\r
155 @retval EFI_NOT_FOUND Block not found\r
d7dec593 156\r
6aab8214 157**/\r
158EFI_STATUS\r
159GetFvbByAddress (\r
160 IN EFI_PHYSICAL_ADDRESS Address,\r
161 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
162 )\r
d7dec593 163{\r
164 EFI_STATUS Status;\r
165 EFI_HANDLE *HandleBuffer;\r
166 UINTN HandleCount;\r
167 UINTN Index;\r
168 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
169 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
170 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
171\r
172 *FvBlock = NULL;\r
173 //\r
174 // Locate all handles of Fvb protocol\r
175 //\r
176 Status = gBS->LocateHandleBuffer (\r
177 ByProtocol,\r
178 &gEfiFirmwareVolumeBlockProtocolGuid,\r
179 NULL,\r
180 &HandleCount,\r
181 &HandleBuffer\r
182 );\r
183 if (EFI_ERROR (Status)) {\r
184 return EFI_NOT_FOUND;\r
185 }\r
186 //\r
187 // Search all FVB until find the right one\r
188 //\r
189 for (Index = 0; Index < HandleCount; Index += 1) {\r
190 Status = gBS->HandleProtocol (\r
191 HandleBuffer[Index],\r
192 &gEfiFirmwareVolumeBlockProtocolGuid,\r
193 (VOID **) &Fvb\r
194 );\r
195 if (EFI_ERROR (Status)) {\r
196 Status = EFI_NOT_FOUND;\r
197 break;\r
198 }\r
199 //\r
200 // Compare the address and select the right one\r
201 //\r
202 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
203 if (EFI_ERROR (Status)) {\r
204 continue;\r
205 }\r
206\r
207 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
208 if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + (FwVolHeader->FvLength - 1)))) {\r
209 *FvBlock = Fvb;\r
210 Status = EFI_SUCCESS;\r
211 break;\r
212 }\r
213 }\r
214\r
215 FreePool (HandleBuffer);\r
216 return Status;\r
217}\r
218\r
6aab8214 219/**\r
d7dec593 220\r
221 Is it in working block?\r
222\r
d7dec593 223\r
6aab8214 224 @param FtwLiteDevice Calling context\r
225 @param FvBlock Fvb protocol instance\r
226 @param Lba The block specified\r
d7dec593 227\r
6aab8214 228 @return A BOOLEAN value indicating in working block or not.\r
d7dec593 229\r
6aab8214 230**/\r
231BOOLEAN\r
232IsInWorkingBlock (\r
233 EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
234 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,\r
235 EFI_LBA Lba\r
236 )\r
d7dec593 237{\r
238 //\r
239 // If matching the following condition, the target block is in working block.\r
240 // 1. Target block is on the FV of working block (Using the same FVB protocol instance).\r
241 // 2. Lba falls into the range of working block.\r
242 //\r
243 return (BOOLEAN)\r
244 (\r
245 (FvBlock == FtwLiteDevice->FtwFvBlock) &&\r
246 (Lba >= FtwLiteDevice->FtwWorkBlockLba) &&\r
247 (Lba <= FtwLiteDevice->FtwWorkSpaceLba)\r
248 );\r
249}\r
250\r
6aab8214 251/**\r
252 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.\r
253 Spare block is accessed by FTW backup FVB protocol interface. LBA is\r
254 FtwLiteDevice->FtwSpareLba.\r
255 Target block is accessed by FvBlock protocol interface. LBA is Lba.\r
256\r
257\r
258 @param FtwLiteDevice The private data of FTW_LITE driver\r
259 @param FvBlock FVB Protocol interface to access target block\r
260 @param Lba Lba of the target block\r
261\r
262 @retval EFI_SUCCESS Spare block content is copied to target block\r
263 @retval EFI_INVALID_PARAMETER Input parameter error\r
264 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
265 @retval EFI_ABORTED The function could not complete successfully\r
266\r
267**/\r
d7dec593 268EFI_STATUS\r
269FlushSpareBlockToTargetBlock (\r
270 EFI_FTW_LITE_DEVICE *FtwLiteDevice,\r
271 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,\r
272 EFI_LBA Lba\r
273 )\r
d7dec593 274{\r
275 EFI_STATUS Status;\r
276 UINTN Length;\r
277 UINT8 *Buffer;\r
278 UINTN Count;\r
279 UINT8 *Ptr;\r
280 UINTN Index;\r
281\r
282 if ((FtwLiteDevice == NULL) || (FvBlock == NULL)) {\r
283 return EFI_INVALID_PARAMETER;\r
284 }\r
285 //\r
286 // Allocate a memory buffer\r
287 //\r
288 Length = FtwLiteDevice->SpareAreaLength;\r
289 Buffer = AllocatePool (Length);\r
290 if (Buffer == NULL) {\r
291 return EFI_OUT_OF_RESOURCES;\r
292 }\r
293 //\r
294 // Read all content of spare block to memory buffer\r
295 //\r
296 Ptr = Buffer;\r
297 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
298 Count = FtwLiteDevice->SizeOfSpareBlock;\r
299 Status = FtwLiteDevice->FtwBackupFvb->Read (\r
300 FtwLiteDevice->FtwBackupFvb,\r
301 FtwLiteDevice->FtwSpareLba + Index,\r
302 0,\r
303 &Count,\r
304 Ptr\r
305 );\r
306 if (EFI_ERROR (Status)) {\r
307 FreePool (Buffer);\r
308 return Status;\r
309 }\r
310\r
311 Ptr += Count;\r
312 }\r
313 //\r
314 // Erase the target block\r
315 //\r
316 Status = FtwEraseBlock (FtwLiteDevice, FvBlock, Lba);\r
317 if (EFI_ERROR (Status)) {\r
318 FreePool (Buffer);\r
319 return EFI_ABORTED;\r
320 }\r
321 //\r
322 // Write memory buffer to block, using the FvbBlock protocol interface\r
323 //\r
324 Ptr = Buffer;\r
325 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
326 Count = FtwLiteDevice->SizeOfSpareBlock;\r
327 Status = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);\r
328 if (EFI_ERROR (Status)) {\r
329 DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write block - %r\n", Status));\r
330 FreePool (Buffer);\r
331 return Status;\r
332 }\r
333\r
334 Ptr += Count;\r
335 }\r
336\r
337 FreePool (Buffer);\r
338\r
339 return Status;\r
340}\r
341\r
6aab8214 342/**\r
343 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.\r
344 Spare block is accessed by FTW backup FVB protocol interface. LBA is\r
345 FtwLiteDevice->FtwSpareLba.\r
346 Working block is accessed by FTW working FVB protocol interface. LBA is\r
347 FtwLiteDevice->FtwWorkBlockLba.\r
d7dec593 348\r
d7dec593 349\r
6aab8214 350 @param FtwLiteDevice The private data of FTW_LITE driver\r
d7dec593 351\r
6aab8214 352 @retval EFI_SUCCESS Spare block content is copied to target block\r
353 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
354 @retval EFI_ABORTED The function could not complete successfully\r
355 Notes:\r
356 Since the working block header is important when FTW initializes, the\r
357 state of the operation should be handled carefully. The Crc value is\r
358 calculated without STATE element.\r
d7dec593 359\r
6aab8214 360**/\r
361EFI_STATUS\r
362FlushSpareBlockToWorkingBlock (\r
363 EFI_FTW_LITE_DEVICE *FtwLiteDevice\r
364 )\r
d7dec593 365{\r
366 EFI_STATUS Status;\r
367 UINTN Length;\r
368 UINT8 *Buffer;\r
369 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;\r
370 EFI_LBA WorkSpaceLbaOffset;\r
371 UINTN Count;\r
372 UINT8 *Ptr;\r
373 UINTN Index;\r
374\r
375 //\r
376 // Allocate a memory buffer\r
377 //\r
378 Length = FtwLiteDevice->SpareAreaLength;\r
379 Buffer = AllocatePool (Length);\r
380 if (Buffer == NULL) {\r
381 return EFI_OUT_OF_RESOURCES;\r
382 }\r
383 //\r
384 // To guarantee that the WorkingBlockValid is set on spare block\r
385 //\r
386 WorkSpaceLbaOffset = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba;\r
387 FtwUpdateFvState (\r
388 FtwLiteDevice->FtwBackupFvb,\r
389 FtwLiteDevice->FtwSpareLba + WorkSpaceLbaOffset,\r
390 FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
391 WORKING_BLOCK_VALID\r
392 );\r
393 //\r
394 // Read from spare block to memory buffer\r
395 //\r
396 Ptr = Buffer;\r
397 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
398 Count = FtwLiteDevice->SizeOfSpareBlock;\r
399 Status = FtwLiteDevice->FtwBackupFvb->Read (\r
400 FtwLiteDevice->FtwBackupFvb,\r
401 FtwLiteDevice->FtwSpareLba + Index,\r
402 0,\r
403 &Count,\r
404 Ptr\r
405 );\r
406 if (EFI_ERROR (Status)) {\r
407 FreePool (Buffer);\r
408 return Status;\r
409 }\r
410\r
411 Ptr += Count;\r
412 }\r
413 //\r
414 // Clear the CRC and STATE, copy data from spare to working block.\r
415 //\r
416 WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) WorkSpaceLbaOffset * FtwLiteDevice->SizeOfSpareBlock + FtwLiteDevice->FtwWorkSpaceBase);\r
417 InitWorkSpaceHeader (WorkingBlockHeader);\r
418 WorkingBlockHeader->WorkingBlockValid = FTW_ERASE_POLARITY;\r
419 WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;\r
420\r
421 //\r
422 // target block is working block, then\r
423 // Set WorkingBlockInvalid in EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER\r
424 // before erase the working block.\r
425 //\r
426 // Offset = EFI_FIELD_OFFSET(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
427 // WorkingBlockInvalid);\r
428 // To skip Signature and Crc: sizeof(EFI_GUID)+sizeof(UINT32).\r
429 //\r
430 Status = FtwUpdateFvState (\r
431 FtwLiteDevice->FtwFvBlock,\r
432 FtwLiteDevice->FtwWorkSpaceLba,\r
433 FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
434 WORKING_BLOCK_INVALID\r
435 );\r
436 if (EFI_ERROR (Status)) {\r
437 FreePool (Buffer);\r
438 return EFI_ABORTED;\r
439 }\r
440\r
441 FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_VALID_STATE;\r
442\r
443 //\r
444 // Erase the working block\r
445 //\r
446 Status = FtwEraseBlock (\r
447 FtwLiteDevice,\r
448 FtwLiteDevice->FtwFvBlock,\r
449 FtwLiteDevice->FtwWorkBlockLba\r
450 );\r
451 if (EFI_ERROR (Status)) {\r
452 FreePool (Buffer);\r
453 return EFI_ABORTED;\r
454 }\r
455 //\r
456 // Write memory buffer to working block, using the FvbBlock protocol interface\r
457 //\r
458 Ptr = Buffer;\r
459 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
460 Count = FtwLiteDevice->SizeOfSpareBlock;\r
461 Status = FtwLiteDevice->FtwFvBlock->Write (\r
462 FtwLiteDevice->FtwFvBlock,\r
463 FtwLiteDevice->FtwWorkBlockLba + Index,\r
464 0,\r
465 &Count,\r
466 Ptr\r
467 );\r
468 if (EFI_ERROR (Status)) {\r
469 DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write block - %r\n", Status));\r
470 FreePool (Buffer);\r
471 return Status;\r
472 }\r
473\r
474 Ptr += Count;\r
475 }\r
476 //\r
477 // Since the memory buffer will not be used, free memory Buffer.\r
478 //\r
479 FreePool (Buffer);\r
480\r
481 //\r
482 // Update the VALID of the working block\r
483 //\r
484 // Offset = EFI_FIELD_OFFSET(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
485 // WorkingBlockValid);\r
486 // Hardcode offset sizeof(EFI_GUID)+sizeof(UINT32), to skip Signature and Crc\r
487 //\r
488 Status = FtwUpdateFvState (\r
489 FtwLiteDevice->FtwFvBlock,\r
490 FtwLiteDevice->FtwWorkSpaceLba,\r
491 FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
492 WORKING_BLOCK_VALID\r
493 );\r
494 if (EFI_ERROR (Status)) {\r
495 return EFI_ABORTED;\r
496 }\r
497\r
498 FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockValid = FTW_VALID_STATE;\r
499\r
500 return EFI_SUCCESS;\r
501}\r