Skip to content

Commit 4699d73

Browse files
add uart CI test
1 parent dc2caed commit 4699d73

2 files changed

Lines changed: 178 additions & 1 deletion

File tree

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
name: Test UART Communication with SWTPM
2+
3+
on:
4+
push:
5+
branches: [ 'master', 'main', 'release/**' ]
6+
pull_request:
7+
branches: [ 'master', 'main', 'release/**' ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test-uart-swtpm:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Install basic dependencies
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install -y \
22+
automake \
23+
autotools-dev \
24+
libtool \
25+
pkg-config \
26+
gcc \
27+
make \
28+
git \
29+
socat
30+
31+
- name: Install swtpm
32+
run: |
33+
sudo apt-get install -y swtpm swtpm-tools
34+
35+
- name: Setup wolfSSL
36+
uses: actions/checkout@v4
37+
with:
38+
repository: wolfssl/wolfssl
39+
path: wolfssl
40+
41+
- name: Build wolfSSL
42+
working-directory: ./wolfssl
43+
run: |
44+
./autogen.sh
45+
./configure --enable-wolftpm --prefix=$PWD/../wolfssl-install
46+
make -j$(nproc)
47+
make install
48+
49+
- name: Create virtual UART pair
50+
id: uart
51+
run: |
52+
# Create a PTY pair for UART simulation using socat
53+
# This creates two pseudo-terminals that are connected
54+
# One end will be used by swtpm (server side)
55+
# The other end will be used by wolfTPM (client side, as UART device)
56+
socat -d -d pty,raw,echo=0,link=/tmp/tpm-uart-server pty,raw,echo=0,link=/tmp/tpm-uart-client &
57+
SOCAT_PID=$!
58+
echo $SOCAT_PID > /tmp/socat.pid
59+
sleep 2
60+
61+
# Get the actual PTY device names
62+
SERVER_PTY=$(readlink -f /tmp/tpm-uart-server)
63+
CLIENT_PTY=$(readlink -f /tmp/tpm-uart-client)
64+
65+
echo "server_pty=$SERVER_PTY" >> $GITHUB_OUTPUT
66+
echo "client_pty=$CLIENT_PTY" >> $GITHUB_OUTPUT
67+
68+
echo "Server PTY (for swtpm): $SERVER_PTY"
69+
echo "Client PTY (for wolfTPM): $CLIENT_PTY"
70+
71+
# Verify PTYs exist and set permissions
72+
ls -la $SERVER_PTY $CLIENT_PTY || exit 1
73+
74+
# Make PTYs readable/writable by all (needed for swtpm)
75+
# Also ensure they're owned by the current user
76+
sudo chown $USER:$USER $SERVER_PTY $CLIENT_PTY || true
77+
chmod 666 $SERVER_PTY $CLIENT_PTY || true
78+
79+
- name: Start swtpm with chardev (UART)
80+
run: |
81+
SERVER_PTY="${{ steps.uart.outputs.server_pty }}"
82+
mkdir -p /tmp/swtpm-state
83+
84+
# Ensure PTY permissions are correct
85+
chmod 666 $SERVER_PTY || true
86+
87+
# Start swtpm with chardev backend using the server PTY
88+
# This allows swtpm to communicate over the PTY as if it were a UART
89+
# Run without sudo since we've set permissions
90+
swtpm chardev \
91+
--tpm2 \
92+
--tpmstate dir=/tmp/swtpm-state \
93+
--chardev $SERVER_PTY \
94+
--flags not-need-init &
95+
SWTPM_PID=$!
96+
echo $SWTPM_PID > /tmp/swtpm.pid
97+
98+
# Give swtpm time to start
99+
sleep 3
100+
101+
# Verify swtpm is running
102+
ps aux | grep swtpm | grep -v grep || exit 1
103+
104+
- name: Build wolfTPM with UART support
105+
env:
106+
CLIENT_PTY: ${{ steps.uart.outputs.client_pty }}
107+
run: |
108+
cd ${{ github.workspace }}
109+
./autogen.sh
110+
# Set UART device path and baud rate via CFLAGS
111+
# The device path needs to be properly quoted in the C define
112+
export CFLAGS="-DTPM2_SWTPM_HOST=\\\"$CLIENT_PTY\\\" -DTPM2_SWTPM_PORT=115200"
113+
echo "Building with UART device: $CLIENT_PTY"
114+
./configure \
115+
--enable-swtpm=uart \
116+
--with-wolfcrypt=$PWD/../wolfssl-install
117+
make -j$(nproc)
118+
119+
- name: Verify UART setup
120+
env:
121+
CLIENT_PTY: ${{ steps.uart.outputs.client_pty }}
122+
SERVER_PTY: ${{ steps.uart.outputs.server_pty }}
123+
run: |
124+
echo "Verifying UART setup..."
125+
echo "Client PTY: $CLIENT_PTY"
126+
echo "Server PTY: $SERVER_PTY"
127+
128+
# Verify PTYs are still accessible
129+
[ -c "$CLIENT_PTY" ] || (echo "Client PTY not found!" && exit 1)
130+
[ -c "$SERVER_PTY" ] || (echo "Server PTY not found!" && exit 1)
131+
132+
# Verify swtpm is still running
133+
ps aux | grep swtpm | grep -v grep || (echo "swtpm not running!" && exit 1)
134+
135+
echo "UART setup verified successfully"
136+
137+
- name: Run UART communication test
138+
env:
139+
CLIENT_PTY: ${{ steps.uart.outputs.client_pty }}
140+
run: |
141+
cd ${{ github.workspace }}
142+
143+
# Build the caps example
144+
cd examples/wrap
145+
make caps
146+
147+
echo "Running UART communication test..."
148+
echo "Using UART device: $CLIENT_PTY"
149+
150+
# Run the test with a timeout
151+
# The test should connect to the PTY as if it were a UART device
152+
timeout 30 ./caps || {
153+
echo "Test failed!"
154+
echo "Checking if swtpm is still running..."
155+
ps aux | grep swtpm | grep -v grep || echo "swtpm is not running"
156+
exit 1
157+
}
158+
159+
echo "UART communication test passed!"
160+
161+
- name: Cleanup
162+
if: always()
163+
run: |
164+
# Kill swtpm
165+
if [ -f /tmp/swtpm.pid ]; then
166+
kill $(cat /tmp/swtpm.pid) 2>/dev/null || true
167+
fi
168+
169+
# Kill socat PTY pair
170+
if [ -f /tmp/socat.pid ]; then
171+
kill $(cat /tmp/socat.pid) 2>/dev/null || true
172+
fi
173+
174+
# Clean up PTY links
175+
rm -f /tmp/tpm-uart-server /tmp/tpm-uart-client

wolftpm/tpm2_types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,11 @@ typedef int64_t INT64;
298298

299299
/* Helper to convert macro to string */
300300
#ifndef XSTRINGIFY
301-
#define XSTRINGIFY(s) STRINGIFY(s)
301+
#ifndef STRINGIFY
302302
#define STRINGIFY(s) #s
303303
#endif
304+
#define XSTRINGIFY(s) STRINGIFY(s)
305+
#endif
304306

305307
/* ---------------------------------------------------------------------------*/
306308
/* TPM HARDWARE TYPE */

0 commit comments

Comments
 (0)