|
11 | 11 | import os |
12 | 12 | import re |
13 | 13 | import datetime |
| 14 | +import threading |
14 | 15 | from pathlib import Path |
15 | 16 | from edk2toolext.environment.plugintypes import uefi_helper_plugin |
16 | 17 | from edk2toollib import utility_functions |
@@ -43,6 +44,24 @@ def QueryQemuVersion(exec): |
43 | 44 | return ver_str.split('.') |
44 | 45 |
|
45 | 46 |
|
| 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 | + |
46 | 65 | @staticmethod |
47 | 66 | def Runner(env): |
48 | 67 | ''' Runs QEMU ''' |
@@ -109,10 +128,16 @@ def Runner(env): |
109 | 128 | code_fd + ",readonly=on" |
110 | 129 |
|
111 | 130 | tpm_dev = env.GetValue("TPM_DEV") |
| 131 | + thread = None |
112 | 132 | if tpm_dev is not None: |
113 | 133 | args += f" -chardev socket,id=chrtpm,path={tpm_dev}" |
114 | 134 | args += " -tpmdev emulator,id=tpm0,chardev=chrtpm" |
115 | 135 |
|
| 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 | + |
116 | 141 | # Add XHCI USB controller and mouse |
117 | 142 | args += " -device qemu-xhci,id=usb" |
118 | 143 | args += " -device usb-tablet,id=input0,bus=usb.0,port=1" # add a usb mouse |
@@ -179,4 +204,8 @@ def Runner(env): |
179 | 204 | # Linux version of QEMU will mess with the print if its run failed, let's just restore it anyway |
180 | 205 | utility_functions.RunCmd('stty', 'sane', capture=False) |
181 | 206 |
|
| 207 | + if thread is not None: |
| 208 | + logging.critical("Terminate TPM emulator by using Crtl + C now!") |
| 209 | + thread.join() |
| 210 | + |
182 | 211 | return ret |
0 commit comments