summaryrefslogtreecommitdiff
path: root/firmware/stub/tpm_lite_stub.c
blob: 1b49836820fb6ddf564632ebf93a8a8e323bb3a5 (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
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Stub implementations of utility functions which call their linux-specific
 * equivalents.
 */

#define _STUB_IMPLEMENTATION_
#include "tlcl.h"
#include "tlcl_internal.h"
#include "utility.h"
#include "vboot_api.h"

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define TPM_DEVICE_PATH "/dev/tpm0"

/* TODO: these functions should pass errors back rather than returning void */
/* TODO: if the only callers to these are just wrappers, should just
 * remove the wrappers and call us directly. */


/* The file descriptor for the TPM device.
 */
static int tpm_fd = -1;


/* Print |n| bytes from array |a|, with newlines.
 */
POSSIBLY_UNUSED static void PrintBytes(const uint8_t* a, int n) {
  int i;
  for (i = 0; i < n; i++) {
    VBDEBUG(("%02x ", a[i]));
    if ((i + 1) % 16 == 0) {
      VBDEBUG(("\n"));
    }
  }
  if (i % 16 != 0) {
    VBDEBUG(("\n"));
  }
}


/* Executes a command on the TPM.
 */
static void TpmExecute(const uint8_t *in, const uint32_t in_len,
                uint8_t *out, uint32_t *pout_len) {
  uint8_t response[TPM_MAX_COMMAND_SIZE];
  if (in_len <= 0) {
    VbExError("invalid command length %d for command 0x%x\n", in_len, in[9]);
  } else if (tpm_fd < 0) {
    VbExError("the TPM device was not opened.  Forgot to call TlclLibInit?\n");
  } else {
    int n = write(tpm_fd, in, in_len);
    if (n != in_len) {
      VbExError("write failure to TPM device: %s\n", strerror(errno));
    }
    n = read(tpm_fd, response, sizeof(response));
    if (n == 0) {
      VbExError("null read from TPM device\n");
    } else if (n < 0) {
      VbExError("read failure from TPM device: %s\n", strerror(errno));
    } else {
      if (n > *pout_len) {
        VbExError("TPM response too long for output buffer\n");
      } else {
        *pout_len = n;
        Memcpy(out, response, n);
      }
    }
  }
}


/* Gets the tag field of a TPM command.
 */
POSSIBLY_UNUSED static INLINE int TpmTag(const uint8_t* buffer) {
  uint16_t tag;
  FromTpmUint16(buffer, &tag);
  return (int) tag;
}


/* Gets the size field of a TPM command.
 */
POSSIBLY_UNUSED static INLINE int TpmResponseSize(const uint8_t* buffer) {
  uint32_t size;
  FromTpmUint32(buffer + sizeof(uint16_t), &size);
  return (int) size;
}


VbError_t VbExTpmInit(void) {
  return VbExTpmOpen();
}


VbError_t VbExTpmClose(void) {
  if (tpm_fd != -1) {
    close(tpm_fd);
    tpm_fd = -1;
  }
  return VBERROR_SUCCESS;
}


VbError_t VbExTpmOpen(void) {
  char* device_path;

  if (tpm_fd >= 0)
    return VBERROR_SUCCESS;  /* Already open */

  device_path = getenv("TPM_DEVICE_PATH");
  if (device_path == NULL) {
    device_path = TPM_DEVICE_PATH;
  }

  tpm_fd = open(device_path, O_RDWR);
  if (tpm_fd < 0) {
    VbExError("TPM: Cannot open TPM device %s: %s\n",
              device_path, strerror(errno));
  }

  return VBERROR_SUCCESS;
}


VbError_t VbExTpmSendReceive(const uint8_t* request, uint32_t request_length,
                             uint8_t* response, uint32_t* response_length) {
  /*
   * In a real firmware implementation, this function should contain
   * the equivalent API call for the firmware TPM driver which takes a
   * raw sequence of bytes as input command and a pointer to the
   * output buffer for putting in the results.
   *
   * For EFI firmwares, this can make use of the EFI TPM driver as
   * follows (based on page 16, of TCG EFI Protocol Specs Version 1.20
   * availaible from the TCG website):
   *
   * EFI_STATUS status;
   * status = TcgProtocol->EFI_TCG_PASS_THROUGH_TO_TPM(TpmCommandSize(request),
   *                                                   request,
   *                                                   max_length,
   *                                                   response);
   * // Error checking depending on the value of the status above
   */
#ifndef NDEBUG
  int tag, response_tag;
#endif

  struct timeval before, after;
  gettimeofday(&before, NULL);
  TpmExecute(request, request_length, response, response_length);
  gettimeofday(&after, NULL);

#ifdef VBOOT_DEBUG
  {
    int x = request_length;
    int y = *response_length;
    VBDEBUG(("request (%d bytes): ", x));
    PrintBytes(request, 10);
    PrintBytes(request + 10, x - 10);
    VBDEBUG(("response (%d bytes): ", y));
    PrintBytes(response, 10);
    PrintBytes(response + 10, y - 10);
    VBDEBUG(("execution time: %dms\n",
            (int) ((after.tv_sec - before.tv_sec) * 1000 +
                   (after.tv_usec - before.tv_usec) / 1000)));
  }
#endif

#ifndef NDEBUG
  /* sanity checks */
  tag = TpmTag(request);
  response_tag = TpmTag(response);
  assert(
    (tag == TPM_TAG_RQU_COMMAND &&
     response_tag == TPM_TAG_RSP_COMMAND) ||
    (tag == TPM_TAG_RQU_AUTH1_COMMAND &&
     response_tag == TPM_TAG_RSP_AUTH1_COMMAND) ||
    (tag == TPM_TAG_RQU_AUTH2_COMMAND &&
     response_tag == TPM_TAG_RSP_AUTH2_COMMAND));
  assert(*response_length == TpmResponseSize(response));
#endif

  return VBERROR_SUCCESS;
}