xref: /titanic_41/usr/src/common/ucode/ucode_utils.c (revision 7ff178cd8db129d385d3177eb20744d3b6efc59b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/ucode.h>
29 #ifdef	_KERNEL
30 #include <sys/systm.h>
31 #else
32 #include <strings.h>
33 #endif
34 
35 /*
36  * Refer to
37  *	Intel 64 and IA-32 Architectures Software Developers's Manual
38  *		Chapter 9.11 Microcode Update Facilities
39  * for details.
40  */
41 
42 /*
43  * Validates the microcode header.
44  * Returns EM_OK on success, EM_HEADER on failure.
45  */
46 ucode_errno_t
47 ucode_header_validate_intel(ucode_header_intel_t *uhp)
48 {
49 	uint32_t header_size, body_size, total_size;
50 
51 	if (uhp == NULL)
52 		return (EM_HEADER);
53 
54 	/*
55 	 * The only header version number supported is 1.
56 	 */
57 	if (uhp->uh_header_ver != 0x1)
58 		return (EM_HEADER);
59 
60 	header_size = UCODE_HEADER_SIZE_INTEL;
61 	total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
62 	body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
63 
64 	/*
65 	 * The body size field of the microcode code header specifies the size
66 	 * of the encrypted data section, its value must be a multiple of the
67 	 * size of DWORD.  The total size field must be in multiples of 1K
68 	 * bytes.
69 	 */
70 	if ((body_size % sizeof (int)) ||
71 	    (total_size < (header_size + body_size)) ||
72 	    (total_size % UCODE_KB(1)))
73 
74 		return (EM_HEADER);
75 
76 	/*
77 	 * Sanity check to avoid reading bogus files
78 	 */
79 	if (total_size < UCODE_MIN_SIZE || total_size > UCODE_MAX_SIZE)
80 		return (EM_HEADER);
81 
82 	/*
83 	 * If there is extended signature table, total_size is the sum of
84 	 *	header_size
85 	 *	body_size
86 	 *	sizeof (struct ucode_ext_table)
87 	 *	n * sizeof (struct ucode_ext_sig)
88 	 * where n is indicated by uet_count in struct ucode_ext_table.
89 	 */
90 	if (total_size > (header_size + body_size)) {
91 		if ((total_size - body_size - header_size -
92 		    UCODE_EXT_TABLE_SIZE_INTEL) % UCODE_EXT_SIG_SIZE_INTEL) {
93 
94 			return (EM_HEADER);
95 		}
96 	}
97 
98 	return (EM_OK);
99 }
100 
101 /*
102  * Returns checksum.
103  */
104 uint32_t
105 ucode_checksum_intel(uint32_t sum, uint32_t size, uint8_t *code)
106 {
107 	int i;
108 	uint32_t *lcode = (uint32_t *)(intptr_t)code;
109 
110 	i = size >> 2;
111 	while (i--)
112 		sum += lcode[i];
113 
114 	return (sum);
115 }
116 
117 ucode_errno_t
118 ucode_validate_amd(uint8_t *ucodep, int size)
119 {
120 	/* LINTED: pointer alignment */
121 	uint32_t *ptr = (uint32_t *)ucodep;
122 	uint32_t count;
123 
124 	if (ucodep == NULL || size <= 0)
125 		return (EM_INVALIDARG);
126 
127 	/* Magic Number: "AMD\0" */
128 	size -= 4;
129 	if (*ptr++ != 0x00414d44)
130 		return (EM_FILEFORMAT);
131 
132 	/* equivalence table */
133 	size -= 4;
134 	if (*ptr++)
135 		return (EM_FILEFORMAT);
136 
137 	size -= 4;
138 	if (((count = *ptr++) > size) || (count % 16))
139 		return (EM_FILEFORMAT);
140 
141 	/* LINTED: pointer alignment */
142 	ptr = (uint32_t *)(((uint8_t *)ptr) + count);
143 	size -= count;
144 
145 	/*
146 	 * minimum valid size:
147 	 * - type and size fields (8 bytes)
148 	 * - patch header (64 bytes)
149 	 * - one patch triad (28 bytes)
150 	 */
151 	while (size >= 100) {
152 		/* microcode patch */
153 		size -= 4;
154 		if (*ptr++ != 1)
155 			return (EM_FILEFORMAT);
156 
157 		size -= 4;
158 		if (((count = *ptr++) > size) ||
159 		    ((count - sizeof (ucode_header_amd_t)) % 28))
160 			return (EM_FILEFORMAT);
161 
162 		/* LINTED: pointer alignment */
163 		ptr = (uint32_t *)(((uint8_t *)ptr) + count);
164 		size -= count;
165 	}
166 
167 	if (size)
168 		return (EM_FILEFORMAT);
169 
170 	return (EM_OK);
171 }
172 
173 ucode_errno_t
174 ucode_validate_intel(uint8_t *ucodep, int size)
175 {
176 	uint32_t header_size = UCODE_HEADER_SIZE_INTEL;
177 	int remaining;
178 
179 	if (ucodep == NULL || size <= 0)
180 		return (EM_INVALIDARG);
181 
182 	for (remaining = size; remaining > 0; ) {
183 		uint32_t total_size, body_size, ext_size;
184 		ucode_header_intel_t *uhp;
185 		uint8_t *curbuf = &ucodep[size - remaining];
186 		ucode_errno_t rc;
187 
188 		uhp = (ucode_header_intel_t *)(intptr_t)curbuf;
189 
190 		if ((rc = ucode_header_validate_intel(uhp)) != EM_OK)
191 			return (rc);
192 
193 		total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
194 
195 		if (ucode_checksum_intel(0, total_size, curbuf))
196 			return (EM_CHECKSUM);
197 
198 		body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
199 		ext_size = total_size - (header_size + body_size);
200 
201 		if (ext_size > 0) {
202 			uint32_t i;
203 
204 			if (ucode_checksum_intel(0, ext_size,
205 			    &curbuf[header_size + body_size])) {
206 				return (EM_CHECKSUM);
207 			}
208 
209 			ext_size -= UCODE_EXT_TABLE_SIZE_INTEL;
210 			for (i = 0; i < ext_size / UCODE_EXT_SIG_SIZE_INTEL;
211 			    i++) {
212 				if (ucode_checksum_intel(0,
213 				    UCODE_EXT_SIG_SIZE_INTEL,
214 				    &curbuf[total_size - ext_size +
215 				    i * UCODE_EXT_SIG_SIZE_INTEL])) {
216 
217 					return (EM_CHECKSUM);
218 				}
219 			}
220 		}
221 
222 		remaining -= total_size;
223 	}
224 	return (EM_OK);
225 }
226