blob: dab62c495f5e142c7f10a2c1e581e11a8b8bebdf [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "chassishandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05005#include "ipmid.hpp"
6#include "settings.hpp"
Patrick Venture3a5071a2018-09-12 13:27:42 -07007#include "timer.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include "types.hpp"
Ratan Guptacc8feb42017-07-25 21:52:10 +05309#include "utils.hpp"
Ratan Guptadcb10672017-07-10 10:33:50 +053010
Patrick Venture0b02be92018-08-31 11:55:55 -070011#include <arpa/inet.h>
12#include <endian.h>
Patrick Venture46470a32018-09-07 19:26:25 -070013#include <host-ipmid/ipmid-api.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070014#include <limits.h>
15#include <mapper.h>
16#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070017
Ratan Guptafd28dd72016-08-01 04:58:01 -050018#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070019#include <chrono>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070020#include <cstring>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050021#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053022#include <future>
Patrick Venture3a5071a2018-09-12 13:27:42 -070023#include <map>
24#include <phosphor-logging/elog-errors.hpp>
25#include <phosphor-logging/log.hpp>
26#include <sdbusplus/bus.hpp>
27#include <sdbusplus/server/object.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
Vernon Mauery185b9f82018-07-20 10:52:36 -070037#if __has_include(<filesystem>)
38#include <filesystem>
39#elif __has_include(<experimental/filesystem>)
Andrew Geisslera6e3a302017-05-31 19:34:00 -050040#include <experimental/filesystem>
Patrick Venture0b02be92018-08-31 11:55:55 -070041namespace std
42{
43// splice experimental::filesystem into std
44namespace filesystem = std::experimental::filesystem;
45} // namespace std
Vernon Mauery185b9f82018-07-20 10:52:36 -070046#else
Patrick Venture0b02be92018-08-31 11:55:55 -070047#error filesystem not available
Vernon Mauery185b9f82018-07-20 10:52:36 -070048#endif
Patrick Venture0b02be92018-08-31 11:55:55 -070049
Patrick Venture0b02be92018-08-31 11:55:55 -070050// Defines
51#define SET_PARM_VERSION 0x01
52#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40 // boot flags data1 7th bit on
53#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80 // boot flags data1 8th bit on
54#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT \
55 0xC0 // boot flags data1 7 & 8 bit
56 // on
ratagupta6f6bff2016-04-04 06:20:11 -050057
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050058std::unique_ptr<phosphor::ipmi::Timer> identifyTimer = nullptr;
59
Patrick Venture0b02be92018-08-31 11:55:55 -070060constexpr size_t SIZE_MAC = 18;
61constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
62 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
63 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050064constexpr size_t SIZE_PREFIX = 7;
65constexpr size_t MAX_PREFIX_VALUE = 32;
66constexpr size_t SIZE_COOKIE = 4;
67constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053068constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053069
Patrick Venture0b02be92018-08-31 11:55:55 -070070// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053071static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070072 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050073
74static constexpr size_t COOKIE_OFFSET = 1;
75static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053076static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050077static constexpr size_t MAC_OFFSET = 9;
78static constexpr size_t ADDRTYPE_OFFSET = 16;
79static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050080
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050081static constexpr size_t encIdentifyObjectsSize = 1;
82static constexpr size_t chassisIdentifyReqLength = 2;
83static constexpr size_t identifyIntervalPos = 0;
84static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080085
Adriana Kobylak40814c62015-10-27 15:58:44 -050086void register_netfn_chassis_functions() __attribute__((constructor));
87
shgoupfd84fbbf2015-12-17 10:05:51 +080088// Host settings in dbus
89// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070090const char* settings_object_name = "/org/openbmc/settings/host0";
91const char* settings_intf_name = "org.freedesktop.DBus.Properties";
92const char* host_intf_name = "org.openbmc.settings.Host";
93const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053094 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080095
Ratan Guptadcb10672017-07-10 10:33:50 +053096constexpr auto SETTINGS_ROOT = "/";
97constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053098
99constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
100constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
101
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500102static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
103static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -0700104 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500105static constexpr auto pOHCounterProperty = "POHCounter";
106static constexpr auto match = "chassis0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530107
Nan Li8d15fb42016-08-16 22:29:40 +0800108typedef struct
109{
110 uint8_t cap_flags;
111 uint8_t fru_info_dev_addr;
112 uint8_t sdr_dev_addr;
113 uint8_t sel_dev_addr;
114 uint8_t system_management_dev_addr;
115 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700116} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800117
Nan Lifdd8ec52016-08-28 03:57:40 +0800118typedef struct
119{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500120 uint8_t cur_power_state;
121 uint8_t last_power_event;
122 uint8_t misc_power_state;
123 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700124} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800125
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500126/**
127 * @struct Get POH counter command response data
128 */
129struct GetPOHCountResponse
130{
Patrick Venture0b02be92018-08-31 11:55:55 -0700131 uint8_t minPerCount; ///< Minutes per count
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500132 uint8_t counterReading[4]; ///< Counter reading
Patrick Venture0b02be92018-08-31 11:55:55 -0700133} __attribute__((packed));
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500134
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530135// Phosphor Host State manager
136namespace State = sdbusplus::xyz::openbmc_project::State::server;
137
Vernon Mauery185b9f82018-07-20 10:52:36 -0700138namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500139
Ratan Guptadcb10672017-07-10 10:33:50 +0530140using namespace phosphor::logging;
141using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500142using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500143namespace chassis
144{
145namespace internal
146{
147
148constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
149constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500150constexpr auto powerRestoreIntf =
151 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500152sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
153
154namespace cache
155{
156
157settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500158 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500159
160} // namespace cache
161} // namespace internal
162} // namespace chassis
163
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500164namespace poh
165{
166
167constexpr auto minutesPerCount = 60;
168
169} // namespace poh
170
Patrick Venture0b02be92018-08-31 11:55:55 -0700171// TODO : Can remove the below function as we have
Ratan Guptadcb10672017-07-10 10:33:50 +0530172// new functions which uses sdbusplus.
173//
174// openbmc/openbmc#1489
Patrick Venture0b02be92018-08-31 11:55:55 -0700175int dbus_get_property(const char* name, char** buf)
shgoupfd84fbbf2015-12-17 10:05:51 +0800176{
177 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700178 sd_bus_message* m = NULL;
179 sd_bus* bus = NULL;
180 char* temp_buf = NULL;
181 char* connection = NULL;
shgoupfd84fbbf2015-12-17 10:05:51 +0800182 int r;
183
Brad Bishop35518682016-07-22 08:35:41 -0400184 // Get the system bus where most system services are provided.
185 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800186
Brad Bishop35518682016-07-22 08:35:41 -0400187 r = mapper_get_service(bus, settings_object_name, &connection);
Patrick Venture0b02be92018-08-31 11:55:55 -0700188 if (r < 0)
189 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530190 log<level::ERR>("Failed to get connection",
191 entry("OBJ_NAME=%s", settings_object_name),
192 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800193 goto finish;
194 }
195
shgoupfd84fbbf2015-12-17 10:05:51 +0800196 /*
197 * Bus, service, object path, interface and method are provided to call
198 * the method.
199 * Signatures and input arguments are provided by the arguments at the
200 * end.
201 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700202 r = sd_bus_call_method(bus, connection, /* service to contact */
203 settings_object_name, /* object path */
204 settings_intf_name, /* interface name */
205 "Get", /* method name */
206 &error, /* object to return error in */
207 &m, /* return message on success */
208 "ss", /* input signature */
209 host_intf_name, /* first argument */
210 name); /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800211
Patrick Venture0b02be92018-08-31 11:55:55 -0700212 if (r < 0)
213 {
214 log<level::ERR>("Failed to issue Get method call",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530215 entry("ERRNO=0x%X", r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800216 goto finish;
217 }
218
Patrick Venture0b02be92018-08-31 11:55:55 -0700219 /*
220 * The output should be parsed exactly the same as the output formatting
221 * specified.
222 */
223 r = sd_bus_message_read(m, "v", "s", &temp_buf);
224 if (r < 0)
225 {
226 log<level::ERR>("Failed to parse response message",
227 entry("ERRNO=0x%X", -r));
228 goto finish;
229 }
230
231 *buf = strdup(temp_buf);
232 /* *buf = (char*) malloc(strlen(temp_buf));
233 if (*buf) {
234 strcpy(*buf, temp_buf);
235 }
236 */
237
238finish:
shgoupfd84fbbf2015-12-17 10:05:51 +0800239 sd_bus_error_free(&error);
240 sd_bus_message_unref(m);
241 free(connection);
242
243 return r;
244}
245
Patrick Venture0b02be92018-08-31 11:55:55 -0700246// TODO : Can remove the below function as we have
247// new functions which uses sdbusplus.
248//
249// openbmc/openbmc#1489
250
251int dbus_set_property(const char* name, const char* value)
252{
253 sd_bus_error error = SD_BUS_ERROR_NULL;
254 sd_bus_message* m = NULL;
255 sd_bus* bus = NULL;
256 char* connection = NULL;
257 int r;
258
259 // Get the system bus where most system services are provided.
260 bus = ipmid_get_sd_bus_connection();
261
262 r = mapper_get_service(bus, settings_object_name, &connection);
263 if (r < 0)
264 {
265 log<level::ERR>("Failed to get connection",
266 entry("OBJ_NAME=%s", settings_object_name),
267 entry("ERRNO=0x%X", -r));
268 goto finish;
269 }
270
271 /*
272 * Bus, service, object path, interface and method are provided to call
273 * the method.
274 * Signatures and input arguments are provided by the arguments at the
275 * end.
276 */
277 r = sd_bus_call_method(bus, connection, /* service to contact */
278 settings_object_name, /* object path */
279 settings_intf_name, /* interface name */
280 "Set", /* method name */
281 &error, /* object to return error in */
282 &m, /* return message on success */
283 "ssv", /* input signature */
284 host_intf_name, /* first argument */
285 name, /* second argument */
286 "s", /* third argument */
287 value); /* fourth argument */
288
289 if (r < 0)
290 {
291 log<level::ERR>("Failed to issue Set method call",
292 entry("ERRNO=0x%X", r));
293 goto finish;
294 }
295
296finish:
297 sd_bus_error_free(&error);
298 sd_bus_message_unref(m);
299 free(connection);
300
301 return r;
302}
303
304struct get_sys_boot_options_t
305{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500306 uint8_t parameter;
307 uint8_t set;
308 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700309} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500310
Patrick Venture0b02be92018-08-31 11:55:55 -0700311struct get_sys_boot_options_response_t
312{
shgoupfd84fbbf2015-12-17 10:05:51 +0800313 uint8_t version;
314 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500315 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700316} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800317
Patrick Venture0b02be92018-08-31 11:55:55 -0700318struct set_sys_boot_options_t
319{
shgoupfd84fbbf2015-12-17 10:05:51 +0800320 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500321 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700322} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500323
Ratan Guptadcb10672017-07-10 10:33:50 +0530324int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500325{
Ratan Guptadcb10672017-07-10 10:33:50 +0530326 ipmi::PropertyMap properties;
327 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530328 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500329
Ratan Guptadcb10672017-07-10 10:33:50 +0530330 try
331 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700332 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530333 // objects,to handle such cases we are interested on that object
334 // which are on interested busname.
335 // Currenlty mapper doesn't give the readable busname(gives busid)
336 // so we can't match with bus name so giving some object specific info
337 // as SETTINGS_MATCH.
338 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500339
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530340 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530341
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530342 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
343 SETTINGS_ROOT, SETTINGS_MATCH);
344
345 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
346 SETTINGS_ROOT, SETTINGS_MATCH);
347
Patrick Venture0b02be92018-08-31 11:55:55 -0700348 properties = ipmi::getAllDbusProperties(
349 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
350 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
351 macObjectInfo.first, MAC_INTERFACE,
352 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530353
Patrick Venture0b02be92018-08-31 11:55:55 -0700354 auto ipAddress = properties["Address"].get<std::string>();
Ratan Guptad70f4532017-08-04 02:07:31 +0530355
356 auto gateway = properties["Gateway"].get<std::string>();
357
358 auto prefix = properties["PrefixLength"].get<uint8_t>();
359
Patrick Venture0b02be92018-08-31 11:55:55 -0700360 uint8_t isStatic =
361 (properties["Origin"].get<std::string>() ==
362 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
363 ? 1
364 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530365
Ratan Guptacc8feb42017-07-25 21:52:10 +0530366 auto MACAddress = variant.get<std::string>();
367
Ratan Guptad70f4532017-08-04 02:07:31 +0530368 // it is expected here that we should get the valid data
369 // but we may also get the default values.
370 // Validation of the data is done by settings.
371 //
372 // if mac address is default mac address then
373 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530374 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530375 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700376 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530377 rc = -1;
378 return rc;
379 }
380 // if addr is static then ipaddress,gateway,prefix
381 // should not be default one,don't send blank override.
382 if (isStatic)
383 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700384 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
385 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530386 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700387 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530388 rc = -1;
389 return rc;
390 }
391 }
392
Patrick Venture0b02be92018-08-31 11:55:55 -0700393 sscanf(
394 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
395 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
396 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
397 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530398
Ratan Guptadcb10672017-07-10 10:33:50 +0530399 respptr->data[MAC_OFFSET + 6] = 0x00;
400
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700401 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
402 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530403
404 uint8_t addressFamily = (properties["Type"].get<std::string>() ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700405 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
406 ? AF_INET
407 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530408
Patrick Venture0b02be92018-08-31 11:55:55 -0700409 addrSize = (addressFamily == AF_INET)
410 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
411 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530412
413 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530414 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700415 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530416
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530417 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
418
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700419 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530420
421 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
422
Ratan Guptad70f4532017-08-04 02:07:31 +0530423 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700424 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530425 }
426 catch (InternalFailure& e)
427 {
428 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700429 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530430 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500431 return rc;
432 }
433
Patrick Venture0b02be92018-08-31 11:55:55 -0700434 // PetiBoot-Specific
435 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700436 std::memcpy(respptr->data, net_conf_initial_bytes,
437 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500438
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700439 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530440
Ratan Guptafd28dd72016-08-01 04:58:01 -0500441#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700442 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500443
Ratan Guptadcb10672017-07-10 10:33:50 +0530444 for (uint8_t pos = 0; pos < index; pos++)
445 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700446 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530447 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500448#endif
449
Ratan Guptafd28dd72016-08-01 04:58:01 -0500450 return rc;
451}
452
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530453/** @brief convert IPv4 and IPv6 addresses from binary to text form.
454 * @param[in] family - IPv4/Ipv6
455 * @param[in] data - req data pointer.
456 * @param[in] offset - offset in the data.
457 * @param[in] addrSize - size of the data which needs to be read from offset.
458 * @returns address in text form.
459 */
460
Patrick Venture0b02be92018-08-31 11:55:55 -0700461std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
462 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530463{
464 char ipAddr[INET6_ADDRSTRLEN] = {};
465
Patrick Venture0b02be92018-08-31 11:55:55 -0700466 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530467 {
468 case AF_INET:
469 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700470 struct sockaddr_in addr4
471 {
472 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700473 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530474
Patrick Venture0b02be92018-08-31 11:55:55 -0700475 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530476
477 break;
478 }
479 case AF_INET6:
480 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700481 struct sockaddr_in6 addr6
482 {
483 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700484 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530485
Patrick Venture0b02be92018-08-31 11:55:55 -0700486 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530487
488 break;
489 }
490 default:
491 {
492 return {};
493 }
494 }
495
496 return ipAddr;
497}
498
Ratan Guptadcb10672017-07-10 10:33:50 +0530499int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500500{
Ratan Guptadcb10672017-07-10 10:33:50 +0530501 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500502 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700503 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530504 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700505 char addrOrigin{0};
506 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530507 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530508 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700509 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
510 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530511 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530512 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500513
Patrick Venture0b02be92018-08-31 11:55:55 -0700514 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500515 // version starts from sixth byte
516
Ratan Guptadcb10672017-07-10 10:33:50 +0530517 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500518 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530519 do
520 {
521 // cookie == 0x21 0x70 0x62 0x21
522 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700523 (net_conf_initial_bytes + COOKIE_OFFSET),
524 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530525 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700526 // cookie == 0
527 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
528 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530529 {
530 // need to zero out the network settings.
531 break;
532 }
533
534 log<level::ERR>("Invalid Cookie");
535 elog<InternalFailure>();
536 }
537
538 // vesion == 0x00 0x01
539 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700540 (net_conf_initial_bytes + VERSION_OFFSET),
541 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530542 {
543
544 log<level::ERR>("Invalid Version");
545 elog<InternalFailure>();
546 }
547
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700548 std::snprintf(
549 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
550 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
551 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
552 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530553
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700554 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
555 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530556
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530557 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530558 {
559 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530560 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530561 }
562
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530563 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700564 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
565 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530566
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530567 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530568
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700569 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
570 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530571
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530572 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
573
Ratan Gupta8c31d232017-08-13 05:49:43 +0530574 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530575 {
576 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
577 family = AF_INET6;
578 }
579
Patrick Venture0b02be92018-08-31 11:55:55 -0700580 ipAddress =
581 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530582
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530583 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
584
Patrick Venture0b02be92018-08-31 11:55:55 -0700585 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530586
Patrick Venture0b02be92018-08-31 11:55:55 -0700587 // Cookie == 0 or it is a valid cookie
588 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
589 std::to_string(prefix) + ",gateway="s + gateway +
590 ",mac="s + mac + ",addressOrigin="s +
591 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500592
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530593 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
594
595 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
596 SETTINGS_ROOT, SETTINGS_MATCH);
597 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
598 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530599 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530600 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700601 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530602 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700603 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530604 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700605 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530606 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700607 IP_INTERFACE, "Gateway", std::string(gateway));
608 ipmi::setDbusProperty(
609 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
610 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530611 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700612 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500613
Patrick Venture0b02be92018-08-31 11:55:55 -0700614 log<level::DEBUG>(
615 "Network configuration changed",
616 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500617 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530618 catch (InternalFailure& e)
619 {
620 commit<InternalFailure>();
621 return -1;
622 }
623
624 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500625}
626
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500627uint32_t getPOHCounter()
628{
629 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
630
Patrick Venture0b02be92018-08-31 11:55:55 -0700631 auto chassisStateObj =
632 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500633
Patrick Venture0b02be92018-08-31 11:55:55 -0700634 auto service =
635 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500636
Patrick Venture0b02be92018-08-31 11:55:55 -0700637 auto propValue =
638 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
639 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500640
641 return propValue.get<uint32_t>();
642}
643
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500644ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
645 ipmi_request_t request,
646 ipmi_response_t response,
647 ipmi_data_len_t data_len,
648 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500649{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500650 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800651 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500652 *data_len = 0;
653 return rc;
654}
655
Nan Li8d15fb42016-08-16 22:29:40 +0800656ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700657 ipmi_request_t request,
658 ipmi_response_t response,
659 ipmi_data_len_t data_len,
660 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800661{
662 // sd_bus error
663 ipmi_ret_t rc = IPMI_CC_OK;
664
665 ipmi_chassis_cap_t chassis_cap{};
666
667 *data_len = sizeof(ipmi_chassis_cap_t);
668
669 // TODO: need future work. Get those flag from MRW.
670
671 // capabilities flags
672 // [7..4] - reserved
673 // [3] – 1b = provides power interlock (IPM 1.5)
674 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
Patrick Venture0b02be92018-08-31 11:55:55 -0700675 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis has
676 // capabilities
677 // to lock out external power control and reset button or front
678 // panel interfaces and/or detect tampering with those
679 // interfaces).
Nan Li8d15fb42016-08-16 22:29:40 +0800680 // [0] -1b = Chassis provides intrusion (physical security) sensor.
681 // set to default value 0x0.
682 chassis_cap.cap_flags = 0x0;
683
684 // Since we do not have a separate SDR Device/SEL Device/ FRU repository.
685 // The 20h was given as those 5 device addresses.
686 // Chassis FRU info Device Address
687 chassis_cap.fru_info_dev_addr = 0x20;
688
689 // Chassis SDR Device Address
690 chassis_cap.sdr_dev_addr = 0x20;
691
692 // Chassis SEL Device Address
693 chassis_cap.sel_dev_addr = 0x20;
694
695 // Chassis System Management Device Address
696 chassis_cap.system_management_dev_addr = 0x20;
697
698 // Chassis Bridge Device Address.
699 chassis_cap.bridge_dev_addr = 0x20;
700
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700701 std::memcpy(response, &chassis_cap, *data_len);
Nan Li8d15fb42016-08-16 22:29:40 +0800702
703 return rc;
704}
705
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530706//------------------------------------------
707// Calls into Host State Manager Dbus object
708//------------------------------------------
709int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600710{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500711 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700712 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500713 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700714 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
715 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530716
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500717 // sd_bus error
718 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700719 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600720
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500721 // SD Bus error report mechanism.
722 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600723
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500724 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700725 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500726 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
727 if (rc < 0)
728 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700729 log<level::ERR>(
730 "Failed to get bus name",
731 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500732 return rc;
733 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530734
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500735 // Convert to string equivalent of the passed in transition enum.
736 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530737
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500738 rc = sd_bus_call_method(bus_type, // On the system bus
739 busname, // Service to contact
740 HOST_STATE_MANAGER_ROOT, // Object path
741 DBUS_PROPERTY_IFACE, // Interface name
742 "Set", // Method to be called
743 &bus_error, // object to return error
744 nullptr, // Response buffer if any
745 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700746 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
747 request.c_str());
748 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500749 {
750 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530751 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500752 }
753 else
754 {
755 log<level::INFO>("Transition request initiated successfully");
756 }
vishwa36993272015-11-20 12:43:49 -0600757
758 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500759 free(busname);
vishwa36993272015-11-20 12:43:49 -0600760
Sergey Solomineb9b8142016-08-23 09:07:28 -0500761 return rc;
vishwa36993272015-11-20 12:43:49 -0600762}
763
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500764namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500765{
Nan Lifdd8ec52016-08-28 03:57:40 +0800766
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500767using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
768using IpmiValue = uint8_t;
769using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800770
Patrick Venture0b02be92018-08-31 11:55:55 -0700771std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500772 {RestorePolicy::Policy::AlwaysOff, 0x00},
773 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700774 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800775
Yong Lic6713cf2018-09-12 12:35:13 +0800776static constexpr uint8_t noChange = 0x03;
777static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
778static constexpr uint8_t policyBitMask = 0x07;
779static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500780} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800781
782//----------------------------------------------------------------------
783// Get Chassis Status commands
784//----------------------------------------------------------------------
785ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500786 ipmi_request_t request,
787 ipmi_response_t response,
788 ipmi_data_len_t data_len,
789 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800790{
Patrick Venture0b02be92018-08-31 11:55:55 -0700791 const char* objname = "/org/openbmc/control/power0";
792 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800793
Patrick Venture0b02be92018-08-31 11:55:55 -0700794 sd_bus* bus = NULL;
795 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800796 int r = 0;
797 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700798 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800799 ipmi_ret_t rc = IPMI_CC_OK;
800 ipmi_get_chassis_status_t chassis_status{};
801
Nan Lifdd8ec52016-08-28 03:57:40 +0800802 uint8_t s = 0;
803
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500804 using namespace chassis::internal;
805 using namespace chassis::internal::cache;
806 using namespace power_policy;
807
Deepak Kodihallie6027092017-08-27 08:13:37 -0500808 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700809 auto method = dbus.new_method_call(
810 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
811 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500812 method.append(powerRestoreIntf, "PowerRestorePolicy");
813 auto resp = dbus.call(method);
814 if (resp.is_method_error())
815 {
816 log<level::ERR>("Error in PowerRestorePolicy Get");
817 report<InternalFailure>();
818 *data_len = 0;
819 return IPMI_CC_UNSPECIFIED_ERROR;
820 }
821 sdbusplus::message::variant<std::string> result;
822 resp.read(result);
823 auto powerRestore =
824 RestorePolicy::convertPolicyFromString(result.get<std::string>());
Nan Lifdd8ec52016-08-28 03:57:40 +0800825
826 *data_len = 4;
827
Tom Joseph63a00512017-08-09 23:39:59 +0530828 bus = ipmid_get_sd_bus_connection();
829
Nan Lifdd8ec52016-08-28 03:57:40 +0800830 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700831 if (r < 0)
832 {
833 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800834 rc = IPMI_CC_UNSPECIFIED_ERROR;
835 goto finish;
836 }
837
Patrick Venture0b02be92018-08-31 11:55:55 -0700838 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
839 "i");
840 if (r < 0)
841 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530842 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700843 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
844 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530845 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800846 rc = IPMI_CC_UNSPECIFIED_ERROR;
847 goto finish;
848 }
849
850 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700851 if (r < 0)
852 {
853 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800854 rc = IPMI_CC_UNSPECIFIED_ERROR;
855 goto finish;
856 }
857
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500858 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800859
860 // Current Power State
861 // [7] reserved
862 // [6..5] power restore policy
863 // 00b = chassis stays powered off after AC/mains returns
864 // 01b = after AC returns, power is restored to the state that was
865 // in effect when AC/mains was lost.
866 // 10b = chassis always powers up after AC/mains returns
867 // 11b = unknow
868 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700869 // Do we need to define a dbus property to identify the restore
870 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800871
872 // [4] power control fault
873 // 1b = controller attempted to turn system power on or off, but
874 // system did not enter desired state.
875 // Set to 0b, since We don't support it..
876
877 // [3] power fault
878 // 1b = fault detected in main power subsystem.
879 // set to 0b. for we don't support it.
880
881 // [2] 1b = interlock (chassis is presently shut down because a chassis
882 // panel interlock switch is active). (IPMI 1.5)
883 // set to 0b, for we don't support it.
884
885 // [1] power overload
886 // 1b = system shutdown because of power overload condition.
887 // set to 0b, for we don't support it.
888
889 // [0] power is on
890 // 1b = system power is on
891 // 0b = system power is off(soft-off S4/S5, or mechanical off)
892
Patrick Venture0b02be92018-08-31 11:55:55 -0700893 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800894
895 // Last Power Event
896 // [7..5] – reserved
897 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
898 // [3] – 1b = last power down caused by power fault
899 // [2] – 1b = last power down caused by a power interlock being activated
900 // [1] – 1b = last power down caused by a Power overload
901 // [0] – 1b = AC failed
902 // set to 0x0, for we don't support these fields.
903
904 chassis_status.last_power_event = 0;
905
906 // Misc. Chassis State
907 // [7] – reserved
908 // [6] – 1b = Chassis Identify command and state info supported (Optional)
909 // 0b = Chassis Identify command support unspecified via this command.
910 // (The Get Command Support command , if implemented, would still
911 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700912 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
913 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800914 // as 00b) otherwise. Returns the present chassis identify state.
915 // Refer to the Chassis Identify command for more info.
916 // 00b = chassis identify state = Off
917 // 01b = chassis identify state = Temporary(timed) On
918 // 10b = chassis identify state = Indefinite On
919 // 11b = reserved
920 // [3] – 1b = Cooling/fan fault detected
921 // [2] – 1b = Drive Fault
922 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
923 // push-buttons disabled.)
924 // [0] – 1b = Chassis Intrusion active
925 // set to 0, for we don't support them.
926 chassis_status.misc_power_state = 0;
927
928 // Front Panel Button Capabilities and disable/enable status(Optional)
929 // set to 0, for we don't support them.
930 chassis_status.front_panel_button_cap_status = 0;
931
932 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700933 std::memcpy(response, &chassis_status, *data_len);
Nan Lifdd8ec52016-08-28 03:57:40 +0800934
935finish:
936 free(busname);
937 reply = sd_bus_message_unref(reply);
938
939 return rc;
940}
Chris Austen7888c4d2015-12-03 15:26:20 -0600941
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530942//-------------------------------------------------------------
943// Send a command to SoftPowerOff application to stop any timer
944//-------------------------------------------------------------
945int stop_soft_off_timer()
946{
Patrick Venture0b02be92018-08-31 11:55:55 -0700947 constexpr auto iface = "org.freedesktop.DBus.Properties";
948 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
949 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530950
Patrick Venture0b02be92018-08-31 11:55:55 -0700951 constexpr auto property = "ResponseReceived";
952 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
953 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530954
955 // Get the system bus where most system services are provided.
956 auto bus = ipmid_get_sd_bus_connection();
957
958 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500959 // TODO openbmc/openbmc#1661 - Mapper refactor
960 //
961 // See openbmc/openbmc#1743 for some details but high level summary is that
962 // for now the code will directly call the soft off interface due to a
963 // race condition with mapper usage
964 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700965 // char *busname = nullptr;
966 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
967 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500968 //{
969 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530970 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500971 // return r;
972 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530973
974 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500975 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700976 "Set", nullptr, nullptr, "ssv", soft_off_iface,
977 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530978 if (rc < 0)
979 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530980 log<level::ERR>("Failed to set property in SoftPowerOff object",
981 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530982 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500983
Patrick Venture0b02be92018-08-31 11:55:55 -0700984 // TODO openbmc/openbmc#1661 - Mapper refactor
985 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530986 return rc;
987}
988
vishwa36993272015-11-20 12:43:49 -0600989//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500990// Create file to indicate there is no need for softoff notification to host
991//----------------------------------------------------------------------
992void indicate_no_softoff_needed()
993{
994 fs::path path{HOST_INBAND_REQUEST_DIR};
995 if (!fs::is_directory(path))
996 {
997 fs::create_directory(path);
998 }
999
1000 // Add the host instance (default 0 for now) to the file name
1001 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001002 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001003 size++; // null
1004 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001005 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001006
1007 // Append file name to directory and create it
1008 path /= buf.get();
1009 std::ofstream(path.c_str());
1010}
1011
1012//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001013// Chassis Control commands
1014//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001015ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1016 ipmi_request_t request,
1017 ipmi_response_t response,
1018 ipmi_data_len_t data_len,
1019 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001020{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001021 // Error from power off.
1022 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001023
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001024 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001025 *data_len = 0;
1026
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001027 // Catch the actual operaton by peeking into request buffer
Patrick Venture0b02be92018-08-31 11:55:55 -07001028 uint8_t chassis_ctrl_cmd = *(uint8_t*)request;
vishwa36993272015-11-20 12:43:49 -06001029
Patrick Venture0b02be92018-08-31 11:55:55 -07001030 switch (chassis_ctrl_cmd)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001031 {
1032 case CMD_POWER_ON:
1033 rc = initiate_state_transition(State::Host::Transition::On);
1034 break;
1035 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301036 // This path would be hit in 2 conditions.
1037 // 1: When user asks for power off using ipmi chassis command 0x04
1038 // 2: Host asking for power off post shutting down.
1039
1040 // If it's a host requested power off, then need to nudge Softoff
1041 // application that it needs to stop the watchdog timer if running.
1042 // If it is a user requested power off, then this is not really
1043 // needed. But then we need to differentiate between user and host
1044 // calling this same command
1045
1046 // For now, we are going ahead with trying to nudge the soft off and
1047 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001048 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301049
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001050 // Only request the Off transition if the soft power off
1051 // application is not running
1052 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001053 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001054 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301055 // that it should not run. Not doing this will result in State
1056 // manager doing a default soft power off when asked for power
1057 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001058 indicate_no_softoff_needed();
1059
1060 // Now request the shutdown
1061 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001062 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001063 else
1064 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301065 log<level::INFO>("Soft off is running, so let shutdown target "
1066 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001067 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001068 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301069
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001070 case CMD_HARD_RESET:
1071 case CMD_POWER_CYCLE:
1072 // SPEC has a section that says certain implementations can trigger
1073 // PowerOn if power is Off when a command to power cycle is
1074 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001075
1076 // First create a file to indicate to the soft off application
1077 // that it should not run since this is a direct user initiated
1078 // power reboot request (i.e. a reboot request that is not
1079 // originating via a soft power off SMS request)
1080 indicate_no_softoff_needed();
1081
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001082 rc = initiate_state_transition(State::Host::Transition::Reboot);
1083 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301084
1085 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1086 // Request Host State Manager to do a soft power off
1087 rc = initiate_state_transition(State::Host::Transition::Off);
1088 break;
1089
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001090 default:
1091 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301092 log<level::ERR>("Invalid Chassis Control command",
1093 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001094 rc = -1;
1095 }
1096 }
vishwa36993272015-11-20 12:43:49 -06001097
Patrick Venture0b02be92018-08-31 11:55:55 -07001098 return ((rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001099}
1100
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001101/** @brief Return D-Bus connection string to enclosure identify LED object
1102 *
1103 * @param[in, out] connection - connection to D-Bus object
1104 * @return a IPMI return code
1105 */
1106std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301107{
Tom Joseph5110c122018-03-23 17:55:40 +05301108 // lookup enclosure_identify group owner(s) in mapper
1109 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001110 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1111 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301112
1113 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001114 static const std::vector<std::string> interfaces = {
1115 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301116 mapperCall.append(interfaces);
1117 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1118 if (mapperReply.is_method_error())
1119 {
1120 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001121 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301122 }
1123 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1124 mapperReply.read(mapperResp);
1125
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001126 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301127 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001128 log<level::ERR>(
1129 "Invalid number of enclosure identify objects.",
1130 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001131 elog<InternalFailure>();
1132 }
1133 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1134 return pair.first;
1135}
Tom Joseph5110c122018-03-23 17:55:40 +05301136
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001137/** @brief Turn On/Off enclosure identify LED
1138 *
1139 * @param[in] flag - true to turn on LED, false to turn off
1140 * @return a IPMI return code
1141 */
1142void enclosureIdentifyLed(bool flag)
1143{
1144 using namespace chassis::internal;
1145 std::string connection = std::move(getEnclosureIdentifyConnection());
Patrick Venture0b02be92018-08-31 11:55:55 -07001146 auto led =
1147 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1148 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001149 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001150 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001151 auto ledReply = dbus.call(led);
1152 if (ledReply.is_method_error())
1153 {
1154 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001155 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001156 elog<InternalFailure>();
1157 }
1158}
1159
1160/** @brief Callback method to turn off LED
1161 */
1162void enclosureIdentifyLedOff()
1163{
1164 try
1165 {
1166 enclosureIdentifyLed(false);
1167 }
1168 catch (const InternalFailure& e)
1169 {
1170 report<InternalFailure>();
1171 }
1172}
1173
1174/** @brief Create timer to turn on and off the enclosure LED
1175 */
1176void createIdentifyTimer()
1177{
1178 if (!identifyTimer)
1179 {
1180 identifyTimer = std::make_unique<phosphor::ipmi::Timer>(
1181 ipmid_get_sd_event_connection(), enclosureIdentifyLedOff);
1182 }
1183}
1184
1185ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1186 ipmi_request_t request,
1187 ipmi_response_t response,
1188 ipmi_data_len_t data_len,
1189 ipmi_context_t context)
1190{
1191 if (*data_len > chassisIdentifyReqLength)
1192 {
1193 return IPMI_CC_REQ_DATA_LEN_INVALID;
1194 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001195 uint8_t identifyInterval =
1196 *data_len > identifyIntervalPos
1197 ? (static_cast<uint8_t*>(request))[identifyIntervalPos]
1198 : DEFAULT_IDENTIFY_TIME_OUT;
1199 bool forceIdentify =
1200 (*data_len == chassisIdentifyReqLength)
1201 ? (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01
1202 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301203
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001204 if (identifyInterval || forceIdentify)
1205 {
1206 // stop the timer if already started, for force identify we should
1207 // not turn off LED
1208 identifyTimer->setTimer(SD_EVENT_OFF);
1209 try
Tom Joseph5110c122018-03-23 17:55:40 +05301210 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001211 enclosureIdentifyLed(true);
1212 }
1213 catch (const InternalFailure& e)
1214 {
1215 report<InternalFailure>();
1216 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301217 }
1218
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001219 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301220 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001221 return IPMI_CC_OK;
1222 }
1223 // start the timer
1224 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001225 std::chrono::seconds(identifyInterval));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001226 identifyTimer->startTimer(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301227 }
Tom Josephbed26992018-07-31 23:00:24 +05301228 else if (!identifyInterval)
1229 {
1230 identifyTimer->setTimer(SD_EVENT_OFF);
1231 enclosureIdentifyLedOff();
1232 }
Tom Joseph5110c122018-03-23 17:55:40 +05301233 return IPMI_CC_OK;
1234}
1235
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001236namespace boot_options
1237{
1238
1239using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1240using IpmiValue = uint8_t;
1241constexpr auto ipmiDefault = 0;
1242
Patrick Venture0b02be92018-08-31 11:55:55 -07001243std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001244 {0x01, Source::Sources::Network},
1245 {0x02, Source::Sources::Disk},
1246 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001247 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001248
Patrick Venture0b02be92018-08-31 11:55:55 -07001249std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001250 {0x03, Mode::Modes::Safe},
1251 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001252 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001253
Patrick Venture0b02be92018-08-31 11:55:55 -07001254std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001255 {Source::Sources::Network, 0x01},
1256 {Source::Sources::Disk, 0x02},
1257 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001258 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001259
Patrick Venture0b02be92018-08-31 11:55:55 -07001260std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001261 {Mode::Modes::Safe, 0x03},
1262 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001263 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001264
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001265} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001266
Marri Devender Rao81719702018-05-07 00:53:48 -05001267/** @brief Set the property value for boot source
1268 * @param[in] source - boot source value
1269 * @return On failure return IPMI error.
1270 */
1271static ipmi_ret_t setBootSource(const Source::Sources& source)
1272{
1273 using namespace chassis::internal;
1274 using namespace chassis::internal::cache;
1275 sdbusplus::message::variant<std::string> property =
1276 convertForMessage(source);
1277 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1278 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001279 auto method = dbus.new_method_call(
1280 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1281 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001282 method.append(bootSourceIntf, "BootSource", property);
1283 auto reply = dbus.call(method);
1284 if (reply.is_method_error())
1285 {
1286 log<level::ERR>("Error in BootSource Set");
1287 report<InternalFailure>();
1288 return IPMI_CC_UNSPECIFIED_ERROR;
1289 }
1290 return IPMI_CC_OK;
1291}
1292
Patrick Venture0b02be92018-08-31 11:55:55 -07001293/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001294 * @param[in] mode - boot mode value
1295 * @return On failure return IPMI error.
1296 */
1297static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1298{
1299 using namespace chassis::internal;
1300 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001301 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001302 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1303 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001304 auto method = dbus.new_method_call(
1305 objects.service(bootModeSetting, bootModeIntf).c_str(),
1306 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001307 method.append(bootModeIntf, "BootMode", property);
1308 auto reply = dbus.call(method);
1309 if (reply.is_method_error())
1310 {
1311 log<level::ERR>("Error in BootMode Set");
1312 report<InternalFailure>();
1313 return IPMI_CC_UNSPECIFIED_ERROR;
1314 }
1315 return IPMI_CC_OK;
1316}
1317
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001318ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1319 ipmi_request_t request,
1320 ipmi_response_t response,
1321 ipmi_data_len_t data_len,
1322 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001323{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001324 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001325 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001326 char* p = NULL;
1327 get_sys_boot_options_response_t* resp =
1328 (get_sys_boot_options_response_t*)response;
1329 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001330 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001331
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001332 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001333 resp->version = SET_PARM_VERSION;
1334 resp->parm = 5;
1335 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001336
shgoupfd84fbbf2015-12-17 10:05:51 +08001337 /*
1338 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1339 * This is the only parameter used by petitboot.
1340 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001341 if (reqptr->parameter ==
1342 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1343 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001344
Ratan Guptafd28dd72016-08-01 04:58:01 -05001345 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001346 using namespace chassis::internal;
1347 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001348
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001349 try
ratagupta6f6bff2016-04-04 06:20:11 -05001350 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001351 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1352 const auto& bootSourceSetting =
1353 std::get<settings::Path>(bootSetting);
1354 auto oneTimeEnabled =
1355 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001356 auto method = dbus.new_method_call(
1357 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1358 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001359 method.append(bootSourceIntf, "BootSource");
1360 auto reply = dbus.call(method);
1361 if (reply.is_method_error())
1362 {
1363 log<level::ERR>("Error in BootSource Get");
1364 report<InternalFailure>();
1365 *data_len = 0;
1366 return IPMI_CC_UNSPECIFIED_ERROR;
1367 }
1368 sdbusplus::message::variant<std::string> result;
1369 reply.read(result);
1370 auto bootSource =
1371 Source::convertSourcesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001372
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001373 bootSetting = settings::boot::setting(objects, bootModeIntf);
1374 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1375 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001376 objects.service(bootModeSetting, bootModeIntf).c_str(),
1377 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001378 method.append(bootModeIntf, "BootMode");
1379 reply = dbus.call(method);
1380 if (reply.is_method_error())
1381 {
1382 log<level::ERR>("Error in BootMode Get");
1383 report<InternalFailure>();
1384 *data_len = 0;
1385 return IPMI_CC_UNSPECIFIED_ERROR;
1386 }
1387 reply.read(result);
1388 auto bootMode =
1389 Mode::convertModesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001390
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001391 bootOption = sourceDbusToIpmi.at(bootSource);
1392 if ((Mode::Modes::Regular == bootMode) &&
1393 (Source::Sources::Default == bootSource))
1394 {
1395 bootOption = ipmiDefault;
1396 }
1397 else if (Source::Sources::Default == bootSource)
1398 {
1399 bootOption = modeDbusToIpmi.at(bootMode);
1400 }
1401 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001402
Patrick Venture0b02be92018-08-31 11:55:55 -07001403 resp->data[0] = oneTimeEnabled
1404 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1405 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001406
ratagupta6f6bff2016-04-04 06:20:11 -05001407 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001408 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001409 catch (InternalFailure& e)
1410 {
1411 report<InternalFailure>();
1412 *data_len = 0;
1413 return IPMI_CC_UNSPECIFIED_ERROR;
1414 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001415 }
1416 else if (reqptr->parameter ==
1417 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1418 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001419
Patrick Venture0b02be92018-08-31 11:55:55 -07001420 *data_len =
1421 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001422
Patrick Venture0b02be92018-08-31 11:55:55 -07001423 resp->parm =
1424 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001425
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001426 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001427
Patrick Venture0b02be92018-08-31 11:55:55 -07001428 if (ret < 0)
1429 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001430
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301431 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001432 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001433 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001434 }
1435 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001436 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001437 }
1438
Patrick Venture0b02be92018-08-31 11:55:55 -07001439 else
1440 {
1441 log<level::ERR>("Unsupported parameter",
1442 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001443 }
1444
1445 if (p)
1446 free(p);
1447
Ratan Guptafd28dd72016-08-01 04:58:01 -05001448 if (rc == IPMI_CC_OK)
1449 {
1450 *data_len += 2;
1451 }
1452
shgoupfd84fbbf2015-12-17 10:05:51 +08001453 return rc;
1454}
1455
shgoupfd84fbbf2015-12-17 10:05:51 +08001456ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001457 ipmi_request_t request,
1458 ipmi_response_t response,
1459 ipmi_data_len_t data_len,
1460 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001461{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001462 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001463 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001464 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001465
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001466 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1467 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001468
shgoupfd84fbbf2015-12-17 10:05:51 +08001469 // This IPMI command does not have any resposne data
1470 *data_len = 0;
1471
1472 /* 000101
1473 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1474 * This is the only parameter used by petitboot.
1475 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001476
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001477 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1478 {
1479 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1480 using namespace chassis::internal;
1481 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001482 auto oneTimeEnabled = false;
1483 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301484 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001485 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001486
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001487 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001488 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001489 bool permanent =
1490 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1491 SET_PARM_BOOT_FLAGS_PERMANENT;
1492
Patrick Venture0b02be92018-08-31 11:55:55 -07001493 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301494
1495 oneTimeEnabled =
1496 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1497
1498 /*
1499 * Check if the current boot setting is onetime or permanent, if the
1500 * request in the command is otherwise, then set the "Enabled"
1501 * property in one_time object path to 'True' to indicate onetime
1502 * and 'False' to indicate permanent.
1503 *
1504 * Once the onetime/permanent setting is applied, then the bootMode
1505 * and bootSource is updated for the corresponding object.
1506 */
1507 if ((permanent && oneTimeEnabled) ||
1508 (!permanent && !oneTimeEnabled))
1509 {
1510 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1511
Patrick Venture0b02be92018-08-31 11:55:55 -07001512 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1513 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301514 }
1515
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001516 auto modeItr = modeIpmiToDbus.find(bootOption);
1517 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1518 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001519 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001520 rc = setBootSource(sourceItr->second);
1521 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001522 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001523 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001524 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001525 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001526 // If a set boot device is mapping to a boot source, then reset
1527 // the boot mode D-Bus property to default.
1528 // This way the ipmid code can determine which property is not
1529 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001530 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001531 {
1532 setBootMode(Mode::Modes::Regular);
1533 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001534 }
1535 if (modeIpmiToDbus.end() != modeItr)
1536 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001537 rc = setBootMode(modeItr->second);
1538 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001539 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001540 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001541 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001542 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001543 // If a set boot device is mapping to a boot mode, then reset
1544 // the boot source D-Bus property to default.
1545 // This way the ipmid code can determine which property is not
1546 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001547 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001548 {
1549 setBootSource(Source::Sources::Default);
1550 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001551 }
1552 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001553 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001554 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001555 report<InternalFailure>();
1556 *data_len = 0;
1557 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001558 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001559 }
1560 else if (reqptr->parameter ==
1561 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1562 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001563
1564 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001565 if (ret < 0)
1566 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301567 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001568 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001569 rc = IPMI_CC_UNSPECIFIED_ERROR;
1570 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001571 }
1572 else if (reqptr->parameter ==
1573 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1574 {
Tom Josephf536c902017-09-25 18:08:15 +05301575 // Handle parameter #4 and return command completed normally
1576 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1577 // parameter. This is added to support the ipmitool command `chassis
1578 // bootdev` which sends set on parameter #4, before setting the boot
1579 // flags.
1580 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001581 }
1582 else
1583 {
1584 log<level::ERR>("Unsupported parameter",
1585 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001586 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001587 }
1588
1589 return rc;
1590}
1591
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001592ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1593 ipmi_request_t request, ipmi_response_t response,
1594 ipmi_data_len_t data_len, ipmi_context_t context)
1595{
1596 // sd_bus error
1597 ipmi_ret_t rc = IPMI_CC_OK;
1598
1599 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1600
1601 try
1602 {
1603 auto pohCounter = getPOHCounter();
1604 resptr->counterReading[0] = pohCounter;
1605 resptr->counterReading[1] = pohCounter >> 8;
1606 resptr->counterReading[2] = pohCounter >> 16;
1607 resptr->counterReading[3] = pohCounter >> 24;
1608 }
1609 catch (std::exception& e)
1610 {
1611 log<level::ERR>(e.what());
1612 return IPMI_CC_UNSPECIFIED_ERROR;
1613 }
1614
1615 resptr->minPerCount = poh::minutesPerCount;
1616 *data_len = sizeof(GetPOHCountResponse);
1617
1618 return rc;
1619}
1620
Yong Lic6713cf2018-09-12 12:35:13 +08001621ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1622 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1623 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1624{
1625 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1626 auto* resptr = reinterpret_cast<uint8_t*>(response);
1627 uint8_t reqPolicy = 0;
1628
1629 power_policy::DbusValue value =
1630 power_policy::RestorePolicy::Policy::AlwaysOff;
1631
1632 if (*data_len != power_policy::setPolicyReqLen)
1633 {
1634 phosphor::logging::log<level::ERR>("Unsupported request length",
1635 entry("LEN=0x%x", *data_len));
1636 *data_len = 0;
1637 return IPMI_CC_REQ_DATA_LEN_INVALID;
1638 }
1639
1640 reqPolicy = *reqptr & power_policy::policyBitMask;
1641 if (reqPolicy > power_policy::noChange)
1642 {
1643 phosphor::logging::log<level::ERR>("Reserved request parameter",
1644 entry("REQ=0x%x", reqPolicy));
1645 *data_len = 0;
1646 return IPMI_CC_PARM_NOT_SUPPORTED;
1647 }
1648
1649 if (reqPolicy == power_policy::noChange)
1650 {
1651 // just return the supported policy
1652 *resptr = power_policy::allSupport;
1653 *data_len = power_policy::setPolicyReqLen;
1654 return IPMI_CC_OK;
1655 }
1656
1657 for (auto const& it : power_policy::dbusToIpmi)
1658 {
1659 if (it.second == reqPolicy)
1660 {
1661 value = it.first;
1662 break;
1663 }
1664 }
1665
1666 try
1667 {
1668 const settings::Path& powerRestoreSetting =
1669 chassis::internal::cache::objects.map
1670 .at(chassis::internal::powerRestoreIntf)
1671 .front();
1672 sdbusplus::message::variant<std::string> property =
1673 convertForMessage(value);
1674
1675 auto method = chassis::internal::dbus.new_method_call(
1676 chassis::internal::cache::objects
1677 .service(powerRestoreSetting,
1678 chassis::internal::powerRestoreIntf)
1679 .c_str(),
1680 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1681
1682 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1683 property);
1684 auto reply = chassis::internal::dbus.call(method);
1685 if (reply.is_method_error())
1686 {
1687 phosphor::logging::log<level::ERR>("Unspecified Error");
1688 *data_len = 0;
1689 return IPMI_CC_UNSPECIFIED_ERROR;
1690 }
1691 }
1692 catch (InternalFailure& e)
1693 {
1694 report<InternalFailure>();
1695 *data_len = 0;
1696 return IPMI_CC_UNSPECIFIED_ERROR;
1697 }
1698
1699 *data_len = power_policy::setPolicyReqLen;
1700 return IPMI_CC_OK;
1701}
1702
Adriana Kobylak40814c62015-10-27 15:58:44 -05001703void register_netfn_chassis_functions()
1704{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001705 createIdentifyTimer();
1706
Tom05732372016-09-06 17:21:23 +05301707 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001708 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1709 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001710
Tom05732372016-09-06 17:21:23 +05301711 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001712 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1713 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001714
Tom05732372016-09-06 17:21:23 +05301715 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301716 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001717 ipmi_chassis_get_sys_boot_options,
1718 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001719
Tom05732372016-09-06 17:21:23 +05301720 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001721 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1722 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001723
Tom05732372016-09-06 17:21:23 +05301724 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001725 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1726 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001727
Tom Joseph5110c122018-03-23 17:55:40 +05301728 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301729 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1730 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1731
Tom05732372016-09-06 17:21:23 +05301732 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301733 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001734 ipmi_chassis_set_sys_boot_options,
1735 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001736 // <Get POH Counter>
1737 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1738 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001739
1740 // <Set Power Restore Policy>
1741 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1742 ipmi_chassis_set_power_restore_policy,
1743 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001744}