|
| 1 | +/* tpm2_usb.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2023 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfTPM. |
| 6 | + * |
| 7 | + * wolfTPM is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfTPM is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | + |
| 23 | +#ifdef WOLFTPM_USB |
| 24 | + |
| 25 | +#include <wolftpm/tpm2_packet.h> |
| 26 | +#include <wolftpm/tpm2_usb.h> |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +#define VID_CYPRESS 0x04B4u |
| 31 | +#define PID_CYUSBSPI 0x0004u |
| 32 | + |
| 33 | +#define CTRL_SET 0xC0u |
| 34 | +#define CTRL_GET 0x40u |
| 35 | + |
| 36 | +#define CY_CMD_SPI 0xCAu |
| 37 | +#define CY_CMD_GPIO_SET 0xDBu |
| 38 | +#define CY_SPI_WRITEREAD 0x03u |
| 39 | + |
| 40 | +#define EP_OUT 0x01u |
| 41 | +#define EP_IN 0x82u |
| 42 | + |
| 43 | +#define SPI_TIMEOUT 1000 |
| 44 | +#define SPI_MAX_TRANSFER (4 + 64) |
| 45 | + |
| 46 | +static int TPM2_USB_Init(TPM2_CTX* ctx) |
| 47 | +{ |
| 48 | + int ret; |
| 49 | + int nb_ifaces = 0; |
| 50 | + libusb_device *dev = NULL; |
| 51 | + struct libusb_config_descriptor *conf_desc = NULL; |
| 52 | + |
| 53 | + if (ctx->usbCtx.dev_ctx != NULL) { |
| 54 | + return 0; /* already initialized */ |
| 55 | + } |
| 56 | + |
| 57 | + ret = libusb_init(&ctx->usbCtx.dev_ctx); |
| 58 | + if (ret == 0) { |
| 59 | + ctx->usbCtx.dev_handle = libusb_open_device_with_vid_pid(ctx->usbCtx.dev_ctx, |
| 60 | + VID_CYPRESS, PID_CYUSBSPI); |
| 61 | + if (ctx->usbCtx.dev_handle == NULL) { |
| 62 | + ret = -1; |
| 63 | + } |
| 64 | + } |
| 65 | + if (ret == 0) { |
| 66 | + dev = libusb_get_device(ctx->usbCtx.dev_handle); |
| 67 | + if (dev == NULL) { |
| 68 | + ret = -1; |
| 69 | + } |
| 70 | + } |
| 71 | + if (ret == 0) { |
| 72 | + ret = libusb_get_config_descriptor(dev, 0, &conf_desc); |
| 73 | + if (ret == 0) { |
| 74 | + nb_ifaces = conf_desc->bNumInterfaces; |
| 75 | + if (nb_ifaces <= 0) { |
| 76 | + ret = -1; |
| 77 | + } |
| 78 | + libusb_free_config_descriptor(conf_desc); |
| 79 | + } |
| 80 | + } |
| 81 | + if (ret == 0) { |
| 82 | + ret = libusb_set_auto_detach_kernel_driver(ctx->usbCtx.dev_handle, 1); |
| 83 | + } |
| 84 | + if (ret == 0) { |
| 85 | + ret = libusb_claim_interface(ctx->usbCtx.dev_handle, 0); |
| 86 | + } |
| 87 | + |
| 88 | + ctx->usbCtx.spi_dma_buffer = libusb_dev_mem_alloc(ctx->usbCtx.dev_handle, SPI_MAX_TRANSFER); |
| 89 | + /* failure to allocate DMA, means we will use the buffer directly */ |
| 90 | + |
| 91 | + if (ret != 0) { |
| 92 | + TPM2_USB_Cleanup(ctx); |
| 93 | + } |
| 94 | + return ret; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +int TPM2_USB_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) |
| 99 | +{ |
| 100 | + int ret; |
| 101 | + int act_len = 0; |
| 102 | + int retry = 0; |
| 103 | + int transferred = 0; |
| 104 | + int length; |
| 105 | + uint8_t* buffer; |
| 106 | + |
| 107 | + ret = TPM2_USB_Init(ctx); |
| 108 | + |
| 109 | + /* start transfer */ |
| 110 | + if (ret == 0) { |
| 111 | + length = packet->pos; |
| 112 | + if (ctx->usbCtx.spi_dma_buffer != NULL && length < SPI_MAX_TRANSFER) { |
| 113 | + buffer = ctx->usbCtx.spi_dma_buffer; |
| 114 | + XMEMCPY(buffer, packet->buf, length); |
| 115 | + } |
| 116 | + else { |
| 117 | + buffer = packet->buf; |
| 118 | + } |
| 119 | + |
| 120 | + ret = libusb_control_transfer(ctx->usbCtx.dev_handle, CTRL_SET, CY_CMD_SPI, |
| 121 | + CY_SPI_WRITEREAD, length, NULL, 0, SPI_TIMEOUT); |
| 122 | + |
| 123 | + /* do send */ |
| 124 | + while (ret == 0 && transferred < length) { |
| 125 | + ret = libusb_bulk_transfer(ctx->usbCtx.dev_handle, EP_OUT, |
| 126 | + ctx->usbCtx.spi_dma_buffer + transferred, length, &act_len, SPI_TIMEOUT); |
| 127 | + if (ret == 0) { |
| 128 | + transferred += act_len; |
| 129 | + length -= act_len; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /* do receive */ |
| 134 | + transferred = 0; |
| 135 | + length = packet->pos; |
| 136 | + while (ret == 0 && transferred < length) { |
| 137 | + ret = libusb_bulk_transfer(ctx->usbCtx.dev_handle, EP_IN, |
| 138 | + ctx->usbCtx.spi_dma_buffer + transferred, length, &act_len, SPI_TIMEOUT); |
| 139 | + if (ret != 0) { |
| 140 | + /* allow retry up to 5 times */ |
| 141 | + if (retry++ > 5) { |
| 142 | + ret = -1; |
| 143 | + break; |
| 144 | + } |
| 145 | + continue; |
| 146 | + } |
| 147 | + transferred += act_len; |
| 148 | + length -= act_len; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return ret; |
| 153 | +} |
| 154 | + |
| 155 | +int TPM2_USB_Cleanup(TPM2_CTX* ctx) |
| 156 | +{ |
| 157 | + if (ctx->usbCtx.dev_handle != NULL) { |
| 158 | + if (ctx->usbCtx.spi_dma_buffer != NULL) { |
| 159 | + libusb_dev_mem_free(ctx->usbCtx.dev_handle, |
| 160 | + ctx->usbCtx.spi_dma_buffer, SPI_MAX_TRANSFER); |
| 161 | + } |
| 162 | + |
| 163 | + libusb_release_interface(ctx->usbCtx.dev_handle, 0); |
| 164 | + libusb_close(ctx->usbCtx.dev_handle); |
| 165 | + ctx->usbCtx.dev_handle = NULL; |
| 166 | + } |
| 167 | + if (ctx->usbCtx.dev_ctx != NULL) { |
| 168 | + libusb_exit(ctx->usbCtx.dev_ctx); |
| 169 | + ctx->usbCtx.dev_ctx = NULL; |
| 170 | + } |
| 171 | + return 0; |
| 172 | +} |
| 173 | + |
| 174 | +#endif /* WOLFTPM_USB */ |
0 commit comments