def965f13853cf1acdc84998307f5ab1294c5899
[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 - 2012, 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 UINTN DataSize;
39
40 if (MOR_CLEAR_MEMORY_VALUE (mMorControl) == 0x0) {
41 //
42 // MorControl is expected, directly return to avoid unnecessary variable operation
43 //
44 return ;
45 }
46 //
47 // Clear MOR_CLEAR_MEMORY_BIT
48 //
49 DEBUG ((EFI_D_INFO, "TcgMor: Clear MorClearMemory bit\n"));
50 mMorControl &= 0xFE;
51
52 DataSize = sizeof (mMorControl);
53 gRT->SetVariable (
54 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
55 &gEfiMemoryOverwriteControlDataGuid,
56 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
57 DataSize,
58 &mMorControl
59 );
60
61 }
62
63
64 /**
65 Entry Point for TCG MOR Control driver.
66
67 @param[in] ImageHandle Image handle of this driver.
68 @param[in] SystemTable A Pointer to the EFI System Table.
69
70 @retval EFI_SUCEESS
71 @return Others Some error occurs.
72 **/
73 EFI_STATUS
74 EFIAPI
75 MorDriverEntryPoint (
76 IN EFI_HANDLE ImageHandle,
77 IN EFI_SYSTEM_TABLE *SystemTable
78 )
79 {
80 EFI_STATUS Status;
81 UINTN DataSize;
82 EFI_EVENT Event;
83
84 ///
85 /// The firmware is required to create the MemoryOverwriteRequestControl UEFI variable.
86 ///
87
88 DataSize = sizeof (mMorControl);
89 Status = gRT->GetVariable (
90 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
91 &gEfiMemoryOverwriteControlDataGuid,
92 NULL,
93 &DataSize,
94 &mMorControl
95 );
96 if (EFI_ERROR (Status)) {
97 //
98 // Set default value to 0
99 //
100 mMorControl = 0;
101 DEBUG ((EFI_D_INFO, "TcgMor: Create gEfiMemoryOverwriteControlDataGuid!\n"));
102 Status = gRT->SetVariable (
103 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
104 &gEfiMemoryOverwriteControlDataGuid,
105 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
106 DataSize,
107 &mMorControl
108 );
109 ASSERT_EFI_ERROR (Status);
110
111 } else {
112 //
113 // Create a Ready To Boot Event and Clear the MorControl bit in the call back function.
114 //
115 DEBUG ((EFI_D_INFO, "TcgMor: Create ReadyToBoot Event for MorControl Bit cleanning!\n"));
116 Status = EfiCreateEventReadyToBootEx (
117 TPL_CALLBACK,
118 OnReadyToBoot,
119 NULL,
120 &Event
121 );
122 }
123
124 return Status;
125 }
126
127