blob: ef760d52c4ba0b18a3a3f7a5b98d762bf219a658 [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
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05005#include "settings.hpp"
Ratan Guptadcb10672017-07-10 10:33:50 +05306
Patrick Venture0b02be92018-08-31 11:55:55 -07007#include <arpa/inet.h>
8#include <endian.h>
9#include <limits.h>
10#include <mapper.h>
11#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070012
Ratan Guptafd28dd72016-08-01 04:58:01 -050013#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070014#include <chrono>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070015#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080016#include <filesystem>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050017#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053018#include <future>
Vernon Mauerye08fbff2019-04-03 09:19:34 -070019#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070020#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070021#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070022#include <map>
23#include <phosphor-logging/elog-errors.hpp>
24#include <phosphor-logging/log.hpp>
25#include <sdbusplus/bus.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070026#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070027#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070028#include <sdbusplus/timer.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070029#include <sstream>
Patrick Venture3a5071a2018-09-12 13:27:42 -070030#include <string>
31#include <xyz/openbmc_project/Common/error.hpp>
32#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
33#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
34#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
35#include <xyz/openbmc_project/State/Host/server.hpp>
36#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
37
Patrick Venture0b02be92018-08-31 11:55:55 -070038// Defines
39#define SET_PARM_VERSION 0x01
Vernon Mauery1181af72018-10-08 12:05:00 -070040#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40
41#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80
42#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0
ratagupta6f6bff2016-04-04 06:20:11 -050043
Lei YU4b0ddb62019-01-25 16:43:50 +080044std::unique_ptr<phosphor::Timer> identifyTimer
45 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050046
Patrick Venture0b02be92018-08-31 11:55:55 -070047constexpr size_t SIZE_MAC = 18;
48constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
49 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
50 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050051constexpr size_t SIZE_PREFIX = 7;
52constexpr size_t MAX_PREFIX_VALUE = 32;
53constexpr size_t SIZE_COOKIE = 4;
54constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053055constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053056
Patrick Venture0b02be92018-08-31 11:55:55 -070057// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053058static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070059 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050060
61static constexpr size_t COOKIE_OFFSET = 1;
62static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053063static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050064static constexpr size_t MAC_OFFSET = 9;
65static constexpr size_t ADDRTYPE_OFFSET = 16;
66static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050067
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050068static constexpr size_t encIdentifyObjectsSize = 1;
69static constexpr size_t chassisIdentifyReqLength = 2;
70static constexpr size_t identifyIntervalPos = 0;
71static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080072
Adriana Kobylak40814c62015-10-27 15:58:44 -050073void register_netfn_chassis_functions() __attribute__((constructor));
74
shgoupfd84fbbf2015-12-17 10:05:51 +080075// Host settings in dbus
76// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070077const char* settings_object_name = "/org/openbmc/settings/host0";
78const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070079const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053080 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080081
Ratan Guptadcb10672017-07-10 10:33:50 +053082constexpr auto SETTINGS_ROOT = "/";
83constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053084
85constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
86constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
87
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050088static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
89static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070090 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050091static constexpr auto pOHCounterProperty = "POHCounter";
92static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080093const static constexpr char chassisCapIntf[] =
94 "xyz.openbmc_project.Control.ChassisCapabilities";
95const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
96const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
97const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
98const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
99const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
100const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
101static constexpr uint8_t chassisCapFlagMask = 0x0f;
102static constexpr uint8_t chassisCapAddrMask = 0xfe;
Ratan Guptadcb10672017-07-10 10:33:50 +0530103
Nan Li8d15fb42016-08-16 22:29:40 +0800104typedef struct
105{
106 uint8_t cap_flags;
107 uint8_t fru_info_dev_addr;
108 uint8_t sdr_dev_addr;
109 uint8_t sel_dev_addr;
110 uint8_t system_management_dev_addr;
111 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700112} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800113
Nan Lifdd8ec52016-08-28 03:57:40 +0800114typedef struct
115{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500116 uint8_t cur_power_state;
117 uint8_t last_power_event;
118 uint8_t misc_power_state;
119 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700120} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800121
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500122/**
123 * @struct Get POH counter command response data
124 */
125struct GetPOHCountResponse
126{
Patrick Venture0b02be92018-08-31 11:55:55 -0700127 uint8_t minPerCount; ///< Minutes per count
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500128 uint8_t counterReading[4]; ///< Counter reading
Patrick Venture0b02be92018-08-31 11:55:55 -0700129} __attribute__((packed));
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500130
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530131// Phosphor Host State manager
132namespace State = sdbusplus::xyz::openbmc_project::State::server;
133
Vernon Mauery185b9f82018-07-20 10:52:36 -0700134namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500135
Ratan Guptadcb10672017-07-10 10:33:50 +0530136using namespace phosphor::logging;
137using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500138using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700139namespace variant_ns = sdbusplus::message::variant_ns;
140
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500141namespace chassis
142{
143namespace internal
144{
145
146constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
147constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500148constexpr auto powerRestoreIntf =
149 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500150sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
151
152namespace cache
153{
154
155settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500156 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500157
158} // namespace cache
159} // namespace internal
160} // namespace chassis
161
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500162namespace poh
163{
164
165constexpr auto minutesPerCount = 60;
166
167} // namespace poh
168
Patrick Venture0b02be92018-08-31 11:55:55 -0700169struct get_sys_boot_options_t
170{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500171 uint8_t parameter;
172 uint8_t set;
173 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700174} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500175
Patrick Venture0b02be92018-08-31 11:55:55 -0700176struct get_sys_boot_options_response_t
177{
shgoupfd84fbbf2015-12-17 10:05:51 +0800178 uint8_t version;
179 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500180 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700181} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800182
Patrick Venture0b02be92018-08-31 11:55:55 -0700183struct set_sys_boot_options_t
184{
shgoupfd84fbbf2015-12-17 10:05:51 +0800185 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500186 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700187} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500188
Ratan Guptadcb10672017-07-10 10:33:50 +0530189int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500190{
Ratan Guptadcb10672017-07-10 10:33:50 +0530191 ipmi::PropertyMap properties;
192 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530193 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500194
Ratan Guptadcb10672017-07-10 10:33:50 +0530195 try
196 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700197 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530198 // objects,to handle such cases we are interested on that object
199 // which are on interested busname.
200 // Currenlty mapper doesn't give the readable busname(gives busid)
201 // so we can't match with bus name so giving some object specific info
202 // as SETTINGS_MATCH.
203 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500204
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530205 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530206
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530207 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
208 SETTINGS_ROOT, SETTINGS_MATCH);
209
210 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
211 SETTINGS_ROOT, SETTINGS_MATCH);
212
Patrick Venture0b02be92018-08-31 11:55:55 -0700213 properties = ipmi::getAllDbusProperties(
214 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
215 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
216 macObjectInfo.first, MAC_INTERFACE,
217 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530218
William A. Kennington III4c008022018-10-12 17:18:14 -0700219 auto ipAddress = variant_ns::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530220
William A. Kennington III4c008022018-10-12 17:18:14 -0700221 auto gateway = variant_ns::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530222
William A. Kennington III4c008022018-10-12 17:18:14 -0700223 auto prefix = variant_ns::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530224
Patrick Venture0b02be92018-08-31 11:55:55 -0700225 uint8_t isStatic =
William A. Kennington III4c008022018-10-12 17:18:14 -0700226 (variant_ns::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700227 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
228 ? 1
229 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530230
William A. Kennington III4c008022018-10-12 17:18:14 -0700231 auto MACAddress = variant_ns::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530232
Ratan Guptad70f4532017-08-04 02:07:31 +0530233 // it is expected here that we should get the valid data
234 // but we may also get the default values.
235 // Validation of the data is done by settings.
236 //
237 // if mac address is default mac address then
238 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530239 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530240 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700241 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530242 rc = -1;
243 return rc;
244 }
245 // if addr is static then ipaddress,gateway,prefix
246 // should not be default one,don't send blank override.
247 if (isStatic)
248 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700249 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
250 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530251 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700252 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530253 rc = -1;
254 return rc;
255 }
256 }
257
Patrick Venture0b02be92018-08-31 11:55:55 -0700258 sscanf(
259 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
260 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
261 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
262 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530263
Ratan Guptadcb10672017-07-10 10:33:50 +0530264 respptr->data[MAC_OFFSET + 6] = 0x00;
265
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700266 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
267 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530268
William A. Kennington III4c008022018-10-12 17:18:14 -0700269 uint8_t addressFamily =
270 (variant_ns::get<std::string>(properties["Type"]) ==
271 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
272 ? AF_INET
273 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530274
Patrick Venture0b02be92018-08-31 11:55:55 -0700275 addrSize = (addressFamily == AF_INET)
276 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
277 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530278
279 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530280 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700281 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530282
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530283 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
284
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700285 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530286
287 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
288
Ratan Guptad70f4532017-08-04 02:07:31 +0530289 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700290 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530291 }
292 catch (InternalFailure& e)
293 {
294 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700295 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530296 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500297 return rc;
298 }
299
Patrick Venture0b02be92018-08-31 11:55:55 -0700300 // PetiBoot-Specific
301 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700302 std::memcpy(respptr->data, net_conf_initial_bytes,
303 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500304
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700305 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530306
Ratan Guptafd28dd72016-08-01 04:58:01 -0500307#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700308 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500309
Ratan Guptadcb10672017-07-10 10:33:50 +0530310 for (uint8_t pos = 0; pos < index; pos++)
311 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700312 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530313 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500314#endif
315
Ratan Guptafd28dd72016-08-01 04:58:01 -0500316 return rc;
317}
318
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530319/** @brief convert IPv4 and IPv6 addresses from binary to text form.
320 * @param[in] family - IPv4/Ipv6
321 * @param[in] data - req data pointer.
322 * @param[in] offset - offset in the data.
323 * @param[in] addrSize - size of the data which needs to be read from offset.
324 * @returns address in text form.
325 */
326
Patrick Venture0b02be92018-08-31 11:55:55 -0700327std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
328 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530329{
330 char ipAddr[INET6_ADDRSTRLEN] = {};
331
Patrick Venture0b02be92018-08-31 11:55:55 -0700332 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530333 {
334 case AF_INET:
335 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700336 struct sockaddr_in addr4
337 {
338 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700339 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530340
Patrick Venture0b02be92018-08-31 11:55:55 -0700341 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530342
343 break;
344 }
345 case AF_INET6:
346 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700347 struct sockaddr_in6 addr6
348 {
349 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700350 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530351
Patrick Venture0b02be92018-08-31 11:55:55 -0700352 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530353
354 break;
355 }
356 default:
357 {
358 return {};
359 }
360 }
361
362 return ipAddr;
363}
364
Ratan Guptadcb10672017-07-10 10:33:50 +0530365int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500366{
Ratan Guptadcb10672017-07-10 10:33:50 +0530367 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500368 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700369 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530370 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700371 char addrOrigin{0};
372 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530373 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530374 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700375 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
376 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530377 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530378 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500379
Patrick Venture0b02be92018-08-31 11:55:55 -0700380 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500381 // version starts from sixth byte
382
Ratan Guptadcb10672017-07-10 10:33:50 +0530383 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500384 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530385 do
386 {
387 // cookie == 0x21 0x70 0x62 0x21
388 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700389 (net_conf_initial_bytes + COOKIE_OFFSET),
390 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530391 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700392 // cookie == 0
393 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
394 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530395 {
396 // need to zero out the network settings.
397 break;
398 }
399
400 log<level::ERR>("Invalid Cookie");
401 elog<InternalFailure>();
402 }
403
404 // vesion == 0x00 0x01
405 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700406 (net_conf_initial_bytes + VERSION_OFFSET),
407 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530408 {
409
410 log<level::ERR>("Invalid Version");
411 elog<InternalFailure>();
412 }
413
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700414 std::snprintf(
415 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
416 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
417 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
418 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530419
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700420 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
421 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530422
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530423 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530424 {
425 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530426 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530427 }
428
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530429 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700430 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
431 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530432
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530433 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530434
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700435 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
436 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530437
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530438 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
439
Ratan Gupta8c31d232017-08-13 05:49:43 +0530440 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530441 {
442 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
443 family = AF_INET6;
444 }
445
Patrick Venture0b02be92018-08-31 11:55:55 -0700446 ipAddress =
447 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530448
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530449 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
450
Patrick Venture0b02be92018-08-31 11:55:55 -0700451 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530452
Patrick Venture0b02be92018-08-31 11:55:55 -0700453 // Cookie == 0 or it is a valid cookie
454 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
455 std::to_string(prefix) + ",gateway="s + gateway +
456 ",mac="s + mac + ",addressOrigin="s +
457 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500458
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530459 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
460
461 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
462 SETTINGS_ROOT, SETTINGS_MATCH);
463 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
464 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530465 // set the dbus property
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, "Address", std::string(ipAddress));
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, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530470 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700471 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530472 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700473 IP_INTERFACE, "Gateway", std::string(gateway));
474 ipmi::setDbusProperty(
475 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
476 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530477 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700478 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500479
Patrick Venture0b02be92018-08-31 11:55:55 -0700480 log<level::DEBUG>(
481 "Network configuration changed",
482 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500483 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530484 catch (InternalFailure& e)
485 {
486 commit<InternalFailure>();
487 return -1;
488 }
489
490 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500491}
492
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500493uint32_t getPOHCounter()
494{
495 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
496
Patrick Venture0b02be92018-08-31 11:55:55 -0700497 auto chassisStateObj =
498 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500499
Patrick Venture0b02be92018-08-31 11:55:55 -0700500 auto service =
501 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500502
Patrick Venture0b02be92018-08-31 11:55:55 -0700503 auto propValue =
504 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
505 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500506
William A. Kennington III4c008022018-10-12 17:18:14 -0700507 return variant_ns::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500508}
509
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500510ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
511 ipmi_request_t request,
512 ipmi_response_t response,
513 ipmi_data_len_t data_len,
514 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500515{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500516 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800517 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500518 *data_len = 0;
519 return rc;
520}
521
Nan Li8d15fb42016-08-16 22:29:40 +0800522ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700523 ipmi_request_t request,
524 ipmi_response_t response,
525 ipmi_data_len_t data_len,
526 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800527{
528 // sd_bus error
529 ipmi_ret_t rc = IPMI_CC_OK;
530
531 ipmi_chassis_cap_t chassis_cap{};
532
Yong Liae4b0402018-11-02 11:12:14 +0800533 if (*data_len != 0)
534 {
535 return IPMI_CC_REQ_DATA_LEN_INVALID;
536 }
537
Nan Li8d15fb42016-08-16 22:29:40 +0800538 *data_len = sizeof(ipmi_chassis_cap_t);
539
Yong Liae4b0402018-11-02 11:12:14 +0800540 try
541 {
542 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800543
Yong Liae4b0402018-11-02 11:12:14 +0800544 ipmi::DbusObjectInfo chassisCapObject =
545 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800546
Yong Liae4b0402018-11-02 11:12:14 +0800547 // capabilities flags
548 // [7..4] - reserved
549 // [3] – 1b = provides power interlock (IPM 1.5)
550 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
551 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
552 // has capabilities
553 // to lock out external power control and reset button or
554 // front panel interfaces and/or detect tampering with those
555 // interfaces).
556 // [0] -1b = Chassis provides intrusion (physical security) sensor.
557 // set to default value 0x0.
558 ipmi::Value variant = ipmi::getDbusProperty(
559 bus, chassisCapObject.second, chassisCapObject.first,
560 chassisCapIntf, chassisCapFlagsProp);
561 chassis_cap.cap_flags = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800562
Yong Liae4b0402018-11-02 11:12:14 +0800563 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
564 chassisCapObject.first, chassisCapIntf,
565 chassisFRUDevAddrProp);
566 // Chassis FRU info Device Address.
567 chassis_cap.fru_info_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800568
Yong Liae4b0402018-11-02 11:12:14 +0800569 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
570 chassisCapObject.first, chassisCapIntf,
571 chassisSDRDevAddrProp);
572 // Chassis SDR Device Address.
573 chassis_cap.sdr_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800574
Yong Liae4b0402018-11-02 11:12:14 +0800575 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
576 chassisCapObject.first, chassisCapIntf,
577 chassisSELDevAddrProp);
578 // Chassis SEL Device Address.
579 chassis_cap.sel_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800580
Yong Liae4b0402018-11-02 11:12:14 +0800581 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
582 chassisCapObject.first, chassisCapIntf,
583 chassisSMDevAddrProp);
584 // Chassis System Management Device Address.
585 chassis_cap.system_management_dev_addr =
586 variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800587
Yong Liae4b0402018-11-02 11:12:14 +0800588 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
589 chassisCapObject.first, chassisCapIntf,
590 chassisBridgeDevAddrProp);
591 // Chassis Bridge Device Address.
592 chassis_cap.bridge_dev_addr = variant_ns::get<uint8_t>(variant);
593 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
594 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
595 std::copy(chassisP, chassisP + *data_len, respP);
596 }
597 catch (std::exception& e)
598 {
599 log<level::ERR>(e.what());
600 rc = IPMI_CC_UNSPECIFIED_ERROR;
601 *data_len = 0;
602 return rc;
603 }
604
605 return rc;
606}
607
608ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
609 ipmi_request_t request,
610 ipmi_response_t response,
611 ipmi_data_len_t data_len,
612 ipmi_context_t context)
613{
614 ipmi_ret_t rc = IPMI_CC_OK;
615
616 if (*data_len != sizeof(ipmi_chassis_cap_t))
617 {
618 log<level::ERR>("Unsupported request length",
619 entry("LEN=0x%x", *data_len));
620 *data_len = 0;
621 return IPMI_CC_REQ_DATA_LEN_INVALID;
622 }
623
624 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
625
626 *data_len = 0;
627
628 // check input data
629 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
630 {
631 log<level::ERR>("Unsupported request parameter(CAP Flags)",
632 entry("REQ=0x%x", chassisCap->cap_flags));
633 return IPMI_CC_INVALID_FIELD_REQUEST;
634 }
635
636 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
637 {
638 log<level::ERR>("Unsupported request parameter(FRU Addr)",
639 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
640 return IPMI_CC_INVALID_FIELD_REQUEST;
641 }
642
643 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
644 {
645 log<level::ERR>("Unsupported request parameter(SDR Addr)",
646 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
647 return IPMI_CC_INVALID_FIELD_REQUEST;
648 }
649
650 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
651 {
652 log<level::ERR>("Unsupported request parameter(SEL Addr)",
653 entry("REQ=0x%x", chassisCap->sel_dev_addr));
654 return IPMI_CC_INVALID_FIELD_REQUEST;
655 }
656
657 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
658 {
659 log<level::ERR>(
660 "Unsupported request parameter(SM Addr)",
661 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
662 return IPMI_CC_INVALID_FIELD_REQUEST;
663 }
664
665 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
666 {
667 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
668 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
669 return IPMI_CC_INVALID_FIELD_REQUEST;
670 }
671
672 try
673 {
674 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
675 ipmi::DbusObjectInfo chassisCapObject =
676 ipmi::getDbusObject(bus, chassisCapIntf);
677
678 ipmi::setDbusProperty(bus, chassisCapObject.second,
679 chassisCapObject.first, chassisCapIntf,
680 chassisCapFlagsProp, chassisCap->cap_flags);
681
682 ipmi::setDbusProperty(bus, chassisCapObject.second,
683 chassisCapObject.first, chassisCapIntf,
684 chassisFRUDevAddrProp,
685 chassisCap->fru_info_dev_addr);
686
687 ipmi::setDbusProperty(bus, chassisCapObject.second,
688 chassisCapObject.first, chassisCapIntf,
689 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
690
691 ipmi::setDbusProperty(bus, chassisCapObject.second,
692 chassisCapObject.first, chassisCapIntf,
693 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
694
695 ipmi::setDbusProperty(bus, chassisCapObject.second,
696 chassisCapObject.first, chassisCapIntf,
697 chassisSMDevAddrProp,
698 chassisCap->system_management_dev_addr);
699
700 ipmi::setDbusProperty(bus, chassisCapObject.second,
701 chassisCapObject.first, chassisCapIntf,
702 chassisBridgeDevAddrProp,
703 chassisCap->bridge_dev_addr);
704 }
705 catch (std::exception& e)
706 {
707 log<level::ERR>(e.what());
708 rc = IPMI_CC_UNSPECIFIED_ERROR;
709 return rc;
710 }
Nan Li8d15fb42016-08-16 22:29:40 +0800711
712 return rc;
713}
714
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530715//------------------------------------------
716// Calls into Host State Manager Dbus object
717//------------------------------------------
718int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600719{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500720 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700721 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500722 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700723 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
724 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530725
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500726 // sd_bus error
727 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700728 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600729
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500730 // SD Bus error report mechanism.
731 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600732
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500733 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700734 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500735 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
736 if (rc < 0)
737 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700738 log<level::ERR>(
739 "Failed to get bus name",
740 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500741 return rc;
742 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530743
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500744 // Convert to string equivalent of the passed in transition enum.
745 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530746
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500747 rc = sd_bus_call_method(bus_type, // On the system bus
748 busname, // Service to contact
749 HOST_STATE_MANAGER_ROOT, // Object path
750 DBUS_PROPERTY_IFACE, // Interface name
751 "Set", // Method to be called
752 &bus_error, // object to return error
753 nullptr, // Response buffer if any
754 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700755 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
756 request.c_str());
757 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500758 {
759 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530760 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500761 }
762 else
763 {
764 log<level::INFO>("Transition request initiated successfully");
765 }
vishwa36993272015-11-20 12:43:49 -0600766
767 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500768 free(busname);
vishwa36993272015-11-20 12:43:49 -0600769
Sergey Solomineb9b8142016-08-23 09:07:28 -0500770 return rc;
vishwa36993272015-11-20 12:43:49 -0600771}
772
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500773namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500774{
Nan Lifdd8ec52016-08-28 03:57:40 +0800775
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500776using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
777using IpmiValue = uint8_t;
778using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800779
Patrick Venture0b02be92018-08-31 11:55:55 -0700780std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500781 {RestorePolicy::Policy::AlwaysOff, 0x00},
782 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700783 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800784
Yong Lic6713cf2018-09-12 12:35:13 +0800785static constexpr uint8_t noChange = 0x03;
786static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
787static constexpr uint8_t policyBitMask = 0x07;
788static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500789} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800790
791//----------------------------------------------------------------------
792// Get Chassis Status commands
793//----------------------------------------------------------------------
794ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500795 ipmi_request_t request,
796 ipmi_response_t response,
797 ipmi_data_len_t data_len,
798 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800799{
Patrick Venture0b02be92018-08-31 11:55:55 -0700800 const char* objname = "/org/openbmc/control/power0";
801 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800802
Patrick Venture0b02be92018-08-31 11:55:55 -0700803 sd_bus* bus = NULL;
804 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800805 int r = 0;
806 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700807 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800808 ipmi_ret_t rc = IPMI_CC_OK;
809 ipmi_get_chassis_status_t chassis_status{};
810
Nan Lifdd8ec52016-08-28 03:57:40 +0800811 uint8_t s = 0;
812
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500813 using namespace chassis::internal;
814 using namespace chassis::internal::cache;
815 using namespace power_policy;
816
Deepak Kodihallie6027092017-08-27 08:13:37 -0500817 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700818 auto method = dbus.new_method_call(
819 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
820 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500821 method.append(powerRestoreIntf, "PowerRestorePolicy");
822 auto resp = dbus.call(method);
823 if (resp.is_method_error())
824 {
825 log<level::ERR>("Error in PowerRestorePolicy Get");
826 report<InternalFailure>();
827 *data_len = 0;
828 return IPMI_CC_UNSPECIFIED_ERROR;
829 }
830 sdbusplus::message::variant<std::string> result;
831 resp.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -0700832 auto powerRestore = RestorePolicy::convertPolicyFromString(
833 variant_ns::get<std::string>(result));
Nan Lifdd8ec52016-08-28 03:57:40 +0800834
835 *data_len = 4;
836
Tom Joseph63a00512017-08-09 23:39:59 +0530837 bus = ipmid_get_sd_bus_connection();
838
Nan Lifdd8ec52016-08-28 03:57:40 +0800839 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700840 if (r < 0)
841 {
842 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800843 rc = IPMI_CC_UNSPECIFIED_ERROR;
844 goto finish;
845 }
846
Patrick Venture0b02be92018-08-31 11:55:55 -0700847 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
848 "i");
849 if (r < 0)
850 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530851 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700852 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
853 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530854 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800855 rc = IPMI_CC_UNSPECIFIED_ERROR;
856 goto finish;
857 }
858
859 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700860 if (r < 0)
861 {
862 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800863 rc = IPMI_CC_UNSPECIFIED_ERROR;
864 goto finish;
865 }
866
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500867 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800868
869 // Current Power State
870 // [7] reserved
871 // [6..5] power restore policy
872 // 00b = chassis stays powered off after AC/mains returns
873 // 01b = after AC returns, power is restored to the state that was
874 // in effect when AC/mains was lost.
875 // 10b = chassis always powers up after AC/mains returns
876 // 11b = unknow
877 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700878 // Do we need to define a dbus property to identify the restore
879 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800880
881 // [4] power control fault
882 // 1b = controller attempted to turn system power on or off, but
883 // system did not enter desired state.
884 // Set to 0b, since We don't support it..
885
886 // [3] power fault
887 // 1b = fault detected in main power subsystem.
888 // set to 0b. for we don't support it.
889
890 // [2] 1b = interlock (chassis is presently shut down because a chassis
891 // panel interlock switch is active). (IPMI 1.5)
892 // set to 0b, for we don't support it.
893
894 // [1] power overload
895 // 1b = system shutdown because of power overload condition.
896 // set to 0b, for we don't support it.
897
898 // [0] power is on
899 // 1b = system power is on
900 // 0b = system power is off(soft-off S4/S5, or mechanical off)
901
Patrick Venture0b02be92018-08-31 11:55:55 -0700902 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800903
904 // Last Power Event
905 // [7..5] – reserved
906 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
907 // [3] – 1b = last power down caused by power fault
908 // [2] – 1b = last power down caused by a power interlock being activated
909 // [1] – 1b = last power down caused by a Power overload
910 // [0] – 1b = AC failed
911 // set to 0x0, for we don't support these fields.
912
913 chassis_status.last_power_event = 0;
914
915 // Misc. Chassis State
916 // [7] – reserved
917 // [6] – 1b = Chassis Identify command and state info supported (Optional)
918 // 0b = Chassis Identify command support unspecified via this command.
919 // (The Get Command Support command , if implemented, would still
920 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700921 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
922 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800923 // as 00b) otherwise. Returns the present chassis identify state.
924 // Refer to the Chassis Identify command for more info.
925 // 00b = chassis identify state = Off
926 // 01b = chassis identify state = Temporary(timed) On
927 // 10b = chassis identify state = Indefinite On
928 // 11b = reserved
929 // [3] – 1b = Cooling/fan fault detected
930 // [2] – 1b = Drive Fault
931 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
932 // push-buttons disabled.)
933 // [0] – 1b = Chassis Intrusion active
934 // set to 0, for we don't support them.
935 chassis_status.misc_power_state = 0;
936
937 // Front Panel Button Capabilities and disable/enable status(Optional)
938 // set to 0, for we don't support them.
939 chassis_status.front_panel_button_cap_status = 0;
940
941 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700942 std::memcpy(response, &chassis_status, *data_len);
Nan Lifdd8ec52016-08-28 03:57:40 +0800943
944finish:
945 free(busname);
946 reply = sd_bus_message_unref(reply);
947
948 return rc;
949}
Chris Austen7888c4d2015-12-03 15:26:20 -0600950
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530951//-------------------------------------------------------------
952// Send a command to SoftPowerOff application to stop any timer
953//-------------------------------------------------------------
954int stop_soft_off_timer()
955{
Patrick Venture0b02be92018-08-31 11:55:55 -0700956 constexpr auto iface = "org.freedesktop.DBus.Properties";
957 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
958 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530959
Patrick Venture0b02be92018-08-31 11:55:55 -0700960 constexpr auto property = "ResponseReceived";
961 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
962 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530963
964 // Get the system bus where most system services are provided.
965 auto bus = ipmid_get_sd_bus_connection();
966
967 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500968 // TODO openbmc/openbmc#1661 - Mapper refactor
969 //
970 // See openbmc/openbmc#1743 for some details but high level summary is that
971 // for now the code will directly call the soft off interface due to a
972 // race condition with mapper usage
973 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700974 // char *busname = nullptr;
975 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
976 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500977 //{
978 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530979 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500980 // return r;
981 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530982
983 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500984 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700985 "Set", nullptr, nullptr, "ssv", soft_off_iface,
986 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530987 if (rc < 0)
988 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530989 log<level::ERR>("Failed to set property in SoftPowerOff object",
990 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530991 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500992
Patrick Venture0b02be92018-08-31 11:55:55 -0700993 // TODO openbmc/openbmc#1661 - Mapper refactor
994 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530995 return rc;
996}
997
vishwa36993272015-11-20 12:43:49 -0600998//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500999// Create file to indicate there is no need for softoff notification to host
1000//----------------------------------------------------------------------
1001void indicate_no_softoff_needed()
1002{
1003 fs::path path{HOST_INBAND_REQUEST_DIR};
1004 if (!fs::is_directory(path))
1005 {
1006 fs::create_directory(path);
1007 }
1008
1009 // Add the host instance (default 0 for now) to the file name
1010 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001011 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001012 size++; // null
1013 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001014 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001015
1016 // Append file name to directory and create it
1017 path /= buf.get();
1018 std::ofstream(path.c_str());
1019}
1020
1021//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001022// Chassis Control commands
1023//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001024ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1025 ipmi_request_t request,
1026 ipmi_response_t response,
1027 ipmi_data_len_t data_len,
1028 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001029{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001030 // Error from power off.
1031 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001032
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001033 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001034 *data_len = 0;
1035
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001036 // Catch the actual operaton by peeking into request buffer
Patrick Venture0b02be92018-08-31 11:55:55 -07001037 uint8_t chassis_ctrl_cmd = *(uint8_t*)request;
vishwa36993272015-11-20 12:43:49 -06001038
Patrick Venture0b02be92018-08-31 11:55:55 -07001039 switch (chassis_ctrl_cmd)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001040 {
1041 case CMD_POWER_ON:
1042 rc = initiate_state_transition(State::Host::Transition::On);
1043 break;
1044 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301045 // This path would be hit in 2 conditions.
1046 // 1: When user asks for power off using ipmi chassis command 0x04
1047 // 2: Host asking for power off post shutting down.
1048
1049 // If it's a host requested power off, then need to nudge Softoff
1050 // application that it needs to stop the watchdog timer if running.
1051 // If it is a user requested power off, then this is not really
1052 // needed. But then we need to differentiate between user and host
1053 // calling this same command
1054
1055 // For now, we are going ahead with trying to nudge the soft off and
1056 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001057 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301058
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001059 // Only request the Off transition if the soft power off
1060 // application is not running
1061 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001062 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001063 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301064 // that it should not run. Not doing this will result in State
1065 // manager doing a default soft power off when asked for power
1066 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001067 indicate_no_softoff_needed();
1068
1069 // Now request the shutdown
1070 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001071 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001072 else
1073 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301074 log<level::INFO>("Soft off is running, so let shutdown target "
1075 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001076 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001077 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301078
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001079 case CMD_HARD_RESET:
1080 case CMD_POWER_CYCLE:
1081 // SPEC has a section that says certain implementations can trigger
1082 // PowerOn if power is Off when a command to power cycle is
1083 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001084
1085 // First create a file to indicate to the soft off application
1086 // that it should not run since this is a direct user initiated
1087 // power reboot request (i.e. a reboot request that is not
1088 // originating via a soft power off SMS request)
1089 indicate_no_softoff_needed();
1090
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001091 rc = initiate_state_transition(State::Host::Transition::Reboot);
1092 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301093
1094 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1095 // Request Host State Manager to do a soft power off
1096 rc = initiate_state_transition(State::Host::Transition::Off);
1097 break;
1098
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001099 default:
1100 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301101 log<level::ERR>("Invalid Chassis Control command",
1102 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001103 rc = -1;
1104 }
1105 }
vishwa36993272015-11-20 12:43:49 -06001106
Patrick Venture0b02be92018-08-31 11:55:55 -07001107 return ((rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001108}
1109
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001110/** @brief Return D-Bus connection string to enclosure identify LED object
1111 *
1112 * @param[in, out] connection - connection to D-Bus object
1113 * @return a IPMI return code
1114 */
1115std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301116{
Tom Joseph5110c122018-03-23 17:55:40 +05301117 // lookup enclosure_identify group owner(s) in mapper
1118 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001119 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1120 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301121
1122 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001123 static const std::vector<std::string> interfaces = {
1124 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301125 mapperCall.append(interfaces);
1126 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1127 if (mapperReply.is_method_error())
1128 {
1129 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001130 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301131 }
1132 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1133 mapperReply.read(mapperResp);
1134
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001135 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301136 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001137 log<level::ERR>(
1138 "Invalid number of enclosure identify objects.",
1139 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001140 elog<InternalFailure>();
1141 }
1142 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1143 return pair.first;
1144}
Tom Joseph5110c122018-03-23 17:55:40 +05301145
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001146/** @brief Turn On/Off enclosure identify LED
1147 *
1148 * @param[in] flag - true to turn on LED, false to turn off
1149 * @return a IPMI return code
1150 */
1151void enclosureIdentifyLed(bool flag)
1152{
1153 using namespace chassis::internal;
1154 std::string connection = std::move(getEnclosureIdentifyConnection());
Patrick Venture0b02be92018-08-31 11:55:55 -07001155 auto led =
1156 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1157 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001158 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001159 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001160 auto ledReply = dbus.call(led);
1161 if (ledReply.is_method_error())
1162 {
1163 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001164 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001165 elog<InternalFailure>();
1166 }
1167}
1168
1169/** @brief Callback method to turn off LED
1170 */
1171void enclosureIdentifyLedOff()
1172{
1173 try
1174 {
1175 enclosureIdentifyLed(false);
1176 }
1177 catch (const InternalFailure& e)
1178 {
1179 report<InternalFailure>();
1180 }
1181}
1182
1183/** @brief Create timer to turn on and off the enclosure LED
1184 */
1185void createIdentifyTimer()
1186{
1187 if (!identifyTimer)
1188 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001189 identifyTimer =
1190 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001191 }
1192}
1193
1194ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1195 ipmi_request_t request,
1196 ipmi_response_t response,
1197 ipmi_data_len_t data_len,
1198 ipmi_context_t context)
1199{
1200 if (*data_len > chassisIdentifyReqLength)
1201 {
1202 return IPMI_CC_REQ_DATA_LEN_INVALID;
1203 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001204 uint8_t identifyInterval =
1205 *data_len > identifyIntervalPos
1206 ? (static_cast<uint8_t*>(request))[identifyIntervalPos]
1207 : DEFAULT_IDENTIFY_TIME_OUT;
1208 bool forceIdentify =
1209 (*data_len == chassisIdentifyReqLength)
1210 ? (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01
1211 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301212
Jia, Chunhui7cf145f2019-02-28 14:14:27 +08001213 *data_len = 0; // response have complete code only
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001214 if (identifyInterval || forceIdentify)
1215 {
1216 // stop the timer if already started, for force identify we should
1217 // not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001218 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001219 try
Tom Joseph5110c122018-03-23 17:55:40 +05301220 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001221 enclosureIdentifyLed(true);
1222 }
1223 catch (const InternalFailure& e)
1224 {
1225 report<InternalFailure>();
1226 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301227 }
1228
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001229 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301230 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001231 return IPMI_CC_OK;
1232 }
1233 // start the timer
1234 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001235 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001236 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301237 }
Tom Josephbed26992018-07-31 23:00:24 +05301238 else if (!identifyInterval)
1239 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001240 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301241 enclosureIdentifyLedOff();
1242 }
Tom Joseph5110c122018-03-23 17:55:40 +05301243 return IPMI_CC_OK;
1244}
1245
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001246namespace boot_options
1247{
1248
1249using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1250using IpmiValue = uint8_t;
1251constexpr auto ipmiDefault = 0;
1252
Patrick Venture0b02be92018-08-31 11:55:55 -07001253std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001254 {0x01, Source::Sources::Network},
1255 {0x02, Source::Sources::Disk},
1256 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001257 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001258
Patrick Venture0b02be92018-08-31 11:55:55 -07001259std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001260 {0x03, Mode::Modes::Safe},
1261 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001262 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001263
Patrick Venture0b02be92018-08-31 11:55:55 -07001264std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001265 {Source::Sources::Network, 0x01},
1266 {Source::Sources::Disk, 0x02},
1267 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001268 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001269
Patrick Venture0b02be92018-08-31 11:55:55 -07001270std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001271 {Mode::Modes::Safe, 0x03},
1272 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001273 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001274
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001275} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001276
Marri Devender Rao81719702018-05-07 00:53:48 -05001277/** @brief Set the property value for boot source
1278 * @param[in] source - boot source value
1279 * @return On failure return IPMI error.
1280 */
1281static ipmi_ret_t setBootSource(const Source::Sources& source)
1282{
1283 using namespace chassis::internal;
1284 using namespace chassis::internal::cache;
1285 sdbusplus::message::variant<std::string> property =
1286 convertForMessage(source);
1287 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1288 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001289 auto method = dbus.new_method_call(
1290 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1291 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001292 method.append(bootSourceIntf, "BootSource", property);
1293 auto reply = dbus.call(method);
1294 if (reply.is_method_error())
1295 {
1296 log<level::ERR>("Error in BootSource Set");
1297 report<InternalFailure>();
1298 return IPMI_CC_UNSPECIFIED_ERROR;
1299 }
1300 return IPMI_CC_OK;
1301}
1302
Patrick Venture0b02be92018-08-31 11:55:55 -07001303/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001304 * @param[in] mode - boot mode value
1305 * @return On failure return IPMI error.
1306 */
1307static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1308{
1309 using namespace chassis::internal;
1310 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001311 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001312 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1313 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001314 auto method = dbus.new_method_call(
1315 objects.service(bootModeSetting, bootModeIntf).c_str(),
1316 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001317 method.append(bootModeIntf, "BootMode", property);
1318 auto reply = dbus.call(method);
1319 if (reply.is_method_error())
1320 {
1321 log<level::ERR>("Error in BootMode Set");
1322 report<InternalFailure>();
1323 return IPMI_CC_UNSPECIFIED_ERROR;
1324 }
1325 return IPMI_CC_OK;
1326}
1327
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001328ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1329 ipmi_request_t request,
1330 ipmi_response_t response,
1331 ipmi_data_len_t data_len,
1332 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001333{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001334 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001335 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001336 char* p = NULL;
1337 get_sys_boot_options_response_t* resp =
1338 (get_sys_boot_options_response_t*)response;
1339 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001340 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001341
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001342 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001343 resp->version = SET_PARM_VERSION;
1344 resp->parm = 5;
1345 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001346
shgoupfd84fbbf2015-12-17 10:05:51 +08001347 /*
1348 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1349 * This is the only parameter used by petitboot.
1350 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001351 if (reqptr->parameter ==
1352 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1353 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001354
Ratan Guptafd28dd72016-08-01 04:58:01 -05001355 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001356 using namespace chassis::internal;
1357 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001358
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001359 try
ratagupta6f6bff2016-04-04 06:20:11 -05001360 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001361 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1362 const auto& bootSourceSetting =
1363 std::get<settings::Path>(bootSetting);
1364 auto oneTimeEnabled =
1365 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001366 auto method = dbus.new_method_call(
1367 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1368 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001369 method.append(bootSourceIntf, "BootSource");
1370 auto reply = dbus.call(method);
1371 if (reply.is_method_error())
1372 {
1373 log<level::ERR>("Error in BootSource Get");
1374 report<InternalFailure>();
1375 *data_len = 0;
1376 return IPMI_CC_UNSPECIFIED_ERROR;
1377 }
1378 sdbusplus::message::variant<std::string> result;
1379 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001380 auto bootSource = Source::convertSourcesFromString(
1381 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001382
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001383 bootSetting = settings::boot::setting(objects, bootModeIntf);
1384 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1385 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001386 objects.service(bootModeSetting, bootModeIntf).c_str(),
1387 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001388 method.append(bootModeIntf, "BootMode");
1389 reply = dbus.call(method);
1390 if (reply.is_method_error())
1391 {
1392 log<level::ERR>("Error in BootMode Get");
1393 report<InternalFailure>();
1394 *data_len = 0;
1395 return IPMI_CC_UNSPECIFIED_ERROR;
1396 }
1397 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001398 auto bootMode = Mode::convertModesFromString(
1399 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001400
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001401 bootOption = sourceDbusToIpmi.at(bootSource);
1402 if ((Mode::Modes::Regular == bootMode) &&
1403 (Source::Sources::Default == bootSource))
1404 {
1405 bootOption = ipmiDefault;
1406 }
1407 else if (Source::Sources::Default == bootSource)
1408 {
1409 bootOption = modeDbusToIpmi.at(bootMode);
1410 }
1411 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001412
Patrick Venture0b02be92018-08-31 11:55:55 -07001413 resp->data[0] = oneTimeEnabled
1414 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1415 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001416
ratagupta6f6bff2016-04-04 06:20:11 -05001417 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001418 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001419 catch (InternalFailure& e)
1420 {
1421 report<InternalFailure>();
1422 *data_len = 0;
1423 return IPMI_CC_UNSPECIFIED_ERROR;
1424 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001425 }
1426 else if (reqptr->parameter ==
1427 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1428 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001429
Patrick Venture0b02be92018-08-31 11:55:55 -07001430 *data_len =
1431 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001432
Patrick Venture0b02be92018-08-31 11:55:55 -07001433 resp->parm =
1434 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001435
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001436 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001437
Patrick Venture0b02be92018-08-31 11:55:55 -07001438 if (ret < 0)
1439 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001440
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301441 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001442 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001443 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001444 }
1445 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001446 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001447 }
1448
Patrick Venture0b02be92018-08-31 11:55:55 -07001449 else
1450 {
1451 log<level::ERR>("Unsupported parameter",
1452 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001453 }
1454
1455 if (p)
1456 free(p);
1457
Ratan Guptafd28dd72016-08-01 04:58:01 -05001458 if (rc == IPMI_CC_OK)
1459 {
1460 *data_len += 2;
1461 }
1462
shgoupfd84fbbf2015-12-17 10:05:51 +08001463 return rc;
1464}
1465
shgoupfd84fbbf2015-12-17 10:05:51 +08001466ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001467 ipmi_request_t request,
1468 ipmi_response_t response,
1469 ipmi_data_len_t data_len,
1470 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001471{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001472 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001473 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001474 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001475
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001476 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1477 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001478
shgoupfd84fbbf2015-12-17 10:05:51 +08001479 // This IPMI command does not have any resposne data
1480 *data_len = 0;
1481
1482 /* 000101
1483 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1484 * This is the only parameter used by petitboot.
1485 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001486
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001487 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1488 {
1489 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1490 using namespace chassis::internal;
1491 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001492 auto oneTimeEnabled = false;
1493 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301494 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001495 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001496
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001497 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001498 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001499 bool permanent =
1500 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1501 SET_PARM_BOOT_FLAGS_PERMANENT;
1502
Patrick Venture0b02be92018-08-31 11:55:55 -07001503 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301504
1505 oneTimeEnabled =
1506 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1507
1508 /*
1509 * Check if the current boot setting is onetime or permanent, if the
1510 * request in the command is otherwise, then set the "Enabled"
1511 * property in one_time object path to 'True' to indicate onetime
1512 * and 'False' to indicate permanent.
1513 *
1514 * Once the onetime/permanent setting is applied, then the bootMode
1515 * and bootSource is updated for the corresponding object.
1516 */
1517 if ((permanent && oneTimeEnabled) ||
1518 (!permanent && !oneTimeEnabled))
1519 {
1520 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1521
Patrick Venture0b02be92018-08-31 11:55:55 -07001522 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1523 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301524 }
1525
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001526 auto modeItr = modeIpmiToDbus.find(bootOption);
1527 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1528 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001529 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001530 rc = setBootSource(sourceItr->second);
1531 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001532 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001533 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001534 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001535 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001536 // If a set boot device is mapping to a boot source, then reset
1537 // the boot mode D-Bus property to default.
1538 // This way the ipmid code can determine which property is not
1539 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001540 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001541 {
1542 setBootMode(Mode::Modes::Regular);
1543 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001544 }
1545 if (modeIpmiToDbus.end() != modeItr)
1546 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001547 rc = setBootMode(modeItr->second);
1548 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001549 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001550 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001551 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001552 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001553 // If a set boot device is mapping to a boot mode, then reset
1554 // the boot source D-Bus property to default.
1555 // This way the ipmid code can determine which property is not
1556 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001557 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001558 {
1559 setBootSource(Source::Sources::Default);
1560 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001561 }
1562 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001563 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001564 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001565 report<InternalFailure>();
1566 *data_len = 0;
1567 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001568 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001569 }
1570 else if (reqptr->parameter ==
1571 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1572 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001573
1574 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001575 if (ret < 0)
1576 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301577 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001578 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001579 rc = IPMI_CC_UNSPECIFIED_ERROR;
1580 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001581 }
1582 else if (reqptr->parameter ==
1583 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1584 {
Tom Josephf536c902017-09-25 18:08:15 +05301585 // Handle parameter #4 and return command completed normally
1586 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1587 // parameter. This is added to support the ipmitool command `chassis
1588 // bootdev` which sends set on parameter #4, before setting the boot
1589 // flags.
1590 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001591 }
1592 else
1593 {
1594 log<level::ERR>("Unsupported parameter",
1595 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001596 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001597 }
1598
1599 return rc;
1600}
1601
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001602ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1603 ipmi_request_t request, ipmi_response_t response,
1604 ipmi_data_len_t data_len, ipmi_context_t context)
1605{
1606 // sd_bus error
1607 ipmi_ret_t rc = IPMI_CC_OK;
1608
1609 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1610
1611 try
1612 {
1613 auto pohCounter = getPOHCounter();
1614 resptr->counterReading[0] = pohCounter;
1615 resptr->counterReading[1] = pohCounter >> 8;
1616 resptr->counterReading[2] = pohCounter >> 16;
1617 resptr->counterReading[3] = pohCounter >> 24;
1618 }
1619 catch (std::exception& e)
1620 {
1621 log<level::ERR>(e.what());
1622 return IPMI_CC_UNSPECIFIED_ERROR;
1623 }
1624
1625 resptr->minPerCount = poh::minutesPerCount;
1626 *data_len = sizeof(GetPOHCountResponse);
1627
1628 return rc;
1629}
1630
Yong Lic6713cf2018-09-12 12:35:13 +08001631ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1632 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1633 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1634{
1635 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1636 auto* resptr = reinterpret_cast<uint8_t*>(response);
1637 uint8_t reqPolicy = 0;
1638
1639 power_policy::DbusValue value =
1640 power_policy::RestorePolicy::Policy::AlwaysOff;
1641
1642 if (*data_len != power_policy::setPolicyReqLen)
1643 {
1644 phosphor::logging::log<level::ERR>("Unsupported request length",
1645 entry("LEN=0x%x", *data_len));
1646 *data_len = 0;
1647 return IPMI_CC_REQ_DATA_LEN_INVALID;
1648 }
1649
Yong Licb89c0e2019-01-18 17:54:32 +08001650 if (*reqptr > power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001651 {
1652 phosphor::logging::log<level::ERR>("Reserved request parameter",
Yong Licb89c0e2019-01-18 17:54:32 +08001653 entry("REQ=0x%x", *reqptr));
Yong Lic6713cf2018-09-12 12:35:13 +08001654 *data_len = 0;
Yong Licb89c0e2019-01-18 17:54:32 +08001655 return IPMI_CC_PARM_OUT_OF_RANGE;
Yong Lic6713cf2018-09-12 12:35:13 +08001656 }
1657
Yong Licb89c0e2019-01-18 17:54:32 +08001658 reqPolicy = *reqptr & power_policy::policyBitMask;
Yong Lic6713cf2018-09-12 12:35:13 +08001659 if (reqPolicy == power_policy::noChange)
1660 {
1661 // just return the supported policy
1662 *resptr = power_policy::allSupport;
1663 *data_len = power_policy::setPolicyReqLen;
1664 return IPMI_CC_OK;
1665 }
1666
1667 for (auto const& it : power_policy::dbusToIpmi)
1668 {
1669 if (it.second == reqPolicy)
1670 {
1671 value = it.first;
1672 break;
1673 }
1674 }
1675
1676 try
1677 {
1678 const settings::Path& powerRestoreSetting =
1679 chassis::internal::cache::objects.map
1680 .at(chassis::internal::powerRestoreIntf)
1681 .front();
1682 sdbusplus::message::variant<std::string> property =
1683 convertForMessage(value);
1684
1685 auto method = chassis::internal::dbus.new_method_call(
1686 chassis::internal::cache::objects
1687 .service(powerRestoreSetting,
1688 chassis::internal::powerRestoreIntf)
1689 .c_str(),
1690 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1691
1692 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1693 property);
1694 auto reply = chassis::internal::dbus.call(method);
1695 if (reply.is_method_error())
1696 {
1697 phosphor::logging::log<level::ERR>("Unspecified Error");
1698 *data_len = 0;
1699 return IPMI_CC_UNSPECIFIED_ERROR;
1700 }
1701 }
1702 catch (InternalFailure& e)
1703 {
1704 report<InternalFailure>();
1705 *data_len = 0;
1706 return IPMI_CC_UNSPECIFIED_ERROR;
1707 }
1708
Yong Licb89c0e2019-01-18 17:54:32 +08001709 *resptr = power_policy::allSupport;
Yong Lic6713cf2018-09-12 12:35:13 +08001710 *data_len = power_policy::setPolicyReqLen;
1711 return IPMI_CC_OK;
1712}
1713
Adriana Kobylak40814c62015-10-27 15:58:44 -05001714void register_netfn_chassis_functions()
1715{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001716 createIdentifyTimer();
1717
Tom05732372016-09-06 17:21:23 +05301718 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001719 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1720 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001721
Tom05732372016-09-06 17:21:23 +05301722 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001723 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1724 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001725
Yong Liae4b0402018-11-02 11:12:14 +08001726 // Set Chassis Capabilities
1727 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1728 ipmi_set_chassis_cap, PRIVILEGE_USER);
1729
Tom05732372016-09-06 17:21:23 +05301730 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301731 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001732 ipmi_chassis_get_sys_boot_options,
1733 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001734
Tom05732372016-09-06 17:21:23 +05301735 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001736 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1737 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001738
Tom05732372016-09-06 17:21:23 +05301739 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001740 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1741 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001742
Tom Joseph5110c122018-03-23 17:55:40 +05301743 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301744 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1745 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1746
Tom05732372016-09-06 17:21:23 +05301747 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301748 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001749 ipmi_chassis_set_sys_boot_options,
1750 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001751 // <Get POH Counter>
1752 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1753 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001754
1755 // <Set Power Restore Policy>
1756 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1757 ipmi_chassis_set_power_restore_policy,
1758 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001759}