2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2018 The FreeBSD Foundation.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
39 static const size_t bufsize = 65536;
41 /* SDM vol 3 9.11.1 Intel microcode header. */
42 struct microcode_update_header {
43 uint32_t header_version;
44 uint32_t update_revision;
45 uint32_t date; /* BCD mmddyyyy */
46 uint32_t processor_signature;
47 uint32_t checksum; /* Over update data and header */
48 uint32_t loader_revision;
49 uint32_t processor_flags;
56 * SDM vol 2A CPUID EAX = 01h Returns Model, Family, Stepping Information.
57 * Caller must free the returned string.
61 format_signature(uint32_t signature)
64 unsigned family, model, stepping;
66 family = (signature & 0xf00) >> 8;
67 model = (signature & 0xf0) >> 4;
68 stepping = signature & 0xf;
69 if (family == 0x06 || family == 0x0f)
70 model += (signature & 0xf0000) >> 12;
72 family += (signature & 0xff00000) >> 20;
73 asprintf(&buf, "%02x-%02x-%02x", family, model, stepping);
80 dump_header(const struct microcode_update_header *hdr)
84 bool platformid_printed;
86 sig_str = format_signature(hdr->processor_signature);
87 printf("header version\t0x%x\n", hdr->header_version);
88 printf("revision\t0x%x\n", hdr->update_revision);
89 printf("date\t\t0x%x\t%04x-%02x-%02x\n", hdr->date,
90 hdr->date & 0xffff, (hdr->date & 0xff000000) >> 24,
91 (hdr->date & 0xff0000) >> 16);
92 printf("signature\t0x%x\t\t%s\n", hdr->processor_signature, sig_str);
93 printf("checksum\t0x%x\n", hdr->checksum);
94 printf("loader revision\t0x%x\n", hdr->loader_revision);
95 printf("processor flags\t0x%x", hdr->processor_flags);
96 platformid_printed = false;
97 for (i = 0; i < 8; i++) {
98 if (hdr->processor_flags & 1 << i) {
99 printf("%s%d", platformid_printed ? ", " : "\t\t", i);
100 platformid_printed = true;
104 printf("datasize\t0x%x\t\t0x%x\n", hdr->data_size,
105 hdr->data_size != 0 ? hdr->data_size : 2000);
106 printf("size\t\t0x%x\t\t0x%x\n", hdr->total_size,
107 hdr->total_size != 0 ? hdr->total_size : 2048);
115 printf("ucode-split [-nv] microcode_file\n");
120 main(int argc, char *argv[])
122 struct microcode_update_header hdr;
123 char *buf, *output_file, *sig_str;
129 nflag = vflag = false;
130 while ((c = getopt(argc, argv, "nv")) != -1) {
148 ifd = open(argv[0], O_RDONLY);
152 buf = malloc(bufsize);
158 rv = read(ifd, &hdr, sizeof(hdr));
161 } else if (rv == 0) {
163 } else if (rv < (ssize_t)sizeof(hdr)) {
164 errx(1, "invalid microcode header");
166 if (hdr.header_version != 1)
167 errx(1, "invalid header version");
172 resid = (hdr.total_size != 0 ? hdr.total_size : 2048) -
174 if (resid > 1 << 24) /* Arbitrary chosen maximum size. */
175 errx(1, "header total_size too large");
178 if (lseek(ifd, resid, SEEK_CUR) == -1)
182 sig_str = format_signature(hdr.processor_signature);
183 asprintf(&output_file, "%s.%02x", sig_str,
184 hdr.processor_flags & 0xff);
186 if (output_file == NULL)
188 ofd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC,
194 rv = write(ofd, &hdr, sizeof(hdr));
195 if (rv < (ssize_t)sizeof(hdr))
200 len = resid < bufsize ? resid : bufsize;
201 rv = read(ifd, buf, len);
204 else if (rv < (ssize_t)len)
205 errx(1, "truncated microcode data");
206 if (write(ofd, buf, len) < (ssize_t)len)
211 printf("written to %s\n\n", output_file);