]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
Add SMM FTW wrapper driver since non-SMM FTW protocol can be used by some consumers...
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FaultTolerantWriteSmm.c
1 /** @file
2
3 This is a simple fault tolerant write driver that is intended to use in the SMM environment.
4
5 This boot service protocol only provides fault tolerant write capability for
6 block devices. The protocol has internal non-volatile intermediate storage
7 of the data and private information. It should be able to recover
8 automatically from a critical fault, such as power failure.
9
10 The implementation uses an FTW (Fault Tolerant Write) Work Space.
11 This work space is a memory copy of the work space on the Working Block,
12 the size of the work space is the FTW_WORK_SPACE_SIZE bytes.
13
14 The work space stores each write record as EFI_FTW_RECORD structure.
15 The spare block stores the write buffer before write to the target block.
16
17 The write record has three states to specify the different phase of write operation.
18 1) WRITE_ALLOCATED is that the record is allocated in write space.
19 The information of write operation is stored in write record structure.
20 2) SPARE_COMPLETED is that the data from write buffer is writed into the spare block as the backup.
21 3) WRITE_COMPLETED is that the data is copied from the spare block to the target block.
22
23 This driver operates the data as the whole size of spare block.
24 It first read the SpareAreaLength data from the target block into the spare memory buffer.
25 Then copy the write buffer data into the spare memory buffer.
26 Then write the spare memory buffer into the spare block.
27 Final copy the data from the spare block to the target block.
28
29 To make this drive work well, the following conditions must be satisfied:
30 1. The write NumBytes data must be fit within Spare area.
31 Offset + NumBytes <= SpareAreaLength
32 2. The whole flash range has the same block size.
33 3. Working block is an area which contains working space in its last block and has the same size as spare block.
34 4. Working Block area must be in the single one Firmware Volume Block range which FVB protocol is produced on.
35 5. Spare area must be in the single one Firmware Volume Block range which FVB protocol is produced on.
36 6. Any write data area (SpareAreaLength Area) which the data will be written into must be
37 in the single one Firmware Volume Block range which FVB protocol is produced on.
38 7. If write data area (such as Variable range) is enlarged, the spare area range must be enlarged.
39 The spare area must be enough large to store the write data before write them into the target range.
40 If one of them is not satisfied, FtwWrite may fail.
41 Usually, Spare area only takes one block. That's SpareAreaLength = BlockSize, NumberOfSpareBlock = 1.
42
43 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
44 This program and the accompanying materials
45 are licensed and made available under the terms and conditions of the BSD License
46 which accompanies this distribution. The full text of the license may be found at
47 http://opensource.org/licenses/bsd-license.php
48
49 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
50 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
51
52 **/
53
54 #include <PiSmm.h>
55 #include <Library/SmmServicesTableLib.h>
56 #include <Protocol/SmmSwapAddressRange.h>
57 #include "FaultTolerantWrite.h"
58 #include "FaultTolerantWriteSmmCommon.h"
59
60 EFI_EVENT mFvbRegistration = NULL;
61 EFI_FTW_DEVICE *mFtwDevice = NULL;
62
63 /**
64 Retrive the SMM FVB protocol interface by HANDLE.
65
66 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
67 reading, writing, and erasing the target block.
68 @param[out] FvBlock The interface of SMM FVB protocol
69
70 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
71 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
72 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
73
74 **/
75 EFI_STATUS
76 FtwGetFvbByHandle (
77 IN EFI_HANDLE FvBlockHandle,
78 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
79 )
80 {
81 //
82 // To get the SMM FVB protocol interface on the handle
83 //
84 return gSmst->SmmHandleProtocol (
85 FvBlockHandle,
86 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
87 (VOID **) FvBlock
88 );
89 }
90
91 /**
92 Retrive the SMM Swap Address Range protocol interface.
93
94 @param[out] SarProtocol The interface of SMM SAR protocol
95
96 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
97 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
98 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
99
100 **/
101 EFI_STATUS
102 FtwGetSarProtocol (
103 OUT VOID **SarProtocol
104 )
105 {
106 EFI_STATUS Status;
107
108 //
109 // Locate Smm Swap Address Range protocol
110 //
111 Status = gSmst->SmmLocateProtocol (
112 &gEfiSmmSwapAddressRangeProtocolGuid,
113 NULL,
114 SarProtocol
115 );
116 return Status;
117 }
118
119 /**
120 Function returns an array of handles that support the SMM FVB protocol
121 in a buffer allocated from pool.
122
123 @param[out] NumberHandles The number of handles returned in Buffer.
124 @param[out] Buffer A pointer to the buffer to return the requested
125 array of handles that support SMM FVB protocol.
126
127 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
128 handles in Buffer was returned in NumberHandles.
129 @retval EFI_NOT_FOUND No SMM FVB handle was found.
130 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
131 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
132
133 **/
134 EFI_STATUS
135 GetFvbCountAndBuffer (
136 OUT UINTN *NumberHandles,
137 OUT EFI_HANDLE **Buffer
138 )
139 {
140 EFI_STATUS Status;
141 UINTN BufferSize;
142
143 if ((NumberHandles == NULL) || (Buffer == NULL)) {
144 return EFI_INVALID_PARAMETER;
145 }
146
147 BufferSize = 0;
148 *NumberHandles = 0;
149 *Buffer = NULL;
150 Status = gSmst->SmmLocateHandle (
151 ByProtocol,
152 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
153 NULL,
154 &BufferSize,
155 *Buffer
156 );
157 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
158 return EFI_NOT_FOUND;
159 }
160
161 *Buffer = AllocatePool (BufferSize);
162 if (*Buffer == NULL) {
163 return EFI_OUT_OF_RESOURCES;
164 }
165
166 Status = gSmst->SmmLocateHandle (
167 ByProtocol,
168 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
169 NULL,
170 &BufferSize,
171 *Buffer
172 );
173
174 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
175 if (EFI_ERROR(Status)) {
176 *NumberHandles = 0;
177 }
178
179 return Status;
180 }
181
182
183 /**
184 Get the handle of the SMM FVB protocol by the FVB base address and attributes.
185
186 @param[in] Address The base address of SMM FVB protocol.
187 @param[in] Attributes The attributes of the SMM FVB protocol.
188 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.
189
190 @retval EFI_SUCCESS The FVB handle is found.
191 @retval EFI_ABORTED The FVB protocol is not found.
192
193 **/
194 EFI_STATUS
195 GetFvbByAddressAndAttribute (
196 IN EFI_PHYSICAL_ADDRESS Address,
197 IN EFI_FVB_ATTRIBUTES_2 Attributes,
198 OUT EFI_HANDLE *SmmFvbHandle
199 )
200 {
201 EFI_STATUS Status;
202 EFI_HANDLE *HandleBuffer;
203 UINTN HandleCount;
204 UINTN Index;
205 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
206 EFI_FVB_ATTRIBUTES_2 FvbAttributes;
207 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
208
209 //
210 // Locate all handles of SMM Fvb protocol.
211 //
212 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
213 if (EFI_ERROR (Status)) {
214 return EFI_ABORTED;
215 }
216
217 //
218 // Find the proper SMM Fvb handle by the address and attributes.
219 //
220 for (Index = 0; Index < HandleCount; Index++) {
221 Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);
222 if (EFI_ERROR (Status)) {
223 break;
224 }
225 //
226 // Compare the address.
227 //
228 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
229 if (EFI_ERROR (Status)) {
230 continue;
231 }
232 if (Address != FvbBaseAddress) {
233 continue;
234 }
235
236 //
237 // Compare the attribute.
238 //
239 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);
240 if (EFI_ERROR (Status)) {
241 continue;
242 }
243 if (Attributes != FvbAttributes) {
244 continue;
245 }
246
247 //
248 // Found the proper FVB handle.
249 //
250 *SmmFvbHandle = HandleBuffer[Index];
251 FreePool (HandleBuffer);
252 return EFI_SUCCESS;
253 }
254
255 FreePool (HandleBuffer);
256 return EFI_ABORTED;
257 }
258
259 /**
260 Communication service SMI Handler entry.
261
262 This SMI handler provides services for the fault tolerant write wrapper driver.
263
264 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
265 @param[in] RegisterContext Points to an optional handler context which was specified when the
266 handler was registered.
267 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed
268 from a non-SMM environment into an SMM environment.
269 @param[in, out] CommBufferSize The size of the CommBuffer.
270
271 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
272 should still be called.
273 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
274 still be called.
275 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
276 be called.
277 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
278
279 **/
280 EFI_STATUS
281 EFIAPI
282 SmmFaultTolerantWriteHandler (
283 IN EFI_HANDLE DispatchHandle,
284 IN CONST VOID *RegisterContext,
285 IN OUT VOID *CommBuffer,
286 IN OUT UINTN *CommBufferSize
287 )
288 {
289 EFI_STATUS Status;
290 SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;
291 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *SmmGetMaxBlockSizeHeader;
292 SMM_FTW_ALLOCATE_HEADER *SmmFtwAllocateHeader;
293 SMM_FTW_WRITE_HEADER *SmmFtwWriteHeader;
294 SMM_FTW_RESTART_HEADER *SmmFtwRestartHeader;
295 SMM_FTW_GET_LAST_WRITE_HEADER *SmmFtwGetLastWriteHeader;
296 VOID *PrivateData;
297 EFI_HANDLE SmmFvbHandle;
298
299 ASSERT (CommBuffer != NULL);
300 ASSERT (CommBufferSize != NULL);
301
302 SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)CommBuffer;
303 switch (SmmFtwFunctionHeader->Function) {
304 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE:
305 SmmGetMaxBlockSizeHeader = (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *) SmmFtwFunctionHeader->Data;
306 Status = FtwGetMaxBlockSize (
307 &mFtwDevice->FtwInstance,
308 &SmmGetMaxBlockSizeHeader->BlockSize
309 );
310 break;
311
312 case FTW_FUNCTION_ALLOCATE:
313 SmmFtwAllocateHeader = (SMM_FTW_ALLOCATE_HEADER *) SmmFtwFunctionHeader->Data;
314 Status = FtwAllocate (
315 &mFtwDevice->FtwInstance,
316 &SmmFtwAllocateHeader->CallerId,
317 SmmFtwAllocateHeader->PrivateDataSize,
318 SmmFtwAllocateHeader->NumberOfWrites
319 );
320 break;
321
322 case FTW_FUNCTION_WRITE:
323 SmmFtwWriteHeader = (SMM_FTW_WRITE_HEADER *) SmmFtwFunctionHeader->Data;
324 if (SmmFtwWriteHeader->PrivateDataSize == 0) {
325 PrivateData = NULL;
326 } else {
327 PrivateData = (VOID *)&SmmFtwWriteHeader->Data[SmmFtwWriteHeader->Length];
328 }
329 Status = GetFvbByAddressAndAttribute (
330 SmmFtwWriteHeader->FvbBaseAddress,
331 SmmFtwWriteHeader->FvbAttributes,
332 &SmmFvbHandle
333 );
334 if (!EFI_ERROR (Status)) {
335 Status = FtwWrite(
336 &mFtwDevice->FtwInstance,
337 SmmFtwWriteHeader->Lba,
338 SmmFtwWriteHeader->Offset,
339 SmmFtwWriteHeader->Length,
340 PrivateData,
341 SmmFvbHandle,
342 SmmFtwWriteHeader->Data
343 );
344 }
345 break;
346
347 case FTW_FUNCTION_RESTART:
348 SmmFtwRestartHeader = (SMM_FTW_RESTART_HEADER *) SmmFtwFunctionHeader->Data;
349 Status = GetFvbByAddressAndAttribute (
350 SmmFtwRestartHeader->FvbBaseAddress,
351 SmmFtwRestartHeader->FvbAttributes,
352 &SmmFvbHandle
353 );
354 if (!EFI_ERROR (Status)) {
355 Status = FtwRestart (&mFtwDevice->FtwInstance, SmmFvbHandle);
356 }
357 break;
358
359 case FTW_FUNCTION_ABORT:
360 Status = FtwAbort (&mFtwDevice->FtwInstance);
361 break;
362
363 case FTW_FUNCTION_GET_LAST_WRITE:
364 SmmFtwGetLastWriteHeader = (SMM_FTW_GET_LAST_WRITE_HEADER *) SmmFtwFunctionHeader->Data;
365 Status = FtwGetLastWrite (
366 &mFtwDevice->FtwInstance,
367 &SmmFtwGetLastWriteHeader->CallerId,
368 &SmmFtwGetLastWriteHeader->Lba,
369 &SmmFtwGetLastWriteHeader->Offset,
370 &SmmFtwGetLastWriteHeader->Length,
371 &SmmFtwGetLastWriteHeader->PrivateDataSize,
372 (VOID *)SmmFtwGetLastWriteHeader->Data,
373 &SmmFtwGetLastWriteHeader->Complete
374 );
375 break;
376
377 default:
378 ASSERT (FALSE);
379 Status = EFI_UNSUPPORTED;
380 }
381
382 SmmFtwFunctionHeader->ReturnStatus = Status;
383
384 return EFI_SUCCESS;
385 }
386
387
388 /**
389 SMM Firmware Volume Block Protocol notification event handler.
390
391 @param[in] Protocol Points to the protocol's unique identifier
392 @param[in] Interface Points to the interface instance
393 @param[in] Handle The handle on which the interface was installed
394
395 @retval EFI_SUCCESS SmmEventCallback runs successfully
396
397 **/
398 EFI_STATUS
399 EFIAPI
400 FvbNotificationEvent (
401 IN CONST EFI_GUID *Protocol,
402 IN VOID *Interface,
403 IN EFI_HANDLE Handle
404 )
405 {
406 EFI_STATUS Status;
407 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
408 EFI_HANDLE SmmFtwHandle;
409
410 //
411 // Just return to avoid install SMM FaultTolerantWriteProtocol again
412 // if SMM Fault Tolerant Write protocol had been installed.
413 //
414 Status = gSmst->SmmLocateProtocol (
415 &gEfiSmmFaultTolerantWriteProtocolGuid,
416 NULL,
417 (VOID **) &FtwProtocol
418 );
419 if (!EFI_ERROR (Status)) {
420 return EFI_SUCCESS;
421 }
422
423 //
424 // Found proper FVB protocol and initialize FtwDevice for protocol installation
425 //
426 Status = InitFtwProtocol (mFtwDevice);
427 if (EFI_ERROR(Status)) {
428 return Status;
429 }
430
431 //
432 // Install protocol interface
433 //
434 Status = gSmst->SmmInstallProtocolInterface (
435 &mFtwDevice->Handle,
436 &gEfiSmmFaultTolerantWriteProtocolGuid,
437 EFI_NATIVE_INTERFACE,
438 &mFtwDevice->FtwInstance
439 );
440 ASSERT_EFI_ERROR (Status);
441
442 //
443 // Notify the Ftw wrapper driver SMM Ftw is ready
444 //
445 SmmFtwHandle = NULL;
446 Status = gBS->InstallProtocolInterface (
447 &SmmFtwHandle,
448 &gEfiSmmFaultTolerantWriteProtocolGuid,
449 EFI_NATIVE_INTERFACE,
450 NULL
451 );
452 ASSERT_EFI_ERROR (Status);
453
454 return EFI_SUCCESS;
455 }
456
457
458 /**
459 This function is the entry point of the Fault Tolerant Write driver.
460
461 @param[in] ImageHandle A handle for the image that is initializing this driver
462 @param[in] SystemTable A pointer to the EFI system table
463
464 @retval EFI_SUCCESS The initialization finished successfully.
465 @retval EFI_OUT_OF_RESOURCES Allocate memory error
466 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
467
468 **/
469 EFI_STATUS
470 EFIAPI
471 SmmFaultTolerantWriteInitialize (
472 IN EFI_HANDLE ImageHandle,
473 IN EFI_SYSTEM_TABLE *SystemTable
474 )
475 {
476 EFI_STATUS Status;
477 EFI_HANDLE FtwHandle;
478
479 //
480 // Allocate private data structure for SMM FTW protocol and do some initialization
481 //
482 Status = InitFtwDevice (&mFtwDevice);
483 if (EFI_ERROR(Status)) {
484 return Status;
485 }
486
487 //
488 // Register FvbNotificationEvent () notify function.
489 //
490 Status = gSmst->SmmRegisterProtocolNotify (
491 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
492 FvbNotificationEvent,
493 &mFvbRegistration
494 );
495 ASSERT_EFI_ERROR (Status);
496
497 FvbNotificationEvent (NULL, NULL, NULL);
498
499 ///
500 /// Register SMM FTW SMI handler
501 ///
502 Status = gSmst->SmiHandlerRegister (SmmFaultTolerantWriteHandler, &gEfiSmmFaultTolerantWriteProtocolGuid, &FtwHandle);
503 ASSERT_EFI_ERROR (Status);
504
505 return EFI_SUCCESS;
506 }