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