|
63 | 63 | #endif |
64 | 64 | #include <fcntl.h> |
65 | 65 | #include <unistd.h> |
| 66 | + #include <errno.h> |
66 | 67 |
|
67 | 68 | #ifdef WOLFTPM_I2C |
68 | 69 | /* I2C - (Only tested with SLB9673 and ST33 I2C) */ |
69 | 70 | #define TPM2_I2C_ADDR 0x2e |
70 | 71 | #define TPM2_I2C_DEV "/dev/i2c-1" |
71 | 72 | #define TPM2_I2C_HZ 400000 /* 400kHz */ |
| 73 | + static int i2cOpenFailed = 0; |
72 | 74 | #else |
73 | 75 | /* SPI */ |
74 | 76 | #ifndef TPM2_SPI_DEV_CS |
|
97 | 99 | static char TPM2_SPI_DEV[] = TPM2_SPI_DEV_PATH "0"; |
98 | 100 | #define MAX_SPI_DEV_CS '4' |
99 | 101 | static int foundSpiDev = 0; |
| 102 | + static int spiDevNotFound = 0; |
100 | 103 | #else |
101 | 104 | #define TPM2_SPI_DEV TPM2_SPI_DEV_PATH TPM2_SPI_DEV_CS |
| 105 | + static int spiOpenFailed = 0; |
102 | 106 | #endif |
103 | 107 | #endif |
104 | 108 | #endif |
|
190 | 194 |
|
191 | 195 | close(i2cDev); |
192 | 196 | } |
| 197 | + else if (!i2cOpenFailed) { |
| 198 | + i2cOpenFailed = 1; |
| 199 | + if (errno == EACCES) { |
| 200 | + printf("Permission denied on %s\n" |
| 201 | + "Use sudo or add appropriate group to user.\n", |
| 202 | + TPM2_I2C_DEV); |
| 203 | + } |
| 204 | + #ifdef DEBUG_WOLFTPM |
| 205 | + else { |
| 206 | + printf("Failed to open I2C device %s (errno %d)\n", |
| 207 | + TPM2_I2C_DEV, errno); |
| 208 | + } |
| 209 | + #endif |
| 210 | + } |
193 | 211 |
|
194 | 212 | (void)ctx; |
195 | 213 | (void)userCtx; |
|
198 | 216 | } |
199 | 217 |
|
200 | 218 | #else |
| 219 | + /* Called when SPI device cannot be opened or no TPM found on SPI bus. |
| 220 | + * Checks if the Linux kernel TPM driver is available and suggests |
| 221 | + * alternatives. */ |
| 222 | + static void spiOpenFailedMessage(void) |
| 223 | + { |
| 224 | + #ifdef WOLFTPM_LINUX_DEV_AUTODETECT |
| 225 | + /* Autodetect already tried /dev/tpm0; SPI also failed */ |
| 226 | + #ifdef DEBUG_WOLFTPM |
| 227 | + printf("Neither /dev/tpm0 nor SPI bus produced a TPM response.\n" |
| 228 | + "Ensure a TPM is connected and the kernel driver or spidev " |
| 229 | + "is enabled.\n"); |
| 230 | + #endif |
| 231 | + #else |
| 232 | + if (access("/dev/tpm0", F_OK) == 0 || |
| 233 | + access("/dev/tpmrm0", F_OK) == 0) { |
| 234 | + printf("TPM kernel driver detected (/dev/tpm0).\n" |
| 235 | + "Either build wolfTPM with ./configure --enable-devtpm\n" |
| 236 | + "or disable the kernel driver by commenting out the TPM\n" |
| 237 | + "overlay in /boot/config.txt or /boot/firmware/config.txt\n" |
| 238 | + "and enable spidev to use direct SPI access.\n"); |
| 239 | + } |
| 240 | + #ifdef DEBUG_WOLFTPM |
| 241 | + else { |
| 242 | + printf("If using Linux kernel TPM driver (/dev/tpm0), " |
| 243 | + "build with --enable-devtpm.\n" |
| 244 | + "To use SPI directly, make sure /dev/spidev is available " |
| 245 | + "and the TPM\nkernel overlay is disabled in /boot/config.txt " |
| 246 | + "or /boot/firmware/config.txt.\n"); |
| 247 | + } |
| 248 | + #endif |
| 249 | + #endif /* WOLFTPM_LINUX_DEV_AUTODETECT */ |
| 250 | + } |
| 251 | + |
201 | 252 | /* Use Linux SPI synchronous access */ |
202 | 253 | int TPM2_IoCb_Linux_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf, |
203 | 254 | word16 xferSz, void* userCtx) |
|
308 | 359 | else { |
309 | 360 | /* Failed to open device */ |
310 | 361 | ret = TPM_RC_FAILURE; |
| 362 | + #ifndef WOLFTPM_AUTODETECT |
| 363 | + if (!spiOpenFailed) { |
| 364 | + spiOpenFailed = 1; |
| 365 | + if (errno == EACCES) { |
| 366 | + printf("Permission denied on %s\n" |
| 367 | + "Use sudo or check device permissions.\n", |
| 368 | + TPM2_SPI_DEV); |
| 369 | + } |
| 370 | + else { |
| 371 | + #ifdef DEBUG_WOLFTPM |
| 372 | + printf("Failed to open SPI device %s (errno %d)\n", |
| 373 | + TPM2_SPI_DEV, errno); |
| 374 | + #endif |
| 375 | + spiOpenFailedMessage(); |
| 376 | + } |
| 377 | + } |
| 378 | + #endif |
311 | 379 | } |
312 | 380 |
|
313 | 381 | #ifdef WOLFTPM_AUTODETECT |
|
326 | 394 | TPM2_SPI_DEV[devLen-1]++; |
327 | 395 | goto tryagain; |
328 | 396 | } |
| 397 | + if (!spiDevNotFound) { |
| 398 | + spiDevNotFound = 1; |
| 399 | + #ifdef DEBUG_WOLFTPM |
| 400 | + printf("TPM not found on SPI bus %s[0-%c]\n", |
| 401 | + TPM2_SPI_DEV_PATH, MAX_SPI_DEV_CS); |
| 402 | + #endif |
| 403 | + spiOpenFailedMessage(); |
| 404 | + } |
329 | 405 | } |
330 | 406 | } |
331 | 407 | #endif |
|
0 commit comments