blob: 910330d3bb2f251ad30b9ba88c7d9dcc054b525e [file] [log] [blame]
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Patrick Venturec2a07d42020-05-30 16:35:03 -070017#include "types.hpp"
Jason M. Bills64796042018-10-03 16:51:55 -070018#include "xyz/openbmc_project/Common/error.hpp"
Kuiying Wang45f04982018-12-26 09:23:08 +080019#include "xyz/openbmc_project/Led/Physical/server.hpp"
Jason M. Bills64796042018-10-03 16:51:55 -070020
Jayaprakash Mutyala94204162020-10-23 06:17:56 +000021#include <openssl/crypto.h>
Jia, Chunhuicc49b542019-03-20 15:41:07 +080022#include <systemd/sd-journal.h>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080023
Chen Yugang7a04f3a2019-10-08 11:12:35 +080024#include <appcommands.hpp>
James Feist91244a62019-02-19 15:04:54 -080025#include <boost/container/flat_map.hpp>
Yong Li23737fe2019-02-19 08:49:55 +080026#include <boost/process/child.hpp>
27#include <boost/process/io.hpp>
Yong Li0669d192019-05-06 14:01:46 +080028#include <com/intel/Control/OCOTShutdownPolicy/server.hpp>
Jason M. Bills64796042018-10-03 16:51:55 -070029#include <commandutils.hpp>
Zhikui Rence4e73f2019-12-06 13:59:47 -080030#include <gpiod.hpp>
Jia, Chunhuicc49b542019-03-20 15:41:07 +080031#include <ipmid/api.hpp>
Vernon Mauery5480ef62019-03-20 13:43:11 -070032#include <ipmid/utils.hpp>
James Feist63efafa2019-07-24 12:39:21 -070033#include <nlohmann/json.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080034#include <oemcommands.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080035#include <phosphor-logging/log.hpp>
36#include <sdbusplus/bus.hpp>
Suryakanth Sekard509eb92018-11-15 17:44:11 +053037#include <sdbusplus/message/types.hpp>
Chen Yugang97cf96e2019-11-01 08:55:11 +080038#include <xyz/openbmc_project/Chassis/Control/NMISource/server.hpp>
Chen,Yugang4f7e76b2019-08-20 09:28:06 +080039#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
40#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
Cheng C Yang773703a2019-08-15 09:41:11 +080041#include <xyz/openbmc_project/Control/PowerSupplyRedundancy/server.hpp>
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053042#include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp>
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +053043#include <xyz/openbmc_project/Control/Security/SpecialMode/server.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080044
James Feistfcd2d3a2020-05-28 10:38:15 -070045#include <array>
46#include <filesystem>
Jason M. Bills493d7762022-05-04 11:13:19 -070047#include <fstream>
James Feistfcd2d3a2020-05-28 10:38:15 -070048#include <iostream>
49#include <regex>
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +000050#include <set>
James Feistfcd2d3a2020-05-28 10:38:15 -070051#include <string>
52#include <variant>
53#include <vector>
54
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080055namespace ipmi
56{
Jason M. Bills64796042018-10-03 16:51:55 -070057static void registerOEMFunctions() __attribute__((constructor));
Vernon Mauery4ac799d2019-05-20 15:50:37 -070058
Jason M. Bills64796042018-10-03 16:51:55 -070059static constexpr size_t maxFRUStringLength = 0x3F;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080060
Suryakanth Sekard509eb92018-11-15 17:44:11 +053061static constexpr auto ethernetIntf =
62 "xyz.openbmc_project.Network.EthernetInterface";
63static constexpr auto networkIPIntf = "xyz.openbmc_project.Network.IP";
64static constexpr auto networkService = "xyz.openbmc_project.Network";
65static constexpr auto networkRoot = "/xyz/openbmc_project/network";
66
Chen Yugang97cf96e2019-11-01 08:55:11 +080067static constexpr const char* oemNmiSourceIntf =
68 "xyz.openbmc_project.Chassis.Control.NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080069static constexpr const char* oemNmiSourceObjPath =
Chen Yugang97cf96e2019-11-01 08:55:11 +080070 "/xyz/openbmc_project/Chassis/Control/NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080071static constexpr const char* oemNmiBmcSourceObjPathProp = "BMCSource";
72static constexpr const char* oemNmiEnabledObjPathProp = "Enabled";
73
James Feist63efafa2019-07-24 12:39:21 -070074static constexpr const char* dimmOffsetFile = "/var/lib/ipmi/ipmi_dimms.json";
srikanta mondal2030d7c2020-05-03 17:25:25 +000075static constexpr const char* multiNodeObjPath =
76 "/xyz/openbmc_project/MultiNode/Status";
77static constexpr const char* multiNodeIntf =
78 "xyz.openbmc_project.Chassis.MultiNode";
James Feist63efafa2019-07-24 12:39:21 -070079
Chen Yugang39736d52019-07-12 16:24:33 +080080enum class NmiSource : uint8_t
81{
82 none = 0,
Chen Yugang97cf96e2019-11-01 08:55:11 +080083 frontPanelButton = 1,
84 watchdog = 2,
85 chassisCmd = 3,
86 memoryError = 4,
87 pciBusError = 5,
88 pch = 6,
89 chipset = 7,
Chen Yugang39736d52019-07-12 16:24:33 +080090};
91
Suryakanth Sekar822b0b42019-11-15 18:32:53 +053092enum class SpecialUserIndex : uint8_t
93{
94 rootUser = 0,
95 atScaleDebugUser = 1
96};
97
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053098static constexpr const char* restricionModeService =
99 "xyz.openbmc_project.RestrictionMode.Manager";
100static constexpr const char* restricionModeBasePath =
101 "/xyz/openbmc_project/control/security/restriction_mode";
102static constexpr const char* restricionModeIntf =
103 "xyz.openbmc_project.Control.Security.RestrictionMode";
104static constexpr const char* restricionModeProperty = "RestrictionMode";
105
106static constexpr const char* specialModeService =
107 "xyz.openbmc_project.SpecialMode";
108static constexpr const char* specialModeBasePath =
Richard Marian Thomaiyara7b74282019-09-22 21:53:14 +0530109 "/xyz/openbmc_project/security/special_mode";
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +0530110static constexpr const char* specialModeIntf =
111 "xyz.openbmc_project.Security.SpecialMode";
112static constexpr const char* specialModeProperty = "SpecialMode";
113
114static constexpr const char* dBusPropertyIntf =
115 "org.freedesktop.DBus.Properties";
116static constexpr const char* dBusPropertyGetMethod = "Get";
117static constexpr const char* dBusPropertySetMethod = "Set";
118
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800119// return code: 0 successful
Patrick Williamsf944d2e2022-07-22 19:26:52 -0500120int8_t getChassisSerialNumber(sdbusplus::bus_t& bus, std::string& serial)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800121{
122 std::string objpath = "/xyz/openbmc_project/FruDevice";
123 std::string intf = "xyz.openbmc_project.FruDeviceManager";
124 std::string service = getService(bus, intf, objpath);
125 ObjectValueTree valueTree = getManagedObjects(bus, service, "/");
126 if (valueTree.empty())
127 {
128 phosphor::logging::log<phosphor::logging::level::ERR>(
129 "No object implements interface",
130 phosphor::logging::entry("INTF=%s", intf.c_str()));
131 return -1;
132 }
133
Jason M. Bills64796042018-10-03 16:51:55 -0700134 for (const auto& item : valueTree)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800135 {
136 auto interface = item.second.find("xyz.openbmc_project.FruDevice");
137 if (interface == item.second.end())
138 {
139 continue;
140 }
141
142 auto property = interface->second.find("CHASSIS_SERIAL_NUMBER");
143 if (property == interface->second.end())
144 {
145 continue;
146 }
147
148 try
149 {
150 Value variant = property->second;
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700151 std::string& result = std::get<std::string>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700152 if (result.size() > maxFRUStringLength)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800153 {
154 phosphor::logging::log<phosphor::logging::level::ERR>(
155 "FRU serial number exceed maximum length");
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800156 return -1;
157 }
Jason M. Bills64796042018-10-03 16:51:55 -0700158 serial = result;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800159 return 0;
160 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500161 catch (const std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800162 {
Jason M. Bills64796042018-10-03 16:51:55 -0700163 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800164 return -1;
165 }
166 }
167 return -1;
168}
Jason M. Bills64796042018-10-03 16:51:55 -0700169
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +0000170namespace mailbox
171{
172static uint8_t bus = 4;
173static std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
174static uint8_t slaveAddr = 56;
175static constexpr auto systemRoot = "/xyz/openbmc_project/inventory/system";
176static constexpr auto sessionIntf = "xyz.openbmc_project.Configuration.PFR";
177const std::string match = "Baseboard/PFR";
178static bool i2cConfigLoaded = false;
179// Command register for UFM provisioning/access commands; read/write allowed
180// from CPU/BMC.
181static const constexpr uint8_t provisioningCommand = 0x0b;
182// Trigger register for the command set in the previous offset.
183static const constexpr uint8_t triggerCommand = 0x0c;
184// Set 0x0c to 0x05 to execute command specified at “UFM/Provisioning Command”
185// register
186static const constexpr uint8_t flushRead = 0x05;
187// FIFO read registers
188std::set<uint8_t> readFifoReg = {0x08, 0x0C, 0x0D, 0x13};
189
190// UFM Read FIFO
191static const constexpr uint8_t readFifo = 0x0e;
192
193enum registerType : uint8_t
194{
195 singleByteRegister = 0,
196 fifoReadRegister,
197
198};
199
200void loadPfrConfig(ipmi::Context::ptr& ctx, bool& i2cConfigLoaded)
201{
202 ipmi::ObjectTree objectTree;
203
204 boost::system::error_code ec = ipmi::getAllDbusObjects(
205 ctx, systemRoot, sessionIntf, match, objectTree);
206
207 if (ec)
208 {
209
210 phosphor::logging::log<phosphor::logging::level::ERR>(
211 "Failed to fetch PFR object from dbus",
212 phosphor::logging::entry("INTERFACE=%s", sessionIntf),
213 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
214
215 return;
216 }
217
218 for (auto& softObject : objectTree)
219 {
220 const std::string& objPath = softObject.first;
221 const std::string& serviceName = softObject.second.begin()->first;
222 // PFR object found.. check for PFR support
223 ipmi::PropertyMap result;
224
225 ec = ipmi::getAllDbusProperties(ctx, serviceName, objPath, sessionIntf,
226 result);
227
228 if (ec)
229 {
230 phosphor::logging::log<phosphor::logging::level::ERR>(
231 "Failed to fetch pfr properties",
232 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
233 return;
234 }
235
236 const uint64_t* i2cBusNum = nullptr;
237 const uint64_t* address = nullptr;
238
239 for (const auto& [propName, propVariant] : result)
240 {
241
242 if (propName == "Address")
243 {
244 address = std::get_if<uint64_t>(&propVariant);
245 }
246 else if (propName == "Bus")
247 {
248 i2cBusNum = std::get_if<uint64_t>(&propVariant);
249 }
250 }
251
252 if ((address == nullptr) || (i2cBusNum == nullptr))
253 {
254 phosphor::logging::log<phosphor::logging::level::ERR>(
255 "Unable to read the pfr properties");
256 return;
257 }
258
259 bus = static_cast<int>(*i2cBusNum);
260 i2cBus = "/dev/i2c-" + std::to_string(bus);
261 slaveAddr = static_cast<int>(*address);
262
263 i2cConfigLoaded = true;
264 }
265}
266
267void writefifo(const uint8_t cmdReg, const uint8_t val)
268{
269 // Based on the spec, writing cmdReg to address val on this device, will
270 // trigger the write FIFO operation.
271 std::vector<uint8_t> writeData = {cmdReg, val};
272 std::vector<uint8_t> readBuf(0);
273 ipmi::Cc retI2C = ipmi::i2cWriteRead(i2cBus, slaveAddr, writeData, readBuf);
274}
275
276} // namespace mailbox
277
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800278// Returns the Chassis Identifier (serial #)
279ipmi_ret_t ipmiOEMGetChassisIdentifier(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
280 ipmi_request_t request,
281 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700282 ipmi_data_len_t dataLen,
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800283 ipmi_context_t context)
284{
285 std::string serial;
Jason M. Bills64796042018-10-03 16:51:55 -0700286 if (*dataLen != 0) // invalid request if there are extra parameters
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800287 {
Jason M. Bills64796042018-10-03 16:51:55 -0700288 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800289 return IPMI_CC_REQ_DATA_LEN_INVALID;
290 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700291 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
292 if (getChassisSerialNumber(*dbus, serial) == 0)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800293 {
Jason M. Bills64796042018-10-03 16:51:55 -0700294 *dataLen = serial.size(); // length will never exceed response length
295 // as it is checked in getChassisSerialNumber
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800296 char* resp = static_cast<char*>(response);
Jason M. Bills64796042018-10-03 16:51:55 -0700297 serial.copy(resp, *dataLen);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800298 return IPMI_CC_OK;
299 }
Jason M. Bills64796042018-10-03 16:51:55 -0700300 *dataLen = 0;
301 return IPMI_CC_RESPONSE_ERROR;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800302}
303
304ipmi_ret_t ipmiOEMSetSystemGUID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
305 ipmi_request_t request,
306 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700307 ipmi_data_len_t dataLen, ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800308{
309 static constexpr size_t safeBufferLength = 50;
310 char buf[safeBufferLength] = {0};
311 GUIDData* Data = reinterpret_cast<GUIDData*>(request);
312
Jason M. Bills64796042018-10-03 16:51:55 -0700313 if (*dataLen != sizeof(GUIDData)) // 16bytes
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800314 {
Jason M. Bills64796042018-10-03 16:51:55 -0700315 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800316 return IPMI_CC_REQ_DATA_LEN_INVALID;
317 }
318
Jason M. Bills64796042018-10-03 16:51:55 -0700319 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800320
321 snprintf(
322 buf, safeBufferLength,
323 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
324 Data->timeLow4, Data->timeLow3, Data->timeLow2, Data->timeLow1,
325 Data->timeMid2, Data->timeMid1, Data->timeHigh2, Data->timeHigh1,
326 Data->clock2, Data->clock1, Data->node6, Data->node5, Data->node4,
327 Data->node3, Data->node2, Data->node1);
328 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
329 std::string guid = buf;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800330
331 std::string objpath = "/xyz/openbmc_project/control/host0/systemGUID";
332 std::string intf = "xyz.openbmc_project.Common.UUID";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700333 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
334 std::string service = getService(*dbus, intf, objpath);
335 setDbusProperty(*dbus, service, objpath, intf, "UUID", guid);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800336 return IPMI_CC_OK;
337}
338
Jason M. Billsb02bf092019-08-15 13:01:56 -0700339ipmi::RspType<> ipmiOEMDisableBMCSystemReset(bool disableResetOnSMI,
340 uint7_t reserved1)
341{
Jayaprakash Mutyala0a652fa2021-07-01 17:09:39 +0000342 if (reserved1)
343 {
344 return ipmi::responseInvalidFieldRequest();
345 }
346
Jason M. Billsb02bf092019-08-15 13:01:56 -0700347 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
348
349 try
350 {
351 auto service =
352 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
353 ipmi::setDbusProperty(*busp, service, bmcResetDisablesPath,
354 bmcResetDisablesIntf, "ResetOnSMI",
355 !disableResetOnSMI);
356 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500357 catch (const std::exception& e)
Jason M. Billsb02bf092019-08-15 13:01:56 -0700358 {
359 phosphor::logging::log<phosphor::logging::level::ERR>(
360 "Failed to set BMC reset disables",
361 phosphor::logging::entry("EXCEPTION=%s", e.what()));
362 return ipmi::responseUnspecifiedError();
363 }
364
365 return ipmi::responseSuccess();
366}
367
368ipmi::RspType<bool, // disableResetOnSMI
369 uint7_t // reserved
370 >
371 ipmiOEMGetBMCResetDisables()
372{
373 bool disableResetOnSMI = true;
374
375 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
376 try
377 {
378 auto service =
379 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
380 Value variant =
381 ipmi::getDbusProperty(*busp, service, bmcResetDisablesPath,
382 bmcResetDisablesIntf, "ResetOnSMI");
383 disableResetOnSMI = !std::get<bool>(variant);
384 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500385 catch (const std::exception& e)
Jason M. Billsb02bf092019-08-15 13:01:56 -0700386 {
387 phosphor::logging::log<phosphor::logging::level::ERR>(
388 "Failed to get BMC reset disables",
389 phosphor::logging::entry("EXCEPTION=%s", e.what()));
390 return ipmi::responseUnspecifiedError();
391 }
392
393 return ipmi::responseSuccess(disableResetOnSMI, 0);
394}
395
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800396ipmi_ret_t ipmiOEMSetBIOSID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
397 ipmi_request_t request, ipmi_response_t response,
398 ipmi_data_len_t dataLen, ipmi_context_t context)
399{
400 DeviceInfo* data = reinterpret_cast<DeviceInfo*>(request);
401
Jason M. Bills64796042018-10-03 16:51:55 -0700402 if ((*dataLen < 2) || (*dataLen != (1 + data->biosIDLength)))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800403 {
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800404 *dataLen = 0;
405 return IPMI_CC_REQ_DATA_LEN_INVALID;
406 }
Jason M. Bills64796042018-10-03 16:51:55 -0700407 std::string idString((char*)data->biosId, data->biosIDLength);
Chalapathi Venkataramashettyfb9f1aa2021-05-07 08:37:07 +0000408 for (auto idChar : idString)
409 {
410 if (!std::isprint(static_cast<unsigned char>(idChar)))
411 {
412 phosphor::logging::log<phosphor::logging::level::ERR>(
413 "BIOS ID contains non printable character");
414 return IPMI_CC_INVALID_FIELD_REQUEST;
415 }
416 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800417
Vernon Mauery15419dd2019-05-24 09:40:30 -0700418 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Chalapathi899bfd12020-04-15 15:07:02 +0000419 std::string service = getService(*dbus, biosVersionIntf, biosActiveObjPath);
420 setDbusProperty(*dbus, service, biosActiveObjPath, biosVersionIntf,
Yong Li2742b852019-12-16 14:55:11 +0800421 biosVersionProp, idString);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800422 uint8_t* bytesWritten = static_cast<uint8_t*>(response);
423 *bytesWritten =
Jason M. Bills64796042018-10-03 16:51:55 -0700424 data->biosIDLength; // how many bytes are written into storage
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800425 *dataLen = 1;
426 return IPMI_CC_OK;
427}
428
Jayaprakash Mutyala90da3d92021-11-18 22:01:22 +0000429bool getActiveHSCSoftwareVersionInfo(std::string& hscVersion, size_t hscNumber)
430{
431 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
432 try
433 {
434 std::string hsbpObjPath =
435 "/xyz/openbmc_project/software/HSBP_" + std::to_string(hscNumber);
436 auto service = getService(*dbus, biosVersionIntf, hsbpObjPath);
437 Value hscVersionValue =
438 getDbusProperty(*dbus, "xyz.openbmc_project.HsbpManager",
439 hsbpObjPath, biosVersionIntf, "Version");
440 hscVersion = std::get<std::string>(hscVersionValue);
441 }
Patrick Williamsf944d2e2022-07-22 19:26:52 -0500442 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyala90da3d92021-11-18 22:01:22 +0000443 {
444 phosphor::logging::log<phosphor::logging::level::INFO>(
445 "Failed to retrieve HSBP version information",
446 phosphor::logging::entry("HSBP Number=%d", hscNumber));
447 return false;
448 }
449 return true;
450}
451
452bool getHscVerInfo(ipmi::Context::ptr ctx, uint8_t& hsc0Major,
453 uint8_t& hsc0Minor, uint8_t& hsc1Major, uint8_t& hsc1Minor,
454 uint8_t& hsc2Major, uint8_t& hsc2Minor)
455{
456 std::string hscVersion;
457 std::array<uint8_t, 6> hscVersions{0};
458
459 for (size_t hscNumber = 1; hscNumber <= 3; hscNumber++)
460 {
461 if (!getActiveHSCSoftwareVersionInfo(hscVersion, hscNumber))
462 {
463 continue;
464 }
465 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?)");
466 constexpr size_t matchedPhosphor = 4;
467 std::smatch results;
468 // hscVersion = BOOT_VER.FPGA_VER.SECURITY_REVISION (Example: 00.02.01)
469 if (std::regex_match(hscVersion, results, pattern1))
470 {
471 // Major version is FPGA_VER and Minor version is SECURITY_REV
472 if (results.size() == matchedPhosphor)
473 {
474 int index = (hscNumber - 1) * 2;
475 hscVersions[index] =
476 static_cast<uint8_t>(std::stoi(results[2]));
477 hscVersions[index + 1] =
478 static_cast<uint8_t>(std::stoi(results[3]));
479 }
480 }
481 }
482 hsc0Major = hscVersions[0];
483 hsc0Minor = hscVersions[1];
484 hsc1Major = hscVersions[2];
485 hsc1Minor = hscVersions[3];
486 hsc2Major = hscVersions[4];
487 hsc2Minor = hscVersions[5];
488 return true;
489}
490
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530491bool getSwVerInfo(ipmi::Context::ptr ctx, uint8_t& bmcMajor, uint8_t& bmcMinor,
492 uint8_t& meMajor, uint8_t& meMinor)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800493{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800494 // step 1 : get BMC Major and Minor numbers from its DBUS property
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530495 std::string bmcVersion;
496 if (getActiveSoftwareVersionInfo(ctx, versionPurposeBMC, bmcVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800497 {
498 return false;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800499 }
500
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530501 std::optional<MetaRevision> rev = convertIntelVersion(bmcVersion);
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800502 if (rev.has_value())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800503 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800504 MetaRevision revision = rev.value();
505 bmcMajor = revision.major;
506
507 revision.minor = (revision.minor > 99 ? 99 : revision.minor);
508 bmcMinor = revision.minor % 10 + (revision.minor / 10) * 16;
509 }
510
511 // step 2 : get ME Major and Minor numbers from its DBUS property
AppaRao Puli32825a22020-01-17 15:52:41 +0530512 std::string meVersion;
513 if (getActiveSoftwareVersionInfo(ctx, versionPurposeME, meVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800514 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800515 return false;
516 }
AppaRao Puli32825a22020-01-17 15:52:41 +0530517 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?).(\\d+?).(\\d+?)");
518 constexpr size_t matchedPhosphor = 6;
519 std::smatch results;
520 if (std::regex_match(meVersion, results, pattern1))
521 {
522 if (results.size() == matchedPhosphor)
523 {
524 meMajor = static_cast<uint8_t>(std::stoi(results[1]));
Jayaprakash Mutyalad0657022021-08-26 21:18:08 +0000525 meMinor = static_cast<uint8_t>(std::stoi(results[2]) << 4 |
526 std::stoi(results[3]));
AppaRao Puli32825a22020-01-17 15:52:41 +0530527 }
528 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800529 return true;
530}
531
532ipmi::RspType<
533 std::variant<std::string,
534 std::tuple<uint8_t, std::array<uint8_t, 2>,
535 std::array<uint8_t, 2>, std::array<uint8_t, 2>,
536 std::array<uint8_t, 2>, std::array<uint8_t, 2>>,
537 std::tuple<uint8_t, std::array<uint8_t, 2>>>>
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530538 ipmiOEMGetDeviceInfo(ipmi::Context::ptr ctx, uint8_t entityType,
539 std::optional<uint8_t> countToRead,
AppaRao Pulid46cb422020-01-21 18:40:21 +0530540 std::optional<uint8_t> offset)
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800541{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800542 if (entityType > static_cast<uint8_t>(OEMDevEntityType::sdrVer))
543 {
544 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800545 }
546
547 // handle OEM command items
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800548 switch (OEMDevEntityType(entityType))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800549 {
550 case OEMDevEntityType::biosId:
551 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530552 // Byte 2&3, Only used with selecting BIOS
553 if (!countToRead || !offset)
554 {
555 return ipmi::responseReqDataLenInvalid();
556 }
557
Vernon Mauery15419dd2019-05-24 09:40:30 -0700558 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800559 std::string service =
Chalapathi899bfd12020-04-15 15:07:02 +0000560 getService(*dbus, biosVersionIntf, biosActiveObjPath);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800561 try
562 {
Yong Li2742b852019-12-16 14:55:11 +0800563 Value variant =
Chalapathi899bfd12020-04-15 15:07:02 +0000564 getDbusProperty(*dbus, service, biosActiveObjPath,
Yong Li2742b852019-12-16 14:55:11 +0800565 biosVersionIntf, biosVersionProp);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700566 std::string& idString = std::get<std::string>(variant);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530567 if (*offset >= idString.size())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800568 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800569 return ipmi::responseParmOutOfRange();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800570 }
Jason M. Bills64796042018-10-03 16:51:55 -0700571 size_t length = 0;
AppaRao Pulid46cb422020-01-21 18:40:21 +0530572 if (*countToRead > (idString.size() - *offset))
Jason M. Bills64796042018-10-03 16:51:55 -0700573 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530574 length = idString.size() - *offset;
Jason M. Bills64796042018-10-03 16:51:55 -0700575 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800576 else
577 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530578 length = *countToRead;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800579 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800580
581 std::string readBuf = {0};
582 readBuf.resize(length);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530583 std::copy_n(idString.begin() + *offset, length,
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800584 (readBuf.begin()));
585 return ipmi::responseSuccess(readBuf);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800586 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500587 catch (const std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800588 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800589 return ipmi::responseUnspecifiedError();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800590 }
591 }
592 break;
593
594 case OEMDevEntityType::devVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800595 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530596 // Byte 2&3, Only used with selecting BIOS
597 if (countToRead || offset)
598 {
599 return ipmi::responseReqDataLenInvalid();
600 }
601
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800602 constexpr const size_t verLen = 2;
603 constexpr const size_t verTotalLen = 10;
604 std::array<uint8_t, verLen> bmcBuf = {0xff, 0xff};
605 std::array<uint8_t, verLen> hsc0Buf = {0xff, 0xff};
606 std::array<uint8_t, verLen> hsc1Buf = {0xff, 0xff};
607 std::array<uint8_t, verLen> meBuf = {0xff, 0xff};
608 std::array<uint8_t, verLen> hsc2Buf = {0xff, 0xff};
609 // data0/1: BMC version number; data6/7: ME version number
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530610 if (!getSwVerInfo(ctx, bmcBuf[0], bmcBuf[1], meBuf[0], meBuf[1]))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800611 {
612 return ipmi::responseUnspecifiedError();
613 }
Jayaprakash Mutyala90da3d92021-11-18 22:01:22 +0000614 if (!getHscVerInfo(ctx, hsc0Buf[0], hsc0Buf[1], hsc1Buf[0],
615 hsc1Buf[1], hsc2Buf[0], hsc2Buf[1]))
616 {
617 return ipmi::responseUnspecifiedError();
618 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800619 return ipmi::responseSuccess(
620 std::tuple<
621 uint8_t, std::array<uint8_t, verLen>,
622 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>,
623 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>>{
624 verTotalLen, bmcBuf, hsc0Buf, hsc1Buf, meBuf, hsc2Buf});
625 }
626 break;
627
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800628 case OEMDevEntityType::sdrVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800629 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530630 // Byte 2&3, Only used with selecting BIOS
631 if (countToRead || offset)
632 {
633 return ipmi::responseReqDataLenInvalid();
634 }
635
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800636 constexpr const size_t sdrLen = 2;
637 std::array<uint8_t, sdrLen> readBuf = {0x01, 0x0};
638 return ipmi::responseSuccess(
639 std::tuple<uint8_t, std::array<uint8_t, sdrLen>>{sdrLen,
640 readBuf});
641 }
642 break;
643
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800644 default:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800645 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800646 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800647}
648
649ipmi_ret_t ipmiOEMGetAICFRU(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
650 ipmi_request_t request, ipmi_response_t response,
651 ipmi_data_len_t dataLen, ipmi_context_t context)
652{
653 if (*dataLen != 0)
654 {
Jason M. Bills64796042018-10-03 16:51:55 -0700655 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800656 return IPMI_CC_REQ_DATA_LEN_INVALID;
657 }
658
659 *dataLen = 1;
660 uint8_t* res = reinterpret_cast<uint8_t*>(response);
661 // temporary fix. We don't support AIC FRU now. Just tell BIOS that no
662 // AIC is available so that BIOS will not timeout repeatly which leads to
663 // slow booting.
664 *res = 0; // Byte1=Count of SlotPosition/FruID records.
665 return IPMI_CC_OK;
666}
667
Jason M. Bills64796042018-10-03 16:51:55 -0700668ipmi_ret_t ipmiOEMGetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
669 ipmi_request_t request,
670 ipmi_response_t response,
671 ipmi_data_len_t dataLen,
672 ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800673{
Jason M. Bills64796042018-10-03 16:51:55 -0700674 GetPowerRestoreDelayRes* resp =
675 reinterpret_cast<GetPowerRestoreDelayRes*>(response);
676
677 if (*dataLen != 0)
678 {
679 *dataLen = 0;
680 return IPMI_CC_REQ_DATA_LEN_INVALID;
681 }
682
Vernon Mauery15419dd2019-05-24 09:40:30 -0700683 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700684 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700685 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
Jason M. Bills64796042018-10-03 16:51:55 -0700686 Value variant =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700687 getDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700688 powerRestoreDelayIntf, powerRestoreDelayProp);
689
Andrei Kartashevc42c7ed2022-01-10 12:17:34 +0300690 uint64_t val = std::get<uint64_t>(variant);
691 val /= 1000000UL;
692 uint16_t delay = val;
Jason M. Bills64796042018-10-03 16:51:55 -0700693 resp->byteLSB = delay;
694 resp->byteMSB = delay >> 8;
695
696 *dataLen = sizeof(GetPowerRestoreDelayRes);
697
698 return IPMI_CC_OK;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800699}
700
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800701static uint8_t bcdToDec(uint8_t val)
702{
703 return ((val / 16 * 10) + (val % 16));
704}
705
706// Allows an update utility or system BIOS to send the status of an embedded
707// firmware update attempt to the BMC. After received, BMC will create a logging
708// record.
709ipmi::RspType<> ipmiOEMSendEmbeddedFwUpdStatus(uint8_t status, uint8_t target,
710 uint8_t majorRevision,
711 uint8_t minorRevision,
712 uint32_t auxInfo)
713{
714 std::string firmware;
Jason M. Billsdc249272019-04-03 09:58:40 -0700715 int instance = (target & targetInstanceMask) >> targetInstanceShift;
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800716 target = (target & selEvtTargetMask) >> selEvtTargetShift;
717
718 /* make sure the status is 0, 1, or 2 as per the spec */
719 if (status > 2)
720 {
721 return ipmi::response(ipmi::ccInvalidFieldRequest);
722 }
Jason M. Billsdc249272019-04-03 09:58:40 -0700723 /* make sure the target is 0, 1, 2, or 4 as per the spec */
724 if (target > 4 || target == 3)
725 {
726 return ipmi::response(ipmi::ccInvalidFieldRequest);
727 }
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800728 /*orignal OEM command is to record OEM SEL.
729 But openbmc does not support OEM SEL, so we redirect it to redfish event
730 logging. */
731 std::string buildInfo;
732 std::string action;
733 switch (FWUpdateTarget(target))
734 {
735 case FWUpdateTarget::targetBMC:
736 firmware = "BMC";
Jason M. Billsdc249272019-04-03 09:58:40 -0700737 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800738 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
739 " BuildID: " + std::to_string(auxInfo);
740 buildInfo += std::to_string(auxInfo);
741 break;
742 case FWUpdateTarget::targetBIOS:
743 firmware = "BIOS";
744 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700745 "major: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800746 std::to_string(bcdToDec(majorRevision)) + // BCD encoded
747 " minor: " +
748 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
749 " ReleaseNumber: " + // ASCII encoded
750 std::to_string(static_cast<uint8_t>(auxInfo >> 0) - '0') +
751 std::to_string(static_cast<uint8_t>(auxInfo >> 8) - '0') +
752 std::to_string(static_cast<uint8_t>(auxInfo >> 16) - '0') +
753 std::to_string(static_cast<uint8_t>(auxInfo >> 24) - '0');
754 break;
755 case FWUpdateTarget::targetME:
756 firmware = "ME";
757 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700758 "major: " + std::to_string(majorRevision) + " minor1: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800759 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
760 " minor2: " +
761 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 0))) +
762 " build1: " +
763 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 8))) +
764 " build2: " +
765 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 16)));
766 break;
767 case FWUpdateTarget::targetOEMEWS:
768 firmware = "EWS";
Jason M. Billsdc249272019-04-03 09:58:40 -0700769 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800770 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
771 " BuildID: " + std::to_string(auxInfo);
772 break;
773 }
774
Jason M. Billsdc249272019-04-03 09:58:40 -0700775 static const std::string openBMCMessageRegistryVersion("0.1");
776 std::string redfishMsgID = "OpenBMC." + openBMCMessageRegistryVersion;
777
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800778 switch (status)
779 {
780 case 0x0:
781 action = "update started";
Jason M. Billsdc249272019-04-03 09:58:40 -0700782 redfishMsgID += ".FirmwareUpdateStarted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800783 break;
784 case 0x1:
785 action = "update completed successfully";
Jason M. Billsdc249272019-04-03 09:58:40 -0700786 redfishMsgID += ".FirmwareUpdateCompleted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800787 break;
788 case 0x2:
789 action = "update failure";
Jason M. Billsdc249272019-04-03 09:58:40 -0700790 redfishMsgID += ".FirmwareUpdateFailed";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800791 break;
792 default:
793 action = "unknown";
794 break;
795 }
796
Jason M. Billsdc249272019-04-03 09:58:40 -0700797 std::string firmwareInstanceStr =
798 firmware + " instance: " + std::to_string(instance);
799 std::string message("[firmware update] " + firmwareInstanceStr +
800 " status: <" + action + "> " + buildInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800801
802 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", LOG_INFO,
Jason M. Billsdc249272019-04-03 09:58:40 -0700803 "REDFISH_MESSAGE_ID=%s", redfishMsgID.c_str(),
804 "REDFISH_MESSAGE_ARGS=%s,%s", firmwareInstanceStr.c_str(),
805 buildInfo.c_str(), NULL);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800806 return ipmi::responseSuccess();
807}
808
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +0530809ipmi::RspType<uint8_t, std::vector<uint8_t>>
810 ipmiOEMSlotIpmb(ipmi::Context::ptr ctx, uint6_t reserved1,
811 uint2_t slotNumber, uint3_t baseBoardSlotNum,
812 uint3_t riserSlotNum, uint2_t reserved2, uint8_t slaveAddr,
813 uint8_t netFn, uint8_t cmd,
814 std::optional<std::vector<uint8_t>> writeData)
815{
816 if (reserved1 || reserved2)
817 {
818 return ipmi::responseInvalidFieldRequest();
819 }
820
821 boost::system::error_code ec;
822 using ipmbResponse = std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t,
823 std::vector<uint8_t>>;
824 ipmbResponse res = ctx->bus->yield_method_call<ipmbResponse>(
825 ctx->yield, ec, "xyz.openbmc_project.Ipmi.Channel.Ipmb",
826 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
827 "SlotIpmbRequest", static_cast<uint8_t>(slotNumber),
828 static_cast<uint8_t>(baseBoardSlotNum), slaveAddr, netFn, cmd,
829 *writeData);
830 if (ec)
831 {
832 phosphor::logging::log<phosphor::logging::level::ERR>(
833 "Failed to call dbus method SlotIpmbRequest");
834 return ipmi::responseUnspecifiedError();
835 }
836
837 std::vector<uint8_t> dataReceived(0);
838 int status = -1;
839 uint8_t resNetFn = 0, resLun = 0, resCmd = 0, cc = 0;
840
841 std::tie(status, resNetFn, resLun, resCmd, cc, dataReceived) = res;
842
843 if (status)
844 {
845 phosphor::logging::log<phosphor::logging::level::ERR>(
846 "Failed to get response from SlotIpmbRequest");
847 return ipmi::responseResponseError();
848 }
849 return ipmi::responseSuccess(cc, dataReceived);
850}
851
Jason M. Bills64796042018-10-03 16:51:55 -0700852ipmi_ret_t ipmiOEMSetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
853 ipmi_request_t request,
854 ipmi_response_t response,
855 ipmi_data_len_t dataLen,
856 ipmi_context_t context)
857{
858 SetPowerRestoreDelayReq* data =
859 reinterpret_cast<SetPowerRestoreDelayReq*>(request);
860 uint16_t delay = 0;
861
862 if (*dataLen != sizeof(SetPowerRestoreDelayReq))
863 {
864 *dataLen = 0;
865 return IPMI_CC_REQ_DATA_LEN_INVALID;
866 }
867 delay = data->byteMSB;
868 delay = (delay << 8) | data->byteLSB;
Andrei Kartashevc42c7ed2022-01-10 12:17:34 +0300869 uint64_t val = delay * 1000000;
Vernon Mauery15419dd2019-05-24 09:40:30 -0700870 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700871 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700872 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
873 setDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Andrei Kartashevc42c7ed2022-01-10 12:17:34 +0300874 powerRestoreDelayIntf, powerRestoreDelayProp, val);
Jason M. Bills64796042018-10-03 16:51:55 -0700875 *dataLen = 0;
876
877 return IPMI_CC_OK;
878}
879
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700880static bool cpuPresent(const std::string& cpuName)
Jason M. Bills64796042018-10-03 16:51:55 -0700881{
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700882 static constexpr const char* cpuPresencePathPrefix =
883 "/xyz/openbmc_project/inventory/system/chassis/motherboard/";
884 static constexpr const char* cpuPresenceIntf =
885 "xyz.openbmc_project.Inventory.Item";
886 std::string cpuPresencePath = cpuPresencePathPrefix + cpuName;
887 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
888 try
Jason M. Bills64796042018-10-03 16:51:55 -0700889 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700890 auto service =
891 ipmi::getService(*busp, cpuPresenceIntf, cpuPresencePath);
892
893 ipmi::Value result = ipmi::getDbusProperty(
894 *busp, service, cpuPresencePath, cpuPresenceIntf, "Present");
895 return std::get<bool>(result);
896 }
897 catch (const std::exception& e)
898 {
899 phosphor::logging::log<phosphor::logging::level::INFO>(
900 "Cannot find processor presence",
901 phosphor::logging::entry("NAME=%s", cpuName.c_str()));
902 return false;
903 }
904}
905
906ipmi::RspType<bool, // CATERR Reset Enabled
907 bool, // ERR2 Reset Enabled
908 uint6_t, // reserved
909 uint8_t, // reserved, returns 0x3F
910 uint6_t, // CPU1 CATERR Count
911 uint2_t, // CPU1 Status
912 uint6_t, // CPU2 CATERR Count
913 uint2_t, // CPU2 Status
914 uint6_t, // CPU3 CATERR Count
915 uint2_t, // CPU3 Status
916 uint6_t, // CPU4 CATERR Count
917 uint2_t, // CPU4 Status
918 uint8_t // Crashdump Count
919 >
920 ipmiOEMGetProcessorErrConfig()
921{
922 bool resetOnCATERR = false;
923 bool resetOnERR2 = false;
924 uint6_t cpu1CATERRCount = 0;
925 uint6_t cpu2CATERRCount = 0;
926 uint6_t cpu3CATERRCount = 0;
927 uint6_t cpu4CATERRCount = 0;
928 uint8_t crashdumpCount = 0;
Jason M. Bills24df90f2021-06-15 12:46:13 -0700929 uint2_t cpu1Status = cpuPresent("CPU_1")
930 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
931 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
932 uint2_t cpu2Status = cpuPresent("CPU_2")
933 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
934 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
935 uint2_t cpu3Status = cpuPresent("CPU_3")
936 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
937 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
938 uint2_t cpu4Status = cpuPresent("CPU_4")
939 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
940 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700941
942 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
943 try
944 {
945 auto service = ipmi::getService(*busp, processorErrConfigIntf,
946 processorErrConfigObjPath);
947
948 ipmi::PropertyMap result = ipmi::getAllDbusProperties(
949 *busp, service, processorErrConfigObjPath, processorErrConfigIntf);
950 resetOnCATERR = std::get<bool>(result.at("ResetOnCATERR"));
951 resetOnERR2 = std::get<bool>(result.at("ResetOnERR2"));
952 cpu1CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU1"));
953 cpu2CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU2"));
954 cpu3CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU3"));
955 cpu4CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU4"));
956 crashdumpCount = std::get<uint8_t>(result.at("CrashdumpCount"));
957 }
958 catch (const std::exception& e)
959 {
960 phosphor::logging::log<phosphor::logging::level::ERR>(
961 "Failed to fetch processor error config",
962 phosphor::logging::entry("ERROR=%s", e.what()));
963 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700964 }
965
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700966 return ipmi::responseSuccess(resetOnCATERR, resetOnERR2, 0, 0x3F,
967 cpu1CATERRCount, cpu1Status, cpu2CATERRCount,
968 cpu2Status, cpu3CATERRCount, cpu3Status,
969 cpu4CATERRCount, cpu4Status, crashdumpCount);
970}
Jason M. Bills64796042018-10-03 16:51:55 -0700971
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700972ipmi::RspType<> ipmiOEMSetProcessorErrConfig(
973 bool resetOnCATERR, bool resetOnERR2, uint6_t reserved1, uint8_t reserved2,
974 std::optional<bool> clearCPUErrorCount,
975 std::optional<bool> clearCrashdumpCount, std::optional<uint6_t> reserved3)
976{
Jayaprakash Mutyala0a652fa2021-07-01 17:09:39 +0000977 if (reserved1 || reserved2)
978 {
979 return ipmi::responseInvalidFieldRequest();
980 }
981
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700982 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700983
984 try
985 {
Jayaprakash Mutyala0a652fa2021-07-01 17:09:39 +0000986 if (reserved3.value_or(0))
987 {
988 return ipmi::responseInvalidFieldRequest();
989 }
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700990 auto service = ipmi::getService(*busp, processorErrConfigIntf,
991 processorErrConfigObjPath);
992 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
993 processorErrConfigIntf, "ResetOnCATERR",
994 resetOnCATERR);
995 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
996 processorErrConfigIntf, "ResetOnERR2",
997 resetOnERR2);
998 if (clearCPUErrorCount.value_or(false))
999 {
1000 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -07001001 processorErrConfigIntf, "ErrorCountCPU1",
1002 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001003 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -07001004 processorErrConfigIntf, "ErrorCountCPU2",
1005 static_cast<uint8_t>(0));
1006 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
1007 processorErrConfigIntf, "ErrorCountCPU3",
1008 static_cast<uint8_t>(0));
1009 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
1010 processorErrConfigIntf, "ErrorCountCPU4",
1011 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001012 }
1013 if (clearCrashdumpCount.value_or(false))
1014 {
1015 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -07001016 processorErrConfigIntf, "CrashdumpCount",
1017 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001018 }
Jason M. Bills64796042018-10-03 16:51:55 -07001019 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001020 catch (const std::exception& e)
Jason M. Bills64796042018-10-03 16:51:55 -07001021 {
Kuiying Wangbc546672018-11-23 15:41:05 +08001022 phosphor::logging::log<phosphor::logging::level::ERR>(
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001023 "Failed to set processor error config",
1024 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1025 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -07001026 }
1027
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001028 return ipmi::responseSuccess();
Jason M. Bills64796042018-10-03 16:51:55 -07001029}
1030
Yong Li703922d2018-11-06 13:25:31 +08001031ipmi_ret_t ipmiOEMGetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1032 ipmi_request_t request,
1033 ipmi_response_t response,
1034 ipmi_data_len_t dataLen,
1035 ipmi_context_t context)
1036{
1037 GetOEMShutdownPolicyRes* resp =
1038 reinterpret_cast<GetOEMShutdownPolicyRes*>(response);
1039
1040 if (*dataLen != 0)
1041 {
1042 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wang45f04982018-12-26 09:23:08 +08001043 "oem_get_shutdown_policy: invalid input len!");
Yong Li703922d2018-11-06 13:25:31 +08001044 *dataLen = 0;
1045 return IPMI_CC_REQ_DATA_LEN_INVALID;
1046 }
1047
1048 *dataLen = 0;
1049
1050 try
1051 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001052 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +08001053 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001054 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
1055 Value variant = getDbusProperty(
1056 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
1057 oemShutdownPolicyObjPathProp);
Yong Li0669d192019-05-06 14:01:46 +08001058
1059 if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1060 convertPolicyFromString(std::get<std::string>(variant)) ==
1061 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
1062 NoShutdownOnOCOT)
1063 {
1064 resp->policy = 0;
1065 }
1066 else if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1067 convertPolicyFromString(std::get<std::string>(variant)) ==
1068 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1069 Policy::ShutdownOnOCOT)
1070 {
1071 resp->policy = 1;
1072 }
1073 else
1074 {
1075 phosphor::logging::log<phosphor::logging::level::ERR>(
1076 "oem_set_shutdown_policy: invalid property!",
1077 phosphor::logging::entry(
1078 "PROP=%s", std::get<std::string>(variant).c_str()));
1079 return IPMI_CC_UNSPECIFIED_ERROR;
1080 }
Yong Li703922d2018-11-06 13:25:31 +08001081 // TODO needs to check if it is multi-node products,
1082 // policy is only supported on node 3/4
1083 resp->policySupport = shutdownPolicySupported;
1084 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001085 catch (const sdbusplus::exception_t& e)
Yong Li703922d2018-11-06 13:25:31 +08001086 {
1087 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1088 return IPMI_CC_UNSPECIFIED_ERROR;
1089 }
1090
1091 *dataLen = sizeof(GetOEMShutdownPolicyRes);
1092 return IPMI_CC_OK;
1093}
1094
1095ipmi_ret_t ipmiOEMSetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1096 ipmi_request_t request,
1097 ipmi_response_t response,
1098 ipmi_data_len_t dataLen,
1099 ipmi_context_t context)
1100{
1101 uint8_t* req = reinterpret_cast<uint8_t*>(request);
Yong Li0669d192019-05-06 14:01:46 +08001102 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy policy =
1103 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
1104 NoShutdownOnOCOT;
Yong Li703922d2018-11-06 13:25:31 +08001105
1106 // TODO needs to check if it is multi-node products,
1107 // policy is only supported on node 3/4
1108 if (*dataLen != 1)
1109 {
1110 phosphor::logging::log<phosphor::logging::level::ERR>(
1111 "oem_set_shutdown_policy: invalid input len!");
1112 *dataLen = 0;
1113 return IPMI_CC_REQ_DATA_LEN_INVALID;
1114 }
1115
1116 *dataLen = 0;
1117 if ((*req != noShutdownOnOCOT) && (*req != shutdownOnOCOT))
1118 {
1119 phosphor::logging::log<phosphor::logging::level::ERR>(
1120 "oem_set_shutdown_policy: invalid input!");
1121 return IPMI_CC_INVALID_FIELD_REQUEST;
1122 }
1123
Yong Li0669d192019-05-06 14:01:46 +08001124 if (*req == noShutdownOnOCOT)
1125 {
1126 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1127 Policy::NoShutdownOnOCOT;
1128 }
1129 else
1130 {
1131 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1132 Policy::ShutdownOnOCOT;
1133 }
1134
Yong Li703922d2018-11-06 13:25:31 +08001135 try
1136 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001137 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +08001138 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001139 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
Yong Li0669d192019-05-06 14:01:46 +08001140 setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07001141 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
Yong Li0669d192019-05-06 14:01:46 +08001142 oemShutdownPolicyObjPathProp,
1143 sdbusplus::com::intel::Control::server::convertForMessage(policy));
Yong Li703922d2018-11-06 13:25:31 +08001144 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001145 catch (const sdbusplus::exception_t& e)
Yong Li703922d2018-11-06 13:25:31 +08001146 {
1147 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1148 return IPMI_CC_UNSPECIFIED_ERROR;
1149 }
1150
1151 return IPMI_CC_OK;
1152}
1153
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301154/** @brief implementation for check the DHCP or not in IPv4
1155 * @param[in] Channel - Channel number
1156 * @returns true or false.
1157 */
1158static bool isDHCPEnabled(uint8_t Channel)
1159{
1160 try
1161 {
1162 auto ethdevice = getChannelName(Channel);
1163 if (ethdevice.empty())
1164 {
1165 return false;
1166 }
1167 auto ethIP = ethdevice + "/ipv4";
Vernon Mauery15419dd2019-05-24 09:40:30 -07001168 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301169 auto ethernetObj =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001170 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
1171 auto value = getDbusProperty(*dbus, networkService, ethernetObj.first,
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301172 networkIPIntf, "Origin");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001173 if (std::get<std::string>(value) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301174 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1175 {
1176 return true;
1177 }
1178 else
1179 {
1180 return false;
1181 }
1182 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001183 catch (const sdbusplus::exception_t& e)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301184 {
1185 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1186 return true;
1187 }
1188}
1189
1190/** @brief implementes for check the DHCP or not in IPv6
1191 * @param[in] Channel - Channel number
1192 * @returns true or false.
1193 */
1194static bool isDHCPIPv6Enabled(uint8_t Channel)
1195{
1196
1197 try
1198 {
1199 auto ethdevice = getChannelName(Channel);
1200 if (ethdevice.empty())
1201 {
1202 return false;
1203 }
1204 auto ethIP = ethdevice + "/ipv6";
Vernon Mauery15419dd2019-05-24 09:40:30 -07001205 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301206 auto objectInfo =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001207 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
1208 auto properties = getAllDbusProperties(*dbus, objectInfo.second,
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301209 objectInfo.first, networkIPIntf);
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001210 if (std::get<std::string>(properties["Origin"]) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301211 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1212 {
1213 return true;
1214 }
1215 else
1216 {
1217 return false;
1218 }
1219 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001220 catch (const sdbusplus::exception_t& e)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301221 {
1222 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1223 return true;
1224 }
1225}
1226
1227/** @brief implementes the creating of default new user
1228 * @param[in] userName - new username in 16 bytes.
1229 * @param[in] userPassword - new password in 20 bytes
1230 * @returns ipmi completion code.
1231 */
1232ipmi::RspType<> ipmiOEMSetUser2Activation(
1233 std::array<uint8_t, ipmi::ipmiMaxUserName>& userName,
Vernon Mauery3b3d29b2021-08-05 15:03:35 -07001234 const SecureBuffer& userPassword)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301235{
Vernon Mauery3b3d29b2021-08-05 15:03:35 -07001236 if (userPassword.size() != ipmi::maxIpmi20PasswordSize)
1237 {
1238 return ipmi::responseReqDataLenInvalid();
1239 }
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301240 bool userState = false;
1241 // Check for System Interface not exist and LAN should be static
1242 for (uint8_t channel = 0; channel < maxIpmiChannels; channel++)
1243 {
Manish Baing440f62b2021-07-15 22:00:37 +00001244 ChannelInfo chInfo{};
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301245 try
1246 {
1247 getChannelInfo(channel, chInfo);
1248 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001249 catch (const sdbusplus::exception_t& e)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301250 {
1251 phosphor::logging::log<phosphor::logging::level::ERR>(
1252 "ipmiOEMSetUser2Activation: Failed to get Channel Info",
1253 phosphor::logging::entry("MSG: %s", e.description()));
1254 return ipmi::response(ipmi::ccUnspecifiedError);
1255 }
1256 if (chInfo.mediumType ==
1257 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1258 {
1259 phosphor::logging::log<phosphor::logging::level::ERR>(
1260 "ipmiOEMSetUser2Activation: system interface exist .");
1261 return ipmi::response(ipmi::ccCommandNotAvailable);
1262 }
1263 else
1264 {
1265
1266 if (chInfo.mediumType ==
1267 static_cast<uint8_t>(EChannelMediumType::lan8032))
1268 {
1269 if (isDHCPIPv6Enabled(channel) || isDHCPEnabled(channel))
1270 {
1271 phosphor::logging::log<phosphor::logging::level::ERR>(
1272 "ipmiOEMSetUser2Activation: DHCP enabled .");
1273 return ipmi::response(ipmi::ccCommandNotAvailable);
1274 }
1275 }
1276 }
1277 }
1278 uint8_t maxChUsers = 0, enabledUsers = 0, fixedUsers = 0;
1279 if (ipmi::ccSuccess ==
1280 ipmiUserGetAllCounts(maxChUsers, enabledUsers, fixedUsers))
1281 {
1282 if (enabledUsers > 1)
1283 {
1284 phosphor::logging::log<phosphor::logging::level::ERR>(
1285 "ipmiOEMSetUser2Activation: more than one user is enabled.");
1286 return ipmi::response(ipmi::ccCommandNotAvailable);
1287 }
1288 // Check the user 2 is enabled or not
1289 ipmiUserCheckEnabled(ipmiDefaultUserId, userState);
1290 if (userState == true)
1291 {
1292 phosphor::logging::log<phosphor::logging::level::ERR>(
1293 "ipmiOEMSetUser2Activation: user 2 already enabled .");
1294 return ipmi::response(ipmi::ccCommandNotAvailable);
1295 }
1296 }
1297 else
1298 {
1299 return ipmi::response(ipmi::ccUnspecifiedError);
1300 }
1301
1302#if BYTE_ORDER == LITTLE_ENDIAN
1303 PrivAccess privAccess = {PRIVILEGE_ADMIN, true, true, true, 0};
1304#endif
1305#if BYTE_ORDER == BIG_ENDIAN
1306 PrivAccess privAccess = {0, true, true, true, PRIVILEGE_ADMIN};
1307#endif
1308
Vernon Mauery037cabd2020-05-14 12:16:01 -07001309 // ipmiUserSetUserName correctly handles char*, possibly non-null
1310 // terminated strings using ipmiMaxUserName size
Jayaprakash Mutyala3fbe8d22020-10-29 14:42:59 +00001311 size_t nameLen = strnlen(reinterpret_cast<const char*>(userName.data()),
1312 sizeof(userName));
1313 const std::string userNameRaw(
1314 reinterpret_cast<const char*>(userName.data()), nameLen);
jayaprakash Mutyala1429d4f2020-03-04 18:20:16 +00001315
Vernon Mauery037cabd2020-05-14 12:16:01 -07001316 if (ipmi::ccSuccess == ipmiUserSetUserName(ipmiDefaultUserId, userNameRaw))
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301317 {
1318 if (ipmi::ccSuccess ==
1319 ipmiUserSetUserPassword(
1320 ipmiDefaultUserId,
1321 reinterpret_cast<const char*>(userPassword.data())))
1322 {
1323 if (ipmi::ccSuccess ==
1324 ipmiUserSetPrivilegeAccess(
1325 ipmiDefaultUserId,
1326 static_cast<uint8_t>(ipmi::EChannelID::chanLan1),
1327 privAccess, true))
1328 {
1329 phosphor::logging::log<phosphor::logging::level::INFO>(
1330 "ipmiOEMSetUser2Activation: user created successfully ");
Jayaprakash Mutyala94204162020-10-23 06:17:56 +00001331
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301332 return ipmi::responseSuccess();
1333 }
1334 }
1335 // we need to delete the default user id which added in this command as
1336 // password / priv setting is failed.
Jayaprakash Mutyala3fbe8d22020-10-29 14:42:59 +00001337 ipmiUserSetUserName(ipmiDefaultUserId, static_cast<std::string>(""));
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301338 phosphor::logging::log<phosphor::logging::level::ERR>(
1339 "ipmiOEMSetUser2Activation: password / priv setting is failed.");
1340 }
1341 else
1342 {
1343 phosphor::logging::log<phosphor::logging::level::ERR>(
1344 "ipmiOEMSetUser2Activation: Setting username failed.");
1345 }
1346
1347 return ipmi::response(ipmi::ccCommandNotAvailable);
1348}
1349
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301350/** @brief implementes executing the linux command
1351 * @param[in] linux command
1352 * @returns status
1353 */
1354
1355static uint8_t executeCmd(const char* path)
1356{
1357 boost::process::child execProg(path);
1358 execProg.wait();
1359
1360 int retCode = execProg.exit_code();
1361 if (retCode)
1362 {
1363 return ipmi::ccUnspecifiedError;
1364 }
1365 return ipmi::ccSuccess;
1366}
1367
1368/** @brief implementes ASD Security event logging
1369 * @param[in] Event message string
1370 * @param[in] Event Severity
1371 * @returns status
1372 */
1373
1374static void atScaleDebugEventlog(std::string msg, int severity)
1375{
1376 std::string eventStr = "OpenBMC.0.1." + msg;
1377 sd_journal_send("MESSAGE=Security Event: %s", eventStr.c_str(),
1378 "PRIORITY=%i", severity, "REDFISH_MESSAGE_ID=%s",
1379 eventStr.c_str(), NULL);
1380}
1381
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301382/** @brief implementes setting password for special user
1383 * @param[in] specialUserIndex
1384 * @param[in] userPassword - new password in 20 bytes
1385 * @returns ipmi completion code.
1386 */
1387ipmi::RspType<> ipmiOEMSetSpecialUserPassword(ipmi::Context::ptr ctx,
1388 uint8_t specialUserIndex,
1389 std::vector<uint8_t> userPassword)
1390{
1391 ChannelInfo chInfo;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301392 ipmi_ret_t status = ipmi::ccSuccess;
1393
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301394 try
1395 {
1396 getChannelInfo(ctx->channel, chInfo);
1397 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001398 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301399 {
1400 phosphor::logging::log<phosphor::logging::level::ERR>(
1401 "ipmiOEMSetSpecialUserPassword: Failed to get Channel Info",
1402 phosphor::logging::entry("MSG: %s", e.description()));
1403 return ipmi::responseUnspecifiedError();
1404 }
1405 if (chInfo.mediumType !=
1406 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1407 {
1408 phosphor::logging::log<phosphor::logging::level::ERR>(
1409 "ipmiOEMSetSpecialUserPassword: Error - supported only in KCS "
1410 "interface");
1411 return ipmi::responseCommandNotAvailable();
1412 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301413
1414 // 0 for root user and 1 for AtScaleDebug is allowed
1415 if (specialUserIndex >
1416 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301417 {
1418 phosphor::logging::log<phosphor::logging::level::ERR>(
1419 "ipmiOEMSetSpecialUserPassword: Invalid user account");
1420 return ipmi::responseParmOutOfRange();
1421 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301422 if (userPassword.size() != 0)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301423 {
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301424 constexpr uint8_t minPasswordSizeRequired = 6;
Patrick Williams23939852021-09-02 11:18:35 -05001425 SecureString passwd;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301426 if (userPassword.size() < minPasswordSizeRequired ||
1427 userPassword.size() > ipmi::maxIpmi20PasswordSize)
1428 {
Jayaprakash Mutyala94204162020-10-23 06:17:56 +00001429 OPENSSL_cleanse(userPassword.data(), userPassword.size());
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301430 return ipmi::responseReqDataLenInvalid();
1431 }
1432 passwd.assign(reinterpret_cast<const char*>(userPassword.data()),
1433 userPassword.size());
Jayaprakash Mutyala94204162020-10-23 06:17:56 +00001434 // Clear sensitive data
1435 OPENSSL_cleanse(userPassword.data(), userPassword.size());
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301436 if (specialUserIndex ==
1437 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
1438 {
1439 status = ipmiSetSpecialUserPassword("asdbg", passwd);
1440
1441 atScaleDebugEventlog("AtScaleDebugSpecialUserEnabled", LOG_CRIT);
1442 }
1443 else
1444 {
1445 status = ipmiSetSpecialUserPassword("root", passwd);
1446 }
1447 return ipmi::response(status);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301448 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301449 else
1450 {
1451 if (specialUserIndex ==
1452 static_cast<uint8_t>(SpecialUserIndex::rootUser))
1453 {
1454 status = executeCmd("passwd -d root");
1455 }
1456 else
1457 {
1458
1459 status = executeCmd("passwd -d asdbg");
1460
1461 if (status == 0)
1462 {
1463 atScaleDebugEventlog("AtScaleDebugSpecialUserDisabled",
1464 LOG_INFO);
1465 }
1466 }
1467 return ipmi::response(status);
1468 }
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301469}
1470
Kuiying Wang45f04982018-12-26 09:23:08 +08001471namespace ledAction
1472{
1473using namespace sdbusplus::xyz::openbmc_project::Led::server;
1474std::map<Physical::Action, uint8_t> actionDbusToIpmi = {
jayaprakash Mutyala934ee9c2019-12-13 17:49:27 +00001475 {Physical::Action::Off, 0},
1476 {Physical::Action::On, 2},
1477 {Physical::Action::Blink, 1}};
Kuiying Wang45f04982018-12-26 09:23:08 +08001478
1479std::map<uint8_t, std::string> offsetObjPath = {
1480 {2, statusAmberObjPath}, {4, statusGreenObjPath}, {6, identifyLEDObjPath}};
1481
1482} // namespace ledAction
1483
Patrick Williamsf944d2e2022-07-22 19:26:52 -05001484int8_t getLEDState(sdbusplus::bus_t& bus, const std::string& intf,
Kuiying Wang45f04982018-12-26 09:23:08 +08001485 const std::string& objPath, uint8_t& state)
1486{
1487 try
1488 {
1489 std::string service = getService(bus, intf, objPath);
1490 Value stateValue =
1491 getDbusProperty(bus, service, objPath, intf, "State");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001492 std::string strState = std::get<std::string>(stateValue);
Kuiying Wang45f04982018-12-26 09:23:08 +08001493 state = ledAction::actionDbusToIpmi.at(
1494 sdbusplus::xyz::openbmc_project::Led::server::Physical::
1495 convertActionFromString(strState));
1496 }
Patrick Williamsf944d2e2022-07-22 19:26:52 -05001497 catch (const sdbusplus::exception_t& e)
Kuiying Wang45f04982018-12-26 09:23:08 +08001498 {
1499 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1500 return -1;
1501 }
1502 return 0;
1503}
1504
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001505ipmi::RspType<uint8_t> ipmiOEMGetLEDStatus()
Kuiying Wang45f04982018-12-26 09:23:08 +08001506{
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001507 uint8_t ledstate = 0;
Kuiying Wang45f04982018-12-26 09:23:08 +08001508 phosphor::logging::log<phosphor::logging::level::DEBUG>("GET led status");
Vernon Mauery15419dd2019-05-24 09:40:30 -07001509 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Kuiying Wang45f04982018-12-26 09:23:08 +08001510 for (auto it = ledAction::offsetObjPath.begin();
1511 it != ledAction::offsetObjPath.end(); ++it)
1512 {
1513 uint8_t state = 0;
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001514 if (getLEDState(*dbus, ledIntf, it->second, state) == -1)
Kuiying Wang45f04982018-12-26 09:23:08 +08001515 {
1516 phosphor::logging::log<phosphor::logging::level::ERR>(
1517 "oem_get_led_status: fail to get ID LED status!");
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001518 return ipmi::responseUnspecifiedError();
Kuiying Wang45f04982018-12-26 09:23:08 +08001519 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001520 ledstate |= state << it->first;
Kuiying Wang45f04982018-12-26 09:23:08 +08001521 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001522 return ipmi::responseSuccess(ledstate);
Kuiying Wang45f04982018-12-26 09:23:08 +08001523}
1524
Yong Li23737fe2019-02-19 08:49:55 +08001525ipmi_ret_t ipmiOEMCfgHostSerialPortSpeed(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1526 ipmi_request_t request,
1527 ipmi_response_t response,
1528 ipmi_data_len_t dataLen,
1529 ipmi_context_t context)
1530{
1531 CfgHostSerialReq* req = reinterpret_cast<CfgHostSerialReq*>(request);
1532 uint8_t* resp = reinterpret_cast<uint8_t*>(response);
1533
1534 if (*dataLen == 0)
1535 {
1536 phosphor::logging::log<phosphor::logging::level::ERR>(
1537 "CfgHostSerial: invalid input len!",
1538 phosphor::logging::entry("LEN=%d", *dataLen));
1539 return IPMI_CC_REQ_DATA_LEN_INVALID;
1540 }
1541
1542 switch (req->command)
1543 {
1544 case getHostSerialCfgCmd:
1545 {
1546 if (*dataLen != 1)
1547 {
1548 phosphor::logging::log<phosphor::logging::level::ERR>(
1549 "CfgHostSerial: invalid input len!");
1550 *dataLen = 0;
1551 return IPMI_CC_REQ_DATA_LEN_INVALID;
1552 }
1553
1554 *dataLen = 0;
1555
1556 boost::process::ipstream is;
1557 std::vector<std::string> data;
1558 std::string line;
1559 boost::process::child c1(fwGetEnvCmd, "-n", fwHostSerailCfgEnvName,
1560 boost::process::std_out > is);
1561
1562 while (c1.running() && std::getline(is, line) && !line.empty())
1563 {
1564 data.push_back(line);
1565 }
1566
1567 c1.wait();
1568 if (c1.exit_code())
1569 {
1570 phosphor::logging::log<phosphor::logging::level::ERR>(
1571 "CfgHostSerial:: error on execute",
1572 phosphor::logging::entry("EXECUTE=%s", fwSetEnvCmd));
1573 // Using the default value
1574 *resp = 0;
1575 }
1576 else
1577 {
1578 if (data.size() != 1)
1579 {
1580 phosphor::logging::log<phosphor::logging::level::ERR>(
1581 "CfgHostSerial:: error on read env");
1582 return IPMI_CC_UNSPECIFIED_ERROR;
1583 }
1584 try
1585 {
1586 unsigned long tmp = std::stoul(data[0]);
1587 if (tmp > std::numeric_limits<uint8_t>::max())
1588 {
1589 throw std::out_of_range("Out of range");
1590 }
1591 *resp = static_cast<uint8_t>(tmp);
1592 }
1593 catch (const std::invalid_argument& e)
1594 {
1595 phosphor::logging::log<phosphor::logging::level::ERR>(
1596 "invalid config ",
1597 phosphor::logging::entry("ERR=%s", e.what()));
1598 return IPMI_CC_UNSPECIFIED_ERROR;
1599 }
1600 catch (const std::out_of_range& e)
1601 {
1602 phosphor::logging::log<phosphor::logging::level::ERR>(
1603 "out_of_range config ",
1604 phosphor::logging::entry("ERR=%s", e.what()));
1605 return IPMI_CC_UNSPECIFIED_ERROR;
1606 }
1607 }
1608
1609 *dataLen = 1;
1610 break;
1611 }
1612 case setHostSerialCfgCmd:
1613 {
1614 if (*dataLen != sizeof(CfgHostSerialReq))
1615 {
1616 phosphor::logging::log<phosphor::logging::level::ERR>(
1617 "CfgHostSerial: invalid input len!");
1618 *dataLen = 0;
1619 return IPMI_CC_REQ_DATA_LEN_INVALID;
1620 }
1621
1622 *dataLen = 0;
1623
1624 if (req->parameter > HostSerialCfgParamMax)
1625 {
1626 phosphor::logging::log<phosphor::logging::level::ERR>(
1627 "CfgHostSerial: invalid input!");
1628 return IPMI_CC_INVALID_FIELD_REQUEST;
1629 }
1630
1631 boost::process::child c1(fwSetEnvCmd, fwHostSerailCfgEnvName,
1632 std::to_string(req->parameter));
1633
1634 c1.wait();
1635 if (c1.exit_code())
1636 {
1637 phosphor::logging::log<phosphor::logging::level::ERR>(
1638 "CfgHostSerial:: error on execute",
1639 phosphor::logging::entry("EXECUTE=%s", fwGetEnvCmd));
1640 return IPMI_CC_UNSPECIFIED_ERROR;
1641 }
1642 break;
1643 }
1644 default:
1645 phosphor::logging::log<phosphor::logging::level::ERR>(
1646 "CfgHostSerial: invalid input!");
1647 *dataLen = 0;
1648 return IPMI_CC_INVALID_FIELD_REQUEST;
1649 }
1650
1651 return IPMI_CC_OK;
1652}
1653
James Feist91244a62019-02-19 15:04:54 -08001654constexpr const char* thermalModeInterface =
1655 "xyz.openbmc_project.Control.ThermalMode";
1656constexpr const char* thermalModePath =
1657 "/xyz/openbmc_project/control/thermal_mode";
1658
1659bool getFanProfileInterface(
Patrick Williamsf944d2e2022-07-22 19:26:52 -05001660 sdbusplus::bus_t& bus,
James Feist91244a62019-02-19 15:04:54 -08001661 boost::container::flat_map<
1662 std::string, std::variant<std::vector<std::string>, std::string>>& resp)
1663{
1664 auto call = bus.new_method_call(settingsBusName, thermalModePath, PROP_INTF,
1665 "GetAll");
1666 call.append(thermalModeInterface);
1667 try
1668 {
1669 auto data = bus.call(call);
1670 data.read(resp);
1671 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001672 catch (const sdbusplus::exception_t& e)
James Feist91244a62019-02-19 15:04:54 -08001673 {
1674 phosphor::logging::log<phosphor::logging::level::ERR>(
1675 "getFanProfileInterface: can't get thermal mode!",
1676 phosphor::logging::entry("ERR=%s", e.what()));
1677 return false;
1678 }
1679 return true;
1680}
1681
anil kumar appanaf945eee2019-09-25 23:29:11 +00001682/**@brief implements the OEM set fan config.
1683 * @param selectedFanProfile - fan profile to enable
1684 * @param reserved1
1685 * @param performanceMode - Performance/Acoustic mode
1686 * @param reserved2
1687 * @param setPerformanceMode - set Performance/Acoustic mode
1688 * @param setFanProfile - set fan profile
1689 *
1690 * @return IPMI completion code.
1691 **/
1692ipmi::RspType<> ipmiOEMSetFanConfig(uint8_t selectedFanProfile,
1693
1694 uint2_t reserved1, bool performanceMode,
1695 uint3_t reserved2, bool setPerformanceMode,
Joshi-Mansi619186d2020-01-27 19:16:03 +05301696 bool setFanProfile,
1697 std::optional<uint8_t> dimmGroupId,
1698 std::optional<uint32_t> dimmPresenceBitmap)
James Feist91244a62019-02-19 15:04:54 -08001699{
anil kumar appanaf945eee2019-09-25 23:29:11 +00001700 if (reserved1 || reserved2)
James Feist91244a62019-02-19 15:04:54 -08001701 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001702 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001703 }
Joshi-Mansi619186d2020-01-27 19:16:03 +05301704
1705 if (dimmGroupId)
1706 {
1707 if (*dimmGroupId >= maxCPUNum)
1708 {
1709 return ipmi::responseInvalidFieldRequest();
1710 }
1711 if (!cpuPresent("CPU_" + std::to_string(*dimmGroupId + 1)))
1712 {
1713 return ipmi::responseInvalidFieldRequest();
1714 }
1715 }
1716
James Feist91244a62019-02-19 15:04:54 -08001717 // todo: tell bios to only send first 2 bytes
James Feist91244a62019-02-19 15:04:54 -08001718 boost::container::flat_map<
1719 std::string, std::variant<std::vector<std::string>, std::string>>
1720 profileData;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001721 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1722 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001723 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001724 return ipmi::responseUnspecifiedError();
James Feist91244a62019-02-19 15:04:54 -08001725 }
1726
1727 std::vector<std::string>* supported =
1728 std::get_if<std::vector<std::string>>(&profileData["Supported"]);
1729 if (supported == nullptr)
1730 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001731 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001732 }
1733 std::string mode;
anil kumar appanaf945eee2019-09-25 23:29:11 +00001734 if (setPerformanceMode)
James Feist91244a62019-02-19 15:04:54 -08001735 {
James Feist91244a62019-02-19 15:04:54 -08001736 if (performanceMode)
1737 {
1738
1739 if (std::find(supported->begin(), supported->end(),
1740 "Performance") != supported->end())
1741 {
1742 mode = "Performance";
1743 }
1744 }
1745 else
1746 {
James Feist91244a62019-02-19 15:04:54 -08001747 if (std::find(supported->begin(), supported->end(), "Acoustic") !=
1748 supported->end())
1749 {
1750 mode = "Acoustic";
1751 }
1752 }
1753 if (mode.empty())
1754 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001755 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001756 }
anil kumar appanaf945eee2019-09-25 23:29:11 +00001757
1758 try
1759 {
1760 setDbusProperty(*dbus, settingsBusName, thermalModePath,
1761 thermalModeInterface, "Current", mode);
1762 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001763 catch (const sdbusplus::exception_t& e)
anil kumar appanaf945eee2019-09-25 23:29:11 +00001764 {
1765 phosphor::logging::log<phosphor::logging::level::ERR>(
1766 "ipmiOEMSetFanConfig: can't set thermal mode!",
1767 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1768 return ipmi::responseResponseError();
1769 }
James Feist91244a62019-02-19 15:04:54 -08001770 }
1771
anil kumar appanaf945eee2019-09-25 23:29:11 +00001772 return ipmi::responseSuccess();
James Feist91244a62019-02-19 15:04:54 -08001773}
1774
James Feist5b693632019-07-09 09:06:09 -07001775ipmi::RspType<uint8_t, // profile support map
1776 uint8_t, // fan control profile enable
1777 uint8_t, // flags
1778 uint32_t // dimm presence bit map
1779 >
1780 ipmiOEMGetFanConfig(uint8_t dimmGroupId)
James Feist91244a62019-02-19 15:04:54 -08001781{
Joshi-Mansi36f05ce2020-01-14 14:29:34 +05301782 if (dimmGroupId >= maxCPUNum)
1783 {
1784 return ipmi::responseInvalidFieldRequest();
1785 }
1786
1787 bool cpuStatus = cpuPresent("CPU_" + std::to_string(dimmGroupId + 1));
1788
1789 if (!cpuStatus)
1790 {
1791 return ipmi::responseInvalidFieldRequest();
1792 }
1793
James Feist91244a62019-02-19 15:04:54 -08001794 boost::container::flat_map<
1795 std::string, std::variant<std::vector<std::string>, std::string>>
1796 profileData;
1797
Vernon Mauery15419dd2019-05-24 09:40:30 -07001798 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1799 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001800 {
James Feist5b693632019-07-09 09:06:09 -07001801 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001802 }
1803
1804 std::string* current = std::get_if<std::string>(&profileData["Current"]);
1805
1806 if (current == nullptr)
1807 {
1808 phosphor::logging::log<phosphor::logging::level::ERR>(
1809 "ipmiOEMGetFanConfig: can't get current mode!");
James Feist5b693632019-07-09 09:06:09 -07001810 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001811 }
1812 bool performance = (*current == "Performance");
1813
James Feist5b693632019-07-09 09:06:09 -07001814 uint8_t flags = 0;
James Feist91244a62019-02-19 15:04:54 -08001815 if (performance)
1816 {
James Feist5b693632019-07-09 09:06:09 -07001817 flags |= 1 << 2;
James Feist91244a62019-02-19 15:04:54 -08001818 }
1819
jayaprakash Mutyala4b1552d2020-02-11 12:07:29 +00001820 constexpr uint8_t fanControlDefaultProfile = 0x80;
1821 constexpr uint8_t fanControlProfileState = 0x00;
1822 constexpr uint32_t dimmPresenceBitmap = 0x00;
1823
1824 return ipmi::responseSuccess(fanControlDefaultProfile,
1825 fanControlProfileState, flags,
1826 dimmPresenceBitmap);
James Feist91244a62019-02-19 15:04:54 -08001827}
James Feist5f957ca2019-03-14 15:33:55 -07001828constexpr const char* cfmLimitSettingPath =
1829 "/xyz/openbmc_project/control/cfm_limit";
1830constexpr const char* cfmLimitIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistfaa4f222019-03-21 16:21:55 -07001831constexpr const size_t legacyExitAirSensorNumber = 0x2e;
James Feist09f6b602019-08-08 11:30:03 -07001832constexpr const size_t legacyPCHSensorNumber = 0x22;
1833constexpr const char* exitAirPathName = "Exit_Air";
1834constexpr const char* pchPathName = "SSB_Temp";
James Feistacc8a4e2019-04-02 14:23:57 -07001835constexpr const char* pidConfigurationIface =
1836 "xyz.openbmc_project.Configuration.Pid";
James Feistfaa4f222019-03-21 16:21:55 -07001837
James Feist09f6b602019-08-08 11:30:03 -07001838static std::string getConfigPath(const std::string& name)
James Feistfaa4f222019-03-21 16:21:55 -07001839{
Vernon Mauery15419dd2019-05-24 09:40:30 -07001840 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistfaa4f222019-03-21 16:21:55 -07001841 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001842 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1843 "/xyz/openbmc_project/object_mapper",
1844 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistfaa4f222019-03-21 16:21:55 -07001845
James Feistacc8a4e2019-04-02 14:23:57 -07001846 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
James Feistfaa4f222019-03-21 16:21:55 -07001847 std::string path;
1848 GetSubTreeType resp;
1849 try
1850 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001851 auto reply = dbus->call(method);
James Feistfaa4f222019-03-21 16:21:55 -07001852 reply.read(resp);
1853 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001854 catch (const sdbusplus::exception_t&)
James Feistfaa4f222019-03-21 16:21:55 -07001855 {
1856 phosphor::logging::log<phosphor::logging::level::ERR>(
1857 "ipmiOEMGetFscParameter: mapper error");
1858 };
James Feist09f6b602019-08-08 11:30:03 -07001859 auto config =
1860 std::find_if(resp.begin(), resp.end(), [&name](const auto& pair) {
1861 return pair.first.find(name) != std::string::npos;
1862 });
James Feistfaa4f222019-03-21 16:21:55 -07001863 if (config != resp.end())
1864 {
1865 path = std::move(config->first);
1866 }
1867 return path;
1868}
James Feist5f957ca2019-03-14 15:33:55 -07001869
James Feistacc8a4e2019-04-02 14:23:57 -07001870// flat map to make alphabetical
1871static boost::container::flat_map<std::string, PropertyMap> getPidConfigs()
1872{
1873 boost::container::flat_map<std::string, PropertyMap> ret;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001874 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001875 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001876 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1877 "/xyz/openbmc_project/object_mapper",
1878 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistacc8a4e2019-04-02 14:23:57 -07001879
1880 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
1881 GetSubTreeType resp;
1882
1883 try
1884 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001885 auto reply = dbus->call(method);
James Feistacc8a4e2019-04-02 14:23:57 -07001886 reply.read(resp);
1887 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001888 catch (const sdbusplus::exception_t&)
James Feistacc8a4e2019-04-02 14:23:57 -07001889 {
1890 phosphor::logging::log<phosphor::logging::level::ERR>(
1891 "getFanConfigPaths: mapper error");
1892 };
1893 for (const auto& [path, objects] : resp)
1894 {
1895 if (objects.empty())
1896 {
1897 continue; // should be impossible
1898 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04001899
1900 try
1901 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001902 ret.emplace(path,
1903 getAllDbusProperties(*dbus, objects[0].first, path,
1904 pidConfigurationIface));
Zhu, Yungebe560b02019-04-21 21:19:21 -04001905 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001906 catch (const sdbusplus::exception_t& e)
Zhu, Yungebe560b02019-04-21 21:19:21 -04001907 {
1908 phosphor::logging::log<phosphor::logging::level::ERR>(
1909 "getPidConfigs: can't get DbusProperties!",
1910 phosphor::logging::entry("ERR=%s", e.what()));
1911 }
James Feistacc8a4e2019-04-02 14:23:57 -07001912 }
1913 return ret;
1914}
1915
1916ipmi::RspType<uint8_t> ipmiOEMGetFanSpeedOffset(void)
1917{
1918 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1919 if (data.empty())
1920 {
1921 return ipmi::responseResponseError();
1922 }
1923 uint8_t minOffset = std::numeric_limits<uint8_t>::max();
1924 for (const auto& [_, pid] : data)
1925 {
1926 auto findClass = pid.find("Class");
1927 if (findClass == pid.end())
1928 {
1929 phosphor::logging::log<phosphor::logging::level::ERR>(
1930 "ipmiOEMGetFscParameter: found illegal pid "
1931 "configurations");
1932 return ipmi::responseResponseError();
1933 }
1934 std::string type = std::get<std::string>(findClass->second);
1935 if (type == "fan")
1936 {
1937 auto findOutLimit = pid.find("OutLimitMin");
1938 if (findOutLimit == pid.end())
1939 {
1940 phosphor::logging::log<phosphor::logging::level::ERR>(
1941 "ipmiOEMGetFscParameter: found illegal pid "
1942 "configurations");
1943 return ipmi::responseResponseError();
1944 }
1945 // get the min out of all the offsets
1946 minOffset = std::min(
1947 minOffset,
1948 static_cast<uint8_t>(std::get<double>(findOutLimit->second)));
1949 }
1950 }
1951 if (minOffset == std::numeric_limits<uint8_t>::max())
1952 {
1953 phosphor::logging::log<phosphor::logging::level::ERR>(
1954 "ipmiOEMGetFscParameter: found no fan configurations!");
1955 return ipmi::responseResponseError();
1956 }
1957
1958 return ipmi::responseSuccess(minOffset);
1959}
1960
1961ipmi::RspType<> ipmiOEMSetFanSpeedOffset(uint8_t offset)
1962{
Manish Baingbaa579f2021-10-08 22:30:32 +00001963 constexpr uint8_t maxFanSpeedOffset = 100;
1964 if (offset > maxFanSpeedOffset)
1965 {
1966 phosphor::logging::log<phosphor::logging::level::ERR>(
1967 "ipmiOEMSetFanSpeedOffset: fan offset greater than limit");
1968 return ipmi::responseInvalidFieldRequest();
1969 }
James Feistacc8a4e2019-04-02 14:23:57 -07001970 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1971 if (data.empty())
1972 {
1973
1974 phosphor::logging::log<phosphor::logging::level::ERR>(
1975 "ipmiOEMSetFanSpeedOffset: found no pid configurations!");
1976 return ipmi::responseResponseError();
1977 }
1978
Vernon Mauery15419dd2019-05-24 09:40:30 -07001979 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001980 bool found = false;
1981 for (const auto& [path, pid] : data)
1982 {
1983 auto findClass = pid.find("Class");
1984 if (findClass == pid.end())
1985 {
1986
1987 phosphor::logging::log<phosphor::logging::level::ERR>(
1988 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1989 "configurations");
1990 return ipmi::responseResponseError();
1991 }
1992 std::string type = std::get<std::string>(findClass->second);
1993 if (type == "fan")
1994 {
1995 auto findOutLimit = pid.find("OutLimitMin");
1996 if (findOutLimit == pid.end())
1997 {
1998
1999 phosphor::logging::log<phosphor::logging::level::ERR>(
2000 "ipmiOEMSetFanSpeedOffset: found illegal pid "
2001 "configurations");
2002 return ipmi::responseResponseError();
2003 }
Vernon Mauery15419dd2019-05-24 09:40:30 -07002004 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager",
James Feistacc8a4e2019-04-02 14:23:57 -07002005 path, pidConfigurationIface, "OutLimitMin",
2006 static_cast<double>(offset));
2007 found = true;
2008 }
2009 }
2010 if (!found)
2011 {
2012 phosphor::logging::log<phosphor::logging::level::ERR>(
2013 "ipmiOEMSetFanSpeedOffset: set no fan offsets");
2014 return ipmi::responseResponseError();
2015 }
2016
2017 return ipmi::responseSuccess();
2018}
2019
2020ipmi::RspType<> ipmiOEMSetFscParameter(uint8_t command, uint8_t param1,
2021 uint8_t param2)
James Feist5f957ca2019-03-14 15:33:55 -07002022{
2023 constexpr const size_t disableLimiting = 0x0;
2024
Vernon Mauery15419dd2019-05-24 09:40:30 -07002025 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07002026 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07002027 {
James Feist09f6b602019-08-08 11:30:03 -07002028 std::string pathName;
James Feistacc8a4e2019-04-02 14:23:57 -07002029 if (param1 == legacyExitAirSensorNumber)
James Feistfaa4f222019-03-21 16:21:55 -07002030 {
James Feist09f6b602019-08-08 11:30:03 -07002031 pathName = exitAirPathName;
2032 }
2033 else if (param1 == legacyPCHSensorNumber)
2034 {
2035 pathName = pchPathName;
James Feistfaa4f222019-03-21 16:21:55 -07002036 }
2037 else
2038 {
James Feistacc8a4e2019-04-02 14:23:57 -07002039 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07002040 }
James Feist09f6b602019-08-08 11:30:03 -07002041 std::string path = getConfigPath(pathName);
2042 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager", path,
2043 pidConfigurationIface, "SetPoint",
2044 static_cast<double>(param2));
2045 return ipmi::responseSuccess();
James Feistfaa4f222019-03-21 16:21:55 -07002046 }
James Feistacc8a4e2019-04-02 14:23:57 -07002047 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07002048 {
James Feistacc8a4e2019-04-02 14:23:57 -07002049 uint16_t cfm = param1 | (static_cast<uint16_t>(param2) << 8);
James Feist5f957ca2019-03-14 15:33:55 -07002050
2051 // must be greater than 50 based on eps
2052 if (cfm < 50 && cfm != disableLimiting)
2053 {
James Feistacc8a4e2019-04-02 14:23:57 -07002054 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002055 }
2056
2057 try
2058 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002059 ipmi::setDbusProperty(*dbus, settingsBusName, cfmLimitSettingPath,
James Feist5f957ca2019-03-14 15:33:55 -07002060 cfmLimitIface, "Limit",
2061 static_cast<double>(cfm));
2062 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002063 catch (const sdbusplus::exception_t& e)
James Feist5f957ca2019-03-14 15:33:55 -07002064 {
2065 phosphor::logging::log<phosphor::logging::level::ERR>(
2066 "ipmiOEMSetFscParameter: can't set cfm setting!",
2067 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002068 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002069 }
James Feistacc8a4e2019-04-02 14:23:57 -07002070 return ipmi::responseSuccess();
2071 }
2072 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
2073 {
2074 constexpr const size_t maxDomainCount = 8;
2075 uint8_t requestedDomainMask = param1;
2076 boost::container::flat_map data = getPidConfigs();
2077 if (data.empty())
2078 {
2079
2080 phosphor::logging::log<phosphor::logging::level::ERR>(
2081 "ipmiOEMSetFscParameter: found no pid configurations!");
2082 return ipmi::responseResponseError();
2083 }
2084 size_t count = 0;
2085 for (const auto& [path, pid] : data)
2086 {
2087 auto findClass = pid.find("Class");
2088 if (findClass == pid.end())
2089 {
2090
2091 phosphor::logging::log<phosphor::logging::level::ERR>(
2092 "ipmiOEMSetFscParameter: found illegal pid "
2093 "configurations");
2094 return ipmi::responseResponseError();
2095 }
2096 std::string type = std::get<std::string>(findClass->second);
2097 if (type == "fan")
2098 {
2099 if (requestedDomainMask & (1 << count))
2100 {
2101 ipmi::setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002102 *dbus, "xyz.openbmc_project.EntityManager", path,
James Feistacc8a4e2019-04-02 14:23:57 -07002103 pidConfigurationIface, "OutLimitMax",
2104 static_cast<double>(param2));
2105 }
2106 count++;
2107 }
2108 }
2109 return ipmi::responseSuccess();
James Feist5f957ca2019-03-14 15:33:55 -07002110 }
2111 else
2112 {
2113 // todo other command parts possibly
2114 // tcontrol is handled in peci now
2115 // fan speed offset not implemented yet
2116 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002117 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002118 }
2119}
2120
James Feistacc8a4e2019-04-02 14:23:57 -07002121ipmi::RspType<
2122 std::variant<uint8_t, std::array<uint8_t, 2>, std::array<uint16_t, 2>>>
2123 ipmiOEMGetFscParameter(uint8_t command, std::optional<uint8_t> param)
James Feist5f957ca2019-03-14 15:33:55 -07002124{
James Feist09f6b602019-08-08 11:30:03 -07002125 constexpr uint8_t legacyDefaultSetpoint = -128;
James Feist5f957ca2019-03-14 15:33:55 -07002126
Vernon Mauery15419dd2019-05-24 09:40:30 -07002127 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07002128 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07002129 {
James Feistacc8a4e2019-04-02 14:23:57 -07002130 if (!param)
James Feistfaa4f222019-03-21 16:21:55 -07002131 {
James Feistacc8a4e2019-04-02 14:23:57 -07002132 return ipmi::responseReqDataLenInvalid();
James Feistfaa4f222019-03-21 16:21:55 -07002133 }
2134
James Feist09f6b602019-08-08 11:30:03 -07002135 std::string pathName;
2136
2137 if (*param == legacyExitAirSensorNumber)
2138 {
2139 pathName = exitAirPathName;
2140 }
2141 else if (*param == legacyPCHSensorNumber)
2142 {
2143 pathName = pchPathName;
2144 }
2145 else
James Feistfaa4f222019-03-21 16:21:55 -07002146 {
James Feistacc8a4e2019-04-02 14:23:57 -07002147 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07002148 }
James Feist09f6b602019-08-08 11:30:03 -07002149
2150 uint8_t setpoint = legacyDefaultSetpoint;
2151 std::string path = getConfigPath(pathName);
James Feistfaa4f222019-03-21 16:21:55 -07002152 if (path.size())
2153 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002154 Value val = ipmi::getDbusProperty(
2155 *dbus, "xyz.openbmc_project.EntityManager", path,
2156 pidConfigurationIface, "SetPoint");
James Feistfaa4f222019-03-21 16:21:55 -07002157 setpoint = std::floor(std::get<double>(val) + 0.5);
2158 }
2159
2160 // old implementation used to return the "default" and current, we
2161 // don't make the default readily available so just make both the
2162 // same
James Feistfaa4f222019-03-21 16:21:55 -07002163
James Feistacc8a4e2019-04-02 14:23:57 -07002164 return ipmi::responseSuccess(
2165 std::array<uint8_t, 2>{setpoint, setpoint});
James Feistfaa4f222019-03-21 16:21:55 -07002166 }
James Feistacc8a4e2019-04-02 14:23:57 -07002167 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
2168 {
2169 constexpr const size_t maxDomainCount = 8;
2170
2171 if (!param)
2172 {
2173 return ipmi::responseReqDataLenInvalid();
2174 }
2175 uint8_t requestedDomain = *param;
2176 if (requestedDomain >= maxDomainCount)
2177 {
2178 return ipmi::responseInvalidFieldRequest();
2179 }
2180
2181 boost::container::flat_map data = getPidConfigs();
2182 if (data.empty())
2183 {
2184 phosphor::logging::log<phosphor::logging::level::ERR>(
2185 "ipmiOEMGetFscParameter: found no pid configurations!");
2186 return ipmi::responseResponseError();
2187 }
2188 size_t count = 0;
2189 for (const auto& [_, pid] : data)
2190 {
2191 auto findClass = pid.find("Class");
2192 if (findClass == pid.end())
2193 {
2194 phosphor::logging::log<phosphor::logging::level::ERR>(
2195 "ipmiOEMGetFscParameter: found illegal pid "
2196 "configurations");
2197 return ipmi::responseResponseError();
2198 }
2199 std::string type = std::get<std::string>(findClass->second);
2200 if (type == "fan")
2201 {
2202 if (requestedDomain == count)
2203 {
2204 auto findOutLimit = pid.find("OutLimitMax");
2205 if (findOutLimit == pid.end())
2206 {
2207 phosphor::logging::log<phosphor::logging::level::ERR>(
2208 "ipmiOEMGetFscParameter: found illegal pid "
2209 "configurations");
2210 return ipmi::responseResponseError();
2211 }
2212
2213 return ipmi::responseSuccess(
2214 static_cast<uint8_t>(std::floor(
2215 std::get<double>(findOutLimit->second) + 0.5)));
2216 }
2217 else
2218 {
2219 count++;
2220 }
2221 }
2222 }
2223
2224 return ipmi::responseInvalidFieldRequest();
2225 }
2226 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07002227 {
2228
2229 /*
2230 DataLen should be 1, but host is sending us an extra bit. As the
James Feistacc8a4e2019-04-02 14:23:57 -07002231 previous behavior didn't seem to prevent this, ignore the check for
2232 now.
James Feist5f957ca2019-03-14 15:33:55 -07002233
James Feistacc8a4e2019-04-02 14:23:57 -07002234 if (param)
James Feist5f957ca2019-03-14 15:33:55 -07002235 {
2236 phosphor::logging::log<phosphor::logging::level::ERR>(
2237 "ipmiOEMGetFscParameter: invalid input len!");
James Feist5f957ca2019-03-14 15:33:55 -07002238 return IPMI_CC_REQ_DATA_LEN_INVALID;
2239 }
2240 */
2241 Value cfmLimit;
2242 Value cfmMaximum;
2243 try
2244 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002245 cfmLimit = ipmi::getDbusProperty(*dbus, settingsBusName,
James Feist5f957ca2019-03-14 15:33:55 -07002246 cfmLimitSettingPath, cfmLimitIface,
2247 "Limit");
2248 cfmMaximum = ipmi::getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002249 *dbus, "xyz.openbmc_project.ExitAirTempSensor",
James Feist5f957ca2019-03-14 15:33:55 -07002250 "/xyz/openbmc_project/control/MaxCFM", cfmLimitIface, "Limit");
2251 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002252 catch (const sdbusplus::exception_t& e)
James Feist5f957ca2019-03-14 15:33:55 -07002253 {
2254 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feistacc8a4e2019-04-02 14:23:57 -07002255 "ipmiOEMGetFscParameter: can't get cfm setting!",
James Feist5f957ca2019-03-14 15:33:55 -07002256 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002257 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002258 }
2259
James Feistacc8a4e2019-04-02 14:23:57 -07002260 double cfmMax = std::get<double>(cfmMaximum);
2261 double cfmLim = std::get<double>(cfmLimit);
James Feist5f957ca2019-03-14 15:33:55 -07002262
James Feistacc8a4e2019-04-02 14:23:57 -07002263 cfmLim = std::floor(cfmLim + 0.5);
2264 cfmMax = std::floor(cfmMax + 0.5);
2265 uint16_t cfmLimResp = static_cast<uint16_t>(cfmLim);
2266 uint16_t cfmMaxResp = static_cast<uint16_t>(cfmMax);
James Feist5f957ca2019-03-14 15:33:55 -07002267
James Feistacc8a4e2019-04-02 14:23:57 -07002268 return ipmi::responseSuccess(
2269 std::array<uint16_t, 2>{cfmLimResp, cfmMaxResp});
James Feist5f957ca2019-03-14 15:33:55 -07002270 }
James Feistacc8a4e2019-04-02 14:23:57 -07002271
James Feist5f957ca2019-03-14 15:33:55 -07002272 else
2273 {
2274 // todo other command parts possibly
James Feist5f957ca2019-03-14 15:33:55 -07002275 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002276 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002277 }
2278}
2279
Cheng C Yang773703a2019-08-15 09:41:11 +08002280using crConfigVariant =
2281 std::variant<bool, uint8_t, uint32_t, std::vector<uint8_t>, std::string>;
2282
2283int setCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2284 const crConfigVariant& value,
2285 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2286{
2287 boost::system::error_code ec;
2288 ctx->bus->yield_method_call<void>(
Kuiying Wange45333a2020-07-22 22:06:37 +08002289 ctx->yield, ec, "xyz.openbmc_project.PSURedundancy",
Cheng C Yang773703a2019-08-15 09:41:11 +08002290 "/xyz/openbmc_project/control/power_supply_redundancy",
2291 "org.freedesktop.DBus.Properties", "Set",
2292 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property, value);
2293 if (ec)
2294 {
2295 phosphor::logging::log<phosphor::logging::level::ERR>(
2296 "Failed to set dbus property to cold redundancy");
2297 return -1;
2298 }
2299
2300 return 0;
2301}
2302
Kuiying Wange45333a2020-07-22 22:06:37 +08002303int getCRConfig(
2304 ipmi::Context::ptr ctx, const std::string& property, crConfigVariant& value,
2305 const std::string& service = "xyz.openbmc_project.PSURedundancy",
2306 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
Cheng C Yang773703a2019-08-15 09:41:11 +08002307{
2308 boost::system::error_code ec;
2309 value = ctx->bus->yield_method_call<crConfigVariant>(
Yong Li19445ab2019-12-20 18:25:29 +08002310 ctx->yield, ec, service,
Cheng C Yang773703a2019-08-15 09:41:11 +08002311 "/xyz/openbmc_project/control/power_supply_redundancy",
2312 "org.freedesktop.DBus.Properties", "Get",
2313 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property);
2314 if (ec)
2315 {
2316 phosphor::logging::log<phosphor::logging::level::ERR>(
2317 "Failed to get dbus property to cold redundancy");
2318 return -1;
2319 }
2320 return 0;
2321}
2322
2323uint8_t getPSUCount(void)
2324{
2325 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2326 ipmi::Value num;
2327 try
2328 {
2329 num = ipmi::getDbusProperty(
2330 *dbus, "xyz.openbmc_project.PSURedundancy",
2331 "/xyz/openbmc_project/control/power_supply_redundancy",
2332 "xyz.openbmc_project.Control.PowerSupplyRedundancy", "PSUNumber");
2333 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002334 catch (const sdbusplus::exception_t& e)
Cheng C Yang773703a2019-08-15 09:41:11 +08002335 {
2336 phosphor::logging::log<phosphor::logging::level::ERR>(
2337 "Failed to get PSUNumber property from dbus interface");
2338 return 0;
2339 }
2340 uint8_t* pNum = std::get_if<uint8_t>(&num);
2341 if (!pNum)
2342 {
2343 phosphor::logging::log<phosphor::logging::level::ERR>(
2344 "Error to get PSU Number");
2345 return 0;
2346 }
2347 return *pNum;
2348}
2349
2350bool validateCRAlgo(std::vector<uint8_t>& conf, uint8_t num)
2351{
2352 if (conf.size() < num)
2353 {
2354 phosphor::logging::log<phosphor::logging::level::ERR>(
2355 "Invalid PSU Ranking");
2356 return false;
2357 }
2358 std::set<uint8_t> confSet;
2359 for (uint8_t i = 0; i < num; i++)
2360 {
2361 if (conf[i] > num)
2362 {
2363 phosphor::logging::log<phosphor::logging::level::ERR>(
2364 "PSU Ranking is larger than current PSU number");
2365 return false;
2366 }
2367 confSet.emplace(conf[i]);
2368 }
2369
2370 if (confSet.size() != num)
2371 {
2372 phosphor::logging::log<phosphor::logging::level::ERR>(
2373 "duplicate PSU Ranking");
2374 return false;
2375 }
2376 return true;
2377}
2378
2379enum class crParameter
2380{
2381 crStatus = 0,
2382 crFeature = 1,
2383 rotationFeature = 2,
2384 rotationAlgo = 3,
2385 rotationPeriod = 4,
Yong Li19445ab2019-12-20 18:25:29 +08002386 numOfPSU = 5,
2387 rotationRankOrderEffective = 6
Cheng C Yang773703a2019-08-15 09:41:11 +08002388};
2389
2390constexpr ipmi::Cc ccParameterNotSupported = 0x80;
2391static const constexpr uint32_t oneDay = 0x15180;
2392static const constexpr uint32_t oneMonth = 0xf53700;
2393static const constexpr uint8_t userSpecific = 0x01;
2394static const constexpr uint8_t crSetCompleted = 0;
2395ipmi::RspType<uint8_t> ipmiOEMSetCRConfig(ipmi::Context::ptr ctx,
2396 uint8_t parameter,
2397 ipmi::message::Payload& payload)
2398{
2399 switch (static_cast<crParameter>(parameter))
2400 {
Cheng C Yang773703a2019-08-15 09:41:11 +08002401 case crParameter::rotationFeature:
2402 {
2403 uint8_t param1;
2404 if (payload.unpack(param1) || !payload.fullyUnpacked())
2405 {
2406 return ipmi::responseReqDataLenInvalid();
2407 }
2408 // Rotation Enable can only be true or false
2409 if (param1 > 1)
2410 {
2411 return ipmi::responseInvalidFieldRequest();
2412 }
2413 if (setCRConfig(ctx, "RotationEnabled", static_cast<bool>(param1)))
2414 {
2415 return ipmi::responseResponseError();
2416 }
2417 break;
2418 }
2419 case crParameter::rotationAlgo:
2420 {
2421 // Rotation Algorithm can only be 0-BMC Specific or 1-User Specific
2422 std::string algoName;
2423 uint8_t param1;
2424 if (payload.unpack(param1))
2425 {
2426 return ipmi::responseReqDataLenInvalid();
2427 }
2428 switch (param1)
2429 {
2430 case 0:
2431 algoName = "xyz.openbmc_project.Control."
2432 "PowerSupplyRedundancy.Algo.bmcSpecific";
2433 break;
2434 case 1:
2435 algoName = "xyz.openbmc_project.Control."
2436 "PowerSupplyRedundancy.Algo.userSpecific";
2437 break;
2438 default:
2439 return ipmi::responseInvalidFieldRequest();
2440 }
2441 if (setCRConfig(ctx, "RotationAlgorithm", algoName))
2442 {
2443 return ipmi::responseResponseError();
2444 }
2445
2446 uint8_t numberOfPSU = getPSUCount();
2447 if (!numberOfPSU)
2448 {
2449 return ipmi::responseResponseError();
2450 }
2451 std::vector<uint8_t> rankOrder;
2452
2453 if (param1 == userSpecific)
2454 {
2455 if (payload.unpack(rankOrder) || !payload.fullyUnpacked())
2456 {
2457 ipmi::responseReqDataLenInvalid();
2458 }
Yong Li83315132019-10-23 17:42:24 +08002459 if (rankOrder.size() != numberOfPSU)
Cheng C Yang773703a2019-08-15 09:41:11 +08002460 {
2461 return ipmi::responseReqDataLenInvalid();
2462 }
2463
2464 if (!validateCRAlgo(rankOrder, numberOfPSU))
2465 {
2466 return ipmi::responseInvalidFieldRequest();
2467 }
2468 }
2469 else
2470 {
2471 if (rankOrder.size() > 0)
2472 {
2473 return ipmi::responseReqDataLenInvalid();
2474 }
2475 for (uint8_t i = 1; i <= numberOfPSU; i++)
2476 {
2477 rankOrder.emplace_back(i);
2478 }
2479 }
2480 if (setCRConfig(ctx, "RotationRankOrder", rankOrder))
2481 {
2482 return ipmi::responseResponseError();
2483 }
2484 break;
2485 }
2486 case crParameter::rotationPeriod:
2487 {
2488 // Minimum Rotation period is One day (86400 seconds) and Max
2489 // Rotation Period is 6 month (0xf53700 seconds)
2490 uint32_t period;
2491 if (payload.unpack(period) || !payload.fullyUnpacked())
2492 {
2493 return ipmi::responseReqDataLenInvalid();
2494 }
2495 if ((period < oneDay) || (period > oneMonth))
2496 {
2497 return ipmi::responseInvalidFieldRequest();
2498 }
2499 if (setCRConfig(ctx, "PeriodOfRotation", period))
2500 {
2501 return ipmi::responseResponseError();
2502 }
2503 break;
2504 }
2505 default:
2506 {
2507 return ipmi::response(ccParameterNotSupported);
2508 }
2509 }
2510
Cheng C Yang773703a2019-08-15 09:41:11 +08002511 return ipmi::responseSuccess(crSetCompleted);
2512}
2513
Yong Li83315132019-10-23 17:42:24 +08002514ipmi::RspType<uint8_t, std::variant<uint8_t, uint32_t, std::vector<uint8_t>>>
Cheng C Yang773703a2019-08-15 09:41:11 +08002515 ipmiOEMGetCRConfig(ipmi::Context::ptr ctx, uint8_t parameter)
2516{
2517 crConfigVariant value;
2518 switch (static_cast<crParameter>(parameter))
2519 {
2520 case crParameter::crStatus:
2521 {
2522 if (getCRConfig(ctx, "ColdRedundancyStatus", value))
2523 {
2524 return ipmi::responseResponseError();
2525 }
2526 std::string* pStatus = std::get_if<std::string>(&value);
2527 if (!pStatus)
2528 {
2529 phosphor::logging::log<phosphor::logging::level::ERR>(
2530 "Error to get ColdRedundancyStatus property");
2531 return ipmi::responseResponseError();
2532 }
2533 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2534 auto status =
2535 server::PowerSupplyRedundancy::convertStatusFromString(
2536 *pStatus);
2537 switch (status)
2538 {
2539 case server::PowerSupplyRedundancy::Status::inProgress:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002540 return ipmi::responseSuccess(parameter,
Kuiying Wange45333a2020-07-22 22:06:37 +08002541 static_cast<uint8_t>(1));
Cheng C Yang773703a2019-08-15 09:41:11 +08002542
2543 case server::PowerSupplyRedundancy::Status::completed:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002544 return ipmi::responseSuccess(parameter,
Kuiying Wange45333a2020-07-22 22:06:37 +08002545 static_cast<uint8_t>(0));
Cheng C Yang773703a2019-08-15 09:41:11 +08002546 default:
2547 phosphor::logging::log<phosphor::logging::level::ERR>(
2548 "Error to get valid status");
2549 return ipmi::responseResponseError();
2550 }
2551 }
2552 case crParameter::crFeature:
2553 {
Kuiying Wange45333a2020-07-22 22:06:37 +08002554 if (getCRConfig(ctx, "PowerSupplyRedundancyEnabled", value))
Cheng C Yang773703a2019-08-15 09:41:11 +08002555 {
2556 return ipmi::responseResponseError();
2557 }
2558 bool* pResponse = std::get_if<bool>(&value);
2559 if (!pResponse)
2560 {
2561 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wange45333a2020-07-22 22:06:37 +08002562 "Error to get PowerSupplyRedundancyEnabled property");
Cheng C Yang773703a2019-08-15 09:41:11 +08002563 return ipmi::responseResponseError();
2564 }
2565
Cheng C Yangf41e3342019-09-10 04:47:23 +08002566 return ipmi::responseSuccess(parameter,
2567 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002568 }
2569 case crParameter::rotationFeature:
2570 {
2571 if (getCRConfig(ctx, "RotationEnabled", value))
2572 {
2573 return ipmi::responseResponseError();
2574 }
2575 bool* pResponse = std::get_if<bool>(&value);
2576 if (!pResponse)
2577 {
2578 phosphor::logging::log<phosphor::logging::level::ERR>(
2579 "Error to get RotationEnabled property");
2580 return ipmi::responseResponseError();
2581 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002582 return ipmi::responseSuccess(parameter,
2583 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002584 }
2585 case crParameter::rotationAlgo:
2586 {
2587 if (getCRConfig(ctx, "RotationAlgorithm", value))
2588 {
2589 return ipmi::responseResponseError();
2590 }
2591
2592 std::string* pAlgo = std::get_if<std::string>(&value);
2593 if (!pAlgo)
2594 {
2595 phosphor::logging::log<phosphor::logging::level::ERR>(
2596 "Error to get RotationAlgorithm property");
2597 return ipmi::responseResponseError();
2598 }
Yong Li83315132019-10-23 17:42:24 +08002599 std::vector<uint8_t> response;
Cheng C Yang773703a2019-08-15 09:41:11 +08002600 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2601 auto algo =
2602 server::PowerSupplyRedundancy::convertAlgoFromString(*pAlgo);
Yong Li83315132019-10-23 17:42:24 +08002603
Cheng C Yang773703a2019-08-15 09:41:11 +08002604 switch (algo)
2605 {
2606 case server::PowerSupplyRedundancy::Algo::bmcSpecific:
Yong Li83315132019-10-23 17:42:24 +08002607 response.push_back(0);
Cheng C Yang773703a2019-08-15 09:41:11 +08002608 break;
2609 case server::PowerSupplyRedundancy::Algo::userSpecific:
Yong Li83315132019-10-23 17:42:24 +08002610 response.push_back(1);
Cheng C Yang773703a2019-08-15 09:41:11 +08002611 break;
2612 default:
2613 phosphor::logging::log<phosphor::logging::level::ERR>(
2614 "Error to get valid algo");
2615 return ipmi::responseResponseError();
2616 }
2617
2618 if (getCRConfig(ctx, "RotationRankOrder", value))
2619 {
2620 return ipmi::responseResponseError();
2621 }
2622 std::vector<uint8_t>* pResponse =
2623 std::get_if<std::vector<uint8_t>>(&value);
2624 if (!pResponse)
2625 {
2626 phosphor::logging::log<phosphor::logging::level::ERR>(
2627 "Error to get RotationRankOrder property");
2628 return ipmi::responseResponseError();
2629 }
Yong Li83315132019-10-23 17:42:24 +08002630
Cheng C Yang773703a2019-08-15 09:41:11 +08002631 std::copy(pResponse->begin(), pResponse->end(),
Yong Li83315132019-10-23 17:42:24 +08002632 std::back_inserter(response));
2633
Cheng C Yangf41e3342019-09-10 04:47:23 +08002634 return ipmi::responseSuccess(parameter, response);
Cheng C Yang773703a2019-08-15 09:41:11 +08002635 }
2636 case crParameter::rotationPeriod:
2637 {
2638 if (getCRConfig(ctx, "PeriodOfRotation", value))
2639 {
2640 return ipmi::responseResponseError();
2641 }
2642 uint32_t* pResponse = std::get_if<uint32_t>(&value);
2643 if (!pResponse)
2644 {
2645 phosphor::logging::log<phosphor::logging::level::ERR>(
2646 "Error to get RotationAlgorithm property");
2647 return ipmi::responseResponseError();
2648 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002649 return ipmi::responseSuccess(parameter, *pResponse);
Cheng C Yang773703a2019-08-15 09:41:11 +08002650 }
2651 case crParameter::numOfPSU:
2652 {
2653 uint8_t numberOfPSU = getPSUCount();
2654 if (!numberOfPSU)
2655 {
2656 return ipmi::responseResponseError();
2657 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002658 return ipmi::responseSuccess(parameter, numberOfPSU);
Cheng C Yang773703a2019-08-15 09:41:11 +08002659 }
Yong Li19445ab2019-12-20 18:25:29 +08002660 case crParameter::rotationRankOrderEffective:
2661 {
2662 if (getCRConfig(ctx, "RotationRankOrder", value,
2663 "xyz.openbmc_project.PSURedundancy"))
2664 {
2665 return ipmi::responseResponseError();
2666 }
2667 std::vector<uint8_t>* pResponse =
2668 std::get_if<std::vector<uint8_t>>(&value);
2669 if (!pResponse)
2670 {
2671 phosphor::logging::log<phosphor::logging::level::ERR>(
2672 "Error to get effective RotationRankOrder property");
2673 return ipmi::responseResponseError();
2674 }
2675 return ipmi::responseSuccess(parameter, *pResponse);
2676 }
Cheng C Yang773703a2019-08-15 09:41:11 +08002677 default:
2678 {
2679 return ipmi::response(ccParameterNotSupported);
2680 }
2681 }
2682}
2683
Zhu, Yungebe560b02019-04-21 21:19:21 -04002684ipmi::RspType<> ipmiOEMSetFaultIndication(uint8_t sourceId, uint8_t faultType,
2685 uint8_t faultState,
2686 uint8_t faultGroup,
2687 std::array<uint8_t, 8>& ledStateData)
2688{
Zhu, Yungebe560b02019-04-21 21:19:21 -04002689 constexpr auto maxFaultType = static_cast<size_t>(RemoteFaultType::max);
2690 static const std::array<std::string, maxFaultType> faultNames = {
2691 "faultFan", "faultTemp", "faultPower",
2692 "faultDriveSlot", "faultSoftware", "faultMemory"};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002693
2694 constexpr uint8_t maxFaultSource = 0x4;
2695 constexpr uint8_t skipLEDs = 0xFF;
2696 constexpr uint8_t pinSize = 64;
2697 constexpr uint8_t groupSize = 16;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002698 constexpr uint8_t groupNum = 5; // 4 for fault memory, 1 for faultFan
Zhu, Yungebe560b02019-04-21 21:19:21 -04002699
Zhikui Rence4e73f2019-12-06 13:59:47 -08002700 // same pin names need to be defined in dts file
2701 static const std::array<std::array<std::string, groupSize>, groupNum>
2702 faultLedPinNames = {{
2703 "LED_CPU1_CH1_DIMM1_FAULT",
2704 "LED_CPU1_CH1_DIMM2_FAULT",
2705 "LED_CPU1_CH2_DIMM1_FAULT",
2706 "LED_CPU1_CH2_DIMM2_FAULT",
2707 "LED_CPU1_CH3_DIMM1_FAULT",
2708 "LED_CPU1_CH3_DIMM2_FAULT",
2709 "LED_CPU1_CH4_DIMM1_FAULT",
2710 "LED_CPU1_CH4_DIMM2_FAULT",
2711 "LED_CPU1_CH5_DIMM1_FAULT",
2712 "LED_CPU1_CH5_DIMM2_FAULT",
2713 "LED_CPU1_CH6_DIMM1_FAULT",
2714 "LED_CPU1_CH6_DIMM2_FAULT",
2715 "",
2716 "",
2717 "",
2718 "", // end of group1
2719 "LED_CPU2_CH1_DIMM1_FAULT",
2720 "LED_CPU2_CH1_DIMM2_FAULT",
2721 "LED_CPU2_CH2_DIMM1_FAULT",
2722 "LED_CPU2_CH2_DIMM2_FAULT",
2723 "LED_CPU2_CH3_DIMM1_FAULT",
2724 "LED_CPU2_CH3_DIMM2_FAULT",
2725 "LED_CPU2_CH4_DIMM1_FAULT",
2726 "LED_CPU2_CH4_DIMM2_FAULT",
2727 "LED_CPU2_CH5_DIMM1_FAULT",
2728 "LED_CPU2_CH5_DIMM2_FAULT",
2729 "LED_CPU2_CH6_DIMM1_FAULT",
2730 "LED_CPU2_CH6_DIMM2_FAULT",
2731 "",
2732 "",
2733 "",
2734 "", // endof group2
2735 "LED_CPU3_CH1_DIMM1_FAULT",
2736 "LED_CPU3_CH1_DIMM2_FAULT",
2737 "LED_CPU3_CH2_DIMM1_FAULT",
2738 "LED_CPU3_CH2_DIMM2_FAULT",
2739 "LED_CPU3_CH3_DIMM1_FAULT",
2740 "LED_CPU3_CH3_DIMM2_FAULT",
2741 "LED_CPU3_CH4_DIMM1_FAULT",
2742 "LED_CPU3_CH4_DIMM2_FAULT",
2743 "LED_CPU3_CH5_DIMM1_FAULT",
2744 "LED_CPU3_CH5_DIMM2_FAULT",
2745 "LED_CPU3_CH6_DIMM1_FAULT",
2746 "LED_CPU3_CH6_DIMM2_FAULT",
2747 "",
2748 "",
2749 "",
2750 "", // end of group3
2751 "LED_CPU4_CH1_DIMM1_FAULT",
2752 "LED_CPU4_CH1_DIMM2_FAULT",
2753 "LED_CPU4_CH2_DIMM1_FAULT",
2754 "LED_CPU4_CH2_DIMM2_FAULT",
2755 "LED_CPU4_CH3_DIMM1_FAULT",
2756 "LED_CPU4_CH3_DIMM2_FAULT",
2757 "LED_CPU4_CH4_DIMM1_FAULT",
2758 "LED_CPU4_CH4_DIMM2_FAULT",
2759 "LED_CPU4_CH5_DIMM1_FAULT",
2760 "LED_CPU4_CH5_DIMM2_FAULT",
2761 "LED_CPU4_CH6_DIMM1_FAULT",
2762 "LED_CPU4_CH6_DIMM2_FAULT",
2763 "",
2764 "",
2765 "",
2766 "", // end of group4
2767 "LED_FAN1_FAULT",
2768 "LED_FAN2_FAULT",
2769 "LED_FAN3_FAULT",
2770 "LED_FAN4_FAULT",
2771 "LED_FAN5_FAULT",
2772 "LED_FAN6_FAULT",
2773 "LED_FAN7_FAULT",
2774 "LED_FAN8_FAULT",
2775 "",
2776 "",
2777 "",
2778 "",
2779 "",
2780 "",
2781 "",
2782 "" // end of group5
2783 }};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002784
Zhikui Rence4e73f2019-12-06 13:59:47 -08002785 // Validate the source, fault type --
2786 // (Byte 1) sourceId: Unspecified, Hot-Swap Controller 0, Hot-Swap
2787 // Controller 1, BIOS (Byte 2) fault type: fan, temperature, power,
2788 // driveslot, software, memory (Byte 3) FaultState: OK, Degraded,
2789 // Non-Critical, Critical, Non-Recoverable, (Byte 4) is faultGroup,
2790 // definition differs based on fault type (Byte 2)
2791 // Type Fan=> Group: 0=FanGroupID, FF-not used
2792 // Byte 5-11 00h, not used
2793 // Byte12 FanLedState [7:0]-Fans 7:0
2794 // Type Memory=> Group: 0 = DIMM GroupID, FF-not used
2795 // Byte 5:12 - DIMM LED state (64bit field, LS Byte first)
2796 // [63:48] = CPU4 channels 7:0, 2 bits per channel
2797 // [47:32] = CPU3 channels 7:0, 2 bits per channel
2798 // [31:16] = CPU2 channels 7:0, 2 bits per channel
2799 // [15:0] = CPU1 channels 7:0, 2 bits per channel
2800 // Type Other=> Component Fault LED Group ID, not used set to 0xFF
2801 // Byte[5:12]: reserved 0x00h
Zhu, Yungebe560b02019-04-21 21:19:21 -04002802 if ((sourceId >= maxFaultSource) ||
2803 (faultType >= static_cast<int8_t>(RemoteFaultType::max)) ||
2804 (faultState >= static_cast<int8_t>(RemoteFaultState::maxFaultState)) ||
2805 (faultGroup >= static_cast<int8_t>(DimmFaultType::maxFaultGroup)))
2806 {
2807 return ipmi::responseParmOutOfRange();
2808 }
2809
Zhikui Rence4e73f2019-12-06 13:59:47 -08002810 size_t pinGroupOffset = 0;
2811 size_t pinGroupMax = pinSize / groupSize;
2812 if (RemoteFaultType::fan == RemoteFaultType(faultType))
Zhu, Yungebe560b02019-04-21 21:19:21 -04002813 {
Zhikui Rence4e73f2019-12-06 13:59:47 -08002814 pinGroupOffset = 4;
2815 pinGroupMax = groupNum - pinSize / groupSize;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002816 }
2817
2818 switch (RemoteFaultType(faultType))
2819 {
2820 case (RemoteFaultType::fan):
2821 case (RemoteFaultType::memory):
2822 {
2823 if (faultGroup == skipLEDs)
2824 {
2825 return ipmi::responseSuccess();
2826 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002827 // calculate led state bit filed count, each byte has 8bits
2828 // the maximum bits will be 8 * 8 bits
2829 constexpr uint8_t size = sizeof(ledStateData) * 8;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002830
2831 // assemble ledState
2832 uint64_t ledState = 0;
2833 bool hasError = false;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002834 for (int i = 0; i < sizeof(ledStateData); i++)
2835 {
2836 ledState = (uint64_t)(ledState << 8);
2837 ledState = (uint64_t)(ledState | (uint64_t)ledStateData[i]);
2838 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002839 std::bitset<size> ledStateBits(ledState);
Zhu, Yungebe560b02019-04-21 21:19:21 -04002840
Zhikui Rence4e73f2019-12-06 13:59:47 -08002841 for (int group = 0; group < pinGroupMax; group++)
2842 {
2843 for (int i = 0; i < groupSize; i++)
2844 { // skip non-existing pins
2845 if (0 == faultLedPinNames[group + pinGroupOffset][i].size())
2846 {
2847 continue;
2848 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002849
Zhikui Rence4e73f2019-12-06 13:59:47 -08002850 gpiod::line line = gpiod::find_line(
2851 faultLedPinNames[group + pinGroupOffset][i]);
2852 if (!line)
2853 {
2854 phosphor::logging::log<phosphor::logging::level::ERR>(
2855 "Not Find Led Gpio Device!",
2856 phosphor::logging::entry(
2857 "DEVICE=%s",
2858 faultLedPinNames[group + pinGroupOffset][i]
2859 .c_str()));
2860 hasError = true;
2861 continue;
2862 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002863
Zhikui Rence4e73f2019-12-06 13:59:47 -08002864 bool activeHigh =
2865 (line.active_state() == gpiod::line::ACTIVE_HIGH);
2866 try
2867 {
2868 line.request(
2869 {"faultLed", gpiod::line_request::DIRECTION_OUTPUT,
2870 activeHigh
2871 ? 0
2872 : gpiod::line_request::FLAG_ACTIVE_LOW});
2873 line.set_value(ledStateBits[i + group * groupSize]);
2874 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002875 catch (const std::system_error&)
Zhikui Rence4e73f2019-12-06 13:59:47 -08002876 {
2877 phosphor::logging::log<phosphor::logging::level::ERR>(
2878 "Error write Led Gpio Device!",
2879 phosphor::logging::entry(
2880 "DEVICE=%s",
2881 faultLedPinNames[group + pinGroupOffset][i]
2882 .c_str()));
2883 hasError = true;
2884 continue;
2885 }
2886 } // for int i
2887 }
2888 if (hasError)
2889 {
2890 return ipmi::responseResponseError();
Zhu, Yungebe560b02019-04-21 21:19:21 -04002891 }
2892 break;
2893 }
2894 default:
2895 {
2896 // now only support two fault types
2897 return ipmi::responseParmOutOfRange();
2898 }
Zhikui Rence4e73f2019-12-06 13:59:47 -08002899 } // switch
Zhu, Yungebe560b02019-04-21 21:19:21 -04002900 return ipmi::responseSuccess();
2901}
2902
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302903ipmi::RspType<uint8_t> ipmiOEMReadBoardProductId()
2904{
2905 uint8_t prodId = 0;
2906 try
2907 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002908 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302909 const DbusObjectInfo& object = getDbusObject(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002910 *dbus, "xyz.openbmc_project.Inventory.Item.Board",
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302911 "/xyz/openbmc_project/inventory/system/board/", "Baseboard");
2912 const Value& propValue = getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002913 *dbus, object.second, object.first,
Suryakanth Sekar6c57e5c2020-01-10 17:11:58 +05302914 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
2915 "ProductId");
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302916 prodId = static_cast<uint8_t>(std::get<uint64_t>(propValue));
2917 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002918 catch (const std::exception& e)
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302919 {
2920 phosphor::logging::log<phosphor::logging::level::ERR>(
2921 "ipmiOEMReadBoardProductId: Product ID read failed!",
2922 phosphor::logging::entry("ERR=%s", e.what()));
2923 }
2924 return ipmi::responseSuccess(prodId);
2925}
2926
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302927/** @brief implements the get security mode command
2928 * @param ctx - ctx pointer
2929 *
2930 * @returns IPMI completion code with following data
2931 * - restriction mode value - As specified in
2932 * xyz.openbmc_project.Control.Security.RestrictionMode.interface.yaml
2933 * - special mode value - As specified in
2934 * xyz.openbmc_project.Control.Security.SpecialMode.interface.yaml
2935 */
2936ipmi::RspType<uint8_t, uint8_t> ipmiGetSecurityMode(ipmi::Context::ptr ctx)
2937{
2938 namespace securityNameSpace =
2939 sdbusplus::xyz::openbmc_project::Control::Security::server;
2940 uint8_t restrictionModeValue = 0;
2941 uint8_t specialModeValue = 0;
2942
2943 boost::system::error_code ec;
2944 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002945 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302946 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2947 restricionModeProperty);
2948 if (ec)
2949 {
2950 phosphor::logging::log<phosphor::logging::level::ERR>(
2951 "ipmiGetSecurityMode: failed to get RestrictionMode property",
2952 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2953 return ipmi::responseUnspecifiedError();
2954 }
2955 restrictionModeValue = static_cast<uint8_t>(
2956 securityNameSpace::RestrictionMode::convertModesFromString(
2957 std::get<std::string>(varRestrMode)));
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302958 auto varSpecialMode =
2959 ctx->bus->yield_method_call<std::variant<std::string>>(
2960 ctx->yield, ec, specialModeService, specialModeBasePath,
2961 dBusPropertyIntf, dBusPropertyGetMethod, specialModeIntf,
2962 specialModeProperty);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302963 if (ec)
2964 {
2965 phosphor::logging::log<phosphor::logging::level::ERR>(
2966 "ipmiGetSecurityMode: failed to get SpecialMode property",
2967 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2968 // fall through, let us not worry about SpecialMode property, which is
2969 // not required in user scenario
2970 }
2971 else
2972 {
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302973 specialModeValue = static_cast<uint8_t>(
2974 securityNameSpace::SpecialMode::convertModesFromString(
2975 std::get<std::string>(varSpecialMode)));
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302976 }
2977 return ipmi::responseSuccess(restrictionModeValue, specialModeValue);
2978}
2979
2980/** @brief implements the set security mode command
2981 * Command allows to upgrade the restriction mode and won't allow
2982 * to downgrade from system interface
2983 * @param ctx - ctx pointer
2984 * @param restrictionMode - restriction mode value to be set.
2985 *
2986 * @returns IPMI completion code
2987 */
2988ipmi::RspType<> ipmiSetSecurityMode(ipmi::Context::ptr ctx,
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302989 uint8_t restrictionMode,
2990 std::optional<uint8_t> specialMode)
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302991{
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302992#ifndef BMC_VALIDATION_UNSECURE_FEATURE
2993 if (specialMode)
2994 {
2995 return ipmi::responseReqDataLenInvalid();
2996 }
2997#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302998 namespace securityNameSpace =
2999 sdbusplus::xyz::openbmc_project::Control::Security::server;
3000
3001 ChannelInfo chInfo;
3002 if (getChannelInfo(ctx->channel, chInfo) != ccSuccess)
3003 {
3004 phosphor::logging::log<phosphor::logging::level::ERR>(
3005 "ipmiSetSecurityMode: Failed to get Channel Info",
3006 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
3007 return ipmi::responseUnspecifiedError();
3008 }
3009 auto reqMode =
3010 static_cast<securityNameSpace::RestrictionMode::Modes>(restrictionMode);
3011
3012 if ((reqMode < securityNameSpace::RestrictionMode::Modes::Provisioning) ||
3013 (reqMode >
3014 securityNameSpace::RestrictionMode::Modes::ProvisionedHostDisabled))
3015 {
3016 return ipmi::responseInvalidFieldRequest();
3017 }
3018
3019 boost::system::error_code ec;
3020 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07003021 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303022 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
3023 restricionModeProperty);
3024 if (ec)
3025 {
3026 phosphor::logging::log<phosphor::logging::level::ERR>(
3027 "ipmiSetSecurityMode: failed to get RestrictionMode property",
3028 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
3029 return ipmi::responseUnspecifiedError();
3030 }
3031 auto currentRestrictionMode =
3032 securityNameSpace::RestrictionMode::convertModesFromString(
3033 std::get<std::string>(varRestrMode));
3034
3035 if (chInfo.mediumType !=
3036 static_cast<uint8_t>(EChannelMediumType::lan8032) &&
3037 currentRestrictionMode > reqMode)
3038 {
3039 phosphor::logging::log<phosphor::logging::level::ERR>(
3040 "ipmiSetSecurityMode - Downgrading security mode not supported "
3041 "through system interface",
3042 phosphor::logging::entry(
3043 "CUR_MODE=%d", static_cast<uint8_t>(currentRestrictionMode)),
3044 phosphor::logging::entry("REQ_MODE=%d", restrictionMode));
3045 return ipmi::responseCommandNotAvailable();
3046 }
3047
3048 ec.clear();
3049 ctx->bus->yield_method_call<>(
James Feist28c72902019-09-16 10:34:07 -07003050 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303051 dBusPropertyIntf, dBusPropertySetMethod, restricionModeIntf,
3052 restricionModeProperty,
3053 static_cast<std::variant<std::string>>(
3054 securityNameSpace::convertForMessage(reqMode)));
3055
3056 if (ec)
3057 {
3058 phosphor::logging::log<phosphor::logging::level::ERR>(
3059 "ipmiSetSecurityMode: failed to set RestrictionMode property",
3060 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
3061 return ipmi::responseUnspecifiedError();
3062 }
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05303063
3064#ifdef BMC_VALIDATION_UNSECURE_FEATURE
3065 if (specialMode)
3066 {
Jayaprakash Mutyalad77489f2020-09-05 01:00:04 +00003067 constexpr uint8_t mfgMode = 0x01;
3068 // Manufacturing mode is reserved. So can't enable this mode.
3069 if (specialMode.value() == mfgMode)
3070 {
3071 phosphor::logging::log<phosphor::logging::level::INFO>(
3072 "ipmiSetSecurityMode: Can't enable Manufacturing mode");
3073 return ipmi::responseInvalidFieldRequest();
3074 }
3075
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05303076 ec.clear();
3077 ctx->bus->yield_method_call<>(
3078 ctx->yield, ec, specialModeService, specialModeBasePath,
3079 dBusPropertyIntf, dBusPropertySetMethod, specialModeIntf,
3080 specialModeProperty,
3081 static_cast<std::variant<std::string>>(
3082 securityNameSpace::convertForMessage(
3083 static_cast<securityNameSpace::SpecialMode::Modes>(
3084 specialMode.value()))));
3085
3086 if (ec)
3087 {
3088 phosphor::logging::log<phosphor::logging::level::ERR>(
3089 "ipmiSetSecurityMode: failed to set SpecialMode property",
3090 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
3091 return ipmi::responseUnspecifiedError();
3092 }
3093 }
3094#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303095 return ipmi::responseSuccess();
3096}
3097
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003098ipmi::RspType<uint8_t /* restore status */>
3099 ipmiRestoreConfiguration(const std::array<uint8_t, 3>& clr, uint8_t cmd)
3100{
3101 static constexpr std::array<uint8_t, 3> expClr = {'C', 'L', 'R'};
3102
3103 if (clr != expClr)
3104 {
3105 return ipmi::responseInvalidFieldRequest();
3106 }
3107 constexpr uint8_t cmdStatus = 0;
3108 constexpr uint8_t cmdDefaultRestore = 0xaa;
3109 constexpr uint8_t cmdFullRestore = 0xbb;
3110 constexpr uint8_t cmdFormat = 0xcc;
3111
3112 constexpr const char* restoreOpFname = "/tmp/.rwfs/.restore_op";
3113
3114 switch (cmd)
3115 {
3116 case cmdStatus:
3117 break;
3118 case cmdDefaultRestore:
3119 case cmdFullRestore:
3120 case cmdFormat:
3121 {
3122 // write file to rwfs root
3123 int value = (cmd - 1) & 0x03; // map aa, bb, cc => 1, 2, 3
3124 std::ofstream restoreFile(restoreOpFname);
3125 if (!restoreFile)
3126 {
3127 return ipmi::responseUnspecifiedError();
3128 }
3129 restoreFile << value << "\n";
Arun P. Mohananba1fbc82021-04-26 11:26:53 +05303130
3131 phosphor::logging::log<phosphor::logging::level::WARNING>(
3132 "Restore to default will be performed on next BMC boot",
3133 phosphor::logging::entry("ACTION=0x%0X", cmd));
3134
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003135 break;
3136 }
3137 default:
3138 return ipmi::responseInvalidFieldRequest();
3139 }
3140
3141 constexpr uint8_t restorePending = 0;
3142 constexpr uint8_t restoreComplete = 1;
3143
3144 uint8_t restoreStatus = std::filesystem::exists(restoreOpFname)
3145 ? restorePending
3146 : restoreComplete;
3147 return ipmi::responseSuccess(restoreStatus);
3148}
3149
Chen Yugang39736d52019-07-12 16:24:33 +08003150ipmi::RspType<uint8_t> ipmiOEMGetNmiSource(void)
3151{
3152 uint8_t bmcSource;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003153 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08003154
3155 try
3156 {
3157 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3158 std::string service =
3159 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
3160 Value variant =
3161 getDbusProperty(*dbus, service, oemNmiSourceObjPath,
3162 oemNmiSourceIntf, oemNmiBmcSourceObjPathProp);
3163
3164 switch (nmi::NMISource::convertBMCSourceSignalFromString(
3165 std::get<std::string>(variant)))
3166 {
3167 case nmi::NMISource::BMCSourceSignal::None:
3168 bmcSource = static_cast<uint8_t>(NmiSource::none);
3169 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003170 case nmi::NMISource::BMCSourceSignal::FrontPanelButton:
3171 bmcSource = static_cast<uint8_t>(NmiSource::frontPanelButton);
Chen Yugang39736d52019-07-12 16:24:33 +08003172 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003173 case nmi::NMISource::BMCSourceSignal::Watchdog:
3174 bmcSource = static_cast<uint8_t>(NmiSource::watchdog);
Chen Yugang39736d52019-07-12 16:24:33 +08003175 break;
3176 case nmi::NMISource::BMCSourceSignal::ChassisCmd:
3177 bmcSource = static_cast<uint8_t>(NmiSource::chassisCmd);
3178 break;
3179 case nmi::NMISource::BMCSourceSignal::MemoryError:
3180 bmcSource = static_cast<uint8_t>(NmiSource::memoryError);
3181 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003182 case nmi::NMISource::BMCSourceSignal::PciBusError:
3183 bmcSource = static_cast<uint8_t>(NmiSource::pciBusError);
Chen Yugang39736d52019-07-12 16:24:33 +08003184 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003185 case nmi::NMISource::BMCSourceSignal::PCH:
3186 bmcSource = static_cast<uint8_t>(NmiSource::pch);
Chen Yugang39736d52019-07-12 16:24:33 +08003187 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003188 case nmi::NMISource::BMCSourceSignal::Chipset:
3189 bmcSource = static_cast<uint8_t>(NmiSource::chipset);
Chen Yugang39736d52019-07-12 16:24:33 +08003190 break;
3191 default:
3192 phosphor::logging::log<phosphor::logging::level::ERR>(
3193 "NMI source: invalid property!",
3194 phosphor::logging::entry(
3195 "PROP=%s", std::get<std::string>(variant).c_str()));
3196 return ipmi::responseResponseError();
3197 }
3198 }
Patrick Williamsf944d2e2022-07-22 19:26:52 -05003199 catch (const sdbusplus::exception_t& e)
Chen Yugang39736d52019-07-12 16:24:33 +08003200 {
3201 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3202 return ipmi::responseResponseError();
3203 }
3204
3205 return ipmi::responseSuccess(bmcSource);
3206}
3207
3208ipmi::RspType<> ipmiOEMSetNmiSource(uint8_t sourceId)
3209{
Chen Yugang97cf96e2019-11-01 08:55:11 +08003210 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08003211
3212 nmi::NMISource::BMCSourceSignal bmcSourceSignal =
3213 nmi::NMISource::BMCSourceSignal::None;
3214
3215 switch (NmiSource(sourceId))
3216 {
3217 case NmiSource::none:
3218 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::None;
3219 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003220 case NmiSource::frontPanelButton:
3221 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::FrontPanelButton;
Chen Yugang39736d52019-07-12 16:24:33 +08003222 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003223 case NmiSource::watchdog:
3224 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Watchdog;
Chen Yugang39736d52019-07-12 16:24:33 +08003225 break;
3226 case NmiSource::chassisCmd:
3227 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::ChassisCmd;
3228 break;
3229 case NmiSource::memoryError:
3230 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::MemoryError;
3231 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003232 case NmiSource::pciBusError:
3233 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PciBusError;
Chen Yugang39736d52019-07-12 16:24:33 +08003234 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003235 case NmiSource::pch:
3236 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PCH;
Chen Yugang39736d52019-07-12 16:24:33 +08003237 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003238 case NmiSource::chipset:
3239 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Chipset;
Chen Yugang39736d52019-07-12 16:24:33 +08003240 break;
3241 default:
3242 phosphor::logging::log<phosphor::logging::level::ERR>(
3243 "NMI source: invalid property!");
3244 return ipmi::responseResponseError();
3245 }
3246
3247 try
3248 {
3249 // keep NMI signal source
3250 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3251 std::string service =
3252 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
Chen Yugang97cf96e2019-11-01 08:55:11 +08003253 setDbusProperty(*dbus, service, oemNmiSourceObjPath, oemNmiSourceIntf,
3254 oemNmiBmcSourceObjPathProp,
3255 nmi::convertForMessage(bmcSourceSignal));
Chen Yugang99be6332019-08-09 16:20:48 +08003256 // set Enabled property to inform NMI source handling
3257 // to trigger a NMI_OUT BSOD.
3258 // if it's triggered by NMI source property changed,
3259 // NMI_OUT BSOD could be missed if the same source occurs twice in a row
3260 if (bmcSourceSignal != nmi::NMISource::BMCSourceSignal::None)
3261 {
3262 setDbusProperty(*dbus, service, oemNmiSourceObjPath,
3263 oemNmiSourceIntf, oemNmiEnabledObjPathProp,
3264 static_cast<bool>(true));
3265 }
Chen Yugang39736d52019-07-12 16:24:33 +08003266 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003267 catch (const sdbusplus::exception_t& e)
Chen Yugang39736d52019-07-12 16:24:33 +08003268 {
3269 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3270 return ipmi::responseResponseError();
3271 }
3272
3273 return ipmi::responseSuccess();
3274}
3275
James Feist63efafa2019-07-24 12:39:21 -07003276namespace dimmOffset
3277{
3278constexpr const char* dimmPower = "DimmPower";
3279constexpr const char* staticCltt = "StaticCltt";
3280constexpr const char* offsetPath = "/xyz/openbmc_project/Inventory/Item/Dimm";
3281constexpr const char* offsetInterface =
3282 "xyz.openbmc_project.Inventory.Item.Dimm.Offset";
3283constexpr const char* property = "DimmOffset";
3284
3285}; // namespace dimmOffset
3286
3287ipmi::RspType<>
3288 ipmiOEMSetDimmOffset(uint8_t type,
3289 const std::vector<std::tuple<uint8_t, uint8_t>>& data)
3290{
3291 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3292 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3293 {
3294 return ipmi::responseInvalidFieldRequest();
3295 }
3296
3297 if (data.empty())
3298 {
3299 return ipmi::responseInvalidFieldRequest();
3300 }
3301 nlohmann::json json;
3302
3303 std::ifstream jsonStream(dimmOffsetFile);
3304 if (jsonStream.good())
3305 {
3306 json = nlohmann::json::parse(jsonStream, nullptr, false);
3307 if (json.is_discarded())
3308 {
3309 json = nlohmann::json();
3310 }
3311 jsonStream.close();
3312 }
3313
3314 std::string typeName;
3315 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3316 {
3317 typeName = dimmOffset::dimmPower;
3318 }
3319 else
3320 {
3321 typeName = dimmOffset::staticCltt;
3322 }
3323
3324 nlohmann::json& field = json[typeName];
3325
3326 for (const auto& [index, value] : data)
3327 {
3328 field[index] = value;
3329 }
3330
3331 for (nlohmann::json& val : field)
3332 {
3333 if (val == nullptr)
3334 {
3335 val = static_cast<uint8_t>(0);
3336 }
3337 }
3338
3339 std::ofstream output(dimmOffsetFile);
3340 if (!output.good())
3341 {
3342 std::cerr << "Error writing json file\n";
3343 return ipmi::responseResponseError();
3344 }
3345
3346 output << json.dump(4);
3347
3348 if (type == static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3349 {
3350 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
3351
3352 std::variant<std::vector<uint8_t>> offsets =
3353 field.get<std::vector<uint8_t>>();
3354 auto call = bus->new_method_call(
3355 settingsBusName, dimmOffset::offsetPath, PROP_INTF, "Set");
3356 call.append(dimmOffset::offsetInterface, dimmOffset::property, offsets);
3357 try
3358 {
3359 bus->call(call);
3360 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003361 catch (const sdbusplus::exception_t& e)
James Feist63efafa2019-07-24 12:39:21 -07003362 {
3363 phosphor::logging::log<phosphor::logging::level::ERR>(
3364 "ipmiOEMSetDimmOffset: can't set dimm offsets!",
3365 phosphor::logging::entry("ERR=%s", e.what()));
3366 return ipmi::responseResponseError();
3367 }
3368 }
3369
3370 return ipmi::responseSuccess();
3371}
3372
3373ipmi::RspType<uint8_t> ipmiOEMGetDimmOffset(uint8_t type, uint8_t index)
3374{
3375
3376 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3377 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3378 {
3379 return ipmi::responseInvalidFieldRequest();
3380 }
3381
3382 std::ifstream jsonStream(dimmOffsetFile);
3383
3384 auto json = nlohmann::json::parse(jsonStream, nullptr, false);
3385 if (json.is_discarded())
3386 {
3387 std::cerr << "File error in " << dimmOffsetFile << "\n";
3388 return ipmi::responseResponseError();
3389 }
3390
3391 std::string typeName;
3392 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3393 {
3394 typeName = dimmOffset::dimmPower;
3395 }
3396 else
3397 {
3398 typeName = dimmOffset::staticCltt;
3399 }
3400
3401 auto it = json.find(typeName);
3402 if (it == json.end())
3403 {
3404 return ipmi::responseInvalidFieldRequest();
3405 }
3406
3407 if (it->size() <= index)
3408 {
3409 return ipmi::responseInvalidFieldRequest();
3410 }
3411
3412 uint8_t resp = it->at(index).get<uint8_t>();
3413 return ipmi::responseSuccess(resp);
3414}
3415
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003416namespace boot_options
3417{
3418
3419using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
3420using IpmiValue = uint8_t;
3421constexpr auto ipmiDefault = 0;
3422
3423std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
3424 {0x01, Source::Sources::Network},
3425 {0x02, Source::Sources::Disk},
3426 {0x05, Source::Sources::ExternalMedia},
3427 {0x0f, Source::Sources::RemovableMedia},
3428 {ipmiDefault, Source::Sources::Default}};
3429
3430std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003431 {0x06, Mode::Modes::Setup}, {ipmiDefault, Mode::Modes::Regular}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003432
3433std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
3434 {Source::Sources::Network, 0x01},
3435 {Source::Sources::Disk, 0x02},
3436 {Source::Sources::ExternalMedia, 0x05},
3437 {Source::Sources::RemovableMedia, 0x0f},
3438 {Source::Sources::Default, ipmiDefault}};
3439
3440std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003441 {Mode::Modes::Setup, 0x06}, {Mode::Modes::Regular, ipmiDefault}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003442
3443static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
3444static constexpr auto bootSourceIntf =
3445 "xyz.openbmc_project.Control.Boot.Source";
3446static constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
3447static constexpr auto persistentObjPath =
3448 "/xyz/openbmc_project/control/host0/boot";
3449static constexpr auto oneTimePath =
3450 "/xyz/openbmc_project/control/host0/boot/one_time";
3451static constexpr auto bootSourceProp = "BootSource";
3452static constexpr auto bootModeProp = "BootMode";
3453static constexpr auto oneTimeBootEnableProp = "Enabled";
3454static constexpr auto httpBootMode =
3455 "xyz.openbmc_project.Control.Boot.Source.Sources.Http";
3456
3457enum class BootOptionParameter : size_t
3458{
3459 setInProgress = 0x0,
3460 bootFlags = 0x5,
3461};
3462static constexpr uint8_t setComplete = 0x0;
3463static constexpr uint8_t setInProgress = 0x1;
3464static uint8_t transferStatus = setComplete;
3465static constexpr uint8_t setParmVersion = 0x01;
3466static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
3467static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
3468static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
3469static constexpr uint8_t httpBoot = 0xd;
3470static constexpr uint8_t bootSourceMask = 0x3c;
3471
3472} // namespace boot_options
3473
3474ipmi::RspType<uint8_t, // version
3475 uint8_t, // param
3476 uint8_t, // data0, dependent on parameter
3477 std::optional<uint8_t> // data1, dependent on parameter
3478 >
3479 ipmiOemGetEfiBootOptions(uint8_t parameter, uint8_t set, uint8_t block)
3480{
3481 using namespace boot_options;
3482 uint8_t bootOption = 0;
3483
3484 if (parameter == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3485 {
3486 return ipmi::responseSuccess(setParmVersion, parameter, transferStatus,
3487 std::nullopt);
3488 }
3489
3490 if (parameter != static_cast<uint8_t>(BootOptionParameter::bootFlags))
3491 {
3492 phosphor::logging::log<phosphor::logging::level::ERR>(
3493 "Unsupported parameter");
Jayaprakash Mutyala3694d072021-07-22 10:34:37 +00003494 return ipmi::response(ccParameterNotSupported);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003495 }
3496
3497 try
3498 {
3499 auto oneTimeEnabled = false;
3500 // read one time Enabled property
3501 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3502 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3503 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3504 enabledIntf, oneTimeBootEnableProp);
3505 oneTimeEnabled = std::get<bool>(variant);
3506
3507 // get BootSource and BootMode properties
3508 // according to oneTimeEnable
3509 auto bootObjPath = oneTimePath;
3510 if (oneTimeEnabled == false)
3511 {
3512 bootObjPath = persistentObjPath;
3513 }
3514
3515 service = getService(*dbus, bootModeIntf, bootObjPath);
3516 variant = getDbusProperty(*dbus, service, bootObjPath, bootModeIntf,
3517 bootModeProp);
3518
3519 auto bootMode =
3520 Mode::convertModesFromString(std::get<std::string>(variant));
3521
3522 service = getService(*dbus, bootSourceIntf, bootObjPath);
3523 variant = getDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3524 bootSourceProp);
3525
3526 if (std::get<std::string>(variant) == httpBootMode)
3527 {
3528 bootOption = httpBoot;
3529 }
3530 else
3531 {
3532 auto bootSource = Source::convertSourcesFromString(
3533 std::get<std::string>(variant));
3534 bootOption = sourceDbusToIpmi.at(bootSource);
3535 if (Source::Sources::Default == bootSource)
3536 {
3537 bootOption = modeDbusToIpmi.at(bootMode);
3538 }
3539 }
3540
3541 uint8_t oneTime = oneTimeEnabled ? setParmBootFlagsValidOneTime
3542 : setParmBootFlagsValidPermanent;
3543 bootOption <<= 2; // shift for responseconstexpr
3544 return ipmi::responseSuccess(setParmVersion, parameter, oneTime,
3545 bootOption);
3546 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003547 catch (const sdbusplus::exception_t& e)
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003548 {
3549 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3550 return ipmi::responseResponseError();
3551 }
3552}
3553
3554ipmi::RspType<> ipmiOemSetEfiBootOptions(uint8_t bootFlag, uint8_t bootParam,
3555 std::optional<uint8_t> bootOption)
3556{
3557 using namespace boot_options;
3558 auto oneTimeEnabled = false;
3559
Mike Jonesbc01d212022-06-16 12:41:33 -07003560 if (bootFlag == 0 && bootParam == 0)
3561 {
3562 phosphor::logging::log<phosphor::logging::level::ERR>(
3563 "Unsupported parameter");
3564 return ipmi::response(ccParameterNotSupported);
3565 }
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003566 if (bootFlag == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3567 {
3568 if (bootOption)
3569 {
3570 return ipmi::responseReqDataLenInvalid();
3571 }
3572
3573 if (transferStatus == setInProgress)
3574 {
3575 phosphor::logging::log<phosphor::logging::level::ERR>(
3576 "boot option set in progress!");
3577 return ipmi::responseResponseError();
3578 }
3579
3580 transferStatus = bootParam;
3581 return ipmi::responseSuccess();
3582 }
3583
3584 if (bootFlag != (uint8_t)BootOptionParameter::bootFlags)
3585 {
3586 phosphor::logging::log<phosphor::logging::level::ERR>(
3587 "Unsupported parameter");
Jayaprakash Mutyala3694d072021-07-22 10:34:37 +00003588 return ipmi::response(ccParameterNotSupported);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003589 }
3590
3591 if (!bootOption)
3592 {
3593 return ipmi::responseReqDataLenInvalid();
3594 }
3595
3596 if (((bootOption.value() & bootSourceMask) >> 2) !=
3597 httpBoot) // not http boot, exit
3598 {
3599 phosphor::logging::log<phosphor::logging::level::ERR>(
3600 "wrong boot option parameter!");
3601 return ipmi::responseParmOutOfRange();
3602 }
3603
3604 try
3605 {
3606 bool permanent = (bootParam & setParmBootFlagsPermanent) ==
3607 setParmBootFlagsPermanent;
3608
3609 // read one time Enabled property
3610 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3611 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3612 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3613 enabledIntf, oneTimeBootEnableProp);
3614 oneTimeEnabled = std::get<bool>(variant);
3615
3616 /*
3617 * Check if the current boot setting is onetime or permanent, if the
3618 * request in the command is otherwise, then set the "Enabled"
3619 * property in one_time object path to 'True' to indicate onetime
3620 * and 'False' to indicate permanent.
3621 *
3622 * Once the onetime/permanent setting is applied, then the bootMode
3623 * and bootSource is updated for the corresponding object.
3624 */
3625 if (permanent == oneTimeEnabled)
3626 {
3627 setDbusProperty(*dbus, service, oneTimePath, enabledIntf,
3628 oneTimeBootEnableProp, !permanent);
3629 }
3630
3631 // set BootSource and BootMode properties
3632 // according to oneTimeEnable or persistent
3633 auto bootObjPath = oneTimePath;
3634 if (oneTimeEnabled == false)
3635 {
3636 bootObjPath = persistentObjPath;
3637 }
3638 std::string bootMode =
3639 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
3640 std::string bootSource = httpBootMode;
3641
3642 service = getService(*dbus, bootModeIntf, bootObjPath);
3643 setDbusProperty(*dbus, service, bootObjPath, bootModeIntf, bootModeProp,
3644 bootMode);
3645
3646 service = getService(*dbus, bootSourceIntf, bootObjPath);
3647 setDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3648 bootSourceProp, bootSource);
3649 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003650 catch (const sdbusplus::exception_t& e)
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003651 {
3652 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3653 return ipmi::responseResponseError();
3654 }
3655
3656 return ipmi::responseSuccess();
3657}
3658
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003659using BasicVariantType =
3660 std::variant<std::vector<std::string>, std::vector<uint64_t>, std::string,
3661 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
3662 uint16_t, uint8_t, bool>;
3663using PropertyMapType =
3664 boost::container::flat_map<std::string, BasicVariantType>;
3665static constexpr const std::array<const char*, 1> psuPresenceTypes = {
3666 "xyz.openbmc_project.Configuration.PSUPresence"};
3667int getPSUAddress(ipmi::Context::ptr ctx, uint8_t& bus,
3668 std::vector<uint64_t>& addrTable)
3669{
3670 boost::system::error_code ec;
3671 GetSubTreeType subtree = ctx->bus->yield_method_call<GetSubTreeType>(
3672 ctx->yield, ec, "xyz.openbmc_project.ObjectMapper",
3673 "/xyz/openbmc_project/object_mapper",
3674 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3675 "/xyz/openbmc_project/inventory/system", 3, psuPresenceTypes);
3676 if (ec)
3677 {
3678 phosphor::logging::log<phosphor::logging::level::ERR>(
3679 "Failed to set dbus property to cold redundancy");
3680 return -1;
3681 }
3682 for (const auto& object : subtree)
3683 {
3684 std::string pathName = object.first;
3685 for (const auto& serviceIface : object.second)
3686 {
3687 std::string serviceName = serviceIface.first;
3688
3689 ec.clear();
3690 PropertyMapType propMap =
3691 ctx->bus->yield_method_call<PropertyMapType>(
3692 ctx->yield, ec, serviceName, pathName,
3693 "org.freedesktop.DBus.Properties", "GetAll",
3694 "xyz.openbmc_project.Configuration.PSUPresence");
3695 if (ec)
3696 {
3697 phosphor::logging::log<phosphor::logging::level::ERR>(
3698 "Failed to set dbus property to cold redundancy");
3699 return -1;
3700 }
3701 auto psuBus = std::get_if<uint64_t>(&propMap["Bus"]);
3702 auto psuAddress =
3703 std::get_if<std::vector<uint64_t>>(&propMap["Address"]);
3704
3705 if (psuBus == nullptr || psuAddress == nullptr)
3706 {
3707 std::cerr << "error finding necessary "
3708 "entry in configuration\n";
3709 return -1;
3710 }
3711 bus = static_cast<uint8_t>(*psuBus);
3712 addrTable = *psuAddress;
3713 return 0;
3714 }
3715 }
3716 return -1;
3717}
3718
3719static const constexpr uint8_t addrOffset = 8;
3720static const constexpr uint8_t psuRevision = 0xd9;
3721static const constexpr uint8_t defaultPSUBus = 7;
3722// Second Minor, Primary Minor, Major
3723static const constexpr size_t verLen = 3;
3724ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPSUVersion(ipmi::Context::ptr ctx)
3725{
3726 uint8_t bus = defaultPSUBus;
3727 std::vector<uint64_t> addrTable;
3728 std::vector<uint8_t> result;
3729 if (getPSUAddress(ctx, bus, addrTable))
3730 {
3731 std::cerr << "Failed to get PSU bus and address\n";
3732 return ipmi::responseResponseError();
3733 }
3734
3735 for (const auto& slaveAddr : addrTable)
3736 {
3737 std::vector<uint8_t> writeData = {psuRevision};
3738 std::vector<uint8_t> readBuf(verLen);
3739 uint8_t addr = static_cast<uint8_t>(slaveAddr) + addrOffset;
3740 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
3741
3742 auto retI2C = ipmi::i2cWriteRead(i2cBus, addr, writeData, readBuf);
3743 if (retI2C != ipmi::ccSuccess)
3744 {
3745 for (size_t idx = 0; idx < verLen; idx++)
3746 {
3747 result.emplace_back(0x00);
3748 }
3749 }
3750 else
3751 {
3752 for (const uint8_t& data : readBuf)
3753 {
3754 result.emplace_back(data);
3755 }
3756 }
3757 }
3758
3759 return ipmi::responseSuccess(result);
3760}
3761
srikanta mondal2030d7c2020-05-03 17:25:25 +00003762std::optional<uint8_t> getMultiNodeInfoPresence(ipmi::Context::ptr ctx,
3763 const std::string& name)
3764{
3765 Value dbusValue = 0;
3766 std::string serviceName;
3767
3768 boost::system::error_code ec =
3769 ipmi::getService(ctx, multiNodeIntf, multiNodeObjPath, serviceName);
3770
3771 if (ec)
3772 {
3773 phosphor::logging::log<phosphor::logging::level::ERR>(
3774 "Failed to perform Multinode getService.");
3775 return std::nullopt;
3776 }
3777
3778 ec = ipmi::getDbusProperty(ctx, serviceName, multiNodeObjPath,
3779 multiNodeIntf, name, dbusValue);
3780 if (ec)
3781 {
3782 phosphor::logging::log<phosphor::logging::level::ERR>(
3783 "Failed to perform Multinode get property");
3784 return std::nullopt;
3785 }
3786
3787 auto multiNodeVal = std::get_if<uint8_t>(&dbusValue);
3788 if (!multiNodeVal)
3789 {
3790 phosphor::logging::log<phosphor::logging::level::ERR>(
3791 "getMultiNodeInfoPresence: error to get multinode");
3792 return std::nullopt;
3793 }
3794 return *multiNodeVal;
3795}
3796
3797/** @brief implements OEM get reading command
3798 * @param domain ID
3799 * @param reading Type
3800 * - 00h = platform Power Consumption
3801 * - 01h = inlet Air Temp
3802 * - 02h = icc_TDC from PECI
3803 * @param reserved, write as 0000h
3804 *
3805 * @returns IPMI completion code plus response data
3806 * - response
3807 * - domain ID
3808 * - reading Type
3809 * - 00h = platform Power Consumption
3810 * - 01h = inlet Air Temp
3811 * - 02h = icc_TDC from PECI
3812 * - reading
3813 */
3814ipmi::RspType<uint4_t, // domain ID
3815 uint4_t, // reading Type
3816 uint16_t // reading Value
3817 >
3818 ipmiOEMGetReading(ipmi::Context::ptr ctx, uint4_t domainId,
3819 uint4_t readingType, uint16_t reserved)
3820{
3821 constexpr uint8_t platformPower = 0;
3822 constexpr uint8_t inletAirTemp = 1;
3823 constexpr uint8_t iccTdc = 2;
3824
3825 if ((static_cast<uint8_t>(readingType) > iccTdc) || domainId || reserved)
3826 {
3827 return ipmi::responseInvalidFieldRequest();
3828 }
3829
3830 // This command should run only from multi-node product.
3831 // For all other platforms this command will return invalid.
3832
3833 std::optional<uint8_t> nodeInfo =
3834 getMultiNodeInfoPresence(ctx, "NodePresence");
3835 if (!nodeInfo || !*nodeInfo)
3836 {
3837 return ipmi::responseInvalidCommand();
3838 }
3839
3840 uint16_t oemReadingValue = 0;
3841 if (static_cast<uint8_t>(readingType) == inletAirTemp)
3842 {
3843 double value = 0;
3844 boost::system::error_code ec = ipmi::getDbusProperty(
3845 ctx, "xyz.openbmc_project.HwmonTempSensor",
3846 "/xyz/openbmc_project/sensors/temperature/Inlet_BRD_Temp",
3847 "xyz.openbmc_project.Sensor.Value", "Value", value);
3848 if (ec)
3849 {
3850 phosphor::logging::log<phosphor::logging::level::ERR>(
3851 "Failed to get BMC Get OEM temperature",
3852 phosphor::logging::entry("EXCEPTION=%s", ec.message().c_str()));
3853 return ipmi::responseUnspecifiedError();
3854 }
3855 // Take the Inlet temperature
3856 oemReadingValue = static_cast<uint16_t>(value);
3857 }
3858 else
3859 {
3860 phosphor::logging::log<phosphor::logging::level::ERR>(
3861 "Currently Get OEM Reading support only for Inlet Air Temp");
3862 return ipmi::responseParmOutOfRange();
3863 }
3864 return ipmi::responseSuccess(domainId, readingType, oemReadingValue);
3865}
3866
AppaRao Puli28972062019-11-11 02:04:45 +05303867/** @brief implements the maximum size of
3868 * bridgeable messages used between KCS and
3869 * IPMB interfacesget security mode command.
3870 *
3871 * @returns IPMI completion code with following data
3872 * - KCS Buffer Size (In multiples of four bytes)
3873 * - IPMB Buffer Size (In multiples of four bytes)
3874 **/
3875ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetBufferSize()
3876{
3877 // for now this is hard coded; really this number is dependent on
3878 // the BMC kcs driver as well as the host kcs driver....
3879 // we can't know the latter.
3880 uint8_t kcsMaxBufferSize = 63 / 4;
3881 uint8_t ipmbMaxBufferSize = 128 / 4;
3882
3883 return ipmi::responseSuccess(kcsMaxBufferSize, ipmbMaxBufferSize);
3884}
3885
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +00003886ipmi::RspType<std::vector<uint8_t>>
3887 ipmiOEMReadPFRMailbox(ipmi::Context::ptr& ctx, const uint8_t readRegister,
3888 const uint8_t numOfBytes, uint8_t registerIdentifier)
3889{
3890 if (!ipmi::mailbox::i2cConfigLoaded)
3891 {
3892
3893 phosphor::logging::log<phosphor::logging::level::ERR>(
3894 "Calling PFR Load Configuration Function to Get I2C Bus and Slave "
3895 "Address ");
3896
3897 ipmi::mailbox::loadPfrConfig(ctx, ipmi::mailbox::i2cConfigLoaded);
3898 }
3899
3900 if (!numOfBytes && !readRegister)
3901 {
3902 phosphor::logging::log<phosphor::logging::level::ERR>(
3903 "OEM IPMI command: Read & write count are 0 which is invalid ");
3904 return ipmi::responseInvalidFieldRequest();
3905 }
3906
3907 switch (registerIdentifier)
3908 {
3909 case ipmi::mailbox::registerType::fifoReadRegister:
3910 {
3911 // Check if readRegister is an FIFO read register
3912 if (registerIdentifier == 1)
3913 {
3914 if (ipmi::mailbox::readFifoReg.find(readRegister) ==
3915 ipmi::mailbox::readFifoReg.end())
3916 {
3917 phosphor::logging::log<phosphor::logging::level::ERR>(
3918 "OEM IPMI command: Register is not a Read FIFO ");
3919 return ipmi::responseInvalidFieldRequest();
3920 }
3921
3922 phosphor::logging::log<phosphor::logging::level::ERR>(
3923 "OEM IPMI command: Register is a Read FIFO ");
3924
3925 ipmi::mailbox::writefifo(ipmi::mailbox::provisioningCommand,
3926 readRegister);
3927 ipmi::mailbox::writefifo(ipmi::mailbox::triggerCommand,
3928 ipmi::mailbox::flushRead);
3929
3930 std::vector<uint8_t> writeData = {ipmi::mailbox::readFifo};
3931 std::vector<uint8_t> readBuf(1);
3932 std::vector<uint8_t> result;
3933
3934 for (int i = 0; i < numOfBytes; i++)
3935 {
3936
3937 ipmi::Cc ret = ipmi::i2cWriteRead(ipmi::mailbox::i2cBus,
3938 ipmi::mailbox::slaveAddr,
3939 writeData, readBuf);
3940 if (ret != ipmi::ccSuccess)
3941 {
3942 return ipmi::response(ret);
3943 }
3944
3945 else
3946 {
3947 for (const uint8_t& data : readBuf)
3948 {
3949 result.emplace_back(data);
3950 }
3951 }
3952 }
3953
3954 return ipmi::responseSuccess(result);
3955 }
3956 }
3957
3958 case ipmi::mailbox::registerType::singleByteRegister:
3959 {
3960 phosphor::logging::log<phosphor::logging::level::ERR>(
3961 "OEM IPMI command: Register is a Single Byte Register ");
3962
3963 std::vector<uint8_t> writeData = {readRegister};
3964 std::vector<uint8_t> readBuf(numOfBytes);
3965
3966 ipmi::Cc ret = ipmi::i2cWriteRead(ipmi::mailbox::i2cBus,
3967 ipmi::mailbox::slaveAddr,
3968 writeData, readBuf);
3969 if (ret != ipmi::ccSuccess)
3970 {
3971 return ipmi::response(ret);
3972 }
3973 return ipmi::responseSuccess(readBuf);
3974 }
3975
3976 default:
3977 {
3978
3979 phosphor::logging::log<phosphor::logging::level::ERR>(
3980 "OEM IPMI command: Register identifier is not valid.It should "
3981 "be 0 "
3982 "for Single Byte Register and 1 for FIFO Read Register");
3983
3984 return ipmi::responseInvalidFieldRequest();
3985 }
3986 }
3987}
3988
Jason M. Bills64796042018-10-03 16:51:55 -07003989static void registerOEMFunctions(void)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003990{
3991 phosphor::logging::log<phosphor::logging::level::INFO>(
3992 "Registering OEM commands");
Vernon Mauery98bbf692019-09-16 11:14:59 -07003993 ipmiPrintAndRegister(intel::netFnGeneral,
3994 intel::general::cmdGetChassisIdentifier, NULL,
3995 ipmiOEMGetChassisIdentifier,
3996 PRIVILEGE_USER); // get chassis identifier
3997
3998 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetSystemGUID,
3999 NULL, ipmiOEMSetSystemGUID,
4000 PRIVILEGE_ADMIN); // set system guid
Jason M. Billsb02bf092019-08-15 13:01:56 -07004001
4002 // <Disable BMC System Reset Action>
Vernon Mauery98bbf692019-09-16 11:14:59 -07004003 registerHandler(prioOemBase, intel::netFnGeneral,
4004 intel::general::cmdDisableBMCSystemReset, Privilege::Admin,
4005 ipmiOEMDisableBMCSystemReset);
4006
Jason M. Billsb02bf092019-08-15 13:01:56 -07004007 // <Get BMC Reset Disables>
Vernon Mauery98bbf692019-09-16 11:14:59 -07004008 registerHandler(prioOemBase, intel::netFnGeneral,
4009 intel::general::cmdGetBMCResetDisables, Privilege::Admin,
4010 ipmiOEMGetBMCResetDisables);
Jason M. Billsb02bf092019-08-15 13:01:56 -07004011
Vernon Mauery98bbf692019-09-16 11:14:59 -07004012 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetBIOSID,
4013 NULL, ipmiOEMSetBIOSID, PRIVILEGE_ADMIN);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08004014
Chen Yugang7a04f3a2019-10-08 11:12:35 +08004015 registerHandler(prioOemBase, intel::netFnGeneral,
4016 intel::general::cmdGetOEMDeviceInfo, Privilege::User,
4017 ipmiOEMGetDeviceInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08004018
Vernon Mauery98bbf692019-09-16 11:14:59 -07004019 ipmiPrintAndRegister(intel::netFnGeneral,
4020 intel::general::cmdGetAICSlotFRUIDSlotPosRecords, NULL,
4021 ipmiOEMGetAICFRU, PRIVILEGE_USER);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05304022
Vernon Mauery98bbf692019-09-16 11:14:59 -07004023 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4024 intel::general::cmdSendEmbeddedFWUpdStatus,
4025 Privilege::Operator, ipmiOEMSendEmbeddedFwUpdStatus);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05304026
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +05304027 registerHandler(prioOpenBmcBase, intel::netFnApp, intel::app::cmdSlotIpmb,
4028 Privilege::Admin, ipmiOEMSlotIpmb);
4029
Vernon Mauery98bbf692019-09-16 11:14:59 -07004030 ipmiPrintAndRegister(intel::netFnGeneral,
4031 intel::general::cmdSetPowerRestoreDelay, NULL,
4032 ipmiOEMSetPowerRestoreDelay, PRIVILEGE_OPERATOR);
4033
4034 ipmiPrintAndRegister(intel::netFnGeneral,
4035 intel::general::cmdGetPowerRestoreDelay, NULL,
4036 ipmiOEMGetPowerRestoreDelay, PRIVILEGE_USER);
4037
4038 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4039 intel::general::cmdSetOEMUser2Activation,
4040 Privilege::Callback, ipmiOEMSetUser2Activation);
4041
4042 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4043 intel::general::cmdSetSpecialUserPassword,
4044 Privilege::Callback, ipmiOEMSetSpecialUserPassword);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05304045
Jason M. Bills42bd9c82019-06-28 16:39:34 -07004046 // <Get Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07004047 registerHandler(prioOemBase, intel::netFnGeneral,
4048 intel::general::cmdGetProcessorErrConfig, Privilege::User,
4049 ipmiOEMGetProcessorErrConfig);
4050
Jason M. Bills42bd9c82019-06-28 16:39:34 -07004051 // <Set Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07004052 registerHandler(prioOemBase, intel::netFnGeneral,
4053 intel::general::cmdSetProcessorErrConfig, Privilege::Admin,
4054 ipmiOEMSetProcessorErrConfig);
Jason M. Bills42bd9c82019-06-28 16:39:34 -07004055
Vernon Mauery98bbf692019-09-16 11:14:59 -07004056 ipmiPrintAndRegister(intel::netFnGeneral,
4057 intel::general::cmdSetShutdownPolicy, NULL,
4058 ipmiOEMSetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08004059
Vernon Mauery98bbf692019-09-16 11:14:59 -07004060 ipmiPrintAndRegister(intel::netFnGeneral,
4061 intel::general::cmdGetShutdownPolicy, NULL,
4062 ipmiOEMGetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08004063
anil kumar appanaf945eee2019-09-25 23:29:11 +00004064 registerHandler(prioOemBase, intel::netFnGeneral,
4065 intel::general::cmdSetFanConfig, Privilege::User,
4066 ipmiOEMSetFanConfig);
James Feist91244a62019-02-19 15:04:54 -08004067
Vernon Mauery98bbf692019-09-16 11:14:59 -07004068 registerHandler(prioOemBase, intel::netFnGeneral,
4069 intel::general::cmdGetFanConfig, Privilege::User,
4070 ipmiOEMGetFanConfig);
James Feist5f957ca2019-03-14 15:33:55 -07004071
Vernon Mauery98bbf692019-09-16 11:14:59 -07004072 registerHandler(prioOemBase, intel::netFnGeneral,
4073 intel::general::cmdGetFanSpeedOffset, Privilege::User,
4074 ipmiOEMGetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07004075
Vernon Mauery98bbf692019-09-16 11:14:59 -07004076 registerHandler(prioOemBase, intel::netFnGeneral,
4077 intel::general::cmdSetFanSpeedOffset, Privilege::User,
4078 ipmiOEMSetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07004079
Vernon Mauery98bbf692019-09-16 11:14:59 -07004080 registerHandler(prioOemBase, intel::netFnGeneral,
4081 intel::general::cmdSetFscParameter, Privilege::User,
4082 ipmiOEMSetFscParameter);
James Feist5f957ca2019-03-14 15:33:55 -07004083
Vernon Mauery98bbf692019-09-16 11:14:59 -07004084 registerHandler(prioOemBase, intel::netFnGeneral,
4085 intel::general::cmdGetFscParameter, Privilege::User,
4086 ipmiOEMGetFscParameter);
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05304087
Vernon Mauery98bbf692019-09-16 11:14:59 -07004088 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4089 intel::general::cmdReadBaseBoardProductId, Privilege::Admin,
4090 ipmiOEMReadBoardProductId);
Chen Yugang39736d52019-07-12 16:24:33 +08004091
Vernon Mauery98bbf692019-09-16 11:14:59 -07004092 registerHandler(prioOemBase, intel::netFnGeneral,
4093 intel::general::cmdGetNmiStatus, Privilege::User,
4094 ipmiOEMGetNmiSource);
Chen Yugang39736d52019-07-12 16:24:33 +08004095
Vernon Mauery98bbf692019-09-16 11:14:59 -07004096 registerHandler(prioOemBase, intel::netFnGeneral,
4097 intel::general::cmdSetNmiStatus, Privilege::Operator,
4098 ipmiOEMSetNmiSource);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08004099
Vernon Mauery98bbf692019-09-16 11:14:59 -07004100 registerHandler(prioOemBase, intel::netFnGeneral,
4101 intel::general::cmdGetEfiBootOptions, Privilege::User,
4102 ipmiOemGetEfiBootOptions);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08004103
Vernon Mauery98bbf692019-09-16 11:14:59 -07004104 registerHandler(prioOemBase, intel::netFnGeneral,
4105 intel::general::cmdSetEfiBootOptions, Privilege::Operator,
4106 ipmiOemSetEfiBootOptions);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05304107
Vernon Mauery98bbf692019-09-16 11:14:59 -07004108 registerHandler(prioOemBase, intel::netFnGeneral,
4109 intel::general::cmdGetSecurityMode, Privilege::User,
4110 ipmiGetSecurityMode);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05304111
Vernon Mauery98bbf692019-09-16 11:14:59 -07004112 registerHandler(prioOemBase, intel::netFnGeneral,
4113 intel::general::cmdSetSecurityMode, Privilege::Admin,
4114 ipmiSetSecurityMode);
Vernon Mauery4ac799d2019-05-20 15:50:37 -07004115
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00004116 registerHandler(prioOemBase, intel::netFnGeneral,
4117 intel::general::cmdGetLEDStatus, Privilege::Admin,
4118 ipmiOEMGetLEDStatus);
Cheng C Yang773703a2019-08-15 09:41:11 +08004119
Vernon Mauery98bbf692019-09-16 11:14:59 -07004120 ipmiPrintAndRegister(ipmi::intel::netFnPlatform,
4121 ipmi::intel::platform::cmdCfgHostSerialPortSpeed, NULL,
4122 ipmiOEMCfgHostSerialPortSpeed, PRIVILEGE_ADMIN);
4123
4124 registerHandler(prioOemBase, intel::netFnGeneral,
4125 intel::general::cmdSetFaultIndication, Privilege::Operator,
4126 ipmiOEMSetFaultIndication);
4127
4128 registerHandler(prioOemBase, intel::netFnGeneral,
4129 intel::general::cmdSetColdRedundancyConfig, Privilege::User,
4130 ipmiOEMSetCRConfig);
4131
4132 registerHandler(prioOemBase, intel::netFnGeneral,
4133 intel::general::cmdGetColdRedundancyConfig, Privilege::User,
4134 ipmiOEMGetCRConfig);
4135
4136 registerHandler(prioOemBase, intel::netFnGeneral,
4137 intel::general::cmdRestoreConfiguration, Privilege::Admin,
Vernon Mauery4ac799d2019-05-20 15:50:37 -07004138 ipmiRestoreConfiguration);
James Feist63efafa2019-07-24 12:39:21 -07004139
Vernon Mauery98bbf692019-09-16 11:14:59 -07004140 registerHandler(prioOemBase, intel::netFnGeneral,
4141 intel::general::cmdSetDimmOffset, Privilege::Operator,
4142 ipmiOEMSetDimmOffset);
James Feist63efafa2019-07-24 12:39:21 -07004143
Vernon Mauery98bbf692019-09-16 11:14:59 -07004144 registerHandler(prioOemBase, intel::netFnGeneral,
4145 intel::general::cmdGetDimmOffset, Privilege::Operator,
4146 ipmiOEMGetDimmOffset);
Chen Yugangca12a7b2019-09-03 18:11:44 +08004147
Cheng C Yang4e6ee152019-09-25 10:27:44 +08004148 registerHandler(prioOemBase, intel::netFnGeneral,
4149 intel::general::cmdGetPSUVersion, Privilege::User,
4150 ipmiOEMGetPSUVersion);
AppaRao Puli28972062019-11-11 02:04:45 +05304151
4152 registerHandler(prioOemBase, intel::netFnGeneral,
4153 intel::general::cmdGetBufferSize, Privilege::User,
4154 ipmiOEMGetBufferSize);
srikanta mondal2030d7c2020-05-03 17:25:25 +00004155
4156 registerHandler(prioOemBase, intel::netFnGeneral,
4157 intel::general::cmdOEMGetReading, Privilege::User,
4158 ipmiOEMGetReading);
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +00004159
4160 registerHandler(prioOemBase, intel::netFnApp, intel::app::cmdPFRMailboxRead,
4161 Privilege::Admin, ipmiOEMReadPFRMailbox);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08004162}
4163
4164} // namespace ipmi