Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 11 Jun 2013 21:23:28 GMT
From:      John Clark <clarkjc@runbox.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   ports/179499: [patch] port sysutils/devcpu-data is unmaintained and needs update for recent Intel and AMD releases
Message-ID:  <201306112123.r5BLNSxO026686@oldred.freebsd.org>
Resent-Message-ID: <201306112130.r5BLU1tH018040@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         179499
>Category:       ports
>Synopsis:       [patch] port sysutils/devcpu-data is unmaintained and needs update for recent Intel and AMD releases
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-ports-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jun 11 21:30:01 UTC 2013
>Closed-Date:
>Last-Modified:
>Originator:     John Clark
>Release:        9.1-RELEASE-p3
>Organization:
>Environment:
FreeBSD beastie.local 9.1-RELEASE-p3 FreeBSD 9.1-RELEASE-p3 #0: Mon Apr 29 18:27:25 UTC 2013     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
The sysutils/devcpu-data port needs an update to install the latest microcode updates released by Intel and AMD for their popular x86 families of microprocessors.  Attached is a patch that modifies this port to fetch the manufacturer release bundles and build the files required by FreeBSD's microcode update tool (/usr/sbin/cpucontrol) from them.

I noticed that this port is unmaintained, so I volunteer to maintain it.  I would like to get more involved with FreeBSD, and this looks a good place to start.  The attached patch includes a change to MAINTAINER.

Thank you!
>How-To-Repeat:
N/A
>Fix:
Simply apply the attached patch and reinstall the port.

Patch attached with submission follows:

Index: sysutils/devcpu-data/Makefile
===================================================================
--- sysutils/devcpu-data/Makefile	(revision 320635)
+++ sysutils/devcpu-data/Makefile	(working copy)
@@ -2,18 +2,19 @@
 # $FreeBSD$
 
 PORTNAME=	data
-PORTVERSION=	0.6
+PORTVERSION=	1.0
 PORTREVISION=	0
 CATEGORIES=	sysutils
-MASTER_SITES=	ftp://ftp.SpringDaemons.com/soft/
+MASTER_SITES=	http://downloadmirror.intel.com/22508/eng/:intel \
+		http://pkgs.fedoraproject.org/repo/pkgs/microcode_ctl/amd-ucode-2012-09-10.tar/559bc355d3799538584add80df2996f0/:amd
+# Defunct:	http://www.amd64.org/index.php?id=50&file=amd-ucode-2012-09-10.tar
 PKGNAMEPREFIX=	devcpu-
-DISTNAME=	${PKGNAMEPREFIX}${PORTNAME}-${PORTVERSION}
+DISTFILES=	microcode-20130222.tgz:intel amd-ucode-2012-09-10.tar:amd
 
-MAINTAINER=	ports@FreeBSD.org
-COMMENT=	Intel and AMD CPUs microcode updates
+MAINTAINER=	clarkjc@runbox.com
+COMMENT=	Intel and AMD CPU microcode updates
 
-USE_BZIP2=	yes
-NO_BUILD=	yes
+NO_WRKSUBDIR=	yes
 
 ONLY_FOR_ARCHS=	i386 amd64
 
@@ -23,14 +24,20 @@
 
 USE_RC_SUBR=	microcode_update
 
+post-extract:
+	${CP} -p ${FILESDIR}/Makefile ${FILESDIR}/ucode-tool.c ${WRKSRC}
+
 do-install:
 	${MKDIR} ${DATADIR}/
 	${INSTALL_DATA} ${WRKSRC}/mcodes/* ${DATADIR}/
 	@${TOUCH} ${DATADIR}/.do_not_delete
 
 post-install:
-	@${FIND} -ds ${DATADIR}/ ! -type d | \
+	@${FIND} -ds ${WRKSRC}/mcodes/ ! -type d | \
+		${SED} 's,^${WRKSRC}/mcodes/,${DATADIR}/,' | \
 		${SED} 's,^${PREFIX}/,,' >> ${TMPPLIST}
+	@${ECHO_CMD} "${DATADIR}/.do_not_delete" | \
+		${SED} 's,^${PREFIX}/,,' >> ${TMPPLIST}
 	@${ECHO_CMD} "@unexec rmdir %D/${DATADIR:S,^${PREFIX},,}" >> ${TMPPLIST}
 
 .include <bsd.port.post.mk>
Index: sysutils/devcpu-data/distinfo
===================================================================
--- sysutils/devcpu-data/distinfo	(revision 320635)
+++ sysutils/devcpu-data/distinfo	(working copy)
@@ -1,2 +1,4 @@
-SHA256 (devcpu-data-0.6.tar.bz2) = 5153af0de3ce4f30edec7ee95596bfc2ead734d70b6ae0353d51890b527f875f
-SIZE (devcpu-data-0.6.tar.bz2) = 636188
+SHA256 (microcode-20130222.tgz) = fd25bd9777fc3c3b11f01e9090a2d24f7650023c9ec74bbf9f43bffe1d9d01cc
+SIZE (microcode-20130222.tgz) = 602449
+SHA256 (amd-ucode-2012-09-10.tar) = 21845c6cafa99704cdf4862b55e899ca88ed432d57f4d09ad6a5c3d2e186b718
+SIZE (amd-ucode-2012-09-10.tar) = 71680
Index: sysutils/devcpu-data/files/Makefile
===================================================================
--- sysutils/devcpu-data/files/Makefile	(revision 0)
+++ sysutils/devcpu-data/files/Makefile	(working copy)
@@ -0,0 +1,19 @@
+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
Index: sysutils/devcpu-data/files/ucode-tool.c
===================================================================
--- sysutils/devcpu-data/files/ucode-tool.c	(revision 0)
+++ sysutils/devcpu-data/files/ucode-tool.c	(working copy)
@@ -0,0 +1,228 @@
+/*-
+ * 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.
+ */
+
+#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);
+}


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201306112123.r5BLNSxO026686>