blob: 03268067f785d79411114c45c9dc9b82f9ba3586 [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
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500500ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
501 ipmi_request_t request,
502 ipmi_response_t response,
503 ipmi_data_len_t data_len,
504 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500505{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500506 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800507 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500508 *data_len = 0;
509 return rc;
510}
511
anil kumar appana43263c62019-05-27 12:45:04 +0000512/** @brief Implements the get chassis capabilities command
513 *
514 * @returns IPMI completion code plus response data
515 * chassisCapFlags - chassis capability flag
516 * chassisFRUInfoDevAddr - chassis FRU info Device Address
517 * chassisSDRDevAddr - chassis SDR device address
518 * chassisSELDevAddr - chassis SEL device address
519 * chassisSMDevAddr - chassis system management device address
520 * chassisBridgeDevAddr - chassis bridge device address
521 */
522ipmi::RspType<uint8_t, // chassis capabilities flag
523 uint8_t, // chassis FRU info Device Address
524 uint8_t, // chassis SDR device address
525 uint8_t, // chassis SEL device address
526 uint8_t, // chassis system management device address
527 uint8_t // chassis bridge device address
528 >
529 ipmiGetChassisCap()
Nan Li8d15fb42016-08-16 22:29:40 +0800530{
anil kumar appana43263c62019-05-27 12:45:04 +0000531 ipmi::PropertyMap properties;
Yong Liae4b0402018-11-02 11:12:14 +0800532 try
533 {
534 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800535
Yong Liae4b0402018-11-02 11:12:14 +0800536 ipmi::DbusObjectInfo chassisCapObject =
537 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800538
Yong Liae4b0402018-11-02 11:12:14 +0800539 // capabilities flags
540 // [7..4] - reserved
541 // [3] – 1b = provides power interlock (IPM 1.5)
542 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
543 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
544 // has capabilities
545 // to lock out external power control and reset button or
546 // front panel interfaces and/or detect tampering with those
547 // interfaces).
548 // [0] -1b = Chassis provides intrusion (physical security) sensor.
549 // set to default value 0x0.
Nan Li8d15fb42016-08-16 22:29:40 +0800550
anil kumar appana43263c62019-05-27 12:45:04 +0000551 properties =
552 ipmi::getAllDbusProperties(bus, chassisCapObject.second,
553 chassisCapObject.first, chassisCapIntf);
Yong Liae4b0402018-11-02 11:12:14 +0800554 }
555 catch (std::exception& e)
556 {
anil kumar appana43263c62019-05-27 12:45:04 +0000557 log<level::ERR>("Failed to fetch Chassis Capability properties",
558 entry("ERROR=%s", e.what()));
559 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800560 }
561
anil kumar appana43263c62019-05-27 12:45:04 +0000562 uint8_t* chassisCapFlags =
563 std::get_if<uint8_t>(&properties[chassisCapFlagsProp]);
564 if (chassisCapFlags == nullptr)
565 {
566 log<level::ERR>("Error to get chassis capability flags");
567 return ipmi::responseUnspecifiedError();
568 }
569 uint8_t* chassisFRUInfoDevAddr =
570 std::get_if<uint8_t>(&properties[chassisFRUDevAddrProp]);
571 if (chassisFRUInfoDevAddr == nullptr)
572 {
573 log<level::ERR>("Error to get chassis FRU info device address");
574 return ipmi::responseUnspecifiedError();
575 }
576 uint8_t* chassisSDRDevAddr =
577 std::get_if<uint8_t>(&properties[chassisSDRDevAddrProp]);
578 if (chassisSDRDevAddr == nullptr)
579 {
580 log<level::ERR>("Error to get chassis SDR device address");
581 return ipmi::responseUnspecifiedError();
582 }
583 uint8_t* chassisSELDevAddr =
584 std::get_if<uint8_t>(&properties[chassisSELDevAddrProp]);
585 if (chassisSELDevAddr == nullptr)
586 {
587 log<level::ERR>("Error to get chassis SEL device address");
588 return ipmi::responseUnspecifiedError();
589 }
590 uint8_t* chassisSMDevAddr =
591 std::get_if<uint8_t>(&properties[chassisSMDevAddrProp]);
592 if (chassisSMDevAddr == nullptr)
593 {
594 log<level::ERR>("Error to get chassis SM device address");
595 return ipmi::responseUnspecifiedError();
596 }
597 uint8_t* chassisBridgeDevAddr =
598 std::get_if<uint8_t>(&properties[chassisBridgeDevAddrProp]);
599 if (chassisBridgeDevAddr == nullptr)
600 {
601 log<level::ERR>("Error to get chassis bridge device address");
602 return ipmi::responseUnspecifiedError();
603 }
604
605 return ipmi::responseSuccess(*chassisCapFlags, *chassisFRUInfoDevAddr,
606 *chassisSDRDevAddr, *chassisSELDevAddr,
607 *chassisSMDevAddr, *chassisBridgeDevAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800608}
609
anil kumar appana894d0222019-05-27 16:32:14 +0000610/** @brief implements set chassis capalibities command
611 * @param intrusion - chassis intrusion
612 * @param fpLockout - frontpannel lockout
613 * @param reserved1 - skip one bit
614 * @param fruDeviceAddr - chassis FRU info Device Address
615 * @param sdrDeviceAddr - chassis SDR device address
616 * @param selDeviceAddr - chassis SEL device address
617 * @param smDeviceAddr - chassis system management device address
618 * @param bridgeDeviceAddr - chassis bridge device address
619 *
620 * @returns IPMI completion code
621 */
622ipmi::RspType<> ipmiSetChassisCap(bool intrusion, bool fpLockout,
623 uint6_t reserved1,
624
625 uint8_t fruDeviceAddr,
626
627 uint8_t sdrDeviceAddr,
628
629 uint8_t selDeviceAddr,
630
631 uint8_t smDeviceAddr,
632
633 uint8_t bridgeDeviceAddr)
Yong Liae4b0402018-11-02 11:12:14 +0800634{
Yong Liae4b0402018-11-02 11:12:14 +0800635
636 // check input data
anil kumar appana894d0222019-05-27 16:32:14 +0000637 if (reserved1 != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800638 {
anil kumar appana894d0222019-05-27 16:32:14 +0000639 log<level::ERR>("Unsupported request parameter");
640 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800641 }
642
anil kumar appana894d0222019-05-27 16:32:14 +0000643 if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800644 {
645 log<level::ERR>("Unsupported request parameter(FRU Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000646 entry("REQ=0x%x", fruDeviceAddr));
647 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800648 }
anil kumar appana894d0222019-05-27 16:32:14 +0000649 if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800650 {
651 log<level::ERR>("Unsupported request parameter(SDR Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000652 entry("REQ=0x%x", sdrDeviceAddr));
653 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800654 }
655
anil kumar appana894d0222019-05-27 16:32:14 +0000656 if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800657 {
658 log<level::ERR>("Unsupported request parameter(SEL Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000659 entry("REQ=0x%x", selDeviceAddr));
660 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800661 }
662
anil kumar appana894d0222019-05-27 16:32:14 +0000663 if ((smDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800664 {
anil kumar appana894d0222019-05-27 16:32:14 +0000665 log<level::ERR>("Unsupported request parameter(SM Addr)",
666 entry("REQ=0x%x", smDeviceAddr));
667 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800668 }
669
anil kumar appana894d0222019-05-27 16:32:14 +0000670 if ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800671 {
672 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
anil kumar appana894d0222019-05-27 16:32:14 +0000673 entry("REQ=0x%x", bridgeDeviceAddr));
674 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800675 }
676
anil kumar appana894d0222019-05-27 16:32:14 +0000677 uint8_t capFlags = (static_cast<uint8_t>(intrusion)) |
678 ((static_cast<uint8_t>(fpLockout)) << 1);
Yong Liae4b0402018-11-02 11:12:14 +0800679 try
680 {
681 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
682 ipmi::DbusObjectInfo chassisCapObject =
683 ipmi::getDbusObject(bus, chassisCapIntf);
684
685 ipmi::setDbusProperty(bus, chassisCapObject.second,
686 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000687 chassisCapFlagsProp, capFlags);
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 chassisFRUDevAddrProp, fruDeviceAddr);
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 chassisSDRDevAddrProp, sdrDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800696
697 ipmi::setDbusProperty(bus, chassisCapObject.second,
698 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000699 chassisSELDevAddrProp, selDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800700
701 ipmi::setDbusProperty(bus, chassisCapObject.second,
702 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000703 chassisSMDevAddrProp, smDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800704
705 ipmi::setDbusProperty(bus, chassisCapObject.second,
706 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000707 chassisBridgeDevAddrProp, bridgeDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800708 }
709 catch (std::exception& e)
710 {
711 log<level::ERR>(e.what());
anil kumar appana894d0222019-05-27 16:32:14 +0000712 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800713 }
anil kumar appana894d0222019-05-27 16:32:14 +0000714 return ipmi::responseSuccess();
Nan Li8d15fb42016-08-16 22:29:40 +0800715}
716
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530717//------------------------------------------
718// Calls into Host State Manager Dbus object
719//------------------------------------------
720int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600721{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500722 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700723 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500724 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700725 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
726 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530727
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500728 // sd_bus error
729 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700730 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600731
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500732 // SD Bus error report mechanism.
733 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600734
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500735 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700736 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500737 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
738 if (rc < 0)
739 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700740 log<level::ERR>(
741 "Failed to get bus name",
742 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500743 return rc;
744 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530745
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500746 // Convert to string equivalent of the passed in transition enum.
747 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530748
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500749 rc = sd_bus_call_method(bus_type, // On the system bus
750 busname, // Service to contact
751 HOST_STATE_MANAGER_ROOT, // Object path
752 DBUS_PROPERTY_IFACE, // Interface name
753 "Set", // Method to be called
754 &bus_error, // object to return error
755 nullptr, // Response buffer if any
756 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700757 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
758 request.c_str());
759 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500760 {
761 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530762 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500763 }
764 else
765 {
766 log<level::INFO>("Transition request initiated successfully");
767 }
vishwa36993272015-11-20 12:43:49 -0600768
769 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500770 free(busname);
vishwa36993272015-11-20 12:43:49 -0600771
Sergey Solomineb9b8142016-08-23 09:07:28 -0500772 return rc;
vishwa36993272015-11-20 12:43:49 -0600773}
774
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800775//------------------------------------------
776// Set Enabled property to inform NMI source
777// handling to trigger a NMI_OUT BSOD.
778//------------------------------------------
779int setNmiProperty(const bool value)
780{
781 constexpr const char* nmiSourceObjPath =
782 "/xyz/openbmc_project/Chassis/Control/NMISource";
783 constexpr const char* nmiSourceIntf =
784 "xyz.openbmc_project.Chassis.Control.NMISource";
785 std::string bmcSourceSignal = "xyz.openbmc_project.Chassis.Control."
786 "NMISource.BMCSourceSignal.ChassisCmd";
787 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
788
789 try
790 {
791 auto service = ipmi::getService(*busp, nmiSourceIntf, nmiSourceObjPath);
792 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
793 "BMCSource", bmcSourceSignal);
794 ipmi::setDbusProperty(*busp, service, nmiSourceObjPath, nmiSourceIntf,
795 "Enabled", value);
796 }
797 catch (std::exception& e)
798 {
799 log<level::ERR>("Failed to trigger NMI_OUT",
800 entry("EXCEPTION=%s", e.what()));
801 return -1;
802 }
803
804 return 0;
805}
806
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500807namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500808{
Nan Lifdd8ec52016-08-28 03:57:40 +0800809
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500810using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
811using IpmiValue = uint8_t;
812using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800813
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700814const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500815 {RestorePolicy::Policy::AlwaysOff, 0x00},
816 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700817 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800818
Yong Lic6713cf2018-09-12 12:35:13 +0800819static constexpr uint8_t noChange = 0x03;
820static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700821
822/* helper function for Get Chassis Status Command
823 */
824std::optional<uint2_t> getPowerRestorePolicy()
825{
826 uint2_t restorePolicy = 0;
827 using namespace chassis::internal;
828
James Feist225dec82019-11-26 16:25:06 -0800829 settings::Objects& objects = cache::getObjects();
830
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700831 try
832 {
833 const auto& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -0800834 objects.map.at(powerRestoreIntf).front();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700835 ipmi::Value result = ipmi::getDbusProperty(
836 *getSdBus(),
James Feist225dec82019-11-26 16:25:06 -0800837 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700838 powerRestoreSetting.c_str(), powerRestoreIntf,
839 "PowerRestorePolicy");
840 auto powerRestore = RestorePolicy::convertPolicyFromString(
841 std::get<std::string>(result));
842 restorePolicy = dbusToIpmi.at(powerRestore);
843 }
844 catch (const std::exception& e)
845 {
846 log<level::ERR>(
847 "Failed to fetch pgood property", entry("ERROR=%s", e.what()),
James Feist225dec82019-11-26 16:25:06 -0800848 entry("PATH=%s", objects.map.at(powerRestoreIntf).front().c_str()),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700849 entry("INTERFACE=%s", powerRestoreIntf));
James Feist225dec82019-11-26 16:25:06 -0800850 cache::objectsPtr.reset();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700851 return std::nullopt;
852 }
853 return std::make_optional(restorePolicy);
854}
855
856/*
857 * getPowerStatus
858 * helper function for Get Chassis Status Command
859 * return - optional value for pgood (no value on error)
860 */
861std::optional<bool> getPowerStatus()
862{
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700863 bool powerGood = false;
864 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
865 try
866 {
Jason M. Bills3de424c2019-05-21 09:57:16 -0700867 constexpr const char* chassisStatePath =
868 "/xyz/openbmc_project/state/chassis0";
869 constexpr const char* chassisStateIntf =
870 "xyz.openbmc_project.State.Chassis";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700871 auto service =
Jason M. Bills3de424c2019-05-21 09:57:16 -0700872 ipmi::getService(*busp, chassisStateIntf, chassisStatePath);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700873
Jason M. Bills3de424c2019-05-21 09:57:16 -0700874 ipmi::Value powerState =
875 ipmi::getDbusProperty(*busp, service, chassisStatePath,
876 chassisStateIntf, "CurrentPowerState");
877 powerGood = std::get<std::string>(powerState) ==
878 "xyz.openbmc_project.State.Chassis.PowerState.On";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700879 }
880 catch (const std::exception& e)
881 {
882 try
883 {
884 // FIXME: some legacy modules use the older path; try that next
885 constexpr const char* legacyPwrCtrlObj =
886 "/org/openbmc/control/power0";
887 constexpr const char* legacyPwrCtrlIntf =
888 "org.openbmc.control.Power";
889 auto service =
890 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
891
892 ipmi::Value variant = ipmi::getDbusProperty(
893 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
894 powerGood = static_cast<bool>(std::get<int>(variant));
895 }
896 catch (const std::exception& e)
897 {
898 log<level::ERR>("Failed to fetch pgood property",
Jason M. Bills3de424c2019-05-21 09:57:16 -0700899 entry("ERROR=%s", e.what()));
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700900 return std::nullopt;
901 }
902 }
903 return std::make_optional(powerGood);
904}
905
Yong Li70ce7352019-05-16 21:15:27 +0800906/*
907 * getACFailStatus
908 * helper function for Get Chassis Status Command
909 * return - bool value for ACFail (false on error)
910 */
911bool getACFailStatus()
912{
913 constexpr const char* powerControlObj =
914 "/xyz/openbmc_project/Chassis/Control/Power0";
915 constexpr const char* powerControlIntf =
916 "xyz.openbmc_project.Chassis.Control.Power";
917 bool acFail = false;
918 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
919 try
920 {
921 auto service =
922 ipmi::getService(*bus, powerControlIntf, powerControlObj);
923
924 ipmi::Value variant = ipmi::getDbusProperty(
925 *bus, service, powerControlObj, powerControlIntf, "PFail");
926 acFail = std::get<bool>(variant);
927 }
928 catch (const std::exception& e)
929 {
930 log<level::ERR>("Failed to fetch PFail property",
931 entry("ERROR=%s", e.what()),
932 entry("PATH=%s", powerControlObj),
933 entry("INTERFACE=%s", powerControlIntf));
934 }
935 return acFail;
936}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500937} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800938
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700939static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
940 const std::string& buttonIntf)
941{
942 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
943 bool buttonDisabled = false;
944 try
945 {
946 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
947 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
948 buttonIntf, "Enabled");
949 buttonDisabled = !std::get<bool>(enabled);
950 }
951 catch (sdbusplus::exception::SdBusError& e)
952 {
953 log<level::ERR>("Fail to get button Enabled property",
954 entry("PATH=%s", buttonPath.c_str()),
955 entry("ERROR=%s", e.what()));
956 return std::nullopt;
957 }
958 return std::make_optional(buttonDisabled);
959}
960
Nan Lifdd8ec52016-08-28 03:57:40 +0800961//----------------------------------------------------------------------
962// Get Chassis Status commands
963//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700964ipmi::RspType<bool, // Power is on
965 bool, // Power overload
966 bool, // Interlock
967 bool, // power fault
968 bool, // power control fault
969 uint2_t, // power restore policy
970 bool, // reserved
971
972 bool, // AC failed
973 bool, // last power down caused by a Power overload
974 bool, // last power down caused by a power interlock
975 bool, // last power down caused by power fault
976 bool, // last ‘Power is on’ state was entered via IPMI command
977 uint3_t, // reserved
978
979 bool, // Chassis intrusion active
980 bool, // Front Panel Lockout active
981 bool, // Drive Fault
982 bool, // Cooling/fan fault detected
983 uint2_t, // Chassis Identify State
984 bool, // Chassis Identify command and state info supported
985 bool, // reserved
986
987 bool, // Power off button disabled
988 bool, // Reset button disabled
989 bool, // Diagnostic Interrupt button disabled
990 bool, // Standby (sleep) button disabled
991 bool, // Power off button disable allowed
992 bool, // Reset button disable allowed
993 bool, // Diagnostic Interrupt button disable allowed
994 bool // Standby (sleep) button disable allowed
995 >
996 ipmiGetChassisStatus()
Nan Lifdd8ec52016-08-28 03:57:40 +0800997{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500998 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700999 std::optional<uint2_t> restorePolicy =
1000 power_policy::getPowerRestorePolicy();
1001 std::optional<bool> powerGood = power_policy::getPowerStatus();
1002 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001003 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001004 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001005 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001006
1007 // Front Panel Button Capabilities and disable/enable status(Optional)
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001008 std::optional<bool> powerButtonReading =
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001009 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001010 // allow disable if the interface is present
1011 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
1012 // default return the button is enabled (not disabled)
1013 bool powerButtonDisabled = false;
1014 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001015 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001016 // return the real value of the button status, if present
1017 powerButtonDisabled = *powerButtonReading;
1018 }
1019
1020 std::optional<bool> resetButtonReading =
1021 getButtonEnabled(resetButtonPath, resetButtonIntf);
1022 // allow disable if the interface is present
1023 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
1024 // default return the button is enabled (not disabled)
1025 bool resetButtonDisabled = false;
1026 if (resetButtonDisableAllow)
1027 {
1028 // return the real value of the button status, if present
1029 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001030 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001031
Yong Li70ce7352019-05-16 21:15:27 +08001032 bool powerDownAcFailed = power_policy::getACFailStatus();
1033
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001034 // This response has a lot of hard-coded, unsupported fields
1035 // They are set to false or 0
1036 constexpr bool powerOverload = false;
1037 constexpr bool chassisInterlock = false;
1038 constexpr bool powerFault = false;
1039 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001040 constexpr bool powerDownOverload = false;
1041 constexpr bool powerDownInterlock = false;
1042 constexpr bool powerDownPowerFault = false;
1043 constexpr bool powerStatusIPMI = false;
1044 constexpr bool chassisIntrusionActive = false;
1045 constexpr bool frontPanelLockoutActive = false;
1046 constexpr bool driveFault = false;
1047 constexpr bool coolingFanFault = false;
1048 // chassisIdentifySupport set because this command is implemented
1049 constexpr bool chassisIdentifySupport = true;
Yong Lif4e38512019-05-21 14:46:55 +08001050 uint2_t chassisIdentifyState = static_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001051 constexpr bool diagButtonDisabled = false;
1052 constexpr bool sleepButtonDisabled = false;
1053 constexpr bool diagButtonDisableAllow = false;
1054 constexpr bool sleepButtonDisableAllow = false;
1055
1056 return ipmi::responseSuccess(
1057 *powerGood, powerOverload, chassisInterlock, powerFault,
1058 powerControlFault, *restorePolicy,
1059 false, // reserved
1060
1061 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1062 powerDownPowerFault, powerStatusIPMI,
1063 uint3_t(0), // reserved
1064
1065 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1066 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1067 false, // reserved
1068
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001069 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001070 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1071 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001072}
Chris Austen7888c4d2015-12-03 15:26:20 -06001073
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301074//-------------------------------------------------------------
1075// Send a command to SoftPowerOff application to stop any timer
1076//-------------------------------------------------------------
1077int stop_soft_off_timer()
1078{
Patrick Venture0b02be92018-08-31 11:55:55 -07001079 constexpr auto iface = "org.freedesktop.DBus.Properties";
1080 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1081 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301082
Patrick Venture0b02be92018-08-31 11:55:55 -07001083 constexpr auto property = "ResponseReceived";
1084 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1085 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301086
1087 // Get the system bus where most system services are provided.
1088 auto bus = ipmid_get_sd_bus_connection();
1089
1090 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001091 // TODO openbmc/openbmc#1661 - Mapper refactor
1092 //
1093 // See openbmc/openbmc#1743 for some details but high level summary is that
1094 // for now the code will directly call the soft off interface due to a
1095 // race condition with mapper usage
1096 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001097 // char *busname = nullptr;
1098 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1099 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001100 //{
1101 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301102 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001103 // return r;
1104 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301105
1106 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001107 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001108 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1109 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301110 if (rc < 0)
1111 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301112 log<level::ERR>("Failed to set property in SoftPowerOff object",
1113 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301114 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001115
Patrick Venture0b02be92018-08-31 11:55:55 -07001116 // TODO openbmc/openbmc#1661 - Mapper refactor
1117 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301118 return rc;
1119}
1120
vishwa36993272015-11-20 12:43:49 -06001121//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001122// Create file to indicate there is no need for softoff notification to host
1123//----------------------------------------------------------------------
1124void indicate_no_softoff_needed()
1125{
1126 fs::path path{HOST_INBAND_REQUEST_DIR};
1127 if (!fs::is_directory(path))
1128 {
1129 fs::create_directory(path);
1130 }
1131
1132 // Add the host instance (default 0 for now) to the file name
1133 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001134 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001135 size++; // null
1136 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001137 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001138
1139 // Append file name to directory and create it
1140 path /= buf.get();
1141 std::ofstream(path.c_str());
1142}
1143
anil kumar appanadafff5f2019-04-27 18:06:00 +00001144/** @brief Implementation of chassis control command
1145 *
1146 * @param - chassisControl command byte
1147 *
1148 * @return Success or InvalidFieldRequest.
1149 */
1150ipmi::RspType<> ipmiChassisControl(uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001151{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001152 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001153 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001154 {
1155 case CMD_POWER_ON:
1156 rc = initiate_state_transition(State::Host::Transition::On);
1157 break;
1158 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301159 // This path would be hit in 2 conditions.
1160 // 1: When user asks for power off using ipmi chassis command 0x04
1161 // 2: Host asking for power off post shutting down.
1162
1163 // If it's a host requested power off, then need to nudge Softoff
1164 // application that it needs to stop the watchdog timer if running.
1165 // If it is a user requested power off, then this is not really
1166 // needed. But then we need to differentiate between user and host
1167 // calling this same command
1168
1169 // For now, we are going ahead with trying to nudge the soft off and
1170 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001171 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301172
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001173 // Only request the Off transition if the soft power off
1174 // application is not running
1175 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001176 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001177 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301178 // that it should not run. Not doing this will result in State
1179 // manager doing a default soft power off when asked for power
1180 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001181 indicate_no_softoff_needed();
1182
1183 // Now request the shutdown
1184 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001185 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001186 else
1187 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301188 log<level::INFO>("Soft off is running, so let shutdown target "
1189 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001190 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001191 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301192
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001193 case CMD_HARD_RESET:
1194 case CMD_POWER_CYCLE:
1195 // SPEC has a section that says certain implementations can trigger
1196 // PowerOn if power is Off when a command to power cycle is
1197 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001198
1199 // First create a file to indicate to the soft off application
1200 // that it should not run since this is a direct user initiated
1201 // power reboot request (i.e. a reboot request that is not
1202 // originating via a soft power off SMS request)
1203 indicate_no_softoff_needed();
1204
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001205 rc = initiate_state_transition(State::Host::Transition::Reboot);
1206 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301207
1208 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1209 // Request Host State Manager to do a soft power off
1210 rc = initiate_state_transition(State::Host::Transition::Off);
1211 break;
1212
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001213 case CMD_PULSE_DIAGNOSTIC_INTR:
1214 rc = setNmiProperty(true);
1215 break;
1216
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001217 default:
1218 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301219 log<level::ERR>("Invalid Chassis Control command",
anil kumar appanadafff5f2019-04-27 18:06:00 +00001220 entry("CMD=0x%X", chassisControl));
1221 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001222 }
1223 }
vishwa36993272015-11-20 12:43:49 -06001224
anil kumar appanadafff5f2019-04-27 18:06:00 +00001225 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1226 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001227}
1228
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001229/** @brief Return D-Bus connection string to enclosure identify LED object
1230 *
1231 * @param[in, out] connection - connection to D-Bus object
1232 * @return a IPMI return code
1233 */
1234std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301235{
Tom Joseph5110c122018-03-23 17:55:40 +05301236 // lookup enclosure_identify group owner(s) in mapper
1237 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001238 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1239 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301240
1241 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001242 static const std::vector<std::string> interfaces = {
1243 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301244 mapperCall.append(interfaces);
1245 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1246 if (mapperReply.is_method_error())
1247 {
1248 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001249 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301250 }
1251 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1252 mapperReply.read(mapperResp);
1253
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001254 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301255 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001256 log<level::ERR>(
1257 "Invalid number of enclosure identify objects.",
1258 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001259 elog<InternalFailure>();
1260 }
1261 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1262 return pair.first;
1263}
Tom Joseph5110c122018-03-23 17:55:40 +05301264
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001265/** @brief Turn On/Off enclosure identify LED
1266 *
1267 * @param[in] flag - true to turn on LED, false to turn off
1268 * @return a IPMI return code
1269 */
1270void enclosureIdentifyLed(bool flag)
1271{
1272 using namespace chassis::internal;
1273 std::string connection = std::move(getEnclosureIdentifyConnection());
Vernon Mauery400cc782018-10-09 13:49:53 -07001274 auto msg = std::string("enclosureIdentifyLed(") +
1275 boost::lexical_cast<std::string>(flag) + ")";
1276 log<level::DEBUG>(msg.c_str());
Patrick Venture0b02be92018-08-31 11:55:55 -07001277 auto led =
1278 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1279 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001280 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Vernon Mauery16b86932019-05-01 08:36:11 -07001281 std::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001282 auto ledReply = dbus.call(led);
1283 if (ledReply.is_method_error())
1284 {
1285 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001286 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001287 elog<InternalFailure>();
1288 }
1289}
1290
1291/** @brief Callback method to turn off LED
1292 */
1293void enclosureIdentifyLedOff()
1294{
1295 try
1296 {
Yong Lif4e38512019-05-21 14:46:55 +08001297 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001298 enclosureIdentifyLed(false);
1299 }
1300 catch (const InternalFailure& e)
1301 {
1302 report<InternalFailure>();
1303 }
1304}
1305
1306/** @brief Create timer to turn on and off the enclosure LED
1307 */
1308void createIdentifyTimer()
1309{
1310 if (!identifyTimer)
1311 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001312 identifyTimer =
1313 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001314 }
1315}
1316
Vernon Mauery400cc782018-10-09 13:49:53 -07001317ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1318 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001319{
Vernon Mauery400cc782018-10-09 13:49:53 -07001320 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1321 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301322
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001323 if (identifyInterval || forceIdentify)
1324 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001325 // stop the timer if already started;
1326 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001327 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001328 try
Tom Joseph5110c122018-03-23 17:55:40 +05301329 {
Yong Lif4e38512019-05-21 14:46:55 +08001330 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001331 enclosureIdentifyLed(true);
1332 }
1333 catch (const InternalFailure& e)
1334 {
1335 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001336 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301337 }
1338
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001339 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301340 {
Yong Lif4e38512019-05-21 14:46:55 +08001341 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001342 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001343 }
1344 // start the timer
1345 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001346 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001347 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301348 }
Tom Josephbed26992018-07-31 23:00:24 +05301349 else if (!identifyInterval)
1350 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001351 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301352 enclosureIdentifyLedOff();
1353 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001354 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301355}
1356
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001357namespace boot_options
1358{
1359
1360using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1361using IpmiValue = uint8_t;
1362constexpr auto ipmiDefault = 0;
1363
Patrick Venture0b02be92018-08-31 11:55:55 -07001364std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001365 {0x01, Source::Sources::Network},
1366 {0x02, Source::Sources::Disk},
1367 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001368 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001369 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001370
Patrick Venture0b02be92018-08-31 11:55:55 -07001371std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Yong Li5833cb62019-10-30 13:27:12 +08001372#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001373 {0x03, Mode::Modes::Safe},
Yong Li5833cb62019-10-30 13:27:12 +08001374#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001375 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001376 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001377
Patrick Venture0b02be92018-08-31 11:55:55 -07001378std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001379 {Source::Sources::Network, 0x01},
1380 {Source::Sources::Disk, 0x02},
1381 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001382 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001383 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001384
Patrick Venture0b02be92018-08-31 11:55:55 -07001385std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Yong Li5833cb62019-10-30 13:27:12 +08001386#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001387 {Mode::Modes::Safe, 0x03},
Yong Li5833cb62019-10-30 13:27:12 +08001388#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001389 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001390 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001391
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001392} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001393
Marri Devender Rao81719702018-05-07 00:53:48 -05001394/** @brief Set the property value for boot source
1395 * @param[in] source - boot source value
1396 * @return On failure return IPMI error.
1397 */
1398static ipmi_ret_t setBootSource(const Source::Sources& source)
1399{
1400 using namespace chassis::internal;
1401 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001402 std::variant<std::string> property = convertForMessage(source);
James Feist225dec82019-11-26 16:25:06 -08001403 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001404 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1405 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001406 auto method = dbus.new_method_call(
1407 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1408 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001409 method.append(bootSourceIntf, "BootSource", property);
1410 auto reply = dbus.call(method);
1411 if (reply.is_method_error())
1412 {
1413 log<level::ERR>("Error in BootSource Set");
1414 report<InternalFailure>();
1415 return IPMI_CC_UNSPECIFIED_ERROR;
1416 }
1417 return IPMI_CC_OK;
1418}
1419
Patrick Venture0b02be92018-08-31 11:55:55 -07001420/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001421 * @param[in] mode - boot mode value
1422 * @return On failure return IPMI error.
1423 */
1424static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1425{
1426 using namespace chassis::internal;
1427 using namespace chassis::internal::cache;
Vernon Mauery16b86932019-05-01 08:36:11 -07001428 std::variant<std::string> property = convertForMessage(mode);
James Feist225dec82019-11-26 16:25:06 -08001429 settings::Objects& objects = getObjects();
Marri Devender Rao81719702018-05-07 00:53:48 -05001430 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1431 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001432 auto method = dbus.new_method_call(
1433 objects.service(bootModeSetting, bootModeIntf).c_str(),
1434 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001435 method.append(bootModeIntf, "BootMode", property);
1436 auto reply = dbus.call(method);
1437 if (reply.is_method_error())
1438 {
1439 log<level::ERR>("Error in BootMode Set");
1440 report<InternalFailure>();
1441 return IPMI_CC_UNSPECIFIED_ERROR;
1442 }
1443 return IPMI_CC_OK;
1444}
1445
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001446ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1447 ipmi_request_t request,
1448 ipmi_response_t response,
1449 ipmi_data_len_t data_len,
1450 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001451{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001452 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001453 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001454 char* p = NULL;
1455 get_sys_boot_options_response_t* resp =
1456 (get_sys_boot_options_response_t*)response;
1457 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001458 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001459
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001460 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001461 resp->version = SET_PARM_VERSION;
1462 resp->parm = 5;
1463 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001464
shgoupfd84fbbf2015-12-17 10:05:51 +08001465 /*
1466 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1467 * This is the only parameter used by petitboot.
1468 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001469 if (reqptr->parameter ==
1470 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1471 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001472
Ratan Guptafd28dd72016-08-01 04:58:01 -05001473 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001474 using namespace chassis::internal;
1475 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001476
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001477 try
ratagupta6f6bff2016-04-04 06:20:11 -05001478 {
James Feist225dec82019-11-26 16:25:06 -08001479 settings::Objects& objects = getObjects();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001480 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1481 const auto& bootSourceSetting =
1482 std::get<settings::Path>(bootSetting);
1483 auto oneTimeEnabled =
1484 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001485 auto method = dbus.new_method_call(
1486 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1487 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001488 method.append(bootSourceIntf, "BootSource");
1489 auto reply = dbus.call(method);
1490 if (reply.is_method_error())
1491 {
1492 log<level::ERR>("Error in BootSource Get");
1493 report<InternalFailure>();
1494 *data_len = 0;
1495 return IPMI_CC_UNSPECIFIED_ERROR;
1496 }
Vernon Mauery16b86932019-05-01 08:36:11 -07001497 std::variant<std::string> result;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001498 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001499 auto bootSource =
1500 Source::convertSourcesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001501
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001502 bootSetting = settings::boot::setting(objects, bootModeIntf);
1503 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1504 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001505 objects.service(bootModeSetting, bootModeIntf).c_str(),
1506 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001507 method.append(bootModeIntf, "BootMode");
1508 reply = dbus.call(method);
1509 if (reply.is_method_error())
1510 {
1511 log<level::ERR>("Error in BootMode Get");
1512 report<InternalFailure>();
1513 *data_len = 0;
1514 return IPMI_CC_UNSPECIFIED_ERROR;
1515 }
1516 reply.read(result);
Vernon Maueryf442e112019-04-09 11:44:36 -07001517 auto bootMode =
1518 Mode::convertModesFromString(std::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001519
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001520 bootOption = sourceDbusToIpmi.at(bootSource);
1521 if ((Mode::Modes::Regular == bootMode) &&
1522 (Source::Sources::Default == bootSource))
1523 {
1524 bootOption = ipmiDefault;
1525 }
1526 else if (Source::Sources::Default == bootSource)
1527 {
1528 bootOption = modeDbusToIpmi.at(bootMode);
1529 }
1530 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001531
Patrick Venture0b02be92018-08-31 11:55:55 -07001532 resp->data[0] = oneTimeEnabled
1533 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1534 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001535
ratagupta6f6bff2016-04-04 06:20:11 -05001536 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001537 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001538 catch (InternalFailure& e)
1539 {
James Feist225dec82019-11-26 16:25:06 -08001540 cache::objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001541 report<InternalFailure>();
1542 *data_len = 0;
1543 return IPMI_CC_UNSPECIFIED_ERROR;
1544 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001545 }
1546 else if (reqptr->parameter ==
1547 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1548 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001549
Patrick Venture0b02be92018-08-31 11:55:55 -07001550 *data_len =
1551 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001552
Patrick Venture0b02be92018-08-31 11:55:55 -07001553 resp->parm =
1554 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001555
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001556 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001557
Patrick Venture0b02be92018-08-31 11:55:55 -07001558 if (ret < 0)
1559 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001560
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301561 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001562 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001563 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001564 }
1565 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001566 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001567 }
1568
Patrick Venture0b02be92018-08-31 11:55:55 -07001569 else
1570 {
1571 log<level::ERR>("Unsupported parameter",
1572 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001573 }
1574
1575 if (p)
1576 free(p);
1577
Ratan Guptafd28dd72016-08-01 04:58:01 -05001578 if (rc == IPMI_CC_OK)
1579 {
1580 *data_len += 2;
1581 }
1582
shgoupfd84fbbf2015-12-17 10:05:51 +08001583 return rc;
1584}
1585
shgoupfd84fbbf2015-12-17 10:05:51 +08001586ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001587 ipmi_request_t request,
1588 ipmi_response_t response,
1589 ipmi_data_len_t data_len,
1590 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001591{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001592 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001593 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001594 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001595
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001596 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1597 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001598
shgoupfd84fbbf2015-12-17 10:05:51 +08001599 // This IPMI command does not have any resposne data
1600 *data_len = 0;
1601
1602 /* 000101
1603 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1604 * This is the only parameter used by petitboot.
1605 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001606
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001607 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1608 {
1609 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1610 using namespace chassis::internal;
1611 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001612 auto oneTimeEnabled = false;
1613 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301614 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001615 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001616
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001617 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001618 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001619 bool permanent =
1620 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1621 SET_PARM_BOOT_FLAGS_PERMANENT;
1622
James Feist225dec82019-11-26 16:25:06 -08001623 settings::Objects& objects = getObjects();
1624
Patrick Venture0b02be92018-08-31 11:55:55 -07001625 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301626
1627 oneTimeEnabled =
1628 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1629
1630 /*
1631 * Check if the current boot setting is onetime or permanent, if the
1632 * request in the command is otherwise, then set the "Enabled"
1633 * property in one_time object path to 'True' to indicate onetime
1634 * and 'False' to indicate permanent.
1635 *
1636 * Once the onetime/permanent setting is applied, then the bootMode
1637 * and bootSource is updated for the corresponding object.
1638 */
1639 if ((permanent && oneTimeEnabled) ||
1640 (!permanent && !oneTimeEnabled))
1641 {
1642 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1643
Patrick Venture0b02be92018-08-31 11:55:55 -07001644 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1645 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301646 }
1647
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001648 auto modeItr = modeIpmiToDbus.find(bootOption);
1649 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1650 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001651 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001652 rc = setBootSource(sourceItr->second);
1653 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001654 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001655 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001656 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001657 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001658 // If a set boot device is mapping to a boot source, then reset
1659 // the boot mode D-Bus property to default.
1660 // This way the ipmid code can determine which property is not
1661 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001662 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001663 {
1664 setBootMode(Mode::Modes::Regular);
1665 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001666 }
1667 if (modeIpmiToDbus.end() != modeItr)
1668 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001669 rc = setBootMode(modeItr->second);
1670 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001671 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001672 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001673 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001674 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001675 // If a set boot device is mapping to a boot mode, then reset
1676 // the boot source D-Bus property to default.
1677 // This way the ipmid code can determine which property is not
1678 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001679 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001680 {
1681 setBootSource(Source::Sources::Default);
1682 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001683 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001684 if ((modeIpmiToDbus.end() == modeItr) &&
1685 (sourceIpmiToDbus.end() == sourceItr))
1686 {
1687 // return error if boot option is not supported
1688 *data_len = 0;
1689 return IPMI_CC_INVALID_FIELD_REQUEST;
1690 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001691 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001692 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001693 {
James Feist225dec82019-11-26 16:25:06 -08001694 objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001695 report<InternalFailure>();
1696 *data_len = 0;
1697 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001698 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001699 }
1700 else if (reqptr->parameter ==
1701 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1702 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001703
1704 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001705 if (ret < 0)
1706 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301707 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001708 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001709 rc = IPMI_CC_UNSPECIFIED_ERROR;
1710 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001711 }
1712 else if (reqptr->parameter ==
1713 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1714 {
Tom Josephf536c902017-09-25 18:08:15 +05301715 // Handle parameter #4 and return command completed normally
1716 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1717 // parameter. This is added to support the ipmitool command `chassis
1718 // bootdev` which sends set on parameter #4, before setting the boot
1719 // flags.
1720 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001721 }
1722 else
1723 {
1724 log<level::ERR>("Unsupported parameter",
1725 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001726 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001727 }
1728
1729 return rc;
1730}
1731
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001732/** @brief implements Get POH counter command
1733 * @parameter
1734 * - none
1735 * @returns IPMI completion code plus response data
1736 * - minPerCount - Minutes per count
1737 * - counterReading - counter reading
1738 */
1739ipmi::RspType<uint8_t, // Minutes per count
1740 uint32_t // Counter reading
1741 >
1742 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001743{
1744 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001745 try
1746 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001747 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
1748 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001749 }
1750 catch (std::exception& e)
1751 {
1752 log<level::ERR>(e.what());
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001753 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001754 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001755}
1756
Jason M. Billsbc996a32019-06-17 15:46:37 -07001757ipmi::RspType<uint3_t, // policy support
1758 uint5_t // reserved
1759 >
Vernon Mauerye278ead2018-10-09 09:23:43 -07001760 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
Jason M. Billsbc996a32019-06-17 15:46:37 -07001761 uint3_t policy, uint5_t reserved)
Yong Lic6713cf2018-09-12 12:35:13 +08001762{
Yong Lic6713cf2018-09-12 12:35:13 +08001763 power_policy::DbusValue value =
1764 power_policy::RestorePolicy::Policy::AlwaysOff;
1765
Jason M. Billsbc996a32019-06-17 15:46:37 -07001766 if (reserved || (policy > power_policy::noChange))
Yong Lic6713cf2018-09-12 12:35:13 +08001767 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001768 phosphor::logging::log<level::ERR>(
1769 "Reserved request parameter",
1770 entry("REQ=0x%x", static_cast<int>(policy)));
Jason M. Billsbc996a32019-06-17 15:46:37 -07001771 return ipmi::responseInvalidFieldRequest();
Yong Lic6713cf2018-09-12 12:35:13 +08001772 }
1773
Vernon Mauerye278ead2018-10-09 09:23:43 -07001774 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08001775 {
1776 // just return the supported policy
Jason M. Billsbc996a32019-06-17 15:46:37 -07001777 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08001778 }
1779
1780 for (auto const& it : power_policy::dbusToIpmi)
1781 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07001782 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08001783 {
1784 value = it.first;
1785 break;
1786 }
1787 }
1788
1789 try
1790 {
James Feist225dec82019-11-26 16:25:06 -08001791 settings::Objects& objects = chassis::internal::cache::getObjects();
Yong Lic6713cf2018-09-12 12:35:13 +08001792 const settings::Path& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -08001793 objects.map.at(chassis::internal::powerRestoreIntf).front();
Vernon Mauery16b86932019-05-01 08:36:11 -07001794 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08001795
Vernon Mauerye278ead2018-10-09 09:23:43 -07001796 auto sdbusp = getSdBus();
1797 boost::system::error_code ec;
1798 sdbusp->yield_method_call<void>(
1799 yield, ec,
James Feist225dec82019-11-26 16:25:06 -08001800 objects
Yong Lic6713cf2018-09-12 12:35:13 +08001801 .service(powerRestoreSetting,
1802 chassis::internal::powerRestoreIntf)
1803 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07001804 powerRestoreSetting, ipmi::PROP_INTF, "Set",
1805 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1806 property);
1807 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08001808 {
1809 phosphor::logging::log<level::ERR>("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07001810 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001811 }
1812 }
1813 catch (InternalFailure& e)
1814 {
James Feist225dec82019-11-26 16:25:06 -08001815 chassis::internal::cache::objectsPtr.reset();
Yong Lic6713cf2018-09-12 12:35:13 +08001816 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07001817 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08001818 }
1819
Jason M. Billsbc996a32019-06-17 15:46:37 -07001820 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08001821}
1822
Adriana Kobylak40814c62015-10-27 15:58:44 -05001823void register_netfn_chassis_functions()
1824{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001825 createIdentifyTimer();
1826
Tom05732372016-09-06 17:21:23 +05301827 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001828 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1829 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001830
Tom05732372016-09-06 17:21:23 +05301831 // Get Chassis Capabilities
anil kumar appana43263c62019-05-27 12:45:04 +00001832 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1833 ipmi::chassis::cmdGetChassisCapabilities,
1834 ipmi::Privilege::User, ipmiGetChassisCap);
Nan Li8d15fb42016-08-16 22:29:40 +08001835
Yong Liae4b0402018-11-02 11:12:14 +08001836 // Set Chassis Capabilities
anil kumar appana894d0222019-05-27 16:32:14 +00001837 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1838 ipmi::chassis::cmdSetChassisCapabilities,
1839 ipmi::Privilege::User, ipmiSetChassisCap);
Yong Liae4b0402018-11-02 11:12:14 +08001840
Tom05732372016-09-06 17:21:23 +05301841 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301842 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001843 ipmi_chassis_get_sys_boot_options,
1844 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001845
Tom05732372016-09-06 17:21:23 +05301846 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001847 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1848 ipmi::chassis::cmdGetChassisStatus,
1849 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08001850
Tom05732372016-09-06 17:21:23 +05301851 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00001852 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1853 ipmi::chassis::cmdChassisControl,
1854 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08001855
Tom Joseph5110c122018-03-23 17:55:40 +05301856 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07001857 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1858 ipmi::chassis::cmdChassisIdentify,
1859 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05301860
Tom05732372016-09-06 17:21:23 +05301861 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301862 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001863 ipmi_chassis_set_sys_boot_options,
1864 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001865 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00001866 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1867 ipmi::chassis::cmdGetPohCounter,
1868 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08001869
1870 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07001871 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
1872 ipmi::chassis::cmdSetPowerRestorePolicy,
1873 ipmi::Privilege::Operator,
1874 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06001875}