| 1 |
/* |
| 2 |
* Copyright (c) 2010-2012 United States Government, as represented by |
| 3 |
* the Secretary of Defense. All rights reserved. |
| 4 |
* |
| 5 |
* based off of the original tools/vtpm_manager code base which is: |
| 6 |
* Copyright (c) 2005, Intel Corp. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted provided that the following conditions |
| 11 |
* are met: |
| 12 |
* |
| 13 |
* * Redistributions of source code must retain the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer. |
| 15 |
* * Redistributions in binary form must reproduce the above |
| 16 |
* copyright notice, this list of conditions and the following |
| 17 |
* disclaimer in the documentation and/or other materials provided |
| 18 |
* with the distribution. |
| 19 |
* * Neither the name of Intel Corporation nor the names of its |
| 20 |
* contributors may be used to endorse or promote products derived |
| 21 |
* from this software without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 30 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 32 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 34 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <stdint.h> |
| 38 |
#include <mini-os/tpmback.h> |
| 39 |
#include <unistd.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <string.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include "log.h" |
| 44 |
|
| 45 |
#include "vtpmmgr.h" |
| 46 |
#include "tcg.h" |
| 47 |
|
| 48 |
struct tpm_hardware_version hardware_version = { |
| 49 |
.hw_version = TPM1_HARDWARE, |
| 50 |
}; |
| 51 |
|
| 52 |
int parse_cmdline_hw(int argc, char** argv) |
| 53 |
{ |
| 54 |
int i; |
| 55 |
|
| 56 |
for (i = 1; i < argc; ++i) { |
| 57 |
if (!strcmp(argv[i], TPM2_EXTRA_OPT)) { |
| 58 |
hardware_version.hw_version = TPM2_HARDWARE; |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
int hw_is_tpm2(void) |
| 66 |
{ |
| 67 |
return (hardware_version.hw_version == TPM2_HARDWARE) ? 1 : 0; |
| 68 |
} |
| 69 |
|
| 70 |
static int do_shutdown; |
| 71 |
|
| 72 |
void app_shutdown(unsigned int reason) |
| 73 |
{ |
| 74 |
printk("Shutdown requested: %d\n", reason); |
| 75 |
do_shutdown = 1; |
| 76 |
|
| 77 |
shutdown_tpmback(); |
| 78 |
} |
| 79 |
|
| 80 |
void main_loop(void) { |
| 81 |
tpmcmd_t* tpmcmd; |
| 82 |
uint8_t respbuf[TCPA_MAX_BUFFER_LENGTH]; |
| 83 |
|
| 84 |
while (!do_shutdown) { |
| 85 |
/* Wait for requests from a vtpm */ |
| 86 |
vtpmloginfo(VTPM_LOG_VTPM, "Waiting for commands from vTPM's:\n"); |
| 87 |
if((tpmcmd = tpmback_req_any()) == NULL) { |
| 88 |
vtpmlogerror(VTPM_LOG_VTPM, "NULL tpmcmd\n"); |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
tpmcmd->resp = respbuf; |
| 93 |
|
| 94 |
/* Process the command */ |
| 95 |
vtpmmgr_handle_cmd(tpmcmd->opaque, tpmcmd); |
| 96 |
|
| 97 |
/* Send response */ |
| 98 |
tpmback_resp(tpmcmd); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
int main(int argc, char** argv) |
| 103 |
{ |
| 104 |
int rc = 0; |
| 105 |
sleep(2); |
| 106 |
vtpmloginfo(VTPM_LOG_VTPM, "Starting vTPM manager domain\n"); |
| 107 |
|
| 108 |
/*Parse TPM hardware in extra command line*/ |
| 109 |
parse_cmdline_hw(argc, argv); |
| 110 |
|
| 111 |
/* Initialize the vtpm manager */ |
| 112 |
if (hw_is_tpm2()) { |
| 113 |
vtpmloginfo(VTPM_LOG_VTPM, "Hardware : --- TPM 2.0 ---\n"); |
| 114 |
if (vtpmmgr2_init(argc, argv) != TPM_SUCCESS) { |
| 115 |
vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize vtpmmgr domain!\n"); |
| 116 |
rc = -1; |
| 117 |
goto exit; |
| 118 |
} |
| 119 |
}else{ |
| 120 |
vtpmloginfo(VTPM_LOG_VTPM, "Hardware : --- TPM 1.x ---\n"); |
| 121 |
if (vtpmmgr_init(argc, argv) != TPM_SUCCESS) { |
| 122 |
vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize vtpmmgr domain!\n"); |
| 123 |
rc = -1; |
| 124 |
goto exit; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
main_loop(); |
| 129 |
|
| 130 |
vtpmloginfo(VTPM_LOG_VTPM, "vTPM Manager shutting down...\n"); |
| 131 |
|
| 132 |
vtpmmgr_shutdown(); |
| 133 |
|
| 134 |
exit: |
| 135 |
return rc; |
| 136 |
|
| 137 |
} |