summaryrefslogtreecommitdiffstats
path: root/src/usr/secureboot/trusted/tpmLogMgr.H
blob: 6828e42feb2980ef6047540c07bd1833c67e2ce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/secureboot/trusted/tpmLogMgr.H $                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2017                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file tpmLogMgr.H
 *
 * @brief Trustedboot TPM Event Log Manager
 *
 */

/////////////////////////////////////////////////////////////////
// NOTE: This file is exportable as TSS-Lite for skiboot/PHYP  //
/////////////////////////////////////////////////////////////////

#ifndef __TPMLOGMGR_H
#define __TPMLOGMGR_H
// -----------------------------------------------
// Includes
// -----------------------------------------------

#ifdef __HOSTBOOT_MODULE
#include <sys/sync.h>
#endif
#include "trustedboot.H"
#include "trustedTypes.H"

#ifdef __cplusplus
namespace TRUSTEDBOOT
{
#endif

    /// Event log header algorithms
    struct _TCG_EfiSpecIdEventAlgorithmSize
    {
        uint16_t     algorithmId;
        uint16_t     digestSize;
    } PACKED;
    typedef struct _TCG_EfiSpecIdEventAlgorithmSize
                    TCG_EfiSpecIdEventAlgorithmSize;

    /// Event log header event data
    struct _TCG_EfiSpecIdEventStruct
    {
        char         signature[16];
        uint32_t     platformClass;
        uint8_t      specVersionMinor;
        uint8_t      specVersionMajor;
        uint8_t      specErrata;
        uint8_t      uintnSize;
        uint32_t     numberOfAlgorithms;
        TCG_EfiSpecIdEventAlgorithmSize digestSizes[HASH_COUNT];
        uint8_t      vendorInfoSize;
        char         vendorInfo[0];
    } PACKED;
    typedef struct _TCG_EfiSpecIdEventStruct TCG_EfiSpecIdEventStruct;
    uint32_t TCG_EfiSpecIdEventStruct_size(TCG_EfiSpecIdEventStruct* val);

    enum {
        TPMLOG_BUFFER_SIZE   = 3584, ///< Size of event log buffer for HB
        TPMLOG_DEVTREE_SIZE  = 64*1024, ///< Size to allocate for OPAL
    };

    struct _TpmLogMgr
    {
        uint32_t logSize;        ///< Current byte size of log
        uint32_t logMaxSize;     ///< Space allocated for log entries
        uint8_t* newEventPtr;    ///< Pointer to location to add new event
        uint8_t* eventLogInMem;  ///< Event log allocated from memory
#ifdef __HOSTBOOT_MODULE
        uint64_t inMemlogBaseAddr; ///< Base address of log for dev tree
        uint64_t devtreeXscomAddr; ///< Devtree Xscom Address
        uint32_t devtreeI2cMasterOffset; ///< Devtree I2c Master Offset
        uint8_t eventLog[TPMLOG_BUFFER_SIZE]; ///< EventLog Buffer
#endif
        mutex_t  logMutex;       ///< Log mutex
    };
    typedef struct _TpmLogMgr TpmLogMgr;

#ifdef __HOSTBOOT_MODULE
    /**
     * @brief Initialize the log manager
     * @param[in/out] io_logMgr Return a pointer to the log manager
     * @return errlHndl_t NULL if successful, otherwise a pointer to the
     *       error log.
     */
    errlHndl_t TpmLogMgr_initialize(TpmLogMgr * io_logMgr);
#endif

    /**
     * @brief Initialize the log manager to use a pre-existing buffer
     * @param[in] i_val TpmLogMgr structure
     * @param[in] i_eventLogPtr Pointer to event log to use
     * @param[in] i_eventLogSize Allocated log buffer size
     * @return errlHndl_t NULL if successful, otherwise a pointer to the
     *       error log.
     */
    errlHndl_t TpmLogMgr_initializeUsingExistingLog(TpmLogMgr* i_val,
                                                    uint8_t* i_eventLogPtr,
                                                    uint32_t i_eventLogSize);

    /**
     * @brief Add a new event to the log
     * @param[in] i_val TpmLogMgr structure
     * @param[in] i_logEvent Event log entry to add
     * @return errlHndl_t NULL if successful, otherwise a pointer to the
     *       error log.
     */
    errlHndl_t TpmLogMgr_addEvent(TpmLogMgr* i_val,
                                  TCG_PCR_EVENT2* i_logEvent);

    /**
     * @brief Get current log size in bytes
     * @param[in] i_val TpmLogMgr structure
     * @return uint32_t Byte size of log
     */
    uint32_t TpmLogMgr_getLogSize(TpmLogMgr* i_val);

#ifdef __HOSTBOOT_MODULE
    /**
     * @brief Retrieve devtree information
     * @param[in] i_val TpmLogMgr structure
     * @param[in/out] io_logAddr TPM Log address
     * @param[out] o_allocationSize Total memory allocated for log
     * @param[out] o_xscomAddr Chip Xscom Address
     * @param[out] o_i2cMasterOffset I2c Master Offset
     * @return errlHndl_t NULL if successful, otherwise a pointer to the
     *       error log.
     * Function will allocate a new region in memory to store log
     *  for passing to opal
     */
     errlHndl_t TpmLogMgr_getDevtreeInfo(TpmLogMgr* i_val,
                                         uint64_t & io_logAddr,
                                         size_t & o_allocationSize,
                                         uint64_t & o_xscomAddr,
                                         uint32_t & o_i2cMasterOffset);

    /**
     * @brief Store TPM devtree node information
     * @param[in] i_val TpmLogMgr structure
     * @param[in] i_xscomAddr Chip Xscom Address
     * @param[in] i_i2cMasterOffset i2c Master Offset
     */
    void TpmLogMgr_setTpmDevtreeInfo(TpmLogMgr* i_val,
                                     uint64_t i_xscomAddr,
                                     uint32_t i_i2cMasterOffset);
    /**
     * @brief Relocate the TPM log to the provided location
     * @param[in] i_val TpmLogMgr structure
     * @param[in] i_newLog Pointer to locate to place log
     * @param[in] i_maxSize Size of newLog location
     */
    void TpmLogMgr_relocateTpmLog(TpmLogMgr* i_val,
                                  uint8_t* i_newLog,
                                  size_t i_maxSize);
#endif


    /**
     * @brief Calculate the log size in bytes by walking the log
     * @param[in] i_val TpmLogMgr structure
     * @return uint32_t Byte size of log
     */
    uint32_t TpmLogMgr_calcLogSize(TpmLogMgr* i_val);

    /**
     * @brief Get pointer to first event in eventLog past the header event
     * @param[in] i_val TpmLogMgr structure
     * @return uint8_t First event handle
     * @retval NULL On empty log
     * @retval !NULL First event handle
     */
    const uint8_t* TpmLogMgr_getFirstEvent(TpmLogMgr* i_val);

    /**
     * @brief Get pointer to next event in log and unmarshal log contents
     *        into i_eventLog
     *
     * @param[in] i_val         TpmLogMgr structure
     * @param[in] i_handle      Current event to unmarshal
     * @param[in] i_eventLog    EVENT2 structure to populate
     * @param[in] o_err         Indicates if an error occurred during
     *                          LogUnmarshal.
     *
     * @return uint8_t*     Pointer to the next event after i_handle
     * @retval NULL When val contains last entry in log
     * @retval !NULL When addition log entries available
     */
    const uint8_t* TpmLogMgr_getNextEvent(TpmLogMgr* i_val,
                                          const uint8_t* i_handle,
                                          TCG_PCR_EVENT2* i_eventLog,
                                          bool* o_err);

    /**
     * @brief Get a TCG_PCR_EVENT2 populated with required data
     *
     * @param[in] i_pcr PCR to write to
     * @param[in] i_eventType Log event type to use
     * @param[in] i_algId_1 Algorithm to use
     * @param[in] i_digest_1 Digest value to write to PCR
     * @param[in] i_digestSize_1 Byte size of i_digest array
     * @param[in] i_algId_2 Algorithm to use
     * @param[in] i_digest_2 Digest value to write to PCR, NULL if not used
     * @param[in] i_digestSize_2 Byte size of i_digest array
     * @param[in] i_logMsg Null terminated Log message
     *
     * @return TCG_PCR_EVENT2   PCR event log
     */
    TCG_PCR_EVENT2 TpmLogMgr_genLogEventPcrExtend(TPM_Pcr i_pcr,
                                                  EventTypes i_eventType,
                                                  TPM_Alg_Id i_algId_1,
                                                  const uint8_t* i_digest_1,
                                                  size_t i_digestSize_1,
                                                  TPM_Alg_Id i_algId_2,
                                                  const uint8_t* i_digest_2,
                                                  size_t i_digestSize_2,
                                                  const char* i_logMsg);

    /**
     * @brief Dump contents of log to a trace
     * @param[in] i_val TpmLogMgr structure
     */
    void TpmLogMgr_dumpLog(TpmLogMgr* i_val);

    /**
     * @brief Return a pointer to the start of the log
     * @param[in] i_val TpmLogMgr structure
     * @return uint8_t* Pointer to the start of the TPM log
     */
    uint8_t* TpmLogMgr_getLogStartPtr(TpmLogMgr* i_val);


#ifdef __cplusplus
} // end TRUSTEDBOOT namespace
#endif

#endif
OpenPOWER on IntegriCloud