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