]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.h
Update Comments
[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 <Guid/SystemNvDataGuid.h>
24 #include <Protocol/FaultTolerantWriteLite.h>
25 #include <Protocol/FirmwareVolumeBlock.h>
26
27 #include <Library/PcdLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/UefiBootServicesTableLib.h>
33 #include <Library/DevicePathLib.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 SIGNATURE_32 ('F', 'T', 'W', 'L')
70
71 //
72 // MACRO for FTW WORK SPACE Base & Size
73 //
74 #ifdef EFI_FTW_WORKING_OFFSET
75 #define FTW_WORK_SPACE_BASE EFI_FTW_WORKING_OFFSET
76 #else
77 #define FTW_WORK_SPACE_BASE 0x00E000
78 #endif
79
80 #ifdef EFI_FTW_WORKING_LENGTH
81 #define FTW_WORK_SPACE_SIZE EFI_FTW_WORKING_LENGTH
82 #else
83 #define FTW_WORK_SPACE_SIZE 0x002000
84 #endif
85 //
86 // MACRO for FTW header and record
87 //
88 #define FTW_LITE_RECORD_SIZE (sizeof (EFI_FTW_LITE_RECORD))
89
90 //
91 // EFI Fault tolerant protocol private data structure
92 //
93 typedef struct {
94 UINTN Signature;
95 EFI_HANDLE Handle;
96 EFI_FTW_LITE_PROTOCOL FtwLiteInstance;
97 EFI_PHYSICAL_ADDRESS WorkSpaceAddress;
98 UINTN WorkSpaceLength;
99 EFI_PHYSICAL_ADDRESS SpareAreaAddress;
100 UINTN SpareAreaLength;
101 UINTN NumberOfSpareBlock; // Number of the blocks in spare block
102 UINTN SizeOfSpareBlock; // Block size in bytes of the blocks in spare block
103 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader;
104 EFI_FTW_LITE_RECORD *FtwLastRecord;
105 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwFvBlock; // FVB of working block
106 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwBackupFvb; // FVB of spare block
107 EFI_LBA FtwSpareLba;
108 EFI_LBA FtwWorkBlockLba; // Start LBA of working block
109 EFI_LBA FtwWorkSpaceLba; // Start LBA of working space
110 UINTN FtwWorkSpaceBase; // Offset from LBA start addr
111 UINTN FtwWorkSpaceSize;
112 UINT8 *FtwWorkSpace;
113 //
114 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
115 // Allocated with EFI_FTW_LITE_DEVICE.
116 //
117 } EFI_FTW_LITE_DEVICE;
118
119 #define FTW_LITE_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_LITE_DEVICE, FtwLiteInstance, FTW_LITE_DEVICE_SIGNATURE)
120
121 //
122 // Driver entry point
123 //
124 /**
125 This function is the entry point of the Fault Tolerant Write driver.
126
127
128 @param ImageHandle A handle for the image that is initializing
129 this driver
130 @param SystemTable A pointer to the EFI system table
131
132 @retval EFI_SUCCESS FTW has finished the initialization
133 @retval EFI_ABORTED FTW initialization error
134
135 **/
136 EFI_STATUS
137 EFIAPI
138 InitializeFtwLite (
139 IN EFI_HANDLE ImageHandle,
140 IN EFI_SYSTEM_TABLE *SystemTable
141 );
142
143 //
144 // Fault Tolerant Write Protocol API
145 //
146 /**
147 Starts a target block update. This function will record data about write
148 in fault tolerant storage and will complete the write in a recoverable
149 manner, ensuring at all times that either the original contents or
150 the modified contents are available.
151
152
153 @param This Calling context
154 @param FvbHandle The handle of FVB protocol that provides services for
155 reading, writing, and erasing the target block.
156 @param Lba The logical block address of the target block.
157 @param Offset The offset within the target block to place the data.
158 @param NumBytes The number of bytes to write to the target block.
159 @param Buffer The data to write.
160
161 @retval EFI_SUCCESS The function completed successfully
162 @retval EFI_BAD_BUFFER_SIZE The write would span a target block, which is not
163 a valid action.
164 @retval EFI_ACCESS_DENIED No writes have been allocated.
165 @retval EFI_NOT_FOUND Cannot find FVB by handle.
166 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
167 @retval EFI_ABORTED The function could not complete successfully.
168
169 **/
170 EFI_STATUS
171 EFIAPI
172 FtwLiteWrite (
173 IN EFI_FTW_LITE_PROTOCOL *This,
174 IN EFI_HANDLE FvbHandle,
175 IN EFI_LBA Lba,
176 IN UINTN Offset,
177 IN OUT UINTN *NumBytes,
178 IN VOID *Buffer
179 );
180
181 //
182 // Internal functions
183 //
184 /**
185 Restarts a previously interrupted write. The caller must provide the
186 block protocol needed to complete the interrupted write.
187
188
189 @param FtwLiteDevice The private data of FTW_LITE driver
190 FvbHandle - The handle of FVB protocol that provides services for
191 reading, writing, and erasing the target block.
192
193 @retval EFI_SUCCESS The function completed successfully
194 @retval EFI_ACCESS_DENIED No pending writes exist
195 @retval EFI_NOT_FOUND FVB protocol not found by the handle
196 @retval EFI_ABORTED The function could not complete successfully
197
198 **/
199 EFI_STATUS
200 FtwRestart (
201 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
202 );
203
204 /**
205 Aborts all previous allocated writes.
206
207
208 @param FtwLiteDevice The private data of FTW_LITE driver
209
210 @retval EFI_SUCCESS The function completed successfully
211 @retval EFI_ABORTED The function could not complete successfully.
212 @retval EFI_NOT_FOUND No allocated writes exist.
213
214 **/
215 EFI_STATUS
216 FtwAbort (
217 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
218 );
219
220
221 /**
222 Write a record with fault tolerant mannaer.
223 Since the content has already backuped in spare block, the write is
224 guaranteed to be completed with fault tolerant manner.
225
226
227 @param FtwLiteDevice The private data of FTW_LITE driver
228 @param Fvb The FVB protocol that provides services for
229 reading, writing, and erasing the target block.
230
231 @retval EFI_SUCCESS The function completed successfully
232 @retval EFI_ABORTED The function could not complete successfully
233
234 **/
235 EFI_STATUS
236 FtwWriteRecord (
237 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
238 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb
239 );
240
241 /**
242 To erase the block with the spare block size.
243
244
245 @param FtwLiteDevice Calling context
246 @param FvBlock FVB Protocol interface
247 @param Lba Lba of the firmware block
248
249 @retval EFI_SUCCESS Block LBA is Erased successfully
250 @retval Others Error occurs
251
252 **/
253 EFI_STATUS
254 FtwEraseBlock (
255 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
256 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
257 EFI_LBA Lba
258 );
259
260 /**
261
262 Erase spare block.
263
264
265 @param FtwLiteDevice Calling context
266
267 @retval EFI_SUCCESS The erase request was successfully
268 completed.
269
270 @retval EFI_ACCESS_DENIED The firmware volume is in the
271 WriteDisabled state.
272 @retval EFI_DEVICE_ERROR The block device is not functioning
273 correctly and could not be written.
274 The firmware device may have been
275 partially erased.
276 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
277 in the variable argument list do
278 not exist in the firmware volume.
279
280 **/
281 EFI_STATUS
282 FtwEraseSpareBlock (
283 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
284 );
285
286 /**
287 Retrive the proper FVB protocol interface by HANDLE.
288
289
290 @param FvBlockHandle The handle of FVB protocol that provides services for
291 reading, writing, and erasing the target block.
292 @param FvBlock The interface of FVB protocol
293
294 @retval EFI_SUCCESS The function completed successfully
295 @retval EFI_ABORTED The function could not complete successfully
296
297 **/
298 EFI_STATUS
299 FtwGetFvbByHandle (
300 IN EFI_HANDLE FvBlockHandle,
301 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
302 );
303
304 /**
305
306 Get firmware block by address.
307
308
309 @param Address Address specified the block
310 @param FvBlock The block caller wanted
311
312 @retval EFI_SUCCESS The protocol instance if found.
313 @retval EFI_NOT_FOUND Block not found
314
315 **/
316 EFI_STATUS
317 GetFvbByAddress (
318 IN EFI_PHYSICAL_ADDRESS Address,
319 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
320 );
321
322 /**
323
324 Is it in working block?
325
326
327 @param FtwLiteDevice Calling context
328 @param FvBlock Fvb protocol instance
329 @param Lba The block specified
330
331 @return A BOOLEAN value indicating in working block or not.
332
333 **/
334 BOOLEAN
335 IsInWorkingBlock (
336 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
337 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
338 EFI_LBA Lba
339 );
340
341 /**
342 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
343 Spare block is accessed by FTW backup FVB protocol interface. LBA is
344 FtwLiteDevice->FtwSpareLba.
345 Target block is accessed by FvBlock protocol interface. LBA is Lba.
346
347
348 @param FtwLiteDevice The private data of FTW_LITE driver
349 @param FvBlock FVB Protocol interface to access target block
350 @param Lba Lba of the target block
351
352 @retval EFI_SUCCESS Spare block content is copied to target block
353 @retval EFI_INVALID_PARAMETER Input parameter error
354 @retval EFI_OUT_OF_RESOURCES Allocate memory error
355 @retval EFI_ABORTED The function could not complete successfully
356
357 **/
358 EFI_STATUS
359 FlushSpareBlockToTargetBlock (
360 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
361 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
362 EFI_LBA Lba
363 );
364
365 /**
366 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
367 Spare block is accessed by FTW backup FVB protocol interface. LBA is
368 FtwLiteDevice->FtwSpareLba.
369 Working block is accessed by FTW working FVB protocol interface. LBA is
370 FtwLiteDevice->FtwWorkBlockLba.
371
372
373 @param FtwLiteDevice The private data of FTW_LITE driver
374
375 @retval EFI_SUCCESS Spare block content is copied to target block
376 @retval EFI_OUT_OF_RESOURCES Allocate memory error
377 @retval EFI_ABORTED The function could not complete successfully
378 Notes:
379 Since the working block header is important when FTW initializes, the
380 state of the operation should be handled carefully. The Crc value is
381 calculated without STATE element.
382
383 **/
384 EFI_STATUS
385 FlushSpareBlockToWorkingBlock (
386 EFI_FTW_LITE_DEVICE *FtwLiteDevice
387 );
388
389 /**
390 Update a bit of state on a block device. The location of the bit is
391 calculated by the (Lba, Offset, bit). Here bit is determined by the
392 the name of a certain bit.
393
394
395 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
396 @param Lba Lba of a block
397 @param Offset Offset on the Lba
398 @param NewBit New value that will override the old value if it can be change
399
400 @retval EFI_SUCCESS A state bit has been updated successfully
401 @retval Others Access block device error.
402 Notes:
403 Assume all bits of State are inside the same BYTE.
404 @retval EFI_ABORTED Read block fail
405
406 **/
407 EFI_STATUS
408 FtwUpdateFvState (
409 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
410 IN EFI_LBA Lba,
411 IN UINTN Offset,
412 IN UINT8 NewBit
413 );
414
415 /**
416 Get the last Write record pointer.
417 The last record is the record whose 'complete' state hasn't been set.
418 After all, this header may be a EMPTY header entry for next Allocate.
419
420
421 @param FtwLiteDevice Private data of this driver
422 @param FtwLastRecord Pointer to retrieve the last write record
423
424 @retval EFI_SUCCESS Get the last write record successfully
425 @retval EFI_ABORTED The FTW work space is damaged
426
427 **/
428 EFI_STATUS
429 FtwGetLastRecord (
430 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
431 OUT EFI_FTW_LITE_RECORD **FtwLastRecord
432 );
433
434 /**
435
436 Check whether a flash buffer is erased.
437
438
439 @param Polarity All 1 or all 0
440 @param Buffer Buffer to check
441 @param BufferSize Size of the buffer
442
443 @return A BOOLEAN value indicating erased or not.
444
445 **/
446 BOOLEAN
447 IsErasedFlashBuffer (
448 IN BOOLEAN Polarity,
449 IN UINT8 *Buffer,
450 IN UINTN BufferSize
451 );
452
453 /**
454 Initialize a work space when there is no work space.
455
456
457 @param WorkingHeader Pointer of working block header
458
459 @retval EFI_SUCCESS The function completed successfully
460 @retval EFI_ABORTED The function could not complete successfully.
461
462 **/
463 EFI_STATUS
464 InitWorkSpaceHeader (
465 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
466 );
467
468 /**
469 Read from working block to refresh the work space in memory.
470
471
472 @param FtwLiteDevice Point to private data of FTW driver
473
474 @retval EFI_SUCCESS The function completed successfully
475 @retval EFI_ABORTED The function could not complete successfully.
476
477 **/
478 EFI_STATUS
479 WorkSpaceRefresh (
480 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
481 );
482
483 /**
484 Check to see if it is a valid work space.
485
486
487 @param WorkingHeader Pointer of working block header
488
489 @retval EFI_SUCCESS The function completed successfully
490 @retval EFI_ABORTED The function could not complete successfully.
491
492 **/
493 BOOLEAN
494 IsValidWorkSpace (
495 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
496 );
497
498 /**
499 Reclaim the work space on the working block.
500
501
502 @param FtwLiteDevice Point to private data of FTW driver
503 @param PreserveRecord Whether to preserve the working record is needed
504
505 @retval EFI_SUCCESS The function completed successfully
506 @retval EFI_OUT_OF_RESOURCES Allocate memory error
507 @retval EFI_ABORTED The function could not complete successfully
508
509 **/
510 EFI_STATUS
511 FtwReclaimWorkSpace (
512 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
513 IN BOOLEAN PreserveRecord
514 );
515
516 #endif