1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/mman.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <inttypes.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <xenctrl.h>
14 #include <getopt.h>
15
16 #include <xen/platform.h>
17
18 static xc_interface *xch;
19
20 static const char intel_id[] = "GenuineIntel";
21 static const char amd_id[] = "AuthenticAMD";
22
show_curr_cpu(FILE * f)23 static void show_curr_cpu(FILE *f)
24 {
25 int ret;
26 struct xenpf_pcpu_version cpu_ver = { .xen_cpuid = 0 };
27 struct xenpf_ucode_revision ucode_rev = { .cpu = 0 };
28 /* Always exit with 2 when called during usage-info */
29 int exit_code = (f == stderr) ? 2 : 1;
30
31 ret = xc_get_cpu_version(xch, &cpu_ver);
32 if ( ret )
33 {
34 fprintf(stderr, "Failed to get CPU information. (err: %s)\n",
35 strerror(errno));
36 exit(exit_code);
37 }
38
39 ret = xc_get_ucode_revision(xch, &ucode_rev);
40 if ( ret )
41 {
42 fprintf(stderr, "Failed to get microcode information. (err: %s)\n",
43 strerror(errno));
44 exit(exit_code);
45 }
46
47 /*
48 * Print signature in a form that allows to quickly identify which ucode
49 * blob to load, e.g.:
50 *
51 * Intel: /lib/firmware/intel-ucode/06-55-04
52 * AMD: /lib/firmware/amd-ucode/microcode_amd_fam19h.bin
53 */
54 if ( memcmp(cpu_ver.vendor_id, intel_id,
55 sizeof(cpu_ver.vendor_id)) == 0 )
56 {
57 fprintf(f,
58 "CPU signature %02x-%02x-%02x (raw 0x%08x) pf %#x revision 0x%08x\n",
59 cpu_ver.family, cpu_ver.model, cpu_ver.stepping,
60 ucode_rev.signature, ucode_rev.pf, ucode_rev.revision);
61 }
62 else if ( memcmp(cpu_ver.vendor_id, amd_id,
63 sizeof(cpu_ver.vendor_id)) == 0 )
64 {
65 fprintf(f,
66 "CPU signature %02x-%02x-%02x (raw 0x%08x) revision 0x%08x\n",
67 cpu_ver.family, cpu_ver.model, cpu_ver.stepping,
68 ucode_rev.signature, ucode_rev.revision);
69 }
70 else
71 {
72 fprintf(f, "Unsupported CPU vendor: %s\n", cpu_ver.vendor_id);
73 exit(exit_code);
74 }
75 }
76
usage(FILE * stream,const char * name)77 static void usage(FILE *stream, const char *name)
78 {
79 fprintf(stream,
80 "%s: Xen microcode updating tool\n"
81 "Usage: %s [options] [<microcode file> | show-cpu-info]\n"
82 "options:\n"
83 " -h, --help display this help\n"
84 " -s, --show-cpu-info show CPU information\n"
85 " -f, --force skip certain checks when applying\n"
86 " microcode; do not use unless you know\n"
87 " exactly what you are doing\n",
88 name, name);
89 show_curr_cpu(stream);
90 }
91
main(int argc,char * argv[])92 int main(int argc, char *argv[])
93 {
94 static const struct option options[] = {
95 { "help", no_argument, NULL, 'h' },
96 { "show-cpu-info", no_argument, NULL, 's' },
97 { "force", no_argument, NULL, 'f' },
98 {}
99 };
100
101 int fd, ret;
102 char *filename, *buf;
103 size_t len;
104 struct stat st;
105 int opt;
106 uint32_t ucode_flags = 0;
107
108 xch = xc_interface_open(NULL, NULL, 0);
109 if ( xch == NULL )
110 {
111 fprintf(stderr, "Error opening xc interface. (err: %s)\n",
112 strerror(errno));
113 exit(1);
114 }
115
116 while ( (opt = getopt_long(argc, argv, "hsf", options, NULL)) != -1 )
117 {
118 switch ( opt )
119 {
120 case 'h':
121 usage(stdout, argv[0]);
122 exit(EXIT_SUCCESS);
123
124 case 's':
125 show_curr_cpu(stdout);
126 exit(EXIT_SUCCESS);
127
128 case 'f':
129 ucode_flags |= XENPF_UCODE_FORCE;
130 break;
131
132 default:
133 fprintf(stderr, "%s: unknown option\n", argv[0]);
134 goto ext_err;
135 }
136 }
137
138 if ( optind == argc )
139 {
140 fprintf(stderr, "%s: missing microcode file\n", argv[0]);
141 goto ext_err;
142 }
143
144 /* For backwards compatibility to the pre-getopt() cmdline handling */
145 if ( !strcmp(argv[optind], "show-cpu-info") )
146 {
147 show_curr_cpu(stdout);
148 return 0;
149 }
150
151 filename = argv[optind];
152 fd = open(filename, O_RDONLY);
153 if ( fd < 0 )
154 {
155 fprintf(stderr, "Could not open %s. (err: %s)\n",
156 filename, strerror(errno));
157 exit(1);
158 }
159
160 if ( fstat(fd, &st) != 0 )
161 {
162 fprintf(stderr, "Could not get the size of %s. (err: %s)\n",
163 filename, strerror(errno));
164 exit(1);
165 }
166
167 len = st.st_size;
168 buf = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
169 if ( buf == MAP_FAILED )
170 {
171 fprintf(stderr, "mmap failed. (error: %s)\n", strerror(errno));
172 exit(1);
173 }
174
175 errno = 0;
176 ret = xc_microcode_update(xch, buf, len, ucode_flags);
177 if ( ret == -1 && errno == EEXIST )
178 printf("Microcode already up to date\n");
179 else if ( ret )
180 {
181 fprintf(stderr, "Failed to update microcode. (err: %s)\n",
182 strerror(errno));
183 exit(1);
184 }
185
186 xc_interface_close(xch);
187
188 if ( munmap(buf, len) )
189 {
190 printf("Could not unmap: %d(%s)\n", errno, strerror(errno));
191 exit(1);
192 }
193 close(fd);
194
195 return 0;
196
197 ext_err:
198 usage(stderr, argv[0]);
199 exit(EXIT_FAILURE);
200 }
201