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                 return 1;
48             }
49         } else if (MATCH_OPTION("backend", *argv, oparg)) {
50             replace_string(&vtpm.backend_domname, oparg);
51         } else {
52             fprintf(stderr, "unrecognized argument `%s'\n", *argv);
53             return 1;
54         }
55     }
56 
57     if(dryrun_only) {
58        char* json = libxl_device_vtpm_to_json(ctx, &vtpm);
59        printf("vtpm: %s\n", json);
60        free(json);
61        libxl_device_vtpm_dispose(&vtpm);
62        if (ferror(stdout) || fflush(stdout)) { perror("stdout"); exit(-1); }
63        return 0;
64     }
65 
66     if (libxl_device_vtpm_add(ctx, domid, &vtpm, 0)) {
67         fprintf(stderr, "libxl_device_vtpm_add failed.\n");
68         return 1;
69     }
70     libxl_device_vtpm_dispose(&vtpm);
71     return 0;
72 }
73 
main_vtpmlist(int argc,char ** argv)74 int main_vtpmlist(int argc, char **argv)
75 {
76     int opt;
77     libxl_device_vtpm *vtpms;
78     libxl_vtpminfo vtpminfo;
79     int nb, i;
80 
81     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-list", 1) {
82         /* No options */
83     }
84 
85     /*      Idx  BE   UUID   Hdl  Sta  evch rref  BE-path */
86     printf("%-3s %-2s %-36s %-6s %-5s %-6s %-5s %-10s\n",
87            "Idx", "BE", "Uuid", "handle", "state", "evt-ch", "ring-ref", "BE-path");
88     for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
89         uint32_t domid;
90         if (libxl_domain_qualifier_to_domid(ctx, *argv, &domid) < 0) {
91             fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
92             continue;
93         }
94         if (!(vtpms = libxl_device_vtpm_list(ctx, domid, &nb))) {
95             continue;
96         }
97         for (i = 0; i < nb; ++i) {
98            if(!libxl_device_vtpm_getinfo(ctx, domid, &vtpms[i], &vtpminfo)) {
99               /*      Idx  BE     UUID             Hdl Sta evch rref BE-path*/
100               printf("%-3d %-2d " LIBXL_UUID_FMT " %6d %5d %6d %8d %-30s\n",
101                     vtpminfo.devid, vtpminfo.backend_id,
102                     LIBXL_UUID_BYTES(vtpminfo.uuid),
103                     vtpminfo.devid, vtpminfo.state, vtpminfo.evtch,
104                     vtpminfo.rref, vtpminfo.backend);
105 
106               libxl_vtpminfo_dispose(&vtpminfo);
107            }
108         }
109         libxl_device_vtpm_list_free(vtpms, nb);
110     }
111     return 0;
112 }
113 
main_vtpmdetach(int argc,char ** argv)114 int main_vtpmdetach(int argc, char **argv)
115 {
116     uint32_t domid;
117     int opt, rc=0;
118     libxl_device_vtpm vtpm;
119     libxl_uuid uuid;
120 
121     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-detach", 2) {
122         /* No options */
123     }
124 
125     domid = find_domain(argv[optind]);
126 
127     if ( libxl_uuid_from_string(&uuid, argv[optind+1])) {
128         if (libxl_devid_to_device_vtpm(ctx, domid, atoi(argv[optind+1]), &vtpm)) {
129             fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
130             return 1;
131         }
132     } else {
133         if (libxl_uuid_to_device_vtpm(ctx, domid, &uuid, &vtpm)) {
134             fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
135             return 1;
136         }
137     }
138     rc = libxl_device_vtpm_remove(ctx, domid, &vtpm, 0);
139     if (rc) {
140         fprintf(stderr, "libxl_device_vtpm_remove failed.\n");
141     }
142     libxl_device_vtpm_dispose(&vtpm);
143     return rc;
144 }
145 
146 /*
147  * Local variables:
148  * mode: C
149  * c-basic-offset: 4
150  * indent-tabs-mode: nil
151  * End:
152  */
153