blob: 80f4402e7f9c4822989a25ee103d984d34b0ecf0 [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>
Patrick Venture0b02be92018-08-31 11:55:55 -070016
Ratan Guptafd28dd72016-08-01 04:58:01 -050017#include <array>
Patrick Venture0b02be92018-08-31 11:55:55 -070018#include <chrono>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050019#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053020#include <future>
Patrick Venture0b02be92018-08-31 11:55:55 -070021#include <sstream>
Vernon Mauery185b9f82018-07-20 10:52:36 -070022#if __has_include(<filesystem>)
23#include <filesystem>
24#elif __has_include(<experimental/filesystem>)
Andrew Geisslera6e3a302017-05-31 19:34:00 -050025#include <experimental/filesystem>
Patrick Venture0b02be92018-08-31 11:55:55 -070026namespace std
27{
28// splice experimental::filesystem into std
29namespace filesystem = std::experimental::filesystem;
30} // namespace std
Vernon Mauery185b9f82018-07-20 10:52:36 -070031#else
Patrick Venture0b02be92018-08-31 11:55:55 -070032#error filesystem not available
Vernon Mauery185b9f82018-07-20 10:52:36 -070033#endif
Patrick Venture0b02be92018-08-31 11:55:55 -070034
35#include "timer.hpp"
36
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050037#include <map>
Ratan Guptadcb10672017-07-10 10:33:50 +053038#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070039#include <phosphor-logging/log.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053040#include <sdbusplus/bus.hpp>
41#include <sdbusplus/server/object.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070042#include <string>
43#include <xyz/openbmc_project/Common/error.hpp>
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050044#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070045#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
Deepak Kodihalli18b70d12017-07-21 13:36:33 -050046#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070047#include <xyz/openbmc_project/State/Host/server.hpp>
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050048#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
Patrick Venture46470a32018-09-07 19:26:25 -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 {
376 memset(respptr->data, 0, SIZE_BOOT_OPTION);
377 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 {
387 memset(respptr->data, 0, SIZE_BOOT_OPTION);
388 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 Venture0b02be92018-08-31 11:55:55 -0700401 memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic, sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530402
403 uint8_t addressFamily = (properties["Type"].get<std::string>() ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700404 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
405 ? AF_INET
406 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530407
Patrick Venture0b02be92018-08-31 11:55:55 -0700408 addrSize = (addressFamily == AF_INET)
409 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
410 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530411
412 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530413 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700414 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530415
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530416 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
417
418 memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
419
420 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
421
Ratan Guptad70f4532017-08-04 02:07:31 +0530422 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700423 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530424 }
425 catch (InternalFailure& e)
426 {
427 commit<InternalFailure>();
428 memset(respptr->data, 0, SIZE_BOOT_OPTION);
429 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500430 return rc;
431 }
432
Patrick Venture0b02be92018-08-31 11:55:55 -0700433 // PetiBoot-Specific
434 // If success then copy the first 9 bytes to the data
Ratan Guptadcb10672017-07-10 10:33:50 +0530435 memcpy(respptr->data, net_conf_initial_bytes,
436 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500437
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530438 memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
439
Ratan Guptafd28dd72016-08-01 04:58:01 -0500440#ifdef _IPMI_DEBUG_
Ratan Guptadcb10672017-07-10 10:33:50 +0530441 printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500442
Ratan Guptadcb10672017-07-10 10:33:50 +0530443 for (uint8_t pos = 0; pos < index; pos++)
444 {
445 printf("%02x ", respptr->data[pos]);
446 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500447#endif
448
Ratan Guptafd28dd72016-08-01 04:58:01 -0500449 return rc;
450}
451
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530452/** @brief convert IPv4 and IPv6 addresses from binary to text form.
453 * @param[in] family - IPv4/Ipv6
454 * @param[in] data - req data pointer.
455 * @param[in] offset - offset in the data.
456 * @param[in] addrSize - size of the data which needs to be read from offset.
457 * @returns address in text form.
458 */
459
Patrick Venture0b02be92018-08-31 11:55:55 -0700460std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
461 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530462{
463 char ipAddr[INET6_ADDRSTRLEN] = {};
464
Patrick Venture0b02be92018-08-31 11:55:55 -0700465 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530466 {
467 case AF_INET:
468 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 struct sockaddr_in addr4
470 {
471 };
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530472 memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
473
Patrick Venture0b02be92018-08-31 11:55:55 -0700474 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530475
476 break;
477 }
478 case AF_INET6:
479 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700480 struct sockaddr_in6 addr6
481 {
482 };
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530483 memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
484
Patrick Venture0b02be92018-08-31 11:55:55 -0700485 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530486
487 break;
488 }
489 default:
490 {
491 return {};
492 }
493 }
494
495 return ipAddr;
496}
497
Ratan Guptadcb10672017-07-10 10:33:50 +0530498int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500499{
Ratan Guptadcb10672017-07-10 10:33:50 +0530500 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500501 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700502 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530503 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700504 char addrOrigin{0};
505 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530506 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530507 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700508 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
509 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530510 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530511 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500512
Patrick Venture0b02be92018-08-31 11:55:55 -0700513 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500514 // version starts from sixth byte
515
Ratan Guptadcb10672017-07-10 10:33:50 +0530516 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500517 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530518 do
519 {
520 // cookie == 0x21 0x70 0x62 0x21
521 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700522 (net_conf_initial_bytes + COOKIE_OFFSET),
523 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530524 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700525 // cookie == 0
526 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
527 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530528 {
529 // need to zero out the network settings.
530 break;
531 }
532
533 log<level::ERR>("Invalid Cookie");
534 elog<InternalFailure>();
535 }
536
537 // vesion == 0x00 0x01
538 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700539 (net_conf_initial_bytes + VERSION_OFFSET),
540 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530541 {
542
543 log<level::ERR>("Invalid Version");
544 elog<InternalFailure>();
545 }
546
Ratan Gupta8c31d232017-08-13 05:49:43 +0530547 snprintf(mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
Patrick Venture0b02be92018-08-31 11:55:55 -0700548 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
549 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
Ratan Guptadcb10672017-07-10 10:33:50 +0530550 reqptr->data[MAC_OFFSET + 4],
551 reqptr->data[MAC_OFFSET + 5]);
552
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530553 memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
554 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530555
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530556 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530557 {
558 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530559 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530560 }
561
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530562 // Get the address size
Patrick Venture0b02be92018-08-31 11:55:55 -0700563 memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
564 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530565
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530566 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530567
Ratan Guptad70f4532017-08-04 02:07:31 +0530568 memcpy(&prefix, &(reqptr->data[prefixOffset]),
569 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530570
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530571 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
572
Ratan Gupta8c31d232017-08-13 05:49:43 +0530573 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530574 {
575 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
576 family = AF_INET6;
577 }
578
Patrick Venture0b02be92018-08-31 11:55:55 -0700579 ipAddress =
580 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530581
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530582 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
583
Patrick Venture0b02be92018-08-31 11:55:55 -0700584 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530585
Patrick Venture0b02be92018-08-31 11:55:55 -0700586 // Cookie == 0 or it is a valid cookie
587 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
588 std::to_string(prefix) + ",gateway="s + gateway +
589 ",mac="s + mac + ",addressOrigin="s +
590 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500591
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530592 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
593
594 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
595 SETTINGS_ROOT, SETTINGS_MATCH);
596 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
597 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530598 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530599 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700600 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530601 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700602 IP_INTERFACE, "PrefixLength", prefix);
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, "Origin", addressOrigin);
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, "Gateway", std::string(gateway));
607 ipmi::setDbusProperty(
608 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
609 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530610 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700611 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500612
Patrick Venture0b02be92018-08-31 11:55:55 -0700613 log<level::DEBUG>(
614 "Network configuration changed",
615 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500616 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530617 catch (InternalFailure& e)
618 {
619 commit<InternalFailure>();
620 return -1;
621 }
622
623 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500624}
625
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500626uint32_t getPOHCounter()
627{
628 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
629
Patrick Venture0b02be92018-08-31 11:55:55 -0700630 auto chassisStateObj =
631 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500632
Patrick Venture0b02be92018-08-31 11:55:55 -0700633 auto service =
634 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500635
Patrick Venture0b02be92018-08-31 11:55:55 -0700636 auto propValue =
637 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
638 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500639
640 return propValue.get<uint32_t>();
641}
642
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500643ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
644 ipmi_request_t request,
645 ipmi_response_t response,
646 ipmi_data_len_t data_len,
647 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500648{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500649 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800650 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500651 *data_len = 0;
652 return rc;
653}
654
Nan Li8d15fb42016-08-16 22:29:40 +0800655ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700656 ipmi_request_t request,
657 ipmi_response_t response,
658 ipmi_data_len_t data_len,
659 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800660{
661 // sd_bus error
662 ipmi_ret_t rc = IPMI_CC_OK;
663
664 ipmi_chassis_cap_t chassis_cap{};
665
666 *data_len = sizeof(ipmi_chassis_cap_t);
667
668 // TODO: need future work. Get those flag from MRW.
669
670 // capabilities flags
671 // [7..4] - reserved
672 // [3] – 1b = provides power interlock (IPM 1.5)
673 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
Patrick Venture0b02be92018-08-31 11:55:55 -0700674 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis has
675 // capabilities
676 // to lock out external power control and reset button or front
677 // panel interfaces and/or detect tampering with those
678 // interfaces).
Nan Li8d15fb42016-08-16 22:29:40 +0800679 // [0] -1b = Chassis provides intrusion (physical security) sensor.
680 // set to default value 0x0.
681 chassis_cap.cap_flags = 0x0;
682
683 // Since we do not have a separate SDR Device/SEL Device/ FRU repository.
684 // The 20h was given as those 5 device addresses.
685 // Chassis FRU info Device Address
686 chassis_cap.fru_info_dev_addr = 0x20;
687
688 // Chassis SDR Device Address
689 chassis_cap.sdr_dev_addr = 0x20;
690
691 // Chassis SEL Device Address
692 chassis_cap.sel_dev_addr = 0x20;
693
694 // Chassis System Management Device Address
695 chassis_cap.system_management_dev_addr = 0x20;
696
697 // Chassis Bridge Device Address.
698 chassis_cap.bridge_dev_addr = 0x20;
699
700 memcpy(response, &chassis_cap, *data_len);
701
702 return rc;
703}
704
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530705//------------------------------------------
706// Calls into Host State Manager Dbus object
707//------------------------------------------
708int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600709{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500710 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700711 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500712 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700713 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
714 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530715
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500716 // sd_bus error
717 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700718 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600719
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500720 // SD Bus error report mechanism.
721 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600722
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500723 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700724 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500725 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
726 if (rc < 0)
727 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700728 log<level::ERR>(
729 "Failed to get bus name",
730 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500731 return rc;
732 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530733
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500734 // Convert to string equivalent of the passed in transition enum.
735 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530736
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500737 rc = sd_bus_call_method(bus_type, // On the system bus
738 busname, // Service to contact
739 HOST_STATE_MANAGER_ROOT, // Object path
740 DBUS_PROPERTY_IFACE, // Interface name
741 "Set", // Method to be called
742 &bus_error, // object to return error
743 nullptr, // Response buffer if any
744 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700745 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
746 request.c_str());
747 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500748 {
749 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530750 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500751 }
752 else
753 {
754 log<level::INFO>("Transition request initiated successfully");
755 }
vishwa36993272015-11-20 12:43:49 -0600756
757 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500758 free(busname);
vishwa36993272015-11-20 12:43:49 -0600759
Sergey Solomineb9b8142016-08-23 09:07:28 -0500760 return rc;
vishwa36993272015-11-20 12:43:49 -0600761}
762
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500763namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500764{
Nan Lifdd8ec52016-08-28 03:57:40 +0800765
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500766using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
767using IpmiValue = uint8_t;
768using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800769
Patrick Venture0b02be92018-08-31 11:55:55 -0700770std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500771 {RestorePolicy::Policy::AlwaysOff, 0x00},
772 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700773 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800774
Yong Lic6713cf2018-09-12 12:35:13 +0800775static constexpr uint8_t noChange = 0x03;
776static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
777static constexpr uint8_t policyBitMask = 0x07;
778static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500779} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800780
781//----------------------------------------------------------------------
782// Get Chassis Status commands
783//----------------------------------------------------------------------
784ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500785 ipmi_request_t request,
786 ipmi_response_t response,
787 ipmi_data_len_t data_len,
788 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800789{
Patrick Venture0b02be92018-08-31 11:55:55 -0700790 const char* objname = "/org/openbmc/control/power0";
791 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800792
Patrick Venture0b02be92018-08-31 11:55:55 -0700793 sd_bus* bus = NULL;
794 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800795 int r = 0;
796 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700797 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800798 ipmi_ret_t rc = IPMI_CC_OK;
799 ipmi_get_chassis_status_t chassis_status{};
800
Nan Lifdd8ec52016-08-28 03:57:40 +0800801 uint8_t s = 0;
802
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500803 using namespace chassis::internal;
804 using namespace chassis::internal::cache;
805 using namespace power_policy;
806
Deepak Kodihallie6027092017-08-27 08:13:37 -0500807 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700808 auto method = dbus.new_method_call(
809 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
810 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500811 method.append(powerRestoreIntf, "PowerRestorePolicy");
812 auto resp = dbus.call(method);
813 if (resp.is_method_error())
814 {
815 log<level::ERR>("Error in PowerRestorePolicy Get");
816 report<InternalFailure>();
817 *data_len = 0;
818 return IPMI_CC_UNSPECIFIED_ERROR;
819 }
820 sdbusplus::message::variant<std::string> result;
821 resp.read(result);
822 auto powerRestore =
823 RestorePolicy::convertPolicyFromString(result.get<std::string>());
Nan Lifdd8ec52016-08-28 03:57:40 +0800824
825 *data_len = 4;
826
Tom Joseph63a00512017-08-09 23:39:59 +0530827 bus = ipmid_get_sd_bus_connection();
828
Nan Lifdd8ec52016-08-28 03:57:40 +0800829 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700830 if (r < 0)
831 {
832 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800833 rc = IPMI_CC_UNSPECIFIED_ERROR;
834 goto finish;
835 }
836
Patrick Venture0b02be92018-08-31 11:55:55 -0700837 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
838 "i");
839 if (r < 0)
840 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530841 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700842 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
843 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530844 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800845 rc = IPMI_CC_UNSPECIFIED_ERROR;
846 goto finish;
847 }
848
849 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -0700850 if (r < 0)
851 {
852 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800853 rc = IPMI_CC_UNSPECIFIED_ERROR;
854 goto finish;
855 }
856
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500857 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800858
859 // Current Power State
860 // [7] reserved
861 // [6..5] power restore policy
862 // 00b = chassis stays powered off after AC/mains returns
863 // 01b = after AC returns, power is restored to the state that was
864 // in effect when AC/mains was lost.
865 // 10b = chassis always powers up after AC/mains returns
866 // 11b = unknow
867 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -0700868 // Do we need to define a dbus property to identify the restore
869 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +0800870
871 // [4] power control fault
872 // 1b = controller attempted to turn system power on or off, but
873 // system did not enter desired state.
874 // Set to 0b, since We don't support it..
875
876 // [3] power fault
877 // 1b = fault detected in main power subsystem.
878 // set to 0b. for we don't support it.
879
880 // [2] 1b = interlock (chassis is presently shut down because a chassis
881 // panel interlock switch is active). (IPMI 1.5)
882 // set to 0b, for we don't support it.
883
884 // [1] power overload
885 // 1b = system shutdown because of power overload condition.
886 // set to 0b, for we don't support it.
887
888 // [0] power is on
889 // 1b = system power is on
890 // 0b = system power is off(soft-off S4/S5, or mechanical off)
891
Patrick Venture0b02be92018-08-31 11:55:55 -0700892 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +0800893
894 // Last Power Event
895 // [7..5] – reserved
896 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
897 // [3] – 1b = last power down caused by power fault
898 // [2] – 1b = last power down caused by a power interlock being activated
899 // [1] – 1b = last power down caused by a Power overload
900 // [0] – 1b = AC failed
901 // set to 0x0, for we don't support these fields.
902
903 chassis_status.last_power_event = 0;
904
905 // Misc. Chassis State
906 // [7] – reserved
907 // [6] – 1b = Chassis Identify command and state info supported (Optional)
908 // 0b = Chassis Identify command support unspecified via this command.
909 // (The Get Command Support command , if implemented, would still
910 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -0700911 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
912 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +0800913 // as 00b) otherwise. Returns the present chassis identify state.
914 // Refer to the Chassis Identify command for more info.
915 // 00b = chassis identify state = Off
916 // 01b = chassis identify state = Temporary(timed) On
917 // 10b = chassis identify state = Indefinite On
918 // 11b = reserved
919 // [3] – 1b = Cooling/fan fault detected
920 // [2] – 1b = Drive Fault
921 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
922 // push-buttons disabled.)
923 // [0] – 1b = Chassis Intrusion active
924 // set to 0, for we don't support them.
925 chassis_status.misc_power_state = 0;
926
927 // Front Panel Button Capabilities and disable/enable status(Optional)
928 // set to 0, for we don't support them.
929 chassis_status.front_panel_button_cap_status = 0;
930
931 // Pack the actual response
932 memcpy(response, &chassis_status, *data_len);
933
934finish:
935 free(busname);
936 reply = sd_bus_message_unref(reply);
937
938 return rc;
939}
Chris Austen7888c4d2015-12-03 15:26:20 -0600940
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530941//-------------------------------------------------------------
942// Send a command to SoftPowerOff application to stop any timer
943//-------------------------------------------------------------
944int stop_soft_off_timer()
945{
Patrick Venture0b02be92018-08-31 11:55:55 -0700946 constexpr auto iface = "org.freedesktop.DBus.Properties";
947 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
948 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530949
Patrick Venture0b02be92018-08-31 11:55:55 -0700950 constexpr auto property = "ResponseReceived";
951 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
952 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530953
954 // Get the system bus where most system services are provided.
955 auto bus = ipmid_get_sd_bus_connection();
956
957 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500958 // TODO openbmc/openbmc#1661 - Mapper refactor
959 //
960 // See openbmc/openbmc#1743 for some details but high level summary is that
961 // for now the code will directly call the soft off interface due to a
962 // race condition with mapper usage
963 //
Patrick Venture0b02be92018-08-31 11:55:55 -0700964 // char *busname = nullptr;
965 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
966 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500967 //{
968 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530969 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500970 // return r;
971 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530972
973 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500974 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -0700975 "Set", nullptr, nullptr, "ssv", soft_off_iface,
976 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530977 if (rc < 0)
978 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530979 log<level::ERR>("Failed to set property in SoftPowerOff object",
980 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530981 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500982
Patrick Venture0b02be92018-08-31 11:55:55 -0700983 // TODO openbmc/openbmc#1661 - Mapper refactor
984 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530985 return rc;
986}
987
vishwa36993272015-11-20 12:43:49 -0600988//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500989// Create file to indicate there is no need for softoff notification to host
990//----------------------------------------------------------------------
991void indicate_no_softoff_needed()
992{
993 fs::path path{HOST_INBAND_REQUEST_DIR};
994 if (!fs::is_directory(path))
995 {
996 fs::create_directory(path);
997 }
998
999 // Add the host instance (default 0 for now) to the file name
1000 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001001 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001002 size++; // null
1003 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001004 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001005
1006 // Append file name to directory and create it
1007 path /= buf.get();
1008 std::ofstream(path.c_str());
1009}
1010
1011//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001012// Chassis Control commands
1013//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001014ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1015 ipmi_request_t request,
1016 ipmi_response_t response,
1017 ipmi_data_len_t data_len,
1018 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001019{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001020 // Error from power off.
1021 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001022
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001023 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001024 *data_len = 0;
1025
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001026 // Catch the actual operaton by peeking into request buffer
Patrick Venture0b02be92018-08-31 11:55:55 -07001027 uint8_t chassis_ctrl_cmd = *(uint8_t*)request;
vishwa36993272015-11-20 12:43:49 -06001028
Patrick Venture0b02be92018-08-31 11:55:55 -07001029 switch (chassis_ctrl_cmd)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001030 {
1031 case CMD_POWER_ON:
1032 rc = initiate_state_transition(State::Host::Transition::On);
1033 break;
1034 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301035 // This path would be hit in 2 conditions.
1036 // 1: When user asks for power off using ipmi chassis command 0x04
1037 // 2: Host asking for power off post shutting down.
1038
1039 // If it's a host requested power off, then need to nudge Softoff
1040 // application that it needs to stop the watchdog timer if running.
1041 // If it is a user requested power off, then this is not really
1042 // needed. But then we need to differentiate between user and host
1043 // calling this same command
1044
1045 // For now, we are going ahead with trying to nudge the soft off and
1046 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001047 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301048
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001049 // Only request the Off transition if the soft power off
1050 // application is not running
1051 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001052 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001053 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301054 // that it should not run. Not doing this will result in State
1055 // manager doing a default soft power off when asked for power
1056 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001057 indicate_no_softoff_needed();
1058
1059 // Now request the shutdown
1060 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001061 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001062 else
1063 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301064 log<level::INFO>("Soft off is running, so let shutdown target "
1065 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001066 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001067 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301068
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001069 case CMD_HARD_RESET:
1070 case CMD_POWER_CYCLE:
1071 // SPEC has a section that says certain implementations can trigger
1072 // PowerOn if power is Off when a command to power cycle is
1073 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001074
1075 // First create a file to indicate to the soft off application
1076 // that it should not run since this is a direct user initiated
1077 // power reboot request (i.e. a reboot request that is not
1078 // originating via a soft power off SMS request)
1079 indicate_no_softoff_needed();
1080
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001081 rc = initiate_state_transition(State::Host::Transition::Reboot);
1082 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301083
1084 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1085 // Request Host State Manager to do a soft power off
1086 rc = initiate_state_transition(State::Host::Transition::Off);
1087 break;
1088
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001089 default:
1090 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301091 log<level::ERR>("Invalid Chassis Control command",
1092 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001093 rc = -1;
1094 }
1095 }
vishwa36993272015-11-20 12:43:49 -06001096
Patrick Venture0b02be92018-08-31 11:55:55 -07001097 return ((rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001098}
1099
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001100/** @brief Return D-Bus connection string to enclosure identify LED object
1101 *
1102 * @param[in, out] connection - connection to D-Bus object
1103 * @return a IPMI return code
1104 */
1105std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301106{
Tom Joseph5110c122018-03-23 17:55:40 +05301107 // lookup enclosure_identify group owner(s) in mapper
1108 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001109 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1110 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301111
1112 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001113 static const std::vector<std::string> interfaces = {
1114 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301115 mapperCall.append(interfaces);
1116 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1117 if (mapperReply.is_method_error())
1118 {
1119 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001120 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301121 }
1122 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1123 mapperReply.read(mapperResp);
1124
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001125 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301126 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001127 log<level::ERR>(
1128 "Invalid number of enclosure identify objects.",
1129 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001130 elog<InternalFailure>();
1131 }
1132 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1133 return pair.first;
1134}
Tom Joseph5110c122018-03-23 17:55:40 +05301135
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001136/** @brief Turn On/Off enclosure identify LED
1137 *
1138 * @param[in] flag - true to turn on LED, false to turn off
1139 * @return a IPMI return code
1140 */
1141void enclosureIdentifyLed(bool flag)
1142{
1143 using namespace chassis::internal;
1144 std::string connection = std::move(getEnclosureIdentifyConnection());
Patrick Venture0b02be92018-08-31 11:55:55 -07001145 auto led =
1146 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1147 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001148 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001149 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001150 auto ledReply = dbus.call(led);
1151 if (ledReply.is_method_error())
1152 {
1153 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001154 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001155 elog<InternalFailure>();
1156 }
1157}
1158
1159/** @brief Callback method to turn off LED
1160 */
1161void enclosureIdentifyLedOff()
1162{
1163 try
1164 {
1165 enclosureIdentifyLed(false);
1166 }
1167 catch (const InternalFailure& e)
1168 {
1169 report<InternalFailure>();
1170 }
1171}
1172
1173/** @brief Create timer to turn on and off the enclosure LED
1174 */
1175void createIdentifyTimer()
1176{
1177 if (!identifyTimer)
1178 {
1179 identifyTimer = std::make_unique<phosphor::ipmi::Timer>(
1180 ipmid_get_sd_event_connection(), enclosureIdentifyLedOff);
1181 }
1182}
1183
1184ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1185 ipmi_request_t request,
1186 ipmi_response_t response,
1187 ipmi_data_len_t data_len,
1188 ipmi_context_t context)
1189{
1190 if (*data_len > chassisIdentifyReqLength)
1191 {
1192 return IPMI_CC_REQ_DATA_LEN_INVALID;
1193 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001194 uint8_t identifyInterval =
1195 *data_len > identifyIntervalPos
1196 ? (static_cast<uint8_t*>(request))[identifyIntervalPos]
1197 : DEFAULT_IDENTIFY_TIME_OUT;
1198 bool forceIdentify =
1199 (*data_len == chassisIdentifyReqLength)
1200 ? (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01
1201 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301202
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001203 if (identifyInterval || forceIdentify)
1204 {
1205 // stop the timer if already started, for force identify we should
1206 // not turn off LED
1207 identifyTimer->setTimer(SD_EVENT_OFF);
1208 try
Tom Joseph5110c122018-03-23 17:55:40 +05301209 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001210 enclosureIdentifyLed(true);
1211 }
1212 catch (const InternalFailure& e)
1213 {
1214 report<InternalFailure>();
1215 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301216 }
1217
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001218 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301219 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001220 return IPMI_CC_OK;
1221 }
1222 // start the timer
1223 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001224 std::chrono::seconds(identifyInterval));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001225 identifyTimer->startTimer(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301226 }
Tom Josephbed26992018-07-31 23:00:24 +05301227 else if (!identifyInterval)
1228 {
1229 identifyTimer->setTimer(SD_EVENT_OFF);
1230 enclosureIdentifyLedOff();
1231 }
Tom Joseph5110c122018-03-23 17:55:40 +05301232 return IPMI_CC_OK;
1233}
1234
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001235namespace boot_options
1236{
1237
1238using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1239using IpmiValue = uint8_t;
1240constexpr auto ipmiDefault = 0;
1241
Patrick Venture0b02be92018-08-31 11:55:55 -07001242std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001243 {0x01, Source::Sources::Network},
1244 {0x02, Source::Sources::Disk},
1245 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001246 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001247
Patrick Venture0b02be92018-08-31 11:55:55 -07001248std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001249 {0x03, Mode::Modes::Safe},
1250 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001251 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001252
Patrick Venture0b02be92018-08-31 11:55:55 -07001253std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001254 {Source::Sources::Network, 0x01},
1255 {Source::Sources::Disk, 0x02},
1256 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001257 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001258
Patrick Venture0b02be92018-08-31 11:55:55 -07001259std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001260 {Mode::Modes::Safe, 0x03},
1261 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001262 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001263
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001264} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001265
Marri Devender Rao81719702018-05-07 00:53:48 -05001266/** @brief Set the property value for boot source
1267 * @param[in] source - boot source value
1268 * @return On failure return IPMI error.
1269 */
1270static ipmi_ret_t setBootSource(const Source::Sources& source)
1271{
1272 using namespace chassis::internal;
1273 using namespace chassis::internal::cache;
1274 sdbusplus::message::variant<std::string> property =
1275 convertForMessage(source);
1276 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1277 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001278 auto method = dbus.new_method_call(
1279 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1280 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001281 method.append(bootSourceIntf, "BootSource", property);
1282 auto reply = dbus.call(method);
1283 if (reply.is_method_error())
1284 {
1285 log<level::ERR>("Error in BootSource Set");
1286 report<InternalFailure>();
1287 return IPMI_CC_UNSPECIFIED_ERROR;
1288 }
1289 return IPMI_CC_OK;
1290}
1291
Patrick Venture0b02be92018-08-31 11:55:55 -07001292/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001293 * @param[in] mode - boot mode value
1294 * @return On failure return IPMI error.
1295 */
1296static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1297{
1298 using namespace chassis::internal;
1299 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001300 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001301 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1302 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001303 auto method = dbus.new_method_call(
1304 objects.service(bootModeSetting, bootModeIntf).c_str(),
1305 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001306 method.append(bootModeIntf, "BootMode", property);
1307 auto reply = dbus.call(method);
1308 if (reply.is_method_error())
1309 {
1310 log<level::ERR>("Error in BootMode Set");
1311 report<InternalFailure>();
1312 return IPMI_CC_UNSPECIFIED_ERROR;
1313 }
1314 return IPMI_CC_OK;
1315}
1316
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001317ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1318 ipmi_request_t request,
1319 ipmi_response_t response,
1320 ipmi_data_len_t data_len,
1321 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001322{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001323 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001324 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001325 char* p = NULL;
1326 get_sys_boot_options_response_t* resp =
1327 (get_sys_boot_options_response_t*)response;
1328 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001329 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001330
Patrick Venture0b02be92018-08-31 11:55:55 -07001331 memset(resp, 0, sizeof(*resp));
1332 resp->version = SET_PARM_VERSION;
1333 resp->parm = 5;
1334 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001335
shgoupfd84fbbf2015-12-17 10:05:51 +08001336 /*
1337 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1338 * This is the only parameter used by petitboot.
1339 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001340 if (reqptr->parameter ==
1341 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1342 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001343
Ratan Guptafd28dd72016-08-01 04:58:01 -05001344 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001345 using namespace chassis::internal;
1346 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001347
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001348 try
ratagupta6f6bff2016-04-04 06:20:11 -05001349 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001350 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1351 const auto& bootSourceSetting =
1352 std::get<settings::Path>(bootSetting);
1353 auto oneTimeEnabled =
1354 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001355 auto method = dbus.new_method_call(
1356 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1357 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001358 method.append(bootSourceIntf, "BootSource");
1359 auto reply = dbus.call(method);
1360 if (reply.is_method_error())
1361 {
1362 log<level::ERR>("Error in BootSource Get");
1363 report<InternalFailure>();
1364 *data_len = 0;
1365 return IPMI_CC_UNSPECIFIED_ERROR;
1366 }
1367 sdbusplus::message::variant<std::string> result;
1368 reply.read(result);
1369 auto bootSource =
1370 Source::convertSourcesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001371
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001372 bootSetting = settings::boot::setting(objects, bootModeIntf);
1373 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1374 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001375 objects.service(bootModeSetting, bootModeIntf).c_str(),
1376 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001377 method.append(bootModeIntf, "BootMode");
1378 reply = dbus.call(method);
1379 if (reply.is_method_error())
1380 {
1381 log<level::ERR>("Error in BootMode Get");
1382 report<InternalFailure>();
1383 *data_len = 0;
1384 return IPMI_CC_UNSPECIFIED_ERROR;
1385 }
1386 reply.read(result);
1387 auto bootMode =
1388 Mode::convertModesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001389
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001390 bootOption = sourceDbusToIpmi.at(bootSource);
1391 if ((Mode::Modes::Regular == bootMode) &&
1392 (Source::Sources::Default == bootSource))
1393 {
1394 bootOption = ipmiDefault;
1395 }
1396 else if (Source::Sources::Default == bootSource)
1397 {
1398 bootOption = modeDbusToIpmi.at(bootMode);
1399 }
1400 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001401
Patrick Venture0b02be92018-08-31 11:55:55 -07001402 resp->data[0] = oneTimeEnabled
1403 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1404 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001405
ratagupta6f6bff2016-04-04 06:20:11 -05001406 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001407 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001408 catch (InternalFailure& e)
1409 {
1410 report<InternalFailure>();
1411 *data_len = 0;
1412 return IPMI_CC_UNSPECIFIED_ERROR;
1413 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001414 }
1415 else if (reqptr->parameter ==
1416 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1417 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001418
Patrick Venture0b02be92018-08-31 11:55:55 -07001419 *data_len =
1420 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001421
Patrick Venture0b02be92018-08-31 11:55:55 -07001422 resp->parm =
1423 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001424
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001425 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001426
Patrick Venture0b02be92018-08-31 11:55:55 -07001427 if (ret < 0)
1428 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001429
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301430 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001431 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001432 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001433 }
1434 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001435 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001436 }
1437
Patrick Venture0b02be92018-08-31 11:55:55 -07001438 else
1439 {
1440 log<level::ERR>("Unsupported parameter",
1441 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001442 }
1443
1444 if (p)
1445 free(p);
1446
Ratan Guptafd28dd72016-08-01 04:58:01 -05001447 if (rc == IPMI_CC_OK)
1448 {
1449 *data_len += 2;
1450 }
1451
shgoupfd84fbbf2015-12-17 10:05:51 +08001452 return rc;
1453}
1454
shgoupfd84fbbf2015-12-17 10:05:51 +08001455ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001456 ipmi_request_t request,
1457 ipmi_response_t response,
1458 ipmi_data_len_t data_len,
1459 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001460{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001461 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001462 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001463 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001464
Patrick Venture0b02be92018-08-31 11:55:55 -07001465 printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1466 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001467
shgoupfd84fbbf2015-12-17 10:05:51 +08001468 // This IPMI command does not have any resposne data
1469 *data_len = 0;
1470
1471 /* 000101
1472 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1473 * This is the only parameter used by petitboot.
1474 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001475
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001476 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1477 {
1478 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1479 using namespace chassis::internal;
1480 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001481 auto oneTimeEnabled = false;
1482 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301483 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001484 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001485
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001486 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001487 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001488 bool permanent =
1489 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1490 SET_PARM_BOOT_FLAGS_PERMANENT;
1491
Patrick Venture0b02be92018-08-31 11:55:55 -07001492 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301493
1494 oneTimeEnabled =
1495 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1496
1497 /*
1498 * Check if the current boot setting is onetime or permanent, if the
1499 * request in the command is otherwise, then set the "Enabled"
1500 * property in one_time object path to 'True' to indicate onetime
1501 * and 'False' to indicate permanent.
1502 *
1503 * Once the onetime/permanent setting is applied, then the bootMode
1504 * and bootSource is updated for the corresponding object.
1505 */
1506 if ((permanent && oneTimeEnabled) ||
1507 (!permanent && !oneTimeEnabled))
1508 {
1509 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1510
Patrick Venture0b02be92018-08-31 11:55:55 -07001511 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1512 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301513 }
1514
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001515 auto modeItr = modeIpmiToDbus.find(bootOption);
1516 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1517 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001518 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001519 rc = setBootSource(sourceItr->second);
1520 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001521 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001522 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001523 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001524 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001525 // If a set boot device is mapping to a boot source, then reset
1526 // the boot mode D-Bus property to default.
1527 // This way the ipmid code can determine which property is not
1528 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001529 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001530 {
1531 setBootMode(Mode::Modes::Regular);
1532 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001533 }
1534 if (modeIpmiToDbus.end() != modeItr)
1535 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001536 rc = setBootMode(modeItr->second);
1537 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001538 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001539 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001540 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001541 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001542 // If a set boot device is mapping to a boot mode, then reset
1543 // the boot source D-Bus property to default.
1544 // This way the ipmid code can determine which property is not
1545 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001546 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001547 {
1548 setBootSource(Source::Sources::Default);
1549 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001550 }
1551 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001552 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001553 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001554 report<InternalFailure>();
1555 *data_len = 0;
1556 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001557 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001558 }
1559 else if (reqptr->parameter ==
1560 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1561 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001562
1563 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001564 if (ret < 0)
1565 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301566 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001567 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001568 rc = IPMI_CC_UNSPECIFIED_ERROR;
1569 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001570 }
1571 else if (reqptr->parameter ==
1572 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1573 {
Tom Josephf536c902017-09-25 18:08:15 +05301574 // Handle parameter #4 and return command completed normally
1575 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1576 // parameter. This is added to support the ipmitool command `chassis
1577 // bootdev` which sends set on parameter #4, before setting the boot
1578 // flags.
1579 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001580 }
1581 else
1582 {
1583 log<level::ERR>("Unsupported parameter",
1584 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001585 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001586 }
1587
1588 return rc;
1589}
1590
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001591ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1592 ipmi_request_t request, ipmi_response_t response,
1593 ipmi_data_len_t data_len, ipmi_context_t context)
1594{
1595 // sd_bus error
1596 ipmi_ret_t rc = IPMI_CC_OK;
1597
1598 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1599
1600 try
1601 {
1602 auto pohCounter = getPOHCounter();
1603 resptr->counterReading[0] = pohCounter;
1604 resptr->counterReading[1] = pohCounter >> 8;
1605 resptr->counterReading[2] = pohCounter >> 16;
1606 resptr->counterReading[3] = pohCounter >> 24;
1607 }
1608 catch (std::exception& e)
1609 {
1610 log<level::ERR>(e.what());
1611 return IPMI_CC_UNSPECIFIED_ERROR;
1612 }
1613
1614 resptr->minPerCount = poh::minutesPerCount;
1615 *data_len = sizeof(GetPOHCountResponse);
1616
1617 return rc;
1618}
1619
Yong Lic6713cf2018-09-12 12:35:13 +08001620ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1621 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1622 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1623{
1624 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1625 auto* resptr = reinterpret_cast<uint8_t*>(response);
1626 uint8_t reqPolicy = 0;
1627
1628 power_policy::DbusValue value =
1629 power_policy::RestorePolicy::Policy::AlwaysOff;
1630
1631 if (*data_len != power_policy::setPolicyReqLen)
1632 {
1633 phosphor::logging::log<level::ERR>("Unsupported request length",
1634 entry("LEN=0x%x", *data_len));
1635 *data_len = 0;
1636 return IPMI_CC_REQ_DATA_LEN_INVALID;
1637 }
1638
1639 reqPolicy = *reqptr & power_policy::policyBitMask;
1640 if (reqPolicy > power_policy::noChange)
1641 {
1642 phosphor::logging::log<level::ERR>("Reserved request parameter",
1643 entry("REQ=0x%x", reqPolicy));
1644 *data_len = 0;
1645 return IPMI_CC_PARM_NOT_SUPPORTED;
1646 }
1647
1648 if (reqPolicy == power_policy::noChange)
1649 {
1650 // just return the supported policy
1651 *resptr = power_policy::allSupport;
1652 *data_len = power_policy::setPolicyReqLen;
1653 return IPMI_CC_OK;
1654 }
1655
1656 for (auto const& it : power_policy::dbusToIpmi)
1657 {
1658 if (it.second == reqPolicy)
1659 {
1660 value = it.first;
1661 break;
1662 }
1663 }
1664
1665 try
1666 {
1667 const settings::Path& powerRestoreSetting =
1668 chassis::internal::cache::objects.map
1669 .at(chassis::internal::powerRestoreIntf)
1670 .front();
1671 sdbusplus::message::variant<std::string> property =
1672 convertForMessage(value);
1673
1674 auto method = chassis::internal::dbus.new_method_call(
1675 chassis::internal::cache::objects
1676 .service(powerRestoreSetting,
1677 chassis::internal::powerRestoreIntf)
1678 .c_str(),
1679 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1680
1681 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1682 property);
1683 auto reply = chassis::internal::dbus.call(method);
1684 if (reply.is_method_error())
1685 {
1686 phosphor::logging::log<level::ERR>("Unspecified Error");
1687 *data_len = 0;
1688 return IPMI_CC_UNSPECIFIED_ERROR;
1689 }
1690 }
1691 catch (InternalFailure& e)
1692 {
1693 report<InternalFailure>();
1694 *data_len = 0;
1695 return IPMI_CC_UNSPECIFIED_ERROR;
1696 }
1697
1698 *data_len = power_policy::setPolicyReqLen;
1699 return IPMI_CC_OK;
1700}
1701
Adriana Kobylak40814c62015-10-27 15:58:44 -05001702void register_netfn_chassis_functions()
1703{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001704 createIdentifyTimer();
1705
Tom05732372016-09-06 17:21:23 +05301706 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001707 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1708 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001709
Tom05732372016-09-06 17:21:23 +05301710 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001711 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1712 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001713
Tom05732372016-09-06 17:21:23 +05301714 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301715 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001716 ipmi_chassis_get_sys_boot_options,
1717 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001718
Tom05732372016-09-06 17:21:23 +05301719 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001720 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1721 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001722
Tom05732372016-09-06 17:21:23 +05301723 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001724 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1725 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001726
Tom Joseph5110c122018-03-23 17:55:40 +05301727 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301728 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1729 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1730
Tom05732372016-09-06 17:21:23 +05301731 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301732 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001733 ipmi_chassis_set_sys_boot_options,
1734 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001735 // <Get POH Counter>
1736 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1737 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001738
1739 // <Set Power Restore Policy>
1740 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1741 ipmi_chassis_set_power_restore_policy,
1742 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001743}