]>
Commit | Line | Data |
---|---|---|
a332cfd3 | 1 | /** @file\r |
2 | This library is used by other modules to measure data to TPM.\r | |
3 | \r | |
b3548d32 | 4 | Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved. <BR>\r |
289b714b | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a332cfd3 | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include <PiDxe.h>\r | |
10 | \r | |
11 | #include <Protocol/TcgService.h>\r | |
1abfa4ce | 12 | #include <Protocol/Tcg2Protocol.h>\r |
a332cfd3 | 13 | \r |
14 | #include <Library/BaseMemoryLib.h>\r | |
15 | #include <Library/MemoryAllocationLib.h>\r | |
16 | #include <Library/UefiBootServicesTableLib.h>\r | |
17 | #include <Library/DebugLib.h>\r | |
18 | #include <Library/TpmMeasurementLib.h>\r | |
19 | \r | |
20 | #include <Guid/Acpi.h>\r | |
21 | #include <IndustryStandard/Acpi.h>\r | |
22 | \r | |
23 | \r | |
24 | \r | |
25 | /**\r | |
26 | Tpm12 measure and log data, and extend the measurement result into a specific PCR.\r | |
27 | \r | |
28 | @param[in] PcrIndex PCR Index.\r | |
29 | @param[in] EventType Event type.\r | |
30 | @param[in] EventLog Measurement event log.\r | |
31 | @param[in] LogLen Event log length in bytes.\r | |
32 | @param[in] HashData The start of the data buffer to be hashed, extended.\r | |
33 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
34 | \r | |
35 | @retval EFI_SUCCESS Operation completed successfully.\r | |
36 | @retval EFI_UNSUPPORTED TPM device not available.\r | |
37 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
38 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
39 | **/\r | |
a332cfd3 | 40 | EFI_STATUS\r |
41 | Tpm12MeasureAndLogData (\r | |
42 | IN UINT32 PcrIndex,\r | |
43 | IN UINT32 EventType,\r | |
44 | IN VOID *EventLog,\r | |
45 | IN UINT32 LogLen,\r | |
46 | IN VOID *HashData,\r | |
47 | IN UINT64 HashDataLen\r | |
48 | )\r | |
49 | {\r | |
50 | EFI_STATUS Status;\r | |
51 | EFI_TCG_PROTOCOL *TcgProtocol;\r | |
52 | TCG_PCR_EVENT *TcgEvent;\r | |
53 | EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r | |
54 | UINT32 EventNumber;\r | |
55 | \r | |
56 | TcgEvent = NULL;\r | |
57 | \r | |
58 | //\r | |
d6b926e7 | 59 | // Tpm activation state is checked in HashLogExtendEvent\r |
a332cfd3 | 60 | //\r |
61 | Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r | |
62 | if (EFI_ERROR(Status)){\r | |
63 | return Status;\r | |
64 | }\r | |
65 | \r | |
66 | TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);\r | |
67 | if(TcgEvent == NULL) {\r | |
68 | return EFI_OUT_OF_RESOURCES;\r | |
69 | }\r | |
70 | \r | |
71 | TcgEvent->PCRIndex = PcrIndex;\r | |
72 | TcgEvent->EventType = EventType;\r | |
73 | TcgEvent->EventSize = LogLen;\r | |
74 | CopyMem (&TcgEvent->Event[0], EventLog, LogLen);\r | |
75 | EventNumber = 1;\r | |
76 | Status = TcgProtocol->HashLogExtendEvent (\r | |
77 | TcgProtocol,\r | |
78 | (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r | |
79 | HashDataLen,\r | |
80 | TPM_ALG_SHA,\r | |
81 | TcgEvent,\r | |
82 | &EventNumber,\r | |
83 | &EventLogLastEntry\r | |
84 | );\r | |
85 | \r | |
86 | FreePool (TcgEvent);\r | |
87 | \r | |
88 | return Status;\r | |
89 | }\r | |
90 | \r | |
c1d93242 JY |
91 | /**\r |
92 | Tpm20 measure and log data, and extend the measurement result into a specific PCR.\r | |
93 | \r | |
94 | @param[in] PcrIndex PCR Index.\r | |
95 | @param[in] EventType Event type.\r | |
96 | @param[in] EventLog Measurement event log.\r | |
97 | @param[in] LogLen Event log length in bytes.\r | |
98 | @param[in] HashData The start of the data buffer to be hashed, extended.\r | |
99 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
100 | \r | |
101 | @retval EFI_SUCCESS Operation completed successfully.\r | |
102 | @retval EFI_UNSUPPORTED TPM device not available.\r | |
103 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
104 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
105 | **/\r | |
106 | EFI_STATUS\r | |
107 | Tpm20MeasureAndLogData (\r | |
108 | IN UINT32 PcrIndex,\r | |
109 | IN UINT32 EventType,\r | |
110 | IN VOID *EventLog,\r | |
111 | IN UINT32 LogLen,\r | |
112 | IN VOID *HashData,\r | |
113 | IN UINT64 HashDataLen\r | |
114 | )\r | |
115 | {\r | |
116 | EFI_STATUS Status;\r | |
1abfa4ce JY |
117 | EFI_TCG2_PROTOCOL *Tcg2Protocol;\r |
118 | EFI_TCG2_EVENT *Tcg2Event;\r | |
c1d93242 JY |
119 | \r |
120 | //\r | |
1abfa4ce | 121 | // TPMPresentFlag is checked in HashLogExtendEvent\r |
c1d93242 | 122 | //\r |
1abfa4ce | 123 | Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &Tcg2Protocol);\r |
c1d93242 JY |
124 | if (EFI_ERROR (Status)) {\r |
125 | return Status;\r | |
126 | }\r | |
127 | \r | |
1abfa4ce JY |
128 | Tcg2Event = (EFI_TCG2_EVENT *) AllocateZeroPool (LogLen + sizeof (EFI_TCG2_EVENT));\r |
129 | if(Tcg2Event == NULL) {\r | |
c1d93242 JY |
130 | return EFI_OUT_OF_RESOURCES;\r |
131 | }\r | |
132 | \r | |
1abfa4ce JY |
133 | Tcg2Event->Size = (UINT32)LogLen + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event);\r |
134 | Tcg2Event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);\r | |
135 | Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r | |
136 | Tcg2Event->Header.PCRIndex = PcrIndex;\r | |
137 | Tcg2Event->Header.EventType = EventType;\r | |
138 | CopyMem (&Tcg2Event->Event[0], EventLog, LogLen);\r | |
c1d93242 | 139 | \r |
1abfa4ce JY |
140 | Status = Tcg2Protocol->HashLogExtendEvent (\r |
141 | Tcg2Protocol,\r | |
c1d93242 JY |
142 | 0,\r |
143 | (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r | |
144 | HashDataLen,\r | |
1abfa4ce | 145 | Tcg2Event\r |
c1d93242 | 146 | );\r |
1abfa4ce | 147 | FreePool (Tcg2Event);\r |
c1d93242 JY |
148 | \r |
149 | return Status;\r | |
150 | }\r | |
151 | \r | |
a332cfd3 | 152 | /**\r |
153 | Tpm measure and log data, and extend the measurement result into a specific PCR.\r | |
154 | \r | |
155 | @param[in] PcrIndex PCR Index.\r | |
156 | @param[in] EventType Event type.\r | |
157 | @param[in] EventLog Measurement event log.\r | |
158 | @param[in] LogLen Event log length in bytes.\r | |
159 | @param[in] HashData The start of the data buffer to be hashed, extended.\r | |
160 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
161 | \r | |
162 | @retval EFI_SUCCESS Operation completed successfully.\r | |
163 | @retval EFI_UNSUPPORTED TPM device not available.\r | |
164 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
165 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
166 | **/\r | |
a332cfd3 | 167 | EFI_STATUS\r |
b3548d32 | 168 | EFIAPI\r |
a332cfd3 | 169 | TpmMeasureAndLogData (\r |
170 | IN UINT32 PcrIndex,\r | |
171 | IN UINT32 EventType,\r | |
172 | IN VOID *EventLog,\r | |
173 | IN UINT32 LogLen,\r | |
174 | IN VOID *HashData,\r | |
175 | IN UINT64 HashDataLen\r | |
176 | )\r | |
177 | {\r | |
178 | EFI_STATUS Status;\r | |
179 | \r | |
180 | //\r | |
d9e206d4 | 181 | // Try to measure using Tpm20 protocol\r |
a332cfd3 | 182 | //\r |
d9e206d4 ZC |
183 | Status = Tpm20MeasureAndLogData(\r |
184 | PcrIndex,\r | |
185 | EventType,\r | |
186 | EventLog,\r | |
187 | LogLen,\r | |
188 | HashData,\r | |
189 | HashDataLen\r | |
190 | );\r | |
191 | \r | |
c1d93242 JY |
192 | if (EFI_ERROR (Status)) {\r |
193 | //\r | |
d9e206d4 | 194 | // Try to measure using Tpm1.2 protocol\r |
c1d93242 | 195 | //\r |
d9e206d4 | 196 | Status = Tpm12MeasureAndLogData(\r |
c1d93242 JY |
197 | PcrIndex,\r |
198 | EventType,\r | |
199 | EventLog,\r | |
200 | LogLen,\r | |
201 | HashData,\r | |
202 | HashDataLen\r | |
203 | );\r | |
204 | }\r | |
a332cfd3 | 205 | \r |
206 | return Status;\r | |
207 | }\r |