]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
Add SMRAM range check to fault tolerant write SMM SMI handler.
[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 Caution: This module requires additional review when modified.
44 This driver need to make sure the CommBuffer is not in the SMRAM range.
45
46 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
47 This program and the accompanying materials
48 are licensed and made available under the terms and conditions of the BSD License
49 which accompanies this distribution. The full text of the license may be found at
50 http://opensource.org/licenses/bsd-license.php
51
52 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
53 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
54
55 **/
56
57 #include <PiSmm.h>
58 #include <Library/SmmServicesTableLib.h>
59 #include <Protocol/SmmSwapAddressRange.h>
60 #include "FaultTolerantWrite.h"
61 #include "FaultTolerantWriteSmmCommon.h"
62 #include <Protocol/SmmAccess2.h>
63
64 EFI_EVENT mFvbRegistration = NULL;
65 EFI_FTW_DEVICE *mFtwDevice = NULL;
66 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
67 UINTN mSmramRangeCount;
68
69
70 /**
71 This function check if the address is in SMRAM.
72
73 @param Buffer the buffer address to be checked.
74 @param Length the buffer length to be checked.
75
76 @retval TRUE this address is in SMRAM.
77 @retval FALSE this address is NOT in SMRAM.
78 **/
79 BOOLEAN
80 InternalIsAddressInSmram (
81 IN EFI_PHYSICAL_ADDRESS Buffer,
82 IN UINT64 Length
83 )
84 {
85 UINTN Index;
86
87 for (Index = 0; Index < mSmramRangeCount; Index ++) {
88 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
89 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {
90 return TRUE;
91 }
92 }
93
94 return FALSE;
95 }
96
97
98 /**
99 Retrive the SMM FVB protocol interface by HANDLE.
100
101 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
102 reading, writing, and erasing the target block.
103 @param[out] FvBlock The interface of SMM FVB protocol
104
105 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
106 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
107 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
108
109 **/
110 EFI_STATUS
111 FtwGetFvbByHandle (
112 IN EFI_HANDLE FvBlockHandle,
113 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
114 )
115 {
116 //
117 // To get the SMM FVB protocol interface on the handle
118 //
119 return gSmst->SmmHandleProtocol (
120 FvBlockHandle,
121 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
122 (VOID **) FvBlock
123 );
124 }
125
126 /**
127 Retrive the SMM Swap Address Range protocol interface.
128
129 @param[out] SarProtocol The interface of SMM SAR protocol
130
131 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
132 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
133 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
134
135 **/
136 EFI_STATUS
137 FtwGetSarProtocol (
138 OUT VOID **SarProtocol
139 )
140 {
141 EFI_STATUS Status;
142
143 //
144 // Locate Smm Swap Address Range protocol
145 //
146 Status = gSmst->SmmLocateProtocol (
147 &gEfiSmmSwapAddressRangeProtocolGuid,
148 NULL,
149 SarProtocol
150 );
151 return Status;
152 }
153
154 /**
155 Function returns an array of handles that support the SMM FVB protocol
156 in a buffer allocated from pool.
157
158 @param[out] NumberHandles The number of handles returned in Buffer.
159 @param[out] Buffer A pointer to the buffer to return the requested
160 array of handles that support SMM FVB protocol.
161
162 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
163 handles in Buffer was returned in NumberHandles.
164 @retval EFI_NOT_FOUND No SMM FVB handle was found.
165 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
166 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
167
168 **/
169 EFI_STATUS
170 GetFvbCountAndBuffer (
171 OUT UINTN *NumberHandles,
172 OUT EFI_HANDLE **Buffer
173 )
174 {
175 EFI_STATUS Status;
176 UINTN BufferSize;
177
178 if ((NumberHandles == NULL) || (Buffer == NULL)) {
179 return EFI_INVALID_PARAMETER;
180 }
181
182 BufferSize = 0;
183 *NumberHandles = 0;
184 *Buffer = NULL;
185 Status = gSmst->SmmLocateHandle (
186 ByProtocol,
187 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
188 NULL,
189 &BufferSize,
190 *Buffer
191 );
192 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
193 return EFI_NOT_FOUND;
194 }
195
196 *Buffer = AllocatePool (BufferSize);
197 if (*Buffer == NULL) {
198 return EFI_OUT_OF_RESOURCES;
199 }
200
201 Status = gSmst->SmmLocateHandle (
202 ByProtocol,
203 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
204 NULL,
205 &BufferSize,
206 *Buffer
207 );
208
209 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
210 if (EFI_ERROR(Status)) {
211 *NumberHandles = 0;
212 }
213
214 return Status;
215 }
216
217
218 /**
219 Get the handle of the SMM FVB protocol by the FVB base address and attributes.
220
221 @param[in] Address The base address of SMM FVB protocol.
222 @param[in] Attributes The attributes of the SMM FVB protocol.
223 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.
224
225 @retval EFI_SUCCESS The FVB handle is found.
226 @retval EFI_ABORTED The FVB protocol is not found.
227
228 **/
229 EFI_STATUS
230 GetFvbByAddressAndAttribute (
231 IN EFI_PHYSICAL_ADDRESS Address,
232 IN EFI_FVB_ATTRIBUTES_2 Attributes,
233 OUT EFI_HANDLE *SmmFvbHandle
234 )
235 {
236 EFI_STATUS Status;
237 EFI_HANDLE *HandleBuffer;
238 UINTN HandleCount;
239 UINTN Index;
240 EFI_PHYSICAL_ADDRESS FvbBaseAddress;
241 EFI_FVB_ATTRIBUTES_2 FvbAttributes;
242 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
243
244 //
245 // Locate all handles of SMM Fvb protocol.
246 //
247 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
248 if (EFI_ERROR (Status)) {
249 return EFI_ABORTED;
250 }
251
252 //
253 // Find the proper SMM Fvb handle by the address and attributes.
254 //
255 for (Index = 0; Index < HandleCount; Index++) {
256 Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);
257 if (EFI_ERROR (Status)) {
258 break;
259 }
260 //
261 // Compare the address.
262 //
263 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
264 if (EFI_ERROR (Status)) {
265 continue;
266 }
267 if (Address != FvbBaseAddress) {
268 continue;
269 }
270
271 //
272 // Compare the attribute.
273 //
274 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);
275 if (EFI_ERROR (Status)) {
276 continue;
277 }
278 if (Attributes != FvbAttributes) {
279 continue;
280 }
281
282 //
283 // Found the proper FVB handle.
284 //
285 *SmmFvbHandle = HandleBuffer[Index];
286 FreePool (HandleBuffer);
287 return EFI_SUCCESS;
288 }
289
290 FreePool (HandleBuffer);
291 return EFI_ABORTED;
292 }
293
294 /**
295 Communication service SMI Handler entry.
296
297 This SMI handler provides services for the fault tolerant write wrapper driver.
298
299 Caution: This function requires additional review when modified.
300 This driver need to make sure the CommBuffer is not in the SMRAM range.
301 Also in FTW_FUNCTION_GET_LAST_WRITE case, check SmmFtwGetLastWriteHeader->Data +
302 SmmFtwGetLastWriteHeader->PrivateDataSize within communication buffer.
303
304 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
305 @param[in] RegisterContext Points to an optional handler context which was specified when the
306 handler was registered.
307 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed
308 from a non-SMM environment into an SMM environment.
309 @param[in, out] CommBufferSize The size of the CommBuffer.
310
311 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
312 should still be called.
313 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
314 still be called.
315 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
316 be called.
317 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
318
319 **/
320 EFI_STATUS
321 EFIAPI
322 SmmFaultTolerantWriteHandler (
323 IN EFI_HANDLE DispatchHandle,
324 IN CONST VOID *RegisterContext,
325 IN OUT VOID *CommBuffer,
326 IN OUT UINTN *CommBufferSize
327 )
328 {
329 EFI_STATUS Status;
330 SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;
331 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *SmmGetMaxBlockSizeHeader;
332 SMM_FTW_ALLOCATE_HEADER *SmmFtwAllocateHeader;
333 SMM_FTW_WRITE_HEADER *SmmFtwWriteHeader;
334 SMM_FTW_RESTART_HEADER *SmmFtwRestartHeader;
335 SMM_FTW_GET_LAST_WRITE_HEADER *SmmFtwGetLastWriteHeader;
336 VOID *PrivateData;
337 EFI_HANDLE SmmFvbHandle;
338
339 ASSERT (CommBuffer != NULL);
340 ASSERT (CommBufferSize != NULL);
341
342 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBuffer, *CommBufferSize)) {
343 DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n"));
344 return EFI_SUCCESS;
345 }
346
347 SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)CommBuffer;
348 switch (SmmFtwFunctionHeader->Function) {
349 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE:
350 SmmGetMaxBlockSizeHeader = (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *) SmmFtwFunctionHeader->Data;
351 Status = FtwGetMaxBlockSize (
352 &mFtwDevice->FtwInstance,
353 &SmmGetMaxBlockSizeHeader->BlockSize
354 );
355 break;
356
357 case FTW_FUNCTION_ALLOCATE:
358 SmmFtwAllocateHeader = (SMM_FTW_ALLOCATE_HEADER *) SmmFtwFunctionHeader->Data;
359 Status = FtwAllocate (
360 &mFtwDevice->FtwInstance,
361 &SmmFtwAllocateHeader->CallerId,
362 SmmFtwAllocateHeader->PrivateDataSize,
363 SmmFtwAllocateHeader->NumberOfWrites
364 );
365 break;
366
367 case FTW_FUNCTION_WRITE:
368 SmmFtwWriteHeader = (SMM_FTW_WRITE_HEADER *) SmmFtwFunctionHeader->Data;
369 if (SmmFtwWriteHeader->PrivateDataSize == 0) {
370 PrivateData = NULL;
371 } else {
372 PrivateData = (VOID *)&SmmFtwWriteHeader->Data[SmmFtwWriteHeader->Length];
373 }
374 Status = GetFvbByAddressAndAttribute (
375 SmmFtwWriteHeader->FvbBaseAddress,
376 SmmFtwWriteHeader->FvbAttributes,
377 &SmmFvbHandle
378 );
379 if (!EFI_ERROR (Status)) {
380 Status = FtwWrite(
381 &mFtwDevice->FtwInstance,
382 SmmFtwWriteHeader->Lba,
383 SmmFtwWriteHeader->Offset,
384 SmmFtwWriteHeader->Length,
385 PrivateData,
386 SmmFvbHandle,
387 SmmFtwWriteHeader->Data
388 );
389 }
390 break;
391
392 case FTW_FUNCTION_RESTART:
393 SmmFtwRestartHeader = (SMM_FTW_RESTART_HEADER *) SmmFtwFunctionHeader->Data;
394 Status = GetFvbByAddressAndAttribute (
395 SmmFtwRestartHeader->FvbBaseAddress,
396 SmmFtwRestartHeader->FvbAttributes,
397 &SmmFvbHandle
398 );
399 if (!EFI_ERROR (Status)) {
400 Status = FtwRestart (&mFtwDevice->FtwInstance, SmmFvbHandle);
401 }
402 break;
403
404 case FTW_FUNCTION_ABORT:
405 Status = FtwAbort (&mFtwDevice->FtwInstance);
406 break;
407
408 case FTW_FUNCTION_GET_LAST_WRITE:
409 SmmFtwGetLastWriteHeader = (SMM_FTW_GET_LAST_WRITE_HEADER *) SmmFtwFunctionHeader->Data;
410 if (((UINT8*)SmmFtwGetLastWriteHeader->Data > (UINT8*)CommBuffer) &&
411 ((UINT8*)SmmFtwGetLastWriteHeader->Data + SmmFtwGetLastWriteHeader->PrivateDataSize <= (UINT8*)CommBuffer + (*CommBufferSize))) {
412 Status = FtwGetLastWrite (
413 &mFtwDevice->FtwInstance,
414 &SmmFtwGetLastWriteHeader->CallerId,
415 &SmmFtwGetLastWriteHeader->Lba,
416 &SmmFtwGetLastWriteHeader->Offset,
417 &SmmFtwGetLastWriteHeader->Length,
418 &SmmFtwGetLastWriteHeader->PrivateDataSize,
419 (VOID *)SmmFtwGetLastWriteHeader->Data,
420 &SmmFtwGetLastWriteHeader->Complete
421 );
422 } else {
423 Status = EFI_INVALID_PARAMETER;
424 }
425 break;
426
427 default:
428 Status = EFI_UNSUPPORTED;
429 }
430
431 SmmFtwFunctionHeader->ReturnStatus = Status;
432
433 return EFI_SUCCESS;
434 }
435
436
437 /**
438 SMM Firmware Volume Block Protocol notification event handler.
439
440 @param[in] Protocol Points to the protocol's unique identifier
441 @param[in] Interface Points to the interface instance
442 @param[in] Handle The handle on which the interface was installed
443
444 @retval EFI_SUCCESS SmmEventCallback runs successfully
445
446 **/
447 EFI_STATUS
448 EFIAPI
449 FvbNotificationEvent (
450 IN CONST EFI_GUID *Protocol,
451 IN VOID *Interface,
452 IN EFI_HANDLE Handle
453 )
454 {
455 EFI_STATUS Status;
456 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
457 EFI_HANDLE SmmFtwHandle;
458
459 //
460 // Just return to avoid install SMM FaultTolerantWriteProtocol again
461 // if SMM Fault Tolerant Write protocol had been installed.
462 //
463 Status = gSmst->SmmLocateProtocol (
464 &gEfiSmmFaultTolerantWriteProtocolGuid,
465 NULL,
466 (VOID **) &FtwProtocol
467 );
468 if (!EFI_ERROR (Status)) {
469 return EFI_SUCCESS;
470 }
471
472 //
473 // Found proper FVB protocol and initialize FtwDevice for protocol installation
474 //
475 Status = InitFtwProtocol (mFtwDevice);
476 if (EFI_ERROR(Status)) {
477 return Status;
478 }
479
480 //
481 // Install protocol interface
482 //
483 Status = gSmst->SmmInstallProtocolInterface (
484 &mFtwDevice->Handle,
485 &gEfiSmmFaultTolerantWriteProtocolGuid,
486 EFI_NATIVE_INTERFACE,
487 &mFtwDevice->FtwInstance
488 );
489 ASSERT_EFI_ERROR (Status);
490
491 //
492 // Notify the Ftw wrapper driver SMM Ftw is ready
493 //
494 SmmFtwHandle = NULL;
495 Status = gBS->InstallProtocolInterface (
496 &SmmFtwHandle,
497 &gEfiSmmFaultTolerantWriteProtocolGuid,
498 EFI_NATIVE_INTERFACE,
499 NULL
500 );
501 ASSERT_EFI_ERROR (Status);
502
503 return EFI_SUCCESS;
504 }
505
506
507 /**
508 This function is the entry point of the Fault Tolerant Write driver.
509
510 @param[in] ImageHandle A handle for the image that is initializing this driver
511 @param[in] SystemTable A pointer to the EFI system table
512
513 @retval EFI_SUCCESS The initialization finished successfully.
514 @retval EFI_OUT_OF_RESOURCES Allocate memory error
515 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
516
517 **/
518 EFI_STATUS
519 EFIAPI
520 SmmFaultTolerantWriteInitialize (
521 IN EFI_HANDLE ImageHandle,
522 IN EFI_SYSTEM_TABLE *SystemTable
523 )
524 {
525 EFI_STATUS Status;
526 EFI_HANDLE FtwHandle;
527 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
528 UINTN Size;
529
530 //
531 // Allocate private data structure for SMM FTW protocol and do some initialization
532 //
533 Status = InitFtwDevice (&mFtwDevice);
534 if (EFI_ERROR(Status)) {
535 return Status;
536 }
537
538 //
539 // Get SMRAM information
540 //
541 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
542 ASSERT_EFI_ERROR (Status);
543
544 Size = 0;
545 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
546 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
547
548 Status = gSmst->SmmAllocatePool (
549 EfiRuntimeServicesData,
550 Size,
551 (VOID **)&mSmramRanges
552 );
553 ASSERT_EFI_ERROR (Status);
554
555 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
556 ASSERT_EFI_ERROR (Status);
557
558 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
559
560 //
561 // Register FvbNotificationEvent () notify function.
562 //
563 Status = gSmst->SmmRegisterProtocolNotify (
564 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
565 FvbNotificationEvent,
566 &mFvbRegistration
567 );
568 ASSERT_EFI_ERROR (Status);
569
570 FvbNotificationEvent (NULL, NULL, NULL);
571
572 ///
573 /// Register SMM FTW SMI handler
574 ///
575 Status = gSmst->SmiHandlerRegister (SmmFaultTolerantWriteHandler, &gEfiSmmFaultTolerantWriteProtocolGuid, &FtwHandle);
576 ASSERT_EFI_ERROR (Status);
577
578 return EFI_SUCCESS;
579 }