1 /******************************************************************************
2 * Intel Management Engine Interface (Intel MEI) Linux driver
3 * Intel MEI Interface Header
5 * This file is provided under a dual BSD/GPLv2 license. When using or
6 * redistributing this file, you may do so under either license.
10 * Copyright(c) 2012 Intel Corporation. All rights reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called LICENSE.GPL.
29 * Contact Information:
31 * linux-mei@linux.intel.com
32 * http://www.intel.com
36 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
37 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
43 * * Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * * Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in
47 * the documentation and/or other materials provided with the
49 * * Neither the name Intel Corporation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
59 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
63 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *****************************************************************************/
71 #include <sys/ioctl.h>
76 #include <bits/wordsize.h>
77 #include <linux/mei.h>
79 /*****************************************************************************
80 * Intel Management Engine Interface
81 *****************************************************************************/
83 #define mei_msg(_me, fmt, ARGS...) do { \
85 fprintf(stderr, fmt, ##ARGS); \
88 #define mei_err(_me, fmt, ARGS...) do { \
89 fprintf(stderr, "Error: " fmt, ##ARGS); \
96 unsigned int buf_size
;
97 unsigned char prot_ver
;
101 static void mei_deinit(struct mei
*cl
)
108 cl
->initialized
= false;
111 static bool mei_init(struct mei
*me
, const uuid_le
*guid
,
112 unsigned char req_protocol_version
, bool verbose
)
115 struct mei_client
*cl
;
116 struct mei_connect_client_data data
;
120 me
->verbose
= verbose
;
122 me
->fd
= open("/dev/mei", O_RDWR
);
124 mei_err(me
, "Cannot establish a handle to the Intel MEI driver\n");
127 memcpy(&me
->guid
, guid
, sizeof(*guid
));
128 memset(&data
, 0, sizeof(data
));
129 me
->initialized
= true;
131 memcpy(&data
.in_client_uuid
, &me
->guid
, sizeof(me
->guid
));
132 result
= ioctl(me
->fd
, IOCTL_MEI_CONNECT_CLIENT
, &data
);
134 mei_err(me
, "IOCTL_MEI_CONNECT_CLIENT receive message. err=%d\n", result
);
137 cl
= &data
.out_client_properties
;
138 mei_msg(me
, "max_message_length %d\n", cl
->max_msg_length
);
139 mei_msg(me
, "protocol_version %d\n", cl
->protocol_version
);
141 if ((req_protocol_version
> 0) &&
142 (cl
->protocol_version
!= req_protocol_version
)) {
143 mei_err(me
, "Intel MEI protocol version not supported\n");
147 me
->buf_size
= cl
->max_msg_length
;
148 me
->prot_ver
= cl
->protocol_version
;
156 static ssize_t
mei_recv_msg(struct mei
*me
, unsigned char *buffer
,
157 ssize_t len
, unsigned long timeout
)
161 mei_msg(me
, "call read length = %zd\n", len
);
163 rc
= read(me
->fd
, buffer
, len
);
165 mei_err(me
, "read failed with status %zd %s\n",
166 rc
, strerror(errno
));
169 mei_msg(me
, "read succeeded with result %zd\n", rc
);
174 static ssize_t
mei_send_msg(struct mei
*me
, const unsigned char *buffer
,
175 ssize_t len
, unsigned long timeout
)
182 tv
.tv_sec
= timeout
/ 1000;
183 tv
.tv_usec
= (timeout
% 1000) * 1000000;
185 mei_msg(me
, "call write length = %zd\n", len
);
187 written
= write(me
->fd
, buffer
, len
);
190 mei_err(me
, "write failed with status %zd %s\n",
191 written
, strerror(errno
));
196 FD_SET(me
->fd
, &set
);
197 rc
= select(me
->fd
+ 1 , &set
, NULL
, NULL
, &tv
);
198 if (rc
> 0 && FD_ISSET(me
->fd
, &set
)) {
199 mei_msg(me
, "write success\n");
200 } else if (rc
== 0) {
201 mei_err(me
, "write failed on timeout with status\n");
203 } else { /* rc < 0 */
204 mei_err(me
, "write failed on select with status %zd\n", rc
);
216 /***************************************************************************
217 * Intel Advanced Management Technolgy ME Client
218 ***************************************************************************/
220 #define AMT_MAJOR_VERSION 1
221 #define AMT_MINOR_VERSION 1
223 #define AMT_STATUS_SUCCESS 0x0
224 #define AMT_STATUS_INTERNAL_ERROR 0x1
225 #define AMT_STATUS_NOT_READY 0x2
226 #define AMT_STATUS_INVALID_AMT_MODE 0x3
227 #define AMT_STATUS_INVALID_MESSAGE_LENGTH 0x4
229 #define AMT_STATUS_HOST_IF_EMPTY_RESPONSE 0x4000
230 #define AMT_STATUS_SDK_RESOURCES 0x1004
233 #define AMT_BIOS_VERSION_LEN 65
234 #define AMT_VERSIONS_NUMBER 50
235 #define AMT_UNICODE_STRING_LEN 20
237 struct amt_unicode_string
{
239 char string
[AMT_UNICODE_STRING_LEN
];
240 } __attribute__((packed
));
242 struct amt_version_type
{
243 struct amt_unicode_string description
;
244 struct amt_unicode_string version
;
245 } __attribute__((packed
));
250 } __attribute__((packed
));
252 struct amt_code_versions
{
253 uint8_t bios
[AMT_BIOS_VERSION_LEN
];
255 struct amt_version_type versions
[AMT_VERSIONS_NUMBER
];
256 } __attribute__((packed
));
258 /***************************************************************************
259 * Intel Advanced Management Technolgy Host Interface
260 ***************************************************************************/
262 struct amt_host_if_msg_header
{
263 struct amt_version version
;
267 } __attribute__((packed
));
269 struct amt_host_if_resp_header
{
270 struct amt_host_if_msg_header header
;
272 unsigned char data
[0];
273 } __attribute__((packed
));
275 const uuid_le MEI_IAMTHIF
= UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, \
276 0xac, 0xa8, 0x46, 0xe0, 0xff, 0x65, 0x81, 0x4c);
278 #define AMT_HOST_IF_CODE_VERSIONS_REQUEST 0x0400001A
279 #define AMT_HOST_IF_CODE_VERSIONS_RESPONSE 0x0480001A
281 const struct amt_host_if_msg_header CODE_VERSION_REQ
= {
282 .version
= {AMT_MAJOR_VERSION
, AMT_MINOR_VERSION
},
284 .command
= AMT_HOST_IF_CODE_VERSIONS_REQUEST
,
291 unsigned long send_timeout
;
296 static bool amt_host_if_init(struct amt_host_if
*acmd
,
297 unsigned long send_timeout
, bool verbose
)
299 acmd
->send_timeout
= (send_timeout
) ? send_timeout
: 20000;
300 acmd
->initialized
= mei_init(&acmd
->mei_cl
, &MEI_IAMTHIF
, 0, verbose
);
301 return acmd
->initialized
;
304 static void amt_host_if_deinit(struct amt_host_if
*acmd
)
306 mei_deinit(&acmd
->mei_cl
);
307 acmd
->initialized
= false;
310 static uint32_t amt_verify_code_versions(const struct amt_host_if_resp_header
*resp
)
312 uint32_t status
= AMT_STATUS_SUCCESS
;
313 struct amt_code_versions
*code_ver
;
315 uint32_t ver_type_cnt
;
319 code_ver
= (struct amt_code_versions
*)resp
->data
;
320 /* length - sizeof(status) */
321 code_ver_len
= resp
->header
.length
- sizeof(uint32_t);
322 ver_type_cnt
= code_ver_len
-
323 sizeof(code_ver
->bios
) -
324 sizeof(code_ver
->count
);
325 if (code_ver
->count
!= ver_type_cnt
/ sizeof(struct amt_version_type
)) {
326 status
= AMT_STATUS_INTERNAL_ERROR
;
330 for (i
= 0; i
< code_ver
->count
; i
++) {
331 len
= code_ver
->versions
[i
].description
.length
;
333 if (len
> AMT_UNICODE_STRING_LEN
) {
334 status
= AMT_STATUS_INTERNAL_ERROR
;
338 len
= code_ver
->versions
[i
].version
.length
;
339 if (code_ver
->versions
[i
].version
.string
[len
] != '\0' ||
340 len
!= strlen(code_ver
->versions
[i
].version
.string
)) {
341 status
= AMT_STATUS_INTERNAL_ERROR
;
349 static uint32_t amt_verify_response_header(uint32_t command
,
350 const struct amt_host_if_msg_header
*resp_hdr
,
351 uint32_t response_size
)
353 if (response_size
< sizeof(struct amt_host_if_resp_header
)) {
354 return AMT_STATUS_INTERNAL_ERROR
;
355 } else if (response_size
!= (resp_hdr
->length
+
356 sizeof(struct amt_host_if_msg_header
))) {
357 return AMT_STATUS_INTERNAL_ERROR
;
358 } else if (resp_hdr
->command
!= command
) {
359 return AMT_STATUS_INTERNAL_ERROR
;
360 } else if (resp_hdr
->_reserved
!= 0) {
361 return AMT_STATUS_INTERNAL_ERROR
;
362 } else if (resp_hdr
->version
.major
!= AMT_MAJOR_VERSION
||
363 resp_hdr
->version
.minor
< AMT_MINOR_VERSION
) {
364 return AMT_STATUS_INTERNAL_ERROR
;
366 return AMT_STATUS_SUCCESS
;
369 static uint32_t amt_host_if_call(struct amt_host_if
*acmd
,
370 const unsigned char *command
, ssize_t command_sz
,
371 uint8_t **read_buf
, uint32_t rcmd
,
372 unsigned int expected_sz
)
378 struct amt_host_if_resp_header
*msg_hdr
;
380 in_buf_sz
= acmd
->mei_cl
.buf_size
;
381 *read_buf
= (uint8_t *)malloc(sizeof(uint8_t) * in_buf_sz
);
382 if (*read_buf
== NULL
)
383 return AMT_STATUS_SDK_RESOURCES
;
384 memset(*read_buf
, 0, in_buf_sz
);
385 msg_hdr
= (struct amt_host_if_resp_header
*)*read_buf
;
387 written
= mei_send_msg(&acmd
->mei_cl
,
388 command
, command_sz
, acmd
->send_timeout
);
389 if (written
!= command_sz
)
390 return AMT_STATUS_INTERNAL_ERROR
;
392 out_buf_sz
= mei_recv_msg(&acmd
->mei_cl
, *read_buf
, in_buf_sz
, 2000);
394 return AMT_STATUS_HOST_IF_EMPTY_RESPONSE
;
396 status
= msg_hdr
->status
;
397 if (status
!= AMT_STATUS_SUCCESS
)
400 status
= amt_verify_response_header(rcmd
,
401 &msg_hdr
->header
, out_buf_sz
);
402 if (status
!= AMT_STATUS_SUCCESS
)
405 if (expected_sz
&& expected_sz
!= out_buf_sz
)
406 return AMT_STATUS_INTERNAL_ERROR
;
408 return AMT_STATUS_SUCCESS
;
412 static uint32_t amt_get_code_versions(struct amt_host_if
*cmd
,
413 struct amt_code_versions
*versions
)
415 struct amt_host_if_resp_header
*response
= NULL
;
418 status
= amt_host_if_call(cmd
,
419 (const unsigned char *)&CODE_VERSION_REQ
,
420 sizeof(CODE_VERSION_REQ
),
421 (uint8_t **)&response
,
422 AMT_HOST_IF_CODE_VERSIONS_RESPONSE
, 0);
424 if (status
!= AMT_STATUS_SUCCESS
)
427 status
= amt_verify_code_versions(response
);
428 if (status
!= AMT_STATUS_SUCCESS
)
431 memcpy(versions
, response
->data
, sizeof(struct amt_code_versions
));
433 if (response
!= NULL
)
439 /************************** end of amt_host_if_command ***********************/
440 int main(int argc
, char **argv
)
442 struct amt_code_versions ver
;
443 struct amt_host_if acmd
;
449 verbose
= (argc
> 1 && strcmp(argv
[1], "-v") == 0);
451 if (!amt_host_if_init(&acmd
, 5000, verbose
)) {
456 status
= amt_get_code_versions(&acmd
, &ver
);
458 amt_host_if_deinit(&acmd
);
461 case AMT_STATUS_HOST_IF_EMPTY_RESPONSE
:
462 printf("Intel AMT: DISABLED\n");
465 case AMT_STATUS_SUCCESS
:
466 printf("Intel AMT: ENABLED\n");
467 for (i
= 0; i
< ver
.count
; i
++) {
468 printf("%s:\t%s\n", ver
.versions
[i
].description
.string
,
469 ver
.versions
[i
].version
.string
);
474 printf("An error has occurred\n");