blob: 2d639fb852f560b4e2900b3dd66a18a34b3efbf0 [file] [log] [blame]
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Jason M. Bills64796042018-10-03 16:51:55 -070017#include "xyz/openbmc_project/Common/error.hpp"
Kuiying Wang45f04982018-12-26 09:23:08 +080018#include "xyz/openbmc_project/Led/Physical/server.hpp"
Jason M. Bills64796042018-10-03 16:51:55 -070019
Jia, Chunhuicc49b542019-03-20 15:41:07 +080020#include <systemd/sd-journal.h>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080021
Chen Yugang7a04f3a2019-10-08 11:12:35 +080022#include <appcommands.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080023#include <array>
James Feist91244a62019-02-19 15:04:54 -080024#include <boost/container/flat_map.hpp>
Yong Li23737fe2019-02-19 08:49:55 +080025#include <boost/process/child.hpp>
26#include <boost/process/io.hpp>
Yong Li0669d192019-05-06 14:01:46 +080027#include <com/intel/Control/OCOTShutdownPolicy/server.hpp>
Jason M. Bills64796042018-10-03 16:51:55 -070028#include <commandutils.hpp>
Vernon Mauery4ac799d2019-05-20 15:50:37 -070029#include <filesystem>
Zhikui Rence4e73f2019-12-06 13:59:47 -080030#include <gpiod.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080031#include <iostream>
Jia, Chunhuicc49b542019-03-20 15:41:07 +080032#include <ipmid/api.hpp>
Vernon Mauery5480ef62019-03-20 13:43:11 -070033#include <ipmid/utils.hpp>
James Feist63efafa2019-07-24 12:39:21 -070034#include <nlohmann/json.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080035#include <oemcommands.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080036#include <phosphor-logging/log.hpp>
Chen Yugang7a04f3a2019-10-08 11:12:35 +080037#include <regex>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080038#include <sdbusplus/bus.hpp>
Suryakanth Sekard509eb92018-11-15 17:44:11 +053039#include <sdbusplus/message/types.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080040#include <string>
James Feist91244a62019-02-19 15:04:54 -080041#include <variant>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080042#include <vector>
Chen Yugang97cf96e2019-11-01 08:55:11 +080043#include <xyz/openbmc_project/Chassis/Control/NMISource/server.hpp>
Chen,Yugang4f7e76b2019-08-20 09:28:06 +080044#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
45#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
Cheng C Yang773703a2019-08-15 09:41:11 +080046#include <xyz/openbmc_project/Control/PowerSupplyRedundancy/server.hpp>
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053047#include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp>
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +053048#include <xyz/openbmc_project/Control/Security/SpecialMode/server.hpp>
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080049
50namespace ipmi
51{
Jason M. Bills64796042018-10-03 16:51:55 -070052static void registerOEMFunctions() __attribute__((constructor));
Vernon Mauery4ac799d2019-05-20 15:50:37 -070053
Jason M. Bills64796042018-10-03 16:51:55 -070054static constexpr size_t maxFRUStringLength = 0x3F;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +080055
Suryakanth Sekard509eb92018-11-15 17:44:11 +053056static constexpr auto ethernetIntf =
57 "xyz.openbmc_project.Network.EthernetInterface";
58static constexpr auto networkIPIntf = "xyz.openbmc_project.Network.IP";
59static constexpr auto networkService = "xyz.openbmc_project.Network";
60static constexpr auto networkRoot = "/xyz/openbmc_project/network";
61
Chen Yugang97cf96e2019-11-01 08:55:11 +080062static constexpr const char* oemNmiSourceIntf =
63 "xyz.openbmc_project.Chassis.Control.NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080064static constexpr const char* oemNmiSourceObjPath =
Chen Yugang97cf96e2019-11-01 08:55:11 +080065 "/xyz/openbmc_project/Chassis/Control/NMISource";
Chen Yugang39736d52019-07-12 16:24:33 +080066static constexpr const char* oemNmiBmcSourceObjPathProp = "BMCSource";
67static constexpr const char* oemNmiEnabledObjPathProp = "Enabled";
68
James Feist63efafa2019-07-24 12:39:21 -070069static constexpr const char* dimmOffsetFile = "/var/lib/ipmi/ipmi_dimms.json";
70
Chen Yugang39736d52019-07-12 16:24:33 +080071enum class NmiSource : uint8_t
72{
73 none = 0,
Chen Yugang97cf96e2019-11-01 08:55:11 +080074 frontPanelButton = 1,
75 watchdog = 2,
76 chassisCmd = 3,
77 memoryError = 4,
78 pciBusError = 5,
79 pch = 6,
80 chipset = 7,
Chen Yugang39736d52019-07-12 16:24:33 +080081};
82
Suryakanth Sekar822b0b42019-11-15 18:32:53 +053083enum class SpecialUserIndex : uint8_t
84{
85 rootUser = 0,
86 atScaleDebugUser = 1
87};
88
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +053089static constexpr const char* restricionModeService =
90 "xyz.openbmc_project.RestrictionMode.Manager";
91static constexpr const char* restricionModeBasePath =
92 "/xyz/openbmc_project/control/security/restriction_mode";
93static constexpr const char* restricionModeIntf =
94 "xyz.openbmc_project.Control.Security.RestrictionMode";
95static constexpr const char* restricionModeProperty = "RestrictionMode";
96
97static constexpr const char* specialModeService =
98 "xyz.openbmc_project.SpecialMode";
99static constexpr const char* specialModeBasePath =
Richard Marian Thomaiyara7b74282019-09-22 21:53:14 +0530100 "/xyz/openbmc_project/security/special_mode";
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +0530101static constexpr const char* specialModeIntf =
102 "xyz.openbmc_project.Security.SpecialMode";
103static constexpr const char* specialModeProperty = "SpecialMode";
104
105static constexpr const char* dBusPropertyIntf =
106 "org.freedesktop.DBus.Properties";
107static constexpr const char* dBusPropertyGetMethod = "Get";
108static constexpr const char* dBusPropertySetMethod = "Set";
109
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800110// return code: 0 successful
111int8_t getChassisSerialNumber(sdbusplus::bus::bus& bus, std::string& serial)
112{
113 std::string objpath = "/xyz/openbmc_project/FruDevice";
114 std::string intf = "xyz.openbmc_project.FruDeviceManager";
115 std::string service = getService(bus, intf, objpath);
116 ObjectValueTree valueTree = getManagedObjects(bus, service, "/");
117 if (valueTree.empty())
118 {
119 phosphor::logging::log<phosphor::logging::level::ERR>(
120 "No object implements interface",
121 phosphor::logging::entry("INTF=%s", intf.c_str()));
122 return -1;
123 }
124
Jason M. Bills64796042018-10-03 16:51:55 -0700125 for (const auto& item : valueTree)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800126 {
127 auto interface = item.second.find("xyz.openbmc_project.FruDevice");
128 if (interface == item.second.end())
129 {
130 continue;
131 }
132
133 auto property = interface->second.find("CHASSIS_SERIAL_NUMBER");
134 if (property == interface->second.end())
135 {
136 continue;
137 }
138
139 try
140 {
141 Value variant = property->second;
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700142 std::string& result = std::get<std::string>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700143 if (result.size() > maxFRUStringLength)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800144 {
145 phosphor::logging::log<phosphor::logging::level::ERR>(
146 "FRU serial number exceed maximum length");
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800147 return -1;
148 }
Jason M. Bills64796042018-10-03 16:51:55 -0700149 serial = result;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800150 return 0;
151 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700152 catch (std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800153 {
Jason M. Bills64796042018-10-03 16:51:55 -0700154 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800155 return -1;
156 }
157 }
158 return -1;
159}
Jason M. Bills64796042018-10-03 16:51:55 -0700160
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800161// Returns the Chassis Identifier (serial #)
162ipmi_ret_t ipmiOEMGetChassisIdentifier(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
163 ipmi_request_t request,
164 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700165 ipmi_data_len_t dataLen,
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800166 ipmi_context_t context)
167{
168 std::string serial;
Jason M. Bills64796042018-10-03 16:51:55 -0700169 if (*dataLen != 0) // invalid request if there are extra parameters
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800170 {
Jason M. Bills64796042018-10-03 16:51:55 -0700171 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800172 return IPMI_CC_REQ_DATA_LEN_INVALID;
173 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700174 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
175 if (getChassisSerialNumber(*dbus, serial) == 0)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800176 {
Jason M. Bills64796042018-10-03 16:51:55 -0700177 *dataLen = serial.size(); // length will never exceed response length
178 // as it is checked in getChassisSerialNumber
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800179 char* resp = static_cast<char*>(response);
Jason M. Bills64796042018-10-03 16:51:55 -0700180 serial.copy(resp, *dataLen);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800181 return IPMI_CC_OK;
182 }
Jason M. Bills64796042018-10-03 16:51:55 -0700183 *dataLen = 0;
184 return IPMI_CC_RESPONSE_ERROR;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800185}
186
187ipmi_ret_t ipmiOEMSetSystemGUID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
188 ipmi_request_t request,
189 ipmi_response_t response,
Jason M. Bills64796042018-10-03 16:51:55 -0700190 ipmi_data_len_t dataLen, ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800191{
192 static constexpr size_t safeBufferLength = 50;
193 char buf[safeBufferLength] = {0};
194 GUIDData* Data = reinterpret_cast<GUIDData*>(request);
195
Jason M. Bills64796042018-10-03 16:51:55 -0700196 if (*dataLen != sizeof(GUIDData)) // 16bytes
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800197 {
Jason M. Bills64796042018-10-03 16:51:55 -0700198 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800199 return IPMI_CC_REQ_DATA_LEN_INVALID;
200 }
201
Jason M. Bills64796042018-10-03 16:51:55 -0700202 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800203
204 snprintf(
205 buf, safeBufferLength,
206 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
207 Data->timeLow4, Data->timeLow3, Data->timeLow2, Data->timeLow1,
208 Data->timeMid2, Data->timeMid1, Data->timeHigh2, Data->timeHigh1,
209 Data->clock2, Data->clock1, Data->node6, Data->node5, Data->node4,
210 Data->node3, Data->node2, Data->node1);
211 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
212 std::string guid = buf;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800213
214 std::string objpath = "/xyz/openbmc_project/control/host0/systemGUID";
215 std::string intf = "xyz.openbmc_project.Common.UUID";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700216 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
217 std::string service = getService(*dbus, intf, objpath);
218 setDbusProperty(*dbus, service, objpath, intf, "UUID", guid);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800219 return IPMI_CC_OK;
220}
221
Jason M. Billsb02bf092019-08-15 13:01:56 -0700222ipmi::RspType<> ipmiOEMDisableBMCSystemReset(bool disableResetOnSMI,
223 uint7_t reserved1)
224{
225 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
226
227 try
228 {
229 auto service =
230 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
231 ipmi::setDbusProperty(*busp, service, bmcResetDisablesPath,
232 bmcResetDisablesIntf, "ResetOnSMI",
233 !disableResetOnSMI);
234 }
235 catch (std::exception& e)
236 {
237 phosphor::logging::log<phosphor::logging::level::ERR>(
238 "Failed to set BMC reset disables",
239 phosphor::logging::entry("EXCEPTION=%s", e.what()));
240 return ipmi::responseUnspecifiedError();
241 }
242
243 return ipmi::responseSuccess();
244}
245
246ipmi::RspType<bool, // disableResetOnSMI
247 uint7_t // reserved
248 >
249 ipmiOEMGetBMCResetDisables()
250{
251 bool disableResetOnSMI = true;
252
253 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
254 try
255 {
256 auto service =
257 ipmi::getService(*busp, bmcResetDisablesIntf, bmcResetDisablesPath);
258 Value variant =
259 ipmi::getDbusProperty(*busp, service, bmcResetDisablesPath,
260 bmcResetDisablesIntf, "ResetOnSMI");
261 disableResetOnSMI = !std::get<bool>(variant);
262 }
263 catch (std::exception& e)
264 {
265 phosphor::logging::log<phosphor::logging::level::ERR>(
266 "Failed to get BMC reset disables",
267 phosphor::logging::entry("EXCEPTION=%s", e.what()));
268 return ipmi::responseUnspecifiedError();
269 }
270
271 return ipmi::responseSuccess(disableResetOnSMI, 0);
272}
273
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800274ipmi_ret_t ipmiOEMSetBIOSID(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
275 ipmi_request_t request, ipmi_response_t response,
276 ipmi_data_len_t dataLen, ipmi_context_t context)
277{
278 DeviceInfo* data = reinterpret_cast<DeviceInfo*>(request);
279
Jason M. Bills64796042018-10-03 16:51:55 -0700280 if ((*dataLen < 2) || (*dataLen != (1 + data->biosIDLength)))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800281 {
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800282 *dataLen = 0;
283 return IPMI_CC_REQ_DATA_LEN_INVALID;
284 }
Jason M. Bills64796042018-10-03 16:51:55 -0700285 std::string idString((char*)data->biosId, data->biosIDLength);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800286
Vernon Mauery15419dd2019-05-24 09:40:30 -0700287 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800288 std::string service = getService(*dbus, biosVersionIntf, biosObjPath);
289 setDbusProperty(*dbus, service, biosObjPath, biosVersionIntf,
290 biosVersionProp, idString);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800291 uint8_t* bytesWritten = static_cast<uint8_t*>(response);
292 *bytesWritten =
Jason M. Bills64796042018-10-03 16:51:55 -0700293 data->biosIDLength; // how many bytes are written into storage
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800294 *dataLen = 1;
295 return IPMI_CC_OK;
296}
297
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530298bool getSwVerInfo(ipmi::Context::ptr ctx, uint8_t& bmcMajor, uint8_t& bmcMinor,
299 uint8_t& meMajor, uint8_t& meMinor)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800300{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800301 // step 1 : get BMC Major and Minor numbers from its DBUS property
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530302 std::string bmcVersion;
303 if (getActiveSoftwareVersionInfo(ctx, versionPurposeBMC, bmcVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800304 {
305 return false;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800306 }
307
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530308 std::optional<MetaRevision> rev = convertIntelVersion(bmcVersion);
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800309 if (rev.has_value())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800310 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800311 MetaRevision revision = rev.value();
312 bmcMajor = revision.major;
313
314 revision.minor = (revision.minor > 99 ? 99 : revision.minor);
315 bmcMinor = revision.minor % 10 + (revision.minor / 10) * 16;
316 }
317
318 // step 2 : get ME Major and Minor numbers from its DBUS property
AppaRao Puli32825a22020-01-17 15:52:41 +0530319 std::string meVersion;
320 if (getActiveSoftwareVersionInfo(ctx, versionPurposeME, meVersion))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800321 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800322 return false;
323 }
AppaRao Puli32825a22020-01-17 15:52:41 +0530324 std::regex pattern1("(\\d+?).(\\d+?).(\\d+?).(\\d+?).(\\d+?)");
325 constexpr size_t matchedPhosphor = 6;
326 std::smatch results;
327 if (std::regex_match(meVersion, results, pattern1))
328 {
329 if (results.size() == matchedPhosphor)
330 {
331 meMajor = static_cast<uint8_t>(std::stoi(results[1]));
332 meMinor = static_cast<uint8_t>(std::stoi(results[2]));
333 }
334 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800335 return true;
336}
337
338ipmi::RspType<
339 std::variant<std::string,
340 std::tuple<uint8_t, std::array<uint8_t, 2>,
341 std::array<uint8_t, 2>, std::array<uint8_t, 2>,
342 std::array<uint8_t, 2>, std::array<uint8_t, 2>>,
343 std::tuple<uint8_t, std::array<uint8_t, 2>>>>
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530344 ipmiOEMGetDeviceInfo(ipmi::Context::ptr ctx, uint8_t entityType,
345 std::optional<uint8_t> countToRead,
AppaRao Pulid46cb422020-01-21 18:40:21 +0530346 std::optional<uint8_t> offset)
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800347{
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800348 if (entityType > static_cast<uint8_t>(OEMDevEntityType::sdrVer))
349 {
350 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800351 }
352
353 // handle OEM command items
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800354 switch (OEMDevEntityType(entityType))
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800355 {
356 case OEMDevEntityType::biosId:
357 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530358 // Byte 2&3, Only used with selecting BIOS
359 if (!countToRead || !offset)
360 {
361 return ipmi::responseReqDataLenInvalid();
362 }
363
Vernon Mauery15419dd2019-05-24 09:40:30 -0700364 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li2742b852019-12-16 14:55:11 +0800365 std::string service =
366 getService(*dbus, biosVersionIntf, biosObjPath);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800367 try
368 {
Yong Li2742b852019-12-16 14:55:11 +0800369 Value variant =
370 getDbusProperty(*dbus, service, biosObjPath,
371 biosVersionIntf, biosVersionProp);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700372 std::string& idString = std::get<std::string>(variant);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530373 if (*offset >= idString.size())
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800374 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800375 return ipmi::responseParmOutOfRange();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800376 }
Jason M. Bills64796042018-10-03 16:51:55 -0700377 size_t length = 0;
AppaRao Pulid46cb422020-01-21 18:40:21 +0530378 if (*countToRead > (idString.size() - *offset))
Jason M. Bills64796042018-10-03 16:51:55 -0700379 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530380 length = idString.size() - *offset;
Jason M. Bills64796042018-10-03 16:51:55 -0700381 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800382 else
383 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530384 length = *countToRead;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800385 }
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800386
387 std::string readBuf = {0};
388 readBuf.resize(length);
AppaRao Pulid46cb422020-01-21 18:40:21 +0530389 std::copy_n(idString.begin() + *offset, length,
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800390 (readBuf.begin()));
391 return ipmi::responseSuccess(readBuf);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800392 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700393 catch (std::bad_variant_access& e)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800394 {
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800395 return ipmi::responseUnspecifiedError();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800396 }
397 }
398 break;
399
400 case OEMDevEntityType::devVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800401 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530402 // Byte 2&3, Only used with selecting BIOS
403 if (countToRead || offset)
404 {
405 return ipmi::responseReqDataLenInvalid();
406 }
407
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800408 constexpr const size_t verLen = 2;
409 constexpr const size_t verTotalLen = 10;
410 std::array<uint8_t, verLen> bmcBuf = {0xff, 0xff};
411 std::array<uint8_t, verLen> hsc0Buf = {0xff, 0xff};
412 std::array<uint8_t, verLen> hsc1Buf = {0xff, 0xff};
413 std::array<uint8_t, verLen> meBuf = {0xff, 0xff};
414 std::array<uint8_t, verLen> hsc2Buf = {0xff, 0xff};
415 // data0/1: BMC version number; data6/7: ME version number
416 // the others: HSC0/1/2 version number, not avaible.
AppaRao Pulie99e7ed2020-01-17 12:27:10 +0530417 if (!getSwVerInfo(ctx, bmcBuf[0], bmcBuf[1], meBuf[0], meBuf[1]))
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800418 {
419 return ipmi::responseUnspecifiedError();
420 }
421 return ipmi::responseSuccess(
422 std::tuple<
423 uint8_t, std::array<uint8_t, verLen>,
424 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>,
425 std::array<uint8_t, verLen>, std::array<uint8_t, verLen>>{
426 verTotalLen, bmcBuf, hsc0Buf, hsc1Buf, meBuf, hsc2Buf});
427 }
428 break;
429
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800430 case OEMDevEntityType::sdrVer:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800431 {
AppaRao Pulid46cb422020-01-21 18:40:21 +0530432 // Byte 2&3, Only used with selecting BIOS
433 if (countToRead || offset)
434 {
435 return ipmi::responseReqDataLenInvalid();
436 }
437
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800438 constexpr const size_t sdrLen = 2;
439 std::array<uint8_t, sdrLen> readBuf = {0x01, 0x0};
440 return ipmi::responseSuccess(
441 std::tuple<uint8_t, std::array<uint8_t, sdrLen>>{sdrLen,
442 readBuf});
443 }
444 break;
445
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800446 default:
Chen Yugang7a04f3a2019-10-08 11:12:35 +0800447 return ipmi::responseInvalidFieldRequest();
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800448 }
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800449}
450
451ipmi_ret_t ipmiOEMGetAICFRU(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
452 ipmi_request_t request, ipmi_response_t response,
453 ipmi_data_len_t dataLen, ipmi_context_t context)
454{
455 if (*dataLen != 0)
456 {
Jason M. Bills64796042018-10-03 16:51:55 -0700457 *dataLen = 0;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800458 return IPMI_CC_REQ_DATA_LEN_INVALID;
459 }
460
461 *dataLen = 1;
462 uint8_t* res = reinterpret_cast<uint8_t*>(response);
463 // temporary fix. We don't support AIC FRU now. Just tell BIOS that no
464 // AIC is available so that BIOS will not timeout repeatly which leads to
465 // slow booting.
466 *res = 0; // Byte1=Count of SlotPosition/FruID records.
467 return IPMI_CC_OK;
468}
469
Jason M. Bills64796042018-10-03 16:51:55 -0700470ipmi_ret_t ipmiOEMGetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
471 ipmi_request_t request,
472 ipmi_response_t response,
473 ipmi_data_len_t dataLen,
474 ipmi_context_t context)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800475{
Jason M. Bills64796042018-10-03 16:51:55 -0700476 GetPowerRestoreDelayRes* resp =
477 reinterpret_cast<GetPowerRestoreDelayRes*>(response);
478
479 if (*dataLen != 0)
480 {
481 *dataLen = 0;
482 return IPMI_CC_REQ_DATA_LEN_INVALID;
483 }
484
Vernon Mauery15419dd2019-05-24 09:40:30 -0700485 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700486 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700487 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
Jason M. Bills64796042018-10-03 16:51:55 -0700488 Value variant =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700489 getDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700490 powerRestoreDelayIntf, powerRestoreDelayProp);
491
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700492 uint16_t delay = std::get<uint16_t>(variant);
Jason M. Bills64796042018-10-03 16:51:55 -0700493 resp->byteLSB = delay;
494 resp->byteMSB = delay >> 8;
495
496 *dataLen = sizeof(GetPowerRestoreDelayRes);
497
498 return IPMI_CC_OK;
Jia, Chunhuia835eaa2018-09-05 09:00:41 +0800499}
500
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800501static uint8_t bcdToDec(uint8_t val)
502{
503 return ((val / 16 * 10) + (val % 16));
504}
505
506// Allows an update utility or system BIOS to send the status of an embedded
507// firmware update attempt to the BMC. After received, BMC will create a logging
508// record.
509ipmi::RspType<> ipmiOEMSendEmbeddedFwUpdStatus(uint8_t status, uint8_t target,
510 uint8_t majorRevision,
511 uint8_t minorRevision,
512 uint32_t auxInfo)
513{
514 std::string firmware;
Jason M. Billsdc249272019-04-03 09:58:40 -0700515 int instance = (target & targetInstanceMask) >> targetInstanceShift;
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800516 target = (target & selEvtTargetMask) >> selEvtTargetShift;
517
518 /* make sure the status is 0, 1, or 2 as per the spec */
519 if (status > 2)
520 {
521 return ipmi::response(ipmi::ccInvalidFieldRequest);
522 }
Jason M. Billsdc249272019-04-03 09:58:40 -0700523 /* make sure the target is 0, 1, 2, or 4 as per the spec */
524 if (target > 4 || target == 3)
525 {
526 return ipmi::response(ipmi::ccInvalidFieldRequest);
527 }
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800528 /*orignal OEM command is to record OEM SEL.
529 But openbmc does not support OEM SEL, so we redirect it to redfish event
530 logging. */
531 std::string buildInfo;
532 std::string action;
533 switch (FWUpdateTarget(target))
534 {
535 case FWUpdateTarget::targetBMC:
536 firmware = "BMC";
Jason M. Billsdc249272019-04-03 09:58:40 -0700537 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800538 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
539 " BuildID: " + std::to_string(auxInfo);
540 buildInfo += std::to_string(auxInfo);
541 break;
542 case FWUpdateTarget::targetBIOS:
543 firmware = "BIOS";
544 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700545 "major: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800546 std::to_string(bcdToDec(majorRevision)) + // BCD encoded
547 " minor: " +
548 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
549 " ReleaseNumber: " + // ASCII encoded
550 std::to_string(static_cast<uint8_t>(auxInfo >> 0) - '0') +
551 std::to_string(static_cast<uint8_t>(auxInfo >> 8) - '0') +
552 std::to_string(static_cast<uint8_t>(auxInfo >> 16) - '0') +
553 std::to_string(static_cast<uint8_t>(auxInfo >> 24) - '0');
554 break;
555 case FWUpdateTarget::targetME:
556 firmware = "ME";
557 buildInfo =
Jason M. Billsdc249272019-04-03 09:58:40 -0700558 "major: " + std::to_string(majorRevision) + " minor1: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800559 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
560 " minor2: " +
561 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 0))) +
562 " build1: " +
563 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 8))) +
564 " build2: " +
565 std::to_string(bcdToDec(static_cast<uint8_t>(auxInfo >> 16)));
566 break;
567 case FWUpdateTarget::targetOEMEWS:
568 firmware = "EWS";
Jason M. Billsdc249272019-04-03 09:58:40 -0700569 buildInfo = "major: " + std::to_string(majorRevision) + " minor: " +
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800570 std::to_string(bcdToDec(minorRevision)) + // BCD encoded
571 " BuildID: " + std::to_string(auxInfo);
572 break;
573 }
574
Jason M. Billsdc249272019-04-03 09:58:40 -0700575 static const std::string openBMCMessageRegistryVersion("0.1");
576 std::string redfishMsgID = "OpenBMC." + openBMCMessageRegistryVersion;
577
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800578 switch (status)
579 {
580 case 0x0:
581 action = "update started";
Jason M. Billsdc249272019-04-03 09:58:40 -0700582 redfishMsgID += ".FirmwareUpdateStarted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800583 break;
584 case 0x1:
585 action = "update completed successfully";
Jason M. Billsdc249272019-04-03 09:58:40 -0700586 redfishMsgID += ".FirmwareUpdateCompleted";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800587 break;
588 case 0x2:
589 action = "update failure";
Jason M. Billsdc249272019-04-03 09:58:40 -0700590 redfishMsgID += ".FirmwareUpdateFailed";
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800591 break;
592 default:
593 action = "unknown";
594 break;
595 }
596
Jason M. Billsdc249272019-04-03 09:58:40 -0700597 std::string firmwareInstanceStr =
598 firmware + " instance: " + std::to_string(instance);
599 std::string message("[firmware update] " + firmwareInstanceStr +
600 " status: <" + action + "> " + buildInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800601
602 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", LOG_INFO,
Jason M. Billsdc249272019-04-03 09:58:40 -0700603 "REDFISH_MESSAGE_ID=%s", redfishMsgID.c_str(),
604 "REDFISH_MESSAGE_ARGS=%s,%s", firmwareInstanceStr.c_str(),
605 buildInfo.c_str(), NULL);
Jia, Chunhuicc49b542019-03-20 15:41:07 +0800606 return ipmi::responseSuccess();
607}
608
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +0530609ipmi::RspType<uint8_t, std::vector<uint8_t>>
610 ipmiOEMSlotIpmb(ipmi::Context::ptr ctx, uint6_t reserved1,
611 uint2_t slotNumber, uint3_t baseBoardSlotNum,
612 uint3_t riserSlotNum, uint2_t reserved2, uint8_t slaveAddr,
613 uint8_t netFn, uint8_t cmd,
614 std::optional<std::vector<uint8_t>> writeData)
615{
616 if (reserved1 || reserved2)
617 {
618 return ipmi::responseInvalidFieldRequest();
619 }
620
621 boost::system::error_code ec;
622 using ipmbResponse = std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t,
623 std::vector<uint8_t>>;
624 ipmbResponse res = ctx->bus->yield_method_call<ipmbResponse>(
625 ctx->yield, ec, "xyz.openbmc_project.Ipmi.Channel.Ipmb",
626 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
627 "SlotIpmbRequest", static_cast<uint8_t>(slotNumber),
628 static_cast<uint8_t>(baseBoardSlotNum), slaveAddr, netFn, cmd,
629 *writeData);
630 if (ec)
631 {
632 phosphor::logging::log<phosphor::logging::level::ERR>(
633 "Failed to call dbus method SlotIpmbRequest");
634 return ipmi::responseUnspecifiedError();
635 }
636
637 std::vector<uint8_t> dataReceived(0);
638 int status = -1;
639 uint8_t resNetFn = 0, resLun = 0, resCmd = 0, cc = 0;
640
641 std::tie(status, resNetFn, resLun, resCmd, cc, dataReceived) = res;
642
643 if (status)
644 {
645 phosphor::logging::log<phosphor::logging::level::ERR>(
646 "Failed to get response from SlotIpmbRequest");
647 return ipmi::responseResponseError();
648 }
649 return ipmi::responseSuccess(cc, dataReceived);
650}
651
Jason M. Bills64796042018-10-03 16:51:55 -0700652ipmi_ret_t ipmiOEMSetPowerRestoreDelay(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
653 ipmi_request_t request,
654 ipmi_response_t response,
655 ipmi_data_len_t dataLen,
656 ipmi_context_t context)
657{
658 SetPowerRestoreDelayReq* data =
659 reinterpret_cast<SetPowerRestoreDelayReq*>(request);
660 uint16_t delay = 0;
661
662 if (*dataLen != sizeof(SetPowerRestoreDelayReq))
663 {
664 *dataLen = 0;
665 return IPMI_CC_REQ_DATA_LEN_INVALID;
666 }
667 delay = data->byteMSB;
668 delay = (delay << 8) | data->byteLSB;
Vernon Mauery15419dd2019-05-24 09:40:30 -0700669 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700670 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700671 getService(*dbus, powerRestoreDelayIntf, powerRestoreDelayObjPath);
672 setDbusProperty(*dbus, service, powerRestoreDelayObjPath,
Jason M. Bills64796042018-10-03 16:51:55 -0700673 powerRestoreDelayIntf, powerRestoreDelayProp, delay);
674 *dataLen = 0;
675
676 return IPMI_CC_OK;
677}
678
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700679static bool cpuPresent(const std::string& cpuName)
Jason M. Bills64796042018-10-03 16:51:55 -0700680{
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700681 static constexpr const char* cpuPresencePathPrefix =
682 "/xyz/openbmc_project/inventory/system/chassis/motherboard/";
683 static constexpr const char* cpuPresenceIntf =
684 "xyz.openbmc_project.Inventory.Item";
685 std::string cpuPresencePath = cpuPresencePathPrefix + cpuName;
686 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
687 try
Jason M. Bills64796042018-10-03 16:51:55 -0700688 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700689 auto service =
690 ipmi::getService(*busp, cpuPresenceIntf, cpuPresencePath);
691
692 ipmi::Value result = ipmi::getDbusProperty(
693 *busp, service, cpuPresencePath, cpuPresenceIntf, "Present");
694 return std::get<bool>(result);
695 }
696 catch (const std::exception& e)
697 {
698 phosphor::logging::log<phosphor::logging::level::INFO>(
699 "Cannot find processor presence",
700 phosphor::logging::entry("NAME=%s", cpuName.c_str()));
701 return false;
702 }
703}
704
705ipmi::RspType<bool, // CATERR Reset Enabled
706 bool, // ERR2 Reset Enabled
707 uint6_t, // reserved
708 uint8_t, // reserved, returns 0x3F
709 uint6_t, // CPU1 CATERR Count
710 uint2_t, // CPU1 Status
711 uint6_t, // CPU2 CATERR Count
712 uint2_t, // CPU2 Status
713 uint6_t, // CPU3 CATERR Count
714 uint2_t, // CPU3 Status
715 uint6_t, // CPU4 CATERR Count
716 uint2_t, // CPU4 Status
717 uint8_t // Crashdump Count
718 >
719 ipmiOEMGetProcessorErrConfig()
720{
721 bool resetOnCATERR = false;
722 bool resetOnERR2 = false;
723 uint6_t cpu1CATERRCount = 0;
724 uint6_t cpu2CATERRCount = 0;
725 uint6_t cpu3CATERRCount = 0;
726 uint6_t cpu4CATERRCount = 0;
727 uint8_t crashdumpCount = 0;
728 uint2_t cpu1Status =
729 cpuPresent("CPU_1") ? CPUStatus::enabled : CPUStatus::notPresent;
730 uint2_t cpu2Status =
731 cpuPresent("CPU_2") ? CPUStatus::enabled : CPUStatus::notPresent;
732 uint2_t cpu3Status =
733 cpuPresent("CPU_3") ? CPUStatus::enabled : CPUStatus::notPresent;
734 uint2_t cpu4Status =
735 cpuPresent("CPU_4") ? CPUStatus::enabled : CPUStatus::notPresent;
736
737 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
738 try
739 {
740 auto service = ipmi::getService(*busp, processorErrConfigIntf,
741 processorErrConfigObjPath);
742
743 ipmi::PropertyMap result = ipmi::getAllDbusProperties(
744 *busp, service, processorErrConfigObjPath, processorErrConfigIntf);
745 resetOnCATERR = std::get<bool>(result.at("ResetOnCATERR"));
746 resetOnERR2 = std::get<bool>(result.at("ResetOnERR2"));
747 cpu1CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU1"));
748 cpu2CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU2"));
749 cpu3CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU3"));
750 cpu4CATERRCount = std::get<uint8_t>(result.at("ErrorCountCPU4"));
751 crashdumpCount = std::get<uint8_t>(result.at("CrashdumpCount"));
752 }
753 catch (const std::exception& e)
754 {
755 phosphor::logging::log<phosphor::logging::level::ERR>(
756 "Failed to fetch processor error config",
757 phosphor::logging::entry("ERROR=%s", e.what()));
758 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700759 }
760
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700761 return ipmi::responseSuccess(resetOnCATERR, resetOnERR2, 0, 0x3F,
762 cpu1CATERRCount, cpu1Status, cpu2CATERRCount,
763 cpu2Status, cpu3CATERRCount, cpu3Status,
764 cpu4CATERRCount, cpu4Status, crashdumpCount);
765}
Jason M. Bills64796042018-10-03 16:51:55 -0700766
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700767ipmi::RspType<> ipmiOEMSetProcessorErrConfig(
768 bool resetOnCATERR, bool resetOnERR2, uint6_t reserved1, uint8_t reserved2,
769 std::optional<bool> clearCPUErrorCount,
770 std::optional<bool> clearCrashdumpCount, std::optional<uint6_t> reserved3)
771{
772 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
Jason M. Bills64796042018-10-03 16:51:55 -0700773
774 try
775 {
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700776 auto service = ipmi::getService(*busp, processorErrConfigIntf,
777 processorErrConfigObjPath);
778 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
779 processorErrConfigIntf, "ResetOnCATERR",
780 resetOnCATERR);
781 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
782 processorErrConfigIntf, "ResetOnERR2",
783 resetOnERR2);
784 if (clearCPUErrorCount.value_or(false))
785 {
786 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700787 processorErrConfigIntf, "ErrorCountCPU1",
788 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700789 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700790 processorErrConfigIntf, "ErrorCountCPU2",
791 static_cast<uint8_t>(0));
792 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
793 processorErrConfigIntf, "ErrorCountCPU3",
794 static_cast<uint8_t>(0));
795 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
796 processorErrConfigIntf, "ErrorCountCPU4",
797 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700798 }
799 if (clearCrashdumpCount.value_or(false))
800 {
801 ipmi::setDbusProperty(*busp, service, processorErrConfigObjPath,
Jason M. Billsd3e19932019-08-15 12:39:03 -0700802 processorErrConfigIntf, "CrashdumpCount",
803 static_cast<uint8_t>(0));
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700804 }
Jason M. Bills64796042018-10-03 16:51:55 -0700805 }
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700806 catch (std::exception& e)
Jason M. Bills64796042018-10-03 16:51:55 -0700807 {
Kuiying Wangbc546672018-11-23 15:41:05 +0800808 phosphor::logging::log<phosphor::logging::level::ERR>(
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700809 "Failed to set processor error config",
810 phosphor::logging::entry("EXCEPTION=%s", e.what()));
811 return ipmi::responseUnspecifiedError();
Jason M. Bills64796042018-10-03 16:51:55 -0700812 }
813
Jason M. Bills42bd9c82019-06-28 16:39:34 -0700814 return ipmi::responseSuccess();
Jason M. Bills64796042018-10-03 16:51:55 -0700815}
816
Yong Li703922d2018-11-06 13:25:31 +0800817ipmi_ret_t ipmiOEMGetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
818 ipmi_request_t request,
819 ipmi_response_t response,
820 ipmi_data_len_t dataLen,
821 ipmi_context_t context)
822{
823 GetOEMShutdownPolicyRes* resp =
824 reinterpret_cast<GetOEMShutdownPolicyRes*>(response);
825
826 if (*dataLen != 0)
827 {
828 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wang45f04982018-12-26 09:23:08 +0800829 "oem_get_shutdown_policy: invalid input len!");
Yong Li703922d2018-11-06 13:25:31 +0800830 *dataLen = 0;
831 return IPMI_CC_REQ_DATA_LEN_INVALID;
832 }
833
834 *dataLen = 0;
835
836 try
837 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700838 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800839 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700840 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
841 Value variant = getDbusProperty(
842 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
843 oemShutdownPolicyObjPathProp);
Yong Li0669d192019-05-06 14:01:46 +0800844
845 if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
846 convertPolicyFromString(std::get<std::string>(variant)) ==
847 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
848 NoShutdownOnOCOT)
849 {
850 resp->policy = 0;
851 }
852 else if (sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
853 convertPolicyFromString(std::get<std::string>(variant)) ==
854 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
855 Policy::ShutdownOnOCOT)
856 {
857 resp->policy = 1;
858 }
859 else
860 {
861 phosphor::logging::log<phosphor::logging::level::ERR>(
862 "oem_set_shutdown_policy: invalid property!",
863 phosphor::logging::entry(
864 "PROP=%s", std::get<std::string>(variant).c_str()));
865 return IPMI_CC_UNSPECIFIED_ERROR;
866 }
Yong Li703922d2018-11-06 13:25:31 +0800867 // TODO needs to check if it is multi-node products,
868 // policy is only supported on node 3/4
869 resp->policySupport = shutdownPolicySupported;
870 }
871 catch (sdbusplus::exception_t& e)
872 {
873 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
874 return IPMI_CC_UNSPECIFIED_ERROR;
875 }
876
877 *dataLen = sizeof(GetOEMShutdownPolicyRes);
878 return IPMI_CC_OK;
879}
880
881ipmi_ret_t ipmiOEMSetShutdownPolicy(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
882 ipmi_request_t request,
883 ipmi_response_t response,
884 ipmi_data_len_t dataLen,
885 ipmi_context_t context)
886{
887 uint8_t* req = reinterpret_cast<uint8_t*>(request);
Yong Li0669d192019-05-06 14:01:46 +0800888 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy policy =
889 sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::Policy::
890 NoShutdownOnOCOT;
Yong Li703922d2018-11-06 13:25:31 +0800891
892 // TODO needs to check if it is multi-node products,
893 // policy is only supported on node 3/4
894 if (*dataLen != 1)
895 {
896 phosphor::logging::log<phosphor::logging::level::ERR>(
897 "oem_set_shutdown_policy: invalid input len!");
898 *dataLen = 0;
899 return IPMI_CC_REQ_DATA_LEN_INVALID;
900 }
901
902 *dataLen = 0;
903 if ((*req != noShutdownOnOCOT) && (*req != shutdownOnOCOT))
904 {
905 phosphor::logging::log<phosphor::logging::level::ERR>(
906 "oem_set_shutdown_policy: invalid input!");
907 return IPMI_CC_INVALID_FIELD_REQUEST;
908 }
909
Yong Li0669d192019-05-06 14:01:46 +0800910 if (*req == noShutdownOnOCOT)
911 {
912 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
913 Policy::NoShutdownOnOCOT;
914 }
915 else
916 {
917 policy = sdbusplus::com::intel::Control::server::OCOTShutdownPolicy::
918 Policy::ShutdownOnOCOT;
919 }
920
Yong Li703922d2018-11-06 13:25:31 +0800921 try
922 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700923 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Yong Li703922d2018-11-06 13:25:31 +0800924 std::string service =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700925 getService(*dbus, oemShutdownPolicyIntf, oemShutdownPolicyObjPath);
Yong Li0669d192019-05-06 14:01:46 +0800926 setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -0700927 *dbus, service, oemShutdownPolicyObjPath, oemShutdownPolicyIntf,
Yong Li0669d192019-05-06 14:01:46 +0800928 oemShutdownPolicyObjPathProp,
929 sdbusplus::com::intel::Control::server::convertForMessage(policy));
Yong Li703922d2018-11-06 13:25:31 +0800930 }
931 catch (sdbusplus::exception_t& e)
932 {
933 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
934 return IPMI_CC_UNSPECIFIED_ERROR;
935 }
936
937 return IPMI_CC_OK;
938}
939
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530940/** @brief implementation for check the DHCP or not in IPv4
941 * @param[in] Channel - Channel number
942 * @returns true or false.
943 */
944static bool isDHCPEnabled(uint8_t Channel)
945{
946 try
947 {
948 auto ethdevice = getChannelName(Channel);
949 if (ethdevice.empty())
950 {
951 return false;
952 }
953 auto ethIP = ethdevice + "/ipv4";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700954 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530955 auto ethernetObj =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700956 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
957 auto value = getDbusProperty(*dbus, networkService, ethernetObj.first,
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530958 networkIPIntf, "Origin");
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700959 if (std::get<std::string>(value) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530960 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
961 {
962 return true;
963 }
964 else
965 {
966 return false;
967 }
968 }
969 catch (sdbusplus::exception_t& e)
970 {
971 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
972 return true;
973 }
974}
975
976/** @brief implementes for check the DHCP or not in IPv6
977 * @param[in] Channel - Channel number
978 * @returns true or false.
979 */
980static bool isDHCPIPv6Enabled(uint8_t Channel)
981{
982
983 try
984 {
985 auto ethdevice = getChannelName(Channel);
986 if (ethdevice.empty())
987 {
988 return false;
989 }
990 auto ethIP = ethdevice + "/ipv6";
Vernon Mauery15419dd2019-05-24 09:40:30 -0700991 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530992 auto objectInfo =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700993 getDbusObject(*dbus, networkIPIntf, networkRoot, ethIP);
994 auto properties = getAllDbusProperties(*dbus, objectInfo.second,
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530995 objectInfo.first, networkIPIntf);
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700996 if (std::get<std::string>(properties["Origin"]) ==
Suryakanth Sekard509eb92018-11-15 17:44:11 +0530997 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
998 {
999 return true;
1000 }
1001 else
1002 {
1003 return false;
1004 }
1005 }
1006 catch (sdbusplus::exception_t& e)
1007 {
1008 phosphor::logging::log<phosphor::logging::level::ERR>(e.description());
1009 return true;
1010 }
1011}
1012
1013/** @brief implementes the creating of default new user
1014 * @param[in] userName - new username in 16 bytes.
1015 * @param[in] userPassword - new password in 20 bytes
1016 * @returns ipmi completion code.
1017 */
1018ipmi::RspType<> ipmiOEMSetUser2Activation(
1019 std::array<uint8_t, ipmi::ipmiMaxUserName>& userName,
1020 std::array<uint8_t, ipmi::maxIpmi20PasswordSize>& userPassword)
1021{
1022 bool userState = false;
1023 // Check for System Interface not exist and LAN should be static
1024 for (uint8_t channel = 0; channel < maxIpmiChannels; channel++)
1025 {
1026 ChannelInfo chInfo;
1027 try
1028 {
1029 getChannelInfo(channel, chInfo);
1030 }
1031 catch (sdbusplus::exception_t& e)
1032 {
1033 phosphor::logging::log<phosphor::logging::level::ERR>(
1034 "ipmiOEMSetUser2Activation: Failed to get Channel Info",
1035 phosphor::logging::entry("MSG: %s", e.description()));
1036 return ipmi::response(ipmi::ccUnspecifiedError);
1037 }
1038 if (chInfo.mediumType ==
1039 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1040 {
1041 phosphor::logging::log<phosphor::logging::level::ERR>(
1042 "ipmiOEMSetUser2Activation: system interface exist .");
1043 return ipmi::response(ipmi::ccCommandNotAvailable);
1044 }
1045 else
1046 {
1047
1048 if (chInfo.mediumType ==
1049 static_cast<uint8_t>(EChannelMediumType::lan8032))
1050 {
1051 if (isDHCPIPv6Enabled(channel) || isDHCPEnabled(channel))
1052 {
1053 phosphor::logging::log<phosphor::logging::level::ERR>(
1054 "ipmiOEMSetUser2Activation: DHCP enabled .");
1055 return ipmi::response(ipmi::ccCommandNotAvailable);
1056 }
1057 }
1058 }
1059 }
1060 uint8_t maxChUsers = 0, enabledUsers = 0, fixedUsers = 0;
1061 if (ipmi::ccSuccess ==
1062 ipmiUserGetAllCounts(maxChUsers, enabledUsers, fixedUsers))
1063 {
1064 if (enabledUsers > 1)
1065 {
1066 phosphor::logging::log<phosphor::logging::level::ERR>(
1067 "ipmiOEMSetUser2Activation: more than one user is enabled.");
1068 return ipmi::response(ipmi::ccCommandNotAvailable);
1069 }
1070 // Check the user 2 is enabled or not
1071 ipmiUserCheckEnabled(ipmiDefaultUserId, userState);
1072 if (userState == true)
1073 {
1074 phosphor::logging::log<phosphor::logging::level::ERR>(
1075 "ipmiOEMSetUser2Activation: user 2 already enabled .");
1076 return ipmi::response(ipmi::ccCommandNotAvailable);
1077 }
1078 }
1079 else
1080 {
1081 return ipmi::response(ipmi::ccUnspecifiedError);
1082 }
1083
1084#if BYTE_ORDER == LITTLE_ENDIAN
1085 PrivAccess privAccess = {PRIVILEGE_ADMIN, true, true, true, 0};
1086#endif
1087#if BYTE_ORDER == BIG_ENDIAN
1088 PrivAccess privAccess = {0, true, true, true, PRIVILEGE_ADMIN};
1089#endif
1090
1091 if (ipmi::ccSuccess ==
1092 ipmiUserSetUserName(ipmiDefaultUserId,
1093 reinterpret_cast<const char*>(userName.data())))
1094 {
1095 if (ipmi::ccSuccess ==
1096 ipmiUserSetUserPassword(
1097 ipmiDefaultUserId,
1098 reinterpret_cast<const char*>(userPassword.data())))
1099 {
1100 if (ipmi::ccSuccess ==
1101 ipmiUserSetPrivilegeAccess(
1102 ipmiDefaultUserId,
1103 static_cast<uint8_t>(ipmi::EChannelID::chanLan1),
1104 privAccess, true))
1105 {
1106 phosphor::logging::log<phosphor::logging::level::INFO>(
1107 "ipmiOEMSetUser2Activation: user created successfully ");
1108 return ipmi::responseSuccess();
1109 }
1110 }
1111 // we need to delete the default user id which added in this command as
1112 // password / priv setting is failed.
1113 ipmiUserSetUserName(ipmiDefaultUserId, "");
1114 phosphor::logging::log<phosphor::logging::level::ERR>(
1115 "ipmiOEMSetUser2Activation: password / priv setting is failed.");
1116 }
1117 else
1118 {
1119 phosphor::logging::log<phosphor::logging::level::ERR>(
1120 "ipmiOEMSetUser2Activation: Setting username failed.");
1121 }
1122
1123 return ipmi::response(ipmi::ccCommandNotAvailable);
1124}
1125
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301126/** @brief implementes executing the linux command
1127 * @param[in] linux command
1128 * @returns status
1129 */
1130
1131static uint8_t executeCmd(const char* path)
1132{
1133 boost::process::child execProg(path);
1134 execProg.wait();
1135
1136 int retCode = execProg.exit_code();
1137 if (retCode)
1138 {
1139 return ipmi::ccUnspecifiedError;
1140 }
1141 return ipmi::ccSuccess;
1142}
1143
1144/** @brief implementes ASD Security event logging
1145 * @param[in] Event message string
1146 * @param[in] Event Severity
1147 * @returns status
1148 */
1149
1150static void atScaleDebugEventlog(std::string msg, int severity)
1151{
1152 std::string eventStr = "OpenBMC.0.1." + msg;
1153 sd_journal_send("MESSAGE=Security Event: %s", eventStr.c_str(),
1154 "PRIORITY=%i", severity, "REDFISH_MESSAGE_ID=%s",
1155 eventStr.c_str(), NULL);
1156}
1157
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301158/** @brief implementes setting password for special user
1159 * @param[in] specialUserIndex
1160 * @param[in] userPassword - new password in 20 bytes
1161 * @returns ipmi completion code.
1162 */
1163ipmi::RspType<> ipmiOEMSetSpecialUserPassword(ipmi::Context::ptr ctx,
1164 uint8_t specialUserIndex,
1165 std::vector<uint8_t> userPassword)
1166{
1167 ChannelInfo chInfo;
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301168 ipmi_ret_t status = ipmi::ccSuccess;
1169
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301170 try
1171 {
1172 getChannelInfo(ctx->channel, chInfo);
1173 }
1174 catch (sdbusplus::exception_t& e)
1175 {
1176 phosphor::logging::log<phosphor::logging::level::ERR>(
1177 "ipmiOEMSetSpecialUserPassword: Failed to get Channel Info",
1178 phosphor::logging::entry("MSG: %s", e.description()));
1179 return ipmi::responseUnspecifiedError();
1180 }
1181 if (chInfo.mediumType !=
1182 static_cast<uint8_t>(EChannelMediumType::systemInterface))
1183 {
1184 phosphor::logging::log<phosphor::logging::level::ERR>(
1185 "ipmiOEMSetSpecialUserPassword: Error - supported only in KCS "
1186 "interface");
1187 return ipmi::responseCommandNotAvailable();
1188 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301189
1190 // 0 for root user and 1 for AtScaleDebug is allowed
1191 if (specialUserIndex >
1192 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301193 {
1194 phosphor::logging::log<phosphor::logging::level::ERR>(
1195 "ipmiOEMSetSpecialUserPassword: Invalid user account");
1196 return ipmi::responseParmOutOfRange();
1197 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301198 if (userPassword.size() != 0)
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301199 {
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301200 constexpr uint8_t minPasswordSizeRequired = 6;
1201 std::string passwd;
1202 if (userPassword.size() < minPasswordSizeRequired ||
1203 userPassword.size() > ipmi::maxIpmi20PasswordSize)
1204 {
1205 return ipmi::responseReqDataLenInvalid();
1206 }
1207 passwd.assign(reinterpret_cast<const char*>(userPassword.data()),
1208 userPassword.size());
1209 if (specialUserIndex ==
1210 static_cast<uint8_t>(SpecialUserIndex::atScaleDebugUser))
1211 {
1212 status = ipmiSetSpecialUserPassword("asdbg", passwd);
1213
1214 atScaleDebugEventlog("AtScaleDebugSpecialUserEnabled", LOG_CRIT);
1215 }
1216 else
1217 {
1218 status = ipmiSetSpecialUserPassword("root", passwd);
1219 }
1220 return ipmi::response(status);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301221 }
Suryakanth Sekar822b0b42019-11-15 18:32:53 +05301222 else
1223 {
1224 if (specialUserIndex ==
1225 static_cast<uint8_t>(SpecialUserIndex::rootUser))
1226 {
1227 status = executeCmd("passwd -d root");
1228 }
1229 else
1230 {
1231
1232 status = executeCmd("passwd -d asdbg");
1233
1234 if (status == 0)
1235 {
1236 atScaleDebugEventlog("AtScaleDebugSpecialUserDisabled",
1237 LOG_INFO);
1238 }
1239 }
1240 return ipmi::response(status);
1241 }
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05301242}
1243
Kuiying Wang45f04982018-12-26 09:23:08 +08001244namespace ledAction
1245{
1246using namespace sdbusplus::xyz::openbmc_project::Led::server;
1247std::map<Physical::Action, uint8_t> actionDbusToIpmi = {
jayaprakash Mutyala934ee9c2019-12-13 17:49:27 +00001248 {Physical::Action::Off, 0},
1249 {Physical::Action::On, 2},
1250 {Physical::Action::Blink, 1}};
Kuiying Wang45f04982018-12-26 09:23:08 +08001251
1252std::map<uint8_t, std::string> offsetObjPath = {
1253 {2, statusAmberObjPath}, {4, statusGreenObjPath}, {6, identifyLEDObjPath}};
1254
1255} // namespace ledAction
1256
1257int8_t getLEDState(sdbusplus::bus::bus& bus, const std::string& intf,
1258 const std::string& objPath, uint8_t& state)
1259{
1260 try
1261 {
1262 std::string service = getService(bus, intf, objPath);
1263 Value stateValue =
1264 getDbusProperty(bus, service, objPath, intf, "State");
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001265 std::string strState = std::get<std::string>(stateValue);
Kuiying Wang45f04982018-12-26 09:23:08 +08001266 state = ledAction::actionDbusToIpmi.at(
1267 sdbusplus::xyz::openbmc_project::Led::server::Physical::
1268 convertActionFromString(strState));
1269 }
1270 catch (sdbusplus::exception::SdBusError& e)
1271 {
1272 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1273 return -1;
1274 }
1275 return 0;
1276}
1277
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001278ipmi::RspType<uint8_t> ipmiOEMGetLEDStatus()
Kuiying Wang45f04982018-12-26 09:23:08 +08001279{
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001280 uint8_t ledstate = 0;
Kuiying Wang45f04982018-12-26 09:23:08 +08001281 phosphor::logging::log<phosphor::logging::level::DEBUG>("GET led status");
Vernon Mauery15419dd2019-05-24 09:40:30 -07001282 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Kuiying Wang45f04982018-12-26 09:23:08 +08001283 for (auto it = ledAction::offsetObjPath.begin();
1284 it != ledAction::offsetObjPath.end(); ++it)
1285 {
1286 uint8_t state = 0;
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001287 if (getLEDState(*dbus, ledIntf, it->second, state) == -1)
Kuiying Wang45f04982018-12-26 09:23:08 +08001288 {
1289 phosphor::logging::log<phosphor::logging::level::ERR>(
1290 "oem_get_led_status: fail to get ID LED status!");
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001291 return ipmi::responseUnspecifiedError();
Kuiying Wang45f04982018-12-26 09:23:08 +08001292 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001293 ledstate |= state << it->first;
Kuiying Wang45f04982018-12-26 09:23:08 +08001294 }
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00001295 return ipmi::responseSuccess(ledstate);
Kuiying Wang45f04982018-12-26 09:23:08 +08001296}
1297
Yong Li23737fe2019-02-19 08:49:55 +08001298ipmi_ret_t ipmiOEMCfgHostSerialPortSpeed(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1299 ipmi_request_t request,
1300 ipmi_response_t response,
1301 ipmi_data_len_t dataLen,
1302 ipmi_context_t context)
1303{
1304 CfgHostSerialReq* req = reinterpret_cast<CfgHostSerialReq*>(request);
1305 uint8_t* resp = reinterpret_cast<uint8_t*>(response);
1306
1307 if (*dataLen == 0)
1308 {
1309 phosphor::logging::log<phosphor::logging::level::ERR>(
1310 "CfgHostSerial: invalid input len!",
1311 phosphor::logging::entry("LEN=%d", *dataLen));
1312 return IPMI_CC_REQ_DATA_LEN_INVALID;
1313 }
1314
1315 switch (req->command)
1316 {
1317 case getHostSerialCfgCmd:
1318 {
1319 if (*dataLen != 1)
1320 {
1321 phosphor::logging::log<phosphor::logging::level::ERR>(
1322 "CfgHostSerial: invalid input len!");
1323 *dataLen = 0;
1324 return IPMI_CC_REQ_DATA_LEN_INVALID;
1325 }
1326
1327 *dataLen = 0;
1328
1329 boost::process::ipstream is;
1330 std::vector<std::string> data;
1331 std::string line;
1332 boost::process::child c1(fwGetEnvCmd, "-n", fwHostSerailCfgEnvName,
1333 boost::process::std_out > is);
1334
1335 while (c1.running() && std::getline(is, line) && !line.empty())
1336 {
1337 data.push_back(line);
1338 }
1339
1340 c1.wait();
1341 if (c1.exit_code())
1342 {
1343 phosphor::logging::log<phosphor::logging::level::ERR>(
1344 "CfgHostSerial:: error on execute",
1345 phosphor::logging::entry("EXECUTE=%s", fwSetEnvCmd));
1346 // Using the default value
1347 *resp = 0;
1348 }
1349 else
1350 {
1351 if (data.size() != 1)
1352 {
1353 phosphor::logging::log<phosphor::logging::level::ERR>(
1354 "CfgHostSerial:: error on read env");
1355 return IPMI_CC_UNSPECIFIED_ERROR;
1356 }
1357 try
1358 {
1359 unsigned long tmp = std::stoul(data[0]);
1360 if (tmp > std::numeric_limits<uint8_t>::max())
1361 {
1362 throw std::out_of_range("Out of range");
1363 }
1364 *resp = static_cast<uint8_t>(tmp);
1365 }
1366 catch (const std::invalid_argument& e)
1367 {
1368 phosphor::logging::log<phosphor::logging::level::ERR>(
1369 "invalid config ",
1370 phosphor::logging::entry("ERR=%s", e.what()));
1371 return IPMI_CC_UNSPECIFIED_ERROR;
1372 }
1373 catch (const std::out_of_range& e)
1374 {
1375 phosphor::logging::log<phosphor::logging::level::ERR>(
1376 "out_of_range config ",
1377 phosphor::logging::entry("ERR=%s", e.what()));
1378 return IPMI_CC_UNSPECIFIED_ERROR;
1379 }
1380 }
1381
1382 *dataLen = 1;
1383 break;
1384 }
1385 case setHostSerialCfgCmd:
1386 {
1387 if (*dataLen != sizeof(CfgHostSerialReq))
1388 {
1389 phosphor::logging::log<phosphor::logging::level::ERR>(
1390 "CfgHostSerial: invalid input len!");
1391 *dataLen = 0;
1392 return IPMI_CC_REQ_DATA_LEN_INVALID;
1393 }
1394
1395 *dataLen = 0;
1396
1397 if (req->parameter > HostSerialCfgParamMax)
1398 {
1399 phosphor::logging::log<phosphor::logging::level::ERR>(
1400 "CfgHostSerial: invalid input!");
1401 return IPMI_CC_INVALID_FIELD_REQUEST;
1402 }
1403
1404 boost::process::child c1(fwSetEnvCmd, fwHostSerailCfgEnvName,
1405 std::to_string(req->parameter));
1406
1407 c1.wait();
1408 if (c1.exit_code())
1409 {
1410 phosphor::logging::log<phosphor::logging::level::ERR>(
1411 "CfgHostSerial:: error on execute",
1412 phosphor::logging::entry("EXECUTE=%s", fwGetEnvCmd));
1413 return IPMI_CC_UNSPECIFIED_ERROR;
1414 }
1415 break;
1416 }
1417 default:
1418 phosphor::logging::log<phosphor::logging::level::ERR>(
1419 "CfgHostSerial: invalid input!");
1420 *dataLen = 0;
1421 return IPMI_CC_INVALID_FIELD_REQUEST;
1422 }
1423
1424 return IPMI_CC_OK;
1425}
1426
James Feist91244a62019-02-19 15:04:54 -08001427constexpr const char* thermalModeInterface =
1428 "xyz.openbmc_project.Control.ThermalMode";
1429constexpr const char* thermalModePath =
1430 "/xyz/openbmc_project/control/thermal_mode";
1431
1432bool getFanProfileInterface(
1433 sdbusplus::bus::bus& bus,
1434 boost::container::flat_map<
1435 std::string, std::variant<std::vector<std::string>, std::string>>& resp)
1436{
1437 auto call = bus.new_method_call(settingsBusName, thermalModePath, PROP_INTF,
1438 "GetAll");
1439 call.append(thermalModeInterface);
1440 try
1441 {
1442 auto data = bus.call(call);
1443 data.read(resp);
1444 }
1445 catch (sdbusplus::exception_t& e)
1446 {
1447 phosphor::logging::log<phosphor::logging::level::ERR>(
1448 "getFanProfileInterface: can't get thermal mode!",
1449 phosphor::logging::entry("ERR=%s", e.what()));
1450 return false;
1451 }
1452 return true;
1453}
1454
anil kumar appanaf945eee2019-09-25 23:29:11 +00001455/**@brief implements the OEM set fan config.
1456 * @param selectedFanProfile - fan profile to enable
1457 * @param reserved1
1458 * @param performanceMode - Performance/Acoustic mode
1459 * @param reserved2
1460 * @param setPerformanceMode - set Performance/Acoustic mode
1461 * @param setFanProfile - set fan profile
1462 *
1463 * @return IPMI completion code.
1464 **/
1465ipmi::RspType<> ipmiOEMSetFanConfig(uint8_t selectedFanProfile,
1466
1467 uint2_t reserved1, bool performanceMode,
1468 uint3_t reserved2, bool setPerformanceMode,
Joshi-Mansi619186d2020-01-27 19:16:03 +05301469 bool setFanProfile,
1470 std::optional<uint8_t> dimmGroupId,
1471 std::optional<uint32_t> dimmPresenceBitmap)
James Feist91244a62019-02-19 15:04:54 -08001472{
anil kumar appanaf945eee2019-09-25 23:29:11 +00001473 if (reserved1 || reserved2)
James Feist91244a62019-02-19 15:04:54 -08001474 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001475 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001476 }
Joshi-Mansi619186d2020-01-27 19:16:03 +05301477
1478 if (dimmGroupId)
1479 {
1480 if (*dimmGroupId >= maxCPUNum)
1481 {
1482 return ipmi::responseInvalidFieldRequest();
1483 }
1484 if (!cpuPresent("CPU_" + std::to_string(*dimmGroupId + 1)))
1485 {
1486 return ipmi::responseInvalidFieldRequest();
1487 }
1488 }
1489
James Feist91244a62019-02-19 15:04:54 -08001490 // todo: tell bios to only send first 2 bytes
James Feist91244a62019-02-19 15:04:54 -08001491 boost::container::flat_map<
1492 std::string, std::variant<std::vector<std::string>, std::string>>
1493 profileData;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001494 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1495 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001496 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001497 return ipmi::responseUnspecifiedError();
James Feist91244a62019-02-19 15:04:54 -08001498 }
1499
1500 std::vector<std::string>* supported =
1501 std::get_if<std::vector<std::string>>(&profileData["Supported"]);
1502 if (supported == nullptr)
1503 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001504 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001505 }
1506 std::string mode;
anil kumar appanaf945eee2019-09-25 23:29:11 +00001507 if (setPerformanceMode)
James Feist91244a62019-02-19 15:04:54 -08001508 {
James Feist91244a62019-02-19 15:04:54 -08001509 if (performanceMode)
1510 {
1511
1512 if (std::find(supported->begin(), supported->end(),
1513 "Performance") != supported->end())
1514 {
1515 mode = "Performance";
1516 }
1517 }
1518 else
1519 {
James Feist91244a62019-02-19 15:04:54 -08001520 if (std::find(supported->begin(), supported->end(), "Acoustic") !=
1521 supported->end())
1522 {
1523 mode = "Acoustic";
1524 }
1525 }
1526 if (mode.empty())
1527 {
anil kumar appanaf945eee2019-09-25 23:29:11 +00001528 return ipmi::responseInvalidFieldRequest();
James Feist91244a62019-02-19 15:04:54 -08001529 }
anil kumar appanaf945eee2019-09-25 23:29:11 +00001530
1531 try
1532 {
1533 setDbusProperty(*dbus, settingsBusName, thermalModePath,
1534 thermalModeInterface, "Current", mode);
1535 }
1536 catch (sdbusplus::exception_t& e)
1537 {
1538 phosphor::logging::log<phosphor::logging::level::ERR>(
1539 "ipmiOEMSetFanConfig: can't set thermal mode!",
1540 phosphor::logging::entry("EXCEPTION=%s", e.what()));
1541 return ipmi::responseResponseError();
1542 }
James Feist91244a62019-02-19 15:04:54 -08001543 }
1544
anil kumar appanaf945eee2019-09-25 23:29:11 +00001545 return ipmi::responseSuccess();
James Feist91244a62019-02-19 15:04:54 -08001546}
1547
James Feist5b693632019-07-09 09:06:09 -07001548ipmi::RspType<uint8_t, // profile support map
1549 uint8_t, // fan control profile enable
1550 uint8_t, // flags
1551 uint32_t // dimm presence bit map
1552 >
1553 ipmiOEMGetFanConfig(uint8_t dimmGroupId)
James Feist91244a62019-02-19 15:04:54 -08001554{
Joshi-Mansi36f05ce2020-01-14 14:29:34 +05301555 if (dimmGroupId >= maxCPUNum)
1556 {
1557 return ipmi::responseInvalidFieldRequest();
1558 }
1559
1560 bool cpuStatus = cpuPresent("CPU_" + std::to_string(dimmGroupId + 1));
1561
1562 if (!cpuStatus)
1563 {
1564 return ipmi::responseInvalidFieldRequest();
1565 }
1566
James Feist91244a62019-02-19 15:04:54 -08001567 boost::container::flat_map<
1568 std::string, std::variant<std::vector<std::string>, std::string>>
1569 profileData;
1570
Vernon Mauery15419dd2019-05-24 09:40:30 -07001571 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1572 if (!getFanProfileInterface(*dbus, profileData))
James Feist91244a62019-02-19 15:04:54 -08001573 {
James Feist5b693632019-07-09 09:06:09 -07001574 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001575 }
1576
1577 std::string* current = std::get_if<std::string>(&profileData["Current"]);
1578
1579 if (current == nullptr)
1580 {
1581 phosphor::logging::log<phosphor::logging::level::ERR>(
1582 "ipmiOEMGetFanConfig: can't get current mode!");
James Feist5b693632019-07-09 09:06:09 -07001583 return ipmi::responseResponseError();
James Feist91244a62019-02-19 15:04:54 -08001584 }
1585 bool performance = (*current == "Performance");
1586
James Feist5b693632019-07-09 09:06:09 -07001587 uint8_t flags = 0;
James Feist91244a62019-02-19 15:04:54 -08001588 if (performance)
1589 {
James Feist5b693632019-07-09 09:06:09 -07001590 flags |= 1 << 2;
James Feist91244a62019-02-19 15:04:54 -08001591 }
1592
jayaprakash Mutyala4b1552d2020-02-11 12:07:29 +00001593 constexpr uint8_t fanControlDefaultProfile = 0x80;
1594 constexpr uint8_t fanControlProfileState = 0x00;
1595 constexpr uint32_t dimmPresenceBitmap = 0x00;
1596
1597 return ipmi::responseSuccess(fanControlDefaultProfile,
1598 fanControlProfileState, flags,
1599 dimmPresenceBitmap);
James Feist91244a62019-02-19 15:04:54 -08001600}
James Feist5f957ca2019-03-14 15:33:55 -07001601constexpr const char* cfmLimitSettingPath =
1602 "/xyz/openbmc_project/control/cfm_limit";
1603constexpr const char* cfmLimitIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistfaa4f222019-03-21 16:21:55 -07001604constexpr const size_t legacyExitAirSensorNumber = 0x2e;
James Feist09f6b602019-08-08 11:30:03 -07001605constexpr const size_t legacyPCHSensorNumber = 0x22;
1606constexpr const char* exitAirPathName = "Exit_Air";
1607constexpr const char* pchPathName = "SSB_Temp";
James Feistacc8a4e2019-04-02 14:23:57 -07001608constexpr const char* pidConfigurationIface =
1609 "xyz.openbmc_project.Configuration.Pid";
James Feistfaa4f222019-03-21 16:21:55 -07001610
James Feist09f6b602019-08-08 11:30:03 -07001611static std::string getConfigPath(const std::string& name)
James Feistfaa4f222019-03-21 16:21:55 -07001612{
Vernon Mauery15419dd2019-05-24 09:40:30 -07001613 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistfaa4f222019-03-21 16:21:55 -07001614 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001615 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1616 "/xyz/openbmc_project/object_mapper",
1617 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistfaa4f222019-03-21 16:21:55 -07001618
James Feistacc8a4e2019-04-02 14:23:57 -07001619 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
James Feistfaa4f222019-03-21 16:21:55 -07001620 std::string path;
1621 GetSubTreeType resp;
1622 try
1623 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001624 auto reply = dbus->call(method);
James Feistfaa4f222019-03-21 16:21:55 -07001625 reply.read(resp);
1626 }
1627 catch (sdbusplus::exception_t&)
1628 {
1629 phosphor::logging::log<phosphor::logging::level::ERR>(
1630 "ipmiOEMGetFscParameter: mapper error");
1631 };
James Feist09f6b602019-08-08 11:30:03 -07001632 auto config =
1633 std::find_if(resp.begin(), resp.end(), [&name](const auto& pair) {
1634 return pair.first.find(name) != std::string::npos;
1635 });
James Feistfaa4f222019-03-21 16:21:55 -07001636 if (config != resp.end())
1637 {
1638 path = std::move(config->first);
1639 }
1640 return path;
1641}
James Feist5f957ca2019-03-14 15:33:55 -07001642
James Feistacc8a4e2019-04-02 14:23:57 -07001643// flat map to make alphabetical
1644static boost::container::flat_map<std::string, PropertyMap> getPidConfigs()
1645{
1646 boost::container::flat_map<std::string, PropertyMap> ret;
Vernon Mauery15419dd2019-05-24 09:40:30 -07001647 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001648 auto method =
Vernon Mauery15419dd2019-05-24 09:40:30 -07001649 dbus->new_method_call("xyz.openbmc_project.ObjectMapper",
1650 "/xyz/openbmc_project/object_mapper",
1651 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feistacc8a4e2019-04-02 14:23:57 -07001652
1653 method.append("/", 0, std::array<const char*, 1>{pidConfigurationIface});
1654 GetSubTreeType resp;
1655
1656 try
1657 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001658 auto reply = dbus->call(method);
James Feistacc8a4e2019-04-02 14:23:57 -07001659 reply.read(resp);
1660 }
1661 catch (sdbusplus::exception_t&)
1662 {
1663 phosphor::logging::log<phosphor::logging::level::ERR>(
1664 "getFanConfigPaths: mapper error");
1665 };
1666 for (const auto& [path, objects] : resp)
1667 {
1668 if (objects.empty())
1669 {
1670 continue; // should be impossible
1671 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04001672
1673 try
1674 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001675 ret.emplace(path,
1676 getAllDbusProperties(*dbus, objects[0].first, path,
1677 pidConfigurationIface));
Zhu, Yungebe560b02019-04-21 21:19:21 -04001678 }
1679 catch (sdbusplus::exception_t& e)
1680 {
1681 phosphor::logging::log<phosphor::logging::level::ERR>(
1682 "getPidConfigs: can't get DbusProperties!",
1683 phosphor::logging::entry("ERR=%s", e.what()));
1684 }
James Feistacc8a4e2019-04-02 14:23:57 -07001685 }
1686 return ret;
1687}
1688
1689ipmi::RspType<uint8_t> ipmiOEMGetFanSpeedOffset(void)
1690{
1691 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1692 if (data.empty())
1693 {
1694 return ipmi::responseResponseError();
1695 }
1696 uint8_t minOffset = std::numeric_limits<uint8_t>::max();
1697 for (const auto& [_, pid] : data)
1698 {
1699 auto findClass = pid.find("Class");
1700 if (findClass == pid.end())
1701 {
1702 phosphor::logging::log<phosphor::logging::level::ERR>(
1703 "ipmiOEMGetFscParameter: found illegal pid "
1704 "configurations");
1705 return ipmi::responseResponseError();
1706 }
1707 std::string type = std::get<std::string>(findClass->second);
1708 if (type == "fan")
1709 {
1710 auto findOutLimit = pid.find("OutLimitMin");
1711 if (findOutLimit == pid.end())
1712 {
1713 phosphor::logging::log<phosphor::logging::level::ERR>(
1714 "ipmiOEMGetFscParameter: found illegal pid "
1715 "configurations");
1716 return ipmi::responseResponseError();
1717 }
1718 // get the min out of all the offsets
1719 minOffset = std::min(
1720 minOffset,
1721 static_cast<uint8_t>(std::get<double>(findOutLimit->second)));
1722 }
1723 }
1724 if (minOffset == std::numeric_limits<uint8_t>::max())
1725 {
1726 phosphor::logging::log<phosphor::logging::level::ERR>(
1727 "ipmiOEMGetFscParameter: found no fan configurations!");
1728 return ipmi::responseResponseError();
1729 }
1730
1731 return ipmi::responseSuccess(minOffset);
1732}
1733
1734ipmi::RspType<> ipmiOEMSetFanSpeedOffset(uint8_t offset)
1735{
1736 boost::container::flat_map<std::string, PropertyMap> data = getPidConfigs();
1737 if (data.empty())
1738 {
1739
1740 phosphor::logging::log<phosphor::logging::level::ERR>(
1741 "ipmiOEMSetFanSpeedOffset: found no pid configurations!");
1742 return ipmi::responseResponseError();
1743 }
1744
Vernon Mauery15419dd2019-05-24 09:40:30 -07001745 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001746 bool found = false;
1747 for (const auto& [path, pid] : data)
1748 {
1749 auto findClass = pid.find("Class");
1750 if (findClass == pid.end())
1751 {
1752
1753 phosphor::logging::log<phosphor::logging::level::ERR>(
1754 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1755 "configurations");
1756 return ipmi::responseResponseError();
1757 }
1758 std::string type = std::get<std::string>(findClass->second);
1759 if (type == "fan")
1760 {
1761 auto findOutLimit = pid.find("OutLimitMin");
1762 if (findOutLimit == pid.end())
1763 {
1764
1765 phosphor::logging::log<phosphor::logging::level::ERR>(
1766 "ipmiOEMSetFanSpeedOffset: found illegal pid "
1767 "configurations");
1768 return ipmi::responseResponseError();
1769 }
Vernon Mauery15419dd2019-05-24 09:40:30 -07001770 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager",
James Feistacc8a4e2019-04-02 14:23:57 -07001771 path, pidConfigurationIface, "OutLimitMin",
1772 static_cast<double>(offset));
1773 found = true;
1774 }
1775 }
1776 if (!found)
1777 {
1778 phosphor::logging::log<phosphor::logging::level::ERR>(
1779 "ipmiOEMSetFanSpeedOffset: set no fan offsets");
1780 return ipmi::responseResponseError();
1781 }
1782
1783 return ipmi::responseSuccess();
1784}
1785
1786ipmi::RspType<> ipmiOEMSetFscParameter(uint8_t command, uint8_t param1,
1787 uint8_t param2)
James Feist5f957ca2019-03-14 15:33:55 -07001788{
1789 constexpr const size_t disableLimiting = 0x0;
1790
Vernon Mauery15419dd2019-05-24 09:40:30 -07001791 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001792 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001793 {
James Feist09f6b602019-08-08 11:30:03 -07001794 std::string pathName;
James Feistacc8a4e2019-04-02 14:23:57 -07001795 if (param1 == legacyExitAirSensorNumber)
James Feistfaa4f222019-03-21 16:21:55 -07001796 {
James Feist09f6b602019-08-08 11:30:03 -07001797 pathName = exitAirPathName;
1798 }
1799 else if (param1 == legacyPCHSensorNumber)
1800 {
1801 pathName = pchPathName;
James Feistfaa4f222019-03-21 16:21:55 -07001802 }
1803 else
1804 {
James Feistacc8a4e2019-04-02 14:23:57 -07001805 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001806 }
James Feist09f6b602019-08-08 11:30:03 -07001807 std::string path = getConfigPath(pathName);
1808 ipmi::setDbusProperty(*dbus, "xyz.openbmc_project.EntityManager", path,
1809 pidConfigurationIface, "SetPoint",
1810 static_cast<double>(param2));
1811 return ipmi::responseSuccess();
James Feistfaa4f222019-03-21 16:21:55 -07001812 }
James Feistacc8a4e2019-04-02 14:23:57 -07001813 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001814 {
James Feistacc8a4e2019-04-02 14:23:57 -07001815 uint16_t cfm = param1 | (static_cast<uint16_t>(param2) << 8);
James Feist5f957ca2019-03-14 15:33:55 -07001816
1817 // must be greater than 50 based on eps
1818 if (cfm < 50 && cfm != disableLimiting)
1819 {
James Feistacc8a4e2019-04-02 14:23:57 -07001820 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001821 }
1822
1823 try
1824 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001825 ipmi::setDbusProperty(*dbus, settingsBusName, cfmLimitSettingPath,
James Feist5f957ca2019-03-14 15:33:55 -07001826 cfmLimitIface, "Limit",
1827 static_cast<double>(cfm));
1828 }
1829 catch (sdbusplus::exception_t& e)
1830 {
1831 phosphor::logging::log<phosphor::logging::level::ERR>(
1832 "ipmiOEMSetFscParameter: can't set cfm setting!",
1833 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07001834 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07001835 }
James Feistacc8a4e2019-04-02 14:23:57 -07001836 return ipmi::responseSuccess();
1837 }
1838 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1839 {
1840 constexpr const size_t maxDomainCount = 8;
1841 uint8_t requestedDomainMask = param1;
1842 boost::container::flat_map data = getPidConfigs();
1843 if (data.empty())
1844 {
1845
1846 phosphor::logging::log<phosphor::logging::level::ERR>(
1847 "ipmiOEMSetFscParameter: found no pid configurations!");
1848 return ipmi::responseResponseError();
1849 }
1850 size_t count = 0;
1851 for (const auto& [path, pid] : data)
1852 {
1853 auto findClass = pid.find("Class");
1854 if (findClass == pid.end())
1855 {
1856
1857 phosphor::logging::log<phosphor::logging::level::ERR>(
1858 "ipmiOEMSetFscParameter: found illegal pid "
1859 "configurations");
1860 return ipmi::responseResponseError();
1861 }
1862 std::string type = std::get<std::string>(findClass->second);
1863 if (type == "fan")
1864 {
1865 if (requestedDomainMask & (1 << count))
1866 {
1867 ipmi::setDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07001868 *dbus, "xyz.openbmc_project.EntityManager", path,
James Feistacc8a4e2019-04-02 14:23:57 -07001869 pidConfigurationIface, "OutLimitMax",
1870 static_cast<double>(param2));
1871 }
1872 count++;
1873 }
1874 }
1875 return ipmi::responseSuccess();
James Feist5f957ca2019-03-14 15:33:55 -07001876 }
1877 else
1878 {
1879 // todo other command parts possibly
1880 // tcontrol is handled in peci now
1881 // fan speed offset not implemented yet
1882 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07001883 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07001884 }
1885}
1886
James Feistacc8a4e2019-04-02 14:23:57 -07001887ipmi::RspType<
1888 std::variant<uint8_t, std::array<uint8_t, 2>, std::array<uint16_t, 2>>>
1889 ipmiOEMGetFscParameter(uint8_t command, std::optional<uint8_t> param)
James Feist5f957ca2019-03-14 15:33:55 -07001890{
James Feist09f6b602019-08-08 11:30:03 -07001891 constexpr uint8_t legacyDefaultSetpoint = -128;
James Feist5f957ca2019-03-14 15:33:55 -07001892
Vernon Mauery15419dd2019-05-24 09:40:30 -07001893 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
James Feistacc8a4e2019-04-02 14:23:57 -07001894 if (command == static_cast<uint8_t>(setFscParamFlags::tcontrol))
James Feist5f957ca2019-03-14 15:33:55 -07001895 {
James Feistacc8a4e2019-04-02 14:23:57 -07001896 if (!param)
James Feistfaa4f222019-03-21 16:21:55 -07001897 {
James Feistacc8a4e2019-04-02 14:23:57 -07001898 return ipmi::responseReqDataLenInvalid();
James Feistfaa4f222019-03-21 16:21:55 -07001899 }
1900
James Feist09f6b602019-08-08 11:30:03 -07001901 std::string pathName;
1902
1903 if (*param == legacyExitAirSensorNumber)
1904 {
1905 pathName = exitAirPathName;
1906 }
1907 else if (*param == legacyPCHSensorNumber)
1908 {
1909 pathName = pchPathName;
1910 }
1911 else
James Feistfaa4f222019-03-21 16:21:55 -07001912 {
James Feistacc8a4e2019-04-02 14:23:57 -07001913 return ipmi::responseParmOutOfRange();
James Feistfaa4f222019-03-21 16:21:55 -07001914 }
James Feist09f6b602019-08-08 11:30:03 -07001915
1916 uint8_t setpoint = legacyDefaultSetpoint;
1917 std::string path = getConfigPath(pathName);
James Feistfaa4f222019-03-21 16:21:55 -07001918 if (path.size())
1919 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001920 Value val = ipmi::getDbusProperty(
1921 *dbus, "xyz.openbmc_project.EntityManager", path,
1922 pidConfigurationIface, "SetPoint");
James Feistfaa4f222019-03-21 16:21:55 -07001923 setpoint = std::floor(std::get<double>(val) + 0.5);
1924 }
1925
1926 // old implementation used to return the "default" and current, we
1927 // don't make the default readily available so just make both the
1928 // same
James Feistfaa4f222019-03-21 16:21:55 -07001929
James Feistacc8a4e2019-04-02 14:23:57 -07001930 return ipmi::responseSuccess(
1931 std::array<uint8_t, 2>{setpoint, setpoint});
James Feistfaa4f222019-03-21 16:21:55 -07001932 }
James Feistacc8a4e2019-04-02 14:23:57 -07001933 else if (command == static_cast<uint8_t>(setFscParamFlags::maxPwm))
1934 {
1935 constexpr const size_t maxDomainCount = 8;
1936
1937 if (!param)
1938 {
1939 return ipmi::responseReqDataLenInvalid();
1940 }
1941 uint8_t requestedDomain = *param;
1942 if (requestedDomain >= maxDomainCount)
1943 {
1944 return ipmi::responseInvalidFieldRequest();
1945 }
1946
1947 boost::container::flat_map data = getPidConfigs();
1948 if (data.empty())
1949 {
1950 phosphor::logging::log<phosphor::logging::level::ERR>(
1951 "ipmiOEMGetFscParameter: found no pid configurations!");
1952 return ipmi::responseResponseError();
1953 }
1954 size_t count = 0;
1955 for (const auto& [_, pid] : data)
1956 {
1957 auto findClass = pid.find("Class");
1958 if (findClass == pid.end())
1959 {
1960 phosphor::logging::log<phosphor::logging::level::ERR>(
1961 "ipmiOEMGetFscParameter: found illegal pid "
1962 "configurations");
1963 return ipmi::responseResponseError();
1964 }
1965 std::string type = std::get<std::string>(findClass->second);
1966 if (type == "fan")
1967 {
1968 if (requestedDomain == count)
1969 {
1970 auto findOutLimit = pid.find("OutLimitMax");
1971 if (findOutLimit == pid.end())
1972 {
1973 phosphor::logging::log<phosphor::logging::level::ERR>(
1974 "ipmiOEMGetFscParameter: found illegal pid "
1975 "configurations");
1976 return ipmi::responseResponseError();
1977 }
1978
1979 return ipmi::responseSuccess(
1980 static_cast<uint8_t>(std::floor(
1981 std::get<double>(findOutLimit->second) + 0.5)));
1982 }
1983 else
1984 {
1985 count++;
1986 }
1987 }
1988 }
1989
1990 return ipmi::responseInvalidFieldRequest();
1991 }
1992 else if (command == static_cast<uint8_t>(setFscParamFlags::cfm))
James Feist5f957ca2019-03-14 15:33:55 -07001993 {
1994
1995 /*
1996 DataLen should be 1, but host is sending us an extra bit. As the
James Feistacc8a4e2019-04-02 14:23:57 -07001997 previous behavior didn't seem to prevent this, ignore the check for
1998 now.
James Feist5f957ca2019-03-14 15:33:55 -07001999
James Feistacc8a4e2019-04-02 14:23:57 -07002000 if (param)
James Feist5f957ca2019-03-14 15:33:55 -07002001 {
2002 phosphor::logging::log<phosphor::logging::level::ERR>(
2003 "ipmiOEMGetFscParameter: invalid input len!");
James Feist5f957ca2019-03-14 15:33:55 -07002004 return IPMI_CC_REQ_DATA_LEN_INVALID;
2005 }
2006 */
2007 Value cfmLimit;
2008 Value cfmMaximum;
2009 try
2010 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002011 cfmLimit = ipmi::getDbusProperty(*dbus, settingsBusName,
James Feist5f957ca2019-03-14 15:33:55 -07002012 cfmLimitSettingPath, cfmLimitIface,
2013 "Limit");
2014 cfmMaximum = ipmi::getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002015 *dbus, "xyz.openbmc_project.ExitAirTempSensor",
James Feist5f957ca2019-03-14 15:33:55 -07002016 "/xyz/openbmc_project/control/MaxCFM", cfmLimitIface, "Limit");
2017 }
2018 catch (sdbusplus::exception_t& e)
2019 {
2020 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feistacc8a4e2019-04-02 14:23:57 -07002021 "ipmiOEMGetFscParameter: can't get cfm setting!",
James Feist5f957ca2019-03-14 15:33:55 -07002022 phosphor::logging::entry("ERR=%s", e.what()));
James Feistacc8a4e2019-04-02 14:23:57 -07002023 return ipmi::responseResponseError();
James Feist5f957ca2019-03-14 15:33:55 -07002024 }
2025
James Feistacc8a4e2019-04-02 14:23:57 -07002026 double cfmMax = std::get<double>(cfmMaximum);
2027 double cfmLim = std::get<double>(cfmLimit);
James Feist5f957ca2019-03-14 15:33:55 -07002028
James Feistacc8a4e2019-04-02 14:23:57 -07002029 cfmLim = std::floor(cfmLim + 0.5);
2030 cfmMax = std::floor(cfmMax + 0.5);
2031 uint16_t cfmLimResp = static_cast<uint16_t>(cfmLim);
2032 uint16_t cfmMaxResp = static_cast<uint16_t>(cfmMax);
James Feist5f957ca2019-03-14 15:33:55 -07002033
James Feistacc8a4e2019-04-02 14:23:57 -07002034 return ipmi::responseSuccess(
2035 std::array<uint16_t, 2>{cfmLimResp, cfmMaxResp});
James Feist5f957ca2019-03-14 15:33:55 -07002036 }
James Feistacc8a4e2019-04-02 14:23:57 -07002037
James Feist5f957ca2019-03-14 15:33:55 -07002038 else
2039 {
2040 // todo other command parts possibly
James Feist5f957ca2019-03-14 15:33:55 -07002041 // domain pwm limit not implemented
James Feistacc8a4e2019-04-02 14:23:57 -07002042 return ipmi::responseParmOutOfRange();
James Feist5f957ca2019-03-14 15:33:55 -07002043 }
2044}
2045
Cheng C Yang773703a2019-08-15 09:41:11 +08002046using crConfigVariant =
2047 std::variant<bool, uint8_t, uint32_t, std::vector<uint8_t>, std::string>;
2048
2049int setCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2050 const crConfigVariant& value,
2051 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2052{
2053 boost::system::error_code ec;
2054 ctx->bus->yield_method_call<void>(
James Feist28c72902019-09-16 10:34:07 -07002055 ctx->yield, ec, "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002056 "/xyz/openbmc_project/control/power_supply_redundancy",
2057 "org.freedesktop.DBus.Properties", "Set",
2058 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property, value);
2059 if (ec)
2060 {
2061 phosphor::logging::log<phosphor::logging::level::ERR>(
2062 "Failed to set dbus property to cold redundancy");
2063 return -1;
2064 }
2065
2066 return 0;
2067}
2068
2069int getCRConfig(ipmi::Context::ptr ctx, const std::string& property,
2070 crConfigVariant& value,
Yong Li19445ab2019-12-20 18:25:29 +08002071 const std::string& service = "xyz.openbmc_project.Settings",
Cheng C Yang773703a2019-08-15 09:41:11 +08002072 std::chrono::microseconds timeout = ipmi::IPMI_DBUS_TIMEOUT)
2073{
2074 boost::system::error_code ec;
2075 value = ctx->bus->yield_method_call<crConfigVariant>(
Yong Li19445ab2019-12-20 18:25:29 +08002076 ctx->yield, ec, service,
Cheng C Yang773703a2019-08-15 09:41:11 +08002077 "/xyz/openbmc_project/control/power_supply_redundancy",
2078 "org.freedesktop.DBus.Properties", "Get",
2079 "xyz.openbmc_project.Control.PowerSupplyRedundancy", property);
2080 if (ec)
2081 {
2082 phosphor::logging::log<phosphor::logging::level::ERR>(
2083 "Failed to get dbus property to cold redundancy");
2084 return -1;
2085 }
2086 return 0;
2087}
2088
2089uint8_t getPSUCount(void)
2090{
2091 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2092 ipmi::Value num;
2093 try
2094 {
2095 num = ipmi::getDbusProperty(
2096 *dbus, "xyz.openbmc_project.PSURedundancy",
2097 "/xyz/openbmc_project/control/power_supply_redundancy",
2098 "xyz.openbmc_project.Control.PowerSupplyRedundancy", "PSUNumber");
2099 }
2100 catch (sdbusplus::exception_t& e)
2101 {
2102 phosphor::logging::log<phosphor::logging::level::ERR>(
2103 "Failed to get PSUNumber property from dbus interface");
2104 return 0;
2105 }
2106 uint8_t* pNum = std::get_if<uint8_t>(&num);
2107 if (!pNum)
2108 {
2109 phosphor::logging::log<phosphor::logging::level::ERR>(
2110 "Error to get PSU Number");
2111 return 0;
2112 }
2113 return *pNum;
2114}
2115
2116bool validateCRAlgo(std::vector<uint8_t>& conf, uint8_t num)
2117{
2118 if (conf.size() < num)
2119 {
2120 phosphor::logging::log<phosphor::logging::level::ERR>(
2121 "Invalid PSU Ranking");
2122 return false;
2123 }
2124 std::set<uint8_t> confSet;
2125 for (uint8_t i = 0; i < num; i++)
2126 {
2127 if (conf[i] > num)
2128 {
2129 phosphor::logging::log<phosphor::logging::level::ERR>(
2130 "PSU Ranking is larger than current PSU number");
2131 return false;
2132 }
2133 confSet.emplace(conf[i]);
2134 }
2135
2136 if (confSet.size() != num)
2137 {
2138 phosphor::logging::log<phosphor::logging::level::ERR>(
2139 "duplicate PSU Ranking");
2140 return false;
2141 }
2142 return true;
2143}
2144
2145enum class crParameter
2146{
2147 crStatus = 0,
2148 crFeature = 1,
2149 rotationFeature = 2,
2150 rotationAlgo = 3,
2151 rotationPeriod = 4,
Yong Li19445ab2019-12-20 18:25:29 +08002152 numOfPSU = 5,
2153 rotationRankOrderEffective = 6
Cheng C Yang773703a2019-08-15 09:41:11 +08002154};
2155
2156constexpr ipmi::Cc ccParameterNotSupported = 0x80;
2157static const constexpr uint32_t oneDay = 0x15180;
2158static const constexpr uint32_t oneMonth = 0xf53700;
2159static const constexpr uint8_t userSpecific = 0x01;
2160static const constexpr uint8_t crSetCompleted = 0;
2161ipmi::RspType<uint8_t> ipmiOEMSetCRConfig(ipmi::Context::ptr ctx,
2162 uint8_t parameter,
2163 ipmi::message::Payload& payload)
2164{
2165 switch (static_cast<crParameter>(parameter))
2166 {
2167 case crParameter::crFeature:
2168 {
2169 uint8_t param1;
2170 if (payload.unpack(param1) || !payload.fullyUnpacked())
2171 {
2172 return ipmi::responseReqDataLenInvalid();
2173 }
2174 // ColdRedundancy Enable can only be true or flase
2175 if (param1 > 1)
2176 {
2177 return ipmi::responseInvalidFieldRequest();
2178 }
2179 if (setCRConfig(ctx, "ColdRedundancyEnabled",
2180 static_cast<bool>(param1)))
2181 {
2182 return ipmi::responseResponseError();
2183 }
2184 break;
2185 }
2186 case crParameter::rotationFeature:
2187 {
2188 uint8_t param1;
2189 if (payload.unpack(param1) || !payload.fullyUnpacked())
2190 {
2191 return ipmi::responseReqDataLenInvalid();
2192 }
2193 // Rotation Enable can only be true or false
2194 if (param1 > 1)
2195 {
2196 return ipmi::responseInvalidFieldRequest();
2197 }
2198 if (setCRConfig(ctx, "RotationEnabled", static_cast<bool>(param1)))
2199 {
2200 return ipmi::responseResponseError();
2201 }
2202 break;
2203 }
2204 case crParameter::rotationAlgo:
2205 {
2206 // Rotation Algorithm can only be 0-BMC Specific or 1-User Specific
2207 std::string algoName;
2208 uint8_t param1;
2209 if (payload.unpack(param1))
2210 {
2211 return ipmi::responseReqDataLenInvalid();
2212 }
2213 switch (param1)
2214 {
2215 case 0:
2216 algoName = "xyz.openbmc_project.Control."
2217 "PowerSupplyRedundancy.Algo.bmcSpecific";
2218 break;
2219 case 1:
2220 algoName = "xyz.openbmc_project.Control."
2221 "PowerSupplyRedundancy.Algo.userSpecific";
2222 break;
2223 default:
2224 return ipmi::responseInvalidFieldRequest();
2225 }
2226 if (setCRConfig(ctx, "RotationAlgorithm", algoName))
2227 {
2228 return ipmi::responseResponseError();
2229 }
2230
2231 uint8_t numberOfPSU = getPSUCount();
2232 if (!numberOfPSU)
2233 {
2234 return ipmi::responseResponseError();
2235 }
2236 std::vector<uint8_t> rankOrder;
2237
2238 if (param1 == userSpecific)
2239 {
2240 if (payload.unpack(rankOrder) || !payload.fullyUnpacked())
2241 {
2242 ipmi::responseReqDataLenInvalid();
2243 }
Yong Li83315132019-10-23 17:42:24 +08002244 if (rankOrder.size() != numberOfPSU)
Cheng C Yang773703a2019-08-15 09:41:11 +08002245 {
2246 return ipmi::responseReqDataLenInvalid();
2247 }
2248
2249 if (!validateCRAlgo(rankOrder, numberOfPSU))
2250 {
2251 return ipmi::responseInvalidFieldRequest();
2252 }
2253 }
2254 else
2255 {
2256 if (rankOrder.size() > 0)
2257 {
2258 return ipmi::responseReqDataLenInvalid();
2259 }
2260 for (uint8_t i = 1; i <= numberOfPSU; i++)
2261 {
2262 rankOrder.emplace_back(i);
2263 }
2264 }
2265 if (setCRConfig(ctx, "RotationRankOrder", rankOrder))
2266 {
2267 return ipmi::responseResponseError();
2268 }
2269 break;
2270 }
2271 case crParameter::rotationPeriod:
2272 {
2273 // Minimum Rotation period is One day (86400 seconds) and Max
2274 // Rotation Period is 6 month (0xf53700 seconds)
2275 uint32_t period;
2276 if (payload.unpack(period) || !payload.fullyUnpacked())
2277 {
2278 return ipmi::responseReqDataLenInvalid();
2279 }
2280 if ((period < oneDay) || (period > oneMonth))
2281 {
2282 return ipmi::responseInvalidFieldRequest();
2283 }
2284 if (setCRConfig(ctx, "PeriodOfRotation", period))
2285 {
2286 return ipmi::responseResponseError();
2287 }
2288 break;
2289 }
2290 default:
2291 {
2292 return ipmi::response(ccParameterNotSupported);
2293 }
2294 }
2295
2296 // TODO Halfwidth needs to set SetInProgress
2297 if (setCRConfig(ctx, "ColdRedundancyStatus",
Cheng C Yange8cecdf2019-08-26 23:48:08 +08002298 std::string("xyz.openbmc_project.Control."
2299 "PowerSupplyRedundancy.Status.completed")))
Cheng C Yang773703a2019-08-15 09:41:11 +08002300 {
2301 return ipmi::responseResponseError();
2302 }
2303 return ipmi::responseSuccess(crSetCompleted);
2304}
2305
Yong Li83315132019-10-23 17:42:24 +08002306ipmi::RspType<uint8_t, std::variant<uint8_t, uint32_t, std::vector<uint8_t>>>
Cheng C Yang773703a2019-08-15 09:41:11 +08002307 ipmiOEMGetCRConfig(ipmi::Context::ptr ctx, uint8_t parameter)
2308{
2309 crConfigVariant value;
2310 switch (static_cast<crParameter>(parameter))
2311 {
2312 case crParameter::crStatus:
2313 {
2314 if (getCRConfig(ctx, "ColdRedundancyStatus", value))
2315 {
2316 return ipmi::responseResponseError();
2317 }
2318 std::string* pStatus = std::get_if<std::string>(&value);
2319 if (!pStatus)
2320 {
2321 phosphor::logging::log<phosphor::logging::level::ERR>(
2322 "Error to get ColdRedundancyStatus property");
2323 return ipmi::responseResponseError();
2324 }
2325 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2326 auto status =
2327 server::PowerSupplyRedundancy::convertStatusFromString(
2328 *pStatus);
2329 switch (status)
2330 {
2331 case server::PowerSupplyRedundancy::Status::inProgress:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002332 return ipmi::responseSuccess(parameter,
2333 static_cast<uint8_t>(0));
Cheng C Yang773703a2019-08-15 09:41:11 +08002334
2335 case server::PowerSupplyRedundancy::Status::completed:
Cheng C Yangf41e3342019-09-10 04:47:23 +08002336 return ipmi::responseSuccess(parameter,
2337 static_cast<uint8_t>(1));
Cheng C Yang773703a2019-08-15 09:41:11 +08002338 default:
2339 phosphor::logging::log<phosphor::logging::level::ERR>(
2340 "Error to get valid status");
2341 return ipmi::responseResponseError();
2342 }
2343 }
2344 case crParameter::crFeature:
2345 {
2346 if (getCRConfig(ctx, "ColdRedundancyEnabled", value))
2347 {
2348 return ipmi::responseResponseError();
2349 }
2350 bool* pResponse = std::get_if<bool>(&value);
2351 if (!pResponse)
2352 {
2353 phosphor::logging::log<phosphor::logging::level::ERR>(
2354 "Error to get ColdRedundancyEnable property");
2355 return ipmi::responseResponseError();
2356 }
2357
Cheng C Yangf41e3342019-09-10 04:47:23 +08002358 return ipmi::responseSuccess(parameter,
2359 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002360 }
2361 case crParameter::rotationFeature:
2362 {
2363 if (getCRConfig(ctx, "RotationEnabled", value))
2364 {
2365 return ipmi::responseResponseError();
2366 }
2367 bool* pResponse = std::get_if<bool>(&value);
2368 if (!pResponse)
2369 {
2370 phosphor::logging::log<phosphor::logging::level::ERR>(
2371 "Error to get RotationEnabled property");
2372 return ipmi::responseResponseError();
2373 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002374 return ipmi::responseSuccess(parameter,
2375 static_cast<uint8_t>(*pResponse));
Cheng C Yang773703a2019-08-15 09:41:11 +08002376 }
2377 case crParameter::rotationAlgo:
2378 {
2379 if (getCRConfig(ctx, "RotationAlgorithm", value))
2380 {
2381 return ipmi::responseResponseError();
2382 }
2383
2384 std::string* pAlgo = std::get_if<std::string>(&value);
2385 if (!pAlgo)
2386 {
2387 phosphor::logging::log<phosphor::logging::level::ERR>(
2388 "Error to get RotationAlgorithm property");
2389 return ipmi::responseResponseError();
2390 }
Yong Li83315132019-10-23 17:42:24 +08002391 std::vector<uint8_t> response;
Cheng C Yang773703a2019-08-15 09:41:11 +08002392 namespace server = sdbusplus::xyz::openbmc_project::Control::server;
2393 auto algo =
2394 server::PowerSupplyRedundancy::convertAlgoFromString(*pAlgo);
Yong Li83315132019-10-23 17:42:24 +08002395
Cheng C Yang773703a2019-08-15 09:41:11 +08002396 switch (algo)
2397 {
2398 case server::PowerSupplyRedundancy::Algo::bmcSpecific:
Yong Li83315132019-10-23 17:42:24 +08002399 response.push_back(0);
Cheng C Yang773703a2019-08-15 09:41:11 +08002400 break;
2401 case server::PowerSupplyRedundancy::Algo::userSpecific:
Yong Li83315132019-10-23 17:42:24 +08002402 response.push_back(1);
Cheng C Yang773703a2019-08-15 09:41:11 +08002403 break;
2404 default:
2405 phosphor::logging::log<phosphor::logging::level::ERR>(
2406 "Error to get valid algo");
2407 return ipmi::responseResponseError();
2408 }
2409
2410 if (getCRConfig(ctx, "RotationRankOrder", value))
2411 {
2412 return ipmi::responseResponseError();
2413 }
2414 std::vector<uint8_t>* pResponse =
2415 std::get_if<std::vector<uint8_t>>(&value);
2416 if (!pResponse)
2417 {
2418 phosphor::logging::log<phosphor::logging::level::ERR>(
2419 "Error to get RotationRankOrder property");
2420 return ipmi::responseResponseError();
2421 }
Yong Li83315132019-10-23 17:42:24 +08002422
Cheng C Yang773703a2019-08-15 09:41:11 +08002423 std::copy(pResponse->begin(), pResponse->end(),
Yong Li83315132019-10-23 17:42:24 +08002424 std::back_inserter(response));
2425
Cheng C Yangf41e3342019-09-10 04:47:23 +08002426 return ipmi::responseSuccess(parameter, response);
Cheng C Yang773703a2019-08-15 09:41:11 +08002427 }
2428 case crParameter::rotationPeriod:
2429 {
2430 if (getCRConfig(ctx, "PeriodOfRotation", value))
2431 {
2432 return ipmi::responseResponseError();
2433 }
2434 uint32_t* pResponse = std::get_if<uint32_t>(&value);
2435 if (!pResponse)
2436 {
2437 phosphor::logging::log<phosphor::logging::level::ERR>(
2438 "Error to get RotationAlgorithm property");
2439 return ipmi::responseResponseError();
2440 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002441 return ipmi::responseSuccess(parameter, *pResponse);
Cheng C Yang773703a2019-08-15 09:41:11 +08002442 }
2443 case crParameter::numOfPSU:
2444 {
2445 uint8_t numberOfPSU = getPSUCount();
2446 if (!numberOfPSU)
2447 {
2448 return ipmi::responseResponseError();
2449 }
Cheng C Yangf41e3342019-09-10 04:47:23 +08002450 return ipmi::responseSuccess(parameter, numberOfPSU);
Cheng C Yang773703a2019-08-15 09:41:11 +08002451 }
Yong Li19445ab2019-12-20 18:25:29 +08002452 case crParameter::rotationRankOrderEffective:
2453 {
2454 if (getCRConfig(ctx, "RotationRankOrder", value,
2455 "xyz.openbmc_project.PSURedundancy"))
2456 {
2457 return ipmi::responseResponseError();
2458 }
2459 std::vector<uint8_t>* pResponse =
2460 std::get_if<std::vector<uint8_t>>(&value);
2461 if (!pResponse)
2462 {
2463 phosphor::logging::log<phosphor::logging::level::ERR>(
2464 "Error to get effective RotationRankOrder property");
2465 return ipmi::responseResponseError();
2466 }
2467 return ipmi::responseSuccess(parameter, *pResponse);
2468 }
Cheng C Yang773703a2019-08-15 09:41:11 +08002469 default:
2470 {
2471 return ipmi::response(ccParameterNotSupported);
2472 }
2473 }
2474}
2475
Zhu, Yungebe560b02019-04-21 21:19:21 -04002476ipmi::RspType<> ipmiOEMSetFaultIndication(uint8_t sourceId, uint8_t faultType,
2477 uint8_t faultState,
2478 uint8_t faultGroup,
2479 std::array<uint8_t, 8>& ledStateData)
2480{
Zhu, Yungebe560b02019-04-21 21:19:21 -04002481 constexpr auto maxFaultType = static_cast<size_t>(RemoteFaultType::max);
2482 static const std::array<std::string, maxFaultType> faultNames = {
2483 "faultFan", "faultTemp", "faultPower",
2484 "faultDriveSlot", "faultSoftware", "faultMemory"};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002485
2486 constexpr uint8_t maxFaultSource = 0x4;
2487 constexpr uint8_t skipLEDs = 0xFF;
2488 constexpr uint8_t pinSize = 64;
2489 constexpr uint8_t groupSize = 16;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002490 constexpr uint8_t groupNum = 5; // 4 for fault memory, 1 for faultFan
Zhu, Yungebe560b02019-04-21 21:19:21 -04002491
Zhikui Rence4e73f2019-12-06 13:59:47 -08002492 // same pin names need to be defined in dts file
2493 static const std::array<std::array<std::string, groupSize>, groupNum>
2494 faultLedPinNames = {{
2495 "LED_CPU1_CH1_DIMM1_FAULT",
2496 "LED_CPU1_CH1_DIMM2_FAULT",
2497 "LED_CPU1_CH2_DIMM1_FAULT",
2498 "LED_CPU1_CH2_DIMM2_FAULT",
2499 "LED_CPU1_CH3_DIMM1_FAULT",
2500 "LED_CPU1_CH3_DIMM2_FAULT",
2501 "LED_CPU1_CH4_DIMM1_FAULT",
2502 "LED_CPU1_CH4_DIMM2_FAULT",
2503 "LED_CPU1_CH5_DIMM1_FAULT",
2504 "LED_CPU1_CH5_DIMM2_FAULT",
2505 "LED_CPU1_CH6_DIMM1_FAULT",
2506 "LED_CPU1_CH6_DIMM2_FAULT",
2507 "",
2508 "",
2509 "",
2510 "", // end of group1
2511 "LED_CPU2_CH1_DIMM1_FAULT",
2512 "LED_CPU2_CH1_DIMM2_FAULT",
2513 "LED_CPU2_CH2_DIMM1_FAULT",
2514 "LED_CPU2_CH2_DIMM2_FAULT",
2515 "LED_CPU2_CH3_DIMM1_FAULT",
2516 "LED_CPU2_CH3_DIMM2_FAULT",
2517 "LED_CPU2_CH4_DIMM1_FAULT",
2518 "LED_CPU2_CH4_DIMM2_FAULT",
2519 "LED_CPU2_CH5_DIMM1_FAULT",
2520 "LED_CPU2_CH5_DIMM2_FAULT",
2521 "LED_CPU2_CH6_DIMM1_FAULT",
2522 "LED_CPU2_CH6_DIMM2_FAULT",
2523 "",
2524 "",
2525 "",
2526 "", // endof group2
2527 "LED_CPU3_CH1_DIMM1_FAULT",
2528 "LED_CPU3_CH1_DIMM2_FAULT",
2529 "LED_CPU3_CH2_DIMM1_FAULT",
2530 "LED_CPU3_CH2_DIMM2_FAULT",
2531 "LED_CPU3_CH3_DIMM1_FAULT",
2532 "LED_CPU3_CH3_DIMM2_FAULT",
2533 "LED_CPU3_CH4_DIMM1_FAULT",
2534 "LED_CPU3_CH4_DIMM2_FAULT",
2535 "LED_CPU3_CH5_DIMM1_FAULT",
2536 "LED_CPU3_CH5_DIMM2_FAULT",
2537 "LED_CPU3_CH6_DIMM1_FAULT",
2538 "LED_CPU3_CH6_DIMM2_FAULT",
2539 "",
2540 "",
2541 "",
2542 "", // end of group3
2543 "LED_CPU4_CH1_DIMM1_FAULT",
2544 "LED_CPU4_CH1_DIMM2_FAULT",
2545 "LED_CPU4_CH2_DIMM1_FAULT",
2546 "LED_CPU4_CH2_DIMM2_FAULT",
2547 "LED_CPU4_CH3_DIMM1_FAULT",
2548 "LED_CPU4_CH3_DIMM2_FAULT",
2549 "LED_CPU4_CH4_DIMM1_FAULT",
2550 "LED_CPU4_CH4_DIMM2_FAULT",
2551 "LED_CPU4_CH5_DIMM1_FAULT",
2552 "LED_CPU4_CH5_DIMM2_FAULT",
2553 "LED_CPU4_CH6_DIMM1_FAULT",
2554 "LED_CPU4_CH6_DIMM2_FAULT",
2555 "",
2556 "",
2557 "",
2558 "", // end of group4
2559 "LED_FAN1_FAULT",
2560 "LED_FAN2_FAULT",
2561 "LED_FAN3_FAULT",
2562 "LED_FAN4_FAULT",
2563 "LED_FAN5_FAULT",
2564 "LED_FAN6_FAULT",
2565 "LED_FAN7_FAULT",
2566 "LED_FAN8_FAULT",
2567 "",
2568 "",
2569 "",
2570 "",
2571 "",
2572 "",
2573 "",
2574 "" // end of group5
2575 }};
Zhu, Yungebe560b02019-04-21 21:19:21 -04002576
Zhikui Rence4e73f2019-12-06 13:59:47 -08002577 // Validate the source, fault type --
2578 // (Byte 1) sourceId: Unspecified, Hot-Swap Controller 0, Hot-Swap
2579 // Controller 1, BIOS (Byte 2) fault type: fan, temperature, power,
2580 // driveslot, software, memory (Byte 3) FaultState: OK, Degraded,
2581 // Non-Critical, Critical, Non-Recoverable, (Byte 4) is faultGroup,
2582 // definition differs based on fault type (Byte 2)
2583 // Type Fan=> Group: 0=FanGroupID, FF-not used
2584 // Byte 5-11 00h, not used
2585 // Byte12 FanLedState [7:0]-Fans 7:0
2586 // Type Memory=> Group: 0 = DIMM GroupID, FF-not used
2587 // Byte 5:12 - DIMM LED state (64bit field, LS Byte first)
2588 // [63:48] = CPU4 channels 7:0, 2 bits per channel
2589 // [47:32] = CPU3 channels 7:0, 2 bits per channel
2590 // [31:16] = CPU2 channels 7:0, 2 bits per channel
2591 // [15:0] = CPU1 channels 7:0, 2 bits per channel
2592 // Type Other=> Component Fault LED Group ID, not used set to 0xFF
2593 // Byte[5:12]: reserved 0x00h
Zhu, Yungebe560b02019-04-21 21:19:21 -04002594 if ((sourceId >= maxFaultSource) ||
2595 (faultType >= static_cast<int8_t>(RemoteFaultType::max)) ||
2596 (faultState >= static_cast<int8_t>(RemoteFaultState::maxFaultState)) ||
2597 (faultGroup >= static_cast<int8_t>(DimmFaultType::maxFaultGroup)))
2598 {
2599 return ipmi::responseParmOutOfRange();
2600 }
2601
Zhikui Rence4e73f2019-12-06 13:59:47 -08002602 size_t pinGroupOffset = 0;
2603 size_t pinGroupMax = pinSize / groupSize;
2604 if (RemoteFaultType::fan == RemoteFaultType(faultType))
Zhu, Yungebe560b02019-04-21 21:19:21 -04002605 {
Zhikui Rence4e73f2019-12-06 13:59:47 -08002606 pinGroupOffset = 4;
2607 pinGroupMax = groupNum - pinSize / groupSize;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002608 }
2609
2610 switch (RemoteFaultType(faultType))
2611 {
2612 case (RemoteFaultType::fan):
2613 case (RemoteFaultType::memory):
2614 {
2615 if (faultGroup == skipLEDs)
2616 {
2617 return ipmi::responseSuccess();
2618 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002619 // calculate led state bit filed count, each byte has 8bits
2620 // the maximum bits will be 8 * 8 bits
2621 constexpr uint8_t size = sizeof(ledStateData) * 8;
Zhikui Rence4e73f2019-12-06 13:59:47 -08002622
2623 // assemble ledState
2624 uint64_t ledState = 0;
2625 bool hasError = false;
Zhu, Yungebe560b02019-04-21 21:19:21 -04002626 for (int i = 0; i < sizeof(ledStateData); i++)
2627 {
2628 ledState = (uint64_t)(ledState << 8);
2629 ledState = (uint64_t)(ledState | (uint64_t)ledStateData[i]);
2630 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002631 std::bitset<size> ledStateBits(ledState);
Zhu, Yungebe560b02019-04-21 21:19:21 -04002632
Zhikui Rence4e73f2019-12-06 13:59:47 -08002633 for (int group = 0; group < pinGroupMax; group++)
2634 {
2635 for (int i = 0; i < groupSize; i++)
2636 { // skip non-existing pins
2637 if (0 == faultLedPinNames[group + pinGroupOffset][i].size())
2638 {
2639 continue;
2640 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002641
Zhikui Rence4e73f2019-12-06 13:59:47 -08002642 gpiod::line line = gpiod::find_line(
2643 faultLedPinNames[group + pinGroupOffset][i]);
2644 if (!line)
2645 {
2646 phosphor::logging::log<phosphor::logging::level::ERR>(
2647 "Not Find Led Gpio Device!",
2648 phosphor::logging::entry(
2649 "DEVICE=%s",
2650 faultLedPinNames[group + pinGroupOffset][i]
2651 .c_str()));
2652 hasError = true;
2653 continue;
2654 }
Zhu, Yungebe560b02019-04-21 21:19:21 -04002655
Zhikui Rence4e73f2019-12-06 13:59:47 -08002656 bool activeHigh =
2657 (line.active_state() == gpiod::line::ACTIVE_HIGH);
2658 try
2659 {
2660 line.request(
2661 {"faultLed", gpiod::line_request::DIRECTION_OUTPUT,
2662 activeHigh
2663 ? 0
2664 : gpiod::line_request::FLAG_ACTIVE_LOW});
2665 line.set_value(ledStateBits[i + group * groupSize]);
2666 }
2667 catch (std::system_error&)
2668 {
2669 phosphor::logging::log<phosphor::logging::level::ERR>(
2670 "Error write Led Gpio Device!",
2671 phosphor::logging::entry(
2672 "DEVICE=%s",
2673 faultLedPinNames[group + pinGroupOffset][i]
2674 .c_str()));
2675 hasError = true;
2676 continue;
2677 }
2678 } // for int i
2679 }
2680 if (hasError)
2681 {
2682 return ipmi::responseResponseError();
Zhu, Yungebe560b02019-04-21 21:19:21 -04002683 }
2684 break;
2685 }
2686 default:
2687 {
2688 // now only support two fault types
2689 return ipmi::responseParmOutOfRange();
2690 }
Zhikui Rence4e73f2019-12-06 13:59:47 -08002691 } // switch
Zhu, Yungebe560b02019-04-21 21:19:21 -04002692 return ipmi::responseSuccess();
2693}
2694
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302695ipmi::RspType<uint8_t> ipmiOEMReadBoardProductId()
2696{
2697 uint8_t prodId = 0;
2698 try
2699 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07002700 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302701 const DbusObjectInfo& object = getDbusObject(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002702 *dbus, "xyz.openbmc_project.Inventory.Item.Board",
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302703 "/xyz/openbmc_project/inventory/system/board/", "Baseboard");
2704 const Value& propValue = getDbusProperty(
Vernon Mauery15419dd2019-05-24 09:40:30 -07002705 *dbus, object.second, object.first,
Suryakanth Sekar6c57e5c2020-01-10 17:11:58 +05302706 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
2707 "ProductId");
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05302708 prodId = static_cast<uint8_t>(std::get<uint64_t>(propValue));
2709 }
2710 catch (std::exception& e)
2711 {
2712 phosphor::logging::log<phosphor::logging::level::ERR>(
2713 "ipmiOEMReadBoardProductId: Product ID read failed!",
2714 phosphor::logging::entry("ERR=%s", e.what()));
2715 }
2716 return ipmi::responseSuccess(prodId);
2717}
2718
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302719/** @brief implements the get security mode command
2720 * @param ctx - ctx pointer
2721 *
2722 * @returns IPMI completion code with following data
2723 * - restriction mode value - As specified in
2724 * xyz.openbmc_project.Control.Security.RestrictionMode.interface.yaml
2725 * - special mode value - As specified in
2726 * xyz.openbmc_project.Control.Security.SpecialMode.interface.yaml
2727 */
2728ipmi::RspType<uint8_t, uint8_t> ipmiGetSecurityMode(ipmi::Context::ptr ctx)
2729{
2730 namespace securityNameSpace =
2731 sdbusplus::xyz::openbmc_project::Control::Security::server;
2732 uint8_t restrictionModeValue = 0;
2733 uint8_t specialModeValue = 0;
2734
2735 boost::system::error_code ec;
2736 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002737 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302738 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2739 restricionModeProperty);
2740 if (ec)
2741 {
2742 phosphor::logging::log<phosphor::logging::level::ERR>(
2743 "ipmiGetSecurityMode: failed to get RestrictionMode property",
2744 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2745 return ipmi::responseUnspecifiedError();
2746 }
2747 restrictionModeValue = static_cast<uint8_t>(
2748 securityNameSpace::RestrictionMode::convertModesFromString(
2749 std::get<std::string>(varRestrMode)));
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302750 auto varSpecialMode =
2751 ctx->bus->yield_method_call<std::variant<std::string>>(
2752 ctx->yield, ec, specialModeService, specialModeBasePath,
2753 dBusPropertyIntf, dBusPropertyGetMethod, specialModeIntf,
2754 specialModeProperty);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302755 if (ec)
2756 {
2757 phosphor::logging::log<phosphor::logging::level::ERR>(
2758 "ipmiGetSecurityMode: failed to get SpecialMode property",
2759 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2760 // fall through, let us not worry about SpecialMode property, which is
2761 // not required in user scenario
2762 }
2763 else
2764 {
Richard Marian Thomaiyar8d4f8d72019-11-11 12:06:40 +05302765 specialModeValue = static_cast<uint8_t>(
2766 securityNameSpace::SpecialMode::convertModesFromString(
2767 std::get<std::string>(varSpecialMode)));
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302768 }
2769 return ipmi::responseSuccess(restrictionModeValue, specialModeValue);
2770}
2771
2772/** @brief implements the set security mode command
2773 * Command allows to upgrade the restriction mode and won't allow
2774 * to downgrade from system interface
2775 * @param ctx - ctx pointer
2776 * @param restrictionMode - restriction mode value to be set.
2777 *
2778 * @returns IPMI completion code
2779 */
2780ipmi::RspType<> ipmiSetSecurityMode(ipmi::Context::ptr ctx,
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302781 uint8_t restrictionMode,
2782 std::optional<uint8_t> specialMode)
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302783{
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302784#ifndef BMC_VALIDATION_UNSECURE_FEATURE
2785 if (specialMode)
2786 {
2787 return ipmi::responseReqDataLenInvalid();
2788 }
2789#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302790 namespace securityNameSpace =
2791 sdbusplus::xyz::openbmc_project::Control::Security::server;
2792
2793 ChannelInfo chInfo;
2794 if (getChannelInfo(ctx->channel, chInfo) != ccSuccess)
2795 {
2796 phosphor::logging::log<phosphor::logging::level::ERR>(
2797 "ipmiSetSecurityMode: Failed to get Channel Info",
2798 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
2799 return ipmi::responseUnspecifiedError();
2800 }
2801 auto reqMode =
2802 static_cast<securityNameSpace::RestrictionMode::Modes>(restrictionMode);
2803
2804 if ((reqMode < securityNameSpace::RestrictionMode::Modes::Provisioning) ||
2805 (reqMode >
2806 securityNameSpace::RestrictionMode::Modes::ProvisionedHostDisabled))
2807 {
2808 return ipmi::responseInvalidFieldRequest();
2809 }
2810
2811 boost::system::error_code ec;
2812 auto varRestrMode = ctx->bus->yield_method_call<std::variant<std::string>>(
James Feist28c72902019-09-16 10:34:07 -07002813 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302814 dBusPropertyIntf, dBusPropertyGetMethod, restricionModeIntf,
2815 restricionModeProperty);
2816 if (ec)
2817 {
2818 phosphor::logging::log<phosphor::logging::level::ERR>(
2819 "ipmiSetSecurityMode: failed to get RestrictionMode property",
2820 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2821 return ipmi::responseUnspecifiedError();
2822 }
2823 auto currentRestrictionMode =
2824 securityNameSpace::RestrictionMode::convertModesFromString(
2825 std::get<std::string>(varRestrMode));
2826
2827 if (chInfo.mediumType !=
2828 static_cast<uint8_t>(EChannelMediumType::lan8032) &&
2829 currentRestrictionMode > reqMode)
2830 {
2831 phosphor::logging::log<phosphor::logging::level::ERR>(
2832 "ipmiSetSecurityMode - Downgrading security mode not supported "
2833 "through system interface",
2834 phosphor::logging::entry(
2835 "CUR_MODE=%d", static_cast<uint8_t>(currentRestrictionMode)),
2836 phosphor::logging::entry("REQ_MODE=%d", restrictionMode));
2837 return ipmi::responseCommandNotAvailable();
2838 }
2839
2840 ec.clear();
2841 ctx->bus->yield_method_call<>(
James Feist28c72902019-09-16 10:34:07 -07002842 ctx->yield, ec, restricionModeService, restricionModeBasePath,
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302843 dBusPropertyIntf, dBusPropertySetMethod, restricionModeIntf,
2844 restricionModeProperty,
2845 static_cast<std::variant<std::string>>(
2846 securityNameSpace::convertForMessage(reqMode)));
2847
2848 if (ec)
2849 {
2850 phosphor::logging::log<phosphor::logging::level::ERR>(
2851 "ipmiSetSecurityMode: failed to set RestrictionMode property",
2852 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2853 return ipmi::responseUnspecifiedError();
2854 }
Richard Marian Thomaiyar10791062019-11-11 12:19:53 +05302855
2856#ifdef BMC_VALIDATION_UNSECURE_FEATURE
2857 if (specialMode)
2858 {
2859 ec.clear();
2860 ctx->bus->yield_method_call<>(
2861 ctx->yield, ec, specialModeService, specialModeBasePath,
2862 dBusPropertyIntf, dBusPropertySetMethod, specialModeIntf,
2863 specialModeProperty,
2864 static_cast<std::variant<std::string>>(
2865 securityNameSpace::convertForMessage(
2866 static_cast<securityNameSpace::SpecialMode::Modes>(
2867 specialMode.value()))));
2868
2869 if (ec)
2870 {
2871 phosphor::logging::log<phosphor::logging::level::ERR>(
2872 "ipmiSetSecurityMode: failed to set SpecialMode property",
2873 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
2874 return ipmi::responseUnspecifiedError();
2875 }
2876 }
2877#endif
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05302878 return ipmi::responseSuccess();
2879}
2880
Vernon Mauery4ac799d2019-05-20 15:50:37 -07002881ipmi::RspType<uint8_t /* restore status */>
2882 ipmiRestoreConfiguration(const std::array<uint8_t, 3>& clr, uint8_t cmd)
2883{
2884 static constexpr std::array<uint8_t, 3> expClr = {'C', 'L', 'R'};
2885
2886 if (clr != expClr)
2887 {
2888 return ipmi::responseInvalidFieldRequest();
2889 }
2890 constexpr uint8_t cmdStatus = 0;
2891 constexpr uint8_t cmdDefaultRestore = 0xaa;
2892 constexpr uint8_t cmdFullRestore = 0xbb;
2893 constexpr uint8_t cmdFormat = 0xcc;
2894
2895 constexpr const char* restoreOpFname = "/tmp/.rwfs/.restore_op";
2896
2897 switch (cmd)
2898 {
2899 case cmdStatus:
2900 break;
2901 case cmdDefaultRestore:
2902 case cmdFullRestore:
2903 case cmdFormat:
2904 {
2905 // write file to rwfs root
2906 int value = (cmd - 1) & 0x03; // map aa, bb, cc => 1, 2, 3
2907 std::ofstream restoreFile(restoreOpFname);
2908 if (!restoreFile)
2909 {
2910 return ipmi::responseUnspecifiedError();
2911 }
2912 restoreFile << value << "\n";
2913 break;
2914 }
2915 default:
2916 return ipmi::responseInvalidFieldRequest();
2917 }
2918
2919 constexpr uint8_t restorePending = 0;
2920 constexpr uint8_t restoreComplete = 1;
2921
2922 uint8_t restoreStatus = std::filesystem::exists(restoreOpFname)
2923 ? restorePending
2924 : restoreComplete;
2925 return ipmi::responseSuccess(restoreStatus);
2926}
2927
Chen Yugang39736d52019-07-12 16:24:33 +08002928ipmi::RspType<uint8_t> ipmiOEMGetNmiSource(void)
2929{
2930 uint8_t bmcSource;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002931 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002932
2933 try
2934 {
2935 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
2936 std::string service =
2937 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
2938 Value variant =
2939 getDbusProperty(*dbus, service, oemNmiSourceObjPath,
2940 oemNmiSourceIntf, oemNmiBmcSourceObjPathProp);
2941
2942 switch (nmi::NMISource::convertBMCSourceSignalFromString(
2943 std::get<std::string>(variant)))
2944 {
2945 case nmi::NMISource::BMCSourceSignal::None:
2946 bmcSource = static_cast<uint8_t>(NmiSource::none);
2947 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002948 case nmi::NMISource::BMCSourceSignal::FrontPanelButton:
2949 bmcSource = static_cast<uint8_t>(NmiSource::frontPanelButton);
Chen Yugang39736d52019-07-12 16:24:33 +08002950 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002951 case nmi::NMISource::BMCSourceSignal::Watchdog:
2952 bmcSource = static_cast<uint8_t>(NmiSource::watchdog);
Chen Yugang39736d52019-07-12 16:24:33 +08002953 break;
2954 case nmi::NMISource::BMCSourceSignal::ChassisCmd:
2955 bmcSource = static_cast<uint8_t>(NmiSource::chassisCmd);
2956 break;
2957 case nmi::NMISource::BMCSourceSignal::MemoryError:
2958 bmcSource = static_cast<uint8_t>(NmiSource::memoryError);
2959 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002960 case nmi::NMISource::BMCSourceSignal::PciBusError:
2961 bmcSource = static_cast<uint8_t>(NmiSource::pciBusError);
Chen Yugang39736d52019-07-12 16:24:33 +08002962 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002963 case nmi::NMISource::BMCSourceSignal::PCH:
2964 bmcSource = static_cast<uint8_t>(NmiSource::pch);
Chen Yugang39736d52019-07-12 16:24:33 +08002965 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002966 case nmi::NMISource::BMCSourceSignal::Chipset:
2967 bmcSource = static_cast<uint8_t>(NmiSource::chipset);
Chen Yugang39736d52019-07-12 16:24:33 +08002968 break;
2969 default:
2970 phosphor::logging::log<phosphor::logging::level::ERR>(
2971 "NMI source: invalid property!",
2972 phosphor::logging::entry(
2973 "PROP=%s", std::get<std::string>(variant).c_str()));
2974 return ipmi::responseResponseError();
2975 }
2976 }
2977 catch (sdbusplus::exception::SdBusError& e)
2978 {
2979 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
2980 return ipmi::responseResponseError();
2981 }
2982
2983 return ipmi::responseSuccess(bmcSource);
2984}
2985
2986ipmi::RspType<> ipmiOEMSetNmiSource(uint8_t sourceId)
2987{
Chen Yugang97cf96e2019-11-01 08:55:11 +08002988 namespace nmi = sdbusplus::xyz::openbmc_project::Chassis::Control::server;
Chen Yugang39736d52019-07-12 16:24:33 +08002989
2990 nmi::NMISource::BMCSourceSignal bmcSourceSignal =
2991 nmi::NMISource::BMCSourceSignal::None;
2992
2993 switch (NmiSource(sourceId))
2994 {
2995 case NmiSource::none:
2996 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::None;
2997 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08002998 case NmiSource::frontPanelButton:
2999 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::FrontPanelButton;
Chen Yugang39736d52019-07-12 16:24:33 +08003000 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003001 case NmiSource::watchdog:
3002 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Watchdog;
Chen Yugang39736d52019-07-12 16:24:33 +08003003 break;
3004 case NmiSource::chassisCmd:
3005 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::ChassisCmd;
3006 break;
3007 case NmiSource::memoryError:
3008 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::MemoryError;
3009 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003010 case NmiSource::pciBusError:
3011 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PciBusError;
Chen Yugang39736d52019-07-12 16:24:33 +08003012 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003013 case NmiSource::pch:
3014 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::PCH;
Chen Yugang39736d52019-07-12 16:24:33 +08003015 break;
Chen Yugang97cf96e2019-11-01 08:55:11 +08003016 case NmiSource::chipset:
3017 bmcSourceSignal = nmi::NMISource::BMCSourceSignal::Chipset;
Chen Yugang39736d52019-07-12 16:24:33 +08003018 break;
3019 default:
3020 phosphor::logging::log<phosphor::logging::level::ERR>(
3021 "NMI source: invalid property!");
3022 return ipmi::responseResponseError();
3023 }
3024
3025 try
3026 {
3027 // keep NMI signal source
3028 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3029 std::string service =
3030 getService(*dbus, oemNmiSourceIntf, oemNmiSourceObjPath);
Chen Yugang97cf96e2019-11-01 08:55:11 +08003031 setDbusProperty(*dbus, service, oemNmiSourceObjPath, oemNmiSourceIntf,
3032 oemNmiBmcSourceObjPathProp,
3033 nmi::convertForMessage(bmcSourceSignal));
Chen Yugang99be6332019-08-09 16:20:48 +08003034 // set Enabled property to inform NMI source handling
3035 // to trigger a NMI_OUT BSOD.
3036 // if it's triggered by NMI source property changed,
3037 // NMI_OUT BSOD could be missed if the same source occurs twice in a row
3038 if (bmcSourceSignal != nmi::NMISource::BMCSourceSignal::None)
3039 {
3040 setDbusProperty(*dbus, service, oemNmiSourceObjPath,
3041 oemNmiSourceIntf, oemNmiEnabledObjPathProp,
3042 static_cast<bool>(true));
3043 }
Chen Yugang39736d52019-07-12 16:24:33 +08003044 }
3045 catch (sdbusplus::exception_t& e)
3046 {
3047 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3048 return ipmi::responseResponseError();
3049 }
3050
3051 return ipmi::responseSuccess();
3052}
3053
James Feist63efafa2019-07-24 12:39:21 -07003054namespace dimmOffset
3055{
3056constexpr const char* dimmPower = "DimmPower";
3057constexpr const char* staticCltt = "StaticCltt";
3058constexpr const char* offsetPath = "/xyz/openbmc_project/Inventory/Item/Dimm";
3059constexpr const char* offsetInterface =
3060 "xyz.openbmc_project.Inventory.Item.Dimm.Offset";
3061constexpr const char* property = "DimmOffset";
3062
3063}; // namespace dimmOffset
3064
3065ipmi::RspType<>
3066 ipmiOEMSetDimmOffset(uint8_t type,
3067 const std::vector<std::tuple<uint8_t, uint8_t>>& data)
3068{
3069 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3070 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3071 {
3072 return ipmi::responseInvalidFieldRequest();
3073 }
3074
3075 if (data.empty())
3076 {
3077 return ipmi::responseInvalidFieldRequest();
3078 }
3079 nlohmann::json json;
3080
3081 std::ifstream jsonStream(dimmOffsetFile);
3082 if (jsonStream.good())
3083 {
3084 json = nlohmann::json::parse(jsonStream, nullptr, false);
3085 if (json.is_discarded())
3086 {
3087 json = nlohmann::json();
3088 }
3089 jsonStream.close();
3090 }
3091
3092 std::string typeName;
3093 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3094 {
3095 typeName = dimmOffset::dimmPower;
3096 }
3097 else
3098 {
3099 typeName = dimmOffset::staticCltt;
3100 }
3101
3102 nlohmann::json& field = json[typeName];
3103
3104 for (const auto& [index, value] : data)
3105 {
3106 field[index] = value;
3107 }
3108
3109 for (nlohmann::json& val : field)
3110 {
3111 if (val == nullptr)
3112 {
3113 val = static_cast<uint8_t>(0);
3114 }
3115 }
3116
3117 std::ofstream output(dimmOffsetFile);
3118 if (!output.good())
3119 {
3120 std::cerr << "Error writing json file\n";
3121 return ipmi::responseResponseError();
3122 }
3123
3124 output << json.dump(4);
3125
3126 if (type == static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3127 {
3128 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
3129
3130 std::variant<std::vector<uint8_t>> offsets =
3131 field.get<std::vector<uint8_t>>();
3132 auto call = bus->new_method_call(
3133 settingsBusName, dimmOffset::offsetPath, PROP_INTF, "Set");
3134 call.append(dimmOffset::offsetInterface, dimmOffset::property, offsets);
3135 try
3136 {
3137 bus->call(call);
3138 }
3139 catch (sdbusplus::exception_t& e)
3140 {
3141 phosphor::logging::log<phosphor::logging::level::ERR>(
3142 "ipmiOEMSetDimmOffset: can't set dimm offsets!",
3143 phosphor::logging::entry("ERR=%s", e.what()));
3144 return ipmi::responseResponseError();
3145 }
3146 }
3147
3148 return ipmi::responseSuccess();
3149}
3150
3151ipmi::RspType<uint8_t> ipmiOEMGetDimmOffset(uint8_t type, uint8_t index)
3152{
3153
3154 if (type != static_cast<uint8_t>(dimmOffsetTypes::dimmPower) &&
3155 type != static_cast<uint8_t>(dimmOffsetTypes::staticCltt))
3156 {
3157 return ipmi::responseInvalidFieldRequest();
3158 }
3159
3160 std::ifstream jsonStream(dimmOffsetFile);
3161
3162 auto json = nlohmann::json::parse(jsonStream, nullptr, false);
3163 if (json.is_discarded())
3164 {
3165 std::cerr << "File error in " << dimmOffsetFile << "\n";
3166 return ipmi::responseResponseError();
3167 }
3168
3169 std::string typeName;
3170 if (type == static_cast<uint8_t>(dimmOffsetTypes::dimmPower))
3171 {
3172 typeName = dimmOffset::dimmPower;
3173 }
3174 else
3175 {
3176 typeName = dimmOffset::staticCltt;
3177 }
3178
3179 auto it = json.find(typeName);
3180 if (it == json.end())
3181 {
3182 return ipmi::responseInvalidFieldRequest();
3183 }
3184
3185 if (it->size() <= index)
3186 {
3187 return ipmi::responseInvalidFieldRequest();
3188 }
3189
3190 uint8_t resp = it->at(index).get<uint8_t>();
3191 return ipmi::responseSuccess(resp);
3192}
3193
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003194namespace boot_options
3195{
3196
3197using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
3198using IpmiValue = uint8_t;
3199constexpr auto ipmiDefault = 0;
3200
3201std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
3202 {0x01, Source::Sources::Network},
3203 {0x02, Source::Sources::Disk},
3204 {0x05, Source::Sources::ExternalMedia},
3205 {0x0f, Source::Sources::RemovableMedia},
3206 {ipmiDefault, Source::Sources::Default}};
3207
3208std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003209 {0x06, Mode::Modes::Setup}, {ipmiDefault, Mode::Modes::Regular}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003210
3211std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
3212 {Source::Sources::Network, 0x01},
3213 {Source::Sources::Disk, 0x02},
3214 {Source::Sources::ExternalMedia, 0x05},
3215 {Source::Sources::RemovableMedia, 0x0f},
3216 {Source::Sources::Default, ipmiDefault}};
3217
3218std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Chen Yugangca12a7b2019-09-03 18:11:44 +08003219 {Mode::Modes::Setup, 0x06}, {Mode::Modes::Regular, ipmiDefault}};
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003220
3221static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
3222static constexpr auto bootSourceIntf =
3223 "xyz.openbmc_project.Control.Boot.Source";
3224static constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
3225static constexpr auto persistentObjPath =
3226 "/xyz/openbmc_project/control/host0/boot";
3227static constexpr auto oneTimePath =
3228 "/xyz/openbmc_project/control/host0/boot/one_time";
3229static constexpr auto bootSourceProp = "BootSource";
3230static constexpr auto bootModeProp = "BootMode";
3231static constexpr auto oneTimeBootEnableProp = "Enabled";
3232static constexpr auto httpBootMode =
3233 "xyz.openbmc_project.Control.Boot.Source.Sources.Http";
3234
3235enum class BootOptionParameter : size_t
3236{
3237 setInProgress = 0x0,
3238 bootFlags = 0x5,
3239};
3240static constexpr uint8_t setComplete = 0x0;
3241static constexpr uint8_t setInProgress = 0x1;
3242static uint8_t transferStatus = setComplete;
3243static constexpr uint8_t setParmVersion = 0x01;
3244static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
3245static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
3246static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
3247static constexpr uint8_t httpBoot = 0xd;
3248static constexpr uint8_t bootSourceMask = 0x3c;
3249
3250} // namespace boot_options
3251
3252ipmi::RspType<uint8_t, // version
3253 uint8_t, // param
3254 uint8_t, // data0, dependent on parameter
3255 std::optional<uint8_t> // data1, dependent on parameter
3256 >
3257 ipmiOemGetEfiBootOptions(uint8_t parameter, uint8_t set, uint8_t block)
3258{
3259 using namespace boot_options;
3260 uint8_t bootOption = 0;
3261
3262 if (parameter == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3263 {
3264 return ipmi::responseSuccess(setParmVersion, parameter, transferStatus,
3265 std::nullopt);
3266 }
3267
3268 if (parameter != static_cast<uint8_t>(BootOptionParameter::bootFlags))
3269 {
3270 phosphor::logging::log<phosphor::logging::level::ERR>(
3271 "Unsupported parameter");
3272 return ipmi::responseResponseError();
3273 }
3274
3275 try
3276 {
3277 auto oneTimeEnabled = false;
3278 // read one time Enabled property
3279 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3280 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3281 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3282 enabledIntf, oneTimeBootEnableProp);
3283 oneTimeEnabled = std::get<bool>(variant);
3284
3285 // get BootSource and BootMode properties
3286 // according to oneTimeEnable
3287 auto bootObjPath = oneTimePath;
3288 if (oneTimeEnabled == false)
3289 {
3290 bootObjPath = persistentObjPath;
3291 }
3292
3293 service = getService(*dbus, bootModeIntf, bootObjPath);
3294 variant = getDbusProperty(*dbus, service, bootObjPath, bootModeIntf,
3295 bootModeProp);
3296
3297 auto bootMode =
3298 Mode::convertModesFromString(std::get<std::string>(variant));
3299
3300 service = getService(*dbus, bootSourceIntf, bootObjPath);
3301 variant = getDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3302 bootSourceProp);
3303
3304 if (std::get<std::string>(variant) == httpBootMode)
3305 {
3306 bootOption = httpBoot;
3307 }
3308 else
3309 {
3310 auto bootSource = Source::convertSourcesFromString(
3311 std::get<std::string>(variant));
3312 bootOption = sourceDbusToIpmi.at(bootSource);
3313 if (Source::Sources::Default == bootSource)
3314 {
3315 bootOption = modeDbusToIpmi.at(bootMode);
3316 }
3317 }
3318
3319 uint8_t oneTime = oneTimeEnabled ? setParmBootFlagsValidOneTime
3320 : setParmBootFlagsValidPermanent;
3321 bootOption <<= 2; // shift for responseconstexpr
3322 return ipmi::responseSuccess(setParmVersion, parameter, oneTime,
3323 bootOption);
3324 }
3325 catch (sdbusplus::exception_t& e)
3326 {
3327 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3328 return ipmi::responseResponseError();
3329 }
3330}
3331
3332ipmi::RspType<> ipmiOemSetEfiBootOptions(uint8_t bootFlag, uint8_t bootParam,
3333 std::optional<uint8_t> bootOption)
3334{
3335 using namespace boot_options;
3336 auto oneTimeEnabled = false;
3337
3338 if (bootFlag == static_cast<uint8_t>(BootOptionParameter::setInProgress))
3339 {
3340 if (bootOption)
3341 {
3342 return ipmi::responseReqDataLenInvalid();
3343 }
3344
3345 if (transferStatus == setInProgress)
3346 {
3347 phosphor::logging::log<phosphor::logging::level::ERR>(
3348 "boot option set in progress!");
3349 return ipmi::responseResponseError();
3350 }
3351
3352 transferStatus = bootParam;
3353 return ipmi::responseSuccess();
3354 }
3355
3356 if (bootFlag != (uint8_t)BootOptionParameter::bootFlags)
3357 {
3358 phosphor::logging::log<phosphor::logging::level::ERR>(
3359 "Unsupported parameter");
3360 return ipmi::responseResponseError();
3361 }
3362
3363 if (!bootOption)
3364 {
3365 return ipmi::responseReqDataLenInvalid();
3366 }
3367
3368 if (((bootOption.value() & bootSourceMask) >> 2) !=
3369 httpBoot) // not http boot, exit
3370 {
3371 phosphor::logging::log<phosphor::logging::level::ERR>(
3372 "wrong boot option parameter!");
3373 return ipmi::responseParmOutOfRange();
3374 }
3375
3376 try
3377 {
3378 bool permanent = (bootParam & setParmBootFlagsPermanent) ==
3379 setParmBootFlagsPermanent;
3380
3381 // read one time Enabled property
3382 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
3383 std::string service = getService(*dbus, enabledIntf, oneTimePath);
3384 Value variant = getDbusProperty(*dbus, service, oneTimePath,
3385 enabledIntf, oneTimeBootEnableProp);
3386 oneTimeEnabled = std::get<bool>(variant);
3387
3388 /*
3389 * Check if the current boot setting is onetime or permanent, if the
3390 * request in the command is otherwise, then set the "Enabled"
3391 * property in one_time object path to 'True' to indicate onetime
3392 * and 'False' to indicate permanent.
3393 *
3394 * Once the onetime/permanent setting is applied, then the bootMode
3395 * and bootSource is updated for the corresponding object.
3396 */
3397 if (permanent == oneTimeEnabled)
3398 {
3399 setDbusProperty(*dbus, service, oneTimePath, enabledIntf,
3400 oneTimeBootEnableProp, !permanent);
3401 }
3402
3403 // set BootSource and BootMode properties
3404 // according to oneTimeEnable or persistent
3405 auto bootObjPath = oneTimePath;
3406 if (oneTimeEnabled == false)
3407 {
3408 bootObjPath = persistentObjPath;
3409 }
3410 std::string bootMode =
3411 "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular";
3412 std::string bootSource = httpBootMode;
3413
3414 service = getService(*dbus, bootModeIntf, bootObjPath);
3415 setDbusProperty(*dbus, service, bootObjPath, bootModeIntf, bootModeProp,
3416 bootMode);
3417
3418 service = getService(*dbus, bootSourceIntf, bootObjPath);
3419 setDbusProperty(*dbus, service, bootObjPath, bootSourceIntf,
3420 bootSourceProp, bootSource);
3421 }
3422 catch (sdbusplus::exception_t& e)
3423 {
3424 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
3425 return ipmi::responseResponseError();
3426 }
3427
3428 return ipmi::responseSuccess();
3429}
3430
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003431using BasicVariantType =
3432 std::variant<std::vector<std::string>, std::vector<uint64_t>, std::string,
3433 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
3434 uint16_t, uint8_t, bool>;
3435using PropertyMapType =
3436 boost::container::flat_map<std::string, BasicVariantType>;
3437static constexpr const std::array<const char*, 1> psuPresenceTypes = {
3438 "xyz.openbmc_project.Configuration.PSUPresence"};
3439int getPSUAddress(ipmi::Context::ptr ctx, uint8_t& bus,
3440 std::vector<uint64_t>& addrTable)
3441{
3442 boost::system::error_code ec;
3443 GetSubTreeType subtree = ctx->bus->yield_method_call<GetSubTreeType>(
3444 ctx->yield, ec, "xyz.openbmc_project.ObjectMapper",
3445 "/xyz/openbmc_project/object_mapper",
3446 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3447 "/xyz/openbmc_project/inventory/system", 3, psuPresenceTypes);
3448 if (ec)
3449 {
3450 phosphor::logging::log<phosphor::logging::level::ERR>(
3451 "Failed to set dbus property to cold redundancy");
3452 return -1;
3453 }
3454 for (const auto& object : subtree)
3455 {
3456 std::string pathName = object.first;
3457 for (const auto& serviceIface : object.second)
3458 {
3459 std::string serviceName = serviceIface.first;
3460
3461 ec.clear();
3462 PropertyMapType propMap =
3463 ctx->bus->yield_method_call<PropertyMapType>(
3464 ctx->yield, ec, serviceName, pathName,
3465 "org.freedesktop.DBus.Properties", "GetAll",
3466 "xyz.openbmc_project.Configuration.PSUPresence");
3467 if (ec)
3468 {
3469 phosphor::logging::log<phosphor::logging::level::ERR>(
3470 "Failed to set dbus property to cold redundancy");
3471 return -1;
3472 }
3473 auto psuBus = std::get_if<uint64_t>(&propMap["Bus"]);
3474 auto psuAddress =
3475 std::get_if<std::vector<uint64_t>>(&propMap["Address"]);
3476
3477 if (psuBus == nullptr || psuAddress == nullptr)
3478 {
3479 std::cerr << "error finding necessary "
3480 "entry in configuration\n";
3481 return -1;
3482 }
3483 bus = static_cast<uint8_t>(*psuBus);
3484 addrTable = *psuAddress;
3485 return 0;
3486 }
3487 }
3488 return -1;
3489}
3490
3491static const constexpr uint8_t addrOffset = 8;
3492static const constexpr uint8_t psuRevision = 0xd9;
3493static const constexpr uint8_t defaultPSUBus = 7;
3494// Second Minor, Primary Minor, Major
3495static const constexpr size_t verLen = 3;
3496ipmi::RspType<std::vector<uint8_t>> ipmiOEMGetPSUVersion(ipmi::Context::ptr ctx)
3497{
3498 uint8_t bus = defaultPSUBus;
3499 std::vector<uint64_t> addrTable;
3500 std::vector<uint8_t> result;
3501 if (getPSUAddress(ctx, bus, addrTable))
3502 {
3503 std::cerr << "Failed to get PSU bus and address\n";
3504 return ipmi::responseResponseError();
3505 }
3506
3507 for (const auto& slaveAddr : addrTable)
3508 {
3509 std::vector<uint8_t> writeData = {psuRevision};
3510 std::vector<uint8_t> readBuf(verLen);
3511 uint8_t addr = static_cast<uint8_t>(slaveAddr) + addrOffset;
3512 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
3513
3514 auto retI2C = ipmi::i2cWriteRead(i2cBus, addr, writeData, readBuf);
3515 if (retI2C != ipmi::ccSuccess)
3516 {
3517 for (size_t idx = 0; idx < verLen; idx++)
3518 {
3519 result.emplace_back(0x00);
3520 }
3521 }
3522 else
3523 {
3524 for (const uint8_t& data : readBuf)
3525 {
3526 result.emplace_back(data);
3527 }
3528 }
3529 }
3530
3531 return ipmi::responseSuccess(result);
3532}
3533
AppaRao Puli28972062019-11-11 02:04:45 +05303534/** @brief implements the maximum size of
3535 * bridgeable messages used between KCS and
3536 * IPMB interfacesget security mode command.
3537 *
3538 * @returns IPMI completion code with following data
3539 * - KCS Buffer Size (In multiples of four bytes)
3540 * - IPMB Buffer Size (In multiples of four bytes)
3541 **/
3542ipmi::RspType<uint8_t, uint8_t> ipmiOEMGetBufferSize()
3543{
3544 // for now this is hard coded; really this number is dependent on
3545 // the BMC kcs driver as well as the host kcs driver....
3546 // we can't know the latter.
3547 uint8_t kcsMaxBufferSize = 63 / 4;
3548 uint8_t ipmbMaxBufferSize = 128 / 4;
3549
3550 return ipmi::responseSuccess(kcsMaxBufferSize, ipmbMaxBufferSize);
3551}
3552
Jason M. Bills64796042018-10-03 16:51:55 -07003553static void registerOEMFunctions(void)
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003554{
3555 phosphor::logging::log<phosphor::logging::level::INFO>(
3556 "Registering OEM commands");
Vernon Mauery98bbf692019-09-16 11:14:59 -07003557 ipmiPrintAndRegister(intel::netFnGeneral,
3558 intel::general::cmdGetChassisIdentifier, NULL,
3559 ipmiOEMGetChassisIdentifier,
3560 PRIVILEGE_USER); // get chassis identifier
3561
3562 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetSystemGUID,
3563 NULL, ipmiOEMSetSystemGUID,
3564 PRIVILEGE_ADMIN); // set system guid
Jason M. Billsb02bf092019-08-15 13:01:56 -07003565
3566 // <Disable BMC System Reset Action>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003567 registerHandler(prioOemBase, intel::netFnGeneral,
3568 intel::general::cmdDisableBMCSystemReset, Privilege::Admin,
3569 ipmiOEMDisableBMCSystemReset);
3570
Jason M. Billsb02bf092019-08-15 13:01:56 -07003571 // <Get BMC Reset Disables>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003572 registerHandler(prioOemBase, intel::netFnGeneral,
3573 intel::general::cmdGetBMCResetDisables, Privilege::Admin,
3574 ipmiOEMGetBMCResetDisables);
Jason M. Billsb02bf092019-08-15 13:01:56 -07003575
Vernon Mauery98bbf692019-09-16 11:14:59 -07003576 ipmiPrintAndRegister(intel::netFnGeneral, intel::general::cmdSetBIOSID,
3577 NULL, ipmiOEMSetBIOSID, PRIVILEGE_ADMIN);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003578
Chen Yugang7a04f3a2019-10-08 11:12:35 +08003579 registerHandler(prioOemBase, intel::netFnGeneral,
3580 intel::general::cmdGetOEMDeviceInfo, Privilege::User,
3581 ipmiOEMGetDeviceInfo);
Jia, Chunhuicc49b542019-03-20 15:41:07 +08003582
Vernon Mauery98bbf692019-09-16 11:14:59 -07003583 ipmiPrintAndRegister(intel::netFnGeneral,
3584 intel::general::cmdGetAICSlotFRUIDSlotPosRecords, NULL,
3585 ipmiOEMGetAICFRU, PRIVILEGE_USER);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303586
Vernon Mauery98bbf692019-09-16 11:14:59 -07003587 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3588 intel::general::cmdSendEmbeddedFWUpdStatus,
3589 Privilege::Operator, ipmiOEMSendEmbeddedFwUpdStatus);
Suryakanth Sekard509eb92018-11-15 17:44:11 +05303590
Rajashekar Gade Reddy2b664d52020-03-23 22:09:00 +05303591 registerHandler(prioOpenBmcBase, intel::netFnApp, intel::app::cmdSlotIpmb,
3592 Privilege::Admin, ipmiOEMSlotIpmb);
3593
Vernon Mauery98bbf692019-09-16 11:14:59 -07003594 ipmiPrintAndRegister(intel::netFnGeneral,
3595 intel::general::cmdSetPowerRestoreDelay, NULL,
3596 ipmiOEMSetPowerRestoreDelay, PRIVILEGE_OPERATOR);
3597
3598 ipmiPrintAndRegister(intel::netFnGeneral,
3599 intel::general::cmdGetPowerRestoreDelay, NULL,
3600 ipmiOEMGetPowerRestoreDelay, PRIVILEGE_USER);
3601
3602 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3603 intel::general::cmdSetOEMUser2Activation,
3604 Privilege::Callback, ipmiOEMSetUser2Activation);
3605
3606 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3607 intel::general::cmdSetSpecialUserPassword,
3608 Privilege::Callback, ipmiOEMSetSpecialUserPassword);
Richard Marian Thomaiyarfc5e9852019-04-14 15:06:27 +05303609
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003610 // <Get Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003611 registerHandler(prioOemBase, intel::netFnGeneral,
3612 intel::general::cmdGetProcessorErrConfig, Privilege::User,
3613 ipmiOEMGetProcessorErrConfig);
3614
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003615 // <Set Processor Error Config>
Vernon Mauery98bbf692019-09-16 11:14:59 -07003616 registerHandler(prioOemBase, intel::netFnGeneral,
3617 intel::general::cmdSetProcessorErrConfig, Privilege::Admin,
3618 ipmiOEMSetProcessorErrConfig);
Jason M. Bills42bd9c82019-06-28 16:39:34 -07003619
Vernon Mauery98bbf692019-09-16 11:14:59 -07003620 ipmiPrintAndRegister(intel::netFnGeneral,
3621 intel::general::cmdSetShutdownPolicy, NULL,
3622 ipmiOEMSetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003623
Vernon Mauery98bbf692019-09-16 11:14:59 -07003624 ipmiPrintAndRegister(intel::netFnGeneral,
3625 intel::general::cmdGetShutdownPolicy, NULL,
3626 ipmiOEMGetShutdownPolicy, PRIVILEGE_ADMIN);
James Feist91244a62019-02-19 15:04:54 -08003627
anil kumar appanaf945eee2019-09-25 23:29:11 +00003628 registerHandler(prioOemBase, intel::netFnGeneral,
3629 intel::general::cmdSetFanConfig, Privilege::User,
3630 ipmiOEMSetFanConfig);
James Feist91244a62019-02-19 15:04:54 -08003631
Vernon Mauery98bbf692019-09-16 11:14:59 -07003632 registerHandler(prioOemBase, intel::netFnGeneral,
3633 intel::general::cmdGetFanConfig, Privilege::User,
3634 ipmiOEMGetFanConfig);
James Feist5f957ca2019-03-14 15:33:55 -07003635
Vernon Mauery98bbf692019-09-16 11:14:59 -07003636 registerHandler(prioOemBase, intel::netFnGeneral,
3637 intel::general::cmdGetFanSpeedOffset, Privilege::User,
3638 ipmiOEMGetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003639
Vernon Mauery98bbf692019-09-16 11:14:59 -07003640 registerHandler(prioOemBase, intel::netFnGeneral,
3641 intel::general::cmdSetFanSpeedOffset, Privilege::User,
3642 ipmiOEMSetFanSpeedOffset);
James Feistacc8a4e2019-04-02 14:23:57 -07003643
Vernon Mauery98bbf692019-09-16 11:14:59 -07003644 registerHandler(prioOemBase, intel::netFnGeneral,
3645 intel::general::cmdSetFscParameter, Privilege::User,
3646 ipmiOEMSetFscParameter);
James Feist5f957ca2019-03-14 15:33:55 -07003647
Vernon Mauery98bbf692019-09-16 11:14:59 -07003648 registerHandler(prioOemBase, intel::netFnGeneral,
3649 intel::general::cmdGetFscParameter, Privilege::User,
3650 ipmiOEMGetFscParameter);
Richard Marian Thomaiyarea537d52019-04-24 21:33:48 +05303651
Vernon Mauery98bbf692019-09-16 11:14:59 -07003652 registerHandler(prioOpenBmcBase, intel::netFnGeneral,
3653 intel::general::cmdReadBaseBoardProductId, Privilege::Admin,
3654 ipmiOEMReadBoardProductId);
Chen Yugang39736d52019-07-12 16:24:33 +08003655
Vernon Mauery98bbf692019-09-16 11:14:59 -07003656 registerHandler(prioOemBase, intel::netFnGeneral,
3657 intel::general::cmdGetNmiStatus, Privilege::User,
3658 ipmiOEMGetNmiSource);
Chen Yugang39736d52019-07-12 16:24:33 +08003659
Vernon Mauery98bbf692019-09-16 11:14:59 -07003660 registerHandler(prioOemBase, intel::netFnGeneral,
3661 intel::general::cmdSetNmiStatus, Privilege::Operator,
3662 ipmiOEMSetNmiSource);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003663
Vernon Mauery98bbf692019-09-16 11:14:59 -07003664 registerHandler(prioOemBase, intel::netFnGeneral,
3665 intel::general::cmdGetEfiBootOptions, Privilege::User,
3666 ipmiOemGetEfiBootOptions);
Chen,Yugang4f7e76b2019-08-20 09:28:06 +08003667
Vernon Mauery98bbf692019-09-16 11:14:59 -07003668 registerHandler(prioOemBase, intel::netFnGeneral,
3669 intel::general::cmdSetEfiBootOptions, Privilege::Operator,
3670 ipmiOemSetEfiBootOptions);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303671
Vernon Mauery98bbf692019-09-16 11:14:59 -07003672 registerHandler(prioOemBase, intel::netFnGeneral,
3673 intel::general::cmdGetSecurityMode, Privilege::User,
3674 ipmiGetSecurityMode);
Richard Marian Thomaiyard801e462019-06-20 01:05:40 +05303675
Vernon Mauery98bbf692019-09-16 11:14:59 -07003676 registerHandler(prioOemBase, intel::netFnGeneral,
3677 intel::general::cmdSetSecurityMode, Privilege::Admin,
3678 ipmiSetSecurityMode);
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003679
NITIN SHARMAabd11ca2019-06-12 12:31:42 +00003680 registerHandler(prioOemBase, intel::netFnGeneral,
3681 intel::general::cmdGetLEDStatus, Privilege::Admin,
3682 ipmiOEMGetLEDStatus);
Cheng C Yang773703a2019-08-15 09:41:11 +08003683
Vernon Mauery98bbf692019-09-16 11:14:59 -07003684 ipmiPrintAndRegister(ipmi::intel::netFnPlatform,
3685 ipmi::intel::platform::cmdCfgHostSerialPortSpeed, NULL,
3686 ipmiOEMCfgHostSerialPortSpeed, PRIVILEGE_ADMIN);
3687
3688 registerHandler(prioOemBase, intel::netFnGeneral,
3689 intel::general::cmdSetFaultIndication, Privilege::Operator,
3690 ipmiOEMSetFaultIndication);
3691
3692 registerHandler(prioOemBase, intel::netFnGeneral,
3693 intel::general::cmdSetColdRedundancyConfig, Privilege::User,
3694 ipmiOEMSetCRConfig);
3695
3696 registerHandler(prioOemBase, intel::netFnGeneral,
3697 intel::general::cmdGetColdRedundancyConfig, Privilege::User,
3698 ipmiOEMGetCRConfig);
3699
3700 registerHandler(prioOemBase, intel::netFnGeneral,
3701 intel::general::cmdRestoreConfiguration, Privilege::Admin,
Vernon Mauery4ac799d2019-05-20 15:50:37 -07003702 ipmiRestoreConfiguration);
James Feist63efafa2019-07-24 12:39:21 -07003703
Vernon Mauery98bbf692019-09-16 11:14:59 -07003704 registerHandler(prioOemBase, intel::netFnGeneral,
3705 intel::general::cmdSetDimmOffset, Privilege::Operator,
3706 ipmiOEMSetDimmOffset);
James Feist63efafa2019-07-24 12:39:21 -07003707
Vernon Mauery98bbf692019-09-16 11:14:59 -07003708 registerHandler(prioOemBase, intel::netFnGeneral,
3709 intel::general::cmdGetDimmOffset, Privilege::Operator,
3710 ipmiOEMGetDimmOffset);
Chen Yugangca12a7b2019-09-03 18:11:44 +08003711
Cheng C Yang4e6ee152019-09-25 10:27:44 +08003712 registerHandler(prioOemBase, intel::netFnGeneral,
3713 intel::general::cmdGetPSUVersion, Privilege::User,
3714 ipmiOEMGetPSUVersion);
AppaRao Puli28972062019-11-11 02:04:45 +05303715
3716 registerHandler(prioOemBase, intel::netFnGeneral,
3717 intel::general::cmdGetBufferSize, Privilege::User,
3718 ipmiOEMGetBufferSize);
Jia, Chunhuia835eaa2018-09-05 09:00:41 +08003719}
3720
3721} // namespace ipmi