blob: 4a3252a35e7b6dabcc87571251774425e66b9eac [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "chassishandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Patrick Venture0b02be92018-08-31 11:55:55 -07005#include <arpa/inet.h>
6#include <endian.h>
7#include <limits.h>
8#include <mapper.h>
9#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070010
Ratan Guptafd28dd72016-08-01 04:58:01 -050011#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070012#include <chrono>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070013#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080014#include <filesystem>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050015#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053016#include <future>
Vernon Mauerye08fbff2019-04-03 09:19:34 -070017#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070018#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070019#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070020#include <map>
21#include <phosphor-logging/elog-errors.hpp>
22#include <phosphor-logging/log.hpp>
23#include <sdbusplus/bus.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070024#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070025#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070026#include <sdbusplus/timer.hpp>
Vernon Mauerye278ead2018-10-09 09:23:43 -070027#include <settings.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070028#include <sstream>
Patrick Venture3a5071a2018-09-12 13:27:42 -070029#include <string>
30#include <xyz/openbmc_project/Common/error.hpp>
31#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
32#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
33#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
34#include <xyz/openbmc_project/State/Host/server.hpp>
35#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
36
Lei YU4b0ddb62019-01-25 16:43:50 +080037std::unique_ptr<phosphor::Timer> identifyTimer
38 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050039
Yong Lif4e38512019-05-21 14:46:55 +080040static ChassisIDState chassisIDState = ChassisIDState::reserved;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000041static constexpr uint8_t setParmVersion = 0x01;
42static constexpr uint8_t setParmBootFlagsPermanent = 0x40;
43static constexpr uint8_t setParmBootFlagsValidOneTime = 0x80;
44static constexpr uint8_t setParmBootFlagsValidPermanent = 0xC0;
Yong Lif4e38512019-05-21 14:46:55 +080045
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000046constexpr size_t sizeVersion = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053047constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053048
Patrick Venture0b02be92018-08-31 11:55:55 -070049// PetiBoot-Specific
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000050static constexpr uint8_t netConfInitialBytes[] = {0x80, 0x21, 0x70, 0x62,
51 0x21, 0x00, 0x01, 0x06};
52static constexpr uint8_t oemParmStart = 96;
53static constexpr uint8_t oemParmEnd = 127;
Ratan Guptafd28dd72016-08-01 04:58:01 -050054
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000055static constexpr size_t cookieOffset = 1;
56static constexpr size_t versionOffset = 5;
57static constexpr size_t addrSizeOffset = 8;
58static constexpr size_t macOffset = 9;
59static constexpr size_t addrTypeOffset = 16;
60static constexpr size_t ipAddrOffset = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050061
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050062static constexpr size_t encIdentifyObjectsSize = 1;
63static constexpr size_t chassisIdentifyReqLength = 2;
64static constexpr size_t identifyIntervalPos = 0;
65static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080066
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000067namespace ipmi
68{
69constexpr Cc ccParmNotSupported = 0x80;
70
71static inline auto responseParmNotSupported()
72{
73 return response(ccParmNotSupported);
74}
75} // namespace ipmi
76
Adriana Kobylak40814c62015-10-27 15:58:44 -050077void register_netfn_chassis_functions() __attribute__((constructor));
78
shgoupfd84fbbf2015-12-17 10:05:51 +080079// Host settings in dbus
80// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070081const char* settings_object_name = "/org/openbmc/settings/host0";
82const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070083const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053084 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080085
Ratan Guptadcb10672017-07-10 10:33:50 +053086constexpr auto SETTINGS_ROOT = "/";
87constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053088
89constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
90constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
91
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050092static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
93static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070094 "xyz.openbmc_project.State.PowerOnHours";
Patrick Williams7345ac42021-04-30 20:57:39 -050095static constexpr auto pohCounterProperty = "POHCounter";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050096static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080097const static constexpr char chassisCapIntf[] =
98 "xyz.openbmc_project.Control.ChassisCapabilities";
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -080099const static constexpr char chassisIntrusionProp[] = "ChassisIntrusionEnabled";
100const static constexpr char chassisFrontPanelLockoutProp[] =
101 "ChassisFrontPanelLockoutEnabled";
102const static constexpr char chassisNMIProp[] = "ChassisNMIEnabled";
103const static constexpr char chassisPowerInterlockProp[] =
104 "ChassisPowerInterlockEnabled";
Yong Liae4b0402018-11-02 11:12:14 +0800105const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
106const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
107const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
108const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
109const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
110static constexpr uint8_t chassisCapFlagMask = 0x0f;
111static constexpr uint8_t chassisCapAddrMask = 0xfe;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700112static constexpr const char* powerButtonIntf =
113 "xyz.openbmc_project.Chassis.Buttons.Power";
114static constexpr const char* powerButtonPath =
115 "/xyz/openbmc_project/Chassis/Buttons/Power0";
116static constexpr const char* resetButtonIntf =
117 "xyz.openbmc_project.Chassis.Buttons.Reset";
118static constexpr const char* resetButtonPath =
119 "/xyz/openbmc_project/Chassis/Buttons/Reset0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530120
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530121// Phosphor Host State manager
122namespace State = sdbusplus::xyz::openbmc_project::State::server;
123
Vernon Mauery185b9f82018-07-20 10:52:36 -0700124namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500125
Ratan Guptadcb10672017-07-10 10:33:50 +0530126using namespace phosphor::logging;
127using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500128using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700129
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500130namespace chassis
131{
132namespace internal
133{
134
135constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
136constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500137constexpr auto powerRestoreIntf =
138 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500139sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
140
141namespace cache
142{
143
James Feist225dec82019-11-26 16:25:06 -0800144std::unique_ptr<settings::Objects> objectsPtr = nullptr;
145
146settings::Objects& getObjects()
147{
148 if (objectsPtr == nullptr)
149 {
150 objectsPtr = std::make_unique<settings::Objects>(
151 dbus, std::vector<std::string>{bootModeIntf, bootSourceIntf,
152 powerRestoreIntf});
153 }
154 return *objectsPtr;
155}
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500156
157} // namespace cache
158} // namespace internal
159} // namespace chassis
160
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500161namespace poh
162{
163
164constexpr auto minutesPerCount = 60;
165
166} // namespace poh
167
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000168int getHostNetworkData(ipmi::message::Payload& payload)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500169{
Ratan Guptadcb10672017-07-10 10:33:50 +0530170 ipmi::PropertyMap properties;
171 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530172 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500173
Ratan Guptadcb10672017-07-10 10:33:50 +0530174 try
175 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700176 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530177 // objects,to handle such cases we are interested on that object
178 // which are on interested busname.
179 // Currenlty mapper doesn't give the readable busname(gives busid)
180 // so we can't match with bus name so giving some object specific info
181 // as SETTINGS_MATCH.
182 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500183
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530184 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530185
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530186 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
187 SETTINGS_ROOT, SETTINGS_MATCH);
188
189 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
190 SETTINGS_ROOT, SETTINGS_MATCH);
191
Patrick Venture0b02be92018-08-31 11:55:55 -0700192 properties = ipmi::getAllDbusProperties(
193 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
194 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
195 macObjectInfo.first, MAC_INTERFACE,
196 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530197
Vernon Maueryf442e112019-04-09 11:44:36 -0700198 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530199
Vernon Maueryf442e112019-04-09 11:44:36 -0700200 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530201
Vernon Maueryf442e112019-04-09 11:44:36 -0700202 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530203
Patrick Venture0b02be92018-08-31 11:55:55 -0700204 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700205 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700206 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
207 ? 1
208 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530209
Vernon Maueryf442e112019-04-09 11:44:36 -0700210 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530211
Ratan Guptad70f4532017-08-04 02:07:31 +0530212 // it is expected here that we should get the valid data
213 // but we may also get the default values.
214 // Validation of the data is done by settings.
215 //
216 // if mac address is default mac address then
217 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530218 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530219 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530220 rc = -1;
221 return rc;
222 }
223 // if addr is static then ipaddress,gateway,prefix
224 // should not be default one,don't send blank override.
225 if (isStatic)
226 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700227 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
228 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530229 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530230 rc = -1;
231 return rc;
232 }
233 }
234
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000235 std::string token;
236 std::stringstream ss(MACAddress);
Ratan Guptadcb10672017-07-10 10:33:50 +0530237
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000238 // First pack macOffset no of bytes in payload.
239 // Latter this PetiBoot-Specific data will be populated.
240 std::vector<uint8_t> payloadInitialBytes(macOffset);
241 payload.pack(payloadInitialBytes);
Ratan Guptadcb10672017-07-10 10:33:50 +0530242
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000243 while (std::getline(ss, token, ':'))
244 {
245 payload.pack(stoi(token, nullptr, 16));
246 }
247
248 payload.pack(0x00);
249
250 payload.pack(isStatic);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530251
Vernon Maueryf442e112019-04-09 11:44:36 -0700252 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
253 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
254 ? AF_INET
255 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530256
Patrick Venture0b02be92018-08-31 11:55:55 -0700257 addrSize = (addressFamily == AF_INET)
258 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
259 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530260
261 // ipaddress and gateway would be in IPv4 format
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000262 std::vector<uint8_t> addrInBinary(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530263 inet_pton(addressFamily, ipAddress.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000264 reinterpret_cast<void*>(addrInBinary.data()));
Ratan Guptadcb10672017-07-10 10:33:50 +0530265
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000266 payload.pack(addrInBinary);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530267
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000268 payload.pack(prefix);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530269
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000270 std::vector<uint8_t> gatewayDetails(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530271 inet_pton(addressFamily, gateway.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000272 reinterpret_cast<void*>(gatewayDetails.data()));
273 payload.pack(gatewayDetails);
Ratan Guptadcb10672017-07-10 10:33:50 +0530274 }
275 catch (InternalFailure& e)
276 {
277 commit<InternalFailure>();
Ratan Guptadcb10672017-07-10 10:33:50 +0530278 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500279 return rc;
280 }
281
Patrick Venture0b02be92018-08-31 11:55:55 -0700282 // PetiBoot-Specific
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000283 // If success then copy the first 9 bytes to the payload message
284 // payload first 2 bytes contain the parameter values. Skip that 2 bytes.
285 uint8_t skipFirstTwoBytes = 2;
286 size_t payloadSize = payload.size();
287 uint8_t* configDataStartingAddress = payload.data() + skipFirstTwoBytes;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500288
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000289 if (payloadSize < skipFirstTwoBytes + sizeof(netConfInitialBytes))
290 {
291 log<level::ERR>("Invalid net config ");
292 rc = -1;
293 return rc;
294 }
295 std::copy(netConfInitialBytes,
296 netConfInitialBytes + sizeof(netConfInitialBytes),
297 configDataStartingAddress);
298
299 if (payloadSize < skipFirstTwoBytes + addrSizeOffset + sizeof(addrSize))
300 {
301 log<level::ERR>("Invalid length of address size");
302 rc = -1;
303 return rc;
304 }
305 std::copy(&addrSize, &(addrSize) + sizeof(addrSize),
306 configDataStartingAddress + addrSizeOffset);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530307
Ratan Guptafd28dd72016-08-01 04:58:01 -0500308#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700309 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500310
Ratan Guptadcb10672017-07-10 10:33:50 +0530311 for (uint8_t pos = 0; pos < index; pos++)
312 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000313 std::printf("%02x ", payloadStartingAddress[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530314 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500315#endif
316
Ratan Guptafd28dd72016-08-01 04:58:01 -0500317 return rc;
318}
319
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530320/** @brief convert IPv4 and IPv6 addresses from binary to text form.
321 * @param[in] family - IPv4/Ipv6
322 * @param[in] data - req data pointer.
323 * @param[in] offset - offset in the data.
324 * @param[in] addrSize - size of the data which needs to be read from offset.
325 * @returns address in text form.
326 */
327
Patrick Venture0b02be92018-08-31 11:55:55 -0700328std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
329 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530330{
331 char ipAddr[INET6_ADDRSTRLEN] = {};
332
Patrick Venture0b02be92018-08-31 11:55:55 -0700333 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530334 {
335 case AF_INET:
336 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700337 struct sockaddr_in addr4
338 {
339 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700340 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530341
Patrick Venture0b02be92018-08-31 11:55:55 -0700342 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530343
344 break;
345 }
346 case AF_INET6:
347 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700348 struct sockaddr_in6 addr6
349 {
350 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700351 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530352
Patrick Venture0b02be92018-08-31 11:55:55 -0700353 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530354
355 break;
356 }
357 default:
358 {
359 return {};
360 }
361 }
362
363 return ipAddr;
364}
365
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000366ipmi::Cc setHostNetworkData(ipmi::message::Payload& data)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500367{
Ratan Guptadcb10672017-07-10 10:33:50 +0530368 using namespace std::string_literals;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000369 std::string hostNetworkConfig;
370 std::string mac("00:00:00:00:00:00");
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530371 std::string ipAddress, gateway;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000372 std::string addrOrigin{0};
Patrick Venture0b02be92018-08-31 11:55:55 -0700373 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530374 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530375 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
377 uint8_t prefix{0};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530378 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500379
Patrick Venture0b02be92018-08-31 11:55:55 -0700380 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500381 // version starts from sixth byte
382
Ratan Guptadcb10672017-07-10 10:33:50 +0530383 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500384 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530385 do
386 {
387 // cookie == 0x21 0x70 0x62 0x21
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000388 data.trailingOk = true;
389 auto msgLen = data.size();
390 std::vector<uint8_t> msgPayloadBytes(msgLen);
391 if (data.unpack(msgPayloadBytes) != 0 || !data.fullyUnpacked())
Ratan Guptadcb10672017-07-10 10:33:50 +0530392 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000393 log<level::ERR>(
394 "Error in unpacking message of setHostNetworkData");
395 return ipmi::ccReqDataLenInvalid;
396 }
397
398 uint8_t* msgPayloadStartingPos = msgPayloadBytes.data();
399 constexpr size_t cookieSize = 4;
400 if (msgLen < cookieOffset + cookieSize)
401 {
402 log<level::ERR>(
403 "Error in cookie getting of setHostNetworkData");
404 return ipmi::ccReqDataLenInvalid;
405 }
406 if (std::equal(msgPayloadStartingPos + cookieOffset,
407 msgPayloadStartingPos + cookieOffset + cookieSize,
408 (netConfInitialBytes + cookieOffset)) != 0)
409 {
410 // all cookie == 0
411 if (std::all_of(msgPayloadStartingPos + cookieOffset,
412 msgPayloadStartingPos + cookieOffset +
413 cookieSize,
414 [](int i) { return i == 0; }) == true)
Ratan Guptadcb10672017-07-10 10:33:50 +0530415 {
416 // need to zero out the network settings.
417 break;
418 }
419
420 log<level::ERR>("Invalid Cookie");
421 elog<InternalFailure>();
422 }
423
424 // vesion == 0x00 0x01
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000425 if (msgLen < versionOffset + sizeVersion)
Ratan Guptadcb10672017-07-10 10:33:50 +0530426 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000427 log<level::ERR>(
428 "Error in version getting of setHostNetworkData");
429 return ipmi::ccReqDataLenInvalid;
430 }
431 if (std::equal(msgPayloadStartingPos + versionOffset,
432 msgPayloadStartingPos + versionOffset + sizeVersion,
433 (netConfInitialBytes + versionOffset)) != 0)
434 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530435 log<level::ERR>("Invalid Version");
436 elog<InternalFailure>();
437 }
438
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000439 if (msgLen < macOffset + 6)
440 {
441 log<level::ERR>(
442 "Error in mac address getting of setHostNetworkData");
443 return ipmi::ccReqDataLenInvalid;
444 }
445 std::stringstream result;
446 std::copy((msgPayloadStartingPos + macOffset),
447 (msgPayloadStartingPos + macOffset + 5),
448 std::ostream_iterator<int>(result, ":"));
449 mac = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530450
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000451 if (msgLen < addrTypeOffset + sizeof(decltype(addrOrigin)))
452 {
453 log<level::ERR>(
454 "Error in original address getting of setHostNetworkData");
455 return ipmi::ccReqDataLenInvalid;
456 }
457 std::copy(msgPayloadStartingPos + addrTypeOffset,
458 msgPayloadStartingPos + addrTypeOffset +
459 sizeof(decltype(addrOrigin)),
460 std::ostream_iterator<int>(result, ""));
461 addrOrigin = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530462
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000463 if (!addrOrigin.empty())
Ratan Guptadcb10672017-07-10 10:33:50 +0530464 {
465 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530466 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530467 }
468
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000469 if (msgLen < addrSizeOffset + sizeof(decltype(addrSize)))
470 {
471 log<level::ERR>(
472 "Error in address size getting of setHostNetworkData");
473 return ipmi::ccReqDataLenInvalid;
474 }
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530475 // Get the address size
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000476 std::copy(msgPayloadStartingPos + addrSizeOffset,
477 (msgPayloadStartingPos + addrSizeOffset +
478 sizeof(decltype(addrSize))),
479 &addrSize);
Ratan Guptadcb10672017-07-10 10:33:50 +0530480
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000481 uint8_t prefixOffset = ipAddrOffset + addrSize;
482 if (msgLen < prefixOffset + sizeof(decltype(prefix)))
483 {
484 log<level::ERR>(
485 "Error in prefix getting of setHostNetworkData");
486 return ipmi::ccReqDataLenInvalid;
487 }
488 std::copy(msgPayloadStartingPos + prefixOffset,
489 (msgPayloadStartingPos + prefixOffset +
490 sizeof(decltype(prefix))),
491 &prefix);
Ratan Guptadcb10672017-07-10 10:33:50 +0530492
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530493 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
Ratan Gupta8c31d232017-08-13 05:49:43 +0530494 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530495 {
496 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
497 family = AF_INET6;
498 }
499
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000500 if (msgLen < ipAddrOffset + addrSize)
501 {
502 log<level::ERR>(
503 "Error in IP address getting of setHostNetworkData");
504 return ipmi::ccReqDataLenInvalid;
505 }
506 ipAddress = getAddrStr(family, msgPayloadStartingPos, ipAddrOffset,
507 addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530508
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000509 if (msgLen < gatewayOffset + addrSize)
510 {
511 log<level::ERR>(
512 "Error in gateway address getting of setHostNetworkData");
513 return ipmi::ccReqDataLenInvalid;
514 }
515 gateway = getAddrStr(family, msgPayloadStartingPos, gatewayOffset,
516 addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530517
Patrick Venture0b02be92018-08-31 11:55:55 -0700518 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530519
Patrick Venture0b02be92018-08-31 11:55:55 -0700520 // Cookie == 0 or it is a valid cookie
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000521 hostNetworkConfig += "ipaddress="s + ipAddress + ",prefix="s +
522 std::to_string(prefix) + ",gateway="s + gateway +
523 ",mac="s + mac + ",addressOrigin="s +
524 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500525
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530526 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
527
528 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
529 SETTINGS_ROOT, SETTINGS_MATCH);
530 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
531 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530532 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530533 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700534 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530535 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700536 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530537 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700538 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530539 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700540 IP_INTERFACE, "Gateway", std::string(gateway));
541 ipmi::setDbusProperty(
542 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
543 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530544 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700545 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500546
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000547 log<level::DEBUG>("Network configuration changed",
548 entry("NETWORKCONFIG=%s", hostNetworkConfig.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500549 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000550 catch (sdbusplus::exception_t& e)
Ratan Guptadcb10672017-07-10 10:33:50 +0530551 {
552 commit<InternalFailure>();
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000553 log<level::ERR>("Error in ipmiChassisSetSysBootOptions call");
554 return ipmi::ccUnspecifiedError;
Ratan Guptadcb10672017-07-10 10:33:50 +0530555 }
556
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000557 return ipmi::ccSuccess;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500558}
559
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500560uint32_t getPOHCounter()
561{
562 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
563
Patrick Venture0b02be92018-08-31 11:55:55 -0700564 auto chassisStateObj =
565 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500566
Patrick Venture0b02be92018-08-31 11:55:55 -0700567 auto service =
568 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500569
Patrick Venture0b02be92018-08-31 11:55:55 -0700570 auto propValue =
571 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
Patrick Williams7345ac42021-04-30 20:57:39 -0500572 chassisPOHStateIntf, pohCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500573
Vernon Maueryf442e112019-04-09 11:44:36 -0700574 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500575}
576
anil kumar appana43263c62019-05-27 12:45:04 +0000577/** @brief Implements the get chassis capabilities command
578 *
579 * @returns IPMI completion code plus response data
580 * chassisCapFlags - chassis capability flag
581 * chassisFRUInfoDevAddr - chassis FRU info Device Address
582 * chassisSDRDevAddr - chassis SDR device address
583 * chassisSELDevAddr - chassis SEL device address
584 * chassisSMDevAddr - chassis system management device address
585 * chassisBridgeDevAddr - chassis bridge device address
586 */
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800587ipmi::RspType<bool, // chassis intrusion sensor
588 bool, // chassis Front panel lockout
589 bool, // chassis NMI
590 bool, // chassis power interlock
591 uint4_t, // reserved
anil kumar appana43263c62019-05-27 12:45:04 +0000592 uint8_t, // chassis FRU info Device Address
593 uint8_t, // chassis SDR device address
594 uint8_t, // chassis SEL device address
595 uint8_t, // chassis system management device address
596 uint8_t // chassis bridge device address
597 >
598 ipmiGetChassisCap()
Nan Li8d15fb42016-08-16 22:29:40 +0800599{
anil kumar appana43263c62019-05-27 12:45:04 +0000600 ipmi::PropertyMap properties;
Yong Liae4b0402018-11-02 11:12:14 +0800601 try
602 {
603 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800604
Yong Liae4b0402018-11-02 11:12:14 +0800605 ipmi::DbusObjectInfo chassisCapObject =
606 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800607
Yong Liae4b0402018-11-02 11:12:14 +0800608 // capabilities flags
609 // [7..4] - reserved
610 // [3] – 1b = provides power interlock (IPM 1.5)
611 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
612 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
613 // has capabilities
614 // to lock out external power control and reset button or
615 // front panel interfaces and/or detect tampering with those
616 // interfaces).
617 // [0] -1b = Chassis provides intrusion (physical security) sensor.
618 // set to default value 0x0.
Nan Li8d15fb42016-08-16 22:29:40 +0800619
anil kumar appana43263c62019-05-27 12:45:04 +0000620 properties =
621 ipmi::getAllDbusProperties(bus, chassisCapObject.second,
622 chassisCapObject.first, chassisCapIntf);
Yong Liae4b0402018-11-02 11:12:14 +0800623 }
624 catch (std::exception& e)
625 {
anil kumar appana43263c62019-05-27 12:45:04 +0000626 log<level::ERR>("Failed to fetch Chassis Capability properties",
627 entry("ERROR=%s", e.what()));
628 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800629 }
630
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800631 bool* chassisIntrusionFlag =
632 std::get_if<bool>(&properties[chassisIntrusionProp]);
633 if (chassisIntrusionFlag == nullptr)
anil kumar appana43263c62019-05-27 12:45:04 +0000634 {
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800635 log<level::ERR>("Error to get chassis Intrusion flags");
636 return ipmi::responseUnspecifiedError();
637 }
638 bool* chassisFrontPanelFlag =
639 std::get_if<bool>(&properties[chassisFrontPanelLockoutProp]);
640 if (chassisFrontPanelFlag == nullptr)
641 {
642 log<level::ERR>("Error to get chassis intrusion flags");
643 return ipmi::responseUnspecifiedError();
644 }
645 bool* chassisNMIFlag = std::get_if<bool>(&properties[chassisNMIProp]);
646 if (chassisNMIFlag == nullptr)
647 {
648 log<level::ERR>("Error to get chassis NMI flags");
649 return ipmi::responseUnspecifiedError();
650 }
651 bool* chassisPowerInterlockFlag =
652 std::get_if<bool>(&properties[chassisPowerInterlockProp]);
653 if (chassisPowerInterlockFlag == nullptr)
654 {
655 log<level::ERR>("Error to get chassis power interlock flags");
anil kumar appana43263c62019-05-27 12:45:04 +0000656 return ipmi::responseUnspecifiedError();
657 }
658 uint8_t* chassisFRUInfoDevAddr =
659 std::get_if<uint8_t>(&properties[chassisFRUDevAddrProp]);
660 if (chassisFRUInfoDevAddr == nullptr)
661 {
662 log<level::ERR>("Error to get chassis FRU info device address");
663 return ipmi::responseUnspecifiedError();
664 }
665 uint8_t* chassisSDRDevAddr =
666 std::get_if<uint8_t>(&properties[chassisSDRDevAddrProp]);
667 if (chassisSDRDevAddr == nullptr)
668 {
669 log<level::ERR>("Error to get chassis SDR device address");
670 return ipmi::responseUnspecifiedError();
671 }
672 uint8_t* chassisSELDevAddr =
673 std::get_if<uint8_t>(&properties[chassisSELDevAddrProp]);
674 if (chassisSELDevAddr == nullptr)
675 {
676 log<level::ERR>("Error to get chassis SEL device address");
677 return ipmi::responseUnspecifiedError();
678 }
679 uint8_t* chassisSMDevAddr =
680 std::get_if<uint8_t>(&properties[chassisSMDevAddrProp]);
681 if (chassisSMDevAddr == nullptr)
682 {
683 log<level::ERR>("Error to get chassis SM device address");
684 return ipmi::responseUnspecifiedError();
685 }
686 uint8_t* chassisBridgeDevAddr =
687 std::get_if<uint8_t>(&properties[chassisBridgeDevAddrProp]);
688 if (chassisBridgeDevAddr == nullptr)
689 {
690 log<level::ERR>("Error to get chassis bridge device address");
691 return ipmi::responseUnspecifiedError();
692 }
693
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800694 return ipmi::responseSuccess(*chassisIntrusionFlag, *chassisFrontPanelFlag,
695 *chassisNMIFlag, *chassisPowerInterlockFlag, 0,
696 *chassisFRUInfoDevAddr, *chassisSDRDevAddr,
697 *chassisSELDevAddr, *chassisSMDevAddr,
698 *chassisBridgeDevAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800699}
700
anil kumar appana894d0222019-05-27 16:32:14 +0000701/** @brief implements set chassis capalibities command
702 * @param intrusion - chassis intrusion
703 * @param fpLockout - frontpannel lockout
704 * @param reserved1 - skip one bit
705 * @param fruDeviceAddr - chassis FRU info Device Address
706 * @param sdrDeviceAddr - chassis SDR device address
707 * @param selDeviceAddr - chassis SEL device address
708 * @param smDeviceAddr - chassis system management device address
709 * @param bridgeDeviceAddr - chassis bridge device address
710 *
711 * @returns IPMI completion code
712 */
713ipmi::RspType<> ipmiSetChassisCap(bool intrusion, bool fpLockout,
714 uint6_t reserved1,
715
716 uint8_t fruDeviceAddr,
717
718 uint8_t sdrDeviceAddr,
719
720 uint8_t selDeviceAddr,
721
722 uint8_t smDeviceAddr,
723
724 uint8_t bridgeDeviceAddr)
Yong Liae4b0402018-11-02 11:12:14 +0800725{
Yong Liae4b0402018-11-02 11:12:14 +0800726
727 // check input data
anil kumar appana894d0222019-05-27 16:32:14 +0000728 if (reserved1 != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800729 {
anil kumar appana894d0222019-05-27 16:32:14 +0000730 log<level::ERR>("Unsupported request parameter");
731 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800732 }
733
anil kumar appana894d0222019-05-27 16:32:14 +0000734 if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800735 {
736 log<level::ERR>("Unsupported request parameter(FRU Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000737 entry("REQ=0x%x", fruDeviceAddr));
738 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800739 }
anil kumar appana894d0222019-05-27 16:32:14 +0000740 if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800741 {
742 log<level::ERR>("Unsupported request parameter(SDR Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000743 entry("REQ=0x%x", sdrDeviceAddr));
744 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800745 }
746
anil kumar appana894d0222019-05-27 16:32:14 +0000747 if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800748 {
749 log<level::ERR>("Unsupported request parameter(SEL Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000750 entry("REQ=0x%x", selDeviceAddr));
751 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800752 }
753
anil kumar appana894d0222019-05-27 16:32:14 +0000754 if ((smDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800755 {
anil kumar appana894d0222019-05-27 16:32:14 +0000756 log<level::ERR>("Unsupported request parameter(SM Addr)",
757 entry("REQ=0x%x", smDeviceAddr));
758 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800759 }
760
anil kumar appana894d0222019-05-27 16:32:14 +0000761 if ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800762 {
763 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000764 entry("REQ=0x%x", bridgeDeviceAddr));
765 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800766 }
767
768 try
769 {
770 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
771 ipmi::DbusObjectInfo chassisCapObject =
772 ipmi::getDbusObject(bus, chassisCapIntf);
773
774 ipmi::setDbusProperty(bus, chassisCapObject.second,
775 chassisCapObject.first, chassisCapIntf,
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800776 chassisIntrusionProp, intrusion);
777
778 ipmi::setDbusProperty(bus, chassisCapObject.second,
779 chassisCapObject.first, chassisCapIntf,
780 chassisFrontPanelLockoutProp, fpLockout);
Yong Liae4b0402018-11-02 11:12:14 +0800781
782 ipmi::setDbusProperty(bus, chassisCapObject.second,
783 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000784 chassisFRUDevAddrProp, fruDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800785
786 ipmi::setDbusProperty(bus, chassisCapObject.second,
787 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000788 chassisSDRDevAddrProp, sdrDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800789
790 ipmi::setDbusProperty(bus, chassisCapObject.second,
791 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000792 chassisSELDevAddrProp, selDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800793
794 ipmi::setDbusProperty(bus, chassisCapObject.second,
795 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000796 chassisSMDevAddrProp, smDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800797
798 ipmi::setDbusProperty(bus, chassisCapObject.second,
799 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000800 chassisBridgeDevAddrProp, bridgeDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800801 }
802 catch (std::exception& e)
803 {
804 log<level::ERR>(e.what());
anil kumar appana894d0222019-05-27 16:32:14 +0000805 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800806 }
anil kumar appana894d0222019-05-27 16:32:14 +0000807 return ipmi::responseSuccess();
Nan Li8d15fb42016-08-16 22:29:40 +0800808}
809
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530810//------------------------------------------
811// Calls into Host State Manager Dbus object
812//------------------------------------------
813int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600814{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500815 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700816 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500817 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700818 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
819 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530820
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500821 // sd_bus error
822 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700823 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600824
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500825 // SD Bus error report mechanism.
826 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600827
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500828 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700829 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500830 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
831 if (rc < 0)
832 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700833 log<level::ERR>(
834 "Failed to get bus name",
835 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500836 return rc;
837 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530838
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500839 // Convert to string equivalent of the passed in transition enum.
840 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530841
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500842 rc = sd_bus_call_method(bus_type, // On the system bus
843 busname, // Service to contact
844 HOST_STATE_MANAGER_ROOT, // Object path
845 DBUS_PROPERTY_IFACE, // Interface name
846 "Set", // Method to be called
847 &bus_error, // object to return error
848 nullptr, // Response buffer if any
849 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700850 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
851 request.c_str());
852 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500853 {
854 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530855 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500856 }
857 else
858 {
859 log<level::INFO>("Transition request initiated successfully");
860 }
vishwa36993272015-11-20 12:43:49 -0600861
862 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500863 free(busname);
vishwa36993272015-11-20 12:43:49 -0600864
Sergey Solomineb9b8142016-08-23 09:07:28 -0500865 return rc;
vishwa36993272015-11-20 12:43:49 -0600866}
867
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800868//------------------------------------------
869// Set Enabled property to inform NMI source
870// handling to trigger a NMI_OUT BSOD.
871//------------------------------------------
872int setNmiProperty(const bool value)
873{
874 constexpr const char* nmiSourceObjPath =
875 "/xyz/openbmc_project/Chassis/Control/NMISource";
876 constexpr const char* nmiSourceIntf =
877 "xyz.openbmc_project.Chassis.Control.NMISource";
878 std::string bmcSourceSignal = "xyz.openbmc_project.Chassis.Control."
879 "NMISource.BMCSourceSignal.ChassisCmd";
880 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
881
882 try
883 {
884 auto service = ipmi::getService(*busp, nmiSourceIntf, nmiSourceObjPath);
885 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
886 "BMCSource", bmcSourceSignal);
887 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
888 "Enabled", value);
889 }
890 catch (std::exception& e)
891 {
892 log<level::ERR>("Failed to trigger NMI_OUT",
893 entry("EXCEPTION=%s", e.what()));
894 return -1;
895 }
896
897 return 0;
898}
899
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500900namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500901{
Nan Lifdd8ec52016-08-28 03:57:40 +0800902
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500903using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
904using IpmiValue = uint8_t;
905using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800906
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700907const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500908 {RestorePolicy::Policy::AlwaysOff, 0x00},
909 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700910 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800911
Yong Lic6713cf2018-09-12 12:35:13 +0800912static constexpr uint8_t noChange = 0x03;
913static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700914
915/* helper function for Get Chassis Status Command
916 */
917std::optional<uint2_t> getPowerRestorePolicy()
918{
919 uint2_t restorePolicy = 0;
920 using namespace chassis::internal;
921
James Feist225dec82019-11-26 16:25:06 -0800922 settings::Objects& objects = cache::getObjects();
923
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700924 try
925 {
926 const auto& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -0800927 objects.map.at(powerRestoreIntf).front();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700928 ipmi::Value result = ipmi::getDbusProperty(
929 *getSdBus(),
James Feist225dec82019-11-26 16:25:06 -0800930 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700931 powerRestoreSetting.c_str(), powerRestoreIntf,
932 "PowerRestorePolicy");
933 auto powerRestore = RestorePolicy::convertPolicyFromString(
934 std::get<std::string>(result));
935 restorePolicy = dbusToIpmi.at(powerRestore);
936 }
937 catch (const std::exception& e)
938 {
939 log<level::ERR>(
940 "Failed to fetch pgood property", entry("ERROR=%s", e.what()),
James Feist225dec82019-11-26 16:25:06 -0800941 entry("PATH=%s", objects.map.at(powerRestoreIntf).front().c_str()),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700942 entry("INTERFACE=%s", powerRestoreIntf));
James Feist225dec82019-11-26 16:25:06 -0800943 cache::objectsPtr.reset();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700944 return std::nullopt;
945 }
946 return std::make_optional(restorePolicy);
947}
948
949/*
950 * getPowerStatus
951 * helper function for Get Chassis Status Command
952 * return - optional value for pgood (no value on error)
953 */
954std::optional<bool> getPowerStatus()
955{
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700956 bool powerGood = false;
957 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
958 try
959 {
Jason M. Bills3de424c2019-05-21 09:57:16 -0700960 constexpr const char* chassisStatePath =
961 "/xyz/openbmc_project/state/chassis0";
962 constexpr const char* chassisStateIntf =
963 "xyz.openbmc_project.State.Chassis";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700964 auto service =
Jason M. Bills3de424c2019-05-21 09:57:16 -0700965 ipmi::getService(*busp, chassisStateIntf, chassisStatePath);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700966
Jason M. Bills3de424c2019-05-21 09:57:16 -0700967 ipmi::Value powerState =
968 ipmi::getDbusProperty(*busp, service, chassisStatePath,
969 chassisStateIntf, "CurrentPowerState");
970 powerGood = std::get<std::string>(powerState) ==
971 "xyz.openbmc_project.State.Chassis.PowerState.On";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700972 }
973 catch (const std::exception& e)
974 {
975 try
976 {
977 // FIXME: some legacy modules use the older path; try that next
978 constexpr const char* legacyPwrCtrlObj =
979 "/org/openbmc/control/power0";
980 constexpr const char* legacyPwrCtrlIntf =
981 "org.openbmc.control.Power";
982 auto service =
983 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
984
985 ipmi::Value variant = ipmi::getDbusProperty(
986 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
987 powerGood = static_cast<bool>(std::get<int>(variant));
988 }
989 catch (const std::exception& e)
990 {
991 log<level::ERR>("Failed to fetch pgood property",
Jason M. Bills3de424c2019-05-21 09:57:16 -0700992 entry("ERROR=%s", e.what()));
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700993 return std::nullopt;
994 }
995 }
996 return std::make_optional(powerGood);
997}
998
Yong Li70ce7352019-05-16 21:15:27 +0800999/*
1000 * getACFailStatus
1001 * helper function for Get Chassis Status Command
1002 * return - bool value for ACFail (false on error)
1003 */
1004bool getACFailStatus()
1005{
1006 constexpr const char* powerControlObj =
1007 "/xyz/openbmc_project/Chassis/Control/Power0";
1008 constexpr const char* powerControlIntf =
1009 "xyz.openbmc_project.Chassis.Control.Power";
1010 bool acFail = false;
1011 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
1012 try
1013 {
1014 auto service =
1015 ipmi::getService(*bus, powerControlIntf, powerControlObj);
1016
1017 ipmi::Value variant = ipmi::getDbusProperty(
1018 *bus, service, powerControlObj, powerControlIntf, "PFail");
1019 acFail = std::get<bool>(variant);
1020 }
1021 catch (const std::exception& e)
1022 {
1023 log<level::ERR>("Failed to fetch PFail property",
1024 entry("ERROR=%s", e.what()),
1025 entry("PATH=%s", powerControlObj),
1026 entry("INTERFACE=%s", powerControlIntf));
1027 }
1028 return acFail;
1029}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001030} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +08001031
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001032static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
1033 const std::string& buttonIntf)
1034{
1035 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
1036 bool buttonDisabled = false;
1037 try
1038 {
1039 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
1040 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
1041 buttonIntf, "Enabled");
1042 buttonDisabled = !std::get<bool>(enabled);
1043 }
1044 catch (sdbusplus::exception::SdBusError& e)
1045 {
1046 log<level::ERR>("Fail to get button Enabled property",
1047 entry("PATH=%s", buttonPath.c_str()),
1048 entry("ERROR=%s", e.what()));
1049 return std::nullopt;
1050 }
1051 return std::make_optional(buttonDisabled);
1052}
1053
Kuiying Wang21addc52019-01-04 10:50:21 +08001054static bool setButtonEnabled(ipmi::Context::ptr& ctx,
1055 const std::string& buttonPath,
1056 const std::string& buttonIntf, bool enable)
1057{
1058 std::string service;
1059 boost::system::error_code ec;
1060 ec = ipmi::getService(ctx, buttonIntf, buttonPath, service);
1061 if (!ec)
1062 {
1063 ec = ipmi::setDbusProperty(ctx, service, buttonPath, buttonIntf,
1064 "Enabled", enable);
1065 }
1066 if (ec)
1067 {
1068 log<level::ERR>("Fail to set button Enabled property",
1069 entry("SERVICE=%s", service.c_str()),
1070 entry("PATH=%s", buttonPath.c_str()),
1071 entry("ERROR=%s", ec.message().c_str()));
1072 return false;
1073 }
1074 return true;
1075}
1076
Nan Lifdd8ec52016-08-28 03:57:40 +08001077//----------------------------------------------------------------------
1078// Get Chassis Status commands
1079//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001080ipmi::RspType<bool, // Power is on
1081 bool, // Power overload
1082 bool, // Interlock
1083 bool, // power fault
1084 bool, // power control fault
1085 uint2_t, // power restore policy
1086 bool, // reserved
1087
1088 bool, // AC failed
1089 bool, // last power down caused by a Power overload
1090 bool, // last power down caused by a power interlock
1091 bool, // last power down caused by power fault
1092 bool, // last ‘Power is on’ state was entered via IPMI command
1093 uint3_t, // reserved
1094
1095 bool, // Chassis intrusion active
1096 bool, // Front Panel Lockout active
1097 bool, // Drive Fault
1098 bool, // Cooling/fan fault detected
1099 uint2_t, // Chassis Identify State
1100 bool, // Chassis Identify command and state info supported
1101 bool, // reserved
1102
1103 bool, // Power off button disabled
1104 bool, // Reset button disabled
1105 bool, // Diagnostic Interrupt button disabled
1106 bool, // Standby (sleep) button disabled
1107 bool, // Power off button disable allowed
1108 bool, // Reset button disable allowed
1109 bool, // Diagnostic Interrupt button disable allowed
1110 bool // Standby (sleep) button disable allowed
1111 >
1112 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +08001113{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001114 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001115 std::optional<uint2_t> restorePolicy =
1116 power_policy::getPowerRestorePolicy();
1117 std::optional<bool> powerGood = power_policy::getPowerStatus();
1118 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001119 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001120 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001121 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001122
1123 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001124 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001125 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001126 // allow disable if the interface is present
1127 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
1128 // default return the button is enabled (not disabled)
1129 bool powerButtonDisabled = false;
1130 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001131 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001132 // return the real value of the button status, if present
1133 powerButtonDisabled = *powerButtonReading;
1134 }
1135
1136 std::optional<bool> resetButtonReading =
1137 getButtonEnabled(resetButtonPath, resetButtonIntf);
1138 // allow disable if the interface is present
1139 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
1140 // default return the button is enabled (not disabled)
1141 bool resetButtonDisabled = false;
1142 if (resetButtonDisableAllow)
1143 {
1144 // return the real value of the button status, if present
1145 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001146 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001147
Yong Li70ce7352019-05-16 21:15:27 +08001148 bool powerDownAcFailed = power_policy::getACFailStatus();
1149
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001150 // This response has a lot of hard-coded, unsupported fields
1151 // They are set to false or 0
1152 constexpr bool powerOverload = false;
1153 constexpr bool chassisInterlock = false;
1154 constexpr bool powerFault = false;
1155 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001156 constexpr bool powerDownOverload = false;
1157 constexpr bool powerDownInterlock = false;
1158 constexpr bool powerDownPowerFault = false;
1159 constexpr bool powerStatusIPMI = false;
1160 constexpr bool chassisIntrusionActive = false;
1161 constexpr bool frontPanelLockoutActive = false;
1162 constexpr bool driveFault = false;
1163 constexpr bool coolingFanFault = false;
1164 // chassisIdentifySupport set because this command is implemented
1165 constexpr bool chassisIdentifySupport = true;
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001166 uint2_t chassisIdentifyState = types::enum_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001167 constexpr bool diagButtonDisabled = false;
1168 constexpr bool sleepButtonDisabled = false;
1169 constexpr bool diagButtonDisableAllow = false;
1170 constexpr bool sleepButtonDisableAllow = false;
1171
1172 return ipmi::responseSuccess(
1173 *powerGood, powerOverload, chassisInterlock, powerFault,
1174 powerControlFault, *restorePolicy,
1175 false, // reserved
1176
1177 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1178 powerDownPowerFault, powerStatusIPMI,
1179 uint3_t(0), // reserved
1180
1181 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1182 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1183 false, // reserved
1184
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001185 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001186 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1187 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001188}
Chris Austen7888c4d2015-12-03 15:26:20 -06001189
Vijay Khemka074f64d2020-03-03 15:08:43 -08001190enum class IpmiRestartCause
1191{
1192 Unknown = 0x0,
1193 RemoteCommand = 0x1,
1194 ResetButton = 0x2,
1195 PowerButton = 0x3,
1196 WatchdogTimer = 0x4,
1197 PowerPolicyAlwaysOn = 0x6,
1198 PowerPolicyPreviousState = 0x7,
1199 SoftReset = 0xa,
1200};
1201
1202static IpmiRestartCause
1203 restartCauseToIpmiRestartCause(State::Host::RestartCause cause)
1204{
1205 switch (cause)
1206 {
1207 case State::Host::RestartCause::Unknown:
1208 {
1209 return IpmiRestartCause::Unknown;
1210 }
1211 case State::Host::RestartCause::RemoteCommand:
1212 {
1213 return IpmiRestartCause::RemoteCommand;
1214 }
1215 case State::Host::RestartCause::ResetButton:
1216 {
1217 return IpmiRestartCause::ResetButton;
1218 }
1219 case State::Host::RestartCause::PowerButton:
1220 {
1221 return IpmiRestartCause::PowerButton;
1222 }
1223 case State::Host::RestartCause::WatchdogTimer:
1224 {
1225 return IpmiRestartCause::WatchdogTimer;
1226 }
1227 case State::Host::RestartCause::PowerPolicyAlwaysOn:
1228 {
1229 return IpmiRestartCause::PowerPolicyAlwaysOn;
1230 }
1231 case State::Host::RestartCause::PowerPolicyPreviousState:
1232 {
1233 return IpmiRestartCause::PowerPolicyPreviousState;
1234 }
1235 case State::Host::RestartCause::SoftReset:
1236 {
1237 return IpmiRestartCause::SoftReset;
1238 }
1239 default:
1240 {
1241 return IpmiRestartCause::Unknown;
1242 }
1243 }
1244}
1245
1246/*
1247 * getRestartCause
1248 * helper function for Get Host restart cause Command
1249 * return - optional value for RestartCause (no value on error)
1250 */
1251static std::optional<uint4_t> getRestartCause(ipmi::Context::ptr ctx)
1252{
1253 constexpr const char* restartCausePath =
1254 "/xyz/openbmc_project/control/host0/restart_cause";
1255 constexpr const char* restartCauseIntf =
1256 "xyz.openbmc_project.Control.Host.RestartCause";
1257
1258 std::string service;
1259 boost::system::error_code ec =
1260 ipmi::getService(ctx, restartCauseIntf, restartCausePath, service);
1261 if (!ec)
1262 {
1263 std::string restartCauseStr;
1264 ec = ipmi::getDbusProperty<std::string>(
1265 ctx, service, restartCausePath, restartCauseIntf, "RestartCause",
1266 restartCauseStr);
1267 if (!ec)
1268 {
1269 auto cause =
1270 State::Host::convertRestartCauseFromString(restartCauseStr);
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001271 return types::enum_cast<uint4_t>(
1272 restartCauseToIpmiRestartCause(cause));
Vijay Khemka074f64d2020-03-03 15:08:43 -08001273 }
1274 }
1275
1276 log<level::ERR>("Failed to fetch RestartCause property",
1277 entry("ERROR=%s", ec.message().c_str()),
1278 entry("PATH=%s", restartCausePath),
1279 entry("INTERFACE=%s", restartCauseIntf));
1280 return std::nullopt;
1281}
1282
1283ipmi::RspType<uint4_t, // Restart Cause
1284 uint4_t, // reserved
1285 uint8_t // channel number (not supported)
1286 >
1287 ipmiGetSystemRestartCause(ipmi::Context::ptr ctx)
1288{
1289 std::optional<uint4_t> cause = getRestartCause(ctx);
1290 if (!cause)
1291 {
1292 return ipmi::responseUnspecifiedError();
1293 }
1294
Vernon Mauery916d4232021-03-29 13:42:16 -07001295 constexpr uint4_t reserved = 0;
1296 auto channel = static_cast<uint8_t>(ctx->channel);
1297 return ipmi::responseSuccess(cause.value(), reserved, channel);
Vijay Khemka074f64d2020-03-03 15:08:43 -08001298}
1299
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301300//-------------------------------------------------------------
1301// Send a command to SoftPowerOff application to stop any timer
1302//-------------------------------------------------------------
1303int stop_soft_off_timer()
1304{
Patrick Venture0b02be92018-08-31 11:55:55 -07001305 constexpr auto iface = "org.freedesktop.DBus.Properties";
1306 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1307 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301308
Patrick Venture0b02be92018-08-31 11:55:55 -07001309 constexpr auto property = "ResponseReceived";
1310 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1311 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301312
1313 // Get the system bus where most system services are provided.
1314 auto bus = ipmid_get_sd_bus_connection();
1315
1316 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001317 // TODO openbmc/openbmc#1661 - Mapper refactor
1318 //
1319 // See openbmc/openbmc#1743 for some details but high level summary is that
1320 // for now the code will directly call the soft off interface due to a
1321 // race condition with mapper usage
1322 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001323 // char *busname = nullptr;
1324 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1325 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001326 //{
1327 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301328 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001329 // return r;
1330 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301331
1332 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001333 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001334 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1335 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301336 if (rc < 0)
1337 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301338 log<level::ERR>("Failed to set property in SoftPowerOff object",
1339 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301340 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001341
Patrick Venture0b02be92018-08-31 11:55:55 -07001342 // TODO openbmc/openbmc#1661 - Mapper refactor
1343 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301344 return rc;
1345}
1346
vishwa36993272015-11-20 12:43:49 -06001347//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001348// Create file to indicate there is no need for softoff notification to host
1349//----------------------------------------------------------------------
1350void indicate_no_softoff_needed()
1351{
1352 fs::path path{HOST_INBAND_REQUEST_DIR};
1353 if (!fs::is_directory(path))
1354 {
1355 fs::create_directory(path);
1356 }
1357
1358 // Add the host instance (default 0 for now) to the file name
1359 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001360 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001361 size++; // null
1362 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001363 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001364
1365 // Append file name to directory and create it
1366 path /= buf.get();
1367 std::ofstream(path.c_str());
1368}
1369
anil kumar appanadafff5f2019-04-27 18:06:00 +00001370/** @brief Implementation of chassis control command
1371 *
1372 * @param - chassisControl command byte
1373 *
1374 * @return Success or InvalidFieldRequest.
1375 */
1376ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001377{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001378 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001379 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001380 {
1381 case CMD_POWER_ON:
1382 rc = initiate_state_transition(State::Host::Transition::On);
1383 break;
1384 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301385 // This path would be hit in 2 conditions.
1386 // 1: When user asks for power off using ipmi chassis command 0x04
1387 // 2: Host asking for power off post shutting down.
1388
1389 // If it's a host requested power off, then need to nudge Softoff
1390 // application that it needs to stop the watchdog timer if running.
1391 // If it is a user requested power off, then this is not really
1392 // needed. But then we need to differentiate between user and host
1393 // calling this same command
1394
1395 // For now, we are going ahead with trying to nudge the soft off and
1396 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001397 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301398
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001399 // Only request the Off transition if the soft power off
1400 // application is not running
1401 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001402 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001403 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301404 // that it should not run. Not doing this will result in State
1405 // manager doing a default soft power off when asked for power
1406 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001407 indicate_no_softoff_needed();
1408
1409 // Now request the shutdown
1410 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001411 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001412 else
1413 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301414 log<level::INFO>("Soft off is running, so let shutdown target "
1415 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001416 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001417 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301418
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001419 case CMD_HARD_RESET:
1420 case CMD_POWER_CYCLE:
1421 // SPEC has a section that says certain implementations can trigger
1422 // PowerOn if power is Off when a command to power cycle is
1423 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001424
1425 // First create a file to indicate to the soft off application
1426 // that it should not run since this is a direct user initiated
1427 // power reboot request (i.e. a reboot request that is not
1428 // originating via a soft power off SMS request)
1429 indicate_no_softoff_needed();
1430
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001431 rc = initiate_state_transition(State::Host::Transition::Reboot);
1432 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301433
1434 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1435 // Request Host State Manager to do a soft power off
1436 rc = initiate_state_transition(State::Host::Transition::Off);
1437 break;
1438
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001439 case CMD_PULSE_DIAGNOSTIC_INTR:
1440 rc = setNmiProperty(true);
1441 break;
1442
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001443 default:
1444 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301445 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001446 entry("CMD=0x%X", chassisControl));
1447 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001448 }
1449 }
vishwa36993272015-11-20 12:43:49 -06001450
anil kumar appanadafff5f2019-04-27 18:06:00 +00001451 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1452 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001453}
1454
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001455/** @brief Return D-Bus connection string to enclosure identify LED object
1456 *
1457 * @param[in, out] connection - connection to D-Bus object
1458 * @return a IPMI return code
1459 */
1460std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301461{
Tom Joseph5110c122018-03-23 17:55:40 +05301462 // lookup enclosure_identify group owner(s) in mapper
1463 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001464 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1465 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301466
1467 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001468 static const std::vector<std::string> interfaces = {
1469 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301470 mapperCall.append(interfaces);
1471 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1472 if (mapperReply.is_method_error())
1473 {
1474 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001475 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301476 }
1477 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1478 mapperReply.read(mapperResp);
1479
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001480 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301481 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001482 log<level::ERR>(
1483 "Invalid number of enclosure identify objects.",
1484 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001485 elog<InternalFailure>();
1486 }
1487 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1488 return pair.first;
1489}
Tom Joseph5110c122018-03-23 17:55:40 +05301490
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001491/** @brief Turn On/Off enclosure identify LED
1492 *
1493 * @param[in] flag - true to turn on LED, false to turn off
1494 * @return a IPMI return code
1495 */
1496void enclosureIdentifyLed(bool flag)
1497{
1498 using namespace chassis::internal;
1499 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001500 auto msg = std::string("enclosureIdentifyLed(") +
1501 boost::lexical_cast<std::string>(flag) + ")";
1502 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001503 auto led =
1504 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1505 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001506 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001507 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001508 auto ledReply = dbus.call(led);
1509 if (ledReply.is_method_error())
1510 {
1511 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001512 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001513 elog<InternalFailure>();
1514 }
1515}
1516
1517/** @brief Callback method to turn off LED
1518 */
1519void enclosureIdentifyLedOff()
1520{
1521 try
1522 {
Yong Lif4e38512019-05-21 14:46:55 +08001523 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001524 enclosureIdentifyLed(false);
1525 }
1526 catch (const InternalFailure& e)
1527 {
1528 report<InternalFailure>();
1529 }
1530}
1531
1532/** @brief Create timer to turn on and off the enclosure LED
1533 */
1534void createIdentifyTimer()
1535{
1536 if (!identifyTimer)
1537 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001538 identifyTimer =
1539 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001540 }
1541}
1542
Vernon Mauery400cc782018-10-09 13:49:53 -07001543ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1544 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001545{
Vernon Mauery400cc782018-10-09 13:49:53 -07001546 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1547 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301548
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001549 if (identifyInterval || forceIdentify)
1550 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001551 // stop the timer if already started;
1552 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001553 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001554 try
Tom Joseph5110c122018-03-23 17:55:40 +05301555 {
Yong Lif4e38512019-05-21 14:46:55 +08001556 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001557 enclosureIdentifyLed(true);
1558 }
1559 catch (const InternalFailure& e)
1560 {
1561 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001562 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301563 }
1564
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001565 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301566 {
Yong Lif4e38512019-05-21 14:46:55 +08001567 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001568 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001569 }
1570 // start the timer
1571 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001572 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001573 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301574 }
Tom Josephbed26992018-07-31 23:00:24 +05301575 else if (!identifyInterval)
1576 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001577 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301578 enclosureIdentifyLedOff();
1579 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001580 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301581}
1582
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001583namespace boot_options
1584{
1585
1586using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1587using IpmiValue = uint8_t;
1588constexpr auto ipmiDefault = 0;
1589
Patrick Venture0b02be92018-08-31 11:55:55 -07001590std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001591 {0x01, Source::Sources::Network},
1592 {0x02, Source::Sources::Disk},
1593 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001594 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001595 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001596
Patrick Venture0b02be92018-08-31 11:55:55 -07001597std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Yong Li5833cb62019-10-30 13:27:12 +08001598#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001599 {0x03, Mode::Modes::Safe},
Yong Li5833cb62019-10-30 13:27:12 +08001600#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001601 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001602 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001603
Patrick Venture0b02be92018-08-31 11:55:55 -07001604std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001605 {Source::Sources::Network, 0x01},
1606 {Source::Sources::Disk, 0x02},
1607 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001608 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001609 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001610
Patrick Venture0b02be92018-08-31 11:55:55 -07001611std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Yong Li5833cb62019-10-30 13:27:12 +08001612#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001613 {Mode::Modes::Safe, 0x03},
Yong Li5833cb62019-10-30 13:27:12 +08001614#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001615 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001616 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001617
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001618} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001619
Marri Devender Rao81719702018-05-07 00:53:48 -05001620/** @brief Set the property value for boot source
1621 * @param[in] source - boot source value
1622 * @return On failure return IPMI error.
1623 */
1624static ipmi_ret_t setBootSource(const Source::Sources& source)
1625{
1626 using namespace chassis::internal;
1627 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001628 std::variant<std::string> property = convertForMessage(source);
James Feist225dec82019-11-26 16:25:06 -08001629 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001630 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1631 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001632 auto method = dbus.new_method_call(
1633 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1634 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001635 method.append(bootSourceIntf, "BootSource", property);
1636 auto reply = dbus.call(method);
1637 if (reply.is_method_error())
1638 {
1639 log<level::ERR>("Error in BootSource Set");
1640 report<InternalFailure>();
1641 return IPMI_CC_UNSPECIFIED_ERROR;
1642 }
1643 return IPMI_CC_OK;
1644}
1645
Patrick Venture0b02be92018-08-31 11:55:55 -07001646/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001647 * @param[in] mode - boot mode value
1648 * @return On failure return IPMI error.
1649 */
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001650static ipmi::Cc setBootMode(const Mode::Modes& mode)
Marri Devender Rao81719702018-05-07 00:53:48 -05001651{
1652 using namespace chassis::internal;
1653 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001654 std::variant<std::string> property = convertForMessage(mode);
James Feist225dec82019-11-26 16:25:06 -08001655 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001656 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1657 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001658 auto method = dbus.new_method_call(
1659 objects.service(bootModeSetting, bootModeIntf).c_str(),
1660 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001661 method.append(bootModeIntf, "BootMode", property);
1662 auto reply = dbus.call(method);
1663 if (reply.is_method_error())
1664 {
1665 log<level::ERR>("Error in BootMode Set");
1666 report<InternalFailure>();
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001667 return ipmi::ccUnspecifiedError;
Marri Devender Rao81719702018-05-07 00:53:48 -05001668 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001669 return ipmi::ccSuccess;
Marri Devender Rao81719702018-05-07 00:53:48 -05001670}
1671
huangheab369282020-10-10 14:40:00 +08001672static constexpr uint8_t setComplete = 0x0;
1673static constexpr uint8_t setInProgress = 0x1;
1674static uint8_t transferStatus = setComplete;
1675
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001676/** @brief implements the Get Chassis system boot option
1677 * @param bootOptionParameter - boot option parameter selector
1678 * @param reserved1 - reserved bit
1679 * @param setSelector - selects a particular block or set of parameters
1680 * under the given parameter selector
1681 * write as 00h if parameter doesn't use a setSelector
1682 * @param blockSelector- selects a particular block within a set of
1683 * parameters write as 00h if parameter doesn't use a
1684 * blockSelector
1685 *
1686 * @return IPMI completion code plus response data
1687 * @return Payload contains below parameters:
1688 * version - parameter version
1689 * bootOptionParameter - boot option parameter selector
Patrick Venture8b1c3032020-09-15 09:43:03 -07001690 * parmIndicator - parameter valid/invalid indicator
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001691 * data - configuration parameter data
1692 */
1693ipmi::RspType<ipmi::message::Payload>
1694 ipmiChassisGetSysBootOptions(uint7_t bootOptionParameter, bool reserved1,
1695
1696 uint8_t setSelector, uint8_t blockSelector)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001697{
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001698 if (reserved1)
1699 {
1700 return ipmi::responseInvalidFieldRequest();
1701 }
1702
1703 constexpr uint4_t version = 0x01;
1704 ipmi::message::Payload response;
1705 response.pack(version, uint4_t{});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001706 using namespace boot_options;
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001707
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001708 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001709
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001710 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1711 BootOptionParameter::setInProgress)
huangheab369282020-10-10 14:40:00 +08001712 {
1713 response.pack(bootOptionParameter, reserved1, transferStatus);
1714 return ipmi::responseSuccess(std::move(response));
1715 }
1716
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001717 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1718 BootOptionParameter::bootInfo)
John Wang0213f902020-12-28 14:49:57 +08001719 {
1720 constexpr uint8_t writeMask = 0;
1721 constexpr uint8_t bootInfoAck = 0;
1722 response.pack(bootOptionParameter, writeMask, bootInfoAck);
1723 return ipmi::responseSuccess(std::move(response));
1724 }
1725
shgoupfd84fbbf2015-12-17 10:05:51 +08001726 /*
1727 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1728 * This is the only parameter used by petitboot.
1729 */
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001730 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1731 BootOptionParameter::bootFlags)
Patrick Venture0b02be92018-08-31 11:55:55 -07001732 {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001733 using namespace chassis::internal;
1734 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001735
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001736 try
ratagupta6f6bff2016-04-04 06:20:11 -05001737 {
James Feist225dec82019-11-26 16:25:06 -08001738 settings::Objects& objects = getObjects();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001739 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1740 const auto& bootSourceSetting =
1741 std::get<settings::Path>(bootSetting);
1742 auto oneTimeEnabled =
1743 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001744 auto method = dbus.new_method_call(
1745 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1746 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001747 method.append(bootSourceIntf, "BootSource");
1748 auto reply = dbus.call(method);
1749 if (reply.is_method_error())
1750 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001751 log<level::ERR>(
1752 "ipmiChassisGetSysBootOptions: Error in BootSource Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001753 report<InternalFailure>();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001754 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001755 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001756 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001757 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001758 auto bootSource =
1759 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001760
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001761 bootSetting = settings::boot::setting(objects, bootModeIntf);
1762 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1763 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001764 objects.service(bootModeSetting, bootModeIntf).c_str(),
1765 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001766 method.append(bootModeIntf, "BootMode");
1767 reply = dbus.call(method);
1768 if (reply.is_method_error())
1769 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001770 log<level::ERR>(
1771 "ipmiChassisGetSysBootOptions: Error in BootMode Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001772 report<InternalFailure>();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001773 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001774 }
1775 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001776 auto bootMode =
1777 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001778
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001779 bootOption = sourceDbusToIpmi.at(bootSource);
1780 if ((Mode::Modes::Regular == bootMode) &&
1781 (Source::Sources::Default == bootSource))
1782 {
1783 bootOption = ipmiDefault;
1784 }
1785 else if (Source::Sources::Default == bootSource)
1786 {
1787 bootOption = modeDbusToIpmi.at(bootMode);
1788 }
ratagupta6f6bff2016-04-04 06:20:11 -05001789
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001790 uint8_t bootOptionParam = oneTimeEnabled
1791 ? setParmBootFlagsValidOneTime
1792 : setParmBootFlagsValidPermanent;
1793 response.pack(bootOptionParameter, reserved1, bootOptionParam,
1794 uint2_t{}, uint4_t{bootOption}, uint2_t{}, uint8_t{},
1795 uint8_t{}, uint8_t{});
1796 return ipmi::responseSuccess(std::move(response));
ratagupta6f6bff2016-04-04 06:20:11 -05001797 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001798 catch (InternalFailure& e)
1799 {
James Feist225dec82019-11-26 16:25:06 -08001800 cache::objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001801 report<InternalFailure>();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001802 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001803 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001804 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001805 else
1806 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001807 if ((bootOptionParameter >= oemParmStart) &&
1808 (bootOptionParameter <= oemParmEnd))
1809 {
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001810 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1811 BootOptionParameter::opalNetworkSettings)
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001812 {
1813 response.pack(bootOptionParameter, reserved1);
1814 int ret = getHostNetworkData(response);
1815 if (ret < 0)
1816 {
1817 response.trailingOk = true;
1818 log<level::ERR>(
1819 "getHostNetworkData failed for GetSysBootOptions.");
1820 return ipmi::responseUnspecifiedError();
1821 }
1822 else
1823 {
1824 return ipmi::responseSuccess(std::move(response));
1825 }
1826 }
1827 }
1828 else
1829 {
1830 log<level::ERR>(
1831 "ipmiChassisGetSysBootOptions: Unsupported parameter",
1832 entry("PARAM=0x%x", static_cast<uint8_t>(bootOptionParameter)));
1833 return ipmi::responseUnspecifiedError();
1834 }
shgoupfd84fbbf2015-12-17 10:05:51 +08001835 }
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001836 return ipmi::responseUnspecifiedError();
shgoupfd84fbbf2015-12-17 10:05:51 +08001837}
1838
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001839ipmi::RspType<> ipmiChassisSetSysBootOptions(ipmi::Context::ptr ctx,
1840 uint7_t parameterSelector,
1841 bool parameterIsValid,
1842 ipmi::message::Payload& data)
shgoupfd84fbbf2015-12-17 10:05:51 +08001843{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001844 using namespace boot_options;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001845 ipmi::Cc rc;
shgoupfd84fbbf2015-12-17 10:05:51 +08001846
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001847 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
1848 BootOptionParameter::setInProgress)
huangheab369282020-10-10 14:40:00 +08001849 {
1850 uint2_t setInProgressFlag;
1851 uint6_t rsvd;
1852 if (data.unpack(setInProgressFlag, rsvd) != 0 || !data.fullyUnpacked())
1853 {
1854 return ipmi::responseReqDataLenInvalid();
1855 }
1856 if (rsvd)
1857 {
1858 return ipmi::responseInvalidFieldRequest();
1859 }
1860 if ((transferStatus == setInProgress) &&
1861 (static_cast<uint8_t>(setInProgressFlag) != setComplete))
1862 {
1863 return ipmi::response(IPMI_CC_FAIL_SET_IN_PROGRESS);
1864 }
1865 transferStatus = static_cast<uint8_t>(setInProgressFlag);
1866 return ipmi::responseSuccess();
1867 }
1868
shgoupfd84fbbf2015-12-17 10:05:51 +08001869 /* 000101
1870 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1871 * This is the only parameter used by petitboot.
1872 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001873
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001874 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
1875 BootOptionParameter::bootFlags)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001876 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001877 uint5_t rsvd;
1878 bool validFlag;
1879 bool permanent;
1880 bool biosBootType;
1881 bool lockOutResetButton;
1882 bool screenBlank;
1883 uint4_t bootDeviceSelector;
1884 bool lockKeyboard;
1885 bool cmosClear;
1886 uint8_t data3;
1887 uint4_t biosInfo;
1888 uint4_t rsvd1;
1889 uint5_t deviceInstance;
1890 uint3_t rsvd2;
1891
1892 if (data.unpack(rsvd, biosBootType, permanent, validFlag,
1893 lockOutResetButton, screenBlank, bootDeviceSelector,
1894 lockKeyboard, cmosClear, data3, biosInfo, rsvd1,
1895 deviceInstance, rsvd2) != 0 ||
1896 !data.fullyUnpacked())
1897 {
1898 return ipmi::responseReqDataLenInvalid();
1899 }
1900 if (rsvd || rsvd1 || rsvd2)
1901 {
1902 return ipmi::responseInvalidFieldRequest();
1903 }
1904
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001905 using namespace chassis::internal;
1906 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001907 auto oneTimeEnabled = false;
1908 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301909 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001910 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001911
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001912 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001913 {
James Feist225dec82019-11-26 16:25:06 -08001914 settings::Objects& objects = getObjects();
1915
Patrick Venture0b02be92018-08-31 11:55:55 -07001916 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301917
1918 oneTimeEnabled =
1919 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1920
1921 /*
1922 * Check if the current boot setting is onetime or permanent, if the
1923 * request in the command is otherwise, then set the "Enabled"
1924 * property in one_time object path to 'True' to indicate onetime
1925 * and 'False' to indicate permanent.
1926 *
1927 * Once the onetime/permanent setting is applied, then the bootMode
1928 * and bootSource is updated for the corresponding object.
1929 */
1930 if ((permanent && oneTimeEnabled) ||
1931 (!permanent && !oneTimeEnabled))
1932 {
1933 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1934
Patrick Venture0b02be92018-08-31 11:55:55 -07001935 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1936 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301937 }
1938
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001939 auto modeItr =
1940 modeIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
1941 auto sourceItr =
1942 sourceIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001943 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001944 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001945 rc = setBootSource(sourceItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001946 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001947 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001948 log<level::ERR>("ipmiChassisSetSysBootOptions: Error in "
1949 "setting boot source");
1950 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001951 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001952 // If a set boot device is mapping to a boot source, then reset
1953 // the boot mode D-Bus property to default.
1954 // This way the ipmid code can determine which property is not
1955 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001956 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001957 {
1958 setBootMode(Mode::Modes::Regular);
1959 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001960 }
1961 if (modeIpmiToDbus.end() != modeItr)
1962 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001963 rc = setBootMode(modeItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001964 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001965 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001966 log<level::ERR>("ipmiChassisSetSysBootOptions: Error in "
1967 "setting boot mode");
1968 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001969 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001970 // If a set boot device is mapping to a boot mode, then reset
1971 // the boot source D-Bus property to default.
1972 // This way the ipmid code can determine which property is not
1973 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001974 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001975 {
1976 setBootSource(Source::Sources::Default);
1977 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001978 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001979 if ((modeIpmiToDbus.end() == modeItr) &&
1980 (sourceIpmiToDbus.end() == sourceItr))
1981 {
1982 // return error if boot option is not supported
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001983 log<level::ERR>(
1984 "ipmiChassisSetSysBootOptions: Boot option not supported");
1985 return ipmi::responseInvalidFieldRequest();
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001986 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001987 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001988 catch (sdbusplus::exception_t& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001989 {
James Feist225dec82019-11-26 16:25:06 -08001990 objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001991 report<InternalFailure>();
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301992 log<level::ERR>(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001993 "ipmiChassisSetSysBootOptions: Error in setting Boot "
1994 "flag parameters");
1995 return ipmi::responseUnspecifiedError();
Ratan Guptafd28dd72016-08-01 04:58:01 -05001996 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001997 }
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001998 else if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
1999 BootOptionParameter::bootInfo)
Patrick Venture0b02be92018-08-31 11:55:55 -07002000 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002001 uint8_t writeMak;
2002 uint5_t bootInitiatorAckData;
2003 uint3_t rsvd;
2004
2005 if (data.unpack(writeMak, bootInitiatorAckData, rsvd) != 0 ||
2006 !data.fullyUnpacked())
2007 {
2008 return ipmi::responseReqDataLenInvalid();
2009 }
2010 if (rsvd)
2011 {
2012 return ipmi::responseInvalidFieldRequest();
2013 }
2014 // (ccSuccess). There is no implementation in OpenBMC for this
Tom Josephf536c902017-09-25 18:08:15 +05302015 // parameter. This is added to support the ipmitool command `chassis
2016 // bootdev` which sends set on parameter #4, before setting the boot
2017 // flags.
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002018 log<level::INFO>("ipmiChassisSetSysBootOptions: bootInfo parameter set "
2019 "successfully");
2020 data.trailingOk = true;
2021 return ipmi::responseSuccess();
Patrick Venture0b02be92018-08-31 11:55:55 -07002022 }
2023 else
2024 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002025 if ((parameterSelector >= static_cast<uint7_t>(oemParmStart)) &&
2026 (parameterSelector <= static_cast<uint7_t>(oemParmEnd)))
2027 {
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002028 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2029 BootOptionParameter::opalNetworkSettings)
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002030 {
2031 ipmi::Cc ret = setHostNetworkData(data);
2032 if (ret != ipmi::ccSuccess)
2033 {
2034 log<level::ERR>("ipmiChassisSetSysBootOptions: Error in "
2035 "setHostNetworkData");
2036 data.trailingOk = true;
2037 return ipmi::response(ret);
2038 }
2039 data.trailingOk = true;
2040 return ipmi::responseSuccess();
2041 }
2042 else
2043 {
2044 log<level::ERR>(
2045 "ipmiChassisSetSysBootOptions: Unsupported parameters",
2046 entry("PARAM=0x%x",
2047 static_cast<uint8_t>(parameterSelector)));
2048 data.trailingOk = true;
2049 return ipmi::responseParmNotSupported();
2050 }
2051 }
2052 data.trailingOk = true;
2053 return ipmi::responseParmNotSupported();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002054 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002055 return ipmi::responseSuccess();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002056}
2057
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002058/** @brief implements Get POH counter command
2059 * @parameter
2060 * - none
2061 * @returns IPMI completion code plus response data
2062 * - minPerCount - Minutes per count
2063 * - counterReading - counter reading
2064 */
2065ipmi::RspType<uint8_t, // Minutes per count
2066 uint32_t // Counter reading
2067 >
2068 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002069{
2070 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002071 try
2072 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002073 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
2074 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002075 }
2076 catch (std::exception& e)
2077 {
2078 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002079 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002080 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002081}
2082
Jason M. Billsbc996a32019-06-17 15:46:37 -07002083ipmi::RspType<uint3_t, // policy support
2084 uint5_t // reserved
2085 >
Vernon Mauerye278ead2018-10-09 09:23:43 -07002086 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
Jason M. Billsbc996a32019-06-17 15:46:37 -07002087 uint3_t policy, uint5_t reserved)
Yong Lic6713cf2018-09-12 12:35:13 +08002088{
Yong Lic6713cf2018-09-12 12:35:13 +08002089 power_policy::DbusValue value =
2090 power_policy::RestorePolicy::Policy::AlwaysOff;
2091
Jason M. Billsbc996a32019-06-17 15:46:37 -07002092 if (reserved || (policy > power_policy::noChange))
Yong Lic6713cf2018-09-12 12:35:13 +08002093 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07002094 phosphor::logging::log<level::ERR>(
2095 "Reserved request parameter",
2096 entry("REQ=0x%x", static_cast<int>(policy)));
Jason M. Billsbc996a32019-06-17 15:46:37 -07002097 return ipmi::responseInvalidFieldRequest();
Yong Lic6713cf2018-09-12 12:35:13 +08002098 }
2099
Vernon Mauerye278ead2018-10-09 09:23:43 -07002100 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08002101 {
2102 // just return the supported policy
Jason M. Billsbc996a32019-06-17 15:46:37 -07002103 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002104 }
2105
2106 for (auto const& it : power_policy::dbusToIpmi)
2107 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07002108 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08002109 {
2110 value = it.first;
2111 break;
2112 }
2113 }
2114
2115 try
2116 {
James Feist225dec82019-11-26 16:25:06 -08002117 settings::Objects& objects = chassis::internal::cache::getObjects();
Yong Lic6713cf2018-09-12 12:35:13 +08002118 const settings::Path& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -08002119 objects.map.at(chassis::internal::powerRestoreIntf).front();
Vernon Mauery16b86932019-05-01 08:36:11 -07002120 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08002121
Vernon Mauerye278ead2018-10-09 09:23:43 -07002122 auto sdbusp = getSdBus();
2123 boost::system::error_code ec;
2124 sdbusp->yield_method_call<void>(
2125 yield, ec,
James Feist225dec82019-11-26 16:25:06 -08002126 objects
Yong Lic6713cf2018-09-12 12:35:13 +08002127 .service(powerRestoreSetting,
2128 chassis::internal::powerRestoreIntf)
2129 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07002130 powerRestoreSetting, ipmi::PROP_INTF, "Set",
2131 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
2132 property);
2133 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08002134 {
2135 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07002136 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002137 }
2138 }
2139 catch (InternalFailure& e)
2140 {
James Feist225dec82019-11-26 16:25:06 -08002141 chassis::internal::cache::objectsPtr.reset();
Yong Lic6713cf2018-09-12 12:35:13 +08002142 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07002143 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002144 }
2145
Jason M. Billsbc996a32019-06-17 15:46:37 -07002146 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002147}
2148
Kuiying Wang21addc52019-01-04 10:50:21 +08002149ipmi::RspType<> ipmiSetFrontPanelButtonEnables(
2150 ipmi::Context::ptr ctx, bool disablePowerButton, bool disableResetButton,
2151 bool disableDiagButton, bool disableSleepButton, uint4_t reserved)
2152{
2153 using namespace chassis::internal;
2154
2155 // set power button Enabled property
2156 bool success = setButtonEnabled(ctx, powerButtonPath, powerButtonIntf,
2157 !disablePowerButton);
2158
2159 // set reset button Enabled property
2160 success &= setButtonEnabled(ctx, resetButtonPath, resetButtonIntf,
2161 !disableResetButton);
2162
2163 if (!success)
2164 {
2165 // not all buttons were successfully set
2166 return ipmi::responseUnspecifiedError();
2167 }
2168 return ipmi::responseSuccess();
2169}
2170
Adriana Kobylak40814c62015-10-27 15:58:44 -05002171void register_netfn_chassis_functions()
2172{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05002173 createIdentifyTimer();
2174
Tom05732372016-09-06 17:21:23 +05302175 // Get Chassis Capabilities
anil kumar appana43263c62019-05-27 12:45:04 +00002176 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2177 ipmi::chassis::cmdGetChassisCapabilities,
2178 ipmi::Privilege::User, ipmiGetChassisCap);
Nan Li8d15fb42016-08-16 22:29:40 +08002179
Kuiying Wang21addc52019-01-04 10:50:21 +08002180 // Set Front Panel Button Enables
2181 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2182 ipmi::chassis::cmdSetFrontPanelButtonEnables,
2183 ipmi::Privilege::Admin,
2184 ipmiSetFrontPanelButtonEnables);
2185
Yong Liae4b0402018-11-02 11:12:14 +08002186 // Set Chassis Capabilities
anil kumar appana894d0222019-05-27 16:32:14 +00002187 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2188 ipmi::chassis::cmdSetChassisCapabilities,
2189 ipmi::Privilege::User, ipmiSetChassisCap);
Yong Liae4b0402018-11-02 11:12:14 +08002190
Tom05732372016-09-06 17:21:23 +05302191 // <Get System Boot Options>
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00002192 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2193 ipmi::chassis::cmdGetSystemBootOptions,
2194 ipmi::Privilege::Operator,
2195 ipmiChassisGetSysBootOptions);
Adriana Kobylak40814c62015-10-27 15:58:44 -05002196
Tom05732372016-09-06 17:21:23 +05302197 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07002198 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2199 ipmi::chassis::cmdGetChassisStatus,
2200 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08002201
Vijay Khemka074f64d2020-03-03 15:08:43 -08002202 // <Chassis Get System Restart Cause>
2203 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2204 ipmi::chassis::cmdGetSystemRestartCause,
2205 ipmi::Privilege::User, ipmiGetSystemRestartCause);
2206
Tom05732372016-09-06 17:21:23 +05302207 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00002208 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2209 ipmi::chassis::cmdChassisControl,
2210 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08002211
Tom Joseph5110c122018-03-23 17:55:40 +05302212 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07002213 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2214 ipmi::chassis::cmdChassisIdentify,
2215 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05302216
Tom05732372016-09-06 17:21:23 +05302217 // <Set System Boot Options>
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002218 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2219 ipmi::chassis::cmdSetSystemBootOptions,
2220 ipmi::Privilege::Operator,
2221 ipmiChassisSetSysBootOptions);
2222
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002223 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002224 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2225 ipmi::chassis::cmdGetPohCounter,
2226 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08002227
2228 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07002229 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2230 ipmi::chassis::cmdSetPowerRestorePolicy,
2231 ipmi::Privilege::Operator,
2232 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06002233}