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