blob: 515a53907bc65a8dcda56cb96e345d6ec3b79af2 [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
Jason M. Bills64796042018-10-03 16:51:55 -070017#include "xyz/openbmc_project/Common/error.hpp"
Kuiying Wang45f04982018-12-26 09:23:08 +080018#include "xyz/openbmc_project/Led/Physical/server.hpp"
Jason M. Bills64796042018-10-03 16:51:55 -070019
Jia, Chunhuicc49b542019-03-20 15:41:07 +080020#include <systemd/sd-journal.h>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080021
Chen Yugang7a04f3a2019-10-08 11:12:35 +080022#include <appcommands.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080023#include <array>
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>
Vernon Mauery4ac799d2019-05-20 15:50:37 -070029#include <filesystem>
Zhikui Rence4e73f2019-12-06 13:59:47 -080030#include <gpiod.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080031#include <iostream>
Jia, Chunhuicc49b542019-03-20 15:41:07 +080032#include <ipmid/api.hpp>
Vernon Mauery5480ef62019-03-20 13:43:11 -070033#include <ipmid/utils.hpp>
James Feist63efafa2019-07-24 12:39:21 -070034#include <nlohmann/json.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080035#include <oemcommands.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080036#include <phosphor-logging/log.hpp>
Chen Yugang7a04f3a2019-10-08 11:12:35 +080037#include <regex>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080038#include <sdbusplus/bus.hpp>
Suryakanth Sekard509eb92018-11-15 17:44:11 +053039#include <sdbusplus/message/types.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080040#include <string>
James Feist91244a62019-02-19 15:04:54 -080041#include <variant>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080042#include <vector>
Chen Yugang97cf96e2019-11-01 08:55:11 +080043#include <xyz/openbmc_project/Chassis/Control/NMISource/server.hpp>
Chen,Yugang4f7e76b2019-08-20 09:28:06 +080044#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
45#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
Cheng C Yang773703a2019-08-15 09:41:11 +080046#include <xyz/openbmc_project/Control/PowerSupplyRedundancy/server.hpp>
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053047#include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp>
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +053048#include <xyz/openbmc_project/Control/Security/SpecialMode/server.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080049
50namespace ipmi
51{
Jason M. Bills64796042018-10-03 16:51:55 -070052static void registerOEMFunctions() __attribute__((constructor));
Vernon Mauery4ac799d2019-05-20 15:50:37 -070053
Jason M. Bills64796042018-10-03 16:51:55 -070054static constexpr size_t maxFRUStringLength = 0x3F;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080055
Suryakanth Sekard509eb92018-11-15 17:44:11 +053056static constexpr auto ethernetIntf =
57 "xyz.openbmc_project.Network.EthernetInterface";
58static constexpr auto networkIPIntf = "xyz.openbmc_project.Network.IP";
59static constexpr auto networkService = "xyz.openbmc_project.Network";
60static constexpr auto networkRoot = "/xyz/openbmc_project/network";
61
Chen Yugang97cf96e2019-11-01 08:55:11 +080062static constexpr const char* oemNmiSourceIntf =
63 "xyz.openbmc_project.Chassis.Control.NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080064static constexpr const char* oemNmiSourceObjPath =
Chen Yugang97cf96e2019-11-01 08:55:11 +080065 "/xyz/openbmc_project/Chassis/Control/NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080066static constexpr const char* oemNmiBmcSourceObjPathProp = "BMCSource";
67static constexpr const char* oemNmiEnabledObjPathProp = "Enabled";
68
James Feist63efafa2019-07-24 12:39:21 -070069static constexpr const char* dimmOffsetFile = "/var/lib/ipmi/ipmi_dimms.json";
70
Chen Yugang39736d52019-07-12 16:24:33 +080071enum class NmiSource : uint8_t
72{
73 none = 0,
Chen Yugang97cf96e2019-11-01 08:55:11 +080074 frontPanelButton = 1,
75 watchdog = 2,
76 chassisCmd = 3,
77 memoryError = 4,
78 pciBusError = 5,
79 pch = 6,
80 chipset = 7,
Chen Yugang39736d52019-07-12 16:24:33 +080081};
82
Suryakanth Sekar822b0b42019-11-15 18:32:53 +053083enum class SpecialUserIndex : uint8_t
84{
85 rootUser = 0,
86 atScaleDebugUser = 1
87};
88
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053089static constexpr const char* restricionModeService =
90 "xyz.openbmc_project.RestrictionMode.Manager";
91static constexpr const char* restricionModeBasePath =
92 "/xyz/openbmc_project/control/security/restriction_mode";
93static constexpr const char* restricionModeIntf =
94 "xyz.openbmc_project.Control.Security.RestrictionMode";
95static constexpr const char* restricionModeProperty = "RestrictionMode";
96
97static constexpr const char* specialModeService =
98 "xyz.openbmc_project.SpecialMode";
99static constexpr const char* specialModeBasePath =
Richard Marian Thomaiyara7b74282019-09-22 21:53:14 +0530100 "/xyz/openbmc_project/security/special_mode";
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +0530101static constexpr const char* specialModeIntf =
102 "xyz.openbmc_project.Security.SpecialMode";
103static constexpr const char* specialModeProperty = "SpecialMode";
104
105static constexpr const char* dBusPropertyIntf =
106 "org.freedesktop.DBus.Properties";
107static constexpr const char* dBusPropertyGetMethod = "Get";
108static constexpr const char* dBusPropertySetMethod = "Set";
109
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800110// return code: 0 successful
111int8_t getChassisSerialNumber(sdbusplus::bus::bus& bus, std::string& serial)
112{
113 std::string objpath = "/xyz/openbmc_project/FruDevice";
114 std::string intf = "xyz.openbmc_project.FruDeviceManager";
115 std::string service = getService(bus, intf, objpath);
116 ObjectValueTree valueTree = getManagedObjects(bus, service, "/");
117 if (valueTree.empty())
118 {
119 phosphor::logging::log<phosphor::logging::level::ERR>(
120 "No object implements interface",
121 phosphor::logging::entry("INTF=%s", intf.c_str()));
122 return -1;
123 }
124
Jason M. Bills64796042018-10-03 16:51:55 -0700125 for (const auto& item : valueTree)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800126 {
127 auto interface = item.second.find("xyz.openbmc_project.FruDevice");
128 if (interface == item.second.end())
129 {
130 continue;
131 }
132
133 auto property = interface->second.find("CHASSIS_SERIAL_NUMBER");
134 if (property == interface->second.end())
135 {
136 continue;
137 }
138
139 try
140 {
141 Value variant = property->second;
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700142 std::string& result = std::get<std::string>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700143 if (result.size() > maxFRUStringLength)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800144 {
145 phosphor::logging::log<phosphor::logging::level::ERR>(
146 "FRU serial number exceed maximum length");
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800147 return -1;
148 }
Jason M. Bills64796042018-10-03 16:51:55 -0700149 serial = result;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800150 return 0;
151 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700152 catch (std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800153 {
Jason M. Bills64796042018-10-03 16:51:55 -0700154 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800155 return -1;
156 }
157 }
158 return -1;
159}
Jason M. Bills64796042018-10-03 16:51:55 -0700160
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800161// Returns the Chassis Identifier (serial #)
162ipmi_ret_t ipmiOEMGetChassisIdentifier(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
163 ipmi_request_t request,
164 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700165 ipmi_data_len_t dataLen,
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800166 ipmi_context_t context)
167{
168 std::string serial;
Jason M. Bills64796042018-10-03 16:51:55 -0700169 if (*dataLen != 0) // invalid request if there are extra parameters
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800170 {
Jason M. Bills64796042018-10-03 16:51:55 -0700171 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800172 return IPMI_CC_REQ_DATA_LEN_INVALID;
173 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700174 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
175 if (getChassisSerialNumber(*dbus, serial) == 0)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800176 {
Jason M. Bills64796042018-10-03 16:51:55 -0700177 *dataLen = serial.size(); // length will never exceed response length
178 // as it is checked in getChassisSerialNumber
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800179 char* resp = static_cast<char*>(response);
Jason M. Bills64796042018-10-03 16:51:55 -0700180 serial.copy(resp, *dataLen);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800181 return IPMI_CC_OK;
182 }
Jason M. Bills64796042018-10-03 16:51:55 -0700183 *dataLen = 0;
184 return IPMI_CC_RESPONSE_ERROR;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800185}
186
187ipmi_ret_t ipmiOEMSetSystemGUID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
188 ipmi_request_t request,
189 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700190 ipmi_data_len_t dataLen, ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800191{
192 static constexpr size_t safeBufferLength = 50;
193 char buf[safeBufferLength] = {0};
194 GUIDData* Data = reinterpret_cast<GUIDData*>(request);
195
Jason M. Bills64796042018-10-03 16:51:55 -0700196 if (*dataLen != sizeof(GUIDData)) // 16bytes
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800197 {
Jason M. Bills64796042018-10-03 16:51:55 -0700198 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800199 return IPMI_CC_REQ_DATA_LEN_INVALID;
200 }
201
Jason M. Bills64796042018-10-03 16:51:55 -0700202 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800203
204 snprintf(
205 buf, safeBufferLength,
206 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
207 Data->timeLow4, Data->timeLow3, Data->timeLow2, Data->timeLow1,
208 Data->timeMid2, Data->timeMid1, Data->timeHigh2, Data->timeHigh1,
209 Data->clock2, Data->clock1, Data->node6, Data->node5, Data->node4,
210 Data->node3, Data->node2, Data->node1);
211 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
212 std::string guid = buf;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800213
214 std::string objpath = "/xyz/openbmc_project/control/host0/systemGUID";
215 std::string intf = "xyz.openbmc_project.Common.UUID";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700216 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
217 std::string service = getService(*dbus, intf, objpath);
218 setDbusProperty(*dbus, service, objpath, intf, "UUID", guid);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800219 return IPMI_CC_OK;
220}
221
Jason M. Billsb02bf092019-08-15 13:01:56 -0700222ipmi::RspType<> ipmiOEMDisableBMCSystemReset(bool disableResetOnSMI,
223 uint7_t reserved1)
224{
225 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
226
227 try
228 {
229 auto service =
230 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
231 ipmi::setDbusProperty(*busp, service, bmcResetDisablesPath,
232 bmcResetDisablesIntf, "ResetOnSMI",
233 !disableResetOnSMI);
234 }
235 catch (std::exception& e)
236 {
237 phosphor::logging::log<phosphor::logging::level::ERR>(
238 "Failed to set BMC reset disables",
239 phosphor::logging::entry("EXCEPTION=%s", e.what()));
240 return ipmi::responseUnspecifiedError();
241 }
242
243 return ipmi::responseSuccess();
244}
245
246ipmi::RspType<bool, // disableResetOnSMI
247 uint7_t // reserved
248 >
249 ipmiOEMGetBMCResetDisables()
250{
251 bool disableResetOnSMI = true;
252
253 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
254 try
255 {
256 auto service =
257 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
258 Value variant =
259 ipmi::getDbusProperty(*busp, service, bmcResetDisablesPath,
260 bmcResetDisablesIntf, "ResetOnSMI");
261 disableResetOnSMI = !std::get<bool>(variant);
262 }
263 catch (std::exception& e)
264 {
265 phosphor::logging::log<phosphor::logging::level::ERR>(
266 "Failed to get BMC reset disables",
267 phosphor::logging::entry("EXCEPTION=%s", e.what()));
268 return ipmi::responseUnspecifiedError();
269 }
270
271 return ipmi::responseSuccess(disableResetOnSMI, 0);
272}
273
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800274ipmi_ret_t ipmiOEMSetBIOSID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
275 ipmi_request_t request, ipmi_response_t response,
276 ipmi_data_len_t dataLen, ipmi_context_t context)
277{
278 DeviceInfo* data = reinterpret_cast<DeviceInfo*>(request);
279
Jason M. Bills64796042018-10-03 16:51:55 -0700280 if ((*dataLen < 2) || (*dataLen != (1 + data->biosIDLength)))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800281 {
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800282 *dataLen = 0;
283 return IPMI_CC_REQ_DATA_LEN_INVALID;
284 }
Jason M. Bills64796042018-10-03 16:51:55 -0700285 std::string idString((char*)data->biosId, data->biosIDLength);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800286
Vernon Mauery15419dd2019-05-24 09:40:30 -0700287 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Chalapathi899bfd12020-04-15 15:07:02 +0000288 std::string service = getService(*dbus, biosVersionIntf, biosActiveObjPath);
289 setDbusProperty(*dbus, service, biosActiveObjPath, biosVersionIntf,
Yong Li2742b852019-12-16 14:55:11 +0800290 biosVersionProp, idString);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800291 uint8_t* bytesWritten = static_cast<uint8_t*>(response);
292 *bytesWritten =
Jason M. Bills64796042018-10-03 16:51:55 -0700293 data->biosIDLength; // how many bytes are written into storage
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800294 *dataLen = 1;
295 return IPMI_CC_OK;
296}
297
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530298bool getSwVerInfo(ipmi::Context::ptr ctx, uint8_t& bmcMajor, uint8_t& bmcMinor,
299 uint8_t& meMajor, uint8_t& meMinor)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800300{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800301 // step 1 : get BMC Major and Minor numbers from its DBUS property
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530302 std::string bmcVersion;
303 if (getActiveSoftwareVersionInfo(ctx, versionPurposeBMC, bmcVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800304 {
305 return false;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800306 }
307
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530308 std::optional<MetaRevision> rev = convertIntelVersion(bmcVersion);
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800309 if (rev.has_value())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800310 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800311 MetaRevision revision = rev.value();
312 bmcMajor = revision.major;
313
314 revision.minor = (revision.minor > 99 ? 99 : revision.minor);
315 bmcMinor = revision.minor % 10 + (revision.minor / 10) * 16;
316 }
317
318 // step 2 : get ME Major and Minor numbers from its DBUS property
AppaRao Puli32825a22020-01-17 15:52:41 +0530319 std::string meVersion;
320 if (getActiveSoftwareVersionInfo(ctx, versionPurposeME, meVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800321 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800322 return false;
323 }
AppaRao Puli32825a22020-01-17 15:52:41 +0530324 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?).(\\d+?).(\\d+?)");
325 constexpr size_t matchedPhosphor = 6;
326 std::smatch results;
327 if (std::regex_match(meVersion, results, pattern1))
328 {
329 if (results.size() == matchedPhosphor)
330 {
331 meMajor = static_cast<uint8_t>(std::stoi(results[1]));
332 meMinor = static_cast<uint8_t>(std::stoi(results[2]));
333 }
334 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800335 return true;
336}
337
338ipmi::RspType<
339 std::variant<std::string,
340 std::tuple<uint8_t, std::array<uint8_t, 2>,
341 std::array<uint8_t, 2>, std::array<uint8_t, 2>,
342 std::array<uint8_t, 2>, std::array<uint8_t, 2>>,
343 std::tuple<uint8_t, std::array<uint8_t, 2>>>>
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530344 ipmiOEMGetDeviceInfo(ipmi::Context::ptr ctx, uint8_t entityType,
345 std::optional<uint8_t> countToRead,
AppaRao Pulid46cb422020-01-21 18:40:21 +0530346 std::optional<uint8_t> offset)
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800347{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800348 if (entityType > static_cast<uint8_t>(OEMDevEntityType::sdrVer))
349 {
350 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800351 }
352
353 // handle OEM command items
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800354 switch (OEMDevEntityType(entityType))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800355 {
356 case OEMDevEntityType::biosId:
357 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530358 // Byte 2&3, Only used with selecting BIOS
359 if (!countToRead || !offset)
360 {
361 return ipmi::responseReqDataLenInvalid();
362 }
363
Vernon Mauery15419dd2019-05-24 09:40:30 -0700364 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800365 std::string service =
Chalapathi899bfd12020-04-15 15:07:02 +0000366 getService(*dbus, biosVersionIntf, biosActiveObjPath);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800367 try
368 {
Yong Li2742b852019-12-16 14:55:11 +0800369 Value variant =
Chalapathi899bfd12020-04-15 15:07:02 +0000370 getDbusProperty(*dbus, service, biosActiveObjPath,
Yong Li2742b852019-12-16 14:55:11 +0800371 biosVersionIntf, biosVersionProp);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700372 std::string& idString = std::get<std::string>(variant);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530373 if (*offset >= idString.size())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800374 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800375 return ipmi::responseParmOutOfRange();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800376 }
Jason M. Bills64796042018-10-03 16:51:55 -0700377 size_t length = 0;
AppaRao Pulid46cb422020-01-21 18:40:21 +0530378 if (*countToRead > (idString.size() - *offset))
Jason M. Bills64796042018-10-03 16:51:55 -0700379 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530380 length = idString.size() - *offset;
Jason M. Bills64796042018-10-03 16:51:55 -0700381 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800382 else
383 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530384 length = *countToRead;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800385 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800386
387 std::string readBuf = {0};
388 readBuf.resize(length);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530389 std::copy_n(idString.begin() + *offset, length,
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800390 (readBuf.begin()));
391 return ipmi::responseSuccess(readBuf);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800392 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700393 catch (std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800394 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800395 return ipmi::responseUnspecifiedError();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800396 }
397 }
398 break;
399
400 case OEMDevEntityType::devVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800401 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530402 // Byte 2&3, Only used with selecting BIOS
403 if (countToRead || offset)
404 {
405 return ipmi::responseReqDataLenInvalid();
406 }
407
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800408 constexpr const size_t verLen = 2;
409 constexpr const size_t verTotalLen = 10;
410 std::array<uint8_t, verLen> bmcBuf = {0xff, 0xff};
411 std::array<uint8_t, verLen> hsc0Buf = {0xff, 0xff};
412 std::array<uint8_t, verLen> hsc1Buf = {0xff, 0xff};
413 std::array<uint8_t, verLen> meBuf = {0xff, 0xff};
414 std::array<uint8_t, verLen> hsc2Buf = {0xff, 0xff};
415 // data0/1: BMC version number; data6/7: ME version number
416 // the others: HSC0/1/2 version number, not avaible.
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530417 if (!getSwVerInfo(ctx, bmcBuf[0], bmcBuf[1], meBuf[0], meBuf[1]))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800418 {
419 return ipmi::responseUnspecifiedError();
420 }
421 return ipmi::responseSuccess(
422 std::tuple<
423 uint8_t, std::array<uint8_t, verLen>,
424 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>,
425 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>>{
426 verTotalLen, bmcBuf, hsc0Buf, hsc1Buf, meBuf, hsc2Buf});
427 }
428 break;
429
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800430 case OEMDevEntityType::sdrVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800431 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530432 // Byte 2&3, Only used with selecting BIOS
433 if (countToRead || offset)
434 {
435 return ipmi::responseReqDataLenInvalid();
436 }
437
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800438 constexpr const size_t sdrLen = 2;
439 std::array<uint8_t, sdrLen> readBuf = {0x01, 0x0};
440 return ipmi::responseSuccess(
441 std::tuple<uint8_t, std::array<uint8_t, sdrLen>>{sdrLen,
442 readBuf});
443 }
444 break;
445
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800446 default:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800447 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800448 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800449}
450
451ipmi_ret_t ipmiOEMGetAICFRU(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
452 ipmi_request_t request, ipmi_response_t response,
453 ipmi_data_len_t dataLen, ipmi_context_t context)
454{
455 if (*dataLen != 0)
456 {
Jason M. Bills64796042018-10-03 16:51:55 -0700457 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800458 return IPMI_CC_REQ_DATA_LEN_INVALID;
459 }
460
461 *dataLen = 1;
462 uint8_t* res = reinterpret_cast<uint8_t*>(response);
463 // temporary fix. We don't support AIC FRU now. Just tell BIOS that no
464 // AIC is available so that BIOS will not timeout repeatly which leads to
465 // slow booting.
466 *res = 0; // Byte1=Count of SlotPosition/FruID records.
467 return IPMI_CC_OK;
468}
469
Jason M. Bills64796042018-10-03 16:51:55 -0700470ipmi_ret_t ipmiOEMGetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
471 ipmi_request_t request,
472 ipmi_response_t response,
473 ipmi_data_len_t dataLen,
474 ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800475{
Jason M. Bills64796042018-10-03 16:51:55 -0700476 GetPowerRestoreDelayRes* resp =
477 reinterpret_cast<GetPowerRestoreDelayRes*>(response);
478
479 if (*dataLen != 0)
480 {
481 *dataLen = 0;
482 return IPMI_CC_REQ_DATA_LEN_INVALID;
483 }
484
Vernon Mauery15419dd2019-05-24 09:40:30 -0700485 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700486 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700487 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
Jason M. Bills64796042018-10-03 16:51:55 -0700488 Value variant =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700489 getDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700490 powerRestoreDelayIntf, powerRestoreDelayProp);
491
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700492 uint16_t delay = std::get<uint16_t>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700493 resp->byteLSB = delay;
494 resp->byteMSB = delay >> 8;
495
496 *dataLen = sizeof(GetPowerRestoreDelayRes);
497
498 return IPMI_CC_OK;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800499}
500
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800501static uint8_t bcdToDec(uint8_t val)
502{
503 return ((val / 16 * 10) + (val % 16));
504}
505
506// Allows an update utility or system BIOS to send the status of an embedded
507// firmware update attempt to the BMC. After received, BMC will create a logging
508// record.
509ipmi::RspType<> ipmiOEMSendEmbeddedFwUpdStatus(uint8_t status, uint8_t target,
510 uint8_t majorRevision,
511 uint8_t minorRevision,
512 uint32_t auxInfo)
513{
514 std::string firmware;
Jason M. Billsdc249272019-04-03 09:58:40 -0700515 int instance = (target & targetInstanceMask) >> targetInstanceShift;
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800516 target = (target & selEvtTargetMask) >> selEvtTargetShift;
517
518 /* make sure the status is 0, 1, or 2 as per the spec */
519 if (status > 2)
520 {
521 return ipmi::response(ipmi::ccInvalidFieldRequest);
522 }
Jason M. Billsdc249272019-04-03 09:58:40 -0700523 /* make sure the target is 0, 1, 2, or 4 as per the spec */
524 if (target > 4 || target == 3)
525 {
526 return ipmi::response(ipmi::ccInvalidFieldRequest);
527 }
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800528 /*orignal OEM command is to record OEM SEL.
529 But openbmc does not support OEM SEL, so we redirect it to redfish event
530 logging. */
531 std::string buildInfo;
532 std::string action;
533 switch (FWUpdateTarget(target))
534 {
535 case FWUpdateTarget::targetBMC:
536 firmware = "BMC";
Jason M. Billsdc249272019-04-03 09:58:40 -0700537 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800538 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
539 " BuildID: " + std::to_string(auxInfo);
540 buildInfo += std::to_string(auxInfo);
541 break;
542 case FWUpdateTarget::targetBIOS:
543 firmware = "BIOS";
544 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700545 "major: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800546 std::to_string(bcdToDec(majorRevision)) + // BCD encoded
547 " minor: " +
548 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
549 " ReleaseNumber: " + // ASCII encoded
550 std::to_string(static_cast<uint8_t>(auxInfo >> 0) - '0') +
551 std::to_string(static_cast<uint8_t>(auxInfo >> 8) - '0') +
552 std::to_string(static_cast<uint8_t>(auxInfo >> 16) - '0') +
553 std::to_string(static_cast<uint8_t>(auxInfo >> 24) - '0');
554 break;
555 case FWUpdateTarget::targetME:
556 firmware = "ME";
557 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700558 "major: " + std::to_string(majorRevision) + " minor1: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800559 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
560 " minor2: " +
561 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 0))) +
562 " build1: " +
563 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 8))) +
564 " build2: " +
565 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 16)));
566 break;
567 case FWUpdateTarget::targetOEMEWS:
568 firmware = "EWS";
Jason M. Billsdc249272019-04-03 09:58:40 -0700569 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800570 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
571 " BuildID: " + std::to_string(auxInfo);
572 break;
573 }
574
Jason M. Billsdc249272019-04-03 09:58:40 -0700575 static const std::string openBMCMessageRegistryVersion("0.1");
576 std::string redfishMsgID = "OpenBMC." + openBMCMessageRegistryVersion;
577
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800578 switch (status)
579 {
580 case 0x0:
581 action = "update started";
Jason M. Billsdc249272019-04-03 09:58:40 -0700582 redfishMsgID += ".FirmwareUpdateStarted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800583 break;
584 case 0x1:
585 action = "update completed successfully";
Jason M. Billsdc249272019-04-03 09:58:40 -0700586 redfishMsgID += ".FirmwareUpdateCompleted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800587 break;
588 case 0x2:
589 action = "update failure";
Jason M. Billsdc249272019-04-03 09:58:40 -0700590 redfishMsgID += ".FirmwareUpdateFailed";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800591 break;
592 default:
593 action = "unknown";
594 break;
595 }
596
Jason M. Billsdc249272019-04-03 09:58:40 -0700597 std::string firmwareInstanceStr =
598 firmware + " instance: " + std::to_string(instance);
599 std::string message("[firmware update] " + firmwareInstanceStr +
600 " status: <" + action + "> " + buildInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800601
602 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", LOG_INFO,
Jason M. Billsdc249272019-04-03 09:58:40 -0700603 "REDFISH_MESSAGE_ID=%s", redfishMsgID.c_str(),
604 "REDFISH_MESSAGE_ARGS=%s,%s", firmwareInstanceStr.c_str(),
605 buildInfo.c_str(), NULL);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800606 return ipmi::responseSuccess();
607}
608
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +0530609ipmi::RspType<uint8_t, std::vector<uint8_t>>
610 ipmiOEMSlotIpmb(ipmi::Context::ptr ctx, uint6_t reserved1,
611 uint2_t slotNumber, uint3_t baseBoardSlotNum,
612 uint3_t riserSlotNum, uint2_t reserved2, uint8_t slaveAddr,
613 uint8_t netFn, uint8_t cmd,
614 std::optional<std::vector<uint8_t>> writeData)
615{
616 if (reserved1 || reserved2)
617 {
618 return ipmi::responseInvalidFieldRequest();
619 }
620
621 boost::system::error_code ec;
622 using ipmbResponse = std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t,
623 std::vector<uint8_t>>;
624 ipmbResponse res = ctx->bus->yield_method_call<ipmbResponse>(
625 ctx->yield, ec, "xyz.openbmc_project.Ipmi.Channel.Ipmb",
626 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
627 "SlotIpmbRequest", static_cast<uint8_t>(slotNumber),
628 static_cast<uint8_t>(baseBoardSlotNum), slaveAddr, netFn, cmd,
629 *writeData);
630 if (ec)
631 {
632 phosphor::logging::log<phosphor::logging::level::ERR>(
633 "Failed to call dbus method SlotIpmbRequest");
634 return ipmi::responseUnspecifiedError();
635 }
636
637 std::vector<uint8_t> dataReceived(0);
638 int status = -1;
639 uint8_t resNetFn = 0, resLun = 0, resCmd = 0, cc = 0;
640
641 std::tie(status, resNetFn, resLun, resCmd, cc, dataReceived) = res;
642
643 if (status)
644 {
645 phosphor::logging::log<phosphor::logging::level::ERR>(
646 "Failed to get response from SlotIpmbRequest");
647 return ipmi::responseResponseError();
648 }
649 return ipmi::responseSuccess(cc, dataReceived);
650}
651
Jason M. Bills64796042018-10-03 16:51:55 -0700652ipmi_ret_t ipmiOEMSetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
653 ipmi_request_t request,
654 ipmi_response_t response,
655 ipmi_data_len_t dataLen,
656 ipmi_context_t context)
657{
658 SetPowerRestoreDelayReq* data =
659 reinterpret_cast<SetPowerRestoreDelayReq*>(request);
660 uint16_t delay = 0;
661
662 if (*dataLen != sizeof(SetPowerRestoreDelayReq))
663 {
664 *dataLen = 0;
665 return IPMI_CC_REQ_DATA_LEN_INVALID;
666 }
667 delay = data->byteMSB;
668 delay = (delay << 8) | data->byteLSB;
Vernon Mauery15419dd2019-05-24 09:40:30 -0700669 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700670 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700671 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
672 setDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700673 powerRestoreDelayIntf, powerRestoreDelayProp, delay);
674 *dataLen = 0;
675
676 return IPMI_CC_OK;
677}
678
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700679static bool cpuPresent(const std::string& cpuName)
Jason M. Bills64796042018-10-03 16:51:55 -0700680{
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700681 static constexpr const char* cpuPresencePathPrefix =
682 "/xyz/openbmc_project/inventory/system/chassis/motherboard/";
683 static constexpr const char* cpuPresenceIntf =
684 "xyz.openbmc_project.Inventory.Item";
685 std::string cpuPresencePath = cpuPresencePathPrefix + cpuName;
686 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
687 try
Jason M. Bills64796042018-10-03 16:51:55 -0700688 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700689 auto service =
690 ipmi::getService(*busp, cpuPresenceIntf, cpuPresencePath);
691
692 ipmi::Value result = ipmi::getDbusProperty(
693 *busp, service, cpuPresencePath, cpuPresenceIntf, "Present");
694 return std::get<bool>(result);
695 }
696 catch (const std::exception& e)
697 {
698 phosphor::logging::log<phosphor::logging::level::INFO>(
699 "Cannot find processor presence",
700 phosphor::logging::entry("NAME=%s", cpuName.c_str()));
701 return false;
702 }
703}
704
705ipmi::RspType<bool, // CATERR Reset Enabled
706 bool, // ERR2 Reset Enabled
707 uint6_t, // reserved
708 uint8_t, // reserved, returns 0x3F
709 uint6_t, // CPU1 CATERR Count
710 uint2_t, // CPU1 Status
711 uint6_t, // CPU2 CATERR Count
712 uint2_t, // CPU2 Status
713 uint6_t, // CPU3 CATERR Count
714 uint2_t, // CPU3 Status
715 uint6_t, // CPU4 CATERR Count
716 uint2_t, // CPU4 Status
717 uint8_t // Crashdump Count
718 >
719 ipmiOEMGetProcessorErrConfig()
720{
721 bool resetOnCATERR = false;
722 bool resetOnERR2 = false;
723 uint6_t cpu1CATERRCount = 0;
724 uint6_t cpu2CATERRCount = 0;
725 uint6_t cpu3CATERRCount = 0;
726 uint6_t cpu4CATERRCount = 0;
727 uint8_t crashdumpCount = 0;
728 uint2_t cpu1Status =
729 cpuPresent("CPU_1") ? CPUStatus::enabled : CPUStatus::notPresent;
730 uint2_t cpu2Status =
731 cpuPresent("CPU_2") ? CPUStatus::enabled : CPUStatus::notPresent;
732 uint2_t cpu3Status =
733 cpuPresent("CPU_3") ? CPUStatus::enabled : CPUStatus::notPresent;
734 uint2_t cpu4Status =
735 cpuPresent("CPU_4") ? CPUStatus::enabled : CPUStatus::notPresent;
736
737 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
738 try
739 {
740 auto service = ipmi::getService(*busp, processorErrConfigIntf,
741 processorErrConfigObjPath);
742
743 ipmi::PropertyMap result = ipmi::getAllDbusProperties(
744 *busp, service, processorErrConfigObjPath, processorErrConfigIntf);
745 resetOnCATERR = std::get<bool>(result.at("ResetOnCATERR"));
746 resetOnERR2 = std::get<bool>(result.at("ResetOnERR2"));
747 cpu1CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU1"));
748 cpu2CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU2"));
749 cpu3CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU3"));
750 cpu4CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU4"));
751 crashdumpCount = std::get<uint8_t>(result.at("CrashdumpCount"));
752 }
753 catch (const std::exception& e)
754 {
755 phosphor::logging::log<phosphor::logging::level::ERR>(
756 "Failed to fetch processor error config",
757 phosphor::logging::entry("ERROR=%s", e.what()));
758 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700759 }
760
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700761 return ipmi::responseSuccess(resetOnCATERR, resetOnERR2, 0, 0x3F,
762 cpu1CATERRCount, cpu1Status, cpu2CATERRCount,
763 cpu2Status, cpu3CATERRCount, cpu3Status,
764 cpu4CATERRCount, cpu4Status, crashdumpCount);
765}
Jason M. Bills64796042018-10-03 16:51:55 -0700766
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700767ipmi::RspType<> ipmiOEMSetProcessorErrConfig(
768 bool resetOnCATERR, bool resetOnERR2, uint6_t reserved1, uint8_t reserved2,
769 std::optional<bool> clearCPUErrorCount,
770 std::optional<bool> clearCrashdumpCount, std::optional<uint6_t> reserved3)
771{
772 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700773
774 try
775 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700776 auto service = ipmi::getService(*busp, processorErrConfigIntf,
777 processorErrConfigObjPath);
778 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
779 processorErrConfigIntf, "ResetOnCATERR",
780 resetOnCATERR);
781 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
782 processorErrConfigIntf, "ResetOnERR2",
783 resetOnERR2);
784 if (clearCPUErrorCount.value_or(false))
785 {
786 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700787 processorErrConfigIntf, "ErrorCountCPU1",
788 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700789 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700790 processorErrConfigIntf, "ErrorCountCPU2",
791 static_cast<uint8_t>(0));
792 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
793 processorErrConfigIntf, "ErrorCountCPU3",
794 static_cast<uint8_t>(0));
795 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
796 processorErrConfigIntf, "ErrorCountCPU4",
797 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700798 }
799 if (clearCrashdumpCount.value_or(false))
800 {
801 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700802 processorErrConfigIntf, "CrashdumpCount",
803 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700804 }
Jason M. Bills64796042018-10-03 16:51:55 -0700805 }
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700806 catch (std::exception& e)
Jason M. Bills64796042018-10-03 16:51:55 -0700807 {
Kuiying Wangbc546672018-11-23 15:41:05 +0800808 phosphor::logging::log<phosphor::logging::level::ERR>(
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700809 "Failed to set processor error config",
810 phosphor::logging::entry("EXCEPTION=%s", e.what()));
811 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700812 }
813
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700814 return ipmi::responseSuccess();
Jason M. Bills64796042018-10-03 16:51:55 -0700815}
816
Yong Li703922d2018-11-06 13:25:31 +0800817ipmi_ret_t ipmiOEMGetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
818 ipmi_request_t request,
819 ipmi_response_t response,
820 ipmi_data_len_t dataLen,
821 ipmi_context_t context)
822{
823 GetOEMShutdownPolicyRes* resp =
824 reinterpret_cast<GetOEMShutdownPolicyRes*>(response);
825
826 if (*dataLen != 0)
827 {
828 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wang45f04982018-12-26 09:23:08 +0800829 "oem_get_shutdown_policy: invalid input len!");
Yong Li703922d2018-11-06 13:25:31 +0800830 *dataLen = 0;
831 return IPMI_CC_REQ_DATA_LEN_INVALID;
832 }
833
834 *dataLen = 0;
835
836 try
837 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700838 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800839 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700840 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
841 Value variant = getDbusProperty(
842 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
843 oemShutdownPolicyObjPathProp);
Yong Li0669d192019-05-06 14:01:46 +0800844
845 if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
846 convertPolicyFromString(std::get<std::string>(variant)) ==
847 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
848 NoShutdownOnOCOT)
849 {
850 resp->policy = 0;
851 }
852 else if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
853 convertPolicyFromString(std::get<std::string>(variant)) ==
854 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
855 Policy::ShutdownOnOCOT)
856 {
857 resp->policy = 1;
858 }
859 else
860 {
861 phosphor::logging::log<phosphor::logging::level::ERR>(
862 "oem_set_shutdown_policy: invalid property!",
863 phosphor::logging::entry(
864 "PROP=%s", std::get<std::string>(variant).c_str()));
865 return IPMI_CC_UNSPECIFIED_ERROR;
866 }
Yong Li703922d2018-11-06 13:25:31 +0800867 // TODO needs to check if it is multi-node products,
868 // policy is only supported on node 3/4
869 resp->policySupport = shutdownPolicySupported;
870 }
871 catch (sdbusplus::exception_t& e)
872 {
873 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
874 return IPMI_CC_UNSPECIFIED_ERROR;
875 }
876
877 *dataLen = sizeof(GetOEMShutdownPolicyRes);
878 return IPMI_CC_OK;
879}
880
881ipmi_ret_t ipmiOEMSetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
882 ipmi_request_t request,
883 ipmi_response_t response,
884 ipmi_data_len_t dataLen,
885 ipmi_context_t context)
886{
887 uint8_t* req = reinterpret_cast<uint8_t*>(request);
Yong Li0669d192019-05-06 14:01:46 +0800888 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy policy =
889 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
890 NoShutdownOnOCOT;
Yong Li703922d2018-11-06 13:25:31 +0800891
892 // TODO needs to check if it is multi-node products,
893 // policy is only supported on node 3/4
894 if (*dataLen != 1)
895 {
896 phosphor::logging::log<phosphor::logging::level::ERR>(
897 "oem_set_shutdown_policy: invalid input len!");
898 *dataLen = 0;
899 return IPMI_CC_REQ_DATA_LEN_INVALID;
900 }
901
902 *dataLen = 0;
903 if ((*req != noShutdownOnOCOT) && (*req != shutdownOnOCOT))
904 {
905 phosphor::logging::log<phosphor::logging::level::ERR>(
906 "oem_set_shutdown_policy: invalid input!");
907 return IPMI_CC_INVALID_FIELD_REQUEST;
908 }
909
Yong Li0669d192019-05-06 14:01:46 +0800910 if (*req == noShutdownOnOCOT)
911 {
912 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
913 Policy::NoShutdownOnOCOT;
914 }
915 else
916 {
917 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
918 Policy::ShutdownOnOCOT;
919 }
920
Yong Li703922d2018-11-06 13:25:31 +0800921 try
922 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700923 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800924 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700925 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
Yong Li0669d192019-05-06 14:01:46 +0800926 setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -0700927 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
Yong Li0669d192019-05-06 14:01:46 +0800928 oemShutdownPolicyObjPathProp,
929 sdbusplus::com::intel::Control::server::convertForMessage(policy));
Yong Li703922d2018-11-06 13:25:31 +0800930 }
931 catch (sdbusplus::exception_t& e)
932 {
933 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
934 return IPMI_CC_UNSPECIFIED_ERROR;
935 }
936
937 return IPMI_CC_OK;
938}
939
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530940/** @brief implementation for check the DHCP or not in IPv4
941 * @param[in] Channel - Channel number
942 * @returns true or false.
943 */
944static bool isDHCPEnabled(uint8_t Channel)
945{
946 try
947 {
948 auto ethdevice = getChannelName(Channel);
949 if (ethdevice.empty())
950 {
951 return false;
952 }
953 auto ethIP = ethdevice + "/ipv4";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700954 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530955 auto ethernetObj =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700956 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
957 auto value = getDbusProperty(*dbus, networkService, ethernetObj.first,
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530958 networkIPIntf, "Origin");
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700959 if (std::get<std::string>(value) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530960 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
961 {
962 return true;
963 }
964 else
965 {
966 return false;
967 }
968 }
969 catch (sdbusplus::exception_t& e)
970 {
971 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
972 return true;
973 }
974}
975
976/** @brief implementes for check the DHCP or not in IPv6
977 * @param[in] Channel - Channel number
978 * @returns true or false.
979 */
980static bool isDHCPIPv6Enabled(uint8_t Channel)
981{
982
983 try
984 {
985 auto ethdevice = getChannelName(Channel);
986 if (ethdevice.empty())
987 {
988 return false;
989 }
990 auto ethIP = ethdevice + "/ipv6";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700991 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530992 auto objectInfo =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700993 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
994 auto properties = getAllDbusProperties(*dbus, objectInfo.second,
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530995 objectInfo.first, networkIPIntf);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700996 if (std::get<std::string>(properties["Origin"]) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530997 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
998 {
999 return true;
1000 }
1001 else
1002 {
1003 return false;
1004 }
1005 }
1006 catch (sdbusplus::exception_t& e)
1007 {
1008 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1009 return true;
1010 }
1011}
1012
1013/** @brief implementes the creating of default new user
1014 * @param[in] userName - new username in 16 bytes.
1015 * @param[in] userPassword - new password in 20 bytes
1016 * @returns ipmi completion code.
1017 */
1018ipmi::RspType<> ipmiOEMSetUser2Activation(
1019 std::array<uint8_t, ipmi::ipmiMaxUserName>& userName,
1020 std::array<uint8_t, ipmi::maxIpmi20PasswordSize>& userPassword)
1021{
1022 bool userState = false;
1023 // Check for System Interface not exist and LAN should be static
1024 for (uint8_t channel = 0; channel < maxIpmiChannels; channel++)
1025 {
1026 ChannelInfo chInfo;
1027 try
1028 {
1029 getChannelInfo(channel, chInfo);
1030 }
1031 catch (sdbusplus::exception_t& e)
1032 {
1033 phosphor::logging::log<phosphor::logging::level::ERR>(
1034 "ipmiOEMSetUser2Activation: Failed to get Channel Info",
1035 phosphor::logging::entry("MSG: %s", e.description()));
1036 return ipmi::response(ipmi::ccUnspecifiedError);
1037 }
1038 if (chInfo.mediumType ==
1039 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1040 {
1041 phosphor::logging::log<phosphor::logging::level::ERR>(
1042 "ipmiOEMSetUser2Activation: system interface exist .");
1043 return ipmi::response(ipmi::ccCommandNotAvailable);
1044 }
1045 else
1046 {
1047
1048 if (chInfo.mediumType ==
1049 static_cast<uint8_t>(EChannelMediumType::lan8032))
1050 {
1051 if (isDHCPIPv6Enabled(channel) || isDHCPEnabled(channel))
1052 {
1053 phosphor::logging::log<phosphor::logging::level::ERR>(
1054 "ipmiOEMSetUser2Activation: DHCP enabled .");
1055 return ipmi::response(ipmi::ccCommandNotAvailable);
1056 }
1057 }
1058 }
1059 }
1060 uint8_t maxChUsers = 0, enabledUsers = 0, fixedUsers = 0;
1061 if (ipmi::ccSuccess ==
1062 ipmiUserGetAllCounts(maxChUsers, enabledUsers, fixedUsers))
1063 {
1064 if (enabledUsers > 1)
1065 {
1066 phosphor::logging::log<phosphor::logging::level::ERR>(
1067 "ipmiOEMSetUser2Activation: more than one user is enabled.");
1068 return ipmi::response(ipmi::ccCommandNotAvailable);
1069 }
1070 // Check the user 2 is enabled or not
1071 ipmiUserCheckEnabled(ipmiDefaultUserId, userState);
1072 if (userState == true)
1073 {
1074 phosphor::logging::log<phosphor::logging::level::ERR>(
1075 "ipmiOEMSetUser2Activation: user 2 already enabled .");
1076 return ipmi::response(ipmi::ccCommandNotAvailable);
1077 }
1078 }
1079 else
1080 {
1081 return ipmi::response(ipmi::ccUnspecifiedError);
1082 }
1083
1084#if BYTE_ORDER == LITTLE_ENDIAN
1085 PrivAccess privAccess = {PRIVILEGE_ADMIN, true, true, true, 0};
1086#endif
1087#if BYTE_ORDER == BIG_ENDIAN
1088 PrivAccess privAccess = {0, true, true, true, PRIVILEGE_ADMIN};
1089#endif
1090
Vernon Mauery037cabd2020-05-14 12:16:01 -07001091 // ipmiUserSetUserName correctly handles char*, possibly non-null
1092 // terminated strings using ipmiMaxUserName size
1093 auto userNameRaw = reinterpret_cast<const char*>(userName.data());
jayaprakash Mutyala1429d4f2020-03-04 18:20:16 +00001094
Vernon Mauery037cabd2020-05-14 12:16:01 -07001095 if (ipmi::ccSuccess == ipmiUserSetUserName(ipmiDefaultUserId, userNameRaw))
Suryakanth Sekard509eb92018-11-15 17:44:11 +05301096 {
1097 if (ipmi::ccSuccess ==
1098 ipmiUserSetUserPassword(
1099 ipmiDefaultUserId,
1100 reinterpret_cast<const char*>(userPassword.data())))
1101 {
1102 if (ipmi::ccSuccess ==
1103 ipmiUserSetPrivilegeAccess(
1104 ipmiDefaultUserId,
1105 static_cast<uint8_t>(ipmi::EChannelID::chanLan1),
1106 privAccess, true))
1107 {
1108 phosphor::logging::log<phosphor::logging::level::INFO>(
1109 "ipmiOEMSetUser2Activation: user created successfully ");
1110 return ipmi::responseSuccess();
1111 }
1112 }
1113 // we need to delete the default user id which added in this command as
1114 // password / priv setting is failed.
1115 ipmiUserSetUserName(ipmiDefaultUserId, "");
1116 phosphor::logging::log<phosphor::logging::level::ERR>(
1117 "ipmiOEMSetUser2Activation: password / priv setting is failed.");
1118 }
1119 else
1120 {
1121 phosphor::logging::log<phosphor::logging::level::ERR>(
1122 "ipmiOEMSetUser2Activation: Setting username failed.");
1123 }
1124
1125 return ipmi::response(ipmi::ccCommandNotAvailable);
1126}
1127
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301128/** @brief implementes executing the linux command
1129 * @param[in] linux command
1130 * @returns status
1131 */
1132
1133static uint8_t executeCmd(const char* path)
1134{
1135 boost::process::child execProg(path);
1136 execProg.wait();
1137
1138 int retCode = execProg.exit_code();
1139 if (retCode)
1140 {
1141 return ipmi::ccUnspecifiedError;
1142 }
1143 return ipmi::ccSuccess;
1144}
1145
1146/** @brief implementes ASD Security event logging
1147 * @param[in] Event message string
1148 * @param[in] Event Severity
1149 * @returns status
1150 */
1151
1152static void atScaleDebugEventlog(std::string msg, int severity)
1153{
1154 std::string eventStr = "OpenBMC.0.1." + msg;
1155 sd_journal_send("MESSAGE=Security Event: %s", eventStr.c_str(),
1156 "PRIORITY=%i", severity, "REDFISH_MESSAGE_ID=%s",
1157 eventStr.c_str(), NULL);
1158}
1159
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301160/** @brief implementes setting password for special user
1161 * @param[in] specialUserIndex
1162 * @param[in] userPassword - new password in 20 bytes
1163 * @returns ipmi completion code.
1164 */
1165ipmi::RspType<> ipmiOEMSetSpecialUserPassword(ipmi::Context::ptr ctx,
1166 uint8_t specialUserIndex,
1167 std::vector<uint8_t> userPassword)
1168{
1169 ChannelInfo chInfo;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301170 ipmi_ret_t status = ipmi::ccSuccess;
1171
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301172 try
1173 {
1174 getChannelInfo(ctx->channel, chInfo);
1175 }
1176 catch (sdbusplus::exception_t& e)
1177 {
1178 phosphor::logging::log<phosphor::logging::level::ERR>(
1179 "ipmiOEMSetSpecialUserPassword: Failed to get Channel Info",
1180 phosphor::logging::entry("MSG: %s", e.description()));
1181 return ipmi::responseUnspecifiedError();
1182 }
1183 if (chInfo.mediumType !=
1184 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1185 {
1186 phosphor::logging::log<phosphor::logging::level::ERR>(
1187 "ipmiOEMSetSpecialUserPassword: Error - supported only in KCS "
1188 "interface");
1189 return ipmi::responseCommandNotAvailable();
1190 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301191
1192 // 0 for root user and 1 for AtScaleDebug is allowed
1193 if (specialUserIndex >
1194 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301195 {
1196 phosphor::logging::log<phosphor::logging::level::ERR>(
1197 "ipmiOEMSetSpecialUserPassword: Invalid user account");
1198 return ipmi::responseParmOutOfRange();
1199 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301200 if (userPassword.size() != 0)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301201 {
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301202 constexpr uint8_t minPasswordSizeRequired = 6;
1203 std::string passwd;
1204 if (userPassword.size() < minPasswordSizeRequired ||
1205 userPassword.size() > ipmi::maxIpmi20PasswordSize)
1206 {
1207 return ipmi::responseReqDataLenInvalid();
1208 }
1209 passwd.assign(reinterpret_cast<const char*>(userPassword.data()),
1210 userPassword.size());
1211 if (specialUserIndex ==
1212 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
1213 {
1214 status = ipmiSetSpecialUserPassword("asdbg", passwd);
1215
1216 atScaleDebugEventlog("AtScaleDebugSpecialUserEnabled", LOG_CRIT);
1217 }
1218 else
1219 {
1220 status = ipmiSetSpecialUserPassword("root", passwd);
1221 }
1222 return ipmi::response(status);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301223 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301224 else
1225 {
1226 if (specialUserIndex ==
1227 static_cast<uint8_t>(SpecialUserIndex::rootUser))
1228 {
1229 status = executeCmd("passwd -d root");
1230 }
1231 else
1232 {
1233
1234 status = executeCmd("passwd -d asdbg");
1235
1236 if (status == 0)
1237 {
1238 atScaleDebugEventlog("AtScaleDebugSpecialUserDisabled",
1239 LOG_INFO);
1240 }
1241 }
1242 return ipmi::response(status);
1243 }
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301244}
1245
Kuiying Wang45f04982018-12-26 09:23:08 +08001246namespace ledAction
1247{
1248using namespace sdbusplus::xyz::openbmc_project::Led::server;
1249std::map<Physical::Action, uint8_t> actionDbusToIpmi = {
jayaprakash Mutyala934ee9c2019-12-13 17:49:27 +00001250 {Physical::Action::Off, 0},
1251 {Physical::Action::On, 2},
1252 {Physical::Action::Blink, 1}};
Kuiying Wang45f04982018-12-26 09:23:08 +08001253
1254std::map<uint8_t, std::string> offsetObjPath = {
1255 {2, statusAmberObjPath}, {4, statusGreenObjPath}, {6, identifyLEDObjPath}};
1256
1257} // namespace ledAction
1258
1259int8_t getLEDState(sdbusplus::bus::bus& bus, const std::string& intf,
1260 const std::string& objPath, uint8_t& state)
1261{
1262 try
1263 {
1264 std::string service = getService(bus, intf, objPath);
1265 Value stateValue =
1266 getDbusProperty(bus, service, objPath, intf, "State");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001267 std::string strState = std::get<std::string>(stateValue);
Kuiying Wang45f04982018-12-26 09:23:08 +08001268 state = ledAction::actionDbusToIpmi.at(
1269 sdbusplus::xyz::openbmc_project::Led::server::Physical::
1270 convertActionFromString(strState));
1271 }
1272 catch (sdbusplus::exception::SdBusError& e)
1273 {
1274 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1275 return -1;
1276 }
1277 return 0;
1278}
1279
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001280ipmi::RspType<uint8_t> ipmiOEMGetLEDStatus()
Kuiying Wang45f04982018-12-26 09:23:08 +08001281{
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001282 uint8_t ledstate = 0;
Kuiying Wang45f04982018-12-26 09:23:08 +08001283 phosphor::logging::log<phosphor::logging::level::DEBUG>("GET led status");
Vernon Mauery15419dd2019-05-24 09:40:30 -07001284 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Kuiying Wang45f04982018-12-26 09:23:08 +08001285 for (auto it = ledAction::offsetObjPath.begin();
1286 it != ledAction::offsetObjPath.end(); ++it)
1287 {
1288 uint8_t state = 0;
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001289 if (getLEDState(*dbus, ledIntf, it->second, state) == -1)
Kuiying Wang45f04982018-12-26 09:23:08 +08001290 {
1291 phosphor::logging::log<phosphor::logging::level::ERR>(
1292 "oem_get_led_status: fail to get ID LED status!");
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001293 return ipmi::responseUnspecifiedError();
Kuiying Wang45f04982018-12-26 09:23:08 +08001294 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001295 ledstate |= state << it->first;
Kuiying Wang45f04982018-12-26 09:23:08 +08001296 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001297 return ipmi::responseSuccess(ledstate);
Kuiying Wang45f04982018-12-26 09:23:08 +08001298}
1299
Yong Li23737fe2019-02-19 08:49:55 +08001300ipmi_ret_t ipmiOEMCfgHostSerialPortSpeed(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1301 ipmi_request_t request,
1302 ipmi_response_t response,
1303 ipmi_data_len_t dataLen,
1304 ipmi_context_t context)
1305{
1306 CfgHostSerialReq* req = reinterpret_cast<CfgHostSerialReq*>(request);
1307 uint8_t* resp = reinterpret_cast<uint8_t*>(response);
1308
1309 if (*dataLen == 0)
1310 {
1311 phosphor::logging::log<phosphor::logging::level::ERR>(
1312 "CfgHostSerial: invalid input len!",
1313 phosphor::logging::entry("LEN=%d", *dataLen));
1314 return IPMI_CC_REQ_DATA_LEN_INVALID;
1315 }
1316
1317 switch (req->command)
1318 {
1319 case getHostSerialCfgCmd:
1320 {
1321 if (*dataLen != 1)
1322 {
1323 phosphor::logging::log<phosphor::logging::level::ERR>(
1324 "CfgHostSerial: invalid input len!");
1325 *dataLen = 0;
1326 return IPMI_CC_REQ_DATA_LEN_INVALID;
1327 }
1328
1329 *dataLen = 0;
1330
1331 boost::process::ipstream is;
1332 std::vector<std::string> data;
1333 std::string line;
1334 boost::process::child c1(fwGetEnvCmd, "-n", fwHostSerailCfgEnvName,
1335 boost::process::std_out > is);
1336
1337 while (c1.running() && std::getline(is, line) && !line.empty())
1338 {
1339 data.push_back(line);
1340 }
1341
1342 c1.wait();
1343 if (c1.exit_code())
1344 {
1345 phosphor::logging::log<phosphor::logging::level::ERR>(
1346 "CfgHostSerial:: error on execute",
1347 phosphor::logging::entry("EXECUTE=%s", fwSetEnvCmd));
1348 // Using the default value
1349 *resp = 0;
1350 }
1351 else
1352 {
1353 if (data.size() != 1)
1354 {
1355 phosphor::logging::log<phosphor::logging::level::ERR>(
1356 "CfgHostSerial:: error on read env");
1357 return IPMI_CC_UNSPECIFIED_ERROR;
1358 }
1359 try
1360 {
1361 unsigned long tmp = std::stoul(data[0]);
1362 if (tmp > std::numeric_limits<uint8_t>::max())
1363 {
1364 throw std::out_of_range("Out of range");
1365 }
1366 *resp = static_cast<uint8_t>(tmp);
1367 }
1368 catch (const std::invalid_argument& e)
1369 {
1370 phosphor::logging::log<phosphor::logging::level::ERR>(
1371 "invalid config ",
1372 phosphor::logging::entry("ERR=%s", e.what()));
1373 return IPMI_CC_UNSPECIFIED_ERROR;
1374 }
1375 catch (const std::out_of_range& e)
1376 {
1377 phosphor::logging::log<phosphor::logging::level::ERR>(
1378 "out_of_range config ",
1379 phosphor::logging::entry("ERR=%s", e.what()));
1380 return IPMI_CC_UNSPECIFIED_ERROR;
1381 }
1382 }
1383
1384 *dataLen = 1;
1385 break;
1386 }
1387 case setHostSerialCfgCmd:
1388 {
1389 if (*dataLen != sizeof(CfgHostSerialReq))
1390 {
1391 phosphor::logging::log<phosphor::logging::level::ERR>(
1392 "CfgHostSerial: invalid input len!");
1393 *dataLen = 0;
1394 return IPMI_CC_REQ_DATA_LEN_INVALID;
1395 }
1396
1397 *dataLen = 0;
1398
1399 if (req->parameter > HostSerialCfgParamMax)
1400 {
1401 phosphor::logging::log<phosphor::logging::level::ERR>(
1402 "CfgHostSerial: invalid input!");
1403 return IPMI_CC_INVALID_FIELD_REQUEST;
1404 }
1405
1406 boost::process::child c1(fwSetEnvCmd, fwHostSerailCfgEnvName,
1407 std::to_string(req->parameter));
1408
1409 c1.wait();
1410 if (c1.exit_code())
1411 {
1412 phosphor::logging::log<phosphor::logging::level::ERR>(
1413 "CfgHostSerial:: error on execute",
1414 phosphor::logging::entry("EXECUTE=%s", fwGetEnvCmd));
1415 return IPMI_CC_UNSPECIFIED_ERROR;
1416 }
1417 break;
1418 }
1419 default:
1420 phosphor::logging::log<phosphor::logging::level::ERR>(
1421 "CfgHostSerial: invalid input!");
1422 *dataLen = 0;
1423 return IPMI_CC_INVALID_FIELD_REQUEST;
1424 }
1425
1426 return IPMI_CC_OK;
1427}
1428
James Feist91244a62019-02-19 15:04:54 -08001429constexpr const char* thermalModeInterface =
1430 "xyz.openbmc_project.Control.ThermalMode";
1431constexpr const char* thermalModePath =
1432 "/xyz/openbmc_project/control/thermal_mode";
1433
1434bool getFanProfileInterface(
1435 sdbusplus::bus::bus& bus,
1436 boost::container::flat_map<
1437 std::string, std::variant<std::vector<std::string>, std::string>>& resp)
1438{
1439 auto call = bus.new_method_call(settingsBusName, thermalModePath, PROP_INTF,
1440 "GetAll");
1441 call.append(thermalModeInterface);
1442 try
1443 {
1444 auto data = bus.call(call);
1445 data.read(resp);
1446 }
1447 catch (sdbusplus::exception_t& e)
1448 {
1449 phosphor::logging::log<phosphor::logging::level::ERR>(
1450 "getFanProfileInterface: can't get thermal mode!",
1451 phosphor::logging::entry("ERR=%s", e.what()));
1452 return false;
1453 }
1454 return true;
1455}
1456
anil kumar appanaf945eee2019-09-25 23:29:11 +00001457/**@brief implements the OEM set fan config.
1458 * @param selectedFanProfile - fan profile to enable
1459 * @param reserved1
1460 * @param performanceMode - Performance/Acoustic mode
1461 * @param reserved2
1462 * @param setPerformanceMode - set Performance/Acoustic mode
1463 * @param setFanProfile - set fan profile
1464 *
1465 * @return IPMI completion code.
1466 **/
1467ipmi::RspType<> ipmiOEMSetFanConfig(uint8_t selectedFanProfile,
1468
1469 uint2_t reserved1, bool performanceMode,
1470 uint3_t reserved2, bool setPerformanceMode,
Joshi-Mansi619186d2020-01-27 19:16:03 +05301471 bool setFanProfile,
1472 std::optional<uint8_t> dimmGroupId,
1473 std::optional<uint32_t> dimmPresenceBitmap)
James Feist91244a62019-02-19 15:04:54 -08001474{
anil kumar appanaf945eee2019-09-25 23:29:11 +00001475 if (reserved1 || reserved2)
James Feist91244a62019-02-19 15:04:54 -08001476 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001477 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001478 }
Joshi-Mansi619186d2020-01-27 19:16:03 +05301479
1480 if (dimmGroupId)
1481 {
1482 if (*dimmGroupId >= maxCPUNum)
1483 {
1484 return ipmi::responseInvalidFieldRequest();
1485 }
1486 if (!cpuPresent("CPU_" + std::to_string(*dimmGroupId + 1)))
1487 {
1488 return ipmi::responseInvalidFieldRequest();
1489 }
1490 }
1491
James Feist91244a62019-02-19 15:04:54 -08001492 // todo: tell bios to only send first 2 bytes
James Feist91244a62019-02-19 15:04:54 -08001493 boost::container::flat_map<
1494 std::string, std::variant<std::vector<std::string>, std::string>>
1495 profileData;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001496 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1497 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001498 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001499 return ipmi::responseUnspecifiedError();
James Feist91244a62019-02-19 15:04:54 -08001500 }
1501
1502 std::vector<std::string>* supported =
1503 std::get_if<std::vector<std::string>>(&profileData["Supported"]);
1504 if (supported == nullptr)
1505 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001506 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001507 }
1508 std::string mode;
anil kumar appanaf945eee2019-09-25 23:29:11 +00001509 if (setPerformanceMode)
James Feist91244a62019-02-19 15:04:54 -08001510 {
James Feist91244a62019-02-19 15:04:54 -08001511 if (performanceMode)
1512 {
1513
1514 if (std::find(supported->begin(), supported->end(),
1515 "Performance") != supported->end())
1516 {
1517 mode = "Performance";
1518 }
1519 }
1520 else
1521 {
James Feist91244a62019-02-19 15:04:54 -08001522 if (std::find(supported->begin(), supported->end(), "Acoustic") !=
1523 supported->end())
1524 {
1525 mode = "Acoustic";
1526 }
1527 }
1528 if (mode.empty())
1529 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001530 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001531 }
anil kumar appanaf945eee2019-09-25 23:29:11 +00001532
1533 try
1534 {
1535 setDbusProperty(*dbus, settingsBusName, thermalModePath,
1536 thermalModeInterface, "Current", mode);
1537 }
1538 catch (sdbusplus::exception_t& e)
1539 {
1540 phosphor::logging::log<phosphor::logging::level::ERR>(
1541 "ipmiOEMSetFanConfig: can't set thermal mode!",
1542 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1543 return ipmi::responseResponseError();
1544 }
James Feist91244a62019-02-19 15:04:54 -08001545 }
1546
anil kumar appanaf945eee2019-09-25 23:29:11 +00001547 return ipmi::responseSuccess();
James Feist91244a62019-02-19 15:04:54 -08001548}
1549
James Feist5b693632019-07-09 09:06:09 -07001550ipmi::RspType<uint8_t, // profile support map
1551 uint8_t, // fan control profile enable
1552 uint8_t, // flags
1553 uint32_t // dimm presence bit map
1554 >
1555 ipmiOEMGetFanConfig(uint8_t dimmGroupId)
James Feist91244a62019-02-19 15:04:54 -08001556{
Joshi-Mansi36f05ce2020-01-14 14:29:34 +05301557 if (dimmGroupId >= maxCPUNum)
1558 {
1559 return ipmi::responseInvalidFieldRequest();
1560 }
1561
1562 bool cpuStatus = cpuPresent("CPU_" + std::to_string(dimmGroupId + 1));
1563
1564 if (!cpuStatus)
1565 {
1566 return ipmi::responseInvalidFieldRequest();
1567 }
1568
James Feist91244a62019-02-19 15:04:54 -08001569 boost::container::flat_map<
1570 std::string, std::variant<std::vector<std::string>, std::string>>
1571 profileData;
1572
Vernon Mauery15419dd2019-05-24 09:40:30 -07001573 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1574 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001575 {
James Feist5b693632019-07-09 09:06:09 -07001576 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001577 }
1578
1579 std::string* current = std::get_if<std::string>(&profileData["Current"]);
1580
1581 if (current == nullptr)
1582 {
1583 phosphor::logging::log<phosphor::logging::level::ERR>(
1584 "ipmiOEMGetFanConfig: can't get current mode!");
James Feist5b693632019-07-09 09:06:09 -07001585 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001586 }
1587 bool performance = (*current == "Performance");
1588
James Feist5b693632019-07-09 09:06:09 -07001589 uint8_t flags = 0;
James Feist91244a62019-02-19 15:04:54 -08001590 if (performance)
1591 {
James Feist5b693632019-07-09 09:06:09 -07001592 flags |= 1 << 2;
James Feist91244a62019-02-19 15:04:54 -08001593 }
1594
jayaprakash Mutyala4b1552d2020-02-11 12:07:29 +00001595 constexpr uint8_t fanControlDefaultProfile = 0x80;
1596 constexpr uint8_t fanControlProfileState = 0x00;
1597 constexpr uint32_t dimmPresenceBitmap = 0x00;
1598
1599 return ipmi::responseSuccess(fanControlDefaultProfile,
1600 fanControlProfileState, flags,
1601 dimmPresenceBitmap);
James Feist91244a62019-02-19 15:04:54 -08001602}
James Feist5f957ca2019-03-14 15:33:55 -07001603constexpr const char* cfmLimitSettingPath =
1604 "/xyz/openbmc_project/control/cfm_limit";
1605constexpr const char* cfmLimitIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistfaa4f222019-03-21 16:21:55 -07001606constexpr const size_t legacyExitAirSensorNumber = 0x2e;
James Feist09f6b602019-08-08 11:30:03 -07001607constexpr const size_t legacyPCHSensorNumber = 0x22;
1608constexpr const char* exitAirPathName = "Exit_Air";
1609constexpr const char* pchPathName = "SSB_Temp";
James Feistacc8a4e2019-04-02 14:23:57 -07001610constexpr const char* pidConfigurationIface =
1611 "xyz.openbmc_project.Configuration.Pid";
James Feistfaa4f222019-03-21 16:21:55 -07001612
James Feist09f6b602019-08-08 11:30:03 -07001613static std::string getConfigPath(const std::string& name)
James Feistfaa4f222019-03-21 16:21:55 -07001614{
Vernon Mauery15419dd2019-05-24 09:40:30 -07001615 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistfaa4f222019-03-21 16:21:55 -07001616 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001617 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1618 "/xyz/openbmc_project/object_mapper",
1619 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistfaa4f222019-03-21 16:21:55 -07001620
James Feistacc8a4e2019-04-02 14:23:57 -07001621 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
James Feistfaa4f222019-03-21 16:21:55 -07001622 std::string path;
1623 GetSubTreeType resp;
1624 try
1625 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001626 auto reply = dbus->call(method);
James Feistfaa4f222019-03-21 16:21:55 -07001627 reply.read(resp);
1628 }
1629 catch (sdbusplus::exception_t&)
1630 {
1631 phosphor::logging::log<phosphor::logging::level::ERR>(
1632 "ipmiOEMGetFscParameter: mapper error");
1633 };
James Feist09f6b602019-08-08 11:30:03 -07001634 auto config =
1635 std::find_if(resp.begin(), resp.end(), [&name](const auto& pair) {
1636 return pair.first.find(name) != std::string::npos;
1637 });
James Feistfaa4f222019-03-21 16:21:55 -07001638 if (config != resp.end())
1639 {
1640 path = std::move(config->first);
1641 }
1642 return path;
1643}
James Feist5f957ca2019-03-14 15:33:55 -07001644
James Feistacc8a4e2019-04-02 14:23:57 -07001645// flat map to make alphabetical
1646static boost::container::flat_map<std::string, PropertyMap> getPidConfigs()
1647{
1648 boost::container::flat_map<std::string, PropertyMap> ret;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001649 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001650 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001651 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1652 "/xyz/openbmc_project/object_mapper",
1653 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistacc8a4e2019-04-02 14:23:57 -07001654
1655 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
1656 GetSubTreeType resp;
1657
1658 try
1659 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001660 auto reply = dbus->call(method);
James Feistacc8a4e2019-04-02 14:23:57 -07001661 reply.read(resp);
1662 }
1663 catch (sdbusplus::exception_t&)
1664 {
1665 phosphor::logging::log<phosphor::logging::level::ERR>(
1666 "getFanConfigPaths: mapper error");
1667 };
1668 for (const auto& [path, objects] : resp)
1669 {
1670 if (objects.empty())
1671 {
1672 continue; // should be impossible
1673 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04001674
1675 try
1676 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001677 ret.emplace(path,
1678 getAllDbusProperties(*dbus, objects[0].first, path,
1679 pidConfigurationIface));
Zhu, Yungebe560b02019-04-21 21:19:21 -04001680 }
1681 catch (sdbusplus::exception_t& e)
1682 {
1683 phosphor::logging::log<phosphor::logging::level::ERR>(
1684 "getPidConfigs: can't get DbusProperties!",
1685 phosphor::logging::entry("ERR=%s", e.what()));
1686 }
James Feistacc8a4e2019-04-02 14:23:57 -07001687 }
1688 return ret;
1689}
1690
1691ipmi::RspType<uint8_t> ipmiOEMGetFanSpeedOffset(void)
1692{
1693 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1694 if (data.empty())
1695 {
1696 return ipmi::responseResponseError();
1697 }
1698 uint8_t minOffset = std::numeric_limits<uint8_t>::max();
1699 for (const auto& [_, pid] : data)
1700 {
1701 auto findClass = pid.find("Class");
1702 if (findClass == pid.end())
1703 {
1704 phosphor::logging::log<phosphor::logging::level::ERR>(
1705 "ipmiOEMGetFscParameter: found illegal pid "
1706 "configurations");
1707 return ipmi::responseResponseError();
1708 }
1709 std::string type = std::get<std::string>(findClass->second);
1710 if (type == "fan")
1711 {
1712 auto findOutLimit = pid.find("OutLimitMin");
1713 if (findOutLimit == pid.end())
1714 {
1715 phosphor::logging::log<phosphor::logging::level::ERR>(
1716 "ipmiOEMGetFscParameter: found illegal pid "
1717 "configurations");
1718 return ipmi::responseResponseError();
1719 }
1720 // get the min out of all the offsets
1721 minOffset = std::min(
1722 minOffset,
1723 static_cast<uint8_t>(std::get<double>(findOutLimit->second)));
1724 }
1725 }
1726 if (minOffset == std::numeric_limits<uint8_t>::max())
1727 {
1728 phosphor::logging::log<phosphor::logging::level::ERR>(
1729 "ipmiOEMGetFscParameter: found no fan configurations!");
1730 return ipmi::responseResponseError();
1731 }
1732
1733 return ipmi::responseSuccess(minOffset);
1734}
1735
1736ipmi::RspType<> ipmiOEMSetFanSpeedOffset(uint8_t offset)
1737{
1738 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1739 if (data.empty())
1740 {
1741
1742 phosphor::logging::log<phosphor::logging::level::ERR>(
1743 "ipmiOEMSetFanSpeedOffset: found no pid configurations!");
1744 return ipmi::responseResponseError();
1745 }
1746
Vernon Mauery15419dd2019-05-24 09:40:30 -07001747 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001748 bool found = false;
1749 for (const auto& [path, pid] : data)
1750 {
1751 auto findClass = pid.find("Class");
1752 if (findClass == pid.end())
1753 {
1754
1755 phosphor::logging::log<phosphor::logging::level::ERR>(
1756 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1757 "configurations");
1758 return ipmi::responseResponseError();
1759 }
1760 std::string type = std::get<std::string>(findClass->second);
1761 if (type == "fan")
1762 {
1763 auto findOutLimit = pid.find("OutLimitMin");
1764 if (findOutLimit == pid.end())
1765 {
1766
1767 phosphor::logging::log<phosphor::logging::level::ERR>(
1768 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1769 "configurations");
1770 return ipmi::responseResponseError();
1771 }
Vernon Mauery15419dd2019-05-24 09:40:30 -07001772 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager",
James Feistacc8a4e2019-04-02 14:23:57 -07001773 path, pidConfigurationIface, "OutLimitMin",
1774 static_cast<double>(offset));
1775 found = true;
1776 }
1777 }
1778 if (!found)
1779 {
1780 phosphor::logging::log<phosphor::logging::level::ERR>(
1781 "ipmiOEMSetFanSpeedOffset: set no fan offsets");
1782 return ipmi::responseResponseError();
1783 }
1784
1785 return ipmi::responseSuccess();
1786}
1787
1788ipmi::RspType<> ipmiOEMSetFscParameter(uint8_t command, uint8_t param1,
1789 uint8_t param2)
James Feist5f957ca2019-03-14 15:33:55 -07001790{
1791 constexpr const size_t disableLimiting = 0x0;
1792
Vernon Mauery15419dd2019-05-24 09:40:30 -07001793 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001794 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001795 {
James Feist09f6b602019-08-08 11:30:03 -07001796 std::string pathName;
James Feistacc8a4e2019-04-02 14:23:57 -07001797 if (param1 == legacyExitAirSensorNumber)
James Feistfaa4f222019-03-21 16:21:55 -07001798 {
James Feist09f6b602019-08-08 11:30:03 -07001799 pathName = exitAirPathName;
1800 }
1801 else if (param1 == legacyPCHSensorNumber)
1802 {
1803 pathName = pchPathName;
James Feistfaa4f222019-03-21 16:21:55 -07001804 }
1805 else
1806 {
James Feistacc8a4e2019-04-02 14:23:57 -07001807 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001808 }
James Feist09f6b602019-08-08 11:30:03 -07001809 std::string path = getConfigPath(pathName);
1810 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager", path,
1811 pidConfigurationIface, "SetPoint",
1812 static_cast<double>(param2));
1813 return ipmi::responseSuccess();
James Feistfaa4f222019-03-21 16:21:55 -07001814 }
James Feistacc8a4e2019-04-02 14:23:57 -07001815 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001816 {
James Feistacc8a4e2019-04-02 14:23:57 -07001817 uint16_t cfm = param1 | (static_cast<uint16_t>(param2) << 8);
James Feist5f957ca2019-03-14 15:33:55 -07001818
1819 // must be greater than 50 based on eps
1820 if (cfm < 50 && cfm != disableLimiting)
1821 {
James Feistacc8a4e2019-04-02 14:23:57 -07001822 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001823 }
1824
1825 try
1826 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001827 ipmi::setDbusProperty(*dbus, settingsBusName, cfmLimitSettingPath,
James Feist5f957ca2019-03-14 15:33:55 -07001828 cfmLimitIface, "Limit",
1829 static_cast<double>(cfm));
1830 }
1831 catch (sdbusplus::exception_t& e)
1832 {
1833 phosphor::logging::log<phosphor::logging::level::ERR>(
1834 "ipmiOEMSetFscParameter: can't set cfm setting!",
1835 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07001836 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07001837 }
James Feistacc8a4e2019-04-02 14:23:57 -07001838 return ipmi::responseSuccess();
1839 }
1840 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1841 {
1842 constexpr const size_t maxDomainCount = 8;
1843 uint8_t requestedDomainMask = param1;
1844 boost::container::flat_map data = getPidConfigs();
1845 if (data.empty())
1846 {
1847
1848 phosphor::logging::log<phosphor::logging::level::ERR>(
1849 "ipmiOEMSetFscParameter: found no pid configurations!");
1850 return ipmi::responseResponseError();
1851 }
1852 size_t count = 0;
1853 for (const auto& [path, pid] : data)
1854 {
1855 auto findClass = pid.find("Class");
1856 if (findClass == pid.end())
1857 {
1858
1859 phosphor::logging::log<phosphor::logging::level::ERR>(
1860 "ipmiOEMSetFscParameter: found illegal pid "
1861 "configurations");
1862 return ipmi::responseResponseError();
1863 }
1864 std::string type = std::get<std::string>(findClass->second);
1865 if (type == "fan")
1866 {
1867 if (requestedDomainMask & (1 << count))
1868 {
1869 ipmi::setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07001870 *dbus, "xyz.openbmc_project.EntityManager", path,
James Feistacc8a4e2019-04-02 14:23:57 -07001871 pidConfigurationIface, "OutLimitMax",
1872 static_cast<double>(param2));
1873 }
1874 count++;
1875 }
1876 }
1877 return ipmi::responseSuccess();
James Feist5f957ca2019-03-14 15:33:55 -07001878 }
1879 else
1880 {
1881 // todo other command parts possibly
1882 // tcontrol is handled in peci now
1883 // fan speed offset not implemented yet
1884 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07001885 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001886 }
1887}
1888
James Feistacc8a4e2019-04-02 14:23:57 -07001889ipmi::RspType<
1890 std::variant<uint8_t, std::array<uint8_t, 2>, std::array<uint16_t, 2>>>
1891 ipmiOEMGetFscParameter(uint8_t command, std::optional<uint8_t> param)
James Feist5f957ca2019-03-14 15:33:55 -07001892{
James Feist09f6b602019-08-08 11:30:03 -07001893 constexpr uint8_t legacyDefaultSetpoint = -128;
James Feist5f957ca2019-03-14 15:33:55 -07001894
Vernon Mauery15419dd2019-05-24 09:40:30 -07001895 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001896 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001897 {
James Feistacc8a4e2019-04-02 14:23:57 -07001898 if (!param)
James Feistfaa4f222019-03-21 16:21:55 -07001899 {
James Feistacc8a4e2019-04-02 14:23:57 -07001900 return ipmi::responseReqDataLenInvalid();
James Feistfaa4f222019-03-21 16:21:55 -07001901 }
1902
James Feist09f6b602019-08-08 11:30:03 -07001903 std::string pathName;
1904
1905 if (*param == legacyExitAirSensorNumber)
1906 {
1907 pathName = exitAirPathName;
1908 }
1909 else if (*param == legacyPCHSensorNumber)
1910 {
1911 pathName = pchPathName;
1912 }
1913 else
James Feistfaa4f222019-03-21 16:21:55 -07001914 {
James Feistacc8a4e2019-04-02 14:23:57 -07001915 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001916 }
James Feist09f6b602019-08-08 11:30:03 -07001917
1918 uint8_t setpoint = legacyDefaultSetpoint;
1919 std::string path = getConfigPath(pathName);
James Feistfaa4f222019-03-21 16:21:55 -07001920 if (path.size())
1921 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001922 Value val = ipmi::getDbusProperty(
1923 *dbus, "xyz.openbmc_project.EntityManager", path,
1924 pidConfigurationIface, "SetPoint");
James Feistfaa4f222019-03-21 16:21:55 -07001925 setpoint = std::floor(std::get<double>(val) + 0.5);
1926 }
1927
1928 // old implementation used to return the "default" and current, we
1929 // don't make the default readily available so just make both the
1930 // same
James Feistfaa4f222019-03-21 16:21:55 -07001931
James Feistacc8a4e2019-04-02 14:23:57 -07001932 return ipmi::responseSuccess(
1933 std::array<uint8_t, 2>{setpoint, setpoint});
James Feistfaa4f222019-03-21 16:21:55 -07001934 }
James Feistacc8a4e2019-04-02 14:23:57 -07001935 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1936 {
1937 constexpr const size_t maxDomainCount = 8;
1938
1939 if (!param)
1940 {
1941 return ipmi::responseReqDataLenInvalid();
1942 }
1943 uint8_t requestedDomain = *param;
1944 if (requestedDomain >= maxDomainCount)
1945 {
1946 return ipmi::responseInvalidFieldRequest();
1947 }
1948
1949 boost::container::flat_map data = getPidConfigs();
1950 if (data.empty())
1951 {
1952 phosphor::logging::log<phosphor::logging::level::ERR>(
1953 "ipmiOEMGetFscParameter: found no pid configurations!");
1954 return ipmi::responseResponseError();
1955 }
1956 size_t count = 0;
1957 for (const auto& [_, pid] : data)
1958 {
1959 auto findClass = pid.find("Class");
1960 if (findClass == pid.end())
1961 {
1962 phosphor::logging::log<phosphor::logging::level::ERR>(
1963 "ipmiOEMGetFscParameter: found illegal pid "
1964 "configurations");
1965 return ipmi::responseResponseError();
1966 }
1967 std::string type = std::get<std::string>(findClass->second);
1968 if (type == "fan")
1969 {
1970 if (requestedDomain == count)
1971 {
1972 auto findOutLimit = pid.find("OutLimitMax");
1973 if (findOutLimit == pid.end())
1974 {
1975 phosphor::logging::log<phosphor::logging::level::ERR>(
1976 "ipmiOEMGetFscParameter: found illegal pid "
1977 "configurations");
1978 return ipmi::responseResponseError();
1979 }
1980
1981 return ipmi::responseSuccess(
1982 static_cast<uint8_t>(std::floor(
1983 std::get<double>(findOutLimit->second) + 0.5)));
1984 }
1985 else
1986 {
1987 count++;
1988 }
1989 }
1990 }
1991
1992 return ipmi::responseInvalidFieldRequest();
1993 }
1994 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001995 {
1996
1997 /*
1998 DataLen should be 1, but host is sending us an extra bit. As the
James Feistacc8a4e2019-04-02 14:23:57 -07001999 previous behavior didn't seem to prevent this, ignore the check for
2000 now.
James Feist5f957ca2019-03-14 15:33:55 -07002001
James Feistacc8a4e2019-04-02 14:23:57 -07002002 if (param)
James Feist5f957ca2019-03-14 15:33:55 -07002003 {
2004 phosphor::logging::log<phosphor::logging::level::ERR>(
2005 "ipmiOEMGetFscParameter: invalid input len!");
James Feist5f957ca2019-03-14 15:33:55 -07002006 return IPMI_CC_REQ_DATA_LEN_INVALID;
2007 }
2008 */
2009 Value cfmLimit;
2010 Value cfmMaximum;
2011 try
2012 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002013 cfmLimit = ipmi::getDbusProperty(*dbus, settingsBusName,
James Feist5f957ca2019-03-14 15:33:55 -07002014 cfmLimitSettingPath, cfmLimitIface,
2015 "Limit");
2016 cfmMaximum = ipmi::getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002017 *dbus, "xyz.openbmc_project.ExitAirTempSensor",
James Feist5f957ca2019-03-14 15:33:55 -07002018 "/xyz/openbmc_project/control/MaxCFM", cfmLimitIface, "Limit");
2019 }
2020 catch (sdbusplus::exception_t& e)
2021 {
2022 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feistacc8a4e2019-04-02 14:23:57 -07002023 "ipmiOEMGetFscParameter: can't get cfm setting!",
James Feist5f957ca2019-03-14 15:33:55 -07002024 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002025 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002026 }
2027
James Feistacc8a4e2019-04-02 14:23:57 -07002028 double cfmMax = std::get<double>(cfmMaximum);
2029 double cfmLim = std::get<double>(cfmLimit);
James Feist5f957ca2019-03-14 15:33:55 -07002030
James Feistacc8a4e2019-04-02 14:23:57 -07002031 cfmLim = std::floor(cfmLim + 0.5);
2032 cfmMax = std::floor(cfmMax + 0.5);
2033 uint16_t cfmLimResp = static_cast<uint16_t>(cfmLim);
2034 uint16_t cfmMaxResp = static_cast<uint16_t>(cfmMax);
James Feist5f957ca2019-03-14 15:33:55 -07002035
James Feistacc8a4e2019-04-02 14:23:57 -07002036 return ipmi::responseSuccess(
2037 std::array<uint16_t, 2>{cfmLimResp, cfmMaxResp});
James Feist5f957ca2019-03-14 15:33:55 -07002038 }
James Feistacc8a4e2019-04-02 14:23:57 -07002039
James Feist5f957ca2019-03-14 15:33:55 -07002040 else
2041 {
2042 // todo other command parts possibly
James Feist5f957ca2019-03-14 15:33:55 -07002043 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002044 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002045 }
2046}
2047
Cheng C Yang773703a2019-08-15 09:41:11 +08002048using crConfigVariant =
2049 std::variant<bool, uint8_t, uint32_t, std::vector<uint8_t>, std::string>;
2050
2051int setCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2052 const crConfigVariant& value,
2053 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2054{
2055 boost::system::error_code ec;
2056 ctx->bus->yield_method_call<void>(
James Feist28c72902019-09-16 10:34:07 -07002057 ctx->yield, ec, "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002058 "/xyz/openbmc_project/control/power_supply_redundancy",
2059 "org.freedesktop.DBus.Properties", "Set",
2060 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property, value);
2061 if (ec)
2062 {
2063 phosphor::logging::log<phosphor::logging::level::ERR>(
2064 "Failed to set dbus property to cold redundancy");
2065 return -1;
2066 }
2067
2068 return 0;
2069}
2070
2071int getCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2072 crConfigVariant& value,
Yong Li19445ab2019-12-20 18:25:29 +08002073 const std::string& service = "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002074 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2075{
2076 boost::system::error_code ec;
2077 value = ctx->bus->yield_method_call<crConfigVariant>(
Yong Li19445ab2019-12-20 18:25:29 +08002078 ctx->yield, ec, service,
Cheng C Yang773703a2019-08-15 09:41:11 +08002079 "/xyz/openbmc_project/control/power_supply_redundancy",
2080 "org.freedesktop.DBus.Properties", "Get",
2081 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property);
2082 if (ec)
2083 {
2084 phosphor::logging::log<phosphor::logging::level::ERR>(
2085 "Failed to get dbus property to cold redundancy");
2086 return -1;
2087 }
2088 return 0;
2089}
2090
2091uint8_t getPSUCount(void)
2092{
2093 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2094 ipmi::Value num;
2095 try
2096 {
2097 num = ipmi::getDbusProperty(
2098 *dbus, "xyz.openbmc_project.PSURedundancy",
2099 "/xyz/openbmc_project/control/power_supply_redundancy",
2100 "xyz.openbmc_project.Control.PowerSupplyRedundancy", "PSUNumber");
2101 }
2102 catch (sdbusplus::exception_t& e)
2103 {
2104 phosphor::logging::log<phosphor::logging::level::ERR>(
2105 "Failed to get PSUNumber property from dbus interface");
2106 return 0;
2107 }
2108 uint8_t* pNum = std::get_if<uint8_t>(&num);
2109 if (!pNum)
2110 {
2111 phosphor::logging::log<phosphor::logging::level::ERR>(
2112 "Error to get PSU Number");
2113 return 0;
2114 }
2115 return *pNum;
2116}
2117
2118bool validateCRAlgo(std::vector<uint8_t>& conf, uint8_t num)
2119{
2120 if (conf.size() < num)
2121 {
2122 phosphor::logging::log<phosphor::logging::level::ERR>(
2123 "Invalid PSU Ranking");
2124 return false;
2125 }
2126 std::set<uint8_t> confSet;
2127 for (uint8_t i = 0; i < num; i++)
2128 {
2129 if (conf[i] > num)
2130 {
2131 phosphor::logging::log<phosphor::logging::level::ERR>(
2132 "PSU Ranking is larger than current PSU number");
2133 return false;
2134 }
2135 confSet.emplace(conf[i]);
2136 }
2137
2138 if (confSet.size() != num)
2139 {
2140 phosphor::logging::log<phosphor::logging::level::ERR>(
2141 "duplicate PSU Ranking");
2142 return false;
2143 }
2144 return true;
2145}
2146
2147enum class crParameter
2148{
2149 crStatus = 0,
2150 crFeature = 1,
2151 rotationFeature = 2,
2152 rotationAlgo = 3,
2153 rotationPeriod = 4,
Yong Li19445ab2019-12-20 18:25:29 +08002154 numOfPSU = 5,
2155 rotationRankOrderEffective = 6
Cheng C Yang773703a2019-08-15 09:41:11 +08002156};
2157
2158constexpr ipmi::Cc ccParameterNotSupported = 0x80;
2159static const constexpr uint32_t oneDay = 0x15180;
2160static const constexpr uint32_t oneMonth = 0xf53700;
2161static const constexpr uint8_t userSpecific = 0x01;
2162static const constexpr uint8_t crSetCompleted = 0;
2163ipmi::RspType<uint8_t> ipmiOEMSetCRConfig(ipmi::Context::ptr ctx,
2164 uint8_t parameter,
2165 ipmi::message::Payload& payload)
2166{
2167 switch (static_cast<crParameter>(parameter))
2168 {
2169 case crParameter::crFeature:
2170 {
2171 uint8_t param1;
2172 if (payload.unpack(param1) || !payload.fullyUnpacked())
2173 {
2174 return ipmi::responseReqDataLenInvalid();
2175 }
2176 // ColdRedundancy Enable can only be true or flase
2177 if (param1 > 1)
2178 {
2179 return ipmi::responseInvalidFieldRequest();
2180 }
2181 if (setCRConfig(ctx, "ColdRedundancyEnabled",
2182 static_cast<bool>(param1)))
2183 {
2184 return ipmi::responseResponseError();
2185 }
2186 break;
2187 }
2188 case crParameter::rotationFeature:
2189 {
2190 uint8_t param1;
2191 if (payload.unpack(param1) || !payload.fullyUnpacked())
2192 {
2193 return ipmi::responseReqDataLenInvalid();
2194 }
2195 // Rotation Enable can only be true or false
2196 if (param1 > 1)
2197 {
2198 return ipmi::responseInvalidFieldRequest();
2199 }
2200 if (setCRConfig(ctx, "RotationEnabled", static_cast<bool>(param1)))
2201 {
2202 return ipmi::responseResponseError();
2203 }
2204 break;
2205 }
2206 case crParameter::rotationAlgo:
2207 {
2208 // Rotation Algorithm can only be 0-BMC Specific or 1-User Specific
2209 std::string algoName;
2210 uint8_t param1;
2211 if (payload.unpack(param1))
2212 {
2213 return ipmi::responseReqDataLenInvalid();
2214 }
2215 switch (param1)
2216 {
2217 case 0:
2218 algoName = "xyz.openbmc_project.Control."
2219 "PowerSupplyRedundancy.Algo.bmcSpecific";
2220 break;
2221 case 1:
2222 algoName = "xyz.openbmc_project.Control."
2223 "PowerSupplyRedundancy.Algo.userSpecific";
2224 break;
2225 default:
2226 return ipmi::responseInvalidFieldRequest();
2227 }
2228 if (setCRConfig(ctx, "RotationAlgorithm", algoName))
2229 {
2230 return ipmi::responseResponseError();
2231 }
2232
2233 uint8_t numberOfPSU = getPSUCount();
2234 if (!numberOfPSU)
2235 {
2236 return ipmi::responseResponseError();
2237 }
2238 std::vector<uint8_t> rankOrder;
2239
2240 if (param1 == userSpecific)
2241 {
2242 if (payload.unpack(rankOrder) || !payload.fullyUnpacked())
2243 {
2244 ipmi::responseReqDataLenInvalid();
2245 }
Yong Li83315132019-10-23 17:42:24 +08002246 if (rankOrder.size() != numberOfPSU)
Cheng C Yang773703a2019-08-15 09:41:11 +08002247 {
2248 return ipmi::responseReqDataLenInvalid();
2249 }
2250
2251 if (!validateCRAlgo(rankOrder, numberOfPSU))
2252 {
2253 return ipmi::responseInvalidFieldRequest();
2254 }
2255 }
2256 else
2257 {
2258 if (rankOrder.size() > 0)
2259 {
2260 return ipmi::responseReqDataLenInvalid();
2261 }
2262 for (uint8_t i = 1; i <= numberOfPSU; i++)
2263 {
2264 rankOrder.emplace_back(i);
2265 }
2266 }
2267 if (setCRConfig(ctx, "RotationRankOrder", rankOrder))
2268 {
2269 return ipmi::responseResponseError();
2270 }
2271 break;
2272 }
2273 case crParameter::rotationPeriod:
2274 {
2275 // Minimum Rotation period is One day (86400 seconds) and Max
2276 // Rotation Period is 6 month (0xf53700 seconds)
2277 uint32_t period;
2278 if (payload.unpack(period) || !payload.fullyUnpacked())
2279 {
2280 return ipmi::responseReqDataLenInvalid();
2281 }
2282 if ((period < oneDay) || (period > oneMonth))
2283 {
2284 return ipmi::responseInvalidFieldRequest();
2285 }
2286 if (setCRConfig(ctx, "PeriodOfRotation", period))
2287 {
2288 return ipmi::responseResponseError();
2289 }
2290 break;
2291 }
2292 default:
2293 {
2294 return ipmi::response(ccParameterNotSupported);
2295 }
2296 }
2297
2298 // TODO Halfwidth needs to set SetInProgress
2299 if (setCRConfig(ctx, "ColdRedundancyStatus",
Cheng C Yange8cecdf2019-08-26 23:48:08 +08002300 std::string("xyz.openbmc_project.Control."
2301 "PowerSupplyRedundancy.Status.completed")))
Cheng C Yang773703a2019-08-15 09:41:11 +08002302 {
2303 return ipmi::responseResponseError();
2304 }
2305 return ipmi::responseSuccess(crSetCompleted);
2306}
2307
Yong Li83315132019-10-23 17:42:24 +08002308ipmi::RspType<uint8_t, std::variant<uint8_t, uint32_t, std::vector<uint8_t>>>
Cheng C Yang773703a2019-08-15 09:41:11 +08002309 ipmiOEMGetCRConfig(ipmi::Context::ptr ctx, uint8_t parameter)
2310{
2311 crConfigVariant value;
2312 switch (static_cast<crParameter>(parameter))
2313 {
2314 case crParameter::crStatus:
2315 {
2316 if (getCRConfig(ctx, "ColdRedundancyStatus", value))
2317 {
2318 return ipmi::responseResponseError();
2319 }
2320 std::string* pStatus = std::get_if<std::string>(&value);
2321 if (!pStatus)
2322 {
2323 phosphor::logging::log<phosphor::logging::level::ERR>(
2324 "Error to get ColdRedundancyStatus property");
2325 return ipmi::responseResponseError();
2326 }
2327 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2328 auto status =
2329 server::PowerSupplyRedundancy::convertStatusFromString(
2330 *pStatus);
2331 switch (status)
2332 {
2333 case server::PowerSupplyRedundancy::Status::inProgress:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002334 return ipmi::responseSuccess(parameter,
2335 static_cast<uint8_t>(0));
Cheng C Yang773703a2019-08-15 09:41:11 +08002336
2337 case server::PowerSupplyRedundancy::Status::completed:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002338 return ipmi::responseSuccess(parameter,
2339 static_cast<uint8_t>(1));
Cheng C Yang773703a2019-08-15 09:41:11 +08002340 default:
2341 phosphor::logging::log<phosphor::logging::level::ERR>(
2342 "Error to get valid status");
2343 return ipmi::responseResponseError();
2344 }
2345 }
2346 case crParameter::crFeature:
2347 {
2348 if (getCRConfig(ctx, "ColdRedundancyEnabled", value))
2349 {
2350 return ipmi::responseResponseError();
2351 }
2352 bool* pResponse = std::get_if<bool>(&value);
2353 if (!pResponse)
2354 {
2355 phosphor::logging::log<phosphor::logging::level::ERR>(
2356 "Error to get ColdRedundancyEnable property");
2357 return ipmi::responseResponseError();
2358 }
2359
Cheng C Yangf41e3342019-09-10 04:47:23 +08002360 return ipmi::responseSuccess(parameter,
2361 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002362 }
2363 case crParameter::rotationFeature:
2364 {
2365 if (getCRConfig(ctx, "RotationEnabled", value))
2366 {
2367 return ipmi::responseResponseError();
2368 }
2369 bool* pResponse = std::get_if<bool>(&value);
2370 if (!pResponse)
2371 {
2372 phosphor::logging::log<phosphor::logging::level::ERR>(
2373 "Error to get RotationEnabled property");
2374 return ipmi::responseResponseError();
2375 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002376 return ipmi::responseSuccess(parameter,
2377 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002378 }
2379 case crParameter::rotationAlgo:
2380 {
2381 if (getCRConfig(ctx, "RotationAlgorithm", value))
2382 {
2383 return ipmi::responseResponseError();
2384 }
2385
2386 std::string* pAlgo = std::get_if<std::string>(&value);
2387 if (!pAlgo)
2388 {
2389 phosphor::logging::log<phosphor::logging::level::ERR>(
2390 "Error to get RotationAlgorithm property");
2391 return ipmi::responseResponseError();
2392 }
Yong Li83315132019-10-23 17:42:24 +08002393 std::vector<uint8_t> response;
Cheng C Yang773703a2019-08-15 09:41:11 +08002394 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2395 auto algo =
2396 server::PowerSupplyRedundancy::convertAlgoFromString(*pAlgo);
Yong Li83315132019-10-23 17:42:24 +08002397
Cheng C Yang773703a2019-08-15 09:41:11 +08002398 switch (algo)
2399 {
2400 case server::PowerSupplyRedundancy::Algo::bmcSpecific:
Yong Li83315132019-10-23 17:42:24 +08002401 response.push_back(0);
Cheng C Yang773703a2019-08-15 09:41:11 +08002402 break;
2403 case server::PowerSupplyRedundancy::Algo::userSpecific:
Yong Li83315132019-10-23 17:42:24 +08002404 response.push_back(1);
Cheng C Yang773703a2019-08-15 09:41:11 +08002405 break;
2406 default:
2407 phosphor::logging::log<phosphor::logging::level::ERR>(
2408 "Error to get valid algo");
2409 return ipmi::responseResponseError();
2410 }
2411
2412 if (getCRConfig(ctx, "RotationRankOrder", value))
2413 {
2414 return ipmi::responseResponseError();
2415 }
2416 std::vector<uint8_t>* pResponse =
2417 std::get_if<std::vector<uint8_t>>(&value);
2418 if (!pResponse)
2419 {
2420 phosphor::logging::log<phosphor::logging::level::ERR>(
2421 "Error to get RotationRankOrder property");
2422 return ipmi::responseResponseError();
2423 }
Yong Li83315132019-10-23 17:42:24 +08002424
Cheng C Yang773703a2019-08-15 09:41:11 +08002425 std::copy(pResponse->begin(), pResponse->end(),
Yong Li83315132019-10-23 17:42:24 +08002426 std::back_inserter(response));
2427
Cheng C Yangf41e3342019-09-10 04:47:23 +08002428 return ipmi::responseSuccess(parameter, response);
Cheng C Yang773703a2019-08-15 09:41:11 +08002429 }
2430 case crParameter::rotationPeriod:
2431 {
2432 if (getCRConfig(ctx, "PeriodOfRotation", value))
2433 {
2434 return ipmi::responseResponseError();
2435 }
2436 uint32_t* pResponse = std::get_if<uint32_t>(&value);
2437 if (!pResponse)
2438 {
2439 phosphor::logging::log<phosphor::logging::level::ERR>(
2440 "Error to get RotationAlgorithm property");
2441 return ipmi::responseResponseError();
2442 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002443 return ipmi::responseSuccess(parameter, *pResponse);
Cheng C Yang773703a2019-08-15 09:41:11 +08002444 }
2445 case crParameter::numOfPSU:
2446 {
2447 uint8_t numberOfPSU = getPSUCount();
2448 if (!numberOfPSU)
2449 {
2450 return ipmi::responseResponseError();
2451 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002452 return ipmi::responseSuccess(parameter, numberOfPSU);
Cheng C Yang773703a2019-08-15 09:41:11 +08002453 }
Yong Li19445ab2019-12-20 18:25:29 +08002454 case crParameter::rotationRankOrderEffective:
2455 {
2456 if (getCRConfig(ctx, "RotationRankOrder", value,
2457 "xyz.openbmc_project.PSURedundancy"))
2458 {
2459 return ipmi::responseResponseError();
2460 }
2461 std::vector<uint8_t>* pResponse =
2462 std::get_if<std::vector<uint8_t>>(&value);
2463 if (!pResponse)
2464 {
2465 phosphor::logging::log<phosphor::logging::level::ERR>(
2466 "Error to get effective RotationRankOrder property");
2467 return ipmi::responseResponseError();
2468 }
2469 return ipmi::responseSuccess(parameter, *pResponse);
2470 }
Cheng C Yang773703a2019-08-15 09:41:11 +08002471 default:
2472 {
2473 return ipmi::response(ccParameterNotSupported);
2474 }
2475 }
2476}
2477
Zhu, Yungebe560b02019-04-21 21:19:21 -04002478ipmi::RspType<> ipmiOEMSetFaultIndication(uint8_t sourceId, uint8_t faultType,
2479 uint8_t faultState,
2480 uint8_t faultGroup,
2481 std::array<uint8_t, 8>& ledStateData)
2482{
Zhu, Yungebe560b02019-04-21 21:19:21 -04002483 constexpr auto maxFaultType = static_cast<size_t>(RemoteFaultType::max);
2484 static const std::array<std::string, maxFaultType> faultNames = {
2485 "faultFan", "faultTemp", "faultPower",
2486 "faultDriveSlot", "faultSoftware", "faultMemory"};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002487
2488 constexpr uint8_t maxFaultSource = 0x4;
2489 constexpr uint8_t skipLEDs = 0xFF;
2490 constexpr uint8_t pinSize = 64;
2491 constexpr uint8_t groupSize = 16;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002492 constexpr uint8_t groupNum = 5; // 4 for fault memory, 1 for faultFan
Zhu, Yungebe560b02019-04-21 21:19:21 -04002493
Zhikui Rence4e73f2019-12-06 13:59:47 -08002494 // same pin names need to be defined in dts file
2495 static const std::array<std::array<std::string, groupSize>, groupNum>
2496 faultLedPinNames = {{
2497 "LED_CPU1_CH1_DIMM1_FAULT",
2498 "LED_CPU1_CH1_DIMM2_FAULT",
2499 "LED_CPU1_CH2_DIMM1_FAULT",
2500 "LED_CPU1_CH2_DIMM2_FAULT",
2501 "LED_CPU1_CH3_DIMM1_FAULT",
2502 "LED_CPU1_CH3_DIMM2_FAULT",
2503 "LED_CPU1_CH4_DIMM1_FAULT",
2504 "LED_CPU1_CH4_DIMM2_FAULT",
2505 "LED_CPU1_CH5_DIMM1_FAULT",
2506 "LED_CPU1_CH5_DIMM2_FAULT",
2507 "LED_CPU1_CH6_DIMM1_FAULT",
2508 "LED_CPU1_CH6_DIMM2_FAULT",
2509 "",
2510 "",
2511 "",
2512 "", // end of group1
2513 "LED_CPU2_CH1_DIMM1_FAULT",
2514 "LED_CPU2_CH1_DIMM2_FAULT",
2515 "LED_CPU2_CH2_DIMM1_FAULT",
2516 "LED_CPU2_CH2_DIMM2_FAULT",
2517 "LED_CPU2_CH3_DIMM1_FAULT",
2518 "LED_CPU2_CH3_DIMM2_FAULT",
2519 "LED_CPU2_CH4_DIMM1_FAULT",
2520 "LED_CPU2_CH4_DIMM2_FAULT",
2521 "LED_CPU2_CH5_DIMM1_FAULT",
2522 "LED_CPU2_CH5_DIMM2_FAULT",
2523 "LED_CPU2_CH6_DIMM1_FAULT",
2524 "LED_CPU2_CH6_DIMM2_FAULT",
2525 "",
2526 "",
2527 "",
2528 "", // endof group2
2529 "LED_CPU3_CH1_DIMM1_FAULT",
2530 "LED_CPU3_CH1_DIMM2_FAULT",
2531 "LED_CPU3_CH2_DIMM1_FAULT",
2532 "LED_CPU3_CH2_DIMM2_FAULT",
2533 "LED_CPU3_CH3_DIMM1_FAULT",
2534 "LED_CPU3_CH3_DIMM2_FAULT",
2535 "LED_CPU3_CH4_DIMM1_FAULT",
2536 "LED_CPU3_CH4_DIMM2_FAULT",
2537 "LED_CPU3_CH5_DIMM1_FAULT",
2538 "LED_CPU3_CH5_DIMM2_FAULT",
2539 "LED_CPU3_CH6_DIMM1_FAULT",
2540 "LED_CPU3_CH6_DIMM2_FAULT",
2541 "",
2542 "",
2543 "",
2544 "", // end of group3
2545 "LED_CPU4_CH1_DIMM1_FAULT",
2546 "LED_CPU4_CH1_DIMM2_FAULT",
2547 "LED_CPU4_CH2_DIMM1_FAULT",
2548 "LED_CPU4_CH2_DIMM2_FAULT",
2549 "LED_CPU4_CH3_DIMM1_FAULT",
2550 "LED_CPU4_CH3_DIMM2_FAULT",
2551 "LED_CPU4_CH4_DIMM1_FAULT",
2552 "LED_CPU4_CH4_DIMM2_FAULT",
2553 "LED_CPU4_CH5_DIMM1_FAULT",
2554 "LED_CPU4_CH5_DIMM2_FAULT",
2555 "LED_CPU4_CH6_DIMM1_FAULT",
2556 "LED_CPU4_CH6_DIMM2_FAULT",
2557 "",
2558 "",
2559 "",
2560 "", // end of group4
2561 "LED_FAN1_FAULT",
2562 "LED_FAN2_FAULT",
2563 "LED_FAN3_FAULT",
2564 "LED_FAN4_FAULT",
2565 "LED_FAN5_FAULT",
2566 "LED_FAN6_FAULT",
2567 "LED_FAN7_FAULT",
2568 "LED_FAN8_FAULT",
2569 "",
2570 "",
2571 "",
2572 "",
2573 "",
2574 "",
2575 "",
2576 "" // end of group5
2577 }};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002578
Zhikui Rence4e73f2019-12-06 13:59:47 -08002579 // Validate the source, fault type --
2580 // (Byte 1) sourceId: Unspecified, Hot-Swap Controller 0, Hot-Swap
2581 // Controller 1, BIOS (Byte 2) fault type: fan, temperature, power,
2582 // driveslot, software, memory (Byte 3) FaultState: OK, Degraded,
2583 // Non-Critical, Critical, Non-Recoverable, (Byte 4) is faultGroup,
2584 // definition differs based on fault type (Byte 2)
2585 // Type Fan=> Group: 0=FanGroupID, FF-not used
2586 // Byte 5-11 00h, not used
2587 // Byte12 FanLedState [7:0]-Fans 7:0
2588 // Type Memory=> Group: 0 = DIMM GroupID, FF-not used
2589 // Byte 5:12 - DIMM LED state (64bit field, LS Byte first)
2590 // [63:48] = CPU4 channels 7:0, 2 bits per channel
2591 // [47:32] = CPU3 channels 7:0, 2 bits per channel
2592 // [31:16] = CPU2 channels 7:0, 2 bits per channel
2593 // [15:0] = CPU1 channels 7:0, 2 bits per channel
2594 // Type Other=> Component Fault LED Group ID, not used set to 0xFF
2595 // Byte[5:12]: reserved 0x00h
Zhu, Yungebe560b02019-04-21 21:19:21 -04002596 if ((sourceId >= maxFaultSource) ||
2597 (faultType >= static_cast<int8_t>(RemoteFaultType::max)) ||
2598 (faultState >= static_cast<int8_t>(RemoteFaultState::maxFaultState)) ||
2599 (faultGroup >= static_cast<int8_t>(DimmFaultType::maxFaultGroup)))
2600 {
2601 return ipmi::responseParmOutOfRange();
2602 }
2603
Zhikui Rence4e73f2019-12-06 13:59:47 -08002604 size_t pinGroupOffset = 0;
2605 size_t pinGroupMax = pinSize / groupSize;
2606 if (RemoteFaultType::fan == RemoteFaultType(faultType))
Zhu, Yungebe560b02019-04-21 21:19:21 -04002607 {
Zhikui Rence4e73f2019-12-06 13:59:47 -08002608 pinGroupOffset = 4;
2609 pinGroupMax = groupNum - pinSize / groupSize;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002610 }
2611
2612 switch (RemoteFaultType(faultType))
2613 {
2614 case (RemoteFaultType::fan):
2615 case (RemoteFaultType::memory):
2616 {
2617 if (faultGroup == skipLEDs)
2618 {
2619 return ipmi::responseSuccess();
2620 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002621 // calculate led state bit filed count, each byte has 8bits
2622 // the maximum bits will be 8 * 8 bits
2623 constexpr uint8_t size = sizeof(ledStateData) * 8;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002624
2625 // assemble ledState
2626 uint64_t ledState = 0;
2627 bool hasError = false;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002628 for (int i = 0; i < sizeof(ledStateData); i++)
2629 {
2630 ledState = (uint64_t)(ledState << 8);
2631 ledState = (uint64_t)(ledState | (uint64_t)ledStateData[i]);
2632 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002633 std::bitset<size> ledStateBits(ledState);
Zhu, Yungebe560b02019-04-21 21:19:21 -04002634
Zhikui Rence4e73f2019-12-06 13:59:47 -08002635 for (int group = 0; group < pinGroupMax; group++)
2636 {
2637 for (int i = 0; i < groupSize; i++)
2638 { // skip non-existing pins
2639 if (0 == faultLedPinNames[group + pinGroupOffset][i].size())
2640 {
2641 continue;
2642 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002643
Zhikui Rence4e73f2019-12-06 13:59:47 -08002644 gpiod::line line = gpiod::find_line(
2645 faultLedPinNames[group + pinGroupOffset][i]);
2646 if (!line)
2647 {
2648 phosphor::logging::log<phosphor::logging::level::ERR>(
2649 "Not Find Led Gpio Device!",
2650 phosphor::logging::entry(
2651 "DEVICE=%s",
2652 faultLedPinNames[group + pinGroupOffset][i]
2653 .c_str()));
2654 hasError = true;
2655 continue;
2656 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002657
Zhikui Rence4e73f2019-12-06 13:59:47 -08002658 bool activeHigh =
2659 (line.active_state() == gpiod::line::ACTIVE_HIGH);
2660 try
2661 {
2662 line.request(
2663 {"faultLed", gpiod::line_request::DIRECTION_OUTPUT,
2664 activeHigh
2665 ? 0
2666 : gpiod::line_request::FLAG_ACTIVE_LOW});
2667 line.set_value(ledStateBits[i + group * groupSize]);
2668 }
2669 catch (std::system_error&)
2670 {
2671 phosphor::logging::log<phosphor::logging::level::ERR>(
2672 "Error write Led Gpio Device!",
2673 phosphor::logging::entry(
2674 "DEVICE=%s",
2675 faultLedPinNames[group + pinGroupOffset][i]
2676 .c_str()));
2677 hasError = true;
2678 continue;
2679 }
2680 } // for int i
2681 }
2682 if (hasError)
2683 {
2684 return ipmi::responseResponseError();
Zhu, Yungebe560b02019-04-21 21:19:21 -04002685 }
2686 break;
2687 }
2688 default:
2689 {
2690 // now only support two fault types
2691 return ipmi::responseParmOutOfRange();
2692 }
Zhikui Rence4e73f2019-12-06 13:59:47 -08002693 } // switch
Zhu, Yungebe560b02019-04-21 21:19:21 -04002694 return ipmi::responseSuccess();
2695}
2696
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302697ipmi::RspType<uint8_t> ipmiOEMReadBoardProductId()
2698{
2699 uint8_t prodId = 0;
2700 try
2701 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002702 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302703 const DbusObjectInfo& object = getDbusObject(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002704 *dbus, "xyz.openbmc_project.Inventory.Item.Board",
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302705 "/xyz/openbmc_project/inventory/system/board/", "Baseboard");
2706 const Value& propValue = getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002707 *dbus, object.second, object.first,
Suryakanth Sekar6c57e5c2020-01-10 17:11:58 +05302708 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
2709 "ProductId");
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302710 prodId = static_cast<uint8_t>(std::get<uint64_t>(propValue));
2711 }
2712 catch (std::exception& e)
2713 {
2714 phosphor::logging::log<phosphor::logging::level::ERR>(
2715 "ipmiOEMReadBoardProductId: Product ID read failed!",
2716 phosphor::logging::entry("ERR=%s", e.what()));
2717 }
2718 return ipmi::responseSuccess(prodId);
2719}
2720
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302721/** @brief implements the get security mode command
2722 * @param ctx - ctx pointer
2723 *
2724 * @returns IPMI completion code with following data
2725 * - restriction mode value - As specified in
2726 * xyz.openbmc_project.Control.Security.RestrictionMode.interface.yaml
2727 * - special mode value - As specified in
2728 * xyz.openbmc_project.Control.Security.SpecialMode.interface.yaml
2729 */
2730ipmi::RspType<uint8_t, uint8_t> ipmiGetSecurityMode(ipmi::Context::ptr ctx)
2731{
2732 namespace securityNameSpace =
2733 sdbusplus::xyz::openbmc_project::Control::Security::server;
2734 uint8_t restrictionModeValue = 0;
2735 uint8_t specialModeValue = 0;
2736
2737 boost::system::error_code ec;
2738 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002739 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302740 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2741 restricionModeProperty);
2742 if (ec)
2743 {
2744 phosphor::logging::log<phosphor::logging::level::ERR>(
2745 "ipmiGetSecurityMode: failed to get RestrictionMode property",
2746 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2747 return ipmi::responseUnspecifiedError();
2748 }
2749 restrictionModeValue = static_cast<uint8_t>(
2750 securityNameSpace::RestrictionMode::convertModesFromString(
2751 std::get<std::string>(varRestrMode)));
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302752 auto varSpecialMode =
2753 ctx->bus->yield_method_call<std::variant<std::string>>(
2754 ctx->yield, ec, specialModeService, specialModeBasePath,
2755 dBusPropertyIntf, dBusPropertyGetMethod, specialModeIntf,
2756 specialModeProperty);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302757 if (ec)
2758 {
2759 phosphor::logging::log<phosphor::logging::level::ERR>(
2760 "ipmiGetSecurityMode: failed to get SpecialMode property",
2761 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2762 // fall through, let us not worry about SpecialMode property, which is
2763 // not required in user scenario
2764 }
2765 else
2766 {
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302767 specialModeValue = static_cast<uint8_t>(
2768 securityNameSpace::SpecialMode::convertModesFromString(
2769 std::get<std::string>(varSpecialMode)));
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302770 }
2771 return ipmi::responseSuccess(restrictionModeValue, specialModeValue);
2772}
2773
2774/** @brief implements the set security mode command
2775 * Command allows to upgrade the restriction mode and won't allow
2776 * to downgrade from system interface
2777 * @param ctx - ctx pointer
2778 * @param restrictionMode - restriction mode value to be set.
2779 *
2780 * @returns IPMI completion code
2781 */
2782ipmi::RspType<> ipmiSetSecurityMode(ipmi::Context::ptr ctx,
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302783 uint8_t restrictionMode,
2784 std::optional<uint8_t> specialMode)
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302785{
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302786#ifndef BMC_VALIDATION_UNSECURE_FEATURE
2787 if (specialMode)
2788 {
2789 return ipmi::responseReqDataLenInvalid();
2790 }
2791#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302792 namespace securityNameSpace =
2793 sdbusplus::xyz::openbmc_project::Control::Security::server;
2794
2795 ChannelInfo chInfo;
2796 if (getChannelInfo(ctx->channel, chInfo) != ccSuccess)
2797 {
2798 phosphor::logging::log<phosphor::logging::level::ERR>(
2799 "ipmiSetSecurityMode: Failed to get Channel Info",
2800 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
2801 return ipmi::responseUnspecifiedError();
2802 }
2803 auto reqMode =
2804 static_cast<securityNameSpace::RestrictionMode::Modes>(restrictionMode);
2805
2806 if ((reqMode < securityNameSpace::RestrictionMode::Modes::Provisioning) ||
2807 (reqMode >
2808 securityNameSpace::RestrictionMode::Modes::ProvisionedHostDisabled))
2809 {
2810 return ipmi::responseInvalidFieldRequest();
2811 }
2812
2813 boost::system::error_code ec;
2814 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002815 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302816 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2817 restricionModeProperty);
2818 if (ec)
2819 {
2820 phosphor::logging::log<phosphor::logging::level::ERR>(
2821 "ipmiSetSecurityMode: failed to get RestrictionMode property",
2822 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2823 return ipmi::responseUnspecifiedError();
2824 }
2825 auto currentRestrictionMode =
2826 securityNameSpace::RestrictionMode::convertModesFromString(
2827 std::get<std::string>(varRestrMode));
2828
2829 if (chInfo.mediumType !=
2830 static_cast<uint8_t>(EChannelMediumType::lan8032) &&
2831 currentRestrictionMode > reqMode)
2832 {
2833 phosphor::logging::log<phosphor::logging::level::ERR>(
2834 "ipmiSetSecurityMode - Downgrading security mode not supported "
2835 "through system interface",
2836 phosphor::logging::entry(
2837 "CUR_MODE=%d", static_cast<uint8_t>(currentRestrictionMode)),
2838 phosphor::logging::entry("REQ_MODE=%d", restrictionMode));
2839 return ipmi::responseCommandNotAvailable();
2840 }
2841
2842 ec.clear();
2843 ctx->bus->yield_method_call<>(
James Feist28c72902019-09-16 10:34:07 -07002844 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302845 dBusPropertyIntf, dBusPropertySetMethod, restricionModeIntf,
2846 restricionModeProperty,
2847 static_cast<std::variant<std::string>>(
2848 securityNameSpace::convertForMessage(reqMode)));
2849
2850 if (ec)
2851 {
2852 phosphor::logging::log<phosphor::logging::level::ERR>(
2853 "ipmiSetSecurityMode: failed to set RestrictionMode property",
2854 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2855 return ipmi::responseUnspecifiedError();
2856 }
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302857
2858#ifdef BMC_VALIDATION_UNSECURE_FEATURE
2859 if (specialMode)
2860 {
2861 ec.clear();
2862 ctx->bus->yield_method_call<>(
2863 ctx->yield, ec, specialModeService, specialModeBasePath,
2864 dBusPropertyIntf, dBusPropertySetMethod, specialModeIntf,
2865 specialModeProperty,
2866 static_cast<std::variant<std::string>>(
2867 securityNameSpace::convertForMessage(
2868 static_cast<securityNameSpace::SpecialMode::Modes>(
2869 specialMode.value()))));
2870
2871 if (ec)
2872 {
2873 phosphor::logging::log<phosphor::logging::level::ERR>(
2874 "ipmiSetSecurityMode: failed to set SpecialMode property",
2875 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2876 return ipmi::responseUnspecifiedError();
2877 }
2878 }
2879#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302880 return ipmi::responseSuccess();
2881}
2882
Vernon Mauery4ac799d2019-05-20 15:50:37 -07002883ipmi::RspType<uint8_t /* restore status */>
2884 ipmiRestoreConfiguration(const std::array<uint8_t, 3>& clr, uint8_t cmd)
2885{
2886 static constexpr std::array<uint8_t, 3> expClr = {'C', 'L', 'R'};
2887
2888 if (clr != expClr)
2889 {
2890 return ipmi::responseInvalidFieldRequest();
2891 }
2892 constexpr uint8_t cmdStatus = 0;
2893 constexpr uint8_t cmdDefaultRestore = 0xaa;
2894 constexpr uint8_t cmdFullRestore = 0xbb;
2895 constexpr uint8_t cmdFormat = 0xcc;
2896
2897 constexpr const char* restoreOpFname = "/tmp/.rwfs/.restore_op";
2898
2899 switch (cmd)
2900 {
2901 case cmdStatus:
2902 break;
2903 case cmdDefaultRestore:
2904 case cmdFullRestore:
2905 case cmdFormat:
2906 {
2907 // write file to rwfs root
2908 int value = (cmd - 1) & 0x03; // map aa, bb, cc => 1, 2, 3
2909 std::ofstream restoreFile(restoreOpFname);
2910 if (!restoreFile)
2911 {
2912 return ipmi::responseUnspecifiedError();
2913 }
2914 restoreFile << value << "\n";
2915 break;
2916 }
2917 default:
2918 return ipmi::responseInvalidFieldRequest();
2919 }
2920
2921 constexpr uint8_t restorePending = 0;
2922 constexpr uint8_t restoreComplete = 1;
2923
2924 uint8_t restoreStatus = std::filesystem::exists(restoreOpFname)
2925 ? restorePending
2926 : restoreComplete;
2927 return ipmi::responseSuccess(restoreStatus);
2928}
2929
Chen Yugang39736d52019-07-12 16:24:33 +08002930ipmi::RspType<uint8_t> ipmiOEMGetNmiSource(void)
2931{
2932 uint8_t bmcSource;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002933 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002934
2935 try
2936 {
2937 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2938 std::string service =
2939 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
2940 Value variant =
2941 getDbusProperty(*dbus, service, oemNmiSourceObjPath,
2942 oemNmiSourceIntf, oemNmiBmcSourceObjPathProp);
2943
2944 switch (nmi::NMISource::convertBMCSourceSignalFromString(
2945 std::get<std::string>(variant)))
2946 {
2947 case nmi::NMISource::BMCSourceSignal::None:
2948 bmcSource = static_cast<uint8_t>(NmiSource::none);
2949 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002950 case nmi::NMISource::BMCSourceSignal::FrontPanelButton:
2951 bmcSource = static_cast<uint8_t>(NmiSource::frontPanelButton);
Chen Yugang39736d52019-07-12 16:24:33 +08002952 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002953 case nmi::NMISource::BMCSourceSignal::Watchdog:
2954 bmcSource = static_cast<uint8_t>(NmiSource::watchdog);
Chen Yugang39736d52019-07-12 16:24:33 +08002955 break;
2956 case nmi::NMISource::BMCSourceSignal::ChassisCmd:
2957 bmcSource = static_cast<uint8_t>(NmiSource::chassisCmd);
2958 break;
2959 case nmi::NMISource::BMCSourceSignal::MemoryError:
2960 bmcSource = static_cast<uint8_t>(NmiSource::memoryError);
2961 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002962 case nmi::NMISource::BMCSourceSignal::PciBusError:
2963 bmcSource = static_cast<uint8_t>(NmiSource::pciBusError);
Chen Yugang39736d52019-07-12 16:24:33 +08002964 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002965 case nmi::NMISource::BMCSourceSignal::PCH:
2966 bmcSource = static_cast<uint8_t>(NmiSource::pch);
Chen Yugang39736d52019-07-12 16:24:33 +08002967 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002968 case nmi::NMISource::BMCSourceSignal::Chipset:
2969 bmcSource = static_cast<uint8_t>(NmiSource::chipset);
Chen Yugang39736d52019-07-12 16:24:33 +08002970 break;
2971 default:
2972 phosphor::logging::log<phosphor::logging::level::ERR>(
2973 "NMI source: invalid property!",
2974 phosphor::logging::entry(
2975 "PROP=%s", std::get<std::string>(variant).c_str()));
2976 return ipmi::responseResponseError();
2977 }
2978 }
2979 catch (sdbusplus::exception::SdBusError& e)
2980 {
2981 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
2982 return ipmi::responseResponseError();
2983 }
2984
2985 return ipmi::responseSuccess(bmcSource);
2986}
2987
2988ipmi::RspType<> ipmiOEMSetNmiSource(uint8_t sourceId)
2989{
Chen Yugang97cf96e2019-11-01 08:55:11 +08002990 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002991
2992 nmi::NMISource::BMCSourceSignal bmcSourceSignal =
2993 nmi::NMISource::BMCSourceSignal::None;
2994
2995 switch (NmiSource(sourceId))
2996 {
2997 case NmiSource::none:
2998 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::None;
2999 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003000 case NmiSource::frontPanelButton:
3001 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::FrontPanelButton;
Chen Yugang39736d52019-07-12 16:24:33 +08003002 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003003 case NmiSource::watchdog:
3004 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Watchdog;
Chen Yugang39736d52019-07-12 16:24:33 +08003005 break;
3006 case NmiSource::chassisCmd:
3007 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::ChassisCmd;
3008 break;
3009 case NmiSource::memoryError:
3010 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::MemoryError;
3011 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003012 case NmiSource::pciBusError:
3013 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PciBusError;
Chen Yugang39736d52019-07-12 16:24:33 +08003014 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003015 case NmiSource::pch:
3016 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PCH;
Chen Yugang39736d52019-07-12 16:24:33 +08003017 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003018 case NmiSource::chipset:
3019 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Chipset;
Chen Yugang39736d52019-07-12 16:24:33 +08003020 break;
3021 default:
3022 phosphor::logging::log<phosphor::logging::level::ERR>(
3023 "NMI source: invalid property!");
3024 return ipmi::responseResponseError();
3025 }
3026
3027 try
3028 {
3029 // keep NMI signal source
3030 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3031 std::string service =
3032 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
Chen Yugang97cf96e2019-11-01 08:55:11 +08003033 setDbusProperty(*dbus, service, oemNmiSourceObjPath, oemNmiSourceIntf,
3034 oemNmiBmcSourceObjPathProp,
3035 nmi::convertForMessage(bmcSourceSignal));
Chen Yugang99be6332019-08-09 16:20:48 +08003036 // set Enabled property to inform NMI source handling
3037 // to trigger a NMI_OUT BSOD.
3038 // if it's triggered by NMI source property changed,
3039 // NMI_OUT BSOD could be missed if the same source occurs twice in a row
3040 if (bmcSourceSignal != nmi::NMISource::BMCSourceSignal::None)
3041 {
3042 setDbusProperty(*dbus, service, oemNmiSourceObjPath,
3043 oemNmiSourceIntf, oemNmiEnabledObjPathProp,
3044 static_cast<bool>(true));
3045 }
Chen Yugang39736d52019-07-12 16:24:33 +08003046 }
3047 catch (sdbusplus::exception_t& e)
3048 {
3049 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3050 return ipmi::responseResponseError();
3051 }
3052
3053 return ipmi::responseSuccess();
3054}
3055
James Feist63efafa2019-07-24 12:39:21 -07003056namespace dimmOffset
3057{
3058constexpr const char* dimmPower = "DimmPower";
3059constexpr const char* staticCltt = "StaticCltt";
3060constexpr const char* offsetPath = "/xyz/openbmc_project/Inventory/Item/Dimm";
3061constexpr const char* offsetInterface =
3062 "xyz.openbmc_project.Inventory.Item.Dimm.Offset";
3063constexpr const char* property = "DimmOffset";
3064
3065}; // namespace dimmOffset
3066
3067ipmi::RspType<>
3068 ipmiOEMSetDimmOffset(uint8_t type,
3069 const std::vector<std::tuple<uint8_t, uint8_t>>& data)
3070{
3071 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3072 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3073 {
3074 return ipmi::responseInvalidFieldRequest();
3075 }
3076
3077 if (data.empty())
3078 {
3079 return ipmi::responseInvalidFieldRequest();
3080 }
3081 nlohmann::json json;
3082
3083 std::ifstream jsonStream(dimmOffsetFile);
3084 if (jsonStream.good())
3085 {
3086 json = nlohmann::json::parse(jsonStream, nullptr, false);
3087 if (json.is_discarded())
3088 {
3089 json = nlohmann::json();
3090 }
3091 jsonStream.close();
3092 }
3093
3094 std::string typeName;
3095 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3096 {
3097 typeName = dimmOffset::dimmPower;
3098 }
3099 else
3100 {
3101 typeName = dimmOffset::staticCltt;
3102 }
3103
3104 nlohmann::json& field = json[typeName];
3105
3106 for (const auto& [index, value] : data)
3107 {
3108 field[index] = value;
3109 }
3110
3111 for (nlohmann::json& val : field)
3112 {
3113 if (val == nullptr)
3114 {
3115 val = static_cast<uint8_t>(0);
3116 }
3117 }
3118
3119 std::ofstream output(dimmOffsetFile);
3120 if (!output.good())
3121 {
3122 std::cerr << "Error writing json file\n";
3123 return ipmi::responseResponseError();
3124 }
3125
3126 output << json.dump(4);
3127
3128 if (type == static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3129 {
3130 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
3131
3132 std::variant<std::vector<uint8_t>> offsets =
3133 field.get<std::vector<uint8_t>>();
3134 auto call = bus->new_method_call(
3135 settingsBusName, dimmOffset::offsetPath, PROP_INTF, "Set");
3136 call.append(dimmOffset::offsetInterface, dimmOffset::property, offsets);
3137 try
3138 {
3139 bus->call(call);
3140 }
3141 catch (sdbusplus::exception_t& e)
3142 {
3143 phosphor::logging::log<phosphor::logging::level::ERR>(
3144 "ipmiOEMSetDimmOffset: can't set dimm offsets!",
3145 phosphor::logging::entry("ERR=%s", e.what()));
3146 return ipmi::responseResponseError();
3147 }
3148 }
3149
3150 return ipmi::responseSuccess();
3151}
3152
3153ipmi::RspType<uint8_t> ipmiOEMGetDimmOffset(uint8_t type, uint8_t index)
3154{
3155
3156 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3157 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3158 {
3159 return ipmi::responseInvalidFieldRequest();
3160 }
3161
3162 std::ifstream jsonStream(dimmOffsetFile);
3163
3164 auto json = nlohmann::json::parse(jsonStream, nullptr, false);
3165 if (json.is_discarded())
3166 {
3167 std::cerr << "File error in " << dimmOffsetFile << "\n";
3168 return ipmi::responseResponseError();
3169 }
3170
3171 std::string typeName;
3172 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3173 {
3174 typeName = dimmOffset::dimmPower;
3175 }
3176 else
3177 {
3178 typeName = dimmOffset::staticCltt;
3179 }
3180
3181 auto it = json.find(typeName);
3182 if (it == json.end())
3183 {
3184 return ipmi::responseInvalidFieldRequest();
3185 }
3186
3187 if (it->size() <= index)
3188 {
3189 return ipmi::responseInvalidFieldRequest();
3190 }
3191
3192 uint8_t resp = it->at(index).get<uint8_t>();
3193 return ipmi::responseSuccess(resp);
3194}
3195
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003196namespace boot_options
3197{
3198
3199using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
3200using IpmiValue = uint8_t;
3201constexpr auto ipmiDefault = 0;
3202
3203std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
3204 {0x01, Source::Sources::Network},
3205 {0x02, Source::Sources::Disk},
3206 {0x05, Source::Sources::ExternalMedia},
3207 {0x0f, Source::Sources::RemovableMedia},
3208 {ipmiDefault, Source::Sources::Default}};
3209
3210std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003211 {0x06, Mode::Modes::Setup}, {ipmiDefault, Mode::Modes::Regular}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003212
3213std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
3214 {Source::Sources::Network, 0x01},
3215 {Source::Sources::Disk, 0x02},
3216 {Source::Sources::ExternalMedia, 0x05},
3217 {Source::Sources::RemovableMedia, 0x0f},
3218 {Source::Sources::Default, ipmiDefault}};
3219
3220std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003221 {Mode::Modes::Setup, 0x06}, {Mode::Modes::Regular, ipmiDefault}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003222
3223static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
3224static constexpr auto bootSourceIntf =
3225 "xyz.openbmc_project.Control.Boot.Source";
3226static constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
3227static constexpr auto persistentObjPath =
3228 "/xyz/openbmc_project/control/host0/boot";
3229static constexpr auto oneTimePath =
3230 "/xyz/openbmc_project/control/host0/boot/one_time";
3231static constexpr auto bootSourceProp = "BootSource";
3232static constexpr auto bootModeProp = "BootMode";
3233static constexpr auto oneTimeBootEnableProp = "Enabled";
3234static constexpr auto httpBootMode =
3235 "xyz.openbmc_project.Control.Boot.Source.Sources.Http";
3236
3237enum class BootOptionParameter : size_t
3238{
3239 setInProgress = 0x0,
3240 bootFlags = 0x5,
3241};
3242static constexpr uint8_t setComplete = 0x0;
3243static constexpr uint8_t setInProgress = 0x1;
3244static uint8_t transferStatus = setComplete;
3245static constexpr uint8_t setParmVersion = 0x01;
3246static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
3247static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
3248static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
3249static constexpr uint8_t httpBoot = 0xd;
3250static constexpr uint8_t bootSourceMask = 0x3c;
3251
3252} // namespace boot_options
3253
3254ipmi::RspType<uint8_t, // version
3255 uint8_t, // param
3256 uint8_t, // data0, dependent on parameter
3257 std::optional<uint8_t> // data1, dependent on parameter
3258 >
3259 ipmiOemGetEfiBootOptions(uint8_t parameter, uint8_t set, uint8_t block)
3260{
3261 using namespace boot_options;
3262 uint8_t bootOption = 0;
3263
3264 if (parameter == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3265 {
3266 return ipmi::responseSuccess(setParmVersion, parameter, transferStatus,
3267 std::nullopt);
3268 }
3269
3270 if (parameter != static_cast<uint8_t>(BootOptionParameter::bootFlags))
3271 {
3272 phosphor::logging::log<phosphor::logging::level::ERR>(
3273 "Unsupported parameter");
3274 return ipmi::responseResponseError();
3275 }
3276
3277 try
3278 {
3279 auto oneTimeEnabled = false;
3280 // read one time Enabled property
3281 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3282 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3283 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3284 enabledIntf, oneTimeBootEnableProp);
3285 oneTimeEnabled = std::get<bool>(variant);
3286
3287 // get BootSource and BootMode properties
3288 // according to oneTimeEnable
3289 auto bootObjPath = oneTimePath;
3290 if (oneTimeEnabled == false)
3291 {
3292 bootObjPath = persistentObjPath;
3293 }
3294
3295 service = getService(*dbus, bootModeIntf, bootObjPath);
3296 variant = getDbusProperty(*dbus, service, bootObjPath, bootModeIntf,
3297 bootModeProp);
3298
3299 auto bootMode =
3300 Mode::convertModesFromString(std::get<std::string>(variant));
3301
3302 service = getService(*dbus, bootSourceIntf, bootObjPath);
3303 variant = getDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3304 bootSourceProp);
3305
3306 if (std::get<std::string>(variant) == httpBootMode)
3307 {
3308 bootOption = httpBoot;
3309 }
3310 else
3311 {
3312 auto bootSource = Source::convertSourcesFromString(
3313 std::get<std::string>(variant));
3314 bootOption = sourceDbusToIpmi.at(bootSource);
3315 if (Source::Sources::Default == bootSource)
3316 {
3317 bootOption = modeDbusToIpmi.at(bootMode);
3318 }
3319 }
3320
3321 uint8_t oneTime = oneTimeEnabled ? setParmBootFlagsValidOneTime
3322 : setParmBootFlagsValidPermanent;
3323 bootOption <<= 2; // shift for responseconstexpr
3324 return ipmi::responseSuccess(setParmVersion, parameter, oneTime,
3325 bootOption);
3326 }
3327 catch (sdbusplus::exception_t& e)
3328 {
3329 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3330 return ipmi::responseResponseError();
3331 }
3332}
3333
3334ipmi::RspType<> ipmiOemSetEfiBootOptions(uint8_t bootFlag, uint8_t bootParam,
3335 std::optional<uint8_t> bootOption)
3336{
3337 using namespace boot_options;
3338 auto oneTimeEnabled = false;
3339
3340 if (bootFlag == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3341 {
3342 if (bootOption)
3343 {
3344 return ipmi::responseReqDataLenInvalid();
3345 }
3346
3347 if (transferStatus == setInProgress)
3348 {
3349 phosphor::logging::log<phosphor::logging::level::ERR>(
3350 "boot option set in progress!");
3351 return ipmi::responseResponseError();
3352 }
3353
3354 transferStatus = bootParam;
3355 return ipmi::responseSuccess();
3356 }
3357
3358 if (bootFlag != (uint8_t)BootOptionParameter::bootFlags)
3359 {
3360 phosphor::logging::log<phosphor::logging::level::ERR>(
3361 "Unsupported parameter");
3362 return ipmi::responseResponseError();
3363 }
3364
3365 if (!bootOption)
3366 {
3367 return ipmi::responseReqDataLenInvalid();
3368 }
3369
3370 if (((bootOption.value() & bootSourceMask) >> 2) !=
3371 httpBoot) // not http boot, exit
3372 {
3373 phosphor::logging::log<phosphor::logging::level::ERR>(
3374 "wrong boot option parameter!");
3375 return ipmi::responseParmOutOfRange();
3376 }
3377
3378 try
3379 {
3380 bool permanent = (bootParam & setParmBootFlagsPermanent) ==
3381 setParmBootFlagsPermanent;
3382
3383 // read one time Enabled property
3384 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3385 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3386 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3387 enabledIntf, oneTimeBootEnableProp);
3388 oneTimeEnabled = std::get<bool>(variant);
3389
3390 /*
3391 * Check if the current boot setting is onetime or permanent, if the
3392 * request in the command is otherwise, then set the "Enabled"
3393 * property in one_time object path to 'True' to indicate onetime
3394 * and 'False' to indicate permanent.
3395 *
3396 * Once the onetime/permanent setting is applied, then the bootMode
3397 * and bootSource is updated for the corresponding object.
3398 */
3399 if (permanent == oneTimeEnabled)
3400 {
3401 setDbusProperty(*dbus, service, oneTimePath, enabledIntf,
3402 oneTimeBootEnableProp, !permanent);
3403 }
3404
3405 // set BootSource and BootMode properties
3406 // according to oneTimeEnable or persistent
3407 auto bootObjPath = oneTimePath;
3408 if (oneTimeEnabled == false)
3409 {
3410 bootObjPath = persistentObjPath;
3411 }
3412 std::string bootMode =
3413 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
3414 std::string bootSource = httpBootMode;
3415
3416 service = getService(*dbus, bootModeIntf, bootObjPath);
3417 setDbusProperty(*dbus, service, bootObjPath, bootModeIntf, bootModeProp,
3418 bootMode);
3419
3420 service = getService(*dbus, bootSourceIntf, bootObjPath);
3421 setDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3422 bootSourceProp, bootSource);
3423 }
3424 catch (sdbusplus::exception_t& e)
3425 {
3426 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3427 return ipmi::responseResponseError();
3428 }
3429
3430 return ipmi::responseSuccess();
3431}
3432
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003433using BasicVariantType =
3434 std::variant<std::vector<std::string>, std::vector<uint64_t>, std::string,
3435 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
3436 uint16_t, uint8_t, bool>;
3437using PropertyMapType =
3438 boost::container::flat_map<std::string, BasicVariantType>;
3439static constexpr const std::array<const char*, 1> psuPresenceTypes = {
3440 "xyz.openbmc_project.Configuration.PSUPresence"};
3441int getPSUAddress(ipmi::Context::ptr ctx, uint8_t& bus,
3442 std::vector<uint64_t>& addrTable)
3443{
3444 boost::system::error_code ec;
3445 GetSubTreeType subtree = ctx->bus->yield_method_call<GetSubTreeType>(
3446 ctx->yield, ec, "xyz.openbmc_project.ObjectMapper",
3447 "/xyz/openbmc_project/object_mapper",
3448 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3449 "/xyz/openbmc_project/inventory/system", 3, psuPresenceTypes);
3450 if (ec)
3451 {
3452 phosphor::logging::log<phosphor::logging::level::ERR>(
3453 "Failed to set dbus property to cold redundancy");
3454 return -1;
3455 }
3456 for (const auto& object : subtree)
3457 {
3458 std::string pathName = object.first;
3459 for (const auto& serviceIface : object.second)
3460 {
3461 std::string serviceName = serviceIface.first;
3462
3463 ec.clear();
3464 PropertyMapType propMap =
3465 ctx->bus->yield_method_call<PropertyMapType>(
3466 ctx->yield, ec, serviceName, pathName,
3467 "org.freedesktop.DBus.Properties", "GetAll",
3468 "xyz.openbmc_project.Configuration.PSUPresence");
3469 if (ec)
3470 {
3471 phosphor::logging::log<phosphor::logging::level::ERR>(
3472 "Failed to set dbus property to cold redundancy");
3473 return -1;
3474 }
3475 auto psuBus = std::get_if<uint64_t>(&propMap["Bus"]);
3476 auto psuAddress =
3477 std::get_if<std::vector<uint64_t>>(&propMap["Address"]);
3478
3479 if (psuBus == nullptr || psuAddress == nullptr)
3480 {
3481 std::cerr << "error finding necessary "
3482 "entry in configuration\n";
3483 return -1;
3484 }
3485 bus = static_cast<uint8_t>(*psuBus);
3486 addrTable = *psuAddress;
3487 return 0;
3488 }
3489 }
3490 return -1;
3491}
3492
3493static const constexpr uint8_t addrOffset = 8;
3494static const constexpr uint8_t psuRevision = 0xd9;
3495static const constexpr uint8_t defaultPSUBus = 7;
3496// Second Minor, Primary Minor, Major
3497static const constexpr size_t verLen = 3;
3498ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPSUVersion(ipmi::Context::ptr ctx)
3499{
3500 uint8_t bus = defaultPSUBus;
3501 std::vector<uint64_t> addrTable;
3502 std::vector<uint8_t> result;
3503 if (getPSUAddress(ctx, bus, addrTable))
3504 {
3505 std::cerr << "Failed to get PSU bus and address\n";
3506 return ipmi::responseResponseError();
3507 }
3508
3509 for (const auto& slaveAddr : addrTable)
3510 {
3511 std::vector<uint8_t> writeData = {psuRevision};
3512 std::vector<uint8_t> readBuf(verLen);
3513 uint8_t addr = static_cast<uint8_t>(slaveAddr) + addrOffset;
3514 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
3515
3516 auto retI2C = ipmi::i2cWriteRead(i2cBus, addr, writeData, readBuf);
3517 if (retI2C != ipmi::ccSuccess)
3518 {
3519 for (size_t idx = 0; idx < verLen; idx++)
3520 {
3521 result.emplace_back(0x00);
3522 }
3523 }
3524 else
3525 {
3526 for (const uint8_t& data : readBuf)
3527 {
3528 result.emplace_back(data);
3529 }
3530 }
3531 }
3532
3533 return ipmi::responseSuccess(result);
3534}
3535
AppaRao Puli28972062019-11-11 02:04:45 +05303536/** @brief implements the maximum size of
3537 * bridgeable messages used between KCS and
3538 * IPMB interfacesget security mode command.
3539 *
3540 * @returns IPMI completion code with following data
3541 * - KCS Buffer Size (In multiples of four bytes)
3542 * - IPMB Buffer Size (In multiples of four bytes)
3543 **/
3544ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetBufferSize()
3545{
3546 // for now this is hard coded; really this number is dependent on
3547 // the BMC kcs driver as well as the host kcs driver....
3548 // we can't know the latter.
3549 uint8_t kcsMaxBufferSize = 63 / 4;
3550 uint8_t ipmbMaxBufferSize = 128 / 4;
3551
3552 return ipmi::responseSuccess(kcsMaxBufferSize, ipmbMaxBufferSize);
3553}
3554
Jason M. Bills64796042018-10-03 16:51:55 -07003555static void registerOEMFunctions(void)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003556{
3557 phosphor::logging::log<phosphor::logging::level::INFO>(
3558 "Registering OEM commands");
Vernon Mauery98bbf692019-09-16 11:14:59 -07003559 ipmiPrintAndRegister(intel::netFnGeneral,
3560 intel::general::cmdGetChassisIdentifier, NULL,
3561 ipmiOEMGetChassisIdentifier,
3562 PRIVILEGE_USER); // get chassis identifier
3563
3564 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetSystemGUID,
3565 NULL, ipmiOEMSetSystemGUID,
3566 PRIVILEGE_ADMIN); // set system guid
Jason M. Billsb02bf092019-08-15 13:01:56 -07003567
3568 // <Disable BMC System Reset Action>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003569 registerHandler(prioOemBase, intel::netFnGeneral,
3570 intel::general::cmdDisableBMCSystemReset, Privilege::Admin,
3571 ipmiOEMDisableBMCSystemReset);
3572
Jason M. Billsb02bf092019-08-15 13:01:56 -07003573 // <Get BMC Reset Disables>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003574 registerHandler(prioOemBase, intel::netFnGeneral,
3575 intel::general::cmdGetBMCResetDisables, Privilege::Admin,
3576 ipmiOEMGetBMCResetDisables);
Jason M. Billsb02bf092019-08-15 13:01:56 -07003577
Vernon Mauery98bbf692019-09-16 11:14:59 -07003578 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetBIOSID,
3579 NULL, ipmiOEMSetBIOSID, PRIVILEGE_ADMIN);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003580
Chen Yugang7a04f3a2019-10-08 11:12:35 +08003581 registerHandler(prioOemBase, intel::netFnGeneral,
3582 intel::general::cmdGetOEMDeviceInfo, Privilege::User,
3583 ipmiOEMGetDeviceInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003584
Vernon Mauery98bbf692019-09-16 11:14:59 -07003585 ipmiPrintAndRegister(intel::netFnGeneral,
3586 intel::general::cmdGetAICSlotFRUIDSlotPosRecords, NULL,
3587 ipmiOEMGetAICFRU, PRIVILEGE_USER);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303588
Vernon Mauery98bbf692019-09-16 11:14:59 -07003589 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3590 intel::general::cmdSendEmbeddedFWUpdStatus,
3591 Privilege::Operator, ipmiOEMSendEmbeddedFwUpdStatus);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303592
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +05303593 registerHandler(prioOpenBmcBase, intel::netFnApp, intel::app::cmdSlotIpmb,
3594 Privilege::Admin, ipmiOEMSlotIpmb);
3595
Vernon Mauery98bbf692019-09-16 11:14:59 -07003596 ipmiPrintAndRegister(intel::netFnGeneral,
3597 intel::general::cmdSetPowerRestoreDelay, NULL,
3598 ipmiOEMSetPowerRestoreDelay, PRIVILEGE_OPERATOR);
3599
3600 ipmiPrintAndRegister(intel::netFnGeneral,
3601 intel::general::cmdGetPowerRestoreDelay, NULL,
3602 ipmiOEMGetPowerRestoreDelay, PRIVILEGE_USER);
3603
3604 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3605 intel::general::cmdSetOEMUser2Activation,
3606 Privilege::Callback, ipmiOEMSetUser2Activation);
3607
3608 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3609 intel::general::cmdSetSpecialUserPassword,
3610 Privilege::Callback, ipmiOEMSetSpecialUserPassword);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05303611
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003612 // <Get Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003613 registerHandler(prioOemBase, intel::netFnGeneral,
3614 intel::general::cmdGetProcessorErrConfig, Privilege::User,
3615 ipmiOEMGetProcessorErrConfig);
3616
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003617 // <Set Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003618 registerHandler(prioOemBase, intel::netFnGeneral,
3619 intel::general::cmdSetProcessorErrConfig, Privilege::Admin,
3620 ipmiOEMSetProcessorErrConfig);
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003621
Vernon Mauery98bbf692019-09-16 11:14:59 -07003622 ipmiPrintAndRegister(intel::netFnGeneral,
3623 intel::general::cmdSetShutdownPolicy, NULL,
3624 ipmiOEMSetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003625
Vernon Mauery98bbf692019-09-16 11:14:59 -07003626 ipmiPrintAndRegister(intel::netFnGeneral,
3627 intel::general::cmdGetShutdownPolicy, NULL,
3628 ipmiOEMGetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003629
anil kumar appanaf945eee2019-09-25 23:29:11 +00003630 registerHandler(prioOemBase, intel::netFnGeneral,
3631 intel::general::cmdSetFanConfig, Privilege::User,
3632 ipmiOEMSetFanConfig);
James Feist91244a62019-02-19 15:04:54 -08003633
Vernon Mauery98bbf692019-09-16 11:14:59 -07003634 registerHandler(prioOemBase, intel::netFnGeneral,
3635 intel::general::cmdGetFanConfig, Privilege::User,
3636 ipmiOEMGetFanConfig);
James Feist5f957ca2019-03-14 15:33:55 -07003637
Vernon Mauery98bbf692019-09-16 11:14:59 -07003638 registerHandler(prioOemBase, intel::netFnGeneral,
3639 intel::general::cmdGetFanSpeedOffset, Privilege::User,
3640 ipmiOEMGetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003641
Vernon Mauery98bbf692019-09-16 11:14:59 -07003642 registerHandler(prioOemBase, intel::netFnGeneral,
3643 intel::general::cmdSetFanSpeedOffset, Privilege::User,
3644 ipmiOEMSetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003645
Vernon Mauery98bbf692019-09-16 11:14:59 -07003646 registerHandler(prioOemBase, intel::netFnGeneral,
3647 intel::general::cmdSetFscParameter, Privilege::User,
3648 ipmiOEMSetFscParameter);
James Feist5f957ca2019-03-14 15:33:55 -07003649
Vernon Mauery98bbf692019-09-16 11:14:59 -07003650 registerHandler(prioOemBase, intel::netFnGeneral,
3651 intel::general::cmdGetFscParameter, Privilege::User,
3652 ipmiOEMGetFscParameter);
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05303653
Vernon Mauery98bbf692019-09-16 11:14:59 -07003654 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3655 intel::general::cmdReadBaseBoardProductId, Privilege::Admin,
3656 ipmiOEMReadBoardProductId);
Chen Yugang39736d52019-07-12 16:24:33 +08003657
Vernon Mauery98bbf692019-09-16 11:14:59 -07003658 registerHandler(prioOemBase, intel::netFnGeneral,
3659 intel::general::cmdGetNmiStatus, Privilege::User,
3660 ipmiOEMGetNmiSource);
Chen Yugang39736d52019-07-12 16:24:33 +08003661
Vernon Mauery98bbf692019-09-16 11:14:59 -07003662 registerHandler(prioOemBase, intel::netFnGeneral,
3663 intel::general::cmdSetNmiStatus, Privilege::Operator,
3664 ipmiOEMSetNmiSource);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003665
Vernon Mauery98bbf692019-09-16 11:14:59 -07003666 registerHandler(prioOemBase, intel::netFnGeneral,
3667 intel::general::cmdGetEfiBootOptions, Privilege::User,
3668 ipmiOemGetEfiBootOptions);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003669
Vernon Mauery98bbf692019-09-16 11:14:59 -07003670 registerHandler(prioOemBase, intel::netFnGeneral,
3671 intel::general::cmdSetEfiBootOptions, Privilege::Operator,
3672 ipmiOemSetEfiBootOptions);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303673
Vernon Mauery98bbf692019-09-16 11:14:59 -07003674 registerHandler(prioOemBase, intel::netFnGeneral,
3675 intel::general::cmdGetSecurityMode, Privilege::User,
3676 ipmiGetSecurityMode);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303677
Vernon Mauery98bbf692019-09-16 11:14:59 -07003678 registerHandler(prioOemBase, intel::netFnGeneral,
3679 intel::general::cmdSetSecurityMode, Privilege::Admin,
3680 ipmiSetSecurityMode);
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003681
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00003682 registerHandler(prioOemBase, intel::netFnGeneral,
3683 intel::general::cmdGetLEDStatus, Privilege::Admin,
3684 ipmiOEMGetLEDStatus);
Cheng C Yang773703a2019-08-15 09:41:11 +08003685
Vernon Mauery98bbf692019-09-16 11:14:59 -07003686 ipmiPrintAndRegister(ipmi::intel::netFnPlatform,
3687 ipmi::intel::platform::cmdCfgHostSerialPortSpeed, NULL,
3688 ipmiOEMCfgHostSerialPortSpeed, PRIVILEGE_ADMIN);
3689
3690 registerHandler(prioOemBase, intel::netFnGeneral,
3691 intel::general::cmdSetFaultIndication, Privilege::Operator,
3692 ipmiOEMSetFaultIndication);
3693
3694 registerHandler(prioOemBase, intel::netFnGeneral,
3695 intel::general::cmdSetColdRedundancyConfig, Privilege::User,
3696 ipmiOEMSetCRConfig);
3697
3698 registerHandler(prioOemBase, intel::netFnGeneral,
3699 intel::general::cmdGetColdRedundancyConfig, Privilege::User,
3700 ipmiOEMGetCRConfig);
3701
3702 registerHandler(prioOemBase, intel::netFnGeneral,
3703 intel::general::cmdRestoreConfiguration, Privilege::Admin,
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003704 ipmiRestoreConfiguration);
James Feist63efafa2019-07-24 12:39:21 -07003705
Vernon Mauery98bbf692019-09-16 11:14:59 -07003706 registerHandler(prioOemBase, intel::netFnGeneral,
3707 intel::general::cmdSetDimmOffset, Privilege::Operator,
3708 ipmiOEMSetDimmOffset);
James Feist63efafa2019-07-24 12:39:21 -07003709
Vernon Mauery98bbf692019-09-16 11:14:59 -07003710 registerHandler(prioOemBase, intel::netFnGeneral,
3711 intel::general::cmdGetDimmOffset, Privilege::Operator,
3712 ipmiOEMGetDimmOffset);
Chen Yugangca12a7b2019-09-03 18:11:44 +08003713
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003714 registerHandler(prioOemBase, intel::netFnGeneral,
3715 intel::general::cmdGetPSUVersion, Privilege::User,
3716 ipmiOEMGetPSUVersion);
AppaRao Puli28972062019-11-11 02:04:45 +05303717
3718 registerHandler(prioOemBase, intel::netFnGeneral,
3719 intel::general::cmdGetBufferSize, Privilege::User,
3720 ipmiOEMGetBufferSize);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003721}
3722
3723} // namespace ipmi