b5c3b905713bec21c10b2d5a52901c18cbb48caa
[dports.git] / sysutils / devcpu-data / files / ucode-tool.c
1 /*-
2  * Copyright (c) 2013 John Clark <clarkjc@runbox.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/endian.h>
33 #include <unistd.h>
34
35 #define BUFFER_SIZE     4096
36
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);
40
41 /*
42  * This tool extracts microcode from container files provided by
43  * Intel and AMD for their families of popular microprocessors.
44  */
45 int
46 main(int argc, char *argv[])
47 {
48         int ch, i, mode = -1;
49         char *outdir = ".";
50
51         /* Parse the command line arguments. */
52         while ((ch = getopt(argc, argv, "aio:")) != -1) {
53                 switch (ch) {
54                 case 'a':       /* Mode select */
55                 case 'i':
56                         mode = ch;
57                         break;
58                 case 'o':       /* Output directory */
59                         outdir = optarg;
60                         break;
61                 default:        /* Unknown */
62                         error("Error: Invalid argument\n");
63                 }
64         }
65
66         if (mode == 'i') {
67                 /* Process Intel microcode container files */
68                 for (i = optind; i < argc; i++) {
69                         process_intel(argv[i], outdir);
70                 }
71         } else if (mode == 'a') {
72                 /* Process AMD microcode container files */
73                 for (i = optind; i < argc; i++) {
74                         process_amd(argv[i], outdir);
75                 }
76         } else {
77                 error("Error: Invalid mode\n");
78         }
79
80         return 0;
81 }
82
83 /* Display an error message and exit with a status code of 1. */
84 static void
85 error(const char *fmt, ...)
86 {
87         va_list args;
88
89         if (fmt == NULL) {
90                 perror("Error");
91         } else {
92                 va_start(args, fmt);
93                 vfprintf(stderr, fmt, args);
94                 va_end(args);
95         }
96         exit(1);
97 }
98
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
106 static void
107 process_amd(const char *container, const char *outdir)
108 {
109         char outname[FILENAME_MAX];
110         const uint8_t magic[] = {
111                 0x44, 0x4d, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00
112         };
113         FILE *fin, *fout;
114         uint8_t *buf;
115         uint32_t id, len, sig;
116         int num;
117
118         if ((buf = malloc(BUFFER_SIZE)) == NULL)
119                 error(NULL);
120
121         /* Open the container file and read the header. */
122         if ((fin = fopen(container, "rb")) == NULL)
123                 error(NULL);
124         if (fread(buf, AMD_HEADER_LEN, 1, fin) != 1) {
125                 error("Error: Truncated file: %s\n", container);
126         }
127
128         /* Check the magic numbers. */
129         if (memcmp(magic, buf, sizeof(magic)) != 0) {
130                 error("Error: Invalid file: %s\n", container);
131         }
132
133         /* Seek to the first microcode image. */
134         if (fseek(fin, le32dec(buf + AMD_SKIP_OFFSET), SEEK_CUR) != 0)
135                 error(NULL);
136
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);
142                 }
143                 if (le32dec(buf) != AMD_UCODE_HEADER_TYPE) {
144                         error("Error: Invalid type: %s\n", container);
145                 }
146                 len = le32dec(buf + sizeof(uint32_t));
147                 if (len > BUFFER_SIZE) {
148                         if ((buf = realloc(buf, len)) == NULL)
149                                 error(NULL);
150                 }
151                 if (fread(buf, len, 1, fin) != 1) {
152                         error("Error: Truncated file: %s\n", container);
153                 }
154
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",
159                     outdir, sig, id);
160                 if ((fout = fopen(outname, "wb")) == NULL)
161                         error(NULL);
162                 if (fwrite(buf, len, 1, fout) != 1)
163                         error(NULL);
164                 if (fclose(fout) != 0)
165                         error(NULL);
166         }
167
168         if (fclose(fin) != 0)
169                 error(NULL);
170         free(buf);
171 }
172
173 /* Process an Intel supplied microcode container file. */
174 static void
175 process_intel(const char *container, const char *outdir)
176 {
177         char outname[FILENAME_MAX];
178         FILE *fin, *fout = NULL;
179         char *buf, *token;
180         const char * const sep = ",. \t\n";
181         uint32_t val;
182
183         if ((buf = malloc(BUFFER_SIZE)) == NULL)
184                 error(NULL);
185         if ((fin = fopen(container, "r")) == NULL)
186                 error(NULL);
187
188         /* Process the container file line by line. */
189         while (fgets(buf, BUFFER_SIZE, fin) != NULL) {
190                 if ((token = strtok(buf, sep)) == NULL)
191                         continue;
192
193                 if (*token == '/') {
194                         /* Process a comment line. */
195                         if (fout != NULL) {
196                                 /* Close previous output file. */
197                                 if (fclose(fout) != 0)
198                                         error(NULL);
199                                 fout = NULL;
200                         }
201                         if ((token = strtok(NULL, sep)) != NULL) {
202                                 /* Construct next file name. */
203                                 snprintf(outname, sizeof(outname), "%s/%s.fw",
204                                     outdir, token);
205                         }
206                 } else {
207                         /* Process a data line. */
208                         if ((fout == NULL) && (token != NULL)) {
209                                 if ((fout = fopen(outname, "wb")) == NULL)
210                                         error(NULL);
211                         }
212                         while (token != NULL) {
213                                 val = htole32(strtoul(token, NULL, 0));
214                                 if (fwrite(&val, sizeof(val), 1, fout) != 1)
215                                         error(NULL);
216                                 token = strtok(NULL, sep);
217                         }
218                 }
219         }
220
221         if (fout != NULL) {
222                 if (fclose(fout) != 0)
223                         error(NULL);
224         }
225         if (fclose(fin) != 0)
226                 error(NULL);
227         free(buf);
228 }