]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.h
0701532f90ddc2662a2f5cf1d0959b2251e10dbf
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / FaultTolerantWriteLite / Dxe / FtwLite.h
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 FtwLite.h
16
17 Abstract:
18
19 This is a simple fault tolerant write driver, based on PlatformFd library.
20 And it only supports write BufferSize <= SpareAreaLength.
21
22 This boot service only protocol provides fault tolerant write capability for
23 block devices. The protocol has internal non-volatile intermediate storage
24 of the data and private information. It should be able to recover
25 automatically from a critical fault, such as power failure.
26
27 --*/
28
29 #ifndef _EFI_FAULT_TOLERANT_WRITE_LITE_H_
30 #define _EFI_FAULT_TOLERANT_WRITE_LITE_H_
31
32 //
33 // The package level header files this module uses
34 //
35 #include <PiDxe.h>
36 //
37 // The protocols, PPI and GUID defintions for this module
38 //
39 #include <Protocol/PciRootBridgeIo.h>
40 #include <Guid/SystemNvDataGuid.h>
41 #include <Guid/FlashMapHob.h>
42 #include <Protocol/FaultTolerantWriteLite.h>
43 #include <Protocol/FirmwareVolumeBlock.h>
44 //
45 // The Library classes this module consumes
46 //
47 #include <Library/PcdLib.h>
48 #include <Library/DebugLib.h>
49 #include <Library/UefiDriverEntryPoint.h>
50 #include <Library/BaseMemoryLib.h>
51 #include <Library/MemoryAllocationLib.h>
52 #include <Library/UefiBootServicesTableLib.h>
53 #include <Library/HobLib.h>
54
55 #include <Common/WorkingBlockHeader.h>
56 #include <Common/FlashMap.h>
57
58 #define EFI_D_FTW_LITE EFI_D_ERROR
59 #define EFI_D_FTW_INFO EFI_D_INFO
60
61 //
62 // Flash erase polarity is 1
63 //
64 #define FTW_ERASE_POLARITY 1
65
66 #define FTW_VALID_STATE 0
67 #define FTW_INVALID_STATE 1
68
69 #define FTW_ERASED_BYTE ((UINT8) (255))
70 #define FTW_POLARITY_REVERT ((UINT8) (255))
71
72 typedef struct {
73 UINT8 WriteAllocated : 1;
74 UINT8 SpareCompleted : 1;
75 UINT8 WriteCompleted : 1;
76 UINT8 Reserved : 5;
77 #define WRITE_ALLOCATED 0x1
78 #define SPARE_COMPLETED 0x2
79 #define WRITE_COMPLETED 0x4
80
81 EFI_DEV_PATH DevPath;
82 EFI_LBA Lba;
83 UINTN Offset;
84 UINTN NumBytes;
85 //
86 // UINTN SpareAreaOffset;
87 //
88 } EFI_FTW_LITE_RECORD;
89
90 #define FTW_LITE_DEVICE_SIGNATURE EFI_SIGNATURE_32 ('F', 'T', 'W', 'L')
91
92 //
93 // MACRO for Block size.
94 // Flash Erasing will do in block granularity.
95 //
96 #ifdef FV_BLOCK_SIZE
97 #define FTW_BLOCK_SIZE FV_BLOCK_SIZE
98 #else
99 #define FV_BLOCK_SIZE 0x10000
100 #define FTW_BLOCK_SIZE FV_BLOCK_SIZE
101 #endif
102 //
103 // MACRO for FTW WORK SPACE Base & Size
104 //
105 #ifdef EFI_FTW_WORKING_OFFSET
106 #define FTW_WORK_SPACE_BASE EFI_FTW_WORKING_OFFSET
107 #else
108 #define FTW_WORK_SPACE_BASE 0x00E000
109 #endif
110
111 #ifdef EFI_FTW_WORKING_LENGTH
112 #define FTW_WORK_SPACE_SIZE EFI_FTW_WORKING_LENGTH
113 #else
114 #define FTW_WORK_SPACE_SIZE 0x002000
115 #endif
116 //
117 // MACRO for FTW header and record
118 //
119 #define FTW_WORKING_QUEUE_SIZE (FTW_WORK_SPACE_SIZE - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER))
120 #define FTW_LITE_RECORD_SIZE (sizeof (EFI_FTW_LITE_RECORD))
121 #define WRITE_TOTAL_SIZE FTW_LITE_RECORD_SIZE
122
123 //
124 // EFI Fault tolerant protocol private data structure
125 //
126 typedef struct {
127 UINTN Signature;
128 EFI_HANDLE Handle;
129 EFI_FTW_LITE_PROTOCOL FtwLiteInstance;
130 EFI_PHYSICAL_ADDRESS WorkSpaceAddress;
131 UINTN WorkSpaceLength;
132 EFI_PHYSICAL_ADDRESS SpareAreaAddress;
133 UINTN SpareAreaLength;
134 UINTN NumberOfSpareBlock; // Number of the blocks in spare block
135 UINTN SizeOfSpareBlock; // Block size in bytes of the blocks in spare block
136 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader;
137 EFI_FTW_LITE_RECORD *FtwLastRecord;
138 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwFvBlock; // FVB of working block
139 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwBackupFvb; // FVB of spare block
140 EFI_LBA FtwSpareLba;
141 EFI_LBA FtwWorkBlockLba; // Start LBA of working block
142 EFI_LBA FtwWorkSpaceLba; // Start LBA of working space
143 UINTN FtwWorkSpaceBase; // Offset from LBA start addr
144 UINTN FtwWorkSpaceSize;
145 UINT8 *FtwWorkSpace;
146 //
147 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
148 // Allocated with EFI_FTW_LITE_DEVICE.
149 //
150 } EFI_FTW_LITE_DEVICE;
151
152 #define FTW_LITE_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_LITE_DEVICE, FtwLiteInstance, FTW_LITE_DEVICE_SIGNATURE)
153
154 //
155 // Driver entry point
156 //
157 EFI_STATUS
158 EFIAPI
159 InitializeFtwLite (
160 IN EFI_HANDLE ImageHandle,
161 IN EFI_SYSTEM_TABLE *SystemTable
162 )
163 /*++
164
165 Routine Description:
166 This function is the entry point of the Fault Tolerant Write driver.
167
168 Arguments:
169 ImageHandle - EFI_HANDLE: A handle for the image that is initializing
170 this driver
171 SystemTable - EFI_SYSTEM_TABLE: A pointer to the EFI system table
172
173 Returns:
174 EFI_SUCCESS - FTW has finished the initialization
175 EFI_ABORTED - FTW initialization error
176
177 --*/
178 ;
179
180 //
181 // Fault Tolerant Write Protocol API
182 //
183 EFI_STATUS
184 EFIAPI
185 FtwLiteWrite (
186 IN EFI_FTW_LITE_PROTOCOL *This,
187 IN EFI_HANDLE FvbHandle,
188 IN EFI_LBA Lba,
189 IN UINTN Offset,
190 IN UINTN *NumBytes,
191 IN VOID *Buffer
192 )
193 /*++
194
195 Routine Description:
196 Starts a target block update. This function will record data about write
197 in fault tolerant storage and will complete the write in a recoverable
198 manner, ensuring at all times that either the original contents or
199 the modified contents are available.
200
201 Arguments:
202 This - Calling context
203 FvbHandle - The handle of FVB protocol that provides services for
204 reading, writing, and erasing the target block.
205 Lba - The logical block address of the target block.
206 Offset - The offset within the target block to place the data.
207 NumBytes - The number of bytes to write to the target block.
208 Buffer - The data to write.
209
210 Returns:
211 EFI_SUCCESS - The function completed successfully
212 EFI_BAD_BUFFER_SIZE - The write would span a target block, which is not
213 a valid action.
214 EFI_ACCESS_DENIED - No writes have been allocated.
215 EFI_NOT_FOUND - Cannot find FVB by handle.
216 EFI_OUT_OF_RESOURCES - Cannot allocate memory.
217 EFI_ABORTED - The function could not complete successfully.
218
219 --*/
220 ;
221
222 //
223 // Internal functions
224 //
225 EFI_STATUS
226 FtwRestart (
227 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
228 )
229 /*++
230
231 Routine Description:
232 Restarts a previously interrupted write. The caller must provide the
233 block protocol needed to complete the interrupted write.
234
235 Arguments:
236 FtwLiteDevice - The private data of FTW_LITE driver
237 FvbHandle - The handle of FVB protocol that provides services for
238 reading, writing, and erasing the target block.
239
240 Returns:
241 EFI_SUCCESS - The function completed successfully
242 EFI_ACCESS_DENIED - No pending writes exist
243 EFI_NOT_FOUND - FVB protocol not found by the handle
244 EFI_ABORTED - The function could not complete successfully
245
246 --*/
247 ;
248
249 EFI_STATUS
250 FtwAbort (
251 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
252 )
253 /*++
254
255 Routine Description:
256 Aborts all previous allocated writes.
257
258 Arguments:
259 FtwLiteDevice - The private data of FTW_LITE driver
260
261 Returns:
262 EFI_SUCCESS - The function completed successfully
263 EFI_ABORTED - The function could not complete successfully.
264 EFI_NOT_FOUND - No allocated writes exist.
265
266 --*/
267 ;
268
269
270 EFI_STATUS
271 FtwWriteRecord (
272 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
273 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb
274 )
275 /*++
276
277 Routine Description:
278 Write a record with fault tolerant mannaer.
279 Since the content has already backuped in spare block, the write is
280 guaranteed to be completed with fault tolerant manner.
281
282 Arguments:
283 FtwLiteDevice - The private data of FTW_LITE driver
284 Fvb - The FVB protocol that provides services for
285 reading, writing, and erasing the target block.
286
287 Returns:
288 EFI_SUCCESS - The function completed successfully
289 EFI_ABORTED - The function could not complete successfully
290
291 --*/
292 ;
293
294 EFI_STATUS
295 FtwEraseBlock (
296 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
297 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
298 EFI_LBA Lba
299 )
300 /*++
301
302 Routine Description:
303 To Erase one block. The size is FTW_BLOCK_SIZE
304
305 Arguments:
306 FtwLiteDevice - Calling context
307 FvBlock - FVB Protocol interface
308 Lba - Lba of the firmware block
309
310 Returns:
311 EFI_SUCCESS - Block LBA is Erased successfully
312 Others - Error occurs
313
314 --*/
315 ;
316
317 EFI_STATUS
318 FtwEraseSpareBlock (
319 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
320 )
321 /*++
322
323 Routine Description:
324
325 Erase spare block.
326
327 Arguments:
328
329 FtwLiteDevice - Calling context
330
331 Returns:
332
333 Status code
334
335 --*/
336 ;
337
338 EFI_STATUS
339 FtwGetFvbByHandle (
340 IN EFI_HANDLE FvBlockHandle,
341 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
342 )
343 /*++
344
345 Routine Description:
346 Retrive the proper FVB protocol interface by HANDLE.
347
348 Arguments:
349 FvBlockHandle - The handle of FVB protocol that provides services for
350 reading, writing, and erasing the target block.
351 FvBlock - The interface of FVB protocol
352
353 Returns:
354 EFI_SUCCESS - The function completed successfully
355 EFI_ABORTED - The function could not complete successfully
356 --*/
357 ;
358
359 EFI_STATUS
360 GetFvbByAddress (
361 IN EFI_PHYSICAL_ADDRESS Address,
362 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
363 )
364 /*++
365
366 Routine Description:
367
368 Get firmware block by address.
369
370 Arguments:
371
372 Address - Address specified the block
373 FvBlock - The block caller wanted
374
375 Returns:
376
377 Status code
378
379 EFI_NOT_FOUND - Block not found
380
381 --*/
382 ;
383
384 BOOLEAN
385 IsInWorkingBlock (
386 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
387 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
388 EFI_LBA Lba
389 )
390 /*++
391
392 Routine Description:
393
394 Is it in working block?
395
396 Arguments:
397
398 FtwLiteDevice - Calling context
399 FvBlock - Fvb protocol instance
400 Lba - The block specified
401
402 Returns:
403
404 In working block or not
405
406 --*/
407 ;
408
409 BOOLEAN
410 IsBootBlock (
411 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
412 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
413 EFI_LBA Lba
414 )
415 /*++
416
417 Routine Description:
418
419 Check whether the block is a boot block.
420
421 Arguments:
422
423 FtwLiteDevice - Calling context
424 FvBlock - Fvb protocol instance
425 Lba - Lba value
426
427 Returns:
428
429 Is a boot block or not
430
431 --*/
432 ;
433
434 EFI_STATUS
435 FlushSpareBlockToTargetBlock (
436 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
437 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
438 EFI_LBA Lba
439 )
440 /*++
441
442 Routine Description:
443 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
444 Spare block is accessed by FTW backup FVB protocol interface. LBA is
445 FtwLiteDevice->FtwSpareLba.
446 Target block is accessed by FvBlock protocol interface. LBA is Lba.
447
448 Arguments:
449 FtwLiteDevice - The private data of FTW_LITE driver
450 FvBlock - FVB Protocol interface to access target block
451 Lba - Lba of the target block
452
453 Returns:
454 EFI_SUCCESS - Spare block content is copied to target block
455 EFI_INVALID_PARAMETER - Input parameter error
456 EFI_OUT_OF_RESOURCES - Allocate memory error
457 EFI_ABORTED - The function could not complete successfully
458
459 --*/
460 ;
461
462 EFI_STATUS
463 FlushSpareBlockToWorkingBlock (
464 EFI_FTW_LITE_DEVICE *FtwLiteDevice
465 )
466 /*++
467
468 Routine Description:
469 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
470 Spare block is accessed by FTW backup FVB protocol interface. LBA is
471 FtwLiteDevice->FtwSpareLba.
472 Working block is accessed by FTW working FVB protocol interface. LBA is
473 FtwLiteDevice->FtwWorkBlockLba.
474
475 Arguments:
476 FtwLiteDevice - The private data of FTW_LITE driver
477
478 Returns:
479 EFI_SUCCESS - Spare block content is copied to target block
480 EFI_OUT_OF_RESOURCES - Allocate memory error
481 EFI_ABORTED - The function could not complete successfully
482
483 Notes:
484 Since the working block header is important when FTW initializes, the
485 state of the operation should be handled carefully. The Crc value is
486 calculated without STATE element.
487
488 --*/
489 ;
490
491 EFI_STATUS
492 FlushSpareBlockToBootBlock (
493 EFI_FTW_LITE_DEVICE *FtwLiteDevice
494 )
495 /*++
496
497 Routine Description:
498 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
499 Spare block is accessed by FTW backup FVB protocol interface. LBA is
500 FtwLiteDevice->FtwSpareLba.
501 Boot block is accessed by BootFvb protocol interface. LBA is 0.
502
503 Arguments:
504 FtwLiteDevice - The private data of FTW_LITE driver
505
506 Returns:
507 EFI_SUCCESS - Spare block content is copied to boot block
508 EFI_INVALID_PARAMETER - Input parameter error
509 EFI_OUT_OF_RESOURCES - Allocate memory error
510 EFI_ABORTED - The function could not complete successfully
511
512 Notes:
513
514 --*/
515 ;
516
517 EFI_STATUS
518 FtwUpdateFvState (
519 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
520 IN EFI_LBA Lba,
521 IN UINTN Offset,
522 IN UINT8 NewBit
523 )
524 /*++
525
526 Routine Description:
527 Update a bit of state on a block device. The location of the bit is
528 calculated by the (Lba, Offset, bit). Here bit is determined by the
529 the name of a certain bit.
530
531 Arguments:
532 FvBlock - FVB Protocol interface to access SrcBlock and DestBlock
533 Lba - Lba of a block
534 Offset - Offset on the Lba
535 NewBit - New value that will override the old value if it can be change
536
537 Returns:
538 EFI_SUCCESS - A state bit has been updated successfully
539 Others - Access block device error.
540
541 Notes:
542 Assume all bits of State are inside the same BYTE.
543
544 EFI_ABORTED - Read block fail
545 --*/
546 ;
547
548 EFI_STATUS
549 FtwGetLastRecord (
550 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
551 OUT EFI_FTW_LITE_RECORD **FtwLastRecord
552 )
553 /*++
554
555 Routine Description:
556 Get the last Write record pointer.
557 The last record is the record whose 'complete' state hasn't been set.
558 After all, this header may be a EMPTY header entry for next Allocate.
559
560 Arguments:
561 FtwLiteDevice - Private data of this driver
562 FtwLastRecord - Pointer to retrieve the last write record
563
564 Returns:
565 EFI_SUCCESS - Get the last write record successfully
566 EFI_ABORTED - The FTW work space is damaged
567
568 --*/
569 ;
570
571 BOOLEAN
572 IsErasedFlashBuffer (
573 IN BOOLEAN Polarity,
574 IN UINT8 *Buffer,
575 IN UINTN BufferSize
576 )
577 /*++
578
579 Routine Description:
580
581 Check whether a flash buffer is erased.
582
583 Arguments:
584
585 Polarity - All 1 or all 0
586 Buffer - Buffer to check
587 BufferSize - Size of the buffer
588
589 Returns:
590
591 Erased or not.
592
593 --*/
594 ;
595
596 EFI_STATUS
597 InitWorkSpaceHeader (
598 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
599 )
600 /*++
601
602 Routine Description:
603 Initialize a work space when there is no work space.
604
605 Arguments:
606 WorkingHeader - Pointer of working block header
607
608 Returns:
609 EFI_SUCCESS - The function completed successfully
610 EFI_ABORTED - The function could not complete successfully.
611
612 --*/
613 ;
614
615 EFI_STATUS
616 WorkSpaceRefresh (
617 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
618 )
619 /*++
620
621 Routine Description:
622 Read from working block to refresh the work space in memory.
623
624 Arguments:
625 FtwLiteDevice - Point to private data of FTW driver
626
627 Returns:
628 EFI_SUCCESS - The function completed successfully
629 EFI_ABORTED - The function could not complete successfully.
630
631 --*/
632 ;
633
634 BOOLEAN
635 IsValidWorkSpace (
636 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
637 )
638 /*++
639
640 Routine Description:
641 Check to see if it is a valid work space.
642
643 Arguments:
644 WorkingHeader - Pointer of working block header
645
646 Returns:
647 EFI_SUCCESS - The function completed successfully
648 EFI_ABORTED - The function could not complete successfully.
649
650 --*/
651 ;
652
653 EFI_STATUS
654 CleanupWorkSpace (
655 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
656 IN OUT UINT8 *BlockBuffer,
657 IN UINTN BufferSize
658 )
659 /*++
660
661 Routine Description:
662 Reclaim the work space. Get rid of all the completed write records
663 and write records in the Fault Tolerant work space.
664
665 Arguments:
666 FtwLiteDevice - Point to private data of FTW driver
667 FtwSpaceBuffer - Buffer to contain the reclaimed clean data
668 BufferSize - Size of the FtwSpaceBuffer
669
670 Returns:
671 EFI_SUCCESS - The function completed successfully
672 EFI_BUFFER_TOO_SMALL - The FtwSpaceBuffer is too small
673 EFI_ABORTED - The function could not complete successfully.
674
675 --*/
676 ;
677
678 EFI_STATUS
679 FtwReclaimWorkSpace (
680 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
681 )
682 /*++
683
684 Routine Description:
685 Reclaim the work space on the working block.
686
687 Arguments:
688 FtwLiteDevice - Point to private data of FTW driver
689
690 Returns:
691 EFI_SUCCESS - The function completed successfully
692 EFI_OUT_OF_RESOURCES - Allocate memory error
693 EFI_ABORTED - The function could not complete successfully
694
695 --*/
696 ;
697
698 #endif