blob: 0d2265fd9419577b21d0a33de558074ae06dd382 [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
Yong Li70ce7352019-05-16 21:15:27 +0800868/*
869 * getACFailStatus
870 * helper function for Get Chassis Status Command
871 * return - bool value for ACFail (false on error)
872 */
873bool getACFailStatus()
874{
875 constexpr const char* powerControlObj =
876 "/xyz/openbmc_project/Chassis/Control/Power0";
877 constexpr const char* powerControlIntf =
878 "xyz.openbmc_project.Chassis.Control.Power";
879 bool acFail = false;
880 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
881 try
882 {
883 auto service =
884 ipmi::getService(*bus, powerControlIntf, powerControlObj);
885
886 ipmi::Value variant = ipmi::getDbusProperty(
887 *bus, service, powerControlObj, powerControlIntf, "PFail");
888 acFail = std::get<bool>(variant);
889 }
890 catch (const std::exception& e)
891 {
892 log<level::ERR>("Failed to fetch PFail property",
893 entry("ERROR=%s", e.what()),
894 entry("PATH=%s", powerControlObj),
895 entry("INTERFACE=%s", powerControlIntf));
896 }
897 return acFail;
898}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500899} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800900
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700901static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
902 const std::string& buttonIntf)
903{
904 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
905 bool buttonDisabled = false;
906 try
907 {
908 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
909 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
910 buttonIntf, "Enabled");
911 buttonDisabled = !std::get<bool>(enabled);
912 }
913 catch (sdbusplus::exception::SdBusError& e)
914 {
915 log<level::ERR>("Fail to get button Enabled property",
916 entry("PATH=%s", buttonPath.c_str()),
917 entry("ERROR=%s", e.what()));
918 return std::nullopt;
919 }
920 return std::make_optional(buttonDisabled);
921}
922
Nan Lifdd8ec52016-08-28 03:57:40 +0800923//----------------------------------------------------------------------
924// Get Chassis Status commands
925//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700926ipmi::RspType<bool, // Power is on
927 bool, // Power overload
928 bool, // Interlock
929 bool, // power fault
930 bool, // power control fault
931 uint2_t, // power restore policy
932 bool, // reserved
933
934 bool, // AC failed
935 bool, // last power down caused by a Power overload
936 bool, // last power down caused by a power interlock
937 bool, // last power down caused by power fault
938 bool, // last ‘Power is on’ state was entered via IPMI command
939 uint3_t, // reserved
940
941 bool, // Chassis intrusion active
942 bool, // Front Panel Lockout active
943 bool, // Drive Fault
944 bool, // Cooling/fan fault detected
945 uint2_t, // Chassis Identify State
946 bool, // Chassis Identify command and state info supported
947 bool, // reserved
948
949 bool, // Power off button disabled
950 bool, // Reset button disabled
951 bool, // Diagnostic Interrupt button disabled
952 bool, // Standby (sleep) button disabled
953 bool, // Power off button disable allowed
954 bool, // Reset button disable allowed
955 bool, // Diagnostic Interrupt button disable allowed
956 bool // Standby (sleep) button disable allowed
957 >
958 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +0800959{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500960 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700961 std::optional<uint2_t> restorePolicy =
962 power_policy::getPowerRestorePolicy();
963 std::optional<bool> powerGood = power_policy::getPowerStatus();
964 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500965 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700966 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500967 }
Nan Lifdd8ec52016-08-28 03:57:40 +0800968
969 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700970 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700971 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700972 // allow disable if the interface is present
973 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
974 // default return the button is enabled (not disabled)
975 bool powerButtonDisabled = false;
976 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700977 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700978 // return the real value of the button status, if present
979 powerButtonDisabled = *powerButtonReading;
980 }
981
982 std::optional<bool> resetButtonReading =
983 getButtonEnabled(resetButtonPath, resetButtonIntf);
984 // allow disable if the interface is present
985 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
986 // default return the button is enabled (not disabled)
987 bool resetButtonDisabled = false;
988 if (resetButtonDisableAllow)
989 {
990 // return the real value of the button status, if present
991 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700992 }
Nan Lifdd8ec52016-08-28 03:57:40 +0800993
Yong Li70ce7352019-05-16 21:15:27 +0800994 bool powerDownAcFailed = power_policy::getACFailStatus();
995
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700996 // This response has a lot of hard-coded, unsupported fields
997 // They are set to false or 0
998 constexpr bool powerOverload = false;
999 constexpr bool chassisInterlock = false;
1000 constexpr bool powerFault = false;
1001 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001002 constexpr bool powerDownOverload = false;
1003 constexpr bool powerDownInterlock = false;
1004 constexpr bool powerDownPowerFault = false;
1005 constexpr bool powerStatusIPMI = false;
1006 constexpr bool chassisIntrusionActive = false;
1007 constexpr bool frontPanelLockoutActive = false;
1008 constexpr bool driveFault = false;
1009 constexpr bool coolingFanFault = false;
1010 // chassisIdentifySupport set because this command is implemented
1011 constexpr bool chassisIdentifySupport = true;
1012 constexpr uint2_t chassisIdentifyState(0);
1013 constexpr bool diagButtonDisabled = false;
1014 constexpr bool sleepButtonDisabled = false;
1015 constexpr bool diagButtonDisableAllow = false;
1016 constexpr bool sleepButtonDisableAllow = false;
1017
1018 return ipmi::responseSuccess(
1019 *powerGood, powerOverload, chassisInterlock, powerFault,
1020 powerControlFault, *restorePolicy,
1021 false, // reserved
1022
1023 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1024 powerDownPowerFault, powerStatusIPMI,
1025 uint3_t(0), // reserved
1026
1027 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1028 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1029 false, // reserved
1030
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001031 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001032 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1033 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001034}
Chris Austen7888c4d2015-12-03 15:26:20 -06001035
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301036//-------------------------------------------------------------
1037// Send a command to SoftPowerOff application to stop any timer
1038//-------------------------------------------------------------
1039int stop_soft_off_timer()
1040{
Patrick Venture0b02be92018-08-31 11:55:55 -07001041 constexpr auto iface = "org.freedesktop.DBus.Properties";
1042 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1043 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301044
Patrick Venture0b02be92018-08-31 11:55:55 -07001045 constexpr auto property = "ResponseReceived";
1046 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1047 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301048
1049 // Get the system bus where most system services are provided.
1050 auto bus = ipmid_get_sd_bus_connection();
1051
1052 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001053 // TODO openbmc/openbmc#1661 - Mapper refactor
1054 //
1055 // See openbmc/openbmc#1743 for some details but high level summary is that
1056 // for now the code will directly call the soft off interface due to a
1057 // race condition with mapper usage
1058 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001059 // char *busname = nullptr;
1060 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1061 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001062 //{
1063 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301064 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001065 // return r;
1066 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301067
1068 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001069 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001070 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1071 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301072 if (rc < 0)
1073 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301074 log<level::ERR>("Failed to set property in SoftPowerOff object",
1075 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301076 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001077
Patrick Venture0b02be92018-08-31 11:55:55 -07001078 // TODO openbmc/openbmc#1661 - Mapper refactor
1079 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301080 return rc;
1081}
1082
vishwa36993272015-11-20 12:43:49 -06001083//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001084// Create file to indicate there is no need for softoff notification to host
1085//----------------------------------------------------------------------
1086void indicate_no_softoff_needed()
1087{
1088 fs::path path{HOST_INBAND_REQUEST_DIR};
1089 if (!fs::is_directory(path))
1090 {
1091 fs::create_directory(path);
1092 }
1093
1094 // Add the host instance (default 0 for now) to the file name
1095 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001096 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001097 size++; // null
1098 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001099 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001100
1101 // Append file name to directory and create it
1102 path /= buf.get();
1103 std::ofstream(path.c_str());
1104}
1105
anil kumar appanadafff5f2019-04-27 18:06:00 +00001106/** @brief Implementation of chassis control command
1107 *
1108 * @param - chassisControl command byte
1109 *
1110 * @return Success or InvalidFieldRequest.
1111 */
1112ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001113{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001114 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001115 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001116 {
1117 case CMD_POWER_ON:
1118 rc = initiate_state_transition(State::Host::Transition::On);
1119 break;
1120 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301121 // This path would be hit in 2 conditions.
1122 // 1: When user asks for power off using ipmi chassis command 0x04
1123 // 2: Host asking for power off post shutting down.
1124
1125 // If it's a host requested power off, then need to nudge Softoff
1126 // application that it needs to stop the watchdog timer if running.
1127 // If it is a user requested power off, then this is not really
1128 // needed. But then we need to differentiate between user and host
1129 // calling this same command
1130
1131 // For now, we are going ahead with trying to nudge the soft off and
1132 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001133 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301134
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001135 // Only request the Off transition if the soft power off
1136 // application is not running
1137 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001138 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001139 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301140 // that it should not run. Not doing this will result in State
1141 // manager doing a default soft power off when asked for power
1142 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001143 indicate_no_softoff_needed();
1144
1145 // Now request the shutdown
1146 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001147 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001148 else
1149 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301150 log<level::INFO>("Soft off is running, so let shutdown target "
1151 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001152 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001153 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301154
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001155 case CMD_HARD_RESET:
1156 case CMD_POWER_CYCLE:
1157 // SPEC has a section that says certain implementations can trigger
1158 // PowerOn if power is Off when a command to power cycle is
1159 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001160
1161 // First create a file to indicate to the soft off application
1162 // that it should not run since this is a direct user initiated
1163 // power reboot request (i.e. a reboot request that is not
1164 // originating via a soft power off SMS request)
1165 indicate_no_softoff_needed();
1166
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001167 rc = initiate_state_transition(State::Host::Transition::Reboot);
1168 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301169
1170 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1171 // Request Host State Manager to do a soft power off
1172 rc = initiate_state_transition(State::Host::Transition::Off);
1173 break;
1174
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001175 default:
1176 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301177 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001178 entry("CMD=0x%X", chassisControl));
1179 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001180 }
1181 }
vishwa36993272015-11-20 12:43:49 -06001182
anil kumar appanadafff5f2019-04-27 18:06:00 +00001183 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1184 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001185}
1186
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001187/** @brief Return D-Bus connection string to enclosure identify LED object
1188 *
1189 * @param[in, out] connection - connection to D-Bus object
1190 * @return a IPMI return code
1191 */
1192std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301193{
Tom Joseph5110c122018-03-23 17:55:40 +05301194 // lookup enclosure_identify group owner(s) in mapper
1195 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001196 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1197 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301198
1199 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001200 static const std::vector<std::string> interfaces = {
1201 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301202 mapperCall.append(interfaces);
1203 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1204 if (mapperReply.is_method_error())
1205 {
1206 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001207 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301208 }
1209 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1210 mapperReply.read(mapperResp);
1211
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001212 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301213 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001214 log<level::ERR>(
1215 "Invalid number of enclosure identify objects.",
1216 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001217 elog<InternalFailure>();
1218 }
1219 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1220 return pair.first;
1221}
Tom Joseph5110c122018-03-23 17:55:40 +05301222
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001223/** @brief Turn On/Off enclosure identify LED
1224 *
1225 * @param[in] flag - true to turn on LED, false to turn off
1226 * @return a IPMI return code
1227 */
1228void enclosureIdentifyLed(bool flag)
1229{
1230 using namespace chassis::internal;
1231 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001232 auto msg = std::string("enclosureIdentifyLed(") +
1233 boost::lexical_cast<std::string>(flag) + ")";
1234 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001235 auto led =
1236 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1237 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001238 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001239 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001240 auto ledReply = dbus.call(led);
1241 if (ledReply.is_method_error())
1242 {
1243 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001244 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001245 elog<InternalFailure>();
1246 }
1247}
1248
1249/** @brief Callback method to turn off LED
1250 */
1251void enclosureIdentifyLedOff()
1252{
1253 try
1254 {
1255 enclosureIdentifyLed(false);
1256 }
1257 catch (const InternalFailure& e)
1258 {
1259 report<InternalFailure>();
1260 }
1261}
1262
1263/** @brief Create timer to turn on and off the enclosure LED
1264 */
1265void createIdentifyTimer()
1266{
1267 if (!identifyTimer)
1268 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001269 identifyTimer =
1270 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001271 }
1272}
1273
Vernon Mauery400cc782018-10-09 13:49:53 -07001274ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1275 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001276{
Vernon Mauery400cc782018-10-09 13:49:53 -07001277 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1278 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301279
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001280 if (identifyInterval || forceIdentify)
1281 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001282 // stop the timer if already started;
1283 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001284 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001285 try
Tom Joseph5110c122018-03-23 17:55:40 +05301286 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001287 enclosureIdentifyLed(true);
1288 }
1289 catch (const InternalFailure& e)
1290 {
1291 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001292 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301293 }
1294
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001295 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301296 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001297 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001298 }
1299 // start the timer
1300 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001301 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001302 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301303 }
Tom Josephbed26992018-07-31 23:00:24 +05301304 else if (!identifyInterval)
1305 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001306 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301307 enclosureIdentifyLedOff();
1308 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001309 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301310}
1311
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001312namespace boot_options
1313{
1314
1315using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1316using IpmiValue = uint8_t;
1317constexpr auto ipmiDefault = 0;
1318
Patrick Venture0b02be92018-08-31 11:55:55 -07001319std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001320 {0x01, Source::Sources::Network},
1321 {0x02, Source::Sources::Disk},
1322 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001323 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001324 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001325
Patrick Venture0b02be92018-08-31 11:55:55 -07001326std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001327 {0x03, Mode::Modes::Safe},
1328 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001329 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001330
Patrick Venture0b02be92018-08-31 11:55:55 -07001331std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001332 {Source::Sources::Network, 0x01},
1333 {Source::Sources::Disk, 0x02},
1334 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001335 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001336 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001337
Patrick Venture0b02be92018-08-31 11:55:55 -07001338std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001339 {Mode::Modes::Safe, 0x03},
1340 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001341 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001342
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001343} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001344
Marri Devender Rao81719702018-05-07 00:53:48 -05001345/** @brief Set the property value for boot source
1346 * @param[in] source - boot source value
1347 * @return On failure return IPMI error.
1348 */
1349static ipmi_ret_t setBootSource(const Source::Sources& source)
1350{
1351 using namespace chassis::internal;
1352 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001353 std::variant<std::string> property = convertForMessage(source);
Marri Devender Rao81719702018-05-07 00:53:48 -05001354 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1355 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001356 auto method = dbus.new_method_call(
1357 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1358 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001359 method.append(bootSourceIntf, "BootSource", property);
1360 auto reply = dbus.call(method);
1361 if (reply.is_method_error())
1362 {
1363 log<level::ERR>("Error in BootSource Set");
1364 report<InternalFailure>();
1365 return IPMI_CC_UNSPECIFIED_ERROR;
1366 }
1367 return IPMI_CC_OK;
1368}
1369
Patrick Venture0b02be92018-08-31 11:55:55 -07001370/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001371 * @param[in] mode - boot mode value
1372 * @return On failure return IPMI error.
1373 */
1374static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1375{
1376 using namespace chassis::internal;
1377 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001378 std::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001379 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1380 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001381 auto method = dbus.new_method_call(
1382 objects.service(bootModeSetting, bootModeIntf).c_str(),
1383 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001384 method.append(bootModeIntf, "BootMode", property);
1385 auto reply = dbus.call(method);
1386 if (reply.is_method_error())
1387 {
1388 log<level::ERR>("Error in BootMode Set");
1389 report<InternalFailure>();
1390 return IPMI_CC_UNSPECIFIED_ERROR;
1391 }
1392 return IPMI_CC_OK;
1393}
1394
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001395ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1396 ipmi_request_t request,
1397 ipmi_response_t response,
1398 ipmi_data_len_t data_len,
1399 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001400{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001401 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001402 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001403 char* p = NULL;
1404 get_sys_boot_options_response_t* resp =
1405 (get_sys_boot_options_response_t*)response;
1406 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001407 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001408
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001409 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001410 resp->version = SET_PARM_VERSION;
1411 resp->parm = 5;
1412 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001413
shgoupfd84fbbf2015-12-17 10:05:51 +08001414 /*
1415 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1416 * This is the only parameter used by petitboot.
1417 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001418 if (reqptr->parameter ==
1419 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1420 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001421
Ratan Guptafd28dd72016-08-01 04:58:01 -05001422 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001423 using namespace chassis::internal;
1424 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001425
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001426 try
ratagupta6f6bff2016-04-04 06:20:11 -05001427 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001428 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1429 const auto& bootSourceSetting =
1430 std::get<settings::Path>(bootSetting);
1431 auto oneTimeEnabled =
1432 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001433 auto method = dbus.new_method_call(
1434 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1435 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001436 method.append(bootSourceIntf, "BootSource");
1437 auto reply = dbus.call(method);
1438 if (reply.is_method_error())
1439 {
1440 log<level::ERR>("Error in BootSource Get");
1441 report<InternalFailure>();
1442 *data_len = 0;
1443 return IPMI_CC_UNSPECIFIED_ERROR;
1444 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001445 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001446 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001447 auto bootSource =
1448 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001449
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001450 bootSetting = settings::boot::setting(objects, bootModeIntf);
1451 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1452 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001453 objects.service(bootModeSetting, bootModeIntf).c_str(),
1454 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001455 method.append(bootModeIntf, "BootMode");
1456 reply = dbus.call(method);
1457 if (reply.is_method_error())
1458 {
1459 log<level::ERR>("Error in BootMode Get");
1460 report<InternalFailure>();
1461 *data_len = 0;
1462 return IPMI_CC_UNSPECIFIED_ERROR;
1463 }
1464 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001465 auto bootMode =
1466 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001467
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001468 bootOption = sourceDbusToIpmi.at(bootSource);
1469 if ((Mode::Modes::Regular == bootMode) &&
1470 (Source::Sources::Default == bootSource))
1471 {
1472 bootOption = ipmiDefault;
1473 }
1474 else if (Source::Sources::Default == bootSource)
1475 {
1476 bootOption = modeDbusToIpmi.at(bootMode);
1477 }
1478 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001479
Patrick Venture0b02be92018-08-31 11:55:55 -07001480 resp->data[0] = oneTimeEnabled
1481 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1482 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001483
ratagupta6f6bff2016-04-04 06:20:11 -05001484 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001485 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001486 catch (InternalFailure& e)
1487 {
1488 report<InternalFailure>();
1489 *data_len = 0;
1490 return IPMI_CC_UNSPECIFIED_ERROR;
1491 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001492 }
1493 else if (reqptr->parameter ==
1494 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1495 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001496
Patrick Venture0b02be92018-08-31 11:55:55 -07001497 *data_len =
1498 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001499
Patrick Venture0b02be92018-08-31 11:55:55 -07001500 resp->parm =
1501 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001502
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001503 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001504
Patrick Venture0b02be92018-08-31 11:55:55 -07001505 if (ret < 0)
1506 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001507
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301508 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001509 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001510 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001511 }
1512 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001513 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001514 }
1515
Patrick Venture0b02be92018-08-31 11:55:55 -07001516 else
1517 {
1518 log<level::ERR>("Unsupported parameter",
1519 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001520 }
1521
1522 if (p)
1523 free(p);
1524
Ratan Guptafd28dd72016-08-01 04:58:01 -05001525 if (rc == IPMI_CC_OK)
1526 {
1527 *data_len += 2;
1528 }
1529
shgoupfd84fbbf2015-12-17 10:05:51 +08001530 return rc;
1531}
1532
shgoupfd84fbbf2015-12-17 10:05:51 +08001533ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001534 ipmi_request_t request,
1535 ipmi_response_t response,
1536 ipmi_data_len_t data_len,
1537 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001538{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001539 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001540 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001541 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001542
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001543 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1544 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001545
shgoupfd84fbbf2015-12-17 10:05:51 +08001546 // This IPMI command does not have any resposne data
1547 *data_len = 0;
1548
1549 /* 000101
1550 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1551 * This is the only parameter used by petitboot.
1552 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001553
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001554 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1555 {
1556 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1557 using namespace chassis::internal;
1558 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001559 auto oneTimeEnabled = false;
1560 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301561 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001562 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001563
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001564 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001565 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001566 bool permanent =
1567 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1568 SET_PARM_BOOT_FLAGS_PERMANENT;
1569
Patrick Venture0b02be92018-08-31 11:55:55 -07001570 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301571
1572 oneTimeEnabled =
1573 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1574
1575 /*
1576 * Check if the current boot setting is onetime or permanent, if the
1577 * request in the command is otherwise, then set the "Enabled"
1578 * property in one_time object path to 'True' to indicate onetime
1579 * and 'False' to indicate permanent.
1580 *
1581 * Once the onetime/permanent setting is applied, then the bootMode
1582 * and bootSource is updated for the corresponding object.
1583 */
1584 if ((permanent && oneTimeEnabled) ||
1585 (!permanent && !oneTimeEnabled))
1586 {
1587 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1588
Patrick Venture0b02be92018-08-31 11:55:55 -07001589 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1590 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301591 }
1592
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001593 auto modeItr = modeIpmiToDbus.find(bootOption);
1594 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1595 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001596 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001597 rc = setBootSource(sourceItr->second);
1598 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001599 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001600 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001601 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001602 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001603 // If a set boot device is mapping to a boot source, then reset
1604 // the boot mode D-Bus property to default.
1605 // This way the ipmid code can determine which property is not
1606 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001607 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001608 {
1609 setBootMode(Mode::Modes::Regular);
1610 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001611 }
1612 if (modeIpmiToDbus.end() != modeItr)
1613 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001614 rc = setBootMode(modeItr->second);
1615 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001616 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001617 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001618 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001619 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001620 // If a set boot device is mapping to a boot mode, then reset
1621 // the boot source D-Bus property to default.
1622 // This way the ipmid code can determine which property is not
1623 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001624 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001625 {
1626 setBootSource(Source::Sources::Default);
1627 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001628 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001629 if ((modeIpmiToDbus.end() == modeItr) &&
1630 (sourceIpmiToDbus.end() == sourceItr))
1631 {
1632 // return error if boot option is not supported
1633 *data_len = 0;
1634 return IPMI_CC_INVALID_FIELD_REQUEST;
1635 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001636 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001637 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001638 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001639 report<InternalFailure>();
1640 *data_len = 0;
1641 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001642 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001643 }
1644 else if (reqptr->parameter ==
1645 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1646 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001647
1648 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001649 if (ret < 0)
1650 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301651 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001652 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001653 rc = IPMI_CC_UNSPECIFIED_ERROR;
1654 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001655 }
1656 else if (reqptr->parameter ==
1657 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1658 {
Tom Josephf536c902017-09-25 18:08:15 +05301659 // Handle parameter #4 and return command completed normally
1660 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1661 // parameter. This is added to support the ipmitool command `chassis
1662 // bootdev` which sends set on parameter #4, before setting the boot
1663 // flags.
1664 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001665 }
1666 else
1667 {
1668 log<level::ERR>("Unsupported parameter",
1669 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001670 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001671 }
1672
1673 return rc;
1674}
1675
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001676/** @brief implements Get POH counter command
1677 * @parameter
1678 * - none
1679 * @returns IPMI completion code plus response data
1680 * - minPerCount - Minutes per count
1681 * - counterReading - counter reading
1682 */
1683ipmi::RspType<uint8_t, // Minutes per count
1684 uint32_t // Counter reading
1685 >
1686 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001687{
1688 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001689 try
1690 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001691 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1692 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001693 }
1694 catch (std::exception& e)
1695 {
1696 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001697 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001698 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001699}
1700
Vernon Mauerye278ead2018-10-09 09:23:43 -07001701ipmi::RspType<uint8_t>
1702 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
1703 uint8_t policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001704{
Vernon Mauerye278ead2018-10-09 09:23:43 -07001705 constexpr uint8_t ccParamNotSupported = 0x80;
Yong Lic6713cf2018-09-12 12:35:13 +08001706 power_policy::DbusValue value =
1707 power_policy::RestorePolicy::Policy::AlwaysOff;
1708
Vernon Mauerye278ead2018-10-09 09:23:43 -07001709 if (policy & ~power_policy::policyBitMask)
Yong Lic6713cf2018-09-12 12:35:13 +08001710 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001711 phosphor::logging::log<level::ERR>(
1712 "Reserved request parameter",
1713 entry("REQ=0x%x", static_cast<int>(policy)));
1714 return ipmi::response(ccParamNotSupported);
Yong Lic6713cf2018-09-12 12:35:13 +08001715 }
1716
Vernon Mauerye278ead2018-10-09 09:23:43 -07001717 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001718 {
1719 // just return the supported policy
Vernon Mauerye278ead2018-10-09 09:23:43 -07001720 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001721 }
1722
1723 for (auto const& it : power_policy::dbusToIpmi)
1724 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001725 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001726 {
1727 value = it.first;
1728 break;
1729 }
1730 }
1731
1732 try
1733 {
1734 const settings::Path& powerRestoreSetting =
1735 chassis::internal::cache::objects.map
1736 .at(chassis::internal::powerRestoreIntf)
1737 .front();
Vernon Mauery16b86932019-05-01 08:36:11 -07001738 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08001739
Vernon Mauerye278ead2018-10-09 09:23:43 -07001740 auto sdbusp = getSdBus();
1741 boost::system::error_code ec;
1742 sdbusp->yield_method_call<void>(
1743 yield, ec,
Yong Lic6713cf2018-09-12 12:35:13 +08001744 chassis::internal::cache::objects
1745 .service(powerRestoreSetting,
1746 chassis::internal::powerRestoreIntf)
1747 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07001748 powerRestoreSetting, ipmi::PROP_INTF, "Set",
1749 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1750 property);
1751 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08001752 {
1753 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07001754 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001755 }
1756 }
1757 catch (InternalFailure& e)
1758 {
1759 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07001760 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001761 }
1762
Vernon Mauerye278ead2018-10-09 09:23:43 -07001763 return ipmi::responseSuccess(power_policy::allSupport);
Yong Lic6713cf2018-09-12 12:35:13 +08001764}
1765
Adriana Kobylak40814c62015-10-27 15:58:44 -05001766void register_netfn_chassis_functions()
1767{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001768 createIdentifyTimer();
1769
Tom05732372016-09-06 17:21:23 +05301770 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001771 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1772 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001773
Tom05732372016-09-06 17:21:23 +05301774 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001775 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1776 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001777
Yong Liae4b0402018-11-02 11:12:14 +08001778 // Set Chassis Capabilities
1779 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1780 ipmi_set_chassis_cap, PRIVILEGE_USER);
1781
Tom05732372016-09-06 17:21:23 +05301782 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301783 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001784 ipmi_chassis_get_sys_boot_options,
1785 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001786
Tom05732372016-09-06 17:21:23 +05301787 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001788 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1789 ipmi::chassis::cmdGetChassisStatus,
1790 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08001791
Tom05732372016-09-06 17:21:23 +05301792 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001793 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1794 ipmi::chassis::cmdChassisControl,
1795 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001796
Tom Joseph5110c122018-03-23 17:55:40 +05301797 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001798 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1799 ipmi::chassis::cmdChassisIdentify,
1800 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301801
Tom05732372016-09-06 17:21:23 +05301802 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301803 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001804 ipmi_chassis_set_sys_boot_options,
1805 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001806 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001807 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1808 ipmi::chassis::cmdGetPohCounter,
1809 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001810
1811 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07001812 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1813 ipmi::chassis::cmdSetPowerRestorePolicy,
1814 ipmi::Privilege::Operator,
1815 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06001816}