blob: f7d1c6da4a10cdb39d91322b219848d45e7c9581 [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
James Feist5b693632019-07-09 09:06:09 -07001601 return ipmi::responseSuccess(0, 0, flags, 0);
James Feist91244a62019-02-19 15:04:54 -08001602}
James Feist5f957ca2019-03-14 15:33:55 -07001603constexpr const char* cfmLimitSettingPath =
1604 "/xyz/openbmc_project/control/cfm_limit";
1605constexpr const char* cfmLimitIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistfaa4f222019-03-21 16:21:55 -07001606constexpr const size_t legacyExitAirSensorNumber = 0x2e;
James Feist09f6b602019-08-08 11:30:03 -07001607constexpr const size_t legacyPCHSensorNumber = 0x22;
1608constexpr const char* exitAirPathName = "Exit_Air";
1609constexpr const char* pchPathName = "SSB_Temp";
James Feistacc8a4e2019-04-02 14:23:57 -07001610constexpr const char* pidConfigurationIface =
1611 "xyz.openbmc_project.Configuration.Pid";
James Feistfaa4f222019-03-21 16:21:55 -07001612
James Feist09f6b602019-08-08 11:30:03 -07001613static std::string getConfigPath(const std::string& name)
James Feistfaa4f222019-03-21 16:21:55 -07001614{
Vernon Mauery15419dd2019-05-24 09:40:30 -07001615 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistfaa4f222019-03-21 16:21:55 -07001616 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001617 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1618 "/xyz/openbmc_project/object_mapper",
1619 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistfaa4f222019-03-21 16:21:55 -07001620
James Feistacc8a4e2019-04-02 14:23:57 -07001621 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
James Feistfaa4f222019-03-21 16:21:55 -07001622 std::string path;
1623 GetSubTreeType resp;
1624 try
1625 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001626 auto reply = dbus->call(method);
James Feistfaa4f222019-03-21 16:21:55 -07001627 reply.read(resp);
1628 }
1629 catch (sdbusplus::exception_t&)
1630 {
1631 phosphor::logging::log<phosphor::logging::level::ERR>(
1632 "ipmiOEMGetFscParameter: mapper error");
1633 };
James Feist09f6b602019-08-08 11:30:03 -07001634 auto config =
1635 std::find_if(resp.begin(), resp.end(), [&name](const auto& pair) {
1636 return pair.first.find(name) != std::string::npos;
1637 });
James Feistfaa4f222019-03-21 16:21:55 -07001638 if (config != resp.end())
1639 {
1640 path = std::move(config->first);
1641 }
1642 return path;
1643}
James Feist5f957ca2019-03-14 15:33:55 -07001644
James Feistacc8a4e2019-04-02 14:23:57 -07001645// flat map to make alphabetical
1646static boost::container::flat_map<std::string, PropertyMap> getPidConfigs()
1647{
1648 boost::container::flat_map<std::string, PropertyMap> ret;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001649 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001650 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001651 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1652 "/xyz/openbmc_project/object_mapper",
1653 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistacc8a4e2019-04-02 14:23:57 -07001654
1655 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
1656 GetSubTreeType resp;
1657
1658 try
1659 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001660 auto reply = dbus->call(method);
James Feistacc8a4e2019-04-02 14:23:57 -07001661 reply.read(resp);
1662 }
1663 catch (sdbusplus::exception_t&)
1664 {
1665 phosphor::logging::log<phosphor::logging::level::ERR>(
1666 "getFanConfigPaths: mapper error");
1667 };
1668 for (const auto& [path, objects] : resp)
1669 {
1670 if (objects.empty())
1671 {
1672 continue; // should be impossible
1673 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04001674
1675 try
1676 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001677 ret.emplace(path,
1678 getAllDbusProperties(*dbus, objects[0].first, path,
1679 pidConfigurationIface));
Zhu, Yungebe560b02019-04-21 21:19:21 -04001680 }
1681 catch (sdbusplus::exception_t& e)
1682 {
1683 phosphor::logging::log<phosphor::logging::level::ERR>(
1684 "getPidConfigs: can't get DbusProperties!",
1685 phosphor::logging::entry("ERR=%s", e.what()));
1686 }
James Feistacc8a4e2019-04-02 14:23:57 -07001687 }
1688 return ret;
1689}
1690
1691ipmi::RspType<uint8_t> ipmiOEMGetFanSpeedOffset(void)
1692{
1693 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1694 if (data.empty())
1695 {
1696 return ipmi::responseResponseError();
1697 }
1698 uint8_t minOffset = std::numeric_limits<uint8_t>::max();
1699 for (const auto& [_, pid] : data)
1700 {
1701 auto findClass = pid.find("Class");
1702 if (findClass == pid.end())
1703 {
1704 phosphor::logging::log<phosphor::logging::level::ERR>(
1705 "ipmiOEMGetFscParameter: found illegal pid "
1706 "configurations");
1707 return ipmi::responseResponseError();
1708 }
1709 std::string type = std::get<std::string>(findClass->second);
1710 if (type == "fan")
1711 {
1712 auto findOutLimit = pid.find("OutLimitMin");
1713 if (findOutLimit == pid.end())
1714 {
1715 phosphor::logging::log<phosphor::logging::level::ERR>(
1716 "ipmiOEMGetFscParameter: found illegal pid "
1717 "configurations");
1718 return ipmi::responseResponseError();
1719 }
1720 // get the min out of all the offsets
1721 minOffset = std::min(
1722 minOffset,
1723 static_cast<uint8_t>(std::get<double>(findOutLimit->second)));
1724 }
1725 }
1726 if (minOffset == std::numeric_limits<uint8_t>::max())
1727 {
1728 phosphor::logging::log<phosphor::logging::level::ERR>(
1729 "ipmiOEMGetFscParameter: found no fan configurations!");
1730 return ipmi::responseResponseError();
1731 }
1732
1733 return ipmi::responseSuccess(minOffset);
1734}
1735
1736ipmi::RspType<> ipmiOEMSetFanSpeedOffset(uint8_t offset)
1737{
1738 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1739 if (data.empty())
1740 {
1741
1742 phosphor::logging::log<phosphor::logging::level::ERR>(
1743 "ipmiOEMSetFanSpeedOffset: found no pid configurations!");
1744 return ipmi::responseResponseError();
1745 }
1746
Vernon Mauery15419dd2019-05-24 09:40:30 -07001747 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001748 bool found = false;
1749 for (const auto& [path, pid] : data)
1750 {
1751 auto findClass = pid.find("Class");
1752 if (findClass == pid.end())
1753 {
1754
1755 phosphor::logging::log<phosphor::logging::level::ERR>(
1756 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1757 "configurations");
1758 return ipmi::responseResponseError();
1759 }
1760 std::string type = std::get<std::string>(findClass->second);
1761 if (type == "fan")
1762 {
1763 auto findOutLimit = pid.find("OutLimitMin");
1764 if (findOutLimit == pid.end())
1765 {
1766
1767 phosphor::logging::log<phosphor::logging::level::ERR>(
1768 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1769 "configurations");
1770 return ipmi::responseResponseError();
1771 }
Vernon Mauery15419dd2019-05-24 09:40:30 -07001772 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager",
James Feistacc8a4e2019-04-02 14:23:57 -07001773 path, pidConfigurationIface, "OutLimitMin",
1774 static_cast<double>(offset));
1775 found = true;
1776 }
1777 }
1778 if (!found)
1779 {
1780 phosphor::logging::log<phosphor::logging::level::ERR>(
1781 "ipmiOEMSetFanSpeedOffset: set no fan offsets");
1782 return ipmi::responseResponseError();
1783 }
1784
1785 return ipmi::responseSuccess();
1786}
1787
1788ipmi::RspType<> ipmiOEMSetFscParameter(uint8_t command, uint8_t param1,
1789 uint8_t param2)
James Feist5f957ca2019-03-14 15:33:55 -07001790{
1791 constexpr const size_t disableLimiting = 0x0;
1792
Vernon Mauery15419dd2019-05-24 09:40:30 -07001793 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001794 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001795 {
James Feist09f6b602019-08-08 11:30:03 -07001796 std::string pathName;
James Feistacc8a4e2019-04-02 14:23:57 -07001797 if (param1 == legacyExitAirSensorNumber)
James Feistfaa4f222019-03-21 16:21:55 -07001798 {
James Feist09f6b602019-08-08 11:30:03 -07001799 pathName = exitAirPathName;
1800 }
1801 else if (param1 == legacyPCHSensorNumber)
1802 {
1803 pathName = pchPathName;
James Feistfaa4f222019-03-21 16:21:55 -07001804 }
1805 else
1806 {
James Feistacc8a4e2019-04-02 14:23:57 -07001807 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001808 }
James Feist09f6b602019-08-08 11:30:03 -07001809 std::string path = getConfigPath(pathName);
1810 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager", path,
1811 pidConfigurationIface, "SetPoint",
1812 static_cast<double>(param2));
1813 return ipmi::responseSuccess();
James Feistfaa4f222019-03-21 16:21:55 -07001814 }
James Feistacc8a4e2019-04-02 14:23:57 -07001815 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001816 {
James Feistacc8a4e2019-04-02 14:23:57 -07001817 uint16_t cfm = param1 | (static_cast<uint16_t>(param2) << 8);
James Feist5f957ca2019-03-14 15:33:55 -07001818
1819 // must be greater than 50 based on eps
1820 if (cfm < 50 && cfm != disableLimiting)
1821 {
James Feistacc8a4e2019-04-02 14:23:57 -07001822 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001823 }
1824
1825 try
1826 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001827 ipmi::setDbusProperty(*dbus, settingsBusName, cfmLimitSettingPath,
James Feist5f957ca2019-03-14 15:33:55 -07001828 cfmLimitIface, "Limit",
1829 static_cast<double>(cfm));
1830 }
1831 catch (sdbusplus::exception_t& e)
1832 {
1833 phosphor::logging::log<phosphor::logging::level::ERR>(
1834 "ipmiOEMSetFscParameter: can't set cfm setting!",
1835 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07001836 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07001837 }
James Feistacc8a4e2019-04-02 14:23:57 -07001838 return ipmi::responseSuccess();
1839 }
1840 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1841 {
1842 constexpr const size_t maxDomainCount = 8;
1843 uint8_t requestedDomainMask = param1;
1844 boost::container::flat_map data = getPidConfigs();
1845 if (data.empty())
1846 {
1847
1848 phosphor::logging::log<phosphor::logging::level::ERR>(
1849 "ipmiOEMSetFscParameter: found no pid configurations!");
1850 return ipmi::responseResponseError();
1851 }
1852 size_t count = 0;
1853 for (const auto& [path, pid] : data)
1854 {
1855 auto findClass = pid.find("Class");
1856 if (findClass == pid.end())
1857 {
1858
1859 phosphor::logging::log<phosphor::logging::level::ERR>(
1860 "ipmiOEMSetFscParameter: found illegal pid "
1861 "configurations");
1862 return ipmi::responseResponseError();
1863 }
1864 std::string type = std::get<std::string>(findClass->second);
1865 if (type == "fan")
1866 {
1867 if (requestedDomainMask & (1 << count))
1868 {
1869 ipmi::setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07001870 *dbus, "xyz.openbmc_project.EntityManager", path,
James Feistacc8a4e2019-04-02 14:23:57 -07001871 pidConfigurationIface, "OutLimitMax",
1872 static_cast<double>(param2));
1873 }
1874 count++;
1875 }
1876 }
1877 return ipmi::responseSuccess();
James Feist5f957ca2019-03-14 15:33:55 -07001878 }
1879 else
1880 {
1881 // todo other command parts possibly
1882 // tcontrol is handled in peci now
1883 // fan speed offset not implemented yet
1884 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07001885 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001886 }
1887}
1888
James Feistacc8a4e2019-04-02 14:23:57 -07001889ipmi::RspType<
1890 std::variant<uint8_t, std::array<uint8_t, 2>, std::array<uint16_t, 2>>>
1891 ipmiOEMGetFscParameter(uint8_t command, std::optional<uint8_t> param)
James Feist5f957ca2019-03-14 15:33:55 -07001892{
James Feist09f6b602019-08-08 11:30:03 -07001893 constexpr uint8_t legacyDefaultSetpoint = -128;
James Feist5f957ca2019-03-14 15:33:55 -07001894
Vernon Mauery15419dd2019-05-24 09:40:30 -07001895 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001896 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001897 {
James Feistacc8a4e2019-04-02 14:23:57 -07001898 if (!param)
James Feistfaa4f222019-03-21 16:21:55 -07001899 {
James Feistacc8a4e2019-04-02 14:23:57 -07001900 return ipmi::responseReqDataLenInvalid();
James Feistfaa4f222019-03-21 16:21:55 -07001901 }
1902
James Feist09f6b602019-08-08 11:30:03 -07001903 std::string pathName;
1904
1905 if (*param == legacyExitAirSensorNumber)
1906 {
1907 pathName = exitAirPathName;
1908 }
1909 else if (*param == legacyPCHSensorNumber)
1910 {
1911 pathName = pchPathName;
1912 }
1913 else
James Feistfaa4f222019-03-21 16:21:55 -07001914 {
James Feistacc8a4e2019-04-02 14:23:57 -07001915 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001916 }
James Feist09f6b602019-08-08 11:30:03 -07001917
1918 uint8_t setpoint = legacyDefaultSetpoint;
1919 std::string path = getConfigPath(pathName);
James Feistfaa4f222019-03-21 16:21:55 -07001920 if (path.size())
1921 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001922 Value val = ipmi::getDbusProperty(
1923 *dbus, "xyz.openbmc_project.EntityManager", path,
1924 pidConfigurationIface, "SetPoint");
James Feistfaa4f222019-03-21 16:21:55 -07001925 setpoint = std::floor(std::get<double>(val) + 0.5);
1926 }
1927
1928 // old implementation used to return the "default" and current, we
1929 // don't make the default readily available so just make both the
1930 // same
James Feistfaa4f222019-03-21 16:21:55 -07001931
James Feistacc8a4e2019-04-02 14:23:57 -07001932 return ipmi::responseSuccess(
1933 std::array<uint8_t, 2>{setpoint, setpoint});
James Feistfaa4f222019-03-21 16:21:55 -07001934 }
James Feistacc8a4e2019-04-02 14:23:57 -07001935 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1936 {
1937 constexpr const size_t maxDomainCount = 8;
1938
1939 if (!param)
1940 {
1941 return ipmi::responseReqDataLenInvalid();
1942 }
1943 uint8_t requestedDomain = *param;
1944 if (requestedDomain >= maxDomainCount)
1945 {
1946 return ipmi::responseInvalidFieldRequest();
1947 }
1948
1949 boost::container::flat_map data = getPidConfigs();
1950 if (data.empty())
1951 {
1952 phosphor::logging::log<phosphor::logging::level::ERR>(
1953 "ipmiOEMGetFscParameter: found no pid configurations!");
1954 return ipmi::responseResponseError();
1955 }
1956 size_t count = 0;
1957 for (const auto& [_, pid] : data)
1958 {
1959 auto findClass = pid.find("Class");
1960 if (findClass == pid.end())
1961 {
1962 phosphor::logging::log<phosphor::logging::level::ERR>(
1963 "ipmiOEMGetFscParameter: found illegal pid "
1964 "configurations");
1965 return ipmi::responseResponseError();
1966 }
1967 std::string type = std::get<std::string>(findClass->second);
1968 if (type == "fan")
1969 {
1970 if (requestedDomain == count)
1971 {
1972 auto findOutLimit = pid.find("OutLimitMax");
1973 if (findOutLimit == pid.end())
1974 {
1975 phosphor::logging::log<phosphor::logging::level::ERR>(
1976 "ipmiOEMGetFscParameter: found illegal pid "
1977 "configurations");
1978 return ipmi::responseResponseError();
1979 }
1980
1981 return ipmi::responseSuccess(
1982 static_cast<uint8_t>(std::floor(
1983 std::get<double>(findOutLimit->second) + 0.5)));
1984 }
1985 else
1986 {
1987 count++;
1988 }
1989 }
1990 }
1991
1992 return ipmi::responseInvalidFieldRequest();
1993 }
1994 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001995 {
1996
1997 /*
1998 DataLen should be 1, but host is sending us an extra bit. As the
James Feistacc8a4e2019-04-02 14:23:57 -07001999 previous behavior didn't seem to prevent this, ignore the check for
2000 now.
James Feist5f957ca2019-03-14 15:33:55 -07002001
James Feistacc8a4e2019-04-02 14:23:57 -07002002 if (param)
James Feist5f957ca2019-03-14 15:33:55 -07002003 {
2004 phosphor::logging::log<phosphor::logging::level::ERR>(
2005 "ipmiOEMGetFscParameter: invalid input len!");
James Feist5f957ca2019-03-14 15:33:55 -07002006 return IPMI_CC_REQ_DATA_LEN_INVALID;
2007 }
2008 */
2009 Value cfmLimit;
2010 Value cfmMaximum;
2011 try
2012 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002013 cfmLimit = ipmi::getDbusProperty(*dbus, settingsBusName,
James Feist5f957ca2019-03-14 15:33:55 -07002014 cfmLimitSettingPath, cfmLimitIface,
2015 "Limit");
2016 cfmMaximum = ipmi::getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002017 *dbus, "xyz.openbmc_project.ExitAirTempSensor",
James Feist5f957ca2019-03-14 15:33:55 -07002018 "/xyz/openbmc_project/control/MaxCFM", cfmLimitIface, "Limit");
2019 }
2020 catch (sdbusplus::exception_t& e)
2021 {
2022 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feistacc8a4e2019-04-02 14:23:57 -07002023 "ipmiOEMGetFscParameter: can't get cfm setting!",
James Feist5f957ca2019-03-14 15:33:55 -07002024 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002025 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002026 }
2027
James Feistacc8a4e2019-04-02 14:23:57 -07002028 double cfmMax = std::get<double>(cfmMaximum);
2029 double cfmLim = std::get<double>(cfmLimit);
James Feist5f957ca2019-03-14 15:33:55 -07002030
James Feistacc8a4e2019-04-02 14:23:57 -07002031 cfmLim = std::floor(cfmLim + 0.5);
2032 cfmMax = std::floor(cfmMax + 0.5);
2033 uint16_t cfmLimResp = static_cast<uint16_t>(cfmLim);
2034 uint16_t cfmMaxResp = static_cast<uint16_t>(cfmMax);
James Feist5f957ca2019-03-14 15:33:55 -07002035
James Feistacc8a4e2019-04-02 14:23:57 -07002036 return ipmi::responseSuccess(
2037 std::array<uint16_t, 2>{cfmLimResp, cfmMaxResp});
James Feist5f957ca2019-03-14 15:33:55 -07002038 }
James Feistacc8a4e2019-04-02 14:23:57 -07002039
James Feist5f957ca2019-03-14 15:33:55 -07002040 else
2041 {
2042 // todo other command parts possibly
James Feist5f957ca2019-03-14 15:33:55 -07002043 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002044 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002045 }
2046}
2047
Cheng C Yang773703a2019-08-15 09:41:11 +08002048using crConfigVariant =
2049 std::variant<bool, uint8_t, uint32_t, std::vector<uint8_t>, std::string>;
2050
2051int setCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2052 const crConfigVariant& value,
2053 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2054{
2055 boost::system::error_code ec;
2056 ctx->bus->yield_method_call<void>(
James Feist28c72902019-09-16 10:34:07 -07002057 ctx->yield, ec, "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002058 "/xyz/openbmc_project/control/power_supply_redundancy",
2059 "org.freedesktop.DBus.Properties", "Set",
2060 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property, value);
2061 if (ec)
2062 {
2063 phosphor::logging::log<phosphor::logging::level::ERR>(
2064 "Failed to set dbus property to cold redundancy");
2065 return -1;
2066 }
2067
2068 return 0;
2069}
2070
2071int getCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2072 crConfigVariant& value,
Yong Li19445ab2019-12-20 18:25:29 +08002073 const std::string& service = "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002074 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2075{
2076 boost::system::error_code ec;
2077 value = ctx->bus->yield_method_call<crConfigVariant>(
Yong Li19445ab2019-12-20 18:25:29 +08002078 ctx->yield, ec, service,
Cheng C Yang773703a2019-08-15 09:41:11 +08002079 "/xyz/openbmc_project/control/power_supply_redundancy",
2080 "org.freedesktop.DBus.Properties", "Get",
2081 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property);
2082 if (ec)
2083 {
2084 phosphor::logging::log<phosphor::logging::level::ERR>(
2085 "Failed to get dbus property to cold redundancy");
2086 return -1;
2087 }
2088 return 0;
2089}
2090
2091uint8_t getPSUCount(void)
2092{
2093 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2094 ipmi::Value num;
2095 try
2096 {
2097 num = ipmi::getDbusProperty(
2098 *dbus, "xyz.openbmc_project.PSURedundancy",
2099 "/xyz/openbmc_project/control/power_supply_redundancy",
2100 "xyz.openbmc_project.Control.PowerSupplyRedundancy", "PSUNumber");
2101 }
2102 catch (sdbusplus::exception_t& e)
2103 {
2104 phosphor::logging::log<phosphor::logging::level::ERR>(
2105 "Failed to get PSUNumber property from dbus interface");
2106 return 0;
2107 }
2108 uint8_t* pNum = std::get_if<uint8_t>(&num);
2109 if (!pNum)
2110 {
2111 phosphor::logging::log<phosphor::logging::level::ERR>(
2112 "Error to get PSU Number");
2113 return 0;
2114 }
2115 return *pNum;
2116}
2117
2118bool validateCRAlgo(std::vector<uint8_t>& conf, uint8_t num)
2119{
2120 if (conf.size() < num)
2121 {
2122 phosphor::logging::log<phosphor::logging::level::ERR>(
2123 "Invalid PSU Ranking");
2124 return false;
2125 }
2126 std::set<uint8_t> confSet;
2127 for (uint8_t i = 0; i < num; i++)
2128 {
2129 if (conf[i] > num)
2130 {
2131 phosphor::logging::log<phosphor::logging::level::ERR>(
2132 "PSU Ranking is larger than current PSU number");
2133 return false;
2134 }
2135 confSet.emplace(conf[i]);
2136 }
2137
2138 if (confSet.size() != num)
2139 {
2140 phosphor::logging::log<phosphor::logging::level::ERR>(
2141 "duplicate PSU Ranking");
2142 return false;
2143 }
2144 return true;
2145}
2146
2147enum class crParameter
2148{
2149 crStatus = 0,
2150 crFeature = 1,
2151 rotationFeature = 2,
2152 rotationAlgo = 3,
2153 rotationPeriod = 4,
Yong Li19445ab2019-12-20 18:25:29 +08002154 numOfPSU = 5,
2155 rotationRankOrderEffective = 6
Cheng C Yang773703a2019-08-15 09:41:11 +08002156};
2157
2158constexpr ipmi::Cc ccParameterNotSupported = 0x80;
2159static const constexpr uint32_t oneDay = 0x15180;
2160static const constexpr uint32_t oneMonth = 0xf53700;
2161static const constexpr uint8_t userSpecific = 0x01;
2162static const constexpr uint8_t crSetCompleted = 0;
2163ipmi::RspType<uint8_t> ipmiOEMSetCRConfig(ipmi::Context::ptr ctx,
2164 uint8_t parameter,
2165 ipmi::message::Payload& payload)
2166{
2167 switch (static_cast<crParameter>(parameter))
2168 {
2169 case crParameter::crFeature:
2170 {
2171 uint8_t param1;
2172 if (payload.unpack(param1) || !payload.fullyUnpacked())
2173 {
2174 return ipmi::responseReqDataLenInvalid();
2175 }
2176 // ColdRedundancy Enable can only be true or flase
2177 if (param1 > 1)
2178 {
2179 return ipmi::responseInvalidFieldRequest();
2180 }
2181 if (setCRConfig(ctx, "ColdRedundancyEnabled",
2182 static_cast<bool>(param1)))
2183 {
2184 return ipmi::responseResponseError();
2185 }
2186 break;
2187 }
2188 case crParameter::rotationFeature:
2189 {
2190 uint8_t param1;
2191 if (payload.unpack(param1) || !payload.fullyUnpacked())
2192 {
2193 return ipmi::responseReqDataLenInvalid();
2194 }
2195 // Rotation Enable can only be true or false
2196 if (param1 > 1)
2197 {
2198 return ipmi::responseInvalidFieldRequest();
2199 }
2200 if (setCRConfig(ctx, "RotationEnabled", static_cast<bool>(param1)))
2201 {
2202 return ipmi::responseResponseError();
2203 }
2204 break;
2205 }
2206 case crParameter::rotationAlgo:
2207 {
2208 // Rotation Algorithm can only be 0-BMC Specific or 1-User Specific
2209 std::string algoName;
2210 uint8_t param1;
2211 if (payload.unpack(param1))
2212 {
2213 return ipmi::responseReqDataLenInvalid();
2214 }
2215 switch (param1)
2216 {
2217 case 0:
2218 algoName = "xyz.openbmc_project.Control."
2219 "PowerSupplyRedundancy.Algo.bmcSpecific";
2220 break;
2221 case 1:
2222 algoName = "xyz.openbmc_project.Control."
2223 "PowerSupplyRedundancy.Algo.userSpecific";
2224 break;
2225 default:
2226 return ipmi::responseInvalidFieldRequest();
2227 }
2228 if (setCRConfig(ctx, "RotationAlgorithm", algoName))
2229 {
2230 return ipmi::responseResponseError();
2231 }
2232
2233 uint8_t numberOfPSU = getPSUCount();
2234 if (!numberOfPSU)
2235 {
2236 return ipmi::responseResponseError();
2237 }
2238 std::vector<uint8_t> rankOrder;
2239
2240 if (param1 == userSpecific)
2241 {
2242 if (payload.unpack(rankOrder) || !payload.fullyUnpacked())
2243 {
2244 ipmi::responseReqDataLenInvalid();
2245 }
Yong Li83315132019-10-23 17:42:24 +08002246 if (rankOrder.size() != numberOfPSU)
Cheng C Yang773703a2019-08-15 09:41:11 +08002247 {
2248 return ipmi::responseReqDataLenInvalid();
2249 }
2250
2251 if (!validateCRAlgo(rankOrder, numberOfPSU))
2252 {
2253 return ipmi::responseInvalidFieldRequest();
2254 }
2255 }
2256 else
2257 {
2258 if (rankOrder.size() > 0)
2259 {
2260 return ipmi::responseReqDataLenInvalid();
2261 }
2262 for (uint8_t i = 1; i <= numberOfPSU; i++)
2263 {
2264 rankOrder.emplace_back(i);
2265 }
2266 }
2267 if (setCRConfig(ctx, "RotationRankOrder", rankOrder))
2268 {
2269 return ipmi::responseResponseError();
2270 }
2271 break;
2272 }
2273 case crParameter::rotationPeriod:
2274 {
2275 // Minimum Rotation period is One day (86400 seconds) and Max
2276 // Rotation Period is 6 month (0xf53700 seconds)
2277 uint32_t period;
2278 if (payload.unpack(period) || !payload.fullyUnpacked())
2279 {
2280 return ipmi::responseReqDataLenInvalid();
2281 }
2282 if ((period < oneDay) || (period > oneMonth))
2283 {
2284 return ipmi::responseInvalidFieldRequest();
2285 }
2286 if (setCRConfig(ctx, "PeriodOfRotation", period))
2287 {
2288 return ipmi::responseResponseError();
2289 }
2290 break;
2291 }
2292 default:
2293 {
2294 return ipmi::response(ccParameterNotSupported);
2295 }
2296 }
2297
2298 // TODO Halfwidth needs to set SetInProgress
2299 if (setCRConfig(ctx, "ColdRedundancyStatus",
Cheng C Yange8cecdf2019-08-26 23:48:08 +08002300 std::string("xyz.openbmc_project.Control."
2301 "PowerSupplyRedundancy.Status.completed")))
Cheng C Yang773703a2019-08-15 09:41:11 +08002302 {
2303 return ipmi::responseResponseError();
2304 }
2305 return ipmi::responseSuccess(crSetCompleted);
2306}
2307
Yong Li83315132019-10-23 17:42:24 +08002308ipmi::RspType<uint8_t, std::variant<uint8_t, uint32_t, std::vector<uint8_t>>>
Cheng C Yang773703a2019-08-15 09:41:11 +08002309 ipmiOEMGetCRConfig(ipmi::Context::ptr ctx, uint8_t parameter)
2310{
2311 crConfigVariant value;
2312 switch (static_cast<crParameter>(parameter))
2313 {
2314 case crParameter::crStatus:
2315 {
2316 if (getCRConfig(ctx, "ColdRedundancyStatus", value))
2317 {
2318 return ipmi::responseResponseError();
2319 }
2320 std::string* pStatus = std::get_if<std::string>(&value);
2321 if (!pStatus)
2322 {
2323 phosphor::logging::log<phosphor::logging::level::ERR>(
2324 "Error to get ColdRedundancyStatus property");
2325 return ipmi::responseResponseError();
2326 }
2327 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2328 auto status =
2329 server::PowerSupplyRedundancy::convertStatusFromString(
2330 *pStatus);
2331 switch (status)
2332 {
2333 case server::PowerSupplyRedundancy::Status::inProgress:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002334 return ipmi::responseSuccess(parameter,
2335 static_cast<uint8_t>(0));
Cheng C Yang773703a2019-08-15 09:41:11 +08002336
2337 case server::PowerSupplyRedundancy::Status::completed:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002338 return ipmi::responseSuccess(parameter,
2339 static_cast<uint8_t>(1));
Cheng C Yang773703a2019-08-15 09:41:11 +08002340 default:
2341 phosphor::logging::log<phosphor::logging::level::ERR>(
2342 "Error to get valid status");
2343 return ipmi::responseResponseError();
2344 }
2345 }
2346 case crParameter::crFeature:
2347 {
2348 if (getCRConfig(ctx, "ColdRedundancyEnabled", value))
2349 {
2350 return ipmi::responseResponseError();
2351 }
2352 bool* pResponse = std::get_if<bool>(&value);
2353 if (!pResponse)
2354 {
2355 phosphor::logging::log<phosphor::logging::level::ERR>(
2356 "Error to get ColdRedundancyEnable property");
2357 return ipmi::responseResponseError();
2358 }
2359
Cheng C Yangf41e3342019-09-10 04:47:23 +08002360 return ipmi::responseSuccess(parameter,
2361 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002362 }
2363 case crParameter::rotationFeature:
2364 {
2365 if (getCRConfig(ctx, "RotationEnabled", value))
2366 {
2367 return ipmi::responseResponseError();
2368 }
2369 bool* pResponse = std::get_if<bool>(&value);
2370 if (!pResponse)
2371 {
2372 phosphor::logging::log<phosphor::logging::level::ERR>(
2373 "Error to get RotationEnabled property");
2374 return ipmi::responseResponseError();
2375 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002376 return ipmi::responseSuccess(parameter,
2377 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002378 }
2379 case crParameter::rotationAlgo:
2380 {
2381 if (getCRConfig(ctx, "RotationAlgorithm", value))
2382 {
2383 return ipmi::responseResponseError();
2384 }
2385
2386 std::string* pAlgo = std::get_if<std::string>(&value);
2387 if (!pAlgo)
2388 {
2389 phosphor::logging::log<phosphor::logging::level::ERR>(
2390 "Error to get RotationAlgorithm property");
2391 return ipmi::responseResponseError();
2392 }
Yong Li83315132019-10-23 17:42:24 +08002393 std::vector<uint8_t> response;
Cheng C Yang773703a2019-08-15 09:41:11 +08002394 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2395 auto algo =
2396 server::PowerSupplyRedundancy::convertAlgoFromString(*pAlgo);
Yong Li83315132019-10-23 17:42:24 +08002397
Cheng C Yang773703a2019-08-15 09:41:11 +08002398 switch (algo)
2399 {
2400 case server::PowerSupplyRedundancy::Algo::bmcSpecific:
Yong Li83315132019-10-23 17:42:24 +08002401 response.push_back(0);
Cheng C Yang773703a2019-08-15 09:41:11 +08002402 break;
2403 case server::PowerSupplyRedundancy::Algo::userSpecific:
Yong Li83315132019-10-23 17:42:24 +08002404 response.push_back(1);
Cheng C Yang773703a2019-08-15 09:41:11 +08002405 break;
2406 default:
2407 phosphor::logging::log<phosphor::logging::level::ERR>(
2408 "Error to get valid algo");
2409 return ipmi::responseResponseError();
2410 }
2411
2412 if (getCRConfig(ctx, "RotationRankOrder", value))
2413 {
2414 return ipmi::responseResponseError();
2415 }
2416 std::vector<uint8_t>* pResponse =
2417 std::get_if<std::vector<uint8_t>>(&value);
2418 if (!pResponse)
2419 {
2420 phosphor::logging::log<phosphor::logging::level::ERR>(
2421 "Error to get RotationRankOrder property");
2422 return ipmi::responseResponseError();
2423 }
Yong Li83315132019-10-23 17:42:24 +08002424
Cheng C Yang773703a2019-08-15 09:41:11 +08002425 std::copy(pResponse->begin(), pResponse->end(),
Yong Li83315132019-10-23 17:42:24 +08002426 std::back_inserter(response));
2427
Cheng C Yangf41e3342019-09-10 04:47:23 +08002428 return ipmi::responseSuccess(parameter, response);
Cheng C Yang773703a2019-08-15 09:41:11 +08002429 }
2430 case crParameter::rotationPeriod:
2431 {
2432 if (getCRConfig(ctx, "PeriodOfRotation", value))
2433 {
2434 return ipmi::responseResponseError();
2435 }
2436 uint32_t* pResponse = std::get_if<uint32_t>(&value);
2437 if (!pResponse)
2438 {
2439 phosphor::logging::log<phosphor::logging::level::ERR>(
2440 "Error to get RotationAlgorithm property");
2441 return ipmi::responseResponseError();
2442 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002443 return ipmi::responseSuccess(parameter, *pResponse);
Cheng C Yang773703a2019-08-15 09:41:11 +08002444 }
2445 case crParameter::numOfPSU:
2446 {
2447 uint8_t numberOfPSU = getPSUCount();
2448 if (!numberOfPSU)
2449 {
2450 return ipmi::responseResponseError();
2451 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002452 return ipmi::responseSuccess(parameter, numberOfPSU);
Cheng C Yang773703a2019-08-15 09:41:11 +08002453 }
Yong Li19445ab2019-12-20 18:25:29 +08002454 case crParameter::rotationRankOrderEffective:
2455 {
2456 if (getCRConfig(ctx, "RotationRankOrder", value,
2457 "xyz.openbmc_project.PSURedundancy"))
2458 {
2459 return ipmi::responseResponseError();
2460 }
2461 std::vector<uint8_t>* pResponse =
2462 std::get_if<std::vector<uint8_t>>(&value);
2463 if (!pResponse)
2464 {
2465 phosphor::logging::log<phosphor::logging::level::ERR>(
2466 "Error to get effective RotationRankOrder property");
2467 return ipmi::responseResponseError();
2468 }
2469 return ipmi::responseSuccess(parameter, *pResponse);
2470 }
Cheng C Yang773703a2019-08-15 09:41:11 +08002471 default:
2472 {
2473 return ipmi::response(ccParameterNotSupported);
2474 }
2475 }
2476}
2477
Zhu, Yungebe560b02019-04-21 21:19:21 -04002478ipmi::RspType<> ipmiOEMSetFaultIndication(uint8_t sourceId, uint8_t faultType,
2479 uint8_t faultState,
2480 uint8_t faultGroup,
2481 std::array<uint8_t, 8>& ledStateData)
2482{
Zhu, Yungebe560b02019-04-21 21:19:21 -04002483 constexpr auto maxFaultType = static_cast<size_t>(RemoteFaultType::max);
2484 static const std::array<std::string, maxFaultType> faultNames = {
2485 "faultFan", "faultTemp", "faultPower",
2486 "faultDriveSlot", "faultSoftware", "faultMemory"};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002487
2488 constexpr uint8_t maxFaultSource = 0x4;
2489 constexpr uint8_t skipLEDs = 0xFF;
2490 constexpr uint8_t pinSize = 64;
2491 constexpr uint8_t groupSize = 16;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002492 constexpr uint8_t groupNum = 5; // 4 for fault memory, 1 for faultFan
Zhu, Yungebe560b02019-04-21 21:19:21 -04002493
Zhikui Rence4e73f2019-12-06 13:59:47 -08002494 // same pin names need to be defined in dts file
2495 static const std::array<std::array<std::string, groupSize>, groupNum>
2496 faultLedPinNames = {{
2497 "LED_CPU1_CH1_DIMM1_FAULT",
2498 "LED_CPU1_CH1_DIMM2_FAULT",
2499 "LED_CPU1_CH2_DIMM1_FAULT",
2500 "LED_CPU1_CH2_DIMM2_FAULT",
2501 "LED_CPU1_CH3_DIMM1_FAULT",
2502 "LED_CPU1_CH3_DIMM2_FAULT",
2503 "LED_CPU1_CH4_DIMM1_FAULT",
2504 "LED_CPU1_CH4_DIMM2_FAULT",
2505 "LED_CPU1_CH5_DIMM1_FAULT",
2506 "LED_CPU1_CH5_DIMM2_FAULT",
2507 "LED_CPU1_CH6_DIMM1_FAULT",
2508 "LED_CPU1_CH6_DIMM2_FAULT",
2509 "",
2510 "",
2511 "",
2512 "", // end of group1
2513 "LED_CPU2_CH1_DIMM1_FAULT",
2514 "LED_CPU2_CH1_DIMM2_FAULT",
2515 "LED_CPU2_CH2_DIMM1_FAULT",
2516 "LED_CPU2_CH2_DIMM2_FAULT",
2517 "LED_CPU2_CH3_DIMM1_FAULT",
2518 "LED_CPU2_CH3_DIMM2_FAULT",
2519 "LED_CPU2_CH4_DIMM1_FAULT",
2520 "LED_CPU2_CH4_DIMM2_FAULT",
2521 "LED_CPU2_CH5_DIMM1_FAULT",
2522 "LED_CPU2_CH5_DIMM2_FAULT",
2523 "LED_CPU2_CH6_DIMM1_FAULT",
2524 "LED_CPU2_CH6_DIMM2_FAULT",
2525 "",
2526 "",
2527 "",
2528 "", // endof group2
2529 "LED_CPU3_CH1_DIMM1_FAULT",
2530 "LED_CPU3_CH1_DIMM2_FAULT",
2531 "LED_CPU3_CH2_DIMM1_FAULT",
2532 "LED_CPU3_CH2_DIMM2_FAULT",
2533 "LED_CPU3_CH3_DIMM1_FAULT",
2534 "LED_CPU3_CH3_DIMM2_FAULT",
2535 "LED_CPU3_CH4_DIMM1_FAULT",
2536 "LED_CPU3_CH4_DIMM2_FAULT",
2537 "LED_CPU3_CH5_DIMM1_FAULT",
2538 "LED_CPU3_CH5_DIMM2_FAULT",
2539 "LED_CPU3_CH6_DIMM1_FAULT",
2540 "LED_CPU3_CH6_DIMM2_FAULT",
2541 "",
2542 "",
2543 "",
2544 "", // end of group3
2545 "LED_CPU4_CH1_DIMM1_FAULT",
2546 "LED_CPU4_CH1_DIMM2_FAULT",
2547 "LED_CPU4_CH2_DIMM1_FAULT",
2548 "LED_CPU4_CH2_DIMM2_FAULT",
2549 "LED_CPU4_CH3_DIMM1_FAULT",
2550 "LED_CPU4_CH3_DIMM2_FAULT",
2551 "LED_CPU4_CH4_DIMM1_FAULT",
2552 "LED_CPU4_CH4_DIMM2_FAULT",
2553 "LED_CPU4_CH5_DIMM1_FAULT",
2554 "LED_CPU4_CH5_DIMM2_FAULT",
2555 "LED_CPU4_CH6_DIMM1_FAULT",
2556 "LED_CPU4_CH6_DIMM2_FAULT",
2557 "",
2558 "",
2559 "",
2560 "", // end of group4
2561 "LED_FAN1_FAULT",
2562 "LED_FAN2_FAULT",
2563 "LED_FAN3_FAULT",
2564 "LED_FAN4_FAULT",
2565 "LED_FAN5_FAULT",
2566 "LED_FAN6_FAULT",
2567 "LED_FAN7_FAULT",
2568 "LED_FAN8_FAULT",
2569 "",
2570 "",
2571 "",
2572 "",
2573 "",
2574 "",
2575 "",
2576 "" // end of group5
2577 }};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002578
Zhikui Rence4e73f2019-12-06 13:59:47 -08002579 // Validate the source, fault type --
2580 // (Byte 1) sourceId: Unspecified, Hot-Swap Controller 0, Hot-Swap
2581 // Controller 1, BIOS (Byte 2) fault type: fan, temperature, power,
2582 // driveslot, software, memory (Byte 3) FaultState: OK, Degraded,
2583 // Non-Critical, Critical, Non-Recoverable, (Byte 4) is faultGroup,
2584 // definition differs based on fault type (Byte 2)
2585 // Type Fan=> Group: 0=FanGroupID, FF-not used
2586 // Byte 5-11 00h, not used
2587 // Byte12 FanLedState [7:0]-Fans 7:0
2588 // Type Memory=> Group: 0 = DIMM GroupID, FF-not used
2589 // Byte 5:12 - DIMM LED state (64bit field, LS Byte first)
2590 // [63:48] = CPU4 channels 7:0, 2 bits per channel
2591 // [47:32] = CPU3 channels 7:0, 2 bits per channel
2592 // [31:16] = CPU2 channels 7:0, 2 bits per channel
2593 // [15:0] = CPU1 channels 7:0, 2 bits per channel
2594 // Type Other=> Component Fault LED Group ID, not used set to 0xFF
2595 // Byte[5:12]: reserved 0x00h
Zhu, Yungebe560b02019-04-21 21:19:21 -04002596 if ((sourceId >= maxFaultSource) ||
2597 (faultType >= static_cast<int8_t>(RemoteFaultType::max)) ||
2598 (faultState >= static_cast<int8_t>(RemoteFaultState::maxFaultState)) ||
2599 (faultGroup >= static_cast<int8_t>(DimmFaultType::maxFaultGroup)))
2600 {
2601 return ipmi::responseParmOutOfRange();
2602 }
2603
Zhikui Rence4e73f2019-12-06 13:59:47 -08002604 size_t pinGroupOffset = 0;
2605 size_t pinGroupMax = pinSize / groupSize;
2606 if (RemoteFaultType::fan == RemoteFaultType(faultType))
Zhu, Yungebe560b02019-04-21 21:19:21 -04002607 {
Zhikui Rence4e73f2019-12-06 13:59:47 -08002608 pinGroupOffset = 4;
2609 pinGroupMax = groupNum - pinSize / groupSize;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002610 }
2611
2612 switch (RemoteFaultType(faultType))
2613 {
2614 case (RemoteFaultType::fan):
2615 case (RemoteFaultType::memory):
2616 {
2617 if (faultGroup == skipLEDs)
2618 {
2619 return ipmi::responseSuccess();
2620 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002621 // calculate led state bit filed count, each byte has 8bits
2622 // the maximum bits will be 8 * 8 bits
2623 constexpr uint8_t size = sizeof(ledStateData) * 8;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002624
2625 // assemble ledState
2626 uint64_t ledState = 0;
2627 bool hasError = false;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002628 for (int i = 0; i < sizeof(ledStateData); i++)
2629 {
2630 ledState = (uint64_t)(ledState << 8);
2631 ledState = (uint64_t)(ledState | (uint64_t)ledStateData[i]);
2632 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002633 std::bitset<size> ledStateBits(ledState);
Zhu, Yungebe560b02019-04-21 21:19:21 -04002634
Zhikui Rence4e73f2019-12-06 13:59:47 -08002635 for (int group = 0; group < pinGroupMax; group++)
2636 {
2637 for (int i = 0; i < groupSize; i++)
2638 { // skip non-existing pins
2639 if (0 == faultLedPinNames[group + pinGroupOffset][i].size())
2640 {
2641 continue;
2642 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002643
Zhikui Rence4e73f2019-12-06 13:59:47 -08002644 gpiod::line line = gpiod::find_line(
2645 faultLedPinNames[group + pinGroupOffset][i]);
2646 if (!line)
2647 {
2648 phosphor::logging::log<phosphor::logging::level::ERR>(
2649 "Not Find Led Gpio Device!",
2650 phosphor::logging::entry(
2651 "DEVICE=%s",
2652 faultLedPinNames[group + pinGroupOffset][i]
2653 .c_str()));
2654 hasError = true;
2655 continue;
2656 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002657
Zhikui Rence4e73f2019-12-06 13:59:47 -08002658 bool activeHigh =
2659 (line.active_state() == gpiod::line::ACTIVE_HIGH);
2660 try
2661 {
2662 line.request(
2663 {"faultLed", gpiod::line_request::DIRECTION_OUTPUT,
2664 activeHigh
2665 ? 0
2666 : gpiod::line_request::FLAG_ACTIVE_LOW});
2667 line.set_value(ledStateBits[i + group * groupSize]);
2668 }
2669 catch (std::system_error&)
2670 {
2671 phosphor::logging::log<phosphor::logging::level::ERR>(
2672 "Error write Led Gpio Device!",
2673 phosphor::logging::entry(
2674 "DEVICE=%s",
2675 faultLedPinNames[group + pinGroupOffset][i]
2676 .c_str()));
2677 hasError = true;
2678 continue;
2679 }
2680 } // for int i
2681 }
2682 if (hasError)
2683 {
2684 return ipmi::responseResponseError();
Zhu, Yungebe560b02019-04-21 21:19:21 -04002685 }
2686 break;
2687 }
2688 default:
2689 {
2690 // now only support two fault types
2691 return ipmi::responseParmOutOfRange();
2692 }
Zhikui Rence4e73f2019-12-06 13:59:47 -08002693 } // switch
Zhu, Yungebe560b02019-04-21 21:19:21 -04002694 return ipmi::responseSuccess();
2695}
2696
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302697ipmi::RspType<uint8_t> ipmiOEMReadBoardProductId()
2698{
2699 uint8_t prodId = 0;
2700 try
2701 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002702 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302703 const DbusObjectInfo& object = getDbusObject(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002704 *dbus, "xyz.openbmc_project.Inventory.Item.Board",
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302705 "/xyz/openbmc_project/inventory/system/board/", "Baseboard");
2706 const Value& propValue = getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002707 *dbus, object.second, object.first,
Suryakanth Sekar6c57e5c2020-01-10 17:11:58 +05302708 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
2709 "ProductId");
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302710 prodId = static_cast<uint8_t>(std::get<uint64_t>(propValue));
2711 }
2712 catch (std::exception& e)
2713 {
2714 phosphor::logging::log<phosphor::logging::level::ERR>(
2715 "ipmiOEMReadBoardProductId: Product ID read failed!",
2716 phosphor::logging::entry("ERR=%s", e.what()));
2717 }
2718 return ipmi::responseSuccess(prodId);
2719}
2720
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302721/** @brief implements the get security mode command
2722 * @param ctx - ctx pointer
2723 *
2724 * @returns IPMI completion code with following data
2725 * - restriction mode value - As specified in
2726 * xyz.openbmc_project.Control.Security.RestrictionMode.interface.yaml
2727 * - special mode value - As specified in
2728 * xyz.openbmc_project.Control.Security.SpecialMode.interface.yaml
2729 */
2730ipmi::RspType<uint8_t, uint8_t> ipmiGetSecurityMode(ipmi::Context::ptr ctx)
2731{
2732 namespace securityNameSpace =
2733 sdbusplus::xyz::openbmc_project::Control::Security::server;
2734 uint8_t restrictionModeValue = 0;
2735 uint8_t specialModeValue = 0;
2736
2737 boost::system::error_code ec;
2738 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002739 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302740 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2741 restricionModeProperty);
2742 if (ec)
2743 {
2744 phosphor::logging::log<phosphor::logging::level::ERR>(
2745 "ipmiGetSecurityMode: failed to get RestrictionMode property",
2746 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2747 return ipmi::responseUnspecifiedError();
2748 }
2749 restrictionModeValue = static_cast<uint8_t>(
2750 securityNameSpace::RestrictionMode::convertModesFromString(
2751 std::get<std::string>(varRestrMode)));
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302752 auto varSpecialMode =
2753 ctx->bus->yield_method_call<std::variant<std::string>>(
2754 ctx->yield, ec, specialModeService, specialModeBasePath,
2755 dBusPropertyIntf, dBusPropertyGetMethod, specialModeIntf,
2756 specialModeProperty);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302757 if (ec)
2758 {
2759 phosphor::logging::log<phosphor::logging::level::ERR>(
2760 "ipmiGetSecurityMode: failed to get SpecialMode property",
2761 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2762 // fall through, let us not worry about SpecialMode property, which is
2763 // not required in user scenario
2764 }
2765 else
2766 {
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302767 specialModeValue = static_cast<uint8_t>(
2768 securityNameSpace::SpecialMode::convertModesFromString(
2769 std::get<std::string>(varSpecialMode)));
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302770 }
2771 return ipmi::responseSuccess(restrictionModeValue, specialModeValue);
2772}
2773
2774/** @brief implements the set security mode command
2775 * Command allows to upgrade the restriction mode and won't allow
2776 * to downgrade from system interface
2777 * @param ctx - ctx pointer
2778 * @param restrictionMode - restriction mode value to be set.
2779 *
2780 * @returns IPMI completion code
2781 */
2782ipmi::RspType<> ipmiSetSecurityMode(ipmi::Context::ptr ctx,
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302783 uint8_t restrictionMode,
2784 std::optional<uint8_t> specialMode)
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302785{
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302786#ifndef BMC_VALIDATION_UNSECURE_FEATURE
2787 if (specialMode)
2788 {
2789 return ipmi::responseReqDataLenInvalid();
2790 }
2791#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302792 namespace securityNameSpace =
2793 sdbusplus::xyz::openbmc_project::Control::Security::server;
2794
2795 ChannelInfo chInfo;
2796 if (getChannelInfo(ctx->channel, chInfo) != ccSuccess)
2797 {
2798 phosphor::logging::log<phosphor::logging::level::ERR>(
2799 "ipmiSetSecurityMode: Failed to get Channel Info",
2800 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
2801 return ipmi::responseUnspecifiedError();
2802 }
2803 auto reqMode =
2804 static_cast<securityNameSpace::RestrictionMode::Modes>(restrictionMode);
2805
2806 if ((reqMode < securityNameSpace::RestrictionMode::Modes::Provisioning) ||
2807 (reqMode >
2808 securityNameSpace::RestrictionMode::Modes::ProvisionedHostDisabled))
2809 {
2810 return ipmi::responseInvalidFieldRequest();
2811 }
2812
2813 boost::system::error_code ec;
2814 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002815 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302816 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2817 restricionModeProperty);
2818 if (ec)
2819 {
2820 phosphor::logging::log<phosphor::logging::level::ERR>(
2821 "ipmiSetSecurityMode: failed to get RestrictionMode property",
2822 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2823 return ipmi::responseUnspecifiedError();
2824 }
2825 auto currentRestrictionMode =
2826 securityNameSpace::RestrictionMode::convertModesFromString(
2827 std::get<std::string>(varRestrMode));
2828
2829 if (chInfo.mediumType !=
2830 static_cast<uint8_t>(EChannelMediumType::lan8032) &&
2831 currentRestrictionMode > reqMode)
2832 {
2833 phosphor::logging::log<phosphor::logging::level::ERR>(
2834 "ipmiSetSecurityMode - Downgrading security mode not supported "
2835 "through system interface",
2836 phosphor::logging::entry(
2837 "CUR_MODE=%d", static_cast<uint8_t>(currentRestrictionMode)),
2838 phosphor::logging::entry("REQ_MODE=%d", restrictionMode));
2839 return ipmi::responseCommandNotAvailable();
2840 }
2841
2842 ec.clear();
2843 ctx->bus->yield_method_call<>(
James Feist28c72902019-09-16 10:34:07 -07002844 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302845 dBusPropertyIntf, dBusPropertySetMethod, restricionModeIntf,
2846 restricionModeProperty,
2847 static_cast<std::variant<std::string>>(
2848 securityNameSpace::convertForMessage(reqMode)));
2849
2850 if (ec)
2851 {
2852 phosphor::logging::log<phosphor::logging::level::ERR>(
2853 "ipmiSetSecurityMode: failed to set RestrictionMode property",
2854 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2855 return ipmi::responseUnspecifiedError();
2856 }
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302857
2858#ifdef BMC_VALIDATION_UNSECURE_FEATURE
2859 if (specialMode)
2860 {
2861 ec.clear();
2862 ctx->bus->yield_method_call<>(
2863 ctx->yield, ec, specialModeService, specialModeBasePath,
2864 dBusPropertyIntf, dBusPropertySetMethod, specialModeIntf,
2865 specialModeProperty,
2866 static_cast<std::variant<std::string>>(
2867 securityNameSpace::convertForMessage(
2868 static_cast<securityNameSpace::SpecialMode::Modes>(
2869 specialMode.value()))));
2870
2871 if (ec)
2872 {
2873 phosphor::logging::log<phosphor::logging::level::ERR>(
2874 "ipmiSetSecurityMode: failed to set SpecialMode property",
2875 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2876 return ipmi::responseUnspecifiedError();
2877 }
2878 }
2879#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302880 return ipmi::responseSuccess();
2881}
2882
Vernon Mauery4ac799d2019-05-20 15:50:37 -07002883ipmi::RspType<uint8_t /* restore status */>
2884 ipmiRestoreConfiguration(const std::array<uint8_t, 3>& clr, uint8_t cmd)
2885{
2886 static constexpr std::array<uint8_t, 3> expClr = {'C', 'L', 'R'};
2887
2888 if (clr != expClr)
2889 {
2890 return ipmi::responseInvalidFieldRequest();
2891 }
2892 constexpr uint8_t cmdStatus = 0;
2893 constexpr uint8_t cmdDefaultRestore = 0xaa;
2894 constexpr uint8_t cmdFullRestore = 0xbb;
2895 constexpr uint8_t cmdFormat = 0xcc;
2896
2897 constexpr const char* restoreOpFname = "/tmp/.rwfs/.restore_op";
2898
2899 switch (cmd)
2900 {
2901 case cmdStatus:
2902 break;
2903 case cmdDefaultRestore:
2904 case cmdFullRestore:
2905 case cmdFormat:
2906 {
2907 // write file to rwfs root
2908 int value = (cmd - 1) & 0x03; // map aa, bb, cc => 1, 2, 3
2909 std::ofstream restoreFile(restoreOpFname);
2910 if (!restoreFile)
2911 {
2912 return ipmi::responseUnspecifiedError();
2913 }
2914 restoreFile << value << "\n";
2915 break;
2916 }
2917 default:
2918 return ipmi::responseInvalidFieldRequest();
2919 }
2920
2921 constexpr uint8_t restorePending = 0;
2922 constexpr uint8_t restoreComplete = 1;
2923
2924 uint8_t restoreStatus = std::filesystem::exists(restoreOpFname)
2925 ? restorePending
2926 : restoreComplete;
2927 return ipmi::responseSuccess(restoreStatus);
2928}
2929
Chen Yugang39736d52019-07-12 16:24:33 +08002930ipmi::RspType<uint8_t> ipmiOEMGetNmiSource(void)
2931{
2932 uint8_t bmcSource;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002933 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002934
2935 try
2936 {
2937 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2938 std::string service =
2939 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
2940 Value variant =
2941 getDbusProperty(*dbus, service, oemNmiSourceObjPath,
2942 oemNmiSourceIntf, oemNmiBmcSourceObjPathProp);
2943
2944 switch (nmi::NMISource::convertBMCSourceSignalFromString(
2945 std::get<std::string>(variant)))
2946 {
2947 case nmi::NMISource::BMCSourceSignal::None:
2948 bmcSource = static_cast<uint8_t>(NmiSource::none);
2949 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002950 case nmi::NMISource::BMCSourceSignal::FrontPanelButton:
2951 bmcSource = static_cast<uint8_t>(NmiSource::frontPanelButton);
Chen Yugang39736d52019-07-12 16:24:33 +08002952 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002953 case nmi::NMISource::BMCSourceSignal::Watchdog:
2954 bmcSource = static_cast<uint8_t>(NmiSource::watchdog);
Chen Yugang39736d52019-07-12 16:24:33 +08002955 break;
2956 case nmi::NMISource::BMCSourceSignal::ChassisCmd:
2957 bmcSource = static_cast<uint8_t>(NmiSource::chassisCmd);
2958 break;
2959 case nmi::NMISource::BMCSourceSignal::MemoryError:
2960 bmcSource = static_cast<uint8_t>(NmiSource::memoryError);
2961 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002962 case nmi::NMISource::BMCSourceSignal::PciBusError:
2963 bmcSource = static_cast<uint8_t>(NmiSource::pciBusError);
Chen Yugang39736d52019-07-12 16:24:33 +08002964 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002965 case nmi::NMISource::BMCSourceSignal::PCH:
2966 bmcSource = static_cast<uint8_t>(NmiSource::pch);
Chen Yugang39736d52019-07-12 16:24:33 +08002967 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002968 case nmi::NMISource::BMCSourceSignal::Chipset:
2969 bmcSource = static_cast<uint8_t>(NmiSource::chipset);
Chen Yugang39736d52019-07-12 16:24:33 +08002970 break;
2971 default:
2972 phosphor::logging::log<phosphor::logging::level::ERR>(
2973 "NMI source: invalid property!",
2974 phosphor::logging::entry(
2975 "PROP=%s", std::get<std::string>(variant).c_str()));
2976 return ipmi::responseResponseError();
2977 }
2978 }
2979 catch (sdbusplus::exception::SdBusError& e)
2980 {
2981 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
2982 return ipmi::responseResponseError();
2983 }
2984
2985 return ipmi::responseSuccess(bmcSource);
2986}
2987
2988ipmi::RspType<> ipmiOEMSetNmiSource(uint8_t sourceId)
2989{
Chen Yugang97cf96e2019-11-01 08:55:11 +08002990 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002991
2992 nmi::NMISource::BMCSourceSignal bmcSourceSignal =
2993 nmi::NMISource::BMCSourceSignal::None;
2994
2995 switch (NmiSource(sourceId))
2996 {
2997 case NmiSource::none:
2998 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::None;
2999 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003000 case NmiSource::frontPanelButton:
3001 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::FrontPanelButton;
Chen Yugang39736d52019-07-12 16:24:33 +08003002 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003003 case NmiSource::watchdog:
3004 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Watchdog;
Chen Yugang39736d52019-07-12 16:24:33 +08003005 break;
3006 case NmiSource::chassisCmd:
3007 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::ChassisCmd;
3008 break;
3009 case NmiSource::memoryError:
3010 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::MemoryError;
3011 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003012 case NmiSource::pciBusError:
3013 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PciBusError;
Chen Yugang39736d52019-07-12 16:24:33 +08003014 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003015 case NmiSource::pch:
3016 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PCH;
Chen Yugang39736d52019-07-12 16:24:33 +08003017 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003018 case NmiSource::chipset:
3019 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Chipset;
Chen Yugang39736d52019-07-12 16:24:33 +08003020 break;
3021 default:
3022 phosphor::logging::log<phosphor::logging::level::ERR>(
3023 "NMI source: invalid property!");
3024 return ipmi::responseResponseError();
3025 }
3026
3027 try
3028 {
3029 // keep NMI signal source
3030 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3031 std::string service =
3032 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
Chen Yugang97cf96e2019-11-01 08:55:11 +08003033 setDbusProperty(*dbus, service, oemNmiSourceObjPath, oemNmiSourceIntf,
3034 oemNmiBmcSourceObjPathProp,
3035 nmi::convertForMessage(bmcSourceSignal));
Chen Yugang99be6332019-08-09 16:20:48 +08003036 // set Enabled property to inform NMI source handling
3037 // to trigger a NMI_OUT BSOD.
3038 // if it's triggered by NMI source property changed,
3039 // NMI_OUT BSOD could be missed if the same source occurs twice in a row
3040 if (bmcSourceSignal != nmi::NMISource::BMCSourceSignal::None)
3041 {
3042 setDbusProperty(*dbus, service, oemNmiSourceObjPath,
3043 oemNmiSourceIntf, oemNmiEnabledObjPathProp,
3044 static_cast<bool>(true));
3045 }
Chen Yugang39736d52019-07-12 16:24:33 +08003046 }
3047 catch (sdbusplus::exception_t& e)
3048 {
3049 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3050 return ipmi::responseResponseError();
3051 }
3052
3053 return ipmi::responseSuccess();
3054}
3055
James Feist63efafa2019-07-24 12:39:21 -07003056namespace dimmOffset
3057{
3058constexpr const char* dimmPower = "DimmPower";
3059constexpr const char* staticCltt = "StaticCltt";
3060constexpr const char* offsetPath = "/xyz/openbmc_project/Inventory/Item/Dimm";
3061constexpr const char* offsetInterface =
3062 "xyz.openbmc_project.Inventory.Item.Dimm.Offset";
3063constexpr const char* property = "DimmOffset";
3064
3065}; // namespace dimmOffset
3066
3067ipmi::RspType<>
3068 ipmiOEMSetDimmOffset(uint8_t type,
3069 const std::vector<std::tuple<uint8_t, uint8_t>>& data)
3070{
3071 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3072 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3073 {
3074 return ipmi::responseInvalidFieldRequest();
3075 }
3076
3077 if (data.empty())
3078 {
3079 return ipmi::responseInvalidFieldRequest();
3080 }
3081 nlohmann::json json;
3082
3083 std::ifstream jsonStream(dimmOffsetFile);
3084 if (jsonStream.good())
3085 {
3086 json = nlohmann::json::parse(jsonStream, nullptr, false);
3087 if (json.is_discarded())
3088 {
3089 json = nlohmann::json();
3090 }
3091 jsonStream.close();
3092 }
3093
3094 std::string typeName;
3095 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3096 {
3097 typeName = dimmOffset::dimmPower;
3098 }
3099 else
3100 {
3101 typeName = dimmOffset::staticCltt;
3102 }
3103
3104 nlohmann::json& field = json[typeName];
3105
3106 for (const auto& [index, value] : data)
3107 {
3108 field[index] = value;
3109 }
3110
3111 for (nlohmann::json& val : field)
3112 {
3113 if (val == nullptr)
3114 {
3115 val = static_cast<uint8_t>(0);
3116 }
3117 }
3118
3119 std::ofstream output(dimmOffsetFile);
3120 if (!output.good())
3121 {
3122 std::cerr << "Error writing json file\n";
3123 return ipmi::responseResponseError();
3124 }
3125
3126 output << json.dump(4);
3127
3128 if (type == static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3129 {
3130 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
3131
3132 std::variant<std::vector<uint8_t>> offsets =
3133 field.get<std::vector<uint8_t>>();
3134 auto call = bus->new_method_call(
3135 settingsBusName, dimmOffset::offsetPath, PROP_INTF, "Set");
3136 call.append(dimmOffset::offsetInterface, dimmOffset::property, offsets);
3137 try
3138 {
3139 bus->call(call);
3140 }
3141 catch (sdbusplus::exception_t& e)
3142 {
3143 phosphor::logging::log<phosphor::logging::level::ERR>(
3144 "ipmiOEMSetDimmOffset: can't set dimm offsets!",
3145 phosphor::logging::entry("ERR=%s", e.what()));
3146 return ipmi::responseResponseError();
3147 }
3148 }
3149
3150 return ipmi::responseSuccess();
3151}
3152
3153ipmi::RspType<uint8_t> ipmiOEMGetDimmOffset(uint8_t type, uint8_t index)
3154{
3155
3156 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3157 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3158 {
3159 return ipmi::responseInvalidFieldRequest();
3160 }
3161
3162 std::ifstream jsonStream(dimmOffsetFile);
3163
3164 auto json = nlohmann::json::parse(jsonStream, nullptr, false);
3165 if (json.is_discarded())
3166 {
3167 std::cerr << "File error in " << dimmOffsetFile << "\n";
3168 return ipmi::responseResponseError();
3169 }
3170
3171 std::string typeName;
3172 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3173 {
3174 typeName = dimmOffset::dimmPower;
3175 }
3176 else
3177 {
3178 typeName = dimmOffset::staticCltt;
3179 }
3180
3181 auto it = json.find(typeName);
3182 if (it == json.end())
3183 {
3184 return ipmi::responseInvalidFieldRequest();
3185 }
3186
3187 if (it->size() <= index)
3188 {
3189 return ipmi::responseInvalidFieldRequest();
3190 }
3191
3192 uint8_t resp = it->at(index).get<uint8_t>();
3193 return ipmi::responseSuccess(resp);
3194}
3195
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003196namespace boot_options
3197{
3198
3199using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
3200using IpmiValue = uint8_t;
3201constexpr auto ipmiDefault = 0;
3202
3203std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
3204 {0x01, Source::Sources::Network},
3205 {0x02, Source::Sources::Disk},
3206 {0x05, Source::Sources::ExternalMedia},
3207 {0x0f, Source::Sources::RemovableMedia},
3208 {ipmiDefault, Source::Sources::Default}};
3209
3210std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003211 {0x06, Mode::Modes::Setup}, {ipmiDefault, Mode::Modes::Regular}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003212
3213std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
3214 {Source::Sources::Network, 0x01},
3215 {Source::Sources::Disk, 0x02},
3216 {Source::Sources::ExternalMedia, 0x05},
3217 {Source::Sources::RemovableMedia, 0x0f},
3218 {Source::Sources::Default, ipmiDefault}};
3219
3220std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003221 {Mode::Modes::Setup, 0x06}, {Mode::Modes::Regular, ipmiDefault}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003222
3223static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
3224static constexpr auto bootSourceIntf =
3225 "xyz.openbmc_project.Control.Boot.Source";
3226static constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
3227static constexpr auto persistentObjPath =
3228 "/xyz/openbmc_project/control/host0/boot";
3229static constexpr auto oneTimePath =
3230 "/xyz/openbmc_project/control/host0/boot/one_time";
3231static constexpr auto bootSourceProp = "BootSource";
3232static constexpr auto bootModeProp = "BootMode";
3233static constexpr auto oneTimeBootEnableProp = "Enabled";
3234static constexpr auto httpBootMode =
3235 "xyz.openbmc_project.Control.Boot.Source.Sources.Http";
3236
3237enum class BootOptionParameter : size_t
3238{
3239 setInProgress = 0x0,
3240 bootFlags = 0x5,
3241};
3242static constexpr uint8_t setComplete = 0x0;
3243static constexpr uint8_t setInProgress = 0x1;
3244static uint8_t transferStatus = setComplete;
3245static constexpr uint8_t setParmVersion = 0x01;
3246static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
3247static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
3248static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
3249static constexpr uint8_t httpBoot = 0xd;
3250static constexpr uint8_t bootSourceMask = 0x3c;
3251
3252} // namespace boot_options
3253
3254ipmi::RspType<uint8_t, // version
3255 uint8_t, // param
3256 uint8_t, // data0, dependent on parameter
3257 std::optional<uint8_t> // data1, dependent on parameter
3258 >
3259 ipmiOemGetEfiBootOptions(uint8_t parameter, uint8_t set, uint8_t block)
3260{
3261 using namespace boot_options;
3262 uint8_t bootOption = 0;
3263
3264 if (parameter == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3265 {
3266 return ipmi::responseSuccess(setParmVersion, parameter, transferStatus,
3267 std::nullopt);
3268 }
3269
3270 if (parameter != static_cast<uint8_t>(BootOptionParameter::bootFlags))
3271 {
3272 phosphor::logging::log<phosphor::logging::level::ERR>(
3273 "Unsupported parameter");
3274 return ipmi::responseResponseError();
3275 }
3276
3277 try
3278 {
3279 auto oneTimeEnabled = false;
3280 // read one time Enabled property
3281 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3282 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3283 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3284 enabledIntf, oneTimeBootEnableProp);
3285 oneTimeEnabled = std::get<bool>(variant);
3286
3287 // get BootSource and BootMode properties
3288 // according to oneTimeEnable
3289 auto bootObjPath = oneTimePath;
3290 if (oneTimeEnabled == false)
3291 {
3292 bootObjPath = persistentObjPath;
3293 }
3294
3295 service = getService(*dbus, bootModeIntf, bootObjPath);
3296 variant = getDbusProperty(*dbus, service, bootObjPath, bootModeIntf,
3297 bootModeProp);
3298
3299 auto bootMode =
3300 Mode::convertModesFromString(std::get<std::string>(variant));
3301
3302 service = getService(*dbus, bootSourceIntf, bootObjPath);
3303 variant = getDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3304 bootSourceProp);
3305
3306 if (std::get<std::string>(variant) == httpBootMode)
3307 {
3308 bootOption = httpBoot;
3309 }
3310 else
3311 {
3312 auto bootSource = Source::convertSourcesFromString(
3313 std::get<std::string>(variant));
3314 bootOption = sourceDbusToIpmi.at(bootSource);
3315 if (Source::Sources::Default == bootSource)
3316 {
3317 bootOption = modeDbusToIpmi.at(bootMode);
3318 }
3319 }
3320
3321 uint8_t oneTime = oneTimeEnabled ? setParmBootFlagsValidOneTime
3322 : setParmBootFlagsValidPermanent;
3323 bootOption <<= 2; // shift for responseconstexpr
3324 return ipmi::responseSuccess(setParmVersion, parameter, oneTime,
3325 bootOption);
3326 }
3327 catch (sdbusplus::exception_t& e)
3328 {
3329 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3330 return ipmi::responseResponseError();
3331 }
3332}
3333
3334ipmi::RspType<> ipmiOemSetEfiBootOptions(uint8_t bootFlag, uint8_t bootParam,
3335 std::optional<uint8_t> bootOption)
3336{
3337 using namespace boot_options;
3338 auto oneTimeEnabled = false;
3339
3340 if (bootFlag == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3341 {
3342 if (bootOption)
3343 {
3344 return ipmi::responseReqDataLenInvalid();
3345 }
3346
3347 if (transferStatus == setInProgress)
3348 {
3349 phosphor::logging::log<phosphor::logging::level::ERR>(
3350 "boot option set in progress!");
3351 return ipmi::responseResponseError();
3352 }
3353
3354 transferStatus = bootParam;
3355 return ipmi::responseSuccess();
3356 }
3357
3358 if (bootFlag != (uint8_t)BootOptionParameter::bootFlags)
3359 {
3360 phosphor::logging::log<phosphor::logging::level::ERR>(
3361 "Unsupported parameter");
3362 return ipmi::responseResponseError();
3363 }
3364
3365 if (!bootOption)
3366 {
3367 return ipmi::responseReqDataLenInvalid();
3368 }
3369
3370 if (((bootOption.value() & bootSourceMask) >> 2) !=
3371 httpBoot) // not http boot, exit
3372 {
3373 phosphor::logging::log<phosphor::logging::level::ERR>(
3374 "wrong boot option parameter!");
3375 return ipmi::responseParmOutOfRange();
3376 }
3377
3378 try
3379 {
3380 bool permanent = (bootParam & setParmBootFlagsPermanent) ==
3381 setParmBootFlagsPermanent;
3382
3383 // read one time Enabled property
3384 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3385 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3386 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3387 enabledIntf, oneTimeBootEnableProp);
3388 oneTimeEnabled = std::get<bool>(variant);
3389
3390 /*
3391 * Check if the current boot setting is onetime or permanent, if the
3392 * request in the command is otherwise, then set the "Enabled"
3393 * property in one_time object path to 'True' to indicate onetime
3394 * and 'False' to indicate permanent.
3395 *
3396 * Once the onetime/permanent setting is applied, then the bootMode
3397 * and bootSource is updated for the corresponding object.
3398 */
3399 if (permanent == oneTimeEnabled)
3400 {
3401 setDbusProperty(*dbus, service, oneTimePath, enabledIntf,
3402 oneTimeBootEnableProp, !permanent);
3403 }
3404
3405 // set BootSource and BootMode properties
3406 // according to oneTimeEnable or persistent
3407 auto bootObjPath = oneTimePath;
3408 if (oneTimeEnabled == false)
3409 {
3410 bootObjPath = persistentObjPath;
3411 }
3412 std::string bootMode =
3413 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
3414 std::string bootSource = httpBootMode;
3415
3416 service = getService(*dbus, bootModeIntf, bootObjPath);
3417 setDbusProperty(*dbus, service, bootObjPath, bootModeIntf, bootModeProp,
3418 bootMode);
3419
3420 service = getService(*dbus, bootSourceIntf, bootObjPath);
3421 setDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3422 bootSourceProp, bootSource);
3423 }
3424 catch (sdbusplus::exception_t& e)
3425 {
3426 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3427 return ipmi::responseResponseError();
3428 }
3429
3430 return ipmi::responseSuccess();
3431}
3432
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003433using BasicVariantType =
3434 std::variant<std::vector<std::string>, std::vector<uint64_t>, std::string,
3435 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
3436 uint16_t, uint8_t, bool>;
3437using PropertyMapType =
3438 boost::container::flat_map<std::string, BasicVariantType>;
3439static constexpr const std::array<const char*, 1> psuPresenceTypes = {
3440 "xyz.openbmc_project.Configuration.PSUPresence"};
3441int getPSUAddress(ipmi::Context::ptr ctx, uint8_t& bus,
3442 std::vector<uint64_t>& addrTable)
3443{
3444 boost::system::error_code ec;
3445 GetSubTreeType subtree = ctx->bus->yield_method_call<GetSubTreeType>(
3446 ctx->yield, ec, "xyz.openbmc_project.ObjectMapper",
3447 "/xyz/openbmc_project/object_mapper",
3448 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3449 "/xyz/openbmc_project/inventory/system", 3, psuPresenceTypes);
3450 if (ec)
3451 {
3452 phosphor::logging::log<phosphor::logging::level::ERR>(
3453 "Failed to set dbus property to cold redundancy");
3454 return -1;
3455 }
3456 for (const auto& object : subtree)
3457 {
3458 std::string pathName = object.first;
3459 for (const auto& serviceIface : object.second)
3460 {
3461 std::string serviceName = serviceIface.first;
3462
3463 ec.clear();
3464 PropertyMapType propMap =
3465 ctx->bus->yield_method_call<PropertyMapType>(
3466 ctx->yield, ec, serviceName, pathName,
3467 "org.freedesktop.DBus.Properties", "GetAll",
3468 "xyz.openbmc_project.Configuration.PSUPresence");
3469 if (ec)
3470 {
3471 phosphor::logging::log<phosphor::logging::level::ERR>(
3472 "Failed to set dbus property to cold redundancy");
3473 return -1;
3474 }
3475 auto psuBus = std::get_if<uint64_t>(&propMap["Bus"]);
3476 auto psuAddress =
3477 std::get_if<std::vector<uint64_t>>(&propMap["Address"]);
3478
3479 if (psuBus == nullptr || psuAddress == nullptr)
3480 {
3481 std::cerr << "error finding necessary "
3482 "entry in configuration\n";
3483 return -1;
3484 }
3485 bus = static_cast<uint8_t>(*psuBus);
3486 addrTable = *psuAddress;
3487 return 0;
3488 }
3489 }
3490 return -1;
3491}
3492
3493static const constexpr uint8_t addrOffset = 8;
3494static const constexpr uint8_t psuRevision = 0xd9;
3495static const constexpr uint8_t defaultPSUBus = 7;
3496// Second Minor, Primary Minor, Major
3497static const constexpr size_t verLen = 3;
3498ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPSUVersion(ipmi::Context::ptr ctx)
3499{
3500 uint8_t bus = defaultPSUBus;
3501 std::vector<uint64_t> addrTable;
3502 std::vector<uint8_t> result;
3503 if (getPSUAddress(ctx, bus, addrTable))
3504 {
3505 std::cerr << "Failed to get PSU bus and address\n";
3506 return ipmi::responseResponseError();
3507 }
3508
3509 for (const auto& slaveAddr : addrTable)
3510 {
3511 std::vector<uint8_t> writeData = {psuRevision};
3512 std::vector<uint8_t> readBuf(verLen);
3513 uint8_t addr = static_cast<uint8_t>(slaveAddr) + addrOffset;
3514 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
3515
3516 auto retI2C = ipmi::i2cWriteRead(i2cBus, addr, writeData, readBuf);
3517 if (retI2C != ipmi::ccSuccess)
3518 {
3519 for (size_t idx = 0; idx < verLen; idx++)
3520 {
3521 result.emplace_back(0x00);
3522 }
3523 }
3524 else
3525 {
3526 for (const uint8_t& data : readBuf)
3527 {
3528 result.emplace_back(data);
3529 }
3530 }
3531 }
3532
3533 return ipmi::responseSuccess(result);
3534}
3535
AppaRao Puli28972062019-11-11 02:04:45 +05303536/** @brief implements the maximum size of
3537 * bridgeable messages used between KCS and
3538 * IPMB interfacesget security mode command.
3539 *
3540 * @returns IPMI completion code with following data
3541 * - KCS Buffer Size (In multiples of four bytes)
3542 * - IPMB Buffer Size (In multiples of four bytes)
3543 **/
3544ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetBufferSize()
3545{
3546 // for now this is hard coded; really this number is dependent on
3547 // the BMC kcs driver as well as the host kcs driver....
3548 // we can't know the latter.
3549 uint8_t kcsMaxBufferSize = 63 / 4;
3550 uint8_t ipmbMaxBufferSize = 128 / 4;
3551
3552 return ipmi::responseSuccess(kcsMaxBufferSize, ipmbMaxBufferSize);
3553}
3554
Jason M. Bills64796042018-10-03 16:51:55 -07003555static void registerOEMFunctions(void)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003556{
3557 phosphor::logging::log<phosphor::logging::level::INFO>(
3558 "Registering OEM commands");
Vernon Mauery98bbf692019-09-16 11:14:59 -07003559 ipmiPrintAndRegister(intel::netFnGeneral, IPMI_CMD_WILDCARD, NULL,
Jason M. Bills64796042018-10-03 16:51:55 -07003560 ipmiOEMWildcard,
3561 PRIVILEGE_USER); // wildcard default handler
Vernon Mauery98bbf692019-09-16 11:14:59 -07003562
3563 ipmiPrintAndRegister(intel::netFnApp, IPMI_CMD_WILDCARD, NULL,
Jason M. Bills64796042018-10-03 16:51:55 -07003564 ipmiOEMWildcard,
3565 PRIVILEGE_USER); // wildcard default handler
Vernon Mauery98bbf692019-09-16 11:14:59 -07003566
3567 ipmiPrintAndRegister(intel::netFnGeneral,
3568 intel::general::cmdGetChassisIdentifier, NULL,
3569 ipmiOEMGetChassisIdentifier,
3570 PRIVILEGE_USER); // get chassis identifier
3571
3572 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetSystemGUID,
3573 NULL, ipmiOEMSetSystemGUID,
3574 PRIVILEGE_ADMIN); // set system guid
Jason M. Billsb02bf092019-08-15 13:01:56 -07003575
3576 // <Disable BMC System Reset Action>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003577 registerHandler(prioOemBase, intel::netFnGeneral,
3578 intel::general::cmdDisableBMCSystemReset, Privilege::Admin,
3579 ipmiOEMDisableBMCSystemReset);
3580
Jason M. Billsb02bf092019-08-15 13:01:56 -07003581 // <Get BMC Reset Disables>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003582 registerHandler(prioOemBase, intel::netFnGeneral,
3583 intel::general::cmdGetBMCResetDisables, Privilege::Admin,
3584 ipmiOEMGetBMCResetDisables);
Jason M. Billsb02bf092019-08-15 13:01:56 -07003585
Vernon Mauery98bbf692019-09-16 11:14:59 -07003586 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetBIOSID,
3587 NULL, ipmiOEMSetBIOSID, PRIVILEGE_ADMIN);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003588
Chen Yugang7a04f3a2019-10-08 11:12:35 +08003589 registerHandler(prioOemBase, intel::netFnGeneral,
3590 intel::general::cmdGetOEMDeviceInfo, Privilege::User,
3591 ipmiOEMGetDeviceInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003592
Vernon Mauery98bbf692019-09-16 11:14:59 -07003593 ipmiPrintAndRegister(intel::netFnGeneral,
3594 intel::general::cmdGetAICSlotFRUIDSlotPosRecords, NULL,
3595 ipmiOEMGetAICFRU, PRIVILEGE_USER);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303596
Vernon Mauery98bbf692019-09-16 11:14:59 -07003597 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3598 intel::general::cmdSendEmbeddedFWUpdStatus,
3599 Privilege::Operator, ipmiOEMSendEmbeddedFwUpdStatus);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303600
Vernon Mauery98bbf692019-09-16 11:14:59 -07003601 ipmiPrintAndRegister(intel::netFnGeneral,
3602 intel::general::cmdSetPowerRestoreDelay, NULL,
3603 ipmiOEMSetPowerRestoreDelay, PRIVILEGE_OPERATOR);
3604
3605 ipmiPrintAndRegister(intel::netFnGeneral,
3606 intel::general::cmdGetPowerRestoreDelay, NULL,
3607 ipmiOEMGetPowerRestoreDelay, PRIVILEGE_USER);
3608
3609 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3610 intel::general::cmdSetOEMUser2Activation,
3611 Privilege::Callback, ipmiOEMSetUser2Activation);
3612
3613 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3614 intel::general::cmdSetSpecialUserPassword,
3615 Privilege::Callback, ipmiOEMSetSpecialUserPassword);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05303616
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003617 // <Get Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003618 registerHandler(prioOemBase, intel::netFnGeneral,
3619 intel::general::cmdGetProcessorErrConfig, Privilege::User,
3620 ipmiOEMGetProcessorErrConfig);
3621
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003622 // <Set Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003623 registerHandler(prioOemBase, intel::netFnGeneral,
3624 intel::general::cmdSetProcessorErrConfig, Privilege::Admin,
3625 ipmiOEMSetProcessorErrConfig);
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003626
Vernon Mauery98bbf692019-09-16 11:14:59 -07003627 ipmiPrintAndRegister(intel::netFnGeneral,
3628 intel::general::cmdSetShutdownPolicy, NULL,
3629 ipmiOEMSetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003630
Vernon Mauery98bbf692019-09-16 11:14:59 -07003631 ipmiPrintAndRegister(intel::netFnGeneral,
3632 intel::general::cmdGetShutdownPolicy, NULL,
3633 ipmiOEMGetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003634
anil kumar appanaf945eee2019-09-25 23:29:11 +00003635 registerHandler(prioOemBase, intel::netFnGeneral,
3636 intel::general::cmdSetFanConfig, Privilege::User,
3637 ipmiOEMSetFanConfig);
James Feist91244a62019-02-19 15:04:54 -08003638
Vernon Mauery98bbf692019-09-16 11:14:59 -07003639 registerHandler(prioOemBase, intel::netFnGeneral,
3640 intel::general::cmdGetFanConfig, Privilege::User,
3641 ipmiOEMGetFanConfig);
James Feist5f957ca2019-03-14 15:33:55 -07003642
Vernon Mauery98bbf692019-09-16 11:14:59 -07003643 registerHandler(prioOemBase, intel::netFnGeneral,
3644 intel::general::cmdGetFanSpeedOffset, Privilege::User,
3645 ipmiOEMGetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003646
Vernon Mauery98bbf692019-09-16 11:14:59 -07003647 registerHandler(prioOemBase, intel::netFnGeneral,
3648 intel::general::cmdSetFanSpeedOffset, Privilege::User,
3649 ipmiOEMSetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003650
Vernon Mauery98bbf692019-09-16 11:14:59 -07003651 registerHandler(prioOemBase, intel::netFnGeneral,
3652 intel::general::cmdSetFscParameter, Privilege::User,
3653 ipmiOEMSetFscParameter);
James Feist5f957ca2019-03-14 15:33:55 -07003654
Vernon Mauery98bbf692019-09-16 11:14:59 -07003655 registerHandler(prioOemBase, intel::netFnGeneral,
3656 intel::general::cmdGetFscParameter, Privilege::User,
3657 ipmiOEMGetFscParameter);
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05303658
Vernon Mauery98bbf692019-09-16 11:14:59 -07003659 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3660 intel::general::cmdReadBaseBoardProductId, Privilege::Admin,
3661 ipmiOEMReadBoardProductId);
Chen Yugang39736d52019-07-12 16:24:33 +08003662
Vernon Mauery98bbf692019-09-16 11:14:59 -07003663 registerHandler(prioOemBase, intel::netFnGeneral,
3664 intel::general::cmdGetNmiStatus, Privilege::User,
3665 ipmiOEMGetNmiSource);
Chen Yugang39736d52019-07-12 16:24:33 +08003666
Vernon Mauery98bbf692019-09-16 11:14:59 -07003667 registerHandler(prioOemBase, intel::netFnGeneral,
3668 intel::general::cmdSetNmiStatus, Privilege::Operator,
3669 ipmiOEMSetNmiSource);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003670
Vernon Mauery98bbf692019-09-16 11:14:59 -07003671 registerHandler(prioOemBase, intel::netFnGeneral,
3672 intel::general::cmdGetEfiBootOptions, Privilege::User,
3673 ipmiOemGetEfiBootOptions);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003674
Vernon Mauery98bbf692019-09-16 11:14:59 -07003675 registerHandler(prioOemBase, intel::netFnGeneral,
3676 intel::general::cmdSetEfiBootOptions, Privilege::Operator,
3677 ipmiOemSetEfiBootOptions);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303678
Vernon Mauery98bbf692019-09-16 11:14:59 -07003679 registerHandler(prioOemBase, intel::netFnGeneral,
3680 intel::general::cmdGetSecurityMode, Privilege::User,
3681 ipmiGetSecurityMode);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303682
Vernon Mauery98bbf692019-09-16 11:14:59 -07003683 registerHandler(prioOemBase, intel::netFnGeneral,
3684 intel::general::cmdSetSecurityMode, Privilege::Admin,
3685 ipmiSetSecurityMode);
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003686
Vernon Mauery98bbf692019-09-16 11:14:59 -07003687 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdGetLEDStatus,
3688 NULL, ipmiOEMGetLEDStatus, PRIVILEGE_ADMIN);
Cheng C Yang773703a2019-08-15 09:41:11 +08003689
Vernon Mauery98bbf692019-09-16 11:14:59 -07003690 ipmiPrintAndRegister(ipmi::intel::netFnPlatform,
3691 ipmi::intel::platform::cmdCfgHostSerialPortSpeed, NULL,
3692 ipmiOEMCfgHostSerialPortSpeed, PRIVILEGE_ADMIN);
3693
3694 registerHandler(prioOemBase, intel::netFnGeneral,
3695 intel::general::cmdSetFaultIndication, Privilege::Operator,
3696 ipmiOEMSetFaultIndication);
3697
3698 registerHandler(prioOemBase, intel::netFnGeneral,
3699 intel::general::cmdSetColdRedundancyConfig, Privilege::User,
3700 ipmiOEMSetCRConfig);
3701
3702 registerHandler(prioOemBase, intel::netFnGeneral,
3703 intel::general::cmdGetColdRedundancyConfig, Privilege::User,
3704 ipmiOEMGetCRConfig);
3705
3706 registerHandler(prioOemBase, intel::netFnGeneral,
3707 intel::general::cmdRestoreConfiguration, Privilege::Admin,
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003708 ipmiRestoreConfiguration);
James Feist63efafa2019-07-24 12:39:21 -07003709
Vernon Mauery98bbf692019-09-16 11:14:59 -07003710 registerHandler(prioOemBase, intel::netFnGeneral,
3711 intel::general::cmdSetDimmOffset, Privilege::Operator,
3712 ipmiOEMSetDimmOffset);
James Feist63efafa2019-07-24 12:39:21 -07003713
Vernon Mauery98bbf692019-09-16 11:14:59 -07003714 registerHandler(prioOemBase, intel::netFnGeneral,
3715 intel::general::cmdGetDimmOffset, Privilege::Operator,
3716 ipmiOEMGetDimmOffset);
Chen Yugangca12a7b2019-09-03 18:11:44 +08003717
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003718 registerHandler(prioOemBase, intel::netFnGeneral,
3719 intel::general::cmdGetPSUVersion, Privilege::User,
3720 ipmiOEMGetPSUVersion);
AppaRao Puli28972062019-11-11 02:04:45 +05303721
3722 registerHandler(prioOemBase, intel::netFnGeneral,
3723 intel::general::cmdGetBufferSize, Privilege::User,
3724 ipmiOEMGetBufferSize);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003725}
3726
3727} // namespace ipmi