|
31 | 31 | /* For some struct to buffer conversions */ |
32 | 32 | #include <wolftpm/tpm2_packet.h> |
33 | 33 | #include <hal/tpm_io.h> /* for default IO callback */ |
| 34 | +#ifdef WOLFTPM_LINUX_DEV_AUTODETECT |
| 35 | + #include <wolftpm/tpm2_linux.h> |
| 36 | +#endif |
34 | 37 |
|
35 | 38 | /* Local Functions */ |
36 | 39 | static int wolfTPM2_GetCapabilities_NoDev(WOLFTPM2_CAPS* cap); |
@@ -63,7 +66,53 @@ static int wolfTPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx, |
63 | 66 | if (ctx == NULL) |
64 | 67 | return BAD_FUNC_ARG; |
65 | 68 |
|
66 | | -#if defined(WOLFTPM_LINUX_DEV) || defined(WOLFTPM_SWTPM) || \ |
| 69 | +#if defined(WOLFTPM_LINUX_DEV_AUTODETECT) |
| 70 | + /* Phase 1: Minimal init (sets up IO callback, no TIS chip startup) */ |
| 71 | + rc = TPM2_Init_ex(ctx, ioCb, userCtx, 0); |
| 72 | + if (rc != TPM_RC_SUCCESS) { |
| 73 | + printf("TPM2_Init failed 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc)); |
| 74 | + return rc; |
| 75 | + } |
| 76 | + |
| 77 | + /* Phase 2: Try /dev/tpmrm0 then /dev/tpm0 */ |
| 78 | + rc = TPM2_LINUX_TryOpen(ctx); |
| 79 | + if (rc == TPM_RC_SUCCESS) { |
| 80 | + /* Using kernel driver - startup/locality handled by kernel */ |
| 81 | + #ifdef DEBUG_WOLFTPM |
| 82 | + printf("TPM2: Using Linux kernel driver\n"); |
| 83 | + #endif |
| 84 | + return TPM_RC_SUCCESS; |
| 85 | + } |
| 86 | + else if (rc == TPM_RC_FAILURE) { |
| 87 | + /* Permission denied or hard error - don't try SPI */ |
| 88 | + return rc; |
| 89 | + } |
| 90 | + /* rc == TPM_RC_INITIALIZE means "not found", fall through to SPI */ |
| 91 | + |
| 92 | + /* Phase 3: SPI fallback - requires IO callback */ |
| 93 | + if (ioCb == NULL) { |
| 94 | + #ifdef DEBUG_WOLFTPM |
| 95 | + printf("TPM2: Kernel driver not available and no IO callback for SPI\n"); |
| 96 | + #endif |
| 97 | + return TPM_RC_FAILURE; |
| 98 | + } |
| 99 | +#ifdef DEBUG_WOLFTPM |
| 100 | + printf("TPM2: Kernel driver not available, trying SPI\n"); |
| 101 | +#endif |
| 102 | + if (timeoutTries > 0) { |
| 103 | + rc = TPM2_ChipStartup(ctx, timeoutTries); |
| 104 | + if (rc != TPM_RC_SUCCESS) { |
| 105 | + printf("TPM2_Init failed 0x%x: %s\n", rc, |
| 106 | + wolfTPM2_GetRCString(rc)); |
| 107 | + return rc; |
| 108 | + } |
| 109 | + } |
| 110 | + else { |
| 111 | + rc = TPM_RC_SUCCESS; /* clear TryOpen sentinel */ |
| 112 | + } |
| 113 | + /* Fall through to Startup/SelfTest below */ |
| 114 | + |
| 115 | +#elif defined(WOLFTPM_LINUX_DEV) || defined(WOLFTPM_SWTPM) || \ |
67 | 116 | defined(WOLFTPM_WINAPI) |
68 | 117 | rc = TPM2_Init_minimal(ctx); |
69 | 118 | /* Using standard file I/O for the Linux TPM device */ |
@@ -8254,6 +8303,9 @@ static int tpm2_ifx_firmware_start(WOLFTPM2_DEV* dev, TPM_ALG_ID hashAlg, |
8254 | 8303 | #if !defined(WOLFTPM_LINUX_DEV) && !defined(WOLFTPM_SWTPM) && \ |
8255 | 8304 | !defined(WOLFTPM_WINAPI) |
8256 | 8305 | /* Do chip startup and request locality again */ |
| 8306 | + #ifdef WOLFTPM_LINUX_DEV_AUTODETECT |
| 8307 | + if (dev->ctx.fd < 0) /* Only needed for SPI path */ |
| 8308 | + #endif |
8257 | 8309 | rc = TPM2_ChipStartup(&dev->ctx, 10); |
8258 | 8310 | #endif |
8259 | 8311 | } |
@@ -8367,6 +8419,9 @@ static int tpm2_ifx_firmware_data(WOLFTPM2_DEV* dev, |
8367 | 8419 | #if !defined(WOLFTPM_LINUX_DEV) && !defined(WOLFTPM_SWTPM) && \ |
8368 | 8420 | !defined(WOLFTPM_WINAPI) |
8369 | 8421 | /* Do chip startup and request locality again */ |
| 8422 | + #ifdef WOLFTPM_LINUX_DEV_AUTODETECT |
| 8423 | + if (dev->ctx.fd < 0) /* Only needed for SPI path */ |
| 8424 | + #endif |
8370 | 8425 | rc = TPM2_ChipStartup(&dev->ctx, 10); |
8371 | 8426 | #endif |
8372 | 8427 | } |
@@ -8610,6 +8665,9 @@ static int tpm2_st33_firmware_start_common(WOLFTPM2_DEV* dev, |
8610 | 8665 | #if !defined(WOLFTPM_LINUX_DEV) && !defined(WOLFTPM_SWTPM) && \ |
8611 | 8666 | !defined(WOLFTPM_WINAPI) |
8612 | 8667 | /* Do chip startup and request locality again */ |
| 8668 | + #ifdef WOLFTPM_LINUX_DEV_AUTODETECT |
| 8669 | + if (dev->ctx.fd < 0) /* Only needed for SPI path */ |
| 8670 | + #endif |
8613 | 8671 | rc = TPM2_ChipStartup(&dev->ctx, 10); |
8614 | 8672 | #endif |
8615 | 8673 | } |
@@ -8732,6 +8790,9 @@ static int tpm2_st33_firmware_data(WOLFTPM2_DEV* dev, |
8732 | 8790 | #if !defined(WOLFTPM_LINUX_DEV) && !defined(WOLFTPM_SWTPM) && \ |
8733 | 8791 | !defined(WOLFTPM_WINAPI) |
8734 | 8792 | /* Do chip startup and request locality again */ |
| 8793 | + #ifdef WOLFTPM_LINUX_DEV_AUTODETECT |
| 8794 | + if (dev->ctx.fd < 0) /* Only needed for SPI path */ |
| 8795 | + #endif |
8735 | 8796 | rc = TPM2_ChipStartup(&dev->ctx, 10); |
8736 | 8797 | #endif |
8737 | 8798 | } |
|
0 commit comments