]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
b4732bc6fb50ca82e0f58e4899d2bf34fe362adf
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasurementLib / DxeTpmMeasurementLib.c
1 /** @file
2 This library is used by other modules to measure data to TPM.
3
4 Copyright (c) 2012, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiDxe.h>
16
17 #include <Protocol/TcgService.h>
18
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/TpmMeasurementLib.h>
24
25 #include <Guid/Acpi.h>
26 #include <IndustryStandard/Acpi.h>
27
28
29
30 /**
31 Tpm12 measure and log data, and extend the measurement result into a specific PCR.
32
33 @param[in] PcrIndex PCR Index.
34 @param[in] EventType Event type.
35 @param[in] EventLog Measurement event log.
36 @param[in] LogLen Event log length in bytes.
37 @param[in] HashData The start of the data buffer to be hashed, extended.
38 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
39
40 @retval EFI_SUCCESS Operation completed successfully.
41 @retval EFI_UNSUPPORTED TPM device not available.
42 @retval EFI_OUT_OF_RESOURCES Out of memory.
43 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
44 **/
45 EFI_STATUS
46 Tpm12MeasureAndLogData (
47 IN UINT32 PcrIndex,
48 IN UINT32 EventType,
49 IN VOID *EventLog,
50 IN UINT32 LogLen,
51 IN VOID *HashData,
52 IN UINT64 HashDataLen
53 )
54 {
55 EFI_STATUS Status;
56 EFI_TCG_PROTOCOL *TcgProtocol;
57 TCG_PCR_EVENT *TcgEvent;
58 EFI_PHYSICAL_ADDRESS EventLogLastEntry;
59 UINT32 EventNumber;
60
61 TcgEvent = NULL;
62
63 //
64 // Tpm active/deactive state is checked in HashLogExtendEvent
65 //
66 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);
67 if (EFI_ERROR(Status)){
68 return Status;
69 }
70
71 TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);
72 if(TcgEvent == NULL) {
73 return EFI_OUT_OF_RESOURCES;
74 }
75
76 TcgEvent->PCRIndex = PcrIndex;
77 TcgEvent->EventType = EventType;
78 TcgEvent->EventSize = LogLen;
79 CopyMem (&TcgEvent->Event[0], EventLog, LogLen);
80 EventNumber = 1;
81 Status = TcgProtocol->HashLogExtendEvent (
82 TcgProtocol,
83 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,
84 HashDataLen,
85 TPM_ALG_SHA,
86 TcgEvent,
87 &EventNumber,
88 &EventLogLastEntry
89 );
90
91 FreePool (TcgEvent);
92
93 return Status;
94 }
95
96 /**
97 Tpm measure and log data, and extend the measurement result into a specific PCR.
98
99 @param[in] PcrIndex PCR Index.
100 @param[in] EventType Event type.
101 @param[in] EventLog Measurement event log.
102 @param[in] LogLen Event log length in bytes.
103 @param[in] HashData The start of the data buffer to be hashed, extended.
104 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
105
106 @retval EFI_SUCCESS Operation completed successfully.
107 @retval EFI_UNSUPPORTED TPM device not available.
108 @retval EFI_OUT_OF_RESOURCES Out of memory.
109 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
110 **/
111 EFI_STATUS
112 EFIAPI
113 TpmMeasureAndLogData (
114 IN UINT32 PcrIndex,
115 IN UINT32 EventType,
116 IN VOID *EventLog,
117 IN UINT32 LogLen,
118 IN VOID *HashData,
119 IN UINT64 HashDataLen
120 )
121 {
122 EFI_STATUS Status;
123
124 //
125 // Try to measure using Tpm1.2 protocol
126 //
127 Status = Tpm12MeasureAndLogData(
128 PcrIndex,
129 EventType,
130 EventLog,
131 LogLen,
132 HashData,
133 HashDataLen
134 );
135
136 return Status;
137 }