xref: /titanic_41/usr/src/common/ucode/ucode_utils.c (revision d62bc4badc1c1f1549c961cfb8b420e650e1272b)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/ucode.h>
31 #ifdef	_KERNEL
32 #include <sys/systm.h>
33 #else
34 #include <strings.h>
35 #endif
36 
37 /*
38  * Refer to
39  *	Intel 64 and IA-32 Architectures Software Developers's Manual
40  *		Chapter 9.11 Microcode Update Facilities
41  * for details.
42  */
43 
44 /*
45  * Validates the microcode header.
46  * Returns EM_OK on success, EM_HEADER on failure.
47  */
48 ucode_errno_t
49 ucode_header_validate(ucode_header_t *uhp)
50 {
51 	uint32_t header_size, body_size, total_size;
52 
53 	if (uhp == NULL)
54 		return (EM_HEADER);
55 
56 	/*
57 	 * The only header version number supported is 1.
58 	 */
59 	if (uhp->uh_header_ver != 0x1)
60 		return (EM_HEADER);
61 
62 	header_size = UCODE_HEADER_SIZE;
63 	total_size = UCODE_TOTAL_SIZE(uhp->uh_total_size);
64 	body_size = UCODE_BODY_SIZE(uhp->uh_body_size);
65 
66 	/*
67 	 * The body size field of the microcode code header specifies the size
68 	 * of the encrypted data section, its value must be a multiple of the
69 	 * size of DWORD.  The total size field must be in multiples of 1K
70 	 * bytes.
71 	 */
72 	if ((body_size % sizeof (int)) ||
73 	    (total_size < (header_size + body_size)) ||
74 	    (total_size % UCODE_KB(1)))
75 
76 		return (EM_HEADER);
77 
78 	/*
79 	 * Sanity check to avoid reading bogus files
80 	 */
81 	if (total_size < UCODE_MIN_SIZE || total_size > UCODE_MAX_SIZE)
82 		return (EM_HEADER);
83 
84 	/*
85 	 * If there is extended signature table, total_size is the sum of
86 	 *	header_size
87 	 *	body_size
88 	 *	sizeof (struct ucode_ext_table)
89 	 *	n * sizeof (struct ucode_ext_sig)
90 	 * where n is indicated by uet_count in struct ucode_ext_table.
91 	 */
92 	if (total_size > (header_size + body_size)) {
93 		if ((total_size - body_size - header_size -
94 		    UCODE_EXT_TABLE_SIZE) % UCODE_EXT_SIG_SIZE) {
95 
96 			return (EM_HEADER);
97 		}
98 	}
99 
100 	return (EM_OK);
101 }
102 
103 /*
104  * Returns checksum.
105  */
106 uint32_t
107 ucode_checksum(uint32_t sum, uint32_t size, uint8_t *code)
108 {
109 	int i;
110 	uint32_t *lcode = (uint32_t *)(intptr_t)code;
111 
112 	i = size >> 2;
113 	while (i--)
114 		sum += lcode[i];
115 
116 	return (sum);
117 }
118 
119 ucode_errno_t
120 ucode_validate(uint8_t *ucodep, int size)
121 {
122 	uint32_t header_size = UCODE_HEADER_SIZE;
123 	int remaining;
124 
125 	if (ucodep == NULL || size <= 0)
126 		return (EM_INVALIDARG);
127 
128 	for (remaining = size; remaining > 0; ) {
129 		uint32_t total_size, body_size, ext_size;
130 		ucode_header_t *uhp;
131 		uint8_t *curbuf = &ucodep[size - remaining];
132 		ucode_errno_t rc;
133 
134 		uhp = (ucode_header_t *)(intptr_t)curbuf;
135 
136 		if ((rc = ucode_header_validate(uhp)) != EM_OK)
137 			return (rc);
138 
139 		total_size = UCODE_TOTAL_SIZE(uhp->uh_total_size);
140 
141 		if (ucode_checksum(0, total_size, curbuf))
142 			return (EM_CHECKSUM);
143 
144 		body_size = UCODE_BODY_SIZE(uhp->uh_body_size);
145 		ext_size = total_size - (header_size + body_size);
146 
147 		if (ext_size > 0) {
148 			uint32_t i;
149 
150 			if (ucode_checksum(0, ext_size,
151 			    &curbuf[header_size + body_size])) {
152 				return (EM_CHECKSUM);
153 			}
154 
155 			ext_size -= UCODE_EXT_TABLE_SIZE;
156 			for (i = 0; i < ext_size / UCODE_EXT_SIG_SIZE; i++) {
157 				if (ucode_checksum(0, UCODE_EXT_SIG_SIZE,
158 				    &curbuf[total_size - ext_size +
159 				    i * UCODE_EXT_SIG_SIZE])) {
160 
161 					return (EM_CHECKSUM);
162 				}
163 			}
164 		}
165 
166 		remaining -= total_size;
167 	}
168 	return (EM_OK);
169 }
170