2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
6 * Some TPM constants and type definitions for standalone compilation for use
10 #include "rollback_index.h"
11 #include "tpm2_marshaling.h"
15 static struct tpm2_response *tpm_process_command(TPM_CC command,
18 /* Command/response buffer. */
19 static uint8_t cr_buffer[TPM_BUFFER_SIZE];
20 uint32_t out_size, in_size;
21 struct tpm2_response *response;
23 out_size = tpm_marshal_command(command, command_body,
24 cr_buffer, sizeof(cr_buffer));
26 VBDEBUG(("command %#x, cr size %d\n",
31 in_size = sizeof(cr_buffer);
32 if (VbExTpmSendReceive(cr_buffer, out_size,
33 cr_buffer, &in_size) != TPM_SUCCESS) {
34 VBDEBUG(("tpm transaction failed for %#x\n", command));
38 response = tpm_unmarshal_response(command, cr_buffer, in_size);
40 VBDEBUG(("%s: command %#x, return code %#x\n", __func__, command,
41 response ? response->hdr.tpm_code : -1));
46 static uint32_t tlcl_read_ph_disabled(void)
49 TPM_STCLEAR_FLAGS flags;
51 rv = TlclGetSTClearFlags(&flags);
52 if (rv != TPM_SUCCESS)
55 tpm_set_ph_disabled(!flags.phEnable);
60 uint32_t TlclLibInit(void)
65 if (rv != TPM_SUCCESS)
68 rv = tlcl_read_ph_disabled();
69 if (rv != TPM_SUCCESS) {
77 uint32_t TlclLibClose(void)
79 return VbExTpmClose();
82 uint32_t TlclSendReceive(const uint8_t *request, uint8_t *response,
85 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
89 int TlclPacketSize(const uint8_t *packet)
91 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
95 uint32_t TlclStartup(void)
97 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
101 uint32_t TlclSaveState(void)
103 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
107 uint32_t TlclResume(void)
109 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
113 uint32_t TlclSelfTestFull(void)
115 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
119 uint32_t TlclContinueSelfTest(void)
121 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
125 uint32_t TlclDefineSpace(uint32_t index, uint32_t perm, uint32_t size)
127 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
132 * Issue a ForceClear. The TPM error code is returned.
134 uint32_t TlclForceClear(void)
136 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
140 uint32_t TlclSetDeactivated(uint8_t flag)
142 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
146 uint32_t TlclSetEnable(void)
148 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
152 uint32_t TlclGetFlags(uint8_t* disable,
153 uint8_t* deactivated,
156 /* For TPM2 the flags are always the same */
166 int TlclIsOwned(void)
168 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
172 uint32_t TlclExtend(int pcr_num, const uint8_t *in_digest, uint8_t *out_digest)
174 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
179 * Get the permission bits for the NVRAM space with |index|.
181 uint32_t TlclGetPermissions(uint32_t index, uint32_t *permissions)
184 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
188 static uint32_t tlcl_get_capability(TPM_CAP cap, TPM_PT property,
189 struct get_capability_response **presp)
191 struct tpm2_response *response;
192 struct tpm2_get_capability_cmd getcap;
194 getcap.capability = cap;
195 getcap.property = property;
196 getcap.property_count = 1;
198 response = tpm_process_command(TPM2_GetCapability, &getcap);
199 if (!response || response->hdr.tpm_code)
200 return TPM_E_IOERROR;
201 *presp = &response->cap;
206 static uint32_t tlcl_get_tpm_property(TPM_PT property, uint32_t *pvalue)
209 struct get_capability_response *resp;
210 TPML_TAGGED_TPM_PROPERTY *tpm_prop;
212 rv = tlcl_get_capability(TPM_CAP_TPM_PROPERTIES, property, &resp);
213 if (rv != TPM_SUCCESS)
216 if (resp->capability_data.capability != TPM_CAP_TPM_PROPERTIES)
217 return TPM_E_IOERROR;
219 tpm_prop = &resp->capability_data.data.tpm_properties;
221 if ((tpm_prop->count != 1) ||
222 (tpm_prop->tpm_property[0].property != property))
223 return TPM_E_IOERROR;
225 *pvalue = tpm_prop->tpm_property[0].value;
229 uint32_t TlclGetPermanentFlags(TPM_PERMANENT_FLAGS *pflags)
231 return tlcl_get_tpm_property(TPM_PT_PERMANENT,
235 uint32_t TlclGetSTClearFlags(TPM_STCLEAR_FLAGS *pflags)
237 return tlcl_get_tpm_property(TPM_PT_STARTUP_CLEAR,
241 uint32_t TlclGetOwnership(uint8_t *owned)
244 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
248 static uint32_t tlcl_lock_nv_write(uint32_t index)
250 struct tpm2_response *response;
251 struct tpm2_nv_write_lock_cmd nv_wl;
253 nv_wl.nvIndex = HR_NV_INDEX + index;
254 response = tpm_process_command(TPM2_NV_WriteLock, &nv_wl);
256 if (!response || response->hdr.tpm_code)
257 return TPM_E_INTERNAL_INCONSISTENCY;
262 static uint32_t tlcl_disable_platform_hierarchy(void)
264 struct tpm2_response *response;
265 struct tpm2_hierarchy_control_cmd hc;
267 hc.enable = TPM_RH_PLATFORM;
270 response = tpm_process_command(TPM2_Hierarchy_Control, &hc);
272 if (!response || response->hdr.tpm_code)
273 return TPM_E_INTERNAL_INCONSISTENCY;
275 tpm_set_ph_disabled(1);
280 * Turn off physical presence and locks it off until next reboot. The TPM
281 * error code is returned.
283 * The name of the function was kept to maintain the existing TPM API, but
284 * TPM2.0 does not have to use the Physical Presence concept. Instead it just
285 * removes platform authorization - this makes sure that firmware and kernel
286 * rollback counter spaces can not be modified.
288 * It also explicitly locks the kernel rollback counter space (the FW rollback
289 * counter space was locked before RW firmware started.)
291 uint32_t TlclLockPhysicalPresence(void)
295 rv = tlcl_lock_nv_write(KERNEL_NV_INDEX);
296 if (rv == TPM_SUCCESS)
297 rv = tlcl_disable_platform_hierarchy();
302 uint32_t TlclRead(uint32_t index, void* data, uint32_t length)
304 struct tpm2_nv_read_cmd nv_readc;
305 struct tpm2_response *response;
307 Memset(&nv_readc, 0, sizeof(nv_readc));
309 nv_readc.nvIndex = HR_NV_INDEX + index;
310 nv_readc.size = length;
312 response = tpm_process_command(TPM2_NV_Read, &nv_readc);
314 /* Need to map tpm error codes into internal values. */
316 return TPM_E_READ_FAILURE;
318 switch (response->hdr.tpm_code) {
323 return TPM_E_BADINDEX;
326 return TPM_E_READ_FAILURE;
329 if (length > response->nvr.buffer.t.size)
330 return TPM_E_RESPONSE_TOO_LARGE;
332 if (length < response->nvr.buffer.t.size)
333 return TPM_E_READ_EMPTY;
335 Memcpy(data, response->nvr.buffer.t.buffer, length);
340 uint32_t TlclWrite(uint32_t index, const void *data, uint32_t length)
342 struct tpm2_nv_write_cmd nv_writec;
343 struct tpm2_response *response;
345 Memset(&nv_writec, 0, sizeof(nv_writec));
347 nv_writec.nvIndex = HR_NV_INDEX + index;
348 nv_writec.data.t.size = length;
349 nv_writec.data.t.buffer = data;
351 response = tpm_process_command(TPM2_NV_Write, &nv_writec);
353 /* Need to map tpm error codes into internal values. */
355 return TPM_E_WRITE_FAILURE;
360 uint32_t TlclPCRRead(uint32_t index, void *data, uint32_t length)
362 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
366 uint32_t TlclWriteLock(uint32_t index)
368 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
372 uint32_t TlclReadLock(uint32_t index)
374 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
378 uint32_t TlclGetRandom(uint8_t *data, uint32_t length, uint32_t *size)
381 VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
382 return TPM_E_IOERROR;