OSDN Git Service

ce422bd2af31ad3c2b57369c1ec10d7477310161
[openpts/openpts.git] / src / rm2dot.c
1 /*
2  * This file is part of the OpenPTS project.
3  *
4  * The Initial Developer of the Original Code is International
5  * Business Machines Corporation. Portions created by IBM
6  * Corporation are Copyright (C) 2010 International Business
7  * Machines Corporation. All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the Common Public License as published by
11  * IBM Corporation; either version 1 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * Common Public License for more details.
18  *
19  * You should have received a copy of the Common Public License
20  * along with this program; if not, a copy can be viewed at
21  * http://www.opensource.org/licenses/cpl1.0.php.
22  */
23
24 /**
25  * \file src/rm2dot.c
26  * \brief Utility, generate dot file from Refdrence Manifest (RM)
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-09-29
29  * cleanup 2011-01-22 SM
30  *
31  *  RM/UML State Diagram -> DOT --(graphviz)--> Graph(PNG,JPG etc)
32  *
33  */
34
35 /*
36
37  cd tests/data/ThinkpadX200_Fedora12
38
39  ../../../src/rm2dot -p 0 -o bios_pcr0.dot platform_rm.xml
40  dot -Tpng bios_pcr0.dot -o bios_pcr0.png
41  eog bios_pcr0.png
42
43  ../../../src/rm2dot -p 4 -o grub_pcr4.dot runtime_rm.xml
44  dot -Tpng grub_pcr4.dot -o grub_pcr4.png
45  eog grub_pcr4.png
46
47 */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <netdb.h>
53 #include <errno.h>
54
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <unistd.h>
59
60 #include <sys/stat.h>
61 #include <fcntl.h>
62
63 #include <openpts.h>
64
65 int verbose = 0; /**< DEBUG  */
66
67 /**
68  * usage
69  */
70 void usage(void) {
71     fprintf(stderr, "usage: rm2dot [options] RMfile \n");
72     fprintf(stderr, "\t-o output\tset output file (default is stdout)\n");
73     fprintf(stderr, "\t-p pcrindex\tset PCR index\n");
74     fprintf(stderr, "\t-l level\tset snapshot level (0 or 1)\n");
75     fprintf(stderr, "\t$ dot -Tpng foo.dot -o foo.png; eog foo.png\n");
76     fprintf(stderr, "\n");
77 }
78
79 /**
80  * main
81  */
82 int main(int argc, char *argv[]) {
83     int rc = -1;
84     int c;
85     OPENPTS_CONFIG *conf;
86     OPENPTS_CONTEXT *ctx;
87     char *input_filename = NULL;
88     char *output_filename = NULL;
89     OPENPTS_SNAPSHOT *ss;
90     int pcr_index = 0;
91     int level = 0;
92
93     verbose = 0;
94
95     while ((c = getopt(argc, argv, "do:p:l:h")) != EOF) {
96         switch (c) {
97         case 'd':
98             verbose = 1;
99
100             break;
101         case 'o':
102             output_filename = optarg;
103             break;
104         case 'p':
105             pcr_index = atoi(optarg);
106             break;
107         case 'l':
108             level = atoi(optarg);
109             break;
110         case 'h':
111             /* fall through */
112         default:
113             usage();
114             return -1;
115         }
116     }
117     argc -= optind;
118     argv += optind;
119     input_filename = argv[0];
120
121     /* Read RM(XML) file */
122
123     if (input_filename == NULL) {
124         printf("ERROR missing XMLfile\n");
125         usage();
126         return -1;
127     }
128
129     /* new pts context */
130     conf = newPtsConfig();
131     if (conf == NULL) {
132         printf("ERROR\n");
133         return -1;
134     }
135
136     ctx = newPtsContext(conf);
137     if (ctx == NULL) {
138         printf("ERROR\n");
139         return -1;
140     }
141
142     /* read RM */
143     rc = readRmFile(ctx, input_filename, 0);
144     if (rc != PTS_SUCCESS) {
145         printf("ERROR readRmFile\n");
146         goto error;
147     }
148
149     if (level == 0) {
150         ss =  getSnapshotFromTable(ctx->ss_table, pcr_index, 0);
151     } else if (level == 1) {
152         ss =  getSnapshotFromTable(ctx->ss_table, pcr_index, 1);
153     } else {
154         printf("ERROR bad level %d\n", level);
155         goto error;
156     }
157
158     rc = writeDotModel(ss->fsm_binary, output_filename);
159     if (rc != PTS_SUCCESS) {
160         printf("ERROR writeDotModel\n");
161         goto error;
162     }
163
164   error:
165     freePtsContext(ctx);
166     freePtsConfig(conf);
167
168     return rc;
169 }