1 /* IBM_PROLOG_BEGIN_TAG                                                   */
2 /* This is an automatically generated prolog.                             */
3 /*                                                                        */
4 /* $Source: src/usr/secureboot/trusted/tpmLogMgr.H $                      */
5 /*                                                                        */
6 /* OpenPOWER HostBoot Project                                             */
7 /*                                                                        */
8 /* Contributors Listed Below - COPYRIGHT 2015,2016                        */
9 /* [+] International Business Machines Corp.                              */
10 /*                                                                        */
11 /*                                                                        */
12 /* Licensed under the Apache License, Version 2.0 (the "License");        */
13 /* you may not use this file except in compliance with the License.       */
14 /* You may obtain a copy of the License at                                */
15 /*                                                                        */
16 /*     http://www.apache.org/licenses/LICENSE-2.0                         */
17 /*                                                                        */
18 /* Unless required by applicable law or agreed to in writing, software    */
19 /* distributed under the License is distributed on an "AS IS" BASIS,      */
20 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
21 /* implied. See the License for the specific language governing           */
22 /* permissions and limitations under the License.                         */
23 /*                                                                        */
24 /* IBM_PROLOG_END_TAG                                                     */
25 /**
26  * @file tpmLogMgr.H
27  *
28  * @brief Trustedboot TPM Event Log Manager
29  *
30  */
31 
32 /////////////////////////////////////////////////////////////////
33 // NOTE: This file is exportable as TSS-Lite for skiboot/PHYP  //
34 /////////////////////////////////////////////////////////////////
35 
36 #ifndef __TPMLOGMGR_H
37 #define __TPMLOGMGR_H
38 // -----------------------------------------------
39 // Includes
40 // -----------------------------------------------
41 
42 #ifdef __HOSTBOOT_MODULE
43 #include <sys/sync.h>
44 #endif
45 #include "trustedboot.H"
46 #include "trustedTypes.H"
47 
48 #ifdef __cplusplus
49 namespace TRUSTEDBOOT
50 {
51 #endif
52 
53     /// Event log header algorithms
54     struct _TCG_EfiSpecIdEventAlgorithmSize
55     {
56         uint16_t     algorithmId;
57         uint16_t     digestSize;
58     } PACKED;
59     typedef struct _TCG_EfiSpecIdEventAlgorithmSize
60                     TCG_EfiSpecIdEventAlgorithmSize;
61 
62     /// Event log header event data
63     struct _TCG_EfiSpecIdEventStruct
64     {
65         char         signature[16];
66         uint32_t     platformClass;
67         uint8_t      specVersionMinor;
68         uint8_t      specVersionMajor;
69         uint8_t      specErrata;
70         uint8_t      uintnSize;
71         uint32_t     numberOfAlgorithms;
72         TCG_EfiSpecIdEventAlgorithmSize digestSizes[HASH_COUNT];
73         uint8_t      vendorInfoSize;
74         char         vendorInfo[0];
75     } PACKED;
76     typedef struct _TCG_EfiSpecIdEventStruct TCG_EfiSpecIdEventStruct;
77     uint32_t TCG_EfiSpecIdEventStruct_size(TCG_EfiSpecIdEventStruct* val);
78 
79     enum {
80         TPMLOG_BUFFER_SIZE   = 3584, ///< Size of event log buffer for HB
81         TPMLOG_DEVTREE_SIZE  = 64*1024, ///< Size to allocate for OPAL
82     };
83 
84     struct _TpmLogMgr
85     {
86         uint32_t logSize;        ///< Current byte size of log
87         uint32_t logMaxSize;     ///< Space allocated for log entries
88         uint8_t* newEventPtr;    ///< Pointer to location to add new event
89         uint8_t* eventLogInMem;  ///< Event log allocated from memory
90 #ifdef __HOSTBOOT_MODULE
91         uint64_t inMemlogBaseAddr; ///< Base address of log for dev tree
92         uint64_t devtreeXscomAddr; ///< Devtree Xscom Address
93         uint32_t devtreeI2cMasterOffset; ///< Devtree I2c Master Offset
94         uint8_t eventLog[TPMLOG_BUFFER_SIZE]; ///< EventLog Buffer
95 #endif
96         mutex_t  logMutex;       ///< Log mutex
97     };
98     typedef struct _TpmLogMgr TpmLogMgr;
99 
100 #ifdef __HOSTBOOT_MODULE
101     /**
102      * @brief Initialize the log manager
103      * @param[in/out] io_logMgr Return a pointer to the log manager
104      * @return errlHndl_t NULL if successful, otherwise a pointer to the
105      *       error log.
106      */
107     errlHndl_t TpmLogMgr_initialize(TpmLogMgr * io_logMgr);
108 #endif
109 
110     /**
111      * @brief Initialize the log manager to use a pre-existing buffer
112      * @param[in] val TpmLogMgr structure
113      * @param[in] eventLogPtr Pointer to event log to use
114      * @param[in] eventLogSize Allocated log buffer size
115      * @return errlHndl_t NULL if successful, otherwise a pointer to the
116      *       error log.
117      */
118     errlHndl_t TpmLogMgr_initializeUsingExistingLog(TpmLogMgr* val,
119                                                     uint8_t* eventLogPtr,
120                                                     uint32_t eventLogSize);
121 
122     /**
123      * @brief Add a new event to the log
124      * @param[in] val TpmLogMgr structure
125      * @param[in] logEvent Event log entry to add
126      * @return errlHndl_t NULL if successful, otherwise a pointer to the
127      *       error log.
128      */
129     errlHndl_t TpmLogMgr_addEvent(TpmLogMgr* val, TCG_PCR_EVENT2* logEvent);
130 
131     /**
132      * @brief Get current log size in bytes
133      * @param[in] val TpmLogMgr structure
134      * @return uint32_t Byte size of log
135      */
136     uint32_t TpmLogMgr_getLogSize(TpmLogMgr* val);
137 
138 #ifdef __HOSTBOOT_MODULE
139     /**
140      * @brief Retrieve devtree information
141      * @param[in] val TpmLogMgr structure
142      * @param[in/out] io_logAddr TPM Log address
143      * @param[out] o_allocationSize Total memory allocated for log
144      * @param[out] o_xscomAddr Chip Xscom Address
145      * @param[out] o_i2cMasterOffset I2c Master Offset
146      * @return errlHndl_t NULL if successful, otherwise a pointer to the
147      *       error log.
148      * Function will allocate a new region in memory to store log
149      *  for passing to opal
150      */
151      errlHndl_t TpmLogMgr_getDevtreeInfo(TpmLogMgr* val,
152                                          uint64_t & io_logAddr,
153                                          size_t & o_allocationSize,
154                                          uint64_t & o_xscomAddr,
155                                          uint32_t & o_i2cMasterOffset);
156 
157     /**
158      * @brief Store TPM devtree node information
159      * @param[in] val TpmLogMgr structure
160      * @param[in] i_xscomAddr Chip Xscom Address
161      * @param[in] i_i2cMasterOffset i2c Master Offset
162      */
163     void TpmLogMgr_setTpmDevtreeInfo(TpmLogMgr* val,
164                                      uint64_t i_xscomAddr,
165                                      uint32_t i_i2cMasterOffset);
166 #endif
167 
168 
169     /**
170      * @brief Calculate the log size in bytes by walking the log
171      * @param[in] val TpmLogMgr structure
172      * @return uint32_t Byte size of log
173      */
174     uint32_t TpmLogMgr_calcLogSize(TpmLogMgr* val);
175 
176     /**
177      * @brief Get pointer to first event in eventLog past the header event
178      * @param[in] val TpmLogMgr structure
179      * @return uint8_t First event handle
180      * @retval NULL On empty log
181      * @retval !NULL First event handle
182      */
183     const uint8_t* TpmLogMgr_getFirstEvent(TpmLogMgr* val);
184 
185     /**
186      * @brief Get pointer to next event in log and unmarshal log contents
187      *        into i_eventLog
188      *
189      * @param[in] i_handle      Current event to unmarshal
190      * @param[in] i_eventLog    EVENT2 structure to populate
191      * @param[in] o_err         Indicates if an error occurred during
192      *                          LogUnmarshal.
193      *
194      * @return uint8_t*     Pointer to the next event after i_handle
195      * @retval NULL When val contains last entry in log
196      * @retval !NULL When addition log entries available
197      */
198     const uint8_t* TpmLogMgr_getNextEvent(TpmLogMgr* val,
199                                           const uint8_t* i_handle,
200                                           TCG_PCR_EVENT2* i_eventLog,
201                                           bool* o_err);
202 
203     /**
204      * @brief Get a TCG_PCR_EVENT2 populated with required data
205      *
206      * @param[in] i_pcr PCR to write to
207      * @param[in] i_algId_1 Algorithm to use
208      * @param[in] i_digest_1 Digest value to write to PCR
209      * @param[in] i_digestSize_1 Byte size of i_digest array
210      * @param[in] i_algId_2 Algorithm to use
211      * @param[in] i_digest_2 Digest value to write to PCR, NULL if not used
212      * @param[in] i_digestSize_2 Byte size of i_digest array
213      * @param[in] i_logType Event type
214      * @param[in] i_logMsg Null terminated Log message
215      *
216      * @return TCG_PCR_EVENT2   PCR event log
217      */
218     TCG_PCR_EVENT2 TpmLogMgr_genLogEventPcrExtend(TPM_Pcr i_pcr,
219                                                   TPM_Alg_Id i_algId_1,
220                                                   const uint8_t* i_digest_1,
221                                                   size_t i_digestSize_1,
222                                                   TPM_Alg_Id i_algId_2,
223                                                   const uint8_t* i_digest_2,
224                                                   size_t i_digestSize_2,
225 						  uint32_t i_logType,
226                                                   const char* i_logMsg);
227 #ifdef __HOSTBOOT_MODULE
228     /**
229      * @brief Dump contents of log to a trace
230      * @param[in] val TpmLogMgr structure
231      */
232     void TpmLogMgr_dumpLog(TpmLogMgr* val);
233 #endif
234 
235     /**
236      * @brief Return a pointer to the start of the log
237      * @param[in] val TpmLogMgr structure
238      * @return uint8_t* Pointer to the start of the TPM log
239      */
240     uint8_t* TpmLogMgr_getLogStartPtr(TpmLogMgr* val);
241 
242 
243 #ifdef __cplusplus
244 } // end TRUSTEDBOOT namespace
245 #endif
246 
247 #endif
248