1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*******************************************************************************
* Copyright (C) 2004-2008 Intel Corp. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of Intel Corp. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cerrno>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdint.h>
#include <aio.h>
#ifdef __sun
#include <stdio.h>
#include <stdlib.h>
#endif // __sun
#include "HECIUnix.h"
#pragma pack(1)
typedef struct heci_ioctl_data
{
uint32_t size;
char *data;
#ifndef _LP64
/*
* If lms is compiled in 32-bit, padding is needed to
* talk to the driver which is 64-bit only.
*/
char *pad;
#endif
} heci_ioctl_data_t;
/* IOCTL commands */
#undef HECI_IOCTL
#undef IOCTL_HECI_GET_VERSION
#undef IOCTL_HECI_CONNECT_CLIENT
#undef IOCTL_HECI_WD
#define HECI_IOCTL_TYPE 0x48
#define IOCTL_HECI_GET_VERSION \
_IOWR(HECI_IOCTL_TYPE, 0x0, heci_ioctl_data_t)
#define IOCTL_HECI_CONNECT_CLIENT \
_IOWR(HECI_IOCTL_TYPE, 0x01, heci_ioctl_data_t)
#define IOCTL_HECI_WD \
_IOWR(HECI_IOCTL_TYPE, 0x02, heci_ioctl_data_t)
#define IAMT_HECI_GET_RECEIVED_MESSAGE_DATA \
_IOW(HECI_IOCTL_TYPE, 0x03, heci_ioctl_data_t)
#pragma pack(0)
/***************************** public functions *****************************/
HECILinux::HECILinux(const GUID guid, bool verbose) :
HECI(guid, verbose),
_fd(-1),
m_haveHeciVersion(false)
{
}
HECILinux::~HECILinux()
{
if (_fd != -1) {
close(_fd);
}
}
bool HECILinux::GetHeciVersion(HECI_VERSION &version) const
{
if (m_haveHeciVersion) {
memcpy(&version, &m_heciVersion, sizeof(HECI_VERSION));
return true;
}
return false;
}
bool HECILinux::Init(unsigned char reqProtocolVersion)
{
int result;
HECI_CLIENT *heci_client;
bool return_result = true;
heci_ioctl_data_t version_response;
heci_ioctl_data_t client_connect;
m_haveHeciVersion = false;
if (_initialized) {
Deinit();
}
_fd = open("/dev/heci", O_RDWR);
if (_fd == -1 ) {
if (_verbose) {
fprintf(stderr, "Error: Cannot establish a handle to the HECI driver\n");
}
return false;
}
_initialized = true;
version_response.size = sizeof(HECI_VERSION);
version_response.data = (char *)malloc(version_response.size);
if (!version_response.data) {
if (_verbose) {
fprintf(stderr, "malloc failure.\n");
}
return_result = false;
Deinit();
goto heci_free;
}
result = ioctl(_fd, IOCTL_HECI_GET_VERSION, &version_response);
if (result) {
if (_verbose) {
fprintf(stderr, "error in IOCTL_HECI_GET_VERSION recieve message. err=%d\n", result);
}
return_result = false;
Deinit();
goto heci_free;
}
memcpy(&m_heciVersion, version_response.data, sizeof(HECI_VERSION));
m_haveHeciVersion = true;
if (_verbose) {
fprintf(stdout, "Connected to HECI driver, version: %d.%d.%d.%d\n",
m_heciVersion.major, m_heciVersion.minor, m_heciVersion.hotfix, m_heciVersion.build);
fprintf(stdout, "Size of guid = %lu\n", (unsigned long)sizeof(_guid));
}
client_connect.size = sizeof(_guid);
client_connect.data = (char *)malloc(client_connect.size);
if (!client_connect.data) {
if (_verbose) {
fprintf(stderr, "malloc failure.\n");
}
return_result = false;
Deinit();
goto heci_free;
}
memcpy(client_connect.data, &_guid, sizeof(_guid));
result = ioctl(_fd, IOCTL_HECI_CONNECT_CLIENT, &client_connect);
if (result) {
if (_verbose) {
fprintf(stderr, "error in IOCTL_HECI_CONNECT_CLIENT recieve message. err=%d\n", result);
}
return_result = false;
Deinit();
goto heci_free;
}
heci_client = (HECI_CLIENT *) client_connect.data;
if (_verbose) {
fprintf(stdout, "max_message_length %d \n", (heci_client->MaxMessageLength));
fprintf(stdout, "protocol_version %d \n", (heci_client->ProtocolVersion));
}
if ((reqProtocolVersion > 0) && (heci_client->ProtocolVersion != reqProtocolVersion)) {
if (_verbose) {
fprintf(stderr, "Error: MEI protocol version not supported\n");
}
return_result = false;
Deinit();
goto heci_free;
}
_protocolVersion = heci_client->ProtocolVersion;
_bufSize = heci_client->MaxMessageLength;
heci_free:
if (NULL != version_response.data) {
free(version_response.data);
}
if (NULL != client_connect.data) {
free(client_connect.data);
}
return return_result;
}
void HECILinux::Deinit()
{
if (_fd != -1) {
close(_fd);
_fd = -1;
}
_bufSize = 0;
_protocolVersion = 0;
_initialized = false;
}
int HECILinux::ReceiveMessage(unsigned char *buffer, int len, unsigned long timeout)
{
int rv = 0;
int error = 0;
if (_verbose) {
fprintf(stdout, "call read length = %d\n", len);
}
rv = read(_fd, (void*)buffer, len);
if (rv < 0) {
error = errno;
if (_verbose) {
fprintf(stderr, "read failed with status %d %d\n", rv, error);
}
Deinit();
} else {
if (_verbose) {
fprintf(stderr, "read succeded with result %d\n", rv);
}
}
return rv;
}
int HECILinux::SendMessage(const unsigned char *buffer, int len, unsigned long timeout)
{
int rv = 0;
int return_length =0;
int error = 0;
fd_set set;
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec =(timeout % 1000) * 1000000;
if (_verbose) {
fprintf(stdout, "call write length = %d\n", len);
}
rv = write(_fd, (void *)buffer, len);
if (rv < 0) {
error = errno;
if (_verbose) {
fprintf(stderr,"write failed with status %d %d\n", rv, error);
}
goto out;
}
return_length = rv;
FD_ZERO(&set);
FD_SET(_fd, &set);
rv = select(_fd+1 ,&set, NULL, NULL, &tv);
if (rv > 0 && FD_ISSET(_fd, &set)) {
if (_verbose) {
fprintf(stderr, "write success\n");
}
}
else if (rv == 0) {
if (_verbose) {
fprintf(stderr, "write failed on timeout with status\n");
}
goto out;
}
else { //rv<0
if (_verbose) {
fprintf(stderr, "write failed on select with status %d\n", rv);
}
goto out;
}
rv = return_length;
out:
if (rv < 0) {
Deinit();
}
return rv;
}
|