Skip to contentSkip to conversation

tpm: Add wolfTPM library support for TPM 2.0

PATCH Superseded Aidan Garske 2 participants 18 messages May 9 – May 11

Subsystems (6)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover tpm: Add wolfTPM library support for TPM 2.0

Hi all,

This is v3 of the wolfTPM TPM 2.0 stack integration for U-Boot.

wolfTPM (https://github.com/wolfSSL/wolfTPM) is a portable, GPLv2 TPM 2.0 library that provides a full TPM 2.0 command set, an SPI/MMIO HAL, and firmware-update support for Infineon SLB9672/SLB9673 hardware. This series wires it into U-Boot as an optional backend behind the existing 'tpm2' command, alongside support for QEMU+swtpm, sandbox emulation, and real Raspberry Pi 4 + Infineon SLB9672 hardware.

Branch (full 14-commit history including the subtree squash + merge):
https://github.com/aidangarske/u-boot wolftpm-v2-patches

Note on the subtree commits omitted from this email thread:
The branch above contains 14 commits; the email series is 12 patches. Two commits are deliberately not sent to the list:

* "Squashed 'lib/wolftpm/' content from commit 664db130d57"
- the parentless squash commit produced by git subtree add. Its diff is ~3.4MB / ~90k lines and would be rejected by the mailing list on size.
* "Merge commit 'd42fd7b146...' as 'lib/wolftpm'"
- the corresponding subtree merge commit. Merges have no patch form and are routinely omitted by git format-patch.

Please pull from the branch above (or wolfssl/wolfTPM @ 664db130d57) to inspect the imported wolfTPM source. Subsequent updates will go via tools/update-subtree.sh, matching how mbedTLS, lwIP, and dts/upstream are maintained in tree.

Changes since v2:
- Replaced the lib/wolftpm git submodule with a git subtree import (squash + merge), matching the convention used for mbedTLS, lwIP, and dts/upstream. tools/update-subtree.sh is updated to know about the wolftpm subtree (path lib/wolftpm, upstream
https://github.com/wolfssl/wolfTPM.git).
[feedback: Ilias Apalodimas]
- Reverted the changes to include/linux/byteorder/generic.h. The redefinition workaround for cpu_to_beXX / beXX_to_cpu now lives on the wolfTPM side: include/configs/user_settings.h pulls in <asm/byteorder.h> up front so U-Boot's macros are defined before wolfTPM's #ifndef-guarded fallbacks in tpm2_packet.h. [feedback: Ilias Apalodimas]

Testing:
- QEMU arm64 + swtpm Python test framework (./test/py/test.py --bd qemu_arm64 -k "test_wolftpm and not ut_cmd"):
19 passed, 2 skipped (matching doc/usage/cmd/wolftpm.rst).
- Manual QEMU arm64 + swtpm walkthrough per doc/usage/cmd/wolftpm.rst section "Building and Running wolfTPM with U-Boot using QEMU": tpm2 help/info/autostart/startup/ get_capability/pcr_read/pcr_print/caps all return expected output.
- Raspberry Pi 4 + Infineon SLB9672 (real hardware): all wolfTPM cmd tests pass, including firmware update path.

v2 thread:
https://lore.kernel.org/u-boot/?q=PATCH+v2+tpm+wolfTPM

Aidan Garske (12):
tpm: export tpm_show_device, tpm_set_device, and get_tpm include/hash: add SHA384 hash wrapper declaration for wolfTPM
spi: add BCM2835/BCM2711 hardware SPI controller driver
dts: add TPM device tree nodes for RPi4, QEMU, and sandbox
tpm: add wolfTPM build rules and Kconfig
tpm: add wolfTPM headers and SHA384 glue code
tpm: add wolfTPM driver helpers and Kconfig options
cmd: refactor tpm2 command into frontend/backend architecture
tpm: add sandbox TPM SPI emulator
test: add wolfTPM C unit tests and Python integration tests
doc: add wolfTPM documentation
configs: enable wolfTPM in rpi_4_defconfig

--
2.47.3

#1 lore raw

PATCH v3 1/12 tpm: export tpm_show_device, tpm_set_device, and get_tpm

From: Aidan <[email protected]>

Remove static scope from tpm_show_device(), tpm_set_device(), and get_tpm() in cmd/tpm-common.c so they can be called from other translation units. Add corresponding declarations to include/tpm-common.h.

wolfTPM's command backend needs these functions for device enumeration and selection when operating through U-Boot's driver model.

---
 cmd/tpm-common.c     |  4 ++--
 include/tpm-common.h | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
cmd/tpm-common.c +2 -2
index 1cd57f901b6..30e99e10758 100644
@@ -234,7 +234,7 @@ int type_string_write_vars(const char *type_str, u8 *data,
 	return 0;
 }
 
-static int tpm_show_device(void)
+int tpm_show_device(void)
 {
 	struct udevice *dev;
 	char buf[80];
@@ -253,7 +253,7 @@ static int tpm_show_device(void)
 	return 0;
 }
 
-static int tpm_set_device(unsigned long num)
+int tpm_set_device(unsigned long num)
 {
 	struct udevice *dev;
 	unsigned long n = 0;
include/tpm-common.h +22 -0
index bfb84a931d1..1ea4463fbbe 100644
@@ -337,4 +337,26 @@ enum tpm_version tpm_get_version(struct udevice *dev);
 /* Iterate on all TPM devices */
 #define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
 
+/**
+ * tpm_show_device() - Show all TPM devices
+ *
+ * Return: 0 on success, -ve on failure
+ */
+int tpm_show_device(void);
+
+/**
+ * tpm_set_device() - Set the TPM device to use
+ *
+ * @num: The number of the TPM device to use
+ * Return: 0 on success, -ve on failure
+ */
+int tpm_set_device(unsigned long num);
+
+/**
+ * get_tpm() - Get the TPM device
+ *
+ * Return: 0 on success, -ve on failure
+ */
+int get_tpm(struct udevice **devp);
+
 #endif /* __TPM_COMMON_H */
Signature · 1 line
2.47.3
#2 lore raw

PATCH v3 2/12 include/hash: add SHA384 hash wrapper declaration for wolfTPM

In reply to Aidan Garske

From: Aidan <[email protected]>

Add #include <linux/types.h> to include/hash.h so that basic types (u8, u32, etc.) are available to all includers.

Add a wc_Sha384Hash() wrapper declaration in include/hash.h, gated by WOLFTPM2_NO_WOLFCRYPT. When wolfTPM is built without wolfCrypt, this wrapper provides SHA-384 hashing via U-Boot's hash subsystem, which is needed for Infineon TPM firmware update manifest validation.

---
 include/hash.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/hash.h +18 -0
index 8b3f79ec473..26043c43a9c 100644
@@ -6,6 +6,8 @@
 #ifndef _HASH_H
 #define _HASH_H
 
+#include <linux/types.h>
+
 #ifdef USE_HOSTCC
 #include <linux/kconfig.h>
 #endif
@@ -163,4 +165,20 @@ int hash_progressive_lookup_algo(const char *algo_name,
  */
 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result);
 
+#ifdef WOLFTPM2_NO_WOLFCRYPT
+/**
+ * wc_Sha384Hash() - Calculate SHA384 hash
+ * @data:	Data to hash
+ * @len:	Length of data
+ * @hash:	Output buffer for hash
+ *
+ * This is a wrapper function to provide wolfCrypt-compatible SHA384 hashing
+ * when wolfCrypt is not available.
+ *
+ * Return: 0 on success, -1 on error
+ */
+int wc_Sha384Hash(const unsigned char *data, unsigned int len,
+		  unsigned char *hash);
+#endif /* WOLFTPM2_NO_WOLFCRYPT */
+
 #endif
Signature · 1 line
2.47.3
#3 lore raw

PATCH v3 3/12 spi: add BCM2835/BCM2711 hardware SPI controller driver

In reply to Aidan Garske

From: Aidan <[email protected]>

Add a hardware SPI controller driver for the BCM2835/BCM2711 SoC found on Raspberry Pi 3 and Pi 4 boards.

The driver implements:
- Register-based SPI transfers using the BCM2835 SPI0 peripheral
- Software GPIO chip-select control (matching the Linux driver approach) rather than hardware CS, which avoids issues with automatic CS deassertion during multi-byte TPM TIS transactions
- Clock divider calculation for configurable SPI speed
- SPI mode 0/1/2/3 support via CPOL/CPHA configuration
- GPIO pin setup for SPI0 (MISO/MOSI/SCLK as ALT0, CE0/CE1 as output for software CS)
- TPM reset via GPIO4/GPIO24 during probe

This driver is needed for wolfTPM to communicate with SPI-attached TPM chips (e.g., Infineon SLB9670/9672) on Raspberry Pi hardware.

---
 drivers/spi/Kconfig       |   9 +
 drivers/spi/Makefile      |   1 +
 drivers/spi/bcm2835_spi.c | 431 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 441 insertions(+)
 create mode 100644 drivers/spi/bcm2835_spi.c
drivers/spi/Kconfig +9 -0
index 63d61ccf8ed..02ee2b2e6a0 100644
@@ -116,6 +116,15 @@ config ATMEL_SPI
 	  many AT91 (ARM) chips. This driver can be used to access
 	  the SPI Flash, such as AT25DF321.
 
+config BCM2835_SPI
+	bool "BCM2835/BCM2711 SPI driver"
+	depends on ARCH_BCM283X
+	help
+	  Enable the BCM2835/BCM2711 SPI controller driver. This driver
+	  can be used to access SPI devices on Raspberry Pi boards
+	  including Pi 3 and Pi 4. It uses the hardware SPI controller
+	  rather than GPIO bit-banging.
+
 config BCM63XX_HSSPI
 	bool "BCM63XX HSSPI driver"
 	depends on (ARCH_BMIPS || ARCH_BCMBCA)
drivers/spi/Makefile +1 -0
index 0dc2d23e172..47a1c6194b1 100644
@@ -27,6 +27,7 @@ obj-$(CONFIG_APPLE_SPI) += apple_spi.o
 obj-$(CONFIG_ATH79_SPI) += ath79_spi.o
 obj-$(CONFIG_ATMEL_QSPI) += atmel-quadspi.o
 obj-$(CONFIG_ATMEL_SPI) += atmel_spi.o
+obj-$(CONFIG_BCM2835_SPI) += bcm2835_spi.o
 obj-$(CONFIG_BCM63XX_HSSPI) += bcm63xx_hsspi.o
 obj-$(CONFIG_BCMBCA_HSSPI) += bcmbca_hsspi.o
 obj-$(CONFIG_BCM63XX_SPI) += bcm63xx_spi.o
drivers/spi/bcm2835_spi.c new file +431 -0

431 lines not on this page · raw message

Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH v3 03/12] spi: add BCM2835/BCM2711 hardware SPI controller driver

Quoted 66 lines · Aidan Garske

From: Aidan <[email protected]>

Add a hardware SPI controller driver for the BCM2835/BCM2711 SoC found on Raspberry Pi 3 and Pi 4 boards.

The driver implements:
- Register-based SPI transfers using the BCM2835 SPI0 peripheral
- Software GPIO chip-select control (matching the Linux driver approach) rather than hardware CS, which avoids issues with automatic CS deassertion during multi-byte TPM TIS transactions
- Clock divider calculation for configurable SPI speed
- SPI mode 0/1/2/3 support via CPOL/CPHA configuration
- GPIO pin setup for SPI0 (MISO/MOSI/SCLK as ALT0, CE0/CE1 as output for software CS)
- TPM reset via GPIO4/GPIO24 during probe

This driver is needed for wolfTPM to communicate with SPI-attached TPM chips (e.g., Infineon SLB9670/9672) on Raspberry Pi hardware.

---
 drivers/spi/Kconfig       |   9 +
 drivers/spi/Makefile      |   1 +
 drivers/spi/bcm2835_spi.c | 431 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 441 insertions(+)
 create mode 100644 drivers/spi/bcm2835_spi.c
drivers/spi/Kconfig +9 -0
index 63d61ccf8ed..02ee2b2e6a0 100644
@@ -116,6 +116,15 @@ config ATMEL_SPI
          many AT91 (ARM) chips. This driver can be used to access
          the SPI Flash, such as AT25DF321.
 
+config BCM2835_SPI
+       bool "BCM2835/BCM2711 SPI driver"
+       depends on ARCH_BCM283X
+       help
+         Enable the BCM2835/BCM2711 SPI controller driver. This driver
+         can be used to access SPI devices on Raspberry Pi boards
+         including Pi 3 and Pi 4. It uses the hardware SPI controller
+         rather than GPIO bit-banging.
+
 config BCM63XX_HSSPI
        bool "BCM63XX HSSPI driver"
        depends on (ARCH_BMIPS || ARCH_BCMBCA)
drivers/spi/Makefile +1 -0
index 0dc2d23e172..47a1c6194b1 100644
@@ -27,6 +27,7 @@ obj-$(CONFIG_APPLE_SPI) += apple_spi.o
 obj-$(CONFIG_ATH79_SPI) += ath79_spi.o
 obj-$(CONFIG_ATMEL_QSPI) += atmel-quadspi.o
 obj-$(CONFIG_ATMEL_SPI) += atmel_spi.o
+obj-$(CONFIG_BCM2835_SPI) += bcm2835_spi.o
 obj-$(CONFIG_BCM63XX_HSSPI) += bcm63xx_hsspi.o
 obj-$(CONFIG_BCMBCA_HSSPI) += bcmbca_hsspi.o
 obj-$(CONFIG_BCM63XX_SPI) += bcm63xx_spi.o
drivers/spi/bcm2835_spi.c new file +0 -0
new file mode 100644
index 00000000000..8133a41a0f8

drivers/spi/bcm2835_spi.c line 6

Quoted 6 lines
drivers/spi/bcm2835_spi.c new file +6 -0
@@ -0,0 +1,431 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * BCM2835/BCM2711 SPI controller driver for U-Boot
+ *
+ * Copyright (C) 2025 wolfSSL Inc.
+ * Author: Aidan Garske <[email protected]>

I'm by no means a copyright expert but does wolfSSL own the copyright if it's derived directly from the Linux driver?

Quoted 427 lines
drivers/spi/bcm2835_spi.c new file +424 -1
+ * Based on Linux driver by Chris Boot, Martin Sperl, et al.
+ */
+
+#include <dm.h>
+#include <errno.h>
+#include <log.h>
+#include <malloc.h>
+#include <spi.h>
+#include <asm/io.h>
+#include <asm/gpio.h>
+#include <dm/device_compat.h>
+#include <linux/delay.h>
+
+/* SPI register offsets */
+#define BCM2835_SPI_CS      0x00    /* Control and Status */
+#define BCM2835_SPI_FIFO    0x04    /* TX and RX FIFOs */
+#define BCM2835_SPI_CLK     0x08    /* Clock Divider */
+#define BCM2835_SPI_DLEN    0x0c    /* Data Length */
+#define BCM2835_SPI_LTOH    0x10    /* LoSSI mode TOH */
+#define BCM2835_SPI_DC      0x14    /* DMA DREQ Controls */
+
+/* CS register bits */
+#define BCM2835_SPI_CS_LEN_LONG     BIT(25)
+#define BCM2835_SPI_CS_DMA_LEN      BIT(24)
+#define BCM2835_SPI_CS_CSPOL2       BIT(23)
+#define BCM2835_SPI_CS_CSPOL1       BIT(22)
+#define BCM2835_SPI_CS_CSPOL0       BIT(21)
+#define BCM2835_SPI_CS_RXF          BIT(20)
+#define BCM2835_SPI_CS_RXR          BIT(19)
+#define BCM2835_SPI_CS_TXD          BIT(18)
+#define BCM2835_SPI_CS_RXD          BIT(17)
+#define BCM2835_SPI_CS_DONE         BIT(16)
+#define BCM2835_SPI_CS_LEN          BIT(13)
+#define BCM2835_SPI_CS_REN          BIT(12)
+#define BCM2835_SPI_CS_ADCS         BIT(11)
+#define BCM2835_SPI_CS_INTR         BIT(10)
+#define BCM2835_SPI_CS_INTD         BIT(9)
+#define BCM2835_SPI_CS_DMAEN        BIT(8)
+#define BCM2835_SPI_CS_TA           BIT(7)
+#define BCM2835_SPI_CS_CSPOL        BIT(6)
+#define BCM2835_SPI_CS_CLEAR_RX     BIT(5)
+#define BCM2835_SPI_CS_CLEAR_TX     BIT(4)
+#define BCM2835_SPI_CS_CPOL         BIT(3)
+#define BCM2835_SPI_CS_CPHA         BIT(2)
+#define BCM2835_SPI_CS_CS_10        BIT(1)
+#define BCM2835_SPI_CS_CS_01        BIT(0)
+
+/* Default clock rate - 250 MHz for Pi 4 */
+#define BCM2835_SPI_DEFAULT_CLK     250000000
+
+struct bcm2835_spi_priv {
+       void __iomem *regs;
+       u32 clk_hz;
+       u32 cs_reg;         /* Cached CS register value */
+       u32 speed_hz;
+       u8 mode;
+       struct gpio_desc cs_gpio;
+       int cs_gpio_valid;
+       int cs_asserted;    /* Track if CS should stay asserted between transfers */
+};
+
+struct bcm2835_spi_plat {
+       fdt_addr_t base;
+       u32 clk_hz;
+};
+
+static inline u32 bcm2835_spi_readl(struct bcm2835_spi_priv *priv, u32 reg)
+{
+       return readl(priv->regs + reg);
+}
+
+static inline void bcm2835_spi_writel(struct bcm2835_spi_priv *priv,
+                                                                          u32 reg, u32 val)
+{
+       writel(val, priv->regs + reg);
+}
+
+static void bcm2835_spi_reset(struct bcm2835_spi_priv *priv)
+{
+       /* Clear FIFOs and disable SPI */
+       bcm2835_spi_writel(priv, BCM2835_SPI_CS,
+                                          BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
+}
+
+/* GPIO base for software CS control */
+static void __iomem *g_gpio_base = (void __iomem *)0xFE200000;
+
+/* Software CS control - assert (LOW = active) */
+static void bcm2835_spi_cs_assert(int cs_pin)
+{
+       /* GPCLR0 - clear pin (drive LOW) */
+       writel(1 << cs_pin, g_gpio_base + 0x28);
+}
+
+/* Software CS control - deassert (HIGH = inactive) */
+static void bcm2835_spi_cs_deassert(int cs_pin)
+{
+       /* GPSET0 - set pin (drive HIGH) */
+       writel(1 << cs_pin, g_gpio_base + 0x1C);
+}
+
+static int bcm2835_spi_xfer(struct udevice *dev, unsigned int bitlen,
+                                                       const void *dout, void *din, unsigned long flags)
+{
+       struct udevice *bus = dev_get_parent(dev);
+       struct bcm2835_spi_priv *priv = dev_get_priv(bus);
+       const u8 *tx = dout;
+       u8 *rx = din;
+       u32 len = bitlen / 8;
+       u32 cs_reg;
+       u32 tx_count = 0, rx_count = 0;
+       int timeout;
+       int cs = spi_chip_select(dev);  /* Get chip select from slave device */
+       int cs_pin = (cs == 0) ? 8 : 7; /* CS0=GPIO8, CS1=GPIO7 */
+       u32 stat;
+
+       if (bitlen == 0) {
+               /* Handle CS-only operations (deassert) */
+               if (flags & SPI_XFER_END) {
+                       bcm2835_spi_cs_deassert(cs_pin);
+                       priv->cs_asserted = 0;
+               }
+               return 0;
+       }
+
+       if (bitlen % 8) {
+               dev_err(dev, "Non-byte-aligned transfer not supported\n");
+               return -EINVAL;
+       }
+
+       /*
+        * SOFTWARE GPIO CHIP SELECT - like Linux driver
+        * Don't use hardware CS bits - set to 0 (unused)
+        */
+       cs_reg = priv->cs_reg & ~(BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01);
+
+       /* Assert CS at start of transaction (SPI_XFER_BEGIN) */
+       if (flags & SPI_XFER_BEGIN) {
+               bcm2835_spi_cs_assert(cs_pin);
+               priv->cs_asserted = 1;
+               udelay(1);  /* CS setup time */
+       }
+
+       /* Clear FIFOs for new transaction */
+       if (flags & SPI_XFER_BEGIN) {
+               bcm2835_spi_writel(priv, BCM2835_SPI_CS,
+                                                  cs_reg | BCM2835_SPI_CS_CLEAR_RX |
+                                                  BCM2835_SPI_CS_CLEAR_TX);
+               udelay(1);
+       }
+
+       /* Start transfer with TA=1 (but CS is controlled by GPIO, not hardware) */
+       bcm2835_spi_writel(priv, BCM2835_SPI_CS, cs_reg | BCM2835_SPI_CS_TA);
+
+       /* Poll for completion - transfer byte by byte */
+       timeout = 100000;
+       while ((tx_count < len || rx_count < len) && timeout > 0) {
+               stat = bcm2835_spi_readl(priv, BCM2835_SPI_CS);
+
+               /* TX FIFO not full - send next byte */
+               while ((stat & BCM2835_SPI_CS_TXD) && tx_count < len) {
+                       u8 byte = tx ? tx[tx_count] : 0;
+
+                       bcm2835_spi_writel(priv, BCM2835_SPI_FIFO, byte);
+                       tx_count++;
+                       stat = bcm2835_spi_readl(priv, BCM2835_SPI_CS);
+               }
+
+               /* RX FIFO has data - read it */
+               while ((stat & BCM2835_SPI_CS_RXD) && rx_count < len) {
+                       u8 byte = bcm2835_spi_readl(priv, BCM2835_SPI_FIFO) & 0xff;
+
+                       if (rx)
+                               rx[rx_count] = byte;
+                       rx_count++;
+                       stat = bcm2835_spi_readl(priv, BCM2835_SPI_CS);
+               }
+
+               timeout--;
+       }
+
+       /* Wait for DONE */
+       timeout = 10000;
+       while (!(bcm2835_spi_readl(priv, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE) &&
+                  timeout > 0) {
+               udelay(1);
+               timeout--;
+       }
+
+       /* Read any remaining RX data from FIFO */
+       while (bcm2835_spi_readl(priv, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD) {
+               u8 byte = bcm2835_spi_readl(priv, BCM2835_SPI_FIFO) & 0xff;
+
+               if (rx && rx_count < len)
+                       rx[rx_count++] = byte;
+       }
+
+       /* Clear TA to complete this transfer (doesn't affect GPIO CS) */
+       bcm2835_spi_writel(priv, BCM2835_SPI_CS, cs_reg);
+
+       /*
+        * SOFTWARE GPIO CHIP SELECT control:
+        * - SPI_XFER_END: deassert CS (GPIO HIGH)
+        * - No END flag: keep CS asserted for next transfer
+        */
+       if (flags & SPI_XFER_END) {
+               bcm2835_spi_cs_deassert(cs_pin);
+               priv->cs_asserted = 0;
+       } else {
+               /* Keep CS asserted for next transfer (e.g., wait state polling) */
+               priv->cs_asserted = 1;
+       }
+
+       if (timeout == 0) {
+               bcm2835_spi_cs_deassert(cs_pin);  /* Make sure CS is released */
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+
+static int bcm2835_spi_set_speed(struct udevice *bus, uint speed)
+{
+       struct bcm2835_spi_priv *priv = dev_get_priv(bus);
+       u32 cdiv;
+
+       if (speed == 0)
+               speed = 1000000;  /* Default 1 MHz */
+
+       priv->speed_hz = speed;
+
+       /* Calculate clock divider */
+       if (speed >= priv->clk_hz / 2) {
+               cdiv = 2;  /* Fastest possible */
+       } else {
+               cdiv = (priv->clk_hz + speed - 1) / speed;
+               cdiv += (cdiv & 1);  /* Must be even */
+               if (cdiv >= 65536)
+                       cdiv = 0;  /* Slowest: clk/65536 */
+       }
+
+       bcm2835_spi_writel(priv, BCM2835_SPI_CLK, cdiv);
+
+       return 0;
+}
+
+static int bcm2835_spi_set_mode(struct udevice *bus, uint mode)
+{
+       struct bcm2835_spi_priv *priv = dev_get_priv(bus);
+       u32 cs_reg = 0;
+
+       priv->mode = mode;
+
+       /* Set clock polarity and phase */
+       if (mode & SPI_CPOL)
+               cs_reg |= BCM2835_SPI_CS_CPOL;
+       if (mode & SPI_CPHA)
+               cs_reg |= BCM2835_SPI_CS_CPHA;
+
+       /* CS bits will be set in xfer based on slave's chip select */
+       priv->cs_reg = cs_reg;
+
+       return 0;
+}
+
+static int bcm2835_spi_claim_bus(struct udevice *dev)
+{
+       return 0;
+}
+
+static int bcm2835_spi_release_bus(struct udevice *dev)
+{
+       return 0;
+}
+
+/* Setup GPIO pins for SPI0 with SOFTWARE chip select */
+static void bcm2835_spi_setup_gpio(void)
+{
+       u32 val;
+
+       /*
+        * SPI0 pin configuration:
+        * GPIO7  (CE1)  - OUTPUT (software CS) - GPFSEL0 bits 23:21 = 001
+        * GPIO8  (CE0)  - OUTPUT (software CS) - GPFSEL0 bits 26:24 = 001
+        * GPIO9  (MISO) - ALT0 (SPI)           - GPFSEL0 bits 29:27 = 100
+        * GPIO10 (MOSI) - ALT0 (SPI)           - GPFSEL1 bits 2:0   = 100
+        * GPIO11 (SCLK) - ALT0 (SPI)           - GPFSEL1 bits 5:3   = 100
+        */
+
+       /* Set GPIO7, GPIO8 to OUTPUT, GPIO9 to ALT0 in GPFSEL0 */
+       val = readl(g_gpio_base + 0x00);
+       val &= ~((7 << 21) | (7 << 24) | (7 << 27));  /* Clear GPIO7,8,9 */
+       val |= (1 << 21);   /* GPIO7 = OUTPUT (001) */
+       val |= (1 << 24);   /* GPIO8 = OUTPUT (001) */
+       val |= (4 << 27);   /* GPIO9 = ALT0 (100) for MISO */
+       writel(val, g_gpio_base + 0x00);
+
+       /* Set GPIO10, GPIO11 to ALT0 in GPFSEL1 */
+       val = readl(g_gpio_base + 0x04);
+       val &= ~((7 << 0) | (7 << 3));  /* Clear GPIO10,11 */
+       val |= (4 << 0);    /* GPIO10 = ALT0 (100) for MOSI */
+       val |= (4 << 3);    /* GPIO11 = ALT0 (100) for SCLK */
+       writel(val, g_gpio_base + 0x04);
+
+       /* Deassert both CS lines (HIGH = inactive) */
+       bcm2835_spi_cs_deassert(7);  /* CE1 */
+       bcm2835_spi_cs_deassert(8);  /* CE0 */
+}
+
+/* TPM Reset via GPIO4 and GPIO24 */
+static void bcm2835_spi_tpm_reset(void)
+{
+       void __iomem *gpio_base = (void __iomem *)0xFE200000;
+       u32 val;
+
+       /* Set GPIO4 as output (GPFSEL0, bits 14:12) */
+       val = readl(gpio_base + 0x00);  /* GPFSEL0 */
+       val &= ~(7 << 12);  /* Clear bits 14:12 for GPIO4 */
+       val |= (1 << 12);   /* Set to output */
+       writel(val, gpio_base + 0x00);
+
+       /* Set GPIO24 as output (GPFSEL2, bits 14:12) */
+       val = readl(gpio_base + 0x08);  /* GPFSEL2 */
+       val &= ~(7 << 12);  /* Clear bits 14:12 for GPIO24 */
+       val |= (1 << 12);   /* Set to output */
+       writel(val, gpio_base + 0x08);
+
+       /* Assert reset on BOTH pins (LOW) */
+       writel((1 << 4) | (1 << 24), gpio_base + 0x28);  /* GPCLR0 */
+       mdelay(100);
+
+       /* Release reset on BOTH pins (HIGH) */
+       writel((1 << 4) | (1 << 24), gpio_base + 0x1C);  /* GPSET0 */
+       mdelay(150);  /* Wait for TPM to initialize */
+}
+
+static int bcm2835_spi_probe(struct udevice *bus)
+{
+       struct bcm2835_spi_plat *plat = dev_get_plat(bus);
+       struct bcm2835_spi_priv *priv = dev_get_priv(bus);
+       int ret;
+
+       priv->regs = (void __iomem *)plat->base;
+       priv->clk_hz = plat->clk_hz ? plat->clk_hz : BCM2835_SPI_DEFAULT_CLK;
+
+       /* Setup GPIO pins for SPI0 (ALT0 function) */
+       bcm2835_spi_setup_gpio();
+
+       /* Reset TPM before using SPI */
+       bcm2835_spi_tpm_reset();
+
+       /* Try to get CS GPIO from device tree */
+       ret = gpio_request_by_name(bus, "cs-gpios", 0, &priv->cs_gpio,
+                                  GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
+       if (!ret) {
+               priv->cs_gpio_valid = 1;
+               /* Deassert CS initially */
+               dm_gpio_set_value(&priv->cs_gpio, 1);
+       } else {
+               priv->cs_gpio_valid = 0;
+       }
+
+       /* Reset the SPI controller */
+       bcm2835_spi_reset(priv);
+
+       /* Set default speed and mode */
+       bcm2835_spi_set_speed(bus, 1000000);  /* 1 MHz default */
+       bcm2835_spi_set_mode(bus, SPI_MODE_0);
+
+       return 0;
+}
+
+static int bcm2835_spi_of_to_plat(struct udevice *bus)
+{
+       struct bcm2835_spi_plat *plat = dev_get_plat(bus);
+       fdt_addr_t addr;
+
+       addr = dev_read_addr(bus);
+       if (addr == FDT_ADDR_T_NONE) {
+               dev_err(bus, "Failed to get SPI base address\n");
+               return -EINVAL;
+       }
+
+       /*
+        * On BCM2711 (Pi 4), the device tree often uses VideoCore bus addresses
+        * which start with 0x7E. The ARM needs to access these via the ARM
+        * peripheral base at 0xFE000000.
+        */
+       if ((addr & 0xFF000000) == 0x7E000000)
+               addr = (addr & 0x00FFFFFF) | 0xFE000000;
+
+       plat->base = addr;
+
+       /* Try to get clock rate from device tree */
+       plat->clk_hz = dev_read_u32_default(bus, "clock-frequency",
+                                            BCM2835_SPI_DEFAULT_CLK);
+
+       return 0;
+}
+
+static const struct dm_spi_ops bcm2835_spi_ops = {
+       .claim_bus      = bcm2835_spi_claim_bus,
+       .release_bus    = bcm2835_spi_release_bus,
+       .xfer           = bcm2835_spi_xfer,
+       .set_speed      = bcm2835_spi_set_speed,
+       .set_mode       = bcm2835_spi_set_mode,
+};
+
+static const struct udevice_id bcm2835_spi_ids[] = {
+       { .compatible = "brcm,bcm2835-spi" },
+       { .compatible = "brcm,bcm2711-spi" },
+       { }
+};
+
+U_BOOT_DRIVER(bcm2835_spi) = {
+       .name           = "bcm2835_spi",
+       .id             = UCLASS_SPI,
+       .of_match       = bcm2835_spi_ids,
+       .ops            = &bcm2835_spi_ops,
+       .of_to_plat     = bcm2835_spi_of_to_plat,
+       .plat_auto      = sizeof(struct bcm2835_spi_plat),
+       .priv_auto      = sizeof(struct bcm2835_spi_priv),
+       .probe          = bcm2835_spi_probe,
+};
--

2.47.3

#5 lore raw

PATCH v3 4/12 dts: add TPM device tree nodes for RPi4, QEMU, and sandbox

In reply to Aidan Garske

From: Aidan <[email protected]>

Add device tree entries for TPM devices across three platforms:

bcm2711-rpi-4-b.dts:
Add SPI0 node with Infineon SLB9670/9672 TPM on CE1 (GPIO7), matching the standard Linux tpm-slb9670 overlay. Add spi0 alias so wolfTPM finds the bus at index 0.

bcm2711-rpi-4-b-u-boot.dtsi (new file):
Add soft-SPI (GPIO bit-bang) fallback node for TPM communication. This uses spi-gpio with GPIO11/10/9/7 and provides an alternative to the hardware SPI driver when pinctrl naming differs between U-Boot and Linux device trees.

qemu-arm64.dts:
Add MMIO-based TPM TIS node at 0x0c000000 for QEMU virt machine with swtpm. This allows wolfTPM testing via QEMU without hardware.

sandbox.dtsi:
Add sandbox TPM SPI device (sandbox,tpm-spi) on SPI bus CS1 with a phandle reference to a sandbox TPM SPI emulator node. This enables wolfTPM SPI HAL testing in the sandbox environment.

---
 arch/arm/dts/bcm2711-rpi-4-b-u-boot.dtsi | 54 ++++++++++++++++++++++++
 arch/arm/dts/bcm2711-rpi-4-b.dts         | 20 +++++++++
 arch/arm/dts/qemu-arm64.dts              |  4 ++
 arch/sandbox/dts/sandbox.dtsi            | 11 +++++
 4 files changed, 89 insertions(+)
 create mode 100644 arch/arm/dts/bcm2711-rpi-4-b-u-boot.dtsi
arch/arm/dts/bcm2711-rpi-4-b-u-boot.dtsi new file +54 -0
new file mode 100644
index 00000000000..c38276abee9
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * U-Boot specific additions for Raspberry Pi 4 Model B
+ *
+ * Adds soft SPI support for Infineon SLB9670/9672 TPM HAT
+ * connected to SPI0 CE1 (GPIO 7)
+ *
+ * Uses GPIO bit-banging instead of hardware SPI to avoid
+ * GPIO chip naming issues (pinctrl-bcm2835 vs pinctrl-bcm2711)
+ */
+
+#include "bcm283x-u-boot.dtsi"
+#include <dt-bindings/gpio/gpio.h>
+
+/ {
+	aliases {
+		spi0 = &soft_spi;
+	};
+
+	soft_spi: soft-spi {
+		compatible = "spi-gpio";
+		#address-cells = <1>;
+		#size-cells = <0>;
+		status = "okay";
+		bootph-all;
+
+		/*
+		 * GPIO pins for SPI0 on Raspberry Pi 4:
+		 * GPIO 11 = SCLK
+		 * GPIO 10 = MOSI
+		 * GPIO 9  = MISO
+		 * GPIO 7  = CE1 (chip select for TPM)
+		 */
+		gpio-sck = <&gpio 11 GPIO_ACTIVE_HIGH>;
+		gpio-mosi = <&gpio 10 GPIO_ACTIVE_HIGH>;
+		gpio-miso = <&gpio 9 GPIO_ACTIVE_HIGH>;
+		cs-gpios = <&gpio 7 GPIO_ACTIVE_LOW>;
+		num-chipselects = <1>;
+		spi-delay-us = <1>;
+
+		/* Infineon SLB9672 TPM on CS1 */
+		tpm@0 {
+			compatible = "infineon,slb9672", "tcg,tpm-tis-spi";
+			reg = <0>;
+			spi-max-frequency = <10000000>;
+			status = "okay";
+			bootph-all;
+		};
+	};
+};
+
+&gpio {
+	bootph-all;
+};
arch/arm/dts/bcm2711-rpi-4-b.dts +20 -0
index 72ce80fbf26..a09276dc279 100644
@@ -8,6 +8,11 @@
 	compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
 	model = "Raspberry Pi 4 Model B";
 
+	/* Alias hardware SPI as spi0 so wolfTPM finds it at bus 0 */
+	aliases {
+		spi0 = &spi;
+	};
+
 	chosen {
 		/* 8250 auxiliary UART instead of pl011 */
 		stdout-path = "serial1:115200n8";
@@ -54,6 +59,21 @@
 		enable-active-high;
 		gpio = <&expgpio 6 GPIO_ACTIVE_HIGH>;
 	};
+
+};
+
+/* Hardware SPI with TPM - matches Linux tpm-slb9670 overlay */
+&spi {
+	status = "okay";
+	pinctrl-names = "default";
+	pinctrl-0 = <&spi0_gpio7>;
+
+	/* Infineon SLB9670/9672 TPM 2.0 on CE1 (GPIO7) */
+	tpm@1 {
+		compatible = "infineon,slb9670", "tcg,tpm_tis-spi";
+		reg = <1>;  /* CE1 */
+		spi-max-frequency = <32000000>;
+	};
 };
 
 &ddc0 {
arch/arm/dts/qemu-arm64.dts +4 -0
index 95fcf53ed74..e74d036a532 100644
@@ -12,4 +12,8 @@
 #endif
 
 / {
+	tpm@0c000000 {
+		compatible = "tcg,tpm-tis-mmio";
+		reg = <0x0 0x0c000000 0x0 0x5000>;
+	};
 };
arch/sandbox/dts/sandbox.dtsi +11 -0
index 02b03894eaf..2fdd7f0e942 100644
@@ -286,6 +286,17 @@
 			spi-max-frequency = <40000000>;
 			sandbox,filename = "spi.bin";
 		};
+
+		tpm_spi: tpm@1 {
+			reg = <1>;
+			compatible = "sandbox,tpm-spi";
+			spi-max-frequency = <10000000>;
+			sandbox,emul = <&tpm_spi_emul>;
+		};
+	};
+
+	tpm_spi_emul: tpm-spi-emul {
+		compatible = "sandbox,tpm-spi-emul";
 	};
 
 	spl-test {
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH v3 04/12] dts: add TPM device tree nodes for RPi4, QEMU, and sandbox

NAK,

The below should be 4 changes for the 4 different devices.

Also it pollutes the Linux DT if Linux boots with FW DT, such as used for a SystemReady compliant device, why wouldn't these go upstream?

On the RPi4 side of things you should probably use the prior FW with the overlay provided by the config.txt to get the real TPM. Not sure why the DT would need the soft SPI given you disable the driver in the config?

Quoted 170 lines · Aidan Garske

From: Aidan <[email protected]>

Add device tree entries for TPM devices across three platforms:

bcm2711-rpi-4-b.dts:
Add SPI0 node with Infineon SLB9670/9672 TPM on CE1 (GPIO7), matching the standard Linux tpm-slb9670 overlay. Add spi0 alias so wolfTPM finds the bus at index 0.

bcm2711-rpi-4-b-u-boot.dtsi (new file):
Add soft-SPI (GPIO bit-bang) fallback node for TPM communication. This uses spi-gpio with GPIO11/10/9/7 and provides an alternative to the hardware SPI driver when pinctrl naming differs between U-Boot and Linux device trees.

qemu-arm64.dts:
Add MMIO-based TPM TIS node at 0x0c000000 for QEMU virt machine with swtpm. This allows wolfTPM testing via QEMU without hardware.

sandbox.dtsi:
Add sandbox TPM SPI device (sandbox,tpm-spi) on SPI bus CS1 with a phandle reference to a sandbox TPM SPI emulator node. This enables wolfTPM SPI HAL testing in the sandbox environment.

---
 arch/arm/dts/bcm2711-rpi-4-b-u-boot.dtsi | 54 ++++++++++++++++++++++++
 arch/arm/dts/bcm2711-rpi-4-b.dts         | 20 +++++++++
 arch/arm/dts/qemu-arm64.dts              |  4 ++
 arch/sandbox/dts/sandbox.dtsi            | 11 +++++
 4 files changed, 89 insertions(+)
 create mode 100644 arch/arm/dts/bcm2711-rpi-4-b-u-boot.dtsi
arch/arm/dts/bcm2711-rpi-4-b-u-boot.dtsi new file +54 -0
new file mode 100644
index 00000000000..c38276abee9
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * U-Boot specific additions for Raspberry Pi 4 Model B
+ *
+ * Adds soft SPI support for Infineon SLB9670/9672 TPM HAT
+ * connected to SPI0 CE1 (GPIO 7)
+ *
+ * Uses GPIO bit-banging instead of hardware SPI to avoid
+ * GPIO chip naming issues (pinctrl-bcm2835 vs pinctrl-bcm2711)
+ */
+
+#include "bcm283x-u-boot.dtsi"
+#include <dt-bindings/gpio/gpio.h>
+
+/ {
+       aliases {
+               spi0 = &soft_spi;
+       };
+
+       soft_spi: soft-spi {
+               compatible = "spi-gpio";
+               #address-cells = <1>;
+               #size-cells = <0>;
+               status = "okay";
+               bootph-all;
+
+               /*
+                * GPIO pins for SPI0 on Raspberry Pi 4:
+                * GPIO 11 = SCLK
+                * GPIO 10 = MOSI
+                * GPIO 9  = MISO
+                * GPIO 7  = CE1 (chip select for TPM)
+                */
+               gpio-sck = <&gpio 11 GPIO_ACTIVE_HIGH>;
+               gpio-mosi = <&gpio 10 GPIO_ACTIVE_HIGH>;
+               gpio-miso = <&gpio 9 GPIO_ACTIVE_HIGH>;
+               cs-gpios = <&gpio 7 GPIO_ACTIVE_LOW>;
+               num-chipselects = <1>;
+               spi-delay-us = <1>;
+
+               /* Infineon SLB9672 TPM on CS1 */
+               tpm@0 {
+                       compatible = "infineon,slb9672", "tcg,tpm-tis-spi";
+                       reg = <0>;
+                       spi-max-frequency = <10000000>;
+                       status = "okay";
+                       bootph-all;
+               };
+       };
+};
+
+&gpio {
+       bootph-all;
+};
arch/arm/dts/bcm2711-rpi-4-b.dts +20 -0
index 72ce80fbf26..a09276dc279 100644
@@ -8,6 +8,11 @@
        compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
        model = "Raspberry Pi 4 Model B";
 
+       /* Alias hardware SPI as spi0 so wolfTPM finds it at bus 0 */
+       aliases {
+               spi0 = &spi;
+       };
+
        chosen {
                /* 8250 auxiliary UART instead of pl011 */
                stdout-path = "serial1:115200n8";
@@ -54,6 +59,21 @@
                enable-active-high;
                gpio = <&expgpio 6 GPIO_ACTIVE_HIGH>;
        };
+
+};
+
+/* Hardware SPI with TPM - matches Linux tpm-slb9670 overlay */
+&spi {
+       status = "okay";
+       pinctrl-names = "default";
+       pinctrl-0 = <&spi0_gpio7>;
+
+       /* Infineon SLB9670/9672 TPM 2.0 on CE1 (GPIO7) */
+       tpm@1 {
+               compatible = "infineon,slb9670", "tcg,tpm_tis-spi";
+               reg = <1>;  /* CE1 */
+               spi-max-frequency = <32000000>;
+       };
 };
 
 &ddc0 {
arch/arm/dts/qemu-arm64.dts +4 -0
index 95fcf53ed74..e74d036a532 100644
@@ -12,4 +12,8 @@
 #endif
 
 / {
+       tpm@0c000000 {
+               compatible = "tcg,tpm-tis-mmio";
+               reg = <0x0 0x0c000000 0x0 0x5000>;
+       };
 };
arch/sandbox/dts/sandbox.dtsi +11 -1
index 02b03894eaf..2fdd7f0e942 100644
@@ -286,6 +286,17 @@
                        spi-max-frequency = <40000000>;
                        sandbox,filename = "spi.bin";
                };
+
+               tpm_spi: tpm@1 {
+                       reg = <1>;
+                       compatible = "sandbox,tpm-spi";
+                       spi-max-frequency = <10000000>;
+                       sandbox,emul = <&tpm_spi_emul>;
+               };
+       };
+
+       tpm_spi_emul: tpm-spi-emul {
+               compatible = "sandbox,tpm-spi-emul";
        };
 
        spl-test {
--

2.47.3

#7 lore raw

PATCH v3 5/12 tpm: add wolfTPM build rules and Kconfig

In reply to Aidan Garske

From: Aidan <[email protected]>

Hook the wolfTPM source tree (imported as a subtree at lib/wolftpm/ in the preceding commits) into the U-Boot build and add upstream-pull support to tools/update-subtree.sh, matching how mbedtls, dts, and lwip are maintained.

lib/Kconfig:
Adds CONFIG_TPM_WOLF under library routines, depending on DM, implying DM_RNG, and selecting SHA1.

lib/Makefile:
When CONFIG_TPM_WOLF and CONFIG_TPM_V2 are both enabled, compiles wolfTPM core source files (tpm2.c, tpm2_packet.c, tpm2_tis.c, tpm2_wrap.c, tpm2_param_enc.c) and the HAL layer (tpm_io.c). Sets -I include paths and -DWOLFTPM_USER_SETTINGS so wolfTPM picks up include/configs/user_settings.h.

tools/update-subtree.sh:
Registers the wolftpm subtree (path lib/wolftpm, upstream
https://github.com/wolfssl/wolfTPM.git) so the existing pull/pick workflow can be used for future wolfTPM updates.

---
 lib/Kconfig             | 13 +++++++++++++
 lib/Makefile            | 17 +++++++++++++++++
 tools/update-subtree.sh |  7 ++++++-
 3 files changed, 36 insertions(+), 1 deletion(-)
lib/Kconfig +13 -0
index 931d5206936..b7dc422e94c 100644
@@ -500,6 +500,19 @@ config TPM
 	  If you want a fully functional TPM enable all hashing algorithms.
 	  If you enabled measured boot all hashing algorithms are selected.
 
+config TPM_WOLF
+	bool "Enable wolfTPM support"
+	depends on DM
+	imply DM_RNG
+	select SHA1
+	help
+	  This option enables support for wolfTPM in U-Boot. wolfTPM is a
+	  portable, open-source TPM 2.0 stack licensed under GPLv2. Enabling
+	  this option allows U-Boot to interact with the TPM via wolfTPM,
+	  including firmware updates, PCR extend, and other TPM 2.0
+	  operations. The wolfTPM source tree lives under lib/wolftpm/ as
+	  a subtree (see tools/update-subtree.sh).
+
 config SPL_TPM
 	bool "Trusted Platform Module (TPM) Support in SPL"
 	depends on SPL_DM
lib/Makefile +17 -0
index 70667f3728c..0753e33d69e 100644
@@ -64,6 +64,23 @@ obj-$(CONFIG_EFI_TCG2_PROTOCOL) += tpm_tcg2.o
 obj-$(CONFIG_MEASURED_BOOT) += tpm_tcg2.o
 endif
 
+# wolfTPM (TPM 2.0 stack, including firmware update support)
+ifeq ($(CONFIG_TPM_WOLF),y)
+ifeq ($(CONFIG_TPM_V2),y)
+ccflags-y += -I$(srctree)/lib/wolftpm \
+	     -I$(srctree)/include/configs \
+	     -DWOLFTPM_USER_SETTINGS
+obj-y += wolftpm/hal/tpm_io.o
+obj-$(CONFIG_WOLFTPM_LINUX_DEV) += wolftpm/src/tpm2_linux.o
+obj-y += wolftpm/src/tpm2.o
+obj-y += wolftpm/src/tpm2_packet.o
+obj-y += wolftpm/src/tpm2_tis.o
+obj-y += wolftpm/src/tpm2_wrap.o
+obj-y += wolftpm/src/tpm2_param_enc.o
+obj-y += wolftpm.o
+endif
+endif
+
 obj-$(CONFIG_$(PHASE_)CRC8) += crc8.o
 obj-$(CONFIG_$(PHASE_)CRC16) += crc16.o
 obj-$(CONFIG_$(PHASE_)CRC16) += crc16-ccitt.o
tools/update-subtree.sh +6 -1
index 536b3318573..c5963e6a3ae 100755
@@ -17,7 +17,7 @@ set -e
 print_usage() {
     echo "usage: $0 <op> <subtree-name> <ref>"
     echo "  <op>           pull or pick"
-    echo "  <subtree-name> mbedtls or dts or lwip"
+    echo "  <subtree-name> mbedtls or dts or lwip or wolftpm"
     echo "  <ref>          release tag [pull] or commit id [pick]"
 }
 
@@ -47,6 +47,11 @@ set_params() {
             repo_url=https://git.savannah.gnu.org/git/lwip.git
             remote_name="lwip_upstream"
             ;;
+        wolftpm)
+            path=lib/wolftpm
+            repo_url=https://github.com/wolfssl/wolfTPM.git
+            remote_name="wolftpm_upstream"
+            ;;
         *)
             echo "Invalid subtree name: $subtree_name"
             print_usage
Signature · 1 line
2.47.3
#8 lore raw

PATCH v3 6/12 tpm: add wolfTPM headers and SHA384 glue code

In reply to Aidan Garske

From: Aidan <[email protected]>

Add the wolfTPM integration headers and hash wrapper needed to bridge wolfTPM with U-Boot's subsystems.

include/wolftpm.h:
Public header exposing TPM2_PCRs_Print(), TPM2_Init_Device(), and Infineon firmware update helpers (TPM2_IFX_FwData_Cb, TPM2_IFX_GetOpModeStr, TPM2_IFX_PrintInfo). Includes the core wolfTPM headers (tpm2.h, tpm2_wrap.h, tpm2_packet.h).

include/configs/user_settings.h:
wolfTPM compile-time configuration. Selects TPM chip type (SLB9672/SLB9673 for real hardware, WOLFTPM_AUTODETECT for swtpm/QEMU), communication mode (native SPI TIS layer for real hardware, WOLFTPM_LINUX_DEV for U-Boot driver model), timeout tuning, and feature flags (WOLFTPM2_NO_WOLFCRYPT, WOLFTPM2_NO_HEAP, WOLFTPM_CHECK_WAIT_STATE).

user_settings.h pulls in <asm/byteorder.h> up front so U-Boot's cpu_to_beXX / beXX_to_cpu macros are defined before wolfTPM's tpm2_packet.h, whose fallback definitions are #ifndef-guarded. This keeps the workaround on the wolfTPM side rather than modifying linux/byteorder/generic.h.

lib/wolftpm.c:
Provides wc_Sha384Hash() implementation when wolfCrypt is disabled (WOLFTPM2_NO_WOLFCRYPT). Uses U-Boot's hash_lookup_algo("sha384") to compute SHA-384 digests, which is required for Infineon TPM firmware update manifest validation.

---
 include/configs/user_settings.h | 123 ++++++++++++++++++++++++++++++++
 include/wolftpm.h               |  34 +++++++++
 lib/wolftpm.c                   |  56 +++++++++++++++
 3 files changed, 213 insertions(+)
 create mode 100644 include/configs/user_settings.h
 create mode 100644 include/wolftpm.h
 create mode 100644 lib/wolftpm.c
include/configs/user_settings.h new file +123 -0
new file mode 100644
index 00000000000..6afd6ddc520
@@ -0,0 +1,123 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * wolfTPM build configuration for U-Boot
+ *
+ * Copyright (C) 2025 wolfSSL Inc.
+ * Author: Aidan Garske <[email protected]>
+ */
+
+#ifndef USER_SETTINGS_H
+#define USER_SETTINGS_H
+
+/* Define U-Boot's byte-order macros first so wolfTPM's #ifndef-guarded
+ * fallbacks in tpm2_packet.h don't redefine them.
+ */
+#include <asm/byteorder.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/******************************************************************************/
+/* --- BEGIN wolfTPM U-boot Settings -- */
+/******************************************************************************/
+
+/* =========================================================================
+ * TPM Chip Configuration
+ * =========================================================================
+ *
+ * CONFIG_TPM_AUTODETECT: For swtpm/QEMU testing (no specific chip)
+ * !CONFIG_TPM_AUTODETECT: For real hardware (SLB9672/SLB9673)
+ */
+#ifdef CONFIG_TPM_AUTODETECT
+	#define WOLFTPM_AUTODETECT
+#else
+	/* Real hardware - Infineon SLB9672/SLB9673
+	 * Firmware upgrade only supported by these chips */
+	#define WOLFTPM_FIRMWARE_UPGRADE
+	#define WOLFTPM_SLB9672
+	/* #define WOLFTPM_SLB9673 */
+#endif
+
+/* Include delay.h and types.h for
+ * U-boot time delay and types */
+#include <linux/delay.h>
+#include <linux/types.h>
+#include <stdint.h>
+
+/* wolfCrypt disabled - pcr_setauthpolicy/pcr_setauthvalue not available
+ * To enable wolfCrypt, you would need to:
+ * 1. Uncomment the line below to undefine WOLFTPM2_NO_WOLFCRYPT
+ * 2. Add wolfCrypt source files to the U-Boot build (lib/Makefile)
+ * 3. Add wolfCrypt settings for embedded/no-OS use
+ */
+#undef  WOLFTPM2_NO_WOLFCRYPT
+#define WOLFTPM2_NO_WOLFCRYPT
+
+/* =========================================================================
+ * TPM Communication Mode Selection (Auto-detected based on chip type)
+ * =========================================================================
+ *
+ * For real SPI hardware (SLB9672/SLB9673):
+ *   - Uses wolfTPM's native TIS layer with raw SPI via tpm_io_uboot.c
+ *   - Requires CONFIG_SPI and CONFIG_DM_SPI enabled in U-Boot
+ *
+ * For swtpm/QEMU testing (no specific chip defined):
+ *   - Uses WOLFTPM_LINUX_DEV mode with U-Boot's TPM driver (tpm_xfer())
+ *   - Works with MMIO-based TPM via tpm2_tis_mmio.c
+ */
+
+#if defined(WOLFTPM_SLB9672) || defined(WOLFTPM_SLB9673)
+	/* Real SPI hardware - use native wolfTPM TIS with raw SPI */
+	/* WOLFTPM_LINUX_DEV is NOT defined */
+	#define WOLFTPM_EXAMPLE_HAL
+
+	/* SPI bus and chip select for TPM
+	 * Official Raspberry Pi tpm-slb9670 overlay uses CE1 (GPIO7)
+	 * This matches LetsTrust and most Infineon evaluation boards */
+	#ifndef TPM_SPI_BUS
+		#define TPM_SPI_BUS 0
+	#endif
+	#ifndef TPM_SPI_CS
+		#define TPM_SPI_CS 1   /* CE1/GPIO7 - official RPi TPM overlay setting */
+	#endif
+#else
+	/* swtpm/QEMU - use U-Boot's TPM driver with MMIO communication mode */
+	#define WOLFTPM_LINUX_DEV
+#endif
+
+#define XSLEEP_MS(ms) udelay(ms * 1000)
+
+/* Timeout configuration */
+#ifdef WOLFTPM_FIRMWARE_UPGRADE
+	/* Firmware update requires much longer timeout for TPM processing */
+	#define TPM_TIMEOUT_TRIES 2000000
+#else
+	/* Normal operations - reduce from default 1,000,000 to prevent long hangs */
+	#define TPM_TIMEOUT_TRIES 10000
+#endif
+
+/* Add small delay between poll attempts to avoid tight spin loop */
+#define XTPM_WAIT() udelay(100)
+
+/* Do not include API's that use heap(), they are not required */
+#define WOLFTPM2_NO_HEAP
+
+/* Debugging - disabled for clean output */
+/* #define DEBUG_WOLFTPM */
+/* #define WOLFTPM_DEBUG_VERBOSE */
+/* #define WOLFTPM_DEBUG_IO */
+/* #define WOLFTPM_DEBUG_TIMEOUT */
+
+/* SPI Wait state checking - most TPMs use this */
+#define WOLFTPM_CHECK_WAIT_STATE
+
+/******************************************************************************/
+/* --- END wolfTPM U-boot Settings -- */
+/******************************************************************************/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* USER_SETTINGS_H */
include/wolftpm.h new file +34 -0
new file mode 100644
index 00000000000..a3cd9d0d2dd
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * wolfTPM integration header for U-Boot
+ *
+ * Copyright (C) 2025 wolfSSL Inc.
+ * Author: Aidan Garske <[email protected]>
+ */
+
+#ifndef __WOLFTPM_H__
+#define __WOLFTPM_H__
+
+#include <wolftpm/tpm2.h>
+#include <wolftpm/tpm2_wrap.h>
+#include <wolftpm/tpm2_packet.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef WOLFTPM_FIRMWARE_UPGRADE
+int TPM2_IFX_FwData_Cb(uint8_t *data, uint32_t data_req_sz,
+			uint32_t offset, void *cb_ctx);
+const char *TPM2_IFX_GetOpModeStr(int opMode);
+void TPM2_IFX_PrintInfo(WOLFTPM2_CAPS *caps);
+#endif
+
+int TPM2_PCRs_Print(void);
+int TPM2_Init_Device(WOLFTPM2_DEV *dev, void *userCtx);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __WOLFTPM_H__ */
lib/wolftpm.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.47.3
#9 lore raw

PATCH v3 7/12 tpm: add wolfTPM driver helpers and Kconfig options

In reply to Aidan Garske

From: Aidan <[email protected]>

Add wolfTPM helper functions and configuration options to the TPM driver subsystem.

drivers/tpm/wolftpm_common.c:
Shared helper functions used by the wolfTPM command backend:
- TPM2_IFX_FwData_Cb(): firmware data callback for Infineon firmware update streaming
- TPM2_IFX_GetOpModeStr(): converts Infineon operational mode codes to human-readable strings
- TPM2_IFX_PrintInfo(): prints manufacturer, vendor, firmware version, and operational mode from WOLFTPM2_CAPS
- TPM2_PCRs_Print(): enumerates and prints assigned PCR banks and their selected PCR indices
- TPM2_Init_Device(): initializes wolfTPM with the TPM2_IoCb HAL callback

drivers/tpm/Kconfig:
Adds configuration options under TPM_V2:
- TPM2_SPI_SANDBOX: sandbox TPM SPI emulator for testing
- TPM_AUTODETECT: auto-detect TPM chip for swtpm/QEMU
- WOLFTPM_LINUX_DEV: use U-Boot driver model instead of wolfTPM's native TIS layer
- WOLFTPM_SLB9672/SLB9673: Infineon chip-specific features
- WOLFTPM_FIRMWARE_UPGRADE: firmware update support

drivers/tpm/Makefile:
Compiles wolftpm_common.o and sets wolfTPM include paths and -DWOLFTPM_USER_SETTINGS when CONFIG_TPM_WOLF is enabled.

---
 drivers/tpm/Kconfig          |  44 +++++++++++
 drivers/tpm/Makefile         |   9 +++
 drivers/tpm/wolftpm_common.c | 137 +++++++++++++++++++++++++++++++++++
 3 files changed, 190 insertions(+)
 create mode 100644 drivers/tpm/wolftpm_common.c
drivers/tpm/Kconfig +44 -0
index 219ea606b50..a2897a0e040 100644
@@ -158,6 +158,14 @@ config TPM2_TIS_SANDBOX
 	  such as basic configuration, PCR extension and PCR read. Extended
 	  functionalities are not implemented.
 
+config TPM2_SPI_SANDBOX
+	bool "Enable sandbox TPM SPI emulator"
+	depends on TPM_V2 && SANDBOX && DM_SPI
+	help
+	  This driver emulates a TPM connected via SPI for sandbox testing.
+	  It implements the TPM TIS SPI protocol and can be used to test
+	  wolfTPM SPI HAL code without physical hardware.
+
 config TPM2_TIS_SPI
 	bool "Enable support for TPMv2.x SPI chips"
 	depends on TPM_V2 && DM_SPI
@@ -200,6 +208,42 @@ config TPM2_EVENT_LOG_SIZE
 	  allocated twice. One for the eventlog it self and one for the
 	  configuration table that is required from the TCG2 spec
 
+config TPM_AUTODETECT
+    bool "wolfTPM auto-detect TPM chip (for swtpm/QEMU)"
+    depends on TPM_V2 && TPM_WOLF
+    help
+      Enable wolfTPM chip auto-detection instead of using a specific
+      chip type (SLB9672/SLB9673). Use this for swtpm/QEMU testing
+      where no specific hardware chip is present.
+
+config WOLFTPM_LINUX_DEV
+    bool "Use device-level TPM interface (bypass wolfTPM TIS layer)"
+    depends on TPM_V2 && TPM_WOLF
+    default y
+    help
+      Enable wolfTPM to use the underlying TPM driver instead of its own
+      TIS (TPM Interface Specification) layer. On U-Boot, this uses the
+      U-Boot TPM driver model (tpm_xfer). On Linux, this uses /dev/tpm0.
+      This is the recommended setting for U-Boot.
+
+config WOLFTPM_SLB9672
+    bool "Enable support for Infineon SLB9672 TPM"
+    depends on TPM_V2 && TPM_WOLF
+    help
+      Enable support for Infineon SLB9672 TPM features in wolfTPM.
+
+config WOLFTPM_SLB9673
+    bool "Enable support for Infineon SLB9673 TPM"
+    depends on TPM_V2 && TPM_WOLF
+    help
+      Enable support for Infineon SLB9673 TPM features in wolfTPM.
+
+config WOLFTPM_FIRMWARE_UPGRADE
+    bool "Enable firmware upgrade support for wolfTPM"
+    depends on TPM_V2 && TPM_WOLF
+    help
+      Enable support for Infineon TPM firmware upgrade commands in wolfTPM.
+
 endif # TPM_V2
 
 endmenu
drivers/tpm/Makefile +9 -0
index b83ce703ec0..bee4193e9fc 100644
@@ -10,7 +10,16 @@ obj-$(CONFIG_TPM_TIS_SANDBOX) += tpm_tis_sandbox.o sandbox_common.o
 
 obj-$(CONFIG_$(PHASE_)TPM2_CR50_I2C) += cr50_i2c.o
 obj-$(CONFIG_TPM2_TIS_SANDBOX) += tpm2_tis_sandbox.o sandbox_common.o
+obj-$(CONFIG_TPM2_SPI_SANDBOX) += tpm_spi_sandbox.o
 obj-$(CONFIG_TPM2_TIS_SPI) += tpm2_tis_core.o tpm2_tis_spi.o
 obj-$(CONFIG_TPM2_TIS_I2C) += tpm2_tis_core.o tpm2_tis_i2c.o
 obj-$(CONFIG_TPM2_FTPM_TEE) += tpm2_ftpm_tee.o
 obj-$(CONFIG_TPM2_MMIO) += tpm2_tis_core.o tpm2_tis_mmio.o
+
+# wolfTPM helper functions
+ifeq ($(CONFIG_TPM_WOLF),y)
+ccflags-y += -I$(srctree)/lib/wolftpm \
+             -I$(srctree)/include/configs \
+             -DWOLFTPM_USER_SETTINGS
+obj-y += wolftpm_common.o
+endif
drivers/tpm/wolftpm_common.c new file +137 -0

137 lines not on this page · raw message

Signature · 1 line
2.47.3
#10 lore raw

PATCH v3 8/12 cmd: refactor tpm2 command into frontend/backend architecture

In reply to Aidan Garske

From: Aidan <[email protected]>

Split the tpm2 command implementation into a shared frontend and two selectable backends. This allows wolfTPM to provide its own TPM2 command implementations while keeping the command table, dispatcher, and help text shared.

Architecture:

cmd/tpm-v2.c (frontend - always compiled):
Contains the tpm2_commands[] table, get_tpm2_commands(), the do_tpm2() dispatcher, and the U_BOOT_CMD help text. References backend functions via cmd/tpm2-backend.h. When CONFIG_TPM_WOLF is enabled, additional wolfTPM-only commands (caps, pcr_print, firmware_update, firmware_cancel) are added to the table.

cmd/tpm2-backend.h (new):
Declares all backend function prototypes that both backends must
implement: do_tpm2_device, do_tpm2_info, do_tpm2_init, do_tpm2_startup, do_tpm2_selftest, do_tpm2_clear, do_tpm2_pcr_extend, do_tpm2_pcr_read, do_tpm2_get_capability, do_tpm2_dam_reset, do_tpm2_dam_parameters, do_tpm2_change_auth, do_tpm2_pcr_setauthpolicy, do_tpm2_pcr_setauthvalue, do_tpm2_pcr_allocate, plus wolfTPM-only functions.

cmd/native_tpm2.c (new - native backend):
Contains the original tpm2 command implementations that use U-Boot's TPM driver model (tpm_api.h, tpm-v2.h). Compiled when CONFIG_TPM_WOLF is not set. Common commands delegate to tpm-common.c helpers (do_tpm_device, do_tpm_info, etc.).

cmd/wolftpm.c (new - wolfTPM backend):
Implements all tpm2 commands using wolfTPM library APIs directly (wolfTPM2_Init, wolfTPM2_GetCapabilities, wolfTPM2_ExtendPCR, etc.). Includes Infineon-specific firmware update and cancel commands. Each command initializes its own WOLFTPM2_DEV instance rather than going through U-Boot's driver model.

cmd/Kconfig:
Adds CMD_WOLFTPM option that selects TPM_WOLF and CMD_TPM_V2, providing a single menuconfig toggle for wolfTPM support.

cmd/Makefile:
Conditionally compiles wolftpm.o (when CONFIG_TPM_WOLF=y) or native_tpm2.o (otherwise) alongside the shared tpm-v2.o frontend. Sets wolfTPM include paths and -DWOLFTPM_USER_SETTINGS.

The reason for separate backend files rather than a callback-based approach is that wolfTPM uses fundamentally different types and initialization patterns (WOLFTPM2_DEV vs struct udevice, direct library calls vs driver model ops), making runtime dispatch impractical without heavy abstraction.

---
 cmd/Kconfig        |   11 +
 cmd/Makefile       |   10 +-
 cmd/native_tpm2.c  |  516 +++++++++++++++++++
 cmd/tpm-v2.c       |  559 +++------------------
 cmd/tpm2-backend.h |   66 +++
 cmd/wolftpm.c      | 1170 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 1840 insertions(+), 492 deletions(-)
 create mode 100644 cmd/native_tpm2.c
 create mode 100644 cmd/tpm2-backend.h
 create mode 100644 cmd/wolftpm.c
cmd/Kconfig +11 -0
index b71ac554c0b..fe9b3819eaa 100644
@@ -3194,4 +3194,15 @@ config CMD_SPAWN_NUM_JOBS
 	  When a jobs exits, its identifier is available to be re-used by the next
 	  spawn command.
 
+config CMD_WOLFTPM
+	bool "Use wolfTPM as TPM2 backend"
+	depends on TPM_V2
+	select TPM_WOLF
+	select CMD_TPM_V2
+	help
+	  Use the wolfTPM library as the backend for TPM2 commands instead
+	  of the standard U-Boot TPM2 implementation. wolfTPM offers additional
+	  features including firmware update support for Infineon TPMs and
+	  enhanced capabilities reporting.
+
 endif
cmd/Makefile +9 -1
index 4cd13d4fa6e..2b12b26e61f 100644
@@ -191,9 +191,17 @@ obj-$(CONFIG_CMD_TIMER) += timer.o
 obj-$(CONFIG_CMD_TRACE) += trace.o
 obj-$(CONFIG_HUSH_PARSER) += test.o
 obj-$(CONFIG_CMD_TPM) += tpm-common.o
-obj-$(CONFIG_CMD_TPM_V1) += tpm-v1.o
 obj-$(CONFIG_CMD_TPM_TEST) += tpm_test.o
+obj-$(CONFIG_CMD_TPM_V1) += tpm-v1.o
 obj-$(CONFIG_CMD_TPM_V2) += tpm-v2.o
+ifeq ($(CONFIG_TPM_WOLF),y)
+ccflags-y += -I$(srctree)/lib/wolftpm \
+             -I$(srctree)/include/configs \
+             -DWOLFTPM_USER_SETTINGS
+obj-$(CONFIG_CMD_TPM_V2) += wolftpm.o
+else
+obj-$(CONFIG_CMD_TPM_V2) += native_tpm2.o
+endif
 obj-$(CONFIG_CMD_CROS_EC) += cros_ec.o
 obj-$(CONFIG_CMD_UBI) += ubi.o
 obj-$(CONFIG_CMD_UBIFS) += ubifs.o
cmd/native_tpm2.c new file +516 -0

516 lines not on this page · raw message

cmd/tpm-v2.c +68 -491

599 lines not on this page · raw message

cmd/tpm2-backend.h new file +66 -0

66 lines not on this page · raw message

cmd/wolftpm.c new file +1170 -0

1,170 lines not on this page · raw message

Signature · 1 line
2.47.3
#11 lore raw

PATCH v3 9/12 tpm: add sandbox TPM SPI emulator

In reply to Aidan Garske

From: Aidan <[email protected]>

Add a TPM SPI emulator for sandbox testing that implements the TIS (TPM Interface Specification) SPI protocol, allowing wolfTPM's SPI HAL code to be tested without physical hardware.

drivers/tpm/tpm_spi_sandbox.c (new):
Emulates a TPM connected via SPI by implementing the TIS register set and SPI protocol:
- SPI protocol state machine: parses 4-byte TIS SPI headers (R/W bit, transfer length, register address) and handles data phase with immediate ready signaling (no wait states)
- TIS register emulation: TPM_ACCESS (locality request/grant), TPM_STS (command ready, data expect, data available, burst count), TPM_INTF_CAPS, TPM_DID_VID (Infineon SLB9670 IDs), TPM_RID, and TPM_DATA_FIFO (command/response buffering)
- TIS state machine: IDLE -> READY -> RECEPTION -> EXECUTION -> COMPLETION, with command-ready abort support
- Generates simple TPM_RC_SUCCESS responses (a full implementation would integrate the sandbox TPM2 state machine)
- Registers as UCLASS_SPI_EMUL with compatible "sandbox,tpm-spi-emul"
- Also registers a SPI slave driver (UCLASS_SPI_GENERIC) with compatible "sandbox,tpm-spi" for the DTS device node

drivers/mtd/spi/sandbox.c:
Modify sandbox_spi_get_emul() to check for a "sandbox,emul" phandle property on SPI slave devices before falling back to the default SPI flash emulation binding. This allows non-flash SPI devices (like the TPM emulator) to specify their own emulator via device tree phandle.

---
 drivers/mtd/spi/sandbox.c     |  30 ++-
 drivers/tpm/tpm_spi_sandbox.c | 410 ++++++++++++++++++++++++++++++++++
 2 files changed, 431 insertions(+), 9 deletions(-)
 create mode 100644 drivers/tpm/tpm_spi_sandbox.c
drivers/mtd/spi/sandbox.c +21 -9
index e5ebc3479fb..41bd07817aa 100644
@@ -571,16 +571,28 @@ int sandbox_spi_get_emul(struct sandbox_state *state,
 
 	info = &state->spi[busnum][cs];
 	if (!info->emul) {
-		/* Use the same device tree node as the SPI flash device */
-		debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
-		      __func__, busnum, cs);
-		ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
-					   dev_ofnode(slave), slave->name);
-		if (ret) {
-			debug("failed (err=%d)\n", ret);
-			return ret;
+		struct udevice *emul;
+		ofnode node = dev_ofnode(slave);
+
+		/* First check for sandbox,emul phandle property */
+		ret = uclass_get_device_by_phandle(UCLASS_SPI_EMUL, slave,
+						   "sandbox,emul", &emul);
+		if (!ret) {
+			debug("%s: busnum=%u, cs=%u: using phandle emulator\n",
+			      __func__, busnum, cs);
+			info->emul = emul;
+		} else {
+			/* Fall back to SPI flash emulation binding */
+			debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
+			      __func__, busnum, cs);
+			ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
+						   node, slave->name);
+			if (ret) {
+				debug("failed (err=%d)\n", ret);
+				return ret;
+			}
+			debug("OK\n");
 		}
-		debug("OK\n");
 	}
 	*emulp = info->emul;
 
drivers/tpm/tpm_spi_sandbox.c new file +410 -0

410 lines not on this page · raw message

Signature · 1 line
2.47.3
#12 lore raw

PATCH v3 10/12 test: add wolfTPM C unit tests and Python integration tests

In reply to Aidan Garske

From: Aidan <[email protected]>

Add comprehensive test suites for wolfTPM commands, modeled after the existing TPM2 test infrastructure.

test/cmd/wolftpm.c (C unit tests):
18 tests using U-Boot's unit test framework (CMD_TEST macro):
- autostart, init, info, state, device: basic lifecycle
- self_test (full and continue): TPM self-test verification
- startup_clear, startup_state: TPM2_Startup modes
- get_capability: read TPM properties
- caps: wolfTPM-enhanced capabilities display
- clear: TPM state reset via LOCKOUT hierarchy
- pcr_read, pcr_extend, pcr_print: PCR operations
- pcr_allocate: PCR bank reconfiguration
- dam_reset, dam_parameters: dictionary attack mitigation
- change_auth: hierarchy password change (requires wolfCrypt)
- cleanup: reset TPM state after tests
Run with: ut cmd cmd_test_wolftpm_*

test/cmd/Makefile:
Adds wolftpm.o when CONFIG_TPM_WOLF is enabled.

test/py/tests/test_wolftpm.py (Python integration tests):
21 tests using pytest with the U-Boot test framework:
- Requires QEMU + swtpm (not sandbox) because wolfTPM bypasses U-Boot's driver model and communicates directly with TPM hardware via its own SPI/MMIO HAL
- Tests mirror the C tests but run end-to-end through the U-Boot console, checking return codes via 'echo $?'
- Includes force_init() helper for TPM reinitialization after test failures
- Skippable via env__wolftpm_device_test_skip config
- Verified: 19 passed, 2 skipped (change_auth requires wolfCrypt, get_capability may skip on some platforms)

---
 test/cmd/Makefile             |   1 +
 test/cmd/wolftpm.c            | 364 +++++++++++++++++++++++++++++++++
 test/py/tests/test_wolftpm.py | 375 ++++++++++++++++++++++++++++++++++
 3 files changed, 740 insertions(+)
 create mode 100644 test/cmd/wolftpm.c
 create mode 100644 test/py/tests/test_wolftpm.py
test/cmd/Makefile +1 -0
index 2476068aee6..08fbc31a06a 100644
@@ -45,3 +45,4 @@ endif
 obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o
 endif
 obj-$(CONFIG_CMD_SPAWN) += spawn.o
+obj-$(CONFIG_TPM_WOLF) += wolftpm.o
test/cmd/wolftpm.c new file +364 -0

364 lines not on this page · raw message

test/py/tests/test_wolftpm.py new file +375 -0

375 lines not on this page · raw message

Signature · 1 line
2.47.3
#13 lore raw

PATCH v3 11/12 doc: add wolfTPM documentation

In reply to Aidan Garske

From: Aidan <[email protected]>

Add documentation for wolfTPM integration, commands, and testing.

doc/usage/cmd/wolftpm.rst:
Comprehensive RST documentation covering:
- All wolfTPM tpm2 subcommands with usage and examples
- Infineon TPM firmware update step-by-step guide (extract manifest/firmware, load to RAM, perform update, recovery mode)
- Build instructions for RPi4 and QEMU targets
- Enabling debug output (U-Boot log system + wolfTPM library)
- Complete test suite documentation with test coverage table
- Python test framework setup (QEMU + swtpm instructions, helper scripts, verified test results)

README.wolftpm.md:
Quick-start guide with overview, feature comparison vs standard U-Boot TPM, command reference, build instructions, hardware support details, and file listing.

README:
Add CONFIG_TPM_WOLF reference in the configuration options section alongside existing CONFIG_TPM entries.

---
 README                    |   3 +
 README.wolftpm.md         | 154 +++++++++
 doc/usage/cmd/wolftpm.rst | 635 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 792 insertions(+)
 create mode 100644 README.wolftpm.md
 create mode 100644 doc/usage/cmd/wolftpm.rst
README +3 -0
index 20a73bab802..b9bfe14a1c7 100644
@@ -361,6 +361,9 @@ The following options need to be configured:
 		CONFIG_TPM
 		Support TPM devices.
 
+		CONFIG_TPM_WOLF
+		Enables support for wolfTPM library.
+
 		CONFIG_TPM_TIS_INFINEON
 		Support for Infineon i2c bus TPM devices. Only one device
 		per system is supported at this time.
README.wolftpm.md new file +154 -0

154 lines not on this page · raw message

doc/usage/cmd/wolftpm.rst new file +635 -0

635 lines not on this page · raw message

Signature · 1 line
2.47.3
#14 lore raw

PATCH v3 12/12 configs: enable wolfTPM in rpi_4_defconfig

In reply to Aidan Garske

From: Aidan <[email protected]>

Enable SPI, TPM, and wolfTPM support in the Raspberry Pi 4 default configuration for out-of-the-box TPM 2.0 support with Infineon SLB9670/9672 TPM HATs.

SPI support:
CONFIG_SPI, CONFIG_DM_SPI, CONFIG_BCM2835_SPI, CONFIG_CMD_SPI Enables the BCM2835 hardware SPI driver. Soft SPI is disabled since the hardware driver is used.

TPM and wolfTPM:
CONFIG_TPM, CONFIG_TPM_V2, CONFIG_CMD_TPM: core TPM support
CONFIG_TPM_WOLF: wolfTPM library integration
CONFIG_CMD_WOLFTPM: wolfTPM command backend for tpm2
CONFIG_WOLFTPM_SLB9672: Infineon SLB9672 chip support WOLFTPM_LINUX_DEV disabled: uses native wolfTPM TIS layer with direct SPI communication rather than U-Boot's TPM driver model

Logging:
CONFIG_LOG with level 7 (DEBUG) to enable log_debug() output from wolfTPM commands.

Testing:
CONFIG_UNIT_TEST, CONFIG_CONSOLE_RECORD, CONFIG_HEXDUMP for running the wolfTPM C unit test suite.

---
 configs/rpi_4_defconfig | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
configs/rpi_4_defconfig +28 -1
index 8362242b97f..22280d9b70e 100644
@@ -69,4 +69,31 @@ CONFIG_SYS_WHITE_ON_BLACK=y
 CONFIG_VIDEO_BCM2835=y
 CONFIG_CONSOLE_SCROLL_LINES=10
 CONFIG_PHYS_TO_BUS=y
-# CONFIG_HEXDUMP is not set
+# HEXDUMP enabled for unit tests
+
+# SPI support (hardware SPI - matches Linux tpm-slb9670 overlay)
+CONFIG_SPI=y
+CONFIG_DM_SPI=y
+CONFIG_BCM2835_SPI=y
+# CONFIG_SOFT_SPI is not set
+CONFIG_CMD_SPI=y
+
+# TPM and wolfTPM support
+CONFIG_TPM=y
+CONFIG_TPM_V2=y
+CONFIG_CMD_TPM=y
+CONFIG_TPM_WOLF=y
+# CONFIG_WOLFTPM_LINUX_DEV is not set
+CONFIG_WOLFTPM_SLB9672=y
+CONFIG_CMD_WOLFTPM=y
+
+# Logging (debug level to see log_debug output)
+CONFIG_LOG=y
+CONFIG_LOGLEVEL=7
+CONFIG_LOG_MAX_LEVEL=7
+CONFIG_LOG_DEFAULT_LEVEL=7
+
+# Unit testing support
+CONFIG_UNIT_TEST=y
+CONFIG_CONSOLE_RECORD=y
+CONFIG_HEXDUMP=y
Signature · 1 line
2.47.3
#15 lore raw

Re: [PATCH v3 12/12] configs: enable wolfTPM in rpi_4_defconfig

Hi Aidan,

Firstly you should be copying the maintainers of the components you're changing, I see a bunch of changes to the Raspberry Pi but the maintainers aren't copied.

Quoted 8 lines

Enable SPI, TPM, and wolfTPM support in the Raspberry Pi 4 default configuration for out-of-the-box TPM 2.0 support with Infineon SLB9670/9672 TPM HATs.

SPI support:
CONFIG_SPI, CONFIG_DM_SPI, CONFIG_BCM2835_SPI, CONFIG_CMD_SPI Enables the BCM2835 hardware SPI driver. Soft SPI is disabled since the hardware driver is used.

Why would the would the soft SPI driver be enabled, I don't see it as a default y anywhere.

Quoted 8 lines

TPM and wolfTPM:
CONFIG_TPM, CONFIG_TPM_V2, CONFIG_CMD_TPM: core TPM support
CONFIG_TPM_WOLF: wolfTPM library integration
CONFIG_CMD_WOLFTPM: wolfTPM command backend for tpm2
CONFIG_WOLFTPM_SLB9672: Infineon SLB9672 chip support WOLFTPM_LINUX_DEV disabled: uses native wolfTPM TIS layer with direct SPI communication rather than U-Boot's TPM driver model

Quoted 7 lines

Logging:
CONFIG_LOG with level 7 (DEBUG) to enable log_debug() output from wolfTPM commands.

Testing:
CONFIG_UNIT_TEST, CONFIG_CONSOLE_RECORD, CONFIG_HEXDUMP for running the wolfTPM C unit test suite.

The above tells me what you're doing, but I can see that from the config changes below, it doesn't tell me *WHY*! So please explain!

Quoted 10 lines
---
 configs/rpi_4_defconfig | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
configs/rpi_4_defconfig +0 -0
index 8362242b97f..22280d9b70e 100644

configs/rpi_4_defconfig CONFIG_SYS_WHITE_ON_BLACK=y · line 72

Quoted 5 lines
configs/rpi_4_defconfig +1 -1
@@ -69,4 +69,31 @@ CONFIG_SYS_WHITE_ON_BLACK=y
 CONFIG_VIDEO_BCM2835=y
 CONFIG_CONSOLE_SCROLL_LINES=10
 CONFIG_PHYS_TO_BUS=y
-# CONFIG_HEXDUMP is not set
+# HEXDUMP enabled for unit tests

Would we want this enabled for a default user on a RPi? I'm not sure we do.

configs/rpi_4_defconfig CONFIG_SYS_WHITE_ON_BLACK=y · line 79

Quoted 6 lines
configs/rpi_4_defconfig +6 -0
+# SPI support (hardware SPI - matches Linux tpm-slb9670 overlay)
+CONFIG_SPI=y
+CONFIG_DM_SPI=y
+CONFIG_BCM2835_SPI=y
+# CONFIG_SOFT_SPI is not set
+CONFIG_CMD_SPI=y

What does the below bring for the average RPi user? Why would I want it as the maintainer?

configs/rpi_4_defconfig CONFIG_SYS_WHITE_ON_BLACK=y · line 88

Quoted 8 lines
configs/rpi_4_defconfig +8 -0
+# TPM and wolfTPM support
+CONFIG_TPM=y
+CONFIG_TPM_V2=y
+CONFIG_CMD_TPM=y
+CONFIG_TPM_WOLF=y
+# CONFIG_WOLFTPM_LINUX_DEV is not set
+CONFIG_WOLFTPM_SLB9672=y
+CONFIG_CMD_WOLFTPM=y

For the below an absolute NAK, This config isn't what the average user would want. The config isn't your play ground.

Quoted 13 lines
configs/rpi_4_defconfig +10 -1
+# Logging (debug level to see log_debug output)
+CONFIG_LOG=y
+CONFIG_LOGLEVEL=7
+CONFIG_LOG_MAX_LEVEL=7
+CONFIG_LOG_DEFAULT_LEVEL=7
+
+# Unit testing support
+CONFIG_UNIT_TEST=y
+CONFIG_CONSOLE_RECORD=y
+CONFIG_HEXDUMP=y
--

2.47.3

#16 lore raw
In reply to Aidan Garske

Hi Aidan,

Quoted 3 lines

This is v3 of the wolfTPM TPM 2.0 stack integration for U-Boot.

wolfTPM (https://github.com/wolfSSL/wolfTPM) is a portable, GPLv2 TPM 2.0

I go to the github link and it says GPL-3.0 license so which is it?

IANAL but I'm not sure if the GPLv3 is compatible with GPLv2 in the context of U-Boot because it would make the whole output a GPLv3 application

Quoted 5 lines

library that provides a full TPM 2.0 command set, an SPI/MMIO HAL, and firmware-update support for Infineon SLB9672/SLB9673 hardware. This series wires it into U-Boot as an optional backend behind the existing 'tpm2' command, alongside support for QEMU+swtpm, sandbox emulation, and real Raspberry Pi 4 + Infineon SLB9672 hardware.

So that provides the technical detail, but what value does it provide to U-Boot? We already have a relatively complete TPM stack, do people do firmware updates in the early boot process, why else would someone choose to use this over the existing implementation?

One part of what I want to know in a cover letter is what value this provides to the project, what are the pros/cons etc, like what maintenance looks like.

Peter

Quoted 65 lines

Branch (full 14-commit history including the subtree squash + merge):
https://github.com/aidangarske/u-boot wolftpm-v2-patches

Note on the subtree commits omitted from this email thread:
The branch above contains 14 commits; the email series is 12 patches. Two commits are deliberately not sent to the list:

* "Squashed 'lib/wolftpm/' content from commit 664db130d57"
- the parentless squash commit produced by git subtree add. Its diff is ~3.4MB / ~90k lines and would be rejected by the mailing list on size.
* "Merge commit 'd42fd7b146...' as 'lib/wolftpm'"
- the corresponding subtree merge commit. Merges have no patch form and are routinely omitted by git format-patch.

Please pull from the branch above (or wolfssl/wolfTPM @ 664db130d57) to inspect the imported wolfTPM source. Subsequent updates will go via tools/update-subtree.sh, matching how mbedTLS, lwIP, and dts/upstream are maintained in tree.

Changes since v2:
- Replaced the lib/wolftpm git submodule with a git subtree import (squash + merge), matching the convention used for mbedTLS, lwIP, and dts/upstream. tools/update-subtree.sh is updated to know about the wolftpm subtree (path lib/wolftpm, upstream
https://github.com/wolfssl/wolfTPM.git).
[feedback: Ilias Apalodimas]
- Reverted the changes to include/linux/byteorder/generic.h. The redefinition workaround for cpu_to_beXX / beXX_to_cpu now lives on the wolfTPM side: include/configs/user_settings.h pulls in <asm/byteorder.h> up front so U-Boot's macros are defined before wolfTPM's #ifndef-guarded fallbacks in tpm2_packet.h. [feedback: Ilias Apalodimas]

Testing:
- QEMU arm64 + swtpm Python test framework (./test/py/test.py --bd qemu_arm64 -k "test_wolftpm and not ut_cmd"):
19 passed, 2 skipped (matching doc/usage/cmd/wolftpm.rst).
- Manual QEMU arm64 + swtpm walkthrough per doc/usage/cmd/wolftpm.rst section "Building and Running wolfTPM with U-Boot using QEMU": tpm2 help/info/autostart/startup/ get_capability/pcr_read/pcr_print/caps all return expected output.
- Raspberry Pi 4 + Infineon SLB9672 (real hardware): all wolfTPM cmd tests pass, including firmware update path.

v2 thread:
https://lore.kernel.org/u-boot/?q=PATCH+v2+tpm+wolfTPM

Aidan Garske (12):
tpm: export tpm_show_device, tpm_set_device, and get_tpm include/hash: add SHA384 hash wrapper declaration for wolfTPM
spi: add BCM2835/BCM2711 hardware SPI controller driver
dts: add TPM device tree nodes for RPi4, QEMU, and sandbox
tpm: add wolfTPM build rules and Kconfig
tpm: add wolfTPM headers and SHA384 glue code
tpm: add wolfTPM driver helpers and Kconfig options
cmd: refactor tpm2 command into frontend/backend architecture
tpm: add sandbox TPM SPI emulator
test: add wolfTPM C unit tests and Python integration tests
doc: add wolfTPM documentation
configs: enable wolfTPM in rpi_4_defconfig

--
2.47.3

#17 lore raw
Quoted 5 lines · Peter Robinson

Hi Aidan,

Quoted 2 lines

This is v3 of the wolfTPM TPM 2.0 stack integration for U-Boot.

Quoted 7 lines
Quoted 1 line

wolfTPM (https://github.com/wolfSSL/wolfTPM) is a portable, GPLv2 TPM 2.0

I go to the github link and it says GPL-3.0 license so which is it?

IANAL but I'm not sure if the GPLv3 is compatible with GPLv2 in the context of U-Boot because it would make the whole output a GPLv3 application

In fact the majority of U-Boot code, like Linux, is GPLv2 only, not later which makes this directly incompatible with U-Boot.

Quoted 11 lines
Quoted 5 lines

library that provides a full TPM 2.0 command set, an SPI/MMIO HAL, and firmware-update support for Infineon SLB9672/SLB9673 hardware. This series wires it into U-Boot as an optional backend behind the existing 'tpm2' command, alongside support for QEMU+swtpm, sandbox emulation, and real Raspberry Pi 4 + Infineon SLB9672 hardware.

So that provides the technical detail, but what value does it provide to U-Boot? We already have a relatively complete TPM stack, do people do firmware updates in the early boot process, why else would someone choose to use this over the existing implementation?

Quoted 58 lines

One part of what I want to know in a cover letter is what value this provides to the project, what are the pros/cons etc, like what maintenance looks like.

Peter

Quoted 52 lines

Branch (full 14-commit history including the subtree squash + merge):
https://github.com/aidangarske/u-boot wolftpm-v2-patches

Note on the subtree commits omitted from this email thread:
The branch above contains 14 commits; the email series is 12 patches. Two commits are deliberately not sent to the list:

* "Squashed 'lib/wolftpm/' content from commit 664db130d57"
- the parentless squash commit produced by git subtree add. Its diff is ~3.4MB / ~90k lines and would be rejected by the mailing list on size.
* "Merge commit 'd42fd7b146...' as 'lib/wolftpm'"
- the corresponding subtree merge commit. Merges have no patch form and are routinely omitted by git format-patch.

Please pull from the branch above (or wolfssl/wolfTPM @ 664db130d57) to inspect the imported wolfTPM source. Subsequent updates will go via tools/update-subtree.sh, matching how mbedTLS, lwIP, and dts/upstream are maintained in tree.

Changes since v2:
- Replaced the lib/wolftpm git submodule with a git subtree import (squash + merge), matching the convention used for mbedTLS, lwIP, and dts/upstream. tools/update-subtree.sh is updated to know about the wolftpm subtree (path lib/wolftpm, upstream
https://github.com/wolfssl/wolfTPM.git).
[feedback: Ilias Apalodimas]
- Reverted the changes to include/linux/byteorder/generic.h. The redefinition workaround for cpu_to_beXX / beXX_to_cpu now lives on the wolfTPM side: include/configs/user_settings.h pulls in <asm/byteorder.h> up front so U-Boot's macros are defined before wolfTPM's #ifndef-guarded fallbacks in tpm2_packet.h. [feedback: Ilias Apalodimas]

Testing:
- QEMU arm64 + swtpm Python test framework (./test/py/test.py --bd qemu_arm64 -k "test_wolftpm and not ut_cmd"):
19 passed, 2 skipped (matching doc/usage/cmd/wolftpm.rst).
- Manual QEMU arm64 + swtpm walkthrough per doc/usage/cmd/wolftpm.rst section "Building and Running wolfTPM with U-Boot using QEMU": tpm2 help/info/autostart/startup/ get_capability/pcr_read/pcr_print/caps all return expected output.
- Raspberry Pi 4 + Infineon SLB9672 (real hardware): all wolfTPM cmd tests pass, including firmware update path.

v2 thread:
https://lore.kernel.org/u-boot/?q=PATCH+v2+tpm+wolfTPM

Aidan Garske (12):
tpm: export tpm_show_device, tpm_set_device, and get_tpm include/hash: add SHA384 hash wrapper declaration for wolfTPM
spi: add BCM2835/BCM2711 hardware SPI controller driver

While I understand why you would put the above patch in it should in fact be sent on it's own as a standalone patch because it's really nothing to with this series.

Quoted 13 lines
Quoted 13 lines

dts: add TPM device tree nodes for RPi4, QEMU, and sandbox
tpm: add wolfTPM build rules and Kconfig
tpm: add wolfTPM headers and SHA384 glue code
tpm: add wolfTPM driver helpers and Kconfig options
cmd: refactor tpm2 command into frontend/backend architecture
tpm: add sandbox TPM SPI emulator
test: add wolfTPM C unit tests and Python integration tests
doc: add wolfTPM documentation
configs: enable wolfTPM in rpi_4_defconfig

--
2.47.3