blob: b258e21b8123895176dbab2c668e13816030f31d [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>
47#include <iostream>
48#include <regex>
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +000049#include <set>
James Feistfcd2d3a2020-05-28 10:38:15 -070050#include <string>
51#include <variant>
52#include <vector>
53
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080054namespace ipmi
55{
Jason M. Bills64796042018-10-03 16:51:55 -070056static void registerOEMFunctions() __attribute__((constructor));
Vernon Mauery4ac799d2019-05-20 15:50:37 -070057
Jason M. Bills64796042018-10-03 16:51:55 -070058static constexpr size_t maxFRUStringLength = 0x3F;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080059
Suryakanth Sekard509eb92018-11-15 17:44:11 +053060static constexpr auto ethernetIntf =
61 "xyz.openbmc_project.Network.EthernetInterface";
62static constexpr auto networkIPIntf = "xyz.openbmc_project.Network.IP";
63static constexpr auto networkService = "xyz.openbmc_project.Network";
64static constexpr auto networkRoot = "/xyz/openbmc_project/network";
65
Chen Yugang97cf96e2019-11-01 08:55:11 +080066static constexpr const char* oemNmiSourceIntf =
67 "xyz.openbmc_project.Chassis.Control.NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080068static constexpr const char* oemNmiSourceObjPath =
Chen Yugang97cf96e2019-11-01 08:55:11 +080069 "/xyz/openbmc_project/Chassis/Control/NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080070static constexpr const char* oemNmiBmcSourceObjPathProp = "BMCSource";
71static constexpr const char* oemNmiEnabledObjPathProp = "Enabled";
72
James Feist63efafa2019-07-24 12:39:21 -070073static constexpr const char* dimmOffsetFile = "/var/lib/ipmi/ipmi_dimms.json";
srikanta mondal2030d7c2020-05-03 17:25:25 +000074static constexpr const char* multiNodeObjPath =
75 "/xyz/openbmc_project/MultiNode/Status";
76static constexpr const char* multiNodeIntf =
77 "xyz.openbmc_project.Chassis.MultiNode";
James Feist63efafa2019-07-24 12:39:21 -070078
Chen Yugang39736d52019-07-12 16:24:33 +080079enum class NmiSource : uint8_t
80{
81 none = 0,
Chen Yugang97cf96e2019-11-01 08:55:11 +080082 frontPanelButton = 1,
83 watchdog = 2,
84 chassisCmd = 3,
85 memoryError = 4,
86 pciBusError = 5,
87 pch = 6,
88 chipset = 7,
Chen Yugang39736d52019-07-12 16:24:33 +080089};
90
Suryakanth Sekar822b0b42019-11-15 18:32:53 +053091enum class SpecialUserIndex : uint8_t
92{
93 rootUser = 0,
94 atScaleDebugUser = 1
95};
96
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053097static constexpr const char* restricionModeService =
98 "xyz.openbmc_project.RestrictionMode.Manager";
99static constexpr const char* restricionModeBasePath =
100 "/xyz/openbmc_project/control/security/restriction_mode";
101static constexpr const char* restricionModeIntf =
102 "xyz.openbmc_project.Control.Security.RestrictionMode";
103static constexpr const char* restricionModeProperty = "RestrictionMode";
104
105static constexpr const char* specialModeService =
106 "xyz.openbmc_project.SpecialMode";
107static constexpr const char* specialModeBasePath =
Richard Marian Thomaiyara7b74282019-09-22 21:53:14 +0530108 "/xyz/openbmc_project/security/special_mode";
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +0530109static constexpr const char* specialModeIntf =
110 "xyz.openbmc_project.Security.SpecialMode";
111static constexpr const char* specialModeProperty = "SpecialMode";
112
113static constexpr const char* dBusPropertyIntf =
114 "org.freedesktop.DBus.Properties";
115static constexpr const char* dBusPropertyGetMethod = "Get";
116static constexpr const char* dBusPropertySetMethod = "Set";
117
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800118// return code: 0 successful
119int8_t getChassisSerialNumber(sdbusplus::bus::bus& bus, std::string& serial)
120{
121 std::string objpath = "/xyz/openbmc_project/FruDevice";
122 std::string intf = "xyz.openbmc_project.FruDeviceManager";
123 std::string service = getService(bus, intf, objpath);
124 ObjectValueTree valueTree = getManagedObjects(bus, service, "/");
125 if (valueTree.empty())
126 {
127 phosphor::logging::log<phosphor::logging::level::ERR>(
128 "No object implements interface",
129 phosphor::logging::entry("INTF=%s", intf.c_str()));
130 return -1;
131 }
132
Jason M. Bills64796042018-10-03 16:51:55 -0700133 for (const auto& item : valueTree)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800134 {
135 auto interface = item.second.find("xyz.openbmc_project.FruDevice");
136 if (interface == item.second.end())
137 {
138 continue;
139 }
140
141 auto property = interface->second.find("CHASSIS_SERIAL_NUMBER");
142 if (property == interface->second.end())
143 {
144 continue;
145 }
146
147 try
148 {
149 Value variant = property->second;
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700150 std::string& result = std::get<std::string>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700151 if (result.size() > maxFRUStringLength)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800152 {
153 phosphor::logging::log<phosphor::logging::level::ERR>(
154 "FRU serial number exceed maximum length");
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800155 return -1;
156 }
Jason M. Bills64796042018-10-03 16:51:55 -0700157 serial = result;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800158 return 0;
159 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500160 catch (const std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800161 {
Jason M. Bills64796042018-10-03 16:51:55 -0700162 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800163 return -1;
164 }
165 }
166 return -1;
167}
Jason M. Bills64796042018-10-03 16:51:55 -0700168
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +0000169namespace mailbox
170{
171static uint8_t bus = 4;
172static std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
173static uint8_t slaveAddr = 56;
174static constexpr auto systemRoot = "/xyz/openbmc_project/inventory/system";
175static constexpr auto sessionIntf = "xyz.openbmc_project.Configuration.PFR";
176const std::string match = "Baseboard/PFR";
177static bool i2cConfigLoaded = false;
178// Command register for UFM provisioning/access commands; read/write allowed
179// from CPU/BMC.
180static const constexpr uint8_t provisioningCommand = 0x0b;
181// Trigger register for the command set in the previous offset.
182static const constexpr uint8_t triggerCommand = 0x0c;
183// Set 0x0c to 0x05 to execute command specified at “UFM/Provisioning Command”
184// register
185static const constexpr uint8_t flushRead = 0x05;
186// FIFO read registers
187std::set<uint8_t> readFifoReg = {0x08, 0x0C, 0x0D, 0x13};
188
189// UFM Read FIFO
190static const constexpr uint8_t readFifo = 0x0e;
191
192enum registerType : uint8_t
193{
194 singleByteRegister = 0,
195 fifoReadRegister,
196
197};
198
199void loadPfrConfig(ipmi::Context::ptr& ctx, bool& i2cConfigLoaded)
200{
201 ipmi::ObjectTree objectTree;
202
203 boost::system::error_code ec = ipmi::getAllDbusObjects(
204 ctx, systemRoot, sessionIntf, match, objectTree);
205
206 if (ec)
207 {
208
209 phosphor::logging::log<phosphor::logging::level::ERR>(
210 "Failed to fetch PFR object from dbus",
211 phosphor::logging::entry("INTERFACE=%s", sessionIntf),
212 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
213
214 return;
215 }
216
217 for (auto& softObject : objectTree)
218 {
219 const std::string& objPath = softObject.first;
220 const std::string& serviceName = softObject.second.begin()->first;
221 // PFR object found.. check for PFR support
222 ipmi::PropertyMap result;
223
224 ec = ipmi::getAllDbusProperties(ctx, serviceName, objPath, sessionIntf,
225 result);
226
227 if (ec)
228 {
229 phosphor::logging::log<phosphor::logging::level::ERR>(
230 "Failed to fetch pfr properties",
231 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
232 return;
233 }
234
235 const uint64_t* i2cBusNum = nullptr;
236 const uint64_t* address = nullptr;
237
238 for (const auto& [propName, propVariant] : result)
239 {
240
241 if (propName == "Address")
242 {
243 address = std::get_if<uint64_t>(&propVariant);
244 }
245 else if (propName == "Bus")
246 {
247 i2cBusNum = std::get_if<uint64_t>(&propVariant);
248 }
249 }
250
251 if ((address == nullptr) || (i2cBusNum == nullptr))
252 {
253 phosphor::logging::log<phosphor::logging::level::ERR>(
254 "Unable to read the pfr properties");
255 return;
256 }
257
258 bus = static_cast<int>(*i2cBusNum);
259 i2cBus = "/dev/i2c-" + std::to_string(bus);
260 slaveAddr = static_cast<int>(*address);
261
262 i2cConfigLoaded = true;
263 }
264}
265
266void writefifo(const uint8_t cmdReg, const uint8_t val)
267{
268 // Based on the spec, writing cmdReg to address val on this device, will
269 // trigger the write FIFO operation.
270 std::vector<uint8_t> writeData = {cmdReg, val};
271 std::vector<uint8_t> readBuf(0);
272 ipmi::Cc retI2C = ipmi::i2cWriteRead(i2cBus, slaveAddr, writeData, readBuf);
273}
274
275} // namespace mailbox
276
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800277// Returns the Chassis Identifier (serial #)
278ipmi_ret_t ipmiOEMGetChassisIdentifier(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
279 ipmi_request_t request,
280 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700281 ipmi_data_len_t dataLen,
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800282 ipmi_context_t context)
283{
284 std::string serial;
Jason M. Bills64796042018-10-03 16:51:55 -0700285 if (*dataLen != 0) // invalid request if there are extra parameters
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800286 {
Jason M. Bills64796042018-10-03 16:51:55 -0700287 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800288 return IPMI_CC_REQ_DATA_LEN_INVALID;
289 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700290 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
291 if (getChassisSerialNumber(*dbus, serial) == 0)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800292 {
Jason M. Bills64796042018-10-03 16:51:55 -0700293 *dataLen = serial.size(); // length will never exceed response length
294 // as it is checked in getChassisSerialNumber
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800295 char* resp = static_cast<char*>(response);
Jason M. Bills64796042018-10-03 16:51:55 -0700296 serial.copy(resp, *dataLen);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800297 return IPMI_CC_OK;
298 }
Jason M. Bills64796042018-10-03 16:51:55 -0700299 *dataLen = 0;
300 return IPMI_CC_RESPONSE_ERROR;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800301}
302
303ipmi_ret_t ipmiOEMSetSystemGUID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
304 ipmi_request_t request,
305 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700306 ipmi_data_len_t dataLen, ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800307{
308 static constexpr size_t safeBufferLength = 50;
309 char buf[safeBufferLength] = {0};
310 GUIDData* Data = reinterpret_cast<GUIDData*>(request);
311
Jason M. Bills64796042018-10-03 16:51:55 -0700312 if (*dataLen != sizeof(GUIDData)) // 16bytes
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800313 {
Jason M. Bills64796042018-10-03 16:51:55 -0700314 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800315 return IPMI_CC_REQ_DATA_LEN_INVALID;
316 }
317
Jason M. Bills64796042018-10-03 16:51:55 -0700318 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800319
320 snprintf(
321 buf, safeBufferLength,
322 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
323 Data->timeLow4, Data->timeLow3, Data->timeLow2, Data->timeLow1,
324 Data->timeMid2, Data->timeMid1, Data->timeHigh2, Data->timeHigh1,
325 Data->clock2, Data->clock1, Data->node6, Data->node5, Data->node4,
326 Data->node3, Data->node2, Data->node1);
327 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
328 std::string guid = buf;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800329
330 std::string objpath = "/xyz/openbmc_project/control/host0/systemGUID";
331 std::string intf = "xyz.openbmc_project.Common.UUID";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700332 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
333 std::string service = getService(*dbus, intf, objpath);
334 setDbusProperty(*dbus, service, objpath, intf, "UUID", guid);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800335 return IPMI_CC_OK;
336}
337
Jason M. Billsb02bf092019-08-15 13:01:56 -0700338ipmi::RspType<> ipmiOEMDisableBMCSystemReset(bool disableResetOnSMI,
339 uint7_t reserved1)
340{
Jayaprakash Mutyala0a652fa2021-07-01 17:09:39 +0000341 if (reserved1)
342 {
343 return ipmi::responseInvalidFieldRequest();
344 }
345
Jason M. Billsb02bf092019-08-15 13:01:56 -0700346 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
347
348 try
349 {
350 auto service =
351 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
352 ipmi::setDbusProperty(*busp, service, bmcResetDisablesPath,
353 bmcResetDisablesIntf, "ResetOnSMI",
354 !disableResetOnSMI);
355 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500356 catch (const std::exception& e)
Jason M. Billsb02bf092019-08-15 13:01:56 -0700357 {
358 phosphor::logging::log<phosphor::logging::level::ERR>(
359 "Failed to set BMC reset disables",
360 phosphor::logging::entry("EXCEPTION=%s", e.what()));
361 return ipmi::responseUnspecifiedError();
362 }
363
364 return ipmi::responseSuccess();
365}
366
367ipmi::RspType<bool, // disableResetOnSMI
368 uint7_t // reserved
369 >
370 ipmiOEMGetBMCResetDisables()
371{
372 bool disableResetOnSMI = true;
373
374 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
375 try
376 {
377 auto service =
378 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
379 Value variant =
380 ipmi::getDbusProperty(*busp, service, bmcResetDisablesPath,
381 bmcResetDisablesIntf, "ResetOnSMI");
382 disableResetOnSMI = !std::get<bool>(variant);
383 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500384 catch (const std::exception& e)
Jason M. Billsb02bf092019-08-15 13:01:56 -0700385 {
386 phosphor::logging::log<phosphor::logging::level::ERR>(
387 "Failed to get BMC reset disables",
388 phosphor::logging::entry("EXCEPTION=%s", e.what()));
389 return ipmi::responseUnspecifiedError();
390 }
391
392 return ipmi::responseSuccess(disableResetOnSMI, 0);
393}
394
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800395ipmi_ret_t ipmiOEMSetBIOSID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
396 ipmi_request_t request, ipmi_response_t response,
397 ipmi_data_len_t dataLen, ipmi_context_t context)
398{
399 DeviceInfo* data = reinterpret_cast<DeviceInfo*>(request);
400
Jason M. Bills64796042018-10-03 16:51:55 -0700401 if ((*dataLen < 2) || (*dataLen != (1 + data->biosIDLength)))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800402 {
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800403 *dataLen = 0;
404 return IPMI_CC_REQ_DATA_LEN_INVALID;
405 }
Jason M. Bills64796042018-10-03 16:51:55 -0700406 std::string idString((char*)data->biosId, data->biosIDLength);
Chalapathi Venkataramashettyfb9f1aa2021-05-07 08:37:07 +0000407 for (auto idChar : idString)
408 {
409 if (!std::isprint(static_cast<unsigned char>(idChar)))
410 {
411 phosphor::logging::log<phosphor::logging::level::ERR>(
412 "BIOS ID contains non printable character");
413 return IPMI_CC_INVALID_FIELD_REQUEST;
414 }
415 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800416
Vernon Mauery15419dd2019-05-24 09:40:30 -0700417 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Chalapathi899bfd12020-04-15 15:07:02 +0000418 std::string service = getService(*dbus, biosVersionIntf, biosActiveObjPath);
419 setDbusProperty(*dbus, service, biosActiveObjPath, biosVersionIntf,
Yong Li2742b852019-12-16 14:55:11 +0800420 biosVersionProp, idString);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800421 uint8_t* bytesWritten = static_cast<uint8_t*>(response);
422 *bytesWritten =
Jason M. Bills64796042018-10-03 16:51:55 -0700423 data->biosIDLength; // how many bytes are written into storage
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800424 *dataLen = 1;
425 return IPMI_CC_OK;
426}
427
Jayaprakash Mutyala90da3d92021-11-18 22:01:22 +0000428bool getActiveHSCSoftwareVersionInfo(std::string& hscVersion, size_t hscNumber)
429{
430 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
431 try
432 {
433 std::string hsbpObjPath =
434 "/xyz/openbmc_project/software/HSBP_" + std::to_string(hscNumber);
435 auto service = getService(*dbus, biosVersionIntf, hsbpObjPath);
436 Value hscVersionValue =
437 getDbusProperty(*dbus, "xyz.openbmc_project.HsbpManager",
438 hsbpObjPath, biosVersionIntf, "Version");
439 hscVersion = std::get<std::string>(hscVersionValue);
440 }
441 catch (const sdbusplus::exception::exception& e)
442 {
443 phosphor::logging::log<phosphor::logging::level::INFO>(
444 "Failed to retrieve HSBP version information",
445 phosphor::logging::entry("HSBP Number=%d", hscNumber));
446 return false;
447 }
448 return true;
449}
450
451bool getHscVerInfo(ipmi::Context::ptr ctx, uint8_t& hsc0Major,
452 uint8_t& hsc0Minor, uint8_t& hsc1Major, uint8_t& hsc1Minor,
453 uint8_t& hsc2Major, uint8_t& hsc2Minor)
454{
455 std::string hscVersion;
456 std::array<uint8_t, 6> hscVersions{0};
457
458 for (size_t hscNumber = 1; hscNumber <= 3; hscNumber++)
459 {
460 if (!getActiveHSCSoftwareVersionInfo(hscVersion, hscNumber))
461 {
462 continue;
463 }
464 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?)");
465 constexpr size_t matchedPhosphor = 4;
466 std::smatch results;
467 // hscVersion = BOOT_VER.FPGA_VER.SECURITY_REVISION (Example: 00.02.01)
468 if (std::regex_match(hscVersion, results, pattern1))
469 {
470 // Major version is FPGA_VER and Minor version is SECURITY_REV
471 if (results.size() == matchedPhosphor)
472 {
473 int index = (hscNumber - 1) * 2;
474 hscVersions[index] =
475 static_cast<uint8_t>(std::stoi(results[2]));
476 hscVersions[index + 1] =
477 static_cast<uint8_t>(std::stoi(results[3]));
478 }
479 }
480 }
481 hsc0Major = hscVersions[0];
482 hsc0Minor = hscVersions[1];
483 hsc1Major = hscVersions[2];
484 hsc1Minor = hscVersions[3];
485 hsc2Major = hscVersions[4];
486 hsc2Minor = hscVersions[5];
487 return true;
488}
489
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530490bool getSwVerInfo(ipmi::Context::ptr ctx, uint8_t& bmcMajor, uint8_t& bmcMinor,
491 uint8_t& meMajor, uint8_t& meMinor)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800492{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800493 // step 1 : get BMC Major and Minor numbers from its DBUS property
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530494 std::string bmcVersion;
495 if (getActiveSoftwareVersionInfo(ctx, versionPurposeBMC, bmcVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800496 {
497 return false;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800498 }
499
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530500 std::optional<MetaRevision> rev = convertIntelVersion(bmcVersion);
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800501 if (rev.has_value())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800502 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800503 MetaRevision revision = rev.value();
504 bmcMajor = revision.major;
505
506 revision.minor = (revision.minor > 99 ? 99 : revision.minor);
507 bmcMinor = revision.minor % 10 + (revision.minor / 10) * 16;
508 }
509
510 // step 2 : get ME Major and Minor numbers from its DBUS property
AppaRao Puli32825a22020-01-17 15:52:41 +0530511 std::string meVersion;
512 if (getActiveSoftwareVersionInfo(ctx, versionPurposeME, meVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800513 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800514 return false;
515 }
AppaRao Puli32825a22020-01-17 15:52:41 +0530516 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?).(\\d+?).(\\d+?)");
517 constexpr size_t matchedPhosphor = 6;
518 std::smatch results;
519 if (std::regex_match(meVersion, results, pattern1))
520 {
521 if (results.size() == matchedPhosphor)
522 {
523 meMajor = static_cast<uint8_t>(std::stoi(results[1]));
Jayaprakash Mutyalad0657022021-08-26 21:18:08 +0000524 meMinor = static_cast<uint8_t>(std::stoi(results[2]) << 4 |
525 std::stoi(results[3]));
AppaRao Puli32825a22020-01-17 15:52:41 +0530526 }
527 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800528 return true;
529}
530
531ipmi::RspType<
532 std::variant<std::string,
533 std::tuple<uint8_t, std::array<uint8_t, 2>,
534 std::array<uint8_t, 2>, std::array<uint8_t, 2>,
535 std::array<uint8_t, 2>, std::array<uint8_t, 2>>,
536 std::tuple<uint8_t, std::array<uint8_t, 2>>>>
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530537 ipmiOEMGetDeviceInfo(ipmi::Context::ptr ctx, uint8_t entityType,
538 std::optional<uint8_t> countToRead,
AppaRao Pulid46cb422020-01-21 18:40:21 +0530539 std::optional<uint8_t> offset)
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800540{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800541 if (entityType > static_cast<uint8_t>(OEMDevEntityType::sdrVer))
542 {
543 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800544 }
545
546 // handle OEM command items
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800547 switch (OEMDevEntityType(entityType))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800548 {
549 case OEMDevEntityType::biosId:
550 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530551 // Byte 2&3, Only used with selecting BIOS
552 if (!countToRead || !offset)
553 {
554 return ipmi::responseReqDataLenInvalid();
555 }
556
Vernon Mauery15419dd2019-05-24 09:40:30 -0700557 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800558 std::string service =
Chalapathi899bfd12020-04-15 15:07:02 +0000559 getService(*dbus, biosVersionIntf, biosActiveObjPath);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800560 try
561 {
Yong Li2742b852019-12-16 14:55:11 +0800562 Value variant =
Chalapathi899bfd12020-04-15 15:07:02 +0000563 getDbusProperty(*dbus, service, biosActiveObjPath,
Yong Li2742b852019-12-16 14:55:11 +0800564 biosVersionIntf, biosVersionProp);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700565 std::string& idString = std::get<std::string>(variant);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530566 if (*offset >= idString.size())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800567 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800568 return ipmi::responseParmOutOfRange();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800569 }
Jason M. Bills64796042018-10-03 16:51:55 -0700570 size_t length = 0;
AppaRao Pulid46cb422020-01-21 18:40:21 +0530571 if (*countToRead > (idString.size() - *offset))
Jason M. Bills64796042018-10-03 16:51:55 -0700572 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530573 length = idString.size() - *offset;
Jason M. Bills64796042018-10-03 16:51:55 -0700574 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800575 else
576 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530577 length = *countToRead;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800578 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800579
580 std::string readBuf = {0};
581 readBuf.resize(length);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530582 std::copy_n(idString.begin() + *offset, length,
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800583 (readBuf.begin()));
584 return ipmi::responseSuccess(readBuf);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800585 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500586 catch (const std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800587 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800588 return ipmi::responseUnspecifiedError();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800589 }
590 }
591 break;
592
593 case OEMDevEntityType::devVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800594 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530595 // Byte 2&3, Only used with selecting BIOS
596 if (countToRead || offset)
597 {
598 return ipmi::responseReqDataLenInvalid();
599 }
600
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800601 constexpr const size_t verLen = 2;
602 constexpr const size_t verTotalLen = 10;
603 std::array<uint8_t, verLen> bmcBuf = {0xff, 0xff};
604 std::array<uint8_t, verLen> hsc0Buf = {0xff, 0xff};
605 std::array<uint8_t, verLen> hsc1Buf = {0xff, 0xff};
606 std::array<uint8_t, verLen> meBuf = {0xff, 0xff};
607 std::array<uint8_t, verLen> hsc2Buf = {0xff, 0xff};
608 // data0/1: BMC version number; data6/7: ME version number
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530609 if (!getSwVerInfo(ctx, bmcBuf[0], bmcBuf[1], meBuf[0], meBuf[1]))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800610 {
611 return ipmi::responseUnspecifiedError();
612 }
Jayaprakash Mutyala90da3d92021-11-18 22:01:22 +0000613 if (!getHscVerInfo(ctx, hsc0Buf[0], hsc0Buf[1], hsc1Buf[0],
614 hsc1Buf[1], hsc2Buf[0], hsc2Buf[1]))
615 {
616 return ipmi::responseUnspecifiedError();
617 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800618 return ipmi::responseSuccess(
619 std::tuple<
620 uint8_t, std::array<uint8_t, verLen>,
621 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>,
622 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>>{
623 verTotalLen, bmcBuf, hsc0Buf, hsc1Buf, meBuf, hsc2Buf});
624 }
625 break;
626
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800627 case OEMDevEntityType::sdrVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800628 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530629 // Byte 2&3, Only used with selecting BIOS
630 if (countToRead || offset)
631 {
632 return ipmi::responseReqDataLenInvalid();
633 }
634
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800635 constexpr const size_t sdrLen = 2;
636 std::array<uint8_t, sdrLen> readBuf = {0x01, 0x0};
637 return ipmi::responseSuccess(
638 std::tuple<uint8_t, std::array<uint8_t, sdrLen>>{sdrLen,
639 readBuf});
640 }
641 break;
642
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800643 default:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800644 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800645 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800646}
647
648ipmi_ret_t ipmiOEMGetAICFRU(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
649 ipmi_request_t request, ipmi_response_t response,
650 ipmi_data_len_t dataLen, ipmi_context_t context)
651{
652 if (*dataLen != 0)
653 {
Jason M. Bills64796042018-10-03 16:51:55 -0700654 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800655 return IPMI_CC_REQ_DATA_LEN_INVALID;
656 }
657
658 *dataLen = 1;
659 uint8_t* res = reinterpret_cast<uint8_t*>(response);
660 // temporary fix. We don't support AIC FRU now. Just tell BIOS that no
661 // AIC is available so that BIOS will not timeout repeatly which leads to
662 // slow booting.
663 *res = 0; // Byte1=Count of SlotPosition/FruID records.
664 return IPMI_CC_OK;
665}
666
Jason M. Bills64796042018-10-03 16:51:55 -0700667ipmi_ret_t ipmiOEMGetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
668 ipmi_request_t request,
669 ipmi_response_t response,
670 ipmi_data_len_t dataLen,
671 ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800672{
Jason M. Bills64796042018-10-03 16:51:55 -0700673 GetPowerRestoreDelayRes* resp =
674 reinterpret_cast<GetPowerRestoreDelayRes*>(response);
675
676 if (*dataLen != 0)
677 {
678 *dataLen = 0;
679 return IPMI_CC_REQ_DATA_LEN_INVALID;
680 }
681
Vernon Mauery15419dd2019-05-24 09:40:30 -0700682 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700683 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700684 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
Jason M. Bills64796042018-10-03 16:51:55 -0700685 Value variant =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700686 getDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700687 powerRestoreDelayIntf, powerRestoreDelayProp);
688
Andrei Kartashevc42c7ed2022-01-10 12:17:34 +0300689 uint64_t val = std::get<uint64_t>(variant);
690 val /= 1000000UL;
691 uint16_t delay = val;
Jason M. Bills64796042018-10-03 16:51:55 -0700692 resp->byteLSB = delay;
693 resp->byteMSB = delay >> 8;
694
695 *dataLen = sizeof(GetPowerRestoreDelayRes);
696
697 return IPMI_CC_OK;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800698}
699
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800700static uint8_t bcdToDec(uint8_t val)
701{
702 return ((val / 16 * 10) + (val % 16));
703}
704
705// Allows an update utility or system BIOS to send the status of an embedded
706// firmware update attempt to the BMC. After received, BMC will create a logging
707// record.
708ipmi::RspType<> ipmiOEMSendEmbeddedFwUpdStatus(uint8_t status, uint8_t target,
709 uint8_t majorRevision,
710 uint8_t minorRevision,
711 uint32_t auxInfo)
712{
713 std::string firmware;
Jason M. Billsdc249272019-04-03 09:58:40 -0700714 int instance = (target & targetInstanceMask) >> targetInstanceShift;
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800715 target = (target & selEvtTargetMask) >> selEvtTargetShift;
716
717 /* make sure the status is 0, 1, or 2 as per the spec */
718 if (status > 2)
719 {
720 return ipmi::response(ipmi::ccInvalidFieldRequest);
721 }
Jason M. Billsdc249272019-04-03 09:58:40 -0700722 /* make sure the target is 0, 1, 2, or 4 as per the spec */
723 if (target > 4 || target == 3)
724 {
725 return ipmi::response(ipmi::ccInvalidFieldRequest);
726 }
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800727 /*orignal OEM command is to record OEM SEL.
728 But openbmc does not support OEM SEL, so we redirect it to redfish event
729 logging. */
730 std::string buildInfo;
731 std::string action;
732 switch (FWUpdateTarget(target))
733 {
734 case FWUpdateTarget::targetBMC:
735 firmware = "BMC";
Jason M. Billsdc249272019-04-03 09:58:40 -0700736 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800737 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
738 " BuildID: " + std::to_string(auxInfo);
739 buildInfo += std::to_string(auxInfo);
740 break;
741 case FWUpdateTarget::targetBIOS:
742 firmware = "BIOS";
743 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700744 "major: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800745 std::to_string(bcdToDec(majorRevision)) + // BCD encoded
746 " minor: " +
747 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
748 " ReleaseNumber: " + // ASCII encoded
749 std::to_string(static_cast<uint8_t>(auxInfo >> 0) - '0') +
750 std::to_string(static_cast<uint8_t>(auxInfo >> 8) - '0') +
751 std::to_string(static_cast<uint8_t>(auxInfo >> 16) - '0') +
752 std::to_string(static_cast<uint8_t>(auxInfo >> 24) - '0');
753 break;
754 case FWUpdateTarget::targetME:
755 firmware = "ME";
756 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700757 "major: " + std::to_string(majorRevision) + " minor1: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800758 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
759 " minor2: " +
760 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 0))) +
761 " build1: " +
762 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 8))) +
763 " build2: " +
764 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 16)));
765 break;
766 case FWUpdateTarget::targetOEMEWS:
767 firmware = "EWS";
Jason M. Billsdc249272019-04-03 09:58:40 -0700768 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800769 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
770 " BuildID: " + std::to_string(auxInfo);
771 break;
772 }
773
Jason M. Billsdc249272019-04-03 09:58:40 -0700774 static const std::string openBMCMessageRegistryVersion("0.1");
775 std::string redfishMsgID = "OpenBMC." + openBMCMessageRegistryVersion;
776
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800777 switch (status)
778 {
779 case 0x0:
780 action = "update started";
Jason M. Billsdc249272019-04-03 09:58:40 -0700781 redfishMsgID += ".FirmwareUpdateStarted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800782 break;
783 case 0x1:
784 action = "update completed successfully";
Jason M. Billsdc249272019-04-03 09:58:40 -0700785 redfishMsgID += ".FirmwareUpdateCompleted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800786 break;
787 case 0x2:
788 action = "update failure";
Jason M. Billsdc249272019-04-03 09:58:40 -0700789 redfishMsgID += ".FirmwareUpdateFailed";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800790 break;
791 default:
792 action = "unknown";
793 break;
794 }
795
Jason M. Billsdc249272019-04-03 09:58:40 -0700796 std::string firmwareInstanceStr =
797 firmware + " instance: " + std::to_string(instance);
798 std::string message("[firmware update] " + firmwareInstanceStr +
799 " status: <" + action + "> " + buildInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800800
801 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", LOG_INFO,
Jason M. Billsdc249272019-04-03 09:58:40 -0700802 "REDFISH_MESSAGE_ID=%s", redfishMsgID.c_str(),
803 "REDFISH_MESSAGE_ARGS=%s,%s", firmwareInstanceStr.c_str(),
804 buildInfo.c_str(), NULL);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800805 return ipmi::responseSuccess();
806}
807
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +0530808ipmi::RspType<uint8_t, std::vector<uint8_t>>
809 ipmiOEMSlotIpmb(ipmi::Context::ptr ctx, uint6_t reserved1,
810 uint2_t slotNumber, uint3_t baseBoardSlotNum,
811 uint3_t riserSlotNum, uint2_t reserved2, uint8_t slaveAddr,
812 uint8_t netFn, uint8_t cmd,
813 std::optional<std::vector<uint8_t>> writeData)
814{
815 if (reserved1 || reserved2)
816 {
817 return ipmi::responseInvalidFieldRequest();
818 }
819
820 boost::system::error_code ec;
821 using ipmbResponse = std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t,
822 std::vector<uint8_t>>;
823 ipmbResponse res = ctx->bus->yield_method_call<ipmbResponse>(
824 ctx->yield, ec, "xyz.openbmc_project.Ipmi.Channel.Ipmb",
825 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
826 "SlotIpmbRequest", static_cast<uint8_t>(slotNumber),
827 static_cast<uint8_t>(baseBoardSlotNum), slaveAddr, netFn, cmd,
828 *writeData);
829 if (ec)
830 {
831 phosphor::logging::log<phosphor::logging::level::ERR>(
832 "Failed to call dbus method SlotIpmbRequest");
833 return ipmi::responseUnspecifiedError();
834 }
835
836 std::vector<uint8_t> dataReceived(0);
837 int status = -1;
838 uint8_t resNetFn = 0, resLun = 0, resCmd = 0, cc = 0;
839
840 std::tie(status, resNetFn, resLun, resCmd, cc, dataReceived) = res;
841
842 if (status)
843 {
844 phosphor::logging::log<phosphor::logging::level::ERR>(
845 "Failed to get response from SlotIpmbRequest");
846 return ipmi::responseResponseError();
847 }
848 return ipmi::responseSuccess(cc, dataReceived);
849}
850
Jason M. Bills64796042018-10-03 16:51:55 -0700851ipmi_ret_t ipmiOEMSetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
852 ipmi_request_t request,
853 ipmi_response_t response,
854 ipmi_data_len_t dataLen,
855 ipmi_context_t context)
856{
857 SetPowerRestoreDelayReq* data =
858 reinterpret_cast<SetPowerRestoreDelayReq*>(request);
859 uint16_t delay = 0;
860
861 if (*dataLen != sizeof(SetPowerRestoreDelayReq))
862 {
863 *dataLen = 0;
864 return IPMI_CC_REQ_DATA_LEN_INVALID;
865 }
866 delay = data->byteMSB;
867 delay = (delay << 8) | data->byteLSB;
Andrei Kartashevc42c7ed2022-01-10 12:17:34 +0300868 uint64_t val = delay * 1000000;
Vernon Mauery15419dd2019-05-24 09:40:30 -0700869 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700870 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700871 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
872 setDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Andrei Kartashevc42c7ed2022-01-10 12:17:34 +0300873 powerRestoreDelayIntf, powerRestoreDelayProp, val);
Jason M. Bills64796042018-10-03 16:51:55 -0700874 *dataLen = 0;
875
876 return IPMI_CC_OK;
877}
878
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700879static bool cpuPresent(const std::string& cpuName)
Jason M. Bills64796042018-10-03 16:51:55 -0700880{
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700881 static constexpr const char* cpuPresencePathPrefix =
882 "/xyz/openbmc_project/inventory/system/chassis/motherboard/";
883 static constexpr const char* cpuPresenceIntf =
884 "xyz.openbmc_project.Inventory.Item";
885 std::string cpuPresencePath = cpuPresencePathPrefix + cpuName;
886 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
887 try
Jason M. Bills64796042018-10-03 16:51:55 -0700888 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700889 auto service =
890 ipmi::getService(*busp, cpuPresenceIntf, cpuPresencePath);
891
892 ipmi::Value result = ipmi::getDbusProperty(
893 *busp, service, cpuPresencePath, cpuPresenceIntf, "Present");
894 return std::get<bool>(result);
895 }
896 catch (const std::exception& e)
897 {
898 phosphor::logging::log<phosphor::logging::level::INFO>(
899 "Cannot find processor presence",
900 phosphor::logging::entry("NAME=%s", cpuName.c_str()));
901 return false;
902 }
903}
904
905ipmi::RspType<bool, // CATERR Reset Enabled
906 bool, // ERR2 Reset Enabled
907 uint6_t, // reserved
908 uint8_t, // reserved, returns 0x3F
909 uint6_t, // CPU1 CATERR Count
910 uint2_t, // CPU1 Status
911 uint6_t, // CPU2 CATERR Count
912 uint2_t, // CPU2 Status
913 uint6_t, // CPU3 CATERR Count
914 uint2_t, // CPU3 Status
915 uint6_t, // CPU4 CATERR Count
916 uint2_t, // CPU4 Status
917 uint8_t // Crashdump Count
918 >
919 ipmiOEMGetProcessorErrConfig()
920{
921 bool resetOnCATERR = false;
922 bool resetOnERR2 = false;
923 uint6_t cpu1CATERRCount = 0;
924 uint6_t cpu2CATERRCount = 0;
925 uint6_t cpu3CATERRCount = 0;
926 uint6_t cpu4CATERRCount = 0;
927 uint8_t crashdumpCount = 0;
Jason M. Bills24df90f2021-06-15 12:46:13 -0700928 uint2_t cpu1Status = cpuPresent("CPU_1")
929 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
930 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
931 uint2_t cpu2Status = cpuPresent("CPU_2")
932 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
933 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
934 uint2_t cpu3Status = cpuPresent("CPU_3")
935 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
936 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
937 uint2_t cpu4Status = cpuPresent("CPU_4")
938 ? types::enum_cast<uint8_t>(CPUStatus::enabled)
939 : types::enum_cast<uint8_t>(CPUStatus::notPresent);
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700940
941 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
942 try
943 {
944 auto service = ipmi::getService(*busp, processorErrConfigIntf,
945 processorErrConfigObjPath);
946
947 ipmi::PropertyMap result = ipmi::getAllDbusProperties(
948 *busp, service, processorErrConfigObjPath, processorErrConfigIntf);
949 resetOnCATERR = std::get<bool>(result.at("ResetOnCATERR"));
950 resetOnERR2 = std::get<bool>(result.at("ResetOnERR2"));
951 cpu1CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU1"));
952 cpu2CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU2"));
953 cpu3CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU3"));
954 cpu4CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU4"));
955 crashdumpCount = std::get<uint8_t>(result.at("CrashdumpCount"));
956 }
957 catch (const std::exception& e)
958 {
959 phosphor::logging::log<phosphor::logging::level::ERR>(
960 "Failed to fetch processor error config",
961 phosphor::logging::entry("ERROR=%s", e.what()));
962 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700963 }
964
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700965 return ipmi::responseSuccess(resetOnCATERR, resetOnERR2, 0, 0x3F,
966 cpu1CATERRCount, cpu1Status, cpu2CATERRCount,
967 cpu2Status, cpu3CATERRCount, cpu3Status,
968 cpu4CATERRCount, cpu4Status, crashdumpCount);
969}
Jason M. Bills64796042018-10-03 16:51:55 -0700970
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700971ipmi::RspType<> ipmiOEMSetProcessorErrConfig(
972 bool resetOnCATERR, bool resetOnERR2, uint6_t reserved1, uint8_t reserved2,
973 std::optional<bool> clearCPUErrorCount,
974 std::optional<bool> clearCrashdumpCount, std::optional<uint6_t> reserved3)
975{
Jayaprakash Mutyala0a652fa2021-07-01 17:09:39 +0000976 if (reserved1 || reserved2)
977 {
978 return ipmi::responseInvalidFieldRequest();
979 }
980
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700981 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700982
983 try
984 {
Jayaprakash Mutyala0a652fa2021-07-01 17:09:39 +0000985 if (reserved3.value_or(0))
986 {
987 return ipmi::responseInvalidFieldRequest();
988 }
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700989 auto service = ipmi::getService(*busp, processorErrConfigIntf,
990 processorErrConfigObjPath);
991 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
992 processorErrConfigIntf, "ResetOnCATERR",
993 resetOnCATERR);
994 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
995 processorErrConfigIntf, "ResetOnERR2",
996 resetOnERR2);
997 if (clearCPUErrorCount.value_or(false))
998 {
999 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -07001000 processorErrConfigIntf, "ErrorCountCPU1",
1001 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001002 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -07001003 processorErrConfigIntf, "ErrorCountCPU2",
1004 static_cast<uint8_t>(0));
1005 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
1006 processorErrConfigIntf, "ErrorCountCPU3",
1007 static_cast<uint8_t>(0));
1008 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
1009 processorErrConfigIntf, "ErrorCountCPU4",
1010 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001011 }
1012 if (clearCrashdumpCount.value_or(false))
1013 {
1014 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -07001015 processorErrConfigIntf, "CrashdumpCount",
1016 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001017 }
Jason M. Bills64796042018-10-03 16:51:55 -07001018 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001019 catch (const std::exception& e)
Jason M. Bills64796042018-10-03 16:51:55 -07001020 {
Kuiying Wangbc546672018-11-23 15:41:05 +08001021 phosphor::logging::log<phosphor::logging::level::ERR>(
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001022 "Failed to set processor error config",
1023 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1024 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -07001025 }
1026
Jason M. Bills42bd9c82019-06-28 16:39:34 -07001027 return ipmi::responseSuccess();
Jason M. Bills64796042018-10-03 16:51:55 -07001028}
1029
Yong Li703922d2018-11-06 13:25:31 +08001030ipmi_ret_t ipmiOEMGetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1031 ipmi_request_t request,
1032 ipmi_response_t response,
1033 ipmi_data_len_t dataLen,
1034 ipmi_context_t context)
1035{
1036 GetOEMShutdownPolicyRes* resp =
1037 reinterpret_cast<GetOEMShutdownPolicyRes*>(response);
1038
1039 if (*dataLen != 0)
1040 {
1041 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wang45f04982018-12-26 09:23:08 +08001042 "oem_get_shutdown_policy: invalid input len!");
Yong Li703922d2018-11-06 13:25:31 +08001043 *dataLen = 0;
1044 return IPMI_CC_REQ_DATA_LEN_INVALID;
1045 }
1046
1047 *dataLen = 0;
1048
1049 try
1050 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001051 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +08001052 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001053 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
1054 Value variant = getDbusProperty(
1055 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
1056 oemShutdownPolicyObjPathProp);
Yong Li0669d192019-05-06 14:01:46 +08001057
1058 if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1059 convertPolicyFromString(std::get<std::string>(variant)) ==
1060 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
1061 NoShutdownOnOCOT)
1062 {
1063 resp->policy = 0;
1064 }
1065 else if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1066 convertPolicyFromString(std::get<std::string>(variant)) ==
1067 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1068 Policy::ShutdownOnOCOT)
1069 {
1070 resp->policy = 1;
1071 }
1072 else
1073 {
1074 phosphor::logging::log<phosphor::logging::level::ERR>(
1075 "oem_set_shutdown_policy: invalid property!",
1076 phosphor::logging::entry(
1077 "PROP=%s", std::get<std::string>(variant).c_str()));
1078 return IPMI_CC_UNSPECIFIED_ERROR;
1079 }
Yong Li703922d2018-11-06 13:25:31 +08001080 // TODO needs to check if it is multi-node products,
1081 // policy is only supported on node 3/4
1082 resp->policySupport = shutdownPolicySupported;
1083 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001084 catch (const sdbusplus::exception_t& e)
Yong Li703922d2018-11-06 13:25:31 +08001085 {
1086 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1087 return IPMI_CC_UNSPECIFIED_ERROR;
1088 }
1089
1090 *dataLen = sizeof(GetOEMShutdownPolicyRes);
1091 return IPMI_CC_OK;
1092}
1093
1094ipmi_ret_t ipmiOEMSetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1095 ipmi_request_t request,
1096 ipmi_response_t response,
1097 ipmi_data_len_t dataLen,
1098 ipmi_context_t context)
1099{
1100 uint8_t* req = reinterpret_cast<uint8_t*>(request);
Yong Li0669d192019-05-06 14:01:46 +08001101 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy policy =
1102 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
1103 NoShutdownOnOCOT;
Yong Li703922d2018-11-06 13:25:31 +08001104
1105 // TODO needs to check if it is multi-node products,
1106 // policy is only supported on node 3/4
1107 if (*dataLen != 1)
1108 {
1109 phosphor::logging::log<phosphor::logging::level::ERR>(
1110 "oem_set_shutdown_policy: invalid input len!");
1111 *dataLen = 0;
1112 return IPMI_CC_REQ_DATA_LEN_INVALID;
1113 }
1114
1115 *dataLen = 0;
1116 if ((*req != noShutdownOnOCOT) && (*req != shutdownOnOCOT))
1117 {
1118 phosphor::logging::log<phosphor::logging::level::ERR>(
1119 "oem_set_shutdown_policy: invalid input!");
1120 return IPMI_CC_INVALID_FIELD_REQUEST;
1121 }
1122
Yong Li0669d192019-05-06 14:01:46 +08001123 if (*req == noShutdownOnOCOT)
1124 {
1125 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1126 Policy::NoShutdownOnOCOT;
1127 }
1128 else
1129 {
1130 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
1131 Policy::ShutdownOnOCOT;
1132 }
1133
Yong Li703922d2018-11-06 13:25:31 +08001134 try
1135 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001136 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +08001137 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001138 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
Yong Li0669d192019-05-06 14:01:46 +08001139 setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07001140 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
Yong Li0669d192019-05-06 14:01:46 +08001141 oemShutdownPolicyObjPathProp,
1142 sdbusplus::com::intel::Control::server::convertForMessage(policy));
Yong Li703922d2018-11-06 13:25:31 +08001143 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001144 catch (const sdbusplus::exception_t& e)
Yong Li703922d2018-11-06 13:25:31 +08001145 {
1146 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1147 return IPMI_CC_UNSPECIFIED_ERROR;
1148 }
1149
1150 return IPMI_CC_OK;
1151}
1152
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301153/** @brief implementation for check the DHCP or not in IPv4
1154 * @param[in] Channel - Channel number
1155 * @returns true or false.
1156 */
1157static bool isDHCPEnabled(uint8_t Channel)
1158{
1159 try
1160 {
1161 auto ethdevice = getChannelName(Channel);
1162 if (ethdevice.empty())
1163 {
1164 return false;
1165 }
1166 auto ethIP = ethdevice + "/ipv4";
Vernon Mauery15419dd2019-05-24 09:40:30 -07001167 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301168 auto ethernetObj =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001169 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
1170 auto value = getDbusProperty(*dbus, networkService, ethernetObj.first,
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301171 networkIPIntf, "Origin");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001172 if (std::get<std::string>(value) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301173 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1174 {
1175 return true;
1176 }
1177 else
1178 {
1179 return false;
1180 }
1181 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001182 catch (const sdbusplus::exception_t& e)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301183 {
1184 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1185 return true;
1186 }
1187}
1188
1189/** @brief implementes for check the DHCP or not in IPv6
1190 * @param[in] Channel - Channel number
1191 * @returns true or false.
1192 */
1193static bool isDHCPIPv6Enabled(uint8_t Channel)
1194{
1195
1196 try
1197 {
1198 auto ethdevice = getChannelName(Channel);
1199 if (ethdevice.empty())
1200 {
1201 return false;
1202 }
1203 auto ethIP = ethdevice + "/ipv6";
Vernon Mauery15419dd2019-05-24 09:40:30 -07001204 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301205 auto objectInfo =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001206 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
1207 auto properties = getAllDbusProperties(*dbus, objectInfo.second,
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301208 objectInfo.first, networkIPIntf);
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001209 if (std::get<std::string>(properties["Origin"]) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301210 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1211 {
1212 return true;
1213 }
1214 else
1215 {
1216 return false;
1217 }
1218 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001219 catch (const sdbusplus::exception_t& e)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301220 {
1221 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1222 return true;
1223 }
1224}
1225
1226/** @brief implementes the creating of default new user
1227 * @param[in] userName - new username in 16 bytes.
1228 * @param[in] userPassword - new password in 20 bytes
1229 * @returns ipmi completion code.
1230 */
1231ipmi::RspType<> ipmiOEMSetUser2Activation(
1232 std::array<uint8_t, ipmi::ipmiMaxUserName>& userName,
Vernon Mauery3b3d29b2021-08-05 15:03:35 -07001233 const SecureBuffer& userPassword)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301234{
Vernon Mauery3b3d29b2021-08-05 15:03:35 -07001235 if (userPassword.size() != ipmi::maxIpmi20PasswordSize)
1236 {
1237 return ipmi::responseReqDataLenInvalid();
1238 }
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301239 bool userState = false;
1240 // Check for System Interface not exist and LAN should be static
1241 for (uint8_t channel = 0; channel < maxIpmiChannels; channel++)
1242 {
Manish Baing440f62b2021-07-15 22:00:37 +00001243 ChannelInfo chInfo{};
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301244 try
1245 {
1246 getChannelInfo(channel, chInfo);
1247 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001248 catch (const sdbusplus::exception_t& e)
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301249 {
1250 phosphor::logging::log<phosphor::logging::level::ERR>(
1251 "ipmiOEMSetUser2Activation: Failed to get Channel Info",
1252 phosphor::logging::entry("MSG: %s", e.description()));
1253 return ipmi::response(ipmi::ccUnspecifiedError);
1254 }
1255 if (chInfo.mediumType ==
1256 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1257 {
1258 phosphor::logging::log<phosphor::logging::level::ERR>(
1259 "ipmiOEMSetUser2Activation: system interface exist .");
1260 return ipmi::response(ipmi::ccCommandNotAvailable);
1261 }
1262 else
1263 {
1264
1265 if (chInfo.mediumType ==
1266 static_cast<uint8_t>(EChannelMediumType::lan8032))
1267 {
1268 if (isDHCPIPv6Enabled(channel) || isDHCPEnabled(channel))
1269 {
1270 phosphor::logging::log<phosphor::logging::level::ERR>(
1271 "ipmiOEMSetUser2Activation: DHCP enabled .");
1272 return ipmi::response(ipmi::ccCommandNotAvailable);
1273 }
1274 }
1275 }
1276 }
1277 uint8_t maxChUsers = 0, enabledUsers = 0, fixedUsers = 0;
1278 if (ipmi::ccSuccess ==
1279 ipmiUserGetAllCounts(maxChUsers, enabledUsers, fixedUsers))
1280 {
1281 if (enabledUsers > 1)
1282 {
1283 phosphor::logging::log<phosphor::logging::level::ERR>(
1284 "ipmiOEMSetUser2Activation: more than one user is enabled.");
1285 return ipmi::response(ipmi::ccCommandNotAvailable);
1286 }
1287 // Check the user 2 is enabled or not
1288 ipmiUserCheckEnabled(ipmiDefaultUserId, userState);
1289 if (userState == true)
1290 {
1291 phosphor::logging::log<phosphor::logging::level::ERR>(
1292 "ipmiOEMSetUser2Activation: user 2 already enabled .");
1293 return ipmi::response(ipmi::ccCommandNotAvailable);
1294 }
1295 }
1296 else
1297 {
1298 return ipmi::response(ipmi::ccUnspecifiedError);
1299 }
1300
1301#if BYTE_ORDER == LITTLE_ENDIAN
1302 PrivAccess privAccess = {PRIVILEGE_ADMIN, true, true, true, 0};
1303#endif
1304#if BYTE_ORDER == BIG_ENDIAN
1305 PrivAccess privAccess = {0, true, true, true, PRIVILEGE_ADMIN};
1306#endif
1307
Vernon Mauery037cabd2020-05-14 12:16:01 -07001308 // ipmiUserSetUserName correctly handles char*, possibly non-null
1309 // terminated strings using ipmiMaxUserName size
Jayaprakash Mutyala3fbe8d22020-10-29 14:42:59 +00001310 size_t nameLen = strnlen(reinterpret_cast<const char*>(userName.data()),
1311 sizeof(userName));
1312 const std::string userNameRaw(
1313 reinterpret_cast<const char*>(userName.data()), nameLen);
jayaprakash Mutyala1429d4f2020-03-04 18:20:16 +00001314
Vernon Mauery037cabd2020-05-14 12:16:01 -07001315 if (ipmi::ccSuccess == ipmiUserSetUserName(ipmiDefaultUserId, userNameRaw))
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301316 {
1317 if (ipmi::ccSuccess ==
1318 ipmiUserSetUserPassword(
1319 ipmiDefaultUserId,
1320 reinterpret_cast<const char*>(userPassword.data())))
1321 {
1322 if (ipmi::ccSuccess ==
1323 ipmiUserSetPrivilegeAccess(
1324 ipmiDefaultUserId,
1325 static_cast<uint8_t>(ipmi::EChannelID::chanLan1),
1326 privAccess, true))
1327 {
1328 phosphor::logging::log<phosphor::logging::level::INFO>(
1329 "ipmiOEMSetUser2Activation: user created successfully ");
Jayaprakash Mutyala94204162020-10-23 06:17:56 +00001330
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301331 return ipmi::responseSuccess();
1332 }
1333 }
1334 // we need to delete the default user id which added in this command as
1335 // password / priv setting is failed.
Jayaprakash Mutyala3fbe8d22020-10-29 14:42:59 +00001336 ipmiUserSetUserName(ipmiDefaultUserId, static_cast<std::string>(""));
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301337 phosphor::logging::log<phosphor::logging::level::ERR>(
1338 "ipmiOEMSetUser2Activation: password / priv setting is failed.");
1339 }
1340 else
1341 {
1342 phosphor::logging::log<phosphor::logging::level::ERR>(
1343 "ipmiOEMSetUser2Activation: Setting username failed.");
1344 }
1345
1346 return ipmi::response(ipmi::ccCommandNotAvailable);
1347}
1348
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301349/** @brief implementes executing the linux command
1350 * @param[in] linux command
1351 * @returns status
1352 */
1353
1354static uint8_t executeCmd(const char* path)
1355{
1356 boost::process::child execProg(path);
1357 execProg.wait();
1358
1359 int retCode = execProg.exit_code();
1360 if (retCode)
1361 {
1362 return ipmi::ccUnspecifiedError;
1363 }
1364 return ipmi::ccSuccess;
1365}
1366
1367/** @brief implementes ASD Security event logging
1368 * @param[in] Event message string
1369 * @param[in] Event Severity
1370 * @returns status
1371 */
1372
1373static void atScaleDebugEventlog(std::string msg, int severity)
1374{
1375 std::string eventStr = "OpenBMC.0.1." + msg;
1376 sd_journal_send("MESSAGE=Security Event: %s", eventStr.c_str(),
1377 "PRIORITY=%i", severity, "REDFISH_MESSAGE_ID=%s",
1378 eventStr.c_str(), NULL);
1379}
1380
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301381/** @brief implementes setting password for special user
1382 * @param[in] specialUserIndex
1383 * @param[in] userPassword - new password in 20 bytes
1384 * @returns ipmi completion code.
1385 */
1386ipmi::RspType<> ipmiOEMSetSpecialUserPassword(ipmi::Context::ptr ctx,
1387 uint8_t specialUserIndex,
1388 std::vector<uint8_t> userPassword)
1389{
1390 ChannelInfo chInfo;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301391 ipmi_ret_t status = ipmi::ccSuccess;
1392
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301393 try
1394 {
1395 getChannelInfo(ctx->channel, chInfo);
1396 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001397 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301398 {
1399 phosphor::logging::log<phosphor::logging::level::ERR>(
1400 "ipmiOEMSetSpecialUserPassword: Failed to get Channel Info",
1401 phosphor::logging::entry("MSG: %s", e.description()));
1402 return ipmi::responseUnspecifiedError();
1403 }
1404 if (chInfo.mediumType !=
1405 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1406 {
1407 phosphor::logging::log<phosphor::logging::level::ERR>(
1408 "ipmiOEMSetSpecialUserPassword: Error - supported only in KCS "
1409 "interface");
1410 return ipmi::responseCommandNotAvailable();
1411 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301412
1413 // 0 for root user and 1 for AtScaleDebug is allowed
1414 if (specialUserIndex >
1415 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301416 {
1417 phosphor::logging::log<phosphor::logging::level::ERR>(
1418 "ipmiOEMSetSpecialUserPassword: Invalid user account");
1419 return ipmi::responseParmOutOfRange();
1420 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301421 if (userPassword.size() != 0)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301422 {
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301423 constexpr uint8_t minPasswordSizeRequired = 6;
Patrick Williams23939852021-09-02 11:18:35 -05001424 SecureString passwd;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301425 if (userPassword.size() < minPasswordSizeRequired ||
1426 userPassword.size() > ipmi::maxIpmi20PasswordSize)
1427 {
Jayaprakash Mutyala94204162020-10-23 06:17:56 +00001428 OPENSSL_cleanse(userPassword.data(), userPassword.size());
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301429 return ipmi::responseReqDataLenInvalid();
1430 }
1431 passwd.assign(reinterpret_cast<const char*>(userPassword.data()),
1432 userPassword.size());
Jayaprakash Mutyala94204162020-10-23 06:17:56 +00001433 // Clear sensitive data
1434 OPENSSL_cleanse(userPassword.data(), userPassword.size());
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301435 if (specialUserIndex ==
1436 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
1437 {
1438 status = ipmiSetSpecialUserPassword("asdbg", passwd);
1439
1440 atScaleDebugEventlog("AtScaleDebugSpecialUserEnabled", LOG_CRIT);
1441 }
1442 else
1443 {
1444 status = ipmiSetSpecialUserPassword("root", passwd);
1445 }
1446 return ipmi::response(status);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301447 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301448 else
1449 {
1450 if (specialUserIndex ==
1451 static_cast<uint8_t>(SpecialUserIndex::rootUser))
1452 {
1453 status = executeCmd("passwd -d root");
1454 }
1455 else
1456 {
1457
1458 status = executeCmd("passwd -d asdbg");
1459
1460 if (status == 0)
1461 {
1462 atScaleDebugEventlog("AtScaleDebugSpecialUserDisabled",
1463 LOG_INFO);
1464 }
1465 }
1466 return ipmi::response(status);
1467 }
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301468}
1469
Kuiying Wang45f04982018-12-26 09:23:08 +08001470namespace ledAction
1471{
1472using namespace sdbusplus::xyz::openbmc_project::Led::server;
1473std::map<Physical::Action, uint8_t> actionDbusToIpmi = {
jayaprakash Mutyala934ee9c2019-12-13 17:49:27 +00001474 {Physical::Action::Off, 0},
1475 {Physical::Action::On, 2},
1476 {Physical::Action::Blink, 1}};
Kuiying Wang45f04982018-12-26 09:23:08 +08001477
1478std::map<uint8_t, std::string> offsetObjPath = {
1479 {2, statusAmberObjPath}, {4, statusGreenObjPath}, {6, identifyLEDObjPath}};
1480
1481} // namespace ledAction
1482
1483int8_t getLEDState(sdbusplus::bus::bus& bus, const std::string& intf,
1484 const std::string& objPath, uint8_t& state)
1485{
1486 try
1487 {
1488 std::string service = getService(bus, intf, objPath);
1489 Value stateValue =
1490 getDbusProperty(bus, service, objPath, intf, "State");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001491 std::string strState = std::get<std::string>(stateValue);
Kuiying Wang45f04982018-12-26 09:23:08 +08001492 state = ledAction::actionDbusToIpmi.at(
1493 sdbusplus::xyz::openbmc_project::Led::server::Physical::
1494 convertActionFromString(strState));
1495 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001496 catch (const sdbusplus::exception::exception& e)
Kuiying Wang45f04982018-12-26 09:23:08 +08001497 {
1498 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1499 return -1;
1500 }
1501 return 0;
1502}
1503
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001504ipmi::RspType<uint8_t> ipmiOEMGetLEDStatus()
Kuiying Wang45f04982018-12-26 09:23:08 +08001505{
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001506 uint8_t ledstate = 0;
Kuiying Wang45f04982018-12-26 09:23:08 +08001507 phosphor::logging::log<phosphor::logging::level::DEBUG>("GET led status");
Vernon Mauery15419dd2019-05-24 09:40:30 -07001508 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Kuiying Wang45f04982018-12-26 09:23:08 +08001509 for (auto it = ledAction::offsetObjPath.begin();
1510 it != ledAction::offsetObjPath.end(); ++it)
1511 {
1512 uint8_t state = 0;
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001513 if (getLEDState(*dbus, ledIntf, it->second, state) == -1)
Kuiying Wang45f04982018-12-26 09:23:08 +08001514 {
1515 phosphor::logging::log<phosphor::logging::level::ERR>(
1516 "oem_get_led_status: fail to get ID LED status!");
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001517 return ipmi::responseUnspecifiedError();
Kuiying Wang45f04982018-12-26 09:23:08 +08001518 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001519 ledstate |= state << it->first;
Kuiying Wang45f04982018-12-26 09:23:08 +08001520 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001521 return ipmi::responseSuccess(ledstate);
Kuiying Wang45f04982018-12-26 09:23:08 +08001522}
1523
Yong Li23737fe2019-02-19 08:49:55 +08001524ipmi_ret_t ipmiOEMCfgHostSerialPortSpeed(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1525 ipmi_request_t request,
1526 ipmi_response_t response,
1527 ipmi_data_len_t dataLen,
1528 ipmi_context_t context)
1529{
1530 CfgHostSerialReq* req = reinterpret_cast<CfgHostSerialReq*>(request);
1531 uint8_t* resp = reinterpret_cast<uint8_t*>(response);
1532
1533 if (*dataLen == 0)
1534 {
1535 phosphor::logging::log<phosphor::logging::level::ERR>(
1536 "CfgHostSerial: invalid input len!",
1537 phosphor::logging::entry("LEN=%d", *dataLen));
1538 return IPMI_CC_REQ_DATA_LEN_INVALID;
1539 }
1540
1541 switch (req->command)
1542 {
1543 case getHostSerialCfgCmd:
1544 {
1545 if (*dataLen != 1)
1546 {
1547 phosphor::logging::log<phosphor::logging::level::ERR>(
1548 "CfgHostSerial: invalid input len!");
1549 *dataLen = 0;
1550 return IPMI_CC_REQ_DATA_LEN_INVALID;
1551 }
1552
1553 *dataLen = 0;
1554
1555 boost::process::ipstream is;
1556 std::vector<std::string> data;
1557 std::string line;
1558 boost::process::child c1(fwGetEnvCmd, "-n", fwHostSerailCfgEnvName,
1559 boost::process::std_out > is);
1560
1561 while (c1.running() && std::getline(is, line) && !line.empty())
1562 {
1563 data.push_back(line);
1564 }
1565
1566 c1.wait();
1567 if (c1.exit_code())
1568 {
1569 phosphor::logging::log<phosphor::logging::level::ERR>(
1570 "CfgHostSerial:: error on execute",
1571 phosphor::logging::entry("EXECUTE=%s", fwSetEnvCmd));
1572 // Using the default value
1573 *resp = 0;
1574 }
1575 else
1576 {
1577 if (data.size() != 1)
1578 {
1579 phosphor::logging::log<phosphor::logging::level::ERR>(
1580 "CfgHostSerial:: error on read env");
1581 return IPMI_CC_UNSPECIFIED_ERROR;
1582 }
1583 try
1584 {
1585 unsigned long tmp = std::stoul(data[0]);
1586 if (tmp > std::numeric_limits<uint8_t>::max())
1587 {
1588 throw std::out_of_range("Out of range");
1589 }
1590 *resp = static_cast<uint8_t>(tmp);
1591 }
1592 catch (const std::invalid_argument& e)
1593 {
1594 phosphor::logging::log<phosphor::logging::level::ERR>(
1595 "invalid config ",
1596 phosphor::logging::entry("ERR=%s", e.what()));
1597 return IPMI_CC_UNSPECIFIED_ERROR;
1598 }
1599 catch (const std::out_of_range& e)
1600 {
1601 phosphor::logging::log<phosphor::logging::level::ERR>(
1602 "out_of_range config ",
1603 phosphor::logging::entry("ERR=%s", e.what()));
1604 return IPMI_CC_UNSPECIFIED_ERROR;
1605 }
1606 }
1607
1608 *dataLen = 1;
1609 break;
1610 }
1611 case setHostSerialCfgCmd:
1612 {
1613 if (*dataLen != sizeof(CfgHostSerialReq))
1614 {
1615 phosphor::logging::log<phosphor::logging::level::ERR>(
1616 "CfgHostSerial: invalid input len!");
1617 *dataLen = 0;
1618 return IPMI_CC_REQ_DATA_LEN_INVALID;
1619 }
1620
1621 *dataLen = 0;
1622
1623 if (req->parameter > HostSerialCfgParamMax)
1624 {
1625 phosphor::logging::log<phosphor::logging::level::ERR>(
1626 "CfgHostSerial: invalid input!");
1627 return IPMI_CC_INVALID_FIELD_REQUEST;
1628 }
1629
1630 boost::process::child c1(fwSetEnvCmd, fwHostSerailCfgEnvName,
1631 std::to_string(req->parameter));
1632
1633 c1.wait();
1634 if (c1.exit_code())
1635 {
1636 phosphor::logging::log<phosphor::logging::level::ERR>(
1637 "CfgHostSerial:: error on execute",
1638 phosphor::logging::entry("EXECUTE=%s", fwGetEnvCmd));
1639 return IPMI_CC_UNSPECIFIED_ERROR;
1640 }
1641 break;
1642 }
1643 default:
1644 phosphor::logging::log<phosphor::logging::level::ERR>(
1645 "CfgHostSerial: invalid input!");
1646 *dataLen = 0;
1647 return IPMI_CC_INVALID_FIELD_REQUEST;
1648 }
1649
1650 return IPMI_CC_OK;
1651}
1652
James Feist91244a62019-02-19 15:04:54 -08001653constexpr const char* thermalModeInterface =
1654 "xyz.openbmc_project.Control.ThermalMode";
1655constexpr const char* thermalModePath =
1656 "/xyz/openbmc_project/control/thermal_mode";
1657
1658bool getFanProfileInterface(
1659 sdbusplus::bus::bus& bus,
1660 boost::container::flat_map<
1661 std::string, std::variant<std::vector<std::string>, std::string>>& resp)
1662{
1663 auto call = bus.new_method_call(settingsBusName, thermalModePath, PROP_INTF,
1664 "GetAll");
1665 call.append(thermalModeInterface);
1666 try
1667 {
1668 auto data = bus.call(call);
1669 data.read(resp);
1670 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001671 catch (const sdbusplus::exception_t& e)
James Feist91244a62019-02-19 15:04:54 -08001672 {
1673 phosphor::logging::log<phosphor::logging::level::ERR>(
1674 "getFanProfileInterface: can't get thermal mode!",
1675 phosphor::logging::entry("ERR=%s", e.what()));
1676 return false;
1677 }
1678 return true;
1679}
1680
anil kumar appanaf945eee2019-09-25 23:29:11 +00001681/**@brief implements the OEM set fan config.
1682 * @param selectedFanProfile - fan profile to enable
1683 * @param reserved1
1684 * @param performanceMode - Performance/Acoustic mode
1685 * @param reserved2
1686 * @param setPerformanceMode - set Performance/Acoustic mode
1687 * @param setFanProfile - set fan profile
1688 *
1689 * @return IPMI completion code.
1690 **/
1691ipmi::RspType<> ipmiOEMSetFanConfig(uint8_t selectedFanProfile,
1692
1693 uint2_t reserved1, bool performanceMode,
1694 uint3_t reserved2, bool setPerformanceMode,
Joshi-Mansi619186d2020-01-27 19:16:03 +05301695 bool setFanProfile,
1696 std::optional<uint8_t> dimmGroupId,
1697 std::optional<uint32_t> dimmPresenceBitmap)
James Feist91244a62019-02-19 15:04:54 -08001698{
anil kumar appanaf945eee2019-09-25 23:29:11 +00001699 if (reserved1 || reserved2)
James Feist91244a62019-02-19 15:04:54 -08001700 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001701 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001702 }
Joshi-Mansi619186d2020-01-27 19:16:03 +05301703
1704 if (dimmGroupId)
1705 {
1706 if (*dimmGroupId >= maxCPUNum)
1707 {
1708 return ipmi::responseInvalidFieldRequest();
1709 }
1710 if (!cpuPresent("CPU_" + std::to_string(*dimmGroupId + 1)))
1711 {
1712 return ipmi::responseInvalidFieldRequest();
1713 }
1714 }
1715
James Feist91244a62019-02-19 15:04:54 -08001716 // todo: tell bios to only send first 2 bytes
James Feist91244a62019-02-19 15:04:54 -08001717 boost::container::flat_map<
1718 std::string, std::variant<std::vector<std::string>, std::string>>
1719 profileData;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001720 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1721 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001722 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001723 return ipmi::responseUnspecifiedError();
James Feist91244a62019-02-19 15:04:54 -08001724 }
1725
1726 std::vector<std::string>* supported =
1727 std::get_if<std::vector<std::string>>(&profileData["Supported"]);
1728 if (supported == nullptr)
1729 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001730 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001731 }
1732 std::string mode;
anil kumar appanaf945eee2019-09-25 23:29:11 +00001733 if (setPerformanceMode)
James Feist91244a62019-02-19 15:04:54 -08001734 {
James Feist91244a62019-02-19 15:04:54 -08001735 if (performanceMode)
1736 {
1737
1738 if (std::find(supported->begin(), supported->end(),
1739 "Performance") != supported->end())
1740 {
1741 mode = "Performance";
1742 }
1743 }
1744 else
1745 {
James Feist91244a62019-02-19 15:04:54 -08001746 if (std::find(supported->begin(), supported->end(), "Acoustic") !=
1747 supported->end())
1748 {
1749 mode = "Acoustic";
1750 }
1751 }
1752 if (mode.empty())
1753 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001754 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001755 }
anil kumar appanaf945eee2019-09-25 23:29:11 +00001756
1757 try
1758 {
1759 setDbusProperty(*dbus, settingsBusName, thermalModePath,
1760 thermalModeInterface, "Current", mode);
1761 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001762 catch (const sdbusplus::exception_t& e)
anil kumar appanaf945eee2019-09-25 23:29:11 +00001763 {
1764 phosphor::logging::log<phosphor::logging::level::ERR>(
1765 "ipmiOEMSetFanConfig: can't set thermal mode!",
1766 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1767 return ipmi::responseResponseError();
1768 }
James Feist91244a62019-02-19 15:04:54 -08001769 }
1770
anil kumar appanaf945eee2019-09-25 23:29:11 +00001771 return ipmi::responseSuccess();
James Feist91244a62019-02-19 15:04:54 -08001772}
1773
James Feist5b693632019-07-09 09:06:09 -07001774ipmi::RspType<uint8_t, // profile support map
1775 uint8_t, // fan control profile enable
1776 uint8_t, // flags
1777 uint32_t // dimm presence bit map
1778 >
1779 ipmiOEMGetFanConfig(uint8_t dimmGroupId)
James Feist91244a62019-02-19 15:04:54 -08001780{
Joshi-Mansi36f05ce2020-01-14 14:29:34 +05301781 if (dimmGroupId >= maxCPUNum)
1782 {
1783 return ipmi::responseInvalidFieldRequest();
1784 }
1785
1786 bool cpuStatus = cpuPresent("CPU_" + std::to_string(dimmGroupId + 1));
1787
1788 if (!cpuStatus)
1789 {
1790 return ipmi::responseInvalidFieldRequest();
1791 }
1792
James Feist91244a62019-02-19 15:04:54 -08001793 boost::container::flat_map<
1794 std::string, std::variant<std::vector<std::string>, std::string>>
1795 profileData;
1796
Vernon Mauery15419dd2019-05-24 09:40:30 -07001797 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1798 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001799 {
James Feist5b693632019-07-09 09:06:09 -07001800 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001801 }
1802
1803 std::string* current = std::get_if<std::string>(&profileData["Current"]);
1804
1805 if (current == nullptr)
1806 {
1807 phosphor::logging::log<phosphor::logging::level::ERR>(
1808 "ipmiOEMGetFanConfig: can't get current mode!");
James Feist5b693632019-07-09 09:06:09 -07001809 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001810 }
1811 bool performance = (*current == "Performance");
1812
James Feist5b693632019-07-09 09:06:09 -07001813 uint8_t flags = 0;
James Feist91244a62019-02-19 15:04:54 -08001814 if (performance)
1815 {
James Feist5b693632019-07-09 09:06:09 -07001816 flags |= 1 << 2;
James Feist91244a62019-02-19 15:04:54 -08001817 }
1818
jayaprakash Mutyala4b1552d2020-02-11 12:07:29 +00001819 constexpr uint8_t fanControlDefaultProfile = 0x80;
1820 constexpr uint8_t fanControlProfileState = 0x00;
1821 constexpr uint32_t dimmPresenceBitmap = 0x00;
1822
1823 return ipmi::responseSuccess(fanControlDefaultProfile,
1824 fanControlProfileState, flags,
1825 dimmPresenceBitmap);
James Feist91244a62019-02-19 15:04:54 -08001826}
James Feist5f957ca2019-03-14 15:33:55 -07001827constexpr const char* cfmLimitSettingPath =
1828 "/xyz/openbmc_project/control/cfm_limit";
1829constexpr const char* cfmLimitIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistfaa4f222019-03-21 16:21:55 -07001830constexpr const size_t legacyExitAirSensorNumber = 0x2e;
James Feist09f6b602019-08-08 11:30:03 -07001831constexpr const size_t legacyPCHSensorNumber = 0x22;
1832constexpr const char* exitAirPathName = "Exit_Air";
1833constexpr const char* pchPathName = "SSB_Temp";
James Feistacc8a4e2019-04-02 14:23:57 -07001834constexpr const char* pidConfigurationIface =
1835 "xyz.openbmc_project.Configuration.Pid";
James Feistfaa4f222019-03-21 16:21:55 -07001836
James Feist09f6b602019-08-08 11:30:03 -07001837static std::string getConfigPath(const std::string& name)
James Feistfaa4f222019-03-21 16:21:55 -07001838{
Vernon Mauery15419dd2019-05-24 09:40:30 -07001839 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistfaa4f222019-03-21 16:21:55 -07001840 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001841 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1842 "/xyz/openbmc_project/object_mapper",
1843 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistfaa4f222019-03-21 16:21:55 -07001844
James Feistacc8a4e2019-04-02 14:23:57 -07001845 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
James Feistfaa4f222019-03-21 16:21:55 -07001846 std::string path;
1847 GetSubTreeType resp;
1848 try
1849 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001850 auto reply = dbus->call(method);
James Feistfaa4f222019-03-21 16:21:55 -07001851 reply.read(resp);
1852 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001853 catch (const sdbusplus::exception_t&)
James Feistfaa4f222019-03-21 16:21:55 -07001854 {
1855 phosphor::logging::log<phosphor::logging::level::ERR>(
1856 "ipmiOEMGetFscParameter: mapper error");
1857 };
James Feist09f6b602019-08-08 11:30:03 -07001858 auto config =
1859 std::find_if(resp.begin(), resp.end(), [&name](const auto& pair) {
1860 return pair.first.find(name) != std::string::npos;
1861 });
James Feistfaa4f222019-03-21 16:21:55 -07001862 if (config != resp.end())
1863 {
1864 path = std::move(config->first);
1865 }
1866 return path;
1867}
James Feist5f957ca2019-03-14 15:33:55 -07001868
James Feistacc8a4e2019-04-02 14:23:57 -07001869// flat map to make alphabetical
1870static boost::container::flat_map<std::string, PropertyMap> getPidConfigs()
1871{
1872 boost::container::flat_map<std::string, PropertyMap> ret;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001873 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001874 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001875 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1876 "/xyz/openbmc_project/object_mapper",
1877 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistacc8a4e2019-04-02 14:23:57 -07001878
1879 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
1880 GetSubTreeType resp;
1881
1882 try
1883 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001884 auto reply = dbus->call(method);
James Feistacc8a4e2019-04-02 14:23:57 -07001885 reply.read(resp);
1886 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001887 catch (const sdbusplus::exception_t&)
James Feistacc8a4e2019-04-02 14:23:57 -07001888 {
1889 phosphor::logging::log<phosphor::logging::level::ERR>(
1890 "getFanConfigPaths: mapper error");
1891 };
1892 for (const auto& [path, objects] : resp)
1893 {
1894 if (objects.empty())
1895 {
1896 continue; // should be impossible
1897 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04001898
1899 try
1900 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001901 ret.emplace(path,
1902 getAllDbusProperties(*dbus, objects[0].first, path,
1903 pidConfigurationIface));
Zhu, Yungebe560b02019-04-21 21:19:21 -04001904 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05001905 catch (const sdbusplus::exception_t& e)
Zhu, Yungebe560b02019-04-21 21:19:21 -04001906 {
1907 phosphor::logging::log<phosphor::logging::level::ERR>(
1908 "getPidConfigs: can't get DbusProperties!",
1909 phosphor::logging::entry("ERR=%s", e.what()));
1910 }
James Feistacc8a4e2019-04-02 14:23:57 -07001911 }
1912 return ret;
1913}
1914
1915ipmi::RspType<uint8_t> ipmiOEMGetFanSpeedOffset(void)
1916{
1917 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1918 if (data.empty())
1919 {
1920 return ipmi::responseResponseError();
1921 }
1922 uint8_t minOffset = std::numeric_limits<uint8_t>::max();
1923 for (const auto& [_, pid] : data)
1924 {
1925 auto findClass = pid.find("Class");
1926 if (findClass == pid.end())
1927 {
1928 phosphor::logging::log<phosphor::logging::level::ERR>(
1929 "ipmiOEMGetFscParameter: found illegal pid "
1930 "configurations");
1931 return ipmi::responseResponseError();
1932 }
1933 std::string type = std::get<std::string>(findClass->second);
1934 if (type == "fan")
1935 {
1936 auto findOutLimit = pid.find("OutLimitMin");
1937 if (findOutLimit == pid.end())
1938 {
1939 phosphor::logging::log<phosphor::logging::level::ERR>(
1940 "ipmiOEMGetFscParameter: found illegal pid "
1941 "configurations");
1942 return ipmi::responseResponseError();
1943 }
1944 // get the min out of all the offsets
1945 minOffset = std::min(
1946 minOffset,
1947 static_cast<uint8_t>(std::get<double>(findOutLimit->second)));
1948 }
1949 }
1950 if (minOffset == std::numeric_limits<uint8_t>::max())
1951 {
1952 phosphor::logging::log<phosphor::logging::level::ERR>(
1953 "ipmiOEMGetFscParameter: found no fan configurations!");
1954 return ipmi::responseResponseError();
1955 }
1956
1957 return ipmi::responseSuccess(minOffset);
1958}
1959
1960ipmi::RspType<> ipmiOEMSetFanSpeedOffset(uint8_t offset)
1961{
Manish Baingbaa579f2021-10-08 22:30:32 +00001962 constexpr uint8_t maxFanSpeedOffset = 100;
1963 if (offset > maxFanSpeedOffset)
1964 {
1965 phosphor::logging::log<phosphor::logging::level::ERR>(
1966 "ipmiOEMSetFanSpeedOffset: fan offset greater than limit");
1967 return ipmi::responseInvalidFieldRequest();
1968 }
James Feistacc8a4e2019-04-02 14:23:57 -07001969 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1970 if (data.empty())
1971 {
1972
1973 phosphor::logging::log<phosphor::logging::level::ERR>(
1974 "ipmiOEMSetFanSpeedOffset: found no pid configurations!");
1975 return ipmi::responseResponseError();
1976 }
1977
Vernon Mauery15419dd2019-05-24 09:40:30 -07001978 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001979 bool found = false;
1980 for (const auto& [path, pid] : data)
1981 {
1982 auto findClass = pid.find("Class");
1983 if (findClass == pid.end())
1984 {
1985
1986 phosphor::logging::log<phosphor::logging::level::ERR>(
1987 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1988 "configurations");
1989 return ipmi::responseResponseError();
1990 }
1991 std::string type = std::get<std::string>(findClass->second);
1992 if (type == "fan")
1993 {
1994 auto findOutLimit = pid.find("OutLimitMin");
1995 if (findOutLimit == pid.end())
1996 {
1997
1998 phosphor::logging::log<phosphor::logging::level::ERR>(
1999 "ipmiOEMSetFanSpeedOffset: found illegal pid "
2000 "configurations");
2001 return ipmi::responseResponseError();
2002 }
Vernon Mauery15419dd2019-05-24 09:40:30 -07002003 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager",
James Feistacc8a4e2019-04-02 14:23:57 -07002004 path, pidConfigurationIface, "OutLimitMin",
2005 static_cast<double>(offset));
2006 found = true;
2007 }
2008 }
2009 if (!found)
2010 {
2011 phosphor::logging::log<phosphor::logging::level::ERR>(
2012 "ipmiOEMSetFanSpeedOffset: set no fan offsets");
2013 return ipmi::responseResponseError();
2014 }
2015
2016 return ipmi::responseSuccess();
2017}
2018
2019ipmi::RspType<> ipmiOEMSetFscParameter(uint8_t command, uint8_t param1,
2020 uint8_t param2)
James Feist5f957ca2019-03-14 15:33:55 -07002021{
2022 constexpr const size_t disableLimiting = 0x0;
2023
Vernon Mauery15419dd2019-05-24 09:40:30 -07002024 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07002025 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07002026 {
James Feist09f6b602019-08-08 11:30:03 -07002027 std::string pathName;
James Feistacc8a4e2019-04-02 14:23:57 -07002028 if (param1 == legacyExitAirSensorNumber)
James Feistfaa4f222019-03-21 16:21:55 -07002029 {
James Feist09f6b602019-08-08 11:30:03 -07002030 pathName = exitAirPathName;
2031 }
2032 else if (param1 == legacyPCHSensorNumber)
2033 {
2034 pathName = pchPathName;
James Feistfaa4f222019-03-21 16:21:55 -07002035 }
2036 else
2037 {
James Feistacc8a4e2019-04-02 14:23:57 -07002038 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07002039 }
James Feist09f6b602019-08-08 11:30:03 -07002040 std::string path = getConfigPath(pathName);
2041 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager", path,
2042 pidConfigurationIface, "SetPoint",
2043 static_cast<double>(param2));
2044 return ipmi::responseSuccess();
James Feistfaa4f222019-03-21 16:21:55 -07002045 }
James Feistacc8a4e2019-04-02 14:23:57 -07002046 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07002047 {
James Feistacc8a4e2019-04-02 14:23:57 -07002048 uint16_t cfm = param1 | (static_cast<uint16_t>(param2) << 8);
James Feist5f957ca2019-03-14 15:33:55 -07002049
2050 // must be greater than 50 based on eps
2051 if (cfm < 50 && cfm != disableLimiting)
2052 {
James Feistacc8a4e2019-04-02 14:23:57 -07002053 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002054 }
2055
2056 try
2057 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002058 ipmi::setDbusProperty(*dbus, settingsBusName, cfmLimitSettingPath,
James Feist5f957ca2019-03-14 15:33:55 -07002059 cfmLimitIface, "Limit",
2060 static_cast<double>(cfm));
2061 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002062 catch (const sdbusplus::exception_t& e)
James Feist5f957ca2019-03-14 15:33:55 -07002063 {
2064 phosphor::logging::log<phosphor::logging::level::ERR>(
2065 "ipmiOEMSetFscParameter: can't set cfm setting!",
2066 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002067 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002068 }
James Feistacc8a4e2019-04-02 14:23:57 -07002069 return ipmi::responseSuccess();
2070 }
2071 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
2072 {
2073 constexpr const size_t maxDomainCount = 8;
2074 uint8_t requestedDomainMask = param1;
2075 boost::container::flat_map data = getPidConfigs();
2076 if (data.empty())
2077 {
2078
2079 phosphor::logging::log<phosphor::logging::level::ERR>(
2080 "ipmiOEMSetFscParameter: found no pid configurations!");
2081 return ipmi::responseResponseError();
2082 }
2083 size_t count = 0;
2084 for (const auto& [path, pid] : data)
2085 {
2086 auto findClass = pid.find("Class");
2087 if (findClass == pid.end())
2088 {
2089
2090 phosphor::logging::log<phosphor::logging::level::ERR>(
2091 "ipmiOEMSetFscParameter: found illegal pid "
2092 "configurations");
2093 return ipmi::responseResponseError();
2094 }
2095 std::string type = std::get<std::string>(findClass->second);
2096 if (type == "fan")
2097 {
2098 if (requestedDomainMask & (1 << count))
2099 {
2100 ipmi::setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002101 *dbus, "xyz.openbmc_project.EntityManager", path,
James Feistacc8a4e2019-04-02 14:23:57 -07002102 pidConfigurationIface, "OutLimitMax",
2103 static_cast<double>(param2));
2104 }
2105 count++;
2106 }
2107 }
2108 return ipmi::responseSuccess();
James Feist5f957ca2019-03-14 15:33:55 -07002109 }
2110 else
2111 {
2112 // todo other command parts possibly
2113 // tcontrol is handled in peci now
2114 // fan speed offset not implemented yet
2115 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002116 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002117 }
2118}
2119
James Feistacc8a4e2019-04-02 14:23:57 -07002120ipmi::RspType<
2121 std::variant<uint8_t, std::array<uint8_t, 2>, std::array<uint16_t, 2>>>
2122 ipmiOEMGetFscParameter(uint8_t command, std::optional<uint8_t> param)
James Feist5f957ca2019-03-14 15:33:55 -07002123{
James Feist09f6b602019-08-08 11:30:03 -07002124 constexpr uint8_t legacyDefaultSetpoint = -128;
James Feist5f957ca2019-03-14 15:33:55 -07002125
Vernon Mauery15419dd2019-05-24 09:40:30 -07002126 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07002127 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07002128 {
James Feistacc8a4e2019-04-02 14:23:57 -07002129 if (!param)
James Feistfaa4f222019-03-21 16:21:55 -07002130 {
James Feistacc8a4e2019-04-02 14:23:57 -07002131 return ipmi::responseReqDataLenInvalid();
James Feistfaa4f222019-03-21 16:21:55 -07002132 }
2133
James Feist09f6b602019-08-08 11:30:03 -07002134 std::string pathName;
2135
2136 if (*param == legacyExitAirSensorNumber)
2137 {
2138 pathName = exitAirPathName;
2139 }
2140 else if (*param == legacyPCHSensorNumber)
2141 {
2142 pathName = pchPathName;
2143 }
2144 else
James Feistfaa4f222019-03-21 16:21:55 -07002145 {
James Feistacc8a4e2019-04-02 14:23:57 -07002146 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07002147 }
James Feist09f6b602019-08-08 11:30:03 -07002148
2149 uint8_t setpoint = legacyDefaultSetpoint;
2150 std::string path = getConfigPath(pathName);
James Feistfaa4f222019-03-21 16:21:55 -07002151 if (path.size())
2152 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002153 Value val = ipmi::getDbusProperty(
2154 *dbus, "xyz.openbmc_project.EntityManager", path,
2155 pidConfigurationIface, "SetPoint");
James Feistfaa4f222019-03-21 16:21:55 -07002156 setpoint = std::floor(std::get<double>(val) + 0.5);
2157 }
2158
2159 // old implementation used to return the "default" and current, we
2160 // don't make the default readily available so just make both the
2161 // same
James Feistfaa4f222019-03-21 16:21:55 -07002162
James Feistacc8a4e2019-04-02 14:23:57 -07002163 return ipmi::responseSuccess(
2164 std::array<uint8_t, 2>{setpoint, setpoint});
James Feistfaa4f222019-03-21 16:21:55 -07002165 }
James Feistacc8a4e2019-04-02 14:23:57 -07002166 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
2167 {
2168 constexpr const size_t maxDomainCount = 8;
2169
2170 if (!param)
2171 {
2172 return ipmi::responseReqDataLenInvalid();
2173 }
2174 uint8_t requestedDomain = *param;
2175 if (requestedDomain >= maxDomainCount)
2176 {
2177 return ipmi::responseInvalidFieldRequest();
2178 }
2179
2180 boost::container::flat_map data = getPidConfigs();
2181 if (data.empty())
2182 {
2183 phosphor::logging::log<phosphor::logging::level::ERR>(
2184 "ipmiOEMGetFscParameter: found no pid configurations!");
2185 return ipmi::responseResponseError();
2186 }
2187 size_t count = 0;
2188 for (const auto& [_, pid] : data)
2189 {
2190 auto findClass = pid.find("Class");
2191 if (findClass == pid.end())
2192 {
2193 phosphor::logging::log<phosphor::logging::level::ERR>(
2194 "ipmiOEMGetFscParameter: found illegal pid "
2195 "configurations");
2196 return ipmi::responseResponseError();
2197 }
2198 std::string type = std::get<std::string>(findClass->second);
2199 if (type == "fan")
2200 {
2201 if (requestedDomain == count)
2202 {
2203 auto findOutLimit = pid.find("OutLimitMax");
2204 if (findOutLimit == pid.end())
2205 {
2206 phosphor::logging::log<phosphor::logging::level::ERR>(
2207 "ipmiOEMGetFscParameter: found illegal pid "
2208 "configurations");
2209 return ipmi::responseResponseError();
2210 }
2211
2212 return ipmi::responseSuccess(
2213 static_cast<uint8_t>(std::floor(
2214 std::get<double>(findOutLimit->second) + 0.5)));
2215 }
2216 else
2217 {
2218 count++;
2219 }
2220 }
2221 }
2222
2223 return ipmi::responseInvalidFieldRequest();
2224 }
2225 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07002226 {
2227
2228 /*
2229 DataLen should be 1, but host is sending us an extra bit. As the
James Feistacc8a4e2019-04-02 14:23:57 -07002230 previous behavior didn't seem to prevent this, ignore the check for
2231 now.
James Feist5f957ca2019-03-14 15:33:55 -07002232
James Feistacc8a4e2019-04-02 14:23:57 -07002233 if (param)
James Feist5f957ca2019-03-14 15:33:55 -07002234 {
2235 phosphor::logging::log<phosphor::logging::level::ERR>(
2236 "ipmiOEMGetFscParameter: invalid input len!");
James Feist5f957ca2019-03-14 15:33:55 -07002237 return IPMI_CC_REQ_DATA_LEN_INVALID;
2238 }
2239 */
2240 Value cfmLimit;
2241 Value cfmMaximum;
2242 try
2243 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002244 cfmLimit = ipmi::getDbusProperty(*dbus, settingsBusName,
James Feist5f957ca2019-03-14 15:33:55 -07002245 cfmLimitSettingPath, cfmLimitIface,
2246 "Limit");
2247 cfmMaximum = ipmi::getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002248 *dbus, "xyz.openbmc_project.ExitAirTempSensor",
James Feist5f957ca2019-03-14 15:33:55 -07002249 "/xyz/openbmc_project/control/MaxCFM", cfmLimitIface, "Limit");
2250 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002251 catch (const sdbusplus::exception_t& e)
James Feist5f957ca2019-03-14 15:33:55 -07002252 {
2253 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feistacc8a4e2019-04-02 14:23:57 -07002254 "ipmiOEMGetFscParameter: can't get cfm setting!",
James Feist5f957ca2019-03-14 15:33:55 -07002255 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002256 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002257 }
2258
James Feistacc8a4e2019-04-02 14:23:57 -07002259 double cfmMax = std::get<double>(cfmMaximum);
2260 double cfmLim = std::get<double>(cfmLimit);
James Feist5f957ca2019-03-14 15:33:55 -07002261
James Feistacc8a4e2019-04-02 14:23:57 -07002262 cfmLim = std::floor(cfmLim + 0.5);
2263 cfmMax = std::floor(cfmMax + 0.5);
2264 uint16_t cfmLimResp = static_cast<uint16_t>(cfmLim);
2265 uint16_t cfmMaxResp = static_cast<uint16_t>(cfmMax);
James Feist5f957ca2019-03-14 15:33:55 -07002266
James Feistacc8a4e2019-04-02 14:23:57 -07002267 return ipmi::responseSuccess(
2268 std::array<uint16_t, 2>{cfmLimResp, cfmMaxResp});
James Feist5f957ca2019-03-14 15:33:55 -07002269 }
James Feistacc8a4e2019-04-02 14:23:57 -07002270
James Feist5f957ca2019-03-14 15:33:55 -07002271 else
2272 {
2273 // todo other command parts possibly
James Feist5f957ca2019-03-14 15:33:55 -07002274 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002275 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002276 }
2277}
2278
Cheng C Yang773703a2019-08-15 09:41:11 +08002279using crConfigVariant =
2280 std::variant<bool, uint8_t, uint32_t, std::vector<uint8_t>, std::string>;
2281
2282int setCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2283 const crConfigVariant& value,
2284 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2285{
2286 boost::system::error_code ec;
2287 ctx->bus->yield_method_call<void>(
Kuiying Wange45333a2020-07-22 22:06:37 +08002288 ctx->yield, ec, "xyz.openbmc_project.PSURedundancy",
Cheng C Yang773703a2019-08-15 09:41:11 +08002289 "/xyz/openbmc_project/control/power_supply_redundancy",
2290 "org.freedesktop.DBus.Properties", "Set",
2291 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property, value);
2292 if (ec)
2293 {
2294 phosphor::logging::log<phosphor::logging::level::ERR>(
2295 "Failed to set dbus property to cold redundancy");
2296 return -1;
2297 }
2298
2299 return 0;
2300}
2301
Kuiying Wange45333a2020-07-22 22:06:37 +08002302int getCRConfig(
2303 ipmi::Context::ptr ctx, const std::string& property, crConfigVariant& value,
2304 const std::string& service = "xyz.openbmc_project.PSURedundancy",
2305 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
Cheng C Yang773703a2019-08-15 09:41:11 +08002306{
2307 boost::system::error_code ec;
2308 value = ctx->bus->yield_method_call<crConfigVariant>(
Yong Li19445ab2019-12-20 18:25:29 +08002309 ctx->yield, ec, service,
Cheng C Yang773703a2019-08-15 09:41:11 +08002310 "/xyz/openbmc_project/control/power_supply_redundancy",
2311 "org.freedesktop.DBus.Properties", "Get",
2312 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property);
2313 if (ec)
2314 {
2315 phosphor::logging::log<phosphor::logging::level::ERR>(
2316 "Failed to get dbus property to cold redundancy");
2317 return -1;
2318 }
2319 return 0;
2320}
2321
2322uint8_t getPSUCount(void)
2323{
2324 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2325 ipmi::Value num;
2326 try
2327 {
2328 num = ipmi::getDbusProperty(
2329 *dbus, "xyz.openbmc_project.PSURedundancy",
2330 "/xyz/openbmc_project/control/power_supply_redundancy",
2331 "xyz.openbmc_project.Control.PowerSupplyRedundancy", "PSUNumber");
2332 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002333 catch (const sdbusplus::exception_t& e)
Cheng C Yang773703a2019-08-15 09:41:11 +08002334 {
2335 phosphor::logging::log<phosphor::logging::level::ERR>(
2336 "Failed to get PSUNumber property from dbus interface");
2337 return 0;
2338 }
2339 uint8_t* pNum = std::get_if<uint8_t>(&num);
2340 if (!pNum)
2341 {
2342 phosphor::logging::log<phosphor::logging::level::ERR>(
2343 "Error to get PSU Number");
2344 return 0;
2345 }
2346 return *pNum;
2347}
2348
2349bool validateCRAlgo(std::vector<uint8_t>& conf, uint8_t num)
2350{
2351 if (conf.size() < num)
2352 {
2353 phosphor::logging::log<phosphor::logging::level::ERR>(
2354 "Invalid PSU Ranking");
2355 return false;
2356 }
2357 std::set<uint8_t> confSet;
2358 for (uint8_t i = 0; i < num; i++)
2359 {
2360 if (conf[i] > num)
2361 {
2362 phosphor::logging::log<phosphor::logging::level::ERR>(
2363 "PSU Ranking is larger than current PSU number");
2364 return false;
2365 }
2366 confSet.emplace(conf[i]);
2367 }
2368
2369 if (confSet.size() != num)
2370 {
2371 phosphor::logging::log<phosphor::logging::level::ERR>(
2372 "duplicate PSU Ranking");
2373 return false;
2374 }
2375 return true;
2376}
2377
2378enum class crParameter
2379{
2380 crStatus = 0,
2381 crFeature = 1,
2382 rotationFeature = 2,
2383 rotationAlgo = 3,
2384 rotationPeriod = 4,
Yong Li19445ab2019-12-20 18:25:29 +08002385 numOfPSU = 5,
2386 rotationRankOrderEffective = 6
Cheng C Yang773703a2019-08-15 09:41:11 +08002387};
2388
2389constexpr ipmi::Cc ccParameterNotSupported = 0x80;
2390static const constexpr uint32_t oneDay = 0x15180;
2391static const constexpr uint32_t oneMonth = 0xf53700;
2392static const constexpr uint8_t userSpecific = 0x01;
2393static const constexpr uint8_t crSetCompleted = 0;
2394ipmi::RspType<uint8_t> ipmiOEMSetCRConfig(ipmi::Context::ptr ctx,
2395 uint8_t parameter,
2396 ipmi::message::Payload& payload)
2397{
2398 switch (static_cast<crParameter>(parameter))
2399 {
Cheng C Yang773703a2019-08-15 09:41:11 +08002400 case crParameter::rotationFeature:
2401 {
2402 uint8_t param1;
2403 if (payload.unpack(param1) || !payload.fullyUnpacked())
2404 {
2405 return ipmi::responseReqDataLenInvalid();
2406 }
2407 // Rotation Enable can only be true or false
2408 if (param1 > 1)
2409 {
2410 return ipmi::responseInvalidFieldRequest();
2411 }
2412 if (setCRConfig(ctx, "RotationEnabled", static_cast<bool>(param1)))
2413 {
2414 return ipmi::responseResponseError();
2415 }
2416 break;
2417 }
2418 case crParameter::rotationAlgo:
2419 {
2420 // Rotation Algorithm can only be 0-BMC Specific or 1-User Specific
2421 std::string algoName;
2422 uint8_t param1;
2423 if (payload.unpack(param1))
2424 {
2425 return ipmi::responseReqDataLenInvalid();
2426 }
2427 switch (param1)
2428 {
2429 case 0:
2430 algoName = "xyz.openbmc_project.Control."
2431 "PowerSupplyRedundancy.Algo.bmcSpecific";
2432 break;
2433 case 1:
2434 algoName = "xyz.openbmc_project.Control."
2435 "PowerSupplyRedundancy.Algo.userSpecific";
2436 break;
2437 default:
2438 return ipmi::responseInvalidFieldRequest();
2439 }
2440 if (setCRConfig(ctx, "RotationAlgorithm", algoName))
2441 {
2442 return ipmi::responseResponseError();
2443 }
2444
2445 uint8_t numberOfPSU = getPSUCount();
2446 if (!numberOfPSU)
2447 {
2448 return ipmi::responseResponseError();
2449 }
2450 std::vector<uint8_t> rankOrder;
2451
2452 if (param1 == userSpecific)
2453 {
2454 if (payload.unpack(rankOrder) || !payload.fullyUnpacked())
2455 {
2456 ipmi::responseReqDataLenInvalid();
2457 }
Yong Li83315132019-10-23 17:42:24 +08002458 if (rankOrder.size() != numberOfPSU)
Cheng C Yang773703a2019-08-15 09:41:11 +08002459 {
2460 return ipmi::responseReqDataLenInvalid();
2461 }
2462
2463 if (!validateCRAlgo(rankOrder, numberOfPSU))
2464 {
2465 return ipmi::responseInvalidFieldRequest();
2466 }
2467 }
2468 else
2469 {
2470 if (rankOrder.size() > 0)
2471 {
2472 return ipmi::responseReqDataLenInvalid();
2473 }
2474 for (uint8_t i = 1; i <= numberOfPSU; i++)
2475 {
2476 rankOrder.emplace_back(i);
2477 }
2478 }
2479 if (setCRConfig(ctx, "RotationRankOrder", rankOrder))
2480 {
2481 return ipmi::responseResponseError();
2482 }
2483 break;
2484 }
2485 case crParameter::rotationPeriod:
2486 {
2487 // Minimum Rotation period is One day (86400 seconds) and Max
2488 // Rotation Period is 6 month (0xf53700 seconds)
2489 uint32_t period;
2490 if (payload.unpack(period) || !payload.fullyUnpacked())
2491 {
2492 return ipmi::responseReqDataLenInvalid();
2493 }
2494 if ((period < oneDay) || (period > oneMonth))
2495 {
2496 return ipmi::responseInvalidFieldRequest();
2497 }
2498 if (setCRConfig(ctx, "PeriodOfRotation", period))
2499 {
2500 return ipmi::responseResponseError();
2501 }
2502 break;
2503 }
2504 default:
2505 {
2506 return ipmi::response(ccParameterNotSupported);
2507 }
2508 }
2509
Cheng C Yang773703a2019-08-15 09:41:11 +08002510 return ipmi::responseSuccess(crSetCompleted);
2511}
2512
Yong Li83315132019-10-23 17:42:24 +08002513ipmi::RspType<uint8_t, std::variant<uint8_t, uint32_t, std::vector<uint8_t>>>
Cheng C Yang773703a2019-08-15 09:41:11 +08002514 ipmiOEMGetCRConfig(ipmi::Context::ptr ctx, uint8_t parameter)
2515{
2516 crConfigVariant value;
2517 switch (static_cast<crParameter>(parameter))
2518 {
2519 case crParameter::crStatus:
2520 {
2521 if (getCRConfig(ctx, "ColdRedundancyStatus", value))
2522 {
2523 return ipmi::responseResponseError();
2524 }
2525 std::string* pStatus = std::get_if<std::string>(&value);
2526 if (!pStatus)
2527 {
2528 phosphor::logging::log<phosphor::logging::level::ERR>(
2529 "Error to get ColdRedundancyStatus property");
2530 return ipmi::responseResponseError();
2531 }
2532 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2533 auto status =
2534 server::PowerSupplyRedundancy::convertStatusFromString(
2535 *pStatus);
2536 switch (status)
2537 {
2538 case server::PowerSupplyRedundancy::Status::inProgress:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002539 return ipmi::responseSuccess(parameter,
Kuiying Wange45333a2020-07-22 22:06:37 +08002540 static_cast<uint8_t>(1));
Cheng C Yang773703a2019-08-15 09:41:11 +08002541
2542 case server::PowerSupplyRedundancy::Status::completed:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002543 return ipmi::responseSuccess(parameter,
Kuiying Wange45333a2020-07-22 22:06:37 +08002544 static_cast<uint8_t>(0));
Cheng C Yang773703a2019-08-15 09:41:11 +08002545 default:
2546 phosphor::logging::log<phosphor::logging::level::ERR>(
2547 "Error to get valid status");
2548 return ipmi::responseResponseError();
2549 }
2550 }
2551 case crParameter::crFeature:
2552 {
Kuiying Wange45333a2020-07-22 22:06:37 +08002553 if (getCRConfig(ctx, "PowerSupplyRedundancyEnabled", value))
Cheng C Yang773703a2019-08-15 09:41:11 +08002554 {
2555 return ipmi::responseResponseError();
2556 }
2557 bool* pResponse = std::get_if<bool>(&value);
2558 if (!pResponse)
2559 {
2560 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wange45333a2020-07-22 22:06:37 +08002561 "Error to get PowerSupplyRedundancyEnabled property");
Cheng C Yang773703a2019-08-15 09:41:11 +08002562 return ipmi::responseResponseError();
2563 }
2564
Cheng C Yangf41e3342019-09-10 04:47:23 +08002565 return ipmi::responseSuccess(parameter,
2566 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002567 }
2568 case crParameter::rotationFeature:
2569 {
2570 if (getCRConfig(ctx, "RotationEnabled", value))
2571 {
2572 return ipmi::responseResponseError();
2573 }
2574 bool* pResponse = std::get_if<bool>(&value);
2575 if (!pResponse)
2576 {
2577 phosphor::logging::log<phosphor::logging::level::ERR>(
2578 "Error to get RotationEnabled property");
2579 return ipmi::responseResponseError();
2580 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002581 return ipmi::responseSuccess(parameter,
2582 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002583 }
2584 case crParameter::rotationAlgo:
2585 {
2586 if (getCRConfig(ctx, "RotationAlgorithm", value))
2587 {
2588 return ipmi::responseResponseError();
2589 }
2590
2591 std::string* pAlgo = std::get_if<std::string>(&value);
2592 if (!pAlgo)
2593 {
2594 phosphor::logging::log<phosphor::logging::level::ERR>(
2595 "Error to get RotationAlgorithm property");
2596 return ipmi::responseResponseError();
2597 }
Yong Li83315132019-10-23 17:42:24 +08002598 std::vector<uint8_t> response;
Cheng C Yang773703a2019-08-15 09:41:11 +08002599 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2600 auto algo =
2601 server::PowerSupplyRedundancy::convertAlgoFromString(*pAlgo);
Yong Li83315132019-10-23 17:42:24 +08002602
Cheng C Yang773703a2019-08-15 09:41:11 +08002603 switch (algo)
2604 {
2605 case server::PowerSupplyRedundancy::Algo::bmcSpecific:
Yong Li83315132019-10-23 17:42:24 +08002606 response.push_back(0);
Cheng C Yang773703a2019-08-15 09:41:11 +08002607 break;
2608 case server::PowerSupplyRedundancy::Algo::userSpecific:
Yong Li83315132019-10-23 17:42:24 +08002609 response.push_back(1);
Cheng C Yang773703a2019-08-15 09:41:11 +08002610 break;
2611 default:
2612 phosphor::logging::log<phosphor::logging::level::ERR>(
2613 "Error to get valid algo");
2614 return ipmi::responseResponseError();
2615 }
2616
2617 if (getCRConfig(ctx, "RotationRankOrder", value))
2618 {
2619 return ipmi::responseResponseError();
2620 }
2621 std::vector<uint8_t>* pResponse =
2622 std::get_if<std::vector<uint8_t>>(&value);
2623 if (!pResponse)
2624 {
2625 phosphor::logging::log<phosphor::logging::level::ERR>(
2626 "Error to get RotationRankOrder property");
2627 return ipmi::responseResponseError();
2628 }
Yong Li83315132019-10-23 17:42:24 +08002629
Cheng C Yang773703a2019-08-15 09:41:11 +08002630 std::copy(pResponse->begin(), pResponse->end(),
Yong Li83315132019-10-23 17:42:24 +08002631 std::back_inserter(response));
2632
Cheng C Yangf41e3342019-09-10 04:47:23 +08002633 return ipmi::responseSuccess(parameter, response);
Cheng C Yang773703a2019-08-15 09:41:11 +08002634 }
2635 case crParameter::rotationPeriod:
2636 {
2637 if (getCRConfig(ctx, "PeriodOfRotation", value))
2638 {
2639 return ipmi::responseResponseError();
2640 }
2641 uint32_t* pResponse = std::get_if<uint32_t>(&value);
2642 if (!pResponse)
2643 {
2644 phosphor::logging::log<phosphor::logging::level::ERR>(
2645 "Error to get RotationAlgorithm property");
2646 return ipmi::responseResponseError();
2647 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002648 return ipmi::responseSuccess(parameter, *pResponse);
Cheng C Yang773703a2019-08-15 09:41:11 +08002649 }
2650 case crParameter::numOfPSU:
2651 {
2652 uint8_t numberOfPSU = getPSUCount();
2653 if (!numberOfPSU)
2654 {
2655 return ipmi::responseResponseError();
2656 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002657 return ipmi::responseSuccess(parameter, numberOfPSU);
Cheng C Yang773703a2019-08-15 09:41:11 +08002658 }
Yong Li19445ab2019-12-20 18:25:29 +08002659 case crParameter::rotationRankOrderEffective:
2660 {
2661 if (getCRConfig(ctx, "RotationRankOrder", value,
2662 "xyz.openbmc_project.PSURedundancy"))
2663 {
2664 return ipmi::responseResponseError();
2665 }
2666 std::vector<uint8_t>* pResponse =
2667 std::get_if<std::vector<uint8_t>>(&value);
2668 if (!pResponse)
2669 {
2670 phosphor::logging::log<phosphor::logging::level::ERR>(
2671 "Error to get effective RotationRankOrder property");
2672 return ipmi::responseResponseError();
2673 }
2674 return ipmi::responseSuccess(parameter, *pResponse);
2675 }
Cheng C Yang773703a2019-08-15 09:41:11 +08002676 default:
2677 {
2678 return ipmi::response(ccParameterNotSupported);
2679 }
2680 }
2681}
2682
Zhu, Yungebe560b02019-04-21 21:19:21 -04002683ipmi::RspType<> ipmiOEMSetFaultIndication(uint8_t sourceId, uint8_t faultType,
2684 uint8_t faultState,
2685 uint8_t faultGroup,
2686 std::array<uint8_t, 8>& ledStateData)
2687{
Zhu, Yungebe560b02019-04-21 21:19:21 -04002688 constexpr auto maxFaultType = static_cast<size_t>(RemoteFaultType::max);
2689 static const std::array<std::string, maxFaultType> faultNames = {
2690 "faultFan", "faultTemp", "faultPower",
2691 "faultDriveSlot", "faultSoftware", "faultMemory"};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002692
2693 constexpr uint8_t maxFaultSource = 0x4;
2694 constexpr uint8_t skipLEDs = 0xFF;
2695 constexpr uint8_t pinSize = 64;
2696 constexpr uint8_t groupSize = 16;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002697 constexpr uint8_t groupNum = 5; // 4 for fault memory, 1 for faultFan
Zhu, Yungebe560b02019-04-21 21:19:21 -04002698
Zhikui Rence4e73f2019-12-06 13:59:47 -08002699 // same pin names need to be defined in dts file
2700 static const std::array<std::array<std::string, groupSize>, groupNum>
2701 faultLedPinNames = {{
2702 "LED_CPU1_CH1_DIMM1_FAULT",
2703 "LED_CPU1_CH1_DIMM2_FAULT",
2704 "LED_CPU1_CH2_DIMM1_FAULT",
2705 "LED_CPU1_CH2_DIMM2_FAULT",
2706 "LED_CPU1_CH3_DIMM1_FAULT",
2707 "LED_CPU1_CH3_DIMM2_FAULT",
2708 "LED_CPU1_CH4_DIMM1_FAULT",
2709 "LED_CPU1_CH4_DIMM2_FAULT",
2710 "LED_CPU1_CH5_DIMM1_FAULT",
2711 "LED_CPU1_CH5_DIMM2_FAULT",
2712 "LED_CPU1_CH6_DIMM1_FAULT",
2713 "LED_CPU1_CH6_DIMM2_FAULT",
2714 "",
2715 "",
2716 "",
2717 "", // end of group1
2718 "LED_CPU2_CH1_DIMM1_FAULT",
2719 "LED_CPU2_CH1_DIMM2_FAULT",
2720 "LED_CPU2_CH2_DIMM1_FAULT",
2721 "LED_CPU2_CH2_DIMM2_FAULT",
2722 "LED_CPU2_CH3_DIMM1_FAULT",
2723 "LED_CPU2_CH3_DIMM2_FAULT",
2724 "LED_CPU2_CH4_DIMM1_FAULT",
2725 "LED_CPU2_CH4_DIMM2_FAULT",
2726 "LED_CPU2_CH5_DIMM1_FAULT",
2727 "LED_CPU2_CH5_DIMM2_FAULT",
2728 "LED_CPU2_CH6_DIMM1_FAULT",
2729 "LED_CPU2_CH6_DIMM2_FAULT",
2730 "",
2731 "",
2732 "",
2733 "", // endof group2
2734 "LED_CPU3_CH1_DIMM1_FAULT",
2735 "LED_CPU3_CH1_DIMM2_FAULT",
2736 "LED_CPU3_CH2_DIMM1_FAULT",
2737 "LED_CPU3_CH2_DIMM2_FAULT",
2738 "LED_CPU3_CH3_DIMM1_FAULT",
2739 "LED_CPU3_CH3_DIMM2_FAULT",
2740 "LED_CPU3_CH4_DIMM1_FAULT",
2741 "LED_CPU3_CH4_DIMM2_FAULT",
2742 "LED_CPU3_CH5_DIMM1_FAULT",
2743 "LED_CPU3_CH5_DIMM2_FAULT",
2744 "LED_CPU3_CH6_DIMM1_FAULT",
2745 "LED_CPU3_CH6_DIMM2_FAULT",
2746 "",
2747 "",
2748 "",
2749 "", // end of group3
2750 "LED_CPU4_CH1_DIMM1_FAULT",
2751 "LED_CPU4_CH1_DIMM2_FAULT",
2752 "LED_CPU4_CH2_DIMM1_FAULT",
2753 "LED_CPU4_CH2_DIMM2_FAULT",
2754 "LED_CPU4_CH3_DIMM1_FAULT",
2755 "LED_CPU4_CH3_DIMM2_FAULT",
2756 "LED_CPU4_CH4_DIMM1_FAULT",
2757 "LED_CPU4_CH4_DIMM2_FAULT",
2758 "LED_CPU4_CH5_DIMM1_FAULT",
2759 "LED_CPU4_CH5_DIMM2_FAULT",
2760 "LED_CPU4_CH6_DIMM1_FAULT",
2761 "LED_CPU4_CH6_DIMM2_FAULT",
2762 "",
2763 "",
2764 "",
2765 "", // end of group4
2766 "LED_FAN1_FAULT",
2767 "LED_FAN2_FAULT",
2768 "LED_FAN3_FAULT",
2769 "LED_FAN4_FAULT",
2770 "LED_FAN5_FAULT",
2771 "LED_FAN6_FAULT",
2772 "LED_FAN7_FAULT",
2773 "LED_FAN8_FAULT",
2774 "",
2775 "",
2776 "",
2777 "",
2778 "",
2779 "",
2780 "",
2781 "" // end of group5
2782 }};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002783
Zhikui Rence4e73f2019-12-06 13:59:47 -08002784 // Validate the source, fault type --
2785 // (Byte 1) sourceId: Unspecified, Hot-Swap Controller 0, Hot-Swap
2786 // Controller 1, BIOS (Byte 2) fault type: fan, temperature, power,
2787 // driveslot, software, memory (Byte 3) FaultState: OK, Degraded,
2788 // Non-Critical, Critical, Non-Recoverable, (Byte 4) is faultGroup,
2789 // definition differs based on fault type (Byte 2)
2790 // Type Fan=> Group: 0=FanGroupID, FF-not used
2791 // Byte 5-11 00h, not used
2792 // Byte12 FanLedState [7:0]-Fans 7:0
2793 // Type Memory=> Group: 0 = DIMM GroupID, FF-not used
2794 // Byte 5:12 - DIMM LED state (64bit field, LS Byte first)
2795 // [63:48] = CPU4 channels 7:0, 2 bits per channel
2796 // [47:32] = CPU3 channels 7:0, 2 bits per channel
2797 // [31:16] = CPU2 channels 7:0, 2 bits per channel
2798 // [15:0] = CPU1 channels 7:0, 2 bits per channel
2799 // Type Other=> Component Fault LED Group ID, not used set to 0xFF
2800 // Byte[5:12]: reserved 0x00h
Zhu, Yungebe560b02019-04-21 21:19:21 -04002801 if ((sourceId >= maxFaultSource) ||
2802 (faultType >= static_cast<int8_t>(RemoteFaultType::max)) ||
2803 (faultState >= static_cast<int8_t>(RemoteFaultState::maxFaultState)) ||
2804 (faultGroup >= static_cast<int8_t>(DimmFaultType::maxFaultGroup)))
2805 {
2806 return ipmi::responseParmOutOfRange();
2807 }
2808
Zhikui Rence4e73f2019-12-06 13:59:47 -08002809 size_t pinGroupOffset = 0;
2810 size_t pinGroupMax = pinSize / groupSize;
2811 if (RemoteFaultType::fan == RemoteFaultType(faultType))
Zhu, Yungebe560b02019-04-21 21:19:21 -04002812 {
Zhikui Rence4e73f2019-12-06 13:59:47 -08002813 pinGroupOffset = 4;
2814 pinGroupMax = groupNum - pinSize / groupSize;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002815 }
2816
2817 switch (RemoteFaultType(faultType))
2818 {
2819 case (RemoteFaultType::fan):
2820 case (RemoteFaultType::memory):
2821 {
2822 if (faultGroup == skipLEDs)
2823 {
2824 return ipmi::responseSuccess();
2825 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002826 // calculate led state bit filed count, each byte has 8bits
2827 // the maximum bits will be 8 * 8 bits
2828 constexpr uint8_t size = sizeof(ledStateData) * 8;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002829
2830 // assemble ledState
2831 uint64_t ledState = 0;
2832 bool hasError = false;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002833 for (int i = 0; i < sizeof(ledStateData); i++)
2834 {
2835 ledState = (uint64_t)(ledState << 8);
2836 ledState = (uint64_t)(ledState | (uint64_t)ledStateData[i]);
2837 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002838 std::bitset<size> ledStateBits(ledState);
Zhu, Yungebe560b02019-04-21 21:19:21 -04002839
Zhikui Rence4e73f2019-12-06 13:59:47 -08002840 for (int group = 0; group < pinGroupMax; group++)
2841 {
2842 for (int i = 0; i < groupSize; i++)
2843 { // skip non-existing pins
2844 if (0 == faultLedPinNames[group + pinGroupOffset][i].size())
2845 {
2846 continue;
2847 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002848
Zhikui Rence4e73f2019-12-06 13:59:47 -08002849 gpiod::line line = gpiod::find_line(
2850 faultLedPinNames[group + pinGroupOffset][i]);
2851 if (!line)
2852 {
2853 phosphor::logging::log<phosphor::logging::level::ERR>(
2854 "Not Find Led Gpio Device!",
2855 phosphor::logging::entry(
2856 "DEVICE=%s",
2857 faultLedPinNames[group + pinGroupOffset][i]
2858 .c_str()));
2859 hasError = true;
2860 continue;
2861 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002862
Zhikui Rence4e73f2019-12-06 13:59:47 -08002863 bool activeHigh =
2864 (line.active_state() == gpiod::line::ACTIVE_HIGH);
2865 try
2866 {
2867 line.request(
2868 {"faultLed", gpiod::line_request::DIRECTION_OUTPUT,
2869 activeHigh
2870 ? 0
2871 : gpiod::line_request::FLAG_ACTIVE_LOW});
2872 line.set_value(ledStateBits[i + group * groupSize]);
2873 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002874 catch (const std::system_error&)
Zhikui Rence4e73f2019-12-06 13:59:47 -08002875 {
2876 phosphor::logging::log<phosphor::logging::level::ERR>(
2877 "Error write Led Gpio Device!",
2878 phosphor::logging::entry(
2879 "DEVICE=%s",
2880 faultLedPinNames[group + pinGroupOffset][i]
2881 .c_str()));
2882 hasError = true;
2883 continue;
2884 }
2885 } // for int i
2886 }
2887 if (hasError)
2888 {
2889 return ipmi::responseResponseError();
Zhu, Yungebe560b02019-04-21 21:19:21 -04002890 }
2891 break;
2892 }
2893 default:
2894 {
2895 // now only support two fault types
2896 return ipmi::responseParmOutOfRange();
2897 }
Zhikui Rence4e73f2019-12-06 13:59:47 -08002898 } // switch
Zhu, Yungebe560b02019-04-21 21:19:21 -04002899 return ipmi::responseSuccess();
2900}
2901
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302902ipmi::RspType<uint8_t> ipmiOEMReadBoardProductId()
2903{
2904 uint8_t prodId = 0;
2905 try
2906 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002907 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302908 const DbusObjectInfo& object = getDbusObject(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002909 *dbus, "xyz.openbmc_project.Inventory.Item.Board",
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302910 "/xyz/openbmc_project/inventory/system/board/", "Baseboard");
2911 const Value& propValue = getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002912 *dbus, object.second, object.first,
Suryakanth Sekar6c57e5c2020-01-10 17:11:58 +05302913 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
2914 "ProductId");
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302915 prodId = static_cast<uint8_t>(std::get<uint64_t>(propValue));
2916 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05002917 catch (const std::exception& e)
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302918 {
2919 phosphor::logging::log<phosphor::logging::level::ERR>(
2920 "ipmiOEMReadBoardProductId: Product ID read failed!",
2921 phosphor::logging::entry("ERR=%s", e.what()));
2922 }
2923 return ipmi::responseSuccess(prodId);
2924}
2925
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302926/** @brief implements the get security mode command
2927 * @param ctx - ctx pointer
2928 *
2929 * @returns IPMI completion code with following data
2930 * - restriction mode value - As specified in
2931 * xyz.openbmc_project.Control.Security.RestrictionMode.interface.yaml
2932 * - special mode value - As specified in
2933 * xyz.openbmc_project.Control.Security.SpecialMode.interface.yaml
2934 */
2935ipmi::RspType<uint8_t, uint8_t> ipmiGetSecurityMode(ipmi::Context::ptr ctx)
2936{
2937 namespace securityNameSpace =
2938 sdbusplus::xyz::openbmc_project::Control::Security::server;
2939 uint8_t restrictionModeValue = 0;
2940 uint8_t specialModeValue = 0;
2941
2942 boost::system::error_code ec;
2943 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002944 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302945 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2946 restricionModeProperty);
2947 if (ec)
2948 {
2949 phosphor::logging::log<phosphor::logging::level::ERR>(
2950 "ipmiGetSecurityMode: failed to get RestrictionMode property",
2951 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2952 return ipmi::responseUnspecifiedError();
2953 }
2954 restrictionModeValue = static_cast<uint8_t>(
2955 securityNameSpace::RestrictionMode::convertModesFromString(
2956 std::get<std::string>(varRestrMode)));
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302957 auto varSpecialMode =
2958 ctx->bus->yield_method_call<std::variant<std::string>>(
2959 ctx->yield, ec, specialModeService, specialModeBasePath,
2960 dBusPropertyIntf, dBusPropertyGetMethod, specialModeIntf,
2961 specialModeProperty);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302962 if (ec)
2963 {
2964 phosphor::logging::log<phosphor::logging::level::ERR>(
2965 "ipmiGetSecurityMode: failed to get SpecialMode property",
2966 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2967 // fall through, let us not worry about SpecialMode property, which is
2968 // not required in user scenario
2969 }
2970 else
2971 {
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302972 specialModeValue = static_cast<uint8_t>(
2973 securityNameSpace::SpecialMode::convertModesFromString(
2974 std::get<std::string>(varSpecialMode)));
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302975 }
2976 return ipmi::responseSuccess(restrictionModeValue, specialModeValue);
2977}
2978
2979/** @brief implements the set security mode command
2980 * Command allows to upgrade the restriction mode and won't allow
2981 * to downgrade from system interface
2982 * @param ctx - ctx pointer
2983 * @param restrictionMode - restriction mode value to be set.
2984 *
2985 * @returns IPMI completion code
2986 */
2987ipmi::RspType<> ipmiSetSecurityMode(ipmi::Context::ptr ctx,
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302988 uint8_t restrictionMode,
2989 std::optional<uint8_t> specialMode)
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302990{
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302991#ifndef BMC_VALIDATION_UNSECURE_FEATURE
2992 if (specialMode)
2993 {
2994 return ipmi::responseReqDataLenInvalid();
2995 }
2996#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302997 namespace securityNameSpace =
2998 sdbusplus::xyz::openbmc_project::Control::Security::server;
2999
3000 ChannelInfo chInfo;
3001 if (getChannelInfo(ctx->channel, chInfo) != ccSuccess)
3002 {
3003 phosphor::logging::log<phosphor::logging::level::ERR>(
3004 "ipmiSetSecurityMode: Failed to get Channel Info",
3005 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
3006 return ipmi::responseUnspecifiedError();
3007 }
3008 auto reqMode =
3009 static_cast<securityNameSpace::RestrictionMode::Modes>(restrictionMode);
3010
3011 if ((reqMode < securityNameSpace::RestrictionMode::Modes::Provisioning) ||
3012 (reqMode >
3013 securityNameSpace::RestrictionMode::Modes::ProvisionedHostDisabled))
3014 {
3015 return ipmi::responseInvalidFieldRequest();
3016 }
3017
3018 boost::system::error_code ec;
3019 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07003020 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303021 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
3022 restricionModeProperty);
3023 if (ec)
3024 {
3025 phosphor::logging::log<phosphor::logging::level::ERR>(
3026 "ipmiSetSecurityMode: failed to get RestrictionMode property",
3027 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
3028 return ipmi::responseUnspecifiedError();
3029 }
3030 auto currentRestrictionMode =
3031 securityNameSpace::RestrictionMode::convertModesFromString(
3032 std::get<std::string>(varRestrMode));
3033
3034 if (chInfo.mediumType !=
3035 static_cast<uint8_t>(EChannelMediumType::lan8032) &&
3036 currentRestrictionMode > reqMode)
3037 {
3038 phosphor::logging::log<phosphor::logging::level::ERR>(
3039 "ipmiSetSecurityMode - Downgrading security mode not supported "
3040 "through system interface",
3041 phosphor::logging::entry(
3042 "CUR_MODE=%d", static_cast<uint8_t>(currentRestrictionMode)),
3043 phosphor::logging::entry("REQ_MODE=%d", restrictionMode));
3044 return ipmi::responseCommandNotAvailable();
3045 }
3046
3047 ec.clear();
3048 ctx->bus->yield_method_call<>(
James Feist28c72902019-09-16 10:34:07 -07003049 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303050 dBusPropertyIntf, dBusPropertySetMethod, restricionModeIntf,
3051 restricionModeProperty,
3052 static_cast<std::variant<std::string>>(
3053 securityNameSpace::convertForMessage(reqMode)));
3054
3055 if (ec)
3056 {
3057 phosphor::logging::log<phosphor::logging::level::ERR>(
3058 "ipmiSetSecurityMode: failed to set RestrictionMode property",
3059 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
3060 return ipmi::responseUnspecifiedError();
3061 }
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05303062
3063#ifdef BMC_VALIDATION_UNSECURE_FEATURE
3064 if (specialMode)
3065 {
Jayaprakash Mutyalad77489f2020-09-05 01:00:04 +00003066 constexpr uint8_t mfgMode = 0x01;
3067 // Manufacturing mode is reserved. So can't enable this mode.
3068 if (specialMode.value() == mfgMode)
3069 {
3070 phosphor::logging::log<phosphor::logging::level::INFO>(
3071 "ipmiSetSecurityMode: Can't enable Manufacturing mode");
3072 return ipmi::responseInvalidFieldRequest();
3073 }
3074
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05303075 ec.clear();
3076 ctx->bus->yield_method_call<>(
3077 ctx->yield, ec, specialModeService, specialModeBasePath,
3078 dBusPropertyIntf, dBusPropertySetMethod, specialModeIntf,
3079 specialModeProperty,
3080 static_cast<std::variant<std::string>>(
3081 securityNameSpace::convertForMessage(
3082 static_cast<securityNameSpace::SpecialMode::Modes>(
3083 specialMode.value()))));
3084
3085 if (ec)
3086 {
3087 phosphor::logging::log<phosphor::logging::level::ERR>(
3088 "ipmiSetSecurityMode: failed to set SpecialMode property",
3089 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
3090 return ipmi::responseUnspecifiedError();
3091 }
3092 }
3093#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303094 return ipmi::responseSuccess();
3095}
3096
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003097ipmi::RspType<uint8_t /* restore status */>
3098 ipmiRestoreConfiguration(const std::array<uint8_t, 3>& clr, uint8_t cmd)
3099{
3100 static constexpr std::array<uint8_t, 3> expClr = {'C', 'L', 'R'};
3101
3102 if (clr != expClr)
3103 {
3104 return ipmi::responseInvalidFieldRequest();
3105 }
3106 constexpr uint8_t cmdStatus = 0;
3107 constexpr uint8_t cmdDefaultRestore = 0xaa;
3108 constexpr uint8_t cmdFullRestore = 0xbb;
3109 constexpr uint8_t cmdFormat = 0xcc;
3110
3111 constexpr const char* restoreOpFname = "/tmp/.rwfs/.restore_op";
3112
3113 switch (cmd)
3114 {
3115 case cmdStatus:
3116 break;
3117 case cmdDefaultRestore:
3118 case cmdFullRestore:
3119 case cmdFormat:
3120 {
3121 // write file to rwfs root
3122 int value = (cmd - 1) & 0x03; // map aa, bb, cc => 1, 2, 3
3123 std::ofstream restoreFile(restoreOpFname);
3124 if (!restoreFile)
3125 {
3126 return ipmi::responseUnspecifiedError();
3127 }
3128 restoreFile << value << "\n";
Arun P. Mohananba1fbc82021-04-26 11:26:53 +05303129
3130 phosphor::logging::log<phosphor::logging::level::WARNING>(
3131 "Restore to default will be performed on next BMC boot",
3132 phosphor::logging::entry("ACTION=0x%0X", cmd));
3133
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003134 break;
3135 }
3136 default:
3137 return ipmi::responseInvalidFieldRequest();
3138 }
3139
3140 constexpr uint8_t restorePending = 0;
3141 constexpr uint8_t restoreComplete = 1;
3142
3143 uint8_t restoreStatus = std::filesystem::exists(restoreOpFname)
3144 ? restorePending
3145 : restoreComplete;
3146 return ipmi::responseSuccess(restoreStatus);
3147}
3148
Chen Yugang39736d52019-07-12 16:24:33 +08003149ipmi::RspType<uint8_t> ipmiOEMGetNmiSource(void)
3150{
3151 uint8_t bmcSource;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003152 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08003153
3154 try
3155 {
3156 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3157 std::string service =
3158 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
3159 Value variant =
3160 getDbusProperty(*dbus, service, oemNmiSourceObjPath,
3161 oemNmiSourceIntf, oemNmiBmcSourceObjPathProp);
3162
3163 switch (nmi::NMISource::convertBMCSourceSignalFromString(
3164 std::get<std::string>(variant)))
3165 {
3166 case nmi::NMISource::BMCSourceSignal::None:
3167 bmcSource = static_cast<uint8_t>(NmiSource::none);
3168 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003169 case nmi::NMISource::BMCSourceSignal::FrontPanelButton:
3170 bmcSource = static_cast<uint8_t>(NmiSource::frontPanelButton);
Chen Yugang39736d52019-07-12 16:24:33 +08003171 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003172 case nmi::NMISource::BMCSourceSignal::Watchdog:
3173 bmcSource = static_cast<uint8_t>(NmiSource::watchdog);
Chen Yugang39736d52019-07-12 16:24:33 +08003174 break;
3175 case nmi::NMISource::BMCSourceSignal::ChassisCmd:
3176 bmcSource = static_cast<uint8_t>(NmiSource::chassisCmd);
3177 break;
3178 case nmi::NMISource::BMCSourceSignal::MemoryError:
3179 bmcSource = static_cast<uint8_t>(NmiSource::memoryError);
3180 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003181 case nmi::NMISource::BMCSourceSignal::PciBusError:
3182 bmcSource = static_cast<uint8_t>(NmiSource::pciBusError);
Chen Yugang39736d52019-07-12 16:24:33 +08003183 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003184 case nmi::NMISource::BMCSourceSignal::PCH:
3185 bmcSource = static_cast<uint8_t>(NmiSource::pch);
Chen Yugang39736d52019-07-12 16:24:33 +08003186 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003187 case nmi::NMISource::BMCSourceSignal::Chipset:
3188 bmcSource = static_cast<uint8_t>(NmiSource::chipset);
Chen Yugang39736d52019-07-12 16:24:33 +08003189 break;
3190 default:
3191 phosphor::logging::log<phosphor::logging::level::ERR>(
3192 "NMI source: invalid property!",
3193 phosphor::logging::entry(
3194 "PROP=%s", std::get<std::string>(variant).c_str()));
3195 return ipmi::responseResponseError();
3196 }
3197 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003198 catch (const sdbusplus::exception::exception& e)
Chen Yugang39736d52019-07-12 16:24:33 +08003199 {
3200 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3201 return ipmi::responseResponseError();
3202 }
3203
3204 return ipmi::responseSuccess(bmcSource);
3205}
3206
3207ipmi::RspType<> ipmiOEMSetNmiSource(uint8_t sourceId)
3208{
Chen Yugang97cf96e2019-11-01 08:55:11 +08003209 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08003210
3211 nmi::NMISource::BMCSourceSignal bmcSourceSignal =
3212 nmi::NMISource::BMCSourceSignal::None;
3213
3214 switch (NmiSource(sourceId))
3215 {
3216 case NmiSource::none:
3217 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::None;
3218 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003219 case NmiSource::frontPanelButton:
3220 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::FrontPanelButton;
Chen Yugang39736d52019-07-12 16:24:33 +08003221 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003222 case NmiSource::watchdog:
3223 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Watchdog;
Chen Yugang39736d52019-07-12 16:24:33 +08003224 break;
3225 case NmiSource::chassisCmd:
3226 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::ChassisCmd;
3227 break;
3228 case NmiSource::memoryError:
3229 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::MemoryError;
3230 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003231 case NmiSource::pciBusError:
3232 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PciBusError;
Chen Yugang39736d52019-07-12 16:24:33 +08003233 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003234 case NmiSource::pch:
3235 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PCH;
Chen Yugang39736d52019-07-12 16:24:33 +08003236 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003237 case NmiSource::chipset:
3238 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Chipset;
Chen Yugang39736d52019-07-12 16:24:33 +08003239 break;
3240 default:
3241 phosphor::logging::log<phosphor::logging::level::ERR>(
3242 "NMI source: invalid property!");
3243 return ipmi::responseResponseError();
3244 }
3245
3246 try
3247 {
3248 // keep NMI signal source
3249 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3250 std::string service =
3251 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
Chen Yugang97cf96e2019-11-01 08:55:11 +08003252 setDbusProperty(*dbus, service, oemNmiSourceObjPath, oemNmiSourceIntf,
3253 oemNmiBmcSourceObjPathProp,
3254 nmi::convertForMessage(bmcSourceSignal));
Chen Yugang99be6332019-08-09 16:20:48 +08003255 // set Enabled property to inform NMI source handling
3256 // to trigger a NMI_OUT BSOD.
3257 // if it's triggered by NMI source property changed,
3258 // NMI_OUT BSOD could be missed if the same source occurs twice in a row
3259 if (bmcSourceSignal != nmi::NMISource::BMCSourceSignal::None)
3260 {
3261 setDbusProperty(*dbus, service, oemNmiSourceObjPath,
3262 oemNmiSourceIntf, oemNmiEnabledObjPathProp,
3263 static_cast<bool>(true));
3264 }
Chen Yugang39736d52019-07-12 16:24:33 +08003265 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003266 catch (const sdbusplus::exception_t& e)
Chen Yugang39736d52019-07-12 16:24:33 +08003267 {
3268 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3269 return ipmi::responseResponseError();
3270 }
3271
3272 return ipmi::responseSuccess();
3273}
3274
James Feist63efafa2019-07-24 12:39:21 -07003275namespace dimmOffset
3276{
3277constexpr const char* dimmPower = "DimmPower";
3278constexpr const char* staticCltt = "StaticCltt";
3279constexpr const char* offsetPath = "/xyz/openbmc_project/Inventory/Item/Dimm";
3280constexpr const char* offsetInterface =
3281 "xyz.openbmc_project.Inventory.Item.Dimm.Offset";
3282constexpr const char* property = "DimmOffset";
3283
3284}; // namespace dimmOffset
3285
3286ipmi::RspType<>
3287 ipmiOEMSetDimmOffset(uint8_t type,
3288 const std::vector<std::tuple<uint8_t, uint8_t>>& data)
3289{
3290 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3291 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3292 {
3293 return ipmi::responseInvalidFieldRequest();
3294 }
3295
3296 if (data.empty())
3297 {
3298 return ipmi::responseInvalidFieldRequest();
3299 }
3300 nlohmann::json json;
3301
3302 std::ifstream jsonStream(dimmOffsetFile);
3303 if (jsonStream.good())
3304 {
3305 json = nlohmann::json::parse(jsonStream, nullptr, false);
3306 if (json.is_discarded())
3307 {
3308 json = nlohmann::json();
3309 }
3310 jsonStream.close();
3311 }
3312
3313 std::string typeName;
3314 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3315 {
3316 typeName = dimmOffset::dimmPower;
3317 }
3318 else
3319 {
3320 typeName = dimmOffset::staticCltt;
3321 }
3322
3323 nlohmann::json& field = json[typeName];
3324
3325 for (const auto& [index, value] : data)
3326 {
3327 field[index] = value;
3328 }
3329
3330 for (nlohmann::json& val : field)
3331 {
3332 if (val == nullptr)
3333 {
3334 val = static_cast<uint8_t>(0);
3335 }
3336 }
3337
3338 std::ofstream output(dimmOffsetFile);
3339 if (!output.good())
3340 {
3341 std::cerr << "Error writing json file\n";
3342 return ipmi::responseResponseError();
3343 }
3344
3345 output << json.dump(4);
3346
3347 if (type == static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3348 {
3349 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
3350
3351 std::variant<std::vector<uint8_t>> offsets =
3352 field.get<std::vector<uint8_t>>();
3353 auto call = bus->new_method_call(
3354 settingsBusName, dimmOffset::offsetPath, PROP_INTF, "Set");
3355 call.append(dimmOffset::offsetInterface, dimmOffset::property, offsets);
3356 try
3357 {
3358 bus->call(call);
3359 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003360 catch (const sdbusplus::exception_t& e)
James Feist63efafa2019-07-24 12:39:21 -07003361 {
3362 phosphor::logging::log<phosphor::logging::level::ERR>(
3363 "ipmiOEMSetDimmOffset: can't set dimm offsets!",
3364 phosphor::logging::entry("ERR=%s", e.what()));
3365 return ipmi::responseResponseError();
3366 }
3367 }
3368
3369 return ipmi::responseSuccess();
3370}
3371
3372ipmi::RspType<uint8_t> ipmiOEMGetDimmOffset(uint8_t type, uint8_t index)
3373{
3374
3375 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3376 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3377 {
3378 return ipmi::responseInvalidFieldRequest();
3379 }
3380
3381 std::ifstream jsonStream(dimmOffsetFile);
3382
3383 auto json = nlohmann::json::parse(jsonStream, nullptr, false);
3384 if (json.is_discarded())
3385 {
3386 std::cerr << "File error in " << dimmOffsetFile << "\n";
3387 return ipmi::responseResponseError();
3388 }
3389
3390 std::string typeName;
3391 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3392 {
3393 typeName = dimmOffset::dimmPower;
3394 }
3395 else
3396 {
3397 typeName = dimmOffset::staticCltt;
3398 }
3399
3400 auto it = json.find(typeName);
3401 if (it == json.end())
3402 {
3403 return ipmi::responseInvalidFieldRequest();
3404 }
3405
3406 if (it->size() <= index)
3407 {
3408 return ipmi::responseInvalidFieldRequest();
3409 }
3410
3411 uint8_t resp = it->at(index).get<uint8_t>();
3412 return ipmi::responseSuccess(resp);
3413}
3414
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003415namespace boot_options
3416{
3417
3418using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
3419using IpmiValue = uint8_t;
3420constexpr auto ipmiDefault = 0;
3421
3422std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
3423 {0x01, Source::Sources::Network},
3424 {0x02, Source::Sources::Disk},
3425 {0x05, Source::Sources::ExternalMedia},
3426 {0x0f, Source::Sources::RemovableMedia},
3427 {ipmiDefault, Source::Sources::Default}};
3428
3429std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003430 {0x06, Mode::Modes::Setup}, {ipmiDefault, Mode::Modes::Regular}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003431
3432std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
3433 {Source::Sources::Network, 0x01},
3434 {Source::Sources::Disk, 0x02},
3435 {Source::Sources::ExternalMedia, 0x05},
3436 {Source::Sources::RemovableMedia, 0x0f},
3437 {Source::Sources::Default, ipmiDefault}};
3438
3439std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003440 {Mode::Modes::Setup, 0x06}, {Mode::Modes::Regular, ipmiDefault}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003441
3442static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
3443static constexpr auto bootSourceIntf =
3444 "xyz.openbmc_project.Control.Boot.Source";
3445static constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
3446static constexpr auto persistentObjPath =
3447 "/xyz/openbmc_project/control/host0/boot";
3448static constexpr auto oneTimePath =
3449 "/xyz/openbmc_project/control/host0/boot/one_time";
3450static constexpr auto bootSourceProp = "BootSource";
3451static constexpr auto bootModeProp = "BootMode";
3452static constexpr auto oneTimeBootEnableProp = "Enabled";
3453static constexpr auto httpBootMode =
3454 "xyz.openbmc_project.Control.Boot.Source.Sources.Http";
3455
3456enum class BootOptionParameter : size_t
3457{
3458 setInProgress = 0x0,
3459 bootFlags = 0x5,
3460};
3461static constexpr uint8_t setComplete = 0x0;
3462static constexpr uint8_t setInProgress = 0x1;
3463static uint8_t transferStatus = setComplete;
3464static constexpr uint8_t setParmVersion = 0x01;
3465static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
3466static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
3467static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
3468static constexpr uint8_t httpBoot = 0xd;
3469static constexpr uint8_t bootSourceMask = 0x3c;
3470
3471} // namespace boot_options
3472
3473ipmi::RspType<uint8_t, // version
3474 uint8_t, // param
3475 uint8_t, // data0, dependent on parameter
3476 std::optional<uint8_t> // data1, dependent on parameter
3477 >
3478 ipmiOemGetEfiBootOptions(uint8_t parameter, uint8_t set, uint8_t block)
3479{
3480 using namespace boot_options;
3481 uint8_t bootOption = 0;
3482
3483 if (parameter == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3484 {
3485 return ipmi::responseSuccess(setParmVersion, parameter, transferStatus,
3486 std::nullopt);
3487 }
3488
3489 if (parameter != static_cast<uint8_t>(BootOptionParameter::bootFlags))
3490 {
3491 phosphor::logging::log<phosphor::logging::level::ERR>(
3492 "Unsupported parameter");
Jayaprakash Mutyala3694d072021-07-22 10:34:37 +00003493 return ipmi::response(ccParameterNotSupported);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003494 }
3495
3496 try
3497 {
3498 auto oneTimeEnabled = false;
3499 // read one time Enabled property
3500 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3501 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3502 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3503 enabledIntf, oneTimeBootEnableProp);
3504 oneTimeEnabled = std::get<bool>(variant);
3505
3506 // get BootSource and BootMode properties
3507 // according to oneTimeEnable
3508 auto bootObjPath = oneTimePath;
3509 if (oneTimeEnabled == false)
3510 {
3511 bootObjPath = persistentObjPath;
3512 }
3513
3514 service = getService(*dbus, bootModeIntf, bootObjPath);
3515 variant = getDbusProperty(*dbus, service, bootObjPath, bootModeIntf,
3516 bootModeProp);
3517
3518 auto bootMode =
3519 Mode::convertModesFromString(std::get<std::string>(variant));
3520
3521 service = getService(*dbus, bootSourceIntf, bootObjPath);
3522 variant = getDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3523 bootSourceProp);
3524
3525 if (std::get<std::string>(variant) == httpBootMode)
3526 {
3527 bootOption = httpBoot;
3528 }
3529 else
3530 {
3531 auto bootSource = Source::convertSourcesFromString(
3532 std::get<std::string>(variant));
3533 bootOption = sourceDbusToIpmi.at(bootSource);
3534 if (Source::Sources::Default == bootSource)
3535 {
3536 bootOption = modeDbusToIpmi.at(bootMode);
3537 }
3538 }
3539
3540 uint8_t oneTime = oneTimeEnabled ? setParmBootFlagsValidOneTime
3541 : setParmBootFlagsValidPermanent;
3542 bootOption <<= 2; // shift for responseconstexpr
3543 return ipmi::responseSuccess(setParmVersion, parameter, oneTime,
3544 bootOption);
3545 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003546 catch (const sdbusplus::exception_t& e)
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003547 {
3548 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3549 return ipmi::responseResponseError();
3550 }
3551}
3552
3553ipmi::RspType<> ipmiOemSetEfiBootOptions(uint8_t bootFlag, uint8_t bootParam,
3554 std::optional<uint8_t> bootOption)
3555{
3556 using namespace boot_options;
3557 auto oneTimeEnabled = false;
3558
3559 if (bootFlag == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3560 {
3561 if (bootOption)
3562 {
3563 return ipmi::responseReqDataLenInvalid();
3564 }
3565
3566 if (transferStatus == setInProgress)
3567 {
3568 phosphor::logging::log<phosphor::logging::level::ERR>(
3569 "boot option set in progress!");
3570 return ipmi::responseResponseError();
3571 }
3572
3573 transferStatus = bootParam;
3574 return ipmi::responseSuccess();
3575 }
3576
3577 if (bootFlag != (uint8_t)BootOptionParameter::bootFlags)
3578 {
3579 phosphor::logging::log<phosphor::logging::level::ERR>(
3580 "Unsupported parameter");
Jayaprakash Mutyala3694d072021-07-22 10:34:37 +00003581 return ipmi::response(ccParameterNotSupported);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003582 }
3583
3584 if (!bootOption)
3585 {
3586 return ipmi::responseReqDataLenInvalid();
3587 }
3588
3589 if (((bootOption.value() & bootSourceMask) >> 2) !=
3590 httpBoot) // not http boot, exit
3591 {
3592 phosphor::logging::log<phosphor::logging::level::ERR>(
3593 "wrong boot option parameter!");
3594 return ipmi::responseParmOutOfRange();
3595 }
3596
3597 try
3598 {
3599 bool permanent = (bootParam & setParmBootFlagsPermanent) ==
3600 setParmBootFlagsPermanent;
3601
3602 // read one time Enabled property
3603 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3604 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3605 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3606 enabledIntf, oneTimeBootEnableProp);
3607 oneTimeEnabled = std::get<bool>(variant);
3608
3609 /*
3610 * Check if the current boot setting is onetime or permanent, if the
3611 * request in the command is otherwise, then set the "Enabled"
3612 * property in one_time object path to 'True' to indicate onetime
3613 * and 'False' to indicate permanent.
3614 *
3615 * Once the onetime/permanent setting is applied, then the bootMode
3616 * and bootSource is updated for the corresponding object.
3617 */
3618 if (permanent == oneTimeEnabled)
3619 {
3620 setDbusProperty(*dbus, service, oneTimePath, enabledIntf,
3621 oneTimeBootEnableProp, !permanent);
3622 }
3623
3624 // set BootSource and BootMode properties
3625 // according to oneTimeEnable or persistent
3626 auto bootObjPath = oneTimePath;
3627 if (oneTimeEnabled == false)
3628 {
3629 bootObjPath = persistentObjPath;
3630 }
3631 std::string bootMode =
3632 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
3633 std::string bootSource = httpBootMode;
3634
3635 service = getService(*dbus, bootModeIntf, bootObjPath);
3636 setDbusProperty(*dbus, service, bootObjPath, bootModeIntf, bootModeProp,
3637 bootMode);
3638
3639 service = getService(*dbus, bootSourceIntf, bootObjPath);
3640 setDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3641 bootSourceProp, bootSource);
3642 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -05003643 catch (const sdbusplus::exception_t& e)
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003644 {
3645 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3646 return ipmi::responseResponseError();
3647 }
3648
3649 return ipmi::responseSuccess();
3650}
3651
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003652using BasicVariantType =
3653 std::variant<std::vector<std::string>, std::vector<uint64_t>, std::string,
3654 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
3655 uint16_t, uint8_t, bool>;
3656using PropertyMapType =
3657 boost::container::flat_map<std::string, BasicVariantType>;
3658static constexpr const std::array<const char*, 1> psuPresenceTypes = {
3659 "xyz.openbmc_project.Configuration.PSUPresence"};
3660int getPSUAddress(ipmi::Context::ptr ctx, uint8_t& bus,
3661 std::vector<uint64_t>& addrTable)
3662{
3663 boost::system::error_code ec;
3664 GetSubTreeType subtree = ctx->bus->yield_method_call<GetSubTreeType>(
3665 ctx->yield, ec, "xyz.openbmc_project.ObjectMapper",
3666 "/xyz/openbmc_project/object_mapper",
3667 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3668 "/xyz/openbmc_project/inventory/system", 3, psuPresenceTypes);
3669 if (ec)
3670 {
3671 phosphor::logging::log<phosphor::logging::level::ERR>(
3672 "Failed to set dbus property to cold redundancy");
3673 return -1;
3674 }
3675 for (const auto& object : subtree)
3676 {
3677 std::string pathName = object.first;
3678 for (const auto& serviceIface : object.second)
3679 {
3680 std::string serviceName = serviceIface.first;
3681
3682 ec.clear();
3683 PropertyMapType propMap =
3684 ctx->bus->yield_method_call<PropertyMapType>(
3685 ctx->yield, ec, serviceName, pathName,
3686 "org.freedesktop.DBus.Properties", "GetAll",
3687 "xyz.openbmc_project.Configuration.PSUPresence");
3688 if (ec)
3689 {
3690 phosphor::logging::log<phosphor::logging::level::ERR>(
3691 "Failed to set dbus property to cold redundancy");
3692 return -1;
3693 }
3694 auto psuBus = std::get_if<uint64_t>(&propMap["Bus"]);
3695 auto psuAddress =
3696 std::get_if<std::vector<uint64_t>>(&propMap["Address"]);
3697
3698 if (psuBus == nullptr || psuAddress == nullptr)
3699 {
3700 std::cerr << "error finding necessary "
3701 "entry in configuration\n";
3702 return -1;
3703 }
3704 bus = static_cast<uint8_t>(*psuBus);
3705 addrTable = *psuAddress;
3706 return 0;
3707 }
3708 }
3709 return -1;
3710}
3711
3712static const constexpr uint8_t addrOffset = 8;
3713static const constexpr uint8_t psuRevision = 0xd9;
3714static const constexpr uint8_t defaultPSUBus = 7;
3715// Second Minor, Primary Minor, Major
3716static const constexpr size_t verLen = 3;
3717ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPSUVersion(ipmi::Context::ptr ctx)
3718{
3719 uint8_t bus = defaultPSUBus;
3720 std::vector<uint64_t> addrTable;
3721 std::vector<uint8_t> result;
3722 if (getPSUAddress(ctx, bus, addrTable))
3723 {
3724 std::cerr << "Failed to get PSU bus and address\n";
3725 return ipmi::responseResponseError();
3726 }
3727
3728 for (const auto& slaveAddr : addrTable)
3729 {
3730 std::vector<uint8_t> writeData = {psuRevision};
3731 std::vector<uint8_t> readBuf(verLen);
3732 uint8_t addr = static_cast<uint8_t>(slaveAddr) + addrOffset;
3733 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
3734
3735 auto retI2C = ipmi::i2cWriteRead(i2cBus, addr, writeData, readBuf);
3736 if (retI2C != ipmi::ccSuccess)
3737 {
3738 for (size_t idx = 0; idx < verLen; idx++)
3739 {
3740 result.emplace_back(0x00);
3741 }
3742 }
3743 else
3744 {
3745 for (const uint8_t& data : readBuf)
3746 {
3747 result.emplace_back(data);
3748 }
3749 }
3750 }
3751
3752 return ipmi::responseSuccess(result);
3753}
3754
srikanta mondal2030d7c2020-05-03 17:25:25 +00003755std::optional<uint8_t> getMultiNodeInfoPresence(ipmi::Context::ptr ctx,
3756 const std::string& name)
3757{
3758 Value dbusValue = 0;
3759 std::string serviceName;
3760
3761 boost::system::error_code ec =
3762 ipmi::getService(ctx, multiNodeIntf, multiNodeObjPath, serviceName);
3763
3764 if (ec)
3765 {
3766 phosphor::logging::log<phosphor::logging::level::ERR>(
3767 "Failed to perform Multinode getService.");
3768 return std::nullopt;
3769 }
3770
3771 ec = ipmi::getDbusProperty(ctx, serviceName, multiNodeObjPath,
3772 multiNodeIntf, name, dbusValue);
3773 if (ec)
3774 {
3775 phosphor::logging::log<phosphor::logging::level::ERR>(
3776 "Failed to perform Multinode get property");
3777 return std::nullopt;
3778 }
3779
3780 auto multiNodeVal = std::get_if<uint8_t>(&dbusValue);
3781 if (!multiNodeVal)
3782 {
3783 phosphor::logging::log<phosphor::logging::level::ERR>(
3784 "getMultiNodeInfoPresence: error to get multinode");
3785 return std::nullopt;
3786 }
3787 return *multiNodeVal;
3788}
3789
3790/** @brief implements OEM get reading command
3791 * @param domain ID
3792 * @param reading Type
3793 * - 00h = platform Power Consumption
3794 * - 01h = inlet Air Temp
3795 * - 02h = icc_TDC from PECI
3796 * @param reserved, write as 0000h
3797 *
3798 * @returns IPMI completion code plus response data
3799 * - response
3800 * - domain ID
3801 * - reading Type
3802 * - 00h = platform Power Consumption
3803 * - 01h = inlet Air Temp
3804 * - 02h = icc_TDC from PECI
3805 * - reading
3806 */
3807ipmi::RspType<uint4_t, // domain ID
3808 uint4_t, // reading Type
3809 uint16_t // reading Value
3810 >
3811 ipmiOEMGetReading(ipmi::Context::ptr ctx, uint4_t domainId,
3812 uint4_t readingType, uint16_t reserved)
3813{
3814 constexpr uint8_t platformPower = 0;
3815 constexpr uint8_t inletAirTemp = 1;
3816 constexpr uint8_t iccTdc = 2;
3817
3818 if ((static_cast<uint8_t>(readingType) > iccTdc) || domainId || reserved)
3819 {
3820 return ipmi::responseInvalidFieldRequest();
3821 }
3822
3823 // This command should run only from multi-node product.
3824 // For all other platforms this command will return invalid.
3825
3826 std::optional<uint8_t> nodeInfo =
3827 getMultiNodeInfoPresence(ctx, "NodePresence");
3828 if (!nodeInfo || !*nodeInfo)
3829 {
3830 return ipmi::responseInvalidCommand();
3831 }
3832
3833 uint16_t oemReadingValue = 0;
3834 if (static_cast<uint8_t>(readingType) == inletAirTemp)
3835 {
3836 double value = 0;
3837 boost::system::error_code ec = ipmi::getDbusProperty(
3838 ctx, "xyz.openbmc_project.HwmonTempSensor",
3839 "/xyz/openbmc_project/sensors/temperature/Inlet_BRD_Temp",
3840 "xyz.openbmc_project.Sensor.Value", "Value", value);
3841 if (ec)
3842 {
3843 phosphor::logging::log<phosphor::logging::level::ERR>(
3844 "Failed to get BMC Get OEM temperature",
3845 phosphor::logging::entry("EXCEPTION=%s", ec.message().c_str()));
3846 return ipmi::responseUnspecifiedError();
3847 }
3848 // Take the Inlet temperature
3849 oemReadingValue = static_cast<uint16_t>(value);
3850 }
3851 else
3852 {
3853 phosphor::logging::log<phosphor::logging::level::ERR>(
3854 "Currently Get OEM Reading support only for Inlet Air Temp");
3855 return ipmi::responseParmOutOfRange();
3856 }
3857 return ipmi::responseSuccess(domainId, readingType, oemReadingValue);
3858}
3859
AppaRao Puli28972062019-11-11 02:04:45 +05303860/** @brief implements the maximum size of
3861 * bridgeable messages used between KCS and
3862 * IPMB interfacesget security mode command.
3863 *
3864 * @returns IPMI completion code with following data
3865 * - KCS Buffer Size (In multiples of four bytes)
3866 * - IPMB Buffer Size (In multiples of four bytes)
3867 **/
3868ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetBufferSize()
3869{
3870 // for now this is hard coded; really this number is dependent on
3871 // the BMC kcs driver as well as the host kcs driver....
3872 // we can't know the latter.
3873 uint8_t kcsMaxBufferSize = 63 / 4;
3874 uint8_t ipmbMaxBufferSize = 128 / 4;
3875
3876 return ipmi::responseSuccess(kcsMaxBufferSize, ipmbMaxBufferSize);
3877}
3878
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +00003879ipmi::RspType<std::vector<uint8_t>>
3880 ipmiOEMReadPFRMailbox(ipmi::Context::ptr& ctx, const uint8_t readRegister,
3881 const uint8_t numOfBytes, uint8_t registerIdentifier)
3882{
3883 if (!ipmi::mailbox::i2cConfigLoaded)
3884 {
3885
3886 phosphor::logging::log<phosphor::logging::level::ERR>(
3887 "Calling PFR Load Configuration Function to Get I2C Bus and Slave "
3888 "Address ");
3889
3890 ipmi::mailbox::loadPfrConfig(ctx, ipmi::mailbox::i2cConfigLoaded);
3891 }
3892
3893 if (!numOfBytes && !readRegister)
3894 {
3895 phosphor::logging::log<phosphor::logging::level::ERR>(
3896 "OEM IPMI command: Read & write count are 0 which is invalid ");
3897 return ipmi::responseInvalidFieldRequest();
3898 }
3899
3900 switch (registerIdentifier)
3901 {
3902 case ipmi::mailbox::registerType::fifoReadRegister:
3903 {
3904 // Check if readRegister is an FIFO read register
3905 if (registerIdentifier == 1)
3906 {
3907 if (ipmi::mailbox::readFifoReg.find(readRegister) ==
3908 ipmi::mailbox::readFifoReg.end())
3909 {
3910 phosphor::logging::log<phosphor::logging::level::ERR>(
3911 "OEM IPMI command: Register is not a Read FIFO ");
3912 return ipmi::responseInvalidFieldRequest();
3913 }
3914
3915 phosphor::logging::log<phosphor::logging::level::ERR>(
3916 "OEM IPMI command: Register is a Read FIFO ");
3917
3918 ipmi::mailbox::writefifo(ipmi::mailbox::provisioningCommand,
3919 readRegister);
3920 ipmi::mailbox::writefifo(ipmi::mailbox::triggerCommand,
3921 ipmi::mailbox::flushRead);
3922
3923 std::vector<uint8_t> writeData = {ipmi::mailbox::readFifo};
3924 std::vector<uint8_t> readBuf(1);
3925 std::vector<uint8_t> result;
3926
3927 for (int i = 0; i < numOfBytes; i++)
3928 {
3929
3930 ipmi::Cc ret = ipmi::i2cWriteRead(ipmi::mailbox::i2cBus,
3931 ipmi::mailbox::slaveAddr,
3932 writeData, readBuf);
3933 if (ret != ipmi::ccSuccess)
3934 {
3935 return ipmi::response(ret);
3936 }
3937
3938 else
3939 {
3940 for (const uint8_t& data : readBuf)
3941 {
3942 result.emplace_back(data);
3943 }
3944 }
3945 }
3946
3947 return ipmi::responseSuccess(result);
3948 }
3949 }
3950
3951 case ipmi::mailbox::registerType::singleByteRegister:
3952 {
3953 phosphor::logging::log<phosphor::logging::level::ERR>(
3954 "OEM IPMI command: Register is a Single Byte Register ");
3955
3956 std::vector<uint8_t> writeData = {readRegister};
3957 std::vector<uint8_t> readBuf(numOfBytes);
3958
3959 ipmi::Cc ret = ipmi::i2cWriteRead(ipmi::mailbox::i2cBus,
3960 ipmi::mailbox::slaveAddr,
3961 writeData, readBuf);
3962 if (ret != ipmi::ccSuccess)
3963 {
3964 return ipmi::response(ret);
3965 }
3966 return ipmi::responseSuccess(readBuf);
3967 }
3968
3969 default:
3970 {
3971
3972 phosphor::logging::log<phosphor::logging::level::ERR>(
3973 "OEM IPMI command: Register identifier is not valid.It should "
3974 "be 0 "
3975 "for Single Byte Register and 1 for FIFO Read Register");
3976
3977 return ipmi::responseInvalidFieldRequest();
3978 }
3979 }
3980}
3981
Jason M. Bills64796042018-10-03 16:51:55 -07003982static void registerOEMFunctions(void)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003983{
3984 phosphor::logging::log<phosphor::logging::level::INFO>(
3985 "Registering OEM commands");
Vernon Mauery98bbf692019-09-16 11:14:59 -07003986 ipmiPrintAndRegister(intel::netFnGeneral,
3987 intel::general::cmdGetChassisIdentifier, NULL,
3988 ipmiOEMGetChassisIdentifier,
3989 PRIVILEGE_USER); // get chassis identifier
3990
3991 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetSystemGUID,
3992 NULL, ipmiOEMSetSystemGUID,
3993 PRIVILEGE_ADMIN); // set system guid
Jason M. Billsb02bf092019-08-15 13:01:56 -07003994
3995 // <Disable BMC System Reset Action>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003996 registerHandler(prioOemBase, intel::netFnGeneral,
3997 intel::general::cmdDisableBMCSystemReset, Privilege::Admin,
3998 ipmiOEMDisableBMCSystemReset);
3999
Jason M. Billsb02bf092019-08-15 13:01:56 -07004000 // <Get BMC Reset Disables>
Vernon Mauery98bbf692019-09-16 11:14:59 -07004001 registerHandler(prioOemBase, intel::netFnGeneral,
4002 intel::general::cmdGetBMCResetDisables, Privilege::Admin,
4003 ipmiOEMGetBMCResetDisables);
Jason M. Billsb02bf092019-08-15 13:01:56 -07004004
Vernon Mauery98bbf692019-09-16 11:14:59 -07004005 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetBIOSID,
4006 NULL, ipmiOEMSetBIOSID, PRIVILEGE_ADMIN);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08004007
Chen Yugang7a04f3a2019-10-08 11:12:35 +08004008 registerHandler(prioOemBase, intel::netFnGeneral,
4009 intel::general::cmdGetOEMDeviceInfo, Privilege::User,
4010 ipmiOEMGetDeviceInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08004011
Vernon Mauery98bbf692019-09-16 11:14:59 -07004012 ipmiPrintAndRegister(intel::netFnGeneral,
4013 intel::general::cmdGetAICSlotFRUIDSlotPosRecords, NULL,
4014 ipmiOEMGetAICFRU, PRIVILEGE_USER);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05304015
Vernon Mauery98bbf692019-09-16 11:14:59 -07004016 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4017 intel::general::cmdSendEmbeddedFWUpdStatus,
4018 Privilege::Operator, ipmiOEMSendEmbeddedFwUpdStatus);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05304019
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +05304020 registerHandler(prioOpenBmcBase, intel::netFnApp, intel::app::cmdSlotIpmb,
4021 Privilege::Admin, ipmiOEMSlotIpmb);
4022
Vernon Mauery98bbf692019-09-16 11:14:59 -07004023 ipmiPrintAndRegister(intel::netFnGeneral,
4024 intel::general::cmdSetPowerRestoreDelay, NULL,
4025 ipmiOEMSetPowerRestoreDelay, PRIVILEGE_OPERATOR);
4026
4027 ipmiPrintAndRegister(intel::netFnGeneral,
4028 intel::general::cmdGetPowerRestoreDelay, NULL,
4029 ipmiOEMGetPowerRestoreDelay, PRIVILEGE_USER);
4030
4031 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4032 intel::general::cmdSetOEMUser2Activation,
4033 Privilege::Callback, ipmiOEMSetUser2Activation);
4034
4035 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4036 intel::general::cmdSetSpecialUserPassword,
4037 Privilege::Callback, ipmiOEMSetSpecialUserPassword);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05304038
Jason M. Bills42bd9c82019-06-28 16:39:34 -07004039 // <Get Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07004040 registerHandler(prioOemBase, intel::netFnGeneral,
4041 intel::general::cmdGetProcessorErrConfig, Privilege::User,
4042 ipmiOEMGetProcessorErrConfig);
4043
Jason M. Bills42bd9c82019-06-28 16:39:34 -07004044 // <Set Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07004045 registerHandler(prioOemBase, intel::netFnGeneral,
4046 intel::general::cmdSetProcessorErrConfig, Privilege::Admin,
4047 ipmiOEMSetProcessorErrConfig);
Jason M. Bills42bd9c82019-06-28 16:39:34 -07004048
Vernon Mauery98bbf692019-09-16 11:14:59 -07004049 ipmiPrintAndRegister(intel::netFnGeneral,
4050 intel::general::cmdSetShutdownPolicy, NULL,
4051 ipmiOEMSetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08004052
Vernon Mauery98bbf692019-09-16 11:14:59 -07004053 ipmiPrintAndRegister(intel::netFnGeneral,
4054 intel::general::cmdGetShutdownPolicy, NULL,
4055 ipmiOEMGetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08004056
anil kumar appanaf945eee2019-09-25 23:29:11 +00004057 registerHandler(prioOemBase, intel::netFnGeneral,
4058 intel::general::cmdSetFanConfig, Privilege::User,
4059 ipmiOEMSetFanConfig);
James Feist91244a62019-02-19 15:04:54 -08004060
Vernon Mauery98bbf692019-09-16 11:14:59 -07004061 registerHandler(prioOemBase, intel::netFnGeneral,
4062 intel::general::cmdGetFanConfig, Privilege::User,
4063 ipmiOEMGetFanConfig);
James Feist5f957ca2019-03-14 15:33:55 -07004064
Vernon Mauery98bbf692019-09-16 11:14:59 -07004065 registerHandler(prioOemBase, intel::netFnGeneral,
4066 intel::general::cmdGetFanSpeedOffset, Privilege::User,
4067 ipmiOEMGetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07004068
Vernon Mauery98bbf692019-09-16 11:14:59 -07004069 registerHandler(prioOemBase, intel::netFnGeneral,
4070 intel::general::cmdSetFanSpeedOffset, Privilege::User,
4071 ipmiOEMSetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07004072
Vernon Mauery98bbf692019-09-16 11:14:59 -07004073 registerHandler(prioOemBase, intel::netFnGeneral,
4074 intel::general::cmdSetFscParameter, Privilege::User,
4075 ipmiOEMSetFscParameter);
James Feist5f957ca2019-03-14 15:33:55 -07004076
Vernon Mauery98bbf692019-09-16 11:14:59 -07004077 registerHandler(prioOemBase, intel::netFnGeneral,
4078 intel::general::cmdGetFscParameter, Privilege::User,
4079 ipmiOEMGetFscParameter);
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05304080
Vernon Mauery98bbf692019-09-16 11:14:59 -07004081 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
4082 intel::general::cmdReadBaseBoardProductId, Privilege::Admin,
4083 ipmiOEMReadBoardProductId);
Chen Yugang39736d52019-07-12 16:24:33 +08004084
Vernon Mauery98bbf692019-09-16 11:14:59 -07004085 registerHandler(prioOemBase, intel::netFnGeneral,
4086 intel::general::cmdGetNmiStatus, Privilege::User,
4087 ipmiOEMGetNmiSource);
Chen Yugang39736d52019-07-12 16:24:33 +08004088
Vernon Mauery98bbf692019-09-16 11:14:59 -07004089 registerHandler(prioOemBase, intel::netFnGeneral,
4090 intel::general::cmdSetNmiStatus, Privilege::Operator,
4091 ipmiOEMSetNmiSource);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08004092
Vernon Mauery98bbf692019-09-16 11:14:59 -07004093 registerHandler(prioOemBase, intel::netFnGeneral,
4094 intel::general::cmdGetEfiBootOptions, Privilege::User,
4095 ipmiOemGetEfiBootOptions);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08004096
Vernon Mauery98bbf692019-09-16 11:14:59 -07004097 registerHandler(prioOemBase, intel::netFnGeneral,
4098 intel::general::cmdSetEfiBootOptions, Privilege::Operator,
4099 ipmiOemSetEfiBootOptions);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05304100
Vernon Mauery98bbf692019-09-16 11:14:59 -07004101 registerHandler(prioOemBase, intel::netFnGeneral,
4102 intel::general::cmdGetSecurityMode, Privilege::User,
4103 ipmiGetSecurityMode);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05304104
Vernon Mauery98bbf692019-09-16 11:14:59 -07004105 registerHandler(prioOemBase, intel::netFnGeneral,
4106 intel::general::cmdSetSecurityMode, Privilege::Admin,
4107 ipmiSetSecurityMode);
Vernon Mauery4ac799d2019-05-20 15:50:37 -07004108
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00004109 registerHandler(prioOemBase, intel::netFnGeneral,
4110 intel::general::cmdGetLEDStatus, Privilege::Admin,
4111 ipmiOEMGetLEDStatus);
Cheng C Yang773703a2019-08-15 09:41:11 +08004112
Vernon Mauery98bbf692019-09-16 11:14:59 -07004113 ipmiPrintAndRegister(ipmi::intel::netFnPlatform,
4114 ipmi::intel::platform::cmdCfgHostSerialPortSpeed, NULL,
4115 ipmiOEMCfgHostSerialPortSpeed, PRIVILEGE_ADMIN);
4116
4117 registerHandler(prioOemBase, intel::netFnGeneral,
4118 intel::general::cmdSetFaultIndication, Privilege::Operator,
4119 ipmiOEMSetFaultIndication);
4120
4121 registerHandler(prioOemBase, intel::netFnGeneral,
4122 intel::general::cmdSetColdRedundancyConfig, Privilege::User,
4123 ipmiOEMSetCRConfig);
4124
4125 registerHandler(prioOemBase, intel::netFnGeneral,
4126 intel::general::cmdGetColdRedundancyConfig, Privilege::User,
4127 ipmiOEMGetCRConfig);
4128
4129 registerHandler(prioOemBase, intel::netFnGeneral,
4130 intel::general::cmdRestoreConfiguration, Privilege::Admin,
Vernon Mauery4ac799d2019-05-20 15:50:37 -07004131 ipmiRestoreConfiguration);
James Feist63efafa2019-07-24 12:39:21 -07004132
Vernon Mauery98bbf692019-09-16 11:14:59 -07004133 registerHandler(prioOemBase, intel::netFnGeneral,
4134 intel::general::cmdSetDimmOffset, Privilege::Operator,
4135 ipmiOEMSetDimmOffset);
James Feist63efafa2019-07-24 12:39:21 -07004136
Vernon Mauery98bbf692019-09-16 11:14:59 -07004137 registerHandler(prioOemBase, intel::netFnGeneral,
4138 intel::general::cmdGetDimmOffset, Privilege::Operator,
4139 ipmiOEMGetDimmOffset);
Chen Yugangca12a7b2019-09-03 18:11:44 +08004140
Cheng C Yang4e6ee152019-09-25 10:27:44 +08004141 registerHandler(prioOemBase, intel::netFnGeneral,
4142 intel::general::cmdGetPSUVersion, Privilege::User,
4143 ipmiOEMGetPSUVersion);
AppaRao Puli28972062019-11-11 02:04:45 +05304144
4145 registerHandler(prioOemBase, intel::netFnGeneral,
4146 intel::general::cmdGetBufferSize, Privilege::User,
4147 ipmiOEMGetBufferSize);
srikanta mondal2030d7c2020-05-03 17:25:25 +00004148
4149 registerHandler(prioOemBase, intel::netFnGeneral,
4150 intel::general::cmdOEMGetReading, Privilege::User,
4151 ipmiOEMGetReading);
Ankita Vilas Gawadea1650382022-01-08 10:30:40 +00004152
4153 registerHandler(prioOemBase, intel::netFnApp, intel::app::cmdPFRMailboxRead,
4154 Privilege::Admin, ipmiOEMReadPFRMailbox);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08004155}
4156
4157} // namespace ipmi