Skip to content

Commit d771b92

Browse files
committed
make it easier
1 parent 63d865c commit d771b92

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Platforms/QemuSbsaPkg/Plugins/QemuRunner/QemuRunner.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import re
1313
import datetime
14+
import threading
1415
from pathlib import Path
1516
from edk2toolext.environment.plugintypes import uefi_helper_plugin
1617
from edk2toollib import utility_functions
@@ -43,6 +44,24 @@ def QueryQemuVersion(exec):
4344
return ver_str.split('.')
4445

4546

47+
@staticmethod
48+
def RunThread(env):
49+
''' Runs TPM in a separate thread '''
50+
tpm_path = env.GetValue("TPM_DEV")
51+
if tpm_path is None:
52+
logging.critical("TPM Path Invalid")
53+
return
54+
55+
tpm_cmd = "swtpm"
56+
tpm_args = f"socket --tpmstate dir={"/".join(tpm_path.rsplit("/", 1)[:-1])} --ctrl type=unixio,path={tpm_path} --tpm2 --log level=20"
57+
58+
# Start the TPM emulator in a separate thread
59+
ret = utility_functions.RunCmd(tpm_cmd, tpm_args)
60+
if ret != 0:
61+
logging.critical("Failed to start TPM emulator.")
62+
return
63+
64+
4665
@staticmethod
4766
def Runner(env):
4867
''' Runs QEMU '''
@@ -109,10 +128,16 @@ def Runner(env):
109128
code_fd + ",readonly=on"
110129

111130
tpm_dev = env.GetValue("TPM_DEV")
131+
thread = None
112132
if tpm_dev is not None:
113133
args += f" -chardev socket,id=chrtpm,path={tpm_dev}"
114134
args += " -tpmdev emulator,id=tpm0,chardev=chrtpm"
115135

136+
# also spawn the TPM emulator on a different thread
137+
logging.critical("Starting TPM emulator in a different thread.")
138+
thread = threading.Thread(target=QemuRunner.RunThread, args=(env,))
139+
thread.start()
140+
116141
# Add XHCI USB controller and mouse
117142
args += " -device qemu-xhci,id=usb"
118143
args += " -device usb-tablet,id=input0,bus=usb.0,port=1" # add a usb mouse
@@ -179,4 +204,8 @@ def Runner(env):
179204
# Linux version of QEMU will mess with the print if its run failed, let's just restore it anyway
180205
utility_functions.RunCmd('stty', 'sane', capture=False)
181206

207+
if thread is not None:
208+
logging.critical("Terminate TPM emulator by using Crtl + C now!")
209+
thread.join()
210+
182211
return ret

0 commit comments

Comments
 (0)