blob: 3250b2cd0fc03ad635569800d1c83e0095afebcf [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
Vijay Khemka074f64d2020-03-03 15:08:43 -080062static constexpr uint4_t RESERVED = 0;
63static constexpr uint8_t CHANNEL_NOT_SUPPORTED = 0;
64
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050065static constexpr size_t encIdentifyObjectsSize = 1;
66static constexpr size_t chassisIdentifyReqLength = 2;
67static constexpr size_t identifyIntervalPos = 0;
68static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080069
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000070namespace ipmi
71{
72constexpr Cc ccParmNotSupported = 0x80;
73
74static inline auto responseParmNotSupported()
75{
76 return response(ccParmNotSupported);
77}
78} // namespace ipmi
79
Adriana Kobylak40814c62015-10-27 15:58:44 -050080void register_netfn_chassis_functions() __attribute__((constructor));
81
shgoupfd84fbbf2015-12-17 10:05:51 +080082// Host settings in dbus
83// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070084const char* settings_object_name = "/org/openbmc/settings/host0";
85const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070086const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053087 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080088
Ratan Guptadcb10672017-07-10 10:33:50 +053089constexpr auto SETTINGS_ROOT = "/";
90constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053091
92constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
93constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
94
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050095static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
96static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070097 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050098static constexpr auto pOHCounterProperty = "POHCounter";
99static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +0800100const static constexpr char chassisCapIntf[] =
101 "xyz.openbmc_project.Control.ChassisCapabilities";
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800102const static constexpr char chassisIntrusionProp[] = "ChassisIntrusionEnabled";
103const static constexpr char chassisFrontPanelLockoutProp[] =
104 "ChassisFrontPanelLockoutEnabled";
105const static constexpr char chassisNMIProp[] = "ChassisNMIEnabled";
106const static constexpr char chassisPowerInterlockProp[] =
107 "ChassisPowerInterlockEnabled";
Yong Liae4b0402018-11-02 11:12:14 +0800108const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
109const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
110const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
111const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
112const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
113static constexpr uint8_t chassisCapFlagMask = 0x0f;
114static constexpr uint8_t chassisCapAddrMask = 0xfe;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700115static constexpr const char* powerButtonIntf =
116 "xyz.openbmc_project.Chassis.Buttons.Power";
117static constexpr const char* powerButtonPath =
118 "/xyz/openbmc_project/Chassis/Buttons/Power0";
119static constexpr const char* resetButtonIntf =
120 "xyz.openbmc_project.Chassis.Buttons.Reset";
121static constexpr const char* resetButtonPath =
122 "/xyz/openbmc_project/Chassis/Buttons/Reset0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530123
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530124// Phosphor Host State manager
125namespace State = sdbusplus::xyz::openbmc_project::State::server;
126
Vernon Mauery185b9f82018-07-20 10:52:36 -0700127namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500128
Ratan Guptadcb10672017-07-10 10:33:50 +0530129using namespace phosphor::logging;
130using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500131using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700132
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500133namespace chassis
134{
135namespace internal
136{
137
138constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
139constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500140constexpr auto powerRestoreIntf =
141 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500142sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
143
144namespace cache
145{
146
James Feist225dec82019-11-26 16:25:06 -0800147std::unique_ptr<settings::Objects> objectsPtr = nullptr;
148
149settings::Objects& getObjects()
150{
151 if (objectsPtr == nullptr)
152 {
153 objectsPtr = std::make_unique<settings::Objects>(
154 dbus, std::vector<std::string>{bootModeIntf, bootSourceIntf,
155 powerRestoreIntf});
156 }
157 return *objectsPtr;
158}
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500159
160} // namespace cache
161} // namespace internal
162} // namespace chassis
163
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500164namespace poh
165{
166
167constexpr auto minutesPerCount = 60;
168
169} // namespace poh
170
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000171int getHostNetworkData(ipmi::message::Payload& payload)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500172{
Ratan Guptadcb10672017-07-10 10:33:50 +0530173 ipmi::PropertyMap properties;
174 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530175 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500176
Ratan Guptadcb10672017-07-10 10:33:50 +0530177 try
178 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700179 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530180 // objects,to handle such cases we are interested on that object
181 // which are on interested busname.
182 // Currenlty mapper doesn't give the readable busname(gives busid)
183 // so we can't match with bus name so giving some object specific info
184 // as SETTINGS_MATCH.
185 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500186
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530187 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530188
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530189 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
190 SETTINGS_ROOT, SETTINGS_MATCH);
191
192 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
193 SETTINGS_ROOT, SETTINGS_MATCH);
194
Patrick Venture0b02be92018-08-31 11:55:55 -0700195 properties = ipmi::getAllDbusProperties(
196 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
197 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
198 macObjectInfo.first, MAC_INTERFACE,
199 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530200
Vernon Maueryf442e112019-04-09 11:44:36 -0700201 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530202
Vernon Maueryf442e112019-04-09 11:44:36 -0700203 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530204
Vernon Maueryf442e112019-04-09 11:44:36 -0700205 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530206
Patrick Venture0b02be92018-08-31 11:55:55 -0700207 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700208 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700209 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
210 ? 1
211 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530212
Vernon Maueryf442e112019-04-09 11:44:36 -0700213 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530214
Ratan Guptad70f4532017-08-04 02:07:31 +0530215 // it is expected here that we should get the valid data
216 // but we may also get the default values.
217 // Validation of the data is done by settings.
218 //
219 // if mac address is default mac address then
220 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530221 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530222 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530223 rc = -1;
224 return rc;
225 }
226 // if addr is static then ipaddress,gateway,prefix
227 // should not be default one,don't send blank override.
228 if (isStatic)
229 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700230 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
231 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530232 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530233 rc = -1;
234 return rc;
235 }
236 }
237
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000238 std::string token;
239 std::stringstream ss(MACAddress);
Ratan Guptadcb10672017-07-10 10:33:50 +0530240
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000241 // First pack macOffset no of bytes in payload.
242 // Latter this PetiBoot-Specific data will be populated.
243 std::vector<uint8_t> payloadInitialBytes(macOffset);
244 payload.pack(payloadInitialBytes);
Ratan Guptadcb10672017-07-10 10:33:50 +0530245
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000246 while (std::getline(ss, token, ':'))
247 {
248 payload.pack(stoi(token, nullptr, 16));
249 }
250
251 payload.pack(0x00);
252
253 payload.pack(isStatic);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530254
Vernon Maueryf442e112019-04-09 11:44:36 -0700255 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
256 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
257 ? AF_INET
258 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530259
Patrick Venture0b02be92018-08-31 11:55:55 -0700260 addrSize = (addressFamily == AF_INET)
261 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
262 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530263
264 // ipaddress and gateway would be in IPv4 format
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000265 std::vector<uint8_t> addrInBinary(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530266 inet_pton(addressFamily, ipAddress.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000267 reinterpret_cast<void*>(addrInBinary.data()));
Ratan Guptadcb10672017-07-10 10:33:50 +0530268
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000269 payload.pack(addrInBinary);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530270
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000271 payload.pack(prefix);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530272
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000273 std::vector<uint8_t> gatewayDetails(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530274 inet_pton(addressFamily, gateway.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000275 reinterpret_cast<void*>(gatewayDetails.data()));
276 payload.pack(gatewayDetails);
Ratan Guptadcb10672017-07-10 10:33:50 +0530277 }
278 catch (InternalFailure& e)
279 {
280 commit<InternalFailure>();
Ratan Guptadcb10672017-07-10 10:33:50 +0530281 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500282 return rc;
283 }
284
Patrick Venture0b02be92018-08-31 11:55:55 -0700285 // PetiBoot-Specific
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000286 // If success then copy the first 9 bytes to the payload message
287 // payload first 2 bytes contain the parameter values. Skip that 2 bytes.
288 uint8_t skipFirstTwoBytes = 2;
289 size_t payloadSize = payload.size();
290 uint8_t* configDataStartingAddress = payload.data() + skipFirstTwoBytes;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500291
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000292 if (payloadSize < skipFirstTwoBytes + sizeof(netConfInitialBytes))
293 {
294 log<level::ERR>("Invalid net config ");
295 rc = -1;
296 return rc;
297 }
298 std::copy(netConfInitialBytes,
299 netConfInitialBytes + sizeof(netConfInitialBytes),
300 configDataStartingAddress);
301
302 if (payloadSize < skipFirstTwoBytes + addrSizeOffset + sizeof(addrSize))
303 {
304 log<level::ERR>("Invalid length of address size");
305 rc = -1;
306 return rc;
307 }
308 std::copy(&addrSize, &(addrSize) + sizeof(addrSize),
309 configDataStartingAddress + addrSizeOffset);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530310
Ratan Guptafd28dd72016-08-01 04:58:01 -0500311#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700312 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500313
Ratan Guptadcb10672017-07-10 10:33:50 +0530314 for (uint8_t pos = 0; pos < index; pos++)
315 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000316 std::printf("%02x ", payloadStartingAddress[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530317 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500318#endif
319
Ratan Guptafd28dd72016-08-01 04:58:01 -0500320 return rc;
321}
322
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530323/** @brief convert IPv4 and IPv6 addresses from binary to text form.
324 * @param[in] family - IPv4/Ipv6
325 * @param[in] data - req data pointer.
326 * @param[in] offset - offset in the data.
327 * @param[in] addrSize - size of the data which needs to be read from offset.
328 * @returns address in text form.
329 */
330
Patrick Venture0b02be92018-08-31 11:55:55 -0700331std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
332 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530333{
334 char ipAddr[INET6_ADDRSTRLEN] = {};
335
Patrick Venture0b02be92018-08-31 11:55:55 -0700336 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530337 {
338 case AF_INET:
339 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700340 struct sockaddr_in addr4
341 {
342 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700343 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530344
Patrick Venture0b02be92018-08-31 11:55:55 -0700345 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530346
347 break;
348 }
349 case AF_INET6:
350 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700351 struct sockaddr_in6 addr6
352 {
353 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700354 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530355
Patrick Venture0b02be92018-08-31 11:55:55 -0700356 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530357
358 break;
359 }
360 default:
361 {
362 return {};
363 }
364 }
365
366 return ipAddr;
367}
368
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000369ipmi::Cc setHostNetworkData(ipmi::message::Payload& data)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500370{
Ratan Guptadcb10672017-07-10 10:33:50 +0530371 using namespace std::string_literals;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000372 std::string hostNetworkConfig;
373 std::string mac("00:00:00:00:00:00");
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530374 std::string ipAddress, gateway;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000375 std::string addrOrigin{0};
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530377 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530378 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700379 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
380 uint8_t prefix{0};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530381 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500382
Patrick Venture0b02be92018-08-31 11:55:55 -0700383 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500384 // version starts from sixth byte
385
Ratan Guptadcb10672017-07-10 10:33:50 +0530386 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500387 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530388 do
389 {
390 // cookie == 0x21 0x70 0x62 0x21
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000391 data.trailingOk = true;
392 auto msgLen = data.size();
393 std::vector<uint8_t> msgPayloadBytes(msgLen);
394 if (data.unpack(msgPayloadBytes) != 0 || !data.fullyUnpacked())
Ratan Guptadcb10672017-07-10 10:33:50 +0530395 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000396 log<level::ERR>(
397 "Error in unpacking message of setHostNetworkData");
398 return ipmi::ccReqDataLenInvalid;
399 }
400
401 uint8_t* msgPayloadStartingPos = msgPayloadBytes.data();
402 constexpr size_t cookieSize = 4;
403 if (msgLen < cookieOffset + cookieSize)
404 {
405 log<level::ERR>(
406 "Error in cookie getting of setHostNetworkData");
407 return ipmi::ccReqDataLenInvalid;
408 }
409 if (std::equal(msgPayloadStartingPos + cookieOffset,
410 msgPayloadStartingPos + cookieOffset + cookieSize,
411 (netConfInitialBytes + cookieOffset)) != 0)
412 {
413 // all cookie == 0
414 if (std::all_of(msgPayloadStartingPos + cookieOffset,
415 msgPayloadStartingPos + cookieOffset +
416 cookieSize,
417 [](int i) { return i == 0; }) == true)
Ratan Guptadcb10672017-07-10 10:33:50 +0530418 {
419 // need to zero out the network settings.
420 break;
421 }
422
423 log<level::ERR>("Invalid Cookie");
424 elog<InternalFailure>();
425 }
426
427 // vesion == 0x00 0x01
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000428 if (msgLen < versionOffset + sizeVersion)
Ratan Guptadcb10672017-07-10 10:33:50 +0530429 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000430 log<level::ERR>(
431 "Error in version getting of setHostNetworkData");
432 return ipmi::ccReqDataLenInvalid;
433 }
434 if (std::equal(msgPayloadStartingPos + versionOffset,
435 msgPayloadStartingPos + versionOffset + sizeVersion,
436 (netConfInitialBytes + versionOffset)) != 0)
437 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530438 log<level::ERR>("Invalid Version");
439 elog<InternalFailure>();
440 }
441
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000442 if (msgLen < macOffset + 6)
443 {
444 log<level::ERR>(
445 "Error in mac address getting of setHostNetworkData");
446 return ipmi::ccReqDataLenInvalid;
447 }
448 std::stringstream result;
449 std::copy((msgPayloadStartingPos + macOffset),
450 (msgPayloadStartingPos + macOffset + 5),
451 std::ostream_iterator<int>(result, ":"));
452 mac = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530453
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000454 if (msgLen < addrTypeOffset + sizeof(decltype(addrOrigin)))
455 {
456 log<level::ERR>(
457 "Error in original address getting of setHostNetworkData");
458 return ipmi::ccReqDataLenInvalid;
459 }
460 std::copy(msgPayloadStartingPos + addrTypeOffset,
461 msgPayloadStartingPos + addrTypeOffset +
462 sizeof(decltype(addrOrigin)),
463 std::ostream_iterator<int>(result, ""));
464 addrOrigin = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530465
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000466 if (!addrOrigin.empty())
Ratan Guptadcb10672017-07-10 10:33:50 +0530467 {
468 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530469 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530470 }
471
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000472 if (msgLen < addrSizeOffset + sizeof(decltype(addrSize)))
473 {
474 log<level::ERR>(
475 "Error in address size getting of setHostNetworkData");
476 return ipmi::ccReqDataLenInvalid;
477 }
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530478 // Get the address size
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000479 std::copy(msgPayloadStartingPos + addrSizeOffset,
480 (msgPayloadStartingPos + addrSizeOffset +
481 sizeof(decltype(addrSize))),
482 &addrSize);
Ratan Guptadcb10672017-07-10 10:33:50 +0530483
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000484 uint8_t prefixOffset = ipAddrOffset + addrSize;
485 if (msgLen < prefixOffset + sizeof(decltype(prefix)))
486 {
487 log<level::ERR>(
488 "Error in prefix getting of setHostNetworkData");
489 return ipmi::ccReqDataLenInvalid;
490 }
491 std::copy(msgPayloadStartingPos + prefixOffset,
492 (msgPayloadStartingPos + prefixOffset +
493 sizeof(decltype(prefix))),
494 &prefix);
Ratan Guptadcb10672017-07-10 10:33:50 +0530495
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530496 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
Ratan Gupta8c31d232017-08-13 05:49:43 +0530497 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530498 {
499 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
500 family = AF_INET6;
501 }
502
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000503 if (msgLen < ipAddrOffset + addrSize)
504 {
505 log<level::ERR>(
506 "Error in IP address getting of setHostNetworkData");
507 return ipmi::ccReqDataLenInvalid;
508 }
509 ipAddress = getAddrStr(family, msgPayloadStartingPos, ipAddrOffset,
510 addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530511
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000512 if (msgLen < gatewayOffset + addrSize)
513 {
514 log<level::ERR>(
515 "Error in gateway address getting of setHostNetworkData");
516 return ipmi::ccReqDataLenInvalid;
517 }
518 gateway = getAddrStr(family, msgPayloadStartingPos, gatewayOffset,
519 addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530520
Patrick Venture0b02be92018-08-31 11:55:55 -0700521 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530522
Patrick Venture0b02be92018-08-31 11:55:55 -0700523 // Cookie == 0 or it is a valid cookie
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000524 hostNetworkConfig += "ipaddress="s + ipAddress + ",prefix="s +
525 std::to_string(prefix) + ",gateway="s + gateway +
526 ",mac="s + mac + ",addressOrigin="s +
527 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500528
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530529 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
530
531 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
532 SETTINGS_ROOT, SETTINGS_MATCH);
533 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
534 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530535 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530536 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700537 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530538 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700539 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530540 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700541 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530542 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700543 IP_INTERFACE, "Gateway", std::string(gateway));
544 ipmi::setDbusProperty(
545 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
546 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530547 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700548 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500549
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000550 log<level::DEBUG>("Network configuration changed",
551 entry("NETWORKCONFIG=%s", hostNetworkConfig.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500552 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000553 catch (sdbusplus::exception_t& e)
Ratan Guptadcb10672017-07-10 10:33:50 +0530554 {
555 commit<InternalFailure>();
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000556 log<level::ERR>("Error in ipmiChassisSetSysBootOptions call");
557 return ipmi::ccUnspecifiedError;
Ratan Guptadcb10672017-07-10 10:33:50 +0530558 }
559
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000560 return ipmi::ccSuccess;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500561}
562
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500563uint32_t getPOHCounter()
564{
565 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
566
Patrick Venture0b02be92018-08-31 11:55:55 -0700567 auto chassisStateObj =
568 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500569
Patrick Venture0b02be92018-08-31 11:55:55 -0700570 auto service =
571 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500572
Patrick Venture0b02be92018-08-31 11:55:55 -0700573 auto propValue =
574 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
575 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500576
Vernon Maueryf442e112019-04-09 11:44:36 -0700577 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500578}
579
anil kumar appana43263c62019-05-27 12:45:04 +0000580/** @brief Implements the get chassis capabilities command
581 *
582 * @returns IPMI completion code plus response data
583 * chassisCapFlags - chassis capability flag
584 * chassisFRUInfoDevAddr - chassis FRU info Device Address
585 * chassisSDRDevAddr - chassis SDR device address
586 * chassisSELDevAddr - chassis SEL device address
587 * chassisSMDevAddr - chassis system management device address
588 * chassisBridgeDevAddr - chassis bridge device address
589 */
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800590ipmi::RspType<bool, // chassis intrusion sensor
591 bool, // chassis Front panel lockout
592 bool, // chassis NMI
593 bool, // chassis power interlock
594 uint4_t, // reserved
anil kumar appana43263c62019-05-27 12:45:04 +0000595 uint8_t, // chassis FRU info Device Address
596 uint8_t, // chassis SDR device address
597 uint8_t, // chassis SEL device address
598 uint8_t, // chassis system management device address
599 uint8_t // chassis bridge device address
600 >
601 ipmiGetChassisCap()
Nan Li8d15fb42016-08-16 22:29:40 +0800602{
anil kumar appana43263c62019-05-27 12:45:04 +0000603 ipmi::PropertyMap properties;
Yong Liae4b0402018-11-02 11:12:14 +0800604 try
605 {
606 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800607
Yong Liae4b0402018-11-02 11:12:14 +0800608 ipmi::DbusObjectInfo chassisCapObject =
609 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800610
Yong Liae4b0402018-11-02 11:12:14 +0800611 // capabilities flags
612 // [7..4] - reserved
613 // [3] – 1b = provides power interlock (IPM 1.5)
614 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
615 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
616 // has capabilities
617 // to lock out external power control and reset button or
618 // front panel interfaces and/or detect tampering with those
619 // interfaces).
620 // [0] -1b = Chassis provides intrusion (physical security) sensor.
621 // set to default value 0x0.
Nan Li8d15fb42016-08-16 22:29:40 +0800622
anil kumar appana43263c62019-05-27 12:45:04 +0000623 properties =
624 ipmi::getAllDbusProperties(bus, chassisCapObject.second,
625 chassisCapObject.first, chassisCapIntf);
Yong Liae4b0402018-11-02 11:12:14 +0800626 }
627 catch (std::exception& e)
628 {
anil kumar appana43263c62019-05-27 12:45:04 +0000629 log<level::ERR>("Failed to fetch Chassis Capability properties",
630 entry("ERROR=%s", e.what()));
631 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800632 }
633
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800634 bool* chassisIntrusionFlag =
635 std::get_if<bool>(&properties[chassisIntrusionProp]);
636 if (chassisIntrusionFlag == nullptr)
anil kumar appana43263c62019-05-27 12:45:04 +0000637 {
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800638 log<level::ERR>("Error to get chassis Intrusion flags");
639 return ipmi::responseUnspecifiedError();
640 }
641 bool* chassisFrontPanelFlag =
642 std::get_if<bool>(&properties[chassisFrontPanelLockoutProp]);
643 if (chassisFrontPanelFlag == nullptr)
644 {
645 log<level::ERR>("Error to get chassis intrusion flags");
646 return ipmi::responseUnspecifiedError();
647 }
648 bool* chassisNMIFlag = std::get_if<bool>(&properties[chassisNMIProp]);
649 if (chassisNMIFlag == nullptr)
650 {
651 log<level::ERR>("Error to get chassis NMI flags");
652 return ipmi::responseUnspecifiedError();
653 }
654 bool* chassisPowerInterlockFlag =
655 std::get_if<bool>(&properties[chassisPowerInterlockProp]);
656 if (chassisPowerInterlockFlag == nullptr)
657 {
658 log<level::ERR>("Error to get chassis power interlock flags");
anil kumar appana43263c62019-05-27 12:45:04 +0000659 return ipmi::responseUnspecifiedError();
660 }
661 uint8_t* chassisFRUInfoDevAddr =
662 std::get_if<uint8_t>(&properties[chassisFRUDevAddrProp]);
663 if (chassisFRUInfoDevAddr == nullptr)
664 {
665 log<level::ERR>("Error to get chassis FRU info device address");
666 return ipmi::responseUnspecifiedError();
667 }
668 uint8_t* chassisSDRDevAddr =
669 std::get_if<uint8_t>(&properties[chassisSDRDevAddrProp]);
670 if (chassisSDRDevAddr == nullptr)
671 {
672 log<level::ERR>("Error to get chassis SDR device address");
673 return ipmi::responseUnspecifiedError();
674 }
675 uint8_t* chassisSELDevAddr =
676 std::get_if<uint8_t>(&properties[chassisSELDevAddrProp]);
677 if (chassisSELDevAddr == nullptr)
678 {
679 log<level::ERR>("Error to get chassis SEL device address");
680 return ipmi::responseUnspecifiedError();
681 }
682 uint8_t* chassisSMDevAddr =
683 std::get_if<uint8_t>(&properties[chassisSMDevAddrProp]);
684 if (chassisSMDevAddr == nullptr)
685 {
686 log<level::ERR>("Error to get chassis SM device address");
687 return ipmi::responseUnspecifiedError();
688 }
689 uint8_t* chassisBridgeDevAddr =
690 std::get_if<uint8_t>(&properties[chassisBridgeDevAddrProp]);
691 if (chassisBridgeDevAddr == nullptr)
692 {
693 log<level::ERR>("Error to get chassis bridge device address");
694 return ipmi::responseUnspecifiedError();
695 }
696
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800697 return ipmi::responseSuccess(*chassisIntrusionFlag, *chassisFrontPanelFlag,
698 *chassisNMIFlag, *chassisPowerInterlockFlag, 0,
699 *chassisFRUInfoDevAddr, *chassisSDRDevAddr,
700 *chassisSELDevAddr, *chassisSMDevAddr,
701 *chassisBridgeDevAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800702}
703
anil kumar appana894d0222019-05-27 16:32:14 +0000704/** @brief implements set chassis capalibities command
705 * @param intrusion - chassis intrusion
706 * @param fpLockout - frontpannel lockout
707 * @param reserved1 - skip one bit
708 * @param fruDeviceAddr - chassis FRU info Device Address
709 * @param sdrDeviceAddr - chassis SDR device address
710 * @param selDeviceAddr - chassis SEL device address
711 * @param smDeviceAddr - chassis system management device address
712 * @param bridgeDeviceAddr - chassis bridge device address
713 *
714 * @returns IPMI completion code
715 */
716ipmi::RspType<> ipmiSetChassisCap(bool intrusion, bool fpLockout,
717 uint6_t reserved1,
718
719 uint8_t fruDeviceAddr,
720
721 uint8_t sdrDeviceAddr,
722
723 uint8_t selDeviceAddr,
724
725 uint8_t smDeviceAddr,
726
727 uint8_t bridgeDeviceAddr)
Yong Liae4b0402018-11-02 11:12:14 +0800728{
Yong Liae4b0402018-11-02 11:12:14 +0800729
730 // check input data
anil kumar appana894d0222019-05-27 16:32:14 +0000731 if (reserved1 != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800732 {
anil kumar appana894d0222019-05-27 16:32:14 +0000733 log<level::ERR>("Unsupported request parameter");
734 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800735 }
736
anil kumar appana894d0222019-05-27 16:32:14 +0000737 if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800738 {
739 log<level::ERR>("Unsupported request parameter(FRU Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000740 entry("REQ=0x%x", fruDeviceAddr));
741 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800742 }
anil kumar appana894d0222019-05-27 16:32:14 +0000743 if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800744 {
745 log<level::ERR>("Unsupported request parameter(SDR Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000746 entry("REQ=0x%x", sdrDeviceAddr));
747 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800748 }
749
anil kumar appana894d0222019-05-27 16:32:14 +0000750 if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800751 {
752 log<level::ERR>("Unsupported request parameter(SEL Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000753 entry("REQ=0x%x", selDeviceAddr));
754 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800755 }
756
anil kumar appana894d0222019-05-27 16:32:14 +0000757 if ((smDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800758 {
anil kumar appana894d0222019-05-27 16:32:14 +0000759 log<level::ERR>("Unsupported request parameter(SM Addr)",
760 entry("REQ=0x%x", smDeviceAddr));
761 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800762 }
763
anil kumar appana894d0222019-05-27 16:32:14 +0000764 if ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800765 {
766 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000767 entry("REQ=0x%x", bridgeDeviceAddr));
768 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800769 }
770
771 try
772 {
773 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
774 ipmi::DbusObjectInfo chassisCapObject =
775 ipmi::getDbusObject(bus, chassisCapIntf);
776
777 ipmi::setDbusProperty(bus, chassisCapObject.second,
778 chassisCapObject.first, chassisCapIntf,
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800779 chassisIntrusionProp, intrusion);
780
781 ipmi::setDbusProperty(bus, chassisCapObject.second,
782 chassisCapObject.first, chassisCapIntf,
783 chassisFrontPanelLockoutProp, fpLockout);
Yong Liae4b0402018-11-02 11:12:14 +0800784
785 ipmi::setDbusProperty(bus, chassisCapObject.second,
786 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000787 chassisFRUDevAddrProp, fruDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800788
789 ipmi::setDbusProperty(bus, chassisCapObject.second,
790 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000791 chassisSDRDevAddrProp, sdrDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800792
793 ipmi::setDbusProperty(bus, chassisCapObject.second,
794 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000795 chassisSELDevAddrProp, selDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800796
797 ipmi::setDbusProperty(bus, chassisCapObject.second,
798 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000799 chassisSMDevAddrProp, smDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800800
801 ipmi::setDbusProperty(bus, chassisCapObject.second,
802 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000803 chassisBridgeDevAddrProp, bridgeDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800804 }
805 catch (std::exception& e)
806 {
807 log<level::ERR>(e.what());
anil kumar appana894d0222019-05-27 16:32:14 +0000808 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800809 }
anil kumar appana894d0222019-05-27 16:32:14 +0000810 return ipmi::responseSuccess();
Nan Li8d15fb42016-08-16 22:29:40 +0800811}
812
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530813//------------------------------------------
814// Calls into Host State Manager Dbus object
815//------------------------------------------
816int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600817{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500818 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700819 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500820 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700821 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
822 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530823
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500824 // sd_bus error
825 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700826 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600827
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500828 // SD Bus error report mechanism.
829 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600830
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500831 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700832 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500833 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
834 if (rc < 0)
835 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700836 log<level::ERR>(
837 "Failed to get bus name",
838 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500839 return rc;
840 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530841
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500842 // Convert to string equivalent of the passed in transition enum.
843 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530844
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500845 rc = sd_bus_call_method(bus_type, // On the system bus
846 busname, // Service to contact
847 HOST_STATE_MANAGER_ROOT, // Object path
848 DBUS_PROPERTY_IFACE, // Interface name
849 "Set", // Method to be called
850 &bus_error, // object to return error
851 nullptr, // Response buffer if any
852 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700853 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
854 request.c_str());
855 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500856 {
857 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530858 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500859 }
860 else
861 {
862 log<level::INFO>("Transition request initiated successfully");
863 }
vishwa36993272015-11-20 12:43:49 -0600864
865 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500866 free(busname);
vishwa36993272015-11-20 12:43:49 -0600867
Sergey Solomineb9b8142016-08-23 09:07:28 -0500868 return rc;
vishwa36993272015-11-20 12:43:49 -0600869}
870
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800871//------------------------------------------
872// Set Enabled property to inform NMI source
873// handling to trigger a NMI_OUT BSOD.
874//------------------------------------------
875int setNmiProperty(const bool value)
876{
877 constexpr const char* nmiSourceObjPath =
878 "/xyz/openbmc_project/Chassis/Control/NMISource";
879 constexpr const char* nmiSourceIntf =
880 "xyz.openbmc_project.Chassis.Control.NMISource";
881 std::string bmcSourceSignal = "xyz.openbmc_project.Chassis.Control."
882 "NMISource.BMCSourceSignal.ChassisCmd";
883 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
884
885 try
886 {
887 auto service = ipmi::getService(*busp, nmiSourceIntf, nmiSourceObjPath);
888 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
889 "BMCSource", bmcSourceSignal);
890 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
891 "Enabled", value);
892 }
893 catch (std::exception& e)
894 {
895 log<level::ERR>("Failed to trigger NMI_OUT",
896 entry("EXCEPTION=%s", e.what()));
897 return -1;
898 }
899
900 return 0;
901}
902
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500903namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500904{
Nan Lifdd8ec52016-08-28 03:57:40 +0800905
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500906using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
907using IpmiValue = uint8_t;
908using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800909
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700910const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500911 {RestorePolicy::Policy::AlwaysOff, 0x00},
912 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700913 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800914
Yong Lic6713cf2018-09-12 12:35:13 +0800915static constexpr uint8_t noChange = 0x03;
916static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700917
918/* helper function for Get Chassis Status Command
919 */
920std::optional<uint2_t> getPowerRestorePolicy()
921{
922 uint2_t restorePolicy = 0;
923 using namespace chassis::internal;
924
James Feist225dec82019-11-26 16:25:06 -0800925 settings::Objects& objects = cache::getObjects();
926
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700927 try
928 {
929 const auto& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -0800930 objects.map.at(powerRestoreIntf).front();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700931 ipmi::Value result = ipmi::getDbusProperty(
932 *getSdBus(),
James Feist225dec82019-11-26 16:25:06 -0800933 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700934 powerRestoreSetting.c_str(), powerRestoreIntf,
935 "PowerRestorePolicy");
936 auto powerRestore = RestorePolicy::convertPolicyFromString(
937 std::get<std::string>(result));
938 restorePolicy = dbusToIpmi.at(powerRestore);
939 }
940 catch (const std::exception& e)
941 {
942 log<level::ERR>(
943 "Failed to fetch pgood property", entry("ERROR=%s", e.what()),
James Feist225dec82019-11-26 16:25:06 -0800944 entry("PATH=%s", objects.map.at(powerRestoreIntf).front().c_str()),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700945 entry("INTERFACE=%s", powerRestoreIntf));
James Feist225dec82019-11-26 16:25:06 -0800946 cache::objectsPtr.reset();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700947 return std::nullopt;
948 }
949 return std::make_optional(restorePolicy);
950}
951
952/*
953 * getPowerStatus
954 * helper function for Get Chassis Status Command
955 * return - optional value for pgood (no value on error)
956 */
957std::optional<bool> getPowerStatus()
958{
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700959 bool powerGood = false;
960 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
961 try
962 {
Jason M. Bills3de424c2019-05-21 09:57:16 -0700963 constexpr const char* chassisStatePath =
964 "/xyz/openbmc_project/state/chassis0";
965 constexpr const char* chassisStateIntf =
966 "xyz.openbmc_project.State.Chassis";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700967 auto service =
Jason M. Bills3de424c2019-05-21 09:57:16 -0700968 ipmi::getService(*busp, chassisStateIntf, chassisStatePath);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700969
Jason M. Bills3de424c2019-05-21 09:57:16 -0700970 ipmi::Value powerState =
971 ipmi::getDbusProperty(*busp, service, chassisStatePath,
972 chassisStateIntf, "CurrentPowerState");
973 powerGood = std::get<std::string>(powerState) ==
974 "xyz.openbmc_project.State.Chassis.PowerState.On";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700975 }
976 catch (const std::exception& e)
977 {
978 try
979 {
980 // FIXME: some legacy modules use the older path; try that next
981 constexpr const char* legacyPwrCtrlObj =
982 "/org/openbmc/control/power0";
983 constexpr const char* legacyPwrCtrlIntf =
984 "org.openbmc.control.Power";
985 auto service =
986 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
987
988 ipmi::Value variant = ipmi::getDbusProperty(
989 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
990 powerGood = static_cast<bool>(std::get<int>(variant));
991 }
992 catch (const std::exception& e)
993 {
994 log<level::ERR>("Failed to fetch pgood property",
Jason M. Bills3de424c2019-05-21 09:57:16 -0700995 entry("ERROR=%s", e.what()));
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700996 return std::nullopt;
997 }
998 }
999 return std::make_optional(powerGood);
1000}
1001
Yong Li70ce7352019-05-16 21:15:27 +08001002/*
1003 * getACFailStatus
1004 * helper function for Get Chassis Status Command
1005 * return - bool value for ACFail (false on error)
1006 */
1007bool getACFailStatus()
1008{
1009 constexpr const char* powerControlObj =
1010 "/xyz/openbmc_project/Chassis/Control/Power0";
1011 constexpr const char* powerControlIntf =
1012 "xyz.openbmc_project.Chassis.Control.Power";
1013 bool acFail = false;
1014 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
1015 try
1016 {
1017 auto service =
1018 ipmi::getService(*bus, powerControlIntf, powerControlObj);
1019
1020 ipmi::Value variant = ipmi::getDbusProperty(
1021 *bus, service, powerControlObj, powerControlIntf, "PFail");
1022 acFail = std::get<bool>(variant);
1023 }
1024 catch (const std::exception& e)
1025 {
1026 log<level::ERR>("Failed to fetch PFail property",
1027 entry("ERROR=%s", e.what()),
1028 entry("PATH=%s", powerControlObj),
1029 entry("INTERFACE=%s", powerControlIntf));
1030 }
1031 return acFail;
1032}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001033} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +08001034
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001035static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
1036 const std::string& buttonIntf)
1037{
1038 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
1039 bool buttonDisabled = false;
1040 try
1041 {
1042 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
1043 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
1044 buttonIntf, "Enabled");
1045 buttonDisabled = !std::get<bool>(enabled);
1046 }
1047 catch (sdbusplus::exception::SdBusError& e)
1048 {
1049 log<level::ERR>("Fail to get button Enabled property",
1050 entry("PATH=%s", buttonPath.c_str()),
1051 entry("ERROR=%s", e.what()));
1052 return std::nullopt;
1053 }
1054 return std::make_optional(buttonDisabled);
1055}
1056
Kuiying Wang21addc52019-01-04 10:50:21 +08001057static bool setButtonEnabled(ipmi::Context::ptr& ctx,
1058 const std::string& buttonPath,
1059 const std::string& buttonIntf, bool enable)
1060{
1061 std::string service;
1062 boost::system::error_code ec;
1063 ec = ipmi::getService(ctx, buttonIntf, buttonPath, service);
1064 if (!ec)
1065 {
1066 ec = ipmi::setDbusProperty(ctx, service, buttonPath, buttonIntf,
1067 "Enabled", enable);
1068 }
1069 if (ec)
1070 {
1071 log<level::ERR>("Fail to set button Enabled property",
1072 entry("SERVICE=%s", service.c_str()),
1073 entry("PATH=%s", buttonPath.c_str()),
1074 entry("ERROR=%s", ec.message().c_str()));
1075 return false;
1076 }
1077 return true;
1078}
1079
Nan Lifdd8ec52016-08-28 03:57:40 +08001080//----------------------------------------------------------------------
1081// Get Chassis Status commands
1082//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001083ipmi::RspType<bool, // Power is on
1084 bool, // Power overload
1085 bool, // Interlock
1086 bool, // power fault
1087 bool, // power control fault
1088 uint2_t, // power restore policy
1089 bool, // reserved
1090
1091 bool, // AC failed
1092 bool, // last power down caused by a Power overload
1093 bool, // last power down caused by a power interlock
1094 bool, // last power down caused by power fault
1095 bool, // last ‘Power is on’ state was entered via IPMI command
1096 uint3_t, // reserved
1097
1098 bool, // Chassis intrusion active
1099 bool, // Front Panel Lockout active
1100 bool, // Drive Fault
1101 bool, // Cooling/fan fault detected
1102 uint2_t, // Chassis Identify State
1103 bool, // Chassis Identify command and state info supported
1104 bool, // reserved
1105
1106 bool, // Power off button disabled
1107 bool, // Reset button disabled
1108 bool, // Diagnostic Interrupt button disabled
1109 bool, // Standby (sleep) button disabled
1110 bool, // Power off button disable allowed
1111 bool, // Reset button disable allowed
1112 bool, // Diagnostic Interrupt button disable allowed
1113 bool // Standby (sleep) button disable allowed
1114 >
1115 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +08001116{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001117 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001118 std::optional<uint2_t> restorePolicy =
1119 power_policy::getPowerRestorePolicy();
1120 std::optional<bool> powerGood = power_policy::getPowerStatus();
1121 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001122 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001123 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001124 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001125
1126 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001127 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001128 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001129 // allow disable if the interface is present
1130 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
1131 // default return the button is enabled (not disabled)
1132 bool powerButtonDisabled = false;
1133 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001134 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001135 // return the real value of the button status, if present
1136 powerButtonDisabled = *powerButtonReading;
1137 }
1138
1139 std::optional<bool> resetButtonReading =
1140 getButtonEnabled(resetButtonPath, resetButtonIntf);
1141 // allow disable if the interface is present
1142 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
1143 // default return the button is enabled (not disabled)
1144 bool resetButtonDisabled = false;
1145 if (resetButtonDisableAllow)
1146 {
1147 // return the real value of the button status, if present
1148 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001149 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001150
Yong Li70ce7352019-05-16 21:15:27 +08001151 bool powerDownAcFailed = power_policy::getACFailStatus();
1152
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001153 // This response has a lot of hard-coded, unsupported fields
1154 // They are set to false or 0
1155 constexpr bool powerOverload = false;
1156 constexpr bool chassisInterlock = false;
1157 constexpr bool powerFault = false;
1158 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001159 constexpr bool powerDownOverload = false;
1160 constexpr bool powerDownInterlock = false;
1161 constexpr bool powerDownPowerFault = false;
1162 constexpr bool powerStatusIPMI = false;
1163 constexpr bool chassisIntrusionActive = false;
1164 constexpr bool frontPanelLockoutActive = false;
1165 constexpr bool driveFault = false;
1166 constexpr bool coolingFanFault = false;
1167 // chassisIdentifySupport set because this command is implemented
1168 constexpr bool chassisIdentifySupport = true;
Yong Lif4e38512019-05-21 14:46:55 +08001169 uint2_t chassisIdentifyState = static_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001170 constexpr bool diagButtonDisabled = false;
1171 constexpr bool sleepButtonDisabled = false;
1172 constexpr bool diagButtonDisableAllow = false;
1173 constexpr bool sleepButtonDisableAllow = false;
1174
1175 return ipmi::responseSuccess(
1176 *powerGood, powerOverload, chassisInterlock, powerFault,
1177 powerControlFault, *restorePolicy,
1178 false, // reserved
1179
1180 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1181 powerDownPowerFault, powerStatusIPMI,
1182 uint3_t(0), // reserved
1183
1184 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1185 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1186 false, // reserved
1187
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001188 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001189 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1190 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001191}
Chris Austen7888c4d2015-12-03 15:26:20 -06001192
Vijay Khemka074f64d2020-03-03 15:08:43 -08001193enum class IpmiRestartCause
1194{
1195 Unknown = 0x0,
1196 RemoteCommand = 0x1,
1197 ResetButton = 0x2,
1198 PowerButton = 0x3,
1199 WatchdogTimer = 0x4,
1200 PowerPolicyAlwaysOn = 0x6,
1201 PowerPolicyPreviousState = 0x7,
1202 SoftReset = 0xa,
1203};
1204
1205static IpmiRestartCause
1206 restartCauseToIpmiRestartCause(State::Host::RestartCause cause)
1207{
1208 switch (cause)
1209 {
1210 case State::Host::RestartCause::Unknown:
1211 {
1212 return IpmiRestartCause::Unknown;
1213 }
1214 case State::Host::RestartCause::RemoteCommand:
1215 {
1216 return IpmiRestartCause::RemoteCommand;
1217 }
1218 case State::Host::RestartCause::ResetButton:
1219 {
1220 return IpmiRestartCause::ResetButton;
1221 }
1222 case State::Host::RestartCause::PowerButton:
1223 {
1224 return IpmiRestartCause::PowerButton;
1225 }
1226 case State::Host::RestartCause::WatchdogTimer:
1227 {
1228 return IpmiRestartCause::WatchdogTimer;
1229 }
1230 case State::Host::RestartCause::PowerPolicyAlwaysOn:
1231 {
1232 return IpmiRestartCause::PowerPolicyAlwaysOn;
1233 }
1234 case State::Host::RestartCause::PowerPolicyPreviousState:
1235 {
1236 return IpmiRestartCause::PowerPolicyPreviousState;
1237 }
1238 case State::Host::RestartCause::SoftReset:
1239 {
1240 return IpmiRestartCause::SoftReset;
1241 }
1242 default:
1243 {
1244 return IpmiRestartCause::Unknown;
1245 }
1246 }
1247}
1248
1249/*
1250 * getRestartCause
1251 * helper function for Get Host restart cause Command
1252 * return - optional value for RestartCause (no value on error)
1253 */
1254static std::optional<uint4_t> getRestartCause(ipmi::Context::ptr ctx)
1255{
1256 constexpr const char* restartCausePath =
1257 "/xyz/openbmc_project/control/host0/restart_cause";
1258 constexpr const char* restartCauseIntf =
1259 "xyz.openbmc_project.Control.Host.RestartCause";
1260
1261 std::string service;
1262 boost::system::error_code ec =
1263 ipmi::getService(ctx, restartCauseIntf, restartCausePath, service);
1264 if (!ec)
1265 {
1266 std::string restartCauseStr;
1267 ec = ipmi::getDbusProperty<std::string>(
1268 ctx, service, restartCausePath, restartCauseIntf, "RestartCause",
1269 restartCauseStr);
1270 if (!ec)
1271 {
1272 auto cause =
1273 State::Host::convertRestartCauseFromString(restartCauseStr);
1274 return restartCauseToIpmiRestartCause(cause);
1275 }
1276 }
1277
1278 log<level::ERR>("Failed to fetch RestartCause property",
1279 entry("ERROR=%s", ec.message().c_str()),
1280 entry("PATH=%s", restartCausePath),
1281 entry("INTERFACE=%s", restartCauseIntf));
1282 return std::nullopt;
1283}
1284
1285ipmi::RspType<uint4_t, // Restart Cause
1286 uint4_t, // reserved
1287 uint8_t // channel number (not supported)
1288 >
1289 ipmiGetSystemRestartCause(ipmi::Context::ptr ctx)
1290{
1291 std::optional<uint4_t> cause = getRestartCause(ctx);
1292 if (!cause)
1293 {
1294 return ipmi::responseUnspecifiedError();
1295 }
1296
1297 return ipmi::responseSuccess(cause.value(), RESERVED,
1298 CHANNEL_NOT_SUPPORTED);
1299}
1300
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301301//-------------------------------------------------------------
1302// Send a command to SoftPowerOff application to stop any timer
1303//-------------------------------------------------------------
1304int stop_soft_off_timer()
1305{
Patrick Venture0b02be92018-08-31 11:55:55 -07001306 constexpr auto iface = "org.freedesktop.DBus.Properties";
1307 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1308 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301309
Patrick Venture0b02be92018-08-31 11:55:55 -07001310 constexpr auto property = "ResponseReceived";
1311 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1312 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301313
1314 // Get the system bus where most system services are provided.
1315 auto bus = ipmid_get_sd_bus_connection();
1316
1317 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001318 // TODO openbmc/openbmc#1661 - Mapper refactor
1319 //
1320 // See openbmc/openbmc#1743 for some details but high level summary is that
1321 // for now the code will directly call the soft off interface due to a
1322 // race condition with mapper usage
1323 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001324 // char *busname = nullptr;
1325 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1326 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001327 //{
1328 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301329 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001330 // return r;
1331 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301332
1333 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001334 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001335 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1336 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301337 if (rc < 0)
1338 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301339 log<level::ERR>("Failed to set property in SoftPowerOff object",
1340 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301341 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001342
Patrick Venture0b02be92018-08-31 11:55:55 -07001343 // TODO openbmc/openbmc#1661 - Mapper refactor
1344 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301345 return rc;
1346}
1347
vishwa36993272015-11-20 12:43:49 -06001348//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001349// Create file to indicate there is no need for softoff notification to host
1350//----------------------------------------------------------------------
1351void indicate_no_softoff_needed()
1352{
1353 fs::path path{HOST_INBAND_REQUEST_DIR};
1354 if (!fs::is_directory(path))
1355 {
1356 fs::create_directory(path);
1357 }
1358
1359 // Add the host instance (default 0 for now) to the file name
1360 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001361 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001362 size++; // null
1363 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001364 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001365
1366 // Append file name to directory and create it
1367 path /= buf.get();
1368 std::ofstream(path.c_str());
1369}
1370
anil kumar appanadafff5f2019-04-27 18:06:00 +00001371/** @brief Implementation of chassis control command
1372 *
1373 * @param - chassisControl command byte
1374 *
1375 * @return Success or InvalidFieldRequest.
1376 */
1377ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001378{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001379 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001380 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001381 {
1382 case CMD_POWER_ON:
1383 rc = initiate_state_transition(State::Host::Transition::On);
1384 break;
1385 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301386 // This path would be hit in 2 conditions.
1387 // 1: When user asks for power off using ipmi chassis command 0x04
1388 // 2: Host asking for power off post shutting down.
1389
1390 // If it's a host requested power off, then need to nudge Softoff
1391 // application that it needs to stop the watchdog timer if running.
1392 // If it is a user requested power off, then this is not really
1393 // needed. But then we need to differentiate between user and host
1394 // calling this same command
1395
1396 // For now, we are going ahead with trying to nudge the soft off and
1397 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001398 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301399
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001400 // Only request the Off transition if the soft power off
1401 // application is not running
1402 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001403 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001404 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301405 // that it should not run. Not doing this will result in State
1406 // manager doing a default soft power off when asked for power
1407 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001408 indicate_no_softoff_needed();
1409
1410 // Now request the shutdown
1411 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001412 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001413 else
1414 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301415 log<level::INFO>("Soft off is running, so let shutdown target "
1416 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001417 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001418 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301419
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001420 case CMD_HARD_RESET:
1421 case CMD_POWER_CYCLE:
1422 // SPEC has a section that says certain implementations can trigger
1423 // PowerOn if power is Off when a command to power cycle is
1424 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001425
1426 // First create a file to indicate to the soft off application
1427 // that it should not run since this is a direct user initiated
1428 // power reboot request (i.e. a reboot request that is not
1429 // originating via a soft power off SMS request)
1430 indicate_no_softoff_needed();
1431
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001432 rc = initiate_state_transition(State::Host::Transition::Reboot);
1433 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301434
1435 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1436 // Request Host State Manager to do a soft power off
1437 rc = initiate_state_transition(State::Host::Transition::Off);
1438 break;
1439
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001440 case CMD_PULSE_DIAGNOSTIC_INTR:
1441 rc = setNmiProperty(true);
1442 break;
1443
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001444 default:
1445 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301446 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001447 entry("CMD=0x%X", chassisControl));
1448 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001449 }
1450 }
vishwa36993272015-11-20 12:43:49 -06001451
anil kumar appanadafff5f2019-04-27 18:06:00 +00001452 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1453 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001454}
1455
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001456/** @brief Return D-Bus connection string to enclosure identify LED object
1457 *
1458 * @param[in, out] connection - connection to D-Bus object
1459 * @return a IPMI return code
1460 */
1461std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301462{
Tom Joseph5110c122018-03-23 17:55:40 +05301463 // lookup enclosure_identify group owner(s) in mapper
1464 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001465 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1466 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301467
1468 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001469 static const std::vector<std::string> interfaces = {
1470 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301471 mapperCall.append(interfaces);
1472 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1473 if (mapperReply.is_method_error())
1474 {
1475 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001476 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301477 }
1478 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1479 mapperReply.read(mapperResp);
1480
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001481 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301482 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001483 log<level::ERR>(
1484 "Invalid number of enclosure identify objects.",
1485 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001486 elog<InternalFailure>();
1487 }
1488 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1489 return pair.first;
1490}
Tom Joseph5110c122018-03-23 17:55:40 +05301491
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001492/** @brief Turn On/Off enclosure identify LED
1493 *
1494 * @param[in] flag - true to turn on LED, false to turn off
1495 * @return a IPMI return code
1496 */
1497void enclosureIdentifyLed(bool flag)
1498{
1499 using namespace chassis::internal;
1500 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001501 auto msg = std::string("enclosureIdentifyLed(") +
1502 boost::lexical_cast<std::string>(flag) + ")";
1503 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001504 auto led =
1505 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1506 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001507 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001508 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001509 auto ledReply = dbus.call(led);
1510 if (ledReply.is_method_error())
1511 {
1512 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001513 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001514 elog<InternalFailure>();
1515 }
1516}
1517
1518/** @brief Callback method to turn off LED
1519 */
1520void enclosureIdentifyLedOff()
1521{
1522 try
1523 {
Yong Lif4e38512019-05-21 14:46:55 +08001524 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001525 enclosureIdentifyLed(false);
1526 }
1527 catch (const InternalFailure& e)
1528 {
1529 report<InternalFailure>();
1530 }
1531}
1532
1533/** @brief Create timer to turn on and off the enclosure LED
1534 */
1535void createIdentifyTimer()
1536{
1537 if (!identifyTimer)
1538 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001539 identifyTimer =
1540 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001541 }
1542}
1543
Vernon Mauery400cc782018-10-09 13:49:53 -07001544ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1545 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001546{
Vernon Mauery400cc782018-10-09 13:49:53 -07001547 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1548 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301549
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001550 if (identifyInterval || forceIdentify)
1551 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001552 // stop the timer if already started;
1553 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001554 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001555 try
Tom Joseph5110c122018-03-23 17:55:40 +05301556 {
Yong Lif4e38512019-05-21 14:46:55 +08001557 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001558 enclosureIdentifyLed(true);
1559 }
1560 catch (const InternalFailure& e)
1561 {
1562 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001563 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301564 }
1565
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001566 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301567 {
Yong Lif4e38512019-05-21 14:46:55 +08001568 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001569 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001570 }
1571 // start the timer
1572 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001573 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001574 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301575 }
Tom Josephbed26992018-07-31 23:00:24 +05301576 else if (!identifyInterval)
1577 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001578 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301579 enclosureIdentifyLedOff();
1580 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001581 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301582}
1583
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001584namespace boot_options
1585{
1586
1587using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1588using IpmiValue = uint8_t;
1589constexpr auto ipmiDefault = 0;
1590
Patrick Venture0b02be92018-08-31 11:55:55 -07001591std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001592 {0x01, Source::Sources::Network},
1593 {0x02, Source::Sources::Disk},
1594 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001595 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001596 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001597
Patrick Venture0b02be92018-08-31 11:55:55 -07001598std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Yong Li5833cb62019-10-30 13:27:12 +08001599#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001600 {0x03, Mode::Modes::Safe},
Yong Li5833cb62019-10-30 13:27:12 +08001601#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001602 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001603 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001604
Patrick Venture0b02be92018-08-31 11:55:55 -07001605std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001606 {Source::Sources::Network, 0x01},
1607 {Source::Sources::Disk, 0x02},
1608 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001609 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001610 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001611
Patrick Venture0b02be92018-08-31 11:55:55 -07001612std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Yong Li5833cb62019-10-30 13:27:12 +08001613#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001614 {Mode::Modes::Safe, 0x03},
Yong Li5833cb62019-10-30 13:27:12 +08001615#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001616 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001617 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001618
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001619} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001620
Marri Devender Rao81719702018-05-07 00:53:48 -05001621/** @brief Set the property value for boot source
1622 * @param[in] source - boot source value
1623 * @return On failure return IPMI error.
1624 */
1625static ipmi_ret_t setBootSource(const Source::Sources& source)
1626{
1627 using namespace chassis::internal;
1628 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001629 std::variant<std::string> property = convertForMessage(source);
James Feist225dec82019-11-26 16:25:06 -08001630 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001631 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1632 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001633 auto method = dbus.new_method_call(
1634 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1635 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001636 method.append(bootSourceIntf, "BootSource", property);
1637 auto reply = dbus.call(method);
1638 if (reply.is_method_error())
1639 {
1640 log<level::ERR>("Error in BootSource Set");
1641 report<InternalFailure>();
1642 return IPMI_CC_UNSPECIFIED_ERROR;
1643 }
1644 return IPMI_CC_OK;
1645}
1646
Patrick Venture0b02be92018-08-31 11:55:55 -07001647/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001648 * @param[in] mode - boot mode value
1649 * @return On failure return IPMI error.
1650 */
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001651static ipmi::Cc setBootMode(const Mode::Modes& mode)
Marri Devender Rao81719702018-05-07 00:53:48 -05001652{
1653 using namespace chassis::internal;
1654 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001655 std::variant<std::string> property = convertForMessage(mode);
James Feist225dec82019-11-26 16:25:06 -08001656 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001657 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1658 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001659 auto method = dbus.new_method_call(
1660 objects.service(bootModeSetting, bootModeIntf).c_str(),
1661 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001662 method.append(bootModeIntf, "BootMode", property);
1663 auto reply = dbus.call(method);
1664 if (reply.is_method_error())
1665 {
1666 log<level::ERR>("Error in BootMode Set");
1667 report<InternalFailure>();
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001668 return ipmi::ccUnspecifiedError;
Marri Devender Rao81719702018-05-07 00:53:48 -05001669 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001670 return ipmi::ccSuccess;
Marri Devender Rao81719702018-05-07 00:53:48 -05001671}
1672
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001673/** @brief implements the Get Chassis system boot option
1674 * @param bootOptionParameter - boot option parameter selector
1675 * @param reserved1 - reserved bit
1676 * @param setSelector - selects a particular block or set of parameters
1677 * under the given parameter selector
1678 * write as 00h if parameter doesn't use a setSelector
1679 * @param blockSelector- selects a particular block within a set of
1680 * parameters write as 00h if parameter doesn't use a
1681 * blockSelector
1682 *
1683 * @return IPMI completion code plus response data
1684 * @return Payload contains below parameters:
1685 * version - parameter version
1686 * bootOptionParameter - boot option parameter selector
Patrick Venture8b1c3032020-09-15 09:43:03 -07001687 * parmIndicator - parameter valid/invalid indicator
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001688 * data - configuration parameter data
1689 */
1690ipmi::RspType<ipmi::message::Payload>
1691 ipmiChassisGetSysBootOptions(uint7_t bootOptionParameter, bool reserved1,
1692
1693 uint8_t setSelector, uint8_t blockSelector)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001694{
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001695 if (reserved1)
1696 {
1697 return ipmi::responseInvalidFieldRequest();
1698 }
1699
1700 constexpr uint4_t version = 0x01;
1701 ipmi::message::Payload response;
1702 response.pack(version, uint4_t{});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001703 using namespace boot_options;
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001704
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001705 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001706
shgoupfd84fbbf2015-12-17 10:05:51 +08001707 /*
1708 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1709 * This is the only parameter used by petitboot.
1710 */
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001711 if (static_cast<uint8_t>(bootOptionParameter) ==
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001712 static_cast<uint8_t>(BootOptionParameter::bootFlags))
Patrick Venture0b02be92018-08-31 11:55:55 -07001713 {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001714 using namespace chassis::internal;
1715 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001716
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001717 try
ratagupta6f6bff2016-04-04 06:20:11 -05001718 {
James Feist225dec82019-11-26 16:25:06 -08001719 settings::Objects& objects = getObjects();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001720 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1721 const auto& bootSourceSetting =
1722 std::get<settings::Path>(bootSetting);
1723 auto oneTimeEnabled =
1724 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001725 auto method = dbus.new_method_call(
1726 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1727 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001728 method.append(bootSourceIntf, "BootSource");
1729 auto reply = dbus.call(method);
1730 if (reply.is_method_error())
1731 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001732 log<level::ERR>(
1733 "ipmiChassisGetSysBootOptions: Error in BootSource Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001734 report<InternalFailure>();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001735 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001736 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001737 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001738 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001739 auto bootSource =
1740 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001741
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001742 bootSetting = settings::boot::setting(objects, bootModeIntf);
1743 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1744 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001745 objects.service(bootModeSetting, bootModeIntf).c_str(),
1746 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001747 method.append(bootModeIntf, "BootMode");
1748 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 BootMode 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 }
1756 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001757 auto bootMode =
1758 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001759
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001760 bootOption = sourceDbusToIpmi.at(bootSource);
1761 if ((Mode::Modes::Regular == bootMode) &&
1762 (Source::Sources::Default == bootSource))
1763 {
1764 bootOption = ipmiDefault;
1765 }
1766 else if (Source::Sources::Default == bootSource)
1767 {
1768 bootOption = modeDbusToIpmi.at(bootMode);
1769 }
ratagupta6f6bff2016-04-04 06:20:11 -05001770
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001771 uint8_t bootOptionParam = oneTimeEnabled
1772 ? setParmBootFlagsValidOneTime
1773 : setParmBootFlagsValidPermanent;
1774 response.pack(bootOptionParameter, reserved1, bootOptionParam,
1775 uint2_t{}, uint4_t{bootOption}, uint2_t{}, uint8_t{},
1776 uint8_t{}, uint8_t{});
1777 return ipmi::responseSuccess(std::move(response));
ratagupta6f6bff2016-04-04 06:20:11 -05001778 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001779 catch (InternalFailure& e)
1780 {
James Feist225dec82019-11-26 16:25:06 -08001781 cache::objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001782 report<InternalFailure>();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001783 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001784 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001785 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001786 else
1787 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001788 if ((bootOptionParameter >= oemParmStart) &&
1789 (bootOptionParameter <= oemParmEnd))
1790 {
1791 if (static_cast<uint8_t>(bootOptionParameter) ==
1792 static_cast<uint8_t>(BootOptionParameter::opalNetworkSettings))
1793 {
1794 response.pack(bootOptionParameter, reserved1);
1795 int ret = getHostNetworkData(response);
1796 if (ret < 0)
1797 {
1798 response.trailingOk = true;
1799 log<level::ERR>(
1800 "getHostNetworkData failed for GetSysBootOptions.");
1801 return ipmi::responseUnspecifiedError();
1802 }
1803 else
1804 {
1805 return ipmi::responseSuccess(std::move(response));
1806 }
1807 }
1808 }
1809 else
1810 {
1811 log<level::ERR>(
1812 "ipmiChassisGetSysBootOptions: Unsupported parameter",
1813 entry("PARAM=0x%x", static_cast<uint8_t>(bootOptionParameter)));
1814 return ipmi::responseUnspecifiedError();
1815 }
shgoupfd84fbbf2015-12-17 10:05:51 +08001816 }
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001817 return ipmi::responseUnspecifiedError();
shgoupfd84fbbf2015-12-17 10:05:51 +08001818}
1819
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001820ipmi::RspType<> ipmiChassisSetSysBootOptions(ipmi::Context::ptr ctx,
1821 uint7_t parameterSelector,
1822 bool parameterIsValid,
1823 ipmi::message::Payload& data)
shgoupfd84fbbf2015-12-17 10:05:51 +08001824{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001825 using namespace boot_options;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001826 ipmi::Cc rc;
shgoupfd84fbbf2015-12-17 10:05:51 +08001827
1828 /* 000101
1829 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1830 * This is the only parameter used by petitboot.
1831 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001832
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001833 if (parameterSelector ==
1834 static_cast<uint7_t>(BootOptionParameter::bootFlags))
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001835 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001836 uint5_t rsvd;
1837 bool validFlag;
1838 bool permanent;
1839 bool biosBootType;
1840 bool lockOutResetButton;
1841 bool screenBlank;
1842 uint4_t bootDeviceSelector;
1843 bool lockKeyboard;
1844 bool cmosClear;
1845 uint8_t data3;
1846 uint4_t biosInfo;
1847 uint4_t rsvd1;
1848 uint5_t deviceInstance;
1849 uint3_t rsvd2;
1850
1851 if (data.unpack(rsvd, biosBootType, permanent, validFlag,
1852 lockOutResetButton, screenBlank, bootDeviceSelector,
1853 lockKeyboard, cmosClear, data3, biosInfo, rsvd1,
1854 deviceInstance, rsvd2) != 0 ||
1855 !data.fullyUnpacked())
1856 {
1857 return ipmi::responseReqDataLenInvalid();
1858 }
1859 if (rsvd || rsvd1 || rsvd2)
1860 {
1861 return ipmi::responseInvalidFieldRequest();
1862 }
1863
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001864 using namespace chassis::internal;
1865 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001866 auto oneTimeEnabled = false;
1867 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301868 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001869 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001870
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001871 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001872 {
James Feist225dec82019-11-26 16:25:06 -08001873 settings::Objects& objects = getObjects();
1874
Patrick Venture0b02be92018-08-31 11:55:55 -07001875 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301876
1877 oneTimeEnabled =
1878 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1879
1880 /*
1881 * Check if the current boot setting is onetime or permanent, if the
1882 * request in the command is otherwise, then set the "Enabled"
1883 * property in one_time object path to 'True' to indicate onetime
1884 * and 'False' to indicate permanent.
1885 *
1886 * Once the onetime/permanent setting is applied, then the bootMode
1887 * and bootSource is updated for the corresponding object.
1888 */
1889 if ((permanent && oneTimeEnabled) ||
1890 (!permanent && !oneTimeEnabled))
1891 {
1892 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1893
Patrick Venture0b02be92018-08-31 11:55:55 -07001894 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1895 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301896 }
1897
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001898 auto modeItr =
1899 modeIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
1900 auto sourceItr =
1901 sourceIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001902 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001903 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001904 rc = setBootSource(sourceItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001905 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001906 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001907 log<level::ERR>("ipmiChassisSetSysBootOptions: Error in "
1908 "setting boot source");
1909 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001910 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001911 // If a set boot device is mapping to a boot source, then reset
1912 // the boot mode D-Bus property to default.
1913 // This way the ipmid code can determine which property is not
1914 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001915 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001916 {
1917 setBootMode(Mode::Modes::Regular);
1918 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001919 }
1920 if (modeIpmiToDbus.end() != modeItr)
1921 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001922 rc = setBootMode(modeItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001923 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001924 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001925 log<level::ERR>("ipmiChassisSetSysBootOptions: Error in "
1926 "setting boot mode");
1927 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001928 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001929 // If a set boot device is mapping to a boot mode, then reset
1930 // the boot source D-Bus property to default.
1931 // This way the ipmid code can determine which property is not
1932 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001933 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001934 {
1935 setBootSource(Source::Sources::Default);
1936 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001937 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001938 if ((modeIpmiToDbus.end() == modeItr) &&
1939 (sourceIpmiToDbus.end() == sourceItr))
1940 {
1941 // return error if boot option is not supported
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001942 log<level::ERR>(
1943 "ipmiChassisSetSysBootOptions: Boot option not supported");
1944 return ipmi::responseInvalidFieldRequest();
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001945 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001946 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001947 catch (sdbusplus::exception_t& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001948 {
James Feist225dec82019-11-26 16:25:06 -08001949 objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001950 report<InternalFailure>();
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301951 log<level::ERR>(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001952 "ipmiChassisSetSysBootOptions: Error in setting Boot "
1953 "flag parameters");
1954 return ipmi::responseUnspecifiedError();
Ratan Guptafd28dd72016-08-01 04:58:01 -05001955 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001956 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001957 else if (parameterSelector ==
1958 static_cast<uint7_t>(BootOptionParameter::bootInfo))
Patrick Venture0b02be92018-08-31 11:55:55 -07001959 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001960 uint8_t writeMak;
1961 uint5_t bootInitiatorAckData;
1962 uint3_t rsvd;
1963
1964 if (data.unpack(writeMak, bootInitiatorAckData, rsvd) != 0 ||
1965 !data.fullyUnpacked())
1966 {
1967 return ipmi::responseReqDataLenInvalid();
1968 }
1969 if (rsvd)
1970 {
1971 return ipmi::responseInvalidFieldRequest();
1972 }
1973 // (ccSuccess). There is no implementation in OpenBMC for this
Tom Josephf536c902017-09-25 18:08:15 +05301974 // parameter. This is added to support the ipmitool command `chassis
1975 // bootdev` which sends set on parameter #4, before setting the boot
1976 // flags.
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001977 log<level::INFO>("ipmiChassisSetSysBootOptions: bootInfo parameter set "
1978 "successfully");
1979 data.trailingOk = true;
1980 return ipmi::responseSuccess();
Patrick Venture0b02be92018-08-31 11:55:55 -07001981 }
1982 else
1983 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001984 if ((parameterSelector >= static_cast<uint7_t>(oemParmStart)) &&
1985 (parameterSelector <= static_cast<uint7_t>(oemParmEnd)))
1986 {
1987 if (parameterSelector ==
1988 static_cast<uint7_t>(BootOptionParameter::opalNetworkSettings))
1989 {
1990 ipmi::Cc ret = setHostNetworkData(data);
1991 if (ret != ipmi::ccSuccess)
1992 {
1993 log<level::ERR>("ipmiChassisSetSysBootOptions: Error in "
1994 "setHostNetworkData");
1995 data.trailingOk = true;
1996 return ipmi::response(ret);
1997 }
1998 data.trailingOk = true;
1999 return ipmi::responseSuccess();
2000 }
2001 else
2002 {
2003 log<level::ERR>(
2004 "ipmiChassisSetSysBootOptions: Unsupported parameters",
2005 entry("PARAM=0x%x",
2006 static_cast<uint8_t>(parameterSelector)));
2007 data.trailingOk = true;
2008 return ipmi::responseParmNotSupported();
2009 }
2010 }
2011 data.trailingOk = true;
2012 return ipmi::responseParmNotSupported();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002013 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002014 return ipmi::responseSuccess();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002015}
2016
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002017/** @brief implements Get POH counter command
2018 * @parameter
2019 * - none
2020 * @returns IPMI completion code plus response data
2021 * - minPerCount - Minutes per count
2022 * - counterReading - counter reading
2023 */
2024ipmi::RspType<uint8_t, // Minutes per count
2025 uint32_t // Counter reading
2026 >
2027 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002028{
2029 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002030 try
2031 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002032 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
2033 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002034 }
2035 catch (std::exception& e)
2036 {
2037 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002038 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002039 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002040}
2041
Jason M. Billsbc996a32019-06-17 15:46:37 -07002042ipmi::RspType<uint3_t, // policy support
2043 uint5_t // reserved
2044 >
Vernon Mauerye278ead2018-10-09 09:23:43 -07002045 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
Jason M. Billsbc996a32019-06-17 15:46:37 -07002046 uint3_t policy, uint5_t reserved)
Yong Lic6713cf2018-09-12 12:35:13 +08002047{
Yong Lic6713cf2018-09-12 12:35:13 +08002048 power_policy::DbusValue value =
2049 power_policy::RestorePolicy::Policy::AlwaysOff;
2050
Jason M. Billsbc996a32019-06-17 15:46:37 -07002051 if (reserved || (policy > power_policy::noChange))
Yong Lic6713cf2018-09-12 12:35:13 +08002052 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07002053 phosphor::logging::log<level::ERR>(
2054 "Reserved request parameter",
2055 entry("REQ=0x%x", static_cast<int>(policy)));
Jason M. Billsbc996a32019-06-17 15:46:37 -07002056 return ipmi::responseInvalidFieldRequest();
Yong Lic6713cf2018-09-12 12:35:13 +08002057 }
2058
Vernon Mauerye278ead2018-10-09 09:23:43 -07002059 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08002060 {
2061 // just return the supported policy
Jason M. Billsbc996a32019-06-17 15:46:37 -07002062 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002063 }
2064
2065 for (auto const& it : power_policy::dbusToIpmi)
2066 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07002067 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08002068 {
2069 value = it.first;
2070 break;
2071 }
2072 }
2073
2074 try
2075 {
James Feist225dec82019-11-26 16:25:06 -08002076 settings::Objects& objects = chassis::internal::cache::getObjects();
Yong Lic6713cf2018-09-12 12:35:13 +08002077 const settings::Path& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -08002078 objects.map.at(chassis::internal::powerRestoreIntf).front();
Vernon Mauery16b86932019-05-01 08:36:11 -07002079 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08002080
Vernon Mauerye278ead2018-10-09 09:23:43 -07002081 auto sdbusp = getSdBus();
2082 boost::system::error_code ec;
2083 sdbusp->yield_method_call<void>(
2084 yield, ec,
James Feist225dec82019-11-26 16:25:06 -08002085 objects
Yong Lic6713cf2018-09-12 12:35:13 +08002086 .service(powerRestoreSetting,
2087 chassis::internal::powerRestoreIntf)
2088 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07002089 powerRestoreSetting, ipmi::PROP_INTF, "Set",
2090 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
2091 property);
2092 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08002093 {
2094 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07002095 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002096 }
2097 }
2098 catch (InternalFailure& e)
2099 {
James Feist225dec82019-11-26 16:25:06 -08002100 chassis::internal::cache::objectsPtr.reset();
Yong Lic6713cf2018-09-12 12:35:13 +08002101 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07002102 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002103 }
2104
Jason M. Billsbc996a32019-06-17 15:46:37 -07002105 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002106}
2107
Kuiying Wang21addc52019-01-04 10:50:21 +08002108ipmi::RspType<> ipmiSetFrontPanelButtonEnables(
2109 ipmi::Context::ptr ctx, bool disablePowerButton, bool disableResetButton,
2110 bool disableDiagButton, bool disableSleepButton, uint4_t reserved)
2111{
2112 using namespace chassis::internal;
2113
2114 // set power button Enabled property
2115 bool success = setButtonEnabled(ctx, powerButtonPath, powerButtonIntf,
2116 !disablePowerButton);
2117
2118 // set reset button Enabled property
2119 success &= setButtonEnabled(ctx, resetButtonPath, resetButtonIntf,
2120 !disableResetButton);
2121
2122 if (!success)
2123 {
2124 // not all buttons were successfully set
2125 return ipmi::responseUnspecifiedError();
2126 }
2127 return ipmi::responseSuccess();
2128}
2129
Adriana Kobylak40814c62015-10-27 15:58:44 -05002130void register_netfn_chassis_functions()
2131{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05002132 createIdentifyTimer();
2133
Tom05732372016-09-06 17:21:23 +05302134 // Get Chassis Capabilities
anil kumar appana43263c62019-05-27 12:45:04 +00002135 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2136 ipmi::chassis::cmdGetChassisCapabilities,
2137 ipmi::Privilege::User, ipmiGetChassisCap);
Nan Li8d15fb42016-08-16 22:29:40 +08002138
Kuiying Wang21addc52019-01-04 10:50:21 +08002139 // Set Front Panel Button Enables
2140 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2141 ipmi::chassis::cmdSetFrontPanelButtonEnables,
2142 ipmi::Privilege::Admin,
2143 ipmiSetFrontPanelButtonEnables);
2144
Yong Liae4b0402018-11-02 11:12:14 +08002145 // Set Chassis Capabilities
anil kumar appana894d0222019-05-27 16:32:14 +00002146 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2147 ipmi::chassis::cmdSetChassisCapabilities,
2148 ipmi::Privilege::User, ipmiSetChassisCap);
Yong Liae4b0402018-11-02 11:12:14 +08002149
Tom05732372016-09-06 17:21:23 +05302150 // <Get System Boot Options>
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00002151 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2152 ipmi::chassis::cmdGetSystemBootOptions,
2153 ipmi::Privilege::Operator,
2154 ipmiChassisGetSysBootOptions);
Adriana Kobylak40814c62015-10-27 15:58:44 -05002155
Tom05732372016-09-06 17:21:23 +05302156 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07002157 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2158 ipmi::chassis::cmdGetChassisStatus,
2159 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08002160
Vijay Khemka074f64d2020-03-03 15:08:43 -08002161 // <Chassis Get System Restart Cause>
2162 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2163 ipmi::chassis::cmdGetSystemRestartCause,
2164 ipmi::Privilege::User, ipmiGetSystemRestartCause);
2165
Tom05732372016-09-06 17:21:23 +05302166 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00002167 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2168 ipmi::chassis::cmdChassisControl,
2169 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08002170
Tom Joseph5110c122018-03-23 17:55:40 +05302171 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07002172 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2173 ipmi::chassis::cmdChassisIdentify,
2174 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05302175
Tom05732372016-09-06 17:21:23 +05302176 // <Set System Boot Options>
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002177 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2178 ipmi::chassis::cmdSetSystemBootOptions,
2179 ipmi::Privilege::Operator,
2180 ipmiChassisSetSysBootOptions);
2181
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002182 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002183 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2184 ipmi::chassis::cmdGetPohCounter,
2185 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08002186
2187 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07002188 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2189 ipmi::chassis::cmdSetPowerRestorePolicy,
2190 ipmi::Privilege::Operator,
2191 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06002192}