From owner-svn-ports-all@FreeBSD.ORG Wed Jun 4 20:18:56 2014 Return-Path: Delivered-To: svn-ports-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id C46A5850; Wed, 4 Jun 2014 20:18:56 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id A73752BC6; Wed, 4 Jun 2014 20:18:56 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s54KIups094453; Wed, 4 Jun 2014 20:18:56 GMT (envelope-from bapt@svn.freebsd.org) Received: (from bapt@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s54KIuX9094451; Wed, 4 Jun 2014 20:18:56 GMT (envelope-from bapt@svn.freebsd.org) Message-Id: <201406042018.s54KIuX9094451@svn.freebsd.org> From: Baptiste Daroussin Date: Wed, 4 Jun 2014 20:18:56 +0000 (UTC) To: ports-committers@freebsd.org, svn-ports-all@freebsd.org, svn-ports-head@freebsd.org Subject: svn commit: r356543 - head/sysutils/devcpu-data/files X-SVN-Group: ports-head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-ports-all@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: SVN commit messages for the ports tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Jun 2014 20:18:56 -0000 Author: bapt Date: Wed Jun 4 20:18:55 2014 New Revision: 356543 URL: http://svnweb.freebsd.org/changeset/ports/356543 QAT: https://qat.redports.org/buildarchive/r356543/ Log: Add forgotten files Reported by: many Added: head/sysutils/devcpu-data/files/Makefile (contents, props changed) head/sysutils/devcpu-data/files/ucode-tool.c (contents, props changed) Added: head/sysutils/devcpu-data/files/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sysutils/devcpu-data/files/Makefile Wed Jun 4 20:18:55 2014 (r356543) @@ -0,0 +1,17 @@ +AMD_UCODE_DIR= amd-ucode-2012-09-10 +AMD_UCODE= $(AMD_UCODE_DIR)/microcode_amd.bin \ + $(AMD_UCODE_DIR)/microcode_amd_fam15h.bin +INTEL_UCODE= microcode.dat +OUTPUT_DIR= mcodes +all: ucode +ucode: ucode-tool + mkdir -p $(OUTPUT_DIR) + ./ucode-tool -o $(OUTPUT_DIR) -i $(INTEL_UCODE) + ./ucode-tool -o $(OUTPUT_DIR) -a $(AMD_UCODE) + +# Use the host cc to compile ucode-tool in case of cross-compile +ucode-tool: ucode-tool.c + cc ucode-tool.c -o $@ + +clean: + rm -rf $(OUTPUT_DIR) ucode-tool Added: head/sysutils/devcpu-data/files/ucode-tool.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sysutils/devcpu-data/files/ucode-tool.c Wed Jun 4 20:18:55 2014 (r356543) @@ -0,0 +1,228 @@ +/*- + * Copyright (c) 2013 John Clark + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +#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); +}