xref: /netbsd-src/crypto/external/cpl/tpm-tools/dist/src/tpm_mgmt/tpm_reset.c (revision 431955c163a358f3111f7be0c1fa1643cab0b701)
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 <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 reset[] = {
33 		0, 193,		/* TPM_TAG_RQU_COMMAND */
34 		0, 0, 0, 10,	/* length */
35 		0, 0, 0, 90	/* TPM_ORD_Reset */
36 	};
37 
38 	int fd;
39 	int err;
40 	int rc = 1;
41 
42 	fd = open( "/dev/tpm0", O_RDWR );
43 	if ( fd < 0 ) {
44 		printf( "Unable to open the device.\n" );
45 		goto out;
46 	}
47 
48 	err = write( fd, reset, sizeof(reset) );
49 
50 	if ( err != sizeof( reset ) ){
51 		printf( "Error occured while writing the reset command: %d\n", errno );
52 		goto out;
53 	}
54 
55 	err = read( fd, reset, sizeof(reset) );
56 	if ( err != 10 ) {
57 		printf( "Error occured while reading the reset result: %d %d %s\n", err, errno, strerror(errno));
58 		goto out;
59 	}
60 
61 	err = ntohl( *((uint32_t *)(reset+6)) );
62 	if ( err == 38 ) {
63 		printf( "TPM not started.\n" );
64 		goto out;
65 	}
66 
67 	if ( err != 0 ) {
68 		printf( "TPM Error occured: %d\n", err );
69 		goto out;
70 	}
71 
72 	rc = 0;
73 	printf( "Reset successful\n" );
74 
75 out:
76 	close(fd);
77         return rc;
78 }
79 
80