blob: 07e8c5ba7dc13260eb3c3e9dcf3a5fa45d560134 [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 "ipmid.hpp"
6#include "settings.hpp"
Ratan Guptadcb10672017-07-10 10:33:50 +05307
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include <arpa/inet.h>
9#include <endian.h>
William A. Kennington III194375f2018-12-14 02:14:33 -080010#include <ipmid/api.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070011#include <limits.h>
12#include <mapper.h>
13#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070014
Ratan Guptafd28dd72016-08-01 04:58:01 -050015#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070016#include <chrono>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070017#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080018#include <filesystem>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050019#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053020#include <future>
Vernon Mauery33250242019-03-12 16:49:26 -070021#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070022#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070023#include <map>
24#include <phosphor-logging/elog-errors.hpp>
25#include <phosphor-logging/log.hpp>
26#include <sdbusplus/bus.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070027#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070028#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070029#include <sdbusplus/timer.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070030#include <sstream>
Patrick Venture3a5071a2018-09-12 13:27:42 -070031#include <string>
32#include <xyz/openbmc_project/Common/error.hpp>
33#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
34#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
35#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
36#include <xyz/openbmc_project/State/Host/server.hpp>
37#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
38
Patrick Venture0b02be92018-08-31 11:55:55 -070039// Defines
40#define SET_PARM_VERSION 0x01
Vernon Mauery1181af72018-10-08 12:05:00 -070041#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40
42#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80
43#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0
ratagupta6f6bff2016-04-04 06:20:11 -050044
Lei YU4b0ddb62019-01-25 16:43:50 +080045std::unique_ptr<phosphor::Timer> identifyTimer
46 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050047
Patrick Venture0b02be92018-08-31 11:55:55 -070048constexpr size_t SIZE_MAC = 18;
49constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
50 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
51 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050052constexpr size_t SIZE_PREFIX = 7;
53constexpr size_t MAX_PREFIX_VALUE = 32;
54constexpr size_t SIZE_COOKIE = 4;
55constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053056constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053057
Patrick Venture0b02be92018-08-31 11:55:55 -070058// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053059static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070060 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050061
62static constexpr size_t COOKIE_OFFSET = 1;
63static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053064static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050065static constexpr size_t MAC_OFFSET = 9;
66static constexpr size_t ADDRTYPE_OFFSET = 16;
67static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050068
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050069static constexpr size_t encIdentifyObjectsSize = 1;
70static constexpr size_t chassisIdentifyReqLength = 2;
71static constexpr size_t identifyIntervalPos = 0;
72static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080073
Adriana Kobylak40814c62015-10-27 15:58:44 -050074void register_netfn_chassis_functions() __attribute__((constructor));
75
shgoupfd84fbbf2015-12-17 10:05:51 +080076// Host settings in dbus
77// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070078const char* settings_object_name = "/org/openbmc/settings/host0";
79const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070080const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053081 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080082
Ratan Guptadcb10672017-07-10 10:33:50 +053083constexpr auto SETTINGS_ROOT = "/";
84constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053085
86constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
87constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
88
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050089static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
90static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070091 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050092static constexpr auto pOHCounterProperty = "POHCounter";
93static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080094const static constexpr char chassisCapIntf[] =
95 "xyz.openbmc_project.Control.ChassisCapabilities";
96const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
97const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
98const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
99const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
100const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
101const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
102static constexpr uint8_t chassisCapFlagMask = 0x0f;
103static constexpr uint8_t chassisCapAddrMask = 0xfe;
Ratan Guptadcb10672017-07-10 10:33:50 +0530104
Nan Li8d15fb42016-08-16 22:29:40 +0800105typedef struct
106{
107 uint8_t cap_flags;
108 uint8_t fru_info_dev_addr;
109 uint8_t sdr_dev_addr;
110 uint8_t sel_dev_addr;
111 uint8_t system_management_dev_addr;
112 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700113} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800114
Nan Lifdd8ec52016-08-28 03:57:40 +0800115typedef struct
116{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500117 uint8_t cur_power_state;
118 uint8_t last_power_event;
119 uint8_t misc_power_state;
120 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700121} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800122
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500123/**
124 * @struct Get POH counter command response data
125 */
126struct GetPOHCountResponse
127{
Patrick Venture0b02be92018-08-31 11:55:55 -0700128 uint8_t minPerCount; ///< Minutes per count
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500129 uint8_t counterReading[4]; ///< Counter reading
Patrick Venture0b02be92018-08-31 11:55:55 -0700130} __attribute__((packed));
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500131
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530132// Phosphor Host State manager
133namespace State = sdbusplus::xyz::openbmc_project::State::server;
134
Vernon Mauery185b9f82018-07-20 10:52:36 -0700135namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500136
Ratan Guptadcb10672017-07-10 10:33:50 +0530137using namespace phosphor::logging;
138using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500139using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700140namespace variant_ns = sdbusplus::message::variant_ns;
141
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500142namespace chassis
143{
144namespace internal
145{
146
147constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
148constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500149constexpr auto powerRestoreIntf =
150 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500151sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
152
153namespace cache
154{
155
156settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500157 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500158
159} // namespace cache
160} // namespace internal
161} // namespace chassis
162
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500163namespace poh
164{
165
166constexpr auto minutesPerCount = 60;
167
168} // namespace poh
169
Patrick Venture0b02be92018-08-31 11:55:55 -0700170struct get_sys_boot_options_t
171{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500172 uint8_t parameter;
173 uint8_t set;
174 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700175} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500176
Patrick Venture0b02be92018-08-31 11:55:55 -0700177struct get_sys_boot_options_response_t
178{
shgoupfd84fbbf2015-12-17 10:05:51 +0800179 uint8_t version;
180 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500181 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700182} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800183
Patrick Venture0b02be92018-08-31 11:55:55 -0700184struct set_sys_boot_options_t
185{
shgoupfd84fbbf2015-12-17 10:05:51 +0800186 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500187 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700188} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500189
Ratan Guptadcb10672017-07-10 10:33:50 +0530190int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500191{
Ratan Guptadcb10672017-07-10 10:33:50 +0530192 ipmi::PropertyMap properties;
193 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530194 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500195
Ratan Guptadcb10672017-07-10 10:33:50 +0530196 try
197 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700198 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530199 // objects,to handle such cases we are interested on that object
200 // which are on interested busname.
201 // Currenlty mapper doesn't give the readable busname(gives busid)
202 // so we can't match with bus name so giving some object specific info
203 // as SETTINGS_MATCH.
204 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500205
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530206 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530207
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530208 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
209 SETTINGS_ROOT, SETTINGS_MATCH);
210
211 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
212 SETTINGS_ROOT, SETTINGS_MATCH);
213
Patrick Venture0b02be92018-08-31 11:55:55 -0700214 properties = ipmi::getAllDbusProperties(
215 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
216 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
217 macObjectInfo.first, MAC_INTERFACE,
218 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530219
William A. Kennington III4c008022018-10-12 17:18:14 -0700220 auto ipAddress = variant_ns::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530221
William A. Kennington III4c008022018-10-12 17:18:14 -0700222 auto gateway = variant_ns::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530223
William A. Kennington III4c008022018-10-12 17:18:14 -0700224 auto prefix = variant_ns::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530225
Patrick Venture0b02be92018-08-31 11:55:55 -0700226 uint8_t isStatic =
William A. Kennington III4c008022018-10-12 17:18:14 -0700227 (variant_ns::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700228 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
229 ? 1
230 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530231
William A. Kennington III4c008022018-10-12 17:18:14 -0700232 auto MACAddress = variant_ns::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530233
Ratan Guptad70f4532017-08-04 02:07:31 +0530234 // it is expected here that we should get the valid data
235 // but we may also get the default values.
236 // Validation of the data is done by settings.
237 //
238 // if mac address is default mac address then
239 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530240 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530241 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700242 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530243 rc = -1;
244 return rc;
245 }
246 // if addr is static then ipaddress,gateway,prefix
247 // should not be default one,don't send blank override.
248 if (isStatic)
249 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700250 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
251 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530252 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700253 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530254 rc = -1;
255 return rc;
256 }
257 }
258
Patrick Venture0b02be92018-08-31 11:55:55 -0700259 sscanf(
260 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
261 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
262 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
263 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530264
Ratan Guptadcb10672017-07-10 10:33:50 +0530265 respptr->data[MAC_OFFSET + 6] = 0x00;
266
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700267 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
268 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530269
William A. Kennington III4c008022018-10-12 17:18:14 -0700270 uint8_t addressFamily =
271 (variant_ns::get<std::string>(properties["Type"]) ==
272 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
273 ? AF_INET
274 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530275
Patrick Venture0b02be92018-08-31 11:55:55 -0700276 addrSize = (addressFamily == AF_INET)
277 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
278 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530279
280 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530281 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700282 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530283
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530284 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
285
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700286 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530287
288 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
289
Ratan Guptad70f4532017-08-04 02:07:31 +0530290 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700291 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530292 }
293 catch (InternalFailure& e)
294 {
295 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700296 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530297 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500298 return rc;
299 }
300
Patrick Venture0b02be92018-08-31 11:55:55 -0700301 // PetiBoot-Specific
302 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700303 std::memcpy(respptr->data, net_conf_initial_bytes,
304 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500305
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700306 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530307
Ratan Guptafd28dd72016-08-01 04:58:01 -0500308#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700309 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500310
Ratan Guptadcb10672017-07-10 10:33:50 +0530311 for (uint8_t pos = 0; pos < index; pos++)
312 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700313 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530314 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500315#endif
316
Ratan Guptafd28dd72016-08-01 04:58:01 -0500317 return rc;
318}
319
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530320/** @brief convert IPv4 and IPv6 addresses from binary to text form.
321 * @param[in] family - IPv4/Ipv6
322 * @param[in] data - req data pointer.
323 * @param[in] offset - offset in the data.
324 * @param[in] addrSize - size of the data which needs to be read from offset.
325 * @returns address in text form.
326 */
327
Patrick Venture0b02be92018-08-31 11:55:55 -0700328std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
329 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530330{
331 char ipAddr[INET6_ADDRSTRLEN] = {};
332
Patrick Venture0b02be92018-08-31 11:55:55 -0700333 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530334 {
335 case AF_INET:
336 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700337 struct sockaddr_in addr4
338 {
339 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700340 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530341
Patrick Venture0b02be92018-08-31 11:55:55 -0700342 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530343
344 break;
345 }
346 case AF_INET6:
347 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700348 struct sockaddr_in6 addr6
349 {
350 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700351 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530352
Patrick Venture0b02be92018-08-31 11:55:55 -0700353 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530354
355 break;
356 }
357 default:
358 {
359 return {};
360 }
361 }
362
363 return ipAddr;
364}
365
Ratan Guptadcb10672017-07-10 10:33:50 +0530366int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500367{
Ratan Guptadcb10672017-07-10 10:33:50 +0530368 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500369 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700370 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530371 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700372 char addrOrigin{0};
373 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530374 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530375 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
377 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530378 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530379 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500380
Patrick Venture0b02be92018-08-31 11:55:55 -0700381 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500382 // version starts from sixth byte
383
Ratan Guptadcb10672017-07-10 10:33:50 +0530384 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500385 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530386 do
387 {
388 // cookie == 0x21 0x70 0x62 0x21
389 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700390 (net_conf_initial_bytes + COOKIE_OFFSET),
391 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530392 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700393 // cookie == 0
394 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
395 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530396 {
397 // need to zero out the network settings.
398 break;
399 }
400
401 log<level::ERR>("Invalid Cookie");
402 elog<InternalFailure>();
403 }
404
405 // vesion == 0x00 0x01
406 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700407 (net_conf_initial_bytes + VERSION_OFFSET),
408 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530409 {
410
411 log<level::ERR>("Invalid Version");
412 elog<InternalFailure>();
413 }
414
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700415 std::snprintf(
416 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
417 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
418 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
419 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530420
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700421 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
422 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530423
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530424 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530425 {
426 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530427 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530428 }
429
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530430 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700431 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
432 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530433
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530434 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530435
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700436 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
437 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530438
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530439 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
440
Ratan Gupta8c31d232017-08-13 05:49:43 +0530441 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530442 {
443 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
444 family = AF_INET6;
445 }
446
Patrick Venture0b02be92018-08-31 11:55:55 -0700447 ipAddress =
448 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530449
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530450 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
451
Patrick Venture0b02be92018-08-31 11:55:55 -0700452 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530453
Patrick Venture0b02be92018-08-31 11:55:55 -0700454 // Cookie == 0 or it is a valid cookie
455 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
456 std::to_string(prefix) + ",gateway="s + gateway +
457 ",mac="s + mac + ",addressOrigin="s +
458 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500459
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530460 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
461
462 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
463 SETTINGS_ROOT, SETTINGS_MATCH);
464 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
465 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530466 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530467 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700468 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530469 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700470 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530471 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700472 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530473 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700474 IP_INTERFACE, "Gateway", std::string(gateway));
475 ipmi::setDbusProperty(
476 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
477 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530478 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700479 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500480
Patrick Venture0b02be92018-08-31 11:55:55 -0700481 log<level::DEBUG>(
482 "Network configuration changed",
483 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500484 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530485 catch (InternalFailure& e)
486 {
487 commit<InternalFailure>();
488 return -1;
489 }
490
491 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500492}
493
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500494uint32_t getPOHCounter()
495{
496 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
497
Patrick Venture0b02be92018-08-31 11:55:55 -0700498 auto chassisStateObj =
499 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500500
Patrick Venture0b02be92018-08-31 11:55:55 -0700501 auto service =
502 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500503
Patrick Venture0b02be92018-08-31 11:55:55 -0700504 auto propValue =
505 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
506 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500507
William A. Kennington III4c008022018-10-12 17:18:14 -0700508 return variant_ns::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500509}
510
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500511ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
512 ipmi_request_t request,
513 ipmi_response_t response,
514 ipmi_data_len_t data_len,
515 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500516{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500517 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800518 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500519 *data_len = 0;
520 return rc;
521}
522
Nan Li8d15fb42016-08-16 22:29:40 +0800523ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700524 ipmi_request_t request,
525 ipmi_response_t response,
526 ipmi_data_len_t data_len,
527 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800528{
529 // sd_bus error
530 ipmi_ret_t rc = IPMI_CC_OK;
531
532 ipmi_chassis_cap_t chassis_cap{};
533
Yong Liae4b0402018-11-02 11:12:14 +0800534 if (*data_len != 0)
535 {
536 return IPMI_CC_REQ_DATA_LEN_INVALID;
537 }
538
Nan Li8d15fb42016-08-16 22:29:40 +0800539 *data_len = sizeof(ipmi_chassis_cap_t);
540
Yong Liae4b0402018-11-02 11:12:14 +0800541 try
542 {
543 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800544
Yong Liae4b0402018-11-02 11:12:14 +0800545 ipmi::DbusObjectInfo chassisCapObject =
546 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800547
Yong Liae4b0402018-11-02 11:12:14 +0800548 // capabilities flags
549 // [7..4] - reserved
550 // [3] – 1b = provides power interlock (IPM 1.5)
551 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
552 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
553 // has capabilities
554 // to lock out external power control and reset button or
555 // front panel interfaces and/or detect tampering with those
556 // interfaces).
557 // [0] -1b = Chassis provides intrusion (physical security) sensor.
558 // set to default value 0x0.
559 ipmi::Value variant = ipmi::getDbusProperty(
560 bus, chassisCapObject.second, chassisCapObject.first,
561 chassisCapIntf, chassisCapFlagsProp);
562 chassis_cap.cap_flags = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800563
Yong Liae4b0402018-11-02 11:12:14 +0800564 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
565 chassisCapObject.first, chassisCapIntf,
566 chassisFRUDevAddrProp);
567 // Chassis FRU info Device Address.
568 chassis_cap.fru_info_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800569
Yong Liae4b0402018-11-02 11:12:14 +0800570 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
571 chassisCapObject.first, chassisCapIntf,
572 chassisSDRDevAddrProp);
573 // Chassis SDR Device Address.
574 chassis_cap.sdr_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800575
Yong Liae4b0402018-11-02 11:12:14 +0800576 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
577 chassisCapObject.first, chassisCapIntf,
578 chassisSELDevAddrProp);
579 // Chassis SEL Device Address.
580 chassis_cap.sel_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800581
Yong Liae4b0402018-11-02 11:12:14 +0800582 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
583 chassisCapObject.first, chassisCapIntf,
584 chassisSMDevAddrProp);
585 // Chassis System Management Device Address.
586 chassis_cap.system_management_dev_addr =
587 variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800588
Yong Liae4b0402018-11-02 11:12:14 +0800589 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
590 chassisCapObject.first, chassisCapIntf,
591 chassisBridgeDevAddrProp);
592 // Chassis Bridge Device Address.
593 chassis_cap.bridge_dev_addr = variant_ns::get<uint8_t>(variant);
594 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
595 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
596 std::copy(chassisP, chassisP + *data_len, respP);
597 }
598 catch (std::exception& e)
599 {
600 log<level::ERR>(e.what());
601 rc = IPMI_CC_UNSPECIFIED_ERROR;
602 *data_len = 0;
603 return rc;
604 }
605
606 return rc;
607}
608
609ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
610 ipmi_request_t request,
611 ipmi_response_t response,
612 ipmi_data_len_t data_len,
613 ipmi_context_t context)
614{
615 ipmi_ret_t rc = IPMI_CC_OK;
616
617 if (*data_len != sizeof(ipmi_chassis_cap_t))
618 {
619 log<level::ERR>("Unsupported request length",
620 entry("LEN=0x%x", *data_len));
621 *data_len = 0;
622 return IPMI_CC_REQ_DATA_LEN_INVALID;
623 }
624
625 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
626
627 *data_len = 0;
628
629 // check input data
630 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
631 {
632 log<level::ERR>("Unsupported request parameter(CAP Flags)",
633 entry("REQ=0x%x", chassisCap->cap_flags));
634 return IPMI_CC_INVALID_FIELD_REQUEST;
635 }
636
637 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
638 {
639 log<level::ERR>("Unsupported request parameter(FRU Addr)",
640 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
641 return IPMI_CC_INVALID_FIELD_REQUEST;
642 }
643
644 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
645 {
646 log<level::ERR>("Unsupported request parameter(SDR Addr)",
647 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
648 return IPMI_CC_INVALID_FIELD_REQUEST;
649 }
650
651 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
652 {
653 log<level::ERR>("Unsupported request parameter(SEL Addr)",
654 entry("REQ=0x%x", chassisCap->sel_dev_addr));
655 return IPMI_CC_INVALID_FIELD_REQUEST;
656 }
657
658 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
659 {
660 log<level::ERR>(
661 "Unsupported request parameter(SM Addr)",
662 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
663 return IPMI_CC_INVALID_FIELD_REQUEST;
664 }
665
666 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
667 {
668 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
669 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
670 return IPMI_CC_INVALID_FIELD_REQUEST;
671 }
672
673 try
674 {
675 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
676 ipmi::DbusObjectInfo chassisCapObject =
677 ipmi::getDbusObject(bus, chassisCapIntf);
678
679 ipmi::setDbusProperty(bus, chassisCapObject.second,
680 chassisCapObject.first, chassisCapIntf,
681 chassisCapFlagsProp, chassisCap->cap_flags);
682
683 ipmi::setDbusProperty(bus, chassisCapObject.second,
684 chassisCapObject.first, chassisCapIntf,
685 chassisFRUDevAddrProp,
686 chassisCap->fru_info_dev_addr);
687
688 ipmi::setDbusProperty(bus, chassisCapObject.second,
689 chassisCapObject.first, chassisCapIntf,
690 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
691
692 ipmi::setDbusProperty(bus, chassisCapObject.second,
693 chassisCapObject.first, chassisCapIntf,
694 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
695
696 ipmi::setDbusProperty(bus, chassisCapObject.second,
697 chassisCapObject.first, chassisCapIntf,
698 chassisSMDevAddrProp,
699 chassisCap->system_management_dev_addr);
700
701 ipmi::setDbusProperty(bus, chassisCapObject.second,
702 chassisCapObject.first, chassisCapIntf,
703 chassisBridgeDevAddrProp,
704 chassisCap->bridge_dev_addr);
705 }
706 catch (std::exception& e)
707 {
708 log<level::ERR>(e.what());
709 rc = IPMI_CC_UNSPECIFIED_ERROR;
710 return rc;
711 }
Nan Li8d15fb42016-08-16 22:29:40 +0800712
713 return rc;
714}
715
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530716//------------------------------------------
717// Calls into Host State Manager Dbus object
718//------------------------------------------
719int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600720{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500721 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700722 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500723 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700724 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
725 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530726
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500727 // sd_bus error
728 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700729 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600730
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500731 // SD Bus error report mechanism.
732 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600733
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500734 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700735 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500736 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
737 if (rc < 0)
738 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700739 log<level::ERR>(
740 "Failed to get bus name",
741 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500742 return rc;
743 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530744
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500745 // Convert to string equivalent of the passed in transition enum.
746 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530747
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500748 rc = sd_bus_call_method(bus_type, // On the system bus
749 busname, // Service to contact
750 HOST_STATE_MANAGER_ROOT, // Object path
751 DBUS_PROPERTY_IFACE, // Interface name
752 "Set", // Method to be called
753 &bus_error, // object to return error
754 nullptr, // Response buffer if any
755 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700756 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
757 request.c_str());
758 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500759 {
760 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530761 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500762 }
763 else
764 {
765 log<level::INFO>("Transition request initiated successfully");
766 }
vishwa36993272015-11-20 12:43:49 -0600767
768 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500769 free(busname);
vishwa36993272015-11-20 12:43:49 -0600770
Sergey Solomineb9b8142016-08-23 09:07:28 -0500771 return rc;
vishwa36993272015-11-20 12:43:49 -0600772}
773
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500774namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500775{
Nan Lifdd8ec52016-08-28 03:57:40 +0800776
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500777using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
778using IpmiValue = uint8_t;
779using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800780
Patrick Venture0b02be92018-08-31 11:55:55 -0700781std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500782 {RestorePolicy::Policy::AlwaysOff, 0x00},
783 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700784 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800785
Yong Lic6713cf2018-09-12 12:35:13 +0800786static constexpr uint8_t noChange = 0x03;
787static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
788static constexpr uint8_t policyBitMask = 0x07;
789static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500790} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800791
792//----------------------------------------------------------------------
793// Get Chassis Status commands
794//----------------------------------------------------------------------
795ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500796 ipmi_request_t request,
797 ipmi_response_t response,
798 ipmi_data_len_t data_len,
799 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800800{
Patrick Venture0b02be92018-08-31 11:55:55 -0700801 const char* objname = "/org/openbmc/control/power0";
802 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800803
Patrick Venture0b02be92018-08-31 11:55:55 -0700804 sd_bus* bus = NULL;
805 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800806 int r = 0;
807 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700808 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800809 ipmi_ret_t rc = IPMI_CC_OK;
810 ipmi_get_chassis_status_t chassis_status{};
811
Nan Lifdd8ec52016-08-28 03:57:40 +0800812 uint8_t s = 0;
813
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500814 using namespace chassis::internal;
815 using namespace chassis::internal::cache;
816 using namespace power_policy;
817
Deepak Kodihallie6027092017-08-27 08:13:37 -0500818 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700819 auto method = dbus.new_method_call(
820 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
821 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500822 method.append(powerRestoreIntf, "PowerRestorePolicy");
823 auto resp = dbus.call(method);
824 if (resp.is_method_error())
825 {
826 log<level::ERR>("Error in PowerRestorePolicy Get");
827 report<InternalFailure>();
828 *data_len = 0;
829 return IPMI_CC_UNSPECIFIED_ERROR;
830 }
831 sdbusplus::message::variant<std::string> result;
832 resp.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -0700833 auto powerRestore = RestorePolicy::convertPolicyFromString(
834 variant_ns::get<std::string>(result));
Nan Lifdd8ec52016-08-28 03:57:40 +0800835
836 *data_len = 4;
837
Tom Joseph63a00512017-08-09 23:39:59 +0530838 bus = ipmid_get_sd_bus_connection();
839
Nan Lifdd8ec52016-08-28 03:57:40 +0800840 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700841 if (r < 0)
842 {
843 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800844 rc = IPMI_CC_UNSPECIFIED_ERROR;
845 goto finish;
846 }
847
Patrick Venture0b02be92018-08-31 11:55:55 -0700848 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
849 "i");
850 if (r < 0)
851 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530852 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700853 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
854 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530855 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800856 rc = IPMI_CC_UNSPECIFIED_ERROR;
857 goto finish;
858 }
859
860 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700861 if (r < 0)
862 {
863 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800864 rc = IPMI_CC_UNSPECIFIED_ERROR;
865 goto finish;
866 }
867
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500868 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800869
870 // Current Power State
871 // [7] reserved
872 // [6..5] power restore policy
873 // 00b = chassis stays powered off after AC/mains returns
874 // 01b = after AC returns, power is restored to the state that was
875 // in effect when AC/mains was lost.
876 // 10b = chassis always powers up after AC/mains returns
877 // 11b = unknow
878 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700879 // Do we need to define a dbus property to identify the restore
880 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800881
882 // [4] power control fault
883 // 1b = controller attempted to turn system power on or off, but
884 // system did not enter desired state.
885 // Set to 0b, since We don't support it..
886
887 // [3] power fault
888 // 1b = fault detected in main power subsystem.
889 // set to 0b. for we don't support it.
890
891 // [2] 1b = interlock (chassis is presently shut down because a chassis
892 // panel interlock switch is active). (IPMI 1.5)
893 // set to 0b, for we don't support it.
894
895 // [1] power overload
896 // 1b = system shutdown because of power overload condition.
897 // set to 0b, for we don't support it.
898
899 // [0] power is on
900 // 1b = system power is on
901 // 0b = system power is off(soft-off S4/S5, or mechanical off)
902
Patrick Venture0b02be92018-08-31 11:55:55 -0700903 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800904
905 // Last Power Event
906 // [7..5] – reserved
907 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
908 // [3] – 1b = last power down caused by power fault
909 // [2] – 1b = last power down caused by a power interlock being activated
910 // [1] – 1b = last power down caused by a Power overload
911 // [0] – 1b = AC failed
912 // set to 0x0, for we don't support these fields.
913
914 chassis_status.last_power_event = 0;
915
916 // Misc. Chassis State
917 // [7] – reserved
918 // [6] – 1b = Chassis Identify command and state info supported (Optional)
919 // 0b = Chassis Identify command support unspecified via this command.
920 // (The Get Command Support command , if implemented, would still
921 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700922 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
923 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800924 // as 00b) otherwise. Returns the present chassis identify state.
925 // Refer to the Chassis Identify command for more info.
926 // 00b = chassis identify state = Off
927 // 01b = chassis identify state = Temporary(timed) On
928 // 10b = chassis identify state = Indefinite On
929 // 11b = reserved
930 // [3] – 1b = Cooling/fan fault detected
931 // [2] – 1b = Drive Fault
932 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
933 // push-buttons disabled.)
934 // [0] – 1b = Chassis Intrusion active
935 // set to 0, for we don't support them.
936 chassis_status.misc_power_state = 0;
937
938 // Front Panel Button Capabilities and disable/enable status(Optional)
939 // set to 0, for we don't support them.
940 chassis_status.front_panel_button_cap_status = 0;
941
942 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700943 std::memcpy(response, &chassis_status, *data_len);
Nan Lifdd8ec52016-08-28 03:57:40 +0800944
945finish:
946 free(busname);
947 reply = sd_bus_message_unref(reply);
948
949 return rc;
950}
Chris Austen7888c4d2015-12-03 15:26:20 -0600951
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530952//-------------------------------------------------------------
953// Send a command to SoftPowerOff application to stop any timer
954//-------------------------------------------------------------
955int stop_soft_off_timer()
956{
Patrick Venture0b02be92018-08-31 11:55:55 -0700957 constexpr auto iface = "org.freedesktop.DBus.Properties";
958 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
959 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530960
Patrick Venture0b02be92018-08-31 11:55:55 -0700961 constexpr auto property = "ResponseReceived";
962 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
963 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530964
965 // Get the system bus where most system services are provided.
966 auto bus = ipmid_get_sd_bus_connection();
967
968 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500969 // TODO openbmc/openbmc#1661 - Mapper refactor
970 //
971 // See openbmc/openbmc#1743 for some details but high level summary is that
972 // for now the code will directly call the soft off interface due to a
973 // race condition with mapper usage
974 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700975 // char *busname = nullptr;
976 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
977 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500978 //{
979 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530980 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500981 // return r;
982 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530983
984 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500985 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700986 "Set", nullptr, nullptr, "ssv", soft_off_iface,
987 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530988 if (rc < 0)
989 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530990 log<level::ERR>("Failed to set property in SoftPowerOff object",
991 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530992 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500993
Patrick Venture0b02be92018-08-31 11:55:55 -0700994 // TODO openbmc/openbmc#1661 - Mapper refactor
995 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530996 return rc;
997}
998
vishwa36993272015-11-20 12:43:49 -0600999//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001000// Create file to indicate there is no need for softoff notification to host
1001//----------------------------------------------------------------------
1002void indicate_no_softoff_needed()
1003{
1004 fs::path path{HOST_INBAND_REQUEST_DIR};
1005 if (!fs::is_directory(path))
1006 {
1007 fs::create_directory(path);
1008 }
1009
1010 // Add the host instance (default 0 for now) to the file name
1011 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001012 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001013 size++; // null
1014 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001015 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001016
1017 // Append file name to directory and create it
1018 path /= buf.get();
1019 std::ofstream(path.c_str());
1020}
1021
1022//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001023// Chassis Control commands
1024//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001025ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1026 ipmi_request_t request,
1027 ipmi_response_t response,
1028 ipmi_data_len_t data_len,
1029 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001030{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001031 // Error from power off.
1032 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001033
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001034 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001035 *data_len = 0;
1036
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001037 // Catch the actual operaton by peeking into request buffer
Patrick Venture0b02be92018-08-31 11:55:55 -07001038 uint8_t chassis_ctrl_cmd = *(uint8_t*)request;
vishwa36993272015-11-20 12:43:49 -06001039
Patrick Venture0b02be92018-08-31 11:55:55 -07001040 switch (chassis_ctrl_cmd)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001041 {
1042 case CMD_POWER_ON:
1043 rc = initiate_state_transition(State::Host::Transition::On);
1044 break;
1045 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301046 // This path would be hit in 2 conditions.
1047 // 1: When user asks for power off using ipmi chassis command 0x04
1048 // 2: Host asking for power off post shutting down.
1049
1050 // If it's a host requested power off, then need to nudge Softoff
1051 // application that it needs to stop the watchdog timer if running.
1052 // If it is a user requested power off, then this is not really
1053 // needed. But then we need to differentiate between user and host
1054 // calling this same command
1055
1056 // For now, we are going ahead with trying to nudge the soft off and
1057 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001058 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301059
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001060 // Only request the Off transition if the soft power off
1061 // application is not running
1062 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001063 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001064 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301065 // that it should not run. Not doing this will result in State
1066 // manager doing a default soft power off when asked for power
1067 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001068 indicate_no_softoff_needed();
1069
1070 // Now request the shutdown
1071 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001072 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001073 else
1074 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301075 log<level::INFO>("Soft off is running, so let shutdown target "
1076 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001077 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001078 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301079
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001080 case CMD_HARD_RESET:
1081 case CMD_POWER_CYCLE:
1082 // SPEC has a section that says certain implementations can trigger
1083 // PowerOn if power is Off when a command to power cycle is
1084 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001085
1086 // First create a file to indicate to the soft off application
1087 // that it should not run since this is a direct user initiated
1088 // power reboot request (i.e. a reboot request that is not
1089 // originating via a soft power off SMS request)
1090 indicate_no_softoff_needed();
1091
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001092 rc = initiate_state_transition(State::Host::Transition::Reboot);
1093 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301094
1095 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1096 // Request Host State Manager to do a soft power off
1097 rc = initiate_state_transition(State::Host::Transition::Off);
1098 break;
1099
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001100 default:
1101 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301102 log<level::ERR>("Invalid Chassis Control command",
1103 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001104 rc = -1;
1105 }
1106 }
vishwa36993272015-11-20 12:43:49 -06001107
Patrick Venture0b02be92018-08-31 11:55:55 -07001108 return ((rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001109}
1110
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001111/** @brief Return D-Bus connection string to enclosure identify LED object
1112 *
1113 * @param[in, out] connection - connection to D-Bus object
1114 * @return a IPMI return code
1115 */
1116std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301117{
Tom Joseph5110c122018-03-23 17:55:40 +05301118 // lookup enclosure_identify group owner(s) in mapper
1119 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001120 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1121 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301122
1123 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001124 static const std::vector<std::string> interfaces = {
1125 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301126 mapperCall.append(interfaces);
1127 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1128 if (mapperReply.is_method_error())
1129 {
1130 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001131 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301132 }
1133 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1134 mapperReply.read(mapperResp);
1135
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001136 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301137 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001138 log<level::ERR>(
1139 "Invalid number of enclosure identify objects.",
1140 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001141 elog<InternalFailure>();
1142 }
1143 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1144 return pair.first;
1145}
Tom Joseph5110c122018-03-23 17:55:40 +05301146
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001147/** @brief Turn On/Off enclosure identify LED
1148 *
1149 * @param[in] flag - true to turn on LED, false to turn off
1150 * @return a IPMI return code
1151 */
1152void enclosureIdentifyLed(bool flag)
1153{
1154 using namespace chassis::internal;
1155 std::string connection = std::move(getEnclosureIdentifyConnection());
Patrick Venture0b02be92018-08-31 11:55:55 -07001156 auto led =
1157 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1158 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001159 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001160 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001161 auto ledReply = dbus.call(led);
1162 if (ledReply.is_method_error())
1163 {
1164 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001165 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001166 elog<InternalFailure>();
1167 }
1168}
1169
1170/** @brief Callback method to turn off LED
1171 */
1172void enclosureIdentifyLedOff()
1173{
1174 try
1175 {
1176 enclosureIdentifyLed(false);
1177 }
1178 catch (const InternalFailure& e)
1179 {
1180 report<InternalFailure>();
1181 }
1182}
1183
1184/** @brief Create timer to turn on and off the enclosure LED
1185 */
1186void createIdentifyTimer()
1187{
1188 if (!identifyTimer)
1189 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001190 identifyTimer =
1191 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001192 }
1193}
1194
1195ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1196 ipmi_request_t request,
1197 ipmi_response_t response,
1198 ipmi_data_len_t data_len,
1199 ipmi_context_t context)
1200{
1201 if (*data_len > chassisIdentifyReqLength)
1202 {
1203 return IPMI_CC_REQ_DATA_LEN_INVALID;
1204 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001205 uint8_t identifyInterval =
1206 *data_len > identifyIntervalPos
1207 ? (static_cast<uint8_t*>(request))[identifyIntervalPos]
1208 : DEFAULT_IDENTIFY_TIME_OUT;
1209 bool forceIdentify =
1210 (*data_len == chassisIdentifyReqLength)
1211 ? (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01
1212 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301213
Jia, Chunhui7cf145f2019-02-28 14:14:27 +08001214 *data_len = 0; // response have complete code only
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001215 if (identifyInterval || forceIdentify)
1216 {
1217 // stop the timer if already started, for force identify we should
1218 // not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001219 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001220 try
Tom Joseph5110c122018-03-23 17:55:40 +05301221 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001222 enclosureIdentifyLed(true);
1223 }
1224 catch (const InternalFailure& e)
1225 {
1226 report<InternalFailure>();
1227 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301228 }
1229
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001230 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301231 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001232 return IPMI_CC_OK;
1233 }
1234 // start the timer
1235 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001236 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001237 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301238 }
Tom Josephbed26992018-07-31 23:00:24 +05301239 else if (!identifyInterval)
1240 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001241 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301242 enclosureIdentifyLedOff();
1243 }
Tom Joseph5110c122018-03-23 17:55:40 +05301244 return IPMI_CC_OK;
1245}
1246
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001247namespace boot_options
1248{
1249
1250using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1251using IpmiValue = uint8_t;
1252constexpr auto ipmiDefault = 0;
1253
Patrick Venture0b02be92018-08-31 11:55:55 -07001254std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001255 {0x01, Source::Sources::Network},
1256 {0x02, Source::Sources::Disk},
1257 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001258 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001259
Patrick Venture0b02be92018-08-31 11:55:55 -07001260std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001261 {0x03, Mode::Modes::Safe},
1262 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001263 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001264
Patrick Venture0b02be92018-08-31 11:55:55 -07001265std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001266 {Source::Sources::Network, 0x01},
1267 {Source::Sources::Disk, 0x02},
1268 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001269 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001270
Patrick Venture0b02be92018-08-31 11:55:55 -07001271std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001272 {Mode::Modes::Safe, 0x03},
1273 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001274 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001275
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001276} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001277
Marri Devender Rao81719702018-05-07 00:53:48 -05001278/** @brief Set the property value for boot source
1279 * @param[in] source - boot source value
1280 * @return On failure return IPMI error.
1281 */
1282static ipmi_ret_t setBootSource(const Source::Sources& source)
1283{
1284 using namespace chassis::internal;
1285 using namespace chassis::internal::cache;
1286 sdbusplus::message::variant<std::string> property =
1287 convertForMessage(source);
1288 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1289 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001290 auto method = dbus.new_method_call(
1291 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1292 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001293 method.append(bootSourceIntf, "BootSource", property);
1294 auto reply = dbus.call(method);
1295 if (reply.is_method_error())
1296 {
1297 log<level::ERR>("Error in BootSource Set");
1298 report<InternalFailure>();
1299 return IPMI_CC_UNSPECIFIED_ERROR;
1300 }
1301 return IPMI_CC_OK;
1302}
1303
Patrick Venture0b02be92018-08-31 11:55:55 -07001304/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001305 * @param[in] mode - boot mode value
1306 * @return On failure return IPMI error.
1307 */
1308static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1309{
1310 using namespace chassis::internal;
1311 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001312 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001313 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1314 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001315 auto method = dbus.new_method_call(
1316 objects.service(bootModeSetting, bootModeIntf).c_str(),
1317 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001318 method.append(bootModeIntf, "BootMode", property);
1319 auto reply = dbus.call(method);
1320 if (reply.is_method_error())
1321 {
1322 log<level::ERR>("Error in BootMode Set");
1323 report<InternalFailure>();
1324 return IPMI_CC_UNSPECIFIED_ERROR;
1325 }
1326 return IPMI_CC_OK;
1327}
1328
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001329ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1330 ipmi_request_t request,
1331 ipmi_response_t response,
1332 ipmi_data_len_t data_len,
1333 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001334{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001335 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001336 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001337 char* p = NULL;
1338 get_sys_boot_options_response_t* resp =
1339 (get_sys_boot_options_response_t*)response;
1340 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001341 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001342
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001343 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001344 resp->version = SET_PARM_VERSION;
1345 resp->parm = 5;
1346 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001347
shgoupfd84fbbf2015-12-17 10:05:51 +08001348 /*
1349 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1350 * This is the only parameter used by petitboot.
1351 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001352 if (reqptr->parameter ==
1353 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1354 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001355
Ratan Guptafd28dd72016-08-01 04:58:01 -05001356 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001357 using namespace chassis::internal;
1358 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001359
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001360 try
ratagupta6f6bff2016-04-04 06:20:11 -05001361 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001362 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1363 const auto& bootSourceSetting =
1364 std::get<settings::Path>(bootSetting);
1365 auto oneTimeEnabled =
1366 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001367 auto method = dbus.new_method_call(
1368 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1369 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001370 method.append(bootSourceIntf, "BootSource");
1371 auto reply = dbus.call(method);
1372 if (reply.is_method_error())
1373 {
1374 log<level::ERR>("Error in BootSource Get");
1375 report<InternalFailure>();
1376 *data_len = 0;
1377 return IPMI_CC_UNSPECIFIED_ERROR;
1378 }
1379 sdbusplus::message::variant<std::string> result;
1380 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001381 auto bootSource = Source::convertSourcesFromString(
1382 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001383
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001384 bootSetting = settings::boot::setting(objects, bootModeIntf);
1385 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1386 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001387 objects.service(bootModeSetting, bootModeIntf).c_str(),
1388 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001389 method.append(bootModeIntf, "BootMode");
1390 reply = dbus.call(method);
1391 if (reply.is_method_error())
1392 {
1393 log<level::ERR>("Error in BootMode Get");
1394 report<InternalFailure>();
1395 *data_len = 0;
1396 return IPMI_CC_UNSPECIFIED_ERROR;
1397 }
1398 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001399 auto bootMode = Mode::convertModesFromString(
1400 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001401
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001402 bootOption = sourceDbusToIpmi.at(bootSource);
1403 if ((Mode::Modes::Regular == bootMode) &&
1404 (Source::Sources::Default == bootSource))
1405 {
1406 bootOption = ipmiDefault;
1407 }
1408 else if (Source::Sources::Default == bootSource)
1409 {
1410 bootOption = modeDbusToIpmi.at(bootMode);
1411 }
1412 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001413
Patrick Venture0b02be92018-08-31 11:55:55 -07001414 resp->data[0] = oneTimeEnabled
1415 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1416 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001417
ratagupta6f6bff2016-04-04 06:20:11 -05001418 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001419 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001420 catch (InternalFailure& e)
1421 {
1422 report<InternalFailure>();
1423 *data_len = 0;
1424 return IPMI_CC_UNSPECIFIED_ERROR;
1425 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001426 }
1427 else if (reqptr->parameter ==
1428 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1429 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001430
Patrick Venture0b02be92018-08-31 11:55:55 -07001431 *data_len =
1432 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001433
Patrick Venture0b02be92018-08-31 11:55:55 -07001434 resp->parm =
1435 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001436
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001437 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001438
Patrick Venture0b02be92018-08-31 11:55:55 -07001439 if (ret < 0)
1440 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001441
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301442 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001443 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001444 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001445 }
1446 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001447 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001448 }
1449
Patrick Venture0b02be92018-08-31 11:55:55 -07001450 else
1451 {
1452 log<level::ERR>("Unsupported parameter",
1453 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001454 }
1455
1456 if (p)
1457 free(p);
1458
Ratan Guptafd28dd72016-08-01 04:58:01 -05001459 if (rc == IPMI_CC_OK)
1460 {
1461 *data_len += 2;
1462 }
1463
shgoupfd84fbbf2015-12-17 10:05:51 +08001464 return rc;
1465}
1466
shgoupfd84fbbf2015-12-17 10:05:51 +08001467ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001468 ipmi_request_t request,
1469 ipmi_response_t response,
1470 ipmi_data_len_t data_len,
1471 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001472{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001473 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001474 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001475 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001476
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001477 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1478 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001479
shgoupfd84fbbf2015-12-17 10:05:51 +08001480 // This IPMI command does not have any resposne data
1481 *data_len = 0;
1482
1483 /* 000101
1484 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1485 * This is the only parameter used by petitboot.
1486 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001487
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001488 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1489 {
1490 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1491 using namespace chassis::internal;
1492 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001493 auto oneTimeEnabled = false;
1494 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301495 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001496 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001497
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001498 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001499 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001500 bool permanent =
1501 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1502 SET_PARM_BOOT_FLAGS_PERMANENT;
1503
Patrick Venture0b02be92018-08-31 11:55:55 -07001504 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301505
1506 oneTimeEnabled =
1507 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1508
1509 /*
1510 * Check if the current boot setting is onetime or permanent, if the
1511 * request in the command is otherwise, then set the "Enabled"
1512 * property in one_time object path to 'True' to indicate onetime
1513 * and 'False' to indicate permanent.
1514 *
1515 * Once the onetime/permanent setting is applied, then the bootMode
1516 * and bootSource is updated for the corresponding object.
1517 */
1518 if ((permanent && oneTimeEnabled) ||
1519 (!permanent && !oneTimeEnabled))
1520 {
1521 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1522
Patrick Venture0b02be92018-08-31 11:55:55 -07001523 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1524 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301525 }
1526
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001527 auto modeItr = modeIpmiToDbus.find(bootOption);
1528 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1529 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001530 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001531 rc = setBootSource(sourceItr->second);
1532 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001533 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001534 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001535 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001536 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001537 // If a set boot device is mapping to a boot source, then reset
1538 // the boot mode D-Bus property to default.
1539 // This way the ipmid code can determine which property is not
1540 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001541 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001542 {
1543 setBootMode(Mode::Modes::Regular);
1544 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001545 }
1546 if (modeIpmiToDbus.end() != modeItr)
1547 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001548 rc = setBootMode(modeItr->second);
1549 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001550 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001551 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001552 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001553 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001554 // If a set boot device is mapping to a boot mode, then reset
1555 // the boot source D-Bus property to default.
1556 // This way the ipmid code can determine which property is not
1557 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001558 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001559 {
1560 setBootSource(Source::Sources::Default);
1561 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001562 }
1563 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001564 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001565 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001566 report<InternalFailure>();
1567 *data_len = 0;
1568 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001569 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001570 }
1571 else if (reqptr->parameter ==
1572 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1573 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001574
1575 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001576 if (ret < 0)
1577 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301578 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001579 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001580 rc = IPMI_CC_UNSPECIFIED_ERROR;
1581 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001582 }
1583 else if (reqptr->parameter ==
1584 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1585 {
Tom Josephf536c902017-09-25 18:08:15 +05301586 // Handle parameter #4 and return command completed normally
1587 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1588 // parameter. This is added to support the ipmitool command `chassis
1589 // bootdev` which sends set on parameter #4, before setting the boot
1590 // flags.
1591 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001592 }
1593 else
1594 {
1595 log<level::ERR>("Unsupported parameter",
1596 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001597 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001598 }
1599
1600 return rc;
1601}
1602
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001603ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1604 ipmi_request_t request, ipmi_response_t response,
1605 ipmi_data_len_t data_len, ipmi_context_t context)
1606{
1607 // sd_bus error
1608 ipmi_ret_t rc = IPMI_CC_OK;
1609
1610 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1611
1612 try
1613 {
1614 auto pohCounter = getPOHCounter();
1615 resptr->counterReading[0] = pohCounter;
1616 resptr->counterReading[1] = pohCounter >> 8;
1617 resptr->counterReading[2] = pohCounter >> 16;
1618 resptr->counterReading[3] = pohCounter >> 24;
1619 }
1620 catch (std::exception& e)
1621 {
1622 log<level::ERR>(e.what());
1623 return IPMI_CC_UNSPECIFIED_ERROR;
1624 }
1625
1626 resptr->minPerCount = poh::minutesPerCount;
1627 *data_len = sizeof(GetPOHCountResponse);
1628
1629 return rc;
1630}
1631
Yong Lic6713cf2018-09-12 12:35:13 +08001632ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1633 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1634 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1635{
1636 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1637 auto* resptr = reinterpret_cast<uint8_t*>(response);
1638 uint8_t reqPolicy = 0;
1639
1640 power_policy::DbusValue value =
1641 power_policy::RestorePolicy::Policy::AlwaysOff;
1642
1643 if (*data_len != power_policy::setPolicyReqLen)
1644 {
1645 phosphor::logging::log<level::ERR>("Unsupported request length",
1646 entry("LEN=0x%x", *data_len));
1647 *data_len = 0;
1648 return IPMI_CC_REQ_DATA_LEN_INVALID;
1649 }
1650
Yong Licb89c0e2019-01-18 17:54:32 +08001651 if (*reqptr > power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001652 {
1653 phosphor::logging::log<level::ERR>("Reserved request parameter",
Yong Licb89c0e2019-01-18 17:54:32 +08001654 entry("REQ=0x%x", *reqptr));
Yong Lic6713cf2018-09-12 12:35:13 +08001655 *data_len = 0;
Yong Licb89c0e2019-01-18 17:54:32 +08001656 return IPMI_CC_PARM_OUT_OF_RANGE;
Yong Lic6713cf2018-09-12 12:35:13 +08001657 }
1658
Yong Licb89c0e2019-01-18 17:54:32 +08001659 reqPolicy = *reqptr & power_policy::policyBitMask;
Yong Lic6713cf2018-09-12 12:35:13 +08001660 if (reqPolicy == power_policy::noChange)
1661 {
1662 // just return the supported policy
1663 *resptr = power_policy::allSupport;
1664 *data_len = power_policy::setPolicyReqLen;
1665 return IPMI_CC_OK;
1666 }
1667
1668 for (auto const& it : power_policy::dbusToIpmi)
1669 {
1670 if (it.second == reqPolicy)
1671 {
1672 value = it.first;
1673 break;
1674 }
1675 }
1676
1677 try
1678 {
1679 const settings::Path& powerRestoreSetting =
1680 chassis::internal::cache::objects.map
1681 .at(chassis::internal::powerRestoreIntf)
1682 .front();
1683 sdbusplus::message::variant<std::string> property =
1684 convertForMessage(value);
1685
1686 auto method = chassis::internal::dbus.new_method_call(
1687 chassis::internal::cache::objects
1688 .service(powerRestoreSetting,
1689 chassis::internal::powerRestoreIntf)
1690 .c_str(),
1691 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1692
1693 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1694 property);
1695 auto reply = chassis::internal::dbus.call(method);
1696 if (reply.is_method_error())
1697 {
1698 phosphor::logging::log<level::ERR>("Unspecified Error");
1699 *data_len = 0;
1700 return IPMI_CC_UNSPECIFIED_ERROR;
1701 }
1702 }
1703 catch (InternalFailure& e)
1704 {
1705 report<InternalFailure>();
1706 *data_len = 0;
1707 return IPMI_CC_UNSPECIFIED_ERROR;
1708 }
1709
Yong Licb89c0e2019-01-18 17:54:32 +08001710 *resptr = power_policy::allSupport;
Yong Lic6713cf2018-09-12 12:35:13 +08001711 *data_len = power_policy::setPolicyReqLen;
1712 return IPMI_CC_OK;
1713}
1714
Adriana Kobylak40814c62015-10-27 15:58:44 -05001715void register_netfn_chassis_functions()
1716{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001717 createIdentifyTimer();
1718
Tom05732372016-09-06 17:21:23 +05301719 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001720 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1721 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001722
Tom05732372016-09-06 17:21:23 +05301723 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001724 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1725 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001726
Yong Liae4b0402018-11-02 11:12:14 +08001727 // Set Chassis Capabilities
1728 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1729 ipmi_set_chassis_cap, PRIVILEGE_USER);
1730
Tom05732372016-09-06 17:21:23 +05301731 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301732 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001733 ipmi_chassis_get_sys_boot_options,
1734 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001735
Tom05732372016-09-06 17:21:23 +05301736 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001737 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1738 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001739
Tom05732372016-09-06 17:21:23 +05301740 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001741 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1742 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001743
Tom Joseph5110c122018-03-23 17:55:40 +05301744 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301745 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1746 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1747
Tom05732372016-09-06 17:21:23 +05301748 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301749 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001750 ipmi_chassis_set_sys_boot_options,
1751 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001752 // <Get POH Counter>
1753 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1754 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001755
1756 // <Set Power Restore Policy>
1757 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1758 ipmi_chassis_set_power_restore_policy,
1759 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001760}