2 * Convert Intel microcode.dat into a single binary microcode.bin file
4 * Based on code by Kay Sievers <kay.sievers@vrfy.org>
5 * Changed to create a single file by Thomas Bächler <thomas@archlinux.org>
10 # define _GNU_SOURCE 1
25 struct microcode_header_intel {
33 unsigned int datasize;
34 unsigned int totalsize;
35 unsigned int reserved[3];
39 struct microcode_header_intel hdr;
44 int main(int argc, char *argv[])
46 const char *filename = "/lib/firmware/microcode.dat";
51 size_t bufsize, count, start;
52 int rc = EXIT_SUCCESS;
58 mc = (union mcbuf *) buf;
59 f = fopen(filename, "re");
61 printf("open %s: %m\n", filename);
66 while (fgets(line, sizeof(line), f) != NULL) {
67 if (sscanf(line, "%x, %x, %x, %x",
71 &mc->i[count + 3]) != 4)
77 bufsize = count * sizeof(int);
78 printf("%s: %lu(%luk) bytes, %zu integers\n",
84 if (bufsize < sizeof(struct microcode_header_intel))
87 f = fopen("microcode.bin", "we");
89 printf("open microcode.bin: %m\n");
97 unsigned int family, model, stepping, type;
98 unsigned int year, month, day;
100 mc = (union mcbuf *) &buf[start];
102 if (mc->hdr.totalsize)
103 size = mc->hdr.totalsize;
105 size = 2000 + sizeof(struct microcode_header_intel);
107 if (mc->hdr.ldrver != 1 || mc->hdr.hdrver != 1) {
108 printf("unknown version/format:\n");
118 * 16-19 extended model
119 * 20-27 extended family
121 stepping = mc->hdr.sig & 0x0f;
122 model = (mc->hdr.sig >> 4) & 0x0f;
123 family = (mc->hdr.sig >> 8) & 0x0f;
124 type = (mc->hdr.sig >> 12) & 0x0f;
126 model += ((mc->hdr.sig >> 16) & 0x0f) << 4;
128 family += (mc->hdr.sig >> 20) & 0xff;
130 year = mc->hdr.date & 0xffff;
131 month = mc->hdr.date >> 24;
132 day = (mc->hdr.date >> 16) & 0xff;
135 printf("signature: 0x%02x (stepping %d, model %d, family %d, type %d)\n",
136 mc->hdr.sig, stepping, model, family, type);
137 printf("flags: 0x%02x\n", mc->hdr.pf);
138 printf("revision: 0x%02x\n", mc->hdr.rev);
139 printf("date: %04x-%02x-%02x\n", year, month, day);
140 printf("size: %zu\n", size);
142 if (fwrite(mc, size, 1, f) != 1) {
143 printf("write microcode.bin: %m\n");
149 if (start >= bufsize)