blob: 6e1341cc82de07a16188bbb4c784d22d8100e121 [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
Patrick Venture0b02be92018-08-31 11:55:55 -07005#include <arpa/inet.h>
6#include <endian.h>
7#include <limits.h>
8#include <mapper.h>
9#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070010
Ratan Guptafd28dd72016-08-01 04:58:01 -050011#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070012#include <chrono>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070013#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080014#include <filesystem>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050015#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053016#include <future>
Vernon Mauerye08fbff2019-04-03 09:19:34 -070017#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070018#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070019#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070020#include <map>
21#include <phosphor-logging/elog-errors.hpp>
22#include <phosphor-logging/log.hpp>
23#include <sdbusplus/bus.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070024#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070025#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070026#include <sdbusplus/timer.hpp>
Vernon Mauerye278ead2018-10-09 09:23:43 -070027#include <settings.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070028#include <sstream>
Patrick Venture3a5071a2018-09-12 13:27:42 -070029#include <string>
30#include <xyz/openbmc_project/Common/error.hpp>
31#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
32#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
33#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
34#include <xyz/openbmc_project/State/Host/server.hpp>
35#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
36
Patrick Venture0b02be92018-08-31 11:55:55 -070037// Defines
38#define SET_PARM_VERSION 0x01
Vernon Mauery1181af72018-10-08 12:05:00 -070039#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40
40#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80
41#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0
ratagupta6f6bff2016-04-04 06:20:11 -050042
Lei YU4b0ddb62019-01-25 16:43:50 +080043std::unique_ptr<phosphor::Timer> identifyTimer
44 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050045
Yong Lif4e38512019-05-21 14:46:55 +080046static ChassisIDState chassisIDState = ChassisIDState::reserved;
47
Patrick Venture0b02be92018-08-31 11:55:55 -070048constexpr size_t SIZE_MAC = 18;
49constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
50 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
51 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050052constexpr size_t SIZE_PREFIX = 7;
53constexpr size_t MAX_PREFIX_VALUE = 32;
54constexpr size_t SIZE_COOKIE = 4;
55constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053056constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053057
Patrick Venture0b02be92018-08-31 11:55:55 -070058// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053059static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070060 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050061
62static constexpr size_t COOKIE_OFFSET = 1;
63static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053064static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050065static constexpr size_t MAC_OFFSET = 9;
66static constexpr size_t ADDRTYPE_OFFSET = 16;
67static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050068
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050069static constexpr size_t encIdentifyObjectsSize = 1;
70static constexpr size_t chassisIdentifyReqLength = 2;
71static constexpr size_t identifyIntervalPos = 0;
72static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080073
Adriana Kobylak40814c62015-10-27 15:58:44 -050074void register_netfn_chassis_functions() __attribute__((constructor));
75
shgoupfd84fbbf2015-12-17 10:05:51 +080076// Host settings in dbus
77// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070078const char* settings_object_name = "/org/openbmc/settings/host0";
79const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070080const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053081 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080082
Ratan Guptadcb10672017-07-10 10:33:50 +053083constexpr auto SETTINGS_ROOT = "/";
84constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053085
86constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
87constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
88
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050089static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
90static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070091 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050092static constexpr auto pOHCounterProperty = "POHCounter";
93static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080094const static constexpr char chassisCapIntf[] =
95 "xyz.openbmc_project.Control.ChassisCapabilities";
96const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
97const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
98const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
99const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
100const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
101const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
102static constexpr uint8_t chassisCapFlagMask = 0x0f;
103static constexpr uint8_t chassisCapAddrMask = 0xfe;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700104static constexpr const char* powerButtonIntf =
105 "xyz.openbmc_project.Chassis.Buttons.Power";
106static constexpr const char* powerButtonPath =
107 "/xyz/openbmc_project/Chassis/Buttons/Power0";
108static constexpr const char* resetButtonIntf =
109 "xyz.openbmc_project.Chassis.Buttons.Reset";
110static constexpr const char* resetButtonPath =
111 "/xyz/openbmc_project/Chassis/Buttons/Reset0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530112
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530113// Phosphor Host State manager
114namespace State = sdbusplus::xyz::openbmc_project::State::server;
115
Vernon Mauery185b9f82018-07-20 10:52:36 -0700116namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500117
Ratan Guptadcb10672017-07-10 10:33:50 +0530118using namespace phosphor::logging;
119using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500120using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700121
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500122namespace chassis
123{
124namespace internal
125{
126
127constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
128constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500129constexpr auto powerRestoreIntf =
130 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500131sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
132
133namespace cache
134{
135
James Feist225dec82019-11-26 16:25:06 -0800136std::unique_ptr<settings::Objects> objectsPtr = nullptr;
137
138settings::Objects& getObjects()
139{
140 if (objectsPtr == nullptr)
141 {
142 objectsPtr = std::make_unique<settings::Objects>(
143 dbus, std::vector<std::string>{bootModeIntf, bootSourceIntf,
144 powerRestoreIntf});
145 }
146 return *objectsPtr;
147}
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500148
149} // namespace cache
150} // namespace internal
151} // namespace chassis
152
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500153namespace poh
154{
155
156constexpr auto minutesPerCount = 60;
157
158} // namespace poh
159
Patrick Venture0b02be92018-08-31 11:55:55 -0700160struct get_sys_boot_options_t
161{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500162 uint8_t parameter;
163 uint8_t set;
164 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700165} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500166
Patrick Venture0b02be92018-08-31 11:55:55 -0700167struct get_sys_boot_options_response_t
168{
shgoupfd84fbbf2015-12-17 10:05:51 +0800169 uint8_t version;
170 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500171 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700172} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800173
Patrick Venture0b02be92018-08-31 11:55:55 -0700174struct set_sys_boot_options_t
175{
shgoupfd84fbbf2015-12-17 10:05:51 +0800176 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500177 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700178} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500179
Ratan Guptadcb10672017-07-10 10:33:50 +0530180int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500181{
Ratan Guptadcb10672017-07-10 10:33:50 +0530182 ipmi::PropertyMap properties;
183 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530184 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500185
Ratan Guptadcb10672017-07-10 10:33:50 +0530186 try
187 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700188 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530189 // objects,to handle such cases we are interested on that object
190 // which are on interested busname.
191 // Currenlty mapper doesn't give the readable busname(gives busid)
192 // so we can't match with bus name so giving some object specific info
193 // as SETTINGS_MATCH.
194 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500195
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530196 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530197
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530198 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
199 SETTINGS_ROOT, SETTINGS_MATCH);
200
201 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
202 SETTINGS_ROOT, SETTINGS_MATCH);
203
Patrick Venture0b02be92018-08-31 11:55:55 -0700204 properties = ipmi::getAllDbusProperties(
205 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
206 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
207 macObjectInfo.first, MAC_INTERFACE,
208 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530209
Vernon Maueryf442e112019-04-09 11:44:36 -0700210 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530211
Vernon Maueryf442e112019-04-09 11:44:36 -0700212 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530213
Vernon Maueryf442e112019-04-09 11:44:36 -0700214 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530215
Patrick Venture0b02be92018-08-31 11:55:55 -0700216 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700217 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700218 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
219 ? 1
220 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530221
Vernon Maueryf442e112019-04-09 11:44:36 -0700222 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530223
Ratan Guptad70f4532017-08-04 02:07:31 +0530224 // it is expected here that we should get the valid data
225 // but we may also get the default values.
226 // Validation of the data is done by settings.
227 //
228 // if mac address is default mac address then
229 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530230 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530231 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700232 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530233 rc = -1;
234 return rc;
235 }
236 // if addr is static then ipaddress,gateway,prefix
237 // should not be default one,don't send blank override.
238 if (isStatic)
239 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700240 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
241 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530242 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700243 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530244 rc = -1;
245 return rc;
246 }
247 }
248
Patrick Venture0b02be92018-08-31 11:55:55 -0700249 sscanf(
250 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
251 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
252 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
253 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530254
Ratan Guptadcb10672017-07-10 10:33:50 +0530255 respptr->data[MAC_OFFSET + 6] = 0x00;
256
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700257 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
258 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530259
Vernon Maueryf442e112019-04-09 11:44:36 -0700260 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
261 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
262 ? AF_INET
263 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530264
Patrick Venture0b02be92018-08-31 11:55:55 -0700265 addrSize = (addressFamily == AF_INET)
266 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
267 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530268
269 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530270 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700271 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530272
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530273 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
274
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700275 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530276
277 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
278
Ratan Guptad70f4532017-08-04 02:07:31 +0530279 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700280 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530281 }
282 catch (InternalFailure& e)
283 {
284 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700285 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530286 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500287 return rc;
288 }
289
Patrick Venture0b02be92018-08-31 11:55:55 -0700290 // PetiBoot-Specific
291 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700292 std::memcpy(respptr->data, net_conf_initial_bytes,
293 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500294
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700295 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530296
Ratan Guptafd28dd72016-08-01 04:58:01 -0500297#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700298 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500299
Ratan Guptadcb10672017-07-10 10:33:50 +0530300 for (uint8_t pos = 0; pos < index; pos++)
301 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700302 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530303 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500304#endif
305
Ratan Guptafd28dd72016-08-01 04:58:01 -0500306 return rc;
307}
308
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530309/** @brief convert IPv4 and IPv6 addresses from binary to text form.
310 * @param[in] family - IPv4/Ipv6
311 * @param[in] data - req data pointer.
312 * @param[in] offset - offset in the data.
313 * @param[in] addrSize - size of the data which needs to be read from offset.
314 * @returns address in text form.
315 */
316
Patrick Venture0b02be92018-08-31 11:55:55 -0700317std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
318 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530319{
320 char ipAddr[INET6_ADDRSTRLEN] = {};
321
Patrick Venture0b02be92018-08-31 11:55:55 -0700322 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530323 {
324 case AF_INET:
325 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700326 struct sockaddr_in addr4
327 {
328 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700329 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530330
Patrick Venture0b02be92018-08-31 11:55:55 -0700331 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530332
333 break;
334 }
335 case AF_INET6:
336 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700337 struct sockaddr_in6 addr6
338 {
339 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700340 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530341
Patrick Venture0b02be92018-08-31 11:55:55 -0700342 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530343
344 break;
345 }
346 default:
347 {
348 return {};
349 }
350 }
351
352 return ipAddr;
353}
354
Ratan Guptadcb10672017-07-10 10:33:50 +0530355int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500356{
Ratan Guptadcb10672017-07-10 10:33:50 +0530357 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500358 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700359 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530360 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700361 char addrOrigin{0};
362 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530363 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530364 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700365 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
366 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530367 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530368 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500369
Patrick Venture0b02be92018-08-31 11:55:55 -0700370 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500371 // version starts from sixth byte
372
Ratan Guptadcb10672017-07-10 10:33:50 +0530373 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500374 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530375 do
376 {
377 // cookie == 0x21 0x70 0x62 0x21
378 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700379 (net_conf_initial_bytes + COOKIE_OFFSET),
380 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530381 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700382 // cookie == 0
383 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
384 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530385 {
386 // need to zero out the network settings.
387 break;
388 }
389
390 log<level::ERR>("Invalid Cookie");
391 elog<InternalFailure>();
392 }
393
394 // vesion == 0x00 0x01
395 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700396 (net_conf_initial_bytes + VERSION_OFFSET),
397 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530398 {
399
400 log<level::ERR>("Invalid Version");
401 elog<InternalFailure>();
402 }
403
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700404 std::snprintf(
405 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
406 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
407 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
408 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530409
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700410 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
411 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530412
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530413 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530414 {
415 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530416 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530417 }
418
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530419 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700420 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
421 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530422
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530423 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530424
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700425 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
426 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530427
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530428 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
429
Ratan Gupta8c31d232017-08-13 05:49:43 +0530430 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530431 {
432 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
433 family = AF_INET6;
434 }
435
Patrick Venture0b02be92018-08-31 11:55:55 -0700436 ipAddress =
437 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530438
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530439 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
440
Patrick Venture0b02be92018-08-31 11:55:55 -0700441 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530442
Patrick Venture0b02be92018-08-31 11:55:55 -0700443 // Cookie == 0 or it is a valid cookie
444 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
445 std::to_string(prefix) + ",gateway="s + gateway +
446 ",mac="s + mac + ",addressOrigin="s +
447 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500448
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530449 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
450
451 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
452 SETTINGS_ROOT, SETTINGS_MATCH);
453 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
454 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530455 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530456 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700457 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530458 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700459 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530460 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700461 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530462 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700463 IP_INTERFACE, "Gateway", std::string(gateway));
464 ipmi::setDbusProperty(
465 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
466 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530467 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700468 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500469
Patrick Venture0b02be92018-08-31 11:55:55 -0700470 log<level::DEBUG>(
471 "Network configuration changed",
472 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500473 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530474 catch (InternalFailure& e)
475 {
476 commit<InternalFailure>();
477 return -1;
478 }
479
480 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500481}
482
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500483uint32_t getPOHCounter()
484{
485 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
486
Patrick Venture0b02be92018-08-31 11:55:55 -0700487 auto chassisStateObj =
488 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500489
Patrick Venture0b02be92018-08-31 11:55:55 -0700490 auto service =
491 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500492
Patrick Venture0b02be92018-08-31 11:55:55 -0700493 auto propValue =
494 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
495 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500496
Vernon Maueryf442e112019-04-09 11:44:36 -0700497 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500498}
499
anil kumar appana43263c62019-05-27 12:45:04 +0000500/** @brief Implements the get chassis capabilities command
501 *
502 * @returns IPMI completion code plus response data
503 * chassisCapFlags - chassis capability flag
504 * chassisFRUInfoDevAddr - chassis FRU info Device Address
505 * chassisSDRDevAddr - chassis SDR device address
506 * chassisSELDevAddr - chassis SEL device address
507 * chassisSMDevAddr - chassis system management device address
508 * chassisBridgeDevAddr - chassis bridge device address
509 */
510ipmi::RspType<uint8_t, // chassis capabilities flag
511 uint8_t, // chassis FRU info Device Address
512 uint8_t, // chassis SDR device address
513 uint8_t, // chassis SEL device address
514 uint8_t, // chassis system management device address
515 uint8_t // chassis bridge device address
516 >
517 ipmiGetChassisCap()
Nan Li8d15fb42016-08-16 22:29:40 +0800518{
anil kumar appana43263c62019-05-27 12:45:04 +0000519 ipmi::PropertyMap properties;
Yong Liae4b0402018-11-02 11:12:14 +0800520 try
521 {
522 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800523
Yong Liae4b0402018-11-02 11:12:14 +0800524 ipmi::DbusObjectInfo chassisCapObject =
525 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800526
Yong Liae4b0402018-11-02 11:12:14 +0800527 // capabilities flags
528 // [7..4] - reserved
529 // [3] – 1b = provides power interlock (IPM 1.5)
530 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
531 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
532 // has capabilities
533 // to lock out external power control and reset button or
534 // front panel interfaces and/or detect tampering with those
535 // interfaces).
536 // [0] -1b = Chassis provides intrusion (physical security) sensor.
537 // set to default value 0x0.
Nan Li8d15fb42016-08-16 22:29:40 +0800538
anil kumar appana43263c62019-05-27 12:45:04 +0000539 properties =
540 ipmi::getAllDbusProperties(bus, chassisCapObject.second,
541 chassisCapObject.first, chassisCapIntf);
Yong Liae4b0402018-11-02 11:12:14 +0800542 }
543 catch (std::exception& e)
544 {
anil kumar appana43263c62019-05-27 12:45:04 +0000545 log<level::ERR>("Failed to fetch Chassis Capability properties",
546 entry("ERROR=%s", e.what()));
547 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800548 }
549
anil kumar appana43263c62019-05-27 12:45:04 +0000550 uint8_t* chassisCapFlags =
551 std::get_if<uint8_t>(&properties[chassisCapFlagsProp]);
552 if (chassisCapFlags == nullptr)
553 {
554 log<level::ERR>("Error to get chassis capability flags");
555 return ipmi::responseUnspecifiedError();
556 }
557 uint8_t* chassisFRUInfoDevAddr =
558 std::get_if<uint8_t>(&properties[chassisFRUDevAddrProp]);
559 if (chassisFRUInfoDevAddr == nullptr)
560 {
561 log<level::ERR>("Error to get chassis FRU info device address");
562 return ipmi::responseUnspecifiedError();
563 }
564 uint8_t* chassisSDRDevAddr =
565 std::get_if<uint8_t>(&properties[chassisSDRDevAddrProp]);
566 if (chassisSDRDevAddr == nullptr)
567 {
568 log<level::ERR>("Error to get chassis SDR device address");
569 return ipmi::responseUnspecifiedError();
570 }
571 uint8_t* chassisSELDevAddr =
572 std::get_if<uint8_t>(&properties[chassisSELDevAddrProp]);
573 if (chassisSELDevAddr == nullptr)
574 {
575 log<level::ERR>("Error to get chassis SEL device address");
576 return ipmi::responseUnspecifiedError();
577 }
578 uint8_t* chassisSMDevAddr =
579 std::get_if<uint8_t>(&properties[chassisSMDevAddrProp]);
580 if (chassisSMDevAddr == nullptr)
581 {
582 log<level::ERR>("Error to get chassis SM device address");
583 return ipmi::responseUnspecifiedError();
584 }
585 uint8_t* chassisBridgeDevAddr =
586 std::get_if<uint8_t>(&properties[chassisBridgeDevAddrProp]);
587 if (chassisBridgeDevAddr == nullptr)
588 {
589 log<level::ERR>("Error to get chassis bridge device address");
590 return ipmi::responseUnspecifiedError();
591 }
592
593 return ipmi::responseSuccess(*chassisCapFlags, *chassisFRUInfoDevAddr,
594 *chassisSDRDevAddr, *chassisSELDevAddr,
595 *chassisSMDevAddr, *chassisBridgeDevAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800596}
597
anil kumar appana894d0222019-05-27 16:32:14 +0000598/** @brief implements set chassis capalibities command
599 * @param intrusion - chassis intrusion
600 * @param fpLockout - frontpannel lockout
601 * @param reserved1 - skip one bit
602 * @param fruDeviceAddr - chassis FRU info Device Address
603 * @param sdrDeviceAddr - chassis SDR device address
604 * @param selDeviceAddr - chassis SEL device address
605 * @param smDeviceAddr - chassis system management device address
606 * @param bridgeDeviceAddr - chassis bridge device address
607 *
608 * @returns IPMI completion code
609 */
610ipmi::RspType<> ipmiSetChassisCap(bool intrusion, bool fpLockout,
611 uint6_t reserved1,
612
613 uint8_t fruDeviceAddr,
614
615 uint8_t sdrDeviceAddr,
616
617 uint8_t selDeviceAddr,
618
619 uint8_t smDeviceAddr,
620
621 uint8_t bridgeDeviceAddr)
Yong Liae4b0402018-11-02 11:12:14 +0800622{
Yong Liae4b0402018-11-02 11:12:14 +0800623
624 // check input data
anil kumar appana894d0222019-05-27 16:32:14 +0000625 if (reserved1 != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800626 {
anil kumar appana894d0222019-05-27 16:32:14 +0000627 log<level::ERR>("Unsupported request parameter");
628 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800629 }
630
anil kumar appana894d0222019-05-27 16:32:14 +0000631 if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800632 {
633 log<level::ERR>("Unsupported request parameter(FRU Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000634 entry("REQ=0x%x", fruDeviceAddr));
635 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800636 }
anil kumar appana894d0222019-05-27 16:32:14 +0000637 if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800638 {
639 log<level::ERR>("Unsupported request parameter(SDR Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000640 entry("REQ=0x%x", sdrDeviceAddr));
641 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800642 }
643
anil kumar appana894d0222019-05-27 16:32:14 +0000644 if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800645 {
646 log<level::ERR>("Unsupported request parameter(SEL Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000647 entry("REQ=0x%x", selDeviceAddr));
648 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800649 }
650
anil kumar appana894d0222019-05-27 16:32:14 +0000651 if ((smDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800652 {
anil kumar appana894d0222019-05-27 16:32:14 +0000653 log<level::ERR>("Unsupported request parameter(SM Addr)",
654 entry("REQ=0x%x", smDeviceAddr));
655 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800656 }
657
anil kumar appana894d0222019-05-27 16:32:14 +0000658 if ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800659 {
660 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000661 entry("REQ=0x%x", bridgeDeviceAddr));
662 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800663 }
664
anil kumar appana894d0222019-05-27 16:32:14 +0000665 uint8_t capFlags = (static_cast<uint8_t>(intrusion)) |
666 ((static_cast<uint8_t>(fpLockout)) << 1);
Yong Liae4b0402018-11-02 11:12:14 +0800667 try
668 {
669 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
670 ipmi::DbusObjectInfo chassisCapObject =
671 ipmi::getDbusObject(bus, chassisCapIntf);
672
673 ipmi::setDbusProperty(bus, chassisCapObject.second,
674 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000675 chassisCapFlagsProp, capFlags);
Yong Liae4b0402018-11-02 11:12:14 +0800676
677 ipmi::setDbusProperty(bus, chassisCapObject.second,
678 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000679 chassisFRUDevAddrProp, fruDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800680
681 ipmi::setDbusProperty(bus, chassisCapObject.second,
682 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000683 chassisSDRDevAddrProp, sdrDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800684
685 ipmi::setDbusProperty(bus, chassisCapObject.second,
686 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000687 chassisSELDevAddrProp, selDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800688
689 ipmi::setDbusProperty(bus, chassisCapObject.second,
690 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000691 chassisSMDevAddrProp, smDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800692
693 ipmi::setDbusProperty(bus, chassisCapObject.second,
694 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000695 chassisBridgeDevAddrProp, bridgeDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800696 }
697 catch (std::exception& e)
698 {
699 log<level::ERR>(e.what());
anil kumar appana894d0222019-05-27 16:32:14 +0000700 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800701 }
anil kumar appana894d0222019-05-27 16:32:14 +0000702 return ipmi::responseSuccess();
Nan Li8d15fb42016-08-16 22:29:40 +0800703}
704
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530705//------------------------------------------
706// Calls into Host State Manager Dbus object
707//------------------------------------------
708int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600709{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500710 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700711 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500712 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700713 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
714 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530715
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500716 // sd_bus error
717 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700718 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600719
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500720 // SD Bus error report mechanism.
721 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600722
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500723 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700724 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500725 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
726 if (rc < 0)
727 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700728 log<level::ERR>(
729 "Failed to get bus name",
730 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500731 return rc;
732 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530733
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500734 // Convert to string equivalent of the passed in transition enum.
735 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530736
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500737 rc = sd_bus_call_method(bus_type, // On the system bus
738 busname, // Service to contact
739 HOST_STATE_MANAGER_ROOT, // Object path
740 DBUS_PROPERTY_IFACE, // Interface name
741 "Set", // Method to be called
742 &bus_error, // object to return error
743 nullptr, // Response buffer if any
744 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700745 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
746 request.c_str());
747 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500748 {
749 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530750 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500751 }
752 else
753 {
754 log<level::INFO>("Transition request initiated successfully");
755 }
vishwa36993272015-11-20 12:43:49 -0600756
757 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500758 free(busname);
vishwa36993272015-11-20 12:43:49 -0600759
Sergey Solomineb9b8142016-08-23 09:07:28 -0500760 return rc;
vishwa36993272015-11-20 12:43:49 -0600761}
762
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800763//------------------------------------------
764// Set Enabled property to inform NMI source
765// handling to trigger a NMI_OUT BSOD.
766//------------------------------------------
767int setNmiProperty(const bool value)
768{
769 constexpr const char* nmiSourceObjPath =
770 "/xyz/openbmc_project/Chassis/Control/NMISource";
771 constexpr const char* nmiSourceIntf =
772 "xyz.openbmc_project.Chassis.Control.NMISource";
773 std::string bmcSourceSignal = "xyz.openbmc_project.Chassis.Control."
774 "NMISource.BMCSourceSignal.ChassisCmd";
775 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
776
777 try
778 {
779 auto service = ipmi::getService(*busp, nmiSourceIntf, nmiSourceObjPath);
780 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
781 "BMCSource", bmcSourceSignal);
782 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
783 "Enabled", value);
784 }
785 catch (std::exception& e)
786 {
787 log<level::ERR>("Failed to trigger NMI_OUT",
788 entry("EXCEPTION=%s", e.what()));
789 return -1;
790 }
791
792 return 0;
793}
794
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500795namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500796{
Nan Lifdd8ec52016-08-28 03:57:40 +0800797
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500798using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
799using IpmiValue = uint8_t;
800using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800801
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700802const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500803 {RestorePolicy::Policy::AlwaysOff, 0x00},
804 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700805 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800806
Yong Lic6713cf2018-09-12 12:35:13 +0800807static constexpr uint8_t noChange = 0x03;
808static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700809
810/* helper function for Get Chassis Status Command
811 */
812std::optional<uint2_t> getPowerRestorePolicy()
813{
814 uint2_t restorePolicy = 0;
815 using namespace chassis::internal;
816
James Feist225dec82019-11-26 16:25:06 -0800817 settings::Objects& objects = cache::getObjects();
818
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700819 try
820 {
821 const auto& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -0800822 objects.map.at(powerRestoreIntf).front();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700823 ipmi::Value result = ipmi::getDbusProperty(
824 *getSdBus(),
James Feist225dec82019-11-26 16:25:06 -0800825 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700826 powerRestoreSetting.c_str(), powerRestoreIntf,
827 "PowerRestorePolicy");
828 auto powerRestore = RestorePolicy::convertPolicyFromString(
829 std::get<std::string>(result));
830 restorePolicy = dbusToIpmi.at(powerRestore);
831 }
832 catch (const std::exception& e)
833 {
834 log<level::ERR>(
835 "Failed to fetch pgood property", entry("ERROR=%s", e.what()),
James Feist225dec82019-11-26 16:25:06 -0800836 entry("PATH=%s", objects.map.at(powerRestoreIntf).front().c_str()),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700837 entry("INTERFACE=%s", powerRestoreIntf));
James Feist225dec82019-11-26 16:25:06 -0800838 cache::objectsPtr.reset();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700839 return std::nullopt;
840 }
841 return std::make_optional(restorePolicy);
842}
843
844/*
845 * getPowerStatus
846 * helper function for Get Chassis Status Command
847 * return - optional value for pgood (no value on error)
848 */
849std::optional<bool> getPowerStatus()
850{
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700851 bool powerGood = false;
852 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
853 try
854 {
Jason M. Bills3de424c2019-05-21 09:57:16 -0700855 constexpr const char* chassisStatePath =
856 "/xyz/openbmc_project/state/chassis0";
857 constexpr const char* chassisStateIntf =
858 "xyz.openbmc_project.State.Chassis";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700859 auto service =
Jason M. Bills3de424c2019-05-21 09:57:16 -0700860 ipmi::getService(*busp, chassisStateIntf, chassisStatePath);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700861
Jason M. Bills3de424c2019-05-21 09:57:16 -0700862 ipmi::Value powerState =
863 ipmi::getDbusProperty(*busp, service, chassisStatePath,
864 chassisStateIntf, "CurrentPowerState");
865 powerGood = std::get<std::string>(powerState) ==
866 "xyz.openbmc_project.State.Chassis.PowerState.On";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700867 }
868 catch (const std::exception& e)
869 {
870 try
871 {
872 // FIXME: some legacy modules use the older path; try that next
873 constexpr const char* legacyPwrCtrlObj =
874 "/org/openbmc/control/power0";
875 constexpr const char* legacyPwrCtrlIntf =
876 "org.openbmc.control.Power";
877 auto service =
878 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
879
880 ipmi::Value variant = ipmi::getDbusProperty(
881 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
882 powerGood = static_cast<bool>(std::get<int>(variant));
883 }
884 catch (const std::exception& e)
885 {
886 log<level::ERR>("Failed to fetch pgood property",
Jason M. Bills3de424c2019-05-21 09:57:16 -0700887 entry("ERROR=%s", e.what()));
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700888 return std::nullopt;
889 }
890 }
891 return std::make_optional(powerGood);
892}
893
Yong Li70ce7352019-05-16 21:15:27 +0800894/*
895 * getACFailStatus
896 * helper function for Get Chassis Status Command
897 * return - bool value for ACFail (false on error)
898 */
899bool getACFailStatus()
900{
901 constexpr const char* powerControlObj =
902 "/xyz/openbmc_project/Chassis/Control/Power0";
903 constexpr const char* powerControlIntf =
904 "xyz.openbmc_project.Chassis.Control.Power";
905 bool acFail = false;
906 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
907 try
908 {
909 auto service =
910 ipmi::getService(*bus, powerControlIntf, powerControlObj);
911
912 ipmi::Value variant = ipmi::getDbusProperty(
913 *bus, service, powerControlObj, powerControlIntf, "PFail");
914 acFail = std::get<bool>(variant);
915 }
916 catch (const std::exception& e)
917 {
918 log<level::ERR>("Failed to fetch PFail property",
919 entry("ERROR=%s", e.what()),
920 entry("PATH=%s", powerControlObj),
921 entry("INTERFACE=%s", powerControlIntf));
922 }
923 return acFail;
924}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500925} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800926
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700927static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
928 const std::string& buttonIntf)
929{
930 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
931 bool buttonDisabled = false;
932 try
933 {
934 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
935 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
936 buttonIntf, "Enabled");
937 buttonDisabled = !std::get<bool>(enabled);
938 }
939 catch (sdbusplus::exception::SdBusError& e)
940 {
941 log<level::ERR>("Fail to get button Enabled property",
942 entry("PATH=%s", buttonPath.c_str()),
943 entry("ERROR=%s", e.what()));
944 return std::nullopt;
945 }
946 return std::make_optional(buttonDisabled);
947}
948
Nan Lifdd8ec52016-08-28 03:57:40 +0800949//----------------------------------------------------------------------
950// Get Chassis Status commands
951//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700952ipmi::RspType<bool, // Power is on
953 bool, // Power overload
954 bool, // Interlock
955 bool, // power fault
956 bool, // power control fault
957 uint2_t, // power restore policy
958 bool, // reserved
959
960 bool, // AC failed
961 bool, // last power down caused by a Power overload
962 bool, // last power down caused by a power interlock
963 bool, // last power down caused by power fault
964 bool, // last ‘Power is on’ state was entered via IPMI command
965 uint3_t, // reserved
966
967 bool, // Chassis intrusion active
968 bool, // Front Panel Lockout active
969 bool, // Drive Fault
970 bool, // Cooling/fan fault detected
971 uint2_t, // Chassis Identify State
972 bool, // Chassis Identify command and state info supported
973 bool, // reserved
974
975 bool, // Power off button disabled
976 bool, // Reset button disabled
977 bool, // Diagnostic Interrupt button disabled
978 bool, // Standby (sleep) button disabled
979 bool, // Power off button disable allowed
980 bool, // Reset button disable allowed
981 bool, // Diagnostic Interrupt button disable allowed
982 bool // Standby (sleep) button disable allowed
983 >
984 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +0800985{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500986 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700987 std::optional<uint2_t> restorePolicy =
988 power_policy::getPowerRestorePolicy();
989 std::optional<bool> powerGood = power_policy::getPowerStatus();
990 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500991 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700992 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500993 }
Nan Lifdd8ec52016-08-28 03:57:40 +0800994
995 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700996 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700997 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -0700998 // allow disable if the interface is present
999 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
1000 // default return the button is enabled (not disabled)
1001 bool powerButtonDisabled = false;
1002 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001003 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001004 // return the real value of the button status, if present
1005 powerButtonDisabled = *powerButtonReading;
1006 }
1007
1008 std::optional<bool> resetButtonReading =
1009 getButtonEnabled(resetButtonPath, resetButtonIntf);
1010 // allow disable if the interface is present
1011 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
1012 // default return the button is enabled (not disabled)
1013 bool resetButtonDisabled = false;
1014 if (resetButtonDisableAllow)
1015 {
1016 // return the real value of the button status, if present
1017 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001018 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001019
Yong Li70ce7352019-05-16 21:15:27 +08001020 bool powerDownAcFailed = power_policy::getACFailStatus();
1021
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001022 // This response has a lot of hard-coded, unsupported fields
1023 // They are set to false or 0
1024 constexpr bool powerOverload = false;
1025 constexpr bool chassisInterlock = false;
1026 constexpr bool powerFault = false;
1027 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001028 constexpr bool powerDownOverload = false;
1029 constexpr bool powerDownInterlock = false;
1030 constexpr bool powerDownPowerFault = false;
1031 constexpr bool powerStatusIPMI = false;
1032 constexpr bool chassisIntrusionActive = false;
1033 constexpr bool frontPanelLockoutActive = false;
1034 constexpr bool driveFault = false;
1035 constexpr bool coolingFanFault = false;
1036 // chassisIdentifySupport set because this command is implemented
1037 constexpr bool chassisIdentifySupport = true;
Yong Lif4e38512019-05-21 14:46:55 +08001038 uint2_t chassisIdentifyState = static_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001039 constexpr bool diagButtonDisabled = false;
1040 constexpr bool sleepButtonDisabled = false;
1041 constexpr bool diagButtonDisableAllow = false;
1042 constexpr bool sleepButtonDisableAllow = false;
1043
1044 return ipmi::responseSuccess(
1045 *powerGood, powerOverload, chassisInterlock, powerFault,
1046 powerControlFault, *restorePolicy,
1047 false, // reserved
1048
1049 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1050 powerDownPowerFault, powerStatusIPMI,
1051 uint3_t(0), // reserved
1052
1053 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1054 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1055 false, // reserved
1056
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001057 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001058 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1059 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001060}
Chris Austen7888c4d2015-12-03 15:26:20 -06001061
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301062//-------------------------------------------------------------
1063// Send a command to SoftPowerOff application to stop any timer
1064//-------------------------------------------------------------
1065int stop_soft_off_timer()
1066{
Patrick Venture0b02be92018-08-31 11:55:55 -07001067 constexpr auto iface = "org.freedesktop.DBus.Properties";
1068 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1069 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301070
Patrick Venture0b02be92018-08-31 11:55:55 -07001071 constexpr auto property = "ResponseReceived";
1072 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1073 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301074
1075 // Get the system bus where most system services are provided.
1076 auto bus = ipmid_get_sd_bus_connection();
1077
1078 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001079 // TODO openbmc/openbmc#1661 - Mapper refactor
1080 //
1081 // See openbmc/openbmc#1743 for some details but high level summary is that
1082 // for now the code will directly call the soft off interface due to a
1083 // race condition with mapper usage
1084 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001085 // char *busname = nullptr;
1086 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1087 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001088 //{
1089 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301090 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001091 // return r;
1092 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301093
1094 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001095 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001096 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1097 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301098 if (rc < 0)
1099 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301100 log<level::ERR>("Failed to set property in SoftPowerOff object",
1101 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301102 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001103
Patrick Venture0b02be92018-08-31 11:55:55 -07001104 // TODO openbmc/openbmc#1661 - Mapper refactor
1105 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301106 return rc;
1107}
1108
vishwa36993272015-11-20 12:43:49 -06001109//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001110// Create file to indicate there is no need for softoff notification to host
1111//----------------------------------------------------------------------
1112void indicate_no_softoff_needed()
1113{
1114 fs::path path{HOST_INBAND_REQUEST_DIR};
1115 if (!fs::is_directory(path))
1116 {
1117 fs::create_directory(path);
1118 }
1119
1120 // Add the host instance (default 0 for now) to the file name
1121 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001122 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001123 size++; // null
1124 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001125 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001126
1127 // Append file name to directory and create it
1128 path /= buf.get();
1129 std::ofstream(path.c_str());
1130}
1131
anil kumar appanadafff5f2019-04-27 18:06:00 +00001132/** @brief Implementation of chassis control command
1133 *
1134 * @param - chassisControl command byte
1135 *
1136 * @return Success or InvalidFieldRequest.
1137 */
1138ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001139{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001140 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001141 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001142 {
1143 case CMD_POWER_ON:
1144 rc = initiate_state_transition(State::Host::Transition::On);
1145 break;
1146 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301147 // This path would be hit in 2 conditions.
1148 // 1: When user asks for power off using ipmi chassis command 0x04
1149 // 2: Host asking for power off post shutting down.
1150
1151 // If it's a host requested power off, then need to nudge Softoff
1152 // application that it needs to stop the watchdog timer if running.
1153 // If it is a user requested power off, then this is not really
1154 // needed. But then we need to differentiate between user and host
1155 // calling this same command
1156
1157 // For now, we are going ahead with trying to nudge the soft off and
1158 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001159 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301160
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001161 // Only request the Off transition if the soft power off
1162 // application is not running
1163 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001164 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001165 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301166 // that it should not run. Not doing this will result in State
1167 // manager doing a default soft power off when asked for power
1168 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001169 indicate_no_softoff_needed();
1170
1171 // Now request the shutdown
1172 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001173 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001174 else
1175 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301176 log<level::INFO>("Soft off is running, so let shutdown target "
1177 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001178 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001179 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301180
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001181 case CMD_HARD_RESET:
1182 case CMD_POWER_CYCLE:
1183 // SPEC has a section that says certain implementations can trigger
1184 // PowerOn if power is Off when a command to power cycle is
1185 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001186
1187 // First create a file to indicate to the soft off application
1188 // that it should not run since this is a direct user initiated
1189 // power reboot request (i.e. a reboot request that is not
1190 // originating via a soft power off SMS request)
1191 indicate_no_softoff_needed();
1192
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001193 rc = initiate_state_transition(State::Host::Transition::Reboot);
1194 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301195
1196 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1197 // Request Host State Manager to do a soft power off
1198 rc = initiate_state_transition(State::Host::Transition::Off);
1199 break;
1200
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001201 case CMD_PULSE_DIAGNOSTIC_INTR:
1202 rc = setNmiProperty(true);
1203 break;
1204
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001205 default:
1206 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301207 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001208 entry("CMD=0x%X", chassisControl));
1209 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001210 }
1211 }
vishwa36993272015-11-20 12:43:49 -06001212
anil kumar appanadafff5f2019-04-27 18:06:00 +00001213 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1214 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001215}
1216
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001217/** @brief Return D-Bus connection string to enclosure identify LED object
1218 *
1219 * @param[in, out] connection - connection to D-Bus object
1220 * @return a IPMI return code
1221 */
1222std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301223{
Tom Joseph5110c122018-03-23 17:55:40 +05301224 // lookup enclosure_identify group owner(s) in mapper
1225 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001226 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1227 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301228
1229 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001230 static const std::vector<std::string> interfaces = {
1231 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301232 mapperCall.append(interfaces);
1233 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1234 if (mapperReply.is_method_error())
1235 {
1236 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001237 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301238 }
1239 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1240 mapperReply.read(mapperResp);
1241
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001242 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301243 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001244 log<level::ERR>(
1245 "Invalid number of enclosure identify objects.",
1246 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001247 elog<InternalFailure>();
1248 }
1249 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1250 return pair.first;
1251}
Tom Joseph5110c122018-03-23 17:55:40 +05301252
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001253/** @brief Turn On/Off enclosure identify LED
1254 *
1255 * @param[in] flag - true to turn on LED, false to turn off
1256 * @return a IPMI return code
1257 */
1258void enclosureIdentifyLed(bool flag)
1259{
1260 using namespace chassis::internal;
1261 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001262 auto msg = std::string("enclosureIdentifyLed(") +
1263 boost::lexical_cast<std::string>(flag) + ")";
1264 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001265 auto led =
1266 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1267 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001268 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001269 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001270 auto ledReply = dbus.call(led);
1271 if (ledReply.is_method_error())
1272 {
1273 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001274 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001275 elog<InternalFailure>();
1276 }
1277}
1278
1279/** @brief Callback method to turn off LED
1280 */
1281void enclosureIdentifyLedOff()
1282{
1283 try
1284 {
Yong Lif4e38512019-05-21 14:46:55 +08001285 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001286 enclosureIdentifyLed(false);
1287 }
1288 catch (const InternalFailure& e)
1289 {
1290 report<InternalFailure>();
1291 }
1292}
1293
1294/** @brief Create timer to turn on and off the enclosure LED
1295 */
1296void createIdentifyTimer()
1297{
1298 if (!identifyTimer)
1299 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001300 identifyTimer =
1301 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001302 }
1303}
1304
Vernon Mauery400cc782018-10-09 13:49:53 -07001305ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1306 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001307{
Vernon Mauery400cc782018-10-09 13:49:53 -07001308 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1309 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301310
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001311 if (identifyInterval || forceIdentify)
1312 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001313 // stop the timer if already started;
1314 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001315 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001316 try
Tom Joseph5110c122018-03-23 17:55:40 +05301317 {
Yong Lif4e38512019-05-21 14:46:55 +08001318 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001319 enclosureIdentifyLed(true);
1320 }
1321 catch (const InternalFailure& e)
1322 {
1323 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001324 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301325 }
1326
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001327 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301328 {
Yong Lif4e38512019-05-21 14:46:55 +08001329 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001330 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001331 }
1332 // start the timer
1333 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001334 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001335 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301336 }
Tom Josephbed26992018-07-31 23:00:24 +05301337 else if (!identifyInterval)
1338 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001339 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301340 enclosureIdentifyLedOff();
1341 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001342 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301343}
1344
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001345namespace boot_options
1346{
1347
1348using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1349using IpmiValue = uint8_t;
1350constexpr auto ipmiDefault = 0;
1351
Patrick Venture0b02be92018-08-31 11:55:55 -07001352std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001353 {0x01, Source::Sources::Network},
1354 {0x02, Source::Sources::Disk},
1355 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001356 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001357 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001358
Patrick Venture0b02be92018-08-31 11:55:55 -07001359std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Yong Li5833cb62019-10-30 13:27:12 +08001360#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001361 {0x03, Mode::Modes::Safe},
Yong Li5833cb62019-10-30 13:27:12 +08001362#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001363 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001364 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001365
Patrick Venture0b02be92018-08-31 11:55:55 -07001366std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001367 {Source::Sources::Network, 0x01},
1368 {Source::Sources::Disk, 0x02},
1369 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001370 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001371 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001372
Patrick Venture0b02be92018-08-31 11:55:55 -07001373std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Yong Li5833cb62019-10-30 13:27:12 +08001374#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001375 {Mode::Modes::Safe, 0x03},
Yong Li5833cb62019-10-30 13:27:12 +08001376#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001377 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001378 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001379
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001380} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001381
Marri Devender Rao81719702018-05-07 00:53:48 -05001382/** @brief Set the property value for boot source
1383 * @param[in] source - boot source value
1384 * @return On failure return IPMI error.
1385 */
1386static ipmi_ret_t setBootSource(const Source::Sources& source)
1387{
1388 using namespace chassis::internal;
1389 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001390 std::variant<std::string> property = convertForMessage(source);
James Feist225dec82019-11-26 16:25:06 -08001391 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001392 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1393 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001394 auto method = dbus.new_method_call(
1395 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1396 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001397 method.append(bootSourceIntf, "BootSource", property);
1398 auto reply = dbus.call(method);
1399 if (reply.is_method_error())
1400 {
1401 log<level::ERR>("Error in BootSource Set");
1402 report<InternalFailure>();
1403 return IPMI_CC_UNSPECIFIED_ERROR;
1404 }
1405 return IPMI_CC_OK;
1406}
1407
Patrick Venture0b02be92018-08-31 11:55:55 -07001408/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001409 * @param[in] mode - boot mode value
1410 * @return On failure return IPMI error.
1411 */
1412static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1413{
1414 using namespace chassis::internal;
1415 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001416 std::variant<std::string> property = convertForMessage(mode);
James Feist225dec82019-11-26 16:25:06 -08001417 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001418 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1419 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001420 auto method = dbus.new_method_call(
1421 objects.service(bootModeSetting, bootModeIntf).c_str(),
1422 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001423 method.append(bootModeIntf, "BootMode", property);
1424 auto reply = dbus.call(method);
1425 if (reply.is_method_error())
1426 {
1427 log<level::ERR>("Error in BootMode Set");
1428 report<InternalFailure>();
1429 return IPMI_CC_UNSPECIFIED_ERROR;
1430 }
1431 return IPMI_CC_OK;
1432}
1433
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001434ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1435 ipmi_request_t request,
1436 ipmi_response_t response,
1437 ipmi_data_len_t data_len,
1438 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001439{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001440 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001441 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001442 char* p = NULL;
1443 get_sys_boot_options_response_t* resp =
1444 (get_sys_boot_options_response_t*)response;
1445 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001446 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001447
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001448 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001449 resp->version = SET_PARM_VERSION;
1450 resp->parm = 5;
1451 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001452
shgoupfd84fbbf2015-12-17 10:05:51 +08001453 /*
1454 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1455 * This is the only parameter used by petitboot.
1456 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001457 if (reqptr->parameter ==
1458 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1459 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001460
Ratan Guptafd28dd72016-08-01 04:58:01 -05001461 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001462 using namespace chassis::internal;
1463 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001464
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001465 try
ratagupta6f6bff2016-04-04 06:20:11 -05001466 {
James Feist225dec82019-11-26 16:25:06 -08001467 settings::Objects& objects = getObjects();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001468 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1469 const auto& bootSourceSetting =
1470 std::get<settings::Path>(bootSetting);
1471 auto oneTimeEnabled =
1472 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001473 auto method = dbus.new_method_call(
1474 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1475 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001476 method.append(bootSourceIntf, "BootSource");
1477 auto reply = dbus.call(method);
1478 if (reply.is_method_error())
1479 {
1480 log<level::ERR>("Error in BootSource Get");
1481 report<InternalFailure>();
1482 *data_len = 0;
1483 return IPMI_CC_UNSPECIFIED_ERROR;
1484 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001485 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001486 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001487 auto bootSource =
1488 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001489
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001490 bootSetting = settings::boot::setting(objects, bootModeIntf);
1491 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1492 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001493 objects.service(bootModeSetting, bootModeIntf).c_str(),
1494 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001495 method.append(bootModeIntf, "BootMode");
1496 reply = dbus.call(method);
1497 if (reply.is_method_error())
1498 {
1499 log<level::ERR>("Error in BootMode Get");
1500 report<InternalFailure>();
1501 *data_len = 0;
1502 return IPMI_CC_UNSPECIFIED_ERROR;
1503 }
1504 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001505 auto bootMode =
1506 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001507
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001508 bootOption = sourceDbusToIpmi.at(bootSource);
1509 if ((Mode::Modes::Regular == bootMode) &&
1510 (Source::Sources::Default == bootSource))
1511 {
1512 bootOption = ipmiDefault;
1513 }
1514 else if (Source::Sources::Default == bootSource)
1515 {
1516 bootOption = modeDbusToIpmi.at(bootMode);
1517 }
1518 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001519
Patrick Venture0b02be92018-08-31 11:55:55 -07001520 resp->data[0] = oneTimeEnabled
1521 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1522 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001523
ratagupta6f6bff2016-04-04 06:20:11 -05001524 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001525 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001526 catch (InternalFailure& e)
1527 {
James Feist225dec82019-11-26 16:25:06 -08001528 cache::objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001529 report<InternalFailure>();
1530 *data_len = 0;
1531 return IPMI_CC_UNSPECIFIED_ERROR;
1532 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001533 }
1534 else if (reqptr->parameter ==
1535 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1536 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001537
Patrick Venture0b02be92018-08-31 11:55:55 -07001538 *data_len =
1539 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001540
Patrick Venture0b02be92018-08-31 11:55:55 -07001541 resp->parm =
1542 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001543
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001544 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001545
Patrick Venture0b02be92018-08-31 11:55:55 -07001546 if (ret < 0)
1547 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001548
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301549 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001550 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001551 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001552 }
1553 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001554 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001555 }
1556
Patrick Venture0b02be92018-08-31 11:55:55 -07001557 else
1558 {
1559 log<level::ERR>("Unsupported parameter",
1560 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001561 }
1562
1563 if (p)
1564 free(p);
1565
Ratan Guptafd28dd72016-08-01 04:58:01 -05001566 if (rc == IPMI_CC_OK)
1567 {
1568 *data_len += 2;
1569 }
1570
shgoupfd84fbbf2015-12-17 10:05:51 +08001571 return rc;
1572}
1573
shgoupfd84fbbf2015-12-17 10:05:51 +08001574ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001575 ipmi_request_t request,
1576 ipmi_response_t response,
1577 ipmi_data_len_t data_len,
1578 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001579{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001580 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001581 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001582 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001583
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001584 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1585 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001586
shgoupfd84fbbf2015-12-17 10:05:51 +08001587 // This IPMI command does not have any resposne data
1588 *data_len = 0;
1589
1590 /* 000101
1591 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1592 * This is the only parameter used by petitboot.
1593 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001594
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001595 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1596 {
1597 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1598 using namespace chassis::internal;
1599 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001600 auto oneTimeEnabled = false;
1601 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301602 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001603 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001604
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001605 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001606 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001607 bool permanent =
1608 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1609 SET_PARM_BOOT_FLAGS_PERMANENT;
1610
James Feist225dec82019-11-26 16:25:06 -08001611 settings::Objects& objects = getObjects();
1612
Patrick Venture0b02be92018-08-31 11:55:55 -07001613 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301614
1615 oneTimeEnabled =
1616 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1617
1618 /*
1619 * Check if the current boot setting is onetime or permanent, if the
1620 * request in the command is otherwise, then set the "Enabled"
1621 * property in one_time object path to 'True' to indicate onetime
1622 * and 'False' to indicate permanent.
1623 *
1624 * Once the onetime/permanent setting is applied, then the bootMode
1625 * and bootSource is updated for the corresponding object.
1626 */
1627 if ((permanent && oneTimeEnabled) ||
1628 (!permanent && !oneTimeEnabled))
1629 {
1630 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1631
Patrick Venture0b02be92018-08-31 11:55:55 -07001632 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1633 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301634 }
1635
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001636 auto modeItr = modeIpmiToDbus.find(bootOption);
1637 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1638 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001639 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001640 rc = setBootSource(sourceItr->second);
1641 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001642 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001643 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001644 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001645 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001646 // If a set boot device is mapping to a boot source, then reset
1647 // the boot mode D-Bus property to default.
1648 // This way the ipmid code can determine which property is not
1649 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001650 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001651 {
1652 setBootMode(Mode::Modes::Regular);
1653 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001654 }
1655 if (modeIpmiToDbus.end() != modeItr)
1656 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001657 rc = setBootMode(modeItr->second);
1658 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001659 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001660 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001661 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001662 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001663 // If a set boot device is mapping to a boot mode, then reset
1664 // the boot source D-Bus property to default.
1665 // This way the ipmid code can determine which property is not
1666 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001667 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001668 {
1669 setBootSource(Source::Sources::Default);
1670 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001671 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001672 if ((modeIpmiToDbus.end() == modeItr) &&
1673 (sourceIpmiToDbus.end() == sourceItr))
1674 {
1675 // return error if boot option is not supported
1676 *data_len = 0;
1677 return IPMI_CC_INVALID_FIELD_REQUEST;
1678 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001679 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001680 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001681 {
James Feist225dec82019-11-26 16:25:06 -08001682 objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001683 report<InternalFailure>();
1684 *data_len = 0;
1685 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001686 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001687 }
1688 else if (reqptr->parameter ==
1689 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1690 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001691
1692 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001693 if (ret < 0)
1694 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301695 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001696 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001697 rc = IPMI_CC_UNSPECIFIED_ERROR;
1698 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001699 }
1700 else if (reqptr->parameter ==
1701 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1702 {
Tom Josephf536c902017-09-25 18:08:15 +05301703 // Handle parameter #4 and return command completed normally
1704 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1705 // parameter. This is added to support the ipmitool command `chassis
1706 // bootdev` which sends set on parameter #4, before setting the boot
1707 // flags.
1708 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001709 }
1710 else
1711 {
1712 log<level::ERR>("Unsupported parameter",
1713 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001714 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001715 }
1716
1717 return rc;
1718}
1719
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001720/** @brief implements Get POH counter command
1721 * @parameter
1722 * - none
1723 * @returns IPMI completion code plus response data
1724 * - minPerCount - Minutes per count
1725 * - counterReading - counter reading
1726 */
1727ipmi::RspType<uint8_t, // Minutes per count
1728 uint32_t // Counter reading
1729 >
1730 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001731{
1732 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001733 try
1734 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001735 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1736 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001737 }
1738 catch (std::exception& e)
1739 {
1740 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001741 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001742 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001743}
1744
Jason M. Billsbc996a32019-06-17 15:46:37 -07001745ipmi::RspType<uint3_t, // policy support
1746 uint5_t // reserved
1747 >
Vernon Mauerye278ead2018-10-09 09:23:43 -07001748 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
Jason M. Billsbc996a32019-06-17 15:46:37 -07001749 uint3_t policy, uint5_t reserved)
Yong Lic6713cf2018-09-12 12:35:13 +08001750{
Yong Lic6713cf2018-09-12 12:35:13 +08001751 power_policy::DbusValue value =
1752 power_policy::RestorePolicy::Policy::AlwaysOff;
1753
Jason M. Billsbc996a32019-06-17 15:46:37 -07001754 if (reserved || (policy > power_policy::noChange))
Yong Lic6713cf2018-09-12 12:35:13 +08001755 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001756 phosphor::logging::log<level::ERR>(
1757 "Reserved request parameter",
1758 entry("REQ=0x%x", static_cast<int>(policy)));
Jason M. Billsbc996a32019-06-17 15:46:37 -07001759 return ipmi::responseInvalidFieldRequest();
Yong Lic6713cf2018-09-12 12:35:13 +08001760 }
1761
Vernon Mauerye278ead2018-10-09 09:23:43 -07001762 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001763 {
1764 // just return the supported policy
Jason M. Billsbc996a32019-06-17 15:46:37 -07001765 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08001766 }
1767
1768 for (auto const& it : power_policy::dbusToIpmi)
1769 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001770 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001771 {
1772 value = it.first;
1773 break;
1774 }
1775 }
1776
1777 try
1778 {
James Feist225dec82019-11-26 16:25:06 -08001779 settings::Objects& objects = chassis::internal::cache::getObjects();
Yong Lic6713cf2018-09-12 12:35:13 +08001780 const settings::Path& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -08001781 objects.map.at(chassis::internal::powerRestoreIntf).front();
Vernon Mauery16b86932019-05-01 08:36:11 -07001782 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08001783
Vernon Mauerye278ead2018-10-09 09:23:43 -07001784 auto sdbusp = getSdBus();
1785 boost::system::error_code ec;
1786 sdbusp->yield_method_call<void>(
1787 yield, ec,
James Feist225dec82019-11-26 16:25:06 -08001788 objects
Yong Lic6713cf2018-09-12 12:35:13 +08001789 .service(powerRestoreSetting,
1790 chassis::internal::powerRestoreIntf)
1791 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07001792 powerRestoreSetting, ipmi::PROP_INTF, "Set",
1793 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1794 property);
1795 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08001796 {
1797 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07001798 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001799 }
1800 }
1801 catch (InternalFailure& e)
1802 {
James Feist225dec82019-11-26 16:25:06 -08001803 chassis::internal::cache::objectsPtr.reset();
Yong Lic6713cf2018-09-12 12:35:13 +08001804 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07001805 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001806 }
1807
Jason M. Billsbc996a32019-06-17 15:46:37 -07001808 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08001809}
1810
Adriana Kobylak40814c62015-10-27 15:58:44 -05001811void register_netfn_chassis_functions()
1812{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001813 createIdentifyTimer();
1814
Tom05732372016-09-06 17:21:23 +05301815 // Get Chassis Capabilities
anil kumar appana43263c62019-05-27 12:45:04 +00001816 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1817 ipmi::chassis::cmdGetChassisCapabilities,
1818 ipmi::Privilege::User, ipmiGetChassisCap);
Nan Li8d15fb42016-08-16 22:29:40 +08001819
Yong Liae4b0402018-11-02 11:12:14 +08001820 // Set Chassis Capabilities
anil kumar appana894d0222019-05-27 16:32:14 +00001821 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1822 ipmi::chassis::cmdSetChassisCapabilities,
1823 ipmi::Privilege::User, ipmiSetChassisCap);
Yong Liae4b0402018-11-02 11:12:14 +08001824
Tom05732372016-09-06 17:21:23 +05301825 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301826 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001827 ipmi_chassis_get_sys_boot_options,
1828 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001829
Tom05732372016-09-06 17:21:23 +05301830 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001831 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1832 ipmi::chassis::cmdGetChassisStatus,
1833 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08001834
Tom05732372016-09-06 17:21:23 +05301835 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001836 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1837 ipmi::chassis::cmdChassisControl,
1838 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001839
Tom Joseph5110c122018-03-23 17:55:40 +05301840 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001841 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1842 ipmi::chassis::cmdChassisIdentify,
1843 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301844
Tom05732372016-09-06 17:21:23 +05301845 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301846 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001847 ipmi_chassis_set_sys_boot_options,
1848 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001849 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001850 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1851 ipmi::chassis::cmdGetPohCounter,
1852 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001853
1854 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07001855 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1856 ipmi::chassis::cmdSetPowerRestorePolicy,
1857 ipmi::Privilege::Operator,
1858 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06001859}