blob: c95565816d844f54229b7b7d421b9b16ecf08811 [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
Jia, Chunhui7cf145f2019-02-28 14:14:27 +08001226 *data_len = 0; // response have complete code only
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001227 if (identifyInterval || forceIdentify)
1228 {
1229 // stop the timer if already started, for force identify we should
1230 // not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001231 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001232 try
Tom Joseph5110c122018-03-23 17:55:40 +05301233 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001234 enclosureIdentifyLed(true);
1235 }
1236 catch (const InternalFailure& e)
1237 {
1238 report<InternalFailure>();
1239 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301240 }
1241
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001242 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301243 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001244 return IPMI_CC_OK;
1245 }
1246 // start the timer
1247 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001248 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001249 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301250 }
Tom Josephbed26992018-07-31 23:00:24 +05301251 else if (!identifyInterval)
1252 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001253 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301254 enclosureIdentifyLedOff();
1255 }
Tom Joseph5110c122018-03-23 17:55:40 +05301256 return IPMI_CC_OK;
1257}
1258
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001259namespace boot_options
1260{
1261
1262using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1263using IpmiValue = uint8_t;
1264constexpr auto ipmiDefault = 0;
1265
Patrick Venture0b02be92018-08-31 11:55:55 -07001266std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001267 {0x01, Source::Sources::Network},
1268 {0x02, Source::Sources::Disk},
1269 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001270 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001271
Patrick Venture0b02be92018-08-31 11:55:55 -07001272std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001273 {0x03, Mode::Modes::Safe},
1274 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001275 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001276
Patrick Venture0b02be92018-08-31 11:55:55 -07001277std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001278 {Source::Sources::Network, 0x01},
1279 {Source::Sources::Disk, 0x02},
1280 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001281 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001282
Patrick Venture0b02be92018-08-31 11:55:55 -07001283std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001284 {Mode::Modes::Safe, 0x03},
1285 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001286 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001287
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001288} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001289
Marri Devender Rao81719702018-05-07 00:53:48 -05001290/** @brief Set the property value for boot source
1291 * @param[in] source - boot source value
1292 * @return On failure return IPMI error.
1293 */
1294static ipmi_ret_t setBootSource(const Source::Sources& source)
1295{
1296 using namespace chassis::internal;
1297 using namespace chassis::internal::cache;
1298 sdbusplus::message::variant<std::string> property =
1299 convertForMessage(source);
1300 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1301 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001302 auto method = dbus.new_method_call(
1303 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1304 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001305 method.append(bootSourceIntf, "BootSource", property);
1306 auto reply = dbus.call(method);
1307 if (reply.is_method_error())
1308 {
1309 log<level::ERR>("Error in BootSource Set");
1310 report<InternalFailure>();
1311 return IPMI_CC_UNSPECIFIED_ERROR;
1312 }
1313 return IPMI_CC_OK;
1314}
1315
Patrick Venture0b02be92018-08-31 11:55:55 -07001316/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001317 * @param[in] mode - boot mode value
1318 * @return On failure return IPMI error.
1319 */
1320static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1321{
1322 using namespace chassis::internal;
1323 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001324 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001325 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1326 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001327 auto method = dbus.new_method_call(
1328 objects.service(bootModeSetting, bootModeIntf).c_str(),
1329 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001330 method.append(bootModeIntf, "BootMode", property);
1331 auto reply = dbus.call(method);
1332 if (reply.is_method_error())
1333 {
1334 log<level::ERR>("Error in BootMode Set");
1335 report<InternalFailure>();
1336 return IPMI_CC_UNSPECIFIED_ERROR;
1337 }
1338 return IPMI_CC_OK;
1339}
1340
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001341ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1342 ipmi_request_t request,
1343 ipmi_response_t response,
1344 ipmi_data_len_t data_len,
1345 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001346{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001347 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001348 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001349 char* p = NULL;
1350 get_sys_boot_options_response_t* resp =
1351 (get_sys_boot_options_response_t*)response;
1352 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001353 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001354
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001355 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001356 resp->version = SET_PARM_VERSION;
1357 resp->parm = 5;
1358 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001359
shgoupfd84fbbf2015-12-17 10:05:51 +08001360 /*
1361 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1362 * This is the only parameter used by petitboot.
1363 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001364 if (reqptr->parameter ==
1365 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1366 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001367
Ratan Guptafd28dd72016-08-01 04:58:01 -05001368 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001369 using namespace chassis::internal;
1370 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001371
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001372 try
ratagupta6f6bff2016-04-04 06:20:11 -05001373 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001374 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1375 const auto& bootSourceSetting =
1376 std::get<settings::Path>(bootSetting);
1377 auto oneTimeEnabled =
1378 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001379 auto method = dbus.new_method_call(
1380 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1381 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001382 method.append(bootSourceIntf, "BootSource");
1383 auto reply = dbus.call(method);
1384 if (reply.is_method_error())
1385 {
1386 log<level::ERR>("Error in BootSource Get");
1387 report<InternalFailure>();
1388 *data_len = 0;
1389 return IPMI_CC_UNSPECIFIED_ERROR;
1390 }
1391 sdbusplus::message::variant<std::string> result;
1392 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001393 auto bootSource = Source::convertSourcesFromString(
1394 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001395
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001396 bootSetting = settings::boot::setting(objects, bootModeIntf);
1397 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1398 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001399 objects.service(bootModeSetting, bootModeIntf).c_str(),
1400 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001401 method.append(bootModeIntf, "BootMode");
1402 reply = dbus.call(method);
1403 if (reply.is_method_error())
1404 {
1405 log<level::ERR>("Error in BootMode Get");
1406 report<InternalFailure>();
1407 *data_len = 0;
1408 return IPMI_CC_UNSPECIFIED_ERROR;
1409 }
1410 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001411 auto bootMode = Mode::convertModesFromString(
1412 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001413
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001414 bootOption = sourceDbusToIpmi.at(bootSource);
1415 if ((Mode::Modes::Regular == bootMode) &&
1416 (Source::Sources::Default == bootSource))
1417 {
1418 bootOption = ipmiDefault;
1419 }
1420 else if (Source::Sources::Default == bootSource)
1421 {
1422 bootOption = modeDbusToIpmi.at(bootMode);
1423 }
1424 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001425
Patrick Venture0b02be92018-08-31 11:55:55 -07001426 resp->data[0] = oneTimeEnabled
1427 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1428 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001429
ratagupta6f6bff2016-04-04 06:20:11 -05001430 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001431 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001432 catch (InternalFailure& e)
1433 {
1434 report<InternalFailure>();
1435 *data_len = 0;
1436 return IPMI_CC_UNSPECIFIED_ERROR;
1437 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001438 }
1439 else if (reqptr->parameter ==
1440 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1441 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001442
Patrick Venture0b02be92018-08-31 11:55:55 -07001443 *data_len =
1444 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001445
Patrick Venture0b02be92018-08-31 11:55:55 -07001446 resp->parm =
1447 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001448
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001449 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001450
Patrick Venture0b02be92018-08-31 11:55:55 -07001451 if (ret < 0)
1452 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001453
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301454 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001455 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001456 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001457 }
1458 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001459 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001460 }
1461
Patrick Venture0b02be92018-08-31 11:55:55 -07001462 else
1463 {
1464 log<level::ERR>("Unsupported parameter",
1465 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001466 }
1467
1468 if (p)
1469 free(p);
1470
Ratan Guptafd28dd72016-08-01 04:58:01 -05001471 if (rc == IPMI_CC_OK)
1472 {
1473 *data_len += 2;
1474 }
1475
shgoupfd84fbbf2015-12-17 10:05:51 +08001476 return rc;
1477}
1478
shgoupfd84fbbf2015-12-17 10:05:51 +08001479ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001480 ipmi_request_t request,
1481 ipmi_response_t response,
1482 ipmi_data_len_t data_len,
1483 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001484{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001485 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001486 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001487 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001488
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001489 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1490 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001491
shgoupfd84fbbf2015-12-17 10:05:51 +08001492 // This IPMI command does not have any resposne data
1493 *data_len = 0;
1494
1495 /* 000101
1496 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1497 * This is the only parameter used by petitboot.
1498 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001499
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001500 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1501 {
1502 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1503 using namespace chassis::internal;
1504 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001505 auto oneTimeEnabled = false;
1506 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301507 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001508 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001509
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001510 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001511 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001512 bool permanent =
1513 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1514 SET_PARM_BOOT_FLAGS_PERMANENT;
1515
Patrick Venture0b02be92018-08-31 11:55:55 -07001516 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301517
1518 oneTimeEnabled =
1519 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1520
1521 /*
1522 * Check if the current boot setting is onetime or permanent, if the
1523 * request in the command is otherwise, then set the "Enabled"
1524 * property in one_time object path to 'True' to indicate onetime
1525 * and 'False' to indicate permanent.
1526 *
1527 * Once the onetime/permanent setting is applied, then the bootMode
1528 * and bootSource is updated for the corresponding object.
1529 */
1530 if ((permanent && oneTimeEnabled) ||
1531 (!permanent && !oneTimeEnabled))
1532 {
1533 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1534
Patrick Venture0b02be92018-08-31 11:55:55 -07001535 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1536 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301537 }
1538
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001539 auto modeItr = modeIpmiToDbus.find(bootOption);
1540 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1541 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001542 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001543 rc = setBootSource(sourceItr->second);
1544 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001545 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001546 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001547 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001548 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001549 // If a set boot device is mapping to a boot source, then reset
1550 // the boot mode D-Bus property to default.
1551 // This way the ipmid code can determine which property is not
1552 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001553 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001554 {
1555 setBootMode(Mode::Modes::Regular);
1556 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001557 }
1558 if (modeIpmiToDbus.end() != modeItr)
1559 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001560 rc = setBootMode(modeItr->second);
1561 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001562 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001563 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001564 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001565 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001566 // If a set boot device is mapping to a boot mode, then reset
1567 // the boot source D-Bus property to default.
1568 // This way the ipmid code can determine which property is not
1569 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001570 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001571 {
1572 setBootSource(Source::Sources::Default);
1573 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001574 }
1575 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001576 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001577 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001578 report<InternalFailure>();
1579 *data_len = 0;
1580 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001581 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001582 }
1583 else if (reqptr->parameter ==
1584 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1585 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001586
1587 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001588 if (ret < 0)
1589 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301590 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001591 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001592 rc = IPMI_CC_UNSPECIFIED_ERROR;
1593 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001594 }
1595 else if (reqptr->parameter ==
1596 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1597 {
Tom Josephf536c902017-09-25 18:08:15 +05301598 // Handle parameter #4 and return command completed normally
1599 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1600 // parameter. This is added to support the ipmitool command `chassis
1601 // bootdev` which sends set on parameter #4, before setting the boot
1602 // flags.
1603 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001604 }
1605 else
1606 {
1607 log<level::ERR>("Unsupported parameter",
1608 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001609 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001610 }
1611
1612 return rc;
1613}
1614
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001615ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1616 ipmi_request_t request, ipmi_response_t response,
1617 ipmi_data_len_t data_len, ipmi_context_t context)
1618{
1619 // sd_bus error
1620 ipmi_ret_t rc = IPMI_CC_OK;
1621
1622 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1623
1624 try
1625 {
1626 auto pohCounter = getPOHCounter();
1627 resptr->counterReading[0] = pohCounter;
1628 resptr->counterReading[1] = pohCounter >> 8;
1629 resptr->counterReading[2] = pohCounter >> 16;
1630 resptr->counterReading[3] = pohCounter >> 24;
1631 }
1632 catch (std::exception& e)
1633 {
1634 log<level::ERR>(e.what());
1635 return IPMI_CC_UNSPECIFIED_ERROR;
1636 }
1637
1638 resptr->minPerCount = poh::minutesPerCount;
1639 *data_len = sizeof(GetPOHCountResponse);
1640
1641 return rc;
1642}
1643
Yong Lic6713cf2018-09-12 12:35:13 +08001644ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1645 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1646 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1647{
1648 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1649 auto* resptr = reinterpret_cast<uint8_t*>(response);
1650 uint8_t reqPolicy = 0;
1651
1652 power_policy::DbusValue value =
1653 power_policy::RestorePolicy::Policy::AlwaysOff;
1654
1655 if (*data_len != power_policy::setPolicyReqLen)
1656 {
1657 phosphor::logging::log<level::ERR>("Unsupported request length",
1658 entry("LEN=0x%x", *data_len));
1659 *data_len = 0;
1660 return IPMI_CC_REQ_DATA_LEN_INVALID;
1661 }
1662
Yong Licb89c0e2019-01-18 17:54:32 +08001663 if (*reqptr > power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001664 {
1665 phosphor::logging::log<level::ERR>("Reserved request parameter",
Yong Licb89c0e2019-01-18 17:54:32 +08001666 entry("REQ=0x%x", *reqptr));
Yong Lic6713cf2018-09-12 12:35:13 +08001667 *data_len = 0;
Yong Licb89c0e2019-01-18 17:54:32 +08001668 return IPMI_CC_PARM_OUT_OF_RANGE;
Yong Lic6713cf2018-09-12 12:35:13 +08001669 }
1670
Yong Licb89c0e2019-01-18 17:54:32 +08001671 reqPolicy = *reqptr & power_policy::policyBitMask;
Yong Lic6713cf2018-09-12 12:35:13 +08001672 if (reqPolicy == power_policy::noChange)
1673 {
1674 // just return the supported policy
1675 *resptr = power_policy::allSupport;
1676 *data_len = power_policy::setPolicyReqLen;
1677 return IPMI_CC_OK;
1678 }
1679
1680 for (auto const& it : power_policy::dbusToIpmi)
1681 {
1682 if (it.second == reqPolicy)
1683 {
1684 value = it.first;
1685 break;
1686 }
1687 }
1688
1689 try
1690 {
1691 const settings::Path& powerRestoreSetting =
1692 chassis::internal::cache::objects.map
1693 .at(chassis::internal::powerRestoreIntf)
1694 .front();
1695 sdbusplus::message::variant<std::string> property =
1696 convertForMessage(value);
1697
1698 auto method = chassis::internal::dbus.new_method_call(
1699 chassis::internal::cache::objects
1700 .service(powerRestoreSetting,
1701 chassis::internal::powerRestoreIntf)
1702 .c_str(),
1703 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1704
1705 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1706 property);
1707 auto reply = chassis::internal::dbus.call(method);
1708 if (reply.is_method_error())
1709 {
1710 phosphor::logging::log<level::ERR>("Unspecified Error");
1711 *data_len = 0;
1712 return IPMI_CC_UNSPECIFIED_ERROR;
1713 }
1714 }
1715 catch (InternalFailure& e)
1716 {
1717 report<InternalFailure>();
1718 *data_len = 0;
1719 return IPMI_CC_UNSPECIFIED_ERROR;
1720 }
1721
Yong Licb89c0e2019-01-18 17:54:32 +08001722 *resptr = power_policy::allSupport;
Yong Lic6713cf2018-09-12 12:35:13 +08001723 *data_len = power_policy::setPolicyReqLen;
1724 return IPMI_CC_OK;
1725}
1726
Adriana Kobylak40814c62015-10-27 15:58:44 -05001727void register_netfn_chassis_functions()
1728{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001729 createIdentifyTimer();
1730
Tom05732372016-09-06 17:21:23 +05301731 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001732 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1733 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001734
Tom05732372016-09-06 17:21:23 +05301735 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001736 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1737 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001738
Yong Liae4b0402018-11-02 11:12:14 +08001739 // Set Chassis Capabilities
1740 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1741 ipmi_set_chassis_cap, PRIVILEGE_USER);
1742
Tom05732372016-09-06 17:21:23 +05301743 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301744 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001745 ipmi_chassis_get_sys_boot_options,
1746 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001747
Tom05732372016-09-06 17:21:23 +05301748 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001749 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1750 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001751
Tom05732372016-09-06 17:21:23 +05301752 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001753 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1754 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001755
Tom Joseph5110c122018-03-23 17:55:40 +05301756 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301757 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1758 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1759
Tom05732372016-09-06 17:21:23 +05301760 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301761 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001762 ipmi_chassis_set_sys_boot_options,
1763 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001764 // <Get POH Counter>
1765 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1766 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001767
1768 // <Set Power Restore Policy>
1769 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1770 ipmi_chassis_set_power_restore_policy,
1771 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001772}