blob: 107a1fe0a036661e34eb199131f246497f0f9b40 [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
Jia, Chunhuicc49b542019-03-20 15:41:07 +080021#include <systemd/sd-journal.h>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080022
Chen Yugang7a04f3a2019-10-08 11:12:35 +080023#include <appcommands.hpp>
James Feist91244a62019-02-19 15:04:54 -080024#include <boost/container/flat_map.hpp>
Yong Li23737fe2019-02-19 08:49:55 +080025#include <boost/process/child.hpp>
26#include <boost/process/io.hpp>
Yong Li0669d192019-05-06 14:01:46 +080027#include <com/intel/Control/OCOTShutdownPolicy/server.hpp>
Jason M. Bills64796042018-10-03 16:51:55 -070028#include <commandutils.hpp>
Zhikui Rence4e73f2019-12-06 13:59:47 -080029#include <gpiod.hpp>
Jia, Chunhuicc49b542019-03-20 15:41:07 +080030#include <ipmid/api.hpp>
Vernon Mauery5480ef62019-03-20 13:43:11 -070031#include <ipmid/utils.hpp>
James Feist63efafa2019-07-24 12:39:21 -070032#include <nlohmann/json.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080033#include <oemcommands.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080034#include <phosphor-logging/log.hpp>
35#include <sdbusplus/bus.hpp>
Suryakanth Sekard509eb92018-11-15 17:44:11 +053036#include <sdbusplus/message/types.hpp>
Chen Yugang97cf96e2019-11-01 08:55:11 +080037#include <xyz/openbmc_project/Chassis/Control/NMISource/server.hpp>
Chen,Yugang4f7e76b2019-08-20 09:28:06 +080038#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
39#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
Cheng C Yang773703a2019-08-15 09:41:11 +080040#include <xyz/openbmc_project/Control/PowerSupplyRedundancy/server.hpp>
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053041#include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp>
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +053042#include <xyz/openbmc_project/Control/Security/SpecialMode/server.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080043
James Feistfcd2d3a2020-05-28 10:38:15 -070044#include <array>
45#include <filesystem>
46#include <iostream>
47#include <regex>
48#include <string>
49#include <variant>
50#include <vector>
51
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080052namespace ipmi
53{
Jason M. Bills64796042018-10-03 16:51:55 -070054static void registerOEMFunctions() __attribute__((constructor));
Vernon Mauery4ac799d2019-05-20 15:50:37 -070055
Jason M. Bills64796042018-10-03 16:51:55 -070056static constexpr size_t maxFRUStringLength = 0x3F;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080057
Suryakanth Sekard509eb92018-11-15 17:44:11 +053058static constexpr auto ethernetIntf =
59 "xyz.openbmc_project.Network.EthernetInterface";
60static constexpr auto networkIPIntf = "xyz.openbmc_project.Network.IP";
61static constexpr auto networkService = "xyz.openbmc_project.Network";
62static constexpr auto networkRoot = "/xyz/openbmc_project/network";
63
Chen Yugang97cf96e2019-11-01 08:55:11 +080064static constexpr const char* oemNmiSourceIntf =
65 "xyz.openbmc_project.Chassis.Control.NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080066static constexpr const char* oemNmiSourceObjPath =
Chen Yugang97cf96e2019-11-01 08:55:11 +080067 "/xyz/openbmc_project/Chassis/Control/NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080068static constexpr const char* oemNmiBmcSourceObjPathProp = "BMCSource";
69static constexpr const char* oemNmiEnabledObjPathProp = "Enabled";
70
James Feist63efafa2019-07-24 12:39:21 -070071static constexpr const char* dimmOffsetFile = "/var/lib/ipmi/ipmi_dimms.json";
srikanta mondal2030d7c2020-05-03 17:25:25 +000072static constexpr const char* multiNodeObjPath =
73 "/xyz/openbmc_project/MultiNode/Status";
74static constexpr const char* multiNodeIntf =
75 "xyz.openbmc_project.Chassis.MultiNode";
James Feist63efafa2019-07-24 12:39:21 -070076
Chen Yugang39736d52019-07-12 16:24:33 +080077enum class NmiSource : uint8_t
78{
79 none = 0,
Chen Yugang97cf96e2019-11-01 08:55:11 +080080 frontPanelButton = 1,
81 watchdog = 2,
82 chassisCmd = 3,
83 memoryError = 4,
84 pciBusError = 5,
85 pch = 6,
86 chipset = 7,
Chen Yugang39736d52019-07-12 16:24:33 +080087};
88
Suryakanth Sekar822b0b42019-11-15 18:32:53 +053089enum class SpecialUserIndex : uint8_t
90{
91 rootUser = 0,
92 atScaleDebugUser = 1
93};
94
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053095static constexpr const char* restricionModeService =
96 "xyz.openbmc_project.RestrictionMode.Manager";
97static constexpr const char* restricionModeBasePath =
98 "/xyz/openbmc_project/control/security/restriction_mode";
99static constexpr const char* restricionModeIntf =
100 "xyz.openbmc_project.Control.Security.RestrictionMode";
101static constexpr const char* restricionModeProperty = "RestrictionMode";
102
103static constexpr const char* specialModeService =
104 "xyz.openbmc_project.SpecialMode";
105static constexpr const char* specialModeBasePath =
Richard Marian Thomaiyara7b74282019-09-22 21:53:14 +0530106 "/xyz/openbmc_project/security/special_mode";
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +0530107static constexpr const char* specialModeIntf =
108 "xyz.openbmc_project.Security.SpecialMode";
109static constexpr const char* specialModeProperty = "SpecialMode";
110
111static constexpr const char* dBusPropertyIntf =
112 "org.freedesktop.DBus.Properties";
113static constexpr const char* dBusPropertyGetMethod = "Get";
114static constexpr const char* dBusPropertySetMethod = "Set";
115
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800116// return code: 0 successful
117int8_t getChassisSerialNumber(sdbusplus::bus::bus& bus, std::string& serial)
118{
119 std::string objpath = "/xyz/openbmc_project/FruDevice";
120 std::string intf = "xyz.openbmc_project.FruDeviceManager";
121 std::string service = getService(bus, intf, objpath);
122 ObjectValueTree valueTree = getManagedObjects(bus, service, "/");
123 if (valueTree.empty())
124 {
125 phosphor::logging::log<phosphor::logging::level::ERR>(
126 "No object implements interface",
127 phosphor::logging::entry("INTF=%s", intf.c_str()));
128 return -1;
129 }
130
Jason M. Bills64796042018-10-03 16:51:55 -0700131 for (const auto& item : valueTree)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800132 {
133 auto interface = item.second.find("xyz.openbmc_project.FruDevice");
134 if (interface == item.second.end())
135 {
136 continue;
137 }
138
139 auto property = interface->second.find("CHASSIS_SERIAL_NUMBER");
140 if (property == interface->second.end())
141 {
142 continue;
143 }
144
145 try
146 {
147 Value variant = property->second;
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700148 std::string& result = std::get<std::string>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700149 if (result.size() > maxFRUStringLength)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800150 {
151 phosphor::logging::log<phosphor::logging::level::ERR>(
152 "FRU serial number exceed maximum length");
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800153 return -1;
154 }
Jason M. Bills64796042018-10-03 16:51:55 -0700155 serial = result;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800156 return 0;
157 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700158 catch (std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800159 {
Jason M. Bills64796042018-10-03 16:51:55 -0700160 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800161 return -1;
162 }
163 }
164 return -1;
165}
Jason M. Bills64796042018-10-03 16:51:55 -0700166
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800167// Returns the Chassis Identifier (serial #)
168ipmi_ret_t ipmiOEMGetChassisIdentifier(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
169 ipmi_request_t request,
170 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700171 ipmi_data_len_t dataLen,
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800172 ipmi_context_t context)
173{
174 std::string serial;
Jason M. Bills64796042018-10-03 16:51:55 -0700175 if (*dataLen != 0) // invalid request if there are extra parameters
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800176 {
Jason M. Bills64796042018-10-03 16:51:55 -0700177 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800178 return IPMI_CC_REQ_DATA_LEN_INVALID;
179 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700180 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
181 if (getChassisSerialNumber(*dbus, serial) == 0)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800182 {
Jason M. Bills64796042018-10-03 16:51:55 -0700183 *dataLen = serial.size(); // length will never exceed response length
184 // as it is checked in getChassisSerialNumber
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800185 char* resp = static_cast<char*>(response);
Jason M. Bills64796042018-10-03 16:51:55 -0700186 serial.copy(resp, *dataLen);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800187 return IPMI_CC_OK;
188 }
Jason M. Bills64796042018-10-03 16:51:55 -0700189 *dataLen = 0;
190 return IPMI_CC_RESPONSE_ERROR;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800191}
192
193ipmi_ret_t ipmiOEMSetSystemGUID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
194 ipmi_request_t request,
195 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700196 ipmi_data_len_t dataLen, ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800197{
198 static constexpr size_t safeBufferLength = 50;
199 char buf[safeBufferLength] = {0};
200 GUIDData* Data = reinterpret_cast<GUIDData*>(request);
201
Jason M. Bills64796042018-10-03 16:51:55 -0700202 if (*dataLen != sizeof(GUIDData)) // 16bytes
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800203 {
Jason M. Bills64796042018-10-03 16:51:55 -0700204 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800205 return IPMI_CC_REQ_DATA_LEN_INVALID;
206 }
207
Jason M. Bills64796042018-10-03 16:51:55 -0700208 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800209
210 snprintf(
211 buf, safeBufferLength,
212 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
213 Data->timeLow4, Data->timeLow3, Data->timeLow2, Data->timeLow1,
214 Data->timeMid2, Data->timeMid1, Data->timeHigh2, Data->timeHigh1,
215 Data->clock2, Data->clock1, Data->node6, Data->node5, Data->node4,
216 Data->node3, Data->node2, Data->node1);
217 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
218 std::string guid = buf;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800219
220 std::string objpath = "/xyz/openbmc_project/control/host0/systemGUID";
221 std::string intf = "xyz.openbmc_project.Common.UUID";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700222 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
223 std::string service = getService(*dbus, intf, objpath);
224 setDbusProperty(*dbus, service, objpath, intf, "UUID", guid);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800225 return IPMI_CC_OK;
226}
227
Jason M. Billsb02bf092019-08-15 13:01:56 -0700228ipmi::RspType<> ipmiOEMDisableBMCSystemReset(bool disableResetOnSMI,
229 uint7_t reserved1)
230{
231 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
232
233 try
234 {
235 auto service =
236 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
237 ipmi::setDbusProperty(*busp, service, bmcResetDisablesPath,
238 bmcResetDisablesIntf, "ResetOnSMI",
239 !disableResetOnSMI);
240 }
241 catch (std::exception& e)
242 {
243 phosphor::logging::log<phosphor::logging::level::ERR>(
244 "Failed to set BMC reset disables",
245 phosphor::logging::entry("EXCEPTION=%s", e.what()));
246 return ipmi::responseUnspecifiedError();
247 }
248
249 return ipmi::responseSuccess();
250}
251
252ipmi::RspType<bool, // disableResetOnSMI
253 uint7_t // reserved
254 >
255 ipmiOEMGetBMCResetDisables()
256{
257 bool disableResetOnSMI = true;
258
259 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
260 try
261 {
262 auto service =
263 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
264 Value variant =
265 ipmi::getDbusProperty(*busp, service, bmcResetDisablesPath,
266 bmcResetDisablesIntf, "ResetOnSMI");
267 disableResetOnSMI = !std::get<bool>(variant);
268 }
269 catch (std::exception& e)
270 {
271 phosphor::logging::log<phosphor::logging::level::ERR>(
272 "Failed to get BMC reset disables",
273 phosphor::logging::entry("EXCEPTION=%s", e.what()));
274 return ipmi::responseUnspecifiedError();
275 }
276
277 return ipmi::responseSuccess(disableResetOnSMI, 0);
278}
279
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800280ipmi_ret_t ipmiOEMSetBIOSID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
281 ipmi_request_t request, ipmi_response_t response,
282 ipmi_data_len_t dataLen, ipmi_context_t context)
283{
284 DeviceInfo* data = reinterpret_cast<DeviceInfo*>(request);
285
Jason M. Bills64796042018-10-03 16:51:55 -0700286 if ((*dataLen < 2) || (*dataLen != (1 + data->biosIDLength)))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800287 {
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800288 *dataLen = 0;
289 return IPMI_CC_REQ_DATA_LEN_INVALID;
290 }
Jason M. Bills64796042018-10-03 16:51:55 -0700291 std::string idString((char*)data->biosId, data->biosIDLength);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800292
Vernon Mauery15419dd2019-05-24 09:40:30 -0700293 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Chalapathi899bfd12020-04-15 15:07:02 +0000294 std::string service = getService(*dbus, biosVersionIntf, biosActiveObjPath);
295 setDbusProperty(*dbus, service, biosActiveObjPath, biosVersionIntf,
Yong Li2742b852019-12-16 14:55:11 +0800296 biosVersionProp, idString);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800297 uint8_t* bytesWritten = static_cast<uint8_t*>(response);
298 *bytesWritten =
Jason M. Bills64796042018-10-03 16:51:55 -0700299 data->biosIDLength; // how many bytes are written into storage
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800300 *dataLen = 1;
301 return IPMI_CC_OK;
302}
303
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530304bool getSwVerInfo(ipmi::Context::ptr ctx, uint8_t& bmcMajor, uint8_t& bmcMinor,
305 uint8_t& meMajor, uint8_t& meMinor)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800306{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800307 // step 1 : get BMC Major and Minor numbers from its DBUS property
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530308 std::string bmcVersion;
309 if (getActiveSoftwareVersionInfo(ctx, versionPurposeBMC, bmcVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800310 {
311 return false;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800312 }
313
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530314 std::optional<MetaRevision> rev = convertIntelVersion(bmcVersion);
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800315 if (rev.has_value())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800316 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800317 MetaRevision revision = rev.value();
318 bmcMajor = revision.major;
319
320 revision.minor = (revision.minor > 99 ? 99 : revision.minor);
321 bmcMinor = revision.minor % 10 + (revision.minor / 10) * 16;
322 }
323
324 // step 2 : get ME Major and Minor numbers from its DBUS property
AppaRao Puli32825a22020-01-17 15:52:41 +0530325 std::string meVersion;
326 if (getActiveSoftwareVersionInfo(ctx, versionPurposeME, meVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800327 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800328 return false;
329 }
AppaRao Puli32825a22020-01-17 15:52:41 +0530330 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?).(\\d+?).(\\d+?)");
331 constexpr size_t matchedPhosphor = 6;
332 std::smatch results;
333 if (std::regex_match(meVersion, results, pattern1))
334 {
335 if (results.size() == matchedPhosphor)
336 {
337 meMajor = static_cast<uint8_t>(std::stoi(results[1]));
338 meMinor = static_cast<uint8_t>(std::stoi(results[2]));
339 }
340 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800341 return true;
342}
343
344ipmi::RspType<
345 std::variant<std::string,
346 std::tuple<uint8_t, std::array<uint8_t, 2>,
347 std::array<uint8_t, 2>, std::array<uint8_t, 2>,
348 std::array<uint8_t, 2>, std::array<uint8_t, 2>>,
349 std::tuple<uint8_t, std::array<uint8_t, 2>>>>
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530350 ipmiOEMGetDeviceInfo(ipmi::Context::ptr ctx, uint8_t entityType,
351 std::optional<uint8_t> countToRead,
AppaRao Pulid46cb422020-01-21 18:40:21 +0530352 std::optional<uint8_t> offset)
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800353{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800354 if (entityType > static_cast<uint8_t>(OEMDevEntityType::sdrVer))
355 {
356 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800357 }
358
359 // handle OEM command items
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800360 switch (OEMDevEntityType(entityType))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800361 {
362 case OEMDevEntityType::biosId:
363 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530364 // Byte 2&3, Only used with selecting BIOS
365 if (!countToRead || !offset)
366 {
367 return ipmi::responseReqDataLenInvalid();
368 }
369
Vernon Mauery15419dd2019-05-24 09:40:30 -0700370 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800371 std::string service =
Chalapathi899bfd12020-04-15 15:07:02 +0000372 getService(*dbus, biosVersionIntf, biosActiveObjPath);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800373 try
374 {
Yong Li2742b852019-12-16 14:55:11 +0800375 Value variant =
Chalapathi899bfd12020-04-15 15:07:02 +0000376 getDbusProperty(*dbus, service, biosActiveObjPath,
Yong Li2742b852019-12-16 14:55:11 +0800377 biosVersionIntf, biosVersionProp);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700378 std::string& idString = std::get<std::string>(variant);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530379 if (*offset >= idString.size())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800380 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800381 return ipmi::responseParmOutOfRange();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800382 }
Jason M. Bills64796042018-10-03 16:51:55 -0700383 size_t length = 0;
AppaRao Pulid46cb422020-01-21 18:40:21 +0530384 if (*countToRead > (idString.size() - *offset))
Jason M. Bills64796042018-10-03 16:51:55 -0700385 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530386 length = idString.size() - *offset;
Jason M. Bills64796042018-10-03 16:51:55 -0700387 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800388 else
389 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530390 length = *countToRead;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800391 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800392
393 std::string readBuf = {0};
394 readBuf.resize(length);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530395 std::copy_n(idString.begin() + *offset, length,
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800396 (readBuf.begin()));
397 return ipmi::responseSuccess(readBuf);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800398 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700399 catch (std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800400 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800401 return ipmi::responseUnspecifiedError();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800402 }
403 }
404 break;
405
406 case OEMDevEntityType::devVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800407 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530408 // Byte 2&3, Only used with selecting BIOS
409 if (countToRead || offset)
410 {
411 return ipmi::responseReqDataLenInvalid();
412 }
413
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800414 constexpr const size_t verLen = 2;
415 constexpr const size_t verTotalLen = 10;
416 std::array<uint8_t, verLen> bmcBuf = {0xff, 0xff};
417 std::array<uint8_t, verLen> hsc0Buf = {0xff, 0xff};
418 std::array<uint8_t, verLen> hsc1Buf = {0xff, 0xff};
419 std::array<uint8_t, verLen> meBuf = {0xff, 0xff};
420 std::array<uint8_t, verLen> hsc2Buf = {0xff, 0xff};
421 // data0/1: BMC version number; data6/7: ME version number
422 // the others: HSC0/1/2 version number, not avaible.
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530423 if (!getSwVerInfo(ctx, bmcBuf[0], bmcBuf[1], meBuf[0], meBuf[1]))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800424 {
425 return ipmi::responseUnspecifiedError();
426 }
427 return ipmi::responseSuccess(
428 std::tuple<
429 uint8_t, std::array<uint8_t, verLen>,
430 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>,
431 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>>{
432 verTotalLen, bmcBuf, hsc0Buf, hsc1Buf, meBuf, hsc2Buf});
433 }
434 break;
435
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800436 case OEMDevEntityType::sdrVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800437 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530438 // Byte 2&3, Only used with selecting BIOS
439 if (countToRead || offset)
440 {
441 return ipmi::responseReqDataLenInvalid();
442 }
443
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800444 constexpr const size_t sdrLen = 2;
445 std::array<uint8_t, sdrLen> readBuf = {0x01, 0x0};
446 return ipmi::responseSuccess(
447 std::tuple<uint8_t, std::array<uint8_t, sdrLen>>{sdrLen,
448 readBuf});
449 }
450 break;
451
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800452 default:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800453 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800454 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800455}
456
457ipmi_ret_t ipmiOEMGetAICFRU(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
458 ipmi_request_t request, ipmi_response_t response,
459 ipmi_data_len_t dataLen, ipmi_context_t context)
460{
461 if (*dataLen != 0)
462 {
Jason M. Bills64796042018-10-03 16:51:55 -0700463 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800464 return IPMI_CC_REQ_DATA_LEN_INVALID;
465 }
466
467 *dataLen = 1;
468 uint8_t* res = reinterpret_cast<uint8_t*>(response);
469 // temporary fix. We don't support AIC FRU now. Just tell BIOS that no
470 // AIC is available so that BIOS will not timeout repeatly which leads to
471 // slow booting.
472 *res = 0; // Byte1=Count of SlotPosition/FruID records.
473 return IPMI_CC_OK;
474}
475
Jason M. Bills64796042018-10-03 16:51:55 -0700476ipmi_ret_t ipmiOEMGetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
477 ipmi_request_t request,
478 ipmi_response_t response,
479 ipmi_data_len_t dataLen,
480 ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800481{
Jason M. Bills64796042018-10-03 16:51:55 -0700482 GetPowerRestoreDelayRes* resp =
483 reinterpret_cast<GetPowerRestoreDelayRes*>(response);
484
485 if (*dataLen != 0)
486 {
487 *dataLen = 0;
488 return IPMI_CC_REQ_DATA_LEN_INVALID;
489 }
490
Vernon Mauery15419dd2019-05-24 09:40:30 -0700491 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700492 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700493 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
Jason M. Bills64796042018-10-03 16:51:55 -0700494 Value variant =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700495 getDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700496 powerRestoreDelayIntf, powerRestoreDelayProp);
497
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700498 uint16_t delay = std::get<uint16_t>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700499 resp->byteLSB = delay;
500 resp->byteMSB = delay >> 8;
501
502 *dataLen = sizeof(GetPowerRestoreDelayRes);
503
504 return IPMI_CC_OK;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800505}
506
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800507static uint8_t bcdToDec(uint8_t val)
508{
509 return ((val / 16 * 10) + (val % 16));
510}
511
512// Allows an update utility or system BIOS to send the status of an embedded
513// firmware update attempt to the BMC. After received, BMC will create a logging
514// record.
515ipmi::RspType<> ipmiOEMSendEmbeddedFwUpdStatus(uint8_t status, uint8_t target,
516 uint8_t majorRevision,
517 uint8_t minorRevision,
518 uint32_t auxInfo)
519{
520 std::string firmware;
Jason M. Billsdc249272019-04-03 09:58:40 -0700521 int instance = (target & targetInstanceMask) >> targetInstanceShift;
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800522 target = (target & selEvtTargetMask) >> selEvtTargetShift;
523
524 /* make sure the status is 0, 1, or 2 as per the spec */
525 if (status > 2)
526 {
527 return ipmi::response(ipmi::ccInvalidFieldRequest);
528 }
Jason M. Billsdc249272019-04-03 09:58:40 -0700529 /* make sure the target is 0, 1, 2, or 4 as per the spec */
530 if (target > 4 || target == 3)
531 {
532 return ipmi::response(ipmi::ccInvalidFieldRequest);
533 }
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800534 /*orignal OEM command is to record OEM SEL.
535 But openbmc does not support OEM SEL, so we redirect it to redfish event
536 logging. */
537 std::string buildInfo;
538 std::string action;
539 switch (FWUpdateTarget(target))
540 {
541 case FWUpdateTarget::targetBMC:
542 firmware = "BMC";
Jason M. Billsdc249272019-04-03 09:58:40 -0700543 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800544 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
545 " BuildID: " + std::to_string(auxInfo);
546 buildInfo += std::to_string(auxInfo);
547 break;
548 case FWUpdateTarget::targetBIOS:
549 firmware = "BIOS";
550 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700551 "major: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800552 std::to_string(bcdToDec(majorRevision)) + // BCD encoded
553 " minor: " +
554 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
555 " ReleaseNumber: " + // ASCII encoded
556 std::to_string(static_cast<uint8_t>(auxInfo >> 0) - '0') +
557 std::to_string(static_cast<uint8_t>(auxInfo >> 8) - '0') +
558 std::to_string(static_cast<uint8_t>(auxInfo >> 16) - '0') +
559 std::to_string(static_cast<uint8_t>(auxInfo >> 24) - '0');
560 break;
561 case FWUpdateTarget::targetME:
562 firmware = "ME";
563 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700564 "major: " + std::to_string(majorRevision) + " minor1: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800565 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
566 " minor2: " +
567 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 0))) +
568 " build1: " +
569 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 8))) +
570 " build2: " +
571 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 16)));
572 break;
573 case FWUpdateTarget::targetOEMEWS:
574 firmware = "EWS";
Jason M. Billsdc249272019-04-03 09:58:40 -0700575 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800576 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
577 " BuildID: " + std::to_string(auxInfo);
578 break;
579 }
580
Jason M. Billsdc249272019-04-03 09:58:40 -0700581 static const std::string openBMCMessageRegistryVersion("0.1");
582 std::string redfishMsgID = "OpenBMC." + openBMCMessageRegistryVersion;
583
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800584 switch (status)
585 {
586 case 0x0:
587 action = "update started";
Jason M. Billsdc249272019-04-03 09:58:40 -0700588 redfishMsgID += ".FirmwareUpdateStarted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800589 break;
590 case 0x1:
591 action = "update completed successfully";
Jason M. Billsdc249272019-04-03 09:58:40 -0700592 redfishMsgID += ".FirmwareUpdateCompleted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800593 break;
594 case 0x2:
595 action = "update failure";
Jason M. Billsdc249272019-04-03 09:58:40 -0700596 redfishMsgID += ".FirmwareUpdateFailed";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800597 break;
598 default:
599 action = "unknown";
600 break;
601 }
602
Jason M. Billsdc249272019-04-03 09:58:40 -0700603 std::string firmwareInstanceStr =
604 firmware + " instance: " + std::to_string(instance);
605 std::string message("[firmware update] " + firmwareInstanceStr +
606 " status: <" + action + "> " + buildInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800607
608 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", LOG_INFO,
Jason M. Billsdc249272019-04-03 09:58:40 -0700609 "REDFISH_MESSAGE_ID=%s", redfishMsgID.c_str(),
610 "REDFISH_MESSAGE_ARGS=%s,%s", firmwareInstanceStr.c_str(),
611 buildInfo.c_str(), NULL);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800612 return ipmi::responseSuccess();
613}
614
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +0530615ipmi::RspType<uint8_t, std::vector<uint8_t>>
616 ipmiOEMSlotIpmb(ipmi::Context::ptr ctx, uint6_t reserved1,
617 uint2_t slotNumber, uint3_t baseBoardSlotNum,
618 uint3_t riserSlotNum, uint2_t reserved2, uint8_t slaveAddr,
619 uint8_t netFn, uint8_t cmd,
620 std::optional<std::vector<uint8_t>> writeData)
621{
622 if (reserved1 || reserved2)
623 {
624 return ipmi::responseInvalidFieldRequest();
625 }
626
627 boost::system::error_code ec;
628 using ipmbResponse = std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t,
629 std::vector<uint8_t>>;
630 ipmbResponse res = ctx->bus->yield_method_call<ipmbResponse>(
631 ctx->yield, ec, "xyz.openbmc_project.Ipmi.Channel.Ipmb",
632 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
633 "SlotIpmbRequest", static_cast<uint8_t>(slotNumber),
634 static_cast<uint8_t>(baseBoardSlotNum), slaveAddr, netFn, cmd,
635 *writeData);
636 if (ec)
637 {
638 phosphor::logging::log<phosphor::logging::level::ERR>(
639 "Failed to call dbus method SlotIpmbRequest");
640 return ipmi::responseUnspecifiedError();
641 }
642
643 std::vector<uint8_t> dataReceived(0);
644 int status = -1;
645 uint8_t resNetFn = 0, resLun = 0, resCmd = 0, cc = 0;
646
647 std::tie(status, resNetFn, resLun, resCmd, cc, dataReceived) = res;
648
649 if (status)
650 {
651 phosphor::logging::log<phosphor::logging::level::ERR>(
652 "Failed to get response from SlotIpmbRequest");
653 return ipmi::responseResponseError();
654 }
655 return ipmi::responseSuccess(cc, dataReceived);
656}
657
Jason M. Bills64796042018-10-03 16:51:55 -0700658ipmi_ret_t ipmiOEMSetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
659 ipmi_request_t request,
660 ipmi_response_t response,
661 ipmi_data_len_t dataLen,
662 ipmi_context_t context)
663{
664 SetPowerRestoreDelayReq* data =
665 reinterpret_cast<SetPowerRestoreDelayReq*>(request);
666 uint16_t delay = 0;
667
668 if (*dataLen != sizeof(SetPowerRestoreDelayReq))
669 {
670 *dataLen = 0;
671 return IPMI_CC_REQ_DATA_LEN_INVALID;
672 }
673 delay = data->byteMSB;
674 delay = (delay << 8) | data->byteLSB;
Vernon Mauery15419dd2019-05-24 09:40:30 -0700675 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700676 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700677 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
678 setDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700679 powerRestoreDelayIntf, powerRestoreDelayProp, delay);
680 *dataLen = 0;
681
682 return IPMI_CC_OK;
683}
684
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700685static bool cpuPresent(const std::string& cpuName)
Jason M. Bills64796042018-10-03 16:51:55 -0700686{
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700687 static constexpr const char* cpuPresencePathPrefix =
688 "/xyz/openbmc_project/inventory/system/chassis/motherboard/";
689 static constexpr const char* cpuPresenceIntf =
690 "xyz.openbmc_project.Inventory.Item";
691 std::string cpuPresencePath = cpuPresencePathPrefix + cpuName;
692 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
693 try
Jason M. Bills64796042018-10-03 16:51:55 -0700694 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700695 auto service =
696 ipmi::getService(*busp, cpuPresenceIntf, cpuPresencePath);
697
698 ipmi::Value result = ipmi::getDbusProperty(
699 *busp, service, cpuPresencePath, cpuPresenceIntf, "Present");
700 return std::get<bool>(result);
701 }
702 catch (const std::exception& e)
703 {
704 phosphor::logging::log<phosphor::logging::level::INFO>(
705 "Cannot find processor presence",
706 phosphor::logging::entry("NAME=%s", cpuName.c_str()));
707 return false;
708 }
709}
710
711ipmi::RspType<bool, // CATERR Reset Enabled
712 bool, // ERR2 Reset Enabled
713 uint6_t, // reserved
714 uint8_t, // reserved, returns 0x3F
715 uint6_t, // CPU1 CATERR Count
716 uint2_t, // CPU1 Status
717 uint6_t, // CPU2 CATERR Count
718 uint2_t, // CPU2 Status
719 uint6_t, // CPU3 CATERR Count
720 uint2_t, // CPU3 Status
721 uint6_t, // CPU4 CATERR Count
722 uint2_t, // CPU4 Status
723 uint8_t // Crashdump Count
724 >
725 ipmiOEMGetProcessorErrConfig()
726{
727 bool resetOnCATERR = false;
728 bool resetOnERR2 = false;
729 uint6_t cpu1CATERRCount = 0;
730 uint6_t cpu2CATERRCount = 0;
731 uint6_t cpu3CATERRCount = 0;
732 uint6_t cpu4CATERRCount = 0;
733 uint8_t crashdumpCount = 0;
734 uint2_t cpu1Status =
735 cpuPresent("CPU_1") ? CPUStatus::enabled : CPUStatus::notPresent;
736 uint2_t cpu2Status =
737 cpuPresent("CPU_2") ? CPUStatus::enabled : CPUStatus::notPresent;
738 uint2_t cpu3Status =
739 cpuPresent("CPU_3") ? CPUStatus::enabled : CPUStatus::notPresent;
740 uint2_t cpu4Status =
741 cpuPresent("CPU_4") ? CPUStatus::enabled : CPUStatus::notPresent;
742
743 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
744 try
745 {
746 auto service = ipmi::getService(*busp, processorErrConfigIntf,
747 processorErrConfigObjPath);
748
749 ipmi::PropertyMap result = ipmi::getAllDbusProperties(
750 *busp, service, processorErrConfigObjPath, processorErrConfigIntf);
751 resetOnCATERR = std::get<bool>(result.at("ResetOnCATERR"));
752 resetOnERR2 = std::get<bool>(result.at("ResetOnERR2"));
753 cpu1CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU1"));
754 cpu2CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU2"));
755 cpu3CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU3"));
756 cpu4CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU4"));
757 crashdumpCount = std::get<uint8_t>(result.at("CrashdumpCount"));
758 }
759 catch (const std::exception& e)
760 {
761 phosphor::logging::log<phosphor::logging::level::ERR>(
762 "Failed to fetch processor error config",
763 phosphor::logging::entry("ERROR=%s", e.what()));
764 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700765 }
766
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700767 return ipmi::responseSuccess(resetOnCATERR, resetOnERR2, 0, 0x3F,
768 cpu1CATERRCount, cpu1Status, cpu2CATERRCount,
769 cpu2Status, cpu3CATERRCount, cpu3Status,
770 cpu4CATERRCount, cpu4Status, crashdumpCount);
771}
Jason M. Bills64796042018-10-03 16:51:55 -0700772
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700773ipmi::RspType<> ipmiOEMSetProcessorErrConfig(
774 bool resetOnCATERR, bool resetOnERR2, uint6_t reserved1, uint8_t reserved2,
775 std::optional<bool> clearCPUErrorCount,
776 std::optional<bool> clearCrashdumpCount, std::optional<uint6_t> reserved3)
777{
778 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700779
780 try
781 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700782 auto service = ipmi::getService(*busp, processorErrConfigIntf,
783 processorErrConfigObjPath);
784 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
785 processorErrConfigIntf, "ResetOnCATERR",
786 resetOnCATERR);
787 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
788 processorErrConfigIntf, "ResetOnERR2",
789 resetOnERR2);
790 if (clearCPUErrorCount.value_or(false))
791 {
792 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700793 processorErrConfigIntf, "ErrorCountCPU1",
794 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700795 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700796 processorErrConfigIntf, "ErrorCountCPU2",
797 static_cast<uint8_t>(0));
798 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
799 processorErrConfigIntf, "ErrorCountCPU3",
800 static_cast<uint8_t>(0));
801 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
802 processorErrConfigIntf, "ErrorCountCPU4",
803 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700804 }
805 if (clearCrashdumpCount.value_or(false))
806 {
807 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700808 processorErrConfigIntf, "CrashdumpCount",
809 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700810 }
Jason M. Bills64796042018-10-03 16:51:55 -0700811 }
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700812 catch (std::exception& e)
Jason M. Bills64796042018-10-03 16:51:55 -0700813 {
Kuiying Wangbc546672018-11-23 15:41:05 +0800814 phosphor::logging::log<phosphor::logging::level::ERR>(
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700815 "Failed to set processor error config",
816 phosphor::logging::entry("EXCEPTION=%s", e.what()));
817 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700818 }
819
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700820 return ipmi::responseSuccess();
Jason M. Bills64796042018-10-03 16:51:55 -0700821}
822
Yong Li703922d2018-11-06 13:25:31 +0800823ipmi_ret_t ipmiOEMGetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
824 ipmi_request_t request,
825 ipmi_response_t response,
826 ipmi_data_len_t dataLen,
827 ipmi_context_t context)
828{
829 GetOEMShutdownPolicyRes* resp =
830 reinterpret_cast<GetOEMShutdownPolicyRes*>(response);
831
832 if (*dataLen != 0)
833 {
834 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wang45f04982018-12-26 09:23:08 +0800835 "oem_get_shutdown_policy: invalid input len!");
Yong Li703922d2018-11-06 13:25:31 +0800836 *dataLen = 0;
837 return IPMI_CC_REQ_DATA_LEN_INVALID;
838 }
839
840 *dataLen = 0;
841
842 try
843 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700844 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800845 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700846 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
847 Value variant = getDbusProperty(
848 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
849 oemShutdownPolicyObjPathProp);
Yong Li0669d192019-05-06 14:01:46 +0800850
851 if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
852 convertPolicyFromString(std::get<std::string>(variant)) ==
853 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
854 NoShutdownOnOCOT)
855 {
856 resp->policy = 0;
857 }
858 else if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
859 convertPolicyFromString(std::get<std::string>(variant)) ==
860 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
861 Policy::ShutdownOnOCOT)
862 {
863 resp->policy = 1;
864 }
865 else
866 {
867 phosphor::logging::log<phosphor::logging::level::ERR>(
868 "oem_set_shutdown_policy: invalid property!",
869 phosphor::logging::entry(
870 "PROP=%s", std::get<std::string>(variant).c_str()));
871 return IPMI_CC_UNSPECIFIED_ERROR;
872 }
Yong Li703922d2018-11-06 13:25:31 +0800873 // TODO needs to check if it is multi-node products,
874 // policy is only supported on node 3/4
875 resp->policySupport = shutdownPolicySupported;
876 }
877 catch (sdbusplus::exception_t& e)
878 {
879 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
880 return IPMI_CC_UNSPECIFIED_ERROR;
881 }
882
883 *dataLen = sizeof(GetOEMShutdownPolicyRes);
884 return IPMI_CC_OK;
885}
886
887ipmi_ret_t ipmiOEMSetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
888 ipmi_request_t request,
889 ipmi_response_t response,
890 ipmi_data_len_t dataLen,
891 ipmi_context_t context)
892{
893 uint8_t* req = reinterpret_cast<uint8_t*>(request);
Yong Li0669d192019-05-06 14:01:46 +0800894 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy policy =
895 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
896 NoShutdownOnOCOT;
Yong Li703922d2018-11-06 13:25:31 +0800897
898 // TODO needs to check if it is multi-node products,
899 // policy is only supported on node 3/4
900 if (*dataLen != 1)
901 {
902 phosphor::logging::log<phosphor::logging::level::ERR>(
903 "oem_set_shutdown_policy: invalid input len!");
904 *dataLen = 0;
905 return IPMI_CC_REQ_DATA_LEN_INVALID;
906 }
907
908 *dataLen = 0;
909 if ((*req != noShutdownOnOCOT) && (*req != shutdownOnOCOT))
910 {
911 phosphor::logging::log<phosphor::logging::level::ERR>(
912 "oem_set_shutdown_policy: invalid input!");
913 return IPMI_CC_INVALID_FIELD_REQUEST;
914 }
915
Yong Li0669d192019-05-06 14:01:46 +0800916 if (*req == noShutdownOnOCOT)
917 {
918 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
919 Policy::NoShutdownOnOCOT;
920 }
921 else
922 {
923 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
924 Policy::ShutdownOnOCOT;
925 }
926
Yong Li703922d2018-11-06 13:25:31 +0800927 try
928 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700929 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800930 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700931 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
Yong Li0669d192019-05-06 14:01:46 +0800932 setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -0700933 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
Yong Li0669d192019-05-06 14:01:46 +0800934 oemShutdownPolicyObjPathProp,
935 sdbusplus::com::intel::Control::server::convertForMessage(policy));
Yong Li703922d2018-11-06 13:25:31 +0800936 }
937 catch (sdbusplus::exception_t& e)
938 {
939 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
940 return IPMI_CC_UNSPECIFIED_ERROR;
941 }
942
943 return IPMI_CC_OK;
944}
945
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530946/** @brief implementation for check the DHCP or not in IPv4
947 * @param[in] Channel - Channel number
948 * @returns true or false.
949 */
950static bool isDHCPEnabled(uint8_t Channel)
951{
952 try
953 {
954 auto ethdevice = getChannelName(Channel);
955 if (ethdevice.empty())
956 {
957 return false;
958 }
959 auto ethIP = ethdevice + "/ipv4";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700960 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530961 auto ethernetObj =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700962 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
963 auto value = getDbusProperty(*dbus, networkService, ethernetObj.first,
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530964 networkIPIntf, "Origin");
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700965 if (std::get<std::string>(value) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530966 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
967 {
968 return true;
969 }
970 else
971 {
972 return false;
973 }
974 }
975 catch (sdbusplus::exception_t& e)
976 {
977 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
978 return true;
979 }
980}
981
982/** @brief implementes for check the DHCP or not in IPv6
983 * @param[in] Channel - Channel number
984 * @returns true or false.
985 */
986static bool isDHCPIPv6Enabled(uint8_t Channel)
987{
988
989 try
990 {
991 auto ethdevice = getChannelName(Channel);
992 if (ethdevice.empty())
993 {
994 return false;
995 }
996 auto ethIP = ethdevice + "/ipv6";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700997 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530998 auto objectInfo =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700999 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
1000 auto properties = getAllDbusProperties(*dbus, objectInfo.second,
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301001 objectInfo.first, networkIPIntf);
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001002 if (std::get<std::string>(properties["Origin"]) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301003 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1004 {
1005 return true;
1006 }
1007 else
1008 {
1009 return false;
1010 }
1011 }
1012 catch (sdbusplus::exception_t& e)
1013 {
1014 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1015 return true;
1016 }
1017}
1018
1019/** @brief implementes the creating of default new user
1020 * @param[in] userName - new username in 16 bytes.
1021 * @param[in] userPassword - new password in 20 bytes
1022 * @returns ipmi completion code.
1023 */
1024ipmi::RspType<> ipmiOEMSetUser2Activation(
1025 std::array<uint8_t, ipmi::ipmiMaxUserName>& userName,
1026 std::array<uint8_t, ipmi::maxIpmi20PasswordSize>& userPassword)
1027{
1028 bool userState = false;
1029 // Check for System Interface not exist and LAN should be static
1030 for (uint8_t channel = 0; channel < maxIpmiChannels; channel++)
1031 {
1032 ChannelInfo chInfo;
1033 try
1034 {
1035 getChannelInfo(channel, chInfo);
1036 }
1037 catch (sdbusplus::exception_t& e)
1038 {
1039 phosphor::logging::log<phosphor::logging::level::ERR>(
1040 "ipmiOEMSetUser2Activation: Failed to get Channel Info",
1041 phosphor::logging::entry("MSG: %s", e.description()));
1042 return ipmi::response(ipmi::ccUnspecifiedError);
1043 }
1044 if (chInfo.mediumType ==
1045 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1046 {
1047 phosphor::logging::log<phosphor::logging::level::ERR>(
1048 "ipmiOEMSetUser2Activation: system interface exist .");
1049 return ipmi::response(ipmi::ccCommandNotAvailable);
1050 }
1051 else
1052 {
1053
1054 if (chInfo.mediumType ==
1055 static_cast<uint8_t>(EChannelMediumType::lan8032))
1056 {
1057 if (isDHCPIPv6Enabled(channel) || isDHCPEnabled(channel))
1058 {
1059 phosphor::logging::log<phosphor::logging::level::ERR>(
1060 "ipmiOEMSetUser2Activation: DHCP enabled .");
1061 return ipmi::response(ipmi::ccCommandNotAvailable);
1062 }
1063 }
1064 }
1065 }
1066 uint8_t maxChUsers = 0, enabledUsers = 0, fixedUsers = 0;
1067 if (ipmi::ccSuccess ==
1068 ipmiUserGetAllCounts(maxChUsers, enabledUsers, fixedUsers))
1069 {
1070 if (enabledUsers > 1)
1071 {
1072 phosphor::logging::log<phosphor::logging::level::ERR>(
1073 "ipmiOEMSetUser2Activation: more than one user is enabled.");
1074 return ipmi::response(ipmi::ccCommandNotAvailable);
1075 }
1076 // Check the user 2 is enabled or not
1077 ipmiUserCheckEnabled(ipmiDefaultUserId, userState);
1078 if (userState == true)
1079 {
1080 phosphor::logging::log<phosphor::logging::level::ERR>(
1081 "ipmiOEMSetUser2Activation: user 2 already enabled .");
1082 return ipmi::response(ipmi::ccCommandNotAvailable);
1083 }
1084 }
1085 else
1086 {
1087 return ipmi::response(ipmi::ccUnspecifiedError);
1088 }
1089
1090#if BYTE_ORDER == LITTLE_ENDIAN
1091 PrivAccess privAccess = {PRIVILEGE_ADMIN, true, true, true, 0};
1092#endif
1093#if BYTE_ORDER == BIG_ENDIAN
1094 PrivAccess privAccess = {0, true, true, true, PRIVILEGE_ADMIN};
1095#endif
1096
Vernon Mauery037cabd2020-05-14 12:16:01 -07001097 // ipmiUserSetUserName correctly handles char*, possibly non-null
1098 // terminated strings using ipmiMaxUserName size
1099 auto userNameRaw = reinterpret_cast<const char*>(userName.data());
jayaprakash Mutyala1429d4f2020-03-04 18:20:16 +00001100
Vernon Mauery037cabd2020-05-14 12:16:01 -07001101 if (ipmi::ccSuccess == ipmiUserSetUserName(ipmiDefaultUserId, userNameRaw))
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301102 {
1103 if (ipmi::ccSuccess ==
1104 ipmiUserSetUserPassword(
1105 ipmiDefaultUserId,
1106 reinterpret_cast<const char*>(userPassword.data())))
1107 {
1108 if (ipmi::ccSuccess ==
1109 ipmiUserSetPrivilegeAccess(
1110 ipmiDefaultUserId,
1111 static_cast<uint8_t>(ipmi::EChannelID::chanLan1),
1112 privAccess, true))
1113 {
1114 phosphor::logging::log<phosphor::logging::level::INFO>(
1115 "ipmiOEMSetUser2Activation: user created successfully ");
1116 return ipmi::responseSuccess();
1117 }
1118 }
1119 // we need to delete the default user id which added in this command as
1120 // password / priv setting is failed.
1121 ipmiUserSetUserName(ipmiDefaultUserId, "");
1122 phosphor::logging::log<phosphor::logging::level::ERR>(
1123 "ipmiOEMSetUser2Activation: password / priv setting is failed.");
1124 }
1125 else
1126 {
1127 phosphor::logging::log<phosphor::logging::level::ERR>(
1128 "ipmiOEMSetUser2Activation: Setting username failed.");
1129 }
1130
1131 return ipmi::response(ipmi::ccCommandNotAvailable);
1132}
1133
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301134/** @brief implementes executing the linux command
1135 * @param[in] linux command
1136 * @returns status
1137 */
1138
1139static uint8_t executeCmd(const char* path)
1140{
1141 boost::process::child execProg(path);
1142 execProg.wait();
1143
1144 int retCode = execProg.exit_code();
1145 if (retCode)
1146 {
1147 return ipmi::ccUnspecifiedError;
1148 }
1149 return ipmi::ccSuccess;
1150}
1151
1152/** @brief implementes ASD Security event logging
1153 * @param[in] Event message string
1154 * @param[in] Event Severity
1155 * @returns status
1156 */
1157
1158static void atScaleDebugEventlog(std::string msg, int severity)
1159{
1160 std::string eventStr = "OpenBMC.0.1." + msg;
1161 sd_journal_send("MESSAGE=Security Event: %s", eventStr.c_str(),
1162 "PRIORITY=%i", severity, "REDFISH_MESSAGE_ID=%s",
1163 eventStr.c_str(), NULL);
1164}
1165
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301166/** @brief implementes setting password for special user
1167 * @param[in] specialUserIndex
1168 * @param[in] userPassword - new password in 20 bytes
1169 * @returns ipmi completion code.
1170 */
1171ipmi::RspType<> ipmiOEMSetSpecialUserPassword(ipmi::Context::ptr ctx,
1172 uint8_t specialUserIndex,
1173 std::vector<uint8_t> userPassword)
1174{
1175 ChannelInfo chInfo;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301176 ipmi_ret_t status = ipmi::ccSuccess;
1177
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301178 try
1179 {
1180 getChannelInfo(ctx->channel, chInfo);
1181 }
1182 catch (sdbusplus::exception_t& e)
1183 {
1184 phosphor::logging::log<phosphor::logging::level::ERR>(
1185 "ipmiOEMSetSpecialUserPassword: Failed to get Channel Info",
1186 phosphor::logging::entry("MSG: %s", e.description()));
1187 return ipmi::responseUnspecifiedError();
1188 }
1189 if (chInfo.mediumType !=
1190 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1191 {
1192 phosphor::logging::log<phosphor::logging::level::ERR>(
1193 "ipmiOEMSetSpecialUserPassword: Error - supported only in KCS "
1194 "interface");
1195 return ipmi::responseCommandNotAvailable();
1196 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301197
1198 // 0 for root user and 1 for AtScaleDebug is allowed
1199 if (specialUserIndex >
1200 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301201 {
1202 phosphor::logging::log<phosphor::logging::level::ERR>(
1203 "ipmiOEMSetSpecialUserPassword: Invalid user account");
1204 return ipmi::responseParmOutOfRange();
1205 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301206 if (userPassword.size() != 0)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301207 {
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301208 constexpr uint8_t minPasswordSizeRequired = 6;
1209 std::string passwd;
1210 if (userPassword.size() < minPasswordSizeRequired ||
1211 userPassword.size() > ipmi::maxIpmi20PasswordSize)
1212 {
1213 return ipmi::responseReqDataLenInvalid();
1214 }
1215 passwd.assign(reinterpret_cast<const char*>(userPassword.data()),
1216 userPassword.size());
1217 if (specialUserIndex ==
1218 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
1219 {
1220 status = ipmiSetSpecialUserPassword("asdbg", passwd);
1221
1222 atScaleDebugEventlog("AtScaleDebugSpecialUserEnabled", LOG_CRIT);
1223 }
1224 else
1225 {
1226 status = ipmiSetSpecialUserPassword("root", passwd);
1227 }
1228 return ipmi::response(status);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301229 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301230 else
1231 {
1232 if (specialUserIndex ==
1233 static_cast<uint8_t>(SpecialUserIndex::rootUser))
1234 {
1235 status = executeCmd("passwd -d root");
1236 }
1237 else
1238 {
1239
1240 status = executeCmd("passwd -d asdbg");
1241
1242 if (status == 0)
1243 {
1244 atScaleDebugEventlog("AtScaleDebugSpecialUserDisabled",
1245 LOG_INFO);
1246 }
1247 }
1248 return ipmi::response(status);
1249 }
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301250}
1251
Kuiying Wang45f04982018-12-26 09:23:08 +08001252namespace ledAction
1253{
1254using namespace sdbusplus::xyz::openbmc_project::Led::server;
1255std::map<Physical::Action, uint8_t> actionDbusToIpmi = {
jayaprakash Mutyala934ee9c2019-12-13 17:49:27 +00001256 {Physical::Action::Off, 0},
1257 {Physical::Action::On, 2},
1258 {Physical::Action::Blink, 1}};
Kuiying Wang45f04982018-12-26 09:23:08 +08001259
1260std::map<uint8_t, std::string> offsetObjPath = {
1261 {2, statusAmberObjPath}, {4, statusGreenObjPath}, {6, identifyLEDObjPath}};
1262
1263} // namespace ledAction
1264
1265int8_t getLEDState(sdbusplus::bus::bus& bus, const std::string& intf,
1266 const std::string& objPath, uint8_t& state)
1267{
1268 try
1269 {
1270 std::string service = getService(bus, intf, objPath);
1271 Value stateValue =
1272 getDbusProperty(bus, service, objPath, intf, "State");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001273 std::string strState = std::get<std::string>(stateValue);
Kuiying Wang45f04982018-12-26 09:23:08 +08001274 state = ledAction::actionDbusToIpmi.at(
1275 sdbusplus::xyz::openbmc_project::Led::server::Physical::
1276 convertActionFromString(strState));
1277 }
1278 catch (sdbusplus::exception::SdBusError& e)
1279 {
1280 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1281 return -1;
1282 }
1283 return 0;
1284}
1285
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001286ipmi::RspType<uint8_t> ipmiOEMGetLEDStatus()
Kuiying Wang45f04982018-12-26 09:23:08 +08001287{
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001288 uint8_t ledstate = 0;
Kuiying Wang45f04982018-12-26 09:23:08 +08001289 phosphor::logging::log<phosphor::logging::level::DEBUG>("GET led status");
Vernon Mauery15419dd2019-05-24 09:40:30 -07001290 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Kuiying Wang45f04982018-12-26 09:23:08 +08001291 for (auto it = ledAction::offsetObjPath.begin();
1292 it != ledAction::offsetObjPath.end(); ++it)
1293 {
1294 uint8_t state = 0;
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001295 if (getLEDState(*dbus, ledIntf, it->second, state) == -1)
Kuiying Wang45f04982018-12-26 09:23:08 +08001296 {
1297 phosphor::logging::log<phosphor::logging::level::ERR>(
1298 "oem_get_led_status: fail to get ID LED status!");
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001299 return ipmi::responseUnspecifiedError();
Kuiying Wang45f04982018-12-26 09:23:08 +08001300 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001301 ledstate |= state << it->first;
Kuiying Wang45f04982018-12-26 09:23:08 +08001302 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001303 return ipmi::responseSuccess(ledstate);
Kuiying Wang45f04982018-12-26 09:23:08 +08001304}
1305
Yong Li23737fe2019-02-19 08:49:55 +08001306ipmi_ret_t ipmiOEMCfgHostSerialPortSpeed(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1307 ipmi_request_t request,
1308 ipmi_response_t response,
1309 ipmi_data_len_t dataLen,
1310 ipmi_context_t context)
1311{
1312 CfgHostSerialReq* req = reinterpret_cast<CfgHostSerialReq*>(request);
1313 uint8_t* resp = reinterpret_cast<uint8_t*>(response);
1314
1315 if (*dataLen == 0)
1316 {
1317 phosphor::logging::log<phosphor::logging::level::ERR>(
1318 "CfgHostSerial: invalid input len!",
1319 phosphor::logging::entry("LEN=%d", *dataLen));
1320 return IPMI_CC_REQ_DATA_LEN_INVALID;
1321 }
1322
1323 switch (req->command)
1324 {
1325 case getHostSerialCfgCmd:
1326 {
1327 if (*dataLen != 1)
1328 {
1329 phosphor::logging::log<phosphor::logging::level::ERR>(
1330 "CfgHostSerial: invalid input len!");
1331 *dataLen = 0;
1332 return IPMI_CC_REQ_DATA_LEN_INVALID;
1333 }
1334
1335 *dataLen = 0;
1336
1337 boost::process::ipstream is;
1338 std::vector<std::string> data;
1339 std::string line;
1340 boost::process::child c1(fwGetEnvCmd, "-n", fwHostSerailCfgEnvName,
1341 boost::process::std_out > is);
1342
1343 while (c1.running() && std::getline(is, line) && !line.empty())
1344 {
1345 data.push_back(line);
1346 }
1347
1348 c1.wait();
1349 if (c1.exit_code())
1350 {
1351 phosphor::logging::log<phosphor::logging::level::ERR>(
1352 "CfgHostSerial:: error on execute",
1353 phosphor::logging::entry("EXECUTE=%s", fwSetEnvCmd));
1354 // Using the default value
1355 *resp = 0;
1356 }
1357 else
1358 {
1359 if (data.size() != 1)
1360 {
1361 phosphor::logging::log<phosphor::logging::level::ERR>(
1362 "CfgHostSerial:: error on read env");
1363 return IPMI_CC_UNSPECIFIED_ERROR;
1364 }
1365 try
1366 {
1367 unsigned long tmp = std::stoul(data[0]);
1368 if (tmp > std::numeric_limits<uint8_t>::max())
1369 {
1370 throw std::out_of_range("Out of range");
1371 }
1372 *resp = static_cast<uint8_t>(tmp);
1373 }
1374 catch (const std::invalid_argument& e)
1375 {
1376 phosphor::logging::log<phosphor::logging::level::ERR>(
1377 "invalid config ",
1378 phosphor::logging::entry("ERR=%s", e.what()));
1379 return IPMI_CC_UNSPECIFIED_ERROR;
1380 }
1381 catch (const std::out_of_range& e)
1382 {
1383 phosphor::logging::log<phosphor::logging::level::ERR>(
1384 "out_of_range config ",
1385 phosphor::logging::entry("ERR=%s", e.what()));
1386 return IPMI_CC_UNSPECIFIED_ERROR;
1387 }
1388 }
1389
1390 *dataLen = 1;
1391 break;
1392 }
1393 case setHostSerialCfgCmd:
1394 {
1395 if (*dataLen != sizeof(CfgHostSerialReq))
1396 {
1397 phosphor::logging::log<phosphor::logging::level::ERR>(
1398 "CfgHostSerial: invalid input len!");
1399 *dataLen = 0;
1400 return IPMI_CC_REQ_DATA_LEN_INVALID;
1401 }
1402
1403 *dataLen = 0;
1404
1405 if (req->parameter > HostSerialCfgParamMax)
1406 {
1407 phosphor::logging::log<phosphor::logging::level::ERR>(
1408 "CfgHostSerial: invalid input!");
1409 return IPMI_CC_INVALID_FIELD_REQUEST;
1410 }
1411
1412 boost::process::child c1(fwSetEnvCmd, fwHostSerailCfgEnvName,
1413 std::to_string(req->parameter));
1414
1415 c1.wait();
1416 if (c1.exit_code())
1417 {
1418 phosphor::logging::log<phosphor::logging::level::ERR>(
1419 "CfgHostSerial:: error on execute",
1420 phosphor::logging::entry("EXECUTE=%s", fwGetEnvCmd));
1421 return IPMI_CC_UNSPECIFIED_ERROR;
1422 }
1423 break;
1424 }
1425 default:
1426 phosphor::logging::log<phosphor::logging::level::ERR>(
1427 "CfgHostSerial: invalid input!");
1428 *dataLen = 0;
1429 return IPMI_CC_INVALID_FIELD_REQUEST;
1430 }
1431
1432 return IPMI_CC_OK;
1433}
1434
James Feist91244a62019-02-19 15:04:54 -08001435constexpr const char* thermalModeInterface =
1436 "xyz.openbmc_project.Control.ThermalMode";
1437constexpr const char* thermalModePath =
1438 "/xyz/openbmc_project/control/thermal_mode";
1439
1440bool getFanProfileInterface(
1441 sdbusplus::bus::bus& bus,
1442 boost::container::flat_map<
1443 std::string, std::variant<std::vector<std::string>, std::string>>& resp)
1444{
1445 auto call = bus.new_method_call(settingsBusName, thermalModePath, PROP_INTF,
1446 "GetAll");
1447 call.append(thermalModeInterface);
1448 try
1449 {
1450 auto data = bus.call(call);
1451 data.read(resp);
1452 }
1453 catch (sdbusplus::exception_t& e)
1454 {
1455 phosphor::logging::log<phosphor::logging::level::ERR>(
1456 "getFanProfileInterface: can't get thermal mode!",
1457 phosphor::logging::entry("ERR=%s", e.what()));
1458 return false;
1459 }
1460 return true;
1461}
1462
anil kumar appanaf945eee2019-09-25 23:29:11 +00001463/**@brief implements the OEM set fan config.
1464 * @param selectedFanProfile - fan profile to enable
1465 * @param reserved1
1466 * @param performanceMode - Performance/Acoustic mode
1467 * @param reserved2
1468 * @param setPerformanceMode - set Performance/Acoustic mode
1469 * @param setFanProfile - set fan profile
1470 *
1471 * @return IPMI completion code.
1472 **/
1473ipmi::RspType<> ipmiOEMSetFanConfig(uint8_t selectedFanProfile,
1474
1475 uint2_t reserved1, bool performanceMode,
1476 uint3_t reserved2, bool setPerformanceMode,
Joshi-Mansi619186d2020-01-27 19:16:03 +05301477 bool setFanProfile,
1478 std::optional<uint8_t> dimmGroupId,
1479 std::optional<uint32_t> dimmPresenceBitmap)
James Feist91244a62019-02-19 15:04:54 -08001480{
anil kumar appanaf945eee2019-09-25 23:29:11 +00001481 if (reserved1 || reserved2)
James Feist91244a62019-02-19 15:04:54 -08001482 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001483 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001484 }
Joshi-Mansi619186d2020-01-27 19:16:03 +05301485
1486 if (dimmGroupId)
1487 {
1488 if (*dimmGroupId >= maxCPUNum)
1489 {
1490 return ipmi::responseInvalidFieldRequest();
1491 }
1492 if (!cpuPresent("CPU_" + std::to_string(*dimmGroupId + 1)))
1493 {
1494 return ipmi::responseInvalidFieldRequest();
1495 }
1496 }
1497
James Feist91244a62019-02-19 15:04:54 -08001498 // todo: tell bios to only send first 2 bytes
James Feist91244a62019-02-19 15:04:54 -08001499 boost::container::flat_map<
1500 std::string, std::variant<std::vector<std::string>, std::string>>
1501 profileData;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001502 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1503 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001504 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001505 return ipmi::responseUnspecifiedError();
James Feist91244a62019-02-19 15:04:54 -08001506 }
1507
1508 std::vector<std::string>* supported =
1509 std::get_if<std::vector<std::string>>(&profileData["Supported"]);
1510 if (supported == nullptr)
1511 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001512 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001513 }
1514 std::string mode;
anil kumar appanaf945eee2019-09-25 23:29:11 +00001515 if (setPerformanceMode)
James Feist91244a62019-02-19 15:04:54 -08001516 {
James Feist91244a62019-02-19 15:04:54 -08001517 if (performanceMode)
1518 {
1519
1520 if (std::find(supported->begin(), supported->end(),
1521 "Performance") != supported->end())
1522 {
1523 mode = "Performance";
1524 }
1525 }
1526 else
1527 {
James Feist91244a62019-02-19 15:04:54 -08001528 if (std::find(supported->begin(), supported->end(), "Acoustic") !=
1529 supported->end())
1530 {
1531 mode = "Acoustic";
1532 }
1533 }
1534 if (mode.empty())
1535 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001536 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001537 }
anil kumar appanaf945eee2019-09-25 23:29:11 +00001538
1539 try
1540 {
1541 setDbusProperty(*dbus, settingsBusName, thermalModePath,
1542 thermalModeInterface, "Current", mode);
1543 }
1544 catch (sdbusplus::exception_t& e)
1545 {
1546 phosphor::logging::log<phosphor::logging::level::ERR>(
1547 "ipmiOEMSetFanConfig: can't set thermal mode!",
1548 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1549 return ipmi::responseResponseError();
1550 }
James Feist91244a62019-02-19 15:04:54 -08001551 }
1552
anil kumar appanaf945eee2019-09-25 23:29:11 +00001553 return ipmi::responseSuccess();
James Feist91244a62019-02-19 15:04:54 -08001554}
1555
James Feist5b693632019-07-09 09:06:09 -07001556ipmi::RspType<uint8_t, // profile support map
1557 uint8_t, // fan control profile enable
1558 uint8_t, // flags
1559 uint32_t // dimm presence bit map
1560 >
1561 ipmiOEMGetFanConfig(uint8_t dimmGroupId)
James Feist91244a62019-02-19 15:04:54 -08001562{
Joshi-Mansi36f05ce2020-01-14 14:29:34 +05301563 if (dimmGroupId >= maxCPUNum)
1564 {
1565 return ipmi::responseInvalidFieldRequest();
1566 }
1567
1568 bool cpuStatus = cpuPresent("CPU_" + std::to_string(dimmGroupId + 1));
1569
1570 if (!cpuStatus)
1571 {
1572 return ipmi::responseInvalidFieldRequest();
1573 }
1574
James Feist91244a62019-02-19 15:04:54 -08001575 boost::container::flat_map<
1576 std::string, std::variant<std::vector<std::string>, std::string>>
1577 profileData;
1578
Vernon Mauery15419dd2019-05-24 09:40:30 -07001579 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1580 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001581 {
James Feist5b693632019-07-09 09:06:09 -07001582 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001583 }
1584
1585 std::string* current = std::get_if<std::string>(&profileData["Current"]);
1586
1587 if (current == nullptr)
1588 {
1589 phosphor::logging::log<phosphor::logging::level::ERR>(
1590 "ipmiOEMGetFanConfig: can't get current mode!");
James Feist5b693632019-07-09 09:06:09 -07001591 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001592 }
1593 bool performance = (*current == "Performance");
1594
James Feist5b693632019-07-09 09:06:09 -07001595 uint8_t flags = 0;
James Feist91244a62019-02-19 15:04:54 -08001596 if (performance)
1597 {
James Feist5b693632019-07-09 09:06:09 -07001598 flags |= 1 << 2;
James Feist91244a62019-02-19 15:04:54 -08001599 }
1600
jayaprakash Mutyala4b1552d2020-02-11 12:07:29 +00001601 constexpr uint8_t fanControlDefaultProfile = 0x80;
1602 constexpr uint8_t fanControlProfileState = 0x00;
1603 constexpr uint32_t dimmPresenceBitmap = 0x00;
1604
1605 return ipmi::responseSuccess(fanControlDefaultProfile,
1606 fanControlProfileState, flags,
1607 dimmPresenceBitmap);
James Feist91244a62019-02-19 15:04:54 -08001608}
James Feist5f957ca2019-03-14 15:33:55 -07001609constexpr const char* cfmLimitSettingPath =
1610 "/xyz/openbmc_project/control/cfm_limit";
1611constexpr const char* cfmLimitIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistfaa4f222019-03-21 16:21:55 -07001612constexpr const size_t legacyExitAirSensorNumber = 0x2e;
James Feist09f6b602019-08-08 11:30:03 -07001613constexpr const size_t legacyPCHSensorNumber = 0x22;
1614constexpr const char* exitAirPathName = "Exit_Air";
1615constexpr const char* pchPathName = "SSB_Temp";
James Feistacc8a4e2019-04-02 14:23:57 -07001616constexpr const char* pidConfigurationIface =
1617 "xyz.openbmc_project.Configuration.Pid";
James Feistfaa4f222019-03-21 16:21:55 -07001618
James Feist09f6b602019-08-08 11:30:03 -07001619static std::string getConfigPath(const std::string& name)
James Feistfaa4f222019-03-21 16:21:55 -07001620{
Vernon Mauery15419dd2019-05-24 09:40:30 -07001621 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistfaa4f222019-03-21 16:21:55 -07001622 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001623 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1624 "/xyz/openbmc_project/object_mapper",
1625 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistfaa4f222019-03-21 16:21:55 -07001626
James Feistacc8a4e2019-04-02 14:23:57 -07001627 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
James Feistfaa4f222019-03-21 16:21:55 -07001628 std::string path;
1629 GetSubTreeType resp;
1630 try
1631 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001632 auto reply = dbus->call(method);
James Feistfaa4f222019-03-21 16:21:55 -07001633 reply.read(resp);
1634 }
1635 catch (sdbusplus::exception_t&)
1636 {
1637 phosphor::logging::log<phosphor::logging::level::ERR>(
1638 "ipmiOEMGetFscParameter: mapper error");
1639 };
James Feist09f6b602019-08-08 11:30:03 -07001640 auto config =
1641 std::find_if(resp.begin(), resp.end(), [&name](const auto& pair) {
1642 return pair.first.find(name) != std::string::npos;
1643 });
James Feistfaa4f222019-03-21 16:21:55 -07001644 if (config != resp.end())
1645 {
1646 path = std::move(config->first);
1647 }
1648 return path;
1649}
James Feist5f957ca2019-03-14 15:33:55 -07001650
James Feistacc8a4e2019-04-02 14:23:57 -07001651// flat map to make alphabetical
1652static boost::container::flat_map<std::string, PropertyMap> getPidConfigs()
1653{
1654 boost::container::flat_map<std::string, PropertyMap> ret;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001655 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001656 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001657 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1658 "/xyz/openbmc_project/object_mapper",
1659 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistacc8a4e2019-04-02 14:23:57 -07001660
1661 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
1662 GetSubTreeType resp;
1663
1664 try
1665 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001666 auto reply = dbus->call(method);
James Feistacc8a4e2019-04-02 14:23:57 -07001667 reply.read(resp);
1668 }
1669 catch (sdbusplus::exception_t&)
1670 {
1671 phosphor::logging::log<phosphor::logging::level::ERR>(
1672 "getFanConfigPaths: mapper error");
1673 };
1674 for (const auto& [path, objects] : resp)
1675 {
1676 if (objects.empty())
1677 {
1678 continue; // should be impossible
1679 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04001680
1681 try
1682 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001683 ret.emplace(path,
1684 getAllDbusProperties(*dbus, objects[0].first, path,
1685 pidConfigurationIface));
Zhu, Yungebe560b02019-04-21 21:19:21 -04001686 }
1687 catch (sdbusplus::exception_t& e)
1688 {
1689 phosphor::logging::log<phosphor::logging::level::ERR>(
1690 "getPidConfigs: can't get DbusProperties!",
1691 phosphor::logging::entry("ERR=%s", e.what()));
1692 }
James Feistacc8a4e2019-04-02 14:23:57 -07001693 }
1694 return ret;
1695}
1696
1697ipmi::RspType<uint8_t> ipmiOEMGetFanSpeedOffset(void)
1698{
1699 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1700 if (data.empty())
1701 {
1702 return ipmi::responseResponseError();
1703 }
1704 uint8_t minOffset = std::numeric_limits<uint8_t>::max();
1705 for (const auto& [_, pid] : data)
1706 {
1707 auto findClass = pid.find("Class");
1708 if (findClass == pid.end())
1709 {
1710 phosphor::logging::log<phosphor::logging::level::ERR>(
1711 "ipmiOEMGetFscParameter: found illegal pid "
1712 "configurations");
1713 return ipmi::responseResponseError();
1714 }
1715 std::string type = std::get<std::string>(findClass->second);
1716 if (type == "fan")
1717 {
1718 auto findOutLimit = pid.find("OutLimitMin");
1719 if (findOutLimit == pid.end())
1720 {
1721 phosphor::logging::log<phosphor::logging::level::ERR>(
1722 "ipmiOEMGetFscParameter: found illegal pid "
1723 "configurations");
1724 return ipmi::responseResponseError();
1725 }
1726 // get the min out of all the offsets
1727 minOffset = std::min(
1728 minOffset,
1729 static_cast<uint8_t>(std::get<double>(findOutLimit->second)));
1730 }
1731 }
1732 if (minOffset == std::numeric_limits<uint8_t>::max())
1733 {
1734 phosphor::logging::log<phosphor::logging::level::ERR>(
1735 "ipmiOEMGetFscParameter: found no fan configurations!");
1736 return ipmi::responseResponseError();
1737 }
1738
1739 return ipmi::responseSuccess(minOffset);
1740}
1741
1742ipmi::RspType<> ipmiOEMSetFanSpeedOffset(uint8_t offset)
1743{
1744 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1745 if (data.empty())
1746 {
1747
1748 phosphor::logging::log<phosphor::logging::level::ERR>(
1749 "ipmiOEMSetFanSpeedOffset: found no pid configurations!");
1750 return ipmi::responseResponseError();
1751 }
1752
Vernon Mauery15419dd2019-05-24 09:40:30 -07001753 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001754 bool found = false;
1755 for (const auto& [path, pid] : data)
1756 {
1757 auto findClass = pid.find("Class");
1758 if (findClass == pid.end())
1759 {
1760
1761 phosphor::logging::log<phosphor::logging::level::ERR>(
1762 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1763 "configurations");
1764 return ipmi::responseResponseError();
1765 }
1766 std::string type = std::get<std::string>(findClass->second);
1767 if (type == "fan")
1768 {
1769 auto findOutLimit = pid.find("OutLimitMin");
1770 if (findOutLimit == pid.end())
1771 {
1772
1773 phosphor::logging::log<phosphor::logging::level::ERR>(
1774 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1775 "configurations");
1776 return ipmi::responseResponseError();
1777 }
Vernon Mauery15419dd2019-05-24 09:40:30 -07001778 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager",
James Feistacc8a4e2019-04-02 14:23:57 -07001779 path, pidConfigurationIface, "OutLimitMin",
1780 static_cast<double>(offset));
1781 found = true;
1782 }
1783 }
1784 if (!found)
1785 {
1786 phosphor::logging::log<phosphor::logging::level::ERR>(
1787 "ipmiOEMSetFanSpeedOffset: set no fan offsets");
1788 return ipmi::responseResponseError();
1789 }
1790
1791 return ipmi::responseSuccess();
1792}
1793
1794ipmi::RspType<> ipmiOEMSetFscParameter(uint8_t command, uint8_t param1,
1795 uint8_t param2)
James Feist5f957ca2019-03-14 15:33:55 -07001796{
1797 constexpr const size_t disableLimiting = 0x0;
1798
Vernon Mauery15419dd2019-05-24 09:40:30 -07001799 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001800 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001801 {
James Feist09f6b602019-08-08 11:30:03 -07001802 std::string pathName;
James Feistacc8a4e2019-04-02 14:23:57 -07001803 if (param1 == legacyExitAirSensorNumber)
James Feistfaa4f222019-03-21 16:21:55 -07001804 {
James Feist09f6b602019-08-08 11:30:03 -07001805 pathName = exitAirPathName;
1806 }
1807 else if (param1 == legacyPCHSensorNumber)
1808 {
1809 pathName = pchPathName;
James Feistfaa4f222019-03-21 16:21:55 -07001810 }
1811 else
1812 {
James Feistacc8a4e2019-04-02 14:23:57 -07001813 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001814 }
James Feist09f6b602019-08-08 11:30:03 -07001815 std::string path = getConfigPath(pathName);
1816 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager", path,
1817 pidConfigurationIface, "SetPoint",
1818 static_cast<double>(param2));
1819 return ipmi::responseSuccess();
James Feistfaa4f222019-03-21 16:21:55 -07001820 }
James Feistacc8a4e2019-04-02 14:23:57 -07001821 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001822 {
James Feistacc8a4e2019-04-02 14:23:57 -07001823 uint16_t cfm = param1 | (static_cast<uint16_t>(param2) << 8);
James Feist5f957ca2019-03-14 15:33:55 -07001824
1825 // must be greater than 50 based on eps
1826 if (cfm < 50 && cfm != disableLimiting)
1827 {
James Feistacc8a4e2019-04-02 14:23:57 -07001828 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001829 }
1830
1831 try
1832 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001833 ipmi::setDbusProperty(*dbus, settingsBusName, cfmLimitSettingPath,
James Feist5f957ca2019-03-14 15:33:55 -07001834 cfmLimitIface, "Limit",
1835 static_cast<double>(cfm));
1836 }
1837 catch (sdbusplus::exception_t& e)
1838 {
1839 phosphor::logging::log<phosphor::logging::level::ERR>(
1840 "ipmiOEMSetFscParameter: can't set cfm setting!",
1841 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07001842 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07001843 }
James Feistacc8a4e2019-04-02 14:23:57 -07001844 return ipmi::responseSuccess();
1845 }
1846 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1847 {
1848 constexpr const size_t maxDomainCount = 8;
1849 uint8_t requestedDomainMask = param1;
1850 boost::container::flat_map data = getPidConfigs();
1851 if (data.empty())
1852 {
1853
1854 phosphor::logging::log<phosphor::logging::level::ERR>(
1855 "ipmiOEMSetFscParameter: found no pid configurations!");
1856 return ipmi::responseResponseError();
1857 }
1858 size_t count = 0;
1859 for (const auto& [path, pid] : data)
1860 {
1861 auto findClass = pid.find("Class");
1862 if (findClass == pid.end())
1863 {
1864
1865 phosphor::logging::log<phosphor::logging::level::ERR>(
1866 "ipmiOEMSetFscParameter: found illegal pid "
1867 "configurations");
1868 return ipmi::responseResponseError();
1869 }
1870 std::string type = std::get<std::string>(findClass->second);
1871 if (type == "fan")
1872 {
1873 if (requestedDomainMask & (1 << count))
1874 {
1875 ipmi::setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07001876 *dbus, "xyz.openbmc_project.EntityManager", path,
James Feistacc8a4e2019-04-02 14:23:57 -07001877 pidConfigurationIface, "OutLimitMax",
1878 static_cast<double>(param2));
1879 }
1880 count++;
1881 }
1882 }
1883 return ipmi::responseSuccess();
James Feist5f957ca2019-03-14 15:33:55 -07001884 }
1885 else
1886 {
1887 // todo other command parts possibly
1888 // tcontrol is handled in peci now
1889 // fan speed offset not implemented yet
1890 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07001891 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001892 }
1893}
1894
James Feistacc8a4e2019-04-02 14:23:57 -07001895ipmi::RspType<
1896 std::variant<uint8_t, std::array<uint8_t, 2>, std::array<uint16_t, 2>>>
1897 ipmiOEMGetFscParameter(uint8_t command, std::optional<uint8_t> param)
James Feist5f957ca2019-03-14 15:33:55 -07001898{
James Feist09f6b602019-08-08 11:30:03 -07001899 constexpr uint8_t legacyDefaultSetpoint = -128;
James Feist5f957ca2019-03-14 15:33:55 -07001900
Vernon Mauery15419dd2019-05-24 09:40:30 -07001901 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001902 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001903 {
James Feistacc8a4e2019-04-02 14:23:57 -07001904 if (!param)
James Feistfaa4f222019-03-21 16:21:55 -07001905 {
James Feistacc8a4e2019-04-02 14:23:57 -07001906 return ipmi::responseReqDataLenInvalid();
James Feistfaa4f222019-03-21 16:21:55 -07001907 }
1908
James Feist09f6b602019-08-08 11:30:03 -07001909 std::string pathName;
1910
1911 if (*param == legacyExitAirSensorNumber)
1912 {
1913 pathName = exitAirPathName;
1914 }
1915 else if (*param == legacyPCHSensorNumber)
1916 {
1917 pathName = pchPathName;
1918 }
1919 else
James Feistfaa4f222019-03-21 16:21:55 -07001920 {
James Feistacc8a4e2019-04-02 14:23:57 -07001921 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001922 }
James Feist09f6b602019-08-08 11:30:03 -07001923
1924 uint8_t setpoint = legacyDefaultSetpoint;
1925 std::string path = getConfigPath(pathName);
James Feistfaa4f222019-03-21 16:21:55 -07001926 if (path.size())
1927 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001928 Value val = ipmi::getDbusProperty(
1929 *dbus, "xyz.openbmc_project.EntityManager", path,
1930 pidConfigurationIface, "SetPoint");
James Feistfaa4f222019-03-21 16:21:55 -07001931 setpoint = std::floor(std::get<double>(val) + 0.5);
1932 }
1933
1934 // old implementation used to return the "default" and current, we
1935 // don't make the default readily available so just make both the
1936 // same
James Feistfaa4f222019-03-21 16:21:55 -07001937
James Feistacc8a4e2019-04-02 14:23:57 -07001938 return ipmi::responseSuccess(
1939 std::array<uint8_t, 2>{setpoint, setpoint});
James Feistfaa4f222019-03-21 16:21:55 -07001940 }
James Feistacc8a4e2019-04-02 14:23:57 -07001941 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1942 {
1943 constexpr const size_t maxDomainCount = 8;
1944
1945 if (!param)
1946 {
1947 return ipmi::responseReqDataLenInvalid();
1948 }
1949 uint8_t requestedDomain = *param;
1950 if (requestedDomain >= maxDomainCount)
1951 {
1952 return ipmi::responseInvalidFieldRequest();
1953 }
1954
1955 boost::container::flat_map data = getPidConfigs();
1956 if (data.empty())
1957 {
1958 phosphor::logging::log<phosphor::logging::level::ERR>(
1959 "ipmiOEMGetFscParameter: found no pid configurations!");
1960 return ipmi::responseResponseError();
1961 }
1962 size_t count = 0;
1963 for (const auto& [_, pid] : data)
1964 {
1965 auto findClass = pid.find("Class");
1966 if (findClass == pid.end())
1967 {
1968 phosphor::logging::log<phosphor::logging::level::ERR>(
1969 "ipmiOEMGetFscParameter: found illegal pid "
1970 "configurations");
1971 return ipmi::responseResponseError();
1972 }
1973 std::string type = std::get<std::string>(findClass->second);
1974 if (type == "fan")
1975 {
1976 if (requestedDomain == count)
1977 {
1978 auto findOutLimit = pid.find("OutLimitMax");
1979 if (findOutLimit == pid.end())
1980 {
1981 phosphor::logging::log<phosphor::logging::level::ERR>(
1982 "ipmiOEMGetFscParameter: found illegal pid "
1983 "configurations");
1984 return ipmi::responseResponseError();
1985 }
1986
1987 return ipmi::responseSuccess(
1988 static_cast<uint8_t>(std::floor(
1989 std::get<double>(findOutLimit->second) + 0.5)));
1990 }
1991 else
1992 {
1993 count++;
1994 }
1995 }
1996 }
1997
1998 return ipmi::responseInvalidFieldRequest();
1999 }
2000 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07002001 {
2002
2003 /*
2004 DataLen should be 1, but host is sending us an extra bit. As the
James Feistacc8a4e2019-04-02 14:23:57 -07002005 previous behavior didn't seem to prevent this, ignore the check for
2006 now.
James Feist5f957ca2019-03-14 15:33:55 -07002007
James Feistacc8a4e2019-04-02 14:23:57 -07002008 if (param)
James Feist5f957ca2019-03-14 15:33:55 -07002009 {
2010 phosphor::logging::log<phosphor::logging::level::ERR>(
2011 "ipmiOEMGetFscParameter: invalid input len!");
James Feist5f957ca2019-03-14 15:33:55 -07002012 return IPMI_CC_REQ_DATA_LEN_INVALID;
2013 }
2014 */
2015 Value cfmLimit;
2016 Value cfmMaximum;
2017 try
2018 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002019 cfmLimit = ipmi::getDbusProperty(*dbus, settingsBusName,
James Feist5f957ca2019-03-14 15:33:55 -07002020 cfmLimitSettingPath, cfmLimitIface,
2021 "Limit");
2022 cfmMaximum = ipmi::getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002023 *dbus, "xyz.openbmc_project.ExitAirTempSensor",
James Feist5f957ca2019-03-14 15:33:55 -07002024 "/xyz/openbmc_project/control/MaxCFM", cfmLimitIface, "Limit");
2025 }
2026 catch (sdbusplus::exception_t& e)
2027 {
2028 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feistacc8a4e2019-04-02 14:23:57 -07002029 "ipmiOEMGetFscParameter: can't get cfm setting!",
James Feist5f957ca2019-03-14 15:33:55 -07002030 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002031 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002032 }
2033
James Feistacc8a4e2019-04-02 14:23:57 -07002034 double cfmMax = std::get<double>(cfmMaximum);
2035 double cfmLim = std::get<double>(cfmLimit);
James Feist5f957ca2019-03-14 15:33:55 -07002036
James Feistacc8a4e2019-04-02 14:23:57 -07002037 cfmLim = std::floor(cfmLim + 0.5);
2038 cfmMax = std::floor(cfmMax + 0.5);
2039 uint16_t cfmLimResp = static_cast<uint16_t>(cfmLim);
2040 uint16_t cfmMaxResp = static_cast<uint16_t>(cfmMax);
James Feist5f957ca2019-03-14 15:33:55 -07002041
James Feistacc8a4e2019-04-02 14:23:57 -07002042 return ipmi::responseSuccess(
2043 std::array<uint16_t, 2>{cfmLimResp, cfmMaxResp});
James Feist5f957ca2019-03-14 15:33:55 -07002044 }
James Feistacc8a4e2019-04-02 14:23:57 -07002045
James Feist5f957ca2019-03-14 15:33:55 -07002046 else
2047 {
2048 // todo other command parts possibly
James Feist5f957ca2019-03-14 15:33:55 -07002049 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002050 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002051 }
2052}
2053
Cheng C Yang773703a2019-08-15 09:41:11 +08002054using crConfigVariant =
2055 std::variant<bool, uint8_t, uint32_t, std::vector<uint8_t>, std::string>;
2056
2057int setCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2058 const crConfigVariant& value,
2059 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2060{
2061 boost::system::error_code ec;
2062 ctx->bus->yield_method_call<void>(
Kuiying Wange45333a2020-07-22 22:06:37 +08002063 ctx->yield, ec, "xyz.openbmc_project.PSURedundancy",
Cheng C Yang773703a2019-08-15 09:41:11 +08002064 "/xyz/openbmc_project/control/power_supply_redundancy",
2065 "org.freedesktop.DBus.Properties", "Set",
2066 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property, value);
2067 if (ec)
2068 {
2069 phosphor::logging::log<phosphor::logging::level::ERR>(
2070 "Failed to set dbus property to cold redundancy");
2071 return -1;
2072 }
2073
2074 return 0;
2075}
2076
Kuiying Wange45333a2020-07-22 22:06:37 +08002077int getCRConfig(
2078 ipmi::Context::ptr ctx, const std::string& property, crConfigVariant& value,
2079 const std::string& service = "xyz.openbmc_project.PSURedundancy",
2080 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
Cheng C Yang773703a2019-08-15 09:41:11 +08002081{
2082 boost::system::error_code ec;
2083 value = ctx->bus->yield_method_call<crConfigVariant>(
Yong Li19445ab2019-12-20 18:25:29 +08002084 ctx->yield, ec, service,
Cheng C Yang773703a2019-08-15 09:41:11 +08002085 "/xyz/openbmc_project/control/power_supply_redundancy",
2086 "org.freedesktop.DBus.Properties", "Get",
2087 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property);
2088 if (ec)
2089 {
2090 phosphor::logging::log<phosphor::logging::level::ERR>(
2091 "Failed to get dbus property to cold redundancy");
2092 return -1;
2093 }
2094 return 0;
2095}
2096
2097uint8_t getPSUCount(void)
2098{
2099 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2100 ipmi::Value num;
2101 try
2102 {
2103 num = ipmi::getDbusProperty(
2104 *dbus, "xyz.openbmc_project.PSURedundancy",
2105 "/xyz/openbmc_project/control/power_supply_redundancy",
2106 "xyz.openbmc_project.Control.PowerSupplyRedundancy", "PSUNumber");
2107 }
2108 catch (sdbusplus::exception_t& e)
2109 {
2110 phosphor::logging::log<phosphor::logging::level::ERR>(
2111 "Failed to get PSUNumber property from dbus interface");
2112 return 0;
2113 }
2114 uint8_t* pNum = std::get_if<uint8_t>(&num);
2115 if (!pNum)
2116 {
2117 phosphor::logging::log<phosphor::logging::level::ERR>(
2118 "Error to get PSU Number");
2119 return 0;
2120 }
2121 return *pNum;
2122}
2123
2124bool validateCRAlgo(std::vector<uint8_t>& conf, uint8_t num)
2125{
2126 if (conf.size() < num)
2127 {
2128 phosphor::logging::log<phosphor::logging::level::ERR>(
2129 "Invalid PSU Ranking");
2130 return false;
2131 }
2132 std::set<uint8_t> confSet;
2133 for (uint8_t i = 0; i < num; i++)
2134 {
2135 if (conf[i] > num)
2136 {
2137 phosphor::logging::log<phosphor::logging::level::ERR>(
2138 "PSU Ranking is larger than current PSU number");
2139 return false;
2140 }
2141 confSet.emplace(conf[i]);
2142 }
2143
2144 if (confSet.size() != num)
2145 {
2146 phosphor::logging::log<phosphor::logging::level::ERR>(
2147 "duplicate PSU Ranking");
2148 return false;
2149 }
2150 return true;
2151}
2152
2153enum class crParameter
2154{
2155 crStatus = 0,
2156 crFeature = 1,
2157 rotationFeature = 2,
2158 rotationAlgo = 3,
2159 rotationPeriod = 4,
Yong Li19445ab2019-12-20 18:25:29 +08002160 numOfPSU = 5,
2161 rotationRankOrderEffective = 6
Cheng C Yang773703a2019-08-15 09:41:11 +08002162};
2163
2164constexpr ipmi::Cc ccParameterNotSupported = 0x80;
2165static const constexpr uint32_t oneDay = 0x15180;
2166static const constexpr uint32_t oneMonth = 0xf53700;
2167static const constexpr uint8_t userSpecific = 0x01;
2168static const constexpr uint8_t crSetCompleted = 0;
2169ipmi::RspType<uint8_t> ipmiOEMSetCRConfig(ipmi::Context::ptr ctx,
2170 uint8_t parameter,
2171 ipmi::message::Payload& payload)
2172{
2173 switch (static_cast<crParameter>(parameter))
2174 {
Cheng C Yang773703a2019-08-15 09:41:11 +08002175 case crParameter::rotationFeature:
2176 {
2177 uint8_t param1;
2178 if (payload.unpack(param1) || !payload.fullyUnpacked())
2179 {
2180 return ipmi::responseReqDataLenInvalid();
2181 }
2182 // Rotation Enable can only be true or false
2183 if (param1 > 1)
2184 {
2185 return ipmi::responseInvalidFieldRequest();
2186 }
2187 if (setCRConfig(ctx, "RotationEnabled", static_cast<bool>(param1)))
2188 {
2189 return ipmi::responseResponseError();
2190 }
2191 break;
2192 }
2193 case crParameter::rotationAlgo:
2194 {
2195 // Rotation Algorithm can only be 0-BMC Specific or 1-User Specific
2196 std::string algoName;
2197 uint8_t param1;
2198 if (payload.unpack(param1))
2199 {
2200 return ipmi::responseReqDataLenInvalid();
2201 }
2202 switch (param1)
2203 {
2204 case 0:
2205 algoName = "xyz.openbmc_project.Control."
2206 "PowerSupplyRedundancy.Algo.bmcSpecific";
2207 break;
2208 case 1:
2209 algoName = "xyz.openbmc_project.Control."
2210 "PowerSupplyRedundancy.Algo.userSpecific";
2211 break;
2212 default:
2213 return ipmi::responseInvalidFieldRequest();
2214 }
2215 if (setCRConfig(ctx, "RotationAlgorithm", algoName))
2216 {
2217 return ipmi::responseResponseError();
2218 }
2219
2220 uint8_t numberOfPSU = getPSUCount();
2221 if (!numberOfPSU)
2222 {
2223 return ipmi::responseResponseError();
2224 }
2225 std::vector<uint8_t> rankOrder;
2226
2227 if (param1 == userSpecific)
2228 {
2229 if (payload.unpack(rankOrder) || !payload.fullyUnpacked())
2230 {
2231 ipmi::responseReqDataLenInvalid();
2232 }
Yong Li83315132019-10-23 17:42:24 +08002233 if (rankOrder.size() != numberOfPSU)
Cheng C Yang773703a2019-08-15 09:41:11 +08002234 {
2235 return ipmi::responseReqDataLenInvalid();
2236 }
2237
2238 if (!validateCRAlgo(rankOrder, numberOfPSU))
2239 {
2240 return ipmi::responseInvalidFieldRequest();
2241 }
2242 }
2243 else
2244 {
2245 if (rankOrder.size() > 0)
2246 {
2247 return ipmi::responseReqDataLenInvalid();
2248 }
2249 for (uint8_t i = 1; i <= numberOfPSU; i++)
2250 {
2251 rankOrder.emplace_back(i);
2252 }
2253 }
2254 if (setCRConfig(ctx, "RotationRankOrder", rankOrder))
2255 {
2256 return ipmi::responseResponseError();
2257 }
2258 break;
2259 }
2260 case crParameter::rotationPeriod:
2261 {
2262 // Minimum Rotation period is One day (86400 seconds) and Max
2263 // Rotation Period is 6 month (0xf53700 seconds)
2264 uint32_t period;
2265 if (payload.unpack(period) || !payload.fullyUnpacked())
2266 {
2267 return ipmi::responseReqDataLenInvalid();
2268 }
2269 if ((period < oneDay) || (period > oneMonth))
2270 {
2271 return ipmi::responseInvalidFieldRequest();
2272 }
2273 if (setCRConfig(ctx, "PeriodOfRotation", period))
2274 {
2275 return ipmi::responseResponseError();
2276 }
2277 break;
2278 }
2279 default:
2280 {
2281 return ipmi::response(ccParameterNotSupported);
2282 }
2283 }
2284
Cheng C Yang773703a2019-08-15 09:41:11 +08002285 return ipmi::responseSuccess(crSetCompleted);
2286}
2287
Yong Li83315132019-10-23 17:42:24 +08002288ipmi::RspType<uint8_t, std::variant<uint8_t, uint32_t, std::vector<uint8_t>>>
Cheng C Yang773703a2019-08-15 09:41:11 +08002289 ipmiOEMGetCRConfig(ipmi::Context::ptr ctx, uint8_t parameter)
2290{
2291 crConfigVariant value;
2292 switch (static_cast<crParameter>(parameter))
2293 {
2294 case crParameter::crStatus:
2295 {
2296 if (getCRConfig(ctx, "ColdRedundancyStatus", value))
2297 {
2298 return ipmi::responseResponseError();
2299 }
2300 std::string* pStatus = std::get_if<std::string>(&value);
2301 if (!pStatus)
2302 {
2303 phosphor::logging::log<phosphor::logging::level::ERR>(
2304 "Error to get ColdRedundancyStatus property");
2305 return ipmi::responseResponseError();
2306 }
2307 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2308 auto status =
2309 server::PowerSupplyRedundancy::convertStatusFromString(
2310 *pStatus);
2311 switch (status)
2312 {
2313 case server::PowerSupplyRedundancy::Status::inProgress:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002314 return ipmi::responseSuccess(parameter,
Kuiying Wange45333a2020-07-22 22:06:37 +08002315 static_cast<uint8_t>(1));
Cheng C Yang773703a2019-08-15 09:41:11 +08002316
2317 case server::PowerSupplyRedundancy::Status::completed:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002318 return ipmi::responseSuccess(parameter,
Kuiying Wange45333a2020-07-22 22:06:37 +08002319 static_cast<uint8_t>(0));
Cheng C Yang773703a2019-08-15 09:41:11 +08002320 default:
2321 phosphor::logging::log<phosphor::logging::level::ERR>(
2322 "Error to get valid status");
2323 return ipmi::responseResponseError();
2324 }
2325 }
2326 case crParameter::crFeature:
2327 {
Kuiying Wange45333a2020-07-22 22:06:37 +08002328 if (getCRConfig(ctx, "PowerSupplyRedundancyEnabled", value))
Cheng C Yang773703a2019-08-15 09:41:11 +08002329 {
2330 return ipmi::responseResponseError();
2331 }
2332 bool* pResponse = std::get_if<bool>(&value);
2333 if (!pResponse)
2334 {
2335 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wange45333a2020-07-22 22:06:37 +08002336 "Error to get PowerSupplyRedundancyEnabled property");
Cheng C Yang773703a2019-08-15 09:41:11 +08002337 return ipmi::responseResponseError();
2338 }
2339
Cheng C Yangf41e3342019-09-10 04:47:23 +08002340 return ipmi::responseSuccess(parameter,
2341 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002342 }
2343 case crParameter::rotationFeature:
2344 {
2345 if (getCRConfig(ctx, "RotationEnabled", value))
2346 {
2347 return ipmi::responseResponseError();
2348 }
2349 bool* pResponse = std::get_if<bool>(&value);
2350 if (!pResponse)
2351 {
2352 phosphor::logging::log<phosphor::logging::level::ERR>(
2353 "Error to get RotationEnabled property");
2354 return ipmi::responseResponseError();
2355 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002356 return ipmi::responseSuccess(parameter,
2357 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002358 }
2359 case crParameter::rotationAlgo:
2360 {
2361 if (getCRConfig(ctx, "RotationAlgorithm", value))
2362 {
2363 return ipmi::responseResponseError();
2364 }
2365
2366 std::string* pAlgo = std::get_if<std::string>(&value);
2367 if (!pAlgo)
2368 {
2369 phosphor::logging::log<phosphor::logging::level::ERR>(
2370 "Error to get RotationAlgorithm property");
2371 return ipmi::responseResponseError();
2372 }
Yong Li83315132019-10-23 17:42:24 +08002373 std::vector<uint8_t> response;
Cheng C Yang773703a2019-08-15 09:41:11 +08002374 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2375 auto algo =
2376 server::PowerSupplyRedundancy::convertAlgoFromString(*pAlgo);
Yong Li83315132019-10-23 17:42:24 +08002377
Cheng C Yang773703a2019-08-15 09:41:11 +08002378 switch (algo)
2379 {
2380 case server::PowerSupplyRedundancy::Algo::bmcSpecific:
Yong Li83315132019-10-23 17:42:24 +08002381 response.push_back(0);
Cheng C Yang773703a2019-08-15 09:41:11 +08002382 break;
2383 case server::PowerSupplyRedundancy::Algo::userSpecific:
Yong Li83315132019-10-23 17:42:24 +08002384 response.push_back(1);
Cheng C Yang773703a2019-08-15 09:41:11 +08002385 break;
2386 default:
2387 phosphor::logging::log<phosphor::logging::level::ERR>(
2388 "Error to get valid algo");
2389 return ipmi::responseResponseError();
2390 }
2391
2392 if (getCRConfig(ctx, "RotationRankOrder", value))
2393 {
2394 return ipmi::responseResponseError();
2395 }
2396 std::vector<uint8_t>* pResponse =
2397 std::get_if<std::vector<uint8_t>>(&value);
2398 if (!pResponse)
2399 {
2400 phosphor::logging::log<phosphor::logging::level::ERR>(
2401 "Error to get RotationRankOrder property");
2402 return ipmi::responseResponseError();
2403 }
Yong Li83315132019-10-23 17:42:24 +08002404
Cheng C Yang773703a2019-08-15 09:41:11 +08002405 std::copy(pResponse->begin(), pResponse->end(),
Yong Li83315132019-10-23 17:42:24 +08002406 std::back_inserter(response));
2407
Cheng C Yangf41e3342019-09-10 04:47:23 +08002408 return ipmi::responseSuccess(parameter, response);
Cheng C Yang773703a2019-08-15 09:41:11 +08002409 }
2410 case crParameter::rotationPeriod:
2411 {
2412 if (getCRConfig(ctx, "PeriodOfRotation", value))
2413 {
2414 return ipmi::responseResponseError();
2415 }
2416 uint32_t* pResponse = std::get_if<uint32_t>(&value);
2417 if (!pResponse)
2418 {
2419 phosphor::logging::log<phosphor::logging::level::ERR>(
2420 "Error to get RotationAlgorithm property");
2421 return ipmi::responseResponseError();
2422 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002423 return ipmi::responseSuccess(parameter, *pResponse);
Cheng C Yang773703a2019-08-15 09:41:11 +08002424 }
2425 case crParameter::numOfPSU:
2426 {
2427 uint8_t numberOfPSU = getPSUCount();
2428 if (!numberOfPSU)
2429 {
2430 return ipmi::responseResponseError();
2431 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002432 return ipmi::responseSuccess(parameter, numberOfPSU);
Cheng C Yang773703a2019-08-15 09:41:11 +08002433 }
Yong Li19445ab2019-12-20 18:25:29 +08002434 case crParameter::rotationRankOrderEffective:
2435 {
2436 if (getCRConfig(ctx, "RotationRankOrder", value,
2437 "xyz.openbmc_project.PSURedundancy"))
2438 {
2439 return ipmi::responseResponseError();
2440 }
2441 std::vector<uint8_t>* pResponse =
2442 std::get_if<std::vector<uint8_t>>(&value);
2443 if (!pResponse)
2444 {
2445 phosphor::logging::log<phosphor::logging::level::ERR>(
2446 "Error to get effective RotationRankOrder property");
2447 return ipmi::responseResponseError();
2448 }
2449 return ipmi::responseSuccess(parameter, *pResponse);
2450 }
Cheng C Yang773703a2019-08-15 09:41:11 +08002451 default:
2452 {
2453 return ipmi::response(ccParameterNotSupported);
2454 }
2455 }
2456}
2457
Zhu, Yungebe560b02019-04-21 21:19:21 -04002458ipmi::RspType<> ipmiOEMSetFaultIndication(uint8_t sourceId, uint8_t faultType,
2459 uint8_t faultState,
2460 uint8_t faultGroup,
2461 std::array<uint8_t, 8>& ledStateData)
2462{
Zhu, Yungebe560b02019-04-21 21:19:21 -04002463 constexpr auto maxFaultType = static_cast<size_t>(RemoteFaultType::max);
2464 static const std::array<std::string, maxFaultType> faultNames = {
2465 "faultFan", "faultTemp", "faultPower",
2466 "faultDriveSlot", "faultSoftware", "faultMemory"};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002467
2468 constexpr uint8_t maxFaultSource = 0x4;
2469 constexpr uint8_t skipLEDs = 0xFF;
2470 constexpr uint8_t pinSize = 64;
2471 constexpr uint8_t groupSize = 16;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002472 constexpr uint8_t groupNum = 5; // 4 for fault memory, 1 for faultFan
Zhu, Yungebe560b02019-04-21 21:19:21 -04002473
Zhikui Rence4e73f2019-12-06 13:59:47 -08002474 // same pin names need to be defined in dts file
2475 static const std::array<std::array<std::string, groupSize>, groupNum>
2476 faultLedPinNames = {{
2477 "LED_CPU1_CH1_DIMM1_FAULT",
2478 "LED_CPU1_CH1_DIMM2_FAULT",
2479 "LED_CPU1_CH2_DIMM1_FAULT",
2480 "LED_CPU1_CH2_DIMM2_FAULT",
2481 "LED_CPU1_CH3_DIMM1_FAULT",
2482 "LED_CPU1_CH3_DIMM2_FAULT",
2483 "LED_CPU1_CH4_DIMM1_FAULT",
2484 "LED_CPU1_CH4_DIMM2_FAULT",
2485 "LED_CPU1_CH5_DIMM1_FAULT",
2486 "LED_CPU1_CH5_DIMM2_FAULT",
2487 "LED_CPU1_CH6_DIMM1_FAULT",
2488 "LED_CPU1_CH6_DIMM2_FAULT",
2489 "",
2490 "",
2491 "",
2492 "", // end of group1
2493 "LED_CPU2_CH1_DIMM1_FAULT",
2494 "LED_CPU2_CH1_DIMM2_FAULT",
2495 "LED_CPU2_CH2_DIMM1_FAULT",
2496 "LED_CPU2_CH2_DIMM2_FAULT",
2497 "LED_CPU2_CH3_DIMM1_FAULT",
2498 "LED_CPU2_CH3_DIMM2_FAULT",
2499 "LED_CPU2_CH4_DIMM1_FAULT",
2500 "LED_CPU2_CH4_DIMM2_FAULT",
2501 "LED_CPU2_CH5_DIMM1_FAULT",
2502 "LED_CPU2_CH5_DIMM2_FAULT",
2503 "LED_CPU2_CH6_DIMM1_FAULT",
2504 "LED_CPU2_CH6_DIMM2_FAULT",
2505 "",
2506 "",
2507 "",
2508 "", // endof group2
2509 "LED_CPU3_CH1_DIMM1_FAULT",
2510 "LED_CPU3_CH1_DIMM2_FAULT",
2511 "LED_CPU3_CH2_DIMM1_FAULT",
2512 "LED_CPU3_CH2_DIMM2_FAULT",
2513 "LED_CPU3_CH3_DIMM1_FAULT",
2514 "LED_CPU3_CH3_DIMM2_FAULT",
2515 "LED_CPU3_CH4_DIMM1_FAULT",
2516 "LED_CPU3_CH4_DIMM2_FAULT",
2517 "LED_CPU3_CH5_DIMM1_FAULT",
2518 "LED_CPU3_CH5_DIMM2_FAULT",
2519 "LED_CPU3_CH6_DIMM1_FAULT",
2520 "LED_CPU3_CH6_DIMM2_FAULT",
2521 "",
2522 "",
2523 "",
2524 "", // end of group3
2525 "LED_CPU4_CH1_DIMM1_FAULT",
2526 "LED_CPU4_CH1_DIMM2_FAULT",
2527 "LED_CPU4_CH2_DIMM1_FAULT",
2528 "LED_CPU4_CH2_DIMM2_FAULT",
2529 "LED_CPU4_CH3_DIMM1_FAULT",
2530 "LED_CPU4_CH3_DIMM2_FAULT",
2531 "LED_CPU4_CH4_DIMM1_FAULT",
2532 "LED_CPU4_CH4_DIMM2_FAULT",
2533 "LED_CPU4_CH5_DIMM1_FAULT",
2534 "LED_CPU4_CH5_DIMM2_FAULT",
2535 "LED_CPU4_CH6_DIMM1_FAULT",
2536 "LED_CPU4_CH6_DIMM2_FAULT",
2537 "",
2538 "",
2539 "",
2540 "", // end of group4
2541 "LED_FAN1_FAULT",
2542 "LED_FAN2_FAULT",
2543 "LED_FAN3_FAULT",
2544 "LED_FAN4_FAULT",
2545 "LED_FAN5_FAULT",
2546 "LED_FAN6_FAULT",
2547 "LED_FAN7_FAULT",
2548 "LED_FAN8_FAULT",
2549 "",
2550 "",
2551 "",
2552 "",
2553 "",
2554 "",
2555 "",
2556 "" // end of group5
2557 }};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002558
Zhikui Rence4e73f2019-12-06 13:59:47 -08002559 // Validate the source, fault type --
2560 // (Byte 1) sourceId: Unspecified, Hot-Swap Controller 0, Hot-Swap
2561 // Controller 1, BIOS (Byte 2) fault type: fan, temperature, power,
2562 // driveslot, software, memory (Byte 3) FaultState: OK, Degraded,
2563 // Non-Critical, Critical, Non-Recoverable, (Byte 4) is faultGroup,
2564 // definition differs based on fault type (Byte 2)
2565 // Type Fan=> Group: 0=FanGroupID, FF-not used
2566 // Byte 5-11 00h, not used
2567 // Byte12 FanLedState [7:0]-Fans 7:0
2568 // Type Memory=> Group: 0 = DIMM GroupID, FF-not used
2569 // Byte 5:12 - DIMM LED state (64bit field, LS Byte first)
2570 // [63:48] = CPU4 channels 7:0, 2 bits per channel
2571 // [47:32] = CPU3 channels 7:0, 2 bits per channel
2572 // [31:16] = CPU2 channels 7:0, 2 bits per channel
2573 // [15:0] = CPU1 channels 7:0, 2 bits per channel
2574 // Type Other=> Component Fault LED Group ID, not used set to 0xFF
2575 // Byte[5:12]: reserved 0x00h
Zhu, Yungebe560b02019-04-21 21:19:21 -04002576 if ((sourceId >= maxFaultSource) ||
2577 (faultType >= static_cast<int8_t>(RemoteFaultType::max)) ||
2578 (faultState >= static_cast<int8_t>(RemoteFaultState::maxFaultState)) ||
2579 (faultGroup >= static_cast<int8_t>(DimmFaultType::maxFaultGroup)))
2580 {
2581 return ipmi::responseParmOutOfRange();
2582 }
2583
Zhikui Rence4e73f2019-12-06 13:59:47 -08002584 size_t pinGroupOffset = 0;
2585 size_t pinGroupMax = pinSize / groupSize;
2586 if (RemoteFaultType::fan == RemoteFaultType(faultType))
Zhu, Yungebe560b02019-04-21 21:19:21 -04002587 {
Zhikui Rence4e73f2019-12-06 13:59:47 -08002588 pinGroupOffset = 4;
2589 pinGroupMax = groupNum - pinSize / groupSize;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002590 }
2591
2592 switch (RemoteFaultType(faultType))
2593 {
2594 case (RemoteFaultType::fan):
2595 case (RemoteFaultType::memory):
2596 {
2597 if (faultGroup == skipLEDs)
2598 {
2599 return ipmi::responseSuccess();
2600 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002601 // calculate led state bit filed count, each byte has 8bits
2602 // the maximum bits will be 8 * 8 bits
2603 constexpr uint8_t size = sizeof(ledStateData) * 8;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002604
2605 // assemble ledState
2606 uint64_t ledState = 0;
2607 bool hasError = false;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002608 for (int i = 0; i < sizeof(ledStateData); i++)
2609 {
2610 ledState = (uint64_t)(ledState << 8);
2611 ledState = (uint64_t)(ledState | (uint64_t)ledStateData[i]);
2612 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002613 std::bitset<size> ledStateBits(ledState);
Zhu, Yungebe560b02019-04-21 21:19:21 -04002614
Zhikui Rence4e73f2019-12-06 13:59:47 -08002615 for (int group = 0; group < pinGroupMax; group++)
2616 {
2617 for (int i = 0; i < groupSize; i++)
2618 { // skip non-existing pins
2619 if (0 == faultLedPinNames[group + pinGroupOffset][i].size())
2620 {
2621 continue;
2622 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002623
Zhikui Rence4e73f2019-12-06 13:59:47 -08002624 gpiod::line line = gpiod::find_line(
2625 faultLedPinNames[group + pinGroupOffset][i]);
2626 if (!line)
2627 {
2628 phosphor::logging::log<phosphor::logging::level::ERR>(
2629 "Not Find Led Gpio Device!",
2630 phosphor::logging::entry(
2631 "DEVICE=%s",
2632 faultLedPinNames[group + pinGroupOffset][i]
2633 .c_str()));
2634 hasError = true;
2635 continue;
2636 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002637
Zhikui Rence4e73f2019-12-06 13:59:47 -08002638 bool activeHigh =
2639 (line.active_state() == gpiod::line::ACTIVE_HIGH);
2640 try
2641 {
2642 line.request(
2643 {"faultLed", gpiod::line_request::DIRECTION_OUTPUT,
2644 activeHigh
2645 ? 0
2646 : gpiod::line_request::FLAG_ACTIVE_LOW});
2647 line.set_value(ledStateBits[i + group * groupSize]);
2648 }
2649 catch (std::system_error&)
2650 {
2651 phosphor::logging::log<phosphor::logging::level::ERR>(
2652 "Error write Led Gpio Device!",
2653 phosphor::logging::entry(
2654 "DEVICE=%s",
2655 faultLedPinNames[group + pinGroupOffset][i]
2656 .c_str()));
2657 hasError = true;
2658 continue;
2659 }
2660 } // for int i
2661 }
2662 if (hasError)
2663 {
2664 return ipmi::responseResponseError();
Zhu, Yungebe560b02019-04-21 21:19:21 -04002665 }
2666 break;
2667 }
2668 default:
2669 {
2670 // now only support two fault types
2671 return ipmi::responseParmOutOfRange();
2672 }
Zhikui Rence4e73f2019-12-06 13:59:47 -08002673 } // switch
Zhu, Yungebe560b02019-04-21 21:19:21 -04002674 return ipmi::responseSuccess();
2675}
2676
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302677ipmi::RspType<uint8_t> ipmiOEMReadBoardProductId()
2678{
2679 uint8_t prodId = 0;
2680 try
2681 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002682 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302683 const DbusObjectInfo& object = getDbusObject(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002684 *dbus, "xyz.openbmc_project.Inventory.Item.Board",
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302685 "/xyz/openbmc_project/inventory/system/board/", "Baseboard");
2686 const Value& propValue = getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002687 *dbus, object.second, object.first,
Suryakanth Sekar6c57e5c2020-01-10 17:11:58 +05302688 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
2689 "ProductId");
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302690 prodId = static_cast<uint8_t>(std::get<uint64_t>(propValue));
2691 }
2692 catch (std::exception& e)
2693 {
2694 phosphor::logging::log<phosphor::logging::level::ERR>(
2695 "ipmiOEMReadBoardProductId: Product ID read failed!",
2696 phosphor::logging::entry("ERR=%s", e.what()));
2697 }
2698 return ipmi::responseSuccess(prodId);
2699}
2700
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302701/** @brief implements the get security mode command
2702 * @param ctx - ctx pointer
2703 *
2704 * @returns IPMI completion code with following data
2705 * - restriction mode value - As specified in
2706 * xyz.openbmc_project.Control.Security.RestrictionMode.interface.yaml
2707 * - special mode value - As specified in
2708 * xyz.openbmc_project.Control.Security.SpecialMode.interface.yaml
2709 */
2710ipmi::RspType<uint8_t, uint8_t> ipmiGetSecurityMode(ipmi::Context::ptr ctx)
2711{
2712 namespace securityNameSpace =
2713 sdbusplus::xyz::openbmc_project::Control::Security::server;
2714 uint8_t restrictionModeValue = 0;
2715 uint8_t specialModeValue = 0;
2716
2717 boost::system::error_code ec;
2718 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002719 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302720 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2721 restricionModeProperty);
2722 if (ec)
2723 {
2724 phosphor::logging::log<phosphor::logging::level::ERR>(
2725 "ipmiGetSecurityMode: failed to get RestrictionMode property",
2726 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2727 return ipmi::responseUnspecifiedError();
2728 }
2729 restrictionModeValue = static_cast<uint8_t>(
2730 securityNameSpace::RestrictionMode::convertModesFromString(
2731 std::get<std::string>(varRestrMode)));
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302732 auto varSpecialMode =
2733 ctx->bus->yield_method_call<std::variant<std::string>>(
2734 ctx->yield, ec, specialModeService, specialModeBasePath,
2735 dBusPropertyIntf, dBusPropertyGetMethod, specialModeIntf,
2736 specialModeProperty);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302737 if (ec)
2738 {
2739 phosphor::logging::log<phosphor::logging::level::ERR>(
2740 "ipmiGetSecurityMode: failed to get SpecialMode property",
2741 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2742 // fall through, let us not worry about SpecialMode property, which is
2743 // not required in user scenario
2744 }
2745 else
2746 {
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302747 specialModeValue = static_cast<uint8_t>(
2748 securityNameSpace::SpecialMode::convertModesFromString(
2749 std::get<std::string>(varSpecialMode)));
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302750 }
2751 return ipmi::responseSuccess(restrictionModeValue, specialModeValue);
2752}
2753
2754/** @brief implements the set security mode command
2755 * Command allows to upgrade the restriction mode and won't allow
2756 * to downgrade from system interface
2757 * @param ctx - ctx pointer
2758 * @param restrictionMode - restriction mode value to be set.
2759 *
2760 * @returns IPMI completion code
2761 */
2762ipmi::RspType<> ipmiSetSecurityMode(ipmi::Context::ptr ctx,
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302763 uint8_t restrictionMode,
2764 std::optional<uint8_t> specialMode)
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302765{
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302766#ifndef BMC_VALIDATION_UNSECURE_FEATURE
2767 if (specialMode)
2768 {
2769 return ipmi::responseReqDataLenInvalid();
2770 }
2771#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302772 namespace securityNameSpace =
2773 sdbusplus::xyz::openbmc_project::Control::Security::server;
2774
2775 ChannelInfo chInfo;
2776 if (getChannelInfo(ctx->channel, chInfo) != ccSuccess)
2777 {
2778 phosphor::logging::log<phosphor::logging::level::ERR>(
2779 "ipmiSetSecurityMode: Failed to get Channel Info",
2780 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
2781 return ipmi::responseUnspecifiedError();
2782 }
2783 auto reqMode =
2784 static_cast<securityNameSpace::RestrictionMode::Modes>(restrictionMode);
2785
2786 if ((reqMode < securityNameSpace::RestrictionMode::Modes::Provisioning) ||
2787 (reqMode >
2788 securityNameSpace::RestrictionMode::Modes::ProvisionedHostDisabled))
2789 {
2790 return ipmi::responseInvalidFieldRequest();
2791 }
2792
2793 boost::system::error_code ec;
2794 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002795 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302796 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2797 restricionModeProperty);
2798 if (ec)
2799 {
2800 phosphor::logging::log<phosphor::logging::level::ERR>(
2801 "ipmiSetSecurityMode: failed to get RestrictionMode property",
2802 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2803 return ipmi::responseUnspecifiedError();
2804 }
2805 auto currentRestrictionMode =
2806 securityNameSpace::RestrictionMode::convertModesFromString(
2807 std::get<std::string>(varRestrMode));
2808
2809 if (chInfo.mediumType !=
2810 static_cast<uint8_t>(EChannelMediumType::lan8032) &&
2811 currentRestrictionMode > reqMode)
2812 {
2813 phosphor::logging::log<phosphor::logging::level::ERR>(
2814 "ipmiSetSecurityMode - Downgrading security mode not supported "
2815 "through system interface",
2816 phosphor::logging::entry(
2817 "CUR_MODE=%d", static_cast<uint8_t>(currentRestrictionMode)),
2818 phosphor::logging::entry("REQ_MODE=%d", restrictionMode));
2819 return ipmi::responseCommandNotAvailable();
2820 }
2821
2822 ec.clear();
2823 ctx->bus->yield_method_call<>(
James Feist28c72902019-09-16 10:34:07 -07002824 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302825 dBusPropertyIntf, dBusPropertySetMethod, restricionModeIntf,
2826 restricionModeProperty,
2827 static_cast<std::variant<std::string>>(
2828 securityNameSpace::convertForMessage(reqMode)));
2829
2830 if (ec)
2831 {
2832 phosphor::logging::log<phosphor::logging::level::ERR>(
2833 "ipmiSetSecurityMode: failed to set RestrictionMode property",
2834 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2835 return ipmi::responseUnspecifiedError();
2836 }
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302837
2838#ifdef BMC_VALIDATION_UNSECURE_FEATURE
2839 if (specialMode)
2840 {
Jayaprakash Mutyalad77489f2020-09-05 01:00:04 +00002841 constexpr uint8_t mfgMode = 0x01;
2842 // Manufacturing mode is reserved. So can't enable this mode.
2843 if (specialMode.value() == mfgMode)
2844 {
2845 phosphor::logging::log<phosphor::logging::level::INFO>(
2846 "ipmiSetSecurityMode: Can't enable Manufacturing mode");
2847 return ipmi::responseInvalidFieldRequest();
2848 }
2849
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302850 ec.clear();
2851 ctx->bus->yield_method_call<>(
2852 ctx->yield, ec, specialModeService, specialModeBasePath,
2853 dBusPropertyIntf, dBusPropertySetMethod, specialModeIntf,
2854 specialModeProperty,
2855 static_cast<std::variant<std::string>>(
2856 securityNameSpace::convertForMessage(
2857 static_cast<securityNameSpace::SpecialMode::Modes>(
2858 specialMode.value()))));
2859
2860 if (ec)
2861 {
2862 phosphor::logging::log<phosphor::logging::level::ERR>(
2863 "ipmiSetSecurityMode: failed to set SpecialMode property",
2864 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2865 return ipmi::responseUnspecifiedError();
2866 }
2867 }
2868#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302869 return ipmi::responseSuccess();
2870}
2871
Vernon Mauery4ac799d2019-05-20 15:50:37 -07002872ipmi::RspType<uint8_t /* restore status */>
2873 ipmiRestoreConfiguration(const std::array<uint8_t, 3>& clr, uint8_t cmd)
2874{
2875 static constexpr std::array<uint8_t, 3> expClr = {'C', 'L', 'R'};
2876
2877 if (clr != expClr)
2878 {
2879 return ipmi::responseInvalidFieldRequest();
2880 }
2881 constexpr uint8_t cmdStatus = 0;
2882 constexpr uint8_t cmdDefaultRestore = 0xaa;
2883 constexpr uint8_t cmdFullRestore = 0xbb;
2884 constexpr uint8_t cmdFormat = 0xcc;
2885
2886 constexpr const char* restoreOpFname = "/tmp/.rwfs/.restore_op";
2887
2888 switch (cmd)
2889 {
2890 case cmdStatus:
2891 break;
2892 case cmdDefaultRestore:
2893 case cmdFullRestore:
2894 case cmdFormat:
2895 {
2896 // write file to rwfs root
2897 int value = (cmd - 1) & 0x03; // map aa, bb, cc => 1, 2, 3
2898 std::ofstream restoreFile(restoreOpFname);
2899 if (!restoreFile)
2900 {
2901 return ipmi::responseUnspecifiedError();
2902 }
2903 restoreFile << value << "\n";
2904 break;
2905 }
2906 default:
2907 return ipmi::responseInvalidFieldRequest();
2908 }
2909
2910 constexpr uint8_t restorePending = 0;
2911 constexpr uint8_t restoreComplete = 1;
2912
2913 uint8_t restoreStatus = std::filesystem::exists(restoreOpFname)
2914 ? restorePending
2915 : restoreComplete;
2916 return ipmi::responseSuccess(restoreStatus);
2917}
2918
Chen Yugang39736d52019-07-12 16:24:33 +08002919ipmi::RspType<uint8_t> ipmiOEMGetNmiSource(void)
2920{
2921 uint8_t bmcSource;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002922 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002923
2924 try
2925 {
2926 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2927 std::string service =
2928 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
2929 Value variant =
2930 getDbusProperty(*dbus, service, oemNmiSourceObjPath,
2931 oemNmiSourceIntf, oemNmiBmcSourceObjPathProp);
2932
2933 switch (nmi::NMISource::convertBMCSourceSignalFromString(
2934 std::get<std::string>(variant)))
2935 {
2936 case nmi::NMISource::BMCSourceSignal::None:
2937 bmcSource = static_cast<uint8_t>(NmiSource::none);
2938 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002939 case nmi::NMISource::BMCSourceSignal::FrontPanelButton:
2940 bmcSource = static_cast<uint8_t>(NmiSource::frontPanelButton);
Chen Yugang39736d52019-07-12 16:24:33 +08002941 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002942 case nmi::NMISource::BMCSourceSignal::Watchdog:
2943 bmcSource = static_cast<uint8_t>(NmiSource::watchdog);
Chen Yugang39736d52019-07-12 16:24:33 +08002944 break;
2945 case nmi::NMISource::BMCSourceSignal::ChassisCmd:
2946 bmcSource = static_cast<uint8_t>(NmiSource::chassisCmd);
2947 break;
2948 case nmi::NMISource::BMCSourceSignal::MemoryError:
2949 bmcSource = static_cast<uint8_t>(NmiSource::memoryError);
2950 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002951 case nmi::NMISource::BMCSourceSignal::PciBusError:
2952 bmcSource = static_cast<uint8_t>(NmiSource::pciBusError);
Chen Yugang39736d52019-07-12 16:24:33 +08002953 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002954 case nmi::NMISource::BMCSourceSignal::PCH:
2955 bmcSource = static_cast<uint8_t>(NmiSource::pch);
Chen Yugang39736d52019-07-12 16:24:33 +08002956 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002957 case nmi::NMISource::BMCSourceSignal::Chipset:
2958 bmcSource = static_cast<uint8_t>(NmiSource::chipset);
Chen Yugang39736d52019-07-12 16:24:33 +08002959 break;
2960 default:
2961 phosphor::logging::log<phosphor::logging::level::ERR>(
2962 "NMI source: invalid property!",
2963 phosphor::logging::entry(
2964 "PROP=%s", std::get<std::string>(variant).c_str()));
2965 return ipmi::responseResponseError();
2966 }
2967 }
2968 catch (sdbusplus::exception::SdBusError& e)
2969 {
2970 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
2971 return ipmi::responseResponseError();
2972 }
2973
2974 return ipmi::responseSuccess(bmcSource);
2975}
2976
2977ipmi::RspType<> ipmiOEMSetNmiSource(uint8_t sourceId)
2978{
Chen Yugang97cf96e2019-11-01 08:55:11 +08002979 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002980
2981 nmi::NMISource::BMCSourceSignal bmcSourceSignal =
2982 nmi::NMISource::BMCSourceSignal::None;
2983
2984 switch (NmiSource(sourceId))
2985 {
2986 case NmiSource::none:
2987 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::None;
2988 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002989 case NmiSource::frontPanelButton:
2990 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::FrontPanelButton;
Chen Yugang39736d52019-07-12 16:24:33 +08002991 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002992 case NmiSource::watchdog:
2993 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Watchdog;
Chen Yugang39736d52019-07-12 16:24:33 +08002994 break;
2995 case NmiSource::chassisCmd:
2996 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::ChassisCmd;
2997 break;
2998 case NmiSource::memoryError:
2999 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::MemoryError;
3000 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003001 case NmiSource::pciBusError:
3002 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PciBusError;
Chen Yugang39736d52019-07-12 16:24:33 +08003003 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003004 case NmiSource::pch:
3005 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PCH;
Chen Yugang39736d52019-07-12 16:24:33 +08003006 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003007 case NmiSource::chipset:
3008 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Chipset;
Chen Yugang39736d52019-07-12 16:24:33 +08003009 break;
3010 default:
3011 phosphor::logging::log<phosphor::logging::level::ERR>(
3012 "NMI source: invalid property!");
3013 return ipmi::responseResponseError();
3014 }
3015
3016 try
3017 {
3018 // keep NMI signal source
3019 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3020 std::string service =
3021 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
Chen Yugang97cf96e2019-11-01 08:55:11 +08003022 setDbusProperty(*dbus, service, oemNmiSourceObjPath, oemNmiSourceIntf,
3023 oemNmiBmcSourceObjPathProp,
3024 nmi::convertForMessage(bmcSourceSignal));
Chen Yugang99be6332019-08-09 16:20:48 +08003025 // set Enabled property to inform NMI source handling
3026 // to trigger a NMI_OUT BSOD.
3027 // if it's triggered by NMI source property changed,
3028 // NMI_OUT BSOD could be missed if the same source occurs twice in a row
3029 if (bmcSourceSignal != nmi::NMISource::BMCSourceSignal::None)
3030 {
3031 setDbusProperty(*dbus, service, oemNmiSourceObjPath,
3032 oemNmiSourceIntf, oemNmiEnabledObjPathProp,
3033 static_cast<bool>(true));
3034 }
Chen Yugang39736d52019-07-12 16:24:33 +08003035 }
3036 catch (sdbusplus::exception_t& e)
3037 {
3038 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3039 return ipmi::responseResponseError();
3040 }
3041
3042 return ipmi::responseSuccess();
3043}
3044
James Feist63efafa2019-07-24 12:39:21 -07003045namespace dimmOffset
3046{
3047constexpr const char* dimmPower = "DimmPower";
3048constexpr const char* staticCltt = "StaticCltt";
3049constexpr const char* offsetPath = "/xyz/openbmc_project/Inventory/Item/Dimm";
3050constexpr const char* offsetInterface =
3051 "xyz.openbmc_project.Inventory.Item.Dimm.Offset";
3052constexpr const char* property = "DimmOffset";
3053
3054}; // namespace dimmOffset
3055
3056ipmi::RspType<>
3057 ipmiOEMSetDimmOffset(uint8_t type,
3058 const std::vector<std::tuple<uint8_t, uint8_t>>& data)
3059{
3060 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3061 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3062 {
3063 return ipmi::responseInvalidFieldRequest();
3064 }
3065
3066 if (data.empty())
3067 {
3068 return ipmi::responseInvalidFieldRequest();
3069 }
3070 nlohmann::json json;
3071
3072 std::ifstream jsonStream(dimmOffsetFile);
3073 if (jsonStream.good())
3074 {
3075 json = nlohmann::json::parse(jsonStream, nullptr, false);
3076 if (json.is_discarded())
3077 {
3078 json = nlohmann::json();
3079 }
3080 jsonStream.close();
3081 }
3082
3083 std::string typeName;
3084 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3085 {
3086 typeName = dimmOffset::dimmPower;
3087 }
3088 else
3089 {
3090 typeName = dimmOffset::staticCltt;
3091 }
3092
3093 nlohmann::json& field = json[typeName];
3094
3095 for (const auto& [index, value] : data)
3096 {
3097 field[index] = value;
3098 }
3099
3100 for (nlohmann::json& val : field)
3101 {
3102 if (val == nullptr)
3103 {
3104 val = static_cast<uint8_t>(0);
3105 }
3106 }
3107
3108 std::ofstream output(dimmOffsetFile);
3109 if (!output.good())
3110 {
3111 std::cerr << "Error writing json file\n";
3112 return ipmi::responseResponseError();
3113 }
3114
3115 output << json.dump(4);
3116
3117 if (type == static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3118 {
3119 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
3120
3121 std::variant<std::vector<uint8_t>> offsets =
3122 field.get<std::vector<uint8_t>>();
3123 auto call = bus->new_method_call(
3124 settingsBusName, dimmOffset::offsetPath, PROP_INTF, "Set");
3125 call.append(dimmOffset::offsetInterface, dimmOffset::property, offsets);
3126 try
3127 {
3128 bus->call(call);
3129 }
3130 catch (sdbusplus::exception_t& e)
3131 {
3132 phosphor::logging::log<phosphor::logging::level::ERR>(
3133 "ipmiOEMSetDimmOffset: can't set dimm offsets!",
3134 phosphor::logging::entry("ERR=%s", e.what()));
3135 return ipmi::responseResponseError();
3136 }
3137 }
3138
3139 return ipmi::responseSuccess();
3140}
3141
3142ipmi::RspType<uint8_t> ipmiOEMGetDimmOffset(uint8_t type, uint8_t index)
3143{
3144
3145 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3146 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3147 {
3148 return ipmi::responseInvalidFieldRequest();
3149 }
3150
3151 std::ifstream jsonStream(dimmOffsetFile);
3152
3153 auto json = nlohmann::json::parse(jsonStream, nullptr, false);
3154 if (json.is_discarded())
3155 {
3156 std::cerr << "File error in " << dimmOffsetFile << "\n";
3157 return ipmi::responseResponseError();
3158 }
3159
3160 std::string typeName;
3161 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3162 {
3163 typeName = dimmOffset::dimmPower;
3164 }
3165 else
3166 {
3167 typeName = dimmOffset::staticCltt;
3168 }
3169
3170 auto it = json.find(typeName);
3171 if (it == json.end())
3172 {
3173 return ipmi::responseInvalidFieldRequest();
3174 }
3175
3176 if (it->size() <= index)
3177 {
3178 return ipmi::responseInvalidFieldRequest();
3179 }
3180
3181 uint8_t resp = it->at(index).get<uint8_t>();
3182 return ipmi::responseSuccess(resp);
3183}
3184
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003185namespace boot_options
3186{
3187
3188using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
3189using IpmiValue = uint8_t;
3190constexpr auto ipmiDefault = 0;
3191
3192std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
3193 {0x01, Source::Sources::Network},
3194 {0x02, Source::Sources::Disk},
3195 {0x05, Source::Sources::ExternalMedia},
3196 {0x0f, Source::Sources::RemovableMedia},
3197 {ipmiDefault, Source::Sources::Default}};
3198
3199std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003200 {0x06, Mode::Modes::Setup}, {ipmiDefault, Mode::Modes::Regular}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003201
3202std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
3203 {Source::Sources::Network, 0x01},
3204 {Source::Sources::Disk, 0x02},
3205 {Source::Sources::ExternalMedia, 0x05},
3206 {Source::Sources::RemovableMedia, 0x0f},
3207 {Source::Sources::Default, ipmiDefault}};
3208
3209std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003210 {Mode::Modes::Setup, 0x06}, {Mode::Modes::Regular, ipmiDefault}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003211
3212static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
3213static constexpr auto bootSourceIntf =
3214 "xyz.openbmc_project.Control.Boot.Source";
3215static constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
3216static constexpr auto persistentObjPath =
3217 "/xyz/openbmc_project/control/host0/boot";
3218static constexpr auto oneTimePath =
3219 "/xyz/openbmc_project/control/host0/boot/one_time";
3220static constexpr auto bootSourceProp = "BootSource";
3221static constexpr auto bootModeProp = "BootMode";
3222static constexpr auto oneTimeBootEnableProp = "Enabled";
3223static constexpr auto httpBootMode =
3224 "xyz.openbmc_project.Control.Boot.Source.Sources.Http";
3225
3226enum class BootOptionParameter : size_t
3227{
3228 setInProgress = 0x0,
3229 bootFlags = 0x5,
3230};
3231static constexpr uint8_t setComplete = 0x0;
3232static constexpr uint8_t setInProgress = 0x1;
3233static uint8_t transferStatus = setComplete;
3234static constexpr uint8_t setParmVersion = 0x01;
3235static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
3236static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
3237static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
3238static constexpr uint8_t httpBoot = 0xd;
3239static constexpr uint8_t bootSourceMask = 0x3c;
3240
3241} // namespace boot_options
3242
3243ipmi::RspType<uint8_t, // version
3244 uint8_t, // param
3245 uint8_t, // data0, dependent on parameter
3246 std::optional<uint8_t> // data1, dependent on parameter
3247 >
3248 ipmiOemGetEfiBootOptions(uint8_t parameter, uint8_t set, uint8_t block)
3249{
3250 using namespace boot_options;
3251 uint8_t bootOption = 0;
3252
3253 if (parameter == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3254 {
3255 return ipmi::responseSuccess(setParmVersion, parameter, transferStatus,
3256 std::nullopt);
3257 }
3258
3259 if (parameter != static_cast<uint8_t>(BootOptionParameter::bootFlags))
3260 {
3261 phosphor::logging::log<phosphor::logging::level::ERR>(
3262 "Unsupported parameter");
3263 return ipmi::responseResponseError();
3264 }
3265
3266 try
3267 {
3268 auto oneTimeEnabled = false;
3269 // read one time Enabled property
3270 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3271 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3272 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3273 enabledIntf, oneTimeBootEnableProp);
3274 oneTimeEnabled = std::get<bool>(variant);
3275
3276 // get BootSource and BootMode properties
3277 // according to oneTimeEnable
3278 auto bootObjPath = oneTimePath;
3279 if (oneTimeEnabled == false)
3280 {
3281 bootObjPath = persistentObjPath;
3282 }
3283
3284 service = getService(*dbus, bootModeIntf, bootObjPath);
3285 variant = getDbusProperty(*dbus, service, bootObjPath, bootModeIntf,
3286 bootModeProp);
3287
3288 auto bootMode =
3289 Mode::convertModesFromString(std::get<std::string>(variant));
3290
3291 service = getService(*dbus, bootSourceIntf, bootObjPath);
3292 variant = getDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3293 bootSourceProp);
3294
3295 if (std::get<std::string>(variant) == httpBootMode)
3296 {
3297 bootOption = httpBoot;
3298 }
3299 else
3300 {
3301 auto bootSource = Source::convertSourcesFromString(
3302 std::get<std::string>(variant));
3303 bootOption = sourceDbusToIpmi.at(bootSource);
3304 if (Source::Sources::Default == bootSource)
3305 {
3306 bootOption = modeDbusToIpmi.at(bootMode);
3307 }
3308 }
3309
3310 uint8_t oneTime = oneTimeEnabled ? setParmBootFlagsValidOneTime
3311 : setParmBootFlagsValidPermanent;
3312 bootOption <<= 2; // shift for responseconstexpr
3313 return ipmi::responseSuccess(setParmVersion, parameter, oneTime,
3314 bootOption);
3315 }
3316 catch (sdbusplus::exception_t& e)
3317 {
3318 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3319 return ipmi::responseResponseError();
3320 }
3321}
3322
3323ipmi::RspType<> ipmiOemSetEfiBootOptions(uint8_t bootFlag, uint8_t bootParam,
3324 std::optional<uint8_t> bootOption)
3325{
3326 using namespace boot_options;
3327 auto oneTimeEnabled = false;
3328
3329 if (bootFlag == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3330 {
3331 if (bootOption)
3332 {
3333 return ipmi::responseReqDataLenInvalid();
3334 }
3335
3336 if (transferStatus == setInProgress)
3337 {
3338 phosphor::logging::log<phosphor::logging::level::ERR>(
3339 "boot option set in progress!");
3340 return ipmi::responseResponseError();
3341 }
3342
3343 transferStatus = bootParam;
3344 return ipmi::responseSuccess();
3345 }
3346
3347 if (bootFlag != (uint8_t)BootOptionParameter::bootFlags)
3348 {
3349 phosphor::logging::log<phosphor::logging::level::ERR>(
3350 "Unsupported parameter");
3351 return ipmi::responseResponseError();
3352 }
3353
3354 if (!bootOption)
3355 {
3356 return ipmi::responseReqDataLenInvalid();
3357 }
3358
3359 if (((bootOption.value() & bootSourceMask) >> 2) !=
3360 httpBoot) // not http boot, exit
3361 {
3362 phosphor::logging::log<phosphor::logging::level::ERR>(
3363 "wrong boot option parameter!");
3364 return ipmi::responseParmOutOfRange();
3365 }
3366
3367 try
3368 {
3369 bool permanent = (bootParam & setParmBootFlagsPermanent) ==
3370 setParmBootFlagsPermanent;
3371
3372 // read one time Enabled property
3373 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3374 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3375 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3376 enabledIntf, oneTimeBootEnableProp);
3377 oneTimeEnabled = std::get<bool>(variant);
3378
3379 /*
3380 * Check if the current boot setting is onetime or permanent, if the
3381 * request in the command is otherwise, then set the "Enabled"
3382 * property in one_time object path to 'True' to indicate onetime
3383 * and 'False' to indicate permanent.
3384 *
3385 * Once the onetime/permanent setting is applied, then the bootMode
3386 * and bootSource is updated for the corresponding object.
3387 */
3388 if (permanent == oneTimeEnabled)
3389 {
3390 setDbusProperty(*dbus, service, oneTimePath, enabledIntf,
3391 oneTimeBootEnableProp, !permanent);
3392 }
3393
3394 // set BootSource and BootMode properties
3395 // according to oneTimeEnable or persistent
3396 auto bootObjPath = oneTimePath;
3397 if (oneTimeEnabled == false)
3398 {
3399 bootObjPath = persistentObjPath;
3400 }
3401 std::string bootMode =
3402 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
3403 std::string bootSource = httpBootMode;
3404
3405 service = getService(*dbus, bootModeIntf, bootObjPath);
3406 setDbusProperty(*dbus, service, bootObjPath, bootModeIntf, bootModeProp,
3407 bootMode);
3408
3409 service = getService(*dbus, bootSourceIntf, bootObjPath);
3410 setDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3411 bootSourceProp, bootSource);
3412 }
3413 catch (sdbusplus::exception_t& e)
3414 {
3415 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3416 return ipmi::responseResponseError();
3417 }
3418
3419 return ipmi::responseSuccess();
3420}
3421
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003422using BasicVariantType =
3423 std::variant<std::vector<std::string>, std::vector<uint64_t>, std::string,
3424 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
3425 uint16_t, uint8_t, bool>;
3426using PropertyMapType =
3427 boost::container::flat_map<std::string, BasicVariantType>;
3428static constexpr const std::array<const char*, 1> psuPresenceTypes = {
3429 "xyz.openbmc_project.Configuration.PSUPresence"};
3430int getPSUAddress(ipmi::Context::ptr ctx, uint8_t& bus,
3431 std::vector<uint64_t>& addrTable)
3432{
3433 boost::system::error_code ec;
3434 GetSubTreeType subtree = ctx->bus->yield_method_call<GetSubTreeType>(
3435 ctx->yield, ec, "xyz.openbmc_project.ObjectMapper",
3436 "/xyz/openbmc_project/object_mapper",
3437 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3438 "/xyz/openbmc_project/inventory/system", 3, psuPresenceTypes);
3439 if (ec)
3440 {
3441 phosphor::logging::log<phosphor::logging::level::ERR>(
3442 "Failed to set dbus property to cold redundancy");
3443 return -1;
3444 }
3445 for (const auto& object : subtree)
3446 {
3447 std::string pathName = object.first;
3448 for (const auto& serviceIface : object.second)
3449 {
3450 std::string serviceName = serviceIface.first;
3451
3452 ec.clear();
3453 PropertyMapType propMap =
3454 ctx->bus->yield_method_call<PropertyMapType>(
3455 ctx->yield, ec, serviceName, pathName,
3456 "org.freedesktop.DBus.Properties", "GetAll",
3457 "xyz.openbmc_project.Configuration.PSUPresence");
3458 if (ec)
3459 {
3460 phosphor::logging::log<phosphor::logging::level::ERR>(
3461 "Failed to set dbus property to cold redundancy");
3462 return -1;
3463 }
3464 auto psuBus = std::get_if<uint64_t>(&propMap["Bus"]);
3465 auto psuAddress =
3466 std::get_if<std::vector<uint64_t>>(&propMap["Address"]);
3467
3468 if (psuBus == nullptr || psuAddress == nullptr)
3469 {
3470 std::cerr << "error finding necessary "
3471 "entry in configuration\n";
3472 return -1;
3473 }
3474 bus = static_cast<uint8_t>(*psuBus);
3475 addrTable = *psuAddress;
3476 return 0;
3477 }
3478 }
3479 return -1;
3480}
3481
3482static const constexpr uint8_t addrOffset = 8;
3483static const constexpr uint8_t psuRevision = 0xd9;
3484static const constexpr uint8_t defaultPSUBus = 7;
3485// Second Minor, Primary Minor, Major
3486static const constexpr size_t verLen = 3;
3487ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPSUVersion(ipmi::Context::ptr ctx)
3488{
3489 uint8_t bus = defaultPSUBus;
3490 std::vector<uint64_t> addrTable;
3491 std::vector<uint8_t> result;
3492 if (getPSUAddress(ctx, bus, addrTable))
3493 {
3494 std::cerr << "Failed to get PSU bus and address\n";
3495 return ipmi::responseResponseError();
3496 }
3497
3498 for (const auto& slaveAddr : addrTable)
3499 {
3500 std::vector<uint8_t> writeData = {psuRevision};
3501 std::vector<uint8_t> readBuf(verLen);
3502 uint8_t addr = static_cast<uint8_t>(slaveAddr) + addrOffset;
3503 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
3504
3505 auto retI2C = ipmi::i2cWriteRead(i2cBus, addr, writeData, readBuf);
3506 if (retI2C != ipmi::ccSuccess)
3507 {
3508 for (size_t idx = 0; idx < verLen; idx++)
3509 {
3510 result.emplace_back(0x00);
3511 }
3512 }
3513 else
3514 {
3515 for (const uint8_t& data : readBuf)
3516 {
3517 result.emplace_back(data);
3518 }
3519 }
3520 }
3521
3522 return ipmi::responseSuccess(result);
3523}
3524
srikanta mondal2030d7c2020-05-03 17:25:25 +00003525std::optional<uint8_t> getMultiNodeInfoPresence(ipmi::Context::ptr ctx,
3526 const std::string& name)
3527{
3528 Value dbusValue = 0;
3529 std::string serviceName;
3530
3531 boost::system::error_code ec =
3532 ipmi::getService(ctx, multiNodeIntf, multiNodeObjPath, serviceName);
3533
3534 if (ec)
3535 {
3536 phosphor::logging::log<phosphor::logging::level::ERR>(
3537 "Failed to perform Multinode getService.");
3538 return std::nullopt;
3539 }
3540
3541 ec = ipmi::getDbusProperty(ctx, serviceName, multiNodeObjPath,
3542 multiNodeIntf, name, dbusValue);
3543 if (ec)
3544 {
3545 phosphor::logging::log<phosphor::logging::level::ERR>(
3546 "Failed to perform Multinode get property");
3547 return std::nullopt;
3548 }
3549
3550 auto multiNodeVal = std::get_if<uint8_t>(&dbusValue);
3551 if (!multiNodeVal)
3552 {
3553 phosphor::logging::log<phosphor::logging::level::ERR>(
3554 "getMultiNodeInfoPresence: error to get multinode");
3555 return std::nullopt;
3556 }
3557 return *multiNodeVal;
3558}
3559
3560/** @brief implements OEM get reading command
3561 * @param domain ID
3562 * @param reading Type
3563 * - 00h = platform Power Consumption
3564 * - 01h = inlet Air Temp
3565 * - 02h = icc_TDC from PECI
3566 * @param reserved, write as 0000h
3567 *
3568 * @returns IPMI completion code plus response data
3569 * - response
3570 * - domain ID
3571 * - reading Type
3572 * - 00h = platform Power Consumption
3573 * - 01h = inlet Air Temp
3574 * - 02h = icc_TDC from PECI
3575 * - reading
3576 */
3577ipmi::RspType<uint4_t, // domain ID
3578 uint4_t, // reading Type
3579 uint16_t // reading Value
3580 >
3581 ipmiOEMGetReading(ipmi::Context::ptr ctx, uint4_t domainId,
3582 uint4_t readingType, uint16_t reserved)
3583{
3584 constexpr uint8_t platformPower = 0;
3585 constexpr uint8_t inletAirTemp = 1;
3586 constexpr uint8_t iccTdc = 2;
3587
3588 if ((static_cast<uint8_t>(readingType) > iccTdc) || domainId || reserved)
3589 {
3590 return ipmi::responseInvalidFieldRequest();
3591 }
3592
3593 // This command should run only from multi-node product.
3594 // For all other platforms this command will return invalid.
3595
3596 std::optional<uint8_t> nodeInfo =
3597 getMultiNodeInfoPresence(ctx, "NodePresence");
3598 if (!nodeInfo || !*nodeInfo)
3599 {
3600 return ipmi::responseInvalidCommand();
3601 }
3602
3603 uint16_t oemReadingValue = 0;
3604 if (static_cast<uint8_t>(readingType) == inletAirTemp)
3605 {
3606 double value = 0;
3607 boost::system::error_code ec = ipmi::getDbusProperty(
3608 ctx, "xyz.openbmc_project.HwmonTempSensor",
3609 "/xyz/openbmc_project/sensors/temperature/Inlet_BRD_Temp",
3610 "xyz.openbmc_project.Sensor.Value", "Value", value);
3611 if (ec)
3612 {
3613 phosphor::logging::log<phosphor::logging::level::ERR>(
3614 "Failed to get BMC Get OEM temperature",
3615 phosphor::logging::entry("EXCEPTION=%s", ec.message().c_str()));
3616 return ipmi::responseUnspecifiedError();
3617 }
3618 // Take the Inlet temperature
3619 oemReadingValue = static_cast<uint16_t>(value);
3620 }
3621 else
3622 {
3623 phosphor::logging::log<phosphor::logging::level::ERR>(
3624 "Currently Get OEM Reading support only for Inlet Air Temp");
3625 return ipmi::responseParmOutOfRange();
3626 }
3627 return ipmi::responseSuccess(domainId, readingType, oemReadingValue);
3628}
3629
AppaRao Puli28972062019-11-11 02:04:45 +05303630/** @brief implements the maximum size of
3631 * bridgeable messages used between KCS and
3632 * IPMB interfacesget security mode command.
3633 *
3634 * @returns IPMI completion code with following data
3635 * - KCS Buffer Size (In multiples of four bytes)
3636 * - IPMB Buffer Size (In multiples of four bytes)
3637 **/
3638ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetBufferSize()
3639{
3640 // for now this is hard coded; really this number is dependent on
3641 // the BMC kcs driver as well as the host kcs driver....
3642 // we can't know the latter.
3643 uint8_t kcsMaxBufferSize = 63 / 4;
3644 uint8_t ipmbMaxBufferSize = 128 / 4;
3645
3646 return ipmi::responseSuccess(kcsMaxBufferSize, ipmbMaxBufferSize);
3647}
3648
Jason M. Bills64796042018-10-03 16:51:55 -07003649static void registerOEMFunctions(void)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003650{
3651 phosphor::logging::log<phosphor::logging::level::INFO>(
3652 "Registering OEM commands");
Vernon Mauery98bbf692019-09-16 11:14:59 -07003653 ipmiPrintAndRegister(intel::netFnGeneral,
3654 intel::general::cmdGetChassisIdentifier, NULL,
3655 ipmiOEMGetChassisIdentifier,
3656 PRIVILEGE_USER); // get chassis identifier
3657
3658 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetSystemGUID,
3659 NULL, ipmiOEMSetSystemGUID,
3660 PRIVILEGE_ADMIN); // set system guid
Jason M. Billsb02bf092019-08-15 13:01:56 -07003661
3662 // <Disable BMC System Reset Action>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003663 registerHandler(prioOemBase, intel::netFnGeneral,
3664 intel::general::cmdDisableBMCSystemReset, Privilege::Admin,
3665 ipmiOEMDisableBMCSystemReset);
3666
Jason M. Billsb02bf092019-08-15 13:01:56 -07003667 // <Get BMC Reset Disables>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003668 registerHandler(prioOemBase, intel::netFnGeneral,
3669 intel::general::cmdGetBMCResetDisables, Privilege::Admin,
3670 ipmiOEMGetBMCResetDisables);
Jason M. Billsb02bf092019-08-15 13:01:56 -07003671
Vernon Mauery98bbf692019-09-16 11:14:59 -07003672 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetBIOSID,
3673 NULL, ipmiOEMSetBIOSID, PRIVILEGE_ADMIN);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003674
Chen Yugang7a04f3a2019-10-08 11:12:35 +08003675 registerHandler(prioOemBase, intel::netFnGeneral,
3676 intel::general::cmdGetOEMDeviceInfo, Privilege::User,
3677 ipmiOEMGetDeviceInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003678
Vernon Mauery98bbf692019-09-16 11:14:59 -07003679 ipmiPrintAndRegister(intel::netFnGeneral,
3680 intel::general::cmdGetAICSlotFRUIDSlotPosRecords, NULL,
3681 ipmiOEMGetAICFRU, PRIVILEGE_USER);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303682
Vernon Mauery98bbf692019-09-16 11:14:59 -07003683 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3684 intel::general::cmdSendEmbeddedFWUpdStatus,
3685 Privilege::Operator, ipmiOEMSendEmbeddedFwUpdStatus);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303686
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +05303687 registerHandler(prioOpenBmcBase, intel::netFnApp, intel::app::cmdSlotIpmb,
3688 Privilege::Admin, ipmiOEMSlotIpmb);
3689
Vernon Mauery98bbf692019-09-16 11:14:59 -07003690 ipmiPrintAndRegister(intel::netFnGeneral,
3691 intel::general::cmdSetPowerRestoreDelay, NULL,
3692 ipmiOEMSetPowerRestoreDelay, PRIVILEGE_OPERATOR);
3693
3694 ipmiPrintAndRegister(intel::netFnGeneral,
3695 intel::general::cmdGetPowerRestoreDelay, NULL,
3696 ipmiOEMGetPowerRestoreDelay, PRIVILEGE_USER);
3697
3698 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3699 intel::general::cmdSetOEMUser2Activation,
3700 Privilege::Callback, ipmiOEMSetUser2Activation);
3701
3702 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3703 intel::general::cmdSetSpecialUserPassword,
3704 Privilege::Callback, ipmiOEMSetSpecialUserPassword);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05303705
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003706 // <Get Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003707 registerHandler(prioOemBase, intel::netFnGeneral,
3708 intel::general::cmdGetProcessorErrConfig, Privilege::User,
3709 ipmiOEMGetProcessorErrConfig);
3710
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003711 // <Set Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003712 registerHandler(prioOemBase, intel::netFnGeneral,
3713 intel::general::cmdSetProcessorErrConfig, Privilege::Admin,
3714 ipmiOEMSetProcessorErrConfig);
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003715
Vernon Mauery98bbf692019-09-16 11:14:59 -07003716 ipmiPrintAndRegister(intel::netFnGeneral,
3717 intel::general::cmdSetShutdownPolicy, NULL,
3718 ipmiOEMSetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003719
Vernon Mauery98bbf692019-09-16 11:14:59 -07003720 ipmiPrintAndRegister(intel::netFnGeneral,
3721 intel::general::cmdGetShutdownPolicy, NULL,
3722 ipmiOEMGetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003723
anil kumar appanaf945eee2019-09-25 23:29:11 +00003724 registerHandler(prioOemBase, intel::netFnGeneral,
3725 intel::general::cmdSetFanConfig, Privilege::User,
3726 ipmiOEMSetFanConfig);
James Feist91244a62019-02-19 15:04:54 -08003727
Vernon Mauery98bbf692019-09-16 11:14:59 -07003728 registerHandler(prioOemBase, intel::netFnGeneral,
3729 intel::general::cmdGetFanConfig, Privilege::User,
3730 ipmiOEMGetFanConfig);
James Feist5f957ca2019-03-14 15:33:55 -07003731
Vernon Mauery98bbf692019-09-16 11:14:59 -07003732 registerHandler(prioOemBase, intel::netFnGeneral,
3733 intel::general::cmdGetFanSpeedOffset, Privilege::User,
3734 ipmiOEMGetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003735
Vernon Mauery98bbf692019-09-16 11:14:59 -07003736 registerHandler(prioOemBase, intel::netFnGeneral,
3737 intel::general::cmdSetFanSpeedOffset, Privilege::User,
3738 ipmiOEMSetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003739
Vernon Mauery98bbf692019-09-16 11:14:59 -07003740 registerHandler(prioOemBase, intel::netFnGeneral,
3741 intel::general::cmdSetFscParameter, Privilege::User,
3742 ipmiOEMSetFscParameter);
James Feist5f957ca2019-03-14 15:33:55 -07003743
Vernon Mauery98bbf692019-09-16 11:14:59 -07003744 registerHandler(prioOemBase, intel::netFnGeneral,
3745 intel::general::cmdGetFscParameter, Privilege::User,
3746 ipmiOEMGetFscParameter);
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05303747
Vernon Mauery98bbf692019-09-16 11:14:59 -07003748 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3749 intel::general::cmdReadBaseBoardProductId, Privilege::Admin,
3750 ipmiOEMReadBoardProductId);
Chen Yugang39736d52019-07-12 16:24:33 +08003751
Vernon Mauery98bbf692019-09-16 11:14:59 -07003752 registerHandler(prioOemBase, intel::netFnGeneral,
3753 intel::general::cmdGetNmiStatus, Privilege::User,
3754 ipmiOEMGetNmiSource);
Chen Yugang39736d52019-07-12 16:24:33 +08003755
Vernon Mauery98bbf692019-09-16 11:14:59 -07003756 registerHandler(prioOemBase, intel::netFnGeneral,
3757 intel::general::cmdSetNmiStatus, Privilege::Operator,
3758 ipmiOEMSetNmiSource);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003759
Vernon Mauery98bbf692019-09-16 11:14:59 -07003760 registerHandler(prioOemBase, intel::netFnGeneral,
3761 intel::general::cmdGetEfiBootOptions, Privilege::User,
3762 ipmiOemGetEfiBootOptions);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003763
Vernon Mauery98bbf692019-09-16 11:14:59 -07003764 registerHandler(prioOemBase, intel::netFnGeneral,
3765 intel::general::cmdSetEfiBootOptions, Privilege::Operator,
3766 ipmiOemSetEfiBootOptions);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303767
Vernon Mauery98bbf692019-09-16 11:14:59 -07003768 registerHandler(prioOemBase, intel::netFnGeneral,
3769 intel::general::cmdGetSecurityMode, Privilege::User,
3770 ipmiGetSecurityMode);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303771
Vernon Mauery98bbf692019-09-16 11:14:59 -07003772 registerHandler(prioOemBase, intel::netFnGeneral,
3773 intel::general::cmdSetSecurityMode, Privilege::Admin,
3774 ipmiSetSecurityMode);
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003775
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00003776 registerHandler(prioOemBase, intel::netFnGeneral,
3777 intel::general::cmdGetLEDStatus, Privilege::Admin,
3778 ipmiOEMGetLEDStatus);
Cheng C Yang773703a2019-08-15 09:41:11 +08003779
Vernon Mauery98bbf692019-09-16 11:14:59 -07003780 ipmiPrintAndRegister(ipmi::intel::netFnPlatform,
3781 ipmi::intel::platform::cmdCfgHostSerialPortSpeed, NULL,
3782 ipmiOEMCfgHostSerialPortSpeed, PRIVILEGE_ADMIN);
3783
3784 registerHandler(prioOemBase, intel::netFnGeneral,
3785 intel::general::cmdSetFaultIndication, Privilege::Operator,
3786 ipmiOEMSetFaultIndication);
3787
3788 registerHandler(prioOemBase, intel::netFnGeneral,
3789 intel::general::cmdSetColdRedundancyConfig, Privilege::User,
3790 ipmiOEMSetCRConfig);
3791
3792 registerHandler(prioOemBase, intel::netFnGeneral,
3793 intel::general::cmdGetColdRedundancyConfig, Privilege::User,
3794 ipmiOEMGetCRConfig);
3795
3796 registerHandler(prioOemBase, intel::netFnGeneral,
3797 intel::general::cmdRestoreConfiguration, Privilege::Admin,
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003798 ipmiRestoreConfiguration);
James Feist63efafa2019-07-24 12:39:21 -07003799
Vernon Mauery98bbf692019-09-16 11:14:59 -07003800 registerHandler(prioOemBase, intel::netFnGeneral,
3801 intel::general::cmdSetDimmOffset, Privilege::Operator,
3802 ipmiOEMSetDimmOffset);
James Feist63efafa2019-07-24 12:39:21 -07003803
Vernon Mauery98bbf692019-09-16 11:14:59 -07003804 registerHandler(prioOemBase, intel::netFnGeneral,
3805 intel::general::cmdGetDimmOffset, Privilege::Operator,
3806 ipmiOEMGetDimmOffset);
Chen Yugangca12a7b2019-09-03 18:11:44 +08003807
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003808 registerHandler(prioOemBase, intel::netFnGeneral,
3809 intel::general::cmdGetPSUVersion, Privilege::User,
3810 ipmiOEMGetPSUVersion);
AppaRao Puli28972062019-11-11 02:04:45 +05303811
3812 registerHandler(prioOemBase, intel::netFnGeneral,
3813 intel::general::cmdGetBufferSize, Privilege::User,
3814 ipmiOEMGetBufferSize);
srikanta mondal2030d7c2020-05-03 17:25:25 +00003815
3816 registerHandler(prioOemBase, intel::netFnGeneral,
3817 intel::general::cmdOEMGetReading, Privilege::User,
3818 ipmiOEMGetReading);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003819}
3820
3821} // namespace ipmi