/
google_accel_oob.cpp
340 lines (289 loc) · 10.3 KB
/
google_accel_oob.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "google_accel_oob.hpp"
#include "commands.hpp"
#include "errors.hpp"
#include "handler.hpp"
#include <sdbusplus/bus.hpp>
#include <stdplus/print.hpp>
#include <cstdint>
#include <cstring>
#include <span>
#include <string>
#include <vector>
namespace google
{
namespace ipmi
{
#ifndef MAX_IPMI_BUFFER
#define MAX_IPMI_BUFFER 64
#endif
// token + address(8) + num_bytes + data(8) + len + NULL
constexpr size_t MAX_NAME_SIZE = MAX_IPMI_BUFFER - 1 - 8 - 1 - 8 - 1 - 1;
Resp accelOobDeviceCount(std::span<const uint8_t> data,
HandlerInterface* handler)
{
struct Request
{
} __attribute__((packed));
struct Reply
{
uint32_t count;
} __attribute__((packed));
if (data.size_bytes() < sizeof(Request))
{
stdplus::print(stderr, "AccelOob DeviceCount command too small: {}\n",
data.size_bytes());
return ::ipmi::responseReqDataLenInvalid();
}
if (data.size_bytes() + sizeof(Reply) > MAX_IPMI_BUFFER)
{
stdplus::print(
stderr,
"AccelOob DeviceCount command too large for reply buffer: "
"command={}B, payload={}B, max={}B\n",
data.size_bytes(), sizeof(Reply), MAX_IPMI_BUFFER);
return ::ipmi::responseReqDataLenExceeded();
}
uint32_t count = handler->accelOobDeviceCount();
std::vector<uint8_t> replyBuf(sizeof(Reply));
auto* reply = reinterpret_cast<Reply*>(replyBuf.data());
reply->count = count;
return ::ipmi::responseSuccess(SysOEMCommands::SysAccelOobDeviceCount,
replyBuf);
}
Resp accelOobDeviceName(std::span<const uint8_t> data,
HandlerInterface* handler)
{
struct Request
{
uint32_t index;
} __attribute__((packed));
struct Reply
{
uint8_t nameLength;
char name[MAX_NAME_SIZE];
} __attribute__((packed));
if (data.size_bytes() < sizeof(Request))
{
stdplus::print(stderr, "AccelOob DeviceName command too small: {}\n",
data.size_bytes());
return ::ipmi::responseReqDataLenInvalid();
}
if (data.size_bytes() + sizeof(Reply) > MAX_IPMI_BUFFER)
{
stdplus::print(
stderr,
"AccelOob DeviceName command too large for reply buffer: "
"command={}B, payload={}B, max={}B\n",
data.size_bytes(), sizeof(Reply), MAX_IPMI_BUFFER);
return ::ipmi::responseReqDataLenExceeded();
}
auto* req = reinterpret_cast<const Request*>(data.data());
std::string name = handler->accelOobDeviceName(req->index);
if (name.size() > MAX_NAME_SIZE)
{
stdplus::print(stderr,
"AccelOob: name was too long. "
"'{}' len must be <= {}\n",
name, MAX_NAME_SIZE);
return ::ipmi::responseReqDataTruncated();
}
std::vector<uint8_t> replyBuf(data.size_bytes() + sizeof(Reply));
std::copy(data.begin(), data.end(), replyBuf.data());
auto* reply = reinterpret_cast<Reply*>(replyBuf.data() + data.size_bytes());
reply->nameLength = name.length();
memcpy(reply->name, name.c_str(), reply->nameLength + 1);
return ::ipmi::responseSuccess(SysOEMCommands::SysAccelOobDeviceName,
replyBuf);
}
namespace
{
struct NameHeader
{
uint8_t nameLength;
char name[MAX_NAME_SIZE];
} __attribute__((packed));
// Reads the variable-length name from reqBuf and outputs the name and a pointer
// to the payload (next byte after name).
//
// Returns: =0: success.
// >0: if dataLen is too small, returns the minimum buffers size.
//
// Params:
// [in] reqBuf - the request buffer
// [in] dataLen - the length of reqBuf, in bytes
// [in] payloadSize - the size of the expected payload
// [out] name - the name string
// [out] payload - pointer into reqBuf just after name
size_t ReadNameHeader(const uint8_t* reqBuf, size_t dataLen, size_t payloadSize,
std::string* name, const uint8_t** payload)
{
constexpr size_t kNameHeaderSize = sizeof(NameHeader) - MAX_NAME_SIZE;
auto* req_header = reinterpret_cast<const NameHeader*>(reqBuf);
size_t minDataLen = kNameHeaderSize + payloadSize + req_header->nameLength;
if (dataLen < minDataLen)
{
return minDataLen;
}
if (name)
{
*name = std::string(req_header->name, req_header->nameLength);
}
if (payload)
{
*payload = reqBuf + kNameHeaderSize + req_header->nameLength;
}
return 0;
}
} // namespace
Resp accelOobRead(std::span<const uint8_t> data, HandlerInterface* handler)
{
struct Request
{
// Variable length header, handled by ReadNameHeader
// uint8_t nameLength; // <= MAX_NAME_SIZE
// char name[nameLength];
// Additional arguments
uint8_t token;
uint64_t address;
uint8_t num_bytes;
} __attribute__((packed));
struct Reply
{
uint64_t data;
} __attribute__((packed));
stdplus::print(stderr,
"AccelOob Read command sizes: "
"command={}B, payload={}B, max={}B\n",
data.size_bytes(), sizeof(Reply), MAX_IPMI_BUFFER);
std::string name;
const uint8_t* payload;
size_t min_size = ReadNameHeader(data.data(), data.size_bytes(),
sizeof(Request), &name, &payload);
if (min_size != 0)
{
stdplus::print(stderr, "AccelOob Read command too small: {}B < {}B\n",
data.size_bytes(), min_size);
return ::ipmi::responseReqDataLenInvalid();
}
if (data.size_bytes() + sizeof(Reply) > MAX_IPMI_BUFFER)
{
stdplus::print(stderr,
"AccelOob Read command too large for reply buffer: "
"command={}B, payload={}B, max={}B\n",
data.size_bytes(), sizeof(Reply), MAX_IPMI_BUFFER);
return ::ipmi::responseReqDataLenExceeded();
}
auto req = reinterpret_cast<const Request*>(payload);
uint64_t r = handler->accelOobRead(name, req->address, req->num_bytes);
std::vector<uint8_t> replyBuf(data.size_bytes() + sizeof(Reply));
std::copy(data.begin(), data.end(), replyBuf.data());
auto* reply = reinterpret_cast<Reply*>(replyBuf.data() + data.size_bytes());
reply->data = r;
return ::ipmi::responseSuccess(SysOEMCommands::SysAccelOobRead, replyBuf);
}
Resp accelOobWrite(std::span<const uint8_t> data, HandlerInterface* handler)
{
struct Request
{
// Variable length header, handled by ReadNameHeader
// uint8_t nameLength; // <= MAX_NAME_SIZE
// char name[nameLength];
// Additional arguments
uint8_t token;
uint64_t address;
uint8_t num_bytes;
uint64_t data;
} __attribute__((packed));
struct Reply
{
// Empty
} __attribute__((packed));
std::string name{};
const uint8_t* payload;
size_t min_size = ReadNameHeader(data.data(), data.size_bytes(),
sizeof(Request), &name, &payload);
if (min_size != 0)
{
stdplus::print(stderr, "AccelOob Write command too small: {}B < {}B\n",
data.size_bytes(), min_size);
return ::ipmi::responseReqDataLenInvalid();
}
if (data.size_bytes() + sizeof(Reply) > MAX_IPMI_BUFFER)
{
stdplus::print(stderr,
"AccelOob Write command too large for reply buffer: "
"command={}B, payload={}B, max={}B\n",
data.size_bytes(), sizeof(Reply), MAX_IPMI_BUFFER);
return ::ipmi::responseReqDataLenExceeded();
}
auto req = reinterpret_cast<const Request*>(payload);
handler->accelOobWrite(name, req->address, req->num_bytes, req->data);
std::vector<uint8_t> replyBuf(data.size_bytes() + sizeof(Reply));
std::copy(data.begin(), data.end(), replyBuf.data());
return ::ipmi::responseSuccess(SysOEMCommands::SysAccelOobWrite, replyBuf);
}
Resp accelGetVrSettings(::ipmi::Context::ptr ctx, std::span<const uint8_t> data,
HandlerInterface* handler)
{
uint16_t value;
if (data.size_bytes() != 2)
{
stdplus::println(
stderr,
"accelGetVrSettings command has incorrect size: %zuB != %dB\n",
data.size_bytes(), 2);
return ::ipmi::responseReqDataLenInvalid();
}
try
{
value = handler->accelGetVrSettings(ctx, /*chip_id*/ data[0],
/*settings_id*/ data[1]);
}
catch (const IpmiException& e)
{
return ::ipmi::response(e.getIpmiError());
}
return ::ipmi::responseSuccess(
SysOEMCommands::SysGetAccelVrSettings,
std::vector<uint8_t>{static_cast<uint8_t>(value),
static_cast<uint8_t>(value >> 8)});
}
Resp accelSetVrSettings(::ipmi::Context::ptr ctx, std::span<const uint8_t> data,
HandlerInterface* handler)
{
if (data.size_bytes() != 4)
{
stdplus::println(
stderr,
"accelSetVrSettings command has incorrect size: %zuB != %dB\n",
data.size_bytes(), 4);
return ::ipmi::responseReqDataLenInvalid();
}
uint16_t value = static_cast<uint16_t>(data[2] | data[3] << 8);
try
{
handler->accelSetVrSettings(ctx, /*chip_id*/ data[0],
/*settings_id*/ data[1], /*value*/ value);
}
catch (const IpmiException& e)
{
return ::ipmi::response(e.getIpmiError());
}
return ::ipmi::responseSuccess(SysOEMCommands::SysSetAccelVrSettings,
std::vector<uint8_t>{});
}
} // namespace ipmi
} // namespace google