1 /*
2  * Copyright 2009-2017 Citrix Ltd and other contributors
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include <stdlib.h>
16 
17 #include <libxl.h>
18 #include <libxl_utils.h>
19 #include <libxlutil.h>
20 
21 #include "xl.h"
22 #include "xl_utils.h"
23 #include "xl_parse.h"
24 
main_vtpmattach(int argc,char ** argv)25 int main_vtpmattach(int argc, char **argv)
26 {
27     int opt;
28     libxl_device_vtpm vtpm;
29     char *oparg;
30     uint32_t domid;
31 
32     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-attach", 1) {
33         /* No options */
34     }
35 
36     if (libxl_domain_qualifier_to_domid(ctx, argv[optind], &domid) < 0) {
37         fprintf(stderr, "%s is an invalid domain identifier\n", argv[optind]);
38         return 1;
39     }
40     ++optind;
41 
42     libxl_device_vtpm_init(&vtpm);
43     for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) {
44         if (MATCH_OPTION("uuid", *argv, oparg)) {
45             if(libxl_uuid_from_string(&(vtpm.uuid), oparg)) {
46                 fprintf(stderr, "Invalid uuid specified (%s)\n", oparg);
47                 libxl_device_vtpm_dispose(&vtpm);
48                 return 1;
49             }
50         } else if (MATCH_OPTION("backend", *argv, oparg)) {
51             replace_string(&vtpm.backend_domname, oparg);
52         } else {
53             fprintf(stderr, "unrecognized argument `%s'\n", *argv);
54             libxl_device_vtpm_dispose(&vtpm);
55             return 1;
56         }
57     }
58 
59     if(dryrun_only) {
60        char* json = libxl_device_vtpm_to_json(ctx, &vtpm);
61        printf("vtpm: %s\n", json);
62        free(json);
63        libxl_device_vtpm_dispose(&vtpm);
64        if (ferror(stdout) || fflush(stdout)) { perror("stdout"); exit(-1); }
65        return 0;
66     }
67 
68     if (libxl_device_vtpm_add(ctx, domid, &vtpm, 0)) {
69         fprintf(stderr, "libxl_device_vtpm_add failed.\n");
70         libxl_device_vtpm_dispose(&vtpm);
71         return 1;
72     }
73     libxl_device_vtpm_dispose(&vtpm);
74     return 0;
75 }
76 
main_vtpmlist(int argc,char ** argv)77 int main_vtpmlist(int argc, char **argv)
78 {
79     int opt;
80     libxl_device_vtpm *vtpms;
81     libxl_vtpminfo vtpminfo;
82     int nb, i;
83 
84     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-list", 1) {
85         /* No options */
86     }
87 
88     /*      Idx  BE   UUID   Hdl  Sta  evch rref  BE-path */
89     printf("%-3s %-2s %-36s %-6s %-5s %-6s %-5s %-10s\n",
90            "Idx", "BE", "Uuid", "handle", "state", "evt-ch", "ring-ref", "BE-path");
91     for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
92         uint32_t domid;
93         if (libxl_domain_qualifier_to_domid(ctx, *argv, &domid) < 0) {
94             fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
95             continue;
96         }
97         if (!(vtpms = libxl_device_vtpm_list(ctx, domid, &nb))) {
98             continue;
99         }
100         for (i = 0; i < nb; ++i) {
101            if(!libxl_device_vtpm_getinfo(ctx, domid, &vtpms[i], &vtpminfo)) {
102               /*      Idx  BE     UUID             Hdl Sta evch rref BE-path*/
103               printf("%-3d %-2d " LIBXL_UUID_FMT " %6d %5d %6d %8d %-30s\n",
104                     vtpminfo.devid, vtpminfo.backend_id,
105                     LIBXL_UUID_BYTES(vtpminfo.uuid),
106                     vtpminfo.devid, vtpminfo.state, vtpminfo.evtch,
107                     vtpminfo.rref, vtpminfo.backend);
108 
109               libxl_vtpminfo_dispose(&vtpminfo);
110            }
111         }
112         libxl_device_vtpm_list_free(vtpms, nb);
113     }
114     return 0;
115 }
116 
main_vtpmdetach(int argc,char ** argv)117 int main_vtpmdetach(int argc, char **argv)
118 {
119     uint32_t domid;
120     int opt, rc=0;
121     libxl_device_vtpm vtpm;
122     libxl_uuid uuid;
123 
124     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-detach", 2) {
125         /* No options */
126     }
127 
128     domid = find_domain(argv[optind]);
129 
130     if ( libxl_uuid_from_string(&uuid, argv[optind+1])) {
131         if (libxl_devid_to_device_vtpm(ctx, domid, atoi(argv[optind+1]), &vtpm)) {
132             fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
133             return 1;
134         }
135     } else {
136         if (libxl_uuid_to_device_vtpm(ctx, domid, &uuid, &vtpm)) {
137             fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
138             return 1;
139         }
140     }
141     rc = libxl_device_vtpm_remove(ctx, domid, &vtpm, 0);
142     if (rc) {
143         fprintf(stderr, "libxl_device_vtpm_remove failed.\n");
144     }
145     libxl_device_vtpm_dispose(&vtpm);
146     return rc;
147 }
148 
149 /*
150  * Local variables:
151  * mode: C
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * End:
155  */
156