blob: c9885bd35f1962a615d217bf30a4e0306584e95d [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"
Patrick Venture0b02be92018-08-31 11:55:55 -07007#include "types.hpp"
Ratan Guptacc8feb42017-07-25 21:52:10 +05308#include "utils.hpp"
Ratan Guptadcb10672017-07-10 10:33:50 +05309
Patrick Venture0b02be92018-08-31 11:55:55 -070010#include <arpa/inet.h>
11#include <endian.h>
William A. Kennington III194375f2018-12-14 02:14:33 -080012#include <ipmid/api.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070013#include <limits.h>
14#include <mapper.h>
15#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070016
Ratan Guptafd28dd72016-08-01 04:58:01 -050017#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070018#include <chrono>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070019#include <cstring>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050020#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053021#include <future>
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
Vernon Mauery185b9f82018-07-20 10:52:36 -070038#if __has_include(<filesystem>)
39#include <filesystem>
40#elif __has_include(<experimental/filesystem>)
Andrew Geisslera6e3a302017-05-31 19:34:00 -050041#include <experimental/filesystem>
Patrick Venture0b02be92018-08-31 11:55:55 -070042namespace std
43{
44// splice experimental::filesystem into std
45namespace filesystem = std::experimental::filesystem;
46} // namespace std
Vernon Mauery185b9f82018-07-20 10:52:36 -070047#else
Patrick Venture0b02be92018-08-31 11:55:55 -070048#error filesystem not available
Vernon Mauery185b9f82018-07-20 10:52:36 -070049#endif
Patrick Venture0b02be92018-08-31 11:55:55 -070050
Patrick Venture0b02be92018-08-31 11:55:55 -070051// Defines
52#define SET_PARM_VERSION 0x01
Vernon Mauery1181af72018-10-08 12:05:00 -070053#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40
54#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80
55#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0
ratagupta6f6bff2016-04-04 06:20:11 -050056
Lei YU4b0ddb62019-01-25 16:43:50 +080057std::unique_ptr<phosphor::Timer> identifyTimer
58 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050059
Patrick Venture0b02be92018-08-31 11:55:55 -070060constexpr size_t SIZE_MAC = 18;
61constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
62 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
63 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050064constexpr size_t SIZE_PREFIX = 7;
65constexpr size_t MAX_PREFIX_VALUE = 32;
66constexpr size_t SIZE_COOKIE = 4;
67constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053068constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053069
Patrick Venture0b02be92018-08-31 11:55:55 -070070// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053071static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070072 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050073
74static constexpr size_t COOKIE_OFFSET = 1;
75static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053076static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050077static constexpr size_t MAC_OFFSET = 9;
78static constexpr size_t ADDRTYPE_OFFSET = 16;
79static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050080
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050081static constexpr size_t encIdentifyObjectsSize = 1;
82static constexpr size_t chassisIdentifyReqLength = 2;
83static constexpr size_t identifyIntervalPos = 0;
84static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080085
Adriana Kobylak40814c62015-10-27 15:58:44 -050086void register_netfn_chassis_functions() __attribute__((constructor));
87
shgoupfd84fbbf2015-12-17 10:05:51 +080088// Host settings in dbus
89// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070090const char* settings_object_name = "/org/openbmc/settings/host0";
91const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070092const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053093 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080094
Ratan Guptadcb10672017-07-10 10:33:50 +053095constexpr auto SETTINGS_ROOT = "/";
96constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053097
98constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
99constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
100
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500101static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
102static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -0700103 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500104static constexpr auto pOHCounterProperty = "POHCounter";
105static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +0800106const static constexpr char chassisCapIntf[] =
107 "xyz.openbmc_project.Control.ChassisCapabilities";
108const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
109const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
110const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
111const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
112const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
113const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
114static constexpr uint8_t chassisCapFlagMask = 0x0f;
115static constexpr uint8_t chassisCapAddrMask = 0xfe;
Ratan Guptadcb10672017-07-10 10:33:50 +0530116
Nan Li8d15fb42016-08-16 22:29:40 +0800117typedef struct
118{
119 uint8_t cap_flags;
120 uint8_t fru_info_dev_addr;
121 uint8_t sdr_dev_addr;
122 uint8_t sel_dev_addr;
123 uint8_t system_management_dev_addr;
124 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700125} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800126
Nan Lifdd8ec52016-08-28 03:57:40 +0800127typedef struct
128{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500129 uint8_t cur_power_state;
130 uint8_t last_power_event;
131 uint8_t misc_power_state;
132 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700133} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800134
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500135/**
136 * @struct Get POH counter command response data
137 */
138struct GetPOHCountResponse
139{
Patrick Venture0b02be92018-08-31 11:55:55 -0700140 uint8_t minPerCount; ///< Minutes per count
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500141 uint8_t counterReading[4]; ///< Counter reading
Patrick Venture0b02be92018-08-31 11:55:55 -0700142} __attribute__((packed));
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500143
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530144// Phosphor Host State manager
145namespace State = sdbusplus::xyz::openbmc_project::State::server;
146
Vernon Mauery185b9f82018-07-20 10:52:36 -0700147namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500148
Ratan Guptadcb10672017-07-10 10:33:50 +0530149using namespace phosphor::logging;
150using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500151using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700152namespace variant_ns = sdbusplus::message::variant_ns;
153
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500154namespace chassis
155{
156namespace internal
157{
158
159constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
160constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500161constexpr auto powerRestoreIntf =
162 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500163sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
164
165namespace cache
166{
167
168settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500169 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500170
171} // namespace cache
172} // namespace internal
173} // namespace chassis
174
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500175namespace poh
176{
177
178constexpr auto minutesPerCount = 60;
179
180} // namespace poh
181
Patrick Venture0b02be92018-08-31 11:55:55 -0700182struct get_sys_boot_options_t
183{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500184 uint8_t parameter;
185 uint8_t set;
186 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700187} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500188
Patrick Venture0b02be92018-08-31 11:55:55 -0700189struct get_sys_boot_options_response_t
190{
shgoupfd84fbbf2015-12-17 10:05:51 +0800191 uint8_t version;
192 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500193 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700194} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800195
Patrick Venture0b02be92018-08-31 11:55:55 -0700196struct set_sys_boot_options_t
197{
shgoupfd84fbbf2015-12-17 10:05:51 +0800198 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500199 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700200} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500201
Ratan Guptadcb10672017-07-10 10:33:50 +0530202int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500203{
Ratan Guptadcb10672017-07-10 10:33:50 +0530204 ipmi::PropertyMap properties;
205 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530206 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500207
Ratan Guptadcb10672017-07-10 10:33:50 +0530208 try
209 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700210 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530211 // objects,to handle such cases we are interested on that object
212 // which are on interested busname.
213 // Currenlty mapper doesn't give the readable busname(gives busid)
214 // so we can't match with bus name so giving some object specific info
215 // as SETTINGS_MATCH.
216 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500217
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530218 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530219
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530220 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
221 SETTINGS_ROOT, SETTINGS_MATCH);
222
223 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
224 SETTINGS_ROOT, SETTINGS_MATCH);
225
Patrick Venture0b02be92018-08-31 11:55:55 -0700226 properties = ipmi::getAllDbusProperties(
227 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
228 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
229 macObjectInfo.first, MAC_INTERFACE,
230 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530231
William A. Kennington III4c008022018-10-12 17:18:14 -0700232 auto ipAddress = variant_ns::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530233
William A. Kennington III4c008022018-10-12 17:18:14 -0700234 auto gateway = variant_ns::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530235
William A. Kennington III4c008022018-10-12 17:18:14 -0700236 auto prefix = variant_ns::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530237
Patrick Venture0b02be92018-08-31 11:55:55 -0700238 uint8_t isStatic =
William A. Kennington III4c008022018-10-12 17:18:14 -0700239 (variant_ns::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700240 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
241 ? 1
242 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530243
William A. Kennington III4c008022018-10-12 17:18:14 -0700244 auto MACAddress = variant_ns::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530245
Ratan Guptad70f4532017-08-04 02:07:31 +0530246 // it is expected here that we should get the valid data
247 // but we may also get the default values.
248 // Validation of the data is done by settings.
249 //
250 // if mac address is default mac address then
251 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530252 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530253 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700254 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530255 rc = -1;
256 return rc;
257 }
258 // if addr is static then ipaddress,gateway,prefix
259 // should not be default one,don't send blank override.
260 if (isStatic)
261 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700262 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
263 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530264 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700265 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530266 rc = -1;
267 return rc;
268 }
269 }
270
Patrick Venture0b02be92018-08-31 11:55:55 -0700271 sscanf(
272 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
273 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
274 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
275 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530276
Ratan Guptadcb10672017-07-10 10:33:50 +0530277 respptr->data[MAC_OFFSET + 6] = 0x00;
278
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700279 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
280 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530281
William A. Kennington III4c008022018-10-12 17:18:14 -0700282 uint8_t addressFamily =
283 (variant_ns::get<std::string>(properties["Type"]) ==
284 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
285 ? AF_INET
286 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530287
Patrick Venture0b02be92018-08-31 11:55:55 -0700288 addrSize = (addressFamily == AF_INET)
289 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
290 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530291
292 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530293 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700294 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530295
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530296 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
297
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700298 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530299
300 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
301
Ratan Guptad70f4532017-08-04 02:07:31 +0530302 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700303 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530304 }
305 catch (InternalFailure& e)
306 {
307 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700308 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530309 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500310 return rc;
311 }
312
Patrick Venture0b02be92018-08-31 11:55:55 -0700313 // PetiBoot-Specific
314 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700315 std::memcpy(respptr->data, net_conf_initial_bytes,
316 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500317
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700318 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530319
Ratan Guptafd28dd72016-08-01 04:58:01 -0500320#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700321 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500322
Ratan Guptadcb10672017-07-10 10:33:50 +0530323 for (uint8_t pos = 0; pos < index; pos++)
324 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700325 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530326 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500327#endif
328
Ratan Guptafd28dd72016-08-01 04:58:01 -0500329 return rc;
330}
331
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530332/** @brief convert IPv4 and IPv6 addresses from binary to text form.
333 * @param[in] family - IPv4/Ipv6
334 * @param[in] data - req data pointer.
335 * @param[in] offset - offset in the data.
336 * @param[in] addrSize - size of the data which needs to be read from offset.
337 * @returns address in text form.
338 */
339
Patrick Venture0b02be92018-08-31 11:55:55 -0700340std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
341 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530342{
343 char ipAddr[INET6_ADDRSTRLEN] = {};
344
Patrick Venture0b02be92018-08-31 11:55:55 -0700345 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530346 {
347 case AF_INET:
348 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700349 struct sockaddr_in addr4
350 {
351 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700352 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530353
Patrick Venture0b02be92018-08-31 11:55:55 -0700354 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530355
356 break;
357 }
358 case AF_INET6:
359 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700360 struct sockaddr_in6 addr6
361 {
362 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700363 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530364
Patrick Venture0b02be92018-08-31 11:55:55 -0700365 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530366
367 break;
368 }
369 default:
370 {
371 return {};
372 }
373 }
374
375 return ipAddr;
376}
377
Ratan Guptadcb10672017-07-10 10:33:50 +0530378int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500379{
Ratan Guptadcb10672017-07-10 10:33:50 +0530380 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500381 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700382 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530383 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700384 char addrOrigin{0};
385 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530386 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530387 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700388 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
389 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530390 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530391 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500392
Patrick Venture0b02be92018-08-31 11:55:55 -0700393 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500394 // version starts from sixth byte
395
Ratan Guptadcb10672017-07-10 10:33:50 +0530396 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500397 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530398 do
399 {
400 // cookie == 0x21 0x70 0x62 0x21
401 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700402 (net_conf_initial_bytes + COOKIE_OFFSET),
403 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530404 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700405 // cookie == 0
406 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
407 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530408 {
409 // need to zero out the network settings.
410 break;
411 }
412
413 log<level::ERR>("Invalid Cookie");
414 elog<InternalFailure>();
415 }
416
417 // vesion == 0x00 0x01
418 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700419 (net_conf_initial_bytes + VERSION_OFFSET),
420 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530421 {
422
423 log<level::ERR>("Invalid Version");
424 elog<InternalFailure>();
425 }
426
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700427 std::snprintf(
428 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
429 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
430 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
431 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530432
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700433 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
434 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530435
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530436 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530437 {
438 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530439 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530440 }
441
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530442 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700443 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
444 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530445
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530446 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530447
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700448 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
449 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530450
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530451 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
452
Ratan Gupta8c31d232017-08-13 05:49:43 +0530453 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530454 {
455 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
456 family = AF_INET6;
457 }
458
Patrick Venture0b02be92018-08-31 11:55:55 -0700459 ipAddress =
460 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530461
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530462 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
463
Patrick Venture0b02be92018-08-31 11:55:55 -0700464 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530465
Patrick Venture0b02be92018-08-31 11:55:55 -0700466 // Cookie == 0 or it is a valid cookie
467 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
468 std::to_string(prefix) + ",gateway="s + gateway +
469 ",mac="s + mac + ",addressOrigin="s +
470 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500471
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530472 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
473
474 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
475 SETTINGS_ROOT, SETTINGS_MATCH);
476 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
477 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530478 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530479 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700480 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530481 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700482 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530483 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700484 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530485 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700486 IP_INTERFACE, "Gateway", std::string(gateway));
487 ipmi::setDbusProperty(
488 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
489 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530490 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700491 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500492
Patrick Venture0b02be92018-08-31 11:55:55 -0700493 log<level::DEBUG>(
494 "Network configuration changed",
495 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500496 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530497 catch (InternalFailure& e)
498 {
499 commit<InternalFailure>();
500 return -1;
501 }
502
503 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500504}
505
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500506uint32_t getPOHCounter()
507{
508 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
509
Patrick Venture0b02be92018-08-31 11:55:55 -0700510 auto chassisStateObj =
511 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500512
Patrick Venture0b02be92018-08-31 11:55:55 -0700513 auto service =
514 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500515
Patrick Venture0b02be92018-08-31 11:55:55 -0700516 auto propValue =
517 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
518 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500519
William A. Kennington III4c008022018-10-12 17:18:14 -0700520 return variant_ns::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500521}
522
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500523ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
524 ipmi_request_t request,
525 ipmi_response_t response,
526 ipmi_data_len_t data_len,
527 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500528{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500529 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800530 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500531 *data_len = 0;
532 return rc;
533}
534
Nan Li8d15fb42016-08-16 22:29:40 +0800535ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700536 ipmi_request_t request,
537 ipmi_response_t response,
538 ipmi_data_len_t data_len,
539 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800540{
541 // sd_bus error
542 ipmi_ret_t rc = IPMI_CC_OK;
543
544 ipmi_chassis_cap_t chassis_cap{};
545
Yong Liae4b0402018-11-02 11:12:14 +0800546 if (*data_len != 0)
547 {
548 return IPMI_CC_REQ_DATA_LEN_INVALID;
549 }
550
Nan Li8d15fb42016-08-16 22:29:40 +0800551 *data_len = sizeof(ipmi_chassis_cap_t);
552
Yong Liae4b0402018-11-02 11:12:14 +0800553 try
554 {
555 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800556
Yong Liae4b0402018-11-02 11:12:14 +0800557 ipmi::DbusObjectInfo chassisCapObject =
558 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800559
Yong Liae4b0402018-11-02 11:12:14 +0800560 // capabilities flags
561 // [7..4] - reserved
562 // [3] – 1b = provides power interlock (IPM 1.5)
563 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
564 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
565 // has capabilities
566 // to lock out external power control and reset button or
567 // front panel interfaces and/or detect tampering with those
568 // interfaces).
569 // [0] -1b = Chassis provides intrusion (physical security) sensor.
570 // set to default value 0x0.
571 ipmi::Value variant = ipmi::getDbusProperty(
572 bus, chassisCapObject.second, chassisCapObject.first,
573 chassisCapIntf, chassisCapFlagsProp);
574 chassis_cap.cap_flags = 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 chassisFRUDevAddrProp);
579 // Chassis FRU info Device Address.
580 chassis_cap.fru_info_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 chassisSDRDevAddrProp);
585 // Chassis SDR Device Address.
586 chassis_cap.sdr_dev_addr = 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 chassisSELDevAddrProp);
591 // Chassis SEL Device Address.
592 chassis_cap.sel_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800593
Yong Liae4b0402018-11-02 11:12:14 +0800594 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
595 chassisCapObject.first, chassisCapIntf,
596 chassisSMDevAddrProp);
597 // Chassis System Management Device Address.
598 chassis_cap.system_management_dev_addr =
599 variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800600
Yong Liae4b0402018-11-02 11:12:14 +0800601 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
602 chassisCapObject.first, chassisCapIntf,
603 chassisBridgeDevAddrProp);
604 // Chassis Bridge Device Address.
605 chassis_cap.bridge_dev_addr = variant_ns::get<uint8_t>(variant);
606 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
607 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
608 std::copy(chassisP, chassisP + *data_len, respP);
609 }
610 catch (std::exception& e)
611 {
612 log<level::ERR>(e.what());
613 rc = IPMI_CC_UNSPECIFIED_ERROR;
614 *data_len = 0;
615 return rc;
616 }
617
618 return rc;
619}
620
621ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
622 ipmi_request_t request,
623 ipmi_response_t response,
624 ipmi_data_len_t data_len,
625 ipmi_context_t context)
626{
627 ipmi_ret_t rc = IPMI_CC_OK;
628
629 if (*data_len != sizeof(ipmi_chassis_cap_t))
630 {
631 log<level::ERR>("Unsupported request length",
632 entry("LEN=0x%x", *data_len));
633 *data_len = 0;
634 return IPMI_CC_REQ_DATA_LEN_INVALID;
635 }
636
637 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
638
639 *data_len = 0;
640
641 // check input data
642 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
643 {
644 log<level::ERR>("Unsupported request parameter(CAP Flags)",
645 entry("REQ=0x%x", chassisCap->cap_flags));
646 return IPMI_CC_INVALID_FIELD_REQUEST;
647 }
648
649 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
650 {
651 log<level::ERR>("Unsupported request parameter(FRU Addr)",
652 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
653 return IPMI_CC_INVALID_FIELD_REQUEST;
654 }
655
656 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
657 {
658 log<level::ERR>("Unsupported request parameter(SDR Addr)",
659 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
660 return IPMI_CC_INVALID_FIELD_REQUEST;
661 }
662
663 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
664 {
665 log<level::ERR>("Unsupported request parameter(SEL Addr)",
666 entry("REQ=0x%x", chassisCap->sel_dev_addr));
667 return IPMI_CC_INVALID_FIELD_REQUEST;
668 }
669
670 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
671 {
672 log<level::ERR>(
673 "Unsupported request parameter(SM Addr)",
674 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
675 return IPMI_CC_INVALID_FIELD_REQUEST;
676 }
677
678 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
679 {
680 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
681 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
682 return IPMI_CC_INVALID_FIELD_REQUEST;
683 }
684
685 try
686 {
687 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
688 ipmi::DbusObjectInfo chassisCapObject =
689 ipmi::getDbusObject(bus, chassisCapIntf);
690
691 ipmi::setDbusProperty(bus, chassisCapObject.second,
692 chassisCapObject.first, chassisCapIntf,
693 chassisCapFlagsProp, chassisCap->cap_flags);
694
695 ipmi::setDbusProperty(bus, chassisCapObject.second,
696 chassisCapObject.first, chassisCapIntf,
697 chassisFRUDevAddrProp,
698 chassisCap->fru_info_dev_addr);
699
700 ipmi::setDbusProperty(bus, chassisCapObject.second,
701 chassisCapObject.first, chassisCapIntf,
702 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
703
704 ipmi::setDbusProperty(bus, chassisCapObject.second,
705 chassisCapObject.first, chassisCapIntf,
706 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
707
708 ipmi::setDbusProperty(bus, chassisCapObject.second,
709 chassisCapObject.first, chassisCapIntf,
710 chassisSMDevAddrProp,
711 chassisCap->system_management_dev_addr);
712
713 ipmi::setDbusProperty(bus, chassisCapObject.second,
714 chassisCapObject.first, chassisCapIntf,
715 chassisBridgeDevAddrProp,
716 chassisCap->bridge_dev_addr);
717 }
718 catch (std::exception& e)
719 {
720 log<level::ERR>(e.what());
721 rc = IPMI_CC_UNSPECIFIED_ERROR;
722 return rc;
723 }
Nan Li8d15fb42016-08-16 22:29:40 +0800724
725 return rc;
726}
727
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530728//------------------------------------------
729// Calls into Host State Manager Dbus object
730//------------------------------------------
731int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600732{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500733 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700734 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500735 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700736 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
737 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530738
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500739 // sd_bus error
740 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700741 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600742
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500743 // SD Bus error report mechanism.
744 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600745
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500746 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700747 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500748 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
749 if (rc < 0)
750 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700751 log<level::ERR>(
752 "Failed to get bus name",
753 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500754 return rc;
755 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530756
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500757 // Convert to string equivalent of the passed in transition enum.
758 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530759
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500760 rc = sd_bus_call_method(bus_type, // On the system bus
761 busname, // Service to contact
762 HOST_STATE_MANAGER_ROOT, // Object path
763 DBUS_PROPERTY_IFACE, // Interface name
764 "Set", // Method to be called
765 &bus_error, // object to return error
766 nullptr, // Response buffer if any
767 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700768 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
769 request.c_str());
770 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500771 {
772 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530773 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500774 }
775 else
776 {
777 log<level::INFO>("Transition request initiated successfully");
778 }
vishwa36993272015-11-20 12:43:49 -0600779
780 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500781 free(busname);
vishwa36993272015-11-20 12:43:49 -0600782
Sergey Solomineb9b8142016-08-23 09:07:28 -0500783 return rc;
vishwa36993272015-11-20 12:43:49 -0600784}
785
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500786namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500787{
Nan Lifdd8ec52016-08-28 03:57:40 +0800788
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500789using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
790using IpmiValue = uint8_t;
791using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800792
Patrick Venture0b02be92018-08-31 11:55:55 -0700793std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500794 {RestorePolicy::Policy::AlwaysOff, 0x00},
795 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700796 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800797
Yong Lic6713cf2018-09-12 12:35:13 +0800798static constexpr uint8_t noChange = 0x03;
799static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
800static constexpr uint8_t policyBitMask = 0x07;
801static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500802} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800803
804//----------------------------------------------------------------------
805// Get Chassis Status commands
806//----------------------------------------------------------------------
807ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500808 ipmi_request_t request,
809 ipmi_response_t response,
810 ipmi_data_len_t data_len,
811 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800812{
Patrick Venture0b02be92018-08-31 11:55:55 -0700813 const char* objname = "/org/openbmc/control/power0";
814 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800815
Patrick Venture0b02be92018-08-31 11:55:55 -0700816 sd_bus* bus = NULL;
817 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800818 int r = 0;
819 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700820 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800821 ipmi_ret_t rc = IPMI_CC_OK;
822 ipmi_get_chassis_status_t chassis_status{};
823
Nan Lifdd8ec52016-08-28 03:57:40 +0800824 uint8_t s = 0;
825
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500826 using namespace chassis::internal;
827 using namespace chassis::internal::cache;
828 using namespace power_policy;
829
Deepak Kodihallie6027092017-08-27 08:13:37 -0500830 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700831 auto method = dbus.new_method_call(
832 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
833 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500834 method.append(powerRestoreIntf, "PowerRestorePolicy");
835 auto resp = dbus.call(method);
836 if (resp.is_method_error())
837 {
838 log<level::ERR>("Error in PowerRestorePolicy Get");
839 report<InternalFailure>();
840 *data_len = 0;
841 return IPMI_CC_UNSPECIFIED_ERROR;
842 }
843 sdbusplus::message::variant<std::string> result;
844 resp.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -0700845 auto powerRestore = RestorePolicy::convertPolicyFromString(
846 variant_ns::get<std::string>(result));
Nan Lifdd8ec52016-08-28 03:57:40 +0800847
848 *data_len = 4;
849
Tom Joseph63a00512017-08-09 23:39:59 +0530850 bus = ipmid_get_sd_bus_connection();
851
Nan Lifdd8ec52016-08-28 03:57:40 +0800852 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700853 if (r < 0)
854 {
855 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800856 rc = IPMI_CC_UNSPECIFIED_ERROR;
857 goto finish;
858 }
859
Patrick Venture0b02be92018-08-31 11:55:55 -0700860 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
861 "i");
862 if (r < 0)
863 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530864 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700865 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
866 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530867 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800868 rc = IPMI_CC_UNSPECIFIED_ERROR;
869 goto finish;
870 }
871
872 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700873 if (r < 0)
874 {
875 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800876 rc = IPMI_CC_UNSPECIFIED_ERROR;
877 goto finish;
878 }
879
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500880 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800881
882 // Current Power State
883 // [7] reserved
884 // [6..5] power restore policy
885 // 00b = chassis stays powered off after AC/mains returns
886 // 01b = after AC returns, power is restored to the state that was
887 // in effect when AC/mains was lost.
888 // 10b = chassis always powers up after AC/mains returns
889 // 11b = unknow
890 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700891 // Do we need to define a dbus property to identify the restore
892 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800893
894 // [4] power control fault
895 // 1b = controller attempted to turn system power on or off, but
896 // system did not enter desired state.
897 // Set to 0b, since We don't support it..
898
899 // [3] power fault
900 // 1b = fault detected in main power subsystem.
901 // set to 0b. for we don't support it.
902
903 // [2] 1b = interlock (chassis is presently shut down because a chassis
904 // panel interlock switch is active). (IPMI 1.5)
905 // set to 0b, for we don't support it.
906
907 // [1] power overload
908 // 1b = system shutdown because of power overload condition.
909 // set to 0b, for we don't support it.
910
911 // [0] power is on
912 // 1b = system power is on
913 // 0b = system power is off(soft-off S4/S5, or mechanical off)
914
Patrick Venture0b02be92018-08-31 11:55:55 -0700915 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800916
917 // Last Power Event
918 // [7..5] – reserved
919 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
920 // [3] – 1b = last power down caused by power fault
921 // [2] – 1b = last power down caused by a power interlock being activated
922 // [1] – 1b = last power down caused by a Power overload
923 // [0] – 1b = AC failed
924 // set to 0x0, for we don't support these fields.
925
926 chassis_status.last_power_event = 0;
927
928 // Misc. Chassis State
929 // [7] – reserved
930 // [6] – 1b = Chassis Identify command and state info supported (Optional)
931 // 0b = Chassis Identify command support unspecified via this command.
932 // (The Get Command Support command , if implemented, would still
933 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700934 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
935 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800936 // as 00b) otherwise. Returns the present chassis identify state.
937 // Refer to the Chassis Identify command for more info.
938 // 00b = chassis identify state = Off
939 // 01b = chassis identify state = Temporary(timed) On
940 // 10b = chassis identify state = Indefinite On
941 // 11b = reserved
942 // [3] – 1b = Cooling/fan fault detected
943 // [2] – 1b = Drive Fault
944 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
945 // push-buttons disabled.)
946 // [0] – 1b = Chassis Intrusion active
947 // set to 0, for we don't support them.
948 chassis_status.misc_power_state = 0;
949
950 // Front Panel Button Capabilities and disable/enable status(Optional)
951 // set to 0, for we don't support them.
952 chassis_status.front_panel_button_cap_status = 0;
953
954 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700955 std::memcpy(response, &chassis_status, *data_len);
Nan Lifdd8ec52016-08-28 03:57:40 +0800956
957finish:
958 free(busname);
959 reply = sd_bus_message_unref(reply);
960
961 return rc;
962}
Chris Austen7888c4d2015-12-03 15:26:20 -0600963
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530964//-------------------------------------------------------------
965// Send a command to SoftPowerOff application to stop any timer
966//-------------------------------------------------------------
967int stop_soft_off_timer()
968{
Patrick Venture0b02be92018-08-31 11:55:55 -0700969 constexpr auto iface = "org.freedesktop.DBus.Properties";
970 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
971 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530972
Patrick Venture0b02be92018-08-31 11:55:55 -0700973 constexpr auto property = "ResponseReceived";
974 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
975 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530976
977 // Get the system bus where most system services are provided.
978 auto bus = ipmid_get_sd_bus_connection();
979
980 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500981 // TODO openbmc/openbmc#1661 - Mapper refactor
982 //
983 // See openbmc/openbmc#1743 for some details but high level summary is that
984 // for now the code will directly call the soft off interface due to a
985 // race condition with mapper usage
986 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700987 // char *busname = nullptr;
988 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
989 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500990 //{
991 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530992 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500993 // return r;
994 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530995
996 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500997 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700998 "Set", nullptr, nullptr, "ssv", soft_off_iface,
999 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301000 if (rc < 0)
1001 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301002 log<level::ERR>("Failed to set property in SoftPowerOff object",
1003 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301004 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001005
Patrick Venture0b02be92018-08-31 11:55:55 -07001006 // TODO openbmc/openbmc#1661 - Mapper refactor
1007 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301008 return rc;
1009}
1010
vishwa36993272015-11-20 12:43:49 -06001011//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001012// Create file to indicate there is no need for softoff notification to host
1013//----------------------------------------------------------------------
1014void indicate_no_softoff_needed()
1015{
1016 fs::path path{HOST_INBAND_REQUEST_DIR};
1017 if (!fs::is_directory(path))
1018 {
1019 fs::create_directory(path);
1020 }
1021
1022 // Add the host instance (default 0 for now) to the file name
1023 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001024 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001025 size++; // null
1026 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001027 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001028
1029 // Append file name to directory and create it
1030 path /= buf.get();
1031 std::ofstream(path.c_str());
1032}
1033
1034//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001035// Chassis Control commands
1036//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001037ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1038 ipmi_request_t request,
1039 ipmi_response_t response,
1040 ipmi_data_len_t data_len,
1041 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001042{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001043 // Error from power off.
1044 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001045
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001046 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001047 *data_len = 0;
1048
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001049 // Catch the actual operaton by peeking into request buffer
Patrick Venture0b02be92018-08-31 11:55:55 -07001050 uint8_t chassis_ctrl_cmd = *(uint8_t*)request;
vishwa36993272015-11-20 12:43:49 -06001051
Patrick Venture0b02be92018-08-31 11:55:55 -07001052 switch (chassis_ctrl_cmd)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001053 {
1054 case CMD_POWER_ON:
1055 rc = initiate_state_transition(State::Host::Transition::On);
1056 break;
1057 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301058 // This path would be hit in 2 conditions.
1059 // 1: When user asks for power off using ipmi chassis command 0x04
1060 // 2: Host asking for power off post shutting down.
1061
1062 // If it's a host requested power off, then need to nudge Softoff
1063 // application that it needs to stop the watchdog timer if running.
1064 // If it is a user requested power off, then this is not really
1065 // needed. But then we need to differentiate between user and host
1066 // calling this same command
1067
1068 // For now, we are going ahead with trying to nudge the soft off and
1069 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001070 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301071
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001072 // Only request the Off transition if the soft power off
1073 // application is not running
1074 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001075 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001076 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301077 // that it should not run. Not doing this will result in State
1078 // manager doing a default soft power off when asked for power
1079 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001080 indicate_no_softoff_needed();
1081
1082 // Now request the shutdown
1083 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001084 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001085 else
1086 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301087 log<level::INFO>("Soft off is running, so let shutdown target "
1088 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001089 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001090 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301091
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001092 case CMD_HARD_RESET:
1093 case CMD_POWER_CYCLE:
1094 // SPEC has a section that says certain implementations can trigger
1095 // PowerOn if power is Off when a command to power cycle is
1096 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001097
1098 // First create a file to indicate to the soft off application
1099 // that it should not run since this is a direct user initiated
1100 // power reboot request (i.e. a reboot request that is not
1101 // originating via a soft power off SMS request)
1102 indicate_no_softoff_needed();
1103
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001104 rc = initiate_state_transition(State::Host::Transition::Reboot);
1105 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301106
1107 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1108 // Request Host State Manager to do a soft power off
1109 rc = initiate_state_transition(State::Host::Transition::Off);
1110 break;
1111
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001112 default:
1113 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301114 log<level::ERR>("Invalid Chassis Control command",
1115 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001116 rc = -1;
1117 }
1118 }
vishwa36993272015-11-20 12:43:49 -06001119
Patrick Venture0b02be92018-08-31 11:55:55 -07001120 return ((rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001121}
1122
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001123/** @brief Return D-Bus connection string to enclosure identify LED object
1124 *
1125 * @param[in, out] connection - connection to D-Bus object
1126 * @return a IPMI return code
1127 */
1128std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301129{
Tom Joseph5110c122018-03-23 17:55:40 +05301130 // lookup enclosure_identify group owner(s) in mapper
1131 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001132 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1133 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301134
1135 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001136 static const std::vector<std::string> interfaces = {
1137 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301138 mapperCall.append(interfaces);
1139 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1140 if (mapperReply.is_method_error())
1141 {
1142 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001143 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301144 }
1145 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1146 mapperReply.read(mapperResp);
1147
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001148 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301149 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001150 log<level::ERR>(
1151 "Invalid number of enclosure identify objects.",
1152 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001153 elog<InternalFailure>();
1154 }
1155 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1156 return pair.first;
1157}
Tom Joseph5110c122018-03-23 17:55:40 +05301158
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001159/** @brief Turn On/Off enclosure identify LED
1160 *
1161 * @param[in] flag - true to turn on LED, false to turn off
1162 * @return a IPMI return code
1163 */
1164void enclosureIdentifyLed(bool flag)
1165{
1166 using namespace chassis::internal;
1167 std::string connection = std::move(getEnclosureIdentifyConnection());
Patrick Venture0b02be92018-08-31 11:55:55 -07001168 auto led =
1169 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1170 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001171 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001172 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001173 auto ledReply = dbus.call(led);
1174 if (ledReply.is_method_error())
1175 {
1176 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001177 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001178 elog<InternalFailure>();
1179 }
1180}
1181
1182/** @brief Callback method to turn off LED
1183 */
1184void enclosureIdentifyLedOff()
1185{
1186 try
1187 {
1188 enclosureIdentifyLed(false);
1189 }
1190 catch (const InternalFailure& e)
1191 {
1192 report<InternalFailure>();
1193 }
1194}
1195
1196/** @brief Create timer to turn on and off the enclosure LED
1197 */
1198void createIdentifyTimer()
1199{
1200 if (!identifyTimer)
1201 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001202 identifyTimer =
1203 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001204 }
1205}
1206
1207ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1208 ipmi_request_t request,
1209 ipmi_response_t response,
1210 ipmi_data_len_t data_len,
1211 ipmi_context_t context)
1212{
1213 if (*data_len > chassisIdentifyReqLength)
1214 {
1215 return IPMI_CC_REQ_DATA_LEN_INVALID;
1216 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001217 uint8_t identifyInterval =
1218 *data_len > identifyIntervalPos
1219 ? (static_cast<uint8_t*>(request))[identifyIntervalPos]
1220 : DEFAULT_IDENTIFY_TIME_OUT;
1221 bool forceIdentify =
1222 (*data_len == chassisIdentifyReqLength)
1223 ? (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01
1224 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301225
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001226 if (identifyInterval || forceIdentify)
1227 {
1228 // stop the timer if already started, for force identify we should
1229 // not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001230 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001231 try
Tom Joseph5110c122018-03-23 17:55:40 +05301232 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001233 enclosureIdentifyLed(true);
1234 }
1235 catch (const InternalFailure& e)
1236 {
1237 report<InternalFailure>();
1238 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301239 }
1240
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001241 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301242 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001243 return IPMI_CC_OK;
1244 }
1245 // start the timer
1246 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001247 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001248 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301249 }
Tom Josephbed26992018-07-31 23:00:24 +05301250 else if (!identifyInterval)
1251 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001252 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301253 enclosureIdentifyLedOff();
1254 }
Tom Joseph5110c122018-03-23 17:55:40 +05301255 return IPMI_CC_OK;
1256}
1257
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001258namespace boot_options
1259{
1260
1261using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1262using IpmiValue = uint8_t;
1263constexpr auto ipmiDefault = 0;
1264
Patrick Venture0b02be92018-08-31 11:55:55 -07001265std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001266 {0x01, Source::Sources::Network},
1267 {0x02, Source::Sources::Disk},
1268 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001269 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001270
Patrick Venture0b02be92018-08-31 11:55:55 -07001271std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001272 {0x03, Mode::Modes::Safe},
1273 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001274 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001275
Patrick Venture0b02be92018-08-31 11:55:55 -07001276std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001277 {Source::Sources::Network, 0x01},
1278 {Source::Sources::Disk, 0x02},
1279 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001280 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001281
Patrick Venture0b02be92018-08-31 11:55:55 -07001282std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001283 {Mode::Modes::Safe, 0x03},
1284 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001285 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001286
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001287} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001288
Marri Devender Rao81719702018-05-07 00:53:48 -05001289/** @brief Set the property value for boot source
1290 * @param[in] source - boot source value
1291 * @return On failure return IPMI error.
1292 */
1293static ipmi_ret_t setBootSource(const Source::Sources& source)
1294{
1295 using namespace chassis::internal;
1296 using namespace chassis::internal::cache;
1297 sdbusplus::message::variant<std::string> property =
1298 convertForMessage(source);
1299 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1300 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001301 auto method = dbus.new_method_call(
1302 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1303 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001304 method.append(bootSourceIntf, "BootSource", property);
1305 auto reply = dbus.call(method);
1306 if (reply.is_method_error())
1307 {
1308 log<level::ERR>("Error in BootSource Set");
1309 report<InternalFailure>();
1310 return IPMI_CC_UNSPECIFIED_ERROR;
1311 }
1312 return IPMI_CC_OK;
1313}
1314
Patrick Venture0b02be92018-08-31 11:55:55 -07001315/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001316 * @param[in] mode - boot mode value
1317 * @return On failure return IPMI error.
1318 */
1319static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1320{
1321 using namespace chassis::internal;
1322 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001323 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001324 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1325 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001326 auto method = dbus.new_method_call(
1327 objects.service(bootModeSetting, bootModeIntf).c_str(),
1328 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001329 method.append(bootModeIntf, "BootMode", property);
1330 auto reply = dbus.call(method);
1331 if (reply.is_method_error())
1332 {
1333 log<level::ERR>("Error in BootMode Set");
1334 report<InternalFailure>();
1335 return IPMI_CC_UNSPECIFIED_ERROR;
1336 }
1337 return IPMI_CC_OK;
1338}
1339
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001340ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1341 ipmi_request_t request,
1342 ipmi_response_t response,
1343 ipmi_data_len_t data_len,
1344 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001345{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001346 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001347 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001348 char* p = NULL;
1349 get_sys_boot_options_response_t* resp =
1350 (get_sys_boot_options_response_t*)response;
1351 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001352 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001353
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001354 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001355 resp->version = SET_PARM_VERSION;
1356 resp->parm = 5;
1357 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001358
shgoupfd84fbbf2015-12-17 10:05:51 +08001359 /*
1360 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1361 * This is the only parameter used by petitboot.
1362 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001363 if (reqptr->parameter ==
1364 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1365 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001366
Ratan Guptafd28dd72016-08-01 04:58:01 -05001367 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001368 using namespace chassis::internal;
1369 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001370
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001371 try
ratagupta6f6bff2016-04-04 06:20:11 -05001372 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001373 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1374 const auto& bootSourceSetting =
1375 std::get<settings::Path>(bootSetting);
1376 auto oneTimeEnabled =
1377 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001378 auto method = dbus.new_method_call(
1379 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1380 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001381 method.append(bootSourceIntf, "BootSource");
1382 auto reply = dbus.call(method);
1383 if (reply.is_method_error())
1384 {
1385 log<level::ERR>("Error in BootSource Get");
1386 report<InternalFailure>();
1387 *data_len = 0;
1388 return IPMI_CC_UNSPECIFIED_ERROR;
1389 }
1390 sdbusplus::message::variant<std::string> result;
1391 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001392 auto bootSource = Source::convertSourcesFromString(
1393 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001394
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001395 bootSetting = settings::boot::setting(objects, bootModeIntf);
1396 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1397 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001398 objects.service(bootModeSetting, bootModeIntf).c_str(),
1399 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001400 method.append(bootModeIntf, "BootMode");
1401 reply = dbus.call(method);
1402 if (reply.is_method_error())
1403 {
1404 log<level::ERR>("Error in BootMode Get");
1405 report<InternalFailure>();
1406 *data_len = 0;
1407 return IPMI_CC_UNSPECIFIED_ERROR;
1408 }
1409 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001410 auto bootMode = Mode::convertModesFromString(
1411 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001412
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001413 bootOption = sourceDbusToIpmi.at(bootSource);
1414 if ((Mode::Modes::Regular == bootMode) &&
1415 (Source::Sources::Default == bootSource))
1416 {
1417 bootOption = ipmiDefault;
1418 }
1419 else if (Source::Sources::Default == bootSource)
1420 {
1421 bootOption = modeDbusToIpmi.at(bootMode);
1422 }
1423 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001424
Patrick Venture0b02be92018-08-31 11:55:55 -07001425 resp->data[0] = oneTimeEnabled
1426 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1427 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001428
ratagupta6f6bff2016-04-04 06:20:11 -05001429 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001430 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001431 catch (InternalFailure& e)
1432 {
1433 report<InternalFailure>();
1434 *data_len = 0;
1435 return IPMI_CC_UNSPECIFIED_ERROR;
1436 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001437 }
1438 else if (reqptr->parameter ==
1439 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1440 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001441
Patrick Venture0b02be92018-08-31 11:55:55 -07001442 *data_len =
1443 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001444
Patrick Venture0b02be92018-08-31 11:55:55 -07001445 resp->parm =
1446 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001447
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001448 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001449
Patrick Venture0b02be92018-08-31 11:55:55 -07001450 if (ret < 0)
1451 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001452
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301453 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001454 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001455 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001456 }
1457 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001458 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001459 }
1460
Patrick Venture0b02be92018-08-31 11:55:55 -07001461 else
1462 {
1463 log<level::ERR>("Unsupported parameter",
1464 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001465 }
1466
1467 if (p)
1468 free(p);
1469
Ratan Guptafd28dd72016-08-01 04:58:01 -05001470 if (rc == IPMI_CC_OK)
1471 {
1472 *data_len += 2;
1473 }
1474
shgoupfd84fbbf2015-12-17 10:05:51 +08001475 return rc;
1476}
1477
shgoupfd84fbbf2015-12-17 10:05:51 +08001478ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001479 ipmi_request_t request,
1480 ipmi_response_t response,
1481 ipmi_data_len_t data_len,
1482 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001483{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001484 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001485 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001486 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001487
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001488 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1489 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001490
shgoupfd84fbbf2015-12-17 10:05:51 +08001491 // This IPMI command does not have any resposne data
1492 *data_len = 0;
1493
1494 /* 000101
1495 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1496 * This is the only parameter used by petitboot.
1497 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001498
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001499 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1500 {
1501 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1502 using namespace chassis::internal;
1503 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001504 auto oneTimeEnabled = false;
1505 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301506 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001507 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001508
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001509 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001510 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001511 bool permanent =
1512 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1513 SET_PARM_BOOT_FLAGS_PERMANENT;
1514
Patrick Venture0b02be92018-08-31 11:55:55 -07001515 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301516
1517 oneTimeEnabled =
1518 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1519
1520 /*
1521 * Check if the current boot setting is onetime or permanent, if the
1522 * request in the command is otherwise, then set the "Enabled"
1523 * property in one_time object path to 'True' to indicate onetime
1524 * and 'False' to indicate permanent.
1525 *
1526 * Once the onetime/permanent setting is applied, then the bootMode
1527 * and bootSource is updated for the corresponding object.
1528 */
1529 if ((permanent && oneTimeEnabled) ||
1530 (!permanent && !oneTimeEnabled))
1531 {
1532 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1533
Patrick Venture0b02be92018-08-31 11:55:55 -07001534 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1535 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301536 }
1537
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001538 auto modeItr = modeIpmiToDbus.find(bootOption);
1539 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1540 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001541 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001542 rc = setBootSource(sourceItr->second);
1543 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001544 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001545 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001546 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001547 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001548 // If a set boot device is mapping to a boot source, then reset
1549 // the boot mode D-Bus property to default.
1550 // This way the ipmid code can determine which property is not
1551 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001552 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001553 {
1554 setBootMode(Mode::Modes::Regular);
1555 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001556 }
1557 if (modeIpmiToDbus.end() != modeItr)
1558 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001559 rc = setBootMode(modeItr->second);
1560 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001561 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001562 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001563 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001564 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001565 // If a set boot device is mapping to a boot mode, then reset
1566 // the boot source D-Bus property to default.
1567 // This way the ipmid code can determine which property is not
1568 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001569 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001570 {
1571 setBootSource(Source::Sources::Default);
1572 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001573 }
1574 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001575 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001576 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001577 report<InternalFailure>();
1578 *data_len = 0;
1579 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001580 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001581 }
1582 else if (reqptr->parameter ==
1583 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1584 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001585
1586 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001587 if (ret < 0)
1588 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301589 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001590 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001591 rc = IPMI_CC_UNSPECIFIED_ERROR;
1592 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001593 }
1594 else if (reqptr->parameter ==
1595 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1596 {
Tom Josephf536c902017-09-25 18:08:15 +05301597 // Handle parameter #4 and return command completed normally
1598 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1599 // parameter. This is added to support the ipmitool command `chassis
1600 // bootdev` which sends set on parameter #4, before setting the boot
1601 // flags.
1602 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001603 }
1604 else
1605 {
1606 log<level::ERR>("Unsupported parameter",
1607 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001608 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001609 }
1610
1611 return rc;
1612}
1613
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001614ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1615 ipmi_request_t request, ipmi_response_t response,
1616 ipmi_data_len_t data_len, ipmi_context_t context)
1617{
1618 // sd_bus error
1619 ipmi_ret_t rc = IPMI_CC_OK;
1620
1621 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1622
1623 try
1624 {
1625 auto pohCounter = getPOHCounter();
1626 resptr->counterReading[0] = pohCounter;
1627 resptr->counterReading[1] = pohCounter >> 8;
1628 resptr->counterReading[2] = pohCounter >> 16;
1629 resptr->counterReading[3] = pohCounter >> 24;
1630 }
1631 catch (std::exception& e)
1632 {
1633 log<level::ERR>(e.what());
1634 return IPMI_CC_UNSPECIFIED_ERROR;
1635 }
1636
1637 resptr->minPerCount = poh::minutesPerCount;
1638 *data_len = sizeof(GetPOHCountResponse);
1639
1640 return rc;
1641}
1642
Yong Lic6713cf2018-09-12 12:35:13 +08001643ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1644 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1645 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1646{
1647 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1648 auto* resptr = reinterpret_cast<uint8_t*>(response);
1649 uint8_t reqPolicy = 0;
1650
1651 power_policy::DbusValue value =
1652 power_policy::RestorePolicy::Policy::AlwaysOff;
1653
1654 if (*data_len != power_policy::setPolicyReqLen)
1655 {
1656 phosphor::logging::log<level::ERR>("Unsupported request length",
1657 entry("LEN=0x%x", *data_len));
1658 *data_len = 0;
1659 return IPMI_CC_REQ_DATA_LEN_INVALID;
1660 }
1661
Yong Licb89c0e2019-01-18 17:54:32 +08001662 if (*reqptr > power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001663 {
1664 phosphor::logging::log<level::ERR>("Reserved request parameter",
Yong Licb89c0e2019-01-18 17:54:32 +08001665 entry("REQ=0x%x", *reqptr));
Yong Lic6713cf2018-09-12 12:35:13 +08001666 *data_len = 0;
Yong Licb89c0e2019-01-18 17:54:32 +08001667 return IPMI_CC_PARM_OUT_OF_RANGE;
Yong Lic6713cf2018-09-12 12:35:13 +08001668 }
1669
Yong Licb89c0e2019-01-18 17:54:32 +08001670 reqPolicy = *reqptr & power_policy::policyBitMask;
Yong Lic6713cf2018-09-12 12:35:13 +08001671 if (reqPolicy == power_policy::noChange)
1672 {
1673 // just return the supported policy
1674 *resptr = power_policy::allSupport;
1675 *data_len = power_policy::setPolicyReqLen;
1676 return IPMI_CC_OK;
1677 }
1678
1679 for (auto const& it : power_policy::dbusToIpmi)
1680 {
1681 if (it.second == reqPolicy)
1682 {
1683 value = it.first;
1684 break;
1685 }
1686 }
1687
1688 try
1689 {
1690 const settings::Path& powerRestoreSetting =
1691 chassis::internal::cache::objects.map
1692 .at(chassis::internal::powerRestoreIntf)
1693 .front();
1694 sdbusplus::message::variant<std::string> property =
1695 convertForMessage(value);
1696
1697 auto method = chassis::internal::dbus.new_method_call(
1698 chassis::internal::cache::objects
1699 .service(powerRestoreSetting,
1700 chassis::internal::powerRestoreIntf)
1701 .c_str(),
1702 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1703
1704 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1705 property);
1706 auto reply = chassis::internal::dbus.call(method);
1707 if (reply.is_method_error())
1708 {
1709 phosphor::logging::log<level::ERR>("Unspecified Error");
1710 *data_len = 0;
1711 return IPMI_CC_UNSPECIFIED_ERROR;
1712 }
1713 }
1714 catch (InternalFailure& e)
1715 {
1716 report<InternalFailure>();
1717 *data_len = 0;
1718 return IPMI_CC_UNSPECIFIED_ERROR;
1719 }
1720
Yong Licb89c0e2019-01-18 17:54:32 +08001721 *resptr = power_policy::allSupport;
Yong Lic6713cf2018-09-12 12:35:13 +08001722 *data_len = power_policy::setPolicyReqLen;
1723 return IPMI_CC_OK;
1724}
1725
Adriana Kobylak40814c62015-10-27 15:58:44 -05001726void register_netfn_chassis_functions()
1727{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001728 createIdentifyTimer();
1729
Tom05732372016-09-06 17:21:23 +05301730 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001731 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1732 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001733
Tom05732372016-09-06 17:21:23 +05301734 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001735 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1736 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001737
Yong Liae4b0402018-11-02 11:12:14 +08001738 // Set Chassis Capabilities
1739 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1740 ipmi_set_chassis_cap, PRIVILEGE_USER);
1741
Tom05732372016-09-06 17:21:23 +05301742 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301743 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001744 ipmi_chassis_get_sys_boot_options,
1745 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001746
Tom05732372016-09-06 17:21:23 +05301747 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001748 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1749 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001750
Tom05732372016-09-06 17:21:23 +05301751 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001752 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1753 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001754
Tom Joseph5110c122018-03-23 17:55:40 +05301755 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301756 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1757 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1758
Tom05732372016-09-06 17:21:23 +05301759 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301760 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001761 ipmi_chassis_set_sys_boot_options,
1762 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001763 // <Get POH Counter>
1764 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1765 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001766
1767 // <Set Power Restore Policy>
1768 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1769 ipmi_chassis_set_power_restore_policy,
1770 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001771}