OLD | NEW |
(Empty) | |
| 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 |
| 3 * found in the LICENSE file. |
| 4 * |
| 5 * Stub implementations of utility functions which call their linux-specific |
| 6 * equivalents. |
| 7 */ |
| 8 |
| 9 #define _STUB_IMPLEMENTATION_ |
| 10 #include "tlcl.h" |
| 11 #include "tlcl_internal.h" |
| 12 #include "utility.h" |
| 13 |
| 14 #include <errno.h> |
| 15 #include <fcntl.h> |
| 16 #include <stdarg.h> |
| 17 #include <stdio.h> |
| 18 #include <string.h> |
| 19 #include <sys/time.h> |
| 20 #include <sys/types.h> |
| 21 #include <sys/stat.h> |
| 22 #include <unistd.h> |
| 23 #include <tss/tcs.h> |
| 24 #include "tpmextras.h" |
| 25 #define TPM_DEVICE_PATH "/dev/tpm0" |
| 26 |
| 27 /* TODO: these functions should pass errors back rather than returning void */ |
| 28 /* TODO: if the only callers to these are just wrappers, should just |
| 29 * remove the wrappers and call us directly. */ |
| 30 |
| 31 |
| 32 /* The file descriptor for the TPM device. |
| 33 */ |
| 34 static int tpm_fd = -1; |
| 35 |
| 36 |
| 37 /* Print |n| bytes from array |a|, with newlines. |
| 38 */ |
| 39 POSSIBLY_UNUSED static void PrintBytes(uint8_t* a, int n) { |
| 40 int i; |
| 41 for (i = 0; i < n; i++) { |
| 42 VBDEBUG(("%02x ", a[i])); |
| 43 if ((i + 1) % 16 == 0) { |
| 44 VBDEBUG(("\n")); |
| 45 } |
| 46 } |
| 47 if (i % 16 != 0) { |
| 48 VBDEBUG(("\n")); |
| 49 } |
| 50 } |
| 51 |
| 52 |
| 53 /* Executes a command on the TPM. |
| 54 */ |
| 55 static void TpmExecute(const uint8_t *in, const uint32_t in_len, |
| 56 uint8_t *out, uint32_t *pout_len) { |
| 57 uint8_t response[TPM_MAX_COMMAND_SIZE]; |
| 58 if (in_len <= 0) { |
| 59 error("invalid command length %d\n", in_len); |
| 60 } else if (tpm_fd < 0) { |
| 61 error("the TPM device was not opened. Forgot to call TlclLibInit?\n"); |
| 62 } else { |
| 63 int n = write(tpm_fd, in, in_len); |
| 64 if (n != in_len) { |
| 65 error("write failure to TPM device: %s\n", strerror(errno)); |
| 66 } |
| 67 n = read(tpm_fd, response, sizeof(response)); |
| 68 if (n == 0) { |
| 69 error("null read from TPM device\n"); |
| 70 } else if (n < 0) { |
| 71 error("read failure from TPM device: %s\n", strerror(errno)); |
| 72 } else { |
| 73 if (n > *pout_len) { |
| 74 error("TPM response too long for output buffer\n"); |
| 75 } else { |
| 76 *pout_len = n; |
| 77 Memcpy(out, response, n); |
| 78 } |
| 79 } |
| 80 } |
| 81 } |
| 82 |
| 83 |
| 84 /* Gets the tag field of a TPM command. |
| 85 */ |
| 86 POSSIBLY_UNUSED static INLINE int TpmTag(uint8_t* buffer) { |
| 87 uint16_t tag; |
| 88 FromTpmUint16(buffer, &tag); |
| 89 return (int) tag; |
| 90 } |
| 91 |
| 92 |
| 93 /* Gets the size field of a TPM command. |
| 94 */ |
| 95 POSSIBLY_UNUSED static INLINE int TpmResponseSize(const uint8_t* buffer) { |
| 96 uint32_t size; |
| 97 FromTpmUint32(buffer + sizeof(uint16_t), &size); |
| 98 return (int) size; |
| 99 } |
| 100 |
| 101 |
| 102 void TlclStubInit(void) { |
| 103 TlclOpenDevice(); |
| 104 } |
| 105 |
| 106 |
| 107 void TlclCloseDevice(void) { |
| 108 close(tpm_fd); |
| 109 tpm_fd = -1; |
| 110 } |
| 111 |
| 112 |
| 113 void TlclOpenDevice(void) { |
| 114 if (tpm_fd >= 0) |
| 115 return; /* Already open */ |
| 116 |
| 117 tpm_fd = open(TPM_DEVICE_PATH, O_RDWR); |
| 118 if (tpm_fd < 0) { |
| 119 error("cannot open TPM device %s: %s\n", TPM_DEVICE_PATH, strerror(errno)); |
| 120 } |
| 121 } |
| 122 |
| 123 |
| 124 void TlclStubSendReceive(uint8_t* request, int request_length, |
| 125 uint8_t* response, int max_length) { |
| 126 /* |
| 127 * In a real firmware implementation, this function should contain |
| 128 * the equivalent API call for the firmware TPM driver which takes a |
| 129 * raw sequence of bytes as input command and a pointer to the |
| 130 * output buffer for putting in the results. |
| 131 * |
| 132 * For EFI firmwares, this can make use of the EFI TPM driver as |
| 133 * follows (based on page 16, of TCG EFI Protocol Specs Version 1.20 |
| 134 * availaible from the TCG website): |
| 135 * |
| 136 * EFI_STATUS status; |
| 137 * status = TcgProtocol->EFI_TCG_PASS_THROUGH_TO_TPM(TpmCommandSize(request), |
| 138 * request, |
| 139 * max_length, |
| 140 * response); |
| 141 * // Error checking depending on the value of the status above |
| 142 */ |
| 143 uint32_t response_length = max_length; |
| 144 int tag, response_tag; |
| 145 |
| 146 struct timeval before, after; |
| 147 gettimeofday(&before, NULL); |
| 148 TpmExecute(request, request_length, response, &response_length); |
| 149 gettimeofday(&after, NULL); |
| 150 |
| 151 #ifdef VBOOT_DEBUG |
| 152 { |
| 153 int x = request_length; |
| 154 int y = response_length; |
| 155 VBDEBUG(("request (%d bytes): ", x)); |
| 156 PrintBytes(request, 10); |
| 157 PrintBytes(request + 10, x - 10); |
| 158 VBDEBUG(("response (%d bytes): ", y)); |
| 159 PrintBytes(response, 10); |
| 160 PrintBytes(response + 10, y - 10); |
| 161 VBDEBUG(("execution time: %dms\n", |
| 162 (int) ((after.tv_sec - before.tv_sec) * 1000 + |
| 163 (after.tv_usec - before.tv_usec) / 1000))); |
| 164 } |
| 165 #endif |
| 166 |
| 167 /* sanity checks */ |
| 168 tag = TpmTag(request); |
| 169 response_tag = TpmTag(response); |
| 170 assert( |
| 171 (tag == TPM_TAG_RQU_COMMAND && |
| 172 response_tag == TPM_TAG_RSP_COMMAND) || |
| 173 (tag == TPM_TAG_RQU_AUTH1_COMMAND && |
| 174 response_tag == TPM_TAG_RSP_AUTH1_COMMAND) || |
| 175 (tag == TPM_TAG_RQU_AUTH2_COMMAND && |
| 176 response_tag == TPM_TAG_RSP_AUTH2_COMMAND)); |
| 177 assert(response_length == TpmResponseSize(response)); |
| 178 } |
OLD | NEW |