[coreboot] SPD binaries in coreboot

Peter Stuge peter at stuge.se
Fri Oct 23 21:37:56 CEST 2015


Aaron Durbin wrote:
> This one's for Ron.

En guard!

$ wc -c -l spd.*
  81 1462 spd.c
 100 1629 spd.go
$ 


//Peter
-------------- next part --------------
#include <stdio.h>
#include <string.h>
#include <errno.h>

static unsigned char spd[256];

int readhex(const char *filename) {
	int pos = 0, i, end;
	unsigned int n;
	char buf[128], *hex;
	FILE *f = fopen(filename, "r");

	if (!f) {
		fprintf(stderr, "%s: fopen(%s): %s\n", __func__, filename, strerror(errno));
		return 0;
	}

	memset(spd, 0, sizeof spd);

	while (pos < sizeof spd && fgets(buf, sizeof buf, f)) {
		hex = strtok(buf, ":");
		hex = strtok(NULL, ":");

		for (i = 0; i < 16; i++) {
			if (sscanf(hex, " %02x%n", &n, &end) < 1)
				goto done;

			spd[pos++] = n;
			hex += end;
		}
	}

done:
	fclose(f);
	return pos;
}

int writebin(const char *filename) {
	FILE *f = fopen(filename, "wb");
	size_t n;

	if (!f) {
		fprintf(stderr, "%s: fopen(%s): %s\n", __func__, filename, strerror(errno));
		return 0;
	}

	n = fwrite(spd, 1, sizeof spd, f);

	fclose(f);

	if (n != sizeof spd) {
		if (ferror(f))
			fprintf(stderr, "%s: fwrite: %s\n", __func__, strerror(errno));
		else
			fprintf(stderr, "%s: Short write! Got %zd, want %zd.\n", __func__, n, sizeof spd);

		return 0;
	}

	return 1;
}

int main(int argc, char *argv[]) {

	if (argc < 3) {
		fprintf(stderr, "syntax: %s input output\n", argv[0]);
		return 1;
	}

	if (!readhex(argv[1])) {
		fprintf(stderr, "Unable to read hex data from %s\n", argv[1]);
		return 1;
	}

	if (!writebin(argv[2])) {
		fprintf(stderr, "Unable to write binary data to %s\n", argv[2]);
		return 1;
	}

	return 0;
}


More information about the coreboot mailing list