2 * Copyright (c) 2013 John Clark <clarkjc@runbox.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/endian.h>
35 #define BUFFER_SIZE 4096
37 static void error(const char *fmt, ...);
38 static void process_amd(const char *container, const char *outdir);
39 static void process_intel(const char *filename, const char *outdir);
42 * This tool extracts microcode from container files provided by
43 * Intel and AMD for their families of popular microprocessors.
46 main(int argc, char *argv[])
51 /* Parse the command line arguments. */
52 while ((ch = getopt(argc, argv, "aio:")) != -1) {
54 case 'a': /* Mode select */
58 case 'o': /* Output directory */
61 default: /* Unknown */
62 error("Error: Invalid argument\n");
67 /* Process Intel microcode container files */
68 for (i = optind; i < argc; i++) {
69 process_intel(argv[i], outdir);
71 } else if (mode == 'a') {
72 /* Process AMD microcode container files */
73 for (i = optind; i < argc; i++) {
74 process_amd(argv[i], outdir);
77 error("Error: Invalid mode\n");
83 /* Display an error message and exit with a status code of 1. */
85 error(const char *fmt, ...)
93 vfprintf(stderr, fmt, args);
99 /* Process an AMD supplied microcode container file. */
100 #define AMD_HEADER_LEN 12
101 #define AMD_SKIP_OFFSET 8
102 #define AMD_UCODE_HEADER_LEN 8
103 #define AMD_UCODE_HEADER_TYPE 0x00000001
104 #define AMD_UCODE_ID_OFFSET 4
105 #define AMD_UCODE_SIG_OFFSET 24
107 process_amd(const char *container, const char *outdir)
109 char outname[FILENAME_MAX];
110 const uint8_t magic[] = {
111 0x44, 0x4d, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00
115 uint32_t id, len, sig;
118 if ((buf = malloc(BUFFER_SIZE)) == NULL)
121 /* Open the container file and read the header. */
122 if ((fin = fopen(container, "rb")) == NULL)
124 if (fread(buf, AMD_HEADER_LEN, 1, fin) != 1) {
125 error("Error: Truncated file: %s\n", container);
128 /* Check the magic numbers. */
129 if (memcmp(magic, buf, sizeof(magic)) != 0) {
130 error("Error: Invalid file: %s\n", container);
133 /* Seek to the first microcode image. */
134 if (fseek(fin, le32dec(buf + AMD_SKIP_OFFSET), SEEK_CUR) != 0)
137 /* Read all microcode images. */
138 while ((num = fread(buf, 1, AMD_UCODE_HEADER_LEN, fin)) != 0) {
139 /* Read and validate the image. */
140 if (num != AMD_UCODE_HEADER_LEN) {
141 error("Error: Truncated file: %s\n", container);
143 if (le32dec(buf) != AMD_UCODE_HEADER_TYPE) {
144 error("Error: Invalid type: %s\n", container);
146 len = le32dec(buf + sizeof(uint32_t));
147 if (len > BUFFER_SIZE) {
148 if ((buf = realloc(buf, len)) == NULL)
151 if (fread(buf, len, 1, fin) != 1) {
152 error("Error: Truncated file: %s\n", container);
155 /* Write the image to an output file. */
156 sig = le32dec(buf + AMD_UCODE_SIG_OFFSET);
157 id = le32dec(buf + AMD_UCODE_ID_OFFSET);
158 snprintf(outname, sizeof(outname), "%s/AMD-%08x-%08x.fw",
160 if ((fout = fopen(outname, "wb")) == NULL)
162 if (fwrite(buf, len, 1, fout) != 1)
164 if (fclose(fout) != 0)
168 if (fclose(fin) != 0)
173 /* Process an Intel supplied microcode container file. */
175 process_intel(const char *container, const char *outdir)
177 char outname[FILENAME_MAX];
178 FILE *fin, *fout = NULL;
180 const char * const sep = ",. \t\n";
183 if ((buf = malloc(BUFFER_SIZE)) == NULL)
185 if ((fin = fopen(container, "r")) == NULL)
188 /* Process the container file line by line. */
189 while (fgets(buf, BUFFER_SIZE, fin) != NULL) {
190 if ((token = strtok(buf, sep)) == NULL)
194 /* Process a comment line. */
196 /* Close previous output file. */
197 if (fclose(fout) != 0)
201 if ((token = strtok(NULL, sep)) != NULL) {
202 /* Construct next file name. */
203 snprintf(outname, sizeof(outname), "%s/%s.fw",
207 /* Process a data line. */
208 if ((fout == NULL) && (token != NULL)) {
209 if ((fout = fopen(outname, "wb")) == NULL)
212 while (token != NULL) {
213 val = htole32(strtoul(token, NULL, 0));
214 if (fwrite(&val, sizeof(val), 1, fout) != 1)
216 token = strtok(NULL, sep);
222 if (fclose(fout) != 0)
225 if (fclose(fin) != 0)