]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.h
clean up the un-suitable ';' location when declaring the functions.
[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 // Fault Tolerant Write Protocol API
157 //
158 /**
159 Starts a target block update. This function will record data about write
160 in fault tolerant storage and will complete the write in a recoverable
161 manner, ensuring at all times that either the original contents or
162 the modified contents are available.
163
164
165 @param This Calling context
166 @param FvbHandle The handle of FVB protocol that provides services for
167 reading, writing, and erasing the target block.
168 @param Lba The logical block address of the target block.
169 @param Offset The offset within the target block to place the data.
170 @param NumBytes The number of bytes to write to the target block.
171 @param Buffer The data to write.
172
173 @retval EFI_SUCCESS The function completed successfully
174 @retval EFI_BAD_BUFFER_SIZE The write would span a target block, which is not
175 a valid action.
176 @retval EFI_ACCESS_DENIED No writes have been allocated.
177 @retval EFI_NOT_FOUND Cannot find FVB by handle.
178 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
179 @retval EFI_ABORTED The function could not complete successfully.
180
181 **/
182 EFI_STATUS
183 EFIAPI
184 FtwLiteWrite (
185 IN EFI_FTW_LITE_PROTOCOL *This,
186 IN EFI_HANDLE FvbHandle,
187 IN EFI_LBA Lba,
188 IN UINTN Offset,
189 IN OUT UINTN *NumBytes,
190 IN VOID *Buffer
191 );
192
193 //
194 // Internal functions
195 //
196 /**
197 Restarts a previously interrupted write. The caller must provide the
198 block protocol needed to complete the interrupted write.
199
200
201 @param FtwLiteDevice The private data of FTW_LITE driver
202 FvbHandle - The handle of FVB protocol that provides services for
203 reading, writing, and erasing the target block.
204
205 @retval EFI_SUCCESS The function completed successfully
206 @retval EFI_ACCESS_DENIED No pending writes exist
207 @retval EFI_NOT_FOUND FVB protocol not found by the handle
208 @retval EFI_ABORTED The function could not complete successfully
209
210 **/
211 EFI_STATUS
212 FtwRestart (
213 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
214 );
215
216 /**
217 Aborts all previous allocated writes.
218
219
220 @param FtwLiteDevice The private data of FTW_LITE driver
221
222 @retval EFI_SUCCESS The function completed successfully
223 @retval EFI_ABORTED The function could not complete successfully.
224 @retval EFI_NOT_FOUND No allocated writes exist.
225
226 **/
227 EFI_STATUS
228 FtwAbort (
229 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
230 );
231
232
233 /**
234 Write a record with fault tolerant mannaer.
235 Since the content has already backuped in spare block, the write is
236 guaranteed to be completed with fault tolerant manner.
237
238
239 @param FtwLiteDevice The private data of FTW_LITE driver
240 @param Fvb The FVB protocol that provides services for
241 reading, writing, and erasing the target block.
242
243 @retval EFI_SUCCESS The function completed successfully
244 @retval EFI_ABORTED The function could not complete successfully
245
246 **/
247 EFI_STATUS
248 FtwWriteRecord (
249 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
250 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb
251 );
252
253 /**
254 To Erase one block. The size is FTW_BLOCK_SIZE
255
256
257 @param FtwLiteDevice Calling context
258 @param FvBlock FVB Protocol interface
259 @param Lba Lba of the firmware block
260
261 @retval EFI_SUCCESS Block LBA is Erased successfully
262 @retval Others Error occurs
263
264 **/
265 EFI_STATUS
266 FtwEraseBlock (
267 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
268 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
269 EFI_LBA Lba
270 );
271
272 /**
273
274 Erase spare block.
275
276
277 @param FtwLiteDevice Calling context
278
279 @retval EFI_SUCCESS The erase request was successfully
280 completed.
281
282 @retval EFI_ACCESS_DENIED The firmware volume is in the
283 WriteDisabled state.
284 @retval EFI_DEVICE_ERROR The block device is not functioning
285 correctly and could not be written.
286 The firmware device may have been
287 partially erased.
288 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
289 in the variable argument list do
290 not exist in the firmware volume.
291
292 **/
293 EFI_STATUS
294 FtwEraseSpareBlock (
295 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
296 );
297
298 /**
299 Retrive the proper FVB protocol interface by HANDLE.
300
301
302 @param FvBlockHandle The handle of FVB protocol that provides services for
303 reading, writing, and erasing the target block.
304 @param FvBlock The interface of FVB protocol
305
306 @retval EFI_SUCCESS The function completed successfully
307 @retval EFI_ABORTED The function could not complete successfully
308
309 **/
310 EFI_STATUS
311 FtwGetFvbByHandle (
312 IN EFI_HANDLE FvBlockHandle,
313 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
314 );
315
316 /**
317
318 Get firmware block by address.
319
320
321 @param Address Address specified the block
322 @param FvBlock The block caller wanted
323
324 @retval EFI_SUCCESS The protocol instance if found.
325 @retval EFI_NOT_FOUND Block not found
326
327 **/
328 EFI_STATUS
329 GetFvbByAddress (
330 IN EFI_PHYSICAL_ADDRESS Address,
331 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
332 );
333
334 /**
335
336 Is it in working block?
337
338
339 @param FtwLiteDevice Calling context
340 @param FvBlock Fvb protocol instance
341 @param Lba The block specified
342
343 @return A BOOLEAN value indicating in working block or not.
344
345 **/
346 BOOLEAN
347 IsInWorkingBlock (
348 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
349 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
350 EFI_LBA Lba
351 );
352
353 /**
354
355 Check whether the block is a boot block.
356
357
358 @param FtwLiteDevice Calling context
359 @param FvBlock Fvb protocol instance
360 @param Lba Lba value
361
362 @retval FALSE This is a boot block.
363 @retval TRUE This is not a boot block.
364
365 **/
366 BOOLEAN
367 IsBootBlock (
368 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
369 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
370 EFI_LBA Lba
371 );
372
373 /**
374 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
375 Spare block is accessed by FTW backup FVB protocol interface. LBA is
376 FtwLiteDevice->FtwSpareLba.
377 Target block is accessed by FvBlock protocol interface. LBA is Lba.
378
379
380 @param FtwLiteDevice The private data of FTW_LITE driver
381 @param FvBlock FVB Protocol interface to access target block
382 @param Lba Lba of the target block
383
384 @retval EFI_SUCCESS Spare block content is copied to target block
385 @retval EFI_INVALID_PARAMETER Input parameter error
386 @retval EFI_OUT_OF_RESOURCES Allocate memory error
387 @retval EFI_ABORTED The function could not complete successfully
388
389 **/
390 EFI_STATUS
391 FlushSpareBlockToTargetBlock (
392 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
393 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
394 EFI_LBA Lba
395 );
396
397 /**
398 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
399 Spare block is accessed by FTW backup FVB protocol interface. LBA is
400 FtwLiteDevice->FtwSpareLba.
401 Working block is accessed by FTW working FVB protocol interface. LBA is
402 FtwLiteDevice->FtwWorkBlockLba.
403
404
405 @param FtwLiteDevice The private data of FTW_LITE driver
406
407 @retval EFI_SUCCESS Spare block content is copied to target block
408 @retval EFI_OUT_OF_RESOURCES Allocate memory error
409 @retval EFI_ABORTED The function could not complete successfully
410 Notes:
411 Since the working block header is important when FTW initializes, the
412 state of the operation should be handled carefully. The Crc value is
413 calculated without STATE element.
414
415 **/
416 EFI_STATUS
417 FlushSpareBlockToWorkingBlock (
418 EFI_FTW_LITE_DEVICE *FtwLiteDevice
419 );
420
421 /**
422 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
423 Spare block is accessed by FTW backup FVB protocol interface. LBA is
424 FtwLiteDevice->FtwSpareLba.
425 Boot block is accessed by BootFvb protocol interface. LBA is 0.
426
427
428 @param FtwLiteDevice The private data of FTW_LITE driver
429
430 @retval EFI_SUCCESS Spare block content is copied to boot block
431 @retval EFI_INVALID_PARAMETER Input parameter error
432 @retval EFI_OUT_OF_RESOURCES Allocate memory error
433 @retval EFI_ABORTED The function could not complete successfully
434 Notes:
435
436 **/
437 EFI_STATUS
438 FlushSpareBlockToBootBlock (
439 EFI_FTW_LITE_DEVICE *FtwLiteDevice
440 );
441
442 /**
443 Update a bit of state on a block device. The location of the bit is
444 calculated by the (Lba, Offset, bit). Here bit is determined by the
445 the name of a certain bit.
446
447
448 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
449 @param Lba Lba of a block
450 @param Offset Offset on the Lba
451 @param NewBit New value that will override the old value if it can be change
452
453 @retval EFI_SUCCESS A state bit has been updated successfully
454 @retval Others Access block device error.
455 Notes:
456 Assume all bits of State are inside the same BYTE.
457 @retval EFI_ABORTED Read block fail
458
459 **/
460 EFI_STATUS
461 FtwUpdateFvState (
462 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
463 IN EFI_LBA Lba,
464 IN UINTN Offset,
465 IN UINT8 NewBit
466 );
467
468 /**
469 Get the last Write record pointer.
470 The last record is the record whose 'complete' state hasn't been set.
471 After all, this header may be a EMPTY header entry for next Allocate.
472
473
474 @param FtwLiteDevice Private data of this driver
475 @param FtwLastRecord Pointer to retrieve the last write record
476
477 @retval EFI_SUCCESS Get the last write record successfully
478 @retval EFI_ABORTED The FTW work space is damaged
479
480 **/
481 EFI_STATUS
482 FtwGetLastRecord (
483 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
484 OUT EFI_FTW_LITE_RECORD **FtwLastRecord
485 );
486
487 /**
488
489 Check whether a flash buffer is erased.
490
491
492 @param Polarity All 1 or all 0
493 @param Buffer Buffer to check
494 @param BufferSize Size of the buffer
495
496 @return A BOOLEAN value indicating erased or not.
497
498 **/
499 BOOLEAN
500 IsErasedFlashBuffer (
501 IN BOOLEAN Polarity,
502 IN UINT8 *Buffer,
503 IN UINTN BufferSize
504 );
505
506 /**
507 Initialize a work space when there is no work space.
508
509
510 @param WorkingHeader Pointer of working block header
511
512 @retval EFI_SUCCESS The function completed successfully
513 @retval EFI_ABORTED The function could not complete successfully.
514
515 **/
516 EFI_STATUS
517 InitWorkSpaceHeader (
518 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
519 );
520
521 /**
522 Read from working block to refresh the work space in memory.
523
524
525 @param FtwLiteDevice Point to private data of FTW driver
526
527 @retval EFI_SUCCESS The function completed successfully
528 @retval EFI_ABORTED The function could not complete successfully.
529
530 **/
531 EFI_STATUS
532 WorkSpaceRefresh (
533 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
534 );
535
536 /**
537 Check to see if it is a valid work space.
538
539
540 @param WorkingHeader Pointer of working block header
541
542 @retval EFI_SUCCESS The function completed successfully
543 @retval EFI_ABORTED The function could not complete successfully.
544
545 **/
546 BOOLEAN
547 IsValidWorkSpace (
548 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
549 );
550
551 /**
552 Reclaim the work space on the working block.
553
554
555 @param FtwLiteDevice Point to private data of FTW driver
556 @param PreserveRecord Whether to preserve the working record is needed
557
558 @retval EFI_SUCCESS The function completed successfully
559 @retval EFI_OUT_OF_RESOURCES Allocate memory error
560 @retval EFI_ABORTED The function could not complete successfully
561
562 **/
563 EFI_STATUS
564 FtwReclaimWorkSpace (
565 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
566 IN BOOLEAN PreserveRecord
567 );
568
569 #endif