| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /* A lightweight TPM command library. | 6 /* A lightweight TPM command library. |
| 7 * | 7 * |
| 8 * The general idea is that TPM commands are array of bytes whose | 8 * The general idea is that TPM commands are array of bytes whose |
| 9 * fields are mostly compile-time constant. The goal is to build much | 9 * fields are mostly compile-time constant. The goal is to build much |
| 10 * of the commands at compile time (or build time) and change some of | 10 * of the commands at compile time (or build time) and change some of |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 VBDEBUG(("TPM: Self test full\n")); | 97 VBDEBUG(("TPM: Self test full\n")); |
| 98 return Send(tpm_selftestfull_cmd.buffer); | 98 return Send(tpm_selftestfull_cmd.buffer); |
| 99 } | 99 } |
| 100 | 100 |
| 101 uint32_t TlclContinueSelfTest(void) { | 101 uint32_t TlclContinueSelfTest(void) { |
| 102 VBDEBUG(("TPM: Continue self test\n")); | 102 VBDEBUG(("TPM: Continue self test\n")); |
| 103 return Send(tpm_continueselftest_cmd.buffer); | 103 return Send(tpm_continueselftest_cmd.buffer); |
| 104 } | 104 } |
| 105 | 105 |
| 106 uint32_t TlclDefineSpace(uint32_t index, uint32_t perm, uint32_t size) { | 106 uint32_t TlclDefineSpace(uint32_t index, uint32_t perm, uint32_t size) { |
| 107 struct s_tpm_nv_definespace_cmd cmd; |
| 107 VBDEBUG(("TPM: TlclDefineSpace(0x%x, 0x%x, %d)\n", index, perm, size)); | 108 VBDEBUG(("TPM: TlclDefineSpace(0x%x, 0x%x, %d)\n", index, perm, size)); |
| 108 ToTpmUint32(tpm_nv_definespace_cmd.index, index); | 109 Memcpy(&cmd, &tpm_nv_definespace_cmd, sizeof(cmd)); |
| 109 ToTpmUint32(tpm_nv_definespace_cmd.perm, perm); | 110 ToTpmUint32(cmd.buffer + tpm_nv_definespace_cmd.index, index); |
| 110 ToTpmUint32(tpm_nv_definespace_cmd.size, size); | 111 ToTpmUint32(cmd.buffer + tpm_nv_definespace_cmd.perm, perm); |
| 111 return Send(tpm_nv_definespace_cmd.buffer); | 112 ToTpmUint32(cmd.buffer + tpm_nv_definespace_cmd.size, size); |
| 113 return Send(cmd.buffer); |
| 112 } | 114 } |
| 113 | 115 |
| 114 uint32_t TlclWrite(uint32_t index, uint8_t* data, uint32_t length) { | 116 uint32_t TlclWrite(uint32_t index, uint8_t* data, uint32_t length) { |
| 117 struct s_tpm_nv_write_cmd cmd; |
| 115 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; | 118 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 116 const int total_length = | 119 const int total_length = |
| 117 kTpmRequestHeaderLength + kWriteInfoLength + length; | 120 kTpmRequestHeaderLength + kWriteInfoLength + length; |
| 118 | 121 |
| 119 VBDEBUG(("TPM: TlclWrite(0x%x, %d)\n", index, length)); | 122 VBDEBUG(("TPM: TlclWrite(0x%x, %d)\n", index, length)); |
| 123 Memcpy(&cmd, &tpm_nv_write_cmd, sizeof(cmd)); |
| 120 assert(total_length <= TPM_LARGE_ENOUGH_COMMAND_SIZE); | 124 assert(total_length <= TPM_LARGE_ENOUGH_COMMAND_SIZE); |
| 121 SetTpmCommandSize(tpm_nv_write_cmd.buffer, total_length); | 125 SetTpmCommandSize(tpm_nv_write_cmd.buffer, total_length); |
| 122 | 126 |
| 123 ToTpmUint32(tpm_nv_write_cmd.index, index); | 127 ToTpmUint32(cmd.buffer + tpm_nv_write_cmd.index, index); |
| 124 ToTpmUint32(tpm_nv_write_cmd.length, length); | 128 ToTpmUint32(cmd.buffer + tpm_nv_write_cmd.length, length); |
| 125 Memcpy(tpm_nv_write_cmd.data, data, length); | 129 Memcpy(cmd.buffer + tpm_nv_write_cmd.data, data, length); |
| 126 | 130 |
| 127 TlclSendReceive(tpm_nv_write_cmd.buffer, response, sizeof(response)); | 131 TlclSendReceive(cmd.buffer, response, sizeof(response)); |
| 128 CheckResult(tpm_nv_write_cmd.buffer, response, 1); | 132 CheckResult(cmd.buffer, response, 1); |
| 129 | 133 |
| 130 return TpmReturnCode(response); | 134 return TpmReturnCode(response); |
| 131 } | 135 } |
| 132 | 136 |
| 133 uint32_t TlclRead(uint32_t index, uint8_t* data, uint32_t length) { | 137 uint32_t TlclRead(uint32_t index, uint8_t* data, uint32_t length) { |
| 138 struct s_tpm_nv_read_cmd cmd; |
| 134 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; | 139 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 135 uint32_t result_length; | 140 uint32_t result_length; |
| 136 uint32_t result; | 141 uint32_t result; |
| 137 | 142 |
| 138 VBDEBUG(("TPM: TlclRead(0x%x, %d)\n", index, length)); | 143 VBDEBUG(("TPM: TlclRead(0x%x, %d)\n", index, length)); |
| 139 ToTpmUint32(tpm_nv_read_cmd.index, index); | 144 Memcpy(&cmd, &tpm_nv_read_cmd, sizeof(cmd)); |
| 140 ToTpmUint32(tpm_nv_read_cmd.length, length); | 145 ToTpmUint32(cmd.buffer + tpm_nv_read_cmd.index, index); |
| 146 ToTpmUint32(cmd.buffer + tpm_nv_read_cmd.length, length); |
| 141 | 147 |
| 142 TlclSendReceive(tpm_nv_read_cmd.buffer, response, sizeof(response)); | 148 TlclSendReceive(cmd.buffer, response, sizeof(response)); |
| 143 result = TpmReturnCode(response); | 149 result = TpmReturnCode(response); |
| 144 if (result == TPM_SUCCESS && length > 0) { | 150 if (result == TPM_SUCCESS && length > 0) { |
| 145 uint8_t* nv_read_cursor = response + kTpmResponseHeaderLength; | 151 uint8_t* nv_read_cursor = response + kTpmResponseHeaderLength; |
| 146 FromTpmUint32(nv_read_cursor, &result_length); | 152 FromTpmUint32(nv_read_cursor, &result_length); |
| 147 nv_read_cursor += sizeof(uint32_t); | 153 nv_read_cursor += sizeof(uint32_t); |
| 148 Memcpy(data, nv_read_cursor, result_length); | 154 Memcpy(data, nv_read_cursor, result_length); |
| 149 } | 155 } |
| 150 | 156 |
| 151 return result; | 157 return result; |
| 152 } | 158 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 VBDEBUG(("TPM: Enabling TPM\n")); | 205 VBDEBUG(("TPM: Enabling TPM\n")); |
| 200 return Send(tpm_physicalenable_cmd.buffer); | 206 return Send(tpm_physicalenable_cmd.buffer); |
| 201 } | 207 } |
| 202 | 208 |
| 203 uint32_t TlclClearEnable(void) { | 209 uint32_t TlclClearEnable(void) { |
| 204 VBDEBUG(("TPM: Disabling TPM\n")); | 210 VBDEBUG(("TPM: Disabling TPM\n")); |
| 205 return Send(tpm_physicaldisable_cmd.buffer); | 211 return Send(tpm_physicaldisable_cmd.buffer); |
| 206 } | 212 } |
| 207 | 213 |
| 208 uint32_t TlclSetDeactivated(uint8_t flag) { | 214 uint32_t TlclSetDeactivated(uint8_t flag) { |
| 215 struct s_tpm_physicalsetdeactivated_cmd cmd; |
| 209 VBDEBUG(("TPM: SetDeactivated(%d)\n", flag)); | 216 VBDEBUG(("TPM: SetDeactivated(%d)\n", flag)); |
| 210 *((uint8_t*)tpm_physicalsetdeactivated_cmd.deactivated) = flag; | 217 Memcpy(&cmd, &tpm_physicaldisable_cmd, sizeof(cmd)); |
| 211 return Send(tpm_physicalsetdeactivated_cmd.buffer); | 218 *(cmd.buffer + cmd.deactivated) = flag; |
| 219 return Send(cmd.buffer); |
| 212 } | 220 } |
| 213 | 221 |
| 214 uint32_t TlclGetFlags(uint8_t* disable, uint8_t* deactivated, uint8_t *nvlocked)
{ | 222 uint32_t TlclGetFlags(uint8_t* disable, uint8_t* deactivated, uint8_t *nvlocked)
{ |
| 215 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; | 223 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 216 TPM_PERMANENT_FLAGS* pflags; | 224 TPM_PERMANENT_FLAGS* pflags; |
| 217 uint32_t result; | 225 uint32_t result; |
| 218 uint32_t size; | 226 uint32_t size; |
| 219 VBDEBUG(("TPM: Get flags\n")); | 227 VBDEBUG(("TPM: Get flags\n")); |
| 220 | 228 |
| 221 TlclSendReceive(tpm_getflags_cmd.buffer, response, sizeof(response)); | 229 TlclSendReceive(tpm_getflags_cmd.buffer, response, sizeof(response)); |
| 222 result = TpmReturnCode(response); | 230 result = TpmReturnCode(response); |
| 223 if (result != TPM_SUCCESS) { | 231 if (result != TPM_SUCCESS) { |
| 224 return result; | 232 return result; |
| 225 } | 233 } |
| 226 FromTpmUint32(response + kTpmResponseHeaderLength, &size); | 234 FromTpmUint32(response + kTpmResponseHeaderLength, &size); |
| 227 assert(size == sizeof(TPM_PERMANENT_FLAGS)); | 235 assert(size == sizeof(TPM_PERMANENT_FLAGS)); |
| 228 pflags = | 236 pflags = |
| 229 (TPM_PERMANENT_FLAGS*) (response + kTpmResponseHeaderLength + sizeof(size)); | 237 (TPM_PERMANENT_FLAGS*) (response + kTpmResponseHeaderLength + sizeof(size)); |
| 230 VBDEBUG(("TPM: Got flags disable=%d, deactivated=%d, nvlocked=%d\n", | 238 VBDEBUG(("TPM: Got flags disable=%d, deactivated=%d, nvlocked=%d\n", |
| 231 pflags->disable, pflags->deactivated, pflags->nvLocked)); | 239 pflags->disable, pflags->deactivated, pflags->nvLocked)); |
| 232 if (disable) | 240 if (disable) |
| 233 *disable = pflags->disable; | 241 *disable = pflags->disable; |
| 234 if (deactivated) | 242 if (deactivated) |
| 235 *deactivated = pflags->deactivated; | 243 *deactivated = pflags->deactivated; |
| 236 if (nvlocked) | 244 if (nvlocked) |
| 237 *nvlocked = pflags->nvLocked; | 245 *nvlocked = pflags->nvLocked; |
| 238 return result; | 246 return result; |
| 239 } | 247 } |
| 240 | 248 |
| 241 uint32_t TlclSetGlobalLock(void) { | 249 uint32_t TlclSetGlobalLock(void) { |
| 242 uint32_t x; | 250 uint32_t x; |
| 243 VBDEBUG(("TPM: Set Set global lock\n")); | 251 VBDEBUG(("TPM: Set Set global lock\n")); |
| 244 return TlclWrite(TPM_NV_INDEX0, (uint8_t*) &x, 0); | 252 return TlclWrite(TPM_NV_INDEX0, (uint8_t*) &x, 0); |
| 245 } | 253 } |
| 246 | 254 |
| 247 uint32_t TlclExtend(int pcr_num, uint8_t* in_digest, uint8_t* out_digest) { | 255 uint32_t TlclExtend(int pcr_num, uint8_t* in_digest, uint8_t* out_digest) { |
| 256 struct s_tpm_extend_cmd cmd; |
| 257 Memcpy(&cmd, &tpm_extend_cmd, sizeof(cmd)); |
| 248 uint8_t response[kTpmResponseHeaderLength + kPcrDigestLength]; | 258 uint8_t response[kTpmResponseHeaderLength + kPcrDigestLength]; |
| 249 ToTpmUint32(tpm_extend_cmd.pcrNum, pcr_num); | 259 ToTpmUint32(cmd.buffer + tpm_extend_cmd.pcrNum, pcr_num); |
| 250 Memcpy(tpm_extend_cmd.inDigest, in_digest, kPcrDigestLength); | 260 Memcpy(cmd.buffer + cmd.inDigest, in_digest, kPcrDigestLength); |
| 251 TlclSendReceive(tpm_extend_cmd.buffer, response, sizeof(response)); | 261 TlclSendReceive(cmd.buffer, response, sizeof(response)); |
| 252 Memcpy(out_digest, response + kTpmResponseHeaderLength, kPcrDigestLength); | 262 Memcpy(out_digest, response + kTpmResponseHeaderLength, kPcrDigestLength); |
| 253 return TpmReturnCode(response); | 263 return TpmReturnCode(response); |
| 254 } | 264 } |
| 255 | 265 |
| 256 uint32_t TlclGetPermissions(uint32_t index, uint32_t* permissions) { | 266 uint32_t TlclGetPermissions(uint32_t index, uint32_t* permissions) { |
| 267 struct s_tpm_getpermissions_cmd cmd; |
| 257 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; | 268 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 258 uint8_t* nvdata; | 269 uint8_t* nvdata; |
| 259 uint32_t result; | 270 uint32_t result; |
| 260 uint32_t size; | 271 uint32_t size; |
| 261 | 272 |
| 262 ToTpmUint32(tpm_getpermissions_cmd.index, index); | 273 Memcpy(&cmd, &tpm_getpermissions_cmd, sizeof(cmd)); |
| 274 ToTpmUint32(cmd.buffer + tpm_getpermissions_cmd.index, index); |
| 263 TlclSendReceive(tpm_getpermissions_cmd.buffer, response, sizeof(response)); | 275 TlclSendReceive(tpm_getpermissions_cmd.buffer, response, sizeof(response)); |
| 264 result = TpmReturnCode(response); | 276 result = TpmReturnCode(response); |
| 265 if (result != TPM_SUCCESS) { | 277 if (result != TPM_SUCCESS) { |
| 266 return result; | 278 return result; |
| 267 } | 279 } |
| 268 nvdata = response + kTpmResponseHeaderLength + sizeof(size); | 280 nvdata = response + kTpmResponseHeaderLength + sizeof(size); |
| 269 FromTpmUint32(nvdata + kNvDataPublicPermissionsOffset, permissions); | 281 FromTpmUint32(nvdata + kNvDataPublicPermissionsOffset, permissions); |
| 270 return result; | 282 return result; |
| 271 } | 283 } |
| OLD | NEW |