blob: 1738ccce6839ec29f2f4182631f08cb8dfe46cef [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
154settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500155 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500156
157} // namespace cache
158} // namespace internal
159} // namespace chassis
160
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500161namespace poh
162{
163
164constexpr auto minutesPerCount = 60;
165
166} // namespace poh
167
Patrick Venture0b02be92018-08-31 11:55:55 -0700168struct get_sys_boot_options_t
169{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500170 uint8_t parameter;
171 uint8_t set;
172 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700173} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500174
Patrick Venture0b02be92018-08-31 11:55:55 -0700175struct get_sys_boot_options_response_t
176{
shgoupfd84fbbf2015-12-17 10:05:51 +0800177 uint8_t version;
178 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500179 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700180} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800181
Patrick Venture0b02be92018-08-31 11:55:55 -0700182struct set_sys_boot_options_t
183{
shgoupfd84fbbf2015-12-17 10:05:51 +0800184 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500185 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700186} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500187
Ratan Guptadcb10672017-07-10 10:33:50 +0530188int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500189{
Ratan Guptadcb10672017-07-10 10:33:50 +0530190 ipmi::PropertyMap properties;
191 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530192 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500193
Ratan Guptadcb10672017-07-10 10:33:50 +0530194 try
195 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700196 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530197 // objects,to handle such cases we are interested on that object
198 // which are on interested busname.
199 // Currenlty mapper doesn't give the readable busname(gives busid)
200 // so we can't match with bus name so giving some object specific info
201 // as SETTINGS_MATCH.
202 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500203
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530204 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530205
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530206 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
207 SETTINGS_ROOT, SETTINGS_MATCH);
208
209 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
210 SETTINGS_ROOT, SETTINGS_MATCH);
211
Patrick Venture0b02be92018-08-31 11:55:55 -0700212 properties = ipmi::getAllDbusProperties(
213 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
214 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
215 macObjectInfo.first, MAC_INTERFACE,
216 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530217
Vernon Maueryf442e112019-04-09 11:44:36 -0700218 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530219
Vernon Maueryf442e112019-04-09 11:44:36 -0700220 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530221
Vernon Maueryf442e112019-04-09 11:44:36 -0700222 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530223
Patrick Venture0b02be92018-08-31 11:55:55 -0700224 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700225 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700226 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
227 ? 1
228 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530229
Vernon Maueryf442e112019-04-09 11:44:36 -0700230 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530231
Ratan Guptad70f4532017-08-04 02:07:31 +0530232 // it is expected here that we should get the valid data
233 // but we may also get the default values.
234 // Validation of the data is done by settings.
235 //
236 // if mac address is default mac address then
237 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530238 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530239 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700240 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530241 rc = -1;
242 return rc;
243 }
244 // if addr is static then ipaddress,gateway,prefix
245 // should not be default one,don't send blank override.
246 if (isStatic)
247 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700248 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
249 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530250 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700251 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530252 rc = -1;
253 return rc;
254 }
255 }
256
Patrick Venture0b02be92018-08-31 11:55:55 -0700257 sscanf(
258 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
259 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
260 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
261 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530262
Ratan Guptadcb10672017-07-10 10:33:50 +0530263 respptr->data[MAC_OFFSET + 6] = 0x00;
264
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700265 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
266 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530267
Vernon Maueryf442e112019-04-09 11:44:36 -0700268 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
269 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
270 ? AF_INET
271 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530272
Patrick Venture0b02be92018-08-31 11:55:55 -0700273 addrSize = (addressFamily == AF_INET)
274 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
275 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530276
277 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530278 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700279 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530280
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530281 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
282
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700283 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530284
285 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
286
Ratan Guptad70f4532017-08-04 02:07:31 +0530287 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700288 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530289 }
290 catch (InternalFailure& e)
291 {
292 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700293 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530294 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500295 return rc;
296 }
297
Patrick Venture0b02be92018-08-31 11:55:55 -0700298 // PetiBoot-Specific
299 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700300 std::memcpy(respptr->data, net_conf_initial_bytes,
301 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500302
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700303 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530304
Ratan Guptafd28dd72016-08-01 04:58:01 -0500305#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700306 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500307
Ratan Guptadcb10672017-07-10 10:33:50 +0530308 for (uint8_t pos = 0; pos < index; pos++)
309 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700310 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530311 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500312#endif
313
Ratan Guptafd28dd72016-08-01 04:58:01 -0500314 return rc;
315}
316
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530317/** @brief convert IPv4 and IPv6 addresses from binary to text form.
318 * @param[in] family - IPv4/Ipv6
319 * @param[in] data - req data pointer.
320 * @param[in] offset - offset in the data.
321 * @param[in] addrSize - size of the data which needs to be read from offset.
322 * @returns address in text form.
323 */
324
Patrick Venture0b02be92018-08-31 11:55:55 -0700325std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
326 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530327{
328 char ipAddr[INET6_ADDRSTRLEN] = {};
329
Patrick Venture0b02be92018-08-31 11:55:55 -0700330 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530331 {
332 case AF_INET:
333 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700334 struct sockaddr_in addr4
335 {
336 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700337 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530338
Patrick Venture0b02be92018-08-31 11:55:55 -0700339 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530340
341 break;
342 }
343 case AF_INET6:
344 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700345 struct sockaddr_in6 addr6
346 {
347 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700348 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530349
Patrick Venture0b02be92018-08-31 11:55:55 -0700350 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530351
352 break;
353 }
354 default:
355 {
356 return {};
357 }
358 }
359
360 return ipAddr;
361}
362
Ratan Guptadcb10672017-07-10 10:33:50 +0530363int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500364{
Ratan Guptadcb10672017-07-10 10:33:50 +0530365 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500366 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700367 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530368 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700369 char addrOrigin{0};
370 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530371 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530372 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700373 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
374 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530375 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530376 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500377
Patrick Venture0b02be92018-08-31 11:55:55 -0700378 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500379 // version starts from sixth byte
380
Ratan Guptadcb10672017-07-10 10:33:50 +0530381 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500382 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530383 do
384 {
385 // cookie == 0x21 0x70 0x62 0x21
386 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700387 (net_conf_initial_bytes + COOKIE_OFFSET),
388 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530389 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700390 // cookie == 0
391 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
392 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530393 {
394 // need to zero out the network settings.
395 break;
396 }
397
398 log<level::ERR>("Invalid Cookie");
399 elog<InternalFailure>();
400 }
401
402 // vesion == 0x00 0x01
403 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700404 (net_conf_initial_bytes + VERSION_OFFSET),
405 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530406 {
407
408 log<level::ERR>("Invalid Version");
409 elog<InternalFailure>();
410 }
411
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700412 std::snprintf(
413 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
414 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
415 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
416 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530417
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700418 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
419 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530420
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530421 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530422 {
423 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530424 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530425 }
426
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530427 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700428 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
429 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530430
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530431 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530432
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700433 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
434 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530435
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530436 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
437
Ratan Gupta8c31d232017-08-13 05:49:43 +0530438 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530439 {
440 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
441 family = AF_INET6;
442 }
443
Patrick Venture0b02be92018-08-31 11:55:55 -0700444 ipAddress =
445 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530446
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530447 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
448
Patrick Venture0b02be92018-08-31 11:55:55 -0700449 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530450
Patrick Venture0b02be92018-08-31 11:55:55 -0700451 // Cookie == 0 or it is a valid cookie
452 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
453 std::to_string(prefix) + ",gateway="s + gateway +
454 ",mac="s + mac + ",addressOrigin="s +
455 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500456
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530457 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
458
459 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
460 SETTINGS_ROOT, SETTINGS_MATCH);
461 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
462 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530463 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530464 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700465 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530466 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700467 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530468 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530470 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700471 IP_INTERFACE, "Gateway", std::string(gateway));
472 ipmi::setDbusProperty(
473 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
474 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530475 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700476 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500477
Patrick Venture0b02be92018-08-31 11:55:55 -0700478 log<level::DEBUG>(
479 "Network configuration changed",
480 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500481 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530482 catch (InternalFailure& e)
483 {
484 commit<InternalFailure>();
485 return -1;
486 }
487
488 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500489}
490
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500491uint32_t getPOHCounter()
492{
493 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
494
Patrick Venture0b02be92018-08-31 11:55:55 -0700495 auto chassisStateObj =
496 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500497
Patrick Venture0b02be92018-08-31 11:55:55 -0700498 auto service =
499 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500500
Patrick Venture0b02be92018-08-31 11:55:55 -0700501 auto propValue =
502 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
503 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500504
Vernon Maueryf442e112019-04-09 11:44:36 -0700505 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500506}
507
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500508ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
509 ipmi_request_t request,
510 ipmi_response_t response,
511 ipmi_data_len_t data_len,
512 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500513{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500514 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800515 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500516 *data_len = 0;
517 return rc;
518}
519
Nan Li8d15fb42016-08-16 22:29:40 +0800520ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700521 ipmi_request_t request,
522 ipmi_response_t response,
523 ipmi_data_len_t data_len,
524 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800525{
526 // sd_bus error
527 ipmi_ret_t rc = IPMI_CC_OK;
528
529 ipmi_chassis_cap_t chassis_cap{};
530
Yong Liae4b0402018-11-02 11:12:14 +0800531 if (*data_len != 0)
532 {
533 return IPMI_CC_REQ_DATA_LEN_INVALID;
534 }
535
Nan Li8d15fb42016-08-16 22:29:40 +0800536 *data_len = sizeof(ipmi_chassis_cap_t);
537
Yong Liae4b0402018-11-02 11:12:14 +0800538 try
539 {
540 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800541
Yong Liae4b0402018-11-02 11:12:14 +0800542 ipmi::DbusObjectInfo chassisCapObject =
543 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800544
Yong Liae4b0402018-11-02 11:12:14 +0800545 // capabilities flags
546 // [7..4] - reserved
547 // [3] – 1b = provides power interlock (IPM 1.5)
548 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
549 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
550 // has capabilities
551 // to lock out external power control and reset button or
552 // front panel interfaces and/or detect tampering with those
553 // interfaces).
554 // [0] -1b = Chassis provides intrusion (physical security) sensor.
555 // set to default value 0x0.
556 ipmi::Value variant = ipmi::getDbusProperty(
557 bus, chassisCapObject.second, chassisCapObject.first,
558 chassisCapIntf, chassisCapFlagsProp);
Vernon Maueryf442e112019-04-09 11:44:36 -0700559 chassis_cap.cap_flags = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800560
Yong Liae4b0402018-11-02 11:12:14 +0800561 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
562 chassisCapObject.first, chassisCapIntf,
563 chassisFRUDevAddrProp);
564 // Chassis FRU info Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700565 chassis_cap.fru_info_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800566
Yong Liae4b0402018-11-02 11:12:14 +0800567 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
568 chassisCapObject.first, chassisCapIntf,
569 chassisSDRDevAddrProp);
570 // Chassis SDR Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700571 chassis_cap.sdr_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800572
Yong Liae4b0402018-11-02 11:12:14 +0800573 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
574 chassisCapObject.first, chassisCapIntf,
575 chassisSELDevAddrProp);
576 // Chassis SEL Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700577 chassis_cap.sel_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800578
Yong Liae4b0402018-11-02 11:12:14 +0800579 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
580 chassisCapObject.first, chassisCapIntf,
581 chassisSMDevAddrProp);
582 // Chassis System Management Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700583 chassis_cap.system_management_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800584
Yong Liae4b0402018-11-02 11:12:14 +0800585 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
586 chassisCapObject.first, chassisCapIntf,
587 chassisBridgeDevAddrProp);
588 // Chassis Bridge Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700589 chassis_cap.bridge_dev_addr = std::get<uint8_t>(variant);
Yong Liae4b0402018-11-02 11:12:14 +0800590 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
591 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
592 std::copy(chassisP, chassisP + *data_len, respP);
593 }
594 catch (std::exception& e)
595 {
596 log<level::ERR>(e.what());
597 rc = IPMI_CC_UNSPECIFIED_ERROR;
598 *data_len = 0;
599 return rc;
600 }
601
602 return rc;
603}
604
605ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
606 ipmi_request_t request,
607 ipmi_response_t response,
608 ipmi_data_len_t data_len,
609 ipmi_context_t context)
610{
611 ipmi_ret_t rc = IPMI_CC_OK;
612
613 if (*data_len != sizeof(ipmi_chassis_cap_t))
614 {
615 log<level::ERR>("Unsupported request length",
616 entry("LEN=0x%x", *data_len));
617 *data_len = 0;
618 return IPMI_CC_REQ_DATA_LEN_INVALID;
619 }
620
621 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
622
623 *data_len = 0;
624
625 // check input data
626 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
627 {
628 log<level::ERR>("Unsupported request parameter(CAP Flags)",
629 entry("REQ=0x%x", chassisCap->cap_flags));
630 return IPMI_CC_INVALID_FIELD_REQUEST;
631 }
632
633 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
634 {
635 log<level::ERR>("Unsupported request parameter(FRU Addr)",
636 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
637 return IPMI_CC_INVALID_FIELD_REQUEST;
638 }
639
640 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
641 {
642 log<level::ERR>("Unsupported request parameter(SDR Addr)",
643 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
644 return IPMI_CC_INVALID_FIELD_REQUEST;
645 }
646
647 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
648 {
649 log<level::ERR>("Unsupported request parameter(SEL Addr)",
650 entry("REQ=0x%x", chassisCap->sel_dev_addr));
651 return IPMI_CC_INVALID_FIELD_REQUEST;
652 }
653
654 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
655 {
656 log<level::ERR>(
657 "Unsupported request parameter(SM Addr)",
658 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
659 return IPMI_CC_INVALID_FIELD_REQUEST;
660 }
661
662 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
663 {
664 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
665 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
666 return IPMI_CC_INVALID_FIELD_REQUEST;
667 }
668
669 try
670 {
671 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
672 ipmi::DbusObjectInfo chassisCapObject =
673 ipmi::getDbusObject(bus, chassisCapIntf);
674
675 ipmi::setDbusProperty(bus, chassisCapObject.second,
676 chassisCapObject.first, chassisCapIntf,
677 chassisCapFlagsProp, chassisCap->cap_flags);
678
679 ipmi::setDbusProperty(bus, chassisCapObject.second,
680 chassisCapObject.first, chassisCapIntf,
681 chassisFRUDevAddrProp,
682 chassisCap->fru_info_dev_addr);
683
684 ipmi::setDbusProperty(bus, chassisCapObject.second,
685 chassisCapObject.first, chassisCapIntf,
686 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
687
688 ipmi::setDbusProperty(bus, chassisCapObject.second,
689 chassisCapObject.first, chassisCapIntf,
690 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
691
692 ipmi::setDbusProperty(bus, chassisCapObject.second,
693 chassisCapObject.first, chassisCapIntf,
694 chassisSMDevAddrProp,
695 chassisCap->system_management_dev_addr);
696
697 ipmi::setDbusProperty(bus, chassisCapObject.second,
698 chassisCapObject.first, chassisCapIntf,
699 chassisBridgeDevAddrProp,
700 chassisCap->bridge_dev_addr);
701 }
702 catch (std::exception& e)
703 {
704 log<level::ERR>(e.what());
705 rc = IPMI_CC_UNSPECIFIED_ERROR;
706 return rc;
707 }
Nan Li8d15fb42016-08-16 22:29:40 +0800708
709 return rc;
710}
711
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530712//------------------------------------------
713// Calls into Host State Manager Dbus object
714//------------------------------------------
715int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600716{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500717 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700718 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500719 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700720 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
721 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530722
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500723 // sd_bus error
724 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700725 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600726
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500727 // SD Bus error report mechanism.
728 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600729
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500730 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700731 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500732 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
733 if (rc < 0)
734 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700735 log<level::ERR>(
736 "Failed to get bus name",
737 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500738 return rc;
739 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530740
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500741 // Convert to string equivalent of the passed in transition enum.
742 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530743
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500744 rc = sd_bus_call_method(bus_type, // On the system bus
745 busname, // Service to contact
746 HOST_STATE_MANAGER_ROOT, // Object path
747 DBUS_PROPERTY_IFACE, // Interface name
748 "Set", // Method to be called
749 &bus_error, // object to return error
750 nullptr, // Response buffer if any
751 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700752 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
753 request.c_str());
754 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500755 {
756 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530757 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500758 }
759 else
760 {
761 log<level::INFO>("Transition request initiated successfully");
762 }
vishwa36993272015-11-20 12:43:49 -0600763
764 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500765 free(busname);
vishwa36993272015-11-20 12:43:49 -0600766
Sergey Solomineb9b8142016-08-23 09:07:28 -0500767 return rc;
vishwa36993272015-11-20 12:43:49 -0600768}
769
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500770namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500771{
Nan Lifdd8ec52016-08-28 03:57:40 +0800772
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500773using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
774using IpmiValue = uint8_t;
775using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800776
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700777const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500778 {RestorePolicy::Policy::AlwaysOff, 0x00},
779 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700780 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800781
Yong Lic6713cf2018-09-12 12:35:13 +0800782static constexpr uint8_t noChange = 0x03;
783static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
784static constexpr uint8_t policyBitMask = 0x07;
785static constexpr uint8_t setPolicyReqLen = 1;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700786
787/* helper function for Get Chassis Status Command
788 */
789std::optional<uint2_t> getPowerRestorePolicy()
790{
791 uint2_t restorePolicy = 0;
792 using namespace chassis::internal;
793
794 try
795 {
796 const auto& powerRestoreSetting =
797 cache::objects.map.at(powerRestoreIntf).front();
798 ipmi::Value result = ipmi::getDbusProperty(
799 *getSdBus(),
800 cache::objects.service(powerRestoreSetting, powerRestoreIntf)
801 .c_str(),
802 powerRestoreSetting.c_str(), powerRestoreIntf,
803 "PowerRestorePolicy");
804 auto powerRestore = RestorePolicy::convertPolicyFromString(
805 std::get<std::string>(result));
806 restorePolicy = dbusToIpmi.at(powerRestore);
807 }
808 catch (const std::exception& e)
809 {
810 log<level::ERR>(
811 "Failed to fetch pgood property", entry("ERROR=%s", e.what()),
812 entry("PATH=%s",
813 cache::objects.map.at(powerRestoreIntf).front().c_str()),
814 entry("INTERFACE=%s", powerRestoreIntf));
815 return std::nullopt;
816 }
817 return std::make_optional(restorePolicy);
818}
819
820/*
821 * getPowerStatus
822 * helper function for Get Chassis Status Command
823 * return - optional value for pgood (no value on error)
824 */
825std::optional<bool> getPowerStatus()
826{
827 constexpr const char* powerControlObj =
828 "/xyz/openbmc_project/Chassis/Control/Power0";
829 constexpr const char* powerControlIntf =
830 "xyz.openbmc_project.Chassis.Control.Power";
831 bool powerGood = false;
832 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
833 try
834 {
835 auto service =
836 ipmi::getService(*busp, powerControlIntf, powerControlObj);
837
838 ipmi::Value variant = ipmi::getDbusProperty(
839 *busp, service, powerControlObj, powerControlIntf, "pgood");
840 powerGood = static_cast<bool>(std::get<int>(variant));
841 }
842 catch (const std::exception& e)
843 {
844 try
845 {
846 // FIXME: some legacy modules use the older path; try that next
847 constexpr const char* legacyPwrCtrlObj =
848 "/org/openbmc/control/power0";
849 constexpr const char* legacyPwrCtrlIntf =
850 "org.openbmc.control.Power";
851 auto service =
852 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
853
854 ipmi::Value variant = ipmi::getDbusProperty(
855 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
856 powerGood = static_cast<bool>(std::get<int>(variant));
857 }
858 catch (const std::exception& e)
859 {
860 log<level::ERR>("Failed to fetch pgood property",
861 entry("ERROR=%s", e.what()),
862 entry("PATH=%s", powerControlObj),
863 entry("INTERFACE=%s", powerControlIntf));
864 return std::nullopt;
865 }
866 }
867 return std::make_optional(powerGood);
868}
869
Yong Li70ce7352019-05-16 21:15:27 +0800870/*
871 * getACFailStatus
872 * helper function for Get Chassis Status Command
873 * return - bool value for ACFail (false on error)
874 */
875bool getACFailStatus()
876{
877 constexpr const char* powerControlObj =
878 "/xyz/openbmc_project/Chassis/Control/Power0";
879 constexpr const char* powerControlIntf =
880 "xyz.openbmc_project.Chassis.Control.Power";
881 bool acFail = false;
882 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
883 try
884 {
885 auto service =
886 ipmi::getService(*bus, powerControlIntf, powerControlObj);
887
888 ipmi::Value variant = ipmi::getDbusProperty(
889 *bus, service, powerControlObj, powerControlIntf, "PFail");
890 acFail = std::get<bool>(variant);
891 }
892 catch (const std::exception& e)
893 {
894 log<level::ERR>("Failed to fetch PFail property",
895 entry("ERROR=%s", e.what()),
896 entry("PATH=%s", powerControlObj),
897 entry("INTERFACE=%s", powerControlIntf));
898 }
899 return acFail;
900}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500901} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800902
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700903static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
904 const std::string& buttonIntf)
905{
906 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
907 bool buttonDisabled = false;
908 try
909 {
910 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
911 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
912 buttonIntf, "Enabled");
913 buttonDisabled = !std::get<bool>(enabled);
914 }
915 catch (sdbusplus::exception::SdBusError& e)
916 {
917 log<level::ERR>("Fail to get button Enabled property",
918 entry("PATH=%s", buttonPath.c_str()),
919 entry("ERROR=%s", e.what()));
920 return std::nullopt;
921 }
922 return std::make_optional(buttonDisabled);
923}
924
Nan Lifdd8ec52016-08-28 03:57:40 +0800925//----------------------------------------------------------------------
926// Get Chassis Status commands
927//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700928ipmi::RspType<bool, // Power is on
929 bool, // Power overload
930 bool, // Interlock
931 bool, // power fault
932 bool, // power control fault
933 uint2_t, // power restore policy
934 bool, // reserved
935
936 bool, // AC failed
937 bool, // last power down caused by a Power overload
938 bool, // last power down caused by a power interlock
939 bool, // last power down caused by power fault
940 bool, // last ‘Power is on’ state was entered via IPMI command
941 uint3_t, // reserved
942
943 bool, // Chassis intrusion active
944 bool, // Front Panel Lockout active
945 bool, // Drive Fault
946 bool, // Cooling/fan fault detected
947 uint2_t, // Chassis Identify State
948 bool, // Chassis Identify command and state info supported
949 bool, // reserved
950
951 bool, // Power off button disabled
952 bool, // Reset button disabled
953 bool, // Diagnostic Interrupt button disabled
954 bool, // Standby (sleep) button disabled
955 bool, // Power off button disable allowed
956 bool, // Reset button disable allowed
957 bool, // Diagnostic Interrupt button disable allowed
958 bool // Standby (sleep) button disable allowed
959 >
960 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +0800961{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500962 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700963 std::optional<uint2_t> restorePolicy =
964 power_policy::getPowerRestorePolicy();
965 std::optional<bool> powerGood = power_policy::getPowerStatus();
966 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500967 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700968 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500969 }
Nan Lifdd8ec52016-08-28 03:57:40 +0800970
971 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700972 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700973 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700974 // allow disable if the interface is present
975 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
976 // default return the button is enabled (not disabled)
977 bool powerButtonDisabled = false;
978 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700979 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700980 // return the real value of the button status, if present
981 powerButtonDisabled = *powerButtonReading;
982 }
983
984 std::optional<bool> resetButtonReading =
985 getButtonEnabled(resetButtonPath, resetButtonIntf);
986 // allow disable if the interface is present
987 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
988 // default return the button is enabled (not disabled)
989 bool resetButtonDisabled = false;
990 if (resetButtonDisableAllow)
991 {
992 // return the real value of the button status, if present
993 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700994 }
Nan Lifdd8ec52016-08-28 03:57:40 +0800995
Yong Li70ce7352019-05-16 21:15:27 +0800996 bool powerDownAcFailed = power_policy::getACFailStatus();
997
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700998 // This response has a lot of hard-coded, unsupported fields
999 // They are set to false or 0
1000 constexpr bool powerOverload = false;
1001 constexpr bool chassisInterlock = false;
1002 constexpr bool powerFault = false;
1003 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001004 constexpr bool powerDownOverload = false;
1005 constexpr bool powerDownInterlock = false;
1006 constexpr bool powerDownPowerFault = false;
1007 constexpr bool powerStatusIPMI = false;
1008 constexpr bool chassisIntrusionActive = false;
1009 constexpr bool frontPanelLockoutActive = false;
1010 constexpr bool driveFault = false;
1011 constexpr bool coolingFanFault = false;
1012 // chassisIdentifySupport set because this command is implemented
1013 constexpr bool chassisIdentifySupport = true;
Yong Lif4e38512019-05-21 14:46:55 +08001014 uint2_t chassisIdentifyState = static_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001015 constexpr bool diagButtonDisabled = false;
1016 constexpr bool sleepButtonDisabled = false;
1017 constexpr bool diagButtonDisableAllow = false;
1018 constexpr bool sleepButtonDisableAllow = false;
1019
1020 return ipmi::responseSuccess(
1021 *powerGood, powerOverload, chassisInterlock, powerFault,
1022 powerControlFault, *restorePolicy,
1023 false, // reserved
1024
1025 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1026 powerDownPowerFault, powerStatusIPMI,
1027 uint3_t(0), // reserved
1028
1029 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1030 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1031 false, // reserved
1032
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001033 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001034 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1035 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001036}
Chris Austen7888c4d2015-12-03 15:26:20 -06001037
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301038//-------------------------------------------------------------
1039// Send a command to SoftPowerOff application to stop any timer
1040//-------------------------------------------------------------
1041int stop_soft_off_timer()
1042{
Patrick Venture0b02be92018-08-31 11:55:55 -07001043 constexpr auto iface = "org.freedesktop.DBus.Properties";
1044 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1045 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301046
Patrick Venture0b02be92018-08-31 11:55:55 -07001047 constexpr auto property = "ResponseReceived";
1048 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1049 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301050
1051 // Get the system bus where most system services are provided.
1052 auto bus = ipmid_get_sd_bus_connection();
1053
1054 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001055 // TODO openbmc/openbmc#1661 - Mapper refactor
1056 //
1057 // See openbmc/openbmc#1743 for some details but high level summary is that
1058 // for now the code will directly call the soft off interface due to a
1059 // race condition with mapper usage
1060 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001061 // char *busname = nullptr;
1062 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1063 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001064 //{
1065 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301066 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001067 // return r;
1068 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301069
1070 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001071 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001072 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1073 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301074 if (rc < 0)
1075 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301076 log<level::ERR>("Failed to set property in SoftPowerOff object",
1077 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301078 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001079
Patrick Venture0b02be92018-08-31 11:55:55 -07001080 // TODO openbmc/openbmc#1661 - Mapper refactor
1081 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301082 return rc;
1083}
1084
vishwa36993272015-11-20 12:43:49 -06001085//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001086// Create file to indicate there is no need for softoff notification to host
1087//----------------------------------------------------------------------
1088void indicate_no_softoff_needed()
1089{
1090 fs::path path{HOST_INBAND_REQUEST_DIR};
1091 if (!fs::is_directory(path))
1092 {
1093 fs::create_directory(path);
1094 }
1095
1096 // Add the host instance (default 0 for now) to the file name
1097 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001098 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001099 size++; // null
1100 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001101 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001102
1103 // Append file name to directory and create it
1104 path /= buf.get();
1105 std::ofstream(path.c_str());
1106}
1107
anil kumar appanadafff5f2019-04-27 18:06:00 +00001108/** @brief Implementation of chassis control command
1109 *
1110 * @param - chassisControl command byte
1111 *
1112 * @return Success or InvalidFieldRequest.
1113 */
1114ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001115{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001116 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001117 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001118 {
1119 case CMD_POWER_ON:
1120 rc = initiate_state_transition(State::Host::Transition::On);
1121 break;
1122 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301123 // This path would be hit in 2 conditions.
1124 // 1: When user asks for power off using ipmi chassis command 0x04
1125 // 2: Host asking for power off post shutting down.
1126
1127 // If it's a host requested power off, then need to nudge Softoff
1128 // application that it needs to stop the watchdog timer if running.
1129 // If it is a user requested power off, then this is not really
1130 // needed. But then we need to differentiate between user and host
1131 // calling this same command
1132
1133 // For now, we are going ahead with trying to nudge the soft off and
1134 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001135 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301136
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001137 // Only request the Off transition if the soft power off
1138 // application is not running
1139 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001140 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001141 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301142 // that it should not run. Not doing this will result in State
1143 // manager doing a default soft power off when asked for power
1144 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001145 indicate_no_softoff_needed();
1146
1147 // Now request the shutdown
1148 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001149 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001150 else
1151 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301152 log<level::INFO>("Soft off is running, so let shutdown target "
1153 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001154 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001155 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301156
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001157 case CMD_HARD_RESET:
1158 case CMD_POWER_CYCLE:
1159 // SPEC has a section that says certain implementations can trigger
1160 // PowerOn if power is Off when a command to power cycle is
1161 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001162
1163 // First create a file to indicate to the soft off application
1164 // that it should not run since this is a direct user initiated
1165 // power reboot request (i.e. a reboot request that is not
1166 // originating via a soft power off SMS request)
1167 indicate_no_softoff_needed();
1168
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001169 rc = initiate_state_transition(State::Host::Transition::Reboot);
1170 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301171
1172 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1173 // Request Host State Manager to do a soft power off
1174 rc = initiate_state_transition(State::Host::Transition::Off);
1175 break;
1176
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001177 default:
1178 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301179 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001180 entry("CMD=0x%X", chassisControl));
1181 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001182 }
1183 }
vishwa36993272015-11-20 12:43:49 -06001184
anil kumar appanadafff5f2019-04-27 18:06:00 +00001185 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1186 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001187}
1188
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001189/** @brief Return D-Bus connection string to enclosure identify LED object
1190 *
1191 * @param[in, out] connection - connection to D-Bus object
1192 * @return a IPMI return code
1193 */
1194std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301195{
Tom Joseph5110c122018-03-23 17:55:40 +05301196 // lookup enclosure_identify group owner(s) in mapper
1197 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001198 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1199 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301200
1201 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001202 static const std::vector<std::string> interfaces = {
1203 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301204 mapperCall.append(interfaces);
1205 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1206 if (mapperReply.is_method_error())
1207 {
1208 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001209 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301210 }
1211 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1212 mapperReply.read(mapperResp);
1213
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001214 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301215 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001216 log<level::ERR>(
1217 "Invalid number of enclosure identify objects.",
1218 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001219 elog<InternalFailure>();
1220 }
1221 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1222 return pair.first;
1223}
Tom Joseph5110c122018-03-23 17:55:40 +05301224
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001225/** @brief Turn On/Off enclosure identify LED
1226 *
1227 * @param[in] flag - true to turn on LED, false to turn off
1228 * @return a IPMI return code
1229 */
1230void enclosureIdentifyLed(bool flag)
1231{
1232 using namespace chassis::internal;
1233 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001234 auto msg = std::string("enclosureIdentifyLed(") +
1235 boost::lexical_cast<std::string>(flag) + ")";
1236 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001237 auto led =
1238 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1239 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001240 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001241 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001242 auto ledReply = dbus.call(led);
1243 if (ledReply.is_method_error())
1244 {
1245 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001246 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001247 elog<InternalFailure>();
1248 }
1249}
1250
1251/** @brief Callback method to turn off LED
1252 */
1253void enclosureIdentifyLedOff()
1254{
1255 try
1256 {
Yong Lif4e38512019-05-21 14:46:55 +08001257 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001258 enclosureIdentifyLed(false);
1259 }
1260 catch (const InternalFailure& e)
1261 {
1262 report<InternalFailure>();
1263 }
1264}
1265
1266/** @brief Create timer to turn on and off the enclosure LED
1267 */
1268void createIdentifyTimer()
1269{
1270 if (!identifyTimer)
1271 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001272 identifyTimer =
1273 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001274 }
1275}
1276
Vernon Mauery400cc782018-10-09 13:49:53 -07001277ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1278 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001279{
Vernon Mauery400cc782018-10-09 13:49:53 -07001280 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1281 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301282
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001283 if (identifyInterval || forceIdentify)
1284 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001285 // stop the timer if already started;
1286 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001287 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001288 try
Tom Joseph5110c122018-03-23 17:55:40 +05301289 {
Yong Lif4e38512019-05-21 14:46:55 +08001290 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001291 enclosureIdentifyLed(true);
1292 }
1293 catch (const InternalFailure& e)
1294 {
1295 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001296 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301297 }
1298
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001299 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301300 {
Yong Lif4e38512019-05-21 14:46:55 +08001301 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001302 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001303 }
1304 // start the timer
1305 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001306 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001307 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301308 }
Tom Josephbed26992018-07-31 23:00:24 +05301309 else if (!identifyInterval)
1310 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001311 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301312 enclosureIdentifyLedOff();
1313 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001314 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301315}
1316
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001317namespace boot_options
1318{
1319
1320using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1321using IpmiValue = uint8_t;
1322constexpr auto ipmiDefault = 0;
1323
Patrick Venture0b02be92018-08-31 11:55:55 -07001324std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001325 {0x01, Source::Sources::Network},
1326 {0x02, Source::Sources::Disk},
1327 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001328 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001329 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001330
Patrick Venture0b02be92018-08-31 11:55:55 -07001331std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001332 {0x03, Mode::Modes::Safe},
1333 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001334 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001335
Patrick Venture0b02be92018-08-31 11:55:55 -07001336std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001337 {Source::Sources::Network, 0x01},
1338 {Source::Sources::Disk, 0x02},
1339 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001340 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001341 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001342
Patrick Venture0b02be92018-08-31 11:55:55 -07001343std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001344 {Mode::Modes::Safe, 0x03},
1345 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001346 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001347
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001348} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001349
Marri Devender Rao81719702018-05-07 00:53:48 -05001350/** @brief Set the property value for boot source
1351 * @param[in] source - boot source value
1352 * @return On failure return IPMI error.
1353 */
1354static ipmi_ret_t setBootSource(const Source::Sources& source)
1355{
1356 using namespace chassis::internal;
1357 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001358 std::variant<std::string> property = convertForMessage(source);
Marri Devender Rao81719702018-05-07 00:53:48 -05001359 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1360 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001361 auto method = dbus.new_method_call(
1362 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1363 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001364 method.append(bootSourceIntf, "BootSource", property);
1365 auto reply = dbus.call(method);
1366 if (reply.is_method_error())
1367 {
1368 log<level::ERR>("Error in BootSource Set");
1369 report<InternalFailure>();
1370 return IPMI_CC_UNSPECIFIED_ERROR;
1371 }
1372 return IPMI_CC_OK;
1373}
1374
Patrick Venture0b02be92018-08-31 11:55:55 -07001375/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001376 * @param[in] mode - boot mode value
1377 * @return On failure return IPMI error.
1378 */
1379static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1380{
1381 using namespace chassis::internal;
1382 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001383 std::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001384 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1385 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001386 auto method = dbus.new_method_call(
1387 objects.service(bootModeSetting, bootModeIntf).c_str(),
1388 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001389 method.append(bootModeIntf, "BootMode", property);
1390 auto reply = dbus.call(method);
1391 if (reply.is_method_error())
1392 {
1393 log<level::ERR>("Error in BootMode Set");
1394 report<InternalFailure>();
1395 return IPMI_CC_UNSPECIFIED_ERROR;
1396 }
1397 return IPMI_CC_OK;
1398}
1399
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001400ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1401 ipmi_request_t request,
1402 ipmi_response_t response,
1403 ipmi_data_len_t data_len,
1404 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001405{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001406 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001407 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001408 char* p = NULL;
1409 get_sys_boot_options_response_t* resp =
1410 (get_sys_boot_options_response_t*)response;
1411 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001412 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001413
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001414 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001415 resp->version = SET_PARM_VERSION;
1416 resp->parm = 5;
1417 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001418
shgoupfd84fbbf2015-12-17 10:05:51 +08001419 /*
1420 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1421 * This is the only parameter used by petitboot.
1422 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001423 if (reqptr->parameter ==
1424 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1425 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001426
Ratan Guptafd28dd72016-08-01 04:58:01 -05001427 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001428 using namespace chassis::internal;
1429 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001430
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001431 try
ratagupta6f6bff2016-04-04 06:20:11 -05001432 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001433 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1434 const auto& bootSourceSetting =
1435 std::get<settings::Path>(bootSetting);
1436 auto oneTimeEnabled =
1437 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001438 auto method = dbus.new_method_call(
1439 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1440 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001441 method.append(bootSourceIntf, "BootSource");
1442 auto reply = dbus.call(method);
1443 if (reply.is_method_error())
1444 {
1445 log<level::ERR>("Error in BootSource Get");
1446 report<InternalFailure>();
1447 *data_len = 0;
1448 return IPMI_CC_UNSPECIFIED_ERROR;
1449 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001450 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001451 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001452 auto bootSource =
1453 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001454
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001455 bootSetting = settings::boot::setting(objects, bootModeIntf);
1456 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1457 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001458 objects.service(bootModeSetting, bootModeIntf).c_str(),
1459 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001460 method.append(bootModeIntf, "BootMode");
1461 reply = dbus.call(method);
1462 if (reply.is_method_error())
1463 {
1464 log<level::ERR>("Error in BootMode Get");
1465 report<InternalFailure>();
1466 *data_len = 0;
1467 return IPMI_CC_UNSPECIFIED_ERROR;
1468 }
1469 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001470 auto bootMode =
1471 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001472
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001473 bootOption = sourceDbusToIpmi.at(bootSource);
1474 if ((Mode::Modes::Regular == bootMode) &&
1475 (Source::Sources::Default == bootSource))
1476 {
1477 bootOption = ipmiDefault;
1478 }
1479 else if (Source::Sources::Default == bootSource)
1480 {
1481 bootOption = modeDbusToIpmi.at(bootMode);
1482 }
1483 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001484
Patrick Venture0b02be92018-08-31 11:55:55 -07001485 resp->data[0] = oneTimeEnabled
1486 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1487 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001488
ratagupta6f6bff2016-04-04 06:20:11 -05001489 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001490 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001491 catch (InternalFailure& e)
1492 {
1493 report<InternalFailure>();
1494 *data_len = 0;
1495 return IPMI_CC_UNSPECIFIED_ERROR;
1496 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001497 }
1498 else if (reqptr->parameter ==
1499 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1500 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001501
Patrick Venture0b02be92018-08-31 11:55:55 -07001502 *data_len =
1503 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001504
Patrick Venture0b02be92018-08-31 11:55:55 -07001505 resp->parm =
1506 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001507
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001508 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001509
Patrick Venture0b02be92018-08-31 11:55:55 -07001510 if (ret < 0)
1511 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001512
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301513 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001514 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001515 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001516 }
1517 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001518 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001519 }
1520
Patrick Venture0b02be92018-08-31 11:55:55 -07001521 else
1522 {
1523 log<level::ERR>("Unsupported parameter",
1524 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001525 }
1526
1527 if (p)
1528 free(p);
1529
Ratan Guptafd28dd72016-08-01 04:58:01 -05001530 if (rc == IPMI_CC_OK)
1531 {
1532 *data_len += 2;
1533 }
1534
shgoupfd84fbbf2015-12-17 10:05:51 +08001535 return rc;
1536}
1537
shgoupfd84fbbf2015-12-17 10:05:51 +08001538ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001539 ipmi_request_t request,
1540 ipmi_response_t response,
1541 ipmi_data_len_t data_len,
1542 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001543{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001544 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001545 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001546 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001547
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001548 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1549 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001550
shgoupfd84fbbf2015-12-17 10:05:51 +08001551 // This IPMI command does not have any resposne data
1552 *data_len = 0;
1553
1554 /* 000101
1555 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1556 * This is the only parameter used by petitboot.
1557 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001558
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001559 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1560 {
1561 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1562 using namespace chassis::internal;
1563 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001564 auto oneTimeEnabled = false;
1565 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301566 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001567 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001568
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001569 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001570 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001571 bool permanent =
1572 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1573 SET_PARM_BOOT_FLAGS_PERMANENT;
1574
Patrick Venture0b02be92018-08-31 11:55:55 -07001575 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301576
1577 oneTimeEnabled =
1578 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1579
1580 /*
1581 * Check if the current boot setting is onetime or permanent, if the
1582 * request in the command is otherwise, then set the "Enabled"
1583 * property in one_time object path to 'True' to indicate onetime
1584 * and 'False' to indicate permanent.
1585 *
1586 * Once the onetime/permanent setting is applied, then the bootMode
1587 * and bootSource is updated for the corresponding object.
1588 */
1589 if ((permanent && oneTimeEnabled) ||
1590 (!permanent && !oneTimeEnabled))
1591 {
1592 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1593
Patrick Venture0b02be92018-08-31 11:55:55 -07001594 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1595 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301596 }
1597
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001598 auto modeItr = modeIpmiToDbus.find(bootOption);
1599 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1600 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001601 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001602 rc = setBootSource(sourceItr->second);
1603 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001604 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001605 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001606 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001607 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001608 // If a set boot device is mapping to a boot source, then reset
1609 // the boot mode D-Bus property to default.
1610 // This way the ipmid code can determine which property is not
1611 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001612 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001613 {
1614 setBootMode(Mode::Modes::Regular);
1615 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001616 }
1617 if (modeIpmiToDbus.end() != modeItr)
1618 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001619 rc = setBootMode(modeItr->second);
1620 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001621 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001622 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001623 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001624 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001625 // If a set boot device is mapping to a boot mode, then reset
1626 // the boot source D-Bus property to default.
1627 // This way the ipmid code can determine which property is not
1628 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001629 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001630 {
1631 setBootSource(Source::Sources::Default);
1632 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001633 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001634 if ((modeIpmiToDbus.end() == modeItr) &&
1635 (sourceIpmiToDbus.end() == sourceItr))
1636 {
1637 // return error if boot option is not supported
1638 *data_len = 0;
1639 return IPMI_CC_INVALID_FIELD_REQUEST;
1640 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001641 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001642 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001643 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001644 report<InternalFailure>();
1645 *data_len = 0;
1646 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001647 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001648 }
1649 else if (reqptr->parameter ==
1650 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1651 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001652
1653 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001654 if (ret < 0)
1655 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301656 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001657 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001658 rc = IPMI_CC_UNSPECIFIED_ERROR;
1659 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001660 }
1661 else if (reqptr->parameter ==
1662 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1663 {
Tom Josephf536c902017-09-25 18:08:15 +05301664 // Handle parameter #4 and return command completed normally
1665 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1666 // parameter. This is added to support the ipmitool command `chassis
1667 // bootdev` which sends set on parameter #4, before setting the boot
1668 // flags.
1669 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001670 }
1671 else
1672 {
1673 log<level::ERR>("Unsupported parameter",
1674 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001675 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001676 }
1677
1678 return rc;
1679}
1680
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001681/** @brief implements Get POH counter command
1682 * @parameter
1683 * - none
1684 * @returns IPMI completion code plus response data
1685 * - minPerCount - Minutes per count
1686 * - counterReading - counter reading
1687 */
1688ipmi::RspType<uint8_t, // Minutes per count
1689 uint32_t // Counter reading
1690 >
1691 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001692{
1693 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001694 try
1695 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001696 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1697 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001698 }
1699 catch (std::exception& e)
1700 {
1701 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001702 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001703 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001704}
1705
Vernon Mauerye278ead2018-10-09 09:23:43 -07001706ipmi::RspType<uint8_t>
1707 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
1708 uint8_t policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001709{
Vernon Mauerye278ead2018-10-09 09:23:43 -07001710 constexpr uint8_t ccParamNotSupported = 0x80;
Yong Lic6713cf2018-09-12 12:35:13 +08001711 power_policy::DbusValue value =
1712 power_policy::RestorePolicy::Policy::AlwaysOff;
1713
Vernon Mauerye278ead2018-10-09 09:23:43 -07001714 if (policy & ~power_policy::policyBitMask)
Yong Lic6713cf2018-09-12 12:35:13 +08001715 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001716 phosphor::logging::log<level::ERR>(
1717 "Reserved request parameter",
1718 entry("REQ=0x%x", static_cast<int>(policy)));
1719 return ipmi::response(ccParamNotSupported);
Yong Lic6713cf2018-09-12 12:35:13 +08001720 }
1721
Vernon Mauerye278ead2018-10-09 09:23:43 -07001722 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001723 {
1724 // just return the supported policy
Vernon Mauerye278ead2018-10-09 09:23:43 -07001725 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001726 }
1727
1728 for (auto const& it : power_policy::dbusToIpmi)
1729 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001730 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001731 {
1732 value = it.first;
1733 break;
1734 }
1735 }
1736
1737 try
1738 {
1739 const settings::Path& powerRestoreSetting =
1740 chassis::internal::cache::objects.map
1741 .at(chassis::internal::powerRestoreIntf)
1742 .front();
Vernon Mauery16b86932019-05-01 08:36:11 -07001743 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08001744
Vernon Mauerye278ead2018-10-09 09:23:43 -07001745 auto sdbusp = getSdBus();
1746 boost::system::error_code ec;
1747 sdbusp->yield_method_call<void>(
1748 yield, ec,
Yong Lic6713cf2018-09-12 12:35:13 +08001749 chassis::internal::cache::objects
1750 .service(powerRestoreSetting,
1751 chassis::internal::powerRestoreIntf)
1752 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07001753 powerRestoreSetting, ipmi::PROP_INTF, "Set",
1754 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1755 property);
1756 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08001757 {
1758 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07001759 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001760 }
1761 }
1762 catch (InternalFailure& e)
1763 {
1764 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07001765 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001766 }
1767
Vernon Mauerye278ead2018-10-09 09:23:43 -07001768 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001769}
1770
Adriana Kobylak40814c62015-10-27 15:58:44 -05001771void register_netfn_chassis_functions()
1772{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001773 createIdentifyTimer();
1774
Tom05732372016-09-06 17:21:23 +05301775 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001776 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1777 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001778
Tom05732372016-09-06 17:21:23 +05301779 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001780 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1781 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001782
Yong Liae4b0402018-11-02 11:12:14 +08001783 // Set Chassis Capabilities
1784 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1785 ipmi_set_chassis_cap, PRIVILEGE_USER);
1786
Tom05732372016-09-06 17:21:23 +05301787 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301788 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001789 ipmi_chassis_get_sys_boot_options,
1790 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001791
Tom05732372016-09-06 17:21:23 +05301792 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001793 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1794 ipmi::chassis::cmdGetChassisStatus,
1795 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08001796
Tom05732372016-09-06 17:21:23 +05301797 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001798 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1799 ipmi::chassis::cmdChassisControl,
1800 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001801
Tom Joseph5110c122018-03-23 17:55:40 +05301802 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001803 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1804 ipmi::chassis::cmdChassisIdentify,
1805 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301806
Tom05732372016-09-06 17:21:23 +05301807 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301808 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001809 ipmi_chassis_set_sys_boot_options,
1810 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001811 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001812 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1813 ipmi::chassis::cmdGetPohCounter,
1814 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001815
1816 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07001817 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1818 ipmi::chassis::cmdSetPowerRestorePolicy,
1819 ipmi::Privilege::Operator,
1820 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06001821}