Skip to content

Commit 041ca75

Browse files
committed
Added support for TPM2.0 module via wolfTPM. Tested with STM32F4. Build using make SIGN=ECC256 WOLFTPM=1.
1 parent 4d49a7a commit 041ca75

12 files changed

Lines changed: 255 additions & 51 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "lib/wolfssl"]
22
path = lib/wolfssl
33
url = https://github.com/wolfSSL/wolfssl.git
4+
[submodule "lib/wolfTPM"]
5+
path = lib/wolfTPM
6+
url = https://github.com/wolfssl/wolfTPM

Makefile

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ OBJS:= \
1717
./src/string.o \
1818
./src/image.o \
1919
./src/libwolfboot.o
20+
WOLFCRYPT_OBJS:=
2021

2122

2223
## Architecture/CPU configuration
@@ -27,7 +28,7 @@ ifeq ($(SIGN),ECC256)
2728
KEYGEN_OPTIONS=--ecc256
2829
SIGN_OPTIONS=--ecc256
2930
PRIVATE_KEY=ecc256.der
30-
OBJS+= \
31+
WOLFCRYPT_OBJS+= \
3132
$(ECC_EXTRA_OBJS) \
3233
$(MATH_OBJS) \
3334
./lib/wolfssl/wolfcrypt/src/ecc.o \
@@ -45,7 +46,7 @@ ifeq ($(SIGN),ED25519)
4546
KEYGEN_OPTIONS=--ed25519
4647
SIGN_OPTIONS=--ed25519
4748
PRIVATE_KEY=ed25519.der
48-
OBJS+= ./lib/wolfssl/wolfcrypt/src/sha512.o \
49+
WOLFCRYPT_OBJS+= ./lib/wolfssl/wolfcrypt/src/sha512.o \
4950
./lib/wolfssl/wolfcrypt/src/ed25519.o \
5051
./lib/wolfssl/wolfcrypt/src/ge_low_mem.o \
5152
./lib/wolfssl/wolfcrypt/src/sha256.o \
@@ -63,7 +64,7 @@ ifeq ($(SIGN),RSA2048)
6364
SIGN_OPTIONS=--rsa2048
6465
PRIVATE_KEY=rsa2048.der
6566
IMAGE_HEADER_SIZE=512
66-
OBJS+= \
67+
WOLFCRYPT_OBJS+= \
6768
$(RSA_EXTRA_OBJS) \
6869
$(MATH_OBJS) \
6970
./lib/wolfssl/wolfcrypt/src/rsa.o \
@@ -93,7 +94,8 @@ endif
9394
ifeq ($(SPI_FLASH),1)
9495
EXT_FLASH=1
9596
CFLAGS+= -DSPI_FLASH=1
96-
OBJS+= src/spi_flash.o hal/spi/spi_drv_$(TARGET).o
97+
OBJS+= src/spi_flash.o
98+
WOLFCRYPT_OBJS+=hal/spi/spi_drv_$(TARGET).o
9799
endif
98100

99101
ifeq ($(EXT_FLASH),1)
@@ -124,6 +126,21 @@ ifeq ($(VTOR),0)
124126
CFLAGS+=-DNO_VTOR
125127
endif
126128

129+
ifeq ($(WOLFTPM),1)
130+
OBJS += lib/wolfTPM/src/tpm2.o \
131+
lib/wolfTPM/src/tpm2_packet.o \
132+
lib/wolfTPM/src/tpm2_tis.o \
133+
lib/wolfTPM/src/tpm2_wrap.o \
134+
src/ecc256_pub_key.o \
135+
hal/spi/spi_drv_$(TARGET).o
136+
CFLAGS+=-DWOLFTPM_SLB9670 -DWOLFTPM2_NO_WOLFCRYPT -DSIZEOF_LONG=4 -Ilib/wolfTPM \
137+
-DMAX_COMMAND_SIZE=1024 -DMAX_RESPONSE_SIZE=1024 -DWOLFTPM2_MAX_BUFFER=1500 -DMAX_SESSION_NUM=1 -DMAX_DIGEST_BUFFER=973 \
138+
-DWOLFTPM_SMALL_STACK
139+
140+
else
141+
OBJS+=$(WOLFCRYPT_OBJS)
142+
endif
143+
127144
ASFLAGS:=$(CFLAGS)
128145

129146
all: factory.bin

docs/Targets.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ reset
352352
halt
353353
```
354354

355+
`openocd --file openocd.cfg`
356+
355357
OpenOCD can be either run in background (to allow remote GDB and monitor terminal connections), or
356358
directly from command line, to execute terminal scripts.
357359

@@ -395,9 +397,10 @@ Use the OpenOCD configuration from the previous section to run OpenOCD.
395397

396398
From another console, connect using gdb, e.g.:
397399

400+
Add wolfboot.elf to the make.
401+
398402
```
399-
arm-none-eabi-gdb
400-
(gdb) target remote:3333
403+
arm-none-eabi-gdb wolfboot.elf -ex "set remotetimeout 240" -ex "target extended-remote localhost:3333"
401404
(gdb) add-symbol-file test-app/image.elf 0x08020000
402405
(gdb) add-symbol-file wolfboot.elf 0x08000000
403406
```

hal/spi/spi_drv_stm32f4.c

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@
2828
#include "spi_drv.h"
2929
#include "spi_drv_stm32f4.h"
3030

31-
void spi_cs_off(void)
31+
void spi_cs_off(int pin)
3232
{
33-
GPIOE_BSRR |= (1 << SPI_FLASH_PIN);
34-
while(!(GPIOE_ODR & (1 << SPI_FLASH_PIN)))
33+
GPIOE_BSRR |= (1 << pin);
34+
while(!(GPIOE_ODR & (1 << pin)))
3535
;
3636
}
3737

38-
void spi_cs_on(void)
38+
void spi_cs_on(int pin)
3939
{
40-
GPIOE_BSRR |= (1 << (SPI_FLASH_PIN + 16));
41-
while(GPIOE_ODR & (1 << SPI_FLASH_PIN))
40+
GPIOE_BSRR |= (1 << (pin + 16));
41+
while(GPIOE_ODR & (1 << pin))
4242
;
4343
}
4444

@@ -47,15 +47,28 @@ static void spi_flash_pin_setup(void)
4747
{
4848
uint32_t reg;
4949
AHB1_CLOCK_ER |= GPIOE_AHB1_CLOCK_ER;
50-
reg = GPIOE_MODE & ~ (0x03 << (SPI_FLASH_PIN * 2));
51-
GPIOE_MODE = reg | (1 << (SPI_FLASH_PIN * 2));
52-
53-
reg = GPIOE_PUPD & ~(0x03 << (SPI_FLASH_PIN * 2));
54-
GPIOE_PUPD = reg | (0x01 << (SPI_FLASH_PIN * 2));
55-
56-
reg = GPIOE_OSPD & ~(0x03 << (SPI_FLASH_PIN * 2));
57-
GPIOE_OSPD |= (0x03 << (SPI_FLASH_PIN * 2));
50+
reg = GPIOE_MODE & ~ (0x03 << (SPI_CS_FLASH * 2));
51+
GPIOE_MODE = reg | (1 << (SPI_CS_FLASH * 2));
52+
reg = GPIOE_PUPD & ~(0x03 << (SPI_CS_FLASH * 2));
53+
GPIOE_PUPD = reg | (0x01 << (SPI_CS_FLASH * 2));
54+
reg = GPIOE_OSPD & ~(0x03 << (SPI_CS_FLASH * 2));
55+
GPIOE_OSPD |= (0x03 << (SPI_CS_FLASH * 2));
56+
spi_cs_off(SPI_CS_FLASH);
57+
}
5858

59+
static void spi_tpm2_pin_setup(void)
60+
{
61+
#ifdef WOLFTPM2_NO_WOLFCRYPT
62+
uint32_t reg;
63+
AHB1_CLOCK_ER |= GPIOE_AHB1_CLOCK_ER;
64+
reg = GPIOE_MODE & ~ (0x03 << (SPI_CS_TPM * 2));
65+
GPIOE_MODE = reg | (1 << (SPI_CS_TPM * 2));
66+
reg = GPIOE_PUPD & ~(0x03 << (SPI_CS_TPM * 2));
67+
GPIOE_PUPD = reg | (0x01 << (SPI_CS_TPM * 2));
68+
reg = GPIOE_OSPD & ~(0x03 << (SPI_CS_TPM * 2));
69+
GPIOE_OSPD |= (0x03 << (SPI_CS_TPM * 2));
70+
spi_cs_off(SPI_CS_TPM);
71+
#endif
5972
}
6073

6174
static void spi1_pins_setup(void)
@@ -98,8 +111,8 @@ static void spi_pins_release(void)
98111
GPIOB_PUPD &= ~ (0x03 << (SPI1_MISO_PIN * 2));
99112

100113
/* Release CS */
101-
GPIOE_MODE &= ~ (0x03 << (SPI_FLASH_PIN * 2));
102-
GPIOE_PUPD &= ~ (0x03 << (SPI_FLASH_PIN * 2));
114+
GPIOE_MODE &= ~ (0x03 << (SPI_CS_FLASH * 2));
115+
GPIOE_PUPD &= ~ (0x03 << (SPI_CS_TPM * 2));
103116

104117
/* Disable GPIOB+GPIOE clock */
105118
AHB1_CLOCK_ER &= ~(GPIOB_AHB1_CLOCK_ER | GPIOE_AHB1_CLOCK_ER);
@@ -136,13 +149,18 @@ void spi_write(const char byte)
136149

137150
void spi_init(int polarity, int phase)
138151
{
139-
spi1_pins_setup();
140-
spi_flash_pin_setup();
141-
APB2_CLOCK_ER |= SPI1_APB2_CLOCK_ER_VAL;
142-
spi1_reset();
143-
SPI1_CR1 = SPI_CR1_MASTER | (5 << 3) | (polarity << 1) | (phase << 0);
144-
SPI1_CR2 |= SPI_CR2_SSOE;
145-
SPI1_CR1 |= SPI_CR1_SPI_EN;
152+
static int initialized = 0;
153+
if (!initialized) {
154+
initialized++;
155+
spi1_pins_setup();
156+
spi_flash_pin_setup();
157+
spi_tpm2_pin_setup();
158+
APB2_CLOCK_ER |= SPI1_APB2_CLOCK_ER_VAL;
159+
spi1_reset();
160+
SPI1_CR1 = SPI_CR1_MASTER | (5 << 3) | (polarity << 1) | (phase << 0);
161+
SPI1_CR2 |= SPI_CR2_SSOE;
162+
SPI1_CR1 |= SPI_CR1_SPI_EN;
163+
}
146164
}
147165

148166
void spi_release(void)

hal/spi/spi_drv_stm32f4.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
/** SPI settings **/
55

66
#define SPI1 (0x40013000)/* SPI1 base address */
7-
#define SPI_FLASH_PIN 1 /* Flash CS connected to GPIOE1 */
7+
#define SPI_CS_FLASH 3 /* Flash CS connected to GPIOE1 */
8+
#define SPI_CS_TPM 1 /* TPM CS connected to GPIOE0 */
89
#define SPI1_PIN_AF 5 /* Alternate function for SPI pins */
910
#define SPI1_CLOCK_PIN 3 /* SPI_SCK: PB3 */
1011
#define SPI1_MISO_PIN 4 /* SPI_MISO PB4 */

include/loader.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@
4747
# error "No public key available for given signing algorithm."
4848
#endif /* Algorithm selection */
4949

50-
50+
#ifdef WOLFTPM2_NO_WOLFCRYPT
51+
int wolfBoot_tpm2_init(void);
52+
#endif
5153
#endif /* LOADER_H */

include/spi_drv.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
#define SPI_DRV_H_INCLUDED
3131

3232
#include <stdint.h>
33+
#ifdef PLATFORM_stm32f4
34+
#include "hal/spi/spi_drv_stm32f4.h"
35+
#endif
3336

3437
void spi_init(int polarity, int phase);
3538
void spi_write(const char byte);
3639
uint8_t spi_read(void);
37-
void spi_cs_on(void);
38-
void spi_cs_off(void);
40+
void spi_cs_on(int pin);
41+
void spi_cs_off(int pin);
3942

4043
#endif /* !SPI_DRV_H_INCLUDED */

lib/wolfTPM

Submodule wolfTPM added at b37ac1d

0 commit comments

Comments
 (0)