xref: /titanic_44/usr/src/common/ucode/ucode_utils.c (revision 5e53ed34920bc03b619b50f459f2475e7bf80b6f)
12449e17fSsherrym /*
22449e17fSsherrym  * CDDL HEADER START
32449e17fSsherrym  *
42449e17fSsherrym  * The contents of this file are subject to the terms of the
52449e17fSsherrym  * Common Development and Distribution License (the "License").
62449e17fSsherrym  * You may not use this file except in compliance with the License.
72449e17fSsherrym  *
82449e17fSsherrym  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92449e17fSsherrym  * or http://www.opensolaris.org/os/licensing.
102449e17fSsherrym  * See the License for the specific language governing permissions
112449e17fSsherrym  * and limitations under the License.
122449e17fSsherrym  *
132449e17fSsherrym  * When distributing Covered Code, include this CDDL HEADER in each
142449e17fSsherrym  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152449e17fSsherrym  * If applicable, add the following below this CDDL HEADER, with the
162449e17fSsherrym  * fields enclosed by brackets "[]" replaced with your own identifying
172449e17fSsherrym  * information: Portions Copyright [yyyy] [name of copyright owner]
182449e17fSsherrym  *
192449e17fSsherrym  * CDDL HEADER END
202449e17fSsherrym  */
212449e17fSsherrym 
222449e17fSsherrym /*
23adc586deSMark Johnson  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
242449e17fSsherrym  * Use is subject to license terms.
252449e17fSsherrym  */
262449e17fSsherrym 
272449e17fSsherrym #include <sys/types.h>
282449e17fSsherrym #include <sys/ucode.h>
292449e17fSsherrym #ifdef	_KERNEL
302449e17fSsherrym #include <sys/systm.h>
312449e17fSsherrym #else
322449e17fSsherrym #include <strings.h>
332449e17fSsherrym #endif
342449e17fSsherrym 
352449e17fSsherrym /*
362449e17fSsherrym  * Refer to
372449e17fSsherrym  *	Intel 64 and IA-32 Architectures Software Developers's Manual
382449e17fSsherrym  *		Chapter 9.11 Microcode Update Facilities
392449e17fSsherrym  * for details.
402449e17fSsherrym  */
412449e17fSsherrym 
422449e17fSsherrym /*
432449e17fSsherrym  * Validates the microcode header.
442449e17fSsherrym  * Returns EM_OK on success, EM_HEADER on failure.
452449e17fSsherrym  */
462449e17fSsherrym ucode_errno_t
ucode_header_validate_intel(ucode_header_intel_t * uhp)47adc586deSMark Johnson ucode_header_validate_intel(ucode_header_intel_t *uhp)
482449e17fSsherrym {
492449e17fSsherrym 	uint32_t header_size, body_size, total_size;
502449e17fSsherrym 
512449e17fSsherrym 	if (uhp == NULL)
522449e17fSsherrym 		return (EM_HEADER);
532449e17fSsherrym 
542449e17fSsherrym 	/*
552449e17fSsherrym 	 * The only header version number supported is 1.
562449e17fSsherrym 	 */
572449e17fSsherrym 	if (uhp->uh_header_ver != 0x1)
582449e17fSsherrym 		return (EM_HEADER);
592449e17fSsherrym 
60adc586deSMark Johnson 	header_size = UCODE_HEADER_SIZE_INTEL;
61adc586deSMark Johnson 	total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
62adc586deSMark Johnson 	body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
632449e17fSsherrym 
642449e17fSsherrym 	/*
652449e17fSsherrym 	 * The body size field of the microcode code header specifies the size
662449e17fSsherrym 	 * of the encrypted data section, its value must be a multiple of the
672449e17fSsherrym 	 * size of DWORD.  The total size field must be in multiples of 1K
682449e17fSsherrym 	 * bytes.
692449e17fSsherrym 	 */
702449e17fSsherrym 	if ((body_size % sizeof (int)) ||
712449e17fSsherrym 	    (total_size < (header_size + body_size)) ||
722449e17fSsherrym 	    (total_size % UCODE_KB(1)))
732449e17fSsherrym 
742449e17fSsherrym 		return (EM_HEADER);
752449e17fSsherrym 
762449e17fSsherrym 	/*
772449e17fSsherrym 	 * Sanity check to avoid reading bogus files
782449e17fSsherrym 	 */
792449e17fSsherrym 	if (total_size < UCODE_MIN_SIZE || total_size > UCODE_MAX_SIZE)
802449e17fSsherrym 		return (EM_HEADER);
812449e17fSsherrym 
822449e17fSsherrym 	/*
832449e17fSsherrym 	 * If there is extended signature table, total_size is the sum of
842449e17fSsherrym 	 *	header_size
852449e17fSsherrym 	 *	body_size
862449e17fSsherrym 	 *	sizeof (struct ucode_ext_table)
872449e17fSsherrym 	 *	n * sizeof (struct ucode_ext_sig)
882449e17fSsherrym 	 * where n is indicated by uet_count in struct ucode_ext_table.
892449e17fSsherrym 	 */
902449e17fSsherrym 	if (total_size > (header_size + body_size)) {
912449e17fSsherrym 		if ((total_size - body_size - header_size -
92adc586deSMark Johnson 		    UCODE_EXT_TABLE_SIZE_INTEL) % UCODE_EXT_SIG_SIZE_INTEL) {
932449e17fSsherrym 
942449e17fSsherrym 			return (EM_HEADER);
952449e17fSsherrym 		}
962449e17fSsherrym 	}
972449e17fSsherrym 
982449e17fSsherrym 	return (EM_OK);
992449e17fSsherrym }
1002449e17fSsherrym 
1012449e17fSsherrym /*
1022449e17fSsherrym  * Returns checksum.
1032449e17fSsherrym  */
1042449e17fSsherrym uint32_t
ucode_checksum_intel(uint32_t sum,uint32_t size,uint8_t * code)105adc586deSMark Johnson ucode_checksum_intel(uint32_t sum, uint32_t size, uint8_t *code)
1062449e17fSsherrym {
1072449e17fSsherrym 	int i;
1082449e17fSsherrym 	uint32_t *lcode = (uint32_t *)(intptr_t)code;
1092449e17fSsherrym 
1102449e17fSsherrym 	i = size >> 2;
1112449e17fSsherrym 	while (i--)
1122449e17fSsherrym 		sum += lcode[i];
1132449e17fSsherrym 
1142449e17fSsherrym 	return (sum);
1152449e17fSsherrym }
1162449e17fSsherrym 
1172449e17fSsherrym ucode_errno_t
ucode_validate_amd(uint8_t * ucodep,int size)118adc586deSMark Johnson ucode_validate_amd(uint8_t *ucodep, int size)
1192449e17fSsherrym {
120adc586deSMark Johnson 	/* LINTED: pointer alignment */
121adc586deSMark Johnson 	uint32_t *ptr = (uint32_t *)ucodep;
122adc586deSMark Johnson 	uint32_t count;
123adc586deSMark Johnson 
124adc586deSMark Johnson 	if (ucodep == NULL || size <= 0)
125adc586deSMark Johnson 		return (EM_INVALIDARG);
126adc586deSMark Johnson 
127adc586deSMark Johnson 	/* Magic Number: "AMD\0" */
128adc586deSMark Johnson 	size -= 4;
129adc586deSMark Johnson 	if (*ptr++ != 0x00414d44)
130adc586deSMark Johnson 		return (EM_FILEFORMAT);
131adc586deSMark Johnson 
132adc586deSMark Johnson 	/* equivalence table */
133adc586deSMark Johnson 	size -= 4;
134adc586deSMark Johnson 	if (*ptr++)
135adc586deSMark Johnson 		return (EM_FILEFORMAT);
136adc586deSMark Johnson 
137adc586deSMark Johnson 	size -= 4;
138adc586deSMark Johnson 	if (((count = *ptr++) > size) || (count % 16))
139adc586deSMark Johnson 		return (EM_FILEFORMAT);
140adc586deSMark Johnson 
141adc586deSMark Johnson 	/* LINTED: pointer alignment */
142adc586deSMark Johnson 	ptr = (uint32_t *)(((uint8_t *)ptr) + count);
143adc586deSMark Johnson 	size -= count;
144adc586deSMark Johnson 
145*5e53ed34SHans Rosenfeld 	while (size > 8) {
146adc586deSMark Johnson 		/* microcode patch */
147adc586deSMark Johnson 		size -= 4;
148adc586deSMark Johnson 		if (*ptr++ != 1)
149adc586deSMark Johnson 			return (EM_FILEFORMAT);
150adc586deSMark Johnson 
151adc586deSMark Johnson 		size -= 4;
152*5e53ed34SHans Rosenfeld 		if (((count = *ptr++) > size))
153adc586deSMark Johnson 			return (EM_FILEFORMAT);
154adc586deSMark Johnson 
155adc586deSMark Johnson 		/* LINTED: pointer alignment */
156adc586deSMark Johnson 		ptr = (uint32_t *)(((uint8_t *)ptr) + count);
157adc586deSMark Johnson 		size -= count;
158adc586deSMark Johnson 	}
159adc586deSMark Johnson 
160adc586deSMark Johnson 	if (size)
161adc586deSMark Johnson 		return (EM_FILEFORMAT);
162adc586deSMark Johnson 
163adc586deSMark Johnson 	return (EM_OK);
164adc586deSMark Johnson }
165adc586deSMark Johnson 
166adc586deSMark Johnson ucode_errno_t
ucode_validate_intel(uint8_t * ucodep,int size)167adc586deSMark Johnson ucode_validate_intel(uint8_t *ucodep, int size)
168adc586deSMark Johnson {
169adc586deSMark Johnson 	uint32_t header_size = UCODE_HEADER_SIZE_INTEL;
1702449e17fSsherrym 	int remaining;
1712449e17fSsherrym 
1722449e17fSsherrym 	if (ucodep == NULL || size <= 0)
1732449e17fSsherrym 		return (EM_INVALIDARG);
1742449e17fSsherrym 
1752449e17fSsherrym 	for (remaining = size; remaining > 0; ) {
1762449e17fSsherrym 		uint32_t total_size, body_size, ext_size;
177adc586deSMark Johnson 		ucode_header_intel_t *uhp;
1782449e17fSsherrym 		uint8_t *curbuf = &ucodep[size - remaining];
1792449e17fSsherrym 		ucode_errno_t rc;
1802449e17fSsherrym 
181adc586deSMark Johnson 		uhp = (ucode_header_intel_t *)(intptr_t)curbuf;
1822449e17fSsherrym 
183adc586deSMark Johnson 		if ((rc = ucode_header_validate_intel(uhp)) != EM_OK)
1842449e17fSsherrym 			return (rc);
1852449e17fSsherrym 
186adc586deSMark Johnson 		total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
1872449e17fSsherrym 
188adc586deSMark Johnson 		if (ucode_checksum_intel(0, total_size, curbuf))
1892449e17fSsherrym 			return (EM_CHECKSUM);
1902449e17fSsherrym 
191adc586deSMark Johnson 		body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
1922449e17fSsherrym 		ext_size = total_size - (header_size + body_size);
1932449e17fSsherrym 
1942449e17fSsherrym 		if (ext_size > 0) {
1952449e17fSsherrym 			uint32_t i;
1962449e17fSsherrym 
197adc586deSMark Johnson 			if (ucode_checksum_intel(0, ext_size,
1982449e17fSsherrym 			    &curbuf[header_size + body_size])) {
1992449e17fSsherrym 				return (EM_CHECKSUM);
2002449e17fSsherrym 			}
2012449e17fSsherrym 
202adc586deSMark Johnson 			ext_size -= UCODE_EXT_TABLE_SIZE_INTEL;
203adc586deSMark Johnson 			for (i = 0; i < ext_size / UCODE_EXT_SIG_SIZE_INTEL;
204adc586deSMark Johnson 			    i++) {
205adc586deSMark Johnson 				if (ucode_checksum_intel(0,
206adc586deSMark Johnson 				    UCODE_EXT_SIG_SIZE_INTEL,
2072449e17fSsherrym 				    &curbuf[total_size - ext_size +
208adc586deSMark Johnson 				    i * UCODE_EXT_SIG_SIZE_INTEL])) {
2092449e17fSsherrym 
2102449e17fSsherrym 					return (EM_CHECKSUM);
2112449e17fSsherrym 				}
2122449e17fSsherrym 			}
2132449e17fSsherrym 		}
2142449e17fSsherrym 
2152449e17fSsherrym 		remaining -= total_size;
2162449e17fSsherrym 	}
2172449e17fSsherrym 	return (EM_OK);
2182449e17fSsherrym }
219