1 /*
2 * The Initial Developer of the Original Code is International
3 * Business Machines Corporation. Portions created by IBM
4 * Corporation are Copyright (C) 2005 International Business
5 * Machines Corporation. All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the Common Public License as published by
9 * IBM Corporation; either version 1 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * Common Public License for more details.
16 *
17 * You should have received a copy of the Common Public License
18 * along with this program; if not, a copy can be viewed at
19 * http://www.opensource.org/licenses/cpl1.0.php.
20 */
21 #include <getopt.h>
22 #include <stdlib.h> //for def. exit
23 #include "tpm_tspi.h"
24
25
26 //Controlled by input options
27 static int resultFlag = FALSE;
28 TSS_HCONTEXT hContext = 0;
29
selfTestFull(TSS_HTPM a_hTpm)30 TSS_RESULT selfTestFull(TSS_HTPM a_hTpm)
31 {
32 TSS_RESULT result = Tspi_TPM_SelfTestFull(a_hTpm);
33 tspiResult("Tspi_TPM_SelfTestFull", result);
34
35 return result;
36 }
37
selfTestResult(TSS_HTPM a_hTpm,UINT32 * a_uiResultLen,BYTE ** a_pResult)38 TSS_RESULT selfTestResult(TSS_HTPM a_hTpm,
39 UINT32 * a_uiResultLen, BYTE ** a_pResult)
40 {
41
42 TSS_RESULT result = Tspi_TPM_GetTestResult(a_hTpm, a_uiResultLen,
43 a_pResult);
44 tspiResult("Tspi_TPM_GetTestResult", result);
45
46 return result;
47 }
48
cmdSelfTest(const char * a_szCmd)49 int cmdSelfTest(const char *a_szCmd)
50 {
51 TSS_HTPM hTpm;
52 UINT32 uiResultLen;
53 BYTE *pResult;
54 int iRc = -1;
55
56 if (contextCreate(&hContext) != TSS_SUCCESS)
57 goto out;
58
59 if (contextConnect(hContext) != TSS_SUCCESS)
60 goto out_close;
61
62 if (contextGetTpm(hContext, &hTpm) != TSS_SUCCESS)
63 goto out_close;
64
65 if ((!resultFlag) && (selfTestFull(hTpm) != TSS_SUCCESS))
66 goto out_close;
67
68 if (selfTestResult(hTpm, &uiResultLen, &pResult) != TSS_SUCCESS)
69 goto out_close;
70 logMsg(_(" TPM Test Results: "));
71 logHex(uiResultLen, pResult);
72
73 iRc = 0;
74 logSuccess(a_szCmd);
75
76 out_close:
77 contextClose(hContext);
78
79 out:
80 return iRc;
81 }
82
selftestUsage(const char * a_szCmd)83 void selftestUsage(const char *a_szCmd)
84 {
85 logCmdHelp(a_szCmd);
86 logCmdOption("-r, --results", _("Report results of last test without retesting."));
87 }
88
parse(const int aOpt,const char * aArg)89 static int parse(const int aOpt, const char *aArg)
90 {
91
92 switch (aOpt) {
93 case 'r':
94 logDebug(_("Results only\n"));
95 resultFlag = TRUE;
96 break;
97 default:
98 return -1;
99 }
100 return 0;
101 }
102
103
main(int argc,char * argv[])104 int main(int argc, char *argv[])
105 {
106 int rc;
107 struct option opts[] = {
108 {"results", no_argument, NULL, 'r'},
109 };
110
111 initIntlSys();
112
113 rc = genericOptHandler(argc, argv, "r", opts,
114 sizeof(opts) / sizeof(struct option), parse,
115 selftestUsage);
116 if (rc)
117 exit(0);
118
119 rc = cmdSelfTest(argv[0]);
120
121 return rc;
122 }
123