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, 2006 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 <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <netinet/in.h>
29
main()30 int main() {
31
32 char startup[] = {
33 0, 193, /* TPM_TAG_RQU_COMMAND */
34 0, 0, 0, 12, /* length */
35 0, 0, 0, 153, /* TPM_ORD_Startup */
36 0, 1 /* ST_CLEAR */
37 };
38
39 char selftest[] = {
40 0, 193, /* TPM_TAG_RQU_COMMAND */
41 0, 0, 0, 10, /* length */
42 0, 0, 0, 80 /* TPM_ORD_SelfTestFull */
43 };
44
45 int fd;
46 int err;
47 int rc = 1;
48
49 fd = open( "/dev/tpm0", O_RDWR );
50 if ( fd < 0 ) {
51 printf( "Unable to open the device.\n" );
52 goto out_noclose;
53 }
54
55 err = write( fd, startup, sizeof(startup) );
56
57 if ( err != sizeof( startup ) ){
58 printf( "Error occured while writing the startup command: %d\n", errno );
59 goto out;
60 }
61
62 err = read( fd, startup, sizeof(startup) );
63 if ( err != 10 ) {
64 printf( "Error occured while reading the startup result: %d %d %s\n", err, errno, strerror(errno));
65 goto out;
66 }
67
68 err = ntohl( *((uint32_t *)(startup+6)) );
69 if ( err == 38 ) {
70 printf( "TPM already started.\n" );
71 goto out;
72 }
73
74 if ( err != 0 ) {
75 printf( "TPM Error occured: %d\n", err );
76 goto out;
77 }
78
79 rc = 0;
80 printf( "Startup successful\n" );
81
82 out:
83 err = write( fd, selftest, sizeof(selftest) );
84
85 if ( err != sizeof( selftest ) ){
86 printf( "Error occured while writing the selftest command: %d\n", errno );
87 goto out;
88 }
89
90 err = read( fd, selftest, sizeof(selftest) );
91 if ( err != 10 ) {
92 printf( "Error occured while reading the selftest result: %d %d %s\n", err, errno, strerror(errno));
93 goto out;
94 }
95
96 err = ntohl( *((uint32_t *)(selftest+6)) );
97 if ( err != 0 ) {
98 printf( "TPM Error occured: %d\n", err );
99 goto out;
100 } else {
101 rc = 0;
102 printf( "Selftest successful\n" );
103 }
104
105 close(fd);
106
107 out_noclose:
108 return rc;
109 }
110
111