blob: f17fa9da51aacd71542081329662967f567e1635 [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;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700102static constexpr const char* powerButtonIntf =
103 "xyz.openbmc_project.Chassis.Buttons.Power";
104static constexpr const char* powerButtonPath =
105 "/xyz/openbmc_project/Chassis/Buttons/Power0";
106static constexpr const char* resetButtonIntf =
107 "xyz.openbmc_project.Chassis.Buttons.Reset";
108static constexpr const char* resetButtonPath =
109 "/xyz/openbmc_project/Chassis/Buttons/Reset0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530110
Nan Li8d15fb42016-08-16 22:29:40 +0800111typedef struct
112{
113 uint8_t cap_flags;
114 uint8_t fru_info_dev_addr;
115 uint8_t sdr_dev_addr;
116 uint8_t sel_dev_addr;
117 uint8_t system_management_dev_addr;
118 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700119} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800120
Nan Lifdd8ec52016-08-28 03:57:40 +0800121typedef struct
122{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500123 uint8_t cur_power_state;
124 uint8_t last_power_event;
125 uint8_t misc_power_state;
126 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700127} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800128
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530129// Phosphor Host State manager
130namespace State = sdbusplus::xyz::openbmc_project::State::server;
131
Vernon Mauery185b9f82018-07-20 10:52:36 -0700132namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500133
Ratan Guptadcb10672017-07-10 10:33:50 +0530134using namespace phosphor::logging;
135using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500136using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700137
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500138namespace chassis
139{
140namespace internal
141{
142
143constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
144constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500145constexpr auto powerRestoreIntf =
146 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500147sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
148
149namespace cache
150{
151
152settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500153 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500154
155} // namespace cache
156} // namespace internal
157} // namespace chassis
158
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500159namespace poh
160{
161
162constexpr auto minutesPerCount = 60;
163
164} // namespace poh
165
Patrick Venture0b02be92018-08-31 11:55:55 -0700166struct get_sys_boot_options_t
167{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500168 uint8_t parameter;
169 uint8_t set;
170 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700171} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500172
Patrick Venture0b02be92018-08-31 11:55:55 -0700173struct get_sys_boot_options_response_t
174{
shgoupfd84fbbf2015-12-17 10:05:51 +0800175 uint8_t version;
176 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500177 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700178} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800179
Patrick Venture0b02be92018-08-31 11:55:55 -0700180struct set_sys_boot_options_t
181{
shgoupfd84fbbf2015-12-17 10:05:51 +0800182 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500183 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700184} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500185
Ratan Guptadcb10672017-07-10 10:33:50 +0530186int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500187{
Ratan Guptadcb10672017-07-10 10:33:50 +0530188 ipmi::PropertyMap properties;
189 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530190 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500191
Ratan Guptadcb10672017-07-10 10:33:50 +0530192 try
193 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700194 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530195 // objects,to handle such cases we are interested on that object
196 // which are on interested busname.
197 // Currenlty mapper doesn't give the readable busname(gives busid)
198 // so we can't match with bus name so giving some object specific info
199 // as SETTINGS_MATCH.
200 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500201
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530202 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530203
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530204 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
205 SETTINGS_ROOT, SETTINGS_MATCH);
206
207 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
208 SETTINGS_ROOT, SETTINGS_MATCH);
209
Patrick Venture0b02be92018-08-31 11:55:55 -0700210 properties = ipmi::getAllDbusProperties(
211 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
212 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
213 macObjectInfo.first, MAC_INTERFACE,
214 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530215
Vernon Maueryf442e112019-04-09 11:44:36 -0700216 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530217
Vernon Maueryf442e112019-04-09 11:44:36 -0700218 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530219
Vernon Maueryf442e112019-04-09 11:44:36 -0700220 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530221
Patrick Venture0b02be92018-08-31 11:55:55 -0700222 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700223 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700224 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
225 ? 1
226 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530227
Vernon Maueryf442e112019-04-09 11:44:36 -0700228 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530229
Ratan Guptad70f4532017-08-04 02:07:31 +0530230 // it is expected here that we should get the valid data
231 // but we may also get the default values.
232 // Validation of the data is done by settings.
233 //
234 // if mac address is default mac address then
235 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530236 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530237 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700238 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530239 rc = -1;
240 return rc;
241 }
242 // if addr is static then ipaddress,gateway,prefix
243 // should not be default one,don't send blank override.
244 if (isStatic)
245 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700246 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
247 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530248 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700249 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530250 rc = -1;
251 return rc;
252 }
253 }
254
Patrick Venture0b02be92018-08-31 11:55:55 -0700255 sscanf(
256 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
257 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
258 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
259 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530260
Ratan Guptadcb10672017-07-10 10:33:50 +0530261 respptr->data[MAC_OFFSET + 6] = 0x00;
262
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700263 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
264 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530265
Vernon Maueryf442e112019-04-09 11:44:36 -0700266 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
267 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
268 ? AF_INET
269 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530270
Patrick Venture0b02be92018-08-31 11:55:55 -0700271 addrSize = (addressFamily == AF_INET)
272 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
273 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530274
275 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530276 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700277 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530278
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530279 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
280
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700281 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530282
283 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
284
Ratan Guptad70f4532017-08-04 02:07:31 +0530285 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700286 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530287 }
288 catch (InternalFailure& e)
289 {
290 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700291 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530292 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500293 return rc;
294 }
295
Patrick Venture0b02be92018-08-31 11:55:55 -0700296 // PetiBoot-Specific
297 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700298 std::memcpy(respptr->data, net_conf_initial_bytes,
299 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500300
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700301 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530302
Ratan Guptafd28dd72016-08-01 04:58:01 -0500303#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700304 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500305
Ratan Guptadcb10672017-07-10 10:33:50 +0530306 for (uint8_t pos = 0; pos < index; pos++)
307 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700308 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530309 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500310#endif
311
Ratan Guptafd28dd72016-08-01 04:58:01 -0500312 return rc;
313}
314
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530315/** @brief convert IPv4 and IPv6 addresses from binary to text form.
316 * @param[in] family - IPv4/Ipv6
317 * @param[in] data - req data pointer.
318 * @param[in] offset - offset in the data.
319 * @param[in] addrSize - size of the data which needs to be read from offset.
320 * @returns address in text form.
321 */
322
Patrick Venture0b02be92018-08-31 11:55:55 -0700323std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
324 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530325{
326 char ipAddr[INET6_ADDRSTRLEN] = {};
327
Patrick Venture0b02be92018-08-31 11:55:55 -0700328 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530329 {
330 case AF_INET:
331 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700332 struct sockaddr_in addr4
333 {
334 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700335 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530336
Patrick Venture0b02be92018-08-31 11:55:55 -0700337 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530338
339 break;
340 }
341 case AF_INET6:
342 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700343 struct sockaddr_in6 addr6
344 {
345 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700346 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530347
Patrick Venture0b02be92018-08-31 11:55:55 -0700348 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530349
350 break;
351 }
352 default:
353 {
354 return {};
355 }
356 }
357
358 return ipAddr;
359}
360
Ratan Guptadcb10672017-07-10 10:33:50 +0530361int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500362{
Ratan Guptadcb10672017-07-10 10:33:50 +0530363 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500364 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700365 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530366 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700367 char addrOrigin{0};
368 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530369 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530370 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700371 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
372 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530373 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530374 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500375
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500377 // version starts from sixth byte
378
Ratan Guptadcb10672017-07-10 10:33:50 +0530379 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500380 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530381 do
382 {
383 // cookie == 0x21 0x70 0x62 0x21
384 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700385 (net_conf_initial_bytes + COOKIE_OFFSET),
386 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530387 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700388 // cookie == 0
389 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
390 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530391 {
392 // need to zero out the network settings.
393 break;
394 }
395
396 log<level::ERR>("Invalid Cookie");
397 elog<InternalFailure>();
398 }
399
400 // vesion == 0x00 0x01
401 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700402 (net_conf_initial_bytes + VERSION_OFFSET),
403 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530404 {
405
406 log<level::ERR>("Invalid Version");
407 elog<InternalFailure>();
408 }
409
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700410 std::snprintf(
411 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
412 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
413 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
414 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530415
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700416 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
417 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530418
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530419 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530420 {
421 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530422 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530423 }
424
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530425 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700426 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
427 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530428
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530429 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530430
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700431 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
432 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530433
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530434 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
435
Ratan Gupta8c31d232017-08-13 05:49:43 +0530436 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530437 {
438 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
439 family = AF_INET6;
440 }
441
Patrick Venture0b02be92018-08-31 11:55:55 -0700442 ipAddress =
443 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530444
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530445 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
446
Patrick Venture0b02be92018-08-31 11:55:55 -0700447 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530448
Patrick Venture0b02be92018-08-31 11:55:55 -0700449 // Cookie == 0 or it is a valid cookie
450 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
451 std::to_string(prefix) + ",gateway="s + gateway +
452 ",mac="s + mac + ",addressOrigin="s +
453 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500454
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530455 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
456
457 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
458 SETTINGS_ROOT, SETTINGS_MATCH);
459 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
460 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530461 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530462 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700463 IP_INTERFACE, "Address", std::string(ipAddress));
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, "PrefixLength", prefix);
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, "Origin", addressOrigin);
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, "Gateway", std::string(gateway));
470 ipmi::setDbusProperty(
471 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
472 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530473 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700474 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500475
Patrick Venture0b02be92018-08-31 11:55:55 -0700476 log<level::DEBUG>(
477 "Network configuration changed",
478 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500479 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530480 catch (InternalFailure& e)
481 {
482 commit<InternalFailure>();
483 return -1;
484 }
485
486 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500487}
488
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500489uint32_t getPOHCounter()
490{
491 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
492
Patrick Venture0b02be92018-08-31 11:55:55 -0700493 auto chassisStateObj =
494 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500495
Patrick Venture0b02be92018-08-31 11:55:55 -0700496 auto service =
497 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500498
Patrick Venture0b02be92018-08-31 11:55:55 -0700499 auto propValue =
500 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
501 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500502
Vernon Maueryf442e112019-04-09 11:44:36 -0700503 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500504}
505
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500506ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
507 ipmi_request_t request,
508 ipmi_response_t response,
509 ipmi_data_len_t data_len,
510 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500511{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500512 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800513 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500514 *data_len = 0;
515 return rc;
516}
517
Nan Li8d15fb42016-08-16 22:29:40 +0800518ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700519 ipmi_request_t request,
520 ipmi_response_t response,
521 ipmi_data_len_t data_len,
522 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800523{
524 // sd_bus error
525 ipmi_ret_t rc = IPMI_CC_OK;
526
527 ipmi_chassis_cap_t chassis_cap{};
528
Yong Liae4b0402018-11-02 11:12:14 +0800529 if (*data_len != 0)
530 {
531 return IPMI_CC_REQ_DATA_LEN_INVALID;
532 }
533
Nan Li8d15fb42016-08-16 22:29:40 +0800534 *data_len = sizeof(ipmi_chassis_cap_t);
535
Yong Liae4b0402018-11-02 11:12:14 +0800536 try
537 {
538 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800539
Yong Liae4b0402018-11-02 11:12:14 +0800540 ipmi::DbusObjectInfo chassisCapObject =
541 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800542
Yong Liae4b0402018-11-02 11:12:14 +0800543 // capabilities flags
544 // [7..4] - reserved
545 // [3] – 1b = provides power interlock (IPM 1.5)
546 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
547 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
548 // has capabilities
549 // to lock out external power control and reset button or
550 // front panel interfaces and/or detect tampering with those
551 // interfaces).
552 // [0] -1b = Chassis provides intrusion (physical security) sensor.
553 // set to default value 0x0.
554 ipmi::Value variant = ipmi::getDbusProperty(
555 bus, chassisCapObject.second, chassisCapObject.first,
556 chassisCapIntf, chassisCapFlagsProp);
Vernon Maueryf442e112019-04-09 11:44:36 -0700557 chassis_cap.cap_flags = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800558
Yong Liae4b0402018-11-02 11:12:14 +0800559 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
560 chassisCapObject.first, chassisCapIntf,
561 chassisFRUDevAddrProp);
562 // Chassis FRU info Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700563 chassis_cap.fru_info_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800564
Yong Liae4b0402018-11-02 11:12:14 +0800565 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
566 chassisCapObject.first, chassisCapIntf,
567 chassisSDRDevAddrProp);
568 // Chassis SDR Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700569 chassis_cap.sdr_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800570
Yong Liae4b0402018-11-02 11:12:14 +0800571 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
572 chassisCapObject.first, chassisCapIntf,
573 chassisSELDevAddrProp);
574 // Chassis SEL Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700575 chassis_cap.sel_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800576
Yong Liae4b0402018-11-02 11:12:14 +0800577 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
578 chassisCapObject.first, chassisCapIntf,
579 chassisSMDevAddrProp);
580 // Chassis System Management Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700581 chassis_cap.system_management_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800582
Yong Liae4b0402018-11-02 11:12:14 +0800583 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
584 chassisCapObject.first, chassisCapIntf,
585 chassisBridgeDevAddrProp);
586 // Chassis Bridge Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700587 chassis_cap.bridge_dev_addr = std::get<uint8_t>(variant);
Yong Liae4b0402018-11-02 11:12:14 +0800588 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
589 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
590 std::copy(chassisP, chassisP + *data_len, respP);
591 }
592 catch (std::exception& e)
593 {
594 log<level::ERR>(e.what());
595 rc = IPMI_CC_UNSPECIFIED_ERROR;
596 *data_len = 0;
597 return rc;
598 }
599
600 return rc;
601}
602
603ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
604 ipmi_request_t request,
605 ipmi_response_t response,
606 ipmi_data_len_t data_len,
607 ipmi_context_t context)
608{
609 ipmi_ret_t rc = IPMI_CC_OK;
610
611 if (*data_len != sizeof(ipmi_chassis_cap_t))
612 {
613 log<level::ERR>("Unsupported request length",
614 entry("LEN=0x%x", *data_len));
615 *data_len = 0;
616 return IPMI_CC_REQ_DATA_LEN_INVALID;
617 }
618
619 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
620
621 *data_len = 0;
622
623 // check input data
624 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
625 {
626 log<level::ERR>("Unsupported request parameter(CAP Flags)",
627 entry("REQ=0x%x", chassisCap->cap_flags));
628 return IPMI_CC_INVALID_FIELD_REQUEST;
629 }
630
631 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
632 {
633 log<level::ERR>("Unsupported request parameter(FRU Addr)",
634 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
635 return IPMI_CC_INVALID_FIELD_REQUEST;
636 }
637
638 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
639 {
640 log<level::ERR>("Unsupported request parameter(SDR Addr)",
641 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
642 return IPMI_CC_INVALID_FIELD_REQUEST;
643 }
644
645 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
646 {
647 log<level::ERR>("Unsupported request parameter(SEL Addr)",
648 entry("REQ=0x%x", chassisCap->sel_dev_addr));
649 return IPMI_CC_INVALID_FIELD_REQUEST;
650 }
651
652 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
653 {
654 log<level::ERR>(
655 "Unsupported request parameter(SM Addr)",
656 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
657 return IPMI_CC_INVALID_FIELD_REQUEST;
658 }
659
660 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
661 {
662 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
663 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
664 return IPMI_CC_INVALID_FIELD_REQUEST;
665 }
666
667 try
668 {
669 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
670 ipmi::DbusObjectInfo chassisCapObject =
671 ipmi::getDbusObject(bus, chassisCapIntf);
672
673 ipmi::setDbusProperty(bus, chassisCapObject.second,
674 chassisCapObject.first, chassisCapIntf,
675 chassisCapFlagsProp, chassisCap->cap_flags);
676
677 ipmi::setDbusProperty(bus, chassisCapObject.second,
678 chassisCapObject.first, chassisCapIntf,
679 chassisFRUDevAddrProp,
680 chassisCap->fru_info_dev_addr);
681
682 ipmi::setDbusProperty(bus, chassisCapObject.second,
683 chassisCapObject.first, chassisCapIntf,
684 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
685
686 ipmi::setDbusProperty(bus, chassisCapObject.second,
687 chassisCapObject.first, chassisCapIntf,
688 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
689
690 ipmi::setDbusProperty(bus, chassisCapObject.second,
691 chassisCapObject.first, chassisCapIntf,
692 chassisSMDevAddrProp,
693 chassisCap->system_management_dev_addr);
694
695 ipmi::setDbusProperty(bus, chassisCapObject.second,
696 chassisCapObject.first, chassisCapIntf,
697 chassisBridgeDevAddrProp,
698 chassisCap->bridge_dev_addr);
699 }
700 catch (std::exception& e)
701 {
702 log<level::ERR>(e.what());
703 rc = IPMI_CC_UNSPECIFIED_ERROR;
704 return rc;
705 }
Nan Li8d15fb42016-08-16 22:29:40 +0800706
707 return rc;
708}
709
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530710//------------------------------------------
711// Calls into Host State Manager Dbus object
712//------------------------------------------
713int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600714{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500715 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700716 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500717 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700718 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
719 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530720
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500721 // sd_bus error
722 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700723 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600724
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500725 // SD Bus error report mechanism.
726 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600727
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500728 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700729 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500730 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
731 if (rc < 0)
732 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700733 log<level::ERR>(
734 "Failed to get bus name",
735 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500736 return rc;
737 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530738
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500739 // Convert to string equivalent of the passed in transition enum.
740 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530741
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500742 rc = sd_bus_call_method(bus_type, // On the system bus
743 busname, // Service to contact
744 HOST_STATE_MANAGER_ROOT, // Object path
745 DBUS_PROPERTY_IFACE, // Interface name
746 "Set", // Method to be called
747 &bus_error, // object to return error
748 nullptr, // Response buffer if any
749 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700750 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
751 request.c_str());
752 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500753 {
754 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530755 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500756 }
757 else
758 {
759 log<level::INFO>("Transition request initiated successfully");
760 }
vishwa36993272015-11-20 12:43:49 -0600761
762 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500763 free(busname);
vishwa36993272015-11-20 12:43:49 -0600764
Sergey Solomineb9b8142016-08-23 09:07:28 -0500765 return rc;
vishwa36993272015-11-20 12:43:49 -0600766}
767
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500768namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500769{
Nan Lifdd8ec52016-08-28 03:57:40 +0800770
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500771using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
772using IpmiValue = uint8_t;
773using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800774
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700775const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500776 {RestorePolicy::Policy::AlwaysOff, 0x00},
777 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700778 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800779
Yong Lic6713cf2018-09-12 12:35:13 +0800780static constexpr uint8_t noChange = 0x03;
781static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
782static constexpr uint8_t policyBitMask = 0x07;
783static constexpr uint8_t setPolicyReqLen = 1;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700784
785/* helper function for Get Chassis Status Command
786 */
787std::optional<uint2_t> getPowerRestorePolicy()
788{
789 uint2_t restorePolicy = 0;
790 using namespace chassis::internal;
791
792 try
793 {
794 const auto& powerRestoreSetting =
795 cache::objects.map.at(powerRestoreIntf).front();
796 ipmi::Value result = ipmi::getDbusProperty(
797 *getSdBus(),
798 cache::objects.service(powerRestoreSetting, powerRestoreIntf)
799 .c_str(),
800 powerRestoreSetting.c_str(), powerRestoreIntf,
801 "PowerRestorePolicy");
802 auto powerRestore = RestorePolicy::convertPolicyFromString(
803 std::get<std::string>(result));
804 restorePolicy = dbusToIpmi.at(powerRestore);
805 }
806 catch (const std::exception& e)
807 {
808 log<level::ERR>(
809 "Failed to fetch pgood property", entry("ERROR=%s", e.what()),
810 entry("PATH=%s",
811 cache::objects.map.at(powerRestoreIntf).front().c_str()),
812 entry("INTERFACE=%s", powerRestoreIntf));
813 return std::nullopt;
814 }
815 return std::make_optional(restorePolicy);
816}
817
818/*
819 * getPowerStatus
820 * helper function for Get Chassis Status Command
821 * return - optional value for pgood (no value on error)
822 */
823std::optional<bool> getPowerStatus()
824{
825 constexpr const char* powerControlObj =
826 "/xyz/openbmc_project/Chassis/Control/Power0";
827 constexpr const char* powerControlIntf =
828 "xyz.openbmc_project.Chassis.Control.Power";
829 bool powerGood = false;
830 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
831 try
832 {
833 auto service =
834 ipmi::getService(*busp, powerControlIntf, powerControlObj);
835
836 ipmi::Value variant = ipmi::getDbusProperty(
837 *busp, service, powerControlObj, powerControlIntf, "pgood");
838 powerGood = static_cast<bool>(std::get<int>(variant));
839 }
840 catch (const std::exception& e)
841 {
842 try
843 {
844 // FIXME: some legacy modules use the older path; try that next
845 constexpr const char* legacyPwrCtrlObj =
846 "/org/openbmc/control/power0";
847 constexpr const char* legacyPwrCtrlIntf =
848 "org.openbmc.control.Power";
849 auto service =
850 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
851
852 ipmi::Value variant = ipmi::getDbusProperty(
853 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
854 powerGood = static_cast<bool>(std::get<int>(variant));
855 }
856 catch (const std::exception& e)
857 {
858 log<level::ERR>("Failed to fetch pgood property",
859 entry("ERROR=%s", e.what()),
860 entry("PATH=%s", powerControlObj),
861 entry("INTERFACE=%s", powerControlIntf));
862 return std::nullopt;
863 }
864 }
865 return std::make_optional(powerGood);
866}
867
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500868} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800869
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700870static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
871 const std::string& buttonIntf)
872{
873 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
874 bool buttonDisabled = false;
875 try
876 {
877 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
878 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
879 buttonIntf, "Enabled");
880 buttonDisabled = !std::get<bool>(enabled);
881 }
882 catch (sdbusplus::exception::SdBusError& e)
883 {
884 log<level::ERR>("Fail to get button Enabled property",
885 entry("PATH=%s", buttonPath.c_str()),
886 entry("ERROR=%s", e.what()));
887 return std::nullopt;
888 }
889 return std::make_optional(buttonDisabled);
890}
891
Nan Lifdd8ec52016-08-28 03:57:40 +0800892//----------------------------------------------------------------------
893// Get Chassis Status commands
894//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700895ipmi::RspType<bool, // Power is on
896 bool, // Power overload
897 bool, // Interlock
898 bool, // power fault
899 bool, // power control fault
900 uint2_t, // power restore policy
901 bool, // reserved
902
903 bool, // AC failed
904 bool, // last power down caused by a Power overload
905 bool, // last power down caused by a power interlock
906 bool, // last power down caused by power fault
907 bool, // last ‘Power is on’ state was entered via IPMI command
908 uint3_t, // reserved
909
910 bool, // Chassis intrusion active
911 bool, // Front Panel Lockout active
912 bool, // Drive Fault
913 bool, // Cooling/fan fault detected
914 uint2_t, // Chassis Identify State
915 bool, // Chassis Identify command and state info supported
916 bool, // reserved
917
918 bool, // Power off button disabled
919 bool, // Reset button disabled
920 bool, // Diagnostic Interrupt button disabled
921 bool, // Standby (sleep) button disabled
922 bool, // Power off button disable allowed
923 bool, // Reset button disable allowed
924 bool, // Diagnostic Interrupt button disable allowed
925 bool // Standby (sleep) button disable allowed
926 >
927 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +0800928{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500929 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700930 std::optional<uint2_t> restorePolicy =
931 power_policy::getPowerRestorePolicy();
932 std::optional<bool> powerGood = power_policy::getPowerStatus();
933 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500934 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700935 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500936 }
Nan Lifdd8ec52016-08-28 03:57:40 +0800937
938 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700939 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700940 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700941 // allow disable if the interface is present
942 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
943 // default return the button is enabled (not disabled)
944 bool powerButtonDisabled = false;
945 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700946 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700947 // return the real value of the button status, if present
948 powerButtonDisabled = *powerButtonReading;
949 }
950
951 std::optional<bool> resetButtonReading =
952 getButtonEnabled(resetButtonPath, resetButtonIntf);
953 // allow disable if the interface is present
954 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
955 // default return the button is enabled (not disabled)
956 bool resetButtonDisabled = false;
957 if (resetButtonDisableAllow)
958 {
959 // return the real value of the button status, if present
960 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700961 }
Nan Lifdd8ec52016-08-28 03:57:40 +0800962
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700963 // This response has a lot of hard-coded, unsupported fields
964 // They are set to false or 0
965 constexpr bool powerOverload = false;
966 constexpr bool chassisInterlock = false;
967 constexpr bool powerFault = false;
968 constexpr bool powerControlFault = false;
969 constexpr bool powerDownAcFailed = false;
970 constexpr bool powerDownOverload = false;
971 constexpr bool powerDownInterlock = false;
972 constexpr bool powerDownPowerFault = false;
973 constexpr bool powerStatusIPMI = false;
974 constexpr bool chassisIntrusionActive = false;
975 constexpr bool frontPanelLockoutActive = false;
976 constexpr bool driveFault = false;
977 constexpr bool coolingFanFault = false;
978 // chassisIdentifySupport set because this command is implemented
979 constexpr bool chassisIdentifySupport = true;
980 constexpr uint2_t chassisIdentifyState(0);
981 constexpr bool diagButtonDisabled = false;
982 constexpr bool sleepButtonDisabled = false;
983 constexpr bool diagButtonDisableAllow = false;
984 constexpr bool sleepButtonDisableAllow = false;
985
986 return ipmi::responseSuccess(
987 *powerGood, powerOverload, chassisInterlock, powerFault,
988 powerControlFault, *restorePolicy,
989 false, // reserved
990
991 powerDownAcFailed, powerDownOverload, powerDownInterlock,
992 powerDownPowerFault, powerStatusIPMI,
993 uint3_t(0), // reserved
994
995 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
996 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
997 false, // reserved
998
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700999 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001000 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1001 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001002}
Chris Austen7888c4d2015-12-03 15:26:20 -06001003
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301004//-------------------------------------------------------------
1005// Send a command to SoftPowerOff application to stop any timer
1006//-------------------------------------------------------------
1007int stop_soft_off_timer()
1008{
Patrick Venture0b02be92018-08-31 11:55:55 -07001009 constexpr auto iface = "org.freedesktop.DBus.Properties";
1010 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1011 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301012
Patrick Venture0b02be92018-08-31 11:55:55 -07001013 constexpr auto property = "ResponseReceived";
1014 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1015 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301016
1017 // Get the system bus where most system services are provided.
1018 auto bus = ipmid_get_sd_bus_connection();
1019
1020 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001021 // TODO openbmc/openbmc#1661 - Mapper refactor
1022 //
1023 // See openbmc/openbmc#1743 for some details but high level summary is that
1024 // for now the code will directly call the soft off interface due to a
1025 // race condition with mapper usage
1026 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001027 // char *busname = nullptr;
1028 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1029 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001030 //{
1031 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301032 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001033 // return r;
1034 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301035
1036 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001037 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001038 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1039 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301040 if (rc < 0)
1041 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301042 log<level::ERR>("Failed to set property in SoftPowerOff object",
1043 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301044 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001045
Patrick Venture0b02be92018-08-31 11:55:55 -07001046 // TODO openbmc/openbmc#1661 - Mapper refactor
1047 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301048 return rc;
1049}
1050
vishwa36993272015-11-20 12:43:49 -06001051//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001052// Create file to indicate there is no need for softoff notification to host
1053//----------------------------------------------------------------------
1054void indicate_no_softoff_needed()
1055{
1056 fs::path path{HOST_INBAND_REQUEST_DIR};
1057 if (!fs::is_directory(path))
1058 {
1059 fs::create_directory(path);
1060 }
1061
1062 // Add the host instance (default 0 for now) to the file name
1063 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001064 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001065 size++; // null
1066 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001067 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001068
1069 // Append file name to directory and create it
1070 path /= buf.get();
1071 std::ofstream(path.c_str());
1072}
1073
anil kumar appanadafff5f2019-04-27 18:06:00 +00001074/** @brief Implementation of chassis control command
1075 *
1076 * @param - chassisControl command byte
1077 *
1078 * @return Success or InvalidFieldRequest.
1079 */
1080ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001081{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001082 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001083 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001084 {
1085 case CMD_POWER_ON:
1086 rc = initiate_state_transition(State::Host::Transition::On);
1087 break;
1088 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301089 // This path would be hit in 2 conditions.
1090 // 1: When user asks for power off using ipmi chassis command 0x04
1091 // 2: Host asking for power off post shutting down.
1092
1093 // If it's a host requested power off, then need to nudge Softoff
1094 // application that it needs to stop the watchdog timer if running.
1095 // If it is a user requested power off, then this is not really
1096 // needed. But then we need to differentiate between user and host
1097 // calling this same command
1098
1099 // For now, we are going ahead with trying to nudge the soft off and
1100 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001101 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301102
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001103 // Only request the Off transition if the soft power off
1104 // application is not running
1105 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001106 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001107 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301108 // that it should not run. Not doing this will result in State
1109 // manager doing a default soft power off when asked for power
1110 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001111 indicate_no_softoff_needed();
1112
1113 // Now request the shutdown
1114 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001115 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001116 else
1117 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301118 log<level::INFO>("Soft off is running, so let shutdown target "
1119 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001120 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001121 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301122
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001123 case CMD_HARD_RESET:
1124 case CMD_POWER_CYCLE:
1125 // SPEC has a section that says certain implementations can trigger
1126 // PowerOn if power is Off when a command to power cycle is
1127 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001128
1129 // First create a file to indicate to the soft off application
1130 // that it should not run since this is a direct user initiated
1131 // power reboot request (i.e. a reboot request that is not
1132 // originating via a soft power off SMS request)
1133 indicate_no_softoff_needed();
1134
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001135 rc = initiate_state_transition(State::Host::Transition::Reboot);
1136 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301137
1138 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1139 // Request Host State Manager to do a soft power off
1140 rc = initiate_state_transition(State::Host::Transition::Off);
1141 break;
1142
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001143 default:
1144 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301145 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001146 entry("CMD=0x%X", chassisControl));
1147 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001148 }
1149 }
vishwa36993272015-11-20 12:43:49 -06001150
anil kumar appanadafff5f2019-04-27 18:06:00 +00001151 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1152 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001153}
1154
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001155/** @brief Return D-Bus connection string to enclosure identify LED object
1156 *
1157 * @param[in, out] connection - connection to D-Bus object
1158 * @return a IPMI return code
1159 */
1160std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301161{
Tom Joseph5110c122018-03-23 17:55:40 +05301162 // lookup enclosure_identify group owner(s) in mapper
1163 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001164 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1165 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301166
1167 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001168 static const std::vector<std::string> interfaces = {
1169 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301170 mapperCall.append(interfaces);
1171 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1172 if (mapperReply.is_method_error())
1173 {
1174 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001175 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301176 }
1177 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1178 mapperReply.read(mapperResp);
1179
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001180 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301181 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001182 log<level::ERR>(
1183 "Invalid number of enclosure identify objects.",
1184 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001185 elog<InternalFailure>();
1186 }
1187 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1188 return pair.first;
1189}
Tom Joseph5110c122018-03-23 17:55:40 +05301190
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001191/** @brief Turn On/Off enclosure identify LED
1192 *
1193 * @param[in] flag - true to turn on LED, false to turn off
1194 * @return a IPMI return code
1195 */
1196void enclosureIdentifyLed(bool flag)
1197{
1198 using namespace chassis::internal;
1199 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001200 auto msg = std::string("enclosureIdentifyLed(") +
1201 boost::lexical_cast<std::string>(flag) + ")";
1202 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001203 auto led =
1204 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1205 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001206 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001207 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001208 auto ledReply = dbus.call(led);
1209 if (ledReply.is_method_error())
1210 {
1211 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001212 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001213 elog<InternalFailure>();
1214 }
1215}
1216
1217/** @brief Callback method to turn off LED
1218 */
1219void enclosureIdentifyLedOff()
1220{
1221 try
1222 {
1223 enclosureIdentifyLed(false);
1224 }
1225 catch (const InternalFailure& e)
1226 {
1227 report<InternalFailure>();
1228 }
1229}
1230
1231/** @brief Create timer to turn on and off the enclosure LED
1232 */
1233void createIdentifyTimer()
1234{
1235 if (!identifyTimer)
1236 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001237 identifyTimer =
1238 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001239 }
1240}
1241
Vernon Mauery400cc782018-10-09 13:49:53 -07001242ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1243 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001244{
Vernon Mauery400cc782018-10-09 13:49:53 -07001245 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1246 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301247
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001248 if (identifyInterval || forceIdentify)
1249 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001250 // stop the timer if already started;
1251 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001252 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001253 try
Tom Joseph5110c122018-03-23 17:55:40 +05301254 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001255 enclosureIdentifyLed(true);
1256 }
1257 catch (const InternalFailure& e)
1258 {
1259 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001260 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301261 }
1262
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001263 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301264 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001265 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001266 }
1267 // start the timer
1268 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001269 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001270 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301271 }
Tom Josephbed26992018-07-31 23:00:24 +05301272 else if (!identifyInterval)
1273 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001274 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301275 enclosureIdentifyLedOff();
1276 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001277 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301278}
1279
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001280namespace boot_options
1281{
1282
1283using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1284using IpmiValue = uint8_t;
1285constexpr auto ipmiDefault = 0;
1286
Patrick Venture0b02be92018-08-31 11:55:55 -07001287std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001288 {0x01, Source::Sources::Network},
1289 {0x02, Source::Sources::Disk},
1290 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001291 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001292 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001293
Patrick Venture0b02be92018-08-31 11:55:55 -07001294std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001295 {0x03, Mode::Modes::Safe},
1296 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001297 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001298
Patrick Venture0b02be92018-08-31 11:55:55 -07001299std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001300 {Source::Sources::Network, 0x01},
1301 {Source::Sources::Disk, 0x02},
1302 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001303 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001304 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001305
Patrick Venture0b02be92018-08-31 11:55:55 -07001306std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001307 {Mode::Modes::Safe, 0x03},
1308 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001309 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001310
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001311} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001312
Marri Devender Rao81719702018-05-07 00:53:48 -05001313/** @brief Set the property value for boot source
1314 * @param[in] source - boot source value
1315 * @return On failure return IPMI error.
1316 */
1317static ipmi_ret_t setBootSource(const Source::Sources& source)
1318{
1319 using namespace chassis::internal;
1320 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001321 std::variant<std::string> property = convertForMessage(source);
Marri Devender Rao81719702018-05-07 00:53:48 -05001322 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1323 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001324 auto method = dbus.new_method_call(
1325 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1326 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001327 method.append(bootSourceIntf, "BootSource", property);
1328 auto reply = dbus.call(method);
1329 if (reply.is_method_error())
1330 {
1331 log<level::ERR>("Error in BootSource Set");
1332 report<InternalFailure>();
1333 return IPMI_CC_UNSPECIFIED_ERROR;
1334 }
1335 return IPMI_CC_OK;
1336}
1337
Patrick Venture0b02be92018-08-31 11:55:55 -07001338/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001339 * @param[in] mode - boot mode value
1340 * @return On failure return IPMI error.
1341 */
1342static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1343{
1344 using namespace chassis::internal;
1345 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001346 std::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001347 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1348 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001349 auto method = dbus.new_method_call(
1350 objects.service(bootModeSetting, bootModeIntf).c_str(),
1351 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001352 method.append(bootModeIntf, "BootMode", property);
1353 auto reply = dbus.call(method);
1354 if (reply.is_method_error())
1355 {
1356 log<level::ERR>("Error in BootMode Set");
1357 report<InternalFailure>();
1358 return IPMI_CC_UNSPECIFIED_ERROR;
1359 }
1360 return IPMI_CC_OK;
1361}
1362
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001363ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1364 ipmi_request_t request,
1365 ipmi_response_t response,
1366 ipmi_data_len_t data_len,
1367 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001368{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001369 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001370 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001371 char* p = NULL;
1372 get_sys_boot_options_response_t* resp =
1373 (get_sys_boot_options_response_t*)response;
1374 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001375 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001376
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001377 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001378 resp->version = SET_PARM_VERSION;
1379 resp->parm = 5;
1380 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001381
shgoupfd84fbbf2015-12-17 10:05:51 +08001382 /*
1383 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1384 * This is the only parameter used by petitboot.
1385 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001386 if (reqptr->parameter ==
1387 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1388 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001389
Ratan Guptafd28dd72016-08-01 04:58:01 -05001390 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001391 using namespace chassis::internal;
1392 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001393
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001394 try
ratagupta6f6bff2016-04-04 06:20:11 -05001395 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001396 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1397 const auto& bootSourceSetting =
1398 std::get<settings::Path>(bootSetting);
1399 auto oneTimeEnabled =
1400 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001401 auto method = dbus.new_method_call(
1402 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1403 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001404 method.append(bootSourceIntf, "BootSource");
1405 auto reply = dbus.call(method);
1406 if (reply.is_method_error())
1407 {
1408 log<level::ERR>("Error in BootSource Get");
1409 report<InternalFailure>();
1410 *data_len = 0;
1411 return IPMI_CC_UNSPECIFIED_ERROR;
1412 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001413 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001414 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001415 auto bootSource =
1416 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001417
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001418 bootSetting = settings::boot::setting(objects, bootModeIntf);
1419 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1420 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001421 objects.service(bootModeSetting, bootModeIntf).c_str(),
1422 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001423 method.append(bootModeIntf, "BootMode");
1424 reply = dbus.call(method);
1425 if (reply.is_method_error())
1426 {
1427 log<level::ERR>("Error in BootMode Get");
1428 report<InternalFailure>();
1429 *data_len = 0;
1430 return IPMI_CC_UNSPECIFIED_ERROR;
1431 }
1432 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001433 auto bootMode =
1434 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001435
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001436 bootOption = sourceDbusToIpmi.at(bootSource);
1437 if ((Mode::Modes::Regular == bootMode) &&
1438 (Source::Sources::Default == bootSource))
1439 {
1440 bootOption = ipmiDefault;
1441 }
1442 else if (Source::Sources::Default == bootSource)
1443 {
1444 bootOption = modeDbusToIpmi.at(bootMode);
1445 }
1446 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001447
Patrick Venture0b02be92018-08-31 11:55:55 -07001448 resp->data[0] = oneTimeEnabled
1449 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1450 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001451
ratagupta6f6bff2016-04-04 06:20:11 -05001452 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001453 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001454 catch (InternalFailure& e)
1455 {
1456 report<InternalFailure>();
1457 *data_len = 0;
1458 return IPMI_CC_UNSPECIFIED_ERROR;
1459 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001460 }
1461 else if (reqptr->parameter ==
1462 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1463 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001464
Patrick Venture0b02be92018-08-31 11:55:55 -07001465 *data_len =
1466 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001467
Patrick Venture0b02be92018-08-31 11:55:55 -07001468 resp->parm =
1469 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001470
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001471 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001472
Patrick Venture0b02be92018-08-31 11:55:55 -07001473 if (ret < 0)
1474 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001475
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301476 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001477 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001478 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001479 }
1480 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001481 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001482 }
1483
Patrick Venture0b02be92018-08-31 11:55:55 -07001484 else
1485 {
1486 log<level::ERR>("Unsupported parameter",
1487 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001488 }
1489
1490 if (p)
1491 free(p);
1492
Ratan Guptafd28dd72016-08-01 04:58:01 -05001493 if (rc == IPMI_CC_OK)
1494 {
1495 *data_len += 2;
1496 }
1497
shgoupfd84fbbf2015-12-17 10:05:51 +08001498 return rc;
1499}
1500
shgoupfd84fbbf2015-12-17 10:05:51 +08001501ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001502 ipmi_request_t request,
1503 ipmi_response_t response,
1504 ipmi_data_len_t data_len,
1505 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001506{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001507 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001508 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001509 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001510
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001511 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1512 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001513
shgoupfd84fbbf2015-12-17 10:05:51 +08001514 // This IPMI command does not have any resposne data
1515 *data_len = 0;
1516
1517 /* 000101
1518 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1519 * This is the only parameter used by petitboot.
1520 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001521
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001522 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1523 {
1524 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1525 using namespace chassis::internal;
1526 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001527 auto oneTimeEnabled = false;
1528 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301529 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001530 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001531
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001532 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001533 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001534 bool permanent =
1535 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1536 SET_PARM_BOOT_FLAGS_PERMANENT;
1537
Patrick Venture0b02be92018-08-31 11:55:55 -07001538 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301539
1540 oneTimeEnabled =
1541 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1542
1543 /*
1544 * Check if the current boot setting is onetime or permanent, if the
1545 * request in the command is otherwise, then set the "Enabled"
1546 * property in one_time object path to 'True' to indicate onetime
1547 * and 'False' to indicate permanent.
1548 *
1549 * Once the onetime/permanent setting is applied, then the bootMode
1550 * and bootSource is updated for the corresponding object.
1551 */
1552 if ((permanent && oneTimeEnabled) ||
1553 (!permanent && !oneTimeEnabled))
1554 {
1555 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1556
Patrick Venture0b02be92018-08-31 11:55:55 -07001557 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1558 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301559 }
1560
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001561 auto modeItr = modeIpmiToDbus.find(bootOption);
1562 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1563 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001564 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001565 rc = setBootSource(sourceItr->second);
1566 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001567 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001568 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001569 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001570 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001571 // If a set boot device is mapping to a boot source, then reset
1572 // the boot mode D-Bus property to default.
1573 // This way the ipmid code can determine which property is not
1574 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001575 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001576 {
1577 setBootMode(Mode::Modes::Regular);
1578 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001579 }
1580 if (modeIpmiToDbus.end() != modeItr)
1581 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001582 rc = setBootMode(modeItr->second);
1583 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001584 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001585 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001586 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001587 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001588 // If a set boot device is mapping to a boot mode, then reset
1589 // the boot source D-Bus property to default.
1590 // This way the ipmid code can determine which property is not
1591 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001592 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001593 {
1594 setBootSource(Source::Sources::Default);
1595 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001596 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001597 if ((modeIpmiToDbus.end() == modeItr) &&
1598 (sourceIpmiToDbus.end() == sourceItr))
1599 {
1600 // return error if boot option is not supported
1601 *data_len = 0;
1602 return IPMI_CC_INVALID_FIELD_REQUEST;
1603 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001604 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001605 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001606 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001607 report<InternalFailure>();
1608 *data_len = 0;
1609 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001610 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001611 }
1612 else if (reqptr->parameter ==
1613 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1614 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001615
1616 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001617 if (ret < 0)
1618 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301619 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001620 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001621 rc = IPMI_CC_UNSPECIFIED_ERROR;
1622 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001623 }
1624 else if (reqptr->parameter ==
1625 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1626 {
Tom Josephf536c902017-09-25 18:08:15 +05301627 // Handle parameter #4 and return command completed normally
1628 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1629 // parameter. This is added to support the ipmitool command `chassis
1630 // bootdev` which sends set on parameter #4, before setting the boot
1631 // flags.
1632 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001633 }
1634 else
1635 {
1636 log<level::ERR>("Unsupported parameter",
1637 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001638 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001639 }
1640
1641 return rc;
1642}
1643
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001644/** @brief implements Get POH counter command
1645 * @parameter
1646 * - none
1647 * @returns IPMI completion code plus response data
1648 * - minPerCount - Minutes per count
1649 * - counterReading - counter reading
1650 */
1651ipmi::RspType<uint8_t, // Minutes per count
1652 uint32_t // Counter reading
1653 >
1654 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001655{
1656 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001657 try
1658 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001659 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1660 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001661 }
1662 catch (std::exception& e)
1663 {
1664 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001665 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001666 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001667}
1668
Vernon Mauerye278ead2018-10-09 09:23:43 -07001669ipmi::RspType<uint8_t>
1670 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
1671 uint8_t policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001672{
Vernon Mauerye278ead2018-10-09 09:23:43 -07001673 constexpr uint8_t ccParamNotSupported = 0x80;
Yong Lic6713cf2018-09-12 12:35:13 +08001674 power_policy::DbusValue value =
1675 power_policy::RestorePolicy::Policy::AlwaysOff;
1676
Vernon Mauerye278ead2018-10-09 09:23:43 -07001677 if (policy & ~power_policy::policyBitMask)
Yong Lic6713cf2018-09-12 12:35:13 +08001678 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001679 phosphor::logging::log<level::ERR>(
1680 "Reserved request parameter",
1681 entry("REQ=0x%x", static_cast<int>(policy)));
1682 return ipmi::response(ccParamNotSupported);
Yong Lic6713cf2018-09-12 12:35:13 +08001683 }
1684
Vernon Mauerye278ead2018-10-09 09:23:43 -07001685 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001686 {
1687 // just return the supported policy
Vernon Mauerye278ead2018-10-09 09:23:43 -07001688 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001689 }
1690
1691 for (auto const& it : power_policy::dbusToIpmi)
1692 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001693 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001694 {
1695 value = it.first;
1696 break;
1697 }
1698 }
1699
1700 try
1701 {
1702 const settings::Path& powerRestoreSetting =
1703 chassis::internal::cache::objects.map
1704 .at(chassis::internal::powerRestoreIntf)
1705 .front();
Vernon Mauery16b86932019-05-01 08:36:11 -07001706 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08001707
Vernon Mauerye278ead2018-10-09 09:23:43 -07001708 auto sdbusp = getSdBus();
1709 boost::system::error_code ec;
1710 sdbusp->yield_method_call<void>(
1711 yield, ec,
Yong Lic6713cf2018-09-12 12:35:13 +08001712 chassis::internal::cache::objects
1713 .service(powerRestoreSetting,
1714 chassis::internal::powerRestoreIntf)
1715 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07001716 powerRestoreSetting, ipmi::PROP_INTF, "Set",
1717 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1718 property);
1719 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08001720 {
1721 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07001722 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001723 }
1724 }
1725 catch (InternalFailure& e)
1726 {
1727 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07001728 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001729 }
1730
Vernon Mauerye278ead2018-10-09 09:23:43 -07001731 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001732}
1733
Adriana Kobylak40814c62015-10-27 15:58:44 -05001734void register_netfn_chassis_functions()
1735{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001736 createIdentifyTimer();
1737
Tom05732372016-09-06 17:21:23 +05301738 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001739 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1740 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001741
Tom05732372016-09-06 17:21:23 +05301742 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001743 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1744 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001745
Yong Liae4b0402018-11-02 11:12:14 +08001746 // Set Chassis Capabilities
1747 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1748 ipmi_set_chassis_cap, PRIVILEGE_USER);
1749
Tom05732372016-09-06 17:21:23 +05301750 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301751 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001752 ipmi_chassis_get_sys_boot_options,
1753 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001754
Tom05732372016-09-06 17:21:23 +05301755 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001756 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1757 ipmi::chassis::cmdGetChassisStatus,
1758 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08001759
Tom05732372016-09-06 17:21:23 +05301760 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001761 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1762 ipmi::chassis::cmdChassisControl,
1763 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001764
Tom Joseph5110c122018-03-23 17:55:40 +05301765 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001766 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1767 ipmi::chassis::cmdChassisIdentify,
1768 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301769
Tom05732372016-09-06 17:21:23 +05301770 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301771 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001772 ipmi_chassis_set_sys_boot_options,
1773 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001774 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001775 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1776 ipmi::chassis::cmdGetPohCounter,
1777 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001778
1779 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07001780 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1781 ipmi::chassis::cmdSetPowerRestorePolicy,
1782 ipmi::Privilege::Operator,
1783 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06001784}