1181430Sstas/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4181430Sstas * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>.
5181430Sstas * All rights reserved.
6181430Sstas *
7181430Sstas * Redistribution and use in source and binary forms, with or without
8181430Sstas * modification, are permitted provided that the following conditions
9181430Sstas * are met:
10181430Sstas * 1. Redistributions of source code must retain the above copyright
11181430Sstas *    notice, this list of conditions and the following disclaimer.
12181430Sstas * 2. Redistributions in binary form must reproduce the above copyright
13181430Sstas *    notice, this list of conditions and the following disclaimer in the
14181430Sstas *    documentation and/or other materials provided with the distribution.
15181430Sstas *
16181430Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17181430Sstas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18181430Sstas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19181430Sstas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20181430Sstas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21181430Sstas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22181430Sstas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23181430Sstas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24181430Sstas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25181430Sstas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26181430Sstas */
27181430Sstas
28181430Sstas#include <sys/cdefs.h>
29181430Sstas__FBSDID("$FreeBSD: stable/11/usr.sbin/cpucontrol/amd.c 330449 2018-03-05 07:26:05Z eadler $");
30181430Sstas
31181430Sstas#include <assert.h>
32181430Sstas#include <stdio.h>
33181430Sstas#include <stdlib.h>
34181430Sstas#include <string.h>
35181430Sstas#include <unistd.h>
36181430Sstas#include <fcntl.h>
37181430Sstas#include <err.h>
38181430Sstas
39181430Sstas#include <sys/types.h>
40181430Sstas#include <sys/stat.h>
41181430Sstas#include <sys/mman.h>
42181430Sstas#include <sys/ioctl.h>
43181430Sstas#include <sys/ioccom.h>
44181430Sstas#include <sys/cpuctl.h>
45181430Sstas
46181430Sstas#include <machine/cpufunc.h>
47181430Sstas#include <machine/specialreg.h>
48181430Sstas
49181430Sstas#include "cpucontrol.h"
50181430Sstas#include "amd.h"
51181430Sstas
52181430Sstasint
53181430Sstasamd_probe(int fd)
54181430Sstas{
55181430Sstas	char vendor[13];
56181430Sstas	int error;
57181430Sstas	cpuctl_cpuid_args_t idargs = {
58181430Sstas		.level  = 0,
59181430Sstas	};
60181430Sstas
61181430Sstas	error = ioctl(fd, CPUCTL_CPUID, &idargs);
62181430Sstas	if (error < 0) {
63181430Sstas		WARN(0, "ioctl()");
64181430Sstas		return (1);
65181430Sstas	}
66181430Sstas	((uint32_t *)vendor)[0] = idargs.data[1];
67181430Sstas	((uint32_t *)vendor)[1] = idargs.data[3];
68181430Sstas	((uint32_t *)vendor)[2] = idargs.data[2];
69181430Sstas	vendor[12] = '\0';
70181430Sstas	if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0)
71181430Sstas		return (1);
72181430Sstas	return (0);
73181430Sstas}
74181430Sstas
75181430Sstasvoid
76181430Sstasamd_update(const char *dev, const char *path)
77181430Sstas{
78181430Sstas	int fd, devfd;
79181430Sstas	unsigned int i;
80181430Sstas	struct stat st;
81181430Sstas	uint32_t *fw_image;
82181430Sstas	amd_fw_header_t *fw_header;
83181430Sstas	uint32_t sum;
84181430Sstas	uint32_t signature;
85181430Sstas	uint32_t *fw_data;
86181430Sstas	size_t fw_size;
87181430Sstas	cpuctl_cpuid_args_t idargs = {
88181430Sstas		.level  = 1,	/* Request signature. */
89181430Sstas	};
90181430Sstas	cpuctl_update_args_t args;
91181430Sstas	int error;
92181430Sstas
93181430Sstas	assert(path);
94181430Sstas	assert(dev);
95181430Sstas
96181430Sstas	fd  = -1;
97181430Sstas	fw_image = MAP_FAILED;
98181430Sstas	devfd = open(dev, O_RDWR);
99181430Sstas	if (devfd < 0) {
100181430Sstas		WARN(0, "could not open %s for writing", dev);
101181430Sstas		return;
102181430Sstas	}
103181430Sstas	error = ioctl(devfd, CPUCTL_CPUID, &idargs);
104181430Sstas	if (error < 0) {
105181430Sstas		WARN(0, "ioctl()");
106181430Sstas		goto fail;
107181430Sstas	}
108181430Sstas	signature = idargs.data[0];
109181430Sstas	WARNX(2, "found cpu family %#x model %#x "
110181430Sstas	    "stepping %#x extfamily %#x extmodel %#x.",
111181430Sstas	    (signature >> 8) & 0x0f, (signature >> 4) & 0x0f,
112181430Sstas	    (signature >> 0) & 0x0f, (signature >> 20) & 0xff,
113181430Sstas	    (signature >> 16) & 0x0f);
114181430Sstas
115181430Sstas	/*
116181430Sstas	 * Open the firmware file.
117181430Sstas	 */
118181430Sstas	fd = open(path, O_RDONLY, 0);
119181430Sstas	if (fd < 0) {
120181430Sstas		WARN(0, "open(%s)", path);
121181430Sstas		goto fail;
122181430Sstas	}
123181430Sstas	error = fstat(fd, &st);
124181430Sstas	if (error != 0) {
125181430Sstas		WARN(0, "fstat(%s)", path);
126181430Sstas		goto fail;
127181430Sstas	}
128181430Sstas	if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) {
129181430Sstas		WARNX(2, "file too short: %s", path);
130181430Sstas		goto fail;
131181430Sstas	}
132181430Sstas	/*
133181430Sstas	 * mmap the whole image.
134181430Sstas	 */
135181430Sstas	fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ,
136181430Sstas	    MAP_PRIVATE, fd, 0);
137181430Sstas	if  (fw_image == MAP_FAILED) {
138181430Sstas		WARN(0, "mmap(%s)", path);
139181430Sstas		goto fail;
140181430Sstas	}
141181430Sstas	fw_header = (amd_fw_header_t *)fw_image;
142181430Sstas	if ((fw_header->magic >> 8) != AMD_MAGIC) {
143181430Sstas		WARNX(2, "%s is not a valid amd firmware: version mismatch",
144181430Sstas		    path);
145181430Sstas		goto fail;
146181430Sstas	}
147181430Sstas	fw_data = (uint32_t *)(fw_header + 1);
148181430Sstas	fw_size = (st.st_size - sizeof(*fw_header)) / sizeof(uint32_t);
149181430Sstas
150181430Sstas	/*
151181430Sstas	 * Check the primary checksum.
152181430Sstas	 */
153181430Sstas	sum = 0;
154181430Sstas	for (i = 0; i < fw_size; i++)
155181430Sstas		sum += fw_data[i];
156181430Sstas	if (sum != fw_header->checksum) {
157181430Sstas		WARNX(2, "%s: update data checksum invalid", path);
158181430Sstas		goto fail;
159181430Sstas	}
160181430Sstas	if (signature == fw_header->signature) {
161181430Sstas		fprintf(stderr, "%s: updating cpu %s... ", path, dev);
162181430Sstas
163181430Sstas		args.data = fw_image;
164181430Sstas		args.size = st.st_size;
165236504Savg		error = ioctl(devfd, CPUCTL_UPDATE, &args);
166181430Sstas		if (error < 0) {
167181430Sstas			fprintf(stderr, "failed.\n");
168181430Sstas			warn("ioctl()");
169181430Sstas			goto fail;
170181430Sstas		}
171181430Sstas		fprintf(stderr, "done.\n");
172181430Sstas	}
173181430Sstas
174181430Sstasfail:
175181430Sstas	if (fd >= 0)
176181430Sstas		close(fd);
177181430Sstas	if (devfd >= 0)
178181430Sstas		close(devfd);
179181430Sstas	if (fw_image != MAP_FAILED)
180181430Sstas		if(munmap(fw_image, st.st_size) != 0)
181181430Sstas			warn("munmap(%s)", path);
182181430Sstas	return;
183181430Sstas}
184