summaryrefslogtreecommitdiff
path: root/sysutils/devcpu-data/files/ucode-tool.c
blob: a45265ba91d06640f901edc784cdec75d9a30988 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*-
 * Copyright (c) 2013 John Clark <clarkjc@runbox.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* $FreeBSD$ */
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/endian.h>
#include <unistd.h>

#define BUFFER_SIZE	4096

static void	error(const char *fmt, ...);
static void	process_amd(const char *container, const char *outdir);
static void	process_intel(const char *filename, const char *outdir);

/*
 * This tool extracts microcode from container files provided by
 * Intel and AMD for their families of popular microprocessors.
 */
int
main(int argc, char *argv[])
{
	int ch, i, mode = -1;
	char *outdir = ".";

	/* Parse the command line arguments. */
	while ((ch = getopt(argc, argv, "aio:")) != -1) {
		switch (ch) {
		case 'a':	/* Mode select */
		case 'i':
			mode = ch;
			break;
		case 'o':	/* Output directory */
			outdir = optarg;
			break;
		default:	/* Unknown */
			error("Error: Invalid argument\n");
		}
	}

	if (mode == 'i') {
		/* Process Intel microcode container files */
		for (i = optind; i < argc; i++) {
			process_intel(argv[i], outdir);
		}
	} else if (mode == 'a') {
		/* Process AMD microcode container files */
		for (i = optind; i < argc; i++) {
			process_amd(argv[i], outdir);
		}
	} else {
		error("Error: Invalid mode\n");
	}

	return 0;
}

/* Display an error message and exit with a status code of 1. */
static void
error(const char *fmt, ...)
{
	va_list args;

	if (fmt == NULL) {
		perror("Error");
	} else {
		va_start(args, fmt);
		vfprintf(stderr, fmt, args);
		va_end(args);
	}
	exit(1);
}

/* Process an AMD supplied microcode container file. */
#define AMD_HEADER_LEN		12
#define AMD_SKIP_OFFSET		8
#define AMD_UCODE_HEADER_LEN	8
#define AMD_UCODE_HEADER_TYPE	0x00000001
#define AMD_UCODE_ID_OFFSET	4
#define AMD_UCODE_SIG_OFFSET	24
static void
process_amd(const char *container, const char *outdir)
{
	char outname[FILENAME_MAX];
	const uint8_t magic[] = {
		0x44, 0x4d, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00
	};
	FILE *fin, *fout;
	uint8_t *buf;
	uint32_t id, len, sig;
	int num;

	if ((buf = malloc(BUFFER_SIZE)) == NULL)
		error(NULL);

	/* Open the container file and read the header. */
	if ((fin = fopen(container, "rb")) == NULL)
		error(NULL);
	if (fread(buf, AMD_HEADER_LEN, 1, fin) != 1) {
		error("Error: Truncated file: %s\n", container);
	}

	/* Check the magic numbers. */
	if (memcmp(magic, buf, sizeof(magic)) != 0) {
		error("Error: Invalid file: %s\n", container);
	}

	/* Seek to the first microcode image. */
	if (fseek(fin, le32dec(buf + AMD_SKIP_OFFSET), SEEK_CUR) != 0)
		error(NULL);

	/* Read all microcode images. */
	while ((num = fread(buf, 1, AMD_UCODE_HEADER_LEN, fin)) != 0) {
		/* Read and validate the image. */
		if (num != AMD_UCODE_HEADER_LEN) {
			error("Error: Truncated file: %s\n", container);
		}
		if (le32dec(buf) != AMD_UCODE_HEADER_TYPE) {
			error("Error: Invalid type: %s\n", container);
		}
		len = le32dec(buf + sizeof(uint32_t));
		if (len > BUFFER_SIZE) {
			if ((buf = realloc(buf, len)) == NULL)
				error(NULL);
		}
		if (fread(buf, len, 1, fin) != 1) {
			error("Error: Truncated file: %s\n", container);
		}

		/* Write the image to an output file. */
		sig = le32dec(buf + AMD_UCODE_SIG_OFFSET);
		id = le32dec(buf + AMD_UCODE_ID_OFFSET);
		snprintf(outname, sizeof(outname), "%s/AMD-%08x-%08x.fw",
		    outdir, sig, id);
		if ((fout = fopen(outname, "wb")) == NULL)
			error(NULL);
		if (fwrite(buf, len, 1, fout) != 1)
			error(NULL);
		if (fclose(fout) != 0)
			error(NULL);
	}

	if (fclose(fin) != 0)
		error(NULL);
	free(buf);
}

/* Process an Intel supplied microcode container file. */
static void
process_intel(const char *container, const char *outdir)
{
	char outname[FILENAME_MAX];
	FILE *fin, *fout = NULL;
	char *buf, *token;
	const char * const sep = ",. \t\n";
	uint32_t val;

	if ((buf = malloc(BUFFER_SIZE)) == NULL)
		error(NULL);
	if ((fin = fopen(container, "r")) == NULL)
		error(NULL);

	/* Process the container file line by line. */
	while (fgets(buf, BUFFER_SIZE, fin) != NULL) {
		if ((token = strtok(buf, sep)) == NULL)
			continue;

		if (*token == '/') {
			/* Process a comment line. */
			if (fout != NULL) {
				/* Close previous output file. */
				if (fclose(fout) != 0)
					error(NULL);
				fout = NULL;
			}
			if ((token = strtok(NULL, sep)) != NULL) {
				/* Construct next file name. */
				snprintf(outname, sizeof(outname), "%s/%s.fw",
				    outdir, token);
			}
		} else {
			/* Process a data line. */
			if ((fout == NULL) && (token != NULL)) {
				if ((fout = fopen(outname, "wb")) == NULL)
					error(NULL);
			}
			while (token != NULL) {
				val = htole32(strtoul(token, NULL, 0));
				if (fwrite(&val, sizeof(val), 1, fout) != 1)
					error(NULL);
				token = strtok(NULL, sep);
			}
		}
	}

	if (fout != NULL) {
		if (fclose(fout) != 0)
			error(NULL);
	}
	if (fclose(fin) != 0)
		error(NULL);
	free(buf);
}