blob: 02c28837ac0c3ad71e46c197f6e9b2f27e4a8a1e [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 +0800161ipmi_ret_t ipmiOEMWildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
162 ipmi_request_t request, ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700163 ipmi_data_len_t dataLen, ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800164{
Jason M. Bills64796042018-10-03 16:51:55 -0700165 printCommand(+netfn, +cmd);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800166 // Status code.
167 ipmi_ret_t rc = IPMI_CC_INVALID;
Jason M. Bills64796042018-10-03 16:51:55 -0700168 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800169 return rc;
170}
171
172// Returns the Chassis Identifier (serial #)
173ipmi_ret_t ipmiOEMGetChassisIdentifier(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
174 ipmi_request_t request,
175 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700176 ipmi_data_len_t dataLen,
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800177 ipmi_context_t context)
178{
179 std::string serial;
Jason M. Bills64796042018-10-03 16:51:55 -0700180 if (*dataLen != 0) // invalid request if there are extra parameters
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800181 {
Jason M. Bills64796042018-10-03 16:51:55 -0700182 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800183 return IPMI_CC_REQ_DATA_LEN_INVALID;
184 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700185 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
186 if (getChassisSerialNumber(*dbus, serial) == 0)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800187 {
Jason M. Bills64796042018-10-03 16:51:55 -0700188 *dataLen = serial.size(); // length will never exceed response length
189 // as it is checked in getChassisSerialNumber
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800190 char* resp = static_cast<char*>(response);
Jason M. Bills64796042018-10-03 16:51:55 -0700191 serial.copy(resp, *dataLen);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800192 return IPMI_CC_OK;
193 }
Jason M. Bills64796042018-10-03 16:51:55 -0700194 *dataLen = 0;
195 return IPMI_CC_RESPONSE_ERROR;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800196}
197
198ipmi_ret_t ipmiOEMSetSystemGUID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
199 ipmi_request_t request,
200 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700201 ipmi_data_len_t dataLen, ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800202{
203 static constexpr size_t safeBufferLength = 50;
204 char buf[safeBufferLength] = {0};
205 GUIDData* Data = reinterpret_cast<GUIDData*>(request);
206
Jason M. Bills64796042018-10-03 16:51:55 -0700207 if (*dataLen != sizeof(GUIDData)) // 16bytes
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800208 {
Jason M. Bills64796042018-10-03 16:51:55 -0700209 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800210 return IPMI_CC_REQ_DATA_LEN_INVALID;
211 }
212
Jason M. Bills64796042018-10-03 16:51:55 -0700213 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800214
215 snprintf(
216 buf, safeBufferLength,
217 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
218 Data->timeLow4, Data->timeLow3, Data->timeLow2, Data->timeLow1,
219 Data->timeMid2, Data->timeMid1, Data->timeHigh2, Data->timeHigh1,
220 Data->clock2, Data->clock1, Data->node6, Data->node5, Data->node4,
221 Data->node3, Data->node2, Data->node1);
222 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
223 std::string guid = buf;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800224
225 std::string objpath = "/xyz/openbmc_project/control/host0/systemGUID";
226 std::string intf = "xyz.openbmc_project.Common.UUID";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700227 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
228 std::string service = getService(*dbus, intf, objpath);
229 setDbusProperty(*dbus, service, objpath, intf, "UUID", guid);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800230 return IPMI_CC_OK;
231}
232
Jason M. Billsb02bf092019-08-15 13:01:56 -0700233ipmi::RspType<> ipmiOEMDisableBMCSystemReset(bool disableResetOnSMI,
234 uint7_t reserved1)
235{
236 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
237
238 try
239 {
240 auto service =
241 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
242 ipmi::setDbusProperty(*busp, service, bmcResetDisablesPath,
243 bmcResetDisablesIntf, "ResetOnSMI",
244 !disableResetOnSMI);
245 }
246 catch (std::exception& e)
247 {
248 phosphor::logging::log<phosphor::logging::level::ERR>(
249 "Failed to set BMC reset disables",
250 phosphor::logging::entry("EXCEPTION=%s", e.what()));
251 return ipmi::responseUnspecifiedError();
252 }
253
254 return ipmi::responseSuccess();
255}
256
257ipmi::RspType<bool, // disableResetOnSMI
258 uint7_t // reserved
259 >
260 ipmiOEMGetBMCResetDisables()
261{
262 bool disableResetOnSMI = true;
263
264 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
265 try
266 {
267 auto service =
268 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
269 Value variant =
270 ipmi::getDbusProperty(*busp, service, bmcResetDisablesPath,
271 bmcResetDisablesIntf, "ResetOnSMI");
272 disableResetOnSMI = !std::get<bool>(variant);
273 }
274 catch (std::exception& e)
275 {
276 phosphor::logging::log<phosphor::logging::level::ERR>(
277 "Failed to get BMC reset disables",
278 phosphor::logging::entry("EXCEPTION=%s", e.what()));
279 return ipmi::responseUnspecifiedError();
280 }
281
282 return ipmi::responseSuccess(disableResetOnSMI, 0);
283}
284
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800285ipmi_ret_t ipmiOEMSetBIOSID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
286 ipmi_request_t request, ipmi_response_t response,
287 ipmi_data_len_t dataLen, ipmi_context_t context)
288{
289 DeviceInfo* data = reinterpret_cast<DeviceInfo*>(request);
290
Jason M. Bills64796042018-10-03 16:51:55 -0700291 if ((*dataLen < 2) || (*dataLen != (1 + data->biosIDLength)))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800292 {
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800293 *dataLen = 0;
294 return IPMI_CC_REQ_DATA_LEN_INVALID;
295 }
Jason M. Bills64796042018-10-03 16:51:55 -0700296 std::string idString((char*)data->biosId, data->biosIDLength);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800297
Vernon Mauery15419dd2019-05-24 09:40:30 -0700298 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800299 std::string service = getService(*dbus, biosVersionIntf, biosObjPath);
300 setDbusProperty(*dbus, service, biosObjPath, biosVersionIntf,
301 biosVersionProp, idString);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800302 uint8_t* bytesWritten = static_cast<uint8_t*>(response);
303 *bytesWritten =
Jason M. Bills64796042018-10-03 16:51:55 -0700304 data->biosIDLength; // how many bytes are written into storage
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800305 *dataLen = 1;
306 return IPMI_CC_OK;
307}
308
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800309bool getSwVerInfo(uint8_t& bmcMajor, uint8_t& bmcMinor, uint8_t& meMajor,
310 uint8_t& meMinor)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800311{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800312 // step 1 : get BMC Major and Minor numbers from its DBUS property
313 std::optional<MetaRevision> rev{};
314 try
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800315 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800316 std::string version = getActiveSoftwareVersionInfo();
317 rev = convertIntelVersion(version);
318 }
319 catch (const std::exception& e)
320 {
321 return false;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800322 }
323
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800324 if (rev.has_value())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800325 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800326 MetaRevision revision = rev.value();
327 bmcMajor = revision.major;
328
329 revision.minor = (revision.minor > 99 ? 99 : revision.minor);
330 bmcMinor = revision.minor % 10 + (revision.minor / 10) * 16;
331 }
332
333 // step 2 : get ME Major and Minor numbers from its DBUS property
334 try
335 {
336 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
337 std::string service =
338 getService(*dbus, "xyz.openbmc_project.Software.Version",
AppaRao Pulid46cb422020-01-21 18:40:21 +0530339 "/xyz/openbmc_project/software/me");
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800340 Value variant =
AppaRao Pulid46cb422020-01-21 18:40:21 +0530341 getDbusProperty(*dbus, service, "/xyz/openbmc_project/software/me",
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800342 "xyz.openbmc_project.Software.Version", "Version");
343
AppaRao Pulid46cb422020-01-21 18:40:21 +0530344 std::string& meVersion = std::get<std::string>(variant);
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800345
346 // get ME major number
347 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?).(\\d+?).(\\d+?)");
348 constexpr size_t matchedPhosphor = 6;
349 std::smatch results;
AppaRao Pulid46cb422020-01-21 18:40:21 +0530350 if (std::regex_match(meVersion, results, pattern1))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800351 {
352 if (results.size() == matchedPhosphor)
353 {
354 meMajor = static_cast<uint8_t>(std::stoi(results[1]));
355 meMinor = static_cast<uint8_t>(std::stoi(results[2]));
356 }
357 }
358 }
359 catch (sdbusplus::exception::SdBusError& e)
360 {
361 return false;
362 }
363 return true;
364}
365
366ipmi::RspType<
367 std::variant<std::string,
368 std::tuple<uint8_t, std::array<uint8_t, 2>,
369 std::array<uint8_t, 2>, std::array<uint8_t, 2>,
370 std::array<uint8_t, 2>, std::array<uint8_t, 2>>,
371 std::tuple<uint8_t, std::array<uint8_t, 2>>>>
AppaRao Pulid46cb422020-01-21 18:40:21 +0530372 ipmiOEMGetDeviceInfo(uint8_t entityType, std::optional<uint8_t> countToRead,
373 std::optional<uint8_t> offset)
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800374{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800375 if (entityType > static_cast<uint8_t>(OEMDevEntityType::sdrVer))
376 {
377 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800378 }
379
380 // handle OEM command items
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800381 switch (OEMDevEntityType(entityType))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800382 {
383 case OEMDevEntityType::biosId:
384 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530385 // Byte 2&3, Only used with selecting BIOS
386 if (!countToRead || !offset)
387 {
388 return ipmi::responseReqDataLenInvalid();
389 }
390
Vernon Mauery15419dd2019-05-24 09:40:30 -0700391 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800392 std::string service =
393 getService(*dbus, biosVersionIntf, biosObjPath);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800394 try
395 {
Yong Li2742b852019-12-16 14:55:11 +0800396 Value variant =
397 getDbusProperty(*dbus, service, biosObjPath,
398 biosVersionIntf, biosVersionProp);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700399 std::string& idString = std::get<std::string>(variant);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530400 if (*offset >= idString.size())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800401 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800402 return ipmi::responseParmOutOfRange();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800403 }
Jason M. Bills64796042018-10-03 16:51:55 -0700404 size_t length = 0;
AppaRao Pulid46cb422020-01-21 18:40:21 +0530405 if (*countToRead > (idString.size() - *offset))
Jason M. Bills64796042018-10-03 16:51:55 -0700406 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530407 length = idString.size() - *offset;
Jason M. Bills64796042018-10-03 16:51:55 -0700408 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800409 else
410 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530411 length = *countToRead;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800412 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800413
414 std::string readBuf = {0};
415 readBuf.resize(length);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530416 std::copy_n(idString.begin() + *offset, length,
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800417 (readBuf.begin()));
418 return ipmi::responseSuccess(readBuf);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800419 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700420 catch (std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800421 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800422 return ipmi::responseUnspecifiedError();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800423 }
424 }
425 break;
426
427 case OEMDevEntityType::devVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800428 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530429 // Byte 2&3, Only used with selecting BIOS
430 if (countToRead || offset)
431 {
432 return ipmi::responseReqDataLenInvalid();
433 }
434
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800435 constexpr const size_t verLen = 2;
436 constexpr const size_t verTotalLen = 10;
437 std::array<uint8_t, verLen> bmcBuf = {0xff, 0xff};
438 std::array<uint8_t, verLen> hsc0Buf = {0xff, 0xff};
439 std::array<uint8_t, verLen> hsc1Buf = {0xff, 0xff};
440 std::array<uint8_t, verLen> meBuf = {0xff, 0xff};
441 std::array<uint8_t, verLen> hsc2Buf = {0xff, 0xff};
442 // data0/1: BMC version number; data6/7: ME version number
443 // the others: HSC0/1/2 version number, not avaible.
444 if (true != getSwVerInfo(bmcBuf[0], bmcBuf[1], meBuf[0], meBuf[1]))
445 {
446 return ipmi::responseUnspecifiedError();
447 }
448 return ipmi::responseSuccess(
449 std::tuple<
450 uint8_t, std::array<uint8_t, verLen>,
451 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>,
452 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>>{
453 verTotalLen, bmcBuf, hsc0Buf, hsc1Buf, meBuf, hsc2Buf});
454 }
455 break;
456
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800457 case OEMDevEntityType::sdrVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800458 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530459 // Byte 2&3, Only used with selecting BIOS
460 if (countToRead || offset)
461 {
462 return ipmi::responseReqDataLenInvalid();
463 }
464
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800465 constexpr const size_t sdrLen = 2;
466 std::array<uint8_t, sdrLen> readBuf = {0x01, 0x0};
467 return ipmi::responseSuccess(
468 std::tuple<uint8_t, std::array<uint8_t, sdrLen>>{sdrLen,
469 readBuf});
470 }
471 break;
472
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800473 default:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800474 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800475 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800476}
477
478ipmi_ret_t ipmiOEMGetAICFRU(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
479 ipmi_request_t request, ipmi_response_t response,
480 ipmi_data_len_t dataLen, ipmi_context_t context)
481{
482 if (*dataLen != 0)
483 {
Jason M. Bills64796042018-10-03 16:51:55 -0700484 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800485 return IPMI_CC_REQ_DATA_LEN_INVALID;
486 }
487
488 *dataLen = 1;
489 uint8_t* res = reinterpret_cast<uint8_t*>(response);
490 // temporary fix. We don't support AIC FRU now. Just tell BIOS that no
491 // AIC is available so that BIOS will not timeout repeatly which leads to
492 // slow booting.
493 *res = 0; // Byte1=Count of SlotPosition/FruID records.
494 return IPMI_CC_OK;
495}
496
Jason M. Bills64796042018-10-03 16:51:55 -0700497ipmi_ret_t ipmiOEMGetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
498 ipmi_request_t request,
499 ipmi_response_t response,
500 ipmi_data_len_t dataLen,
501 ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800502{
Jason M. Bills64796042018-10-03 16:51:55 -0700503 GetPowerRestoreDelayRes* resp =
504 reinterpret_cast<GetPowerRestoreDelayRes*>(response);
505
506 if (*dataLen != 0)
507 {
508 *dataLen = 0;
509 return IPMI_CC_REQ_DATA_LEN_INVALID;
510 }
511
Vernon Mauery15419dd2019-05-24 09:40:30 -0700512 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700513 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700514 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
Jason M. Bills64796042018-10-03 16:51:55 -0700515 Value variant =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700516 getDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700517 powerRestoreDelayIntf, powerRestoreDelayProp);
518
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700519 uint16_t delay = std::get<uint16_t>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700520 resp->byteLSB = delay;
521 resp->byteMSB = delay >> 8;
522
523 *dataLen = sizeof(GetPowerRestoreDelayRes);
524
525 return IPMI_CC_OK;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800526}
527
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800528static uint8_t bcdToDec(uint8_t val)
529{
530 return ((val / 16 * 10) + (val % 16));
531}
532
533// Allows an update utility or system BIOS to send the status of an embedded
534// firmware update attempt to the BMC. After received, BMC will create a logging
535// record.
536ipmi::RspType<> ipmiOEMSendEmbeddedFwUpdStatus(uint8_t status, uint8_t target,
537 uint8_t majorRevision,
538 uint8_t minorRevision,
539 uint32_t auxInfo)
540{
541 std::string firmware;
Jason M. Billsdc249272019-04-03 09:58:40 -0700542 int instance = (target & targetInstanceMask) >> targetInstanceShift;
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800543 target = (target & selEvtTargetMask) >> selEvtTargetShift;
544
545 /* make sure the status is 0, 1, or 2 as per the spec */
546 if (status > 2)
547 {
548 return ipmi::response(ipmi::ccInvalidFieldRequest);
549 }
Jason M. Billsdc249272019-04-03 09:58:40 -0700550 /* make sure the target is 0, 1, 2, or 4 as per the spec */
551 if (target > 4 || target == 3)
552 {
553 return ipmi::response(ipmi::ccInvalidFieldRequest);
554 }
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800555 /*orignal OEM command is to record OEM SEL.
556 But openbmc does not support OEM SEL, so we redirect it to redfish event
557 logging. */
558 std::string buildInfo;
559 std::string action;
560 switch (FWUpdateTarget(target))
561 {
562 case FWUpdateTarget::targetBMC:
563 firmware = "BMC";
Jason M. Billsdc249272019-04-03 09:58:40 -0700564 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800565 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
566 " BuildID: " + std::to_string(auxInfo);
567 buildInfo += std::to_string(auxInfo);
568 break;
569 case FWUpdateTarget::targetBIOS:
570 firmware = "BIOS";
571 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700572 "major: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800573 std::to_string(bcdToDec(majorRevision)) + // BCD encoded
574 " minor: " +
575 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
576 " ReleaseNumber: " + // ASCII encoded
577 std::to_string(static_cast<uint8_t>(auxInfo >> 0) - '0') +
578 std::to_string(static_cast<uint8_t>(auxInfo >> 8) - '0') +
579 std::to_string(static_cast<uint8_t>(auxInfo >> 16) - '0') +
580 std::to_string(static_cast<uint8_t>(auxInfo >> 24) - '0');
581 break;
582 case FWUpdateTarget::targetME:
583 firmware = "ME";
584 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700585 "major: " + std::to_string(majorRevision) + " minor1: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800586 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
587 " minor2: " +
588 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 0))) +
589 " build1: " +
590 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 8))) +
591 " build2: " +
592 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 16)));
593 break;
594 case FWUpdateTarget::targetOEMEWS:
595 firmware = "EWS";
Jason M. Billsdc249272019-04-03 09:58:40 -0700596 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800597 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
598 " BuildID: " + std::to_string(auxInfo);
599 break;
600 }
601
Jason M. Billsdc249272019-04-03 09:58:40 -0700602 static const std::string openBMCMessageRegistryVersion("0.1");
603 std::string redfishMsgID = "OpenBMC." + openBMCMessageRegistryVersion;
604
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800605 switch (status)
606 {
607 case 0x0:
608 action = "update started";
Jason M. Billsdc249272019-04-03 09:58:40 -0700609 redfishMsgID += ".FirmwareUpdateStarted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800610 break;
611 case 0x1:
612 action = "update completed successfully";
Jason M. Billsdc249272019-04-03 09:58:40 -0700613 redfishMsgID += ".FirmwareUpdateCompleted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800614 break;
615 case 0x2:
616 action = "update failure";
Jason M. Billsdc249272019-04-03 09:58:40 -0700617 redfishMsgID += ".FirmwareUpdateFailed";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800618 break;
619 default:
620 action = "unknown";
621 break;
622 }
623
Jason M. Billsdc249272019-04-03 09:58:40 -0700624 std::string firmwareInstanceStr =
625 firmware + " instance: " + std::to_string(instance);
626 std::string message("[firmware update] " + firmwareInstanceStr +
627 " status: <" + action + "> " + buildInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800628
629 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", LOG_INFO,
Jason M. Billsdc249272019-04-03 09:58:40 -0700630 "REDFISH_MESSAGE_ID=%s", redfishMsgID.c_str(),
631 "REDFISH_MESSAGE_ARGS=%s,%s", firmwareInstanceStr.c_str(),
632 buildInfo.c_str(), NULL);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800633 return ipmi::responseSuccess();
634}
635
Jason M. Bills64796042018-10-03 16:51:55 -0700636ipmi_ret_t ipmiOEMSetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
637 ipmi_request_t request,
638 ipmi_response_t response,
639 ipmi_data_len_t dataLen,
640 ipmi_context_t context)
641{
642 SetPowerRestoreDelayReq* data =
643 reinterpret_cast<SetPowerRestoreDelayReq*>(request);
644 uint16_t delay = 0;
645
646 if (*dataLen != sizeof(SetPowerRestoreDelayReq))
647 {
648 *dataLen = 0;
649 return IPMI_CC_REQ_DATA_LEN_INVALID;
650 }
651 delay = data->byteMSB;
652 delay = (delay << 8) | data->byteLSB;
Vernon Mauery15419dd2019-05-24 09:40:30 -0700653 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700654 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700655 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
656 setDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700657 powerRestoreDelayIntf, powerRestoreDelayProp, delay);
658 *dataLen = 0;
659
660 return IPMI_CC_OK;
661}
662
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700663static bool cpuPresent(const std::string& cpuName)
Jason M. Bills64796042018-10-03 16:51:55 -0700664{
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700665 static constexpr const char* cpuPresencePathPrefix =
666 "/xyz/openbmc_project/inventory/system/chassis/motherboard/";
667 static constexpr const char* cpuPresenceIntf =
668 "xyz.openbmc_project.Inventory.Item";
669 std::string cpuPresencePath = cpuPresencePathPrefix + cpuName;
670 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
671 try
Jason M. Bills64796042018-10-03 16:51:55 -0700672 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700673 auto service =
674 ipmi::getService(*busp, cpuPresenceIntf, cpuPresencePath);
675
676 ipmi::Value result = ipmi::getDbusProperty(
677 *busp, service, cpuPresencePath, cpuPresenceIntf, "Present");
678 return std::get<bool>(result);
679 }
680 catch (const std::exception& e)
681 {
682 phosphor::logging::log<phosphor::logging::level::INFO>(
683 "Cannot find processor presence",
684 phosphor::logging::entry("NAME=%s", cpuName.c_str()));
685 return false;
686 }
687}
688
689ipmi::RspType<bool, // CATERR Reset Enabled
690 bool, // ERR2 Reset Enabled
691 uint6_t, // reserved
692 uint8_t, // reserved, returns 0x3F
693 uint6_t, // CPU1 CATERR Count
694 uint2_t, // CPU1 Status
695 uint6_t, // CPU2 CATERR Count
696 uint2_t, // CPU2 Status
697 uint6_t, // CPU3 CATERR Count
698 uint2_t, // CPU3 Status
699 uint6_t, // CPU4 CATERR Count
700 uint2_t, // CPU4 Status
701 uint8_t // Crashdump Count
702 >
703 ipmiOEMGetProcessorErrConfig()
704{
705 bool resetOnCATERR = false;
706 bool resetOnERR2 = false;
707 uint6_t cpu1CATERRCount = 0;
708 uint6_t cpu2CATERRCount = 0;
709 uint6_t cpu3CATERRCount = 0;
710 uint6_t cpu4CATERRCount = 0;
711 uint8_t crashdumpCount = 0;
712 uint2_t cpu1Status =
713 cpuPresent("CPU_1") ? CPUStatus::enabled : CPUStatus::notPresent;
714 uint2_t cpu2Status =
715 cpuPresent("CPU_2") ? CPUStatus::enabled : CPUStatus::notPresent;
716 uint2_t cpu3Status =
717 cpuPresent("CPU_3") ? CPUStatus::enabled : CPUStatus::notPresent;
718 uint2_t cpu4Status =
719 cpuPresent("CPU_4") ? CPUStatus::enabled : CPUStatus::notPresent;
720
721 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
722 try
723 {
724 auto service = ipmi::getService(*busp, processorErrConfigIntf,
725 processorErrConfigObjPath);
726
727 ipmi::PropertyMap result = ipmi::getAllDbusProperties(
728 *busp, service, processorErrConfigObjPath, processorErrConfigIntf);
729 resetOnCATERR = std::get<bool>(result.at("ResetOnCATERR"));
730 resetOnERR2 = std::get<bool>(result.at("ResetOnERR2"));
731 cpu1CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU1"));
732 cpu2CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU2"));
733 cpu3CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU3"));
734 cpu4CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU4"));
735 crashdumpCount = std::get<uint8_t>(result.at("CrashdumpCount"));
736 }
737 catch (const std::exception& e)
738 {
739 phosphor::logging::log<phosphor::logging::level::ERR>(
740 "Failed to fetch processor error config",
741 phosphor::logging::entry("ERROR=%s", e.what()));
742 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700743 }
744
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700745 return ipmi::responseSuccess(resetOnCATERR, resetOnERR2, 0, 0x3F,
746 cpu1CATERRCount, cpu1Status, cpu2CATERRCount,
747 cpu2Status, cpu3CATERRCount, cpu3Status,
748 cpu4CATERRCount, cpu4Status, crashdumpCount);
749}
Jason M. Bills64796042018-10-03 16:51:55 -0700750
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700751ipmi::RspType<> ipmiOEMSetProcessorErrConfig(
752 bool resetOnCATERR, bool resetOnERR2, uint6_t reserved1, uint8_t reserved2,
753 std::optional<bool> clearCPUErrorCount,
754 std::optional<bool> clearCrashdumpCount, std::optional<uint6_t> reserved3)
755{
756 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700757
758 try
759 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700760 auto service = ipmi::getService(*busp, processorErrConfigIntf,
761 processorErrConfigObjPath);
762 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
763 processorErrConfigIntf, "ResetOnCATERR",
764 resetOnCATERR);
765 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
766 processorErrConfigIntf, "ResetOnERR2",
767 resetOnERR2);
768 if (clearCPUErrorCount.value_or(false))
769 {
770 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700771 processorErrConfigIntf, "ErrorCountCPU1",
772 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700773 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700774 processorErrConfigIntf, "ErrorCountCPU2",
775 static_cast<uint8_t>(0));
776 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
777 processorErrConfigIntf, "ErrorCountCPU3",
778 static_cast<uint8_t>(0));
779 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
780 processorErrConfigIntf, "ErrorCountCPU4",
781 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700782 }
783 if (clearCrashdumpCount.value_or(false))
784 {
785 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700786 processorErrConfigIntf, "CrashdumpCount",
787 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700788 }
Jason M. Bills64796042018-10-03 16:51:55 -0700789 }
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700790 catch (std::exception& e)
Jason M. Bills64796042018-10-03 16:51:55 -0700791 {
Kuiying Wangbc546672018-11-23 15:41:05 +0800792 phosphor::logging::log<phosphor::logging::level::ERR>(
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700793 "Failed to set processor error config",
794 phosphor::logging::entry("EXCEPTION=%s", e.what()));
795 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700796 }
797
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700798 return ipmi::responseSuccess();
Jason M. Bills64796042018-10-03 16:51:55 -0700799}
800
Yong Li703922d2018-11-06 13:25:31 +0800801ipmi_ret_t ipmiOEMGetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
802 ipmi_request_t request,
803 ipmi_response_t response,
804 ipmi_data_len_t dataLen,
805 ipmi_context_t context)
806{
807 GetOEMShutdownPolicyRes* resp =
808 reinterpret_cast<GetOEMShutdownPolicyRes*>(response);
809
810 if (*dataLen != 0)
811 {
812 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wang45f04982018-12-26 09:23:08 +0800813 "oem_get_shutdown_policy: invalid input len!");
Yong Li703922d2018-11-06 13:25:31 +0800814 *dataLen = 0;
815 return IPMI_CC_REQ_DATA_LEN_INVALID;
816 }
817
818 *dataLen = 0;
819
820 try
821 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700822 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800823 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700824 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
825 Value variant = getDbusProperty(
826 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
827 oemShutdownPolicyObjPathProp);
Yong Li0669d192019-05-06 14:01:46 +0800828
829 if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
830 convertPolicyFromString(std::get<std::string>(variant)) ==
831 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
832 NoShutdownOnOCOT)
833 {
834 resp->policy = 0;
835 }
836 else if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
837 convertPolicyFromString(std::get<std::string>(variant)) ==
838 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
839 Policy::ShutdownOnOCOT)
840 {
841 resp->policy = 1;
842 }
843 else
844 {
845 phosphor::logging::log<phosphor::logging::level::ERR>(
846 "oem_set_shutdown_policy: invalid property!",
847 phosphor::logging::entry(
848 "PROP=%s", std::get<std::string>(variant).c_str()));
849 return IPMI_CC_UNSPECIFIED_ERROR;
850 }
Yong Li703922d2018-11-06 13:25:31 +0800851 // TODO needs to check if it is multi-node products,
852 // policy is only supported on node 3/4
853 resp->policySupport = shutdownPolicySupported;
854 }
855 catch (sdbusplus::exception_t& e)
856 {
857 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
858 return IPMI_CC_UNSPECIFIED_ERROR;
859 }
860
861 *dataLen = sizeof(GetOEMShutdownPolicyRes);
862 return IPMI_CC_OK;
863}
864
865ipmi_ret_t ipmiOEMSetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
866 ipmi_request_t request,
867 ipmi_response_t response,
868 ipmi_data_len_t dataLen,
869 ipmi_context_t context)
870{
871 uint8_t* req = reinterpret_cast<uint8_t*>(request);
Yong Li0669d192019-05-06 14:01:46 +0800872 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy policy =
873 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
874 NoShutdownOnOCOT;
Yong Li703922d2018-11-06 13:25:31 +0800875
876 // TODO needs to check if it is multi-node products,
877 // policy is only supported on node 3/4
878 if (*dataLen != 1)
879 {
880 phosphor::logging::log<phosphor::logging::level::ERR>(
881 "oem_set_shutdown_policy: invalid input len!");
882 *dataLen = 0;
883 return IPMI_CC_REQ_DATA_LEN_INVALID;
884 }
885
886 *dataLen = 0;
887 if ((*req != noShutdownOnOCOT) && (*req != shutdownOnOCOT))
888 {
889 phosphor::logging::log<phosphor::logging::level::ERR>(
890 "oem_set_shutdown_policy: invalid input!");
891 return IPMI_CC_INVALID_FIELD_REQUEST;
892 }
893
Yong Li0669d192019-05-06 14:01:46 +0800894 if (*req == noShutdownOnOCOT)
895 {
896 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
897 Policy::NoShutdownOnOCOT;
898 }
899 else
900 {
901 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
902 Policy::ShutdownOnOCOT;
903 }
904
Yong Li703922d2018-11-06 13:25:31 +0800905 try
906 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700907 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800908 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700909 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
Yong Li0669d192019-05-06 14:01:46 +0800910 setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -0700911 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
Yong Li0669d192019-05-06 14:01:46 +0800912 oemShutdownPolicyObjPathProp,
913 sdbusplus::com::intel::Control::server::convertForMessage(policy));
Yong Li703922d2018-11-06 13:25:31 +0800914 }
915 catch (sdbusplus::exception_t& e)
916 {
917 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
918 return IPMI_CC_UNSPECIFIED_ERROR;
919 }
920
921 return IPMI_CC_OK;
922}
923
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530924/** @brief implementation for check the DHCP or not in IPv4
925 * @param[in] Channel - Channel number
926 * @returns true or false.
927 */
928static bool isDHCPEnabled(uint8_t Channel)
929{
930 try
931 {
932 auto ethdevice = getChannelName(Channel);
933 if (ethdevice.empty())
934 {
935 return false;
936 }
937 auto ethIP = ethdevice + "/ipv4";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700938 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530939 auto ethernetObj =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700940 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
941 auto value = getDbusProperty(*dbus, networkService, ethernetObj.first,
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530942 networkIPIntf, "Origin");
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700943 if (std::get<std::string>(value) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530944 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
945 {
946 return true;
947 }
948 else
949 {
950 return false;
951 }
952 }
953 catch (sdbusplus::exception_t& e)
954 {
955 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
956 return true;
957 }
958}
959
960/** @brief implementes for check the DHCP or not in IPv6
961 * @param[in] Channel - Channel number
962 * @returns true or false.
963 */
964static bool isDHCPIPv6Enabled(uint8_t Channel)
965{
966
967 try
968 {
969 auto ethdevice = getChannelName(Channel);
970 if (ethdevice.empty())
971 {
972 return false;
973 }
974 auto ethIP = ethdevice + "/ipv6";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700975 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530976 auto objectInfo =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700977 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
978 auto properties = getAllDbusProperties(*dbus, objectInfo.second,
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530979 objectInfo.first, networkIPIntf);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700980 if (std::get<std::string>(properties["Origin"]) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530981 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
982 {
983 return true;
984 }
985 else
986 {
987 return false;
988 }
989 }
990 catch (sdbusplus::exception_t& e)
991 {
992 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
993 return true;
994 }
995}
996
997/** @brief implementes the creating of default new user
998 * @param[in] userName - new username in 16 bytes.
999 * @param[in] userPassword - new password in 20 bytes
1000 * @returns ipmi completion code.
1001 */
1002ipmi::RspType<> ipmiOEMSetUser2Activation(
1003 std::array<uint8_t, ipmi::ipmiMaxUserName>& userName,
1004 std::array<uint8_t, ipmi::maxIpmi20PasswordSize>& userPassword)
1005{
1006 bool userState = false;
1007 // Check for System Interface not exist and LAN should be static
1008 for (uint8_t channel = 0; channel < maxIpmiChannels; channel++)
1009 {
1010 ChannelInfo chInfo;
1011 try
1012 {
1013 getChannelInfo(channel, chInfo);
1014 }
1015 catch (sdbusplus::exception_t& e)
1016 {
1017 phosphor::logging::log<phosphor::logging::level::ERR>(
1018 "ipmiOEMSetUser2Activation: Failed to get Channel Info",
1019 phosphor::logging::entry("MSG: %s", e.description()));
1020 return ipmi::response(ipmi::ccUnspecifiedError);
1021 }
1022 if (chInfo.mediumType ==
1023 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1024 {
1025 phosphor::logging::log<phosphor::logging::level::ERR>(
1026 "ipmiOEMSetUser2Activation: system interface exist .");
1027 return ipmi::response(ipmi::ccCommandNotAvailable);
1028 }
1029 else
1030 {
1031
1032 if (chInfo.mediumType ==
1033 static_cast<uint8_t>(EChannelMediumType::lan8032))
1034 {
1035 if (isDHCPIPv6Enabled(channel) || isDHCPEnabled(channel))
1036 {
1037 phosphor::logging::log<phosphor::logging::level::ERR>(
1038 "ipmiOEMSetUser2Activation: DHCP enabled .");
1039 return ipmi::response(ipmi::ccCommandNotAvailable);
1040 }
1041 }
1042 }
1043 }
1044 uint8_t maxChUsers = 0, enabledUsers = 0, fixedUsers = 0;
1045 if (ipmi::ccSuccess ==
1046 ipmiUserGetAllCounts(maxChUsers, enabledUsers, fixedUsers))
1047 {
1048 if (enabledUsers > 1)
1049 {
1050 phosphor::logging::log<phosphor::logging::level::ERR>(
1051 "ipmiOEMSetUser2Activation: more than one user is enabled.");
1052 return ipmi::response(ipmi::ccCommandNotAvailable);
1053 }
1054 // Check the user 2 is enabled or not
1055 ipmiUserCheckEnabled(ipmiDefaultUserId, userState);
1056 if (userState == true)
1057 {
1058 phosphor::logging::log<phosphor::logging::level::ERR>(
1059 "ipmiOEMSetUser2Activation: user 2 already enabled .");
1060 return ipmi::response(ipmi::ccCommandNotAvailable);
1061 }
1062 }
1063 else
1064 {
1065 return ipmi::response(ipmi::ccUnspecifiedError);
1066 }
1067
1068#if BYTE_ORDER == LITTLE_ENDIAN
1069 PrivAccess privAccess = {PRIVILEGE_ADMIN, true, true, true, 0};
1070#endif
1071#if BYTE_ORDER == BIG_ENDIAN
1072 PrivAccess privAccess = {0, true, true, true, PRIVILEGE_ADMIN};
1073#endif
1074
1075 if (ipmi::ccSuccess ==
1076 ipmiUserSetUserName(ipmiDefaultUserId,
1077 reinterpret_cast<const char*>(userName.data())))
1078 {
1079 if (ipmi::ccSuccess ==
1080 ipmiUserSetUserPassword(
1081 ipmiDefaultUserId,
1082 reinterpret_cast<const char*>(userPassword.data())))
1083 {
1084 if (ipmi::ccSuccess ==
1085 ipmiUserSetPrivilegeAccess(
1086 ipmiDefaultUserId,
1087 static_cast<uint8_t>(ipmi::EChannelID::chanLan1),
1088 privAccess, true))
1089 {
1090 phosphor::logging::log<phosphor::logging::level::INFO>(
1091 "ipmiOEMSetUser2Activation: user created successfully ");
1092 return ipmi::responseSuccess();
1093 }
1094 }
1095 // we need to delete the default user id which added in this command as
1096 // password / priv setting is failed.
1097 ipmiUserSetUserName(ipmiDefaultUserId, "");
1098 phosphor::logging::log<phosphor::logging::level::ERR>(
1099 "ipmiOEMSetUser2Activation: password / priv setting is failed.");
1100 }
1101 else
1102 {
1103 phosphor::logging::log<phosphor::logging::level::ERR>(
1104 "ipmiOEMSetUser2Activation: Setting username failed.");
1105 }
1106
1107 return ipmi::response(ipmi::ccCommandNotAvailable);
1108}
1109
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301110/** @brief implementes executing the linux command
1111 * @param[in] linux command
1112 * @returns status
1113 */
1114
1115static uint8_t executeCmd(const char* path)
1116{
1117 boost::process::child execProg(path);
1118 execProg.wait();
1119
1120 int retCode = execProg.exit_code();
1121 if (retCode)
1122 {
1123 return ipmi::ccUnspecifiedError;
1124 }
1125 return ipmi::ccSuccess;
1126}
1127
1128/** @brief implementes ASD Security event logging
1129 * @param[in] Event message string
1130 * @param[in] Event Severity
1131 * @returns status
1132 */
1133
1134static void atScaleDebugEventlog(std::string msg, int severity)
1135{
1136 std::string eventStr = "OpenBMC.0.1." + msg;
1137 sd_journal_send("MESSAGE=Security Event: %s", eventStr.c_str(),
1138 "PRIORITY=%i", severity, "REDFISH_MESSAGE_ID=%s",
1139 eventStr.c_str(), NULL);
1140}
1141
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301142/** @brief implementes setting password for special user
1143 * @param[in] specialUserIndex
1144 * @param[in] userPassword - new password in 20 bytes
1145 * @returns ipmi completion code.
1146 */
1147ipmi::RspType<> ipmiOEMSetSpecialUserPassword(ipmi::Context::ptr ctx,
1148 uint8_t specialUserIndex,
1149 std::vector<uint8_t> userPassword)
1150{
1151 ChannelInfo chInfo;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301152 ipmi_ret_t status = ipmi::ccSuccess;
1153
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301154 try
1155 {
1156 getChannelInfo(ctx->channel, chInfo);
1157 }
1158 catch (sdbusplus::exception_t& e)
1159 {
1160 phosphor::logging::log<phosphor::logging::level::ERR>(
1161 "ipmiOEMSetSpecialUserPassword: Failed to get Channel Info",
1162 phosphor::logging::entry("MSG: %s", e.description()));
1163 return ipmi::responseUnspecifiedError();
1164 }
1165 if (chInfo.mediumType !=
1166 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1167 {
1168 phosphor::logging::log<phosphor::logging::level::ERR>(
1169 "ipmiOEMSetSpecialUserPassword: Error - supported only in KCS "
1170 "interface");
1171 return ipmi::responseCommandNotAvailable();
1172 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301173
1174 // 0 for root user and 1 for AtScaleDebug is allowed
1175 if (specialUserIndex >
1176 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301177 {
1178 phosphor::logging::log<phosphor::logging::level::ERR>(
1179 "ipmiOEMSetSpecialUserPassword: Invalid user account");
1180 return ipmi::responseParmOutOfRange();
1181 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301182 if (userPassword.size() != 0)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301183 {
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301184 constexpr uint8_t minPasswordSizeRequired = 6;
1185 std::string passwd;
1186 if (userPassword.size() < minPasswordSizeRequired ||
1187 userPassword.size() > ipmi::maxIpmi20PasswordSize)
1188 {
1189 return ipmi::responseReqDataLenInvalid();
1190 }
1191 passwd.assign(reinterpret_cast<const char*>(userPassword.data()),
1192 userPassword.size());
1193 if (specialUserIndex ==
1194 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
1195 {
1196 status = ipmiSetSpecialUserPassword("asdbg", passwd);
1197
1198 atScaleDebugEventlog("AtScaleDebugSpecialUserEnabled", LOG_CRIT);
1199 }
1200 else
1201 {
1202 status = ipmiSetSpecialUserPassword("root", passwd);
1203 }
1204 return ipmi::response(status);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301205 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301206 else
1207 {
1208 if (specialUserIndex ==
1209 static_cast<uint8_t>(SpecialUserIndex::rootUser))
1210 {
1211 status = executeCmd("passwd -d root");
1212 }
1213 else
1214 {
1215
1216 status = executeCmd("passwd -d asdbg");
1217
1218 if (status == 0)
1219 {
1220 atScaleDebugEventlog("AtScaleDebugSpecialUserDisabled",
1221 LOG_INFO);
1222 }
1223 }
1224 return ipmi::response(status);
1225 }
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301226}
1227
Kuiying Wang45f04982018-12-26 09:23:08 +08001228namespace ledAction
1229{
1230using namespace sdbusplus::xyz::openbmc_project::Led::server;
1231std::map<Physical::Action, uint8_t> actionDbusToIpmi = {
jayaprakash Mutyala934ee9c2019-12-13 17:49:27 +00001232 {Physical::Action::Off, 0},
1233 {Physical::Action::On, 2},
1234 {Physical::Action::Blink, 1}};
Kuiying Wang45f04982018-12-26 09:23:08 +08001235
1236std::map<uint8_t, std::string> offsetObjPath = {
1237 {2, statusAmberObjPath}, {4, statusGreenObjPath}, {6, identifyLEDObjPath}};
1238
1239} // namespace ledAction
1240
1241int8_t getLEDState(sdbusplus::bus::bus& bus, const std::string& intf,
1242 const std::string& objPath, uint8_t& state)
1243{
1244 try
1245 {
1246 std::string service = getService(bus, intf, objPath);
1247 Value stateValue =
1248 getDbusProperty(bus, service, objPath, intf, "State");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001249 std::string strState = std::get<std::string>(stateValue);
Kuiying Wang45f04982018-12-26 09:23:08 +08001250 state = ledAction::actionDbusToIpmi.at(
1251 sdbusplus::xyz::openbmc_project::Led::server::Physical::
1252 convertActionFromString(strState));
1253 }
1254 catch (sdbusplus::exception::SdBusError& e)
1255 {
1256 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1257 return -1;
1258 }
1259 return 0;
1260}
1261
1262ipmi_ret_t ipmiOEMGetLEDStatus(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1263 ipmi_request_t request, ipmi_response_t response,
1264 ipmi_data_len_t dataLen, ipmi_context_t context)
1265{
1266 uint8_t* resp = reinterpret_cast<uint8_t*>(response);
1267 // LED Status
1268 //[1:0] = Reserved
1269 //[3:2] = Status(Amber)
1270 //[5:4] = Status(Green)
1271 //[7:6] = System Identify
1272 // Status definitions:
1273 // 00b = Off
1274 // 01b = Blink
1275 // 10b = On
1276 // 11b = invalid
1277 if (*dataLen != 0)
1278 {
1279 phosphor::logging::log<phosphor::logging::level::ERR>(
1280 "oem_get_led_status: invalid input len!");
1281 *dataLen = 0;
1282 return IPMI_CC_REQ_DATA_LEN_INVALID;
1283 }
1284
1285 phosphor::logging::log<phosphor::logging::level::DEBUG>("GET led status");
1286 *resp = 0;
1287 *dataLen = 0;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001288 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Kuiying Wang45f04982018-12-26 09:23:08 +08001289 for (auto it = ledAction::offsetObjPath.begin();
1290 it != ledAction::offsetObjPath.end(); ++it)
1291 {
1292 uint8_t state = 0;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001293 if (-1 == getLEDState(*dbus, ledIntf, it->second, state))
Kuiying Wang45f04982018-12-26 09:23:08 +08001294 {
1295 phosphor::logging::log<phosphor::logging::level::ERR>(
1296 "oem_get_led_status: fail to get ID LED status!");
1297 return IPMI_CC_UNSPECIFIED_ERROR;
1298 }
1299 *resp |= state << it->first;
1300 }
1301
1302 *dataLen = sizeof(*resp);
1303 return IPMI_CC_OK;
1304}
1305
Yong Li23737fe2019-02-19 08:49:55 +08001306ipmi_ret_t ipmiOEMCfgHostSerialPortSpeed(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1307 ipmi_request_t request,
1308 ipmi_response_t response,
1309 ipmi_data_len_t dataLen,
1310 ipmi_context_t context)
1311{
1312 CfgHostSerialReq* req = reinterpret_cast<CfgHostSerialReq*>(request);
1313 uint8_t* resp = reinterpret_cast<uint8_t*>(response);
1314
1315 if (*dataLen == 0)
1316 {
1317 phosphor::logging::log<phosphor::logging::level::ERR>(
1318 "CfgHostSerial: invalid input len!",
1319 phosphor::logging::entry("LEN=%d", *dataLen));
1320 return IPMI_CC_REQ_DATA_LEN_INVALID;
1321 }
1322
1323 switch (req->command)
1324 {
1325 case getHostSerialCfgCmd:
1326 {
1327 if (*dataLen != 1)
1328 {
1329 phosphor::logging::log<phosphor::logging::level::ERR>(
1330 "CfgHostSerial: invalid input len!");
1331 *dataLen = 0;
1332 return IPMI_CC_REQ_DATA_LEN_INVALID;
1333 }
1334
1335 *dataLen = 0;
1336
1337 boost::process::ipstream is;
1338 std::vector<std::string> data;
1339 std::string line;
1340 boost::process::child c1(fwGetEnvCmd, "-n", fwHostSerailCfgEnvName,
1341 boost::process::std_out > is);
1342
1343 while (c1.running() && std::getline(is, line) && !line.empty())
1344 {
1345 data.push_back(line);
1346 }
1347
1348 c1.wait();
1349 if (c1.exit_code())
1350 {
1351 phosphor::logging::log<phosphor::logging::level::ERR>(
1352 "CfgHostSerial:: error on execute",
1353 phosphor::logging::entry("EXECUTE=%s", fwSetEnvCmd));
1354 // Using the default value
1355 *resp = 0;
1356 }
1357 else
1358 {
1359 if (data.size() != 1)
1360 {
1361 phosphor::logging::log<phosphor::logging::level::ERR>(
1362 "CfgHostSerial:: error on read env");
1363 return IPMI_CC_UNSPECIFIED_ERROR;
1364 }
1365 try
1366 {
1367 unsigned long tmp = std::stoul(data[0]);
1368 if (tmp > std::numeric_limits<uint8_t>::max())
1369 {
1370 throw std::out_of_range("Out of range");
1371 }
1372 *resp = static_cast<uint8_t>(tmp);
1373 }
1374 catch (const std::invalid_argument& e)
1375 {
1376 phosphor::logging::log<phosphor::logging::level::ERR>(
1377 "invalid config ",
1378 phosphor::logging::entry("ERR=%s", e.what()));
1379 return IPMI_CC_UNSPECIFIED_ERROR;
1380 }
1381 catch (const std::out_of_range& e)
1382 {
1383 phosphor::logging::log<phosphor::logging::level::ERR>(
1384 "out_of_range config ",
1385 phosphor::logging::entry("ERR=%s", e.what()));
1386 return IPMI_CC_UNSPECIFIED_ERROR;
1387 }
1388 }
1389
1390 *dataLen = 1;
1391 break;
1392 }
1393 case setHostSerialCfgCmd:
1394 {
1395 if (*dataLen != sizeof(CfgHostSerialReq))
1396 {
1397 phosphor::logging::log<phosphor::logging::level::ERR>(
1398 "CfgHostSerial: invalid input len!");
1399 *dataLen = 0;
1400 return IPMI_CC_REQ_DATA_LEN_INVALID;
1401 }
1402
1403 *dataLen = 0;
1404
1405 if (req->parameter > HostSerialCfgParamMax)
1406 {
1407 phosphor::logging::log<phosphor::logging::level::ERR>(
1408 "CfgHostSerial: invalid input!");
1409 return IPMI_CC_INVALID_FIELD_REQUEST;
1410 }
1411
1412 boost::process::child c1(fwSetEnvCmd, fwHostSerailCfgEnvName,
1413 std::to_string(req->parameter));
1414
1415 c1.wait();
1416 if (c1.exit_code())
1417 {
1418 phosphor::logging::log<phosphor::logging::level::ERR>(
1419 "CfgHostSerial:: error on execute",
1420 phosphor::logging::entry("EXECUTE=%s", fwGetEnvCmd));
1421 return IPMI_CC_UNSPECIFIED_ERROR;
1422 }
1423 break;
1424 }
1425 default:
1426 phosphor::logging::log<phosphor::logging::level::ERR>(
1427 "CfgHostSerial: invalid input!");
1428 *dataLen = 0;
1429 return IPMI_CC_INVALID_FIELD_REQUEST;
1430 }
1431
1432 return IPMI_CC_OK;
1433}
1434
James Feist91244a62019-02-19 15:04:54 -08001435constexpr const char* thermalModeInterface =
1436 "xyz.openbmc_project.Control.ThermalMode";
1437constexpr const char* thermalModePath =
1438 "/xyz/openbmc_project/control/thermal_mode";
1439
1440bool getFanProfileInterface(
1441 sdbusplus::bus::bus& bus,
1442 boost::container::flat_map<
1443 std::string, std::variant<std::vector<std::string>, std::string>>& resp)
1444{
1445 auto call = bus.new_method_call(settingsBusName, thermalModePath, PROP_INTF,
1446 "GetAll");
1447 call.append(thermalModeInterface);
1448 try
1449 {
1450 auto data = bus.call(call);
1451 data.read(resp);
1452 }
1453 catch (sdbusplus::exception_t& e)
1454 {
1455 phosphor::logging::log<phosphor::logging::level::ERR>(
1456 "getFanProfileInterface: can't get thermal mode!",
1457 phosphor::logging::entry("ERR=%s", e.what()));
1458 return false;
1459 }
1460 return true;
1461}
1462
anil kumar appanaf945eee2019-09-25 23:29:11 +00001463/**@brief implements the OEM set fan config.
1464 * @param selectedFanProfile - fan profile to enable
1465 * @param reserved1
1466 * @param performanceMode - Performance/Acoustic mode
1467 * @param reserved2
1468 * @param setPerformanceMode - set Performance/Acoustic mode
1469 * @param setFanProfile - set fan profile
1470 *
1471 * @return IPMI completion code.
1472 **/
1473ipmi::RspType<> ipmiOEMSetFanConfig(uint8_t selectedFanProfile,
1474
1475 uint2_t reserved1, bool performanceMode,
1476 uint3_t reserved2, bool setPerformanceMode,
Joshi-Mansi619186d2020-01-27 19:16:03 +05301477 bool setFanProfile,
1478 std::optional<uint8_t> dimmGroupId,
1479 std::optional<uint32_t> dimmPresenceBitmap)
James Feist91244a62019-02-19 15:04:54 -08001480{
anil kumar appanaf945eee2019-09-25 23:29:11 +00001481 if (reserved1 || reserved2)
James Feist91244a62019-02-19 15:04:54 -08001482 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001483 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001484 }
Joshi-Mansi619186d2020-01-27 19:16:03 +05301485
1486 if (dimmGroupId)
1487 {
1488 if (*dimmGroupId >= maxCPUNum)
1489 {
1490 return ipmi::responseInvalidFieldRequest();
1491 }
1492 if (!cpuPresent("CPU_" + std::to_string(*dimmGroupId + 1)))
1493 {
1494 return ipmi::responseInvalidFieldRequest();
1495 }
1496 }
1497
James Feist91244a62019-02-19 15:04:54 -08001498 // todo: tell bios to only send first 2 bytes
James Feist91244a62019-02-19 15:04:54 -08001499 boost::container::flat_map<
1500 std::string, std::variant<std::vector<std::string>, std::string>>
1501 profileData;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001502 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1503 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001504 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001505 return ipmi::responseUnspecifiedError();
James Feist91244a62019-02-19 15:04:54 -08001506 }
1507
1508 std::vector<std::string>* supported =
1509 std::get_if<std::vector<std::string>>(&profileData["Supported"]);
1510 if (supported == nullptr)
1511 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001512 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001513 }
1514 std::string mode;
anil kumar appanaf945eee2019-09-25 23:29:11 +00001515 if (setPerformanceMode)
James Feist91244a62019-02-19 15:04:54 -08001516 {
James Feist91244a62019-02-19 15:04:54 -08001517 if (performanceMode)
1518 {
1519
1520 if (std::find(supported->begin(), supported->end(),
1521 "Performance") != supported->end())
1522 {
1523 mode = "Performance";
1524 }
1525 }
1526 else
1527 {
James Feist91244a62019-02-19 15:04:54 -08001528 if (std::find(supported->begin(), supported->end(), "Acoustic") !=
1529 supported->end())
1530 {
1531 mode = "Acoustic";
1532 }
1533 }
1534 if (mode.empty())
1535 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001536 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001537 }
anil kumar appanaf945eee2019-09-25 23:29:11 +00001538
1539 try
1540 {
1541 setDbusProperty(*dbus, settingsBusName, thermalModePath,
1542 thermalModeInterface, "Current", mode);
1543 }
1544 catch (sdbusplus::exception_t& e)
1545 {
1546 phosphor::logging::log<phosphor::logging::level::ERR>(
1547 "ipmiOEMSetFanConfig: can't set thermal mode!",
1548 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1549 return ipmi::responseResponseError();
1550 }
James Feist91244a62019-02-19 15:04:54 -08001551 }
1552
anil kumar appanaf945eee2019-09-25 23:29:11 +00001553 return ipmi::responseSuccess();
James Feist91244a62019-02-19 15:04:54 -08001554}
1555
James Feist5b693632019-07-09 09:06:09 -07001556ipmi::RspType<uint8_t, // profile support map
1557 uint8_t, // fan control profile enable
1558 uint8_t, // flags
1559 uint32_t // dimm presence bit map
1560 >
1561 ipmiOEMGetFanConfig(uint8_t dimmGroupId)
James Feist91244a62019-02-19 15:04:54 -08001562{
Joshi-Mansi36f05ce2020-01-14 14:29:34 +05301563 if (dimmGroupId >= maxCPUNum)
1564 {
1565 return ipmi::responseInvalidFieldRequest();
1566 }
1567
1568 bool cpuStatus = cpuPresent("CPU_" + std::to_string(dimmGroupId + 1));
1569
1570 if (!cpuStatus)
1571 {
1572 return ipmi::responseInvalidFieldRequest();
1573 }
1574
James Feist91244a62019-02-19 15:04:54 -08001575 boost::container::flat_map<
1576 std::string, std::variant<std::vector<std::string>, std::string>>
1577 profileData;
1578
Vernon Mauery15419dd2019-05-24 09:40:30 -07001579 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1580 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001581 {
James Feist5b693632019-07-09 09:06:09 -07001582 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001583 }
1584
1585 std::string* current = std::get_if<std::string>(&profileData["Current"]);
1586
1587 if (current == nullptr)
1588 {
1589 phosphor::logging::log<phosphor::logging::level::ERR>(
1590 "ipmiOEMGetFanConfig: can't get current mode!");
James Feist5b693632019-07-09 09:06:09 -07001591 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001592 }
1593 bool performance = (*current == "Performance");
1594
James Feist5b693632019-07-09 09:06:09 -07001595 uint8_t flags = 0;
James Feist91244a62019-02-19 15:04:54 -08001596 if (performance)
1597 {
James Feist5b693632019-07-09 09:06:09 -07001598 flags |= 1 << 2;
James Feist91244a62019-02-19 15:04:54 -08001599 }
1600
jayaprakash Mutyala4b1552d2020-02-11 12:07:29 +00001601 constexpr uint8_t fanControlDefaultProfile = 0x80;
1602 constexpr uint8_t fanControlProfileState = 0x00;
1603 constexpr uint32_t dimmPresenceBitmap = 0x00;
1604
1605 return ipmi::responseSuccess(fanControlDefaultProfile,
1606 fanControlProfileState, flags,
1607 dimmPresenceBitmap);
James Feist91244a62019-02-19 15:04:54 -08001608}
James Feist5f957ca2019-03-14 15:33:55 -07001609constexpr const char* cfmLimitSettingPath =
1610 "/xyz/openbmc_project/control/cfm_limit";
1611constexpr const char* cfmLimitIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistfaa4f222019-03-21 16:21:55 -07001612constexpr const size_t legacyExitAirSensorNumber = 0x2e;
James Feist09f6b602019-08-08 11:30:03 -07001613constexpr const size_t legacyPCHSensorNumber = 0x22;
1614constexpr const char* exitAirPathName = "Exit_Air";
1615constexpr const char* pchPathName = "SSB_Temp";
James Feistacc8a4e2019-04-02 14:23:57 -07001616constexpr const char* pidConfigurationIface =
1617 "xyz.openbmc_project.Configuration.Pid";
James Feistfaa4f222019-03-21 16:21:55 -07001618
James Feist09f6b602019-08-08 11:30:03 -07001619static std::string getConfigPath(const std::string& name)
James Feistfaa4f222019-03-21 16:21:55 -07001620{
Vernon Mauery15419dd2019-05-24 09:40:30 -07001621 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistfaa4f222019-03-21 16:21:55 -07001622 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001623 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1624 "/xyz/openbmc_project/object_mapper",
1625 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistfaa4f222019-03-21 16:21:55 -07001626
James Feistacc8a4e2019-04-02 14:23:57 -07001627 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
James Feistfaa4f222019-03-21 16:21:55 -07001628 std::string path;
1629 GetSubTreeType resp;
1630 try
1631 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001632 auto reply = dbus->call(method);
James Feistfaa4f222019-03-21 16:21:55 -07001633 reply.read(resp);
1634 }
1635 catch (sdbusplus::exception_t&)
1636 {
1637 phosphor::logging::log<phosphor::logging::level::ERR>(
1638 "ipmiOEMGetFscParameter: mapper error");
1639 };
James Feist09f6b602019-08-08 11:30:03 -07001640 auto config =
1641 std::find_if(resp.begin(), resp.end(), [&name](const auto& pair) {
1642 return pair.first.find(name) != std::string::npos;
1643 });
James Feistfaa4f222019-03-21 16:21:55 -07001644 if (config != resp.end())
1645 {
1646 path = std::move(config->first);
1647 }
1648 return path;
1649}
James Feist5f957ca2019-03-14 15:33:55 -07001650
James Feistacc8a4e2019-04-02 14:23:57 -07001651// flat map to make alphabetical
1652static boost::container::flat_map<std::string, PropertyMap> getPidConfigs()
1653{
1654 boost::container::flat_map<std::string, PropertyMap> ret;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001655 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001656 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001657 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1658 "/xyz/openbmc_project/object_mapper",
1659 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistacc8a4e2019-04-02 14:23:57 -07001660
1661 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
1662 GetSubTreeType resp;
1663
1664 try
1665 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001666 auto reply = dbus->call(method);
James Feistacc8a4e2019-04-02 14:23:57 -07001667 reply.read(resp);
1668 }
1669 catch (sdbusplus::exception_t&)
1670 {
1671 phosphor::logging::log<phosphor::logging::level::ERR>(
1672 "getFanConfigPaths: mapper error");
1673 };
1674 for (const auto& [path, objects] : resp)
1675 {
1676 if (objects.empty())
1677 {
1678 continue; // should be impossible
1679 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04001680
1681 try
1682 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001683 ret.emplace(path,
1684 getAllDbusProperties(*dbus, objects[0].first, path,
1685 pidConfigurationIface));
Zhu, Yungebe560b02019-04-21 21:19:21 -04001686 }
1687 catch (sdbusplus::exception_t& e)
1688 {
1689 phosphor::logging::log<phosphor::logging::level::ERR>(
1690 "getPidConfigs: can't get DbusProperties!",
1691 phosphor::logging::entry("ERR=%s", e.what()));
1692 }
James Feistacc8a4e2019-04-02 14:23:57 -07001693 }
1694 return ret;
1695}
1696
1697ipmi::RspType<uint8_t> ipmiOEMGetFanSpeedOffset(void)
1698{
1699 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1700 if (data.empty())
1701 {
1702 return ipmi::responseResponseError();
1703 }
1704 uint8_t minOffset = std::numeric_limits<uint8_t>::max();
1705 for (const auto& [_, pid] : data)
1706 {
1707 auto findClass = pid.find("Class");
1708 if (findClass == pid.end())
1709 {
1710 phosphor::logging::log<phosphor::logging::level::ERR>(
1711 "ipmiOEMGetFscParameter: found illegal pid "
1712 "configurations");
1713 return ipmi::responseResponseError();
1714 }
1715 std::string type = std::get<std::string>(findClass->second);
1716 if (type == "fan")
1717 {
1718 auto findOutLimit = pid.find("OutLimitMin");
1719 if (findOutLimit == pid.end())
1720 {
1721 phosphor::logging::log<phosphor::logging::level::ERR>(
1722 "ipmiOEMGetFscParameter: found illegal pid "
1723 "configurations");
1724 return ipmi::responseResponseError();
1725 }
1726 // get the min out of all the offsets
1727 minOffset = std::min(
1728 minOffset,
1729 static_cast<uint8_t>(std::get<double>(findOutLimit->second)));
1730 }
1731 }
1732 if (minOffset == std::numeric_limits<uint8_t>::max())
1733 {
1734 phosphor::logging::log<phosphor::logging::level::ERR>(
1735 "ipmiOEMGetFscParameter: found no fan configurations!");
1736 return ipmi::responseResponseError();
1737 }
1738
1739 return ipmi::responseSuccess(minOffset);
1740}
1741
1742ipmi::RspType<> ipmiOEMSetFanSpeedOffset(uint8_t offset)
1743{
1744 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1745 if (data.empty())
1746 {
1747
1748 phosphor::logging::log<phosphor::logging::level::ERR>(
1749 "ipmiOEMSetFanSpeedOffset: found no pid configurations!");
1750 return ipmi::responseResponseError();
1751 }
1752
Vernon Mauery15419dd2019-05-24 09:40:30 -07001753 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001754 bool found = false;
1755 for (const auto& [path, pid] : data)
1756 {
1757 auto findClass = pid.find("Class");
1758 if (findClass == pid.end())
1759 {
1760
1761 phosphor::logging::log<phosphor::logging::level::ERR>(
1762 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1763 "configurations");
1764 return ipmi::responseResponseError();
1765 }
1766 std::string type = std::get<std::string>(findClass->second);
1767 if (type == "fan")
1768 {
1769 auto findOutLimit = pid.find("OutLimitMin");
1770 if (findOutLimit == pid.end())
1771 {
1772
1773 phosphor::logging::log<phosphor::logging::level::ERR>(
1774 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1775 "configurations");
1776 return ipmi::responseResponseError();
1777 }
Vernon Mauery15419dd2019-05-24 09:40:30 -07001778 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager",
James Feistacc8a4e2019-04-02 14:23:57 -07001779 path, pidConfigurationIface, "OutLimitMin",
1780 static_cast<double>(offset));
1781 found = true;
1782 }
1783 }
1784 if (!found)
1785 {
1786 phosphor::logging::log<phosphor::logging::level::ERR>(
1787 "ipmiOEMSetFanSpeedOffset: set no fan offsets");
1788 return ipmi::responseResponseError();
1789 }
1790
1791 return ipmi::responseSuccess();
1792}
1793
1794ipmi::RspType<> ipmiOEMSetFscParameter(uint8_t command, uint8_t param1,
1795 uint8_t param2)
James Feist5f957ca2019-03-14 15:33:55 -07001796{
1797 constexpr const size_t disableLimiting = 0x0;
1798
Vernon Mauery15419dd2019-05-24 09:40:30 -07001799 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001800 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001801 {
James Feist09f6b602019-08-08 11:30:03 -07001802 std::string pathName;
James Feistacc8a4e2019-04-02 14:23:57 -07001803 if (param1 == legacyExitAirSensorNumber)
James Feistfaa4f222019-03-21 16:21:55 -07001804 {
James Feist09f6b602019-08-08 11:30:03 -07001805 pathName = exitAirPathName;
1806 }
1807 else if (param1 == legacyPCHSensorNumber)
1808 {
1809 pathName = pchPathName;
James Feistfaa4f222019-03-21 16:21:55 -07001810 }
1811 else
1812 {
James Feistacc8a4e2019-04-02 14:23:57 -07001813 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001814 }
James Feist09f6b602019-08-08 11:30:03 -07001815 std::string path = getConfigPath(pathName);
1816 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager", path,
1817 pidConfigurationIface, "SetPoint",
1818 static_cast<double>(param2));
1819 return ipmi::responseSuccess();
James Feistfaa4f222019-03-21 16:21:55 -07001820 }
James Feistacc8a4e2019-04-02 14:23:57 -07001821 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001822 {
James Feistacc8a4e2019-04-02 14:23:57 -07001823 uint16_t cfm = param1 | (static_cast<uint16_t>(param2) << 8);
James Feist5f957ca2019-03-14 15:33:55 -07001824
1825 // must be greater than 50 based on eps
1826 if (cfm < 50 && cfm != disableLimiting)
1827 {
James Feistacc8a4e2019-04-02 14:23:57 -07001828 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001829 }
1830
1831 try
1832 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001833 ipmi::setDbusProperty(*dbus, settingsBusName, cfmLimitSettingPath,
James Feist5f957ca2019-03-14 15:33:55 -07001834 cfmLimitIface, "Limit",
1835 static_cast<double>(cfm));
1836 }
1837 catch (sdbusplus::exception_t& e)
1838 {
1839 phosphor::logging::log<phosphor::logging::level::ERR>(
1840 "ipmiOEMSetFscParameter: can't set cfm setting!",
1841 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07001842 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07001843 }
James Feistacc8a4e2019-04-02 14:23:57 -07001844 return ipmi::responseSuccess();
1845 }
1846 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1847 {
1848 constexpr const size_t maxDomainCount = 8;
1849 uint8_t requestedDomainMask = param1;
1850 boost::container::flat_map data = getPidConfigs();
1851 if (data.empty())
1852 {
1853
1854 phosphor::logging::log<phosphor::logging::level::ERR>(
1855 "ipmiOEMSetFscParameter: found no pid configurations!");
1856 return ipmi::responseResponseError();
1857 }
1858 size_t count = 0;
1859 for (const auto& [path, pid] : data)
1860 {
1861 auto findClass = pid.find("Class");
1862 if (findClass == pid.end())
1863 {
1864
1865 phosphor::logging::log<phosphor::logging::level::ERR>(
1866 "ipmiOEMSetFscParameter: found illegal pid "
1867 "configurations");
1868 return ipmi::responseResponseError();
1869 }
1870 std::string type = std::get<std::string>(findClass->second);
1871 if (type == "fan")
1872 {
1873 if (requestedDomainMask & (1 << count))
1874 {
1875 ipmi::setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07001876 *dbus, "xyz.openbmc_project.EntityManager", path,
James Feistacc8a4e2019-04-02 14:23:57 -07001877 pidConfigurationIface, "OutLimitMax",
1878 static_cast<double>(param2));
1879 }
1880 count++;
1881 }
1882 }
1883 return ipmi::responseSuccess();
James Feist5f957ca2019-03-14 15:33:55 -07001884 }
1885 else
1886 {
1887 // todo other command parts possibly
1888 // tcontrol is handled in peci now
1889 // fan speed offset not implemented yet
1890 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07001891 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001892 }
1893}
1894
James Feistacc8a4e2019-04-02 14:23:57 -07001895ipmi::RspType<
1896 std::variant<uint8_t, std::array<uint8_t, 2>, std::array<uint16_t, 2>>>
1897 ipmiOEMGetFscParameter(uint8_t command, std::optional<uint8_t> param)
James Feist5f957ca2019-03-14 15:33:55 -07001898{
James Feist09f6b602019-08-08 11:30:03 -07001899 constexpr uint8_t legacyDefaultSetpoint = -128;
James Feist5f957ca2019-03-14 15:33:55 -07001900
Vernon Mauery15419dd2019-05-24 09:40:30 -07001901 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001902 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001903 {
James Feistacc8a4e2019-04-02 14:23:57 -07001904 if (!param)
James Feistfaa4f222019-03-21 16:21:55 -07001905 {
James Feistacc8a4e2019-04-02 14:23:57 -07001906 return ipmi::responseReqDataLenInvalid();
James Feistfaa4f222019-03-21 16:21:55 -07001907 }
1908
James Feist09f6b602019-08-08 11:30:03 -07001909 std::string pathName;
1910
1911 if (*param == legacyExitAirSensorNumber)
1912 {
1913 pathName = exitAirPathName;
1914 }
1915 else if (*param == legacyPCHSensorNumber)
1916 {
1917 pathName = pchPathName;
1918 }
1919 else
James Feistfaa4f222019-03-21 16:21:55 -07001920 {
James Feistacc8a4e2019-04-02 14:23:57 -07001921 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001922 }
James Feist09f6b602019-08-08 11:30:03 -07001923
1924 uint8_t setpoint = legacyDefaultSetpoint;
1925 std::string path = getConfigPath(pathName);
James Feistfaa4f222019-03-21 16:21:55 -07001926 if (path.size())
1927 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001928 Value val = ipmi::getDbusProperty(
1929 *dbus, "xyz.openbmc_project.EntityManager", path,
1930 pidConfigurationIface, "SetPoint");
James Feistfaa4f222019-03-21 16:21:55 -07001931 setpoint = std::floor(std::get<double>(val) + 0.5);
1932 }
1933
1934 // old implementation used to return the "default" and current, we
1935 // don't make the default readily available so just make both the
1936 // same
James Feistfaa4f222019-03-21 16:21:55 -07001937
James Feistacc8a4e2019-04-02 14:23:57 -07001938 return ipmi::responseSuccess(
1939 std::array<uint8_t, 2>{setpoint, setpoint});
James Feistfaa4f222019-03-21 16:21:55 -07001940 }
James Feistacc8a4e2019-04-02 14:23:57 -07001941 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1942 {
1943 constexpr const size_t maxDomainCount = 8;
1944
1945 if (!param)
1946 {
1947 return ipmi::responseReqDataLenInvalid();
1948 }
1949 uint8_t requestedDomain = *param;
1950 if (requestedDomain >= maxDomainCount)
1951 {
1952 return ipmi::responseInvalidFieldRequest();
1953 }
1954
1955 boost::container::flat_map data = getPidConfigs();
1956 if (data.empty())
1957 {
1958 phosphor::logging::log<phosphor::logging::level::ERR>(
1959 "ipmiOEMGetFscParameter: found no pid configurations!");
1960 return ipmi::responseResponseError();
1961 }
1962 size_t count = 0;
1963 for (const auto& [_, pid] : data)
1964 {
1965 auto findClass = pid.find("Class");
1966 if (findClass == pid.end())
1967 {
1968 phosphor::logging::log<phosphor::logging::level::ERR>(
1969 "ipmiOEMGetFscParameter: found illegal pid "
1970 "configurations");
1971 return ipmi::responseResponseError();
1972 }
1973 std::string type = std::get<std::string>(findClass->second);
1974 if (type == "fan")
1975 {
1976 if (requestedDomain == count)
1977 {
1978 auto findOutLimit = pid.find("OutLimitMax");
1979 if (findOutLimit == pid.end())
1980 {
1981 phosphor::logging::log<phosphor::logging::level::ERR>(
1982 "ipmiOEMGetFscParameter: found illegal pid "
1983 "configurations");
1984 return ipmi::responseResponseError();
1985 }
1986
1987 return ipmi::responseSuccess(
1988 static_cast<uint8_t>(std::floor(
1989 std::get<double>(findOutLimit->second) + 0.5)));
1990 }
1991 else
1992 {
1993 count++;
1994 }
1995 }
1996 }
1997
1998 return ipmi::responseInvalidFieldRequest();
1999 }
2000 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07002001 {
2002
2003 /*
2004 DataLen should be 1, but host is sending us an extra bit. As the
James Feistacc8a4e2019-04-02 14:23:57 -07002005 previous behavior didn't seem to prevent this, ignore the check for
2006 now.
James Feist5f957ca2019-03-14 15:33:55 -07002007
James Feistacc8a4e2019-04-02 14:23:57 -07002008 if (param)
James Feist5f957ca2019-03-14 15:33:55 -07002009 {
2010 phosphor::logging::log<phosphor::logging::level::ERR>(
2011 "ipmiOEMGetFscParameter: invalid input len!");
James Feist5f957ca2019-03-14 15:33:55 -07002012 return IPMI_CC_REQ_DATA_LEN_INVALID;
2013 }
2014 */
2015 Value cfmLimit;
2016 Value cfmMaximum;
2017 try
2018 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002019 cfmLimit = ipmi::getDbusProperty(*dbus, settingsBusName,
James Feist5f957ca2019-03-14 15:33:55 -07002020 cfmLimitSettingPath, cfmLimitIface,
2021 "Limit");
2022 cfmMaximum = ipmi::getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002023 *dbus, "xyz.openbmc_project.ExitAirTempSensor",
James Feist5f957ca2019-03-14 15:33:55 -07002024 "/xyz/openbmc_project/control/MaxCFM", cfmLimitIface, "Limit");
2025 }
2026 catch (sdbusplus::exception_t& e)
2027 {
2028 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feistacc8a4e2019-04-02 14:23:57 -07002029 "ipmiOEMGetFscParameter: can't get cfm setting!",
James Feist5f957ca2019-03-14 15:33:55 -07002030 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002031 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002032 }
2033
James Feistacc8a4e2019-04-02 14:23:57 -07002034 double cfmMax = std::get<double>(cfmMaximum);
2035 double cfmLim = std::get<double>(cfmLimit);
James Feist5f957ca2019-03-14 15:33:55 -07002036
James Feistacc8a4e2019-04-02 14:23:57 -07002037 cfmLim = std::floor(cfmLim + 0.5);
2038 cfmMax = std::floor(cfmMax + 0.5);
2039 uint16_t cfmLimResp = static_cast<uint16_t>(cfmLim);
2040 uint16_t cfmMaxResp = static_cast<uint16_t>(cfmMax);
James Feist5f957ca2019-03-14 15:33:55 -07002041
James Feistacc8a4e2019-04-02 14:23:57 -07002042 return ipmi::responseSuccess(
2043 std::array<uint16_t, 2>{cfmLimResp, cfmMaxResp});
James Feist5f957ca2019-03-14 15:33:55 -07002044 }
James Feistacc8a4e2019-04-02 14:23:57 -07002045
James Feist5f957ca2019-03-14 15:33:55 -07002046 else
2047 {
2048 // todo other command parts possibly
James Feist5f957ca2019-03-14 15:33:55 -07002049 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002050 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002051 }
2052}
2053
Cheng C Yang773703a2019-08-15 09:41:11 +08002054using crConfigVariant =
2055 std::variant<bool, uint8_t, uint32_t, std::vector<uint8_t>, std::string>;
2056
2057int setCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2058 const crConfigVariant& value,
2059 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2060{
2061 boost::system::error_code ec;
2062 ctx->bus->yield_method_call<void>(
James Feist28c72902019-09-16 10:34:07 -07002063 ctx->yield, ec, "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002064 "/xyz/openbmc_project/control/power_supply_redundancy",
2065 "org.freedesktop.DBus.Properties", "Set",
2066 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property, value);
2067 if (ec)
2068 {
2069 phosphor::logging::log<phosphor::logging::level::ERR>(
2070 "Failed to set dbus property to cold redundancy");
2071 return -1;
2072 }
2073
2074 return 0;
2075}
2076
2077int getCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2078 crConfigVariant& value,
Yong Li19445ab2019-12-20 18:25:29 +08002079 const std::string& service = "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002080 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2081{
2082 boost::system::error_code ec;
2083 value = ctx->bus->yield_method_call<crConfigVariant>(
Yong Li19445ab2019-12-20 18:25:29 +08002084 ctx->yield, ec, service,
Cheng C Yang773703a2019-08-15 09:41:11 +08002085 "/xyz/openbmc_project/control/power_supply_redundancy",
2086 "org.freedesktop.DBus.Properties", "Get",
2087 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property);
2088 if (ec)
2089 {
2090 phosphor::logging::log<phosphor::logging::level::ERR>(
2091 "Failed to get dbus property to cold redundancy");
2092 return -1;
2093 }
2094 return 0;
2095}
2096
2097uint8_t getPSUCount(void)
2098{
2099 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2100 ipmi::Value num;
2101 try
2102 {
2103 num = ipmi::getDbusProperty(
2104 *dbus, "xyz.openbmc_project.PSURedundancy",
2105 "/xyz/openbmc_project/control/power_supply_redundancy",
2106 "xyz.openbmc_project.Control.PowerSupplyRedundancy", "PSUNumber");
2107 }
2108 catch (sdbusplus::exception_t& e)
2109 {
2110 phosphor::logging::log<phosphor::logging::level::ERR>(
2111 "Failed to get PSUNumber property from dbus interface");
2112 return 0;
2113 }
2114 uint8_t* pNum = std::get_if<uint8_t>(&num);
2115 if (!pNum)
2116 {
2117 phosphor::logging::log<phosphor::logging::level::ERR>(
2118 "Error to get PSU Number");
2119 return 0;
2120 }
2121 return *pNum;
2122}
2123
2124bool validateCRAlgo(std::vector<uint8_t>& conf, uint8_t num)
2125{
2126 if (conf.size() < num)
2127 {
2128 phosphor::logging::log<phosphor::logging::level::ERR>(
2129 "Invalid PSU Ranking");
2130 return false;
2131 }
2132 std::set<uint8_t> confSet;
2133 for (uint8_t i = 0; i < num; i++)
2134 {
2135 if (conf[i] > num)
2136 {
2137 phosphor::logging::log<phosphor::logging::level::ERR>(
2138 "PSU Ranking is larger than current PSU number");
2139 return false;
2140 }
2141 confSet.emplace(conf[i]);
2142 }
2143
2144 if (confSet.size() != num)
2145 {
2146 phosphor::logging::log<phosphor::logging::level::ERR>(
2147 "duplicate PSU Ranking");
2148 return false;
2149 }
2150 return true;
2151}
2152
2153enum class crParameter
2154{
2155 crStatus = 0,
2156 crFeature = 1,
2157 rotationFeature = 2,
2158 rotationAlgo = 3,
2159 rotationPeriod = 4,
Yong Li19445ab2019-12-20 18:25:29 +08002160 numOfPSU = 5,
2161 rotationRankOrderEffective = 6
Cheng C Yang773703a2019-08-15 09:41:11 +08002162};
2163
2164constexpr ipmi::Cc ccParameterNotSupported = 0x80;
2165static const constexpr uint32_t oneDay = 0x15180;
2166static const constexpr uint32_t oneMonth = 0xf53700;
2167static const constexpr uint8_t userSpecific = 0x01;
2168static const constexpr uint8_t crSetCompleted = 0;
2169ipmi::RspType<uint8_t> ipmiOEMSetCRConfig(ipmi::Context::ptr ctx,
2170 uint8_t parameter,
2171 ipmi::message::Payload& payload)
2172{
2173 switch (static_cast<crParameter>(parameter))
2174 {
2175 case crParameter::crFeature:
2176 {
2177 uint8_t param1;
2178 if (payload.unpack(param1) || !payload.fullyUnpacked())
2179 {
2180 return ipmi::responseReqDataLenInvalid();
2181 }
2182 // ColdRedundancy Enable can only be true or flase
2183 if (param1 > 1)
2184 {
2185 return ipmi::responseInvalidFieldRequest();
2186 }
2187 if (setCRConfig(ctx, "ColdRedundancyEnabled",
2188 static_cast<bool>(param1)))
2189 {
2190 return ipmi::responseResponseError();
2191 }
2192 break;
2193 }
2194 case crParameter::rotationFeature:
2195 {
2196 uint8_t param1;
2197 if (payload.unpack(param1) || !payload.fullyUnpacked())
2198 {
2199 return ipmi::responseReqDataLenInvalid();
2200 }
2201 // Rotation Enable can only be true or false
2202 if (param1 > 1)
2203 {
2204 return ipmi::responseInvalidFieldRequest();
2205 }
2206 if (setCRConfig(ctx, "RotationEnabled", static_cast<bool>(param1)))
2207 {
2208 return ipmi::responseResponseError();
2209 }
2210 break;
2211 }
2212 case crParameter::rotationAlgo:
2213 {
2214 // Rotation Algorithm can only be 0-BMC Specific or 1-User Specific
2215 std::string algoName;
2216 uint8_t param1;
2217 if (payload.unpack(param1))
2218 {
2219 return ipmi::responseReqDataLenInvalid();
2220 }
2221 switch (param1)
2222 {
2223 case 0:
2224 algoName = "xyz.openbmc_project.Control."
2225 "PowerSupplyRedundancy.Algo.bmcSpecific";
2226 break;
2227 case 1:
2228 algoName = "xyz.openbmc_project.Control."
2229 "PowerSupplyRedundancy.Algo.userSpecific";
2230 break;
2231 default:
2232 return ipmi::responseInvalidFieldRequest();
2233 }
2234 if (setCRConfig(ctx, "RotationAlgorithm", algoName))
2235 {
2236 return ipmi::responseResponseError();
2237 }
2238
2239 uint8_t numberOfPSU = getPSUCount();
2240 if (!numberOfPSU)
2241 {
2242 return ipmi::responseResponseError();
2243 }
2244 std::vector<uint8_t> rankOrder;
2245
2246 if (param1 == userSpecific)
2247 {
2248 if (payload.unpack(rankOrder) || !payload.fullyUnpacked())
2249 {
2250 ipmi::responseReqDataLenInvalid();
2251 }
Yong Li83315132019-10-23 17:42:24 +08002252 if (rankOrder.size() != numberOfPSU)
Cheng C Yang773703a2019-08-15 09:41:11 +08002253 {
2254 return ipmi::responseReqDataLenInvalid();
2255 }
2256
2257 if (!validateCRAlgo(rankOrder, numberOfPSU))
2258 {
2259 return ipmi::responseInvalidFieldRequest();
2260 }
2261 }
2262 else
2263 {
2264 if (rankOrder.size() > 0)
2265 {
2266 return ipmi::responseReqDataLenInvalid();
2267 }
2268 for (uint8_t i = 1; i <= numberOfPSU; i++)
2269 {
2270 rankOrder.emplace_back(i);
2271 }
2272 }
2273 if (setCRConfig(ctx, "RotationRankOrder", rankOrder))
2274 {
2275 return ipmi::responseResponseError();
2276 }
2277 break;
2278 }
2279 case crParameter::rotationPeriod:
2280 {
2281 // Minimum Rotation period is One day (86400 seconds) and Max
2282 // Rotation Period is 6 month (0xf53700 seconds)
2283 uint32_t period;
2284 if (payload.unpack(period) || !payload.fullyUnpacked())
2285 {
2286 return ipmi::responseReqDataLenInvalid();
2287 }
2288 if ((period < oneDay) || (period > oneMonth))
2289 {
2290 return ipmi::responseInvalidFieldRequest();
2291 }
2292 if (setCRConfig(ctx, "PeriodOfRotation", period))
2293 {
2294 return ipmi::responseResponseError();
2295 }
2296 break;
2297 }
2298 default:
2299 {
2300 return ipmi::response(ccParameterNotSupported);
2301 }
2302 }
2303
2304 // TODO Halfwidth needs to set SetInProgress
2305 if (setCRConfig(ctx, "ColdRedundancyStatus",
Cheng C Yange8cecdf2019-08-26 23:48:08 +08002306 std::string("xyz.openbmc_project.Control."
2307 "PowerSupplyRedundancy.Status.completed")))
Cheng C Yang773703a2019-08-15 09:41:11 +08002308 {
2309 return ipmi::responseResponseError();
2310 }
2311 return ipmi::responseSuccess(crSetCompleted);
2312}
2313
Yong Li83315132019-10-23 17:42:24 +08002314ipmi::RspType<uint8_t, std::variant<uint8_t, uint32_t, std::vector<uint8_t>>>
Cheng C Yang773703a2019-08-15 09:41:11 +08002315 ipmiOEMGetCRConfig(ipmi::Context::ptr ctx, uint8_t parameter)
2316{
2317 crConfigVariant value;
2318 switch (static_cast<crParameter>(parameter))
2319 {
2320 case crParameter::crStatus:
2321 {
2322 if (getCRConfig(ctx, "ColdRedundancyStatus", value))
2323 {
2324 return ipmi::responseResponseError();
2325 }
2326 std::string* pStatus = std::get_if<std::string>(&value);
2327 if (!pStatus)
2328 {
2329 phosphor::logging::log<phosphor::logging::level::ERR>(
2330 "Error to get ColdRedundancyStatus property");
2331 return ipmi::responseResponseError();
2332 }
2333 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2334 auto status =
2335 server::PowerSupplyRedundancy::convertStatusFromString(
2336 *pStatus);
2337 switch (status)
2338 {
2339 case server::PowerSupplyRedundancy::Status::inProgress:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002340 return ipmi::responseSuccess(parameter,
2341 static_cast<uint8_t>(0));
Cheng C Yang773703a2019-08-15 09:41:11 +08002342
2343 case server::PowerSupplyRedundancy::Status::completed:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002344 return ipmi::responseSuccess(parameter,
2345 static_cast<uint8_t>(1));
Cheng C Yang773703a2019-08-15 09:41:11 +08002346 default:
2347 phosphor::logging::log<phosphor::logging::level::ERR>(
2348 "Error to get valid status");
2349 return ipmi::responseResponseError();
2350 }
2351 }
2352 case crParameter::crFeature:
2353 {
2354 if (getCRConfig(ctx, "ColdRedundancyEnabled", value))
2355 {
2356 return ipmi::responseResponseError();
2357 }
2358 bool* pResponse = std::get_if<bool>(&value);
2359 if (!pResponse)
2360 {
2361 phosphor::logging::log<phosphor::logging::level::ERR>(
2362 "Error to get ColdRedundancyEnable property");
2363 return ipmi::responseResponseError();
2364 }
2365
Cheng C Yangf41e3342019-09-10 04:47:23 +08002366 return ipmi::responseSuccess(parameter,
2367 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002368 }
2369 case crParameter::rotationFeature:
2370 {
2371 if (getCRConfig(ctx, "RotationEnabled", value))
2372 {
2373 return ipmi::responseResponseError();
2374 }
2375 bool* pResponse = std::get_if<bool>(&value);
2376 if (!pResponse)
2377 {
2378 phosphor::logging::log<phosphor::logging::level::ERR>(
2379 "Error to get RotationEnabled property");
2380 return ipmi::responseResponseError();
2381 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002382 return ipmi::responseSuccess(parameter,
2383 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002384 }
2385 case crParameter::rotationAlgo:
2386 {
2387 if (getCRConfig(ctx, "RotationAlgorithm", value))
2388 {
2389 return ipmi::responseResponseError();
2390 }
2391
2392 std::string* pAlgo = std::get_if<std::string>(&value);
2393 if (!pAlgo)
2394 {
2395 phosphor::logging::log<phosphor::logging::level::ERR>(
2396 "Error to get RotationAlgorithm property");
2397 return ipmi::responseResponseError();
2398 }
Yong Li83315132019-10-23 17:42:24 +08002399 std::vector<uint8_t> response;
Cheng C Yang773703a2019-08-15 09:41:11 +08002400 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2401 auto algo =
2402 server::PowerSupplyRedundancy::convertAlgoFromString(*pAlgo);
Yong Li83315132019-10-23 17:42:24 +08002403
Cheng C Yang773703a2019-08-15 09:41:11 +08002404 switch (algo)
2405 {
2406 case server::PowerSupplyRedundancy::Algo::bmcSpecific:
Yong Li83315132019-10-23 17:42:24 +08002407 response.push_back(0);
Cheng C Yang773703a2019-08-15 09:41:11 +08002408 break;
2409 case server::PowerSupplyRedundancy::Algo::userSpecific:
Yong Li83315132019-10-23 17:42:24 +08002410 response.push_back(1);
Cheng C Yang773703a2019-08-15 09:41:11 +08002411 break;
2412 default:
2413 phosphor::logging::log<phosphor::logging::level::ERR>(
2414 "Error to get valid algo");
2415 return ipmi::responseResponseError();
2416 }
2417
2418 if (getCRConfig(ctx, "RotationRankOrder", value))
2419 {
2420 return ipmi::responseResponseError();
2421 }
2422 std::vector<uint8_t>* pResponse =
2423 std::get_if<std::vector<uint8_t>>(&value);
2424 if (!pResponse)
2425 {
2426 phosphor::logging::log<phosphor::logging::level::ERR>(
2427 "Error to get RotationRankOrder property");
2428 return ipmi::responseResponseError();
2429 }
Yong Li83315132019-10-23 17:42:24 +08002430
Cheng C Yang773703a2019-08-15 09:41:11 +08002431 std::copy(pResponse->begin(), pResponse->end(),
Yong Li83315132019-10-23 17:42:24 +08002432 std::back_inserter(response));
2433
Cheng C Yangf41e3342019-09-10 04:47:23 +08002434 return ipmi::responseSuccess(parameter, response);
Cheng C Yang773703a2019-08-15 09:41:11 +08002435 }
2436 case crParameter::rotationPeriod:
2437 {
2438 if (getCRConfig(ctx, "PeriodOfRotation", value))
2439 {
2440 return ipmi::responseResponseError();
2441 }
2442 uint32_t* pResponse = std::get_if<uint32_t>(&value);
2443 if (!pResponse)
2444 {
2445 phosphor::logging::log<phosphor::logging::level::ERR>(
2446 "Error to get RotationAlgorithm property");
2447 return ipmi::responseResponseError();
2448 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002449 return ipmi::responseSuccess(parameter, *pResponse);
Cheng C Yang773703a2019-08-15 09:41:11 +08002450 }
2451 case crParameter::numOfPSU:
2452 {
2453 uint8_t numberOfPSU = getPSUCount();
2454 if (!numberOfPSU)
2455 {
2456 return ipmi::responseResponseError();
2457 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002458 return ipmi::responseSuccess(parameter, numberOfPSU);
Cheng C Yang773703a2019-08-15 09:41:11 +08002459 }
Yong Li19445ab2019-12-20 18:25:29 +08002460 case crParameter::rotationRankOrderEffective:
2461 {
2462 if (getCRConfig(ctx, "RotationRankOrder", value,
2463 "xyz.openbmc_project.PSURedundancy"))
2464 {
2465 return ipmi::responseResponseError();
2466 }
2467 std::vector<uint8_t>* pResponse =
2468 std::get_if<std::vector<uint8_t>>(&value);
2469 if (!pResponse)
2470 {
2471 phosphor::logging::log<phosphor::logging::level::ERR>(
2472 "Error to get effective RotationRankOrder property");
2473 return ipmi::responseResponseError();
2474 }
2475 return ipmi::responseSuccess(parameter, *pResponse);
2476 }
Cheng C Yang773703a2019-08-15 09:41:11 +08002477 default:
2478 {
2479 return ipmi::response(ccParameterNotSupported);
2480 }
2481 }
2482}
2483
Zhu, Yungebe560b02019-04-21 21:19:21 -04002484ipmi::RspType<> ipmiOEMSetFaultIndication(uint8_t sourceId, uint8_t faultType,
2485 uint8_t faultState,
2486 uint8_t faultGroup,
2487 std::array<uint8_t, 8>& ledStateData)
2488{
Zhu, Yungebe560b02019-04-21 21:19:21 -04002489 constexpr auto maxFaultType = static_cast<size_t>(RemoteFaultType::max);
2490 static const std::array<std::string, maxFaultType> faultNames = {
2491 "faultFan", "faultTemp", "faultPower",
2492 "faultDriveSlot", "faultSoftware", "faultMemory"};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002493
2494 constexpr uint8_t maxFaultSource = 0x4;
2495 constexpr uint8_t skipLEDs = 0xFF;
2496 constexpr uint8_t pinSize = 64;
2497 constexpr uint8_t groupSize = 16;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002498 constexpr uint8_t groupNum = 5; // 4 for fault memory, 1 for faultFan
Zhu, Yungebe560b02019-04-21 21:19:21 -04002499
Zhikui Rence4e73f2019-12-06 13:59:47 -08002500 // same pin names need to be defined in dts file
2501 static const std::array<std::array<std::string, groupSize>, groupNum>
2502 faultLedPinNames = {{
2503 "LED_CPU1_CH1_DIMM1_FAULT",
2504 "LED_CPU1_CH1_DIMM2_FAULT",
2505 "LED_CPU1_CH2_DIMM1_FAULT",
2506 "LED_CPU1_CH2_DIMM2_FAULT",
2507 "LED_CPU1_CH3_DIMM1_FAULT",
2508 "LED_CPU1_CH3_DIMM2_FAULT",
2509 "LED_CPU1_CH4_DIMM1_FAULT",
2510 "LED_CPU1_CH4_DIMM2_FAULT",
2511 "LED_CPU1_CH5_DIMM1_FAULT",
2512 "LED_CPU1_CH5_DIMM2_FAULT",
2513 "LED_CPU1_CH6_DIMM1_FAULT",
2514 "LED_CPU1_CH6_DIMM2_FAULT",
2515 "",
2516 "",
2517 "",
2518 "", // end of group1
2519 "LED_CPU2_CH1_DIMM1_FAULT",
2520 "LED_CPU2_CH1_DIMM2_FAULT",
2521 "LED_CPU2_CH2_DIMM1_FAULT",
2522 "LED_CPU2_CH2_DIMM2_FAULT",
2523 "LED_CPU2_CH3_DIMM1_FAULT",
2524 "LED_CPU2_CH3_DIMM2_FAULT",
2525 "LED_CPU2_CH4_DIMM1_FAULT",
2526 "LED_CPU2_CH4_DIMM2_FAULT",
2527 "LED_CPU2_CH5_DIMM1_FAULT",
2528 "LED_CPU2_CH5_DIMM2_FAULT",
2529 "LED_CPU2_CH6_DIMM1_FAULT",
2530 "LED_CPU2_CH6_DIMM2_FAULT",
2531 "",
2532 "",
2533 "",
2534 "", // endof group2
2535 "LED_CPU3_CH1_DIMM1_FAULT",
2536 "LED_CPU3_CH1_DIMM2_FAULT",
2537 "LED_CPU3_CH2_DIMM1_FAULT",
2538 "LED_CPU3_CH2_DIMM2_FAULT",
2539 "LED_CPU3_CH3_DIMM1_FAULT",
2540 "LED_CPU3_CH3_DIMM2_FAULT",
2541 "LED_CPU3_CH4_DIMM1_FAULT",
2542 "LED_CPU3_CH4_DIMM2_FAULT",
2543 "LED_CPU3_CH5_DIMM1_FAULT",
2544 "LED_CPU3_CH5_DIMM2_FAULT",
2545 "LED_CPU3_CH6_DIMM1_FAULT",
2546 "LED_CPU3_CH6_DIMM2_FAULT",
2547 "",
2548 "",
2549 "",
2550 "", // end of group3
2551 "LED_CPU4_CH1_DIMM1_FAULT",
2552 "LED_CPU4_CH1_DIMM2_FAULT",
2553 "LED_CPU4_CH2_DIMM1_FAULT",
2554 "LED_CPU4_CH2_DIMM2_FAULT",
2555 "LED_CPU4_CH3_DIMM1_FAULT",
2556 "LED_CPU4_CH3_DIMM2_FAULT",
2557 "LED_CPU4_CH4_DIMM1_FAULT",
2558 "LED_CPU4_CH4_DIMM2_FAULT",
2559 "LED_CPU4_CH5_DIMM1_FAULT",
2560 "LED_CPU4_CH5_DIMM2_FAULT",
2561 "LED_CPU4_CH6_DIMM1_FAULT",
2562 "LED_CPU4_CH6_DIMM2_FAULT",
2563 "",
2564 "",
2565 "",
2566 "", // end of group4
2567 "LED_FAN1_FAULT",
2568 "LED_FAN2_FAULT",
2569 "LED_FAN3_FAULT",
2570 "LED_FAN4_FAULT",
2571 "LED_FAN5_FAULT",
2572 "LED_FAN6_FAULT",
2573 "LED_FAN7_FAULT",
2574 "LED_FAN8_FAULT",
2575 "",
2576 "",
2577 "",
2578 "",
2579 "",
2580 "",
2581 "",
2582 "" // end of group5
2583 }};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002584
Zhikui Rence4e73f2019-12-06 13:59:47 -08002585 // Validate the source, fault type --
2586 // (Byte 1) sourceId: Unspecified, Hot-Swap Controller 0, Hot-Swap
2587 // Controller 1, BIOS (Byte 2) fault type: fan, temperature, power,
2588 // driveslot, software, memory (Byte 3) FaultState: OK, Degraded,
2589 // Non-Critical, Critical, Non-Recoverable, (Byte 4) is faultGroup,
2590 // definition differs based on fault type (Byte 2)
2591 // Type Fan=> Group: 0=FanGroupID, FF-not used
2592 // Byte 5-11 00h, not used
2593 // Byte12 FanLedState [7:0]-Fans 7:0
2594 // Type Memory=> Group: 0 = DIMM GroupID, FF-not used
2595 // Byte 5:12 - DIMM LED state (64bit field, LS Byte first)
2596 // [63:48] = CPU4 channels 7:0, 2 bits per channel
2597 // [47:32] = CPU3 channels 7:0, 2 bits per channel
2598 // [31:16] = CPU2 channels 7:0, 2 bits per channel
2599 // [15:0] = CPU1 channels 7:0, 2 bits per channel
2600 // Type Other=> Component Fault LED Group ID, not used set to 0xFF
2601 // Byte[5:12]: reserved 0x00h
Zhu, Yungebe560b02019-04-21 21:19:21 -04002602 if ((sourceId >= maxFaultSource) ||
2603 (faultType >= static_cast<int8_t>(RemoteFaultType::max)) ||
2604 (faultState >= static_cast<int8_t>(RemoteFaultState::maxFaultState)) ||
2605 (faultGroup >= static_cast<int8_t>(DimmFaultType::maxFaultGroup)))
2606 {
2607 return ipmi::responseParmOutOfRange();
2608 }
2609
Zhikui Rence4e73f2019-12-06 13:59:47 -08002610 size_t pinGroupOffset = 0;
2611 size_t pinGroupMax = pinSize / groupSize;
2612 if (RemoteFaultType::fan == RemoteFaultType(faultType))
Zhu, Yungebe560b02019-04-21 21:19:21 -04002613 {
Zhikui Rence4e73f2019-12-06 13:59:47 -08002614 pinGroupOffset = 4;
2615 pinGroupMax = groupNum - pinSize / groupSize;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002616 }
2617
2618 switch (RemoteFaultType(faultType))
2619 {
2620 case (RemoteFaultType::fan):
2621 case (RemoteFaultType::memory):
2622 {
2623 if (faultGroup == skipLEDs)
2624 {
2625 return ipmi::responseSuccess();
2626 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002627 // calculate led state bit filed count, each byte has 8bits
2628 // the maximum bits will be 8 * 8 bits
2629 constexpr uint8_t size = sizeof(ledStateData) * 8;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002630
2631 // assemble ledState
2632 uint64_t ledState = 0;
2633 bool hasError = false;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002634 for (int i = 0; i < sizeof(ledStateData); i++)
2635 {
2636 ledState = (uint64_t)(ledState << 8);
2637 ledState = (uint64_t)(ledState | (uint64_t)ledStateData[i]);
2638 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002639 std::bitset<size> ledStateBits(ledState);
Zhu, Yungebe560b02019-04-21 21:19:21 -04002640
Zhikui Rence4e73f2019-12-06 13:59:47 -08002641 for (int group = 0; group < pinGroupMax; group++)
2642 {
2643 for (int i = 0; i < groupSize; i++)
2644 { // skip non-existing pins
2645 if (0 == faultLedPinNames[group + pinGroupOffset][i].size())
2646 {
2647 continue;
2648 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002649
Zhikui Rence4e73f2019-12-06 13:59:47 -08002650 gpiod::line line = gpiod::find_line(
2651 faultLedPinNames[group + pinGroupOffset][i]);
2652 if (!line)
2653 {
2654 phosphor::logging::log<phosphor::logging::level::ERR>(
2655 "Not Find Led Gpio Device!",
2656 phosphor::logging::entry(
2657 "DEVICE=%s",
2658 faultLedPinNames[group + pinGroupOffset][i]
2659 .c_str()));
2660 hasError = true;
2661 continue;
2662 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002663
Zhikui Rence4e73f2019-12-06 13:59:47 -08002664 bool activeHigh =
2665 (line.active_state() == gpiod::line::ACTIVE_HIGH);
2666 try
2667 {
2668 line.request(
2669 {"faultLed", gpiod::line_request::DIRECTION_OUTPUT,
2670 activeHigh
2671 ? 0
2672 : gpiod::line_request::FLAG_ACTIVE_LOW});
2673 line.set_value(ledStateBits[i + group * groupSize]);
2674 }
2675 catch (std::system_error&)
2676 {
2677 phosphor::logging::log<phosphor::logging::level::ERR>(
2678 "Error write Led Gpio Device!",
2679 phosphor::logging::entry(
2680 "DEVICE=%s",
2681 faultLedPinNames[group + pinGroupOffset][i]
2682 .c_str()));
2683 hasError = true;
2684 continue;
2685 }
2686 } // for int i
2687 }
2688 if (hasError)
2689 {
2690 return ipmi::responseResponseError();
Zhu, Yungebe560b02019-04-21 21:19:21 -04002691 }
2692 break;
2693 }
2694 default:
2695 {
2696 // now only support two fault types
2697 return ipmi::responseParmOutOfRange();
2698 }
Zhikui Rence4e73f2019-12-06 13:59:47 -08002699 } // switch
Zhu, Yungebe560b02019-04-21 21:19:21 -04002700 return ipmi::responseSuccess();
2701}
2702
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302703ipmi::RspType<uint8_t> ipmiOEMReadBoardProductId()
2704{
2705 uint8_t prodId = 0;
2706 try
2707 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002708 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302709 const DbusObjectInfo& object = getDbusObject(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002710 *dbus, "xyz.openbmc_project.Inventory.Item.Board",
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302711 "/xyz/openbmc_project/inventory/system/board/", "Baseboard");
2712 const Value& propValue = getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002713 *dbus, object.second, object.first,
Suryakanth Sekar6c57e5c2020-01-10 17:11:58 +05302714 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
2715 "ProductId");
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302716 prodId = static_cast<uint8_t>(std::get<uint64_t>(propValue));
2717 }
2718 catch (std::exception& e)
2719 {
2720 phosphor::logging::log<phosphor::logging::level::ERR>(
2721 "ipmiOEMReadBoardProductId: Product ID read failed!",
2722 phosphor::logging::entry("ERR=%s", e.what()));
2723 }
2724 return ipmi::responseSuccess(prodId);
2725}
2726
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302727/** @brief implements the get security mode command
2728 * @param ctx - ctx pointer
2729 *
2730 * @returns IPMI completion code with following data
2731 * - restriction mode value - As specified in
2732 * xyz.openbmc_project.Control.Security.RestrictionMode.interface.yaml
2733 * - special mode value - As specified in
2734 * xyz.openbmc_project.Control.Security.SpecialMode.interface.yaml
2735 */
2736ipmi::RspType<uint8_t, uint8_t> ipmiGetSecurityMode(ipmi::Context::ptr ctx)
2737{
2738 namespace securityNameSpace =
2739 sdbusplus::xyz::openbmc_project::Control::Security::server;
2740 uint8_t restrictionModeValue = 0;
2741 uint8_t specialModeValue = 0;
2742
2743 boost::system::error_code ec;
2744 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002745 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302746 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2747 restricionModeProperty);
2748 if (ec)
2749 {
2750 phosphor::logging::log<phosphor::logging::level::ERR>(
2751 "ipmiGetSecurityMode: failed to get RestrictionMode property",
2752 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2753 return ipmi::responseUnspecifiedError();
2754 }
2755 restrictionModeValue = static_cast<uint8_t>(
2756 securityNameSpace::RestrictionMode::convertModesFromString(
2757 std::get<std::string>(varRestrMode)));
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302758 auto varSpecialMode =
2759 ctx->bus->yield_method_call<std::variant<std::string>>(
2760 ctx->yield, ec, specialModeService, specialModeBasePath,
2761 dBusPropertyIntf, dBusPropertyGetMethod, specialModeIntf,
2762 specialModeProperty);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302763 if (ec)
2764 {
2765 phosphor::logging::log<phosphor::logging::level::ERR>(
2766 "ipmiGetSecurityMode: failed to get SpecialMode property",
2767 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2768 // fall through, let us not worry about SpecialMode property, which is
2769 // not required in user scenario
2770 }
2771 else
2772 {
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302773 specialModeValue = static_cast<uint8_t>(
2774 securityNameSpace::SpecialMode::convertModesFromString(
2775 std::get<std::string>(varSpecialMode)));
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302776 }
2777 return ipmi::responseSuccess(restrictionModeValue, specialModeValue);
2778}
2779
2780/** @brief implements the set security mode command
2781 * Command allows to upgrade the restriction mode and won't allow
2782 * to downgrade from system interface
2783 * @param ctx - ctx pointer
2784 * @param restrictionMode - restriction mode value to be set.
2785 *
2786 * @returns IPMI completion code
2787 */
2788ipmi::RspType<> ipmiSetSecurityMode(ipmi::Context::ptr ctx,
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302789 uint8_t restrictionMode,
2790 std::optional<uint8_t> specialMode)
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302791{
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302792#ifndef BMC_VALIDATION_UNSECURE_FEATURE
2793 if (specialMode)
2794 {
2795 return ipmi::responseReqDataLenInvalid();
2796 }
2797#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302798 namespace securityNameSpace =
2799 sdbusplus::xyz::openbmc_project::Control::Security::server;
2800
2801 ChannelInfo chInfo;
2802 if (getChannelInfo(ctx->channel, chInfo) != ccSuccess)
2803 {
2804 phosphor::logging::log<phosphor::logging::level::ERR>(
2805 "ipmiSetSecurityMode: Failed to get Channel Info",
2806 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
2807 return ipmi::responseUnspecifiedError();
2808 }
2809 auto reqMode =
2810 static_cast<securityNameSpace::RestrictionMode::Modes>(restrictionMode);
2811
2812 if ((reqMode < securityNameSpace::RestrictionMode::Modes::Provisioning) ||
2813 (reqMode >
2814 securityNameSpace::RestrictionMode::Modes::ProvisionedHostDisabled))
2815 {
2816 return ipmi::responseInvalidFieldRequest();
2817 }
2818
2819 boost::system::error_code ec;
2820 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002821 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302822 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2823 restricionModeProperty);
2824 if (ec)
2825 {
2826 phosphor::logging::log<phosphor::logging::level::ERR>(
2827 "ipmiSetSecurityMode: failed to get RestrictionMode property",
2828 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2829 return ipmi::responseUnspecifiedError();
2830 }
2831 auto currentRestrictionMode =
2832 securityNameSpace::RestrictionMode::convertModesFromString(
2833 std::get<std::string>(varRestrMode));
2834
2835 if (chInfo.mediumType !=
2836 static_cast<uint8_t>(EChannelMediumType::lan8032) &&
2837 currentRestrictionMode > reqMode)
2838 {
2839 phosphor::logging::log<phosphor::logging::level::ERR>(
2840 "ipmiSetSecurityMode - Downgrading security mode not supported "
2841 "through system interface",
2842 phosphor::logging::entry(
2843 "CUR_MODE=%d", static_cast<uint8_t>(currentRestrictionMode)),
2844 phosphor::logging::entry("REQ_MODE=%d", restrictionMode));
2845 return ipmi::responseCommandNotAvailable();
2846 }
2847
2848 ec.clear();
2849 ctx->bus->yield_method_call<>(
James Feist28c72902019-09-16 10:34:07 -07002850 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302851 dBusPropertyIntf, dBusPropertySetMethod, restricionModeIntf,
2852 restricionModeProperty,
2853 static_cast<std::variant<std::string>>(
2854 securityNameSpace::convertForMessage(reqMode)));
2855
2856 if (ec)
2857 {
2858 phosphor::logging::log<phosphor::logging::level::ERR>(
2859 "ipmiSetSecurityMode: failed to set RestrictionMode property",
2860 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2861 return ipmi::responseUnspecifiedError();
2862 }
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302863
2864#ifdef BMC_VALIDATION_UNSECURE_FEATURE
2865 if (specialMode)
2866 {
2867 ec.clear();
2868 ctx->bus->yield_method_call<>(
2869 ctx->yield, ec, specialModeService, specialModeBasePath,
2870 dBusPropertyIntf, dBusPropertySetMethod, specialModeIntf,
2871 specialModeProperty,
2872 static_cast<std::variant<std::string>>(
2873 securityNameSpace::convertForMessage(
2874 static_cast<securityNameSpace::SpecialMode::Modes>(
2875 specialMode.value()))));
2876
2877 if (ec)
2878 {
2879 phosphor::logging::log<phosphor::logging::level::ERR>(
2880 "ipmiSetSecurityMode: failed to set SpecialMode property",
2881 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2882 return ipmi::responseUnspecifiedError();
2883 }
2884 }
2885#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302886 return ipmi::responseSuccess();
2887}
2888
Vernon Mauery4ac799d2019-05-20 15:50:37 -07002889ipmi::RspType<uint8_t /* restore status */>
2890 ipmiRestoreConfiguration(const std::array<uint8_t, 3>& clr, uint8_t cmd)
2891{
2892 static constexpr std::array<uint8_t, 3> expClr = {'C', 'L', 'R'};
2893
2894 if (clr != expClr)
2895 {
2896 return ipmi::responseInvalidFieldRequest();
2897 }
2898 constexpr uint8_t cmdStatus = 0;
2899 constexpr uint8_t cmdDefaultRestore = 0xaa;
2900 constexpr uint8_t cmdFullRestore = 0xbb;
2901 constexpr uint8_t cmdFormat = 0xcc;
2902
2903 constexpr const char* restoreOpFname = "/tmp/.rwfs/.restore_op";
2904
2905 switch (cmd)
2906 {
2907 case cmdStatus:
2908 break;
2909 case cmdDefaultRestore:
2910 case cmdFullRestore:
2911 case cmdFormat:
2912 {
2913 // write file to rwfs root
2914 int value = (cmd - 1) & 0x03; // map aa, bb, cc => 1, 2, 3
2915 std::ofstream restoreFile(restoreOpFname);
2916 if (!restoreFile)
2917 {
2918 return ipmi::responseUnspecifiedError();
2919 }
2920 restoreFile << value << "\n";
2921 break;
2922 }
2923 default:
2924 return ipmi::responseInvalidFieldRequest();
2925 }
2926
2927 constexpr uint8_t restorePending = 0;
2928 constexpr uint8_t restoreComplete = 1;
2929
2930 uint8_t restoreStatus = std::filesystem::exists(restoreOpFname)
2931 ? restorePending
2932 : restoreComplete;
2933 return ipmi::responseSuccess(restoreStatus);
2934}
2935
Chen Yugang39736d52019-07-12 16:24:33 +08002936ipmi::RspType<uint8_t> ipmiOEMGetNmiSource(void)
2937{
2938 uint8_t bmcSource;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002939 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002940
2941 try
2942 {
2943 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2944 std::string service =
2945 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
2946 Value variant =
2947 getDbusProperty(*dbus, service, oemNmiSourceObjPath,
2948 oemNmiSourceIntf, oemNmiBmcSourceObjPathProp);
2949
2950 switch (nmi::NMISource::convertBMCSourceSignalFromString(
2951 std::get<std::string>(variant)))
2952 {
2953 case nmi::NMISource::BMCSourceSignal::None:
2954 bmcSource = static_cast<uint8_t>(NmiSource::none);
2955 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002956 case nmi::NMISource::BMCSourceSignal::FrontPanelButton:
2957 bmcSource = static_cast<uint8_t>(NmiSource::frontPanelButton);
Chen Yugang39736d52019-07-12 16:24:33 +08002958 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002959 case nmi::NMISource::BMCSourceSignal::Watchdog:
2960 bmcSource = static_cast<uint8_t>(NmiSource::watchdog);
Chen Yugang39736d52019-07-12 16:24:33 +08002961 break;
2962 case nmi::NMISource::BMCSourceSignal::ChassisCmd:
2963 bmcSource = static_cast<uint8_t>(NmiSource::chassisCmd);
2964 break;
2965 case nmi::NMISource::BMCSourceSignal::MemoryError:
2966 bmcSource = static_cast<uint8_t>(NmiSource::memoryError);
2967 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002968 case nmi::NMISource::BMCSourceSignal::PciBusError:
2969 bmcSource = static_cast<uint8_t>(NmiSource::pciBusError);
Chen Yugang39736d52019-07-12 16:24:33 +08002970 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002971 case nmi::NMISource::BMCSourceSignal::PCH:
2972 bmcSource = static_cast<uint8_t>(NmiSource::pch);
Chen Yugang39736d52019-07-12 16:24:33 +08002973 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002974 case nmi::NMISource::BMCSourceSignal::Chipset:
2975 bmcSource = static_cast<uint8_t>(NmiSource::chipset);
Chen Yugang39736d52019-07-12 16:24:33 +08002976 break;
2977 default:
2978 phosphor::logging::log<phosphor::logging::level::ERR>(
2979 "NMI source: invalid property!",
2980 phosphor::logging::entry(
2981 "PROP=%s", std::get<std::string>(variant).c_str()));
2982 return ipmi::responseResponseError();
2983 }
2984 }
2985 catch (sdbusplus::exception::SdBusError& e)
2986 {
2987 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
2988 return ipmi::responseResponseError();
2989 }
2990
2991 return ipmi::responseSuccess(bmcSource);
2992}
2993
2994ipmi::RspType<> ipmiOEMSetNmiSource(uint8_t sourceId)
2995{
Chen Yugang97cf96e2019-11-01 08:55:11 +08002996 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002997
2998 nmi::NMISource::BMCSourceSignal bmcSourceSignal =
2999 nmi::NMISource::BMCSourceSignal::None;
3000
3001 switch (NmiSource(sourceId))
3002 {
3003 case NmiSource::none:
3004 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::None;
3005 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003006 case NmiSource::frontPanelButton:
3007 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::FrontPanelButton;
Chen Yugang39736d52019-07-12 16:24:33 +08003008 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003009 case NmiSource::watchdog:
3010 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Watchdog;
Chen Yugang39736d52019-07-12 16:24:33 +08003011 break;
3012 case NmiSource::chassisCmd:
3013 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::ChassisCmd;
3014 break;
3015 case NmiSource::memoryError:
3016 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::MemoryError;
3017 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003018 case NmiSource::pciBusError:
3019 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PciBusError;
Chen Yugang39736d52019-07-12 16:24:33 +08003020 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003021 case NmiSource::pch:
3022 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PCH;
Chen Yugang39736d52019-07-12 16:24:33 +08003023 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003024 case NmiSource::chipset:
3025 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Chipset;
Chen Yugang39736d52019-07-12 16:24:33 +08003026 break;
3027 default:
3028 phosphor::logging::log<phosphor::logging::level::ERR>(
3029 "NMI source: invalid property!");
3030 return ipmi::responseResponseError();
3031 }
3032
3033 try
3034 {
3035 // keep NMI signal source
3036 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3037 std::string service =
3038 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
Chen Yugang97cf96e2019-11-01 08:55:11 +08003039 setDbusProperty(*dbus, service, oemNmiSourceObjPath, oemNmiSourceIntf,
3040 oemNmiBmcSourceObjPathProp,
3041 nmi::convertForMessage(bmcSourceSignal));
Chen Yugang99be6332019-08-09 16:20:48 +08003042 // set Enabled property to inform NMI source handling
3043 // to trigger a NMI_OUT BSOD.
3044 // if it's triggered by NMI source property changed,
3045 // NMI_OUT BSOD could be missed if the same source occurs twice in a row
3046 if (bmcSourceSignal != nmi::NMISource::BMCSourceSignal::None)
3047 {
3048 setDbusProperty(*dbus, service, oemNmiSourceObjPath,
3049 oemNmiSourceIntf, oemNmiEnabledObjPathProp,
3050 static_cast<bool>(true));
3051 }
Chen Yugang39736d52019-07-12 16:24:33 +08003052 }
3053 catch (sdbusplus::exception_t& e)
3054 {
3055 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3056 return ipmi::responseResponseError();
3057 }
3058
3059 return ipmi::responseSuccess();
3060}
3061
James Feist63efafa2019-07-24 12:39:21 -07003062namespace dimmOffset
3063{
3064constexpr const char* dimmPower = "DimmPower";
3065constexpr const char* staticCltt = "StaticCltt";
3066constexpr const char* offsetPath = "/xyz/openbmc_project/Inventory/Item/Dimm";
3067constexpr const char* offsetInterface =
3068 "xyz.openbmc_project.Inventory.Item.Dimm.Offset";
3069constexpr const char* property = "DimmOffset";
3070
3071}; // namespace dimmOffset
3072
3073ipmi::RspType<>
3074 ipmiOEMSetDimmOffset(uint8_t type,
3075 const std::vector<std::tuple<uint8_t, uint8_t>>& data)
3076{
3077 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3078 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3079 {
3080 return ipmi::responseInvalidFieldRequest();
3081 }
3082
3083 if (data.empty())
3084 {
3085 return ipmi::responseInvalidFieldRequest();
3086 }
3087 nlohmann::json json;
3088
3089 std::ifstream jsonStream(dimmOffsetFile);
3090 if (jsonStream.good())
3091 {
3092 json = nlohmann::json::parse(jsonStream, nullptr, false);
3093 if (json.is_discarded())
3094 {
3095 json = nlohmann::json();
3096 }
3097 jsonStream.close();
3098 }
3099
3100 std::string typeName;
3101 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3102 {
3103 typeName = dimmOffset::dimmPower;
3104 }
3105 else
3106 {
3107 typeName = dimmOffset::staticCltt;
3108 }
3109
3110 nlohmann::json& field = json[typeName];
3111
3112 for (const auto& [index, value] : data)
3113 {
3114 field[index] = value;
3115 }
3116
3117 for (nlohmann::json& val : field)
3118 {
3119 if (val == nullptr)
3120 {
3121 val = static_cast<uint8_t>(0);
3122 }
3123 }
3124
3125 std::ofstream output(dimmOffsetFile);
3126 if (!output.good())
3127 {
3128 std::cerr << "Error writing json file\n";
3129 return ipmi::responseResponseError();
3130 }
3131
3132 output << json.dump(4);
3133
3134 if (type == static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3135 {
3136 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
3137
3138 std::variant<std::vector<uint8_t>> offsets =
3139 field.get<std::vector<uint8_t>>();
3140 auto call = bus->new_method_call(
3141 settingsBusName, dimmOffset::offsetPath, PROP_INTF, "Set");
3142 call.append(dimmOffset::offsetInterface, dimmOffset::property, offsets);
3143 try
3144 {
3145 bus->call(call);
3146 }
3147 catch (sdbusplus::exception_t& e)
3148 {
3149 phosphor::logging::log<phosphor::logging::level::ERR>(
3150 "ipmiOEMSetDimmOffset: can't set dimm offsets!",
3151 phosphor::logging::entry("ERR=%s", e.what()));
3152 return ipmi::responseResponseError();
3153 }
3154 }
3155
3156 return ipmi::responseSuccess();
3157}
3158
3159ipmi::RspType<uint8_t> ipmiOEMGetDimmOffset(uint8_t type, uint8_t index)
3160{
3161
3162 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3163 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3164 {
3165 return ipmi::responseInvalidFieldRequest();
3166 }
3167
3168 std::ifstream jsonStream(dimmOffsetFile);
3169
3170 auto json = nlohmann::json::parse(jsonStream, nullptr, false);
3171 if (json.is_discarded())
3172 {
3173 std::cerr << "File error in " << dimmOffsetFile << "\n";
3174 return ipmi::responseResponseError();
3175 }
3176
3177 std::string typeName;
3178 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3179 {
3180 typeName = dimmOffset::dimmPower;
3181 }
3182 else
3183 {
3184 typeName = dimmOffset::staticCltt;
3185 }
3186
3187 auto it = json.find(typeName);
3188 if (it == json.end())
3189 {
3190 return ipmi::responseInvalidFieldRequest();
3191 }
3192
3193 if (it->size() <= index)
3194 {
3195 return ipmi::responseInvalidFieldRequest();
3196 }
3197
3198 uint8_t resp = it->at(index).get<uint8_t>();
3199 return ipmi::responseSuccess(resp);
3200}
3201
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003202namespace boot_options
3203{
3204
3205using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
3206using IpmiValue = uint8_t;
3207constexpr auto ipmiDefault = 0;
3208
3209std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
3210 {0x01, Source::Sources::Network},
3211 {0x02, Source::Sources::Disk},
3212 {0x05, Source::Sources::ExternalMedia},
3213 {0x0f, Source::Sources::RemovableMedia},
3214 {ipmiDefault, Source::Sources::Default}};
3215
3216std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003217 {0x06, Mode::Modes::Setup}, {ipmiDefault, Mode::Modes::Regular}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003218
3219std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
3220 {Source::Sources::Network, 0x01},
3221 {Source::Sources::Disk, 0x02},
3222 {Source::Sources::ExternalMedia, 0x05},
3223 {Source::Sources::RemovableMedia, 0x0f},
3224 {Source::Sources::Default, ipmiDefault}};
3225
3226std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003227 {Mode::Modes::Setup, 0x06}, {Mode::Modes::Regular, ipmiDefault}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003228
3229static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
3230static constexpr auto bootSourceIntf =
3231 "xyz.openbmc_project.Control.Boot.Source";
3232static constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
3233static constexpr auto persistentObjPath =
3234 "/xyz/openbmc_project/control/host0/boot";
3235static constexpr auto oneTimePath =
3236 "/xyz/openbmc_project/control/host0/boot/one_time";
3237static constexpr auto bootSourceProp = "BootSource";
3238static constexpr auto bootModeProp = "BootMode";
3239static constexpr auto oneTimeBootEnableProp = "Enabled";
3240static constexpr auto httpBootMode =
3241 "xyz.openbmc_project.Control.Boot.Source.Sources.Http";
3242
3243enum class BootOptionParameter : size_t
3244{
3245 setInProgress = 0x0,
3246 bootFlags = 0x5,
3247};
3248static constexpr uint8_t setComplete = 0x0;
3249static constexpr uint8_t setInProgress = 0x1;
3250static uint8_t transferStatus = setComplete;
3251static constexpr uint8_t setParmVersion = 0x01;
3252static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
3253static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
3254static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
3255static constexpr uint8_t httpBoot = 0xd;
3256static constexpr uint8_t bootSourceMask = 0x3c;
3257
3258} // namespace boot_options
3259
3260ipmi::RspType<uint8_t, // version
3261 uint8_t, // param
3262 uint8_t, // data0, dependent on parameter
3263 std::optional<uint8_t> // data1, dependent on parameter
3264 >
3265 ipmiOemGetEfiBootOptions(uint8_t parameter, uint8_t set, uint8_t block)
3266{
3267 using namespace boot_options;
3268 uint8_t bootOption = 0;
3269
3270 if (parameter == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3271 {
3272 return ipmi::responseSuccess(setParmVersion, parameter, transferStatus,
3273 std::nullopt);
3274 }
3275
3276 if (parameter != static_cast<uint8_t>(BootOptionParameter::bootFlags))
3277 {
3278 phosphor::logging::log<phosphor::logging::level::ERR>(
3279 "Unsupported parameter");
3280 return ipmi::responseResponseError();
3281 }
3282
3283 try
3284 {
3285 auto oneTimeEnabled = false;
3286 // read one time Enabled property
3287 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3288 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3289 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3290 enabledIntf, oneTimeBootEnableProp);
3291 oneTimeEnabled = std::get<bool>(variant);
3292
3293 // get BootSource and BootMode properties
3294 // according to oneTimeEnable
3295 auto bootObjPath = oneTimePath;
3296 if (oneTimeEnabled == false)
3297 {
3298 bootObjPath = persistentObjPath;
3299 }
3300
3301 service = getService(*dbus, bootModeIntf, bootObjPath);
3302 variant = getDbusProperty(*dbus, service, bootObjPath, bootModeIntf,
3303 bootModeProp);
3304
3305 auto bootMode =
3306 Mode::convertModesFromString(std::get<std::string>(variant));
3307
3308 service = getService(*dbus, bootSourceIntf, bootObjPath);
3309 variant = getDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3310 bootSourceProp);
3311
3312 if (std::get<std::string>(variant) == httpBootMode)
3313 {
3314 bootOption = httpBoot;
3315 }
3316 else
3317 {
3318 auto bootSource = Source::convertSourcesFromString(
3319 std::get<std::string>(variant));
3320 bootOption = sourceDbusToIpmi.at(bootSource);
3321 if (Source::Sources::Default == bootSource)
3322 {
3323 bootOption = modeDbusToIpmi.at(bootMode);
3324 }
3325 }
3326
3327 uint8_t oneTime = oneTimeEnabled ? setParmBootFlagsValidOneTime
3328 : setParmBootFlagsValidPermanent;
3329 bootOption <<= 2; // shift for responseconstexpr
3330 return ipmi::responseSuccess(setParmVersion, parameter, oneTime,
3331 bootOption);
3332 }
3333 catch (sdbusplus::exception_t& e)
3334 {
3335 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3336 return ipmi::responseResponseError();
3337 }
3338}
3339
3340ipmi::RspType<> ipmiOemSetEfiBootOptions(uint8_t bootFlag, uint8_t bootParam,
3341 std::optional<uint8_t> bootOption)
3342{
3343 using namespace boot_options;
3344 auto oneTimeEnabled = false;
3345
3346 if (bootFlag == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3347 {
3348 if (bootOption)
3349 {
3350 return ipmi::responseReqDataLenInvalid();
3351 }
3352
3353 if (transferStatus == setInProgress)
3354 {
3355 phosphor::logging::log<phosphor::logging::level::ERR>(
3356 "boot option set in progress!");
3357 return ipmi::responseResponseError();
3358 }
3359
3360 transferStatus = bootParam;
3361 return ipmi::responseSuccess();
3362 }
3363
3364 if (bootFlag != (uint8_t)BootOptionParameter::bootFlags)
3365 {
3366 phosphor::logging::log<phosphor::logging::level::ERR>(
3367 "Unsupported parameter");
3368 return ipmi::responseResponseError();
3369 }
3370
3371 if (!bootOption)
3372 {
3373 return ipmi::responseReqDataLenInvalid();
3374 }
3375
3376 if (((bootOption.value() & bootSourceMask) >> 2) !=
3377 httpBoot) // not http boot, exit
3378 {
3379 phosphor::logging::log<phosphor::logging::level::ERR>(
3380 "wrong boot option parameter!");
3381 return ipmi::responseParmOutOfRange();
3382 }
3383
3384 try
3385 {
3386 bool permanent = (bootParam & setParmBootFlagsPermanent) ==
3387 setParmBootFlagsPermanent;
3388
3389 // read one time Enabled property
3390 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3391 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3392 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3393 enabledIntf, oneTimeBootEnableProp);
3394 oneTimeEnabled = std::get<bool>(variant);
3395
3396 /*
3397 * Check if the current boot setting is onetime or permanent, if the
3398 * request in the command is otherwise, then set the "Enabled"
3399 * property in one_time object path to 'True' to indicate onetime
3400 * and 'False' to indicate permanent.
3401 *
3402 * Once the onetime/permanent setting is applied, then the bootMode
3403 * and bootSource is updated for the corresponding object.
3404 */
3405 if (permanent == oneTimeEnabled)
3406 {
3407 setDbusProperty(*dbus, service, oneTimePath, enabledIntf,
3408 oneTimeBootEnableProp, !permanent);
3409 }
3410
3411 // set BootSource and BootMode properties
3412 // according to oneTimeEnable or persistent
3413 auto bootObjPath = oneTimePath;
3414 if (oneTimeEnabled == false)
3415 {
3416 bootObjPath = persistentObjPath;
3417 }
3418 std::string bootMode =
3419 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
3420 std::string bootSource = httpBootMode;
3421
3422 service = getService(*dbus, bootModeIntf, bootObjPath);
3423 setDbusProperty(*dbus, service, bootObjPath, bootModeIntf, bootModeProp,
3424 bootMode);
3425
3426 service = getService(*dbus, bootSourceIntf, bootObjPath);
3427 setDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3428 bootSourceProp, bootSource);
3429 }
3430 catch (sdbusplus::exception_t& e)
3431 {
3432 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3433 return ipmi::responseResponseError();
3434 }
3435
3436 return ipmi::responseSuccess();
3437}
3438
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003439using BasicVariantType =
3440 std::variant<std::vector<std::string>, std::vector<uint64_t>, std::string,
3441 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
3442 uint16_t, uint8_t, bool>;
3443using PropertyMapType =
3444 boost::container::flat_map<std::string, BasicVariantType>;
3445static constexpr const std::array<const char*, 1> psuPresenceTypes = {
3446 "xyz.openbmc_project.Configuration.PSUPresence"};
3447int getPSUAddress(ipmi::Context::ptr ctx, uint8_t& bus,
3448 std::vector<uint64_t>& addrTable)
3449{
3450 boost::system::error_code ec;
3451 GetSubTreeType subtree = ctx->bus->yield_method_call<GetSubTreeType>(
3452 ctx->yield, ec, "xyz.openbmc_project.ObjectMapper",
3453 "/xyz/openbmc_project/object_mapper",
3454 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3455 "/xyz/openbmc_project/inventory/system", 3, psuPresenceTypes);
3456 if (ec)
3457 {
3458 phosphor::logging::log<phosphor::logging::level::ERR>(
3459 "Failed to set dbus property to cold redundancy");
3460 return -1;
3461 }
3462 for (const auto& object : subtree)
3463 {
3464 std::string pathName = object.first;
3465 for (const auto& serviceIface : object.second)
3466 {
3467 std::string serviceName = serviceIface.first;
3468
3469 ec.clear();
3470 PropertyMapType propMap =
3471 ctx->bus->yield_method_call<PropertyMapType>(
3472 ctx->yield, ec, serviceName, pathName,
3473 "org.freedesktop.DBus.Properties", "GetAll",
3474 "xyz.openbmc_project.Configuration.PSUPresence");
3475 if (ec)
3476 {
3477 phosphor::logging::log<phosphor::logging::level::ERR>(
3478 "Failed to set dbus property to cold redundancy");
3479 return -1;
3480 }
3481 auto psuBus = std::get_if<uint64_t>(&propMap["Bus"]);
3482 auto psuAddress =
3483 std::get_if<std::vector<uint64_t>>(&propMap["Address"]);
3484
3485 if (psuBus == nullptr || psuAddress == nullptr)
3486 {
3487 std::cerr << "error finding necessary "
3488 "entry in configuration\n";
3489 return -1;
3490 }
3491 bus = static_cast<uint8_t>(*psuBus);
3492 addrTable = *psuAddress;
3493 return 0;
3494 }
3495 }
3496 return -1;
3497}
3498
3499static const constexpr uint8_t addrOffset = 8;
3500static const constexpr uint8_t psuRevision = 0xd9;
3501static const constexpr uint8_t defaultPSUBus = 7;
3502// Second Minor, Primary Minor, Major
3503static const constexpr size_t verLen = 3;
3504ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPSUVersion(ipmi::Context::ptr ctx)
3505{
3506 uint8_t bus = defaultPSUBus;
3507 std::vector<uint64_t> addrTable;
3508 std::vector<uint8_t> result;
3509 if (getPSUAddress(ctx, bus, addrTable))
3510 {
3511 std::cerr << "Failed to get PSU bus and address\n";
3512 return ipmi::responseResponseError();
3513 }
3514
3515 for (const auto& slaveAddr : addrTable)
3516 {
3517 std::vector<uint8_t> writeData = {psuRevision};
3518 std::vector<uint8_t> readBuf(verLen);
3519 uint8_t addr = static_cast<uint8_t>(slaveAddr) + addrOffset;
3520 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
3521
3522 auto retI2C = ipmi::i2cWriteRead(i2cBus, addr, writeData, readBuf);
3523 if (retI2C != ipmi::ccSuccess)
3524 {
3525 for (size_t idx = 0; idx < verLen; idx++)
3526 {
3527 result.emplace_back(0x00);
3528 }
3529 }
3530 else
3531 {
3532 for (const uint8_t& data : readBuf)
3533 {
3534 result.emplace_back(data);
3535 }
3536 }
3537 }
3538
3539 return ipmi::responseSuccess(result);
3540}
3541
AppaRao Puli28972062019-11-11 02:04:45 +05303542/** @brief implements the maximum size of
3543 * bridgeable messages used between KCS and
3544 * IPMB interfacesget security mode command.
3545 *
3546 * @returns IPMI completion code with following data
3547 * - KCS Buffer Size (In multiples of four bytes)
3548 * - IPMB Buffer Size (In multiples of four bytes)
3549 **/
3550ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetBufferSize()
3551{
3552 // for now this is hard coded; really this number is dependent on
3553 // the BMC kcs driver as well as the host kcs driver....
3554 // we can't know the latter.
3555 uint8_t kcsMaxBufferSize = 63 / 4;
3556 uint8_t ipmbMaxBufferSize = 128 / 4;
3557
3558 return ipmi::responseSuccess(kcsMaxBufferSize, ipmbMaxBufferSize);
3559}
3560
Jason M. Bills64796042018-10-03 16:51:55 -07003561static void registerOEMFunctions(void)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003562{
3563 phosphor::logging::log<phosphor::logging::level::INFO>(
3564 "Registering OEM commands");
Vernon Mauery98bbf692019-09-16 11:14:59 -07003565 ipmiPrintAndRegister(intel::netFnGeneral, IPMI_CMD_WILDCARD, NULL,
Jason M. Bills64796042018-10-03 16:51:55 -07003566 ipmiOEMWildcard,
3567 PRIVILEGE_USER); // wildcard default handler
Vernon Mauery98bbf692019-09-16 11:14:59 -07003568
3569 ipmiPrintAndRegister(intel::netFnApp, IPMI_CMD_WILDCARD, NULL,
Jason M. Bills64796042018-10-03 16:51:55 -07003570 ipmiOEMWildcard,
3571 PRIVILEGE_USER); // wildcard default handler
Vernon Mauery98bbf692019-09-16 11:14:59 -07003572
3573 ipmiPrintAndRegister(intel::netFnGeneral,
3574 intel::general::cmdGetChassisIdentifier, NULL,
3575 ipmiOEMGetChassisIdentifier,
3576 PRIVILEGE_USER); // get chassis identifier
3577
3578 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetSystemGUID,
3579 NULL, ipmiOEMSetSystemGUID,
3580 PRIVILEGE_ADMIN); // set system guid
Jason M. Billsb02bf092019-08-15 13:01:56 -07003581
3582 // <Disable BMC System Reset Action>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003583 registerHandler(prioOemBase, intel::netFnGeneral,
3584 intel::general::cmdDisableBMCSystemReset, Privilege::Admin,
3585 ipmiOEMDisableBMCSystemReset);
3586
Jason M. Billsb02bf092019-08-15 13:01:56 -07003587 // <Get BMC Reset Disables>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003588 registerHandler(prioOemBase, intel::netFnGeneral,
3589 intel::general::cmdGetBMCResetDisables, Privilege::Admin,
3590 ipmiOEMGetBMCResetDisables);
Jason M. Billsb02bf092019-08-15 13:01:56 -07003591
Vernon Mauery98bbf692019-09-16 11:14:59 -07003592 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetBIOSID,
3593 NULL, ipmiOEMSetBIOSID, PRIVILEGE_ADMIN);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003594
Chen Yugang7a04f3a2019-10-08 11:12:35 +08003595 registerHandler(prioOemBase, intel::netFnGeneral,
3596 intel::general::cmdGetOEMDeviceInfo, Privilege::User,
3597 ipmiOEMGetDeviceInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003598
Vernon Mauery98bbf692019-09-16 11:14:59 -07003599 ipmiPrintAndRegister(intel::netFnGeneral,
3600 intel::general::cmdGetAICSlotFRUIDSlotPosRecords, NULL,
3601 ipmiOEMGetAICFRU, PRIVILEGE_USER);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303602
Vernon Mauery98bbf692019-09-16 11:14:59 -07003603 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3604 intel::general::cmdSendEmbeddedFWUpdStatus,
3605 Privilege::Operator, ipmiOEMSendEmbeddedFwUpdStatus);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303606
Vernon Mauery98bbf692019-09-16 11:14:59 -07003607 ipmiPrintAndRegister(intel::netFnGeneral,
3608 intel::general::cmdSetPowerRestoreDelay, NULL,
3609 ipmiOEMSetPowerRestoreDelay, PRIVILEGE_OPERATOR);
3610
3611 ipmiPrintAndRegister(intel::netFnGeneral,
3612 intel::general::cmdGetPowerRestoreDelay, NULL,
3613 ipmiOEMGetPowerRestoreDelay, PRIVILEGE_USER);
3614
3615 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3616 intel::general::cmdSetOEMUser2Activation,
3617 Privilege::Callback, ipmiOEMSetUser2Activation);
3618
3619 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3620 intel::general::cmdSetSpecialUserPassword,
3621 Privilege::Callback, ipmiOEMSetSpecialUserPassword);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05303622
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003623 // <Get Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003624 registerHandler(prioOemBase, intel::netFnGeneral,
3625 intel::general::cmdGetProcessorErrConfig, Privilege::User,
3626 ipmiOEMGetProcessorErrConfig);
3627
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003628 // <Set Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003629 registerHandler(prioOemBase, intel::netFnGeneral,
3630 intel::general::cmdSetProcessorErrConfig, Privilege::Admin,
3631 ipmiOEMSetProcessorErrConfig);
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003632
Vernon Mauery98bbf692019-09-16 11:14:59 -07003633 ipmiPrintAndRegister(intel::netFnGeneral,
3634 intel::general::cmdSetShutdownPolicy, NULL,
3635 ipmiOEMSetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003636
Vernon Mauery98bbf692019-09-16 11:14:59 -07003637 ipmiPrintAndRegister(intel::netFnGeneral,
3638 intel::general::cmdGetShutdownPolicy, NULL,
3639 ipmiOEMGetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003640
anil kumar appanaf945eee2019-09-25 23:29:11 +00003641 registerHandler(prioOemBase, intel::netFnGeneral,
3642 intel::general::cmdSetFanConfig, Privilege::User,
3643 ipmiOEMSetFanConfig);
James Feist91244a62019-02-19 15:04:54 -08003644
Vernon Mauery98bbf692019-09-16 11:14:59 -07003645 registerHandler(prioOemBase, intel::netFnGeneral,
3646 intel::general::cmdGetFanConfig, Privilege::User,
3647 ipmiOEMGetFanConfig);
James Feist5f957ca2019-03-14 15:33:55 -07003648
Vernon Mauery98bbf692019-09-16 11:14:59 -07003649 registerHandler(prioOemBase, intel::netFnGeneral,
3650 intel::general::cmdGetFanSpeedOffset, Privilege::User,
3651 ipmiOEMGetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003652
Vernon Mauery98bbf692019-09-16 11:14:59 -07003653 registerHandler(prioOemBase, intel::netFnGeneral,
3654 intel::general::cmdSetFanSpeedOffset, Privilege::User,
3655 ipmiOEMSetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003656
Vernon Mauery98bbf692019-09-16 11:14:59 -07003657 registerHandler(prioOemBase, intel::netFnGeneral,
3658 intel::general::cmdSetFscParameter, Privilege::User,
3659 ipmiOEMSetFscParameter);
James Feist5f957ca2019-03-14 15:33:55 -07003660
Vernon Mauery98bbf692019-09-16 11:14:59 -07003661 registerHandler(prioOemBase, intel::netFnGeneral,
3662 intel::general::cmdGetFscParameter, Privilege::User,
3663 ipmiOEMGetFscParameter);
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05303664
Vernon Mauery98bbf692019-09-16 11:14:59 -07003665 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3666 intel::general::cmdReadBaseBoardProductId, Privilege::Admin,
3667 ipmiOEMReadBoardProductId);
Chen Yugang39736d52019-07-12 16:24:33 +08003668
Vernon Mauery98bbf692019-09-16 11:14:59 -07003669 registerHandler(prioOemBase, intel::netFnGeneral,
3670 intel::general::cmdGetNmiStatus, Privilege::User,
3671 ipmiOEMGetNmiSource);
Chen Yugang39736d52019-07-12 16:24:33 +08003672
Vernon Mauery98bbf692019-09-16 11:14:59 -07003673 registerHandler(prioOemBase, intel::netFnGeneral,
3674 intel::general::cmdSetNmiStatus, Privilege::Operator,
3675 ipmiOEMSetNmiSource);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003676
Vernon Mauery98bbf692019-09-16 11:14:59 -07003677 registerHandler(prioOemBase, intel::netFnGeneral,
3678 intel::general::cmdGetEfiBootOptions, Privilege::User,
3679 ipmiOemGetEfiBootOptions);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003680
Vernon Mauery98bbf692019-09-16 11:14:59 -07003681 registerHandler(prioOemBase, intel::netFnGeneral,
3682 intel::general::cmdSetEfiBootOptions, Privilege::Operator,
3683 ipmiOemSetEfiBootOptions);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303684
Vernon Mauery98bbf692019-09-16 11:14:59 -07003685 registerHandler(prioOemBase, intel::netFnGeneral,
3686 intel::general::cmdGetSecurityMode, Privilege::User,
3687 ipmiGetSecurityMode);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303688
Vernon Mauery98bbf692019-09-16 11:14:59 -07003689 registerHandler(prioOemBase, intel::netFnGeneral,
3690 intel::general::cmdSetSecurityMode, Privilege::Admin,
3691 ipmiSetSecurityMode);
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003692
Vernon Mauery98bbf692019-09-16 11:14:59 -07003693 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdGetLEDStatus,
3694 NULL, ipmiOEMGetLEDStatus, PRIVILEGE_ADMIN);
Cheng C Yang773703a2019-08-15 09:41:11 +08003695
Vernon Mauery98bbf692019-09-16 11:14:59 -07003696 ipmiPrintAndRegister(ipmi::intel::netFnPlatform,
3697 ipmi::intel::platform::cmdCfgHostSerialPortSpeed, NULL,
3698 ipmiOEMCfgHostSerialPortSpeed, PRIVILEGE_ADMIN);
3699
3700 registerHandler(prioOemBase, intel::netFnGeneral,
3701 intel::general::cmdSetFaultIndication, Privilege::Operator,
3702 ipmiOEMSetFaultIndication);
3703
3704 registerHandler(prioOemBase, intel::netFnGeneral,
3705 intel::general::cmdSetColdRedundancyConfig, Privilege::User,
3706 ipmiOEMSetCRConfig);
3707
3708 registerHandler(prioOemBase, intel::netFnGeneral,
3709 intel::general::cmdGetColdRedundancyConfig, Privilege::User,
3710 ipmiOEMGetCRConfig);
3711
3712 registerHandler(prioOemBase, intel::netFnGeneral,
3713 intel::general::cmdRestoreConfiguration, Privilege::Admin,
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003714 ipmiRestoreConfiguration);
James Feist63efafa2019-07-24 12:39:21 -07003715
Vernon Mauery98bbf692019-09-16 11:14:59 -07003716 registerHandler(prioOemBase, intel::netFnGeneral,
3717 intel::general::cmdSetDimmOffset, Privilege::Operator,
3718 ipmiOEMSetDimmOffset);
James Feist63efafa2019-07-24 12:39:21 -07003719
Vernon Mauery98bbf692019-09-16 11:14:59 -07003720 registerHandler(prioOemBase, intel::netFnGeneral,
3721 intel::general::cmdGetDimmOffset, Privilege::Operator,
3722 ipmiOEMGetDimmOffset);
Chen Yugangca12a7b2019-09-03 18:11:44 +08003723
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003724 registerHandler(prioOemBase, intel::netFnGeneral,
3725 intel::general::cmdGetPSUVersion, Privilege::User,
3726 ipmiOEMGetPSUVersion);
AppaRao Puli28972062019-11-11 02:04:45 +05303727
3728 registerHandler(prioOemBase, intel::netFnGeneral,
3729 intel::general::cmdGetBufferSize, Privilege::User,
3730 ipmiOEMGetBufferSize);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003731}
3732
3733} // namespace ipmi