While this test is great to see, it only tests the case where TPM device is only visible for the userspace of the Linux system. Exposing the TPM device to bootloader etc requires starting it outside of qemu and configuring qemu to expose the char device.
With poky master branch with QB_SETUP_CMD support, this can be done in machine config with:
# setup SW based TPM for testing, note socket file path has 107 character # length limitations from sockaddr_un QB_SETUP_CMD = " \ set -ex; pwd; which swtpm; swtpm --version; which swtpm_setup; \ test -d '${IMAGE_BASENAME}_swtpm' || ( mkdir -p '${IMAGE_BASENAME}_swtpm' && \ swtpm_setup --reconfigure --tpmstate '${IMAGE_BASENAME}_swtpm' --tpm2 --pcr-banks sha256 --config $(dirname $( which swtpm ) )/../../etc/swtpm_setup.conf ) && \ test -f '${IMAGE_BASENAME}_swtpm/tpm2-00.permall' && \ swtpm socket --tpmstate dir='${IMAGE_BASENAME}_swtpm' \ --ctrl type=unixio,path='${IMAGE_BASENAME}_swtpm/swtpm-sock' \ --flags startup-clear \ --log level=30 --tpm2 -t -d \ " QB_OPT_APPEND += "-chardev socket,id=chrtpm,path='${IMAGE_BASENAME}_swtpm/swtpm-sock' -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis-device,tpmdev=tpm0"
In normal cases swtpm exits when client qemu machine exits. If the qemu machines exits before it connects to the swtpm device, the swtpm process is leaked but I haven't fixed this yet.