blob: 068152729450449a4b9d8f05cc74e3649b521f0f [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
Patrick Venture0b02be92018-08-31 11:55:55 -070046constexpr size_t SIZE_MAC = 18;
47constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
48 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
49 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050050constexpr size_t SIZE_PREFIX = 7;
51constexpr size_t MAX_PREFIX_VALUE = 32;
52constexpr size_t SIZE_COOKIE = 4;
53constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053054constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053055
Patrick Venture0b02be92018-08-31 11:55:55 -070056// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053057static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070058 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050059
60static constexpr size_t COOKIE_OFFSET = 1;
61static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053062static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050063static constexpr size_t MAC_OFFSET = 9;
64static constexpr size_t ADDRTYPE_OFFSET = 16;
65static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050066
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050067static constexpr size_t encIdentifyObjectsSize = 1;
68static constexpr size_t chassisIdentifyReqLength = 2;
69static constexpr size_t identifyIntervalPos = 0;
70static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080071
Adriana Kobylak40814c62015-10-27 15:58:44 -050072void register_netfn_chassis_functions() __attribute__((constructor));
73
shgoupfd84fbbf2015-12-17 10:05:51 +080074// Host settings in dbus
75// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070076const char* settings_object_name = "/org/openbmc/settings/host0";
77const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070078const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053079 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080080
Ratan Guptadcb10672017-07-10 10:33:50 +053081constexpr auto SETTINGS_ROOT = "/";
82constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053083
84constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
85constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
86
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050087static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
88static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070089 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050090static constexpr auto pOHCounterProperty = "POHCounter";
91static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080092const static constexpr char chassisCapIntf[] =
93 "xyz.openbmc_project.Control.ChassisCapabilities";
94const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
95const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
96const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
97const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
98const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
99const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
100static constexpr uint8_t chassisCapFlagMask = 0x0f;
101static constexpr uint8_t chassisCapAddrMask = 0xfe;
Ratan Guptadcb10672017-07-10 10:33:50 +0530102
Nan Li8d15fb42016-08-16 22:29:40 +0800103typedef struct
104{
105 uint8_t cap_flags;
106 uint8_t fru_info_dev_addr;
107 uint8_t sdr_dev_addr;
108 uint8_t sel_dev_addr;
109 uint8_t system_management_dev_addr;
110 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700111} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800112
Nan Lifdd8ec52016-08-28 03:57:40 +0800113typedef struct
114{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500115 uint8_t cur_power_state;
116 uint8_t last_power_event;
117 uint8_t misc_power_state;
118 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700119} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800120
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530121// Phosphor Host State manager
122namespace State = sdbusplus::xyz::openbmc_project::State::server;
123
Vernon Mauery185b9f82018-07-20 10:52:36 -0700124namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500125
Ratan Guptadcb10672017-07-10 10:33:50 +0530126using namespace phosphor::logging;
127using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500128using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700129
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500130namespace chassis
131{
132namespace internal
133{
134
135constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
136constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500137constexpr auto powerRestoreIntf =
138 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500139sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
140
141namespace cache
142{
143
144settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500145 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500146
147} // namespace cache
148} // namespace internal
149} // namespace chassis
150
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500151namespace poh
152{
153
154constexpr auto minutesPerCount = 60;
155
156} // namespace poh
157
Patrick Venture0b02be92018-08-31 11:55:55 -0700158struct get_sys_boot_options_t
159{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500160 uint8_t parameter;
161 uint8_t set;
162 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700163} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500164
Patrick Venture0b02be92018-08-31 11:55:55 -0700165struct get_sys_boot_options_response_t
166{
shgoupfd84fbbf2015-12-17 10:05:51 +0800167 uint8_t version;
168 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500169 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700170} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800171
Patrick Venture0b02be92018-08-31 11:55:55 -0700172struct set_sys_boot_options_t
173{
shgoupfd84fbbf2015-12-17 10:05:51 +0800174 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500175 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700176} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500177
Ratan Guptadcb10672017-07-10 10:33:50 +0530178int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500179{
Ratan Guptadcb10672017-07-10 10:33:50 +0530180 ipmi::PropertyMap properties;
181 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530182 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500183
Ratan Guptadcb10672017-07-10 10:33:50 +0530184 try
185 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700186 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530187 // objects,to handle such cases we are interested on that object
188 // which are on interested busname.
189 // Currenlty mapper doesn't give the readable busname(gives busid)
190 // so we can't match with bus name so giving some object specific info
191 // as SETTINGS_MATCH.
192 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500193
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530194 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530195
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530196 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
197 SETTINGS_ROOT, SETTINGS_MATCH);
198
199 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
200 SETTINGS_ROOT, SETTINGS_MATCH);
201
Patrick Venture0b02be92018-08-31 11:55:55 -0700202 properties = ipmi::getAllDbusProperties(
203 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
204 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
205 macObjectInfo.first, MAC_INTERFACE,
206 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530207
Vernon Maueryf442e112019-04-09 11:44:36 -0700208 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530209
Vernon Maueryf442e112019-04-09 11:44:36 -0700210 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530211
Vernon Maueryf442e112019-04-09 11:44:36 -0700212 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530213
Patrick Venture0b02be92018-08-31 11:55:55 -0700214 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700215 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700216 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
217 ? 1
218 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530219
Vernon Maueryf442e112019-04-09 11:44:36 -0700220 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530221
Ratan Guptad70f4532017-08-04 02:07:31 +0530222 // it is expected here that we should get the valid data
223 // but we may also get the default values.
224 // Validation of the data is done by settings.
225 //
226 // if mac address is default mac address then
227 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530228 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530229 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700230 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530231 rc = -1;
232 return rc;
233 }
234 // if addr is static then ipaddress,gateway,prefix
235 // should not be default one,don't send blank override.
236 if (isStatic)
237 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700238 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
239 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530240 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700241 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530242 rc = -1;
243 return rc;
244 }
245 }
246
Patrick Venture0b02be92018-08-31 11:55:55 -0700247 sscanf(
248 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
249 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
250 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
251 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530252
Ratan Guptadcb10672017-07-10 10:33:50 +0530253 respptr->data[MAC_OFFSET + 6] = 0x00;
254
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700255 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
256 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530257
Vernon Maueryf442e112019-04-09 11:44:36 -0700258 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
259 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
260 ? AF_INET
261 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530262
Patrick Venture0b02be92018-08-31 11:55:55 -0700263 addrSize = (addressFamily == AF_INET)
264 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
265 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530266
267 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530268 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700269 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530270
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530271 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
272
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700273 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530274
275 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
276
Ratan Guptad70f4532017-08-04 02:07:31 +0530277 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700278 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530279 }
280 catch (InternalFailure& e)
281 {
282 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700283 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530284 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500285 return rc;
286 }
287
Patrick Venture0b02be92018-08-31 11:55:55 -0700288 // PetiBoot-Specific
289 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700290 std::memcpy(respptr->data, net_conf_initial_bytes,
291 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500292
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700293 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530294
Ratan Guptafd28dd72016-08-01 04:58:01 -0500295#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700296 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500297
Ratan Guptadcb10672017-07-10 10:33:50 +0530298 for (uint8_t pos = 0; pos < index; pos++)
299 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700300 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530301 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500302#endif
303
Ratan Guptafd28dd72016-08-01 04:58:01 -0500304 return rc;
305}
306
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530307/** @brief convert IPv4 and IPv6 addresses from binary to text form.
308 * @param[in] family - IPv4/Ipv6
309 * @param[in] data - req data pointer.
310 * @param[in] offset - offset in the data.
311 * @param[in] addrSize - size of the data which needs to be read from offset.
312 * @returns address in text form.
313 */
314
Patrick Venture0b02be92018-08-31 11:55:55 -0700315std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
316 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530317{
318 char ipAddr[INET6_ADDRSTRLEN] = {};
319
Patrick Venture0b02be92018-08-31 11:55:55 -0700320 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530321 {
322 case AF_INET:
323 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700324 struct sockaddr_in addr4
325 {
326 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700327 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530328
Patrick Venture0b02be92018-08-31 11:55:55 -0700329 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530330
331 break;
332 }
333 case AF_INET6:
334 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700335 struct sockaddr_in6 addr6
336 {
337 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700338 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530339
Patrick Venture0b02be92018-08-31 11:55:55 -0700340 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530341
342 break;
343 }
344 default:
345 {
346 return {};
347 }
348 }
349
350 return ipAddr;
351}
352
Ratan Guptadcb10672017-07-10 10:33:50 +0530353int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500354{
Ratan Guptadcb10672017-07-10 10:33:50 +0530355 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500356 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700357 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530358 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700359 char addrOrigin{0};
360 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530361 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530362 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700363 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
364 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530365 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530366 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500367
Patrick Venture0b02be92018-08-31 11:55:55 -0700368 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500369 // version starts from sixth byte
370
Ratan Guptadcb10672017-07-10 10:33:50 +0530371 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500372 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530373 do
374 {
375 // cookie == 0x21 0x70 0x62 0x21
376 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700377 (net_conf_initial_bytes + COOKIE_OFFSET),
378 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530379 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700380 // cookie == 0
381 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
382 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530383 {
384 // need to zero out the network settings.
385 break;
386 }
387
388 log<level::ERR>("Invalid Cookie");
389 elog<InternalFailure>();
390 }
391
392 // vesion == 0x00 0x01
393 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700394 (net_conf_initial_bytes + VERSION_OFFSET),
395 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530396 {
397
398 log<level::ERR>("Invalid Version");
399 elog<InternalFailure>();
400 }
401
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700402 std::snprintf(
403 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
404 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
405 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
406 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530407
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700408 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
409 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530410
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530411 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530412 {
413 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530414 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530415 }
416
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530417 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700418 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
419 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530420
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530421 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530422
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700423 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
424 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530425
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530426 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
427
Ratan Gupta8c31d232017-08-13 05:49:43 +0530428 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530429 {
430 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
431 family = AF_INET6;
432 }
433
Patrick Venture0b02be92018-08-31 11:55:55 -0700434 ipAddress =
435 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530436
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530437 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
438
Patrick Venture0b02be92018-08-31 11:55:55 -0700439 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530440
Patrick Venture0b02be92018-08-31 11:55:55 -0700441 // Cookie == 0 or it is a valid cookie
442 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
443 std::to_string(prefix) + ",gateway="s + gateway +
444 ",mac="s + mac + ",addressOrigin="s +
445 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500446
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530447 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
448
449 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
450 SETTINGS_ROOT, SETTINGS_MATCH);
451 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
452 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530453 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530454 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700455 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530456 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700457 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530458 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700459 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530460 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700461 IP_INTERFACE, "Gateway", std::string(gateway));
462 ipmi::setDbusProperty(
463 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
464 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530465 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700466 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500467
Patrick Venture0b02be92018-08-31 11:55:55 -0700468 log<level::DEBUG>(
469 "Network configuration changed",
470 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500471 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530472 catch (InternalFailure& e)
473 {
474 commit<InternalFailure>();
475 return -1;
476 }
477
478 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500479}
480
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500481uint32_t getPOHCounter()
482{
483 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
484
Patrick Venture0b02be92018-08-31 11:55:55 -0700485 auto chassisStateObj =
486 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500487
Patrick Venture0b02be92018-08-31 11:55:55 -0700488 auto service =
489 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500490
Patrick Venture0b02be92018-08-31 11:55:55 -0700491 auto propValue =
492 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
493 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500494
Vernon Maueryf442e112019-04-09 11:44:36 -0700495 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500496}
497
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500498ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
499 ipmi_request_t request,
500 ipmi_response_t response,
501 ipmi_data_len_t data_len,
502 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500503{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500504 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800505 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500506 *data_len = 0;
507 return rc;
508}
509
Nan Li8d15fb42016-08-16 22:29:40 +0800510ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700511 ipmi_request_t request,
512 ipmi_response_t response,
513 ipmi_data_len_t data_len,
514 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800515{
516 // sd_bus error
517 ipmi_ret_t rc = IPMI_CC_OK;
518
519 ipmi_chassis_cap_t chassis_cap{};
520
Yong Liae4b0402018-11-02 11:12:14 +0800521 if (*data_len != 0)
522 {
523 return IPMI_CC_REQ_DATA_LEN_INVALID;
524 }
525
Nan Li8d15fb42016-08-16 22:29:40 +0800526 *data_len = sizeof(ipmi_chassis_cap_t);
527
Yong Liae4b0402018-11-02 11:12:14 +0800528 try
529 {
530 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800531
Yong Liae4b0402018-11-02 11:12:14 +0800532 ipmi::DbusObjectInfo chassisCapObject =
533 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800534
Yong Liae4b0402018-11-02 11:12:14 +0800535 // capabilities flags
536 // [7..4] - reserved
537 // [3] – 1b = provides power interlock (IPM 1.5)
538 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
539 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
540 // has capabilities
541 // to lock out external power control and reset button or
542 // front panel interfaces and/or detect tampering with those
543 // interfaces).
544 // [0] -1b = Chassis provides intrusion (physical security) sensor.
545 // set to default value 0x0.
546 ipmi::Value variant = ipmi::getDbusProperty(
547 bus, chassisCapObject.second, chassisCapObject.first,
548 chassisCapIntf, chassisCapFlagsProp);
Vernon Maueryf442e112019-04-09 11:44:36 -0700549 chassis_cap.cap_flags = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800550
Yong Liae4b0402018-11-02 11:12:14 +0800551 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
552 chassisCapObject.first, chassisCapIntf,
553 chassisFRUDevAddrProp);
554 // Chassis FRU info Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700555 chassis_cap.fru_info_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800556
Yong Liae4b0402018-11-02 11:12:14 +0800557 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
558 chassisCapObject.first, chassisCapIntf,
559 chassisSDRDevAddrProp);
560 // Chassis SDR Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700561 chassis_cap.sdr_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800562
Yong Liae4b0402018-11-02 11:12:14 +0800563 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
564 chassisCapObject.first, chassisCapIntf,
565 chassisSELDevAddrProp);
566 // Chassis SEL Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700567 chassis_cap.sel_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800568
Yong Liae4b0402018-11-02 11:12:14 +0800569 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
570 chassisCapObject.first, chassisCapIntf,
571 chassisSMDevAddrProp);
572 // Chassis System Management Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700573 chassis_cap.system_management_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800574
Yong Liae4b0402018-11-02 11:12:14 +0800575 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
576 chassisCapObject.first, chassisCapIntf,
577 chassisBridgeDevAddrProp);
578 // Chassis Bridge Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700579 chassis_cap.bridge_dev_addr = std::get<uint8_t>(variant);
Yong Liae4b0402018-11-02 11:12:14 +0800580 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
581 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
582 std::copy(chassisP, chassisP + *data_len, respP);
583 }
584 catch (std::exception& e)
585 {
586 log<level::ERR>(e.what());
587 rc = IPMI_CC_UNSPECIFIED_ERROR;
588 *data_len = 0;
589 return rc;
590 }
591
592 return rc;
593}
594
595ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
596 ipmi_request_t request,
597 ipmi_response_t response,
598 ipmi_data_len_t data_len,
599 ipmi_context_t context)
600{
601 ipmi_ret_t rc = IPMI_CC_OK;
602
603 if (*data_len != sizeof(ipmi_chassis_cap_t))
604 {
605 log<level::ERR>("Unsupported request length",
606 entry("LEN=0x%x", *data_len));
607 *data_len = 0;
608 return IPMI_CC_REQ_DATA_LEN_INVALID;
609 }
610
611 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
612
613 *data_len = 0;
614
615 // check input data
616 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
617 {
618 log<level::ERR>("Unsupported request parameter(CAP Flags)",
619 entry("REQ=0x%x", chassisCap->cap_flags));
620 return IPMI_CC_INVALID_FIELD_REQUEST;
621 }
622
623 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
624 {
625 log<level::ERR>("Unsupported request parameter(FRU Addr)",
626 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
627 return IPMI_CC_INVALID_FIELD_REQUEST;
628 }
629
630 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
631 {
632 log<level::ERR>("Unsupported request parameter(SDR Addr)",
633 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
634 return IPMI_CC_INVALID_FIELD_REQUEST;
635 }
636
637 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
638 {
639 log<level::ERR>("Unsupported request parameter(SEL Addr)",
640 entry("REQ=0x%x", chassisCap->sel_dev_addr));
641 return IPMI_CC_INVALID_FIELD_REQUEST;
642 }
643
644 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
645 {
646 log<level::ERR>(
647 "Unsupported request parameter(SM Addr)",
648 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
649 return IPMI_CC_INVALID_FIELD_REQUEST;
650 }
651
652 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
653 {
654 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
655 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
656 return IPMI_CC_INVALID_FIELD_REQUEST;
657 }
658
659 try
660 {
661 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
662 ipmi::DbusObjectInfo chassisCapObject =
663 ipmi::getDbusObject(bus, chassisCapIntf);
664
665 ipmi::setDbusProperty(bus, chassisCapObject.second,
666 chassisCapObject.first, chassisCapIntf,
667 chassisCapFlagsProp, chassisCap->cap_flags);
668
669 ipmi::setDbusProperty(bus, chassisCapObject.second,
670 chassisCapObject.first, chassisCapIntf,
671 chassisFRUDevAddrProp,
672 chassisCap->fru_info_dev_addr);
673
674 ipmi::setDbusProperty(bus, chassisCapObject.second,
675 chassisCapObject.first, chassisCapIntf,
676 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
677
678 ipmi::setDbusProperty(bus, chassisCapObject.second,
679 chassisCapObject.first, chassisCapIntf,
680 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
681
682 ipmi::setDbusProperty(bus, chassisCapObject.second,
683 chassisCapObject.first, chassisCapIntf,
684 chassisSMDevAddrProp,
685 chassisCap->system_management_dev_addr);
686
687 ipmi::setDbusProperty(bus, chassisCapObject.second,
688 chassisCapObject.first, chassisCapIntf,
689 chassisBridgeDevAddrProp,
690 chassisCap->bridge_dev_addr);
691 }
692 catch (std::exception& e)
693 {
694 log<level::ERR>(e.what());
695 rc = IPMI_CC_UNSPECIFIED_ERROR;
696 return rc;
697 }
Nan Li8d15fb42016-08-16 22:29:40 +0800698
699 return rc;
700}
701
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530702//------------------------------------------
703// Calls into Host State Manager Dbus object
704//------------------------------------------
705int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600706{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500707 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700708 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500709 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700710 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
711 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530712
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500713 // sd_bus error
714 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700715 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600716
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500717 // SD Bus error report mechanism.
718 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600719
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500720 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700721 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500722 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
723 if (rc < 0)
724 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700725 log<level::ERR>(
726 "Failed to get bus name",
727 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500728 return rc;
729 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530730
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500731 // Convert to string equivalent of the passed in transition enum.
732 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530733
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500734 rc = sd_bus_call_method(bus_type, // On the system bus
735 busname, // Service to contact
736 HOST_STATE_MANAGER_ROOT, // Object path
737 DBUS_PROPERTY_IFACE, // Interface name
738 "Set", // Method to be called
739 &bus_error, // object to return error
740 nullptr, // Response buffer if any
741 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700742 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
743 request.c_str());
744 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500745 {
746 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530747 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500748 }
749 else
750 {
751 log<level::INFO>("Transition request initiated successfully");
752 }
vishwa36993272015-11-20 12:43:49 -0600753
754 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500755 free(busname);
vishwa36993272015-11-20 12:43:49 -0600756
Sergey Solomineb9b8142016-08-23 09:07:28 -0500757 return rc;
vishwa36993272015-11-20 12:43:49 -0600758}
759
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500760namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500761{
Nan Lifdd8ec52016-08-28 03:57:40 +0800762
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500763using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
764using IpmiValue = uint8_t;
765using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800766
Patrick Venture0b02be92018-08-31 11:55:55 -0700767std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500768 {RestorePolicy::Policy::AlwaysOff, 0x00},
769 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700770 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800771
Yong Lic6713cf2018-09-12 12:35:13 +0800772static constexpr uint8_t noChange = 0x03;
773static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
774static constexpr uint8_t policyBitMask = 0x07;
775static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500776} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800777
778//----------------------------------------------------------------------
779// Get Chassis Status commands
780//----------------------------------------------------------------------
781ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500782 ipmi_request_t request,
783 ipmi_response_t response,
784 ipmi_data_len_t data_len,
785 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800786{
Patrick Venture0b02be92018-08-31 11:55:55 -0700787 const char* objname = "/org/openbmc/control/power0";
788 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800789
Patrick Venture0b02be92018-08-31 11:55:55 -0700790 sd_bus* bus = NULL;
791 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800792 int r = 0;
793 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700794 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800795 ipmi_ret_t rc = IPMI_CC_OK;
796 ipmi_get_chassis_status_t chassis_status{};
797
Nan Lifdd8ec52016-08-28 03:57:40 +0800798 uint8_t s = 0;
799
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500800 using namespace chassis::internal;
801 using namespace chassis::internal::cache;
802 using namespace power_policy;
803
Deepak Kodihallie6027092017-08-27 08:13:37 -0500804 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700805 auto method = dbus.new_method_call(
806 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
807 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500808 method.append(powerRestoreIntf, "PowerRestorePolicy");
809 auto resp = dbus.call(method);
810 if (resp.is_method_error())
811 {
812 log<level::ERR>("Error in PowerRestorePolicy Get");
813 report<InternalFailure>();
814 *data_len = 0;
815 return IPMI_CC_UNSPECIFIED_ERROR;
816 }
817 sdbusplus::message::variant<std::string> result;
818 resp.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -0700819 auto powerRestore =
820 RestorePolicy::convertPolicyFromString(std::get<std::string>(result));
Nan Lifdd8ec52016-08-28 03:57:40 +0800821
822 *data_len = 4;
823
Tom Joseph63a00512017-08-09 23:39:59 +0530824 bus = ipmid_get_sd_bus_connection();
825
Nan Lifdd8ec52016-08-28 03:57:40 +0800826 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700827 if (r < 0)
828 {
829 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800830 rc = IPMI_CC_UNSPECIFIED_ERROR;
831 goto finish;
832 }
833
Patrick Venture0b02be92018-08-31 11:55:55 -0700834 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
835 "i");
836 if (r < 0)
837 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530838 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700839 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
840 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530841 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800842 rc = IPMI_CC_UNSPECIFIED_ERROR;
843 goto finish;
844 }
845
846 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700847 if (r < 0)
848 {
849 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800850 rc = IPMI_CC_UNSPECIFIED_ERROR;
851 goto finish;
852 }
853
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500854 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800855
856 // Current Power State
857 // [7] reserved
858 // [6..5] power restore policy
859 // 00b = chassis stays powered off after AC/mains returns
860 // 01b = after AC returns, power is restored to the state that was
861 // in effect when AC/mains was lost.
862 // 10b = chassis always powers up after AC/mains returns
863 // 11b = unknow
864 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700865 // Do we need to define a dbus property to identify the restore
866 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800867
868 // [4] power control fault
869 // 1b = controller attempted to turn system power on or off, but
870 // system did not enter desired state.
871 // Set to 0b, since We don't support it..
872
873 // [3] power fault
874 // 1b = fault detected in main power subsystem.
875 // set to 0b. for we don't support it.
876
877 // [2] 1b = interlock (chassis is presently shut down because a chassis
878 // panel interlock switch is active). (IPMI 1.5)
879 // set to 0b, for we don't support it.
880
881 // [1] power overload
882 // 1b = system shutdown because of power overload condition.
883 // set to 0b, for we don't support it.
884
885 // [0] power is on
886 // 1b = system power is on
887 // 0b = system power is off(soft-off S4/S5, or mechanical off)
888
Patrick Venture0b02be92018-08-31 11:55:55 -0700889 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800890
891 // Last Power Event
892 // [7..5] – reserved
893 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
894 // [3] – 1b = last power down caused by power fault
895 // [2] – 1b = last power down caused by a power interlock being activated
896 // [1] – 1b = last power down caused by a Power overload
897 // [0] – 1b = AC failed
898 // set to 0x0, for we don't support these fields.
899
900 chassis_status.last_power_event = 0;
901
902 // Misc. Chassis State
903 // [7] – reserved
904 // [6] – 1b = Chassis Identify command and state info supported (Optional)
905 // 0b = Chassis Identify command support unspecified via this command.
906 // (The Get Command Support command , if implemented, would still
907 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700908 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
909 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800910 // as 00b) otherwise. Returns the present chassis identify state.
911 // Refer to the Chassis Identify command for more info.
912 // 00b = chassis identify state = Off
913 // 01b = chassis identify state = Temporary(timed) On
914 // 10b = chassis identify state = Indefinite On
915 // 11b = reserved
916 // [3] – 1b = Cooling/fan fault detected
917 // [2] – 1b = Drive Fault
918 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
919 // push-buttons disabled.)
920 // [0] – 1b = Chassis Intrusion active
921 // set to 0, for we don't support them.
922 chassis_status.misc_power_state = 0;
923
924 // Front Panel Button Capabilities and disable/enable status(Optional)
925 // set to 0, for we don't support them.
926 chassis_status.front_panel_button_cap_status = 0;
927
928 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700929 std::memcpy(response, &chassis_status, *data_len);
Nan Lifdd8ec52016-08-28 03:57:40 +0800930
931finish:
932 free(busname);
933 reply = sd_bus_message_unref(reply);
934
935 return rc;
936}
Chris Austen7888c4d2015-12-03 15:26:20 -0600937
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530938//-------------------------------------------------------------
939// Send a command to SoftPowerOff application to stop any timer
940//-------------------------------------------------------------
941int stop_soft_off_timer()
942{
Patrick Venture0b02be92018-08-31 11:55:55 -0700943 constexpr auto iface = "org.freedesktop.DBus.Properties";
944 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
945 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530946
Patrick Venture0b02be92018-08-31 11:55:55 -0700947 constexpr auto property = "ResponseReceived";
948 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
949 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530950
951 // Get the system bus where most system services are provided.
952 auto bus = ipmid_get_sd_bus_connection();
953
954 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500955 // TODO openbmc/openbmc#1661 - Mapper refactor
956 //
957 // See openbmc/openbmc#1743 for some details but high level summary is that
958 // for now the code will directly call the soft off interface due to a
959 // race condition with mapper usage
960 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700961 // char *busname = nullptr;
962 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
963 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500964 //{
965 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530966 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500967 // return r;
968 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530969
970 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500971 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700972 "Set", nullptr, nullptr, "ssv", soft_off_iface,
973 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530974 if (rc < 0)
975 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530976 log<level::ERR>("Failed to set property in SoftPowerOff object",
977 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530978 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500979
Patrick Venture0b02be92018-08-31 11:55:55 -0700980 // TODO openbmc/openbmc#1661 - Mapper refactor
981 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530982 return rc;
983}
984
vishwa36993272015-11-20 12:43:49 -0600985//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500986// Create file to indicate there is no need for softoff notification to host
987//----------------------------------------------------------------------
988void indicate_no_softoff_needed()
989{
990 fs::path path{HOST_INBAND_REQUEST_DIR};
991 if (!fs::is_directory(path))
992 {
993 fs::create_directory(path);
994 }
995
996 // Add the host instance (default 0 for now) to the file name
997 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -0700998 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500999 size++; // null
1000 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001001 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001002
1003 // Append file name to directory and create it
1004 path /= buf.get();
1005 std::ofstream(path.c_str());
1006}
1007
anil kumar appanadafff5f2019-04-27 18:06:00 +00001008/** @brief Implementation of chassis control command
1009 *
1010 * @param - chassisControl command byte
1011 *
1012 * @return Success or InvalidFieldRequest.
1013 */
1014ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001015{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001016 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001017 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001018 {
1019 case CMD_POWER_ON:
1020 rc = initiate_state_transition(State::Host::Transition::On);
1021 break;
1022 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301023 // This path would be hit in 2 conditions.
1024 // 1: When user asks for power off using ipmi chassis command 0x04
1025 // 2: Host asking for power off post shutting down.
1026
1027 // If it's a host requested power off, then need to nudge Softoff
1028 // application that it needs to stop the watchdog timer if running.
1029 // If it is a user requested power off, then this is not really
1030 // needed. But then we need to differentiate between user and host
1031 // calling this same command
1032
1033 // For now, we are going ahead with trying to nudge the soft off and
1034 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001035 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301036
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001037 // Only request the Off transition if the soft power off
1038 // application is not running
1039 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001040 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001041 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301042 // that it should not run. Not doing this will result in State
1043 // manager doing a default soft power off when asked for power
1044 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001045 indicate_no_softoff_needed();
1046
1047 // Now request the shutdown
1048 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001049 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001050 else
1051 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301052 log<level::INFO>("Soft off is running, so let shutdown target "
1053 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001054 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001055 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301056
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001057 case CMD_HARD_RESET:
1058 case CMD_POWER_CYCLE:
1059 // SPEC has a section that says certain implementations can trigger
1060 // PowerOn if power is Off when a command to power cycle is
1061 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001062
1063 // First create a file to indicate to the soft off application
1064 // that it should not run since this is a direct user initiated
1065 // power reboot request (i.e. a reboot request that is not
1066 // originating via a soft power off SMS request)
1067 indicate_no_softoff_needed();
1068
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001069 rc = initiate_state_transition(State::Host::Transition::Reboot);
1070 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301071
1072 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1073 // Request Host State Manager to do a soft power off
1074 rc = initiate_state_transition(State::Host::Transition::Off);
1075 break;
1076
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001077 default:
1078 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301079 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001080 entry("CMD=0x%X", chassisControl));
1081 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001082 }
1083 }
vishwa36993272015-11-20 12:43:49 -06001084
anil kumar appanadafff5f2019-04-27 18:06:00 +00001085 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1086 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001087}
1088
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001089/** @brief Return D-Bus connection string to enclosure identify LED object
1090 *
1091 * @param[in, out] connection - connection to D-Bus object
1092 * @return a IPMI return code
1093 */
1094std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301095{
Tom Joseph5110c122018-03-23 17:55:40 +05301096 // lookup enclosure_identify group owner(s) in mapper
1097 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001098 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1099 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301100
1101 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001102 static const std::vector<std::string> interfaces = {
1103 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301104 mapperCall.append(interfaces);
1105 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1106 if (mapperReply.is_method_error())
1107 {
1108 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001109 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301110 }
1111 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1112 mapperReply.read(mapperResp);
1113
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001114 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301115 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001116 log<level::ERR>(
1117 "Invalid number of enclosure identify objects.",
1118 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001119 elog<InternalFailure>();
1120 }
1121 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1122 return pair.first;
1123}
Tom Joseph5110c122018-03-23 17:55:40 +05301124
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001125/** @brief Turn On/Off enclosure identify LED
1126 *
1127 * @param[in] flag - true to turn on LED, false to turn off
1128 * @return a IPMI return code
1129 */
1130void enclosureIdentifyLed(bool flag)
1131{
1132 using namespace chassis::internal;
1133 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001134 auto msg = std::string("enclosureIdentifyLed(") +
1135 boost::lexical_cast<std::string>(flag) + ")";
1136 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001137 auto led =
1138 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1139 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001140 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001141 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001142 auto ledReply = dbus.call(led);
1143 if (ledReply.is_method_error())
1144 {
1145 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001146 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001147 elog<InternalFailure>();
1148 }
1149}
1150
1151/** @brief Callback method to turn off LED
1152 */
1153void enclosureIdentifyLedOff()
1154{
1155 try
1156 {
1157 enclosureIdentifyLed(false);
1158 }
1159 catch (const InternalFailure& e)
1160 {
1161 report<InternalFailure>();
1162 }
1163}
1164
1165/** @brief Create timer to turn on and off the enclosure LED
1166 */
1167void createIdentifyTimer()
1168{
1169 if (!identifyTimer)
1170 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001171 identifyTimer =
1172 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001173 }
1174}
1175
Vernon Mauery400cc782018-10-09 13:49:53 -07001176ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1177 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001178{
Vernon Mauery400cc782018-10-09 13:49:53 -07001179 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1180 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301181
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001182 if (identifyInterval || forceIdentify)
1183 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001184 // stop the timer if already started;
1185 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001186 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001187 try
Tom Joseph5110c122018-03-23 17:55:40 +05301188 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001189 enclosureIdentifyLed(true);
1190 }
1191 catch (const InternalFailure& e)
1192 {
1193 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001194 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301195 }
1196
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001197 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301198 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001199 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001200 }
1201 // start the timer
1202 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001203 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001204 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301205 }
Tom Josephbed26992018-07-31 23:00:24 +05301206 else if (!identifyInterval)
1207 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001208 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301209 enclosureIdentifyLedOff();
1210 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001211 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301212}
1213
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001214namespace boot_options
1215{
1216
1217using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1218using IpmiValue = uint8_t;
1219constexpr auto ipmiDefault = 0;
1220
Patrick Venture0b02be92018-08-31 11:55:55 -07001221std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001222 {0x01, Source::Sources::Network},
1223 {0x02, Source::Sources::Disk},
1224 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001225 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001226
Patrick Venture0b02be92018-08-31 11:55:55 -07001227std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001228 {0x03, Mode::Modes::Safe},
1229 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001230 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001231
Patrick Venture0b02be92018-08-31 11:55:55 -07001232std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001233 {Source::Sources::Network, 0x01},
1234 {Source::Sources::Disk, 0x02},
1235 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001236 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001237
Patrick Venture0b02be92018-08-31 11:55:55 -07001238std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001239 {Mode::Modes::Safe, 0x03},
1240 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001241 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001242
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001243} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001244
Marri Devender Rao81719702018-05-07 00:53:48 -05001245/** @brief Set the property value for boot source
1246 * @param[in] source - boot source value
1247 * @return On failure return IPMI error.
1248 */
1249static ipmi_ret_t setBootSource(const Source::Sources& source)
1250{
1251 using namespace chassis::internal;
1252 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001253 std::variant<std::string> property = convertForMessage(source);
Marri Devender Rao81719702018-05-07 00:53:48 -05001254 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1255 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001256 auto method = dbus.new_method_call(
1257 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1258 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001259 method.append(bootSourceIntf, "BootSource", property);
1260 auto reply = dbus.call(method);
1261 if (reply.is_method_error())
1262 {
1263 log<level::ERR>("Error in BootSource Set");
1264 report<InternalFailure>();
1265 return IPMI_CC_UNSPECIFIED_ERROR;
1266 }
1267 return IPMI_CC_OK;
1268}
1269
Patrick Venture0b02be92018-08-31 11:55:55 -07001270/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001271 * @param[in] mode - boot mode value
1272 * @return On failure return IPMI error.
1273 */
1274static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1275{
1276 using namespace chassis::internal;
1277 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001278 std::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001279 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1280 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001281 auto method = dbus.new_method_call(
1282 objects.service(bootModeSetting, bootModeIntf).c_str(),
1283 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001284 method.append(bootModeIntf, "BootMode", property);
1285 auto reply = dbus.call(method);
1286 if (reply.is_method_error())
1287 {
1288 log<level::ERR>("Error in BootMode Set");
1289 report<InternalFailure>();
1290 return IPMI_CC_UNSPECIFIED_ERROR;
1291 }
1292 return IPMI_CC_OK;
1293}
1294
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001295ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1296 ipmi_request_t request,
1297 ipmi_response_t response,
1298 ipmi_data_len_t data_len,
1299 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001300{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001301 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001302 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001303 char* p = NULL;
1304 get_sys_boot_options_response_t* resp =
1305 (get_sys_boot_options_response_t*)response;
1306 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001307 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001308
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001309 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001310 resp->version = SET_PARM_VERSION;
1311 resp->parm = 5;
1312 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001313
shgoupfd84fbbf2015-12-17 10:05:51 +08001314 /*
1315 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1316 * This is the only parameter used by petitboot.
1317 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001318 if (reqptr->parameter ==
1319 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1320 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001321
Ratan Guptafd28dd72016-08-01 04:58:01 -05001322 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001323 using namespace chassis::internal;
1324 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001325
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001326 try
ratagupta6f6bff2016-04-04 06:20:11 -05001327 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001328 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1329 const auto& bootSourceSetting =
1330 std::get<settings::Path>(bootSetting);
1331 auto oneTimeEnabled =
1332 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001333 auto method = dbus.new_method_call(
1334 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1335 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001336 method.append(bootSourceIntf, "BootSource");
1337 auto reply = dbus.call(method);
1338 if (reply.is_method_error())
1339 {
1340 log<level::ERR>("Error in BootSource Get");
1341 report<InternalFailure>();
1342 *data_len = 0;
1343 return IPMI_CC_UNSPECIFIED_ERROR;
1344 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001345 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001346 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001347 auto bootSource =
1348 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001349
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001350 bootSetting = settings::boot::setting(objects, bootModeIntf);
1351 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1352 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001353 objects.service(bootModeSetting, bootModeIntf).c_str(),
1354 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001355 method.append(bootModeIntf, "BootMode");
1356 reply = dbus.call(method);
1357 if (reply.is_method_error())
1358 {
1359 log<level::ERR>("Error in BootMode Get");
1360 report<InternalFailure>();
1361 *data_len = 0;
1362 return IPMI_CC_UNSPECIFIED_ERROR;
1363 }
1364 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001365 auto bootMode =
1366 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001367
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001368 bootOption = sourceDbusToIpmi.at(bootSource);
1369 if ((Mode::Modes::Regular == bootMode) &&
1370 (Source::Sources::Default == bootSource))
1371 {
1372 bootOption = ipmiDefault;
1373 }
1374 else if (Source::Sources::Default == bootSource)
1375 {
1376 bootOption = modeDbusToIpmi.at(bootMode);
1377 }
1378 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001379
Patrick Venture0b02be92018-08-31 11:55:55 -07001380 resp->data[0] = oneTimeEnabled
1381 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1382 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001383
ratagupta6f6bff2016-04-04 06:20:11 -05001384 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001385 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001386 catch (InternalFailure& e)
1387 {
1388 report<InternalFailure>();
1389 *data_len = 0;
1390 return IPMI_CC_UNSPECIFIED_ERROR;
1391 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001392 }
1393 else if (reqptr->parameter ==
1394 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1395 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001396
Patrick Venture0b02be92018-08-31 11:55:55 -07001397 *data_len =
1398 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001399
Patrick Venture0b02be92018-08-31 11:55:55 -07001400 resp->parm =
1401 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001402
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001403 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001404
Patrick Venture0b02be92018-08-31 11:55:55 -07001405 if (ret < 0)
1406 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001407
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301408 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001409 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001410 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001411 }
1412 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001413 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001414 }
1415
Patrick Venture0b02be92018-08-31 11:55:55 -07001416 else
1417 {
1418 log<level::ERR>("Unsupported parameter",
1419 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001420 }
1421
1422 if (p)
1423 free(p);
1424
Ratan Guptafd28dd72016-08-01 04:58:01 -05001425 if (rc == IPMI_CC_OK)
1426 {
1427 *data_len += 2;
1428 }
1429
shgoupfd84fbbf2015-12-17 10:05:51 +08001430 return rc;
1431}
1432
shgoupfd84fbbf2015-12-17 10:05:51 +08001433ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001434 ipmi_request_t request,
1435 ipmi_response_t response,
1436 ipmi_data_len_t data_len,
1437 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001438{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001439 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001440 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001441 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001442
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001443 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1444 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001445
shgoupfd84fbbf2015-12-17 10:05:51 +08001446 // This IPMI command does not have any resposne data
1447 *data_len = 0;
1448
1449 /* 000101
1450 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1451 * This is the only parameter used by petitboot.
1452 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001453
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001454 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1455 {
1456 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1457 using namespace chassis::internal;
1458 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001459 auto oneTimeEnabled = false;
1460 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301461 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001462 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001463
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001464 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001465 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001466 bool permanent =
1467 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1468 SET_PARM_BOOT_FLAGS_PERMANENT;
1469
Patrick Venture0b02be92018-08-31 11:55:55 -07001470 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301471
1472 oneTimeEnabled =
1473 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1474
1475 /*
1476 * Check if the current boot setting is onetime or permanent, if the
1477 * request in the command is otherwise, then set the "Enabled"
1478 * property in one_time object path to 'True' to indicate onetime
1479 * and 'False' to indicate permanent.
1480 *
1481 * Once the onetime/permanent setting is applied, then the bootMode
1482 * and bootSource is updated for the corresponding object.
1483 */
1484 if ((permanent && oneTimeEnabled) ||
1485 (!permanent && !oneTimeEnabled))
1486 {
1487 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1488
Patrick Venture0b02be92018-08-31 11:55:55 -07001489 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1490 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301491 }
1492
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001493 auto modeItr = modeIpmiToDbus.find(bootOption);
1494 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1495 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001496 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001497 rc = setBootSource(sourceItr->second);
1498 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001499 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001500 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001501 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001502 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001503 // If a set boot device is mapping to a boot source, then reset
1504 // the boot mode D-Bus property to default.
1505 // This way the ipmid code can determine which property is not
1506 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001507 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001508 {
1509 setBootMode(Mode::Modes::Regular);
1510 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001511 }
1512 if (modeIpmiToDbus.end() != modeItr)
1513 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001514 rc = setBootMode(modeItr->second);
1515 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001516 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001517 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001518 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001519 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001520 // If a set boot device is mapping to a boot mode, then reset
1521 // the boot source D-Bus property to default.
1522 // This way the ipmid code can determine which property is not
1523 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001524 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001525 {
1526 setBootSource(Source::Sources::Default);
1527 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001528 }
1529 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001530 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001531 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001532 report<InternalFailure>();
1533 *data_len = 0;
1534 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001535 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001536 }
1537 else if (reqptr->parameter ==
1538 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1539 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001540
1541 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001542 if (ret < 0)
1543 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301544 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001545 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001546 rc = IPMI_CC_UNSPECIFIED_ERROR;
1547 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001548 }
1549 else if (reqptr->parameter ==
1550 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1551 {
Tom Josephf536c902017-09-25 18:08:15 +05301552 // Handle parameter #4 and return command completed normally
1553 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1554 // parameter. This is added to support the ipmitool command `chassis
1555 // bootdev` which sends set on parameter #4, before setting the boot
1556 // flags.
1557 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001558 }
1559 else
1560 {
1561 log<level::ERR>("Unsupported parameter",
1562 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001563 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001564 }
1565
1566 return rc;
1567}
1568
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001569/** @brief implements Get POH counter command
1570 * @parameter
1571 * - none
1572 * @returns IPMI completion code plus response data
1573 * - minPerCount - Minutes per count
1574 * - counterReading - counter reading
1575 */
1576ipmi::RspType<uint8_t, // Minutes per count
1577 uint32_t // Counter reading
1578 >
1579 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001580{
1581 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001582 try
1583 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001584 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1585 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001586 }
1587 catch (std::exception& e)
1588 {
1589 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001590 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001591 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001592}
1593
Vernon Mauerye278ead2018-10-09 09:23:43 -07001594ipmi::RspType<uint8_t>
1595 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
1596 uint8_t policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001597{
Vernon Mauerye278ead2018-10-09 09:23:43 -07001598 constexpr uint8_t ccParamNotSupported = 0x80;
Yong Lic6713cf2018-09-12 12:35:13 +08001599 power_policy::DbusValue value =
1600 power_policy::RestorePolicy::Policy::AlwaysOff;
1601
Vernon Mauerye278ead2018-10-09 09:23:43 -07001602 if (policy & ~power_policy::policyBitMask)
Yong Lic6713cf2018-09-12 12:35:13 +08001603 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001604 phosphor::logging::log<level::ERR>(
1605 "Reserved request parameter",
1606 entry("REQ=0x%x", static_cast<int>(policy)));
1607 return ipmi::response(ccParamNotSupported);
Yong Lic6713cf2018-09-12 12:35:13 +08001608 }
1609
Vernon Mauerye278ead2018-10-09 09:23:43 -07001610 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001611 {
1612 // just return the supported policy
Vernon Mauerye278ead2018-10-09 09:23:43 -07001613 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001614 }
1615
1616 for (auto const& it : power_policy::dbusToIpmi)
1617 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001618 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001619 {
1620 value = it.first;
1621 break;
1622 }
1623 }
1624
1625 try
1626 {
1627 const settings::Path& powerRestoreSetting =
1628 chassis::internal::cache::objects.map
1629 .at(chassis::internal::powerRestoreIntf)
1630 .front();
Vernon Mauery16b86932019-05-01 08:36:11 -07001631 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08001632
Vernon Mauerye278ead2018-10-09 09:23:43 -07001633 auto sdbusp = getSdBus();
1634 boost::system::error_code ec;
1635 sdbusp->yield_method_call<void>(
1636 yield, ec,
Yong Lic6713cf2018-09-12 12:35:13 +08001637 chassis::internal::cache::objects
1638 .service(powerRestoreSetting,
1639 chassis::internal::powerRestoreIntf)
1640 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07001641 powerRestoreSetting, ipmi::PROP_INTF, "Set",
1642 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1643 property);
1644 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08001645 {
1646 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07001647 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001648 }
1649 }
1650 catch (InternalFailure& e)
1651 {
1652 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07001653 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001654 }
1655
Vernon Mauerye278ead2018-10-09 09:23:43 -07001656 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001657}
1658
Adriana Kobylak40814c62015-10-27 15:58:44 -05001659void register_netfn_chassis_functions()
1660{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001661 createIdentifyTimer();
1662
Tom05732372016-09-06 17:21:23 +05301663 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001664 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1665 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001666
Tom05732372016-09-06 17:21:23 +05301667 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001668 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1669 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001670
Yong Liae4b0402018-11-02 11:12:14 +08001671 // Set Chassis Capabilities
1672 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1673 ipmi_set_chassis_cap, PRIVILEGE_USER);
1674
Tom05732372016-09-06 17:21:23 +05301675 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301676 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001677 ipmi_chassis_get_sys_boot_options,
1678 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001679
Tom05732372016-09-06 17:21:23 +05301680 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001681 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1682 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001683
Tom05732372016-09-06 17:21:23 +05301684 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001685 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1686 ipmi::chassis::cmdChassisControl,
1687 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001688
Tom Joseph5110c122018-03-23 17:55:40 +05301689 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001690 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1691 ipmi::chassis::cmdChassisIdentify,
1692 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301693
Tom05732372016-09-06 17:21:23 +05301694 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301695 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001696 ipmi_chassis_set_sys_boot_options,
1697 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001698 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001699 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1700 ipmi::chassis::cmdGetPohCounter,
1701 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001702
1703 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07001704 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1705 ipmi::chassis::cmdSetPowerRestorePolicy,
1706 ipmi::Privilege::Operator,
1707 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06001708}