2 * Convert Intel microcode.dat into individual ucode files
3 * named: intel-ucode/$family-$model-$stepping
5 * The subdir intel-ucode/ is created in the current working
6 * directory. We get multiple ucodes in the same file, so they
7 * are appended to an existing file. Make sure the directory
8 * is empty before every run of the converter.
10 * Kay Sievers <kay.sievers@vrfy.org>
15 # define _GNU_SOURCE 1
30 struct microcode_header_intel {
38 unsigned int datasize;
39 unsigned int totalsize;
40 unsigned int reserved[3];
44 struct microcode_header_intel hdr;
49 int main(int argc, char *argv[])
51 char *filename = "/lib/firmware/microcode.dat";
56 size_t bufsize, count, start;
57 int rc = EXIT_SUCCESS;
63 mc = (union mcbuf *) buf;
64 f = fopen(filename, "re");
66 printf("open %s: %m\n", filename);
71 while (fgets(line, sizeof(line), f) != NULL) {
72 if (sscanf(line, "%x, %x, %x, %x",
76 &mc->i[count + 3]) != 4)
82 bufsize = count * sizeof(int);
83 printf("%s: %lu(%luk) bytes, %zu integers\n",
89 if (bufsize < sizeof(struct microcode_header_intel))
92 mkdir("intel-ucode", 0750);
97 unsigned int family, model, stepping;
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 family = (mc->hdr.sig >> 8) & 0xf;
123 family += (mc->hdr.sig >> 20) & 0xff;
124 model = (mc->hdr.sig >> 4) & 0x0f;
126 model += ((mc->hdr.sig >> 16) & 0x0f) << 4;
127 stepping = mc->hdr.sig & 0x0f;
129 year = mc->hdr.date & 0xffff;
130 month = mc->hdr.date >> 24;
131 day = (mc->hdr.date >> 16) & 0xff;
133 asprintf(&filename, "intel-ucode/%02x-%02x-%02x", family, model, stepping);
135 printf("%s\n", filename);
136 printf("signature: 0x%02x\n", mc->hdr.sig);
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 f = fopen(filename, "ae");
144 printf("open %s: %m\n", filename);
148 if (fwrite(mc, size, 1, f) != 1) {
149 printf("write %s: %m\n", filename);
157 if (start >= bufsize)