Fix the return status when physical presence variable and MemoryOverwriteRequestContr...
[mirror_edk2.git] / SecurityPkg / Tcg / MemoryOverwriteControl / TcgMor.c
1 /** @file
2 TCG MOR (Memory Overwrite Request) Control Driver.
3
4 This driver initilize MemoryOverwriteRequestControl variable. It
5 will clear MOR_CLEAR_MEMORY_BIT bit if it is set.
6
7 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include "TcgMor.h"
19
20 UINT8 mMorControl;
21
22 /**
23 Ready to Boot Event notification handler.
24
25 Sequence of OS boot events is measured in this event notification handler.
26
27 @param[in] Event Event whose notification function is being invoked
28 @param[in] Context Pointer to the notification function's context
29
30 **/
31 VOID
32 EFIAPI
33 OnReadyToBoot (
34 IN EFI_EVENT Event,
35 IN VOID *Context
36 )
37 {
38 EFI_STATUS Status;
39 UINTN DataSize;
40
41 if (MOR_CLEAR_MEMORY_VALUE (mMorControl) == 0x0) {
42 //
43 // MorControl is expected, directly return to avoid unnecessary variable operation
44 //
45 return ;
46 }
47 //
48 // Clear MOR_CLEAR_MEMORY_BIT
49 //
50 DEBUG ((EFI_D_INFO, "TcgMor: Clear MorClearMemory bit\n"));
51 mMorControl &= 0xFE;
52
53 DataSize = sizeof (mMorControl);
54 Status = gRT->SetVariable (
55 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
56 &gEfiMemoryOverwriteControlDataGuid,
57 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
58 DataSize,
59 &mMorControl
60 );
61 if (EFI_ERROR (Status)) {
62 DEBUG ((EFI_D_ERROR, "TcgMor: Clear MOR_CLEAR_MEMORY_BIT failure, Status = %r\n"));
63 }
64 }
65
66
67 /**
68 Entry Point for TCG MOR Control driver.
69
70 @param[in] ImageHandle Image handle of this driver.
71 @param[in] SystemTable A Pointer to the EFI System Table.
72
73 @retval EFI_SUCEESS
74 @return Others Some error occurs.
75 **/
76 EFI_STATUS
77 EFIAPI
78 MorDriverEntryPoint (
79 IN EFI_HANDLE ImageHandle,
80 IN EFI_SYSTEM_TABLE *SystemTable
81 )
82 {
83 EFI_STATUS Status;
84 UINTN DataSize;
85 EFI_EVENT Event;
86
87 ///
88 /// The firmware is required to create the MemoryOverwriteRequestControl UEFI variable.
89 ///
90
91 DataSize = sizeof (mMorControl);
92 Status = gRT->GetVariable (
93 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
94 &gEfiMemoryOverwriteControlDataGuid,
95 NULL,
96 &DataSize,
97 &mMorControl
98 );
99 if (EFI_ERROR (Status)) {
100 //
101 // Set default value to 0
102 //
103 mMorControl = 0;
104 Status = gRT->SetVariable (
105 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
106 &gEfiMemoryOverwriteControlDataGuid,
107 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
108 DataSize,
109 &mMorControl
110 );
111 DEBUG ((EFI_D_INFO, "TcgMor: Create MOR variable! Status = %r\n", Status));
112 } else {
113 //
114 // Create a Ready To Boot Event and Clear the MorControl bit in the call back function.
115 //
116 DEBUG ((EFI_D_INFO, "TcgMor: Create ReadyToBoot Event for MorControl Bit cleanning!\n"));
117 Status = EfiCreateEventReadyToBootEx (
118 TPL_CALLBACK,
119 OnReadyToBoot,
120 NULL,
121 &Event
122 );
123 }
124
125 return Status;
126 }
127
128