Skip to content

Commit aa71cfd

Browse files
committed
Minor cleanups.
1 parent 8946234 commit aa71cfd

8 files changed

Lines changed: 83 additions & 88 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ examples/nvram/read
5555
examples/gpio/gpio_config
5656
examples/gpio/gpio_set
5757
examples/gpio/gpio_read
58+
examples/gpio/gpio_nuvoton
5859
examples/seal/seal
5960
examples/seal/unseal
6061
examples/attestation/make_credential

examples/README.md

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -427,22 +427,27 @@ mySecretMessage
427427

428428
After a successful unsealing, the data is stored into a new file. If no filename is provided, the `unseal` tool stores the data in `unseal.bin`.
429429

430-
## GPIO control
430+
431+
## GPIO Control
431432

432433
Some TPM 2.0 modules have extra I/O functionalities and additional GPIO that the developer could use. This extra GPIO could be used to signal other subsystems about security events or system states.
433434

434-
Currently, the GPIO control examples support only ST33 TPM 2.0 modules.
435+
Currently, the GPIO control examples support ST33 and NPCT75x TPM 2.0 modules.
435436

436-
There are four examples available: `gpio/gpio_config` for ST33 and `gpio/gpio_nuvoton` for NPCT75x. Once configured, a GPIO can be controlled using `gpio/gpio_set` and `gpio/gpio_read`.
437+
There are four examples available: `gpio/gpio_config` for ST33 and `gpio/gpio_nuvoton` for NPCT75x.
437438

438439
Every example has a help option `-h`. Please consult with `gpio_config -h` about the various GPIO modes.
439440

441+
Once configured, a GPIO can be controlled using `gpio/gpio_set` and `gpio/gpio_read`.
442+
440443
Demo usage is available, when no parameters are supplied. Recommended is to use carefully selected options, because GPIO interact with the physical world.
441444

445+
### GPIO Config
446+
442447
ST33 supports 6 modes, information from `gpio/gpio_config` below:
443-
```
444448

445-
examples/gpio/gpio_config -h
449+
```
450+
$ ./examples/gpio/gpio_config -h
446451
Expected usage:
447452
./examples/gpio/gpio_config [num] [mode]
448453
* num is a GPIO number between 0-3 (default 0)
@@ -455,77 +460,81 @@ Expected usage:
455460
5. pushpull - output in push pull configuration
456461
6. unconfigure - delete the NV index for the selected GPIO
457462
Example usage, without parameters, configures GPIO0 as input with a pull down.
458-
459463
```
460464

461-
NPCT75x supports 3 output modes, information from `gpio/gpio_nuvoton` below:
465+
Example usage for configuring a GPIO to output can be found below:
462466

463467
```
464-
xpected usage:
465-
./examples/gpio/gpio_config [num] [mode]
466-
* num is a GPIO number between 3 and 4 (default 3)
467-
* mode is either push-pull, open-drain or open-drain with pull-up
468-
1. pushpull - output in push pull configuration
469-
2. opendrain - output in open drain configuration
470-
3. pullup - output in open drain with pull-up enabled
471-
4. unconfig - delete NV index for GPIO access
472-
Example usage, without parameters, configures GPIO3 as push-pull output.
468+
$ ./examples/gpio/gpio_config 0 5
469+
GPIO num is: 0
470+
GPIO mode is: 5
471+
Example how to use extra GPIO on a TPM 2.0 modules
472+
Trying to configure GPIO0...
473+
TPM2_GPIO_Config success
474+
NV Index for GPIO access created
473475
```
474476

475-
Please note that NPCT75x GPIO numbering starts from GPIO3, while ST33 starts from GPIO0.
476-
477-
Example usage for configuring a GPIO to output can be found below:
477+
Example usage for configuring a GPIO as input with a pull-up on ST33 can be found below:
478478

479-
- ST33
480479
```
481-
482-
$ ./examples/gpio/gpio_config
480+
$ ./examples/gpio/gpio_config 0 3
483481
GPIO num is: 0
484-
GPIO mode is: 5
485-
Example how to use extra GPIO on a TPM 2.0 modules
486-
wolfTPM2_Init: success
482+
GPIO mode is: 3
483+
Demo how to use extra GPIO on a TPM 2.0 modules
487484
Trying to configure GPIO0...
488485
TPM2_GPIO_Config success
489486
NV Index for GPIO access created
487+
```
490488

491-
$ ./examples/gpio/gpio_set
492-
GPIO0 set to high level
489+
### GPIO Config (NPCT75xx)
490+
491+
NPCT75x supports 3 output modes, information from `gpio/gpio_nuvoton` below:
493492

493+
```
494+
$ ./examples/gpio/gpio_nuvoton -h
495+
Expected usage:
496+
./examples/gpio/gpio_nuvoton [num] [mode]
497+
* num is a GPIO number between 3 and 4 (default 3)
498+
* mode is either push-pull, open-drain or open-drain with pull-up
499+
1. pushpull - output in push pull configuration
500+
2. opendrain - output in open drain configuration
501+
3. pullup - output in open drain with pull-up enabled
502+
4. unconfig - delete NV index for GPIO access
503+
Example usage, without parameters, configures GPIO3 as push-pull output.
494504
```
495505

496-
- NPCT75xx
506+
Please note that NPCT75x GPIO numbering starts from GPIO3, while ST33 starts from GPIO0.
497507

498508
```
499-
pi@raspberrypi:~/wolftpm $ sudo ./examples/gpio/gpio_nuvoton 4 1
509+
$ ./examples/gpio/gpio_nuvoton 4 1
500510
Example for GPIO configuration of a NPTC7xx TPM 2.0 module
501511
GPIO number: 4
502512
GPIO mode: 1
503-
wolfTPM2_Init: success
504-
First, the current NPCT7xx config will be read
505-
then modified with the new GPIO configuration
506-
Successfully read the current NPCT7xx configuration
507-
NTC2_PreConfig success
513+
Successfully read the current configuration
514+
Successfully wrote new configuration
508515
NV Index for GPIO access created
509516
```
510517

511-
Switching a GPIO configuration is seamless. Because for ST33 `gpio/gpio_config` takes care of deleting existing NV Index, so a new GPIO configuration can be chosen. And for NPCT75xx `gpio/gpio_nuvoton` can reconfigure any GPIO without deleteing the creating NV index.
518+
### GPIO Usage
512519

513-
Example usage for configuring a GPIO as input with a pull-up on ST33 can be found below:
520+
Switching a GPIO configuration is seamless.
521+
* For ST33 `gpio/gpio_config` takes care of deleting existing NV Index, so a new GPIO configuration can be chosen.
522+
* For NPCT75xx `gpio/gpio_nuvoton` can reconfigure any GPIO without deleting the created NV index.
514523

515524
```
525+
$ ./examples/gpio/gpio_set 0 -high
526+
GPIO0 set to high level
516527
517-
$ ./examples/gpio/gpio_config 0 3
518-
GPIO num is: 0
519-
GPIO mode is: 3
520-
Demo how to use extra GPIO on a TPM 2.0 modules
521-
wolfTPM2_Init: success
522-
Trying to configure GPIO0...
523-
TPM2_GPIO_Config success
524-
NV Index for GPIO access created
528+
$ ./examples/gpio/gpio_set 0 -low
529+
GPIO0 set to low level
530+
```
525531

532+
```
526533
$ ./examples/gpio/gpio_read 0
527534
GPIO0 is Low
528-
529535
```
530536

537+
538+
## Support
539+
531540
If you need more information about using these examples please contact us at support@wolfssl.com

examples/gpio/gpio_config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
150150

151151
/* Enable TPM2_GPIO_Config command */
152152
if (caps.mfg != TPM_MFG_STM) {
153-
printf("Extra GPIO is supported only on ST33 TPM 2.0 modules\n");
153+
printf("TPM model mismatch. GPIO support requires an ST33 TPM 2.0 module\n");
154154
goto exit;
155155
}
156156

@@ -255,7 +255,7 @@ int main(int argc, char *argv[])
255255
#if defined(WOLFTPM_ST33) || defined(WOLFTPM_AUTODETECT)
256256
rc = TPM2_GPIO_Config_Example(NULL, argc, argv);
257257
#else
258-
printf("Extra GPIO is supported only on ST33 TPM 2.0 modules.\n");
258+
printf("GPIO configuration requires an ST33 TPM 2.0 module built with WOLFTPM_ST33 or --enable-st33\n");
259259
(void)argc;
260260
(void)argv;
261261
#endif /* WOLFTPM_ST33 || WOLFTPM_AUTODETECT */

examples/gpio/gpio_nuvoton.c

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
static void usage(void)
4343
{
4444
printf("Expected usage:\n");
45-
printf("./examples/gpio/gpio_config [num] [mode]\n");
45+
printf("./examples/gpio/gpio_nuvoton [num] [mode]\n");
4646
printf("* num is a GPIO number between 3 and 4 (default %d)\n", GPIO_NUM_MIN);
4747
printf("* mode is either push-pull, open-drain or open-drain with pull-up\n");
4848
printf("\t1. pushpull - output in push pull configuration\n");
@@ -127,58 +127,39 @@ int TPM2_GPIO_Nuvoton_Example(void* userCtx, int argc, char *argv[])
127127

128128
/* Confirm the TPM vendor */
129129
if (caps.mfg != TPM_MFG_NUVOTON) {
130-
printf("TPM model mismatch. This example demonstrates extra GPIO on NPCT7xx.\n");
130+
printf("TPM model mismatch. GPIO support requires a Nuvoton NPCT7xx TPM 2.0 module\n");
131131
goto exit;
132132
}
133133

134-
#if 0 /* TODO: Satisfy NV_POLICY_DELETE */
135-
#ifdef DEBUG_WOLFTPM
136-
printf("Trying to remove NV index 0x%8.8X used for GPIO\n", nvIndex);
137-
#endif
138-
/* Make sure NV Index for this GPIO is cleared before use
139-
* This way we make sure a new GPIO config can be set
140-
*/
141-
rc = wolfTPM2_NVDelete(&dev, TPM_RH_PLATFORM, nvIndex);
142-
if (rc == TPM_RC_SUCCESS) {
143-
printf("NV index undefined\n");
144-
}
145-
else if (rc == (TPM_RC_HANDLE | TPM_RC_2)) {
146-
printf("NV Index is available for GPIO use\n");
147-
}
148-
else {
149-
printf("wolfTPM2_NVDelete failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
150-
}
151-
#endif
152134
/* GPIO un-configuration is done using NVDelete, no further action needed */
135+
/* Nuvoton can reconfigure any GPIO without deleting the created NV index */
153136
if (gpioMode == NUVOTON_GPIO_MODE_UNCONFIG) {
154137
printf("Reconfiguration does not require to NV index deletion\n");
155138
goto exit;
156139
}
157140

158-
printf("First, the current NPCT7xx config will be read\n" \
159-
"then modified with the new GPIO configuration\n");
160-
161141
XMEMSET(&newConfig, 0, sizeof(newConfig));
162142
XMEMSET(&getConfig, 0, sizeof(getConfig));
163143
rc = TPM2_NTC2_GetConfig(&getConfig);
164144
if (rc != TPM_RC_SUCCESS) {
165-
printf("NTC2_getConfig failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
145+
printf("TPM2_NTC2_GetConfig failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
166146
goto exit;
167147
}
168-
printf("Successfully read the current NPCT7xx configuration\n");
148+
printf("Successfully read the current configuration\n");
169149
XMEMCPY(&newConfig, &getConfig.preConfig, sizeof(newConfig));
170150

171-
#ifdef DEBUG_WOLFTPM
151+
#ifdef WOLFTPM_DEBUG_VERBOSE
172152
printf("getConfig CFG_CONFIG structure:\n");
173153
TPM2_PrintBin((byte*)&getConfig.preConfig, sizeof(getConfig.preConfig));
174154
#endif
175155

176156
/* Prepare GPIO configuration according to Nuvoton requirements */
177157
if(gpioMode == NUVOTON_GPIO_MODE_PUSHPULL) {
158+
/* For NUVOTON_GPIO_MODE_PUSHPULL */
178159
newConfig.GpioPushPull |= (1 << gpioNum);
179160
}
180161
else {
181-
/* NUVOTON_GPIO_MODE_OPENDRAIN || NUVOTON_GPIO_MODE_PULLUP */
162+
/* For NUVOTON_GPIO_MODE_OPENDRAIN or NUVOTON_GPIO_MODE_PULLUP */
182163
newConfig.GpioPushPull &= ~(1 << gpioNum);
183164
}
184165

@@ -190,7 +171,7 @@ int TPM2_GPIO_Nuvoton_Example(void* userCtx, int argc, char *argv[])
190171
newConfig.GpioPullUp &= ~(1 << gpioNum);
191172
}
192173

193-
#ifdef DEBUG_WOLFTPM
174+
#ifdef WOLFTPM_DEBUG_VERBOSE
194175
printf("newConfig CFG_CONFIG structure:\n");
195176
TPM2_PrintBin((byte*)&newConfig, sizeof(newConfig));
196177
#endif
@@ -204,10 +185,10 @@ int TPM2_GPIO_Nuvoton_Example(void* userCtx, int argc, char *argv[])
204185
XMEMCPY(&preConfig.preConfig, &newConfig, sizeof(newConfig));
205186
rc = TPM2_NTC2_PreConfig(&preConfig);
206187
if (rc != TPM_RC_SUCCESS) {
207-
printf("TPM2_SetCommandSet failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
188+
printf("TPM2_NTC2_PreConfig failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
208189
goto exit;
209190
}
210-
printf("NTC2_PreConfig success\n");
191+
printf("Successfully wrote new configuration\n");
211192

212193
/* Configure NV Index for access to this GPIO */
213194
XMEMSET(&nv, 0, sizeof(nv));
@@ -257,7 +238,7 @@ int main(int argc, char *argv[])
257238
#if defined(WOLFTPM_NUVOTON)
258239
rc = TPM2_GPIO_Nuvoton_Example(NULL, argc, argv);
259240
#else
260-
printf("This example demonstrates extra GPIO on Nuvoton TPM 2.0 modules.\n");
241+
printf("GPIO configuration requires a Nuvoton NPCT75x TPM 2.0 module built with WOLFTPM_NUVOTON or --enable-nuvoton.\n");
261242
(void)argc;
262243
(void)argv;
263244
#endif /* WOLFTPM_NUVOTON */

examples/gpio/gpio_read.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/* Example for reading the voltage level of TPM's GPIO
2323
*
24-
* Note: GPIO must be first configured using gpio/config
24+
* Note: GPIO must be first configured using gpio/gpio_config
2525
*
2626
*/
2727

examples/gpio/gpio_set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/* Example for setting the voltage level of TPM's GPIO
2323
*
24-
* Note: GPIO must be first configured using gpio/config
24+
* Note: GPIO must be first configured using gpio/gpio_config
2525
*
2626
*/
2727

examples/gpio/include.am

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,35 @@
44
if BUILD_EXAMPLES
55
noinst_PROGRAMS += examples/gpio/gpio_config \
66
examples/gpio/gpio_read \
7-
examples/gpio/gpio_set \
8-
examples/gpio/gpio_nuvoton
7+
examples/gpio/gpio_set
98

109
noinst_HEADERS += examples/gpio/gpio.h
1110

1211
examples_gpio_gpio_config_SOURCES = examples/gpio/gpio_config.c \
13-
examples/tpm_io.c
12+
examples/tpm_io.c
1413
examples_gpio_gpio_config_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1514
examples_gpio_gpio_config_DEPENDENCIES = src/libwolftpm.la
1615

1716
examples_gpio_gpio_read_SOURCES = examples/gpio/gpio_read.c \
18-
examples/tpm_io.c
17+
examples/tpm_io.c
1918
examples_gpio_gpio_read_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
2019
examples_gpio_gpio_read_DEPENDENCIES = src/libwolftpm.la
2120

2221
examples_gpio_gpio_set_SOURCES = examples/gpio/gpio_set.c \
23-
examples/tpm_io.c
22+
examples/tpm_io.c
2423
examples_gpio_gpio_set_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
2524
examples_gpio_gpio_set_DEPENDENCIES = src/libwolftpm.la
25+
endif
2626

27+
if BUILD_NUVOTON
28+
noinst_PROGRAMS += examples/gpio/gpio_nuvoton
2729
examples_gpio_gpio_nuvoton_SOURCES = examples/gpio/gpio_nuvoton.c \
28-
examples/tpm_io.c
30+
examples/tpm_io.c
2931
examples_gpio_gpio_nuvoton_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
3032
examples_gpio_gpio_nuvoton_DEPENDENCIES = src/libwolftpm.la
31-
3233
endif
3334

35+
3436
dist_example_DATA+= examples/gpio/gpio_config.c
3537
dist_example_DATA+= examples/gpio/gpio_read.c
3638
dist_example_DATA+= examples/gpio/gpio_set.c

src/tpm2.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ static TPM_RC TPM2_AcquireLock(TPM2_CTX* ctx)
6363

6464
if (!ctx->hwLockInit) {
6565
if (wc_InitMutex(&ctx->hwLock) != 0) {
66-
WOLFSSL_MSG("TPM Mutex Init failed");
66+
#ifdef DEBUG_WOLFTPM
67+
printf("TPM Mutex Init failed\n");
68+
#endif
6769
return TPM_RC_FAILURE;
6870
}
6971
ctx->hwLockInit = 1;

0 commit comments

Comments
 (0)