-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoemcommands.cpp
391 lines (348 loc) · 12.6 KB
/
oemcommands.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
// Copyright (c) 2020 Nuvoton Technology Corporation
//
// 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 "oemcommands.hpp"
#include <ipmid/api.hpp>
#include <phosphor-logging/log.hpp>
#include <xyz/openbmc_project/Software/Activation/server.hpp>
#include <xyz/openbmc_project/Software/Version/server.hpp>
static constexpr auto softwareRoot = "/xyz/openbmc_project/software";
static constexpr auto biosObj = "/xyz/openbmc_project/software/bios_active";
static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
static constexpr auto redundancyIntf =
"xyz.openbmc_project.Software.RedundancyPriority";
static constexpr auto activationIntf =
"xyz.openbmc_project.Software.Activation";
using Version = sdbusplus::xyz::openbmc_project::Software::server::Version;
using Activation =
sdbusplus::xyz::openbmc_project::Software::server::Activation;
namespace ipmi
{
using namespace phosphor::logging;
namespace nuvoton
{
ipmi::RspType<uint8_t> ipmiOEMGetPwmMode()
{
uint8_t rc, value;
rc = pwm_control->getPwmMode(&value);
return ipmi::response(rc, value);
}
ipmi::RspType<> ipmiOEMSetManualPwm(uint8_t enabled)
{
uint8_t rc;
rc = pwm_control->setManualPwm(enabled);
return ipmi::response(rc);
}
ipmi::RspType<uint8_t> ipmiOEMGetPwm(uint8_t pwm_id)
{
uint8_t value, rc;
rc = pwm_control->getPwmValue(pwm_id, &value);
return ipmi::response(rc, value);
}
ipmi::RspType<uint8_t> ipmiOEMSetPwm(uint8_t pwm_id, uint8_t value)
{
uint8_t rc = pwm_control->setPwmValue(pwm_id, value);
return ipmi::response(rc, value);
}
void createPwmControl()
{
pwm_control = std::make_unique<IpmiPwmcontrol>();
}
ipmi::RspType<uint8_t> ipmiOEMGetPostCode(ipmi::Context::ptr& ctx)
{
using postcode_t = std::tuple<uint64_t, std::vector<uint8_t>>;
auto conn = getSdBus();
try
{
auto method =
conn->new_method_call("xyz.openbmc_project.State.Boot.Raw",
"/xyz/openbmc_project/state/boot/raw0",
"org.freedesktop.DBus.Properties", "Get");
method.append("xyz.openbmc_project.State.Boot.Raw", "Value");
auto reply = conn->call(method);
std::variant<postcode_t> postCode;
reply.read(postCode);
return ipmi::responseSuccess(
std::get<0>(std::get<postcode_t>(postCode)));
}
catch (std::exception& e)
{
log<level::ERR>("ipmiOEMGetPostCode err");
return ipmi::responseUnspecifiedError();
}
return ipmi::responseSuccess();
}
namespace postcode
{
static constexpr auto postCodeIntf = "xyz.openbmc_project.State.Boot.PostCode";
static constexpr auto postCodeObj = "/xyz/openbmc_project/State/Boot/PostCode0";
static constexpr auto MAX_CODE_BYTES = 240;
using postcode_t = std::tuple<uint64_t, std::vector<uint8_t>>;
int getPostCodes(uint16_t index, std::string service,
std::vector<uint8_t>& codes)
{
auto conn = getSdBus();
std::vector<postcode_t> post_codes;
try
{
auto method = conn->new_method_call(service.c_str(), postCodeObj,
postCodeIntf, "GetPostCodes");
method.append(index);
auto reply = conn->call(method);
reply.read(post_codes);
}
catch (const std::exception& e)
{
log<level::ERR>(e.what());
return -1;
}
if (post_codes.empty())
{
codes.resize(MAX_CODE_BYTES, 0);
return 0;
}
// convert a(tay) to at, we only need the code and ignore desc
std::transform(post_codes.begin(), post_codes.end(),
std::back_inserter(codes),
[](const auto& pc) { return std::get<0>(pc); });
// according to spec we only can send 240 bytes, remove the front data
if (codes.size() > MAX_CODE_BYTES)
{
size_t rm_count = codes.size() - MAX_CODE_BYTES;
codes.erase(codes.begin(), codes.begin() + rm_count);
}
// Vendor ask add this
if (codes.size() < MAX_CODE_BYTES)
{
size_t leak_num = MAX_CODE_BYTES - codes.size();
std::string msg = "code size:" + std::to_string(codes.size()) +
", add size: " + std::to_string(leak_num);
log<level::INFO>(msg.c_str());
codes.insert(codes.begin(), leak_num, 0);
}
return 0;
}
// if previous == 0, use current post code
ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPostCodes(uint8_t previous)
{
std::string msg, service;
std::vector<uint8_t> codes;
int ret;
if (previous > 1)
{
return ipmi::responseParmOutOfRange();
}
try
{
service = ipmi::getService(*getSdBus(), postCodeIntf, postCodeObj);
}
catch (const std::exception& e)
{
log<level::ERR>("Cannot get post code manager service");
return ipmi::responseUnspecifiedError();
}
// if previous post code not exist, we will get empty codes, so we don't
// need handle index error
ret = getPostCodes(previous + 1, service, codes);
if (ret)
{
return ipmi::responseUnspecifiedError();
}
return ipmi::responseSuccess(codes);
}
} // namespace postcode
int getBMCVersionInfo(ipmi::Context::ptr& ctx, std::string& ver)
{
ipmi::ObjectTree objectTree;
try
{
objectTree =
ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, versionIntf);
}
catch (const sdbusplus::exception::exception& e)
{
log<level::ERR>("Failed to fetch software object from dbus",
entry("INTERFACE=%s", versionIntf),
entry("ERRMSG=%s", e.what()));
return -1;
}
auto objectFound = false;
for (auto& softObject : objectTree)
{
auto service =
ipmi::getService(*ctx->bus, versionIntf, softObject.first);
auto objValueTree =
ipmi::getManagedObjects(*ctx->bus, service, softwareRoot);
auto minPriority = 0xFF;
for (const auto& objIter : objValueTree)
{
try
{
auto& intfMap = objIter.second;
auto& redundancyPriorityProps = intfMap.at(redundancyIntf);
auto& versionProps = intfMap.at(versionIntf);
auto& activationProps = intfMap.at(activationIntf);
auto priority =
std::get<uint8_t>(redundancyPriorityProps.at("Priority"));
auto purpose =
std::get<std::string>(versionProps.at("Purpose"));
auto activation =
std::get<std::string>(activationProps.at("Activation"));
auto version =
std::get<std::string>(versionProps.at("Version"));
if ((Version::convertVersionPurposeFromString(purpose) ==
Version::VersionPurpose::BMC) &&
(Activation::convertActivationsFromString(activation) ==
Activation::Activations::Active))
{
if (priority < minPriority)
{
minPriority = priority;
objectFound = true;
ver = std::move(version);
}
}
}
catch (const std::exception& e)
{
log<level::ERR>(e.what());
}
}
}
if (!objectFound)
{
log<level::ERR>("Could not found an BMC software Object");
return -1;
}
return 0;
}
int getBIOSVersionInfo(ipmi::Context::ptr& ctx, std::string& ver)
{
// read software manager object first
auto service = ipmi::getService(*ctx->bus, versionIntf, biosObj);
try
{
auto objProperty = ipmi::getDbusProperty(*ctx->bus, service, biosObj,
versionIntf, "Version");
auto version = std::get<std::string>(objProperty);
ver = std::move(version);
}
catch (const sdbusplus::exception::exception& e)
{
log<level::ERR>("Failed to fetch BIOS object from dbus",
entry("ERRMSG=%s", e.what()));
return -1;
}
return 0;
}
ipmi::RspType<std::vector<char>>
ipmiOEMGetFirmwareVersion(ipmi::Context::ptr ctx, uint8_t type)
{
int res = 0;
std::string version{};
log<level::INFO>("ipmiOEMGetFirmwareVersion");
switch (type)
{
case as_int(FirmwareType::BMC):
res = getBMCVersionInfo(ctx, version);
break;
case as_int(FirmwareType::BIOS):
res = getBIOSVersionInfo(ctx, version);
break;
// TODO: wait for implement
case as_int(FirmwareType::CPLD):
case as_int(FirmwareType::SCM_CPLD):
res = cpld::getCpldVersionInfo(ctx, version, type);
// return ipmi::responseDestinationUnavailable();
break;
case as_int(FirmwareType::PSU):
res = psu::getPsuVersionInfo(ctx, version);
break;
default:
log<level::ERR>("GetFirmwareVersion: invalid type");
return ipmi::responseParmOutOfRange();
}
if (res)
return ipmi::responseUnspecifiedError();
// convert string to vector<char> to avoid byte count
std::vector<char> v(version.length());
std::copy(version.begin(), version.end(), v.begin());
return ipmi::responseSuccess(v);
}
ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetGpio(uint8_t pinNum)
{
return ipmiOEMGetGpioStatus(pinNum);
}
ipmi::RspType<uint8_t> ipmiFwUpdate(uint8_t target, uint8_t region,
uint8_t action,
std::optional<std::vector<uint8_t>> image)
{
std::string img;
std::string msg;
// convert std::vector<uint8_t> to std::string
if (image.has_value())
{
auto image_bytes = image.value();
img = std::string(image_bytes.begin(), image_bytes.end());
}
msg = "target: " + std::to_string(target) +
", region: " + std::to_string(region) +
", action:" + std::to_string(action);
log<level::INFO>(msg.c_str());
switch (target)
{
case as_int(FirmwareTarget::PSU):
return psu::ipmiOemPsuFwUpdate(region, action, img);
}
return ipmi::responseUnspecifiedError();
}
} // namespace nuvoton
static void registerOEMFunctions() __attribute__((constructor));
static void registerOEMFunctions(void)
{
log<level::INFO>("Registering Nuvoton IPMI OEM commands");
nuvoton::createPwmControl();
// <Get Manual PWM command>
registerHandler(prioOemBase, netFnOemTwo, nuvoton::fan::cmdGetPwmMode,
Privilege::Callback, nuvoton::ipmiOEMGetPwmMode);
// <Set Manual PWM command>
registerHandler(prioOemBase, netFnOemTwo, nuvoton::fan::cmdSetManualPwm,
Privilege::Callback, nuvoton::ipmiOEMSetManualPwm);
// <Get PWM value command>
registerHandler(prioOemBase, netFnOemTwo, nuvoton::fan::cmdGetPwm,
Privilege::Callback, nuvoton::ipmiOEMGetPwm);
// <Set PWM value command>
registerHandler(prioOemBase, netFnOemTwo, nuvoton::fan::cmdSetPwm,
Privilege::Callback, nuvoton::ipmiOEMSetPwm);
// Get BIOS post code
registerHandler(prioOemBase, netFnOemTwo, nuvoton::cmdGetPostCode,
Privilege::User, nuvoton::postcode::ipmiOEMGetPostCodes);
// Get firmware version
registerHandler(prioOemBase, netFnOemFive, nuvoton::cmdGetFimwareVer,
Privilege::User, nuvoton::ipmiOEMGetFirmwareVersion);
// <Get GPIO status command>
registerHandler(prioOemBase, netFnOemOne, nuvoton::cmdGetGpioStatus,
Privilege::User, nuvoton::ipmiOEMGetGpio);
// <OEM firmware update command>
registerHandler(prioOemBase, netFnOemFive, nuvoton::cmdFirmwareUpdate,
Privilege::User, nuvoton::ipmiFwUpdate);
// Master Phase Write Read
registerHandler(prioOemBase, netFnOemFive, nuvoton::cmdPsuPhase,
Privilege::User, nuvoton::psu::masterPhase);
// Master Mux Write Read
registerHandler(prioOemBase, netFnOemFive, nuvoton::cmdMasterMuxWR,
Privilege::User, nuvoton::masterMuxWR);
}
} // namespace ipmi