blob: 5e86f17edfd78a69f306176a65f4e7025528169f [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "chassishandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05005#include "ipmid.hpp"
6#include "settings.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07007#include "types.hpp"
Ratan Guptacc8feb42017-07-25 21:52:10 +05308#include "utils.hpp"
Ratan Guptadcb10672017-07-10 10:33:50 +05309
Patrick Venture0b02be92018-08-31 11:55:55 -070010#include <arpa/inet.h>
11#include <endian.h>
Patrick Venture46470a32018-09-07 19:26:25 -070012#include <host-ipmid/ipmid-api.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070013#include <limits.h>
14#include <mapper.h>
15#include <netinet/in.h>
16#include <stdint.h>
Adriana Kobylak40814c62015-10-27 15:58:44 -050017#include <stdio.h>
Ratan Guptafd28dd72016-08-01 04:58:01 -050018#include <stdlib.h>
Ratan Guptafd28dd72016-08-01 04:58:01 -050019#include <string.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070020
Ratan Guptafd28dd72016-08-01 04:58:01 -050021#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070022#include <chrono>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050023#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053024#include <future>
Patrick Venture0b02be92018-08-31 11:55:55 -070025#include <sstream>
Vernon Mauery185b9f82018-07-20 10:52:36 -070026#if __has_include(<filesystem>)
27#include <filesystem>
28#elif __has_include(<experimental/filesystem>)
Andrew Geisslera6e3a302017-05-31 19:34:00 -050029#include <experimental/filesystem>
Patrick Venture0b02be92018-08-31 11:55:55 -070030namespace std
31{
32// splice experimental::filesystem into std
33namespace filesystem = std::experimental::filesystem;
34} // namespace std
Vernon Mauery185b9f82018-07-20 10:52:36 -070035#else
Patrick Venture0b02be92018-08-31 11:55:55 -070036#error filesystem not available
Vernon Mauery185b9f82018-07-20 10:52:36 -070037#endif
Patrick Venture0b02be92018-08-31 11:55:55 -070038
39#include "timer.hpp"
40
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050041#include <map>
Ratan Guptadcb10672017-07-10 10:33:50 +053042#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070043#include <phosphor-logging/log.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053044#include <sdbusplus/bus.hpp>
45#include <sdbusplus/server/object.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070046#include <string>
47#include <xyz/openbmc_project/Common/error.hpp>
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050048#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070049#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
Deepak Kodihalli18b70d12017-07-21 13:36:33 -050050#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070051#include <xyz/openbmc_project/State/Host/server.hpp>
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050052#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
Patrick Venture46470a32018-09-07 19:26:25 -070053
Patrick Venture0b02be92018-08-31 11:55:55 -070054// Defines
55#define SET_PARM_VERSION 0x01
56#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40 // boot flags data1 7th bit on
57#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80 // boot flags data1 8th bit on
58#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT \
59 0xC0 // boot flags data1 7 & 8 bit
60 // on
ratagupta6f6bff2016-04-04 06:20:11 -050061
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050062std::unique_ptr<phosphor::ipmi::Timer> identifyTimer = nullptr;
63
Patrick Venture0b02be92018-08-31 11:55:55 -070064constexpr size_t SIZE_MAC = 18;
65constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
66 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
67 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050068constexpr size_t SIZE_PREFIX = 7;
69constexpr size_t MAX_PREFIX_VALUE = 32;
70constexpr size_t SIZE_COOKIE = 4;
71constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053072constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053073
Patrick Venture0b02be92018-08-31 11:55:55 -070074// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053075static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070076 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050077
78static constexpr size_t COOKIE_OFFSET = 1;
79static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053080static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050081static constexpr size_t MAC_OFFSET = 9;
82static constexpr size_t ADDRTYPE_OFFSET = 16;
83static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050084
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050085static constexpr size_t encIdentifyObjectsSize = 1;
86static constexpr size_t chassisIdentifyReqLength = 2;
87static constexpr size_t identifyIntervalPos = 0;
88static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080089
Adriana Kobylak40814c62015-10-27 15:58:44 -050090void register_netfn_chassis_functions() __attribute__((constructor));
91
shgoupfd84fbbf2015-12-17 10:05:51 +080092// Host settings in dbus
93// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070094const char* settings_object_name = "/org/openbmc/settings/host0";
95const char* settings_intf_name = "org.freedesktop.DBus.Properties";
96const char* host_intf_name = "org.openbmc.settings.Host";
97const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053098 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080099
Ratan Guptadcb10672017-07-10 10:33:50 +0530100constexpr auto SETTINGS_ROOT = "/";
101constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530102
103constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
104constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
105
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500106static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
107static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -0700108 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500109static constexpr auto pOHCounterProperty = "POHCounter";
110static constexpr auto match = "chassis0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530111
Nan Li8d15fb42016-08-16 22:29:40 +0800112typedef struct
113{
114 uint8_t cap_flags;
115 uint8_t fru_info_dev_addr;
116 uint8_t sdr_dev_addr;
117 uint8_t sel_dev_addr;
118 uint8_t system_management_dev_addr;
119 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700120} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800121
Nan Lifdd8ec52016-08-28 03:57:40 +0800122typedef struct
123{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500124 uint8_t cur_power_state;
125 uint8_t last_power_event;
126 uint8_t misc_power_state;
127 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700128} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800129
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500130/**
131 * @struct Get POH counter command response data
132 */
133struct GetPOHCountResponse
134{
Patrick Venture0b02be92018-08-31 11:55:55 -0700135 uint8_t minPerCount; ///< Minutes per count
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500136 uint8_t counterReading[4]; ///< Counter reading
Patrick Venture0b02be92018-08-31 11:55:55 -0700137} __attribute__((packed));
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500138
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530139// Phosphor Host State manager
140namespace State = sdbusplus::xyz::openbmc_project::State::server;
141
Vernon Mauery185b9f82018-07-20 10:52:36 -0700142namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500143
Ratan Guptadcb10672017-07-10 10:33:50 +0530144using namespace phosphor::logging;
145using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500146using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500147namespace chassis
148{
149namespace internal
150{
151
152constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
153constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500154constexpr auto powerRestoreIntf =
155 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500156sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
157
158namespace cache
159{
160
161settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500162 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500163
164} // namespace cache
165} // namespace internal
166} // namespace chassis
167
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500168namespace poh
169{
170
171constexpr auto minutesPerCount = 60;
172
173} // namespace poh
174
Patrick Venture0b02be92018-08-31 11:55:55 -0700175// TODO : Can remove the below function as we have
Ratan Guptadcb10672017-07-10 10:33:50 +0530176// new functions which uses sdbusplus.
177//
178// openbmc/openbmc#1489
Patrick Venture0b02be92018-08-31 11:55:55 -0700179int dbus_get_property(const char* name, char** buf)
shgoupfd84fbbf2015-12-17 10:05:51 +0800180{
181 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700182 sd_bus_message* m = NULL;
183 sd_bus* bus = NULL;
184 char* temp_buf = NULL;
185 char* connection = NULL;
shgoupfd84fbbf2015-12-17 10:05:51 +0800186 int r;
187
Brad Bishop35518682016-07-22 08:35:41 -0400188 // Get the system bus where most system services are provided.
189 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800190
Brad Bishop35518682016-07-22 08:35:41 -0400191 r = mapper_get_service(bus, settings_object_name, &connection);
Patrick Venture0b02be92018-08-31 11:55:55 -0700192 if (r < 0)
193 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530194 log<level::ERR>("Failed to get connection",
195 entry("OBJ_NAME=%s", settings_object_name),
196 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800197 goto finish;
198 }
199
shgoupfd84fbbf2015-12-17 10:05:51 +0800200 /*
201 * Bus, service, object path, interface and method are provided to call
202 * the method.
203 * Signatures and input arguments are provided by the arguments at the
204 * end.
205 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700206 r = sd_bus_call_method(bus, connection, /* service to contact */
207 settings_object_name, /* object path */
208 settings_intf_name, /* interface name */
209 "Get", /* method name */
210 &error, /* object to return error in */
211 &m, /* return message on success */
212 "ss", /* input signature */
213 host_intf_name, /* first argument */
214 name); /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800215
Patrick Venture0b02be92018-08-31 11:55:55 -0700216 if (r < 0)
217 {
218 log<level::ERR>("Failed to issue Get method call",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530219 entry("ERRNO=0x%X", r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800220 goto finish;
221 }
222
Patrick Venture0b02be92018-08-31 11:55:55 -0700223 /*
224 * The output should be parsed exactly the same as the output formatting
225 * specified.
226 */
227 r = sd_bus_message_read(m, "v", "s", &temp_buf);
228 if (r < 0)
229 {
230 log<level::ERR>("Failed to parse response message",
231 entry("ERRNO=0x%X", -r));
232 goto finish;
233 }
234
235 *buf = strdup(temp_buf);
236 /* *buf = (char*) malloc(strlen(temp_buf));
237 if (*buf) {
238 strcpy(*buf, temp_buf);
239 }
240 */
241
242finish:
shgoupfd84fbbf2015-12-17 10:05:51 +0800243 sd_bus_error_free(&error);
244 sd_bus_message_unref(m);
245 free(connection);
246
247 return r;
248}
249
Patrick Venture0b02be92018-08-31 11:55:55 -0700250// TODO : Can remove the below function as we have
251// new functions which uses sdbusplus.
252//
253// openbmc/openbmc#1489
254
255int dbus_set_property(const char* name, const char* value)
256{
257 sd_bus_error error = SD_BUS_ERROR_NULL;
258 sd_bus_message* m = NULL;
259 sd_bus* bus = NULL;
260 char* connection = NULL;
261 int r;
262
263 // Get the system bus where most system services are provided.
264 bus = ipmid_get_sd_bus_connection();
265
266 r = mapper_get_service(bus, settings_object_name, &connection);
267 if (r < 0)
268 {
269 log<level::ERR>("Failed to get connection",
270 entry("OBJ_NAME=%s", settings_object_name),
271 entry("ERRNO=0x%X", -r));
272 goto finish;
273 }
274
275 /*
276 * Bus, service, object path, interface and method are provided to call
277 * the method.
278 * Signatures and input arguments are provided by the arguments at the
279 * end.
280 */
281 r = sd_bus_call_method(bus, connection, /* service to contact */
282 settings_object_name, /* object path */
283 settings_intf_name, /* interface name */
284 "Set", /* method name */
285 &error, /* object to return error in */
286 &m, /* return message on success */
287 "ssv", /* input signature */
288 host_intf_name, /* first argument */
289 name, /* second argument */
290 "s", /* third argument */
291 value); /* fourth argument */
292
293 if (r < 0)
294 {
295 log<level::ERR>("Failed to issue Set method call",
296 entry("ERRNO=0x%X", r));
297 goto finish;
298 }
299
300finish:
301 sd_bus_error_free(&error);
302 sd_bus_message_unref(m);
303 free(connection);
304
305 return r;
306}
307
308struct get_sys_boot_options_t
309{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500310 uint8_t parameter;
311 uint8_t set;
312 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700313} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500314
Patrick Venture0b02be92018-08-31 11:55:55 -0700315struct get_sys_boot_options_response_t
316{
shgoupfd84fbbf2015-12-17 10:05:51 +0800317 uint8_t version;
318 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500319 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700320} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800321
Patrick Venture0b02be92018-08-31 11:55:55 -0700322struct set_sys_boot_options_t
323{
shgoupfd84fbbf2015-12-17 10:05:51 +0800324 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500325 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700326} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500327
Ratan Guptadcb10672017-07-10 10:33:50 +0530328int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500329{
Ratan Guptadcb10672017-07-10 10:33:50 +0530330 ipmi::PropertyMap properties;
331 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530332 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500333
Ratan Guptadcb10672017-07-10 10:33:50 +0530334 try
335 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700336 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530337 // objects,to handle such cases we are interested on that object
338 // which are on interested busname.
339 // Currenlty mapper doesn't give the readable busname(gives busid)
340 // so we can't match with bus name so giving some object specific info
341 // as SETTINGS_MATCH.
342 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500343
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530344 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530345
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530346 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
347 SETTINGS_ROOT, SETTINGS_MATCH);
348
349 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
350 SETTINGS_ROOT, SETTINGS_MATCH);
351
Patrick Venture0b02be92018-08-31 11:55:55 -0700352 properties = ipmi::getAllDbusProperties(
353 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
354 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
355 macObjectInfo.first, MAC_INTERFACE,
356 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530357
Patrick Venture0b02be92018-08-31 11:55:55 -0700358 auto ipAddress = properties["Address"].get<std::string>();
Ratan Guptad70f4532017-08-04 02:07:31 +0530359
360 auto gateway = properties["Gateway"].get<std::string>();
361
362 auto prefix = properties["PrefixLength"].get<uint8_t>();
363
Patrick Venture0b02be92018-08-31 11:55:55 -0700364 uint8_t isStatic =
365 (properties["Origin"].get<std::string>() ==
366 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
367 ? 1
368 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530369
Ratan Guptacc8feb42017-07-25 21:52:10 +0530370 auto MACAddress = variant.get<std::string>();
371
Ratan Guptad70f4532017-08-04 02:07:31 +0530372 // it is expected here that we should get the valid data
373 // but we may also get the default values.
374 // Validation of the data is done by settings.
375 //
376 // if mac address is default mac address then
377 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530378 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530379 {
380 memset(respptr->data, 0, SIZE_BOOT_OPTION);
381 rc = -1;
382 return rc;
383 }
384 // if addr is static then ipaddress,gateway,prefix
385 // should not be default one,don't send blank override.
386 if (isStatic)
387 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700388 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
389 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530390 {
391 memset(respptr->data, 0, SIZE_BOOT_OPTION);
392 rc = -1;
393 return rc;
394 }
395 }
396
Patrick Venture0b02be92018-08-31 11:55:55 -0700397 sscanf(
398 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
399 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
400 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
401 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530402
Ratan Guptadcb10672017-07-10 10:33:50 +0530403 respptr->data[MAC_OFFSET + 6] = 0x00;
404
Patrick Venture0b02be92018-08-31 11:55:55 -0700405 memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic, sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530406
407 uint8_t addressFamily = (properties["Type"].get<std::string>() ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700408 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
409 ? AF_INET
410 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530411
Patrick Venture0b02be92018-08-31 11:55:55 -0700412 addrSize = (addressFamily == AF_INET)
413 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
414 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530415
416 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530417 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700418 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530419
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530420 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
421
422 memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
423
424 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
425
Ratan Guptad70f4532017-08-04 02:07:31 +0530426 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700427 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530428 }
429 catch (InternalFailure& e)
430 {
431 commit<InternalFailure>();
432 memset(respptr->data, 0, SIZE_BOOT_OPTION);
433 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500434 return rc;
435 }
436
Patrick Venture0b02be92018-08-31 11:55:55 -0700437 // PetiBoot-Specific
438 // If success then copy the first 9 bytes to the data
Ratan Guptadcb10672017-07-10 10:33:50 +0530439 memcpy(respptr->data, net_conf_initial_bytes,
440 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500441
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530442 memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
443
Ratan Guptafd28dd72016-08-01 04:58:01 -0500444#ifdef _IPMI_DEBUG_
Ratan Guptadcb10672017-07-10 10:33:50 +0530445 printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500446
Ratan Guptadcb10672017-07-10 10:33:50 +0530447 for (uint8_t pos = 0; pos < index; pos++)
448 {
449 printf("%02x ", respptr->data[pos]);
450 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500451#endif
452
Ratan Guptafd28dd72016-08-01 04:58:01 -0500453 return rc;
454}
455
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530456/** @brief convert IPv4 and IPv6 addresses from binary to text form.
457 * @param[in] family - IPv4/Ipv6
458 * @param[in] data - req data pointer.
459 * @param[in] offset - offset in the data.
460 * @param[in] addrSize - size of the data which needs to be read from offset.
461 * @returns address in text form.
462 */
463
Patrick Venture0b02be92018-08-31 11:55:55 -0700464std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
465 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530466{
467 char ipAddr[INET6_ADDRSTRLEN] = {};
468
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530470 {
471 case AF_INET:
472 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700473 struct sockaddr_in addr4
474 {
475 };
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530476 memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
477
Patrick Venture0b02be92018-08-31 11:55:55 -0700478 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530479
480 break;
481 }
482 case AF_INET6:
483 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700484 struct sockaddr_in6 addr6
485 {
486 };
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530487 memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
488
Patrick Venture0b02be92018-08-31 11:55:55 -0700489 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530490
491 break;
492 }
493 default:
494 {
495 return {};
496 }
497 }
498
499 return ipAddr;
500}
501
Ratan Guptadcb10672017-07-10 10:33:50 +0530502int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500503{
Ratan Guptadcb10672017-07-10 10:33:50 +0530504 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500505 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700506 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530507 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700508 char addrOrigin{0};
509 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530510 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530511 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700512 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
513 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530514 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530515 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500516
Patrick Venture0b02be92018-08-31 11:55:55 -0700517 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500518 // version starts from sixth byte
519
Ratan Guptadcb10672017-07-10 10:33:50 +0530520 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500521 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530522 do
523 {
524 // cookie == 0x21 0x70 0x62 0x21
525 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700526 (net_conf_initial_bytes + COOKIE_OFFSET),
527 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530528 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700529 // cookie == 0
530 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
531 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530532 {
533 // need to zero out the network settings.
534 break;
535 }
536
537 log<level::ERR>("Invalid Cookie");
538 elog<InternalFailure>();
539 }
540
541 // vesion == 0x00 0x01
542 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700543 (net_conf_initial_bytes + VERSION_OFFSET),
544 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530545 {
546
547 log<level::ERR>("Invalid Version");
548 elog<InternalFailure>();
549 }
550
Ratan Gupta8c31d232017-08-13 05:49:43 +0530551 snprintf(mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
Patrick Venture0b02be92018-08-31 11:55:55 -0700552 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
553 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
Ratan Guptadcb10672017-07-10 10:33:50 +0530554 reqptr->data[MAC_OFFSET + 4],
555 reqptr->data[MAC_OFFSET + 5]);
556
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530557 memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
558 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530559
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530560 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530561 {
562 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530563 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530564 }
565
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530566 // Get the address size
Patrick Venture0b02be92018-08-31 11:55:55 -0700567 memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
568 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530569
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530570 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530571
Ratan Guptad70f4532017-08-04 02:07:31 +0530572 memcpy(&prefix, &(reqptr->data[prefixOffset]),
573 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530574
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530575 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
576
Ratan Gupta8c31d232017-08-13 05:49:43 +0530577 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530578 {
579 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
580 family = AF_INET6;
581 }
582
Patrick Venture0b02be92018-08-31 11:55:55 -0700583 ipAddress =
584 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530585
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530586 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
587
Patrick Venture0b02be92018-08-31 11:55:55 -0700588 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530589
Patrick Venture0b02be92018-08-31 11:55:55 -0700590 // Cookie == 0 or it is a valid cookie
591 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
592 std::to_string(prefix) + ",gateway="s + gateway +
593 ",mac="s + mac + ",addressOrigin="s +
594 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500595
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530596 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
597
598 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
599 SETTINGS_ROOT, SETTINGS_MATCH);
600 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
601 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530602 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530603 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700604 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530605 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700606 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530607 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700608 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530609 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700610 IP_INTERFACE, "Gateway", std::string(gateway));
611 ipmi::setDbusProperty(
612 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
613 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530614 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700615 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500616
Patrick Venture0b02be92018-08-31 11:55:55 -0700617 log<level::DEBUG>(
618 "Network configuration changed",
619 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500620 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530621 catch (InternalFailure& e)
622 {
623 commit<InternalFailure>();
624 return -1;
625 }
626
627 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500628}
629
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500630uint32_t getPOHCounter()
631{
632 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
633
Patrick Venture0b02be92018-08-31 11:55:55 -0700634 auto chassisStateObj =
635 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500636
Patrick Venture0b02be92018-08-31 11:55:55 -0700637 auto service =
638 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500639
Patrick Venture0b02be92018-08-31 11:55:55 -0700640 auto propValue =
641 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
642 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500643
644 return propValue.get<uint32_t>();
645}
646
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500647ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
648 ipmi_request_t request,
649 ipmi_response_t response,
650 ipmi_data_len_t data_len,
651 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500652{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500653 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800654 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500655 *data_len = 0;
656 return rc;
657}
658
Nan Li8d15fb42016-08-16 22:29:40 +0800659ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700660 ipmi_request_t request,
661 ipmi_response_t response,
662 ipmi_data_len_t data_len,
663 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800664{
665 // sd_bus error
666 ipmi_ret_t rc = IPMI_CC_OK;
667
668 ipmi_chassis_cap_t chassis_cap{};
669
670 *data_len = sizeof(ipmi_chassis_cap_t);
671
672 // TODO: need future work. Get those flag from MRW.
673
674 // capabilities flags
675 // [7..4] - reserved
676 // [3] – 1b = provides power interlock (IPM 1.5)
677 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
Patrick Venture0b02be92018-08-31 11:55:55 -0700678 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis has
679 // capabilities
680 // to lock out external power control and reset button or front
681 // panel interfaces and/or detect tampering with those
682 // interfaces).
Nan Li8d15fb42016-08-16 22:29:40 +0800683 // [0] -1b = Chassis provides intrusion (physical security) sensor.
684 // set to default value 0x0.
685 chassis_cap.cap_flags = 0x0;
686
687 // Since we do not have a separate SDR Device/SEL Device/ FRU repository.
688 // The 20h was given as those 5 device addresses.
689 // Chassis FRU info Device Address
690 chassis_cap.fru_info_dev_addr = 0x20;
691
692 // Chassis SDR Device Address
693 chassis_cap.sdr_dev_addr = 0x20;
694
695 // Chassis SEL Device Address
696 chassis_cap.sel_dev_addr = 0x20;
697
698 // Chassis System Management Device Address
699 chassis_cap.system_management_dev_addr = 0x20;
700
701 // Chassis Bridge Device Address.
702 chassis_cap.bridge_dev_addr = 0x20;
703
704 memcpy(response, &chassis_cap, *data_len);
705
706 return rc;
707}
708
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530709//------------------------------------------
710// Calls into Host State Manager Dbus object
711//------------------------------------------
712int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600713{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500714 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700715 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500716 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700717 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
718 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530719
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500720 // sd_bus error
721 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700722 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600723
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500724 // SD Bus error report mechanism.
725 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600726
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500727 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700728 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500729 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
730 if (rc < 0)
731 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700732 log<level::ERR>(
733 "Failed to get bus name",
734 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500735 return rc;
736 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530737
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500738 // Convert to string equivalent of the passed in transition enum.
739 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530740
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500741 rc = sd_bus_call_method(bus_type, // On the system bus
742 busname, // Service to contact
743 HOST_STATE_MANAGER_ROOT, // Object path
744 DBUS_PROPERTY_IFACE, // Interface name
745 "Set", // Method to be called
746 &bus_error, // object to return error
747 nullptr, // Response buffer if any
748 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700749 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
750 request.c_str());
751 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500752 {
753 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530754 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500755 }
756 else
757 {
758 log<level::INFO>("Transition request initiated successfully");
759 }
vishwa36993272015-11-20 12:43:49 -0600760
761 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500762 free(busname);
vishwa36993272015-11-20 12:43:49 -0600763
Sergey Solomineb9b8142016-08-23 09:07:28 -0500764 return rc;
vishwa36993272015-11-20 12:43:49 -0600765}
766
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500767namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500768{
Nan Lifdd8ec52016-08-28 03:57:40 +0800769
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500770using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
771using IpmiValue = uint8_t;
772using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800773
Patrick Venture0b02be92018-08-31 11:55:55 -0700774std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500775 {RestorePolicy::Policy::AlwaysOff, 0x00},
776 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700777 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800778
Yong Lic6713cf2018-09-12 12:35:13 +0800779static constexpr uint8_t noChange = 0x03;
780static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
781static constexpr uint8_t policyBitMask = 0x07;
782static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500783} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800784
785//----------------------------------------------------------------------
786// Get Chassis Status commands
787//----------------------------------------------------------------------
788ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500789 ipmi_request_t request,
790 ipmi_response_t response,
791 ipmi_data_len_t data_len,
792 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800793{
Patrick Venture0b02be92018-08-31 11:55:55 -0700794 const char* objname = "/org/openbmc/control/power0";
795 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800796
Patrick Venture0b02be92018-08-31 11:55:55 -0700797 sd_bus* bus = NULL;
798 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800799 int r = 0;
800 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700801 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800802 ipmi_ret_t rc = IPMI_CC_OK;
803 ipmi_get_chassis_status_t chassis_status{};
804
Nan Lifdd8ec52016-08-28 03:57:40 +0800805 uint8_t s = 0;
806
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500807 using namespace chassis::internal;
808 using namespace chassis::internal::cache;
809 using namespace power_policy;
810
Deepak Kodihallie6027092017-08-27 08:13:37 -0500811 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700812 auto method = dbus.new_method_call(
813 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
814 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500815 method.append(powerRestoreIntf, "PowerRestorePolicy");
816 auto resp = dbus.call(method);
817 if (resp.is_method_error())
818 {
819 log<level::ERR>("Error in PowerRestorePolicy Get");
820 report<InternalFailure>();
821 *data_len = 0;
822 return IPMI_CC_UNSPECIFIED_ERROR;
823 }
824 sdbusplus::message::variant<std::string> result;
825 resp.read(result);
826 auto powerRestore =
827 RestorePolicy::convertPolicyFromString(result.get<std::string>());
Nan Lifdd8ec52016-08-28 03:57:40 +0800828
829 *data_len = 4;
830
Tom Joseph63a00512017-08-09 23:39:59 +0530831 bus = ipmid_get_sd_bus_connection();
832
Nan Lifdd8ec52016-08-28 03:57:40 +0800833 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700834 if (r < 0)
835 {
836 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800837 rc = IPMI_CC_UNSPECIFIED_ERROR;
838 goto finish;
839 }
840
Patrick Venture0b02be92018-08-31 11:55:55 -0700841 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
842 "i");
843 if (r < 0)
844 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530845 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700846 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
847 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530848 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800849 rc = IPMI_CC_UNSPECIFIED_ERROR;
850 goto finish;
851 }
852
853 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700854 if (r < 0)
855 {
856 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800857 rc = IPMI_CC_UNSPECIFIED_ERROR;
858 goto finish;
859 }
860
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500861 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800862
863 // Current Power State
864 // [7] reserved
865 // [6..5] power restore policy
866 // 00b = chassis stays powered off after AC/mains returns
867 // 01b = after AC returns, power is restored to the state that was
868 // in effect when AC/mains was lost.
869 // 10b = chassis always powers up after AC/mains returns
870 // 11b = unknow
871 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700872 // Do we need to define a dbus property to identify the restore
873 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800874
875 // [4] power control fault
876 // 1b = controller attempted to turn system power on or off, but
877 // system did not enter desired state.
878 // Set to 0b, since We don't support it..
879
880 // [3] power fault
881 // 1b = fault detected in main power subsystem.
882 // set to 0b. for we don't support it.
883
884 // [2] 1b = interlock (chassis is presently shut down because a chassis
885 // panel interlock switch is active). (IPMI 1.5)
886 // set to 0b, for we don't support it.
887
888 // [1] power overload
889 // 1b = system shutdown because of power overload condition.
890 // set to 0b, for we don't support it.
891
892 // [0] power is on
893 // 1b = system power is on
894 // 0b = system power is off(soft-off S4/S5, or mechanical off)
895
Patrick Venture0b02be92018-08-31 11:55:55 -0700896 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800897
898 // Last Power Event
899 // [7..5] – reserved
900 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
901 // [3] – 1b = last power down caused by power fault
902 // [2] – 1b = last power down caused by a power interlock being activated
903 // [1] – 1b = last power down caused by a Power overload
904 // [0] – 1b = AC failed
905 // set to 0x0, for we don't support these fields.
906
907 chassis_status.last_power_event = 0;
908
909 // Misc. Chassis State
910 // [7] – reserved
911 // [6] – 1b = Chassis Identify command and state info supported (Optional)
912 // 0b = Chassis Identify command support unspecified via this command.
913 // (The Get Command Support command , if implemented, would still
914 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700915 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
916 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800917 // as 00b) otherwise. Returns the present chassis identify state.
918 // Refer to the Chassis Identify command for more info.
919 // 00b = chassis identify state = Off
920 // 01b = chassis identify state = Temporary(timed) On
921 // 10b = chassis identify state = Indefinite On
922 // 11b = reserved
923 // [3] – 1b = Cooling/fan fault detected
924 // [2] – 1b = Drive Fault
925 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
926 // push-buttons disabled.)
927 // [0] – 1b = Chassis Intrusion active
928 // set to 0, for we don't support them.
929 chassis_status.misc_power_state = 0;
930
931 // Front Panel Button Capabilities and disable/enable status(Optional)
932 // set to 0, for we don't support them.
933 chassis_status.front_panel_button_cap_status = 0;
934
935 // Pack the actual response
936 memcpy(response, &chassis_status, *data_len);
937
938finish:
939 free(busname);
940 reply = sd_bus_message_unref(reply);
941
942 return rc;
943}
Chris Austen7888c4d2015-12-03 15:26:20 -0600944
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530945//-------------------------------------------------------------
946// Send a command to SoftPowerOff application to stop any timer
947//-------------------------------------------------------------
948int stop_soft_off_timer()
949{
Patrick Venture0b02be92018-08-31 11:55:55 -0700950 constexpr auto iface = "org.freedesktop.DBus.Properties";
951 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
952 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530953
Patrick Venture0b02be92018-08-31 11:55:55 -0700954 constexpr auto property = "ResponseReceived";
955 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
956 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530957
958 // Get the system bus where most system services are provided.
959 auto bus = ipmid_get_sd_bus_connection();
960
961 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500962 // TODO openbmc/openbmc#1661 - Mapper refactor
963 //
964 // See openbmc/openbmc#1743 for some details but high level summary is that
965 // for now the code will directly call the soft off interface due to a
966 // race condition with mapper usage
967 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700968 // char *busname = nullptr;
969 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
970 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500971 //{
972 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530973 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500974 // return r;
975 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530976
977 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500978 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700979 "Set", nullptr, nullptr, "ssv", soft_off_iface,
980 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530981 if (rc < 0)
982 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530983 log<level::ERR>("Failed to set property in SoftPowerOff object",
984 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530985 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500986
Patrick Venture0b02be92018-08-31 11:55:55 -0700987 // TODO openbmc/openbmc#1661 - Mapper refactor
988 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530989 return rc;
990}
991
vishwa36993272015-11-20 12:43:49 -0600992//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500993// Create file to indicate there is no need for softoff notification to host
994//----------------------------------------------------------------------
995void indicate_no_softoff_needed()
996{
997 fs::path path{HOST_INBAND_REQUEST_DIR};
998 if (!fs::is_directory(path))
999 {
1000 fs::create_directory(path);
1001 }
1002
1003 // Add the host instance (default 0 for now) to the file name
1004 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001005 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001006 size++; // null
1007 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001008 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001009
1010 // Append file name to directory and create it
1011 path /= buf.get();
1012 std::ofstream(path.c_str());
1013}
1014
1015//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001016// Chassis Control commands
1017//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001018ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1019 ipmi_request_t request,
1020 ipmi_response_t response,
1021 ipmi_data_len_t data_len,
1022 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001023{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001024 // Error from power off.
1025 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001026
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001027 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001028 *data_len = 0;
1029
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001030 // Catch the actual operaton by peeking into request buffer
Patrick Venture0b02be92018-08-31 11:55:55 -07001031 uint8_t chassis_ctrl_cmd = *(uint8_t*)request;
vishwa36993272015-11-20 12:43:49 -06001032
Patrick Venture0b02be92018-08-31 11:55:55 -07001033 switch (chassis_ctrl_cmd)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001034 {
1035 case CMD_POWER_ON:
1036 rc = initiate_state_transition(State::Host::Transition::On);
1037 break;
1038 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301039 // This path would be hit in 2 conditions.
1040 // 1: When user asks for power off using ipmi chassis command 0x04
1041 // 2: Host asking for power off post shutting down.
1042
1043 // If it's a host requested power off, then need to nudge Softoff
1044 // application that it needs to stop the watchdog timer if running.
1045 // If it is a user requested power off, then this is not really
1046 // needed. But then we need to differentiate between user and host
1047 // calling this same command
1048
1049 // For now, we are going ahead with trying to nudge the soft off and
1050 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001051 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301052
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001053 // Only request the Off transition if the soft power off
1054 // application is not running
1055 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001056 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001057 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301058 // that it should not run. Not doing this will result in State
1059 // manager doing a default soft power off when asked for power
1060 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001061 indicate_no_softoff_needed();
1062
1063 // Now request the shutdown
1064 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001065 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001066 else
1067 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301068 log<level::INFO>("Soft off is running, so let shutdown target "
1069 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001070 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001071 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301072
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001073 case CMD_HARD_RESET:
1074 case CMD_POWER_CYCLE:
1075 // SPEC has a section that says certain implementations can trigger
1076 // PowerOn if power is Off when a command to power cycle is
1077 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001078
1079 // First create a file to indicate to the soft off application
1080 // that it should not run since this is a direct user initiated
1081 // power reboot request (i.e. a reboot request that is not
1082 // originating via a soft power off SMS request)
1083 indicate_no_softoff_needed();
1084
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001085 rc = initiate_state_transition(State::Host::Transition::Reboot);
1086 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301087
1088 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1089 // Request Host State Manager to do a soft power off
1090 rc = initiate_state_transition(State::Host::Transition::Off);
1091 break;
1092
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001093 default:
1094 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301095 log<level::ERR>("Invalid Chassis Control command",
1096 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001097 rc = -1;
1098 }
1099 }
vishwa36993272015-11-20 12:43:49 -06001100
Patrick Venture0b02be92018-08-31 11:55:55 -07001101 return ((rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001102}
1103
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001104/** @brief Return D-Bus connection string to enclosure identify LED object
1105 *
1106 * @param[in, out] connection - connection to D-Bus object
1107 * @return a IPMI return code
1108 */
1109std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301110{
Tom Joseph5110c122018-03-23 17:55:40 +05301111 // lookup enclosure_identify group owner(s) in mapper
1112 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001113 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1114 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301115
1116 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001117 static const std::vector<std::string> interfaces = {
1118 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301119 mapperCall.append(interfaces);
1120 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1121 if (mapperReply.is_method_error())
1122 {
1123 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001124 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301125 }
1126 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1127 mapperReply.read(mapperResp);
1128
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001129 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301130 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001131 log<level::ERR>(
1132 "Invalid number of enclosure identify objects.",
1133 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001134 elog<InternalFailure>();
1135 }
1136 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1137 return pair.first;
1138}
Tom Joseph5110c122018-03-23 17:55:40 +05301139
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001140/** @brief Turn On/Off enclosure identify LED
1141 *
1142 * @param[in] flag - true to turn on LED, false to turn off
1143 * @return a IPMI return code
1144 */
1145void enclosureIdentifyLed(bool flag)
1146{
1147 using namespace chassis::internal;
1148 std::string connection = std::move(getEnclosureIdentifyConnection());
Patrick Venture0b02be92018-08-31 11:55:55 -07001149 auto led =
1150 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1151 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001152 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001153 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001154 auto ledReply = dbus.call(led);
1155 if (ledReply.is_method_error())
1156 {
1157 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001158 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001159 elog<InternalFailure>();
1160 }
1161}
1162
1163/** @brief Callback method to turn off LED
1164 */
1165void enclosureIdentifyLedOff()
1166{
1167 try
1168 {
1169 enclosureIdentifyLed(false);
1170 }
1171 catch (const InternalFailure& e)
1172 {
1173 report<InternalFailure>();
1174 }
1175}
1176
1177/** @brief Create timer to turn on and off the enclosure LED
1178 */
1179void createIdentifyTimer()
1180{
1181 if (!identifyTimer)
1182 {
1183 identifyTimer = std::make_unique<phosphor::ipmi::Timer>(
1184 ipmid_get_sd_event_connection(), enclosureIdentifyLedOff);
1185 }
1186}
1187
1188ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1189 ipmi_request_t request,
1190 ipmi_response_t response,
1191 ipmi_data_len_t data_len,
1192 ipmi_context_t context)
1193{
1194 if (*data_len > chassisIdentifyReqLength)
1195 {
1196 return IPMI_CC_REQ_DATA_LEN_INVALID;
1197 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001198 uint8_t identifyInterval =
1199 *data_len > identifyIntervalPos
1200 ? (static_cast<uint8_t*>(request))[identifyIntervalPos]
1201 : DEFAULT_IDENTIFY_TIME_OUT;
1202 bool forceIdentify =
1203 (*data_len == chassisIdentifyReqLength)
1204 ? (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01
1205 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301206
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001207 if (identifyInterval || forceIdentify)
1208 {
1209 // stop the timer if already started, for force identify we should
1210 // not turn off LED
1211 identifyTimer->setTimer(SD_EVENT_OFF);
1212 try
Tom Joseph5110c122018-03-23 17:55:40 +05301213 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001214 enclosureIdentifyLed(true);
1215 }
1216 catch (const InternalFailure& e)
1217 {
1218 report<InternalFailure>();
1219 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301220 }
1221
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001222 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301223 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001224 return IPMI_CC_OK;
1225 }
1226 // start the timer
1227 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001228 std::chrono::seconds(identifyInterval));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001229 identifyTimer->startTimer(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301230 }
Tom Josephbed26992018-07-31 23:00:24 +05301231 else if (!identifyInterval)
1232 {
1233 identifyTimer->setTimer(SD_EVENT_OFF);
1234 enclosureIdentifyLedOff();
1235 }
Tom Joseph5110c122018-03-23 17:55:40 +05301236 return IPMI_CC_OK;
1237}
1238
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001239namespace boot_options
1240{
1241
1242using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1243using IpmiValue = uint8_t;
1244constexpr auto ipmiDefault = 0;
1245
Patrick Venture0b02be92018-08-31 11:55:55 -07001246std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001247 {0x01, Source::Sources::Network},
1248 {0x02, Source::Sources::Disk},
1249 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001250 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001251
Patrick Venture0b02be92018-08-31 11:55:55 -07001252std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001253 {0x03, Mode::Modes::Safe},
1254 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001255 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001256
Patrick Venture0b02be92018-08-31 11:55:55 -07001257std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001258 {Source::Sources::Network, 0x01},
1259 {Source::Sources::Disk, 0x02},
1260 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001261 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001262
Patrick Venture0b02be92018-08-31 11:55:55 -07001263std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001264 {Mode::Modes::Safe, 0x03},
1265 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001266 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001267
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001268} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001269
Marri Devender Rao81719702018-05-07 00:53:48 -05001270/** @brief Set the property value for boot source
1271 * @param[in] source - boot source value
1272 * @return On failure return IPMI error.
1273 */
1274static ipmi_ret_t setBootSource(const Source::Sources& source)
1275{
1276 using namespace chassis::internal;
1277 using namespace chassis::internal::cache;
1278 sdbusplus::message::variant<std::string> property =
1279 convertForMessage(source);
1280 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1281 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001282 auto method = dbus.new_method_call(
1283 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1284 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001285 method.append(bootSourceIntf, "BootSource", property);
1286 auto reply = dbus.call(method);
1287 if (reply.is_method_error())
1288 {
1289 log<level::ERR>("Error in BootSource Set");
1290 report<InternalFailure>();
1291 return IPMI_CC_UNSPECIFIED_ERROR;
1292 }
1293 return IPMI_CC_OK;
1294}
1295
Patrick Venture0b02be92018-08-31 11:55:55 -07001296/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001297 * @param[in] mode - boot mode value
1298 * @return On failure return IPMI error.
1299 */
1300static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1301{
1302 using namespace chassis::internal;
1303 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001304 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001305 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1306 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001307 auto method = dbus.new_method_call(
1308 objects.service(bootModeSetting, bootModeIntf).c_str(),
1309 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001310 method.append(bootModeIntf, "BootMode", property);
1311 auto reply = dbus.call(method);
1312 if (reply.is_method_error())
1313 {
1314 log<level::ERR>("Error in BootMode Set");
1315 report<InternalFailure>();
1316 return IPMI_CC_UNSPECIFIED_ERROR;
1317 }
1318 return IPMI_CC_OK;
1319}
1320
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001321ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1322 ipmi_request_t request,
1323 ipmi_response_t response,
1324 ipmi_data_len_t data_len,
1325 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001326{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001327 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001328 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001329 char* p = NULL;
1330 get_sys_boot_options_response_t* resp =
1331 (get_sys_boot_options_response_t*)response;
1332 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001333 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001334
Patrick Venture0b02be92018-08-31 11:55:55 -07001335 memset(resp, 0, sizeof(*resp));
1336 resp->version = SET_PARM_VERSION;
1337 resp->parm = 5;
1338 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001339
shgoupfd84fbbf2015-12-17 10:05:51 +08001340 /*
1341 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1342 * This is the only parameter used by petitboot.
1343 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001344 if (reqptr->parameter ==
1345 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1346 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001347
Ratan Guptafd28dd72016-08-01 04:58:01 -05001348 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001349 using namespace chassis::internal;
1350 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001351
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001352 try
ratagupta6f6bff2016-04-04 06:20:11 -05001353 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001354 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1355 const auto& bootSourceSetting =
1356 std::get<settings::Path>(bootSetting);
1357 auto oneTimeEnabled =
1358 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001359 auto method = dbus.new_method_call(
1360 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1361 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001362 method.append(bootSourceIntf, "BootSource");
1363 auto reply = dbus.call(method);
1364 if (reply.is_method_error())
1365 {
1366 log<level::ERR>("Error in BootSource Get");
1367 report<InternalFailure>();
1368 *data_len = 0;
1369 return IPMI_CC_UNSPECIFIED_ERROR;
1370 }
1371 sdbusplus::message::variant<std::string> result;
1372 reply.read(result);
1373 auto bootSource =
1374 Source::convertSourcesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001375
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001376 bootSetting = settings::boot::setting(objects, bootModeIntf);
1377 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1378 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001379 objects.service(bootModeSetting, bootModeIntf).c_str(),
1380 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001381 method.append(bootModeIntf, "BootMode");
1382 reply = dbus.call(method);
1383 if (reply.is_method_error())
1384 {
1385 log<level::ERR>("Error in BootMode Get");
1386 report<InternalFailure>();
1387 *data_len = 0;
1388 return IPMI_CC_UNSPECIFIED_ERROR;
1389 }
1390 reply.read(result);
1391 auto bootMode =
1392 Mode::convertModesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001393
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001394 bootOption = sourceDbusToIpmi.at(bootSource);
1395 if ((Mode::Modes::Regular == bootMode) &&
1396 (Source::Sources::Default == bootSource))
1397 {
1398 bootOption = ipmiDefault;
1399 }
1400 else if (Source::Sources::Default == bootSource)
1401 {
1402 bootOption = modeDbusToIpmi.at(bootMode);
1403 }
1404 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001405
Patrick Venture0b02be92018-08-31 11:55:55 -07001406 resp->data[0] = oneTimeEnabled
1407 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1408 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001409
ratagupta6f6bff2016-04-04 06:20:11 -05001410 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001411 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001412 catch (InternalFailure& e)
1413 {
1414 report<InternalFailure>();
1415 *data_len = 0;
1416 return IPMI_CC_UNSPECIFIED_ERROR;
1417 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001418 }
1419 else if (reqptr->parameter ==
1420 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1421 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001422
Patrick Venture0b02be92018-08-31 11:55:55 -07001423 *data_len =
1424 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001425
Patrick Venture0b02be92018-08-31 11:55:55 -07001426 resp->parm =
1427 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001428
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001429 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001430
Patrick Venture0b02be92018-08-31 11:55:55 -07001431 if (ret < 0)
1432 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001433
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301434 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001435 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001436 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001437 }
1438 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001439 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001440 }
1441
Patrick Venture0b02be92018-08-31 11:55:55 -07001442 else
1443 {
1444 log<level::ERR>("Unsupported parameter",
1445 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001446 }
1447
1448 if (p)
1449 free(p);
1450
Ratan Guptafd28dd72016-08-01 04:58:01 -05001451 if (rc == IPMI_CC_OK)
1452 {
1453 *data_len += 2;
1454 }
1455
shgoupfd84fbbf2015-12-17 10:05:51 +08001456 return rc;
1457}
1458
shgoupfd84fbbf2015-12-17 10:05:51 +08001459ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001460 ipmi_request_t request,
1461 ipmi_response_t response,
1462 ipmi_data_len_t data_len,
1463 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001464{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001465 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001466 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001467 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001468
Patrick Venture0b02be92018-08-31 11:55:55 -07001469 printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1470 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001471
shgoupfd84fbbf2015-12-17 10:05:51 +08001472 // This IPMI command does not have any resposne data
1473 *data_len = 0;
1474
1475 /* 000101
1476 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1477 * This is the only parameter used by petitboot.
1478 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001479
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001480 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1481 {
1482 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1483 using namespace chassis::internal;
1484 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001485 auto oneTimeEnabled = false;
1486 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301487 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001488 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001489
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001490 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001491 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001492 bool permanent =
1493 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1494 SET_PARM_BOOT_FLAGS_PERMANENT;
1495
Patrick Venture0b02be92018-08-31 11:55:55 -07001496 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301497
1498 oneTimeEnabled =
1499 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1500
1501 /*
1502 * Check if the current boot setting is onetime or permanent, if the
1503 * request in the command is otherwise, then set the "Enabled"
1504 * property in one_time object path to 'True' to indicate onetime
1505 * and 'False' to indicate permanent.
1506 *
1507 * Once the onetime/permanent setting is applied, then the bootMode
1508 * and bootSource is updated for the corresponding object.
1509 */
1510 if ((permanent && oneTimeEnabled) ||
1511 (!permanent && !oneTimeEnabled))
1512 {
1513 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1514
Patrick Venture0b02be92018-08-31 11:55:55 -07001515 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1516 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301517 }
1518
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001519 auto modeItr = modeIpmiToDbus.find(bootOption);
1520 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1521 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001522 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001523 rc = setBootSource(sourceItr->second);
1524 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001525 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001526 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001527 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001528 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001529 // If a set boot device is mapping to a boot source, then reset
1530 // the boot mode D-Bus property to default.
1531 // This way the ipmid code can determine which property is not
1532 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001533 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001534 {
1535 setBootMode(Mode::Modes::Regular);
1536 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001537 }
1538 if (modeIpmiToDbus.end() != modeItr)
1539 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001540 rc = setBootMode(modeItr->second);
1541 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001542 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001543 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001544 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001545 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001546 // If a set boot device is mapping to a boot mode, then reset
1547 // the boot source D-Bus property to default.
1548 // This way the ipmid code can determine which property is not
1549 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001550 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001551 {
1552 setBootSource(Source::Sources::Default);
1553 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001554 }
1555 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001556 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001557 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001558 report<InternalFailure>();
1559 *data_len = 0;
1560 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001561 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001562 }
1563 else if (reqptr->parameter ==
1564 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1565 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001566
1567 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001568 if (ret < 0)
1569 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301570 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001571 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001572 rc = IPMI_CC_UNSPECIFIED_ERROR;
1573 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001574 }
1575 else if (reqptr->parameter ==
1576 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1577 {
Tom Josephf536c902017-09-25 18:08:15 +05301578 // Handle parameter #4 and return command completed normally
1579 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1580 // parameter. This is added to support the ipmitool command `chassis
1581 // bootdev` which sends set on parameter #4, before setting the boot
1582 // flags.
1583 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001584 }
1585 else
1586 {
1587 log<level::ERR>("Unsupported parameter",
1588 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001589 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001590 }
1591
1592 return rc;
1593}
1594
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001595ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1596 ipmi_request_t request, ipmi_response_t response,
1597 ipmi_data_len_t data_len, ipmi_context_t context)
1598{
1599 // sd_bus error
1600 ipmi_ret_t rc = IPMI_CC_OK;
1601
1602 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1603
1604 try
1605 {
1606 auto pohCounter = getPOHCounter();
1607 resptr->counterReading[0] = pohCounter;
1608 resptr->counterReading[1] = pohCounter >> 8;
1609 resptr->counterReading[2] = pohCounter >> 16;
1610 resptr->counterReading[3] = pohCounter >> 24;
1611 }
1612 catch (std::exception& e)
1613 {
1614 log<level::ERR>(e.what());
1615 return IPMI_CC_UNSPECIFIED_ERROR;
1616 }
1617
1618 resptr->minPerCount = poh::minutesPerCount;
1619 *data_len = sizeof(GetPOHCountResponse);
1620
1621 return rc;
1622}
1623
Yong Lic6713cf2018-09-12 12:35:13 +08001624ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1625 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1626 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1627{
1628 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1629 auto* resptr = reinterpret_cast<uint8_t*>(response);
1630 uint8_t reqPolicy = 0;
1631
1632 power_policy::DbusValue value =
1633 power_policy::RestorePolicy::Policy::AlwaysOff;
1634
1635 if (*data_len != power_policy::setPolicyReqLen)
1636 {
1637 phosphor::logging::log<level::ERR>("Unsupported request length",
1638 entry("LEN=0x%x", *data_len));
1639 *data_len = 0;
1640 return IPMI_CC_REQ_DATA_LEN_INVALID;
1641 }
1642
1643 reqPolicy = *reqptr & power_policy::policyBitMask;
1644 if (reqPolicy > power_policy::noChange)
1645 {
1646 phosphor::logging::log<level::ERR>("Reserved request parameter",
1647 entry("REQ=0x%x", reqPolicy));
1648 *data_len = 0;
1649 return IPMI_CC_PARM_NOT_SUPPORTED;
1650 }
1651
1652 if (reqPolicy == power_policy::noChange)
1653 {
1654 // just return the supported policy
1655 *resptr = power_policy::allSupport;
1656 *data_len = power_policy::setPolicyReqLen;
1657 return IPMI_CC_OK;
1658 }
1659
1660 for (auto const& it : power_policy::dbusToIpmi)
1661 {
1662 if (it.second == reqPolicy)
1663 {
1664 value = it.first;
1665 break;
1666 }
1667 }
1668
1669 try
1670 {
1671 const settings::Path& powerRestoreSetting =
1672 chassis::internal::cache::objects.map
1673 .at(chassis::internal::powerRestoreIntf)
1674 .front();
1675 sdbusplus::message::variant<std::string> property =
1676 convertForMessage(value);
1677
1678 auto method = chassis::internal::dbus.new_method_call(
1679 chassis::internal::cache::objects
1680 .service(powerRestoreSetting,
1681 chassis::internal::powerRestoreIntf)
1682 .c_str(),
1683 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1684
1685 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1686 property);
1687 auto reply = chassis::internal::dbus.call(method);
1688 if (reply.is_method_error())
1689 {
1690 phosphor::logging::log<level::ERR>("Unspecified Error");
1691 *data_len = 0;
1692 return IPMI_CC_UNSPECIFIED_ERROR;
1693 }
1694 }
1695 catch (InternalFailure& e)
1696 {
1697 report<InternalFailure>();
1698 *data_len = 0;
1699 return IPMI_CC_UNSPECIFIED_ERROR;
1700 }
1701
1702 *data_len = power_policy::setPolicyReqLen;
1703 return IPMI_CC_OK;
1704}
1705
Adriana Kobylak40814c62015-10-27 15:58:44 -05001706void register_netfn_chassis_functions()
1707{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001708 createIdentifyTimer();
1709
Tom05732372016-09-06 17:21:23 +05301710 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001711 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1712 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001713
Tom05732372016-09-06 17:21:23 +05301714 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001715 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1716 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001717
Tom05732372016-09-06 17:21:23 +05301718 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301719 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001720 ipmi_chassis_get_sys_boot_options,
1721 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001722
Tom05732372016-09-06 17:21:23 +05301723 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001724 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1725 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001726
Tom05732372016-09-06 17:21:23 +05301727 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001728 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1729 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001730
Tom Joseph5110c122018-03-23 17:55:40 +05301731 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301732 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1733 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1734
Tom05732372016-09-06 17:21:23 +05301735 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301736 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001737 ipmi_chassis_set_sys_boot_options,
1738 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001739 // <Get POH Counter>
1740 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1741 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001742
1743 // <Set Power Restore Policy>
1744 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1745 ipmi_chassis_set_power_restore_policy,
1746 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001747}