2 * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/ioccom.h>
42 #include <sys/cpuctl.h>
44 #include <machine/cpufunc.h>
45 #include <machine/specialreg.h>
47 #include "cpucontrol.h"
55 cpuctl_cpuid_args_t idargs
= {
59 error
= ioctl(fd
, CPUCTL_CPUID
, &idargs
);
64 ((uint32_t *)vendor
)[0] = idargs
.data
[1];
65 ((uint32_t *)vendor
)[1] = idargs
.data
[3];
66 ((uint32_t *)vendor
)[2] = idargs
.data
[2];
68 if (strncmp(vendor
, AMD_VENDOR_ID
, sizeof(AMD_VENDOR_ID
)) != 0)
74 amd_update(const char *dev
, const char *path
)
80 amd_fw_header_t
*fw_header
;
85 cpuctl_cpuid_args_t idargs
= {
86 .level
= 1, /* Request signature. */
88 cpuctl_update_args_t args
;
96 fw_image
= MAP_FAILED
;
98 devfd
= open(dev
, O_RDWR
);
100 WARN(0, "could not open %s for writing", dev
);
103 error
= ioctl(devfd
, CPUCTL_CPUID
, &idargs
);
108 signature
= idargs
.data
[0];
109 WARNX(2, "found cpu family %#x model %#x "
110 "stepping %#x extfamily %#x extmodel %#x.",
111 (signature
>> 8) & 0x0f, (signature
>> 4) & 0x0f,
112 (signature
>> 0) & 0x0f, (signature
>> 20) & 0xff,
113 (signature
>> 16) & 0x0f);
116 * Open the firmware file.
118 fd
= open(path
, O_RDONLY
, 0);
120 WARN(0, "open(%s)", path
);
123 error
= fstat(fd
, &st
);
125 WARN(0, "fstat(%s)", path
);
128 if (st
.st_size
< 0 || (unsigned)st
.st_size
< sizeof(*fw_header
)) {
129 WARNX(2, "file too short: %s", path
);
133 * mmap the whole image.
135 fw_image
= (uint32_t *)mmap(NULL
, st
.st_size
, PROT_READ
,
137 if (fw_image
== MAP_FAILED
) {
138 WARN(0, "mmap(%s)", path
);
141 fw_header
= (amd_fw_header_t
*)fw_image
;
142 if ((fw_header
->magic
>> 8) != AMD_MAGIC
) {
143 WARNX(2, "%s is not a valid amd firmware: version mismatch",
147 fw_data
= (uint32_t *)(fw_header
+ 1);
148 fw_size
= (st
.st_size
- sizeof(*fw_header
)) / sizeof(uint32_t);
151 * Check the primary checksum.
154 for (i
= 0; i
< fw_size
; i
++)
156 if (sum
!= fw_header
->checksum
) {
157 WARNX(2, "%s: update data checksum invalid", path
);
160 if (signature
== fw_header
->signature
) {
161 fprintf(stderr
, "%s: updating cpu %s... ", path
, dev
);
163 args
.data
= fw_image
;
164 args
.size
= st
.st_size
;
165 error
= ioctl(fd
, CPUCTL_UPDATE
, &args
);
167 fprintf(stderr
, "failed.\n");
171 fprintf(stderr
, "done.\n");
179 if (fw_image
!= MAP_FAILED
)
180 if(munmap(fw_image
, st
.st_size
) != 0)
181 warn("munmap(%s)", path
);