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
main(int argc,char * argv[])15 int main(int argc, char *argv[])
16 {
17 int fd, ret;
18 char *filename, *buf;
19 size_t len;
20 struct stat st;
21 xc_interface *xch;
22
23 if ( argc < 2 )
24 {
25 fprintf(stderr,
26 "xen-ucode: Xen microcode updating tool\n"
27 "Usage: %s <microcode blob>\n", argv[0]);
28 exit(2);
29 }
30
31 filename = argv[1];
32 fd = open(filename, O_RDONLY);
33 if ( fd < 0 )
34 {
35 fprintf(stderr, "Could not open %s. (err: %s)\n",
36 filename, strerror(errno));
37 exit(1);
38 }
39
40 if ( fstat(fd, &st) != 0 )
41 {
42 fprintf(stderr, "Could not get the size of %s. (err: %s)\n",
43 filename, strerror(errno));
44 exit(1);
45 }
46
47 len = st.st_size;
48 buf = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
49 if ( buf == MAP_FAILED )
50 {
51 fprintf(stderr, "mmap failed. (error: %s)\n", strerror(errno));
52 exit(1);
53 }
54
55 xch = xc_interface_open(NULL, NULL, 0);
56 if ( xch == NULL )
57 {
58 fprintf(stderr, "Error opening xc interface. (err: %s)\n",
59 strerror(errno));
60 exit(1);
61 }
62
63 ret = xc_microcode_update(xch, buf, len);
64 if ( ret )
65 {
66 fprintf(stderr, "Failed to update microcode. (err: %s)\n",
67 strerror(errno));
68 exit(1);
69 }
70
71 xc_interface_close(xch);
72
73 if ( munmap(buf, len) )
74 {
75 printf("Could not unmap: %d(%s)\n", errno, strerror(errno));
76 exit(1);
77 }
78 close(fd);
79
80 return 0;
81 }
82