blob: a9e188f5f0f391967c5df2e6cc5981dcfaf2920e [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "chassishandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05005#include "settings.hpp"
Ratan Guptadcb10672017-07-10 10:33:50 +05306
Patrick Venture0b02be92018-08-31 11:55:55 -07007#include <arpa/inet.h>
8#include <endian.h>
9#include <limits.h>
10#include <mapper.h>
11#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070012
Ratan Guptafd28dd72016-08-01 04:58:01 -050013#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070014#include <chrono>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070015#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080016#include <filesystem>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050017#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053018#include <future>
Vernon Mauerye08fbff2019-04-03 09:19:34 -070019#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070020#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070021#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070022#include <map>
23#include <phosphor-logging/elog-errors.hpp>
24#include <phosphor-logging/log.hpp>
25#include <sdbusplus/bus.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070026#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070027#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070028#include <sdbusplus/timer.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070029#include <sstream>
Patrick Venture3a5071a2018-09-12 13:27:42 -070030#include <string>
31#include <xyz/openbmc_project/Common/error.hpp>
32#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
33#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
34#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
35#include <xyz/openbmc_project/State/Host/server.hpp>
36#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
37
Patrick Venture0b02be92018-08-31 11:55:55 -070038// Defines
39#define SET_PARM_VERSION 0x01
Vernon Mauery1181af72018-10-08 12:05:00 -070040#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40
41#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80
42#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0
ratagupta6f6bff2016-04-04 06:20:11 -050043
Lei YU4b0ddb62019-01-25 16:43:50 +080044std::unique_ptr<phosphor::Timer> identifyTimer
45 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050046
Patrick Venture0b02be92018-08-31 11:55:55 -070047constexpr size_t SIZE_MAC = 18;
48constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
49 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
50 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050051constexpr size_t SIZE_PREFIX = 7;
52constexpr size_t MAX_PREFIX_VALUE = 32;
53constexpr size_t SIZE_COOKIE = 4;
54constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053055constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053056
Patrick Venture0b02be92018-08-31 11:55:55 -070057// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053058static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070059 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050060
61static constexpr size_t COOKIE_OFFSET = 1;
62static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053063static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050064static constexpr size_t MAC_OFFSET = 9;
65static constexpr size_t ADDRTYPE_OFFSET = 16;
66static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050067
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050068static constexpr size_t encIdentifyObjectsSize = 1;
69static constexpr size_t chassisIdentifyReqLength = 2;
70static constexpr size_t identifyIntervalPos = 0;
71static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080072
Adriana Kobylak40814c62015-10-27 15:58:44 -050073void register_netfn_chassis_functions() __attribute__((constructor));
74
shgoupfd84fbbf2015-12-17 10:05:51 +080075// Host settings in dbus
76// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070077const char* settings_object_name = "/org/openbmc/settings/host0";
78const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070079const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053080 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080081
Ratan Guptadcb10672017-07-10 10:33:50 +053082constexpr auto SETTINGS_ROOT = "/";
83constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053084
85constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
86constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
87
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050088static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
89static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070090 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050091static constexpr auto pOHCounterProperty = "POHCounter";
92static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080093const static constexpr char chassisCapIntf[] =
94 "xyz.openbmc_project.Control.ChassisCapabilities";
95const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
96const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
97const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
98const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
99const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
100const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
101static constexpr uint8_t chassisCapFlagMask = 0x0f;
102static constexpr uint8_t chassisCapAddrMask = 0xfe;
Ratan Guptadcb10672017-07-10 10:33:50 +0530103
Nan Li8d15fb42016-08-16 22:29:40 +0800104typedef struct
105{
106 uint8_t cap_flags;
107 uint8_t fru_info_dev_addr;
108 uint8_t sdr_dev_addr;
109 uint8_t sel_dev_addr;
110 uint8_t system_management_dev_addr;
111 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700112} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800113
Nan Lifdd8ec52016-08-28 03:57:40 +0800114typedef struct
115{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500116 uint8_t cur_power_state;
117 uint8_t last_power_event;
118 uint8_t misc_power_state;
119 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700120} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800121
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530122// Phosphor Host State manager
123namespace State = sdbusplus::xyz::openbmc_project::State::server;
124
Vernon Mauery185b9f82018-07-20 10:52:36 -0700125namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500126
Ratan Guptadcb10672017-07-10 10:33:50 +0530127using namespace phosphor::logging;
128using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500129using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700130
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500131namespace chassis
132{
133namespace internal
134{
135
136constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
137constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500138constexpr auto powerRestoreIntf =
139 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500140sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
141
142namespace cache
143{
144
145settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500146 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500147
148} // namespace cache
149} // namespace internal
150} // namespace chassis
151
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500152namespace poh
153{
154
155constexpr auto minutesPerCount = 60;
156
157} // namespace poh
158
Patrick Venture0b02be92018-08-31 11:55:55 -0700159struct get_sys_boot_options_t
160{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500161 uint8_t parameter;
162 uint8_t set;
163 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700164} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500165
Patrick Venture0b02be92018-08-31 11:55:55 -0700166struct get_sys_boot_options_response_t
167{
shgoupfd84fbbf2015-12-17 10:05:51 +0800168 uint8_t version;
169 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500170 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700171} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800172
Patrick Venture0b02be92018-08-31 11:55:55 -0700173struct set_sys_boot_options_t
174{
shgoupfd84fbbf2015-12-17 10:05:51 +0800175 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500176 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700177} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500178
Ratan Guptadcb10672017-07-10 10:33:50 +0530179int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500180{
Ratan Guptadcb10672017-07-10 10:33:50 +0530181 ipmi::PropertyMap properties;
182 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530183 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500184
Ratan Guptadcb10672017-07-10 10:33:50 +0530185 try
186 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700187 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530188 // objects,to handle such cases we are interested on that object
189 // which are on interested busname.
190 // Currenlty mapper doesn't give the readable busname(gives busid)
191 // so we can't match with bus name so giving some object specific info
192 // as SETTINGS_MATCH.
193 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500194
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530195 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530196
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530197 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
198 SETTINGS_ROOT, SETTINGS_MATCH);
199
200 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
201 SETTINGS_ROOT, SETTINGS_MATCH);
202
Patrick Venture0b02be92018-08-31 11:55:55 -0700203 properties = ipmi::getAllDbusProperties(
204 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
205 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
206 macObjectInfo.first, MAC_INTERFACE,
207 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530208
Vernon Maueryf442e112019-04-09 11:44:36 -0700209 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530210
Vernon Maueryf442e112019-04-09 11:44:36 -0700211 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530212
Vernon Maueryf442e112019-04-09 11:44:36 -0700213 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530214
Patrick Venture0b02be92018-08-31 11:55:55 -0700215 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700216 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700217 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
218 ? 1
219 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530220
Vernon Maueryf442e112019-04-09 11:44:36 -0700221 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530222
Ratan Guptad70f4532017-08-04 02:07:31 +0530223 // it is expected here that we should get the valid data
224 // but we may also get the default values.
225 // Validation of the data is done by settings.
226 //
227 // if mac address is default mac address then
228 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530229 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530230 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700231 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530232 rc = -1;
233 return rc;
234 }
235 // if addr is static then ipaddress,gateway,prefix
236 // should not be default one,don't send blank override.
237 if (isStatic)
238 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700239 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
240 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530241 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700242 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530243 rc = -1;
244 return rc;
245 }
246 }
247
Patrick Venture0b02be92018-08-31 11:55:55 -0700248 sscanf(
249 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
250 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
251 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
252 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530253
Ratan Guptadcb10672017-07-10 10:33:50 +0530254 respptr->data[MAC_OFFSET + 6] = 0x00;
255
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700256 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
257 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530258
Vernon Maueryf442e112019-04-09 11:44:36 -0700259 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
260 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
261 ? AF_INET
262 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530263
Patrick Venture0b02be92018-08-31 11:55:55 -0700264 addrSize = (addressFamily == AF_INET)
265 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
266 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530267
268 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530269 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700270 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530271
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530272 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
273
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700274 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530275
276 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
277
Ratan Guptad70f4532017-08-04 02:07:31 +0530278 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700279 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530280 }
281 catch (InternalFailure& e)
282 {
283 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700284 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530285 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500286 return rc;
287 }
288
Patrick Venture0b02be92018-08-31 11:55:55 -0700289 // PetiBoot-Specific
290 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700291 std::memcpy(respptr->data, net_conf_initial_bytes,
292 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500293
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700294 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530295
Ratan Guptafd28dd72016-08-01 04:58:01 -0500296#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700297 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500298
Ratan Guptadcb10672017-07-10 10:33:50 +0530299 for (uint8_t pos = 0; pos < index; pos++)
300 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700301 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530302 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500303#endif
304
Ratan Guptafd28dd72016-08-01 04:58:01 -0500305 return rc;
306}
307
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530308/** @brief convert IPv4 and IPv6 addresses from binary to text form.
309 * @param[in] family - IPv4/Ipv6
310 * @param[in] data - req data pointer.
311 * @param[in] offset - offset in the data.
312 * @param[in] addrSize - size of the data which needs to be read from offset.
313 * @returns address in text form.
314 */
315
Patrick Venture0b02be92018-08-31 11:55:55 -0700316std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
317 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530318{
319 char ipAddr[INET6_ADDRSTRLEN] = {};
320
Patrick Venture0b02be92018-08-31 11:55:55 -0700321 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530322 {
323 case AF_INET:
324 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700325 struct sockaddr_in addr4
326 {
327 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700328 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530329
Patrick Venture0b02be92018-08-31 11:55:55 -0700330 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530331
332 break;
333 }
334 case AF_INET6:
335 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700336 struct sockaddr_in6 addr6
337 {
338 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700339 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530340
Patrick Venture0b02be92018-08-31 11:55:55 -0700341 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530342
343 break;
344 }
345 default:
346 {
347 return {};
348 }
349 }
350
351 return ipAddr;
352}
353
Ratan Guptadcb10672017-07-10 10:33:50 +0530354int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500355{
Ratan Guptadcb10672017-07-10 10:33:50 +0530356 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500357 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700358 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530359 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700360 char addrOrigin{0};
361 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530362 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530363 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700364 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
365 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530366 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530367 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500368
Patrick Venture0b02be92018-08-31 11:55:55 -0700369 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500370 // version starts from sixth byte
371
Ratan Guptadcb10672017-07-10 10:33:50 +0530372 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500373 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530374 do
375 {
376 // cookie == 0x21 0x70 0x62 0x21
377 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700378 (net_conf_initial_bytes + COOKIE_OFFSET),
379 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530380 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700381 // cookie == 0
382 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
383 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530384 {
385 // need to zero out the network settings.
386 break;
387 }
388
389 log<level::ERR>("Invalid Cookie");
390 elog<InternalFailure>();
391 }
392
393 // vesion == 0x00 0x01
394 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700395 (net_conf_initial_bytes + VERSION_OFFSET),
396 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530397 {
398
399 log<level::ERR>("Invalid Version");
400 elog<InternalFailure>();
401 }
402
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700403 std::snprintf(
404 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
405 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
406 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
407 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530408
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700409 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
410 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530411
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530412 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530413 {
414 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530415 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530416 }
417
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530418 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700419 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
420 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530421
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530422 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530423
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700424 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
425 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530426
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530427 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
428
Ratan Gupta8c31d232017-08-13 05:49:43 +0530429 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530430 {
431 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
432 family = AF_INET6;
433 }
434
Patrick Venture0b02be92018-08-31 11:55:55 -0700435 ipAddress =
436 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530437
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530438 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
439
Patrick Venture0b02be92018-08-31 11:55:55 -0700440 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530441
Patrick Venture0b02be92018-08-31 11:55:55 -0700442 // Cookie == 0 or it is a valid cookie
443 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
444 std::to_string(prefix) + ",gateway="s + gateway +
445 ",mac="s + mac + ",addressOrigin="s +
446 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500447
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530448 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
449
450 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
451 SETTINGS_ROOT, SETTINGS_MATCH);
452 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
453 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530454 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530455 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700456 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530457 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700458 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530459 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700460 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530461 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700462 IP_INTERFACE, "Gateway", std::string(gateway));
463 ipmi::setDbusProperty(
464 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
465 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530466 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700467 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500468
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 log<level::DEBUG>(
470 "Network configuration changed",
471 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500472 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530473 catch (InternalFailure& e)
474 {
475 commit<InternalFailure>();
476 return -1;
477 }
478
479 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500480}
481
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500482uint32_t getPOHCounter()
483{
484 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
485
Patrick Venture0b02be92018-08-31 11:55:55 -0700486 auto chassisStateObj =
487 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500488
Patrick Venture0b02be92018-08-31 11:55:55 -0700489 auto service =
490 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500491
Patrick Venture0b02be92018-08-31 11:55:55 -0700492 auto propValue =
493 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
494 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500495
Vernon Maueryf442e112019-04-09 11:44:36 -0700496 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500497}
498
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500499ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
500 ipmi_request_t request,
501 ipmi_response_t response,
502 ipmi_data_len_t data_len,
503 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500504{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500505 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800506 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500507 *data_len = 0;
508 return rc;
509}
510
Nan Li8d15fb42016-08-16 22:29:40 +0800511ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700512 ipmi_request_t request,
513 ipmi_response_t response,
514 ipmi_data_len_t data_len,
515 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800516{
517 // sd_bus error
518 ipmi_ret_t rc = IPMI_CC_OK;
519
520 ipmi_chassis_cap_t chassis_cap{};
521
Yong Liae4b0402018-11-02 11:12:14 +0800522 if (*data_len != 0)
523 {
524 return IPMI_CC_REQ_DATA_LEN_INVALID;
525 }
526
Nan Li8d15fb42016-08-16 22:29:40 +0800527 *data_len = sizeof(ipmi_chassis_cap_t);
528
Yong Liae4b0402018-11-02 11:12:14 +0800529 try
530 {
531 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800532
Yong Liae4b0402018-11-02 11:12:14 +0800533 ipmi::DbusObjectInfo chassisCapObject =
534 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800535
Yong Liae4b0402018-11-02 11:12:14 +0800536 // capabilities flags
537 // [7..4] - reserved
538 // [3] – 1b = provides power interlock (IPM 1.5)
539 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
540 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
541 // has capabilities
542 // to lock out external power control and reset button or
543 // front panel interfaces and/or detect tampering with those
544 // interfaces).
545 // [0] -1b = Chassis provides intrusion (physical security) sensor.
546 // set to default value 0x0.
547 ipmi::Value variant = ipmi::getDbusProperty(
548 bus, chassisCapObject.second, chassisCapObject.first,
549 chassisCapIntf, chassisCapFlagsProp);
Vernon Maueryf442e112019-04-09 11:44:36 -0700550 chassis_cap.cap_flags = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800551
Yong Liae4b0402018-11-02 11:12:14 +0800552 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
553 chassisCapObject.first, chassisCapIntf,
554 chassisFRUDevAddrProp);
555 // Chassis FRU info Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700556 chassis_cap.fru_info_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800557
Yong Liae4b0402018-11-02 11:12:14 +0800558 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
559 chassisCapObject.first, chassisCapIntf,
560 chassisSDRDevAddrProp);
561 // Chassis SDR Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700562 chassis_cap.sdr_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800563
Yong Liae4b0402018-11-02 11:12:14 +0800564 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
565 chassisCapObject.first, chassisCapIntf,
566 chassisSELDevAddrProp);
567 // Chassis SEL Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700568 chassis_cap.sel_dev_addr = std::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800569
Yong Liae4b0402018-11-02 11:12:14 +0800570 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
571 chassisCapObject.first, chassisCapIntf,
572 chassisSMDevAddrProp);
573 // Chassis System Management Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700574 chassis_cap.system_management_dev_addr = std::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 chassisBridgeDevAddrProp);
579 // Chassis Bridge Device Address.
Vernon Maueryf442e112019-04-09 11:44:36 -0700580 chassis_cap.bridge_dev_addr = std::get<uint8_t>(variant);
Yong Liae4b0402018-11-02 11:12:14 +0800581 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
582 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
583 std::copy(chassisP, chassisP + *data_len, respP);
584 }
585 catch (std::exception& e)
586 {
587 log<level::ERR>(e.what());
588 rc = IPMI_CC_UNSPECIFIED_ERROR;
589 *data_len = 0;
590 return rc;
591 }
592
593 return rc;
594}
595
596ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
597 ipmi_request_t request,
598 ipmi_response_t response,
599 ipmi_data_len_t data_len,
600 ipmi_context_t context)
601{
602 ipmi_ret_t rc = IPMI_CC_OK;
603
604 if (*data_len != sizeof(ipmi_chassis_cap_t))
605 {
606 log<level::ERR>("Unsupported request length",
607 entry("LEN=0x%x", *data_len));
608 *data_len = 0;
609 return IPMI_CC_REQ_DATA_LEN_INVALID;
610 }
611
612 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
613
614 *data_len = 0;
615
616 // check input data
617 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
618 {
619 log<level::ERR>("Unsupported request parameter(CAP Flags)",
620 entry("REQ=0x%x", chassisCap->cap_flags));
621 return IPMI_CC_INVALID_FIELD_REQUEST;
622 }
623
624 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
625 {
626 log<level::ERR>("Unsupported request parameter(FRU Addr)",
627 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
628 return IPMI_CC_INVALID_FIELD_REQUEST;
629 }
630
631 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
632 {
633 log<level::ERR>("Unsupported request parameter(SDR Addr)",
634 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
635 return IPMI_CC_INVALID_FIELD_REQUEST;
636 }
637
638 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
639 {
640 log<level::ERR>("Unsupported request parameter(SEL Addr)",
641 entry("REQ=0x%x", chassisCap->sel_dev_addr));
642 return IPMI_CC_INVALID_FIELD_REQUEST;
643 }
644
645 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
646 {
647 log<level::ERR>(
648 "Unsupported request parameter(SM Addr)",
649 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
650 return IPMI_CC_INVALID_FIELD_REQUEST;
651 }
652
653 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
654 {
655 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
656 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
657 return IPMI_CC_INVALID_FIELD_REQUEST;
658 }
659
660 try
661 {
662 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
663 ipmi::DbusObjectInfo chassisCapObject =
664 ipmi::getDbusObject(bus, chassisCapIntf);
665
666 ipmi::setDbusProperty(bus, chassisCapObject.second,
667 chassisCapObject.first, chassisCapIntf,
668 chassisCapFlagsProp, chassisCap->cap_flags);
669
670 ipmi::setDbusProperty(bus, chassisCapObject.second,
671 chassisCapObject.first, chassisCapIntf,
672 chassisFRUDevAddrProp,
673 chassisCap->fru_info_dev_addr);
674
675 ipmi::setDbusProperty(bus, chassisCapObject.second,
676 chassisCapObject.first, chassisCapIntf,
677 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
678
679 ipmi::setDbusProperty(bus, chassisCapObject.second,
680 chassisCapObject.first, chassisCapIntf,
681 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
682
683 ipmi::setDbusProperty(bus, chassisCapObject.second,
684 chassisCapObject.first, chassisCapIntf,
685 chassisSMDevAddrProp,
686 chassisCap->system_management_dev_addr);
687
688 ipmi::setDbusProperty(bus, chassisCapObject.second,
689 chassisCapObject.first, chassisCapIntf,
690 chassisBridgeDevAddrProp,
691 chassisCap->bridge_dev_addr);
692 }
693 catch (std::exception& e)
694 {
695 log<level::ERR>(e.what());
696 rc = IPMI_CC_UNSPECIFIED_ERROR;
697 return rc;
698 }
Nan Li8d15fb42016-08-16 22:29:40 +0800699
700 return rc;
701}
702
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530703//------------------------------------------
704// Calls into Host State Manager Dbus object
705//------------------------------------------
706int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600707{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500708 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700709 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500710 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700711 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
712 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530713
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500714 // sd_bus error
715 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700716 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600717
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500718 // SD Bus error report mechanism.
719 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600720
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500721 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700722 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500723 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
724 if (rc < 0)
725 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700726 log<level::ERR>(
727 "Failed to get bus name",
728 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500729 return rc;
730 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530731
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500732 // Convert to string equivalent of the passed in transition enum.
733 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530734
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500735 rc = sd_bus_call_method(bus_type, // On the system bus
736 busname, // Service to contact
737 HOST_STATE_MANAGER_ROOT, // Object path
738 DBUS_PROPERTY_IFACE, // Interface name
739 "Set", // Method to be called
740 &bus_error, // object to return error
741 nullptr, // Response buffer if any
742 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700743 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
744 request.c_str());
745 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500746 {
747 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530748 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500749 }
750 else
751 {
752 log<level::INFO>("Transition request initiated successfully");
753 }
vishwa36993272015-11-20 12:43:49 -0600754
755 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500756 free(busname);
vishwa36993272015-11-20 12:43:49 -0600757
Sergey Solomineb9b8142016-08-23 09:07:28 -0500758 return rc;
vishwa36993272015-11-20 12:43:49 -0600759}
760
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500761namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500762{
Nan Lifdd8ec52016-08-28 03:57:40 +0800763
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500764using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
765using IpmiValue = uint8_t;
766using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800767
Patrick Venture0b02be92018-08-31 11:55:55 -0700768std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500769 {RestorePolicy::Policy::AlwaysOff, 0x00},
770 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700771 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800772
Yong Lic6713cf2018-09-12 12:35:13 +0800773static constexpr uint8_t noChange = 0x03;
774static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
775static constexpr uint8_t policyBitMask = 0x07;
776static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500777} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800778
779//----------------------------------------------------------------------
780// Get Chassis Status commands
781//----------------------------------------------------------------------
782ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500783 ipmi_request_t request,
784 ipmi_response_t response,
785 ipmi_data_len_t data_len,
786 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800787{
Patrick Venture0b02be92018-08-31 11:55:55 -0700788 const char* objname = "/org/openbmc/control/power0";
789 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800790
Patrick Venture0b02be92018-08-31 11:55:55 -0700791 sd_bus* bus = NULL;
792 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800793 int r = 0;
794 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700795 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800796 ipmi_ret_t rc = IPMI_CC_OK;
797 ipmi_get_chassis_status_t chassis_status{};
798
Nan Lifdd8ec52016-08-28 03:57:40 +0800799 uint8_t s = 0;
800
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500801 using namespace chassis::internal;
802 using namespace chassis::internal::cache;
803 using namespace power_policy;
804
Deepak Kodihallie6027092017-08-27 08:13:37 -0500805 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700806 auto method = dbus.new_method_call(
807 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
808 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500809 method.append(powerRestoreIntf, "PowerRestorePolicy");
810 auto resp = dbus.call(method);
811 if (resp.is_method_error())
812 {
813 log<level::ERR>("Error in PowerRestorePolicy Get");
814 report<InternalFailure>();
815 *data_len = 0;
816 return IPMI_CC_UNSPECIFIED_ERROR;
817 }
818 sdbusplus::message::variant<std::string> result;
819 resp.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -0700820 auto powerRestore =
821 RestorePolicy::convertPolicyFromString(std::get<std::string>(result));
Nan Lifdd8ec52016-08-28 03:57:40 +0800822
823 *data_len = 4;
824
Tom Joseph63a00512017-08-09 23:39:59 +0530825 bus = ipmid_get_sd_bus_connection();
826
Nan Lifdd8ec52016-08-28 03:57:40 +0800827 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700828 if (r < 0)
829 {
830 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800831 rc = IPMI_CC_UNSPECIFIED_ERROR;
832 goto finish;
833 }
834
Patrick Venture0b02be92018-08-31 11:55:55 -0700835 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
836 "i");
837 if (r < 0)
838 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530839 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700840 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
841 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530842 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800843 rc = IPMI_CC_UNSPECIFIED_ERROR;
844 goto finish;
845 }
846
847 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700848 if (r < 0)
849 {
850 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800851 rc = IPMI_CC_UNSPECIFIED_ERROR;
852 goto finish;
853 }
854
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500855 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800856
857 // Current Power State
858 // [7] reserved
859 // [6..5] power restore policy
860 // 00b = chassis stays powered off after AC/mains returns
861 // 01b = after AC returns, power is restored to the state that was
862 // in effect when AC/mains was lost.
863 // 10b = chassis always powers up after AC/mains returns
864 // 11b = unknow
865 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700866 // Do we need to define a dbus property to identify the restore
867 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800868
869 // [4] power control fault
870 // 1b = controller attempted to turn system power on or off, but
871 // system did not enter desired state.
872 // Set to 0b, since We don't support it..
873
874 // [3] power fault
875 // 1b = fault detected in main power subsystem.
876 // set to 0b. for we don't support it.
877
878 // [2] 1b = interlock (chassis is presently shut down because a chassis
879 // panel interlock switch is active). (IPMI 1.5)
880 // set to 0b, for we don't support it.
881
882 // [1] power overload
883 // 1b = system shutdown because of power overload condition.
884 // set to 0b, for we don't support it.
885
886 // [0] power is on
887 // 1b = system power is on
888 // 0b = system power is off(soft-off S4/S5, or mechanical off)
889
Patrick Venture0b02be92018-08-31 11:55:55 -0700890 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800891
892 // Last Power Event
893 // [7..5] – reserved
894 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
895 // [3] – 1b = last power down caused by power fault
896 // [2] – 1b = last power down caused by a power interlock being activated
897 // [1] – 1b = last power down caused by a Power overload
898 // [0] – 1b = AC failed
899 // set to 0x0, for we don't support these fields.
900
901 chassis_status.last_power_event = 0;
902
903 // Misc. Chassis State
904 // [7] – reserved
905 // [6] – 1b = Chassis Identify command and state info supported (Optional)
906 // 0b = Chassis Identify command support unspecified via this command.
907 // (The Get Command Support command , if implemented, would still
908 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700909 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
910 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800911 // as 00b) otherwise. Returns the present chassis identify state.
912 // Refer to the Chassis Identify command for more info.
913 // 00b = chassis identify state = Off
914 // 01b = chassis identify state = Temporary(timed) On
915 // 10b = chassis identify state = Indefinite On
916 // 11b = reserved
917 // [3] – 1b = Cooling/fan fault detected
918 // [2] – 1b = Drive Fault
919 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
920 // push-buttons disabled.)
921 // [0] – 1b = Chassis Intrusion active
922 // set to 0, for we don't support them.
923 chassis_status.misc_power_state = 0;
924
925 // Front Panel Button Capabilities and disable/enable status(Optional)
926 // set to 0, for we don't support them.
927 chassis_status.front_panel_button_cap_status = 0;
928
929 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700930 std::memcpy(response, &chassis_status, *data_len);
Nan Lifdd8ec52016-08-28 03:57:40 +0800931
932finish:
933 free(busname);
934 reply = sd_bus_message_unref(reply);
935
936 return rc;
937}
Chris Austen7888c4d2015-12-03 15:26:20 -0600938
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530939//-------------------------------------------------------------
940// Send a command to SoftPowerOff application to stop any timer
941//-------------------------------------------------------------
942int stop_soft_off_timer()
943{
Patrick Venture0b02be92018-08-31 11:55:55 -0700944 constexpr auto iface = "org.freedesktop.DBus.Properties";
945 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
946 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530947
Patrick Venture0b02be92018-08-31 11:55:55 -0700948 constexpr auto property = "ResponseReceived";
949 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
950 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530951
952 // Get the system bus where most system services are provided.
953 auto bus = ipmid_get_sd_bus_connection();
954
955 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500956 // TODO openbmc/openbmc#1661 - Mapper refactor
957 //
958 // See openbmc/openbmc#1743 for some details but high level summary is that
959 // for now the code will directly call the soft off interface due to a
960 // race condition with mapper usage
961 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700962 // char *busname = nullptr;
963 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
964 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500965 //{
966 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530967 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500968 // return r;
969 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530970
971 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500972 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700973 "Set", nullptr, nullptr, "ssv", soft_off_iface,
974 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530975 if (rc < 0)
976 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530977 log<level::ERR>("Failed to set property in SoftPowerOff object",
978 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530979 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500980
Patrick Venture0b02be92018-08-31 11:55:55 -0700981 // TODO openbmc/openbmc#1661 - Mapper refactor
982 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530983 return rc;
984}
985
vishwa36993272015-11-20 12:43:49 -0600986//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500987// Create file to indicate there is no need for softoff notification to host
988//----------------------------------------------------------------------
989void indicate_no_softoff_needed()
990{
991 fs::path path{HOST_INBAND_REQUEST_DIR};
992 if (!fs::is_directory(path))
993 {
994 fs::create_directory(path);
995 }
996
997 // Add the host instance (default 0 for now) to the file name
998 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -0700999 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001000 size++; // null
1001 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001002 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001003
1004 // Append file name to directory and create it
1005 path /= buf.get();
1006 std::ofstream(path.c_str());
1007}
1008
anil kumar appanadafff5f2019-04-27 18:06:00 +00001009/** @brief Implementation of chassis control command
1010 *
1011 * @param - chassisControl command byte
1012 *
1013 * @return Success or InvalidFieldRequest.
1014 */
1015ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001016{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001017 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001018 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001019 {
1020 case CMD_POWER_ON:
1021 rc = initiate_state_transition(State::Host::Transition::On);
1022 break;
1023 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301024 // This path would be hit in 2 conditions.
1025 // 1: When user asks for power off using ipmi chassis command 0x04
1026 // 2: Host asking for power off post shutting down.
1027
1028 // If it's a host requested power off, then need to nudge Softoff
1029 // application that it needs to stop the watchdog timer if running.
1030 // If it is a user requested power off, then this is not really
1031 // needed. But then we need to differentiate between user and host
1032 // calling this same command
1033
1034 // For now, we are going ahead with trying to nudge the soft off and
1035 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001036 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301037
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001038 // Only request the Off transition if the soft power off
1039 // application is not running
1040 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001041 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001042 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301043 // that it should not run. Not doing this will result in State
1044 // manager doing a default soft power off when asked for power
1045 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001046 indicate_no_softoff_needed();
1047
1048 // Now request the shutdown
1049 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001050 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001051 else
1052 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301053 log<level::INFO>("Soft off is running, so let shutdown target "
1054 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001055 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001056 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301057
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001058 case CMD_HARD_RESET:
1059 case CMD_POWER_CYCLE:
1060 // SPEC has a section that says certain implementations can trigger
1061 // PowerOn if power is Off when a command to power cycle is
1062 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001063
1064 // First create a file to indicate to the soft off application
1065 // that it should not run since this is a direct user initiated
1066 // power reboot request (i.e. a reboot request that is not
1067 // originating via a soft power off SMS request)
1068 indicate_no_softoff_needed();
1069
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001070 rc = initiate_state_transition(State::Host::Transition::Reboot);
1071 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301072
1073 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1074 // Request Host State Manager to do a soft power off
1075 rc = initiate_state_transition(State::Host::Transition::Off);
1076 break;
1077
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001078 default:
1079 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301080 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001081 entry("CMD=0x%X", chassisControl));
1082 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001083 }
1084 }
vishwa36993272015-11-20 12:43:49 -06001085
anil kumar appanadafff5f2019-04-27 18:06:00 +00001086 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1087 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001088}
1089
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001090/** @brief Return D-Bus connection string to enclosure identify LED object
1091 *
1092 * @param[in, out] connection - connection to D-Bus object
1093 * @return a IPMI return code
1094 */
1095std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301096{
Tom Joseph5110c122018-03-23 17:55:40 +05301097 // lookup enclosure_identify group owner(s) in mapper
1098 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001099 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1100 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301101
1102 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001103 static const std::vector<std::string> interfaces = {
1104 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301105 mapperCall.append(interfaces);
1106 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1107 if (mapperReply.is_method_error())
1108 {
1109 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001110 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301111 }
1112 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1113 mapperReply.read(mapperResp);
1114
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001115 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301116 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001117 log<level::ERR>(
1118 "Invalid number of enclosure identify objects.",
1119 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001120 elog<InternalFailure>();
1121 }
1122 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1123 return pair.first;
1124}
Tom Joseph5110c122018-03-23 17:55:40 +05301125
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001126/** @brief Turn On/Off enclosure identify LED
1127 *
1128 * @param[in] flag - true to turn on LED, false to turn off
1129 * @return a IPMI return code
1130 */
1131void enclosureIdentifyLed(bool flag)
1132{
1133 using namespace chassis::internal;
1134 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001135 auto msg = std::string("enclosureIdentifyLed(") +
1136 boost::lexical_cast<std::string>(flag) + ")";
1137 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001138 auto led =
1139 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1140 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001141 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001142 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001143 auto ledReply = dbus.call(led);
1144 if (ledReply.is_method_error())
1145 {
1146 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001147 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001148 elog<InternalFailure>();
1149 }
1150}
1151
1152/** @brief Callback method to turn off LED
1153 */
1154void enclosureIdentifyLedOff()
1155{
1156 try
1157 {
1158 enclosureIdentifyLed(false);
1159 }
1160 catch (const InternalFailure& e)
1161 {
1162 report<InternalFailure>();
1163 }
1164}
1165
1166/** @brief Create timer to turn on and off the enclosure LED
1167 */
1168void createIdentifyTimer()
1169{
1170 if (!identifyTimer)
1171 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001172 identifyTimer =
1173 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001174 }
1175}
1176
Vernon Mauery400cc782018-10-09 13:49:53 -07001177ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1178 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001179{
Vernon Mauery400cc782018-10-09 13:49:53 -07001180 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1181 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301182
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001183 if (identifyInterval || forceIdentify)
1184 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001185 // stop the timer if already started;
1186 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001187 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001188 try
Tom Joseph5110c122018-03-23 17:55:40 +05301189 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001190 enclosureIdentifyLed(true);
1191 }
1192 catch (const InternalFailure& e)
1193 {
1194 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001195 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301196 }
1197
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001198 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301199 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001200 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001201 }
1202 // start the timer
1203 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001204 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001205 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301206 }
Tom Josephbed26992018-07-31 23:00:24 +05301207 else if (!identifyInterval)
1208 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001209 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301210 enclosureIdentifyLedOff();
1211 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001212 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301213}
1214
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001215namespace boot_options
1216{
1217
1218using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1219using IpmiValue = uint8_t;
1220constexpr auto ipmiDefault = 0;
1221
Patrick Venture0b02be92018-08-31 11:55:55 -07001222std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001223 {0x01, Source::Sources::Network},
1224 {0x02, Source::Sources::Disk},
1225 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001226 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001227
Patrick Venture0b02be92018-08-31 11:55:55 -07001228std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001229 {0x03, Mode::Modes::Safe},
1230 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001231 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001232
Patrick Venture0b02be92018-08-31 11:55:55 -07001233std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001234 {Source::Sources::Network, 0x01},
1235 {Source::Sources::Disk, 0x02},
1236 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001237 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001238
Patrick Venture0b02be92018-08-31 11:55:55 -07001239std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001240 {Mode::Modes::Safe, 0x03},
1241 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001242 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001243
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001244} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001245
Marri Devender Rao81719702018-05-07 00:53:48 -05001246/** @brief Set the property value for boot source
1247 * @param[in] source - boot source value
1248 * @return On failure return IPMI error.
1249 */
1250static ipmi_ret_t setBootSource(const Source::Sources& source)
1251{
1252 using namespace chassis::internal;
1253 using namespace chassis::internal::cache;
1254 sdbusplus::message::variant<std::string> property =
1255 convertForMessage(source);
1256 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1257 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001258 auto method = dbus.new_method_call(
1259 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1260 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001261 method.append(bootSourceIntf, "BootSource", property);
1262 auto reply = dbus.call(method);
1263 if (reply.is_method_error())
1264 {
1265 log<level::ERR>("Error in BootSource Set");
1266 report<InternalFailure>();
1267 return IPMI_CC_UNSPECIFIED_ERROR;
1268 }
1269 return IPMI_CC_OK;
1270}
1271
Patrick Venture0b02be92018-08-31 11:55:55 -07001272/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001273 * @param[in] mode - boot mode value
1274 * @return On failure return IPMI error.
1275 */
1276static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1277{
1278 using namespace chassis::internal;
1279 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001280 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001281 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1282 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001283 auto method = dbus.new_method_call(
1284 objects.service(bootModeSetting, bootModeIntf).c_str(),
1285 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001286 method.append(bootModeIntf, "BootMode", property);
1287 auto reply = dbus.call(method);
1288 if (reply.is_method_error())
1289 {
1290 log<level::ERR>("Error in BootMode Set");
1291 report<InternalFailure>();
1292 return IPMI_CC_UNSPECIFIED_ERROR;
1293 }
1294 return IPMI_CC_OK;
1295}
1296
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001297ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1298 ipmi_request_t request,
1299 ipmi_response_t response,
1300 ipmi_data_len_t data_len,
1301 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001302{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001303 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001304 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001305 char* p = NULL;
1306 get_sys_boot_options_response_t* resp =
1307 (get_sys_boot_options_response_t*)response;
1308 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001309 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001310
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001311 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001312 resp->version = SET_PARM_VERSION;
1313 resp->parm = 5;
1314 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001315
shgoupfd84fbbf2015-12-17 10:05:51 +08001316 /*
1317 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1318 * This is the only parameter used by petitboot.
1319 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001320 if (reqptr->parameter ==
1321 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1322 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001323
Ratan Guptafd28dd72016-08-01 04:58:01 -05001324 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001325 using namespace chassis::internal;
1326 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001327
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001328 try
ratagupta6f6bff2016-04-04 06:20:11 -05001329 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001330 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1331 const auto& bootSourceSetting =
1332 std::get<settings::Path>(bootSetting);
1333 auto oneTimeEnabled =
1334 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001335 auto method = dbus.new_method_call(
1336 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1337 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001338 method.append(bootSourceIntf, "BootSource");
1339 auto reply = dbus.call(method);
1340 if (reply.is_method_error())
1341 {
1342 log<level::ERR>("Error in BootSource Get");
1343 report<InternalFailure>();
1344 *data_len = 0;
1345 return IPMI_CC_UNSPECIFIED_ERROR;
1346 }
1347 sdbusplus::message::variant<std::string> result;
1348 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001349 auto bootSource =
1350 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001351
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001352 bootSetting = settings::boot::setting(objects, bootModeIntf);
1353 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1354 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001355 objects.service(bootModeSetting, bootModeIntf).c_str(),
1356 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001357 method.append(bootModeIntf, "BootMode");
1358 reply = dbus.call(method);
1359 if (reply.is_method_error())
1360 {
1361 log<level::ERR>("Error in BootMode Get");
1362 report<InternalFailure>();
1363 *data_len = 0;
1364 return IPMI_CC_UNSPECIFIED_ERROR;
1365 }
1366 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001367 auto bootMode =
1368 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001369
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001370 bootOption = sourceDbusToIpmi.at(bootSource);
1371 if ((Mode::Modes::Regular == bootMode) &&
1372 (Source::Sources::Default == bootSource))
1373 {
1374 bootOption = ipmiDefault;
1375 }
1376 else if (Source::Sources::Default == bootSource)
1377 {
1378 bootOption = modeDbusToIpmi.at(bootMode);
1379 }
1380 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001381
Patrick Venture0b02be92018-08-31 11:55:55 -07001382 resp->data[0] = oneTimeEnabled
1383 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1384 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001385
ratagupta6f6bff2016-04-04 06:20:11 -05001386 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001387 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001388 catch (InternalFailure& e)
1389 {
1390 report<InternalFailure>();
1391 *data_len = 0;
1392 return IPMI_CC_UNSPECIFIED_ERROR;
1393 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001394 }
1395 else if (reqptr->parameter ==
1396 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1397 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001398
Patrick Venture0b02be92018-08-31 11:55:55 -07001399 *data_len =
1400 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001401
Patrick Venture0b02be92018-08-31 11:55:55 -07001402 resp->parm =
1403 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001404
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001405 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001406
Patrick Venture0b02be92018-08-31 11:55:55 -07001407 if (ret < 0)
1408 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001409
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301410 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001411 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001412 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001413 }
1414 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001415 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001416 }
1417
Patrick Venture0b02be92018-08-31 11:55:55 -07001418 else
1419 {
1420 log<level::ERR>("Unsupported parameter",
1421 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001422 }
1423
1424 if (p)
1425 free(p);
1426
Ratan Guptafd28dd72016-08-01 04:58:01 -05001427 if (rc == IPMI_CC_OK)
1428 {
1429 *data_len += 2;
1430 }
1431
shgoupfd84fbbf2015-12-17 10:05:51 +08001432 return rc;
1433}
1434
shgoupfd84fbbf2015-12-17 10:05:51 +08001435ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001436 ipmi_request_t request,
1437 ipmi_response_t response,
1438 ipmi_data_len_t data_len,
1439 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001440{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001441 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001442 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001443 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001444
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001445 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1446 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001447
shgoupfd84fbbf2015-12-17 10:05:51 +08001448 // This IPMI command does not have any resposne data
1449 *data_len = 0;
1450
1451 /* 000101
1452 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1453 * This is the only parameter used by petitboot.
1454 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001455
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001456 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1457 {
1458 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1459 using namespace chassis::internal;
1460 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001461 auto oneTimeEnabled = false;
1462 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301463 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001464 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001465
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001466 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001467 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001468 bool permanent =
1469 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1470 SET_PARM_BOOT_FLAGS_PERMANENT;
1471
Patrick Venture0b02be92018-08-31 11:55:55 -07001472 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301473
1474 oneTimeEnabled =
1475 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1476
1477 /*
1478 * Check if the current boot setting is onetime or permanent, if the
1479 * request in the command is otherwise, then set the "Enabled"
1480 * property in one_time object path to 'True' to indicate onetime
1481 * and 'False' to indicate permanent.
1482 *
1483 * Once the onetime/permanent setting is applied, then the bootMode
1484 * and bootSource is updated for the corresponding object.
1485 */
1486 if ((permanent && oneTimeEnabled) ||
1487 (!permanent && !oneTimeEnabled))
1488 {
1489 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1490
Patrick Venture0b02be92018-08-31 11:55:55 -07001491 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1492 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301493 }
1494
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001495 auto modeItr = modeIpmiToDbus.find(bootOption);
1496 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1497 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001498 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001499 rc = setBootSource(sourceItr->second);
1500 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001501 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001502 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001503 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001504 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001505 // If a set boot device is mapping to a boot source, then reset
1506 // the boot mode D-Bus property to default.
1507 // This way the ipmid code can determine which property is not
1508 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001509 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001510 {
1511 setBootMode(Mode::Modes::Regular);
1512 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001513 }
1514 if (modeIpmiToDbus.end() != modeItr)
1515 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001516 rc = setBootMode(modeItr->second);
1517 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001518 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001519 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001520 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001521 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001522 // If a set boot device is mapping to a boot mode, then reset
1523 // the boot source D-Bus property to default.
1524 // This way the ipmid code can determine which property is not
1525 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001526 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001527 {
1528 setBootSource(Source::Sources::Default);
1529 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001530 }
1531 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001532 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001533 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001534 report<InternalFailure>();
1535 *data_len = 0;
1536 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001537 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001538 }
1539 else if (reqptr->parameter ==
1540 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1541 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001542
1543 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001544 if (ret < 0)
1545 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301546 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001547 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001548 rc = IPMI_CC_UNSPECIFIED_ERROR;
1549 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001550 }
1551 else if (reqptr->parameter ==
1552 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1553 {
Tom Josephf536c902017-09-25 18:08:15 +05301554 // Handle parameter #4 and return command completed normally
1555 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1556 // parameter. This is added to support the ipmitool command `chassis
1557 // bootdev` which sends set on parameter #4, before setting the boot
1558 // flags.
1559 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001560 }
1561 else
1562 {
1563 log<level::ERR>("Unsupported parameter",
1564 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001565 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001566 }
1567
1568 return rc;
1569}
1570
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001571/** @brief implements Get POH counter command
1572 * @parameter
1573 * - none
1574 * @returns IPMI completion code plus response data
1575 * - minPerCount - Minutes per count
1576 * - counterReading - counter reading
1577 */
1578ipmi::RspType<uint8_t, // Minutes per count
1579 uint32_t // Counter reading
1580 >
1581 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001582{
1583 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001584 try
1585 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001586 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1587 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001588 }
1589 catch (std::exception& e)
1590 {
1591 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001592 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001593 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001594}
1595
Yong Lic6713cf2018-09-12 12:35:13 +08001596ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1597 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1598 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1599{
1600 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1601 auto* resptr = reinterpret_cast<uint8_t*>(response);
1602 uint8_t reqPolicy = 0;
1603
1604 power_policy::DbusValue value =
1605 power_policy::RestorePolicy::Policy::AlwaysOff;
1606
1607 if (*data_len != power_policy::setPolicyReqLen)
1608 {
1609 phosphor::logging::log<level::ERR>("Unsupported request length",
1610 entry("LEN=0x%x", *data_len));
1611 *data_len = 0;
1612 return IPMI_CC_REQ_DATA_LEN_INVALID;
1613 }
1614
Yong Licb89c0e2019-01-18 17:54:32 +08001615 if (*reqptr > power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001616 {
1617 phosphor::logging::log<level::ERR>("Reserved request parameter",
Yong Licb89c0e2019-01-18 17:54:32 +08001618 entry("REQ=0x%x", *reqptr));
Yong Lic6713cf2018-09-12 12:35:13 +08001619 *data_len = 0;
Yong Licb89c0e2019-01-18 17:54:32 +08001620 return IPMI_CC_PARM_OUT_OF_RANGE;
Yong Lic6713cf2018-09-12 12:35:13 +08001621 }
1622
Yong Licb89c0e2019-01-18 17:54:32 +08001623 reqPolicy = *reqptr & power_policy::policyBitMask;
Yong Lic6713cf2018-09-12 12:35:13 +08001624 if (reqPolicy == power_policy::noChange)
1625 {
1626 // just return the supported policy
1627 *resptr = power_policy::allSupport;
1628 *data_len = power_policy::setPolicyReqLen;
1629 return IPMI_CC_OK;
1630 }
1631
1632 for (auto const& it : power_policy::dbusToIpmi)
1633 {
1634 if (it.second == reqPolicy)
1635 {
1636 value = it.first;
1637 break;
1638 }
1639 }
1640
1641 try
1642 {
1643 const settings::Path& powerRestoreSetting =
1644 chassis::internal::cache::objects.map
1645 .at(chassis::internal::powerRestoreIntf)
1646 .front();
1647 sdbusplus::message::variant<std::string> property =
1648 convertForMessage(value);
1649
1650 auto method = chassis::internal::dbus.new_method_call(
1651 chassis::internal::cache::objects
1652 .service(powerRestoreSetting,
1653 chassis::internal::powerRestoreIntf)
1654 .c_str(),
1655 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1656
1657 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1658 property);
1659 auto reply = chassis::internal::dbus.call(method);
1660 if (reply.is_method_error())
1661 {
1662 phosphor::logging::log<level::ERR>("Unspecified Error");
1663 *data_len = 0;
1664 return IPMI_CC_UNSPECIFIED_ERROR;
1665 }
1666 }
1667 catch (InternalFailure& e)
1668 {
1669 report<InternalFailure>();
1670 *data_len = 0;
1671 return IPMI_CC_UNSPECIFIED_ERROR;
1672 }
1673
Yong Licb89c0e2019-01-18 17:54:32 +08001674 *resptr = power_policy::allSupport;
Yong Lic6713cf2018-09-12 12:35:13 +08001675 *data_len = power_policy::setPolicyReqLen;
1676 return IPMI_CC_OK;
1677}
1678
Adriana Kobylak40814c62015-10-27 15:58:44 -05001679void register_netfn_chassis_functions()
1680{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001681 createIdentifyTimer();
1682
Tom05732372016-09-06 17:21:23 +05301683 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001684 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1685 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001686
Tom05732372016-09-06 17:21:23 +05301687 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001688 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1689 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001690
Yong Liae4b0402018-11-02 11:12:14 +08001691 // Set Chassis Capabilities
1692 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1693 ipmi_set_chassis_cap, PRIVILEGE_USER);
1694
Tom05732372016-09-06 17:21:23 +05301695 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301696 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001697 ipmi_chassis_get_sys_boot_options,
1698 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001699
Tom05732372016-09-06 17:21:23 +05301700 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001701 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1702 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001703
Tom05732372016-09-06 17:21:23 +05301704 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001705 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1706 ipmi::chassis::cmdChassisControl,
1707 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001708
Tom Joseph5110c122018-03-23 17:55:40 +05301709 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001710 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1711 ipmi::chassis::cmdChassisIdentify,
1712 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301713
Tom05732372016-09-06 17:21:23 +05301714 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301715 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001716 ipmi_chassis_set_sys_boot_options,
1717 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001718 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001719 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1720 ipmi::chassis::cmdGetPohCounter,
1721 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001722
1723 // <Set Power Restore Policy>
1724 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1725 ipmi_chassis_set_power_restore_policy,
1726 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001727}