Skip to content

Commit f319296

Browse files
committed
Rebase wolfTPM Support For Das U-Boot Bootloader
1 parent 0cd21b5 commit f319296

File tree

15 files changed

+565
-59
lines changed

15 files changed

+565
-59
lines changed

examples/u-boot/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# wolfTPM Support For Das U-boot
2+
3+
wolfTPM provides experimental support for U-Boot with the following key features:
4+
5+
- Utilizes SOFT SPI driver in U-Boot for TPM communication
6+
- Implements TPM 2.0 driver functionality through its internal TIS layer
7+
- Provides native API access to all TPM 2.0 commands
8+
- Includes wrapper API for common TPM 2.0 operations
9+
- Supports two integration paths:
10+
- `__linux__`: Uses existing tpm interface via tpm2_linux.c
11+
- `__UBOOT__`: Direct SPI communication through tpm_io_uboot.c
12+
13+
## wolfTPM U-Boot Commands
14+
15+
The following commands are available through the `wolftpm` interface:
16+
17+
### Basic Commands
18+
19+
- `help` - Show help text
20+
- `device [num device]` - Show all devices or set the specified device
21+
- `info` - Show information about the TPM
22+
- `state` - Show internal state from the TPM (if available)
23+
- `autostart` - Initialize the TPM, perform a Startup(clear) and run a full selftest sequence
24+
- `init` - Initialize the software stack (must be first command)
25+
- `startup <mode> [<op>]` - Issue a TPM2_Startup command
26+
- `<mode>`: TPM2_SU_CLEAR (reset state) or TPM2_SU_STATE (preserved state)
27+
- `[<op>]`: optional shutdown with "off"
28+
- `self_test <type>` - Test TPM capabilities
29+
- `<type>`: "full" (all tests) or "continue" (untested tests only)
30+
31+
### PCR Operations
32+
33+
- `pcr_extend <pcr> <digest_addr> [<digest_algo>]` - Extend PCR with digest
34+
- `pcr_read <pcr> <digest_addr> [<digest_algo>]` - Read PCR to memory
35+
- `pcr_allocate <algorithm> <on/off> [<password>]` - Reconfig PCR bank algorithm
36+
- `pcr_setauthpolicy | pcr_setauthvalue <pcr> <key> [<password>]` - Change PCR access key
37+
- `pcr_print` - Print current PCR state
38+
39+
### Security Management
40+
41+
- `clear <hierarchy>` - Issue TPM2_Clear command
42+
- `<hierarchy>`: TPM2_RH_LOCKOUT or TPM2_RH_PLATFORM
43+
- `change_auth <hierarchy> <new_pw> [<old_pw>]` - Change hierarchy password
44+
- `<hierarchy>`: TPM2_RH_LOCKOUT, TPM2_RH_ENDORSEMENT, TPM2_RH_OWNER, or TPM2_RH_PLATFORM
45+
- `dam_reset [<password>]` - Reset internal error counter
46+
- `dam_parameters <max_tries> <recovery_time> <lockout_recovery> [<password>]` - Set DAM parameters
47+
- `caps` - Show TPM capabilities and info
48+
49+
### Firmware Management
50+
51+
- `firmware_update <manifest_addr> <manifest_sz> <firmware_addr> <firmware_sz>` - Update TPM firmware
52+
- `firmware_cancel` - Cancel TPM firmware update
53+
54+
## Enabling wolfTPM in U-Boot
55+
56+
Enable wolfTPM support in U-Boot by adding these options to your board's defconfig:
57+
58+
```
59+
CONFIG_TPM=y
60+
CONFIG_TPM_V2=y
61+
CONFIG_TPM_WOLF=y
62+
CONFIG_CMD_WOLFTPM=y
63+
```
64+
65+
Or use `make menuconfig` and enable:
66+
- Device Drivers → TPM → TPM 2.0 Support
67+
- Device Drivers → TPM → wolfTPM Support
68+
- Command line interface → Security commands → Enable wolfTPM commands
69+
70+
## Building and Running wolfTPM with U-Boot using QEMU
71+
72+
To build and run wolfTPM with U-Boot using QEMU and a tpm simulator, follow these steps:
73+
74+
1. Install swtpm:
75+
```
76+
git clone git@github.com:stefanberger/swtpm.git
77+
cd swtpm
78+
./autogen.sh
79+
make
80+
```
81+
82+
2. Build U-Boot:
83+
```
84+
make distclean
85+
export CROSS_COMPILE=aarch64-linux-gnu-
86+
export ARCH=aarch64
87+
make qemu_arm64_defconfig
88+
make -j4
89+
```
90+
91+
3. Create TPM directory:
92+
```
93+
mkdir -p ./tmp/mytpm1
94+
```
95+
96+
4. Start swtpm (in first terminal):
97+
```
98+
swtpm socket --tpm2 --tpmstate dir=./tmp/mytpm1 --ctrl type=unixio,path=./tmp/mytpm1/swtpm-sock --log level=20
99+
```
100+
101+
5. Start QEMU (in second terminal):
102+
```
103+
qemu-system-aarch64 -machine virt -nographic -cpu cortex-a57 -bios u-boot.bin -chardev socket,id=chrtpm,path=./tmp/mytpm1/swtpm-sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis-device,tpmdev=tpm0
104+
```
105+
106+
6. Exiting the QEMU:
107+
Press Ctrl-A followed by X

examples/u-boot/options.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* examples/u-boot/options.h
2+
*
3+
* Copyright (C) 2006-2021 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+
/* Example wolfTPM options.h for U-boot compilation */
23+
24+
#ifndef WOLFTPM_OPTIONS_H
25+
#define WOLFTPM_OPTIONS_H
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
#undef __UBOOT__
32+
#define __UBOOT__
33+
34+
#undef SIZEOF_LONG
35+
#define SIZEOF_LONG 8
36+
37+
#undef WOLFTPM2_NO_WOLFCRYPT
38+
#define WOLFTPM2_NO_WOLFCRYPT
39+
40+
#undef WOLFTPM_AUTODETECT
41+
#define WOLFTPM_AUTODETECT
42+
43+
#ifdef __cplusplus
44+
}
45+
#endif
46+
47+
#endif /* WOLFTPM_OPTIONS_H */
48+

examples/wrap/caps.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ int TPM2_Wrapper_CapsArgs(void* userCtx, int argc, char *argv[])
130130
TPM2_PCRs_Print();
131131

132132
exit:
133-
wolfTPM2_Shutdown(&dev, 0); /* 0=just shutdown, no startup */
133+
/* Only doShutdown=1: Just shut down the TPM */
134+
wolfTPM2_Reset(&dev, 1, 0);
134135

135136
wolfTPM2_Cleanup(&dev);
136137

examples/wrap/wrap_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,8 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
10101010
wolfTPM2_UnloadHandle(&dev, &ekKey.handle);
10111011
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
10121012

1013-
wolfTPM2_Shutdown(&dev, 0); /* 0=just shutdown, no startup */
1013+
/* Only doShutdown=1: Just shut down the TPM */
1014+
wolfTPM2_Reset(&dev, 1, 0);
10141015

10151016
wolfTPM2_Cleanup(&dev);
10161017

hal/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ src_libwolftpm_la_SOURCES += \
1313
hal/tpm_io_microchip.c \
1414
hal/tpm_io_st.c \
1515
hal/tpm_io_qnx.c \
16+
hal/tpm_io_uboot.c \
1617
hal/tpm_io_xilinx.c
1718
endif
1819

hal/tpm_io.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353

5454
#if defined(WOLFTPM_MMIO)
5555
#include "tpm_io_mmio.c"
56+
#elif defined(__UBOOT__)
57+
#include "hal/tpm_io_uboot.c"
5658
#elif defined(__linux__)
5759
#include "hal/tpm_io_linux.c"
5860
#elif defined(WOLFSSL_STM32_CUBEMX)
@@ -78,8 +80,10 @@ static int TPM2_IoCb_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
7880
word16 xferSz, void* userCtx)
7981
{
8082
int ret = TPM_RC_FAILURE;
81-
82-
#if defined(__linux__)
83+
84+
#if defined(__UBOOT__)
85+
ret = TPM2_IoCb_Uboot_SPI(ctx, txBuf, rxBuf, xferSz, userCtx);
86+
#elif defined(__linux__)
8387
ret = TPM2_IoCb_Linux_SPI(ctx, txBuf, rxBuf, xferSz, userCtx);
8488
#elif defined(WOLFSSL_STM32_CUBEMX)
8589
ret = TPM2_IoCb_STCubeMX_SPI(ctx, txBuf, rxBuf, xferSz, userCtx);

hal/tpm_io.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* - Xilinx Zynq
5050
* - Barebox
5151
* - QNX
52+
* - uboot
5253
* - Infineon Tri-Core
5354
* - Microchip MPLAB X Harmony (WOLFTPM_MICROCHIP_HARMONY)
5455
* Using custom IO Callback is always possible.
@@ -101,6 +102,9 @@ WOLFTPM_LOCAL int TPM2_IoCb_Atmel_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rx
101102
#elif defined(__BAREBOX__)
102103
WOLFTPM_LOCAL int TPM2_IoCb_Barebox_SPI(TPM2_CTX* ctx, const byte* txBuf,
103104
byte* rxBuf, word16 xferSz, void* userCtx);
105+
#elif defined(__UBOOT__)
106+
WOLFTPM_LOCAL int TPM2_IoCb_Uboot_SPI(TPM2_CTX* ctx, const byte* txBuf,
107+
byte* rxBuf, word16 xferSz, void* userCtx);
104108
#elif defined(__linux__)
105109
WOLFTPM_LOCAL int TPM2_IoCb_Linux_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
106110
word16 xferSz, void* userCtx);

hal/tpm_io_uboot.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* tpm_io_uboot.c
2+
*
3+
* Copyright (C) 2006-2025 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-1301, USA
20+
*/
21+
22+
/* This example shows IO interfaces for U-boot */
23+
24+
#include <wolftpm/tpm2.h>
25+
#include <wolftpm/tpm2_tis.h>
26+
#include "tpm_io.h"
27+
28+
/******************************************************************************/
29+
/* --- BEGIN IO Callback Logic -- */
30+
/******************************************************************************/
31+
32+
/* Included via tpm_io.c if WOLFTPM_INCLUDE_IO_FILE is defined */
33+
#ifdef WOLFTPM_INCLUDE_IO_FILE
34+
35+
#if ! (defined(WOLFTPM_LINUX_DEV) || \
36+
defined(WOLFTPM_SWTPM) || \
37+
defined(WOLFTPM_WINAPI) )
38+
39+
/* Use the max speed by default - see tpm2_types.h for chip specific max values */
40+
#ifndef TPM2_SPI_HZ
41+
#define TPM2_SPI_HZ TPM2_SPI_MAX_HZ
42+
#endif
43+
44+
#if defined(__UBOOT__)
45+
#include <config.h>
46+
int TPM2_IoCb_Uboot_SPI(TPM2_CTX* ctx, const byte* txBuf,
47+
byte* rxBuf, word16 xferSz, void* userCtx)
48+
{
49+
int ret = 0;
50+
struct udevice *dev;
51+
52+
/* Get the TPM device */
53+
if (ret == 0) {
54+
ret = tcg2_platform_get_tpm2(&dev);
55+
if ( ret != 0 || dev == NULL) {
56+
#ifdef DEBUG_WOLFTPM
57+
printf("Failed to get TPM device with error: %d\n", ret);
58+
#endif
59+
return TPM_RC_FAILURE;
60+
}
61+
}
62+
63+
/* Transfer the device data using tpm_xfer */
64+
if (ret == 0) {
65+
ret = tpm_xfer(dev, txBuf, xferSz, rxBuf, &xferSz);
66+
if (ret != 0) {
67+
#ifdef DEBUG_WOLFTPM
68+
printf("tpm_xfer failed with error: %d\n", ret);
69+
#endif
70+
return TPM_RC_FAILURE;
71+
}
72+
}
73+
74+
return TPM_RC_SUCCESS;
75+
}
76+
#endif /* __UBOOT__ */
77+
#endif /* WOLFTPM_LINUX_DEV || WOLFTPM_SWTPM || WOLFTPM_WINAPI */
78+
#endif /* WOLFTPM_INCLUDE_IO_FILE */
79+
80+
/******************************************************************************/
81+
/* --- END IO Callback Logic -- */
82+
/******************************************************************************/

src/tpm2.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6122,6 +6122,81 @@ const char* TPM2_GetAlgName(TPM_ALG_ID alg)
61226122
return "Unknown";
61236123
}
61246124

6125+
TPM_ALG_ID TPM2_GetAlgId(const char* name)
6126+
{
6127+
if (!name)
6128+
return TPM_ALG_ERROR;
6129+
6130+
if (!XSTRCMP(name, "RSA"))
6131+
return TPM_ALG_RSA;
6132+
if (!XSTRCMP(name, "SHA1"))
6133+
return TPM_ALG_SHA1;
6134+
if (!XSTRCMP(name, "HMAC"))
6135+
return TPM_ALG_HMAC;
6136+
if (!XSTRCMP(name, "AES"))
6137+
return TPM_ALG_AES;
6138+
if (!XSTRCMP(name, "MGF1"))
6139+
return TPM_ALG_MGF1;
6140+
if (!XSTRCMP(name, "KEYEDHASH"))
6141+
return TPM_ALG_KEYEDHASH;
6142+
if (!XSTRCMP(name, "XOR"))
6143+
return TPM_ALG_XOR;
6144+
if (!XSTRCMP(name, "SHA256"))
6145+
return TPM_ALG_SHA256;
6146+
if (!XSTRCMP(name, "SHA384"))
6147+
return TPM_ALG_SHA384;
6148+
if (!XSTRCMP(name, "SHA512"))
6149+
return TPM_ALG_SHA512;
6150+
if (!XSTRCMP(name, "NULL"))
6151+
return TPM_ALG_NULL;
6152+
if (!XSTRCMP(name, "SM3_256"))
6153+
return TPM_ALG_SM3_256;
6154+
if (!XSTRCMP(name, "SM4"))
6155+
return TPM_ALG_SM4;
6156+
if (!XSTRCMP(name, "RSASSA"))
6157+
return TPM_ALG_RSASSA;
6158+
if (!XSTRCMP(name, "RSAES"))
6159+
return TPM_ALG_RSAES;
6160+
if (!XSTRCMP(name, "RSAPSS"))
6161+
return TPM_ALG_RSAPSS;
6162+
if (!XSTRCMP(name, "OAEP"))
6163+
return TPM_ALG_OAEP;
6164+
if (!XSTRCMP(name, "ECDSA"))
6165+
return TPM_ALG_ECDSA;
6166+
if (!XSTRCMP(name, "ECDH"))
6167+
return TPM_ALG_ECDH;
6168+
if (!XSTRCMP(name, "ECDAA"))
6169+
return TPM_ALG_ECDAA;
6170+
if (!XSTRCMP(name, "SM2"))
6171+
return TPM_ALG_SM2;
6172+
if (!XSTRCMP(name, "ECSCHNORR"))
6173+
return TPM_ALG_ECSCHNORR;
6174+
if (!XSTRCMP(name, "ECMQV"))
6175+
return TPM_ALG_ECMQV;
6176+
if (!XSTRCMP(name, "KDF1_SP800_56A"))
6177+
return TPM_ALG_KDF1_SP800_56A;
6178+
if (!XSTRCMP(name, "KDF2"))
6179+
return TPM_ALG_KDF2;
6180+
if (!XSTRCMP(name, "KDF1_SP800_108"))
6181+
return TPM_ALG_KDF1_SP800_108;
6182+
if (!XSTRCMP(name, "ECC"))
6183+
return TPM_ALG_ECC;
6184+
if (!XSTRCMP(name, "SYMCIPHER"))
6185+
return TPM_ALG_SYMCIPHER;
6186+
if (!XSTRCMP(name, "AES-CTR"))
6187+
return TPM_ALG_CTR;
6188+
if (!XSTRCMP(name, "AES-OFB"))
6189+
return TPM_ALG_OFB;
6190+
if (!XSTRCMP(name, "AES-CBC"))
6191+
return TPM_ALG_CBC;
6192+
if (!XSTRCMP(name, "AES-CFB"))
6193+
return TPM_ALG_CFB;
6194+
if (!XSTRCMP(name, "AES-ECB"))
6195+
return TPM_ALG_ECB;
6196+
6197+
return TPM_ALG_ERROR;
6198+
}
6199+
61256200
int TPM2_GetCurveSize(TPM_ECC_CURVE curveID)
61266201
{
61276202
switch (curveID) {

0 commit comments

Comments
 (0)