blob: d4ce0e449262d034fe40e9ccd7eba1d255ff3ff9 [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>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070019#include <cstring>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050020#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053021#include <future>
Patrick Venture3a5071a2018-09-12 13:27:42 -070022#include <map>
23#include <phosphor-logging/elog-errors.hpp>
24#include <phosphor-logging/log.hpp>
25#include <sdbusplus/bus.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070026#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070027#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070028#include <sdbusplus/timer.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070029#include <sstream>
Patrick Venture3a5071a2018-09-12 13:27:42 -070030#include <string>
31#include <xyz/openbmc_project/Common/error.hpp>
32#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
33#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
34#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
35#include <xyz/openbmc_project/State/Host/server.hpp>
36#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
37
Vernon Mauery185b9f82018-07-20 10:52:36 -070038#if __has_include(<filesystem>)
39#include <filesystem>
40#elif __has_include(<experimental/filesystem>)
Andrew Geisslera6e3a302017-05-31 19:34:00 -050041#include <experimental/filesystem>
Patrick Venture0b02be92018-08-31 11:55:55 -070042namespace std
43{
44// splice experimental::filesystem into std
45namespace filesystem = std::experimental::filesystem;
46} // namespace std
Vernon Mauery185b9f82018-07-20 10:52:36 -070047#else
Patrick Venture0b02be92018-08-31 11:55:55 -070048#error filesystem not available
Vernon Mauery185b9f82018-07-20 10:52:36 -070049#endif
Patrick Venture0b02be92018-08-31 11:55:55 -070050
Patrick Venture0b02be92018-08-31 11:55:55 -070051// Defines
52#define SET_PARM_VERSION 0x01
Vernon Mauery1181af72018-10-08 12:05:00 -070053#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40
54#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80
55#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0
ratagupta6f6bff2016-04-04 06:20:11 -050056
Vernon Mauery1181af72018-10-08 12:05:00 -070057std::unique_ptr<phosphor::Timer> identifyTimer = nullptr;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050058
Patrick Venture0b02be92018-08-31 11:55:55 -070059constexpr size_t SIZE_MAC = 18;
60constexpr size_t SIZE_BOOT_OPTION = (uint8_t)
61 BootOptionResponseSize::OPAL_NETWORK_SETTINGS; // Maximum size of the boot
62 // option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050063constexpr size_t SIZE_PREFIX = 7;
64constexpr size_t MAX_PREFIX_VALUE = 32;
65constexpr size_t SIZE_COOKIE = 4;
66constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053067constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053068
Patrick Venture0b02be92018-08-31 11:55:55 -070069// PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053070static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
Patrick Venture0b02be92018-08-31 11:55:55 -070071 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050072
73static constexpr size_t COOKIE_OFFSET = 1;
74static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053075static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050076static constexpr size_t MAC_OFFSET = 9;
77static constexpr size_t ADDRTYPE_OFFSET = 16;
78static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050079
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050080static constexpr size_t encIdentifyObjectsSize = 1;
81static constexpr size_t chassisIdentifyReqLength = 2;
82static constexpr size_t identifyIntervalPos = 0;
83static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080084
Adriana Kobylak40814c62015-10-27 15:58:44 -050085void register_netfn_chassis_functions() __attribute__((constructor));
86
shgoupfd84fbbf2015-12-17 10:05:51 +080087// Host settings in dbus
88// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070089const char* settings_object_name = "/org/openbmc/settings/host0";
90const char* settings_intf_name = "org.freedesktop.DBus.Properties";
91const char* host_intf_name = "org.openbmc.settings.Host";
92const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053093 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080094
Ratan Guptadcb10672017-07-10 10:33:50 +053095constexpr auto SETTINGS_ROOT = "/";
96constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053097
98constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
99constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
100
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500101static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
102static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -0700103 "xyz.openbmc_project.State.PowerOnHours";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500104static constexpr auto pOHCounterProperty = "POHCounter";
105static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +0800106const static constexpr char chassisCapIntf[] =
107 "xyz.openbmc_project.Control.ChassisCapabilities";
108const static constexpr char chassisCapFlagsProp[] = "CapabilitiesFlags";
109const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
110const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
111const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
112const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
113const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
114static constexpr uint8_t chassisCapFlagMask = 0x0f;
115static constexpr uint8_t chassisCapAddrMask = 0xfe;
Ratan Guptadcb10672017-07-10 10:33:50 +0530116
Nan Li8d15fb42016-08-16 22:29:40 +0800117typedef struct
118{
119 uint8_t cap_flags;
120 uint8_t fru_info_dev_addr;
121 uint8_t sdr_dev_addr;
122 uint8_t sel_dev_addr;
123 uint8_t system_management_dev_addr;
124 uint8_t bridge_dev_addr;
Patrick Venture0b02be92018-08-31 11:55:55 -0700125} __attribute__((packed)) ipmi_chassis_cap_t;
Nan Li8d15fb42016-08-16 22:29:40 +0800126
Nan Lifdd8ec52016-08-28 03:57:40 +0800127typedef struct
128{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500129 uint8_t cur_power_state;
130 uint8_t last_power_event;
131 uint8_t misc_power_state;
132 uint8_t front_panel_button_cap_status;
Patrick Venture0b02be92018-08-31 11:55:55 -0700133} __attribute__((packed)) ipmi_get_chassis_status_t;
Nan Lifdd8ec52016-08-28 03:57:40 +0800134
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500135/**
136 * @struct Get POH counter command response data
137 */
138struct GetPOHCountResponse
139{
Patrick Venture0b02be92018-08-31 11:55:55 -0700140 uint8_t minPerCount; ///< Minutes per count
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500141 uint8_t counterReading[4]; ///< Counter reading
Patrick Venture0b02be92018-08-31 11:55:55 -0700142} __attribute__((packed));
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500143
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530144// Phosphor Host State manager
145namespace State = sdbusplus::xyz::openbmc_project::State::server;
146
Vernon Mauery185b9f82018-07-20 10:52:36 -0700147namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500148
Ratan Guptadcb10672017-07-10 10:33:50 +0530149using namespace phosphor::logging;
150using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500151using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
William A. Kennington III4c008022018-10-12 17:18:14 -0700152namespace variant_ns = sdbusplus::message::variant_ns;
153
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500154namespace chassis
155{
156namespace internal
157{
158
159constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
160constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500161constexpr auto powerRestoreIntf =
162 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500163sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
164
165namespace cache
166{
167
168settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500169 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500170
171} // namespace cache
172} // namespace internal
173} // namespace chassis
174
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500175namespace poh
176{
177
178constexpr auto minutesPerCount = 60;
179
180} // namespace poh
181
Patrick Venture0b02be92018-08-31 11:55:55 -0700182// TODO : Can remove the below function as we have
Ratan Guptadcb10672017-07-10 10:33:50 +0530183// new functions which uses sdbusplus.
184//
185// openbmc/openbmc#1489
Patrick Venture0b02be92018-08-31 11:55:55 -0700186int dbus_get_property(const char* name, char** buf)
shgoupfd84fbbf2015-12-17 10:05:51 +0800187{
188 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700189 sd_bus_message* m = NULL;
190 sd_bus* bus = NULL;
191 char* temp_buf = NULL;
192 char* connection = NULL;
shgoupfd84fbbf2015-12-17 10:05:51 +0800193 int r;
194
Brad Bishop35518682016-07-22 08:35:41 -0400195 // Get the system bus where most system services are provided.
196 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800197
Brad Bishop35518682016-07-22 08:35:41 -0400198 r = mapper_get_service(bus, settings_object_name, &connection);
Patrick Venture0b02be92018-08-31 11:55:55 -0700199 if (r < 0)
200 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530201 log<level::ERR>("Failed to get connection",
202 entry("OBJ_NAME=%s", settings_object_name),
203 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800204 goto finish;
205 }
206
shgoupfd84fbbf2015-12-17 10:05:51 +0800207 /*
208 * Bus, service, object path, interface and method are provided to call
209 * the method.
210 * Signatures and input arguments are provided by the arguments at the
211 * end.
212 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700213 r = sd_bus_call_method(bus, connection, /* service to contact */
214 settings_object_name, /* object path */
215 settings_intf_name, /* interface name */
216 "Get", /* method name */
217 &error, /* object to return error in */
218 &m, /* return message on success */
219 "ss", /* input signature */
220 host_intf_name, /* first argument */
221 name); /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800222
Patrick Venture0b02be92018-08-31 11:55:55 -0700223 if (r < 0)
224 {
225 log<level::ERR>("Failed to issue Get method call",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530226 entry("ERRNO=0x%X", r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800227 goto finish;
228 }
229
Patrick Venture0b02be92018-08-31 11:55:55 -0700230 /*
231 * The output should be parsed exactly the same as the output formatting
232 * specified.
233 */
234 r = sd_bus_message_read(m, "v", "s", &temp_buf);
235 if (r < 0)
236 {
237 log<level::ERR>("Failed to parse response message",
238 entry("ERRNO=0x%X", -r));
239 goto finish;
240 }
241
242 *buf = strdup(temp_buf);
243 /* *buf = (char*) malloc(strlen(temp_buf));
244 if (*buf) {
245 strcpy(*buf, temp_buf);
246 }
247 */
248
249finish:
shgoupfd84fbbf2015-12-17 10:05:51 +0800250 sd_bus_error_free(&error);
251 sd_bus_message_unref(m);
252 free(connection);
253
254 return r;
255}
256
Patrick Venture0b02be92018-08-31 11:55:55 -0700257// TODO : Can remove the below function as we have
258// new functions which uses sdbusplus.
259//
260// openbmc/openbmc#1489
261
262int dbus_set_property(const char* name, const char* value)
263{
264 sd_bus_error error = SD_BUS_ERROR_NULL;
265 sd_bus_message* m = NULL;
266 sd_bus* bus = NULL;
267 char* connection = NULL;
268 int r;
269
270 // Get the system bus where most system services are provided.
271 bus = ipmid_get_sd_bus_connection();
272
273 r = mapper_get_service(bus, settings_object_name, &connection);
274 if (r < 0)
275 {
276 log<level::ERR>("Failed to get connection",
277 entry("OBJ_NAME=%s", settings_object_name),
278 entry("ERRNO=0x%X", -r));
279 goto finish;
280 }
281
282 /*
283 * Bus, service, object path, interface and method are provided to call
284 * the method.
285 * Signatures and input arguments are provided by the arguments at the
286 * end.
287 */
288 r = sd_bus_call_method(bus, connection, /* service to contact */
289 settings_object_name, /* object path */
290 settings_intf_name, /* interface name */
291 "Set", /* method name */
292 &error, /* object to return error in */
293 &m, /* return message on success */
294 "ssv", /* input signature */
295 host_intf_name, /* first argument */
296 name, /* second argument */
297 "s", /* third argument */
298 value); /* fourth argument */
299
300 if (r < 0)
301 {
302 log<level::ERR>("Failed to issue Set method call",
303 entry("ERRNO=0x%X", r));
304 goto finish;
305 }
306
307finish:
308 sd_bus_error_free(&error);
309 sd_bus_message_unref(m);
310 free(connection);
311
312 return r;
313}
314
315struct get_sys_boot_options_t
316{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500317 uint8_t parameter;
318 uint8_t set;
319 uint8_t block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700320} __attribute__((packed));
Adriana Kobylak40814c62015-10-27 15:58:44 -0500321
Patrick Venture0b02be92018-08-31 11:55:55 -0700322struct get_sys_boot_options_response_t
323{
shgoupfd84fbbf2015-12-17 10:05:51 +0800324 uint8_t version;
325 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500326 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700327} __attribute__((packed));
shgoupfd84fbbf2015-12-17 10:05:51 +0800328
Patrick Venture0b02be92018-08-31 11:55:55 -0700329struct set_sys_boot_options_t
330{
shgoupfd84fbbf2015-12-17 10:05:51 +0800331 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500332 uint8_t data[SIZE_BOOT_OPTION];
Patrick Venture0b02be92018-08-31 11:55:55 -0700333} __attribute__((packed));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500334
Ratan Guptadcb10672017-07-10 10:33:50 +0530335int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500336{
Ratan Guptadcb10672017-07-10 10:33:50 +0530337 ipmi::PropertyMap properties;
338 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530339 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500340
Ratan Guptadcb10672017-07-10 10:33:50 +0530341 try
342 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700343 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530344 // objects,to handle such cases we are interested on that object
345 // which are on interested busname.
346 // Currenlty mapper doesn't give the readable busname(gives busid)
347 // so we can't match with bus name so giving some object specific info
348 // as SETTINGS_MATCH.
349 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500350
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530351 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530352
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530353 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
354 SETTINGS_ROOT, SETTINGS_MATCH);
355
356 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
357 SETTINGS_ROOT, SETTINGS_MATCH);
358
Patrick Venture0b02be92018-08-31 11:55:55 -0700359 properties = ipmi::getAllDbusProperties(
360 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
361 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
362 macObjectInfo.first, MAC_INTERFACE,
363 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530364
William A. Kennington III4c008022018-10-12 17:18:14 -0700365 auto ipAddress = variant_ns::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530366
William A. Kennington III4c008022018-10-12 17:18:14 -0700367 auto gateway = variant_ns::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530368
William A. Kennington III4c008022018-10-12 17:18:14 -0700369 auto prefix = variant_ns::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530370
Patrick Venture0b02be92018-08-31 11:55:55 -0700371 uint8_t isStatic =
William A. Kennington III4c008022018-10-12 17:18:14 -0700372 (variant_ns::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700373 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
374 ? 1
375 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530376
William A. Kennington III4c008022018-10-12 17:18:14 -0700377 auto MACAddress = variant_ns::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530378
Ratan Guptad70f4532017-08-04 02:07:31 +0530379 // it is expected here that we should get the valid data
380 // but we may also get the default values.
381 // Validation of the data is done by settings.
382 //
383 // if mac address is default mac address then
384 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530385 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530386 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700387 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530388 rc = -1;
389 return rc;
390 }
391 // if addr is static then ipaddress,gateway,prefix
392 // should not be default one,don't send blank override.
393 if (isStatic)
394 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700395 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
396 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530397 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700398 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptad70f4532017-08-04 02:07:31 +0530399 rc = -1;
400 return rc;
401 }
402 }
403
Patrick Venture0b02be92018-08-31 11:55:55 -0700404 sscanf(
405 MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
406 (respptr->data + MAC_OFFSET), (respptr->data + MAC_OFFSET + 1),
407 (respptr->data + MAC_OFFSET + 2), (respptr->data + MAC_OFFSET + 3),
408 (respptr->data + MAC_OFFSET + 4), (respptr->data + MAC_OFFSET + 5));
Ratan Guptadcb10672017-07-10 10:33:50 +0530409
Ratan Guptadcb10672017-07-10 10:33:50 +0530410 respptr->data[MAC_OFFSET + 6] = 0x00;
411
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700412 std::memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
413 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530414
William A. Kennington III4c008022018-10-12 17:18:14 -0700415 uint8_t addressFamily =
416 (variant_ns::get<std::string>(properties["Type"]) ==
417 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
418 ? AF_INET
419 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530420
Patrick Venture0b02be92018-08-31 11:55:55 -0700421 addrSize = (addressFamily == AF_INET)
422 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
423 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530424
425 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530426 inet_pton(addressFamily, ipAddress.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700427 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530428
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530429 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
430
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700431 std::memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530432
433 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
434
Ratan Guptad70f4532017-08-04 02:07:31 +0530435 inet_pton(addressFamily, gateway.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700436 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530437 }
438 catch (InternalFailure& e)
439 {
440 commit<InternalFailure>();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700441 std::memset(respptr->data, 0, SIZE_BOOT_OPTION);
Ratan Guptadcb10672017-07-10 10:33:50 +0530442 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500443 return rc;
444 }
445
Patrick Venture0b02be92018-08-31 11:55:55 -0700446 // PetiBoot-Specific
447 // If success then copy the first 9 bytes to the data
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700448 std::memcpy(respptr->data, net_conf_initial_bytes,
449 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500450
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700451 std::memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530452
Ratan Guptafd28dd72016-08-01 04:58:01 -0500453#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700454 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500455
Ratan Guptadcb10672017-07-10 10:33:50 +0530456 for (uint8_t pos = 0; pos < index; pos++)
457 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700458 std::printf("%02x ", respptr->data[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530459 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500460#endif
461
Ratan Guptafd28dd72016-08-01 04:58:01 -0500462 return rc;
463}
464
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530465/** @brief convert IPv4 and IPv6 addresses from binary to text form.
466 * @param[in] family - IPv4/Ipv6
467 * @param[in] data - req data pointer.
468 * @param[in] offset - offset in the data.
469 * @param[in] addrSize - size of the data which needs to be read from offset.
470 * @returns address in text form.
471 */
472
Patrick Venture0b02be92018-08-31 11:55:55 -0700473std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
474 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530475{
476 char ipAddr[INET6_ADDRSTRLEN] = {};
477
Patrick Venture0b02be92018-08-31 11:55:55 -0700478 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530479 {
480 case AF_INET:
481 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700482 struct sockaddr_in addr4
483 {
484 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700485 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530486
Patrick Venture0b02be92018-08-31 11:55:55 -0700487 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530488
489 break;
490 }
491 case AF_INET6:
492 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700493 struct sockaddr_in6 addr6
494 {
495 };
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700496 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530497
Patrick Venture0b02be92018-08-31 11:55:55 -0700498 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530499
500 break;
501 }
502 default:
503 {
504 return {};
505 }
506 }
507
508 return ipAddr;
509}
510
Ratan Guptadcb10672017-07-10 10:33:50 +0530511int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500512{
Ratan Guptadcb10672017-07-10 10:33:50 +0530513 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500514 std::string host_network_config;
Patrick Venture0b02be92018-08-31 11:55:55 -0700515 char mac[]{"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530516 std::string ipAddress, gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700517 char addrOrigin{0};
518 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530519 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530520 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700521 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
522 uint8_t prefix{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530523 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530524 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500525
Patrick Venture0b02be92018-08-31 11:55:55 -0700526 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500527 // version starts from sixth byte
528
Ratan Guptadcb10672017-07-10 10:33:50 +0530529 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500530 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530531 do
532 {
533 // cookie == 0x21 0x70 0x62 0x21
534 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700535 (net_conf_initial_bytes + COOKIE_OFFSET),
536 SIZE_COOKIE) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530537 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700538 // cookie == 0
539 if (memcmp(&(reqptr->data[COOKIE_OFFSET]), &zeroCookie,
540 SIZE_COOKIE) == 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530541 {
542 // need to zero out the network settings.
543 break;
544 }
545
546 log<level::ERR>("Invalid Cookie");
547 elog<InternalFailure>();
548 }
549
550 // vesion == 0x00 0x01
551 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
Patrick Venture0b02be92018-08-31 11:55:55 -0700552 (net_conf_initial_bytes + VERSION_OFFSET),
553 SIZE_VERSION) != 0)
Ratan Guptadcb10672017-07-10 10:33:50 +0530554 {
555
556 log<level::ERR>("Invalid Version");
557 elog<InternalFailure>();
558 }
559
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700560 std::snprintf(
561 mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
562 reqptr->data[MAC_OFFSET], reqptr->data[MAC_OFFSET + 1],
563 reqptr->data[MAC_OFFSET + 2], reqptr->data[MAC_OFFSET + 3],
564 reqptr->data[MAC_OFFSET + 4], reqptr->data[MAC_OFFSET + 5]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530565
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700566 std::memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
567 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530568
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530569 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530570 {
571 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530572 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530573 }
574
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530575 // Get the address size
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700576 std::memcpy(&addrSize, &reqptr->data[ADDR_SIZE_OFFSET],
577 sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530578
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530579 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530580
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700581 std::memcpy(&prefix, &(reqptr->data[prefixOffset]),
582 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530583
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530584 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
585
Ratan Gupta8c31d232017-08-13 05:49:43 +0530586 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530587 {
588 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
589 family = AF_INET6;
590 }
591
Patrick Venture0b02be92018-08-31 11:55:55 -0700592 ipAddress =
593 getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530594
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530595 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
596
Patrick Venture0b02be92018-08-31 11:55:55 -0700597 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530598
Patrick Venture0b02be92018-08-31 11:55:55 -0700599 // Cookie == 0 or it is a valid cookie
600 host_network_config += "ipaddress="s + ipAddress + ",prefix="s +
601 std::to_string(prefix) + ",gateway="s + gateway +
602 ",mac="s + mac + ",addressOrigin="s +
603 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500604
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530605 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
606
607 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
608 SETTINGS_ROOT, SETTINGS_MATCH);
609 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
610 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530611 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530612 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700613 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530614 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700615 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530616 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700617 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530618 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700619 IP_INTERFACE, "Gateway", std::string(gateway));
620 ipmi::setDbusProperty(
621 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
622 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530623 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700624 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500625
Patrick Venture0b02be92018-08-31 11:55:55 -0700626 log<level::DEBUG>(
627 "Network configuration changed",
628 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500629 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530630 catch (InternalFailure& e)
631 {
632 commit<InternalFailure>();
633 return -1;
634 }
635
636 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500637}
638
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500639uint32_t getPOHCounter()
640{
641 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
642
Patrick Venture0b02be92018-08-31 11:55:55 -0700643 auto chassisStateObj =
644 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500645
Patrick Venture0b02be92018-08-31 11:55:55 -0700646 auto service =
647 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500648
Patrick Venture0b02be92018-08-31 11:55:55 -0700649 auto propValue =
650 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
651 chassisPOHStateIntf, pOHCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500652
William A. Kennington III4c008022018-10-12 17:18:14 -0700653 return variant_ns::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500654}
655
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500656ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
657 ipmi_request_t request,
658 ipmi_response_t response,
659 ipmi_data_len_t data_len,
660 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500661{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500662 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800663 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500664 *data_len = 0;
665 return rc;
666}
667
Nan Li8d15fb42016-08-16 22:29:40 +0800668ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700669 ipmi_request_t request,
670 ipmi_response_t response,
671 ipmi_data_len_t data_len,
672 ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800673{
674 // sd_bus error
675 ipmi_ret_t rc = IPMI_CC_OK;
676
677 ipmi_chassis_cap_t chassis_cap{};
678
Yong Liae4b0402018-11-02 11:12:14 +0800679 if (*data_len != 0)
680 {
681 return IPMI_CC_REQ_DATA_LEN_INVALID;
682 }
683
Nan Li8d15fb42016-08-16 22:29:40 +0800684 *data_len = sizeof(ipmi_chassis_cap_t);
685
Yong Liae4b0402018-11-02 11:12:14 +0800686 try
687 {
688 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800689
Yong Liae4b0402018-11-02 11:12:14 +0800690 ipmi::DbusObjectInfo chassisCapObject =
691 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800692
Yong Liae4b0402018-11-02 11:12:14 +0800693 // capabilities flags
694 // [7..4] - reserved
695 // [3] – 1b = provides power interlock (IPM 1.5)
696 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
697 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
698 // has capabilities
699 // to lock out external power control and reset button or
700 // front panel interfaces and/or detect tampering with those
701 // interfaces).
702 // [0] -1b = Chassis provides intrusion (physical security) sensor.
703 // set to default value 0x0.
704 ipmi::Value variant = ipmi::getDbusProperty(
705 bus, chassisCapObject.second, chassisCapObject.first,
706 chassisCapIntf, chassisCapFlagsProp);
707 chassis_cap.cap_flags = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800708
Yong Liae4b0402018-11-02 11:12:14 +0800709 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
710 chassisCapObject.first, chassisCapIntf,
711 chassisFRUDevAddrProp);
712 // Chassis FRU info Device Address.
713 chassis_cap.fru_info_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800714
Yong Liae4b0402018-11-02 11:12:14 +0800715 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
716 chassisCapObject.first, chassisCapIntf,
717 chassisSDRDevAddrProp);
718 // Chassis SDR Device Address.
719 chassis_cap.sdr_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800720
Yong Liae4b0402018-11-02 11:12:14 +0800721 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
722 chassisCapObject.first, chassisCapIntf,
723 chassisSELDevAddrProp);
724 // Chassis SEL Device Address.
725 chassis_cap.sel_dev_addr = variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800726
Yong Liae4b0402018-11-02 11:12:14 +0800727 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
728 chassisCapObject.first, chassisCapIntf,
729 chassisSMDevAddrProp);
730 // Chassis System Management Device Address.
731 chassis_cap.system_management_dev_addr =
732 variant_ns::get<uint8_t>(variant);
Nan Li8d15fb42016-08-16 22:29:40 +0800733
Yong Liae4b0402018-11-02 11:12:14 +0800734 variant = ipmi::getDbusProperty(bus, chassisCapObject.second,
735 chassisCapObject.first, chassisCapIntf,
736 chassisBridgeDevAddrProp);
737 // Chassis Bridge Device Address.
738 chassis_cap.bridge_dev_addr = variant_ns::get<uint8_t>(variant);
739 uint8_t* respP = reinterpret_cast<uint8_t*>(response);
740 uint8_t* chassisP = reinterpret_cast<uint8_t*>(&chassis_cap);
741 std::copy(chassisP, chassisP + *data_len, respP);
742 }
743 catch (std::exception& e)
744 {
745 log<level::ERR>(e.what());
746 rc = IPMI_CC_UNSPECIFIED_ERROR;
747 *data_len = 0;
748 return rc;
749 }
750
751 return rc;
752}
753
754ipmi_ret_t ipmi_set_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
755 ipmi_request_t request,
756 ipmi_response_t response,
757 ipmi_data_len_t data_len,
758 ipmi_context_t context)
759{
760 ipmi_ret_t rc = IPMI_CC_OK;
761
762 if (*data_len != sizeof(ipmi_chassis_cap_t))
763 {
764 log<level::ERR>("Unsupported request length",
765 entry("LEN=0x%x", *data_len));
766 *data_len = 0;
767 return IPMI_CC_REQ_DATA_LEN_INVALID;
768 }
769
770 ipmi_chassis_cap_t* chassisCap = static_cast<ipmi_chassis_cap_t*>(request);
771
772 *data_len = 0;
773
774 // check input data
775 if (0 != (chassisCap->cap_flags & ~chassisCapFlagMask))
776 {
777 log<level::ERR>("Unsupported request parameter(CAP Flags)",
778 entry("REQ=0x%x", chassisCap->cap_flags));
779 return IPMI_CC_INVALID_FIELD_REQUEST;
780 }
781
782 if (0 != (chassisCap->fru_info_dev_addr & ~chassisCapAddrMask))
783 {
784 log<level::ERR>("Unsupported request parameter(FRU Addr)",
785 entry("REQ=0x%x", chassisCap->fru_info_dev_addr));
786 return IPMI_CC_INVALID_FIELD_REQUEST;
787 }
788
789 if (0 != (chassisCap->sdr_dev_addr & ~chassisCapAddrMask))
790 {
791 log<level::ERR>("Unsupported request parameter(SDR Addr)",
792 entry("REQ=0x%x", chassisCap->sdr_dev_addr));
793 return IPMI_CC_INVALID_FIELD_REQUEST;
794 }
795
796 if (0 != (chassisCap->sel_dev_addr & ~chassisCapAddrMask))
797 {
798 log<level::ERR>("Unsupported request parameter(SEL Addr)",
799 entry("REQ=0x%x", chassisCap->sel_dev_addr));
800 return IPMI_CC_INVALID_FIELD_REQUEST;
801 }
802
803 if (0 != (chassisCap->system_management_dev_addr & ~chassisCapAddrMask))
804 {
805 log<level::ERR>(
806 "Unsupported request parameter(SM Addr)",
807 entry("REQ=0x%x", chassisCap->system_management_dev_addr));
808 return IPMI_CC_INVALID_FIELD_REQUEST;
809 }
810
811 if (0 != (chassisCap->bridge_dev_addr & ~chassisCapAddrMask))
812 {
813 log<level::ERR>("Unsupported request parameter(Bridge Addr)",
814 entry("REQ=0x%x", chassisCap->bridge_dev_addr));
815 return IPMI_CC_INVALID_FIELD_REQUEST;
816 }
817
818 try
819 {
820 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
821 ipmi::DbusObjectInfo chassisCapObject =
822 ipmi::getDbusObject(bus, chassisCapIntf);
823
824 ipmi::setDbusProperty(bus, chassisCapObject.second,
825 chassisCapObject.first, chassisCapIntf,
826 chassisCapFlagsProp, chassisCap->cap_flags);
827
828 ipmi::setDbusProperty(bus, chassisCapObject.second,
829 chassisCapObject.first, chassisCapIntf,
830 chassisFRUDevAddrProp,
831 chassisCap->fru_info_dev_addr);
832
833 ipmi::setDbusProperty(bus, chassisCapObject.second,
834 chassisCapObject.first, chassisCapIntf,
835 chassisSDRDevAddrProp, chassisCap->sdr_dev_addr);
836
837 ipmi::setDbusProperty(bus, chassisCapObject.second,
838 chassisCapObject.first, chassisCapIntf,
839 chassisSELDevAddrProp, chassisCap->sel_dev_addr);
840
841 ipmi::setDbusProperty(bus, chassisCapObject.second,
842 chassisCapObject.first, chassisCapIntf,
843 chassisSMDevAddrProp,
844 chassisCap->system_management_dev_addr);
845
846 ipmi::setDbusProperty(bus, chassisCapObject.second,
847 chassisCapObject.first, chassisCapIntf,
848 chassisBridgeDevAddrProp,
849 chassisCap->bridge_dev_addr);
850 }
851 catch (std::exception& e)
852 {
853 log<level::ERR>(e.what());
854 rc = IPMI_CC_UNSPECIFIED_ERROR;
855 return rc;
856 }
Nan Li8d15fb42016-08-16 22:29:40 +0800857
858 return rc;
859}
860
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530861//------------------------------------------
862// Calls into Host State Manager Dbus object
863//------------------------------------------
864int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600865{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500866 // OpenBMC Host State Manager dbus framework
Patrick Venture0b02be92018-08-31 11:55:55 -0700867 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500868 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
Patrick Venture0b02be92018-08-31 11:55:55 -0700869 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
870 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530871
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500872 // sd_bus error
873 int rc = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700874 char* busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600875
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500876 // SD Bus error report mechanism.
877 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600878
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500879 // Gets a hook onto either a SYSTEM or SESSION bus
Patrick Venture0b02be92018-08-31 11:55:55 -0700880 sd_bus* bus_type = ipmid_get_sd_bus_connection();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500881 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
882 if (rc < 0)
883 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700884 log<level::ERR>(
885 "Failed to get bus name",
886 entry("ERRNO=0x%X, OBJPATH=%s", -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500887 return rc;
888 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530889
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500890 // Convert to string equivalent of the passed in transition enum.
891 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530892
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500893 rc = sd_bus_call_method(bus_type, // On the system bus
894 busname, // Service to contact
895 HOST_STATE_MANAGER_ROOT, // Object path
896 DBUS_PROPERTY_IFACE, // Interface name
897 "Set", // Method to be called
898 &bus_error, // object to return error
899 nullptr, // Response buffer if any
900 "ssv", // Takes 3 arguments
Patrick Venture0b02be92018-08-31 11:55:55 -0700901 HOST_STATE_MANAGER_IFACE, PROPERTY, "s",
902 request.c_str());
903 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500904 {
905 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530906 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500907 }
908 else
909 {
910 log<level::INFO>("Transition request initiated successfully");
911 }
vishwa36993272015-11-20 12:43:49 -0600912
913 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500914 free(busname);
vishwa36993272015-11-20 12:43:49 -0600915
Sergey Solomineb9b8142016-08-23 09:07:28 -0500916 return rc;
vishwa36993272015-11-20 12:43:49 -0600917}
918
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500919namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500920{
Nan Lifdd8ec52016-08-28 03:57:40 +0800921
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500922using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
923using IpmiValue = uint8_t;
924using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800925
Patrick Venture0b02be92018-08-31 11:55:55 -0700926std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500927 {RestorePolicy::Policy::AlwaysOff, 0x00},
928 {RestorePolicy::Policy::Restore, 0x01},
Patrick Venture0b02be92018-08-31 11:55:55 -0700929 {RestorePolicy::Policy::AlwaysOn, 0x02}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800930
Yong Lic6713cf2018-09-12 12:35:13 +0800931static constexpr uint8_t noChange = 0x03;
932static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
933static constexpr uint8_t policyBitMask = 0x07;
934static constexpr uint8_t setPolicyReqLen = 1;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500935} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800936
937//----------------------------------------------------------------------
938// Get Chassis Status commands
939//----------------------------------------------------------------------
940ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500941 ipmi_request_t request,
942 ipmi_response_t response,
943 ipmi_data_len_t data_len,
944 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800945{
Patrick Venture0b02be92018-08-31 11:55:55 -0700946 const char* objname = "/org/openbmc/control/power0";
947 const char* intf = "org.openbmc.control.Power";
Nan Lifdd8ec52016-08-28 03:57:40 +0800948
Patrick Venture0b02be92018-08-31 11:55:55 -0700949 sd_bus* bus = NULL;
950 sd_bus_message* reply = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800951 int r = 0;
952 int pgood = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700953 char* busname = NULL;
Nan Lifdd8ec52016-08-28 03:57:40 +0800954 ipmi_ret_t rc = IPMI_CC_OK;
955 ipmi_get_chassis_status_t chassis_status{};
956
Nan Lifdd8ec52016-08-28 03:57:40 +0800957 uint8_t s = 0;
958
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500959 using namespace chassis::internal;
960 using namespace chassis::internal::cache;
961 using namespace power_policy;
962
Deepak Kodihallie6027092017-08-27 08:13:37 -0500963 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Patrick Venture0b02be92018-08-31 11:55:55 -0700964 auto method = dbus.new_method_call(
965 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
966 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500967 method.append(powerRestoreIntf, "PowerRestorePolicy");
968 auto resp = dbus.call(method);
969 if (resp.is_method_error())
970 {
971 log<level::ERR>("Error in PowerRestorePolicy Get");
972 report<InternalFailure>();
973 *data_len = 0;
974 return IPMI_CC_UNSPECIFIED_ERROR;
975 }
976 sdbusplus::message::variant<std::string> result;
977 resp.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -0700978 auto powerRestore = RestorePolicy::convertPolicyFromString(
979 variant_ns::get<std::string>(result));
Nan Lifdd8ec52016-08-28 03:57:40 +0800980
981 *data_len = 4;
982
Tom Joseph63a00512017-08-09 23:39:59 +0530983 bus = ipmid_get_sd_bus_connection();
984
Nan Lifdd8ec52016-08-28 03:57:40 +0800985 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700986 if (r < 0)
987 {
988 log<level::ERR>("Failed to get bus name", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800989 rc = IPMI_CC_UNSPECIFIED_ERROR;
990 goto finish;
991 }
992
Patrick Venture0b02be92018-08-31 11:55:55 -0700993 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply,
994 "i");
995 if (r < 0)
996 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530997 log<level::ERR>("Failed to call sd_bus_get_property",
Patrick Venture0b02be92018-08-31 11:55:55 -0700998 entry("PROPERTY=%s", "pgood"), entry("ERRNO=0x%X", -r),
999 entry("BUS=%s", busname), entry("PATH=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301000 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +08001001 rc = IPMI_CC_UNSPECIFIED_ERROR;
1002 goto finish;
1003 }
1004
1005 r = sd_bus_message_read(reply, "i", &pgood);
Patrick Venture0b02be92018-08-31 11:55:55 -07001006 if (r < 0)
1007 {
1008 log<level::ERR>("Failed to read sensor:", entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +08001009 rc = IPMI_CC_UNSPECIFIED_ERROR;
1010 goto finish;
1011 }
1012
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001013 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +08001014
1015 // Current Power State
1016 // [7] reserved
1017 // [6..5] power restore policy
1018 // 00b = chassis stays powered off after AC/mains returns
1019 // 01b = after AC returns, power is restored to the state that was
1020 // in effect when AC/mains was lost.
1021 // 10b = chassis always powers up after AC/mains returns
1022 // 11b = unknow
1023 // Set to 00b, by observing the hardware behavior.
Patrick Venture0b02be92018-08-31 11:55:55 -07001024 // Do we need to define a dbus property to identify the restore
1025 // policy?
Nan Lifdd8ec52016-08-28 03:57:40 +08001026
1027 // [4] power control fault
1028 // 1b = controller attempted to turn system power on or off, but
1029 // system did not enter desired state.
1030 // Set to 0b, since We don't support it..
1031
1032 // [3] power fault
1033 // 1b = fault detected in main power subsystem.
1034 // set to 0b. for we don't support it.
1035
1036 // [2] 1b = interlock (chassis is presently shut down because a chassis
1037 // panel interlock switch is active). (IPMI 1.5)
1038 // set to 0b, for we don't support it.
1039
1040 // [1] power overload
1041 // 1b = system shutdown because of power overload condition.
1042 // set to 0b, for we don't support it.
1043
1044 // [0] power is on
1045 // 1b = system power is on
1046 // 0b = system power is off(soft-off S4/S5, or mechanical off)
1047
Patrick Venture0b02be92018-08-31 11:55:55 -07001048 chassis_status.cur_power_state = ((s & 0x3) << 5) | (pgood & 0x1);
Nan Lifdd8ec52016-08-28 03:57:40 +08001049
1050 // Last Power Event
1051 // [7..5] – reserved
1052 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
1053 // [3] – 1b = last power down caused by power fault
1054 // [2] – 1b = last power down caused by a power interlock being activated
1055 // [1] – 1b = last power down caused by a Power overload
1056 // [0] – 1b = AC failed
1057 // set to 0x0, for we don't support these fields.
1058
1059 chassis_status.last_power_event = 0;
1060
1061 // Misc. Chassis State
1062 // [7] – reserved
1063 // [6] – 1b = Chassis Identify command and state info supported (Optional)
1064 // 0b = Chassis Identify command support unspecified via this command.
1065 // (The Get Command Support command , if implemented, would still
1066 // indicate support for the Chassis Identify command)
Patrick Venture0b02be92018-08-31 11:55:55 -07001067 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved
1068 // (return
Nan Lifdd8ec52016-08-28 03:57:40 +08001069 // as 00b) otherwise. Returns the present chassis identify state.
1070 // Refer to the Chassis Identify command for more info.
1071 // 00b = chassis identify state = Off
1072 // 01b = chassis identify state = Temporary(timed) On
1073 // 10b = chassis identify state = Indefinite On
1074 // 11b = reserved
1075 // [3] – 1b = Cooling/fan fault detected
1076 // [2] – 1b = Drive Fault
1077 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
1078 // push-buttons disabled.)
1079 // [0] – 1b = Chassis Intrusion active
1080 // set to 0, for we don't support them.
1081 chassis_status.misc_power_state = 0;
1082
1083 // Front Panel Button Capabilities and disable/enable status(Optional)
1084 // set to 0, for we don't support them.
1085 chassis_status.front_panel_button_cap_status = 0;
1086
1087 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001088 std::memcpy(response, &chassis_status, *data_len);
Nan Lifdd8ec52016-08-28 03:57:40 +08001089
1090finish:
1091 free(busname);
1092 reply = sd_bus_message_unref(reply);
1093
1094 return rc;
1095}
Chris Austen7888c4d2015-12-03 15:26:20 -06001096
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301097//-------------------------------------------------------------
1098// Send a command to SoftPowerOff application to stop any timer
1099//-------------------------------------------------------------
1100int stop_soft_off_timer()
1101{
Patrick Venture0b02be92018-08-31 11:55:55 -07001102 constexpr auto iface = "org.freedesktop.DBus.Properties";
1103 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
1104 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301105
Patrick Venture0b02be92018-08-31 11:55:55 -07001106 constexpr auto property = "ResponseReceived";
1107 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
1108 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301109
1110 // Get the system bus where most system services are provided.
1111 auto bus = ipmid_get_sd_bus_connection();
1112
1113 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001114 // TODO openbmc/openbmc#1661 - Mapper refactor
1115 //
1116 // See openbmc/openbmc#1743 for some details but high level summary is that
1117 // for now the code will directly call the soft off interface due to a
1118 // race condition with mapper usage
1119 //
Patrick Venture0b02be92018-08-31 11:55:55 -07001120 // char *busname = nullptr;
1121 // auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
1122 // if (r < 0)
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001123 //{
1124 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301125 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001126 // return r;
1127 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301128
1129 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -05001130 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Patrick Venture0b02be92018-08-31 11:55:55 -07001131 "Set", nullptr, nullptr, "ssv", soft_off_iface,
1132 property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301133 if (rc < 0)
1134 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301135 log<level::ERR>("Failed to set property in SoftPowerOff object",
1136 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301137 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001138
Patrick Venture0b02be92018-08-31 11:55:55 -07001139 // TODO openbmc/openbmc#1661 - Mapper refactor
1140 // free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301141 return rc;
1142}
1143
vishwa36993272015-11-20 12:43:49 -06001144//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001145// Create file to indicate there is no need for softoff notification to host
1146//----------------------------------------------------------------------
1147void indicate_no_softoff_needed()
1148{
1149 fs::path path{HOST_INBAND_REQUEST_DIR};
1150 if (!fs::is_directory(path))
1151 {
1152 fs::create_directory(path);
1153 }
1154
1155 // Add the host instance (default 0 for now) to the file name
1156 std::string file{HOST_INBAND_REQUEST_FILE};
Patrick Venture0b02be92018-08-31 11:55:55 -07001157 auto size = std::snprintf(nullptr, 0, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001158 size++; // null
1159 std::unique_ptr<char[]> buf(new char[size]);
Patrick Venture0b02be92018-08-31 11:55:55 -07001160 std::snprintf(buf.get(), size, file.c_str(), 0);
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001161
1162 // Append file name to directory and create it
1163 path /= buf.get();
1164 std::ofstream(path.c_str());
1165}
1166
1167//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001168// Chassis Control commands
1169//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001170ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1171 ipmi_request_t request,
1172 ipmi_response_t response,
1173 ipmi_data_len_t data_len,
1174 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001175{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001176 // Error from power off.
1177 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001178
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001179 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001180 *data_len = 0;
1181
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001182 // Catch the actual operaton by peeking into request buffer
Patrick Venture0b02be92018-08-31 11:55:55 -07001183 uint8_t chassis_ctrl_cmd = *(uint8_t*)request;
vishwa36993272015-11-20 12:43:49 -06001184
Patrick Venture0b02be92018-08-31 11:55:55 -07001185 switch (chassis_ctrl_cmd)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001186 {
1187 case CMD_POWER_ON:
1188 rc = initiate_state_transition(State::Host::Transition::On);
1189 break;
1190 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301191 // This path would be hit in 2 conditions.
1192 // 1: When user asks for power off using ipmi chassis command 0x04
1193 // 2: Host asking for power off post shutting down.
1194
1195 // If it's a host requested power off, then need to nudge Softoff
1196 // application that it needs to stop the watchdog timer if running.
1197 // If it is a user requested power off, then this is not really
1198 // needed. But then we need to differentiate between user and host
1199 // calling this same command
1200
1201 // For now, we are going ahead with trying to nudge the soft off and
1202 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001203 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301204
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001205 // Only request the Off transition if the soft power off
1206 // application is not running
1207 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001208 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001209 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301210 // that it should not run. Not doing this will result in State
1211 // manager doing a default soft power off when asked for power
1212 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001213 indicate_no_softoff_needed();
1214
1215 // Now request the shutdown
1216 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001217 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001218 else
1219 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301220 log<level::INFO>("Soft off is running, so let shutdown target "
1221 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001222 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001223 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301224
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001225 case CMD_HARD_RESET:
1226 case CMD_POWER_CYCLE:
1227 // SPEC has a section that says certain implementations can trigger
1228 // PowerOn if power is Off when a command to power cycle is
1229 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001230
1231 // First create a file to indicate to the soft off application
1232 // that it should not run since this is a direct user initiated
1233 // power reboot request (i.e. a reboot request that is not
1234 // originating via a soft power off SMS request)
1235 indicate_no_softoff_needed();
1236
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001237 rc = initiate_state_transition(State::Host::Transition::Reboot);
1238 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301239
1240 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1241 // Request Host State Manager to do a soft power off
1242 rc = initiate_state_transition(State::Host::Transition::Off);
1243 break;
1244
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001245 default:
1246 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301247 log<level::ERR>("Invalid Chassis Control command",
1248 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001249 rc = -1;
1250 }
1251 }
vishwa36993272015-11-20 12:43:49 -06001252
Patrick Venture0b02be92018-08-31 11:55:55 -07001253 return ((rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001254}
1255
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001256/** @brief Return D-Bus connection string to enclosure identify LED object
1257 *
1258 * @param[in, out] connection - connection to D-Bus object
1259 * @return a IPMI return code
1260 */
1261std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301262{
Tom Joseph5110c122018-03-23 17:55:40 +05301263 // lookup enclosure_identify group owner(s) in mapper
1264 auto mapperCall = chassis::internal::dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001265 ipmi::MAPPER_BUS_NAME, ipmi::MAPPER_OBJ, ipmi::MAPPER_INTF,
1266 "GetObject");
Tom Joseph5110c122018-03-23 17:55:40 +05301267
1268 mapperCall.append(identify_led_object_name);
Patrick Venture0b02be92018-08-31 11:55:55 -07001269 static const std::vector<std::string> interfaces = {
1270 "xyz.openbmc_project.Led.Group"};
Tom Joseph5110c122018-03-23 17:55:40 +05301271 mapperCall.append(interfaces);
1272 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1273 if (mapperReply.is_method_error())
1274 {
1275 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001276 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301277 }
1278 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1279 mapperReply.read(mapperResp);
1280
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001281 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301282 {
Patrick Venture0b02be92018-08-31 11:55:55 -07001283 log<level::ERR>(
1284 "Invalid number of enclosure identify objects.",
1285 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001286 elog<InternalFailure>();
1287 }
1288 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1289 return pair.first;
1290}
Tom Joseph5110c122018-03-23 17:55:40 +05301291
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001292/** @brief Turn On/Off enclosure identify LED
1293 *
1294 * @param[in] flag - true to turn on LED, false to turn off
1295 * @return a IPMI return code
1296 */
1297void enclosureIdentifyLed(bool flag)
1298{
1299 using namespace chassis::internal;
1300 std::string connection = std::move(getEnclosureIdentifyConnection());
Patrick Venture0b02be92018-08-31 11:55:55 -07001301 auto led =
1302 dbus.new_method_call(connection.c_str(), identify_led_object_name,
1303 "org.freedesktop.DBus.Properties", "Set");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001304 led.append("xyz.openbmc_project.Led.Group", "Asserted",
Patrick Venture0b02be92018-08-31 11:55:55 -07001305 sdbusplus::message::variant<bool>(flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001306 auto ledReply = dbus.call(led);
1307 if (ledReply.is_method_error())
1308 {
1309 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
Patrick Venture0b02be92018-08-31 11:55:55 -07001310 entry("LED_STATE=%d", flag));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001311 elog<InternalFailure>();
1312 }
1313}
1314
1315/** @brief Callback method to turn off LED
1316 */
1317void enclosureIdentifyLedOff()
1318{
1319 try
1320 {
1321 enclosureIdentifyLed(false);
1322 }
1323 catch (const InternalFailure& e)
1324 {
1325 report<InternalFailure>();
1326 }
1327}
1328
1329/** @brief Create timer to turn on and off the enclosure LED
1330 */
1331void createIdentifyTimer()
1332{
1333 if (!identifyTimer)
1334 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001335 identifyTimer =
1336 std::make_unique<phosphor::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001337 }
1338}
1339
1340ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1341 ipmi_request_t request,
1342 ipmi_response_t response,
1343 ipmi_data_len_t data_len,
1344 ipmi_context_t context)
1345{
1346 if (*data_len > chassisIdentifyReqLength)
1347 {
1348 return IPMI_CC_REQ_DATA_LEN_INVALID;
1349 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001350 uint8_t identifyInterval =
1351 *data_len > identifyIntervalPos
1352 ? (static_cast<uint8_t*>(request))[identifyIntervalPos]
1353 : DEFAULT_IDENTIFY_TIME_OUT;
1354 bool forceIdentify =
1355 (*data_len == chassisIdentifyReqLength)
1356 ? (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01
1357 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301358
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001359 if (identifyInterval || forceIdentify)
1360 {
1361 // stop the timer if already started, for force identify we should
1362 // not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001363 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001364 try
Tom Joseph5110c122018-03-23 17:55:40 +05301365 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001366 enclosureIdentifyLed(true);
1367 }
1368 catch (const InternalFailure& e)
1369 {
1370 report<InternalFailure>();
1371 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301372 }
1373
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001374 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301375 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001376 return IPMI_CC_OK;
1377 }
1378 // start the timer
1379 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001380 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001381 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301382 }
Tom Josephbed26992018-07-31 23:00:24 +05301383 else if (!identifyInterval)
1384 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001385 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301386 enclosureIdentifyLedOff();
1387 }
Tom Joseph5110c122018-03-23 17:55:40 +05301388 return IPMI_CC_OK;
1389}
1390
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001391namespace boot_options
1392{
1393
1394using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1395using IpmiValue = uint8_t;
1396constexpr auto ipmiDefault = 0;
1397
Patrick Venture0b02be92018-08-31 11:55:55 -07001398std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001399 {0x01, Source::Sources::Network},
1400 {0x02, Source::Sources::Disk},
1401 {0x05, Source::Sources::ExternalMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001402 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001403
Patrick Venture0b02be92018-08-31 11:55:55 -07001404std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001405 {0x03, Mode::Modes::Safe},
1406 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001407 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001408
Patrick Venture0b02be92018-08-31 11:55:55 -07001409std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001410 {Source::Sources::Network, 0x01},
1411 {Source::Sources::Disk, 0x02},
1412 {Source::Sources::ExternalMedia, 0x05},
Patrick Venture0b02be92018-08-31 11:55:55 -07001413 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001414
Patrick Venture0b02be92018-08-31 11:55:55 -07001415std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001416 {Mode::Modes::Safe, 0x03},
1417 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001418 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001419
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001420} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001421
Marri Devender Rao81719702018-05-07 00:53:48 -05001422/** @brief Set the property value for boot source
1423 * @param[in] source - boot source value
1424 * @return On failure return IPMI error.
1425 */
1426static ipmi_ret_t setBootSource(const Source::Sources& source)
1427{
1428 using namespace chassis::internal;
1429 using namespace chassis::internal::cache;
1430 sdbusplus::message::variant<std::string> property =
1431 convertForMessage(source);
1432 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1433 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001434 auto method = dbus.new_method_call(
1435 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1436 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001437 method.append(bootSourceIntf, "BootSource", property);
1438 auto reply = dbus.call(method);
1439 if (reply.is_method_error())
1440 {
1441 log<level::ERR>("Error in BootSource Set");
1442 report<InternalFailure>();
1443 return IPMI_CC_UNSPECIFIED_ERROR;
1444 }
1445 return IPMI_CC_OK;
1446}
1447
Patrick Venture0b02be92018-08-31 11:55:55 -07001448/** @brief Set the property value for boot mode
Marri Devender Rao81719702018-05-07 00:53:48 -05001449 * @param[in] mode - boot mode value
1450 * @return On failure return IPMI error.
1451 */
1452static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1453{
1454 using namespace chassis::internal;
1455 using namespace chassis::internal::cache;
Patrick Venture0b02be92018-08-31 11:55:55 -07001456 sdbusplus::message::variant<std::string> property = convertForMessage(mode);
Marri Devender Rao81719702018-05-07 00:53:48 -05001457 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1458 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001459 auto method = dbus.new_method_call(
1460 objects.service(bootModeSetting, bootModeIntf).c_str(),
1461 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
Marri Devender Rao81719702018-05-07 00:53:48 -05001462 method.append(bootModeIntf, "BootMode", property);
1463 auto reply = dbus.call(method);
1464 if (reply.is_method_error())
1465 {
1466 log<level::ERR>("Error in BootMode Set");
1467 report<InternalFailure>();
1468 return IPMI_CC_UNSPECIFIED_ERROR;
1469 }
1470 return IPMI_CC_OK;
1471}
1472
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001473ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1474 ipmi_request_t request,
1475 ipmi_response_t response,
1476 ipmi_data_len_t data_len,
1477 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001478{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001479 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001480 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
Patrick Venture0b02be92018-08-31 11:55:55 -07001481 char* p = NULL;
1482 get_sys_boot_options_response_t* resp =
1483 (get_sys_boot_options_response_t*)response;
1484 get_sys_boot_options_t* reqptr = (get_sys_boot_options_t*)request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001485 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001486
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001487 std::memset(resp, 0, sizeof(*resp));
Patrick Venture0b02be92018-08-31 11:55:55 -07001488 resp->version = SET_PARM_VERSION;
1489 resp->parm = 5;
1490 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001491
shgoupfd84fbbf2015-12-17 10:05:51 +08001492 /*
1493 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1494 * This is the only parameter used by petitboot.
1495 */
Patrick Venture0b02be92018-08-31 11:55:55 -07001496 if (reqptr->parameter ==
1497 static_cast<uint8_t>(BootOptionParameter::BOOT_FLAGS))
1498 {
shgoupfd84fbbf2015-12-17 10:05:51 +08001499
Ratan Guptafd28dd72016-08-01 04:58:01 -05001500 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001501 using namespace chassis::internal;
1502 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001503
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001504 try
ratagupta6f6bff2016-04-04 06:20:11 -05001505 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001506 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1507 const auto& bootSourceSetting =
1508 std::get<settings::Path>(bootSetting);
1509 auto oneTimeEnabled =
1510 std::get<settings::boot::OneTimeEnabled>(bootSetting);
Patrick Venture0b02be92018-08-31 11:55:55 -07001511 auto method = dbus.new_method_call(
1512 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1513 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001514 method.append(bootSourceIntf, "BootSource");
1515 auto reply = dbus.call(method);
1516 if (reply.is_method_error())
1517 {
1518 log<level::ERR>("Error in BootSource Get");
1519 report<InternalFailure>();
1520 *data_len = 0;
1521 return IPMI_CC_UNSPECIFIED_ERROR;
1522 }
1523 sdbusplus::message::variant<std::string> result;
1524 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001525 auto bootSource = Source::convertSourcesFromString(
1526 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001527
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001528 bootSetting = settings::boot::setting(objects, bootModeIntf);
1529 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1530 method = dbus.new_method_call(
Patrick Venture0b02be92018-08-31 11:55:55 -07001531 objects.service(bootModeSetting, bootModeIntf).c_str(),
1532 bootModeSetting.c_str(), ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001533 method.append(bootModeIntf, "BootMode");
1534 reply = dbus.call(method);
1535 if (reply.is_method_error())
1536 {
1537 log<level::ERR>("Error in BootMode Get");
1538 report<InternalFailure>();
1539 *data_len = 0;
1540 return IPMI_CC_UNSPECIFIED_ERROR;
1541 }
1542 reply.read(result);
William A. Kennington III4c008022018-10-12 17:18:14 -07001543 auto bootMode = Mode::convertModesFromString(
1544 variant_ns::get<std::string>(result));
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001545
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001546 bootOption = sourceDbusToIpmi.at(bootSource);
1547 if ((Mode::Modes::Regular == bootMode) &&
1548 (Source::Sources::Default == bootSource))
1549 {
1550 bootOption = ipmiDefault;
1551 }
1552 else if (Source::Sources::Default == bootSource)
1553 {
1554 bootOption = modeDbusToIpmi.at(bootMode);
1555 }
1556 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001557
Patrick Venture0b02be92018-08-31 11:55:55 -07001558 resp->data[0] = oneTimeEnabled
1559 ? SET_PARM_BOOT_FLAGS_VALID_ONE_TIME
1560 : SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001561
ratagupta6f6bff2016-04-04 06:20:11 -05001562 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001563 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001564 catch (InternalFailure& e)
1565 {
1566 report<InternalFailure>();
1567 *data_len = 0;
1568 return IPMI_CC_UNSPECIFIED_ERROR;
1569 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001570 }
1571 else if (reqptr->parameter ==
1572 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS))
1573 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001574
Patrick Venture0b02be92018-08-31 11:55:55 -07001575 *data_len =
1576 static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001577
Patrick Venture0b02be92018-08-31 11:55:55 -07001578 resp->parm =
1579 static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001580
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001581 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001582
Patrick Venture0b02be92018-08-31 11:55:55 -07001583 if (ret < 0)
1584 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001585
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301586 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001587 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001588 rc = IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture0b02be92018-08-31 11:55:55 -07001589 }
1590 else
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001591 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001592 }
1593
Patrick Venture0b02be92018-08-31 11:55:55 -07001594 else
1595 {
1596 log<level::ERR>("Unsupported parameter",
1597 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001598 }
1599
1600 if (p)
1601 free(p);
1602
Ratan Guptafd28dd72016-08-01 04:58:01 -05001603 if (rc == IPMI_CC_OK)
1604 {
1605 *data_len += 2;
1606 }
1607
shgoupfd84fbbf2015-12-17 10:05:51 +08001608 return rc;
1609}
1610
shgoupfd84fbbf2015-12-17 10:05:51 +08001611ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001612 ipmi_request_t request,
1613 ipmi_response_t response,
1614 ipmi_data_len_t data_len,
1615 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001616{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001617 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001618 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001619 set_sys_boot_options_t* reqptr = (set_sys_boot_options_t*)request;
shgoupfd84fbbf2015-12-17 10:05:51 +08001620
Patrick Ventureb51bf9c2018-09-10 15:53:14 -07001621 std::printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",
1622 reqptr->parameter);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001623
shgoupfd84fbbf2015-12-17 10:05:51 +08001624 // This IPMI command does not have any resposne data
1625 *data_len = 0;
1626
1627 /* 000101
1628 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1629 * This is the only parameter used by petitboot.
1630 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001631
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001632 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1633 {
1634 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1635 using namespace chassis::internal;
1636 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001637 auto oneTimeEnabled = false;
1638 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301639 constexpr auto oneTimePath =
Patrick Venture0b02be92018-08-31 11:55:55 -07001640 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001641
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001642 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001643 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001644 bool permanent =
1645 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1646 SET_PARM_BOOT_FLAGS_PERMANENT;
1647
Patrick Venture0b02be92018-08-31 11:55:55 -07001648 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301649
1650 oneTimeEnabled =
1651 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1652
1653 /*
1654 * Check if the current boot setting is onetime or permanent, if the
1655 * request in the command is otherwise, then set the "Enabled"
1656 * property in one_time object path to 'True' to indicate onetime
1657 * and 'False' to indicate permanent.
1658 *
1659 * Once the onetime/permanent setting is applied, then the bootMode
1660 * and bootSource is updated for the corresponding object.
1661 */
1662 if ((permanent && oneTimeEnabled) ||
1663 (!permanent && !oneTimeEnabled))
1664 {
1665 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1666
Patrick Venture0b02be92018-08-31 11:55:55 -07001667 ipmi::setDbusProperty(dbus, service, oneTimePath, enabledIntf,
1668 "Enabled", !permanent);
Tom Joseph57e8eb72017-09-25 18:05:02 +05301669 }
1670
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001671 auto modeItr = modeIpmiToDbus.find(bootOption);
1672 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1673 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001674 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001675 rc = setBootSource(sourceItr->second);
1676 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001677 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001678 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001679 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001680 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001681 // If a set boot device is mapping to a boot source, then reset
1682 // the boot mode D-Bus property to default.
1683 // This way the ipmid code can determine which property is not
1684 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001685 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001686 {
1687 setBootMode(Mode::Modes::Regular);
1688 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001689 }
1690 if (modeIpmiToDbus.end() != modeItr)
1691 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001692 rc = setBootMode(modeItr->second);
1693 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001694 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001695 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001696 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001697 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001698 // If a set boot device is mapping to a boot mode, then reset
1699 // the boot source D-Bus property to default.
1700 // This way the ipmid code can determine which property is not
1701 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07001702 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001703 {
1704 setBootSource(Source::Sources::Default);
1705 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001706 }
1707 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001708 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001709 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001710 report<InternalFailure>();
1711 *data_len = 0;
1712 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001713 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001714 }
1715 else if (reqptr->parameter ==
1716 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS)
1717 {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001718
1719 int ret = setHostNetworkData(reqptr);
Patrick Venture0b02be92018-08-31 11:55:55 -07001720 if (ret < 0)
1721 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301722 log<level::ERR>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001723 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001724 rc = IPMI_CC_UNSPECIFIED_ERROR;
1725 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001726 }
1727 else if (reqptr->parameter ==
1728 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO))
1729 {
Tom Josephf536c902017-09-25 18:08:15 +05301730 // Handle parameter #4 and return command completed normally
1731 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1732 // parameter. This is added to support the ipmitool command `chassis
1733 // bootdev` which sends set on parameter #4, before setting the boot
1734 // flags.
1735 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001736 }
1737 else
1738 {
1739 log<level::ERR>("Unsupported parameter",
1740 entry("PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001741 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001742 }
1743
1744 return rc;
1745}
1746
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001747ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1748 ipmi_request_t request, ipmi_response_t response,
1749 ipmi_data_len_t data_len, ipmi_context_t context)
1750{
1751 // sd_bus error
1752 ipmi_ret_t rc = IPMI_CC_OK;
1753
1754 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1755
1756 try
1757 {
1758 auto pohCounter = getPOHCounter();
1759 resptr->counterReading[0] = pohCounter;
1760 resptr->counterReading[1] = pohCounter >> 8;
1761 resptr->counterReading[2] = pohCounter >> 16;
1762 resptr->counterReading[3] = pohCounter >> 24;
1763 }
1764 catch (std::exception& e)
1765 {
1766 log<level::ERR>(e.what());
1767 return IPMI_CC_UNSPECIFIED_ERROR;
1768 }
1769
1770 resptr->minPerCount = poh::minutesPerCount;
1771 *data_len = sizeof(GetPOHCountResponse);
1772
1773 return rc;
1774}
1775
Yong Lic6713cf2018-09-12 12:35:13 +08001776ipmi_ret_t ipmi_chassis_set_power_restore_policy(
1777 ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
1778 ipmi_response_t response, ipmi_data_len_t data_len, ipmi_context_t context)
1779{
1780 auto* reqptr = reinterpret_cast<uint8_t*>(request);
1781 auto* resptr = reinterpret_cast<uint8_t*>(response);
1782 uint8_t reqPolicy = 0;
1783
1784 power_policy::DbusValue value =
1785 power_policy::RestorePolicy::Policy::AlwaysOff;
1786
1787 if (*data_len != power_policy::setPolicyReqLen)
1788 {
1789 phosphor::logging::log<level::ERR>("Unsupported request length",
1790 entry("LEN=0x%x", *data_len));
1791 *data_len = 0;
1792 return IPMI_CC_REQ_DATA_LEN_INVALID;
1793 }
1794
1795 reqPolicy = *reqptr & power_policy::policyBitMask;
1796 if (reqPolicy > power_policy::noChange)
1797 {
1798 phosphor::logging::log<level::ERR>("Reserved request parameter",
1799 entry("REQ=0x%x", reqPolicy));
1800 *data_len = 0;
1801 return IPMI_CC_PARM_NOT_SUPPORTED;
1802 }
1803
1804 if (reqPolicy == power_policy::noChange)
1805 {
1806 // just return the supported policy
1807 *resptr = power_policy::allSupport;
1808 *data_len = power_policy::setPolicyReqLen;
1809 return IPMI_CC_OK;
1810 }
1811
1812 for (auto const& it : power_policy::dbusToIpmi)
1813 {
1814 if (it.second == reqPolicy)
1815 {
1816 value = it.first;
1817 break;
1818 }
1819 }
1820
1821 try
1822 {
1823 const settings::Path& powerRestoreSetting =
1824 chassis::internal::cache::objects.map
1825 .at(chassis::internal::powerRestoreIntf)
1826 .front();
1827 sdbusplus::message::variant<std::string> property =
1828 convertForMessage(value);
1829
1830 auto method = chassis::internal::dbus.new_method_call(
1831 chassis::internal::cache::objects
1832 .service(powerRestoreSetting,
1833 chassis::internal::powerRestoreIntf)
1834 .c_str(),
1835 powerRestoreSetting.c_str(), ipmi::PROP_INTF, "Set");
1836
1837 method.append(chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
1838 property);
1839 auto reply = chassis::internal::dbus.call(method);
1840 if (reply.is_method_error())
1841 {
1842 phosphor::logging::log<level::ERR>("Unspecified Error");
1843 *data_len = 0;
1844 return IPMI_CC_UNSPECIFIED_ERROR;
1845 }
1846 }
1847 catch (InternalFailure& e)
1848 {
1849 report<InternalFailure>();
1850 *data_len = 0;
1851 return IPMI_CC_UNSPECIFIED_ERROR;
1852 }
1853
1854 *data_len = power_policy::setPolicyReqLen;
1855 return IPMI_CC_OK;
1856}
1857
Adriana Kobylak40814c62015-10-27 15:58:44 -05001858void register_netfn_chassis_functions()
1859{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001860 createIdentifyTimer();
1861
Tom05732372016-09-06 17:21:23 +05301862 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001863 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL,
1864 ipmi_chassis_wildcard, PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001865
Tom05732372016-09-06 17:21:23 +05301866 // Get Chassis Capabilities
Patrick Venture0b02be92018-08-31 11:55:55 -07001867 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL,
1868 ipmi_get_chassis_cap, PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001869
Yong Liae4b0402018-11-02 11:12:14 +08001870 // Set Chassis Capabilities
1871 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_CHASSIS_CAP, NULL,
1872 ipmi_set_chassis_cap, PRIVILEGE_USER);
1873
Tom05732372016-09-06 17:21:23 +05301874 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301875 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001876 ipmi_chassis_get_sys_boot_options,
1877 PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001878
Tom05732372016-09-06 17:21:23 +05301879 // <Get Chassis Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001880 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL,
1881 ipmi_get_chassis_status, PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001882
Tom05732372016-09-06 17:21:23 +05301883 // <Chassis Control>
Patrick Venture0b02be92018-08-31 11:55:55 -07001884 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL,
1885 ipmi_chassis_control, PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001886
Tom Joseph5110c122018-03-23 17:55:40 +05301887 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301888 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1889 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1890
Tom05732372016-09-06 17:21:23 +05301891 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301892 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -07001893 ipmi_chassis_set_sys_boot_options,
1894 PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001895 // <Get POH Counter>
1896 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1897 ipmiGetPOHCounter, PRIVILEGE_USER);
Yong Lic6713cf2018-09-12 12:35:13 +08001898
1899 // <Set Power Restore Policy>
1900 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_RESTORE_POLICY, NULL,
1901 ipmi_chassis_set_power_restore_policy,
1902 PRIVILEGE_OPERATOR);
vishwa36993272015-11-20 12:43:49 -06001903}