]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
Libraries and utilities for instrumenting regions of code and measuring their perform...
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FaultTolerantWrite.c
CommitLineData
85e923a5
LG
1/** @file\r
2\r
3 This is a simple fault tolerant write driver.\r
4\r
5 This boot service protocol only provides fault tolerant write capability for \r
6 block devices. The protocol has internal non-volatile intermediate storage \r
7 of the data and private information. It should be able to recover \r
8 automatically from a critical fault, such as power failure. \r
9\r
10 The implementation uses an FTW (Fault Tolerant Write) Work Space. \r
11 This work space is a memory copy of the work space on the Working Block,\r
12 the size of the work space is the FTW_WORK_SPACE_SIZE bytes.\r
13 \r
14 The work space stores each write record as EFI_FTW_RECORD structure.\r
15 The spare block stores the write buffer before write to the target block.\r
16 \r
17 The write record has three states to specify the different phase of write operation.\r
18 1) WRITE_ALLOCATED is that the record is allocated in write space.\r
19 The information of write operation is stored in write record structure.\r
20 2) SPARE_COMPLETED is that the data from write buffer is writed into the spare block as the backup.\r
21 3) WRITE_COMPLETED is that the data is copied from the spare block to the target block.\r
22\r
23 This driver operates the data as the whole size of spare block.\r
24 It first read the SpareAreaLength data from the target block into the spare memory buffer.\r
25 Then copy the write buffer data into the spare memory buffer.\r
26 Then write the spare memory buffer into the spare block.\r
27 Final copy the data from the spare block to the target block.\r
28\r
29 To make this drive work well, the following conditions must be satisfied:\r
30 1. The write NumBytes data must be fit within Spare area. \r
31 Offset + NumBytes <= SpareAreaLength\r
32 2. The whole flash range has the same block size.\r
33 3. Working block is an area which contains working space in its last block and has the same size as spare block.\r
34 4. Working Block area must be in the single one Firmware Volume Block range which FVB protocol is produced on. \r
35 5. Spare area must be in the single one Firmware Volume Block range which FVB protocol is produced on.\r
36 6. Any write data area (SpareAreaLength Area) which the data will be written into must be \r
37 in the single one Firmware Volume Block range which FVB protocol is produced on.\r
38 7. If write data area (such as Variable range) is enlarged, the spare area range must be enlarged.\r
39 The spare area must be enough large to store the write data before write them into the target range.\r
40 If one of them is not satisfied, FtwWrite may fail.\r
41 Usually, Spare area only takes one block. That's SpareAreaLength = BlockSize, NumberOfSpareBlock = 1.\r
42\r
d2fbaaab 43Copyright (c) 2006 - 2010, Intel Corporation \r
85e923a5
LG
44All rights reserved. This program and the accompanying materials \r
45are licensed and made available under the terms and conditions of the BSD License \r
46which accompanies this distribution. The full text of the license may be found at \r
47http://opensource.org/licenses/bsd-license.php \r
48 \r
49THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
50WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
51\r
52**/\r
53\r
54#include "FaultTolerantWrite.h"\r
55\r
93367605 56EFI_EVENT mFvbRegistration = NULL;\r
85e923a5
LG
57\r
58//\r
59// Fault Tolerant Write Protocol API\r
60//\r
61/**\r
62 Query the largest block that may be updated in a fault tolerant manner.\r
63\r
64\r
65 @param This The pointer to this protocol instance. \r
66 @param BlockSize A pointer to a caller allocated UINTN that is updated to\r
67 indicate the size of the largest block that can be updated.\r
68\r
69 @return EFI_SUCCESS The function completed successfully\r
70\r
71**/\r
72EFI_STATUS\r
73EFIAPI\r
74FtwGetMaxBlockSize (\r
75 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r
76 OUT UINTN *BlockSize\r
77 )\r
78{\r
79 EFI_FTW_DEVICE *FtwDevice;\r
80\r
81 if (!FeaturePcdGet(PcdFullFtwServiceEnable)) {\r
82 return EFI_UNSUPPORTED;\r
83 }\r
84\r
85 FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
86\r
87 *BlockSize = FtwDevice->SpareAreaLength;\r
88\r
89 return EFI_SUCCESS;\r
90}\r
91\r
92/**\r
93 Allocates space for the protocol to maintain information about writes.\r
94 Since writes must be completed in a fault tolerant manner and multiple\r
95 updates will require more resources to be successful, this function\r
96 enables the protocol to ensure that enough space exists to track\r
97 information about the upcoming writes.\r
98\r
99 All writes must be completed or aborted before another fault tolerant write can occur.\r
100\r
101 @param This The pointer to this protocol instance. \r
102 @param CallerId The GUID identifying the write.\r
103 @param PrivateDataSize The size of the caller's private data\r
104 that must be recorded for each write.\r
105 @param NumberOfWrites The number of fault tolerant block writes\r
106 that will need to occur.\r
107\r
108 @return EFI_SUCCESS The function completed successfully\r
109 @retval EFI_ABORTED The function could not complete successfully.\r
110 @retval EFI_ACCESS_DENIED All allocated writes have not been completed.\r
111\r
112**/\r
113EFI_STATUS\r
114EFIAPI\r
115FtwAllocate (\r
116 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r
117 IN EFI_GUID *CallerId,\r
118 IN UINTN PrivateDataSize,\r
119 IN UINTN NumberOfWrites\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
123 UINTN Length;\r
124 UINTN Offset;\r
125 EFI_FTW_DEVICE *FtwDevice;\r
126 EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;\r
127\r
128 FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
129\r
130 Status = WorkSpaceRefresh (FtwDevice);\r
131 if (EFI_ERROR (Status)) {\r
132 return EFI_ABORTED;\r
133 }\r
134 //\r
135 // Check if there is enough space for the coming allocation\r
136 //\r
137 if (WRITE_TOTAL_SIZE (NumberOfWrites, PrivateDataSize) > FtwDevice->FtwWorkSpaceHeader->WriteQueueSize) {\r
138 DEBUG ((EFI_D_ERROR, "Ftw: Allocate() request exceed Workspace, Caller: %g\n", CallerId));\r
139 return EFI_BUFFER_TOO_SMALL;\r
140 }\r
141 //\r
142 // Find the last write header and record.\r
143 // If the FtwHeader is complete, skip the completed last write header/records\r
144 //\r
145 FtwHeader = FtwDevice->FtwLastWriteHeader;\r
146\r
147 //\r
148 // Previous write has not completed, access denied.\r
149 //\r
150 if ((FtwHeader->HeaderAllocated == FTW_VALID_STATE) || (FtwHeader->WritesAllocated == FTW_VALID_STATE)) {\r
151 return EFI_ACCESS_DENIED;\r
152 }\r
153 //\r
154 // If workspace is not enough, then reclaim workspace\r
155 //\r
156 Offset = (UINT8 *) FtwHeader - (UINT8 *) FtwDevice->FtwWorkSpace;\r
157 if (Offset + WRITE_TOTAL_SIZE (NumberOfWrites, PrivateDataSize) > FtwDevice->FtwWorkSpaceSize) {\r
158 Status = FtwReclaimWorkSpace (FtwDevice, TRUE);\r
159 if (EFI_ERROR (Status)) {\r
160 return EFI_ABORTED;\r
161 }\r
162\r
163 FtwHeader = FtwDevice->FtwLastWriteHeader;\r
164 }\r
165 //\r
166 // Prepare FTW write header,\r
167 // overwrite the buffer and write to workspace.\r
168 //\r
169 FtwHeader->WritesAllocated = FTW_INVALID_STATE;\r
170 FtwHeader->Complete = FTW_INVALID_STATE;\r
171 CopyMem (&FtwHeader->CallerId, CallerId, sizeof (EFI_GUID));\r
172 FtwHeader->NumberOfWrites = NumberOfWrites;\r
173 FtwHeader->PrivateDataSize = PrivateDataSize;\r
174 FtwHeader->HeaderAllocated = FTW_VALID_STATE;\r
175\r
176 Length = sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER);\r
177 Status = FtwDevice->FtwFvBlock->Write (\r
178 FtwDevice->FtwFvBlock,\r
179 FtwDevice->FtwWorkSpaceLba,\r
180 FtwDevice->FtwWorkSpaceBase + Offset,\r
181 &Length,\r
182 (UINT8 *) FtwHeader\r
183 );\r
184 if (EFI_ERROR (Status)) {\r
185 return EFI_ABORTED;\r
186 }\r
187 //\r
188 // Update Header->WriteAllocated as VALID\r
189 //\r
190 Status = FtwUpdateFvState (\r
191 FtwDevice->FtwFvBlock,\r
192 FtwDevice->FtwWorkSpaceLba,\r
193 FtwDevice->FtwWorkSpaceBase + Offset,\r
194 WRITES_ALLOCATED\r
195 );\r
196 if (EFI_ERROR (Status)) {\r
197 return EFI_ABORTED;\r
198 }\r
199\r
200 DEBUG (\r
201 (EFI_D_ERROR,\r
202 "Ftw: Allocate() success, Caller:%g, # %d\n",\r
203 CallerId,\r
204 NumberOfWrites)\r
205 );\r
206\r
207 return EFI_SUCCESS;\r
208}\r
209\r
210\r
211/**\r
212 Write a record with fault tolerant mannaer.\r
213 Since the content has already backuped in spare block, the write is\r
214 guaranteed to be completed with fault tolerant manner.\r
215\r
216 @param This The pointer to this protocol instance. \r
217 @param Fvb The FVB protocol that provides services for\r
218 reading, writing, and erasing the target block.\r
219\r
220 @retval EFI_SUCCESS The function completed successfully\r
221 @retval EFI_ABORTED The function could not complete successfully\r
222\r
223**/\r
224EFI_STATUS\r
225FtwWriteRecord (\r
226 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r
227 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb\r
228 )\r
229{\r
230 EFI_STATUS Status;\r
231 EFI_FTW_DEVICE *FtwDevice;\r
232 EFI_FAULT_TOLERANT_WRITE_HEADER *Header;\r
233 EFI_FAULT_TOLERANT_WRITE_RECORD *Record;\r
234 UINTN Offset;\r
235\r
236 FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
237\r
238 //\r
239 // Spare Complete but Destination not complete,\r
240 // Recover the targt block with the spare block.\r
241 //\r
242 Header = FtwDevice->FtwLastWriteHeader;\r
243 Record = FtwDevice->FtwLastWriteRecord;\r
244\r
245 //\r
246 // IF target block is working block, THEN Flush Spare Block To Working Block;\r
247 // ELSE flush spare block to target block, which may be boot block after all.\r
248 //\r
249 if (IsWorkingBlock (FtwDevice, Fvb, Record->Lba)) {\r
250 //\r
251 // If target block is working block,\r
252 // it also need to set SPARE_COMPLETED to spare block.\r
253 //\r
254 Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;\r
255 Status = FtwUpdateFvState (\r
256 FtwDevice->FtwBackupFvb,\r
257 FtwDevice->FtwWorkSpaceLba,\r
258 FtwDevice->FtwWorkSpaceBase + Offset,\r
259 SPARE_COMPLETED\r
260 );\r
261 if (EFI_ERROR (Status)) {\r
262 return EFI_ABORTED;\r
263 }\r
264\r
265 Status = FlushSpareBlockToWorkingBlock (FtwDevice);\r
266 } else if (IsBootBlock (FtwDevice, Fvb, Record->Lba)) {\r
267 //\r
268 // Update boot block\r
269 //\r
270 Status = FlushSpareBlockToBootBlock (FtwDevice);\r
271 } else {\r
272 //\r
273 // Update blocks other than working block or boot block\r
274 //\r
275 Status = FlushSpareBlockToTargetBlock (FtwDevice, Fvb, Record->Lba);\r
276 }\r
277\r
278 if (EFI_ERROR (Status)) {\r
279 return EFI_ABORTED;\r
280 }\r
281 //\r
282 // Record the DestionationComplete in record\r
283 //\r
284 Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;\r
285 Status = FtwUpdateFvState (\r
286 FtwDevice->FtwFvBlock,\r
287 FtwDevice->FtwWorkSpaceLba,\r
288 FtwDevice->FtwWorkSpaceBase + Offset,\r
289 DEST_COMPLETED\r
290 );\r
291 if (EFI_ERROR (Status)) {\r
292 return EFI_ABORTED;\r
293 }\r
294\r
295 Record->DestinationComplete = FTW_VALID_STATE;\r
296\r
297 //\r
298 // If this is the last Write in these write sequence,\r
299 // set the complete flag of write header.\r
300 //\r
301 if (IsLastRecordOfWrites (Header, Record)) {\r
302 Offset = (UINT8 *) Header - FtwDevice->FtwWorkSpace;\r
303 Status = FtwUpdateFvState (\r
304 FtwDevice->FtwFvBlock,\r
305 FtwDevice->FtwWorkSpaceLba,\r
306 FtwDevice->FtwWorkSpaceBase + Offset,\r
307 WRITES_COMPLETED\r
308 );\r
309 Header->Complete = FTW_VALID_STATE;\r
310 if (EFI_ERROR (Status)) {\r
311 return EFI_ABORTED;\r
312 }\r
313 }\r
314\r
315 return EFI_SUCCESS;\r
316}\r
317\r
318/**\r
319 Starts a target block update. This function will record data about write\r
320 in fault tolerant storage and will complete the write in a recoverable\r
321 manner, ensuring at all times that either the original contents or\r
322 the modified contents are available.\r
323\r
324 @param This The pointer to this protocol instance. \r
325 @param Lba The logical block address of the target block.\r
326 @param Offset The offset within the target block to place the data.\r
327 @param Length The number of bytes to write to the target block.\r
328 @param PrivateData A pointer to private data that the caller requires to\r
329 complete any pending writes in the event of a fault.\r
330 @param FvBlockHandle The handle of FVB protocol that provides services for\r
331 reading, writing, and erasing the target block.\r
332 @param Buffer The data to write.\r
333\r
334 @retval EFI_SUCCESS The function completed successfully \r
335 @retval EFI_ABORTED The function could not complete successfully. \r
336 @retval EFI_BAD_BUFFER_SIZE The input data can't fit within the spare block. \r
337 Offset + *NumBytes > SpareAreaLength.\r
338 @retval EFI_ACCESS_DENIED No writes have been allocated. \r
339 @retval EFI_OUT_OF_RESOURCES Cannot allocate enough memory resource.\r
340 @retval EFI_NOT_FOUND Cannot find FVB protocol by handle.\r
341\r
342**/\r
343EFI_STATUS\r
344EFIAPI\r
345FtwWrite (\r
346 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r
347 IN EFI_LBA Lba,\r
348 IN UINTN Offset,\r
349 IN UINTN Length,\r
350 IN VOID *PrivateData,\r
351 IN EFI_HANDLE FvBlockHandle,\r
352 IN VOID *Buffer\r
353 )\r
354{\r
355 EFI_STATUS Status;\r
356 EFI_FTW_DEVICE *FtwDevice;\r
357 EFI_FAULT_TOLERANT_WRITE_HEADER *Header;\r
358 EFI_FAULT_TOLERANT_WRITE_RECORD *Record;\r
359 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
360 UINTN MyLength;\r
361 UINTN MyOffset;\r
362 UINTN MyBufferSize;\r
363 UINT8 *MyBuffer;\r
364 UINTN SpareBufferSize;\r
365 UINT8 *SpareBuffer;\r
366 UINTN Index;\r
367 UINT8 *Ptr;\r
368 EFI_PHYSICAL_ADDRESS FvbPhysicalAddress;\r
369\r
370 FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
371\r
372 Status = WorkSpaceRefresh (FtwDevice);\r
373 if (EFI_ERROR (Status)) {\r
374 return EFI_ABORTED;\r
375 }\r
376\r
377 Header = FtwDevice->FtwLastWriteHeader;\r
378 Record = FtwDevice->FtwLastWriteRecord;\r
379 \r
380 if (IsErasedFlashBuffer ((UINT8 *) Header, sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER))) {\r
381 if (PrivateData == NULL) {\r
382 //\r
383 // Ftw Write Header is not allocated.\r
384 // No additional private data, the private data size is zero. Number of record can be set to 1.\r
385 //\r
386 Status = FtwAllocate (This, &gEfiCallerIdGuid, 0, 1);\r
387 if (EFI_ERROR (Status)) {\r
388 return Status;\r
389 }\r
390 } else {\r
391 //\r
392 // Ftw Write Header is not allocated\r
393 // Additional private data is not NULL, the private data size can't be determined.\r
394 //\r
395 DEBUG ((EFI_D_ERROR, "Ftw: no allocates space for write record!\n"));\r
396 DEBUG ((EFI_D_ERROR, "Ftw: Allocate service should be called before Write service!\n"));\r
397 return EFI_NOT_READY;\r
398 }\r
399 }\r
400\r
401 //\r
402 // If Record is out of the range of Header, return access denied.\r
403 //\r
404 if (((UINTN)((UINT8 *) Record - (UINT8 *) Header)) > WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {\r
405 return EFI_ACCESS_DENIED;\r
406 }\r
407\r
408 //\r
409 // Check the COMPLETE flag of last write header\r
410 //\r
411 if (Header->Complete == FTW_VALID_STATE) {\r
412 return EFI_ACCESS_DENIED;\r
413 }\r
414\r
415 if (Record->DestinationComplete == FTW_VALID_STATE) {\r
416 return EFI_ACCESS_DENIED;\r
417 }\r
418\r
419 if ((Record->SpareComplete == FTW_VALID_STATE) && (Record->DestinationComplete != FTW_VALID_STATE)) {\r
420 return EFI_NOT_READY;\r
421 }\r
422 //\r
423 // Check if the input data can fit within the target block\r
424 //\r
425 if ((Offset + Length) > FtwDevice->SpareAreaLength) {\r
426 return EFI_BAD_BUFFER_SIZE;\r
427 }\r
428 //\r
429 // Get the FVB protocol by handle\r
430 //\r
431 Status = FtwGetFvbByHandle (FvBlockHandle, &Fvb);\r
432 if (EFI_ERROR (Status)) {\r
433 return EFI_NOT_FOUND;\r
434 }\r
435\r
436 Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress);\r
437 if (EFI_ERROR (Status)) {\r
438 DEBUG ((EFI_D_ERROR, "FtwLite: Get FVB physical address - %r\n", Status));\r
439 return EFI_ABORTED;\r
440 }\r
441\r
442 //\r
443 // Set BootBlockUpdate FLAG if it's updating boot block.\r
444 //\r
445 if (IsBootBlock (FtwDevice, Fvb, Lba)) {\r
446 Record->BootBlockUpdate = FTW_VALID_STATE;\r
447 }\r
448 //\r
449 // Write the record to the work space.\r
450 //\r
451 Record->Lba = Lba;\r
452 Record->Offset = Offset;\r
453 Record->Length = Length;\r
454 Record->FvBaseAddress = FvbPhysicalAddress;\r
f0480ecf
LG
455 if (PrivateData != NULL) {\r
456 CopyMem ((Record + 1), PrivateData, Header->PrivateDataSize);\r
457 }\r
85e923a5
LG
458\r
459 MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;\r
460 MyLength = RECORD_SIZE (Header->PrivateDataSize);\r
461\r
462 Status = FtwDevice->FtwFvBlock->Write (\r
463 FtwDevice->FtwFvBlock,\r
464 FtwDevice->FtwWorkSpaceLba,\r
465 FtwDevice->FtwWorkSpaceBase + MyOffset,\r
466 &MyLength,\r
467 (UINT8 *) Record\r
468 );\r
469 if (EFI_ERROR (Status)) {\r
470 return EFI_ABORTED;\r
471 }\r
472 //\r
473 // Record has written to working block, then do the data.\r
474 //\r
475 //\r
476 // Allocate a memory buffer\r
477 //\r
478 MyBufferSize = FtwDevice->SpareAreaLength;\r
479 MyBuffer = AllocatePool (MyBufferSize);\r
480 if (MyBuffer == NULL) {\r
481 return EFI_OUT_OF_RESOURCES;\r
482 }\r
483 //\r
484 // Read all original data from target block to memory buffer\r
485 //\r
486 Ptr = MyBuffer;\r
487 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {\r
488 MyLength = FtwDevice->BlockSize;\r
489 Status = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr);\r
490 if (EFI_ERROR (Status)) {\r
491 FreePool (MyBuffer);\r
492 return EFI_ABORTED;\r
493 }\r
494\r
495 Ptr += MyLength;\r
496 }\r
497 //\r
498 // Overwrite the updating range data with\r
499 // the input buffer content\r
500 //\r
501 CopyMem (MyBuffer + Offset, Buffer, Length);\r
502\r
503 //\r
504 // Try to keep the content of spare block\r
505 // Save spare block into a spare backup memory buffer (Sparebuffer)\r
506 //\r
507 SpareBufferSize = FtwDevice->SpareAreaLength;\r
508 SpareBuffer = AllocatePool (SpareBufferSize);\r
509 if (SpareBuffer == NULL) {\r
510 FreePool (MyBuffer);\r
511 return EFI_OUT_OF_RESOURCES;\r
512 }\r
513\r
514 Ptr = SpareBuffer;\r
515 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {\r
516 MyLength = FtwDevice->BlockSize;\r
517 Status = FtwDevice->FtwBackupFvb->Read (\r
518 FtwDevice->FtwBackupFvb,\r
519 FtwDevice->FtwSpareLba + Index,\r
520 0,\r
521 &MyLength,\r
522 Ptr\r
523 );\r
524 if (EFI_ERROR (Status)) {\r
525 FreePool (MyBuffer);\r
526 FreePool (SpareBuffer);\r
527 return EFI_ABORTED;\r
528 }\r
529\r
530 Ptr += MyLength;\r
531 }\r
532 //\r
533 // Write the memory buffer to spare block\r
534 //\r
535 Status = FtwEraseSpareBlock (FtwDevice);\r
536 Ptr = MyBuffer;\r
537 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {\r
538 MyLength = FtwDevice->BlockSize;\r
539 Status = FtwDevice->FtwBackupFvb->Write (\r
540 FtwDevice->FtwBackupFvb,\r
541 FtwDevice->FtwSpareLba + Index,\r
542 0,\r
543 &MyLength,\r
544 Ptr\r
545 );\r
546 if (EFI_ERROR (Status)) {\r
547 FreePool (MyBuffer);\r
548 FreePool (SpareBuffer);\r
549 return EFI_ABORTED;\r
550 }\r
551\r
552 Ptr += MyLength;\r
553 }\r
554 //\r
555 // Free MyBuffer\r
556 //\r
557 FreePool (MyBuffer);\r
558\r
559 //\r
560 // Set the SpareComplete in the FTW record,\r
561 //\r
562 MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;\r
563 Status = FtwUpdateFvState (\r
564 FtwDevice->FtwFvBlock,\r
565 FtwDevice->FtwWorkSpaceLba,\r
566 FtwDevice->FtwWorkSpaceBase + MyOffset,\r
567 SPARE_COMPLETED\r
568 );\r
569 if (EFI_ERROR (Status)) {\r
570 FreePool (SpareBuffer);\r
571 return EFI_ABORTED;\r
572 }\r
573\r
574 Record->SpareComplete = FTW_VALID_STATE;\r
575\r
576 //\r
577 // Since the content has already backuped in spare block, the write is\r
578 // guaranteed to be completed with fault tolerant manner.\r
579 //\r
580 Status = FtwWriteRecord (This, Fvb);\r
581 if (EFI_ERROR (Status)) {\r
582 FreePool (SpareBuffer);\r
583 return EFI_ABORTED;\r
584 }\r
585 //\r
586 // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.\r
587 //\r
588 Status = FtwEraseSpareBlock (FtwDevice);\r
589 Ptr = SpareBuffer;\r
590 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {\r
591 MyLength = FtwDevice->BlockSize;\r
592 Status = FtwDevice->FtwBackupFvb->Write (\r
593 FtwDevice->FtwBackupFvb,\r
594 FtwDevice->FtwSpareLba + Index,\r
595 0,\r
596 &MyLength,\r
597 Ptr\r
598 );\r
599 if (EFI_ERROR (Status)) {\r
600 FreePool (SpareBuffer);\r
601 return EFI_ABORTED;\r
602 }\r
603\r
604 Ptr += MyLength;\r
605 }\r
606 //\r
607 // All success.\r
608 //\r
609 FreePool (SpareBuffer);\r
610\r
611 DEBUG (\r
612 (EFI_D_ERROR,\r
613 "Ftw: Write() success, (Lba:Offset)=(%lx:0x%x), Length: 0x%x\n",\r
614 Lba,\r
615 Offset,\r
616 Length)\r
617 );\r
618\r
619 return EFI_SUCCESS;\r
620}\r
621\r
622/**\r
623 Restarts a previously interrupted write. The caller must provide the\r
624 block protocol needed to complete the interrupted write.\r
625\r
626 @param This The pointer to this protocol instance. \r
627 @param FvBlockHandle The handle of FVB protocol that provides services for\r
628 reading, writing, and erasing the target block.\r
629\r
630 @retval EFI_SUCCESS The function completed successfully\r
631 @retval EFI_ACCESS_DENIED No pending writes exist\r
632 @retval EFI_NOT_FOUND FVB protocol not found by the handle\r
633 @retval EFI_ABORTED The function could not complete successfully\r
634\r
635**/\r
636EFI_STATUS\r
637EFIAPI\r
638FtwRestart (\r
639 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r
640 IN EFI_HANDLE FvBlockHandle\r
641 )\r
642{\r
643 EFI_STATUS Status;\r
644 EFI_FTW_DEVICE *FtwDevice;\r
645 EFI_FAULT_TOLERANT_WRITE_HEADER *Header;\r
646 EFI_FAULT_TOLERANT_WRITE_RECORD *Record;\r
647 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
648\r
649 FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
650\r
651 Status = WorkSpaceRefresh (FtwDevice);\r
652 if (EFI_ERROR (Status)) {\r
653 return EFI_ABORTED;\r
654 }\r
655\r
656 Header = FtwDevice->FtwLastWriteHeader;\r
657 Record = FtwDevice->FtwLastWriteRecord;\r
658\r
659 //\r
660 // Spare Complete but Destination not complete,\r
661 // Recover the targt block with the spare block.\r
662 //\r
663 Status = FtwGetFvbByHandle (FvBlockHandle, &Fvb);\r
664 if (EFI_ERROR (Status)) {\r
665 return EFI_NOT_FOUND;\r
666 }\r
667\r
668 //\r
669 // Check the COMPLETE flag of last write header\r
670 //\r
671 if (Header->Complete == FTW_VALID_STATE) {\r
672 return EFI_ACCESS_DENIED;\r
673 }\r
674\r
675 //\r
676 // Check the flags of last write record\r
677 //\r
678 if (Record->DestinationComplete == FTW_VALID_STATE) {\r
679 return EFI_ACCESS_DENIED;\r
680 }\r
681\r
682 if ((Record->SpareComplete != FTW_VALID_STATE)) {\r
683 return EFI_ABORTED;\r
684 }\r
685\r
686 //\r
687 // Since the content has already backuped in spare block, the write is\r
688 // guaranteed to be completed with fault tolerant manner.\r
689 //\r
690 Status = FtwWriteRecord (This, Fvb);\r
691 if (EFI_ERROR (Status)) {\r
692 return EFI_ABORTED;\r
693 }\r
694\r
695 //\r
696 // Erase Spare block\r
697 // This is restart, no need to keep spareblock content.\r
698 //\r
699 FtwEraseSpareBlock (FtwDevice);\r
700\r
701 DEBUG ((EFI_D_ERROR, "Ftw: Restart() success \n"));\r
702 return EFI_SUCCESS;\r
703}\r
704\r
705/**\r
706 Aborts all previous allocated writes.\r
707\r
708 @param This The pointer to this protocol instance. \r
709\r
710 @retval EFI_SUCCESS The function completed successfully\r
711 @retval EFI_ABORTED The function could not complete successfully.\r
712 @retval EFI_NOT_FOUND No allocated writes exist.\r
713\r
714**/\r
715EFI_STATUS\r
716EFIAPI\r
717FtwAbort (\r
718 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This\r
719 )\r
720{\r
721 EFI_STATUS Status;\r
722 UINTN Offset;\r
723 EFI_FTW_DEVICE *FtwDevice;\r
724\r
725 FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
726\r
727 Status = WorkSpaceRefresh (FtwDevice);\r
728 if (EFI_ERROR (Status)) {\r
729 return EFI_ABORTED;\r
730 }\r
731\r
732 if (FtwDevice->FtwLastWriteHeader->Complete == FTW_VALID_STATE) {\r
733 return EFI_NOT_FOUND;\r
734 }\r
735 //\r
736 // Update the complete state of the header as VALID and abort.\r
737 //\r
738 Offset = (UINT8 *) FtwDevice->FtwLastWriteHeader - FtwDevice->FtwWorkSpace;\r
739 Status = FtwUpdateFvState (\r
740 FtwDevice->FtwFvBlock,\r
741 FtwDevice->FtwWorkSpaceLba,\r
742 FtwDevice->FtwWorkSpaceBase + Offset,\r
743 WRITES_COMPLETED\r
744 );\r
745 if (EFI_ERROR (Status)) {\r
746 return EFI_ABORTED;\r
747 }\r
748\r
749 FtwDevice->FtwLastWriteHeader->Complete = FTW_VALID_STATE;\r
750\r
751 DEBUG ((EFI_D_ERROR, "Ftw: Abort() success \n"));\r
752 return EFI_SUCCESS;\r
753}\r
754\r
755/**\r
756 Starts a target block update. This records information about the write\r
757 in fault tolerant storage and will complete the write in a recoverable\r
758 manner, ensuring at all times that either the original contents or\r
759 the modified contents are available.\r
760\r
761 @param This The pointer to this protocol instance. \r
762 @param CallerId The GUID identifying the last write.\r
763 @param Lba The logical block address of the last write.\r
764 @param Offset The offset within the block of the last write.\r
765 @param Length The length of the last write.\r
766 @param PrivateDataSize bytes from the private data\r
767 stored for this write.\r
768 @param PrivateData A pointer to a buffer. The function will copy\r
769 @param Complete A Boolean value with TRUE indicating\r
770 that the write was completed.\r
771\r
772 @retval EFI_SUCCESS The function completed successfully\r
773 @retval EFI_ABORTED The function could not complete successfully\r
774 @retval EFI_NOT_FOUND No allocated writes exist\r
775 @retval EFI_BUFFER_TOO_SMALL Input buffer is not larget enough\r
776\r
777**/\r
778EFI_STATUS\r
779EFIAPI\r
780FtwGetLastWrite (\r
781 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r
782 OUT EFI_GUID *CallerId,\r
783 OUT EFI_LBA *Lba,\r
784 OUT UINTN *Offset,\r
785 OUT UINTN *Length,\r
786 IN OUT UINTN *PrivateDataSize,\r
787 OUT VOID *PrivateData,\r
788 OUT BOOLEAN *Complete\r
789 )\r
790{\r
791 EFI_STATUS Status;\r
792 EFI_FTW_DEVICE *FtwDevice;\r
793 EFI_FAULT_TOLERANT_WRITE_HEADER *Header;\r
794 EFI_FAULT_TOLERANT_WRITE_RECORD *Record;\r
795\r
796 if (!FeaturePcdGet(PcdFullFtwServiceEnable)) {\r
797 return EFI_UNSUPPORTED;\r
798 }\r
799\r
800 FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
801\r
802 Status = WorkSpaceRefresh (FtwDevice);\r
803 if (EFI_ERROR (Status)) {\r
804 return EFI_ABORTED;\r
805 }\r
806\r
807 Header = FtwDevice->FtwLastWriteHeader;\r
808 Record = FtwDevice->FtwLastWriteRecord;\r
809\r
810 //\r
811 // If Header is incompleted and the last record has completed, then\r
812 // call Abort() to set the Header->Complete FLAG.\r
813 //\r
814 if ((Header->Complete != FTW_VALID_STATE) &&\r
815 (Record->DestinationComplete == FTW_VALID_STATE) &&\r
816 IsLastRecordOfWrites (Header, Record)\r
817 ) {\r
818\r
819 Status = FtwAbort (This);\r
820 *Complete = TRUE;\r
821 return EFI_NOT_FOUND;\r
822 }\r
823 //\r
824 // If there is no write header/record, return not found.\r
825 //\r
826 if (Header->HeaderAllocated != FTW_VALID_STATE) {\r
827 *Complete = TRUE;\r
828 return EFI_NOT_FOUND;\r
829 }\r
830 //\r
831 // If this record SpareComplete has not set, then it can not restart.\r
832 //\r
833 if (Record->SpareComplete != FTW_VALID_STATE) {\r
f0480ecf
LG
834 Status = GetPreviousRecordOfWrites (Header, &Record);\r
835 if (EFI_ERROR (Status)) {\r
85e923a5 836 FtwAbort (This);\r
85e923a5
LG
837 *Complete = TRUE;\r
838 return EFI_NOT_FOUND;\r
85e923a5 839 }\r
d2fbaaab 840 ASSERT (Record != NULL);\r
85e923a5 841 }\r
f0480ecf 842\r
85e923a5
LG
843 //\r
844 // Fill all the requested values\r
845 //\r
846 CopyMem (CallerId, &Header->CallerId, sizeof (EFI_GUID));\r
847 *Lba = Record->Lba;\r
848 *Offset = Record->Offset;\r
849 *Length = Record->Length;\r
850 *Complete = (BOOLEAN) (Record->DestinationComplete == FTW_VALID_STATE);\r
851\r
852 if (*PrivateDataSize < Header->PrivateDataSize) {\r
853 *PrivateDataSize = Header->PrivateDataSize;\r
854 PrivateData = NULL;\r
855 Status = EFI_BUFFER_TOO_SMALL;\r
856 } else {\r
857 *PrivateDataSize = Header->PrivateDataSize;\r
858 CopyMem (PrivateData, Record + 1, *PrivateDataSize);\r
859 Status = EFI_SUCCESS;\r
860 }\r
861\r
862 DEBUG ((EFI_D_ERROR, "Ftw: GetLasetWrite() success\n"));\r
863\r
864 return Status;\r
865}\r
866\r
aa75dfec 867/**\r
868 Firmware Volume Block Protocol notification event handler.\r
869\r
870 Initialization for Fault Tolerant Write is done in this handler.\r
871\r
872 @param[in] Event Event whose notification function is being invoked.\r
873 @param[in] Context Pointer to the notification function's context.\r
874**/\r
93367605 875VOID\r
85e923a5 876EFIAPI\r
93367605 877FvbNotificationEvent (\r
878 IN EFI_EVENT Event,\r
879 IN VOID *Context\r
85e923a5
LG
880 )\r
881{\r
93367605 882 EFI_STATUS Status;\r
85e923a5
LG
883 EFI_HANDLE *HandleBuffer;\r
884 UINTN HandleCount;\r
93367605 885 UINTN Index;\r
886 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
887 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
85e923a5 888 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
93367605 889 EFI_FVB_ATTRIBUTES_2 Attributes;\r
85e923a5 890 EFI_FTW_DEVICE *FtwDevice;\r
85e923a5
LG
891 EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r
892 UINT32 LbaIndex;\r
93367605 893 UINTN Length;\r
894 EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;\r
895 UINTN Offset;\r
85e923a5
LG
896 EFI_HANDLE FvbHandle;\r
897\r
93367605 898 FtwDevice = (EFI_FTW_DEVICE *)Context;\r
85e923a5 899 FvbHandle = NULL;\r
93367605 900 Fvb = NULL;\r
85e923a5 901\r
b6183e41 902 FtwDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);\r
903 FtwDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwSpareBase);\r
904\r
85e923a5 905 //\r
93367605 906 // Locate all handles of Fvb protocol\r
85e923a5
LG
907 //\r
908 Status = gBS->LocateHandleBuffer (\r
93367605 909 ByProtocol,\r
910 &gEfiFirmwareVolumeBlockProtocolGuid,\r
911 NULL,\r
912 &HandleCount,\r
913 &HandleBuffer\r
914 );\r
85e923a5 915 if (EFI_ERROR (Status)) {\r
93367605 916 return;\r
85e923a5
LG
917 }\r
918\r
93367605 919 //\r
920 // Get the FVB to access variable store\r
921 //\r
85e923a5
LG
922 for (Index = 0; Index < HandleCount; Index += 1) {\r
923 Status = gBS->HandleProtocol (\r
93367605 924 HandleBuffer[Index],\r
925 &gEfiFirmwareVolumeBlockProtocolGuid,\r
926 (VOID **) &Fvb\r
927 );\r
85e923a5 928 if (EFI_ERROR (Status)) {\r
93367605 929 Status = EFI_NOT_FOUND;\r
930 break;\r
85e923a5
LG
931 }\r
932\r
93367605 933 //\r
934 // Ensure this FVB protocol supported Write operation.\r
935 //\r
936 Status = Fvb->GetAttributes (Fvb, &Attributes);\r
937 if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
938 continue; \r
939 }\r
940 //\r
941 // Compare the address and select the right one\r
942 //\r
943 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
85e923a5
LG
944 if (EFI_ERROR (Status)) {\r
945 continue;\r
946 }\r
947\r
93367605 948 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
949 if ((FtwDevice->FtwFvBlock == NULL) && (FtwDevice->WorkSpaceAddress >= FvbBaseAddress) &&\r
950 ((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (FvbBaseAddress + FwVolHeader->FvLength))\r
951 ) {\r
85e923a5
LG
952 FtwDevice->FtwFvBlock = Fvb;\r
953 //\r
954 // To get the LBA of work space\r
955 //\r
956 if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
957 //\r
958 // Now, one FV has one type of BlockLength\r
959 //\r
960 FvbMapEntry = &FwVolHeader->BlockMap[0];\r
961 for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
93367605 962 if ((FtwDevice->WorkSpaceAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))\r
963 && (FtwDevice->WorkSpaceAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {\r
85e923a5
LG
964 FtwDevice->FtwWorkSpaceLba = LbaIndex - 1;\r
965 //\r
966 // Get the Work space size and Base(Offset)\r
967 //\r
968 FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength;\r
93367605 969 FtwDevice->FtwWorkSpaceBase = (UINTN) (FtwDevice->WorkSpaceAddress - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
85e923a5
LG
970 break;\r
971 }\r
972 }\r
973 }\r
974 }\r
93367605 975 \r
976 if ((FtwDevice->FtwBackupFvb == NULL) && (FtwDevice->SpareAreaAddress >= FvbBaseAddress) &&\r
977 ((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (FvbBaseAddress + FwVolHeader->FvLength))\r
978 ) {\r
85e923a5
LG
979 FtwDevice->FtwBackupFvb = Fvb;\r
980 //\r
981 // To get the LBA of spare\r
982 //\r
983 if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
984 //\r
985 // Now, one FV has one type of BlockLength\r
986 //\r
987 FvbMapEntry = &FwVolHeader->BlockMap[0];\r
988 for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
93367605 989 if ((FtwDevice->SpareAreaAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))\r
990 && (FtwDevice->SpareAreaAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {\r
85e923a5
LG
991 //\r
992 // Get the NumberOfSpareBlock and BlockSize\r
993 //\r
93367605 994 FtwDevice->FtwSpareLba = LbaIndex - 1;\r
995 FtwDevice->BlockSize = FvbMapEntry->Length;\r
85e923a5
LG
996 FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->BlockSize;\r
997 //\r
998 // Check the range of spare area to make sure that it's in FV range\r
999 //\r
1000 if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > FvbMapEntry->NumBlocks) {\r
1001 DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n"));\r
93367605 1002 ASSERT (FALSE);\r
1003 return;\r
85e923a5
LG
1004 }\r
1005 break;\r
1006 }\r
1007 }\r
1008 }\r
1009 }\r
1010 }\r
1011\r
93367605 1012 if ((FtwDevice->FtwBackupFvb == NULL) || (FtwDevice->FtwFvBlock == NULL) ||\r
1013 (FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) || (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))) {\r
1014 return;\r
1015 }\r
1016\r
1017 DEBUG ((EFI_D_INFO, "Ftw: Working and spare FVB is ready\n"));\r
85e923a5
LG
1018 //\r
1019 // Calculate the start LBA of working block. Working block is an area which\r
1020 // contains working space in its last block and has the same size as spare\r
1021 // block, unless there are not enough blocks before the block that contains\r
1022 // working space.\r
1023 //\r
1024 FtwDevice->FtwWorkBlockLba = FtwDevice->FtwWorkSpaceLba - FtwDevice->NumberOfSpareBlock + 1;\r
93367605 1025 ASSERT ((INT64) (FtwDevice->FtwWorkBlockLba) >= 0); \r
85e923a5 1026\r
85e923a5
LG
1027 //\r
1028 // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
1029 //\r
1030 FtwDevice->FtwWorkSpace = (UINT8 *) (FtwDevice + 1);\r
1031 FtwDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwDevice->FtwWorkSpace;\r
1032\r
1033 FtwDevice->FtwLastWriteHeader = NULL;\r
1034 FtwDevice->FtwLastWriteRecord = NULL;\r
1035\r
1036 //\r
1037 // Refresh the working space data from working block\r
1038 //\r
1039 Status = WorkSpaceRefresh (FtwDevice);\r
93367605 1040 ASSERT_EFI_ERROR (Status);\r
85e923a5
LG
1041 //\r
1042 // If the working block workspace is not valid, try the spare block\r
1043 //\r
1044 if (!IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {\r
1045 //\r
1046 // Read from spare block\r
1047 //\r
1048 Length = FtwDevice->FtwWorkSpaceSize;\r
1049 Status = FtwDevice->FtwBackupFvb->Read (\r
93367605 1050 FtwDevice->FtwBackupFvb,\r
1051 FtwDevice->FtwSpareLba,\r
1052 FtwDevice->FtwWorkSpaceBase,\r
1053 &Length,\r
1054 FtwDevice->FtwWorkSpace\r
1055 );\r
1056 ASSERT_EFI_ERROR (Status);\r
1057\r
85e923a5
LG
1058 //\r
1059 // If spare block is valid, then replace working block content.\r
1060 //\r
1061 if (IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {\r
1062 Status = FlushSpareBlockToWorkingBlock (FtwDevice);\r
1063 DEBUG ((EFI_D_ERROR, "Ftw: Restart working block update in Init() - %r\n", Status));\r
1064 FtwAbort (&FtwDevice->FtwInstance);\r
1065 //\r
1066 // Refresh work space.\r
1067 //\r
1068 Status = WorkSpaceRefresh (FtwDevice);\r
93367605 1069 ASSERT_EFI_ERROR (Status);\r
85e923a5
LG
1070 } else {\r
1071 DEBUG ((EFI_D_ERROR, "Ftw: Both are invalid, init workspace\n"));\r
1072 //\r
1073 // If both are invalid, then initialize work space.\r
1074 //\r
1075 SetMem (\r
1076 FtwDevice->FtwWorkSpace,\r
1077 FtwDevice->FtwWorkSpaceSize,\r
1078 FTW_ERASED_BYTE\r
1079 );\r
1080 InitWorkSpaceHeader (FtwDevice->FtwWorkSpaceHeader);\r
1081 //\r
1082 // Initialize the work space\r
1083 //\r
1084 Status = FtwReclaimWorkSpace (FtwDevice, FALSE);\r
93367605 1085 ASSERT_EFI_ERROR (Status);\r
85e923a5
LG
1086 }\r
1087 }\r
85e923a5
LG
1088 //\r
1089 // If the FtwDevice->FtwLastWriteRecord is 1st record of write header &&\r
93367605 1090 // (! SpareComplete) THEN call Abort().\r
85e923a5
LG
1091 //\r
1092 if ((FtwDevice->FtwLastWriteHeader->HeaderAllocated == FTW_VALID_STATE) &&\r
93367605 1093 (FtwDevice->FtwLastWriteRecord->SpareComplete != FTW_VALID_STATE) &&\r
1094 IsFirstRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)\r
1095 ) {\r
85e923a5
LG
1096 DEBUG ((EFI_D_ERROR, "Ftw: Init.. find first record not SpareCompleted, abort()\n"));\r
1097 FtwAbort (&FtwDevice->FtwInstance);\r
1098 }\r
1099 //\r
1100 // If Header is incompleted and the last record has completed, then\r
1101 // call Abort() to set the Header->Complete FLAG.\r
1102 //\r
1103 if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&\r
93367605 1104 (FtwDevice->FtwLastWriteRecord->DestinationComplete == FTW_VALID_STATE) &&\r
1105 IsLastRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)\r
1106 ) {\r
85e923a5
LG
1107 DEBUG ((EFI_D_ERROR, "Ftw: Init.. find last record completed but header not, abort()\n"));\r
1108 FtwAbort (&FtwDevice->FtwInstance);\r
1109 }\r
1110 //\r
1111 // To check the workspace buffer following last Write header/records is EMPTY or not.\r
1112 // If it's not EMPTY, FTW also need to call reclaim().\r
1113 //\r
1114 FtwHeader = FtwDevice->FtwLastWriteHeader;\r
1115 Offset = (UINT8 *) FtwHeader - FtwDevice->FtwWorkSpace;\r
1116 if (FtwDevice->FtwWorkSpace[Offset] != FTW_ERASED_BYTE) {\r
1117 Offset += WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites, FtwHeader->PrivateDataSize);\r
1118 }\r
93367605 1119 \r
1120 if (!IsErasedFlashBuffer (FtwDevice->FtwWorkSpace + Offset, FtwDevice->FtwWorkSpaceSize - Offset)) {\r
85e923a5 1121 Status = FtwReclaimWorkSpace (FtwDevice, TRUE);\r
93367605 1122 ASSERT_EFI_ERROR (Status);\r
85e923a5 1123 }\r
93367605 1124\r
85e923a5
LG
1125 //\r
1126 // Restart if it's boot block\r
1127 //\r
1128 if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&\r
93367605 1129 (FtwDevice->FtwLastWriteRecord->SpareComplete == FTW_VALID_STATE)\r
1130 ) {\r
85e923a5
LG
1131 if (FtwDevice->FtwLastWriteRecord->BootBlockUpdate == FTW_VALID_STATE) {\r
1132 Status = FlushSpareBlockToBootBlock (FtwDevice);\r
1133 DEBUG ((EFI_D_ERROR, "Ftw: Restart boot block update - %r\n", Status));\r
93367605 1134 ASSERT_EFI_ERROR (Status);\r
85e923a5
LG
1135 FtwAbort (&FtwDevice->FtwInstance);\r
1136 } else {\r
1137 //\r
1138 // if (SpareCompleted) THEN Restart to fault tolerant write.\r
1139 //\r
1140 FvbHandle = GetFvbByAddress (FtwDevice->FtwLastWriteRecord->FvBaseAddress, &Fvb);\r
1141 if (FvbHandle != NULL) {\r
1142 Status = FtwRestart (&FtwDevice->FtwInstance, FvbHandle);\r
1143 DEBUG ((EFI_D_ERROR, "FtwLite: Restart last write - %r\n", Status));\r
93367605 1144 ASSERT_EFI_ERROR (Status);\r
85e923a5
LG
1145 }\r
1146 FtwAbort (&FtwDevice->FtwInstance);\r
1147 }\r
1148 }\r
85e923a5
LG
1149 //\r
1150 // Hook the protocol API\r
1151 //\r
93367605 1152 FtwDevice->FtwInstance.GetMaxBlockSize = FtwGetMaxBlockSize;\r
1153 FtwDevice->FtwInstance.Allocate = FtwAllocate;\r
1154 FtwDevice->FtwInstance.Write = FtwWrite;\r
1155 FtwDevice->FtwInstance.Restart = FtwRestart;\r
1156 FtwDevice->FtwInstance.Abort = FtwAbort;\r
1157 FtwDevice->FtwInstance.GetLastWrite = FtwGetLastWrite;\r
1158 \r
85e923a5
LG
1159 //\r
1160 // Install protocol interface\r
1161 //\r
1162 Status = gBS->InstallProtocolInterface (\r
93367605 1163 &FtwDevice->Handle,\r
1164 &gEfiFaultTolerantWriteProtocolGuid,\r
1165 EFI_NATIVE_INTERFACE,\r
1166 &FtwDevice->FtwInstance\r
1167 );\r
1168\r
1169 ASSERT_EFI_ERROR (Status);\r
1170 \r
1171 //\r
1172 // Close the notify event to avoid install FaultTolerantWriteProtocol again.\r
1173 //\r
1174 Status = gBS->CloseEvent (Event); \r
1175 ASSERT_EFI_ERROR (Status);\r
1176 \r
1177 return;\r
1178}\r
1179\r
1180/**\r
1181 This function is the entry point of the Fault Tolerant Write driver.\r
1182\r
1183 @param ImageHandle A handle for the image that is initializing this driver\r
1184 @param SystemTable A pointer to the EFI system table\r
1185\r
1186 @return EFI_SUCCESS FTW has finished the initialization\r
1187 @retval EFI_NOT_FOUND Locate FVB protocol error\r
1188 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
1189 @retval EFI_VOLUME_CORRUPTED Firmware volume is error\r
1190 @retval EFI_ABORTED FTW initialization error\r
1191\r
1192**/\r
1193EFI_STATUS\r
1194EFIAPI\r
1195InitializeFaultTolerantWrite (\r
1196 IN EFI_HANDLE ImageHandle,\r
1197 IN EFI_SYSTEM_TABLE *SystemTable\r
1198 )\r
1199{\r
1200 EFI_FTW_DEVICE *FtwDevice;\r
1201\r
1202 //\r
1203 // Allocate Private data of this driver,\r
1204 // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].\r
1205 //\r
1206 FtwDevice = NULL;\r
1207 FtwDevice = AllocateZeroPool (sizeof (EFI_FTW_DEVICE) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize));\r
1208 if (FtwDevice == NULL) {\r
1209 return EFI_OUT_OF_RESOURCES;\r
85e923a5
LG
1210 }\r
1211\r
93367605 1212 ZeroMem (FtwDevice, sizeof (EFI_FTW_DEVICE));\r
1213 FtwDevice->Signature = FTW_DEVICE_SIGNATURE;\r
85e923a5 1214\r
93367605 1215 //\r
1216 // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
1217 //\r
1218\r
93367605 1219 FtwDevice->WorkSpaceLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwWorkingSize);\r
1220\r
93367605 1221 FtwDevice->SpareAreaLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwSpareSize);\r
85e923a5 1222\r
93367605 1223 if ((FtwDevice->WorkSpaceLength == 0) || (FtwDevice->SpareAreaLength == 0)) {\r
1224 DEBUG ((EFI_D_ERROR, "Ftw: Workspace or Spare block does not exist!\n"));\r
85e923a5 1225 FreePool (FtwDevice);\r
93367605 1226 return EFI_OUT_OF_RESOURCES;\r
85e923a5 1227 }\r
93367605 1228 FtwDevice->FtwFvBlock = NULL;\r
1229 FtwDevice->FtwBackupFvb = NULL;\r
1230 FtwDevice->FtwWorkSpaceLba = (EFI_LBA) (-1);\r
1231 FtwDevice->FtwSpareLba = (EFI_LBA) (-1);\r
85e923a5 1232\r
93367605 1233 //\r
1234 // Register FvbNotificationEvent () notify function.\r
1235 // \r
1236 EfiCreateProtocolNotifyEvent (\r
1237 &gEfiFirmwareVolumeBlockProtocolGuid,\r
1238 TPL_CALLBACK,\r
1239 FvbNotificationEvent,\r
1240 (VOID *)FtwDevice,\r
1241 &mFvbRegistration\r
1242 );\r
85e923a5 1243\r
93367605 1244 return EFI_SUCCESS;\r
85e923a5 1245}\r