1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (C) 2018 The FreeBSD Foundation.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 static const size_t bufsize = 65536;
38
39 /* SDM vol 3 9.11.1 Intel microcode header. */
40 struct microcode_update_header {
41 uint32_t header_version;
42 uint32_t update_revision;
43 uint32_t date; /* BCD mmddyyyy */
44 uint32_t processor_signature;
45 uint32_t checksum; /* Over update data and header */
46 uint32_t loader_revision;
47 uint32_t processor_flags;
48 uint32_t data_size;
49 uint32_t total_size;
50 uint32_t reserved[3];
51 };
52
53 /*
54 * SDM vol 2A CPUID EAX = 01h Returns Model, Family, Stepping Information.
55 * Caller must free the returned string.
56 */
57
58 static char *
format_signature(uint32_t signature)59 format_signature(uint32_t signature)
60 {
61 char *buf;
62 unsigned family, model, stepping;
63
64 family = (signature & 0xf00) >> 8;
65 model = (signature & 0xf0) >> 4;
66 stepping = signature & 0xf;
67 if (family == 0x06 || family == 0x0f)
68 model += (signature & 0xf0000) >> 12;
69 if (family == 0x0f)
70 family += (signature & 0xff00000) >> 20;
71 asprintf(&buf, "%02x-%02x-%02x", family, model, stepping);
72 if (buf == NULL)
73 err(1, "asprintf");
74 return (buf);
75 }
76
77 static void
dump_header(const struct microcode_update_header * hdr)78 dump_header(const struct microcode_update_header *hdr)
79 {
80 char *sig_str;
81 int i;
82 bool platformid_printed;
83
84 sig_str = format_signature(hdr->processor_signature);
85 printf("header version\t0x%x\n", hdr->header_version);
86 printf("revision\t0x%x\n", hdr->update_revision);
87 printf("date\t\t0x%x\t%04x-%02x-%02x\n", hdr->date,
88 hdr->date & 0xffff, (hdr->date & 0xff000000) >> 24,
89 (hdr->date & 0xff0000) >> 16);
90 printf("signature\t0x%x\t\t%s\n", hdr->processor_signature, sig_str);
91 printf("checksum\t0x%x\n", hdr->checksum);
92 printf("loader revision\t0x%x\n", hdr->loader_revision);
93 printf("processor flags\t0x%x", hdr->processor_flags);
94 platformid_printed = false;
95 for (i = 0; i < 8; i++) {
96 if (hdr->processor_flags & 1 << i) {
97 printf("%s%d", platformid_printed ? ", " : "\t\t", i);
98 platformid_printed = true;
99 }
100 }
101 printf("\n");
102 printf("datasize\t0x%x\t\t0x%x\n", hdr->data_size,
103 hdr->data_size != 0 ? hdr->data_size : 2000);
104 printf("size\t\t0x%x\t\t0x%x\n", hdr->total_size,
105 hdr->total_size != 0 ? hdr->total_size : 2048);
106 free(sig_str);
107 }
108
109 static void
usage(void)110 usage(void)
111 {
112
113 printf("ucode-split [-v] microcode_file\n");
114 exit(1);
115 }
116
117 int
main(int argc,char * argv[])118 main(int argc, char *argv[])
119 {
120 struct microcode_update_header hdr;
121 char *buf, *output_file, *sig_str;
122 size_t len, resid;
123 ssize_t rv;
124 int c, ifd, ofd;
125 bool vflag;
126
127 vflag = false;
128 while ((c = getopt(argc, argv, "v")) != -1) {
129 switch (c) {
130 case 'v':
131 vflag = true;
132 break;
133 default:
134 usage();
135 }
136 }
137 argc -= optind;
138 argv += optind;
139
140 if (argc != 1)
141 usage();
142
143 ifd = open(argv[0], O_RDONLY);
144 if (ifd < 0)
145 err(1, "open");
146
147 buf = malloc(bufsize);
148 if (buf == NULL)
149 err(1, "malloc");
150
151 for (;;) {
152 /* Read header. */
153 rv = read(ifd, &hdr, sizeof(hdr));
154 if (rv < 0) {
155 err(1, "read");
156 } else if (rv == 0) {
157 break;
158 } else if (rv < (ssize_t)sizeof(hdr)) {
159 errx(1, "invalid microcode header");
160 }
161 if (hdr.header_version != 1)
162 errx(1, "invalid header version");
163
164 if (vflag)
165 dump_header(&hdr);
166
167 sig_str = format_signature(hdr.processor_signature);
168 asprintf(&output_file, "%s.%02x", sig_str,
169 hdr.processor_flags & 0xff);
170 free(sig_str);
171 if (output_file == NULL)
172 err(1, "asprintf");
173 ofd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
174 if (ofd < 0)
175 err(1, "open");
176
177 /* Write header. */
178 rv = write(ofd, &hdr, sizeof(hdr));
179 if (rv < (ssize_t)sizeof(hdr))
180 err(1, "write");
181
182 /* Copy data. */
183 resid = (hdr.total_size != 0 ? hdr.total_size : 2048) -
184 sizeof(hdr);
185 if (resid > 1 << 24) /* Arbitrary chosen maximum size. */
186 errx(1, "header total_size too large");
187 while (resid > 0) {
188 len = resid < bufsize ? resid : bufsize;
189 rv = read(ifd, buf, len);
190 if (rv < 0)
191 err(1, "read");
192 else if (rv < (ssize_t)len)
193 errx(1, "truncated microcode data");
194 if (write(ofd, buf, len) < (ssize_t)len)
195 err(1, "write");
196 resid -= len;
197 }
198 if (vflag)
199 printf("written to %s\n\n", output_file);
200 close(ofd);
201 free(output_file);
202 }
203 }
204