OSDN Git Service

3ca370817f6f808e252962fe9ee3308aafbb81e8
[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 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <unistd.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60
61 #include <openpts.h>
62
63 /**
64  * usage
65  */
66 void usage(void) {
67     fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_RM2DOT_USAGE, "usage: rm2dot [options] RMfile \n"
68                     "\t-o output\tset output file (default is stdout)\n"
69                     "\t-p pcrindex\tset PCR index\n"
70                     "\t-l level\tset snapshot level (0 or 1)\n"
71                     "\t$ dot -Tpng foo.dot -o foo.png; eog foo.png\n"
72                     "\n"));
73 }
74
75 /**
76  * main
77  */
78 int main(int argc, char *argv[]) {
79     int rc = -1;
80     int c;
81     OPENPTS_CONFIG *conf;
82     OPENPTS_CONTEXT *ctx;
83     char *input_filename = NULL;
84     char *output_filename = NULL;
85     OPENPTS_SNAPSHOT *ss;
86     int pcr_index = 0;
87     int level = 0;
88
89     initCatalog();
90
91     while ((c = getopt(argc, argv, "do:p:l:h")) != EOF) {
92         switch (c) {
93         case 'd':
94             setDebugFlags(DEBUG_FLAG);
95             break;
96         case 'o':
97             output_filename = optarg;
98             break;
99         case 'p':
100             pcr_index = atoi(optarg);
101             break;
102         case 'l':
103             level = atoi(optarg);
104             break;
105         case 'h':
106             /* fall through */
107         default:
108             usage();
109             return -1;
110         }
111     }
112     argc -= optind;
113     argv += optind;
114     input_filename = argv[0];
115
116     /* Read RM(XML) file */
117
118     if (input_filename == NULL) {
119         printf(NLS(MS_OPENPTS, OPENPTS_RM2DOT_MISSING_XML_FILE, "ERROR missing XMLfile\n"));
120         usage();
121         return -1;
122     }
123
124     /* new pts context */
125     conf = newPtsConfig();
126     if (conf == NULL) {
127         ERROR("ERROR\n");
128         return -1;
129     }
130
131     ctx = newPtsContext(conf);
132     if (ctx == NULL) {
133         ERROR("ERROR\n");
134         return -1;
135     }
136
137     /* read RM */
138     rc = readRmFile(ctx, input_filename, 0);
139     if (rc != PTS_SUCCESS) {
140         ERROR("ERROR readRmFile\n");
141         goto error;
142     }
143
144     if (level == 0) {
145         ss =  getSnapshotFromTable(ctx->ss_table, pcr_index, 0);
146     } else if (level == 1) {
147         ss =  getSnapshotFromTable(ctx->ss_table, pcr_index, 1);
148     } else {
149         fprintf(stderr, NLS(MS_OPENPTS, OPENPTS_RM2DOT_BAD_LEVEL, "ERROR bad level %d\n"), level);
150         goto error;
151     }
152
153     rc = writeDotModel(ss->fsm_binary, output_filename);
154     if (rc != PTS_SUCCESS) {
155         ERROR("ERROR writeDotModel\n");
156         goto error;
157     }
158
159   error:
160     freePtsContext(ctx);
161     freePtsConfig(conf);
162
163     return rc;
164 }