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 
parse_cmdline_hw(int argc,char ** argv)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 
hw_is_tpm2(void)65 int hw_is_tpm2(void)
66 {
67     return (hardware_version.hw_version == TPM2_HARDWARE) ? 1 : 0;
68 }
69 
main_loop(void)70 void main_loop(void) {
71    tpmcmd_t* tpmcmd;
72    uint8_t respbuf[TCPA_MAX_BUFFER_LENGTH];
73 
74    while(1) {
75       /* Wait for requests from a vtpm */
76       vtpmloginfo(VTPM_LOG_VTPM, "Waiting for commands from vTPM's:\n");
77       if((tpmcmd = tpmback_req_any()) == NULL) {
78          vtpmlogerror(VTPM_LOG_VTPM, "NULL tpmcmd\n");
79          continue;
80       }
81 
82       tpmcmd->resp = respbuf;
83 
84       /* Process the command */
85       vtpmmgr_handle_cmd(tpmcmd->opaque, tpmcmd);
86 
87       /* Send response */
88       tpmback_resp(tpmcmd);
89    }
90 }
91 
main(int argc,char ** argv)92 int main(int argc, char** argv)
93 {
94    int rc = 0;
95    sleep(2);
96    vtpmloginfo(VTPM_LOG_VTPM, "Starting vTPM manager domain\n");
97 
98     /*Parse TPM hardware in extra command line*/
99     parse_cmdline_hw(argc, argv);
100 
101     /* Initialize the vtpm manager */
102     if (hw_is_tpm2()) {
103         vtpmloginfo(VTPM_LOG_VTPM, "Hardware : --- TPM 2.0 ---\n");
104         if (vtpmmgr2_init(argc, argv) != TPM_SUCCESS) {
105             vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize vtpmmgr domain!\n");
106             rc = -1;
107             goto exit;
108         }
109     }else{
110         vtpmloginfo(VTPM_LOG_VTPM, "Hardware : --- TPM 1.x ---\n");
111         if (vtpmmgr_init(argc, argv) != TPM_SUCCESS) {
112             vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize vtpmmgr domain!\n");
113             rc = -1;
114             goto exit;
115         }
116     }
117 
118    main_loop();
119 
120    vtpmloginfo(VTPM_LOG_VTPM, "vTPM Manager shutting down...\n");
121 
122    vtpmmgr_shutdown();
123 
124 exit:
125    return rc;
126 
127 }
128