2 # ===================================================================
4 # Copyright (c) 2005, Intel Corp.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Intel Corporation nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 # OF THE POSSIBILITY OF SUCH DAMAGE.
33 # ===================================================================
35 # | SRC | TAG | CMD SIZE | ORD |mtype|strt
36 TPM_CMD_OPEN=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x01
37 TPM_CMD_RESM=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x02
38 TPM_CMD_CLOS=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x02
39 TPM_CMD_DELE=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x03
46 TX_VTPM_MANAGER=/var/vtpm/fifos/from_console.fifo
47 RX_VTPM_MANAGER=/var/vtpm/fifos/to_console.fifo
49 VTPM_MIG=/usr/bin/vtpm_migrator
51 # -------------------- Helpers for binary streams -----------
53 function str_to_hex32() {
57 function hex32_to_bin() {
58 local inst=$(str_to_hex32 $1);
60 local n1=`echo $inst | sed 's/\(..\)....../\\\\x\1/'`
61 local n2=`echo $inst | sed 's/..\(..\)..../\\\\x\1/'`
62 local n3=`echo $inst | sed 's/....\(..\)../\\\\x\1/'`
63 local n4=`echo $inst | sed 's/......\(..\)/\\\\x\1/'`
68 function vtpm_manager_cmd() {
71 local inst_bin=$(hex32_to_bin $inst);
75 #send cmd to vtpm_manager
76 printf "$cmd$inst_bin" > $TX_VTPM_MANAGER
80 local resp_hex=`dd skip=10 bs=1 count=4 if=$RX_VTPM_MANAGER 2> /dev/null | xxd -ps`
85 #return whether the command was successful
86 if [ $resp_hex -ne $TPM_SUCCESS ]; then
94 # Helper to get vm type to pass to vtpm_manager open/resume
95 function vtpm_get_type() {
96 local inst=$(xenstore_read $XENBUS_PATH/frontend-id)
97 local vm=$(xenstore_read /local/domain/$inst/vm)
98 if [ "$vm" != "" ]; then
99 local ostype=$(xenstore-read $vm/image/ostype)
100 if [ "$ostype" == "hvm" ]; then
108 # ------------------ Command handlers -----------------
110 # Create new vtpm instance & set it up for use
111 function vtpm_create () {
112 # Creation is handled implicitly by the manager on first setup
113 # so just set it up for use
117 # Setup vtpm instance for use.
118 function vtpm_start() {
119 local vmtype=$(vtpm_get_type);
120 $(vtpm_manager_cmd $TPM_CMD_OPEN$vmtype $1)
123 function vtpm_resume() {
124 local vmtype=$(vtpm_get_type);
125 $(vtpm_manager_cmd $TPM_CMD_RESM$vmtype $1)
128 # Reset the vtpm AKA clear PCRs
129 function vtpm_reset() {
130 #not used by current implemenation
134 # Shutdown the vtpm while the vm is down
135 # This could be a suspend of shutdown
136 # we cannot distinquish, so save the state
137 # and decide on startup if we should keep is
138 function vtpm_suspend() {
139 $(vtpm_manager_cmd $TPM_CMD_CLOS $1)
143 function vtpm_delete() {
145 if $(vtpm_manager_cmd $TPM_CMD_DELE $inst); then
146 rm -f /var/vtpm/vtpm_dm_$1.data
154 # Perform a migration step. This function differentiates between migration
155 # to the local host or to a remote machine.
157 # 1st: destination host to migrate to
158 # 2nd: name of the domain to migrate
159 # 3rd: the migration step to perform
160 function vtpm_migrate() {
163 instance=$(vtpmdb_find_instance $2)
164 if [ "$instance" == "" ]; then
165 log err "VTPM Migratoin failed. Unable to translation of domain name"
166 echo "Error: VTPM Migration failed while looking up instance number"
171 #Incicate migration supported
176 # Get Public Key from Destination
177 # Call vtpm_manager's migration part 1
179 $VTPM_MIG $1 $2 $instance $3
180 release_lock vtpm_mgr
184 # Call manager's migration step 2 and send result to destination
185 # If successful remove from db
187 $VTPM_MIG $1 $2 $instance $3
188 release_lock vtpm_mgr
192 if `ps x | grep "$VTPM_MIG $1"`; then
193 log err "VTPM Migration failed to complete."
194 echo "Error: VTPM Migration failed to complete."
202 function vtpm_migrate_recover() {
203 echo "Error: Recovery not supported yet"
206 function vtpm_migrate_local() {
207 echo "Error: local vTPM migration not supported"