3 # For the license, see the LICENSE file in the root directory.
6 ROOT
=${abs_top_builddir:-$(pwd)/..}
7 TESTDIR
=${abs_top_testdir:-$(dirname "$0")}
9 VTPM_NAME
="${VTPM_NAME:-vtpm-test-save-load-state}"
10 SWTPM_DEV_NAME
="/dev/${VTPM_NAME}"
11 export TPM_PATH
="$(mktemp -d)" ||
exit 1
12 STATE_FILE
=$TPM_PATH/tpm-00.permall
13 VOLATILE_STATE_FILE
=$TPM_PATH/tpm-00.volatilestate
14 MY_VOLATILE_STATE_FILE
=$TPM_PATH/my.volatilestate
15 MY_PERMANENT_STATE_FILE
=$TPM_PATH/my.permanent
16 MY_SAVESTATE_STATE_FILE
=$TPM_PATH/my.savestate
17 SWTPM_CMD_UNIX_PATH
=${TPM_PATH}/unix-cmd.sock
18 SWTPM_CTRL_UNIX_PATH
=${TPM_PATH}/unix-ctrl.sock
19 SWTPM_INTERFACE
=${SWTPM_INTERFACE:-cuse}
20 LINEAR_STATE_FILE
=$TPM_PATH/linear-state
23 if [ ${SWTPM_TEST_LINEAR_FILE:-0} -ne 0 ]; then
24 BACKEND_PARAM
="--tpmstate backend-uri=file://$LINEAR_STATE_FILE"
27 logfile
="$(mktemp)" ||
exit 1
32 if [ -n "$pid" ]; then
41 [ "${SWTPM_INTERFACE}" == cuse
] && source ${TESTDIR}/test_cuse
42 source ${TESTDIR}/common
44 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev
/null
46 run_swtpm
${SWTPM_INTERFACE} \
50 display_processes_by_name
"$SWTPM"
52 kill_quiet
-0 ${SWTPM_PID}
54 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
61 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
63 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
69 kill_quiet
-0 ${SWTPM_PID} 2>/dev
/null
71 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
78 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
79 exp
=' 00 c4 00 00 00 0a 00 00 00 00'
80 if [ "$RES" != "$exp" ]; then
81 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
87 run_swtpm_ioctl
${SWTPM_INTERFACE} -h 1234
89 echo "Error: Hash command did not work."
96 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
97 exp
=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
98 if [ "$RES" != "$exp" ]; then
99 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
100 echo "expected: $exp"
101 echo "received: $RES"
105 # Assert physical presence
106 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x40\x00\x00\x0A\x00\x20')
107 exp
=' 00 c4 00 00 00 0a 00 00 00 00'
108 if [ "$RES" != "$exp" ]; then
109 echo "Error: (1) Did not get expected result from TSC_PhysicalPresence(ENABLE)"
110 echo "expected: $exp"
111 echo "received: $RES"
115 # Create a big NVRAM Area with 4000 bytes (0xfa0)
116 tmp
='\x00\xC1\x00\x00\x00\x65\x00\x00\x00\xcc\x00\x18\x00\x00\x00\x01'
117 tmp
+='\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
118 tmp
+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01'
119 tmp
+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
120 tmp
+='\x00\x00\x00\x00\x00\x17\x00\x01\x00\x01\x00\x00\x00\x00\x00\x0f'
121 tmp
+='\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
122 tmp
+='\x00\x00\x00\x00\x00'
123 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} $tmp)
124 exp
=' 00 c4 00 00 00 0a 00 00 00 00'
125 if [ "$RES" != "$exp" ]; then
126 echo "Error: (1) Did not get expected result from TPM_NVDefineSpace()"
127 echo "expected: $exp"
128 echo "received: $RES"
132 # Send SaveState command
133 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0a\x00\x00\x00\x98')
134 exp
=' 00 c4 00 00 00 0a 00 00 00 00'
135 if [ "$RES" != "$exp" ]; then
136 echo "Error: (1) Did not get expected result from TPM_SaveState()"
137 echo "expected: $exp"
138 echo "received: $RES"
142 run_swtpm_ioctl
${SWTPM_INTERFACE} --save permanent
$MY_PERMANENT_STATE_FILE
143 if [ $?
-ne 0 ]; then
144 echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
149 if [ ! -r $MY_PERMANENT_STATE_FILE ]; then
150 echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
155 echo "Saved permanent state."
157 run_swtpm_ioctl
${SWTPM_INTERFACE} --save volatile
$MY_VOLATILE_STATE_FILE
158 if [ $?
-ne 0 ]; then
159 echo "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE."
164 if [ ! -r $MY_VOLATILE_STATE_FILE ]; then
165 echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
170 echo "Saved volatile state."
172 run_swtpm_ioctl
${SWTPM_INTERFACE} --save savestate
$MY_SAVESTATE_STATE_FILE
173 if [ $?
-ne 0 ]; then
174 echo "Error: Could not write savestate state file $MY_SAVESTATE_STATE_FILE."
179 if [ ! -r $MY_SAVESTATE_STATE_FILE ]; then
180 echo "Error: Savestate state file $MY_SAVESTATE_STATE_FILE does not exist."
185 echo "Saved savestate state."
187 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
188 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
190 # we will use our own volatile state
191 rm -f $VOLATILE_STATE_FILE $STATE_FILE
193 # Stop the TPM; this will not shut it down
195 run_swtpm_ioctl
${SWTPM_INTERFACE} --stop
196 if [ $?
-ne 0 ]; then
197 echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM."
203 kill_quiet
-0 ${SWTPM_PID}
204 if [ $?
-ne 0 ]; then
205 echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
211 # load state into the TPM
212 run_swtpm_ioctl
${SWTPM_INTERFACE} --load permanent
$MY_PERMANENT_STATE_FILE
213 if [ $?
-ne 0 ]; then
214 echo "Could not load permanent state into vTPM"
219 echo "Loaded permanent state."
221 run_swtpm_ioctl
${SWTPM_INTERFACE} --load volatile
$MY_VOLATILE_STATE_FILE
222 if [ $?
-ne 0 ]; then
223 echo "Could not load volatile state into vTPM"
228 echo "Loaded volatile state."
230 run_swtpm_ioctl
${SWTPM_INTERFACE} --load savestate
$MY_SAVESTATE_STATE_FILE
231 if [ $?
-ne 0 ]; then
232 echo "Could not load savestate state into vTPM"
237 echo "Loaded savestate state."
239 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
240 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
243 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
244 if [ $?
-ne 0 ]; then
245 echo "TPM Init failed."
251 # Volatile state must have been removed by TPM now
252 if [ -r $VOLATILE_STATE_FILE ]; then
253 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
259 # Read the PCR again ...
260 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
261 exp
=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
262 if [ "$RES" != "$exp" ]; then
263 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
264 echo "expected: $exp"
265 echo "received: $RES"
269 # Save the volatile state again
270 run_swtpm_ioctl
${SWTPM_INTERFACE} -v
271 if [ $?
-ne 0 ]; then
272 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
278 if [ ${SWTPM_TEST_LINEAR_FILE:-0} -eq 0 ] && [ ! -r $VOLATILE_STATE_FILE ]; then
279 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
285 # Send a new TPM_Init
286 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
287 if [ $?
-ne 0 ]; then
288 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
294 # Volatile state must have been removed by TPM now
295 if [ -r $VOLATILE_STATE_FILE ]; then
296 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
302 # Read the PCR again ...
303 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
304 exp
=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
305 if [ "$RES" != "$exp" ]; then
306 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
307 echo "expected: $exp"
308 echo "received: $RES"
314 run_swtpm_ioctl
${SWTPM_INTERFACE} -s
315 if [ $?
-ne 0 ]; then
316 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
322 if wait_process_gone
${SWTPM_PID} 4; then
323 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
329 if [ ${SWTPM_TEST_LINEAR_FILE:-0} -ne 0 ]; then
330 if [ ! -e $LINEAR_STATE_FILE ]; then
331 echo "Error: TPM state file $LINEAR_STATE_FILE does not exist."
337 if [ ! -e $STATE_FILE ]; then
338 echo "Error: TPM state file $STATE_FILE does not exist."