summaryrefslogtreecommitdiff
path: root/util/mec1322/mec1322.c
blob: 7ee83bb2b1d6a22eec86c37c81bd9eb6cb2e1a4d (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

#ifdef VERIFY_SIG
#include <openssl/obj_mac.h>
#include <openssl/rsa.h>
#include <openssl/sha.h>
#endif

void usage(void)
{
	fprintf(stderr,
		"Usage:\n"
		"\tmec1322 dump [-s <flash size>] [-o <outfile>] <infile>\n"
		"\tmec1322 insert [-s <flash size>] [-f <image>] <infile> <header_loc>\n");

	exit(1);
}

const int SPI_CLOCK_LIST[] = {48, 24, 12, 8};
const uint8_t SPI_READ_CMD_LIST[] = {0x3, 0xb, 0x3b};

// FIXME: uint32_t cannot be used directly on big endian systems
struct mec1322_header {
	uint8_t sig[6]; // "CSMS\0\0"
	uint8_t spi_clk;
	uint8_t spi_read_cmd;
	uint32_t load_addr;
	uint32_t entry_point;
	uint16_t payload_blks; // payload_len >> 6
	uint8_t padding0[2];
	uint32_t payload_offset;
	uint8_t padding1[8];
	uint8_t payload_key_exp[16];	  // @0x20
	uint8_t payload_key_modulus[256]; // @0x30
};

uint8_t crc8(const uint8_t *data, size_t len)
{
	const uint8_t CRC_TABLE[] = {0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
				     0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d};
	uint8_t crc = 0;
	for (size_t i = 0; i < len; i++) {
		uint8_t v = data[i];
		crc = (crc << 4) ^ (CRC_TABLE[(crc >> 4) ^ (v >> 4)]);
		crc = (crc << 4) ^ (CRC_TABLE[(crc >> 4) ^ (v & 0xf)]);
	}
	return crc ^ 0x55;
}

// return the total length of the firmware, including header,
// header_signature, and payload
// return 0 if there's error
size_t parse_header(const struct mec1322_header *hdr)
{
	if (memcmp(hdr->sig, "CSMS", 4) != 0) {
		fprintf(stderr, "MEC1322 header signature error!\n");
		return 0;
	}

	printf("SPI clock = %hhd MHz\n", SPI_CLOCK_LIST[hdr->spi_clk]);
	printf("SPI read cmd = 0x%hhx\n", SPI_READ_CMD_LIST[hdr->spi_read_cmd]);
	printf("load address = 0x%x\n", hdr->load_addr);
	printf("entry point = 0x%x\n", hdr->entry_point);
	size_t payload_len = (size_t)hdr->payload_blks * 64;
	printf("payload length = 0x%lx\n", payload_len);
	printf("payload offset = 0x%x\n", hdr->payload_offset);
	return payload_len + hdr->payload_offset;
}

int verify_payload_signature(const struct mec1322_header *hdr, void *payload,
			     const uint8_t *sig)
{
#ifdef VERIFY_SIG
	uint8_t hash[32];
	uint8_t sigrev[256];

	for (size_t i = 0; i < 256; i++)
		sigrev[i] = sig[255 - i];

	RSA *rsa = RSA_new();
	BIGNUM *modulus = BN_lebin2bn(hdr->payload_key_modulus, 256, NULL);
	BIGNUM *exp = BN_lebin2bn(hdr->payload_key_exp, 8, NULL);
	RSA_set0_key(rsa, modulus, exp, NULL);

	SHA256(payload, (size_t)(hdr->payload_blks) * 64, hash);

	int v = RSA_verify(NID_sha256, hash, 32, sigrev, 256, rsa);
	if (!v)
		fprintf(stderr, "MEC1322 payload RSA signature error!\n");
	else
		printf("MEC1322 payload RSA signature matches the payload.\n");

	return v;
#else
	fprintf(stderr,
		"** MEC1322 payload signature verification is not enabled.\n"
		"** You can run `make VERIFY_SIG=1` to enable it.\n");
	return 1;
#endif
}

int mec1322_dump(int argc, char *argv[])
{
	FILE *infile, *outfile = NULL;
	long spisize = 0;

	while (argc > 2) {
		if (strcmp(argv[0], "-s") == 0) {
			spisize = strtol(argv[1], NULL, 0);
			argc -= 2;
			argv += 2;
			continue;
		}
		if (strcmp(argv[0], "-o") == 0) {
			outfile = fopen(argv[1], "w");
			if (outfile == NULL) {
				fprintf(stderr, "Fail to open file %s!\n", argv[1]);
				exit(1);
			}
			argc -= 2;
			argv += 2;
			continue;
		}
		// otherwise, print usage and exit
		usage();
	}

	if (argc != 1)
		usage();

	// without the above options, it's the input file
	infile = fopen(argv[0], "r");
	if (infile == NULL) {
		fprintf(stderr, "Fail to open file %s!\n", argv[0]);
		exit(1);
	}

	if (spisize == 0) {
		fseek(infile, -1, SEEK_END);
		spisize = ftell(infile) + 1;

		printf("Input file size is 0x%lx.\n", spisize);
	}

	// read MEC1322 firmware tag
	fseek(infile, -256, SEEK_END);
	uint8_t fwTag[4];
	uint32_t header_loc;
	fread(fwTag, 1, 4, infile);

	if (crc8(fwTag, 3) == fwTag[3]) {
		header_loc = (((uint32_t)fwTag[0]) << 8) | (((uint32_t)fwTag[1]) << 16)
			     | (((uint32_t)fwTag[2]) << 24);
		printf("MEC1322 firmware header location is 0x%x.\n", header_loc);
	} else {
		fprintf(stderr, "MEC1322 tag CRC8 error!\n");
		return 1;
	}

	// we use the distance of header_loc to end when seeking
	struct mec1322_header hdr;
	fseek(infile, header_loc - spisize, SEEK_END);
	fread(&hdr, 1, sizeof(hdr), infile);
	size_t fwlen = parse_header(&hdr);
	if (fwlen == 0) {
		fprintf(stderr, "Error parsing MEC1322 firmware header!\n");
		return 1;
	}

	if (outfile) {
		fseek(infile, header_loc - spisize, SEEK_END);
		void *image = malloc(fwlen);
		fread(image, 1, fwlen, infile);
		fwrite(image, 1, fwlen, outfile);

		uint8_t rsasig[256];
		fread(rsasig, 1, 256, infile);
		fwrite(rsasig, 1, 256, outfile);

		if (!verify_payload_signature(&hdr, image + hdr.payload_offset, rsasig))
			return 1;

		free(image);
		fclose(outfile);
	}

	fclose(infile);
	return 0;
}

int mec1322_insert(int argc, char *argv[])
{
	FILE *infile, *ecfile = NULL;
	void *image = NULL;
	size_t fwlen;
	long spisize = 0;
	long header_loc;

	if (argc < 2)
		usage();

	while (argc > 2) {
		if (strcmp(argv[0], "-s") == 0) {
			spisize = strtol(argv[1], NULL, 0);
			argc -= 2;
			argv += 2;
			continue;
		}
		if (strcmp(argv[0], "-f") == 0) {
			ecfile = fopen(argv[1], "r");
			if (ecfile == NULL) {
				fprintf(stderr, "Fail to open EC file %s!\n", argv[1]);
				return 1;
			}
			argc -= 2;
			argv += 2;
			continue;
		}
		usage();
	}

	if (argc != 2)
		usage();

	infile = fopen(argv[0], "r+");
	header_loc = strtol(argv[1], NULL, 0);

	if (infile == NULL) {
		fprintf(stderr, "Fail to open firmware image %s!\n", argv[1]);
		return 1;
	}

	if (spisize == 0) {
		fseek(infile, -1, SEEK_END);
		spisize = ftell(infile) + 1;

		printf("Input file size is 0x%lx.\n", spisize);
	}

	if (ecfile != NULL) {
		struct mec1322_header hdr;
		fread(&hdr, 1, sizeof(hdr), ecfile);
		fwlen = parse_header(&hdr);

		if (fwlen == 0) {
			fprintf(stderr, "Error parsing MEC1322 firmware header!\n");
			return 1;
		}
		fseek(ecfile, 0, SEEK_SET);
		size_t totallen = fwlen + 256; // firmware length with signature
		image = malloc(totallen);
		fread(image, 1, totallen, ecfile);
		fclose(ecfile);

		if (!verify_payload_signature(&hdr, image + hdr.payload_offset, image + fwlen))
			return 1;

		fwlen = totallen;
	}

	if (header_loc & 0xff) {
		fprintf(stderr, "Header location should be aligned to 256 bytes!\n");
		return 1;
	}
	uint8_t fwTag[4] = {header_loc >> 8, header_loc >> 16, header_loc >> 24};
	fwTag[3] = crc8(fwTag, 3);

	// write tag
	fseek(infile, -256, SEEK_END);
	fwrite(fwTag, 1, 4, infile);

	// if EC image is given, write EC image
	if (image) {
		fseek(infile, header_loc - spisize, SEEK_END);
		fwrite(image, 1, fwlen, infile);
		free(image);
	}

	fclose(infile);

	return 0;
}

int main(int argc, char *argv[])
{
	if (argc < 3)
		usage();

	if (strcmp(argv[1], "dump") == 0)
		return mec1322_dump(argc - 2, argv + 2);

	if (strcmp(argv[1], "insert") == 0)
		return mec1322_insert(argc - 2, argv + 2);

	// no action provided
	usage();
}