blob: 088adfcbbef65781b4313a6d19f207ee1843089c [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
Patrick Venture0b02be92018-08-31 11:55:55 -070037// Defines
38#define SET_PARM_VERSION 0x01
Vernon Mauery1181af72018-10-08 12:05:00 -070039#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40
40#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80
41#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0
ratagupta6f6bff2016-04-04 06:20:11 -050042
Lei YU4b0ddb62019-01-25 16:43:50 +080043std::unique_ptr<phosphor::Timer> identifyTimer
44 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050045
Yong Lif4e38512019-05-21 14:46:55 +080046static ChassisIDState chassisIDState = ChassisIDState::reserved;
47
Patrick Venture0b02be92018-08-31 11:55:55 -070048constexpr size_t SIZE_MAC = 18;
49constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
50 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
51 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050052constexpr size_t SIZE_PREFIX = 7;
53constexpr size_t MAX_PREFIX_VALUE = 32;
54constexpr size_t SIZE_COOKIE = 4;
55constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053056constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053057
Patrick Venture0b02be92018-08-31 11:55:55 -070058// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053059static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070060 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050061
62static constexpr size_t COOKIE_OFFSET = 1;
63static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053064static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050065static constexpr size_t MAC_OFFSET = 9;
66static constexpr size_t ADDRTYPE_OFFSET = 16;
67static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050068
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050069static constexpr size_t encIdentifyObjectsSize = 1;
70static constexpr size_t chassisIdentifyReqLength = 2;
71static constexpr size_t identifyIntervalPos = 0;
72static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080073
Adriana Kobylak40814c62015-10-27 15:58:44 -050074void register_netfn_chassis_functions() __attribute__((constructor));
75
shgoupfd84fbbf2015-12-17 10:05:51 +080076// Host settings in dbus
77// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070078const char* settings_object_name = "/org/openbmc/settings/host0";
79const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070080const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053081 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080082
Ratan Guptadcb10672017-07-10 10:33:50 +053083constexpr auto SETTINGS_ROOT = "/";
84constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053085
86constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
87constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
88
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050089static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
90static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070091 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050092static constexpr auto pOHCounterProperty = "POHCounter";
93static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080094const static constexpr char chassisCapIntf[] =
95 "xyz.openbmc_project.Control.ChassisCapabilities";
96const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
97const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
98const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
99const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
100const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
101const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
102static constexpr uint8_t chassisCapFlagMask = 0x0f;
103static constexpr uint8_t chassisCapAddrMask = 0xfe;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700104static constexpr const char* powerButtonIntf =
105 "xyz.openbmc_project.Chassis.Buttons.Power";
106static constexpr const char* powerButtonPath =
107 "/xyz/openbmc_project/Chassis/Buttons/Power0";
108static constexpr const char* resetButtonIntf =
109 "xyz.openbmc_project.Chassis.Buttons.Reset";
110static constexpr const char* resetButtonPath =
111 "/xyz/openbmc_project/Chassis/Buttons/Reset0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530112
Nan Li8d15fb42016-08-16 22:29:40 +0800113typedef struct
114{
115 uint8_t cap_flags;
116 uint8_t fru_info_dev_addr;
117 uint8_t sdr_dev_addr;
118 uint8_t sel_dev_addr;
119 uint8_t system_management_dev_addr;
120 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700121} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800122
Nan Lifdd8ec52016-08-28 03:57:40 +0800123typedef struct
124{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500125 uint8_t cur_power_state;
126 uint8_t last_power_event;
127 uint8_t misc_power_state;
128 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700129} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800130
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530131// Phosphor Host State manager
132namespace State = sdbusplus::xyz::openbmc_project::State::server;
133
Vernon Mauery185b9f82018-07-20 10:52:36 -0700134namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500135
Ratan Guptadcb10672017-07-10 10:33:50 +0530136using namespace phosphor::logging;
137using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500138using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700139
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500140namespace chassis
141{
142namespace internal
143{
144
145constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
146constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500147constexpr auto powerRestoreIntf =
148 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500149sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
150
151namespace cache
152{
153
James Feist225dec82019-11-26 16:25:06 -0800154std::unique_ptr<settings::Objects> objectsPtr = nullptr;
155
156settings::Objects& getObjects()
157{
158 if (objectsPtr == nullptr)
159 {
160 objectsPtr = std::make_unique<settings::Objects>(
161 dbus, std::vector<std::string>{bootModeIntf, bootSourceIntf,
162 powerRestoreIntf});
163 }
164 return *objectsPtr;
165}
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500166
167} // namespace cache
168} // namespace internal
169} // namespace chassis
170
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500171namespace poh
172{
173
174constexpr auto minutesPerCount = 60;
175
176} // namespace poh
177
Patrick Venture0b02be92018-08-31 11:55:55 -0700178struct get_sys_boot_options_t
179{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500180 uint8_t parameter;
181 uint8_t set;
182 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700183} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500184
Patrick Venture0b02be92018-08-31 11:55:55 -0700185struct get_sys_boot_options_response_t
186{
shgoupfd84fbbf2015-12-17 10:05:51 +0800187 uint8_t version;
188 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500189 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700190} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800191
Patrick Venture0b02be92018-08-31 11:55:55 -0700192struct set_sys_boot_options_t
193{
shgoupfd84fbbf2015-12-17 10:05:51 +0800194 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500195 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700196} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500197
Ratan Guptadcb10672017-07-10 10:33:50 +0530198int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500199{
Ratan Guptadcb10672017-07-10 10:33:50 +0530200 ipmi::PropertyMap properties;
201 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530202 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500203
Ratan Guptadcb10672017-07-10 10:33:50 +0530204 try
205 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700206 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530207 // objects,to handle such cases we are interested on that object
208 // which are on interested busname.
209 // Currenlty mapper doesn't give the readable busname(gives busid)
210 // so we can't match with bus name so giving some object specific info
211 // as SETTINGS_MATCH.
212 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500213
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530214 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530215
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530216 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
217 SETTINGS_ROOT, SETTINGS_MATCH);
218
219 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
220 SETTINGS_ROOT, SETTINGS_MATCH);
221
Patrick Venture0b02be92018-08-31 11:55:55 -0700222 properties = ipmi::getAllDbusProperties(
223 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
224 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
225 macObjectInfo.first, MAC_INTERFACE,
226 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530227
Vernon Maueryf442e112019-04-09 11:44:36 -0700228 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530229
Vernon Maueryf442e112019-04-09 11:44:36 -0700230 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530231
Vernon Maueryf442e112019-04-09 11:44:36 -0700232 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530233
Patrick Venture0b02be92018-08-31 11:55:55 -0700234 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700235 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700236 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
237 ? 1
238 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530239
Vernon Maueryf442e112019-04-09 11:44:36 -0700240 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530241
Ratan Guptad70f4532017-08-04 02:07:31 +0530242 // it is expected here that we should get the valid data
243 // but we may also get the default values.
244 // Validation of the data is done by settings.
245 //
246 // if mac address is default mac address then
247 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530248 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530249 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700250 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530251 rc = -1;
252 return rc;
253 }
254 // if addr is static then ipaddress,gateway,prefix
255 // should not be default one,don't send blank override.
256 if (isStatic)
257 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700258 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
259 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530260 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700261 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530262 rc = -1;
263 return rc;
264 }
265 }
266
Patrick Venture0b02be92018-08-31 11:55:55 -0700267 sscanf(
268 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
269 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
270 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
271 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530272
Ratan Guptadcb10672017-07-10 10:33:50 +0530273 respptr->data[MAC_OFFSET + 6] = 0x00;
274
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700275 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
276 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530277
Vernon Maueryf442e112019-04-09 11:44:36 -0700278 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
279 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
280 ? AF_INET
281 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530282
Patrick Venture0b02be92018-08-31 11:55:55 -0700283 addrSize = (addressFamily == AF_INET)
284 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
285 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530286
287 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530288 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700289 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530290
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530291 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
292
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700293 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530294
295 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
296
Ratan Guptad70f4532017-08-04 02:07:31 +0530297 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700298 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530299 }
300 catch (InternalFailure& e)
301 {
302 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700303 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530304 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500305 return rc;
306 }
307
Patrick Venture0b02be92018-08-31 11:55:55 -0700308 // PetiBoot-Specific
309 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700310 std::memcpy(respptr->data, net_conf_initial_bytes,
311 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500312
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700313 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530314
Ratan Guptafd28dd72016-08-01 04:58:01 -0500315#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700316 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500317
Ratan Guptadcb10672017-07-10 10:33:50 +0530318 for (uint8_t pos = 0; pos < index; pos++)
319 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700320 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530321 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500322#endif
323
Ratan Guptafd28dd72016-08-01 04:58:01 -0500324 return rc;
325}
326
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530327/** @brief convert IPv4 and IPv6 addresses from binary to text form.
328 * @param[in] family - IPv4/Ipv6
329 * @param[in] data - req data pointer.
330 * @param[in] offset - offset in the data.
331 * @param[in] addrSize - size of the data which needs to be read from offset.
332 * @returns address in text form.
333 */
334
Patrick Venture0b02be92018-08-31 11:55:55 -0700335std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
336 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530337{
338 char ipAddr[INET6_ADDRSTRLEN] = {};
339
Patrick Venture0b02be92018-08-31 11:55:55 -0700340 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530341 {
342 case AF_INET:
343 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700344 struct sockaddr_in addr4
345 {
346 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700347 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530348
Patrick Venture0b02be92018-08-31 11:55:55 -0700349 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530350
351 break;
352 }
353 case AF_INET6:
354 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700355 struct sockaddr_in6 addr6
356 {
357 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700358 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530359
Patrick Venture0b02be92018-08-31 11:55:55 -0700360 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530361
362 break;
363 }
364 default:
365 {
366 return {};
367 }
368 }
369
370 return ipAddr;
371}
372
Ratan Guptadcb10672017-07-10 10:33:50 +0530373int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500374{
Ratan Guptadcb10672017-07-10 10:33:50 +0530375 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500376 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700377 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530378 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700379 char addrOrigin{0};
380 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530381 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530382 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700383 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
384 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530385 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530386 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500387
Patrick Venture0b02be92018-08-31 11:55:55 -0700388 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500389 // version starts from sixth byte
390
Ratan Guptadcb10672017-07-10 10:33:50 +0530391 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500392 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530393 do
394 {
395 // cookie == 0x21 0x70 0x62 0x21
396 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700397 (net_conf_initial_bytes + COOKIE_OFFSET),
398 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530399 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700400 // cookie == 0
401 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
402 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530403 {
404 // need to zero out the network settings.
405 break;
406 }
407
408 log<level::ERR>("Invalid Cookie");
409 elog<InternalFailure>();
410 }
411
412 // vesion == 0x00 0x01
413 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700414 (net_conf_initial_bytes + VERSION_OFFSET),
415 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530416 {
417
418 log<level::ERR>("Invalid Version");
419 elog<InternalFailure>();
420 }
421
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700422 std::snprintf(
423 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
424 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
425 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
426 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530427
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700428 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
429 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530430
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530431 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530432 {
433 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530434 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530435 }
436
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530437 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700438 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
439 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530440
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530441 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530442
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700443 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
444 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530445
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530446 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
447
Ratan Gupta8c31d232017-08-13 05:49:43 +0530448 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530449 {
450 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
451 family = AF_INET6;
452 }
453
Patrick Venture0b02be92018-08-31 11:55:55 -0700454 ipAddress =
455 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530456
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530457 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
458
Patrick Venture0b02be92018-08-31 11:55:55 -0700459 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530460
Patrick Venture0b02be92018-08-31 11:55:55 -0700461 // Cookie == 0 or it is a valid cookie
462 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
463 std::to_string(prefix) + ",gateway="s + gateway +
464 ",mac="s + mac + ",addressOrigin="s +
465 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500466
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530467 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
468
469 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
470 SETTINGS_ROOT, SETTINGS_MATCH);
471 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
472 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530473 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530474 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700475 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530476 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700477 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530478 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700479 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530480 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700481 IP_INTERFACE, "Gateway", std::string(gateway));
482 ipmi::setDbusProperty(
483 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
484 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530485 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700486 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500487
Patrick Venture0b02be92018-08-31 11:55:55 -0700488 log<level::DEBUG>(
489 "Network configuration changed",
490 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500491 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530492 catch (InternalFailure& e)
493 {
494 commit<InternalFailure>();
495 return -1;
496 }
497
498 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500499}
500
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500501uint32_t getPOHCounter()
502{
503 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
504
Patrick Venture0b02be92018-08-31 11:55:55 -0700505 auto chassisStateObj =
506 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500507
Patrick Venture0b02be92018-08-31 11:55:55 -0700508 auto service =
509 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500510
Patrick Venture0b02be92018-08-31 11:55:55 -0700511 auto propValue =
512 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
513 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500514
Vernon Maueryf442e112019-04-09 11:44:36 -0700515 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500516}
517
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500518ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
519 ipmi_request_t request,
520 ipmi_response_t response,
521 ipmi_data_len_t data_len,
522 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500523{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500524 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800525 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500526 *data_len = 0;
527 return rc;
528}
529
Nan Li8d15fb42016-08-16 22:29:40 +0800530ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700531 ipmi_request_t request,
532 ipmi_response_t response,
533 ipmi_data_len_t data_len,
534 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800535{
536 // sd_bus error
537 ipmi_ret_t rc = IPMI_CC_OK;
538
539 ipmi_chassis_cap_t chassis_cap{};
540
Yong Liae4b0402018-11-02 11:12:14 +0800541 if (*data_len != 0)
542 {
543 return IPMI_CC_REQ_DATA_LEN_INVALID;
544 }
545
Nan Li8d15fb42016-08-16 22:29:40 +0800546 *data_len = sizeof(ipmi_chassis_cap_t);
547
Yong Liae4b0402018-11-02 11:12:14 +0800548 try
549 {
550 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800551
Yong Liae4b0402018-11-02 11:12:14 +0800552 ipmi::DbusObjectInfo chassisCapObject =
553 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800554
Yong Liae4b0402018-11-02 11:12:14 +0800555 // capabilities flags
556 // [7..4] - reserved
557 // [3] – 1b = provides power interlock (IPM 1.5)
558 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
559 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
560 // has capabilities
561 // to lock out external power control and reset button or
562 // front panel interfaces and/or detect tampering with those
563 // interfaces).
564 // [0] -1b = Chassis provides intrusion (physical security) sensor.
565 // set to default value 0x0.
566 ipmi::Value variant = ipmi::getDbusProperty(
567 bus, chassisCapObject.second, chassisCapObject.first,
568 chassisCapIntf, chassisCapFlagsProp);
Vernon Maueryf442e112019-04-09 11:44:36 -0700569 chassis_cap.cap_flags = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800570
Yong Liae4b0402018-11-02 11:12:14 +0800571 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
572 chassisCapObject.first, chassisCapIntf,
573 chassisFRUDevAddrProp);
574 // Chassis FRU info Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700575 chassis_cap.fru_info_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800576
Yong Liae4b0402018-11-02 11:12:14 +0800577 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
578 chassisCapObject.first, chassisCapIntf,
579 chassisSDRDevAddrProp);
580 // Chassis SDR Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700581 chassis_cap.sdr_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800582
Yong Liae4b0402018-11-02 11:12:14 +0800583 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
584 chassisCapObject.first, chassisCapIntf,
585 chassisSELDevAddrProp);
586 // Chassis SEL Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700587 chassis_cap.sel_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800588
Yong Liae4b0402018-11-02 11:12:14 +0800589 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
590 chassisCapObject.first, chassisCapIntf,
591 chassisSMDevAddrProp);
592 // Chassis System Management Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700593 chassis_cap.system_management_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800594
Yong Liae4b0402018-11-02 11:12:14 +0800595 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
596 chassisCapObject.first, chassisCapIntf,
597 chassisBridgeDevAddrProp);
598 // Chassis Bridge Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700599 chassis_cap.bridge_dev_addr = std::get<uint8_t>(variant);
Yong Liae4b0402018-11-02 11:12:14 +0800600 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
601 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
602 std::copy(chassisP, chassisP + *data_len, respP);
603 }
604 catch (std::exception& e)
605 {
606 log<level::ERR>(e.what());
607 rc = IPMI_CC_UNSPECIFIED_ERROR;
608 *data_len = 0;
609 return rc;
610 }
611
612 return rc;
613}
614
anil kumar appana894d0222019-05-27 16:32:14 +0000615/** @brief implements set chassis capalibities command
616 * @param intrusion - chassis intrusion
617 * @param fpLockout - frontpannel lockout
618 * @param reserved1 - skip one bit
619 * @param fruDeviceAddr - chassis FRU info Device Address
620 * @param sdrDeviceAddr - chassis SDR device address
621 * @param selDeviceAddr - chassis SEL device address
622 * @param smDeviceAddr - chassis system management device address
623 * @param bridgeDeviceAddr - chassis bridge device address
624 *
625 * @returns IPMI completion code
626 */
627ipmi::RspType<> ipmiSetChassisCap(bool intrusion, bool fpLockout,
628 uint6_t reserved1,
629
630 uint8_t fruDeviceAddr,
631
632 uint8_t sdrDeviceAddr,
633
634 uint8_t selDeviceAddr,
635
636 uint8_t smDeviceAddr,
637
638 uint8_t bridgeDeviceAddr)
Yong Liae4b0402018-11-02 11:12:14 +0800639{
Yong Liae4b0402018-11-02 11:12:14 +0800640
641 // check input data
anil kumar appana894d0222019-05-27 16:32:14 +0000642 if (reserved1 != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800643 {
anil kumar appana894d0222019-05-27 16:32:14 +0000644 log<level::ERR>("Unsupported request parameter");
645 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800646 }
647
anil kumar appana894d0222019-05-27 16:32:14 +0000648 if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800649 {
650 log<level::ERR>("Unsupported request parameter(FRU Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000651 entry("REQ=0x%x", fruDeviceAddr));
652 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800653 }
anil kumar appana894d0222019-05-27 16:32:14 +0000654 if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800655 {
656 log<level::ERR>("Unsupported request parameter(SDR Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000657 entry("REQ=0x%x", sdrDeviceAddr));
658 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800659 }
660
anil kumar appana894d0222019-05-27 16:32:14 +0000661 if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800662 {
663 log<level::ERR>("Unsupported request parameter(SEL Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000664 entry("REQ=0x%x", selDeviceAddr));
665 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800666 }
667
anil kumar appana894d0222019-05-27 16:32:14 +0000668 if ((smDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800669 {
anil kumar appana894d0222019-05-27 16:32:14 +0000670 log<level::ERR>("Unsupported request parameter(SM Addr)",
671 entry("REQ=0x%x", smDeviceAddr));
672 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800673 }
674
anil kumar appana894d0222019-05-27 16:32:14 +0000675 if ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800676 {
677 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000678 entry("REQ=0x%x", bridgeDeviceAddr));
679 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800680 }
681
anil kumar appana894d0222019-05-27 16:32:14 +0000682 uint8_t capFlags = (static_cast<uint8_t>(intrusion)) |
683 ((static_cast<uint8_t>(fpLockout)) << 1);
Yong Liae4b0402018-11-02 11:12:14 +0800684 try
685 {
686 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
687 ipmi::DbusObjectInfo chassisCapObject =
688 ipmi::getDbusObject(bus, chassisCapIntf);
689
690 ipmi::setDbusProperty(bus, chassisCapObject.second,
691 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000692 chassisCapFlagsProp, capFlags);
Yong Liae4b0402018-11-02 11:12:14 +0800693
694 ipmi::setDbusProperty(bus, chassisCapObject.second,
695 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000696 chassisFRUDevAddrProp, fruDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800697
698 ipmi::setDbusProperty(bus, chassisCapObject.second,
699 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000700 chassisSDRDevAddrProp, sdrDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800701
702 ipmi::setDbusProperty(bus, chassisCapObject.second,
703 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000704 chassisSELDevAddrProp, selDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800705
706 ipmi::setDbusProperty(bus, chassisCapObject.second,
707 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000708 chassisSMDevAddrProp, smDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800709
710 ipmi::setDbusProperty(bus, chassisCapObject.second,
711 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000712 chassisBridgeDevAddrProp, bridgeDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800713 }
714 catch (std::exception& e)
715 {
716 log<level::ERR>(e.what());
anil kumar appana894d0222019-05-27 16:32:14 +0000717 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800718 }
anil kumar appana894d0222019-05-27 16:32:14 +0000719 return ipmi::responseSuccess();
Nan Li8d15fb42016-08-16 22:29:40 +0800720}
721
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530722//------------------------------------------
723// Calls into Host State Manager Dbus object
724//------------------------------------------
725int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600726{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500727 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700728 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500729 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700730 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
731 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530732
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500733 // sd_bus error
734 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700735 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600736
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500737 // SD Bus error report mechanism.
738 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600739
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500740 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700741 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500742 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
743 if (rc < 0)
744 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700745 log<level::ERR>(
746 "Failed to get bus name",
747 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500748 return rc;
749 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530750
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500751 // Convert to string equivalent of the passed in transition enum.
752 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530753
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500754 rc = sd_bus_call_method(bus_type, // On the system bus
755 busname, // Service to contact
756 HOST_STATE_MANAGER_ROOT, // Object path
757 DBUS_PROPERTY_IFACE, // Interface name
758 "Set", // Method to be called
759 &bus_error, // object to return error
760 nullptr, // Response buffer if any
761 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700762 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
763 request.c_str());
764 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500765 {
766 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530767 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500768 }
769 else
770 {
771 log<level::INFO>("Transition request initiated successfully");
772 }
vishwa36993272015-11-20 12:43:49 -0600773
774 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500775 free(busname);
vishwa36993272015-11-20 12:43:49 -0600776
Sergey Solomineb9b8142016-08-23 09:07:28 -0500777 return rc;
vishwa36993272015-11-20 12:43:49 -0600778}
779
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800780//------------------------------------------
781// Set Enabled property to inform NMI source
782// handling to trigger a NMI_OUT BSOD.
783//------------------------------------------
784int setNmiProperty(const bool value)
785{
786 constexpr const char* nmiSourceObjPath =
787 "/xyz/openbmc_project/Chassis/Control/NMISource";
788 constexpr const char* nmiSourceIntf =
789 "xyz.openbmc_project.Chassis.Control.NMISource";
790 std::string bmcSourceSignal = "xyz.openbmc_project.Chassis.Control."
791 "NMISource.BMCSourceSignal.ChassisCmd";
792 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
793
794 try
795 {
796 auto service = ipmi::getService(*busp, nmiSourceIntf, nmiSourceObjPath);
797 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
798 "BMCSource", bmcSourceSignal);
799 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
800 "Enabled", value);
801 }
802 catch (std::exception& e)
803 {
804 log<level::ERR>("Failed to trigger NMI_OUT",
805 entry("EXCEPTION=%s", e.what()));
806 return -1;
807 }
808
809 return 0;
810}
811
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500812namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500813{
Nan Lifdd8ec52016-08-28 03:57:40 +0800814
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500815using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
816using IpmiValue = uint8_t;
817using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800818
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700819const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500820 {RestorePolicy::Policy::AlwaysOff, 0x00},
821 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700822 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800823
Yong Lic6713cf2018-09-12 12:35:13 +0800824static constexpr uint8_t noChange = 0x03;
825static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700826
827/* helper function for Get Chassis Status Command
828 */
829std::optional<uint2_t> getPowerRestorePolicy()
830{
831 uint2_t restorePolicy = 0;
832 using namespace chassis::internal;
833
James Feist225dec82019-11-26 16:25:06 -0800834 settings::Objects& objects = cache::getObjects();
835
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700836 try
837 {
838 const auto& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -0800839 objects.map.at(powerRestoreIntf).front();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700840 ipmi::Value result = ipmi::getDbusProperty(
841 *getSdBus(),
James Feist225dec82019-11-26 16:25:06 -0800842 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700843 powerRestoreSetting.c_str(), powerRestoreIntf,
844 "PowerRestorePolicy");
845 auto powerRestore = RestorePolicy::convertPolicyFromString(
846 std::get<std::string>(result));
847 restorePolicy = dbusToIpmi.at(powerRestore);
848 }
849 catch (const std::exception& e)
850 {
851 log<level::ERR>(
852 "Failed to fetch pgood property", entry("ERROR=%s", e.what()),
James Feist225dec82019-11-26 16:25:06 -0800853 entry("PATH=%s", objects.map.at(powerRestoreIntf).front().c_str()),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700854 entry("INTERFACE=%s", powerRestoreIntf));
James Feist225dec82019-11-26 16:25:06 -0800855 cache::objectsPtr.reset();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700856 return std::nullopt;
857 }
858 return std::make_optional(restorePolicy);
859}
860
861/*
862 * getPowerStatus
863 * helper function for Get Chassis Status Command
864 * return - optional value for pgood (no value on error)
865 */
866std::optional<bool> getPowerStatus()
867{
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700868 bool powerGood = false;
869 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
870 try
871 {
Jason M. Bills3de424c2019-05-21 09:57:16 -0700872 constexpr const char* chassisStatePath =
873 "/xyz/openbmc_project/state/chassis0";
874 constexpr const char* chassisStateIntf =
875 "xyz.openbmc_project.State.Chassis";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700876 auto service =
Jason M. Bills3de424c2019-05-21 09:57:16 -0700877 ipmi::getService(*busp, chassisStateIntf, chassisStatePath);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700878
Jason M. Bills3de424c2019-05-21 09:57:16 -0700879 ipmi::Value powerState =
880 ipmi::getDbusProperty(*busp, service, chassisStatePath,
881 chassisStateIntf, "CurrentPowerState");
882 powerGood = std::get<std::string>(powerState) ==
883 "xyz.openbmc_project.State.Chassis.PowerState.On";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700884 }
885 catch (const std::exception& e)
886 {
887 try
888 {
889 // FIXME: some legacy modules use the older path; try that next
890 constexpr const char* legacyPwrCtrlObj =
891 "/org/openbmc/control/power0";
892 constexpr const char* legacyPwrCtrlIntf =
893 "org.openbmc.control.Power";
894 auto service =
895 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
896
897 ipmi::Value variant = ipmi::getDbusProperty(
898 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
899 powerGood = static_cast<bool>(std::get<int>(variant));
900 }
901 catch (const std::exception& e)
902 {
903 log<level::ERR>("Failed to fetch pgood property",
Jason M. Bills3de424c2019-05-21 09:57:16 -0700904 entry("ERROR=%s", e.what()));
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700905 return std::nullopt;
906 }
907 }
908 return std::make_optional(powerGood);
909}
910
Yong Li70ce7352019-05-16 21:15:27 +0800911/*
912 * getACFailStatus
913 * helper function for Get Chassis Status Command
914 * return - bool value for ACFail (false on error)
915 */
916bool getACFailStatus()
917{
918 constexpr const char* powerControlObj =
919 "/xyz/openbmc_project/Chassis/Control/Power0";
920 constexpr const char* powerControlIntf =
921 "xyz.openbmc_project.Chassis.Control.Power";
922 bool acFail = false;
923 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
924 try
925 {
926 auto service =
927 ipmi::getService(*bus, powerControlIntf, powerControlObj);
928
929 ipmi::Value variant = ipmi::getDbusProperty(
930 *bus, service, powerControlObj, powerControlIntf, "PFail");
931 acFail = std::get<bool>(variant);
932 }
933 catch (const std::exception& e)
934 {
935 log<level::ERR>("Failed to fetch PFail property",
936 entry("ERROR=%s", e.what()),
937 entry("PATH=%s", powerControlObj),
938 entry("INTERFACE=%s", powerControlIntf));
939 }
940 return acFail;
941}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500942} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800943
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700944static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
945 const std::string& buttonIntf)
946{
947 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
948 bool buttonDisabled = false;
949 try
950 {
951 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
952 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
953 buttonIntf, "Enabled");
954 buttonDisabled = !std::get<bool>(enabled);
955 }
956 catch (sdbusplus::exception::SdBusError& e)
957 {
958 log<level::ERR>("Fail to get button Enabled property",
959 entry("PATH=%s", buttonPath.c_str()),
960 entry("ERROR=%s", e.what()));
961 return std::nullopt;
962 }
963 return std::make_optional(buttonDisabled);
964}
965
Nan Lifdd8ec52016-08-28 03:57:40 +0800966//----------------------------------------------------------------------
967// Get Chassis Status commands
968//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700969ipmi::RspType<bool, // Power is on
970 bool, // Power overload
971 bool, // Interlock
972 bool, // power fault
973 bool, // power control fault
974 uint2_t, // power restore policy
975 bool, // reserved
976
977 bool, // AC failed
978 bool, // last power down caused by a Power overload
979 bool, // last power down caused by a power interlock
980 bool, // last power down caused by power fault
981 bool, // last ‘Power is on’ state was entered via IPMI command
982 uint3_t, // reserved
983
984 bool, // Chassis intrusion active
985 bool, // Front Panel Lockout active
986 bool, // Drive Fault
987 bool, // Cooling/fan fault detected
988 uint2_t, // Chassis Identify State
989 bool, // Chassis Identify command and state info supported
990 bool, // reserved
991
992 bool, // Power off button disabled
993 bool, // Reset button disabled
994 bool, // Diagnostic Interrupt button disabled
995 bool, // Standby (sleep) button disabled
996 bool, // Power off button disable allowed
997 bool, // Reset button disable allowed
998 bool, // Diagnostic Interrupt button disable allowed
999 bool // Standby (sleep) button disable allowed
1000 >
1001 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +08001002{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001003 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001004 std::optional<uint2_t> restorePolicy =
1005 power_policy::getPowerRestorePolicy();
1006 std::optional<bool> powerGood = power_policy::getPowerStatus();
1007 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001008 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001009 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001010 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001011
1012 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001013 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001014 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001015 // allow disable if the interface is present
1016 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
1017 // default return the button is enabled (not disabled)
1018 bool powerButtonDisabled = false;
1019 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001020 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001021 // return the real value of the button status, if present
1022 powerButtonDisabled = *powerButtonReading;
1023 }
1024
1025 std::optional<bool> resetButtonReading =
1026 getButtonEnabled(resetButtonPath, resetButtonIntf);
1027 // allow disable if the interface is present
1028 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
1029 // default return the button is enabled (not disabled)
1030 bool resetButtonDisabled = false;
1031 if (resetButtonDisableAllow)
1032 {
1033 // return the real value of the button status, if present
1034 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001035 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001036
Yong Li70ce7352019-05-16 21:15:27 +08001037 bool powerDownAcFailed = power_policy::getACFailStatus();
1038
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001039 // This response has a lot of hard-coded, unsupported fields
1040 // They are set to false or 0
1041 constexpr bool powerOverload = false;
1042 constexpr bool chassisInterlock = false;
1043 constexpr bool powerFault = false;
1044 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001045 constexpr bool powerDownOverload = false;
1046 constexpr bool powerDownInterlock = false;
1047 constexpr bool powerDownPowerFault = false;
1048 constexpr bool powerStatusIPMI = false;
1049 constexpr bool chassisIntrusionActive = false;
1050 constexpr bool frontPanelLockoutActive = false;
1051 constexpr bool driveFault = false;
1052 constexpr bool coolingFanFault = false;
1053 // chassisIdentifySupport set because this command is implemented
1054 constexpr bool chassisIdentifySupport = true;
Yong Lif4e38512019-05-21 14:46:55 +08001055 uint2_t chassisIdentifyState = static_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001056 constexpr bool diagButtonDisabled = false;
1057 constexpr bool sleepButtonDisabled = false;
1058 constexpr bool diagButtonDisableAllow = false;
1059 constexpr bool sleepButtonDisableAllow = false;
1060
1061 return ipmi::responseSuccess(
1062 *powerGood, powerOverload, chassisInterlock, powerFault,
1063 powerControlFault, *restorePolicy,
1064 false, // reserved
1065
1066 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1067 powerDownPowerFault, powerStatusIPMI,
1068 uint3_t(0), // reserved
1069
1070 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1071 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1072 false, // reserved
1073
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001074 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001075 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1076 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001077}
Chris Austen7888c4d2015-12-03 15:26:20 -06001078
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301079//-------------------------------------------------------------
1080// Send a command to SoftPowerOff application to stop any timer
1081//-------------------------------------------------------------
1082int stop_soft_off_timer()
1083{
Patrick Venture0b02be92018-08-31 11:55:55 -07001084 constexpr auto iface = "org.freedesktop.DBus.Properties";
1085 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1086 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301087
Patrick Venture0b02be92018-08-31 11:55:55 -07001088 constexpr auto property = "ResponseReceived";
1089 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1090 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301091
1092 // Get the system bus where most system services are provided.
1093 auto bus = ipmid_get_sd_bus_connection();
1094
1095 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001096 // TODO openbmc/openbmc#1661 - Mapper refactor
1097 //
1098 // See openbmc/openbmc#1743 for some details but high level summary is that
1099 // for now the code will directly call the soft off interface due to a
1100 // race condition with mapper usage
1101 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001102 // char *busname = nullptr;
1103 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1104 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001105 //{
1106 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301107 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001108 // return r;
1109 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301110
1111 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001112 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001113 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1114 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301115 if (rc < 0)
1116 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301117 log<level::ERR>("Failed to set property in SoftPowerOff object",
1118 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301119 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001120
Patrick Venture0b02be92018-08-31 11:55:55 -07001121 // TODO openbmc/openbmc#1661 - Mapper refactor
1122 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301123 return rc;
1124}
1125
vishwa36993272015-11-20 12:43:49 -06001126//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001127// Create file to indicate there is no need for softoff notification to host
1128//----------------------------------------------------------------------
1129void indicate_no_softoff_needed()
1130{
1131 fs::path path{HOST_INBAND_REQUEST_DIR};
1132 if (!fs::is_directory(path))
1133 {
1134 fs::create_directory(path);
1135 }
1136
1137 // Add the host instance (default 0 for now) to the file name
1138 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001139 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001140 size++; // null
1141 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001142 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001143
1144 // Append file name to directory and create it
1145 path /= buf.get();
1146 std::ofstream(path.c_str());
1147}
1148
anil kumar appanadafff5f2019-04-27 18:06:00 +00001149/** @brief Implementation of chassis control command
1150 *
1151 * @param - chassisControl command byte
1152 *
1153 * @return Success or InvalidFieldRequest.
1154 */
1155ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001156{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001157 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001158 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001159 {
1160 case CMD_POWER_ON:
1161 rc = initiate_state_transition(State::Host::Transition::On);
1162 break;
1163 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301164 // This path would be hit in 2 conditions.
1165 // 1: When user asks for power off using ipmi chassis command 0x04
1166 // 2: Host asking for power off post shutting down.
1167
1168 // If it's a host requested power off, then need to nudge Softoff
1169 // application that it needs to stop the watchdog timer if running.
1170 // If it is a user requested power off, then this is not really
1171 // needed. But then we need to differentiate between user and host
1172 // calling this same command
1173
1174 // For now, we are going ahead with trying to nudge the soft off and
1175 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001176 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301177
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001178 // Only request the Off transition if the soft power off
1179 // application is not running
1180 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001181 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001182 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301183 // that it should not run. Not doing this will result in State
1184 // manager doing a default soft power off when asked for power
1185 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001186 indicate_no_softoff_needed();
1187
1188 // Now request the shutdown
1189 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001190 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001191 else
1192 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301193 log<level::INFO>("Soft off is running, so let shutdown target "
1194 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001195 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001196 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301197
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001198 case CMD_HARD_RESET:
1199 case CMD_POWER_CYCLE:
1200 // SPEC has a section that says certain implementations can trigger
1201 // PowerOn if power is Off when a command to power cycle is
1202 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001203
1204 // First create a file to indicate to the soft off application
1205 // that it should not run since this is a direct user initiated
1206 // power reboot request (i.e. a reboot request that is not
1207 // originating via a soft power off SMS request)
1208 indicate_no_softoff_needed();
1209
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001210 rc = initiate_state_transition(State::Host::Transition::Reboot);
1211 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301212
1213 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1214 // Request Host State Manager to do a soft power off
1215 rc = initiate_state_transition(State::Host::Transition::Off);
1216 break;
1217
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001218 case CMD_PULSE_DIAGNOSTIC_INTR:
1219 rc = setNmiProperty(true);
1220 break;
1221
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001222 default:
1223 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301224 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001225 entry("CMD=0x%X", chassisControl));
1226 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001227 }
1228 }
vishwa36993272015-11-20 12:43:49 -06001229
anil kumar appanadafff5f2019-04-27 18:06:00 +00001230 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1231 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001232}
1233
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001234/** @brief Return D-Bus connection string to enclosure identify LED object
1235 *
1236 * @param[in, out] connection - connection to D-Bus object
1237 * @return a IPMI return code
1238 */
1239std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301240{
Tom Joseph5110c122018-03-23 17:55:40 +05301241 // lookup enclosure_identify group owner(s) in mapper
1242 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001243 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1244 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301245
1246 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001247 static const std::vector<std::string> interfaces = {
1248 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301249 mapperCall.append(interfaces);
1250 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1251 if (mapperReply.is_method_error())
1252 {
1253 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001254 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301255 }
1256 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1257 mapperReply.read(mapperResp);
1258
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001259 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301260 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001261 log<level::ERR>(
1262 "Invalid number of enclosure identify objects.",
1263 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001264 elog<InternalFailure>();
1265 }
1266 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1267 return pair.first;
1268}
Tom Joseph5110c122018-03-23 17:55:40 +05301269
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001270/** @brief Turn On/Off enclosure identify LED
1271 *
1272 * @param[in] flag - true to turn on LED, false to turn off
1273 * @return a IPMI return code
1274 */
1275void enclosureIdentifyLed(bool flag)
1276{
1277 using namespace chassis::internal;
1278 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001279 auto msg = std::string("enclosureIdentifyLed(") +
1280 boost::lexical_cast<std::string>(flag) + ")";
1281 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001282 auto led =
1283 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1284 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001285 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001286 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001287 auto ledReply = dbus.call(led);
1288 if (ledReply.is_method_error())
1289 {
1290 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001291 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001292 elog<InternalFailure>();
1293 }
1294}
1295
1296/** @brief Callback method to turn off LED
1297 */
1298void enclosureIdentifyLedOff()
1299{
1300 try
1301 {
Yong Lif4e38512019-05-21 14:46:55 +08001302 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001303 enclosureIdentifyLed(false);
1304 }
1305 catch (const InternalFailure& e)
1306 {
1307 report<InternalFailure>();
1308 }
1309}
1310
1311/** @brief Create timer to turn on and off the enclosure LED
1312 */
1313void createIdentifyTimer()
1314{
1315 if (!identifyTimer)
1316 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001317 identifyTimer =
1318 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001319 }
1320}
1321
Vernon Mauery400cc782018-10-09 13:49:53 -07001322ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1323 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001324{
Vernon Mauery400cc782018-10-09 13:49:53 -07001325 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1326 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301327
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001328 if (identifyInterval || forceIdentify)
1329 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001330 // stop the timer if already started;
1331 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001332 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001333 try
Tom Joseph5110c122018-03-23 17:55:40 +05301334 {
Yong Lif4e38512019-05-21 14:46:55 +08001335 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001336 enclosureIdentifyLed(true);
1337 }
1338 catch (const InternalFailure& e)
1339 {
1340 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001341 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301342 }
1343
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001344 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301345 {
Yong Lif4e38512019-05-21 14:46:55 +08001346 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001347 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001348 }
1349 // start the timer
1350 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001351 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001352 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301353 }
Tom Josephbed26992018-07-31 23:00:24 +05301354 else if (!identifyInterval)
1355 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001356 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301357 enclosureIdentifyLedOff();
1358 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001359 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301360}
1361
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001362namespace boot_options
1363{
1364
1365using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1366using IpmiValue = uint8_t;
1367constexpr auto ipmiDefault = 0;
1368
Patrick Venture0b02be92018-08-31 11:55:55 -07001369std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001370 {0x01, Source::Sources::Network},
1371 {0x02, Source::Sources::Disk},
1372 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001373 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001374 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001375
Patrick Venture0b02be92018-08-31 11:55:55 -07001376std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Yong Li5833cb62019-10-30 13:27:12 +08001377#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001378 {0x03, Mode::Modes::Safe},
Yong Li5833cb62019-10-30 13:27:12 +08001379#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001380 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001381 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001382
Patrick Venture0b02be92018-08-31 11:55:55 -07001383std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001384 {Source::Sources::Network, 0x01},
1385 {Source::Sources::Disk, 0x02},
1386 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001387 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001388 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001389
Patrick Venture0b02be92018-08-31 11:55:55 -07001390std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Yong Li5833cb62019-10-30 13:27:12 +08001391#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001392 {Mode::Modes::Safe, 0x03},
Yong Li5833cb62019-10-30 13:27:12 +08001393#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001394 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001395 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001396
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001397} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001398
Marri Devender Rao81719702018-05-07 00:53:48 -05001399/** @brief Set the property value for boot source
1400 * @param[in] source - boot source value
1401 * @return On failure return IPMI error.
1402 */
1403static ipmi_ret_t setBootSource(const Source::Sources& source)
1404{
1405 using namespace chassis::internal;
1406 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001407 std::variant<std::string> property = convertForMessage(source);
James Feist225dec82019-11-26 16:25:06 -08001408 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001409 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1410 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001411 auto method = dbus.new_method_call(
1412 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1413 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001414 method.append(bootSourceIntf, "BootSource", property);
1415 auto reply = dbus.call(method);
1416 if (reply.is_method_error())
1417 {
1418 log<level::ERR>("Error in BootSource Set");
1419 report<InternalFailure>();
1420 return IPMI_CC_UNSPECIFIED_ERROR;
1421 }
1422 return IPMI_CC_OK;
1423}
1424
Patrick Venture0b02be92018-08-31 11:55:55 -07001425/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001426 * @param[in] mode - boot mode value
1427 * @return On failure return IPMI error.
1428 */
1429static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1430{
1431 using namespace chassis::internal;
1432 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001433 std::variant<std::string> property = convertForMessage(mode);
James Feist225dec82019-11-26 16:25:06 -08001434 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001435 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1436 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001437 auto method = dbus.new_method_call(
1438 objects.service(bootModeSetting, bootModeIntf).c_str(),
1439 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001440 method.append(bootModeIntf, "BootMode", property);
1441 auto reply = dbus.call(method);
1442 if (reply.is_method_error())
1443 {
1444 log<level::ERR>("Error in BootMode Set");
1445 report<InternalFailure>();
1446 return IPMI_CC_UNSPECIFIED_ERROR;
1447 }
1448 return IPMI_CC_OK;
1449}
1450
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001451ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1452 ipmi_request_t request,
1453 ipmi_response_t response,
1454 ipmi_data_len_t data_len,
1455 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001456{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001457 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001458 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001459 char* p = NULL;
1460 get_sys_boot_options_response_t* resp =
1461 (get_sys_boot_options_response_t*)response;
1462 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001463 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001464
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001465 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001466 resp->version = SET_PARM_VERSION;
1467 resp->parm = 5;
1468 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001469
shgoupfd84fbbf2015-12-17 10:05:51 +08001470 /*
1471 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1472 * This is the only parameter used by petitboot.
1473 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001474 if (reqptr->parameter ==
1475 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1476 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001477
Ratan Guptafd28dd72016-08-01 04:58:01 -05001478 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001479 using namespace chassis::internal;
1480 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001481
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001482 try
ratagupta6f6bff2016-04-04 06:20:11 -05001483 {
James Feist225dec82019-11-26 16:25:06 -08001484 settings::Objects& objects = getObjects();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001485 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1486 const auto& bootSourceSetting =
1487 std::get<settings::Path>(bootSetting);
1488 auto oneTimeEnabled =
1489 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001490 auto method = dbus.new_method_call(
1491 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1492 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001493 method.append(bootSourceIntf, "BootSource");
1494 auto reply = dbus.call(method);
1495 if (reply.is_method_error())
1496 {
1497 log<level::ERR>("Error in BootSource Get");
1498 report<InternalFailure>();
1499 *data_len = 0;
1500 return IPMI_CC_UNSPECIFIED_ERROR;
1501 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001502 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001503 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001504 auto bootSource =
1505 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001506
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001507 bootSetting = settings::boot::setting(objects, bootModeIntf);
1508 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1509 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001510 objects.service(bootModeSetting, bootModeIntf).c_str(),
1511 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001512 method.append(bootModeIntf, "BootMode");
1513 reply = dbus.call(method);
1514 if (reply.is_method_error())
1515 {
1516 log<level::ERR>("Error in BootMode Get");
1517 report<InternalFailure>();
1518 *data_len = 0;
1519 return IPMI_CC_UNSPECIFIED_ERROR;
1520 }
1521 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001522 auto bootMode =
1523 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001524
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001525 bootOption = sourceDbusToIpmi.at(bootSource);
1526 if ((Mode::Modes::Regular == bootMode) &&
1527 (Source::Sources::Default == bootSource))
1528 {
1529 bootOption = ipmiDefault;
1530 }
1531 else if (Source::Sources::Default == bootSource)
1532 {
1533 bootOption = modeDbusToIpmi.at(bootMode);
1534 }
1535 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001536
Patrick Venture0b02be92018-08-31 11:55:55 -07001537 resp->data[0] = oneTimeEnabled
1538 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1539 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001540
ratagupta6f6bff2016-04-04 06:20:11 -05001541 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001542 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001543 catch (InternalFailure& e)
1544 {
James Feist225dec82019-11-26 16:25:06 -08001545 cache::objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001546 report<InternalFailure>();
1547 *data_len = 0;
1548 return IPMI_CC_UNSPECIFIED_ERROR;
1549 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001550 }
1551 else if (reqptr->parameter ==
1552 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1553 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001554
Patrick Venture0b02be92018-08-31 11:55:55 -07001555 *data_len =
1556 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001557
Patrick Venture0b02be92018-08-31 11:55:55 -07001558 resp->parm =
1559 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001560
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001561 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001562
Patrick Venture0b02be92018-08-31 11:55:55 -07001563 if (ret < 0)
1564 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001565
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301566 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001567 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001568 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001569 }
1570 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001571 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001572 }
1573
Patrick Venture0b02be92018-08-31 11:55:55 -07001574 else
1575 {
1576 log<level::ERR>("Unsupported parameter",
1577 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001578 }
1579
1580 if (p)
1581 free(p);
1582
Ratan Guptafd28dd72016-08-01 04:58:01 -05001583 if (rc == IPMI_CC_OK)
1584 {
1585 *data_len += 2;
1586 }
1587
shgoupfd84fbbf2015-12-17 10:05:51 +08001588 return rc;
1589}
1590
shgoupfd84fbbf2015-12-17 10:05:51 +08001591ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001592 ipmi_request_t request,
1593 ipmi_response_t response,
1594 ipmi_data_len_t data_len,
1595 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001596{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001597 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001598 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001599 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001600
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001601 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1602 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001603
shgoupfd84fbbf2015-12-17 10:05:51 +08001604 // This IPMI command does not have any resposne data
1605 *data_len = 0;
1606
1607 /* 000101
1608 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1609 * This is the only parameter used by petitboot.
1610 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001611
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001612 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1613 {
1614 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1615 using namespace chassis::internal;
1616 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001617 auto oneTimeEnabled = false;
1618 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301619 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001620 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001621
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001622 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001623 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001624 bool permanent =
1625 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1626 SET_PARM_BOOT_FLAGS_PERMANENT;
1627
James Feist225dec82019-11-26 16:25:06 -08001628 settings::Objects& objects = getObjects();
1629
Patrick Venture0b02be92018-08-31 11:55:55 -07001630 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301631
1632 oneTimeEnabled =
1633 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1634
1635 /*
1636 * Check if the current boot setting is onetime or permanent, if the
1637 * request in the command is otherwise, then set the "Enabled"
1638 * property in one_time object path to 'True' to indicate onetime
1639 * and 'False' to indicate permanent.
1640 *
1641 * Once the onetime/permanent setting is applied, then the bootMode
1642 * and bootSource is updated for the corresponding object.
1643 */
1644 if ((permanent && oneTimeEnabled) ||
1645 (!permanent && !oneTimeEnabled))
1646 {
1647 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1648
Patrick Venture0b02be92018-08-31 11:55:55 -07001649 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1650 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301651 }
1652
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001653 auto modeItr = modeIpmiToDbus.find(bootOption);
1654 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1655 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001656 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001657 rc = setBootSource(sourceItr->second);
1658 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001659 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001660 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001661 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001662 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001663 // If a set boot device is mapping to a boot source, then reset
1664 // the boot mode D-Bus property to default.
1665 // This way the ipmid code can determine which property is not
1666 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001667 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001668 {
1669 setBootMode(Mode::Modes::Regular);
1670 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001671 }
1672 if (modeIpmiToDbus.end() != modeItr)
1673 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001674 rc = setBootMode(modeItr->second);
1675 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001676 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001677 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001678 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001679 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001680 // If a set boot device is mapping to a boot mode, then reset
1681 // the boot source D-Bus property to default.
1682 // This way the ipmid code can determine which property is not
1683 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001684 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001685 {
1686 setBootSource(Source::Sources::Default);
1687 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001688 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001689 if ((modeIpmiToDbus.end() == modeItr) &&
1690 (sourceIpmiToDbus.end() == sourceItr))
1691 {
1692 // return error if boot option is not supported
1693 *data_len = 0;
1694 return IPMI_CC_INVALID_FIELD_REQUEST;
1695 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001696 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001697 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001698 {
James Feist225dec82019-11-26 16:25:06 -08001699 objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001700 report<InternalFailure>();
1701 *data_len = 0;
1702 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001703 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001704 }
1705 else if (reqptr->parameter ==
1706 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1707 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001708
1709 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001710 if (ret < 0)
1711 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301712 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001713 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001714 rc = IPMI_CC_UNSPECIFIED_ERROR;
1715 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001716 }
1717 else if (reqptr->parameter ==
1718 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1719 {
Tom Josephf536c902017-09-25 18:08:15 +05301720 // Handle parameter #4 and return command completed normally
1721 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1722 // parameter. This is added to support the ipmitool command `chassis
1723 // bootdev` which sends set on parameter #4, before setting the boot
1724 // flags.
1725 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001726 }
1727 else
1728 {
1729 log<level::ERR>("Unsupported parameter",
1730 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001731 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001732 }
1733
1734 return rc;
1735}
1736
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001737/** @brief implements Get POH counter command
1738 * @parameter
1739 * - none
1740 * @returns IPMI completion code plus response data
1741 * - minPerCount - Minutes per count
1742 * - counterReading - counter reading
1743 */
1744ipmi::RspType<uint8_t, // Minutes per count
1745 uint32_t // Counter reading
1746 >
1747 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001748{
1749 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001750 try
1751 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001752 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1753 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001754 }
1755 catch (std::exception& e)
1756 {
1757 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001758 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001759 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001760}
1761
Jason M. Billsbc996a32019-06-17 15:46:37 -07001762ipmi::RspType<uint3_t, // policy support
1763 uint5_t // reserved
1764 >
Vernon Mauerye278ead2018-10-09 09:23:43 -07001765 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
Jason M. Billsbc996a32019-06-17 15:46:37 -07001766 uint3_t policy, uint5_t reserved)
Yong Lic6713cf2018-09-12 12:35:13 +08001767{
Yong Lic6713cf2018-09-12 12:35:13 +08001768 power_policy::DbusValue value =
1769 power_policy::RestorePolicy::Policy::AlwaysOff;
1770
Jason M. Billsbc996a32019-06-17 15:46:37 -07001771 if (reserved || (policy > power_policy::noChange))
Yong Lic6713cf2018-09-12 12:35:13 +08001772 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001773 phosphor::logging::log<level::ERR>(
1774 "Reserved request parameter",
1775 entry("REQ=0x%x", static_cast<int>(policy)));
Jason M. Billsbc996a32019-06-17 15:46:37 -07001776 return ipmi::responseInvalidFieldRequest();
Yong Lic6713cf2018-09-12 12:35:13 +08001777 }
1778
Vernon Mauerye278ead2018-10-09 09:23:43 -07001779 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001780 {
1781 // just return the supported policy
Jason M. Billsbc996a32019-06-17 15:46:37 -07001782 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08001783 }
1784
1785 for (auto const& it : power_policy::dbusToIpmi)
1786 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001787 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001788 {
1789 value = it.first;
1790 break;
1791 }
1792 }
1793
1794 try
1795 {
James Feist225dec82019-11-26 16:25:06 -08001796 settings::Objects& objects = chassis::internal::cache::getObjects();
Yong Lic6713cf2018-09-12 12:35:13 +08001797 const settings::Path& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -08001798 objects.map.at(chassis::internal::powerRestoreIntf).front();
Vernon Mauery16b86932019-05-01 08:36:11 -07001799 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08001800
Vernon Mauerye278ead2018-10-09 09:23:43 -07001801 auto sdbusp = getSdBus();
1802 boost::system::error_code ec;
1803 sdbusp->yield_method_call<void>(
1804 yield, ec,
James Feist225dec82019-11-26 16:25:06 -08001805 objects
Yong Lic6713cf2018-09-12 12:35:13 +08001806 .service(powerRestoreSetting,
1807 chassis::internal::powerRestoreIntf)
1808 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07001809 powerRestoreSetting, ipmi::PROP_INTF, "Set",
1810 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1811 property);
1812 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08001813 {
1814 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07001815 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001816 }
1817 }
1818 catch (InternalFailure& e)
1819 {
James Feist225dec82019-11-26 16:25:06 -08001820 chassis::internal::cache::objectsPtr.reset();
Yong Lic6713cf2018-09-12 12:35:13 +08001821 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07001822 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001823 }
1824
Jason M. Billsbc996a32019-06-17 15:46:37 -07001825 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08001826}
1827
Adriana Kobylak40814c62015-10-27 15:58:44 -05001828void register_netfn_chassis_functions()
1829{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001830 createIdentifyTimer();
1831
Tom05732372016-09-06 17:21:23 +05301832 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001833 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1834 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001835
Tom05732372016-09-06 17:21:23 +05301836 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001837 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1838 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001839
Yong Liae4b0402018-11-02 11:12:14 +08001840 // Set Chassis Capabilities
anil kumar appana894d0222019-05-27 16:32:14 +00001841 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1842 ipmi::chassis::cmdSetChassisCapabilities,
1843 ipmi::Privilege::User, ipmiSetChassisCap);
Yong Liae4b0402018-11-02 11:12:14 +08001844
Tom05732372016-09-06 17:21:23 +05301845 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301846 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001847 ipmi_chassis_get_sys_boot_options,
1848 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001849
Tom05732372016-09-06 17:21:23 +05301850 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001851 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1852 ipmi::chassis::cmdGetChassisStatus,
1853 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08001854
Tom05732372016-09-06 17:21:23 +05301855 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001856 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1857 ipmi::chassis::cmdChassisControl,
1858 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001859
Tom Joseph5110c122018-03-23 17:55:40 +05301860 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001861 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1862 ipmi::chassis::cmdChassisIdentify,
1863 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301864
Tom05732372016-09-06 17:21:23 +05301865 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301866 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001867 ipmi_chassis_set_sys_boot_options,
1868 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001869 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001870 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1871 ipmi::chassis::cmdGetPohCounter,
1872 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001873
1874 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07001875 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1876 ipmi::chassis::cmdSetPowerRestorePolicy,
1877 ipmi::Privilege::Operator,
1878 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06001879}