blob: 2ad9948ef76578f51f40a9da5355df57c35ba315 [file] [log] [blame]
Adriana Kobylak40814c62015-10-27 15:58:44 -05001#include "chassishandler.h"
Patrick Williams37af7332016-09-02 21:21:42 -05002#include "host-ipmid/ipmid-api.h"
Ratan Guptadcb10672017-07-10 10:33:50 +05303#include "types.hpp"
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05004#include "ipmid.hpp"
5#include "settings.hpp"
Ratan Guptacc8feb42017-07-25 21:52:10 +05306#include "utils.hpp"
Ratan Guptadcb10672017-07-10 10:33:50 +05307
Adriana Kobylak40814c62015-10-27 15:58:44 -05008#include <stdio.h>
Ratan Guptafd28dd72016-08-01 04:58:01 -05009#include <stdlib.h>
Adriana Kobylak40814c62015-10-27 15:58:44 -050010#include <stdint.h>
Brad Bishop35518682016-07-22 08:35:41 -040011#include <mapper.h>
Ratan Guptafd28dd72016-08-01 04:58:01 -050012#include <arpa/inet.h>
13#include <netinet/in.h>
14#include <limits.h>
15#include <string.h>
16#include <endian.h>
17#include <sstream>
18#include <array>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050019#include <fstream>
Tom Joseph5110c122018-03-23 17:55:40 +053020#include <future>
21#include <chrono>
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>
Vernon Mauery185b9f82018-07-20 10:52:36 -070026namespace std {
27 // splice experimental::filesystem into std
28 namespace filesystem = std::experimental::filesystem;
29}
30#else
31# error filesystem not available
32#endif
Ratan Guptadcb10672017-07-10 10:33:50 +053033#include <string>
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050034#include <map>
Ratan Guptadcb10672017-07-10 10:33:50 +053035
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +053036#include <phosphor-logging/log.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053037#include <phosphor-logging/elog-errors.hpp>
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +053038#include <xyz/openbmc_project/State/Host/server.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053039#include "xyz/openbmc_project/Common/error.hpp"
40
41#include <sdbusplus/bus.hpp>
42#include <sdbusplus/server/object.hpp>
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050043#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
44#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
Deepak Kodihalli18b70d12017-07-21 13:36:33 -050045#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050046#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053047
Vishwanatha Subbannab891a572017-03-31 11:34:48 +053048#include "config.h"
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050049#include "timer.hpp"
ratagupta6f6bff2016-04-04 06:20:11 -050050//Defines
Ratan Guptafd28dd72016-08-01 04:58:01 -050051#define SET_PARM_VERSION 0x01
52#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40 //boot flags data1 7th bit on
ratagupta6f6bff2016-04-04 06:20:11 -050053#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80 //boot flags data1 8th bit on
Ratan Guptafd28dd72016-08-01 04:58:01 -050054#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0 //boot flags data1 7 & 8 bit on
ratagupta6f6bff2016-04-04 06:20:11 -050055
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050056std::unique_ptr<phosphor::ipmi::Timer> identifyTimer = nullptr;
57
Ratan Guptafd28dd72016-08-01 04:58:01 -050058constexpr size_t SIZE_MAC = 18;
59constexpr size_t SIZE_BOOT_OPTION = (uint8_t)BootOptionResponseSize::
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -050060 OPAL_NETWORK_SETTINGS;//Maximum size of the boot option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050061constexpr size_t SIZE_PREFIX = 7;
62constexpr size_t MAX_PREFIX_VALUE = 32;
63constexpr size_t SIZE_COOKIE = 4;
64constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053065constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053066
Ratan Guptafd28dd72016-08-01 04:58:01 -050067//PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053068static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
69 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050070
71static constexpr size_t COOKIE_OFFSET = 1;
72static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053073static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050074static constexpr size_t MAC_OFFSET = 9;
75static constexpr size_t ADDRTYPE_OFFSET = 16;
76static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050077
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050078static constexpr size_t encIdentifyObjectsSize = 1;
79static constexpr size_t chassisIdentifyReqLength = 2;
80static constexpr size_t identifyIntervalPos = 0;
81static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080082
Adriana Kobylak40814c62015-10-27 15:58:44 -050083void register_netfn_chassis_functions() __attribute__((constructor));
84
shgoupfd84fbbf2015-12-17 10:05:51 +080085// Host settings in dbus
86// Service name should be referenced by connection name got via object mapper
87const char *settings_object_name = "/org/openbmc/settings/host0";
88const char *settings_intf_name = "org.freedesktop.DBus.Properties";
89const char *host_intf_name = "org.openbmc.settings.Host";
Tom Joseph5110c122018-03-23 17:55:40 +053090const char *identify_led_object_name =
91 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080092
Ratan Guptadcb10672017-07-10 10:33:50 +053093constexpr auto SETTINGS_ROOT = "/";
94constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053095
96constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
97constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
98
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050099static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
100static constexpr auto chassisPOHStateIntf =
101 "xyz.openbmc_project.State.PowerOnHours";
102static constexpr auto pOHCounterProperty = "POHCounter";
103static constexpr auto match = "chassis0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530104
Nan Li8d15fb42016-08-16 22:29:40 +0800105typedef struct
106{
107 uint8_t cap_flags;
108 uint8_t fru_info_dev_addr;
109 uint8_t sdr_dev_addr;
110 uint8_t sel_dev_addr;
111 uint8_t system_management_dev_addr;
112 uint8_t bridge_dev_addr;
113}__attribute__((packed)) ipmi_chassis_cap_t;
114
Nan Lifdd8ec52016-08-28 03:57:40 +0800115typedef struct
116{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500117 uint8_t cur_power_state;
118 uint8_t last_power_event;
119 uint8_t misc_power_state;
120 uint8_t front_panel_button_cap_status;
Nan Lifdd8ec52016-08-28 03:57:40 +0800121}__attribute__((packed)) ipmi_get_chassis_status_t;
122
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500123/**
124 * @struct Get POH counter command response data
125 */
126struct GetPOHCountResponse
127{
128 uint8_t minPerCount; ///< Minutes per count
129 uint8_t counterReading[4]; ///< Counter reading
130}__attribute__((packed));
131
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530132// Phosphor Host State manager
133namespace State = sdbusplus::xyz::openbmc_project::State::server;
134
Vernon Mauery185b9f82018-07-20 10:52:36 -0700135namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500136
Ratan Guptadcb10672017-07-10 10:33:50 +0530137using namespace phosphor::logging;
138using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500139using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500140namespace chassis
141{
142namespace internal
143{
144
145constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
146constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500147constexpr auto powerRestoreIntf =
148 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500149sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
150
151namespace cache
152{
153
154settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500155 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500156
157} // namespace cache
158} // namespace internal
159} // namespace chassis
160
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500161namespace poh
162{
163
164constexpr auto minutesPerCount = 60;
165
166} // namespace poh
167
Ratan Guptadcb10672017-07-10 10:33:50 +0530168//TODO : Can remove the below function as we have
169// new functions which uses sdbusplus.
170//
171// openbmc/openbmc#1489
ratagupta6f6bff2016-04-04 06:20:11 -0500172int dbus_get_property(const char *name, char **buf)
shgoupfd84fbbf2015-12-17 10:05:51 +0800173{
174 sd_bus_error error = SD_BUS_ERROR_NULL;
175 sd_bus_message *m = NULL;
176 sd_bus *bus = NULL;
177 char *temp_buf = NULL;
178 char *connection = NULL;
179 int r;
180
Brad Bishop35518682016-07-22 08:35:41 -0400181 // Get the system bus where most system services are provided.
182 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800183
Brad Bishop35518682016-07-22 08:35:41 -0400184 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +0800185 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530186 log<level::ERR>("Failed to get connection",
187 entry("OBJ_NAME=%s", settings_object_name),
188 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800189 goto finish;
190 }
191
shgoupfd84fbbf2015-12-17 10:05:51 +0800192 /*
193 * Bus, service, object path, interface and method are provided to call
194 * the method.
195 * Signatures and input arguments are provided by the arguments at the
196 * end.
197 */
198 r = sd_bus_call_method(bus,
199 connection, /* service to contact */
200 settings_object_name, /* object path */
201 settings_intf_name, /* interface name */
202 "Get", /* method name */
203 &error, /* object to return error in */
204 &m, /* return message on success */
205 "ss", /* input signature */
206 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500207 name); /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800208
209 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530210 log<level::ERR>("Failed to issue Get method call",
211 entry("ERRNO=0x%X", r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800212 goto finish;
213 }
214
215 /*
216 * The output should be parsed exactly the same as the output formatting
217 * specified.
218 */
219 r = sd_bus_message_read(m, "v", "s", &temp_buf);
220 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530221 log<level::ERR>("Failed to parse response message",
222 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800223 goto finish;
224 }
225
Matthew Barth56181052017-01-23 09:36:29 -0600226 *buf = strdup(temp_buf);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500227 /* *buf = (char*) malloc(strlen(temp_buf));
shgoupfd84fbbf2015-12-17 10:05:51 +0800228 if (*buf) {
229 strcpy(*buf, temp_buf);
230 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500231 */
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530232
shgoupfd84fbbf2015-12-17 10:05:51 +0800233
234finish:
235 sd_bus_error_free(&error);
236 sd_bus_message_unref(m);
237 free(connection);
238
239 return r;
240}
241
Ratan Guptadcb10672017-07-10 10:33:50 +0530242//TODO : Can remove the below function as we have
243// new functions which uses sdbusplus.
244//
245// openbmc/openbmc#1489
246
ratagupta6f6bff2016-04-04 06:20:11 -0500247int dbus_set_property(const char * name, const char *value)
shgoupfd84fbbf2015-12-17 10:05:51 +0800248{
249 sd_bus_error error = SD_BUS_ERROR_NULL;
250 sd_bus_message *m = NULL;
251 sd_bus *bus = NULL;
252 char *connection = NULL;
253 int r;
254
Brad Bishop35518682016-07-22 08:35:41 -0400255 // Get the system bus where most system services are provided.
256 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800257
Brad Bishop35518682016-07-22 08:35:41 -0400258 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +0800259 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530260 log<level::ERR>("Failed to get connection",
261 entry("OBJ_NAME=%s", settings_object_name),
262 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800263 goto finish;
264 }
265
shgoupfd84fbbf2015-12-17 10:05:51 +0800266 /*
267 * Bus, service, object path, interface and method are provided to call
268 * the method.
269 * Signatures and input arguments are provided by the arguments at the
270 * end.
271 */
272 r = sd_bus_call_method(bus,
273 connection, /* service to contact */
274 settings_object_name, /* object path */
275 settings_intf_name, /* interface name */
276 "Set", /* method name */
277 &error, /* object to return error in */
278 &m, /* return message on success */
279 "ssv", /* input signature */
280 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500281 name, /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800282 "s", /* third argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500283 value); /* fourth argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800284
285 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530286 log<level::ERR>("Failed to issue Set method call",
287 entry("ERRNO=0x%X", r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800288 goto finish;
289 }
290
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500291 finish:
shgoupfd84fbbf2015-12-17 10:05:51 +0800292 sd_bus_error_free(&error);
293 sd_bus_message_unref(m);
294 free(connection);
295
296 return r;
297}
298
Adriana Kobylak40814c62015-10-27 15:58:44 -0500299struct get_sys_boot_options_t {
300 uint8_t parameter;
301 uint8_t set;
302 uint8_t block;
303} __attribute__ ((packed));
304
shgoupfd84fbbf2015-12-17 10:05:51 +0800305struct get_sys_boot_options_response_t {
306 uint8_t version;
307 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500308 uint8_t data[SIZE_BOOT_OPTION];
shgoupfd84fbbf2015-12-17 10:05:51 +0800309} __attribute__ ((packed));
310
311struct set_sys_boot_options_t {
312 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500313 uint8_t data[SIZE_BOOT_OPTION];
shgoupfd84fbbf2015-12-17 10:05:51 +0800314} __attribute__ ((packed));
315
Ratan Guptafd28dd72016-08-01 04:58:01 -0500316
Ratan Guptadcb10672017-07-10 10:33:50 +0530317int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500318{
Ratan Guptadcb10672017-07-10 10:33:50 +0530319 ipmi::PropertyMap properties;
320 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530321 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500322
Ratan Guptadcb10672017-07-10 10:33:50 +0530323 try
324 {
325 //TODO There may be cases where an interface is implemented by multiple
326 // objects,to handle such cases we are interested on that object
327 // which are on interested busname.
328 // Currenlty mapper doesn't give the readable busname(gives busid)
329 // so we can't match with bus name so giving some object specific info
330 // as SETTINGS_MATCH.
331 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500332
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530333 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530334
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530335 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
336 SETTINGS_ROOT, SETTINGS_MATCH);
337
338 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
339 SETTINGS_ROOT, SETTINGS_MATCH);
340
341 properties = ipmi::getAllDbusProperties(bus, ipObjectInfo.second,
342 ipObjectInfo.first, IP_INTERFACE);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530343 auto variant =
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530344 ipmi::getDbusProperty(bus, macObjectInfo.second,
345 macObjectInfo.first,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530346 MAC_INTERFACE, "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530347
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530348 auto ipAddress = properties["Address"].get<std::string>();
Ratan Guptad70f4532017-08-04 02:07:31 +0530349
350 auto gateway = properties["Gateway"].get<std::string>();
351
352 auto prefix = properties["PrefixLength"].get<uint8_t>();
353
354 uint8_t isStatic = (properties["Origin"].get<std::string>() ==
355 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
356 ? 1 : 0;
357
Ratan Guptacc8feb42017-07-25 21:52:10 +0530358 auto MACAddress = variant.get<std::string>();
359
Ratan Guptad70f4532017-08-04 02:07:31 +0530360 // it is expected here that we should get the valid data
361 // but we may also get the default values.
362 // Validation of the data is done by settings.
363 //
364 // if mac address is default mac address then
365 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530366 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530367 {
368 memset(respptr->data, 0, SIZE_BOOT_OPTION);
369 rc = -1;
370 return rc;
371 }
372 // if addr is static then ipaddress,gateway,prefix
373 // should not be default one,don't send blank override.
374 if (isStatic)
375 {
Ratan Gupta8c31d232017-08-13 05:49:43 +0530376 if((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
377 (gateway == ipmi::network::DEFAULT_ADDRESS) ||
Ratan Guptad70f4532017-08-04 02:07:31 +0530378 (!prefix))
379 {
380 memset(respptr->data, 0, SIZE_BOOT_OPTION);
381 rc = -1;
382 return rc;
383 }
384 }
385
Ratan Gupta8c31d232017-08-13 05:49:43 +0530386 sscanf(MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
Ratan Guptadcb10672017-07-10 10:33:50 +0530387 (respptr->data + MAC_OFFSET),
388 (respptr->data + MAC_OFFSET + 1),
389 (respptr->data + MAC_OFFSET + 2),
390 (respptr->data + MAC_OFFSET + 3),
391 (respptr->data + MAC_OFFSET + 4),
392 (respptr->data + MAC_OFFSET + 5));
393
Ratan Guptadcb10672017-07-10 10:33:50 +0530394 respptr->data[MAC_OFFSET + 6] = 0x00;
395
Ratan Guptad70f4532017-08-04 02:07:31 +0530396 memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
397 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530398
399 uint8_t addressFamily = (properties["Type"].get<std::string>() ==
400 "xyz.openbmc_project.Network.IP.Protocol.IPv4") ?
401 AF_INET : AF_INET6;
402
Ratan Gupta8c31d232017-08-13 05:49:43 +0530403 addrSize = (addressFamily == AF_INET) ?
404 ipmi::network::IPV4_ADDRESS_SIZE_BYTE :
405 ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530406
407 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530408 inet_pton(addressFamily, ipAddress.c_str(),
409 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530410
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530411 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
412
413 memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
414
415 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
416
Ratan Guptad70f4532017-08-04 02:07:31 +0530417 inet_pton(addressFamily, gateway.c_str(),
418 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530419
420 }
421 catch (InternalFailure& e)
422 {
423 commit<InternalFailure>();
424 memset(respptr->data, 0, SIZE_BOOT_OPTION);
425 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500426 return rc;
427 }
428
Ratan Guptadcb10672017-07-10 10:33:50 +0530429 //PetiBoot-Specific
Gunnar Mills8991dd62017-10-25 17:11:29 -0500430 //If success then copy the first 9 bytes to the data
Ratan Guptadcb10672017-07-10 10:33:50 +0530431 memcpy(respptr->data, net_conf_initial_bytes,
432 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500433
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530434 memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
435
Ratan Guptafd28dd72016-08-01 04:58:01 -0500436#ifdef _IPMI_DEBUG_
Ratan Guptadcb10672017-07-10 10:33:50 +0530437 printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500438
Ratan Guptadcb10672017-07-10 10:33:50 +0530439 for (uint8_t pos = 0; pos < index; pos++)
440 {
441 printf("%02x ", respptr->data[pos]);
442 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500443#endif
444
Ratan Guptafd28dd72016-08-01 04:58:01 -0500445 return rc;
446}
447
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530448/** @brief convert IPv4 and IPv6 addresses from binary to text form.
449 * @param[in] family - IPv4/Ipv6
450 * @param[in] data - req data pointer.
451 * @param[in] offset - offset in the data.
452 * @param[in] addrSize - size of the data which needs to be read from offset.
453 * @returns address in text form.
454 */
455
456std::string getAddrStr(uint8_t family, uint8_t* data,
457 uint8_t offset, uint8_t addrSize)
458{
459 char ipAddr[INET6_ADDRSTRLEN] = {};
460
461 switch(family)
462 {
463 case AF_INET:
464 {
465 struct sockaddr_in addr4 {};
466 memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
467
468 inet_ntop(AF_INET, &addr4.sin_addr,
469 ipAddr, INET_ADDRSTRLEN);
470
471 break;
472 }
473 case AF_INET6:
474 {
475 struct sockaddr_in6 addr6 {};
476 memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
477
478 inet_ntop(AF_INET6, &addr6.sin6_addr,
479 ipAddr, INET6_ADDRSTRLEN);
480
481 break;
482 }
483 default:
484 {
485 return {};
486 }
487 }
488
489 return ipAddr;
490}
491
Ratan Guptadcb10672017-07-10 10:33:50 +0530492int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500493{
Ratan Guptadcb10672017-07-10 10:33:50 +0530494 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500495 std::string host_network_config;
Ratan Guptad70f4532017-08-04 02:07:31 +0530496 char mac[] {"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530497 std::string ipAddress, gateway;
498 char addrOrigin {0};
499 uint8_t addrSize {0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530500 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530501 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
502 std::string addressType =
503 "xyz.openbmc_project.Network.IP.Protocol.IPv4";
Ratan Guptadcb10672017-07-10 10:33:50 +0530504 uint8_t prefix {0};
505 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530506 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500507
508 //cookie starts from second byte
509 // version starts from sixth byte
510
Ratan Guptadcb10672017-07-10 10:33:50 +0530511 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500512 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530513 do
514 {
515 // cookie == 0x21 0x70 0x62 0x21
516 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
517 (net_conf_initial_bytes + COOKIE_OFFSET),
518 SIZE_COOKIE) != 0)
519 {
520 //cookie == 0
521 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
522 &zeroCookie,
523 SIZE_COOKIE) == 0)
524 {
525 // need to zero out the network settings.
526 break;
527 }
528
529 log<level::ERR>("Invalid Cookie");
530 elog<InternalFailure>();
531 }
532
533 // vesion == 0x00 0x01
534 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
535 (net_conf_initial_bytes + VERSION_OFFSET),
536 SIZE_VERSION) != 0)
537 {
538
539 log<level::ERR>("Invalid Version");
540 elog<InternalFailure>();
541 }
542
Ratan Gupta8c31d232017-08-13 05:49:43 +0530543 snprintf(mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
Ratan Guptadcb10672017-07-10 10:33:50 +0530544 reqptr->data[MAC_OFFSET],
545 reqptr->data[MAC_OFFSET + 1],
546 reqptr->data[MAC_OFFSET + 2],
547 reqptr->data[MAC_OFFSET + 3],
548 reqptr->data[MAC_OFFSET + 4],
549 reqptr->data[MAC_OFFSET + 5]);
550
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530551 memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
552 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530553
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530554 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530555 {
556 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530557 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530558 }
559
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530560 // Get the address size
561 memcpy(&addrSize ,&reqptr->data[ADDR_SIZE_OFFSET], sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530562
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530563 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530564
Ratan Guptad70f4532017-08-04 02:07:31 +0530565 memcpy(&prefix, &(reqptr->data[prefixOffset]),
566 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530567
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530568 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
569
Ratan Gupta8c31d232017-08-13 05:49:43 +0530570 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530571 {
572 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
573 family = AF_INET6;
574 }
575
576 ipAddress = getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530577
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530578 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
579
Ratan Guptadcb10672017-07-10 10:33:50 +0530580 } while(0);
581
Ratan Guptafd28dd72016-08-01 04:58:01 -0500582 //Cookie == 0 or it is a valid cookie
Ratan Guptadcb10672017-07-10 10:33:50 +0530583 host_network_config += "ipaddress="s + ipAddress +
584 ",prefix="s + std::to_string(prefix) + ",gateway="s + gateway +
585 ",mac="s + mac + ",addressOrigin="s + addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500586
Ratan Guptafd28dd72016-08-01 04:58:01 -0500587
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530588 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
589
590 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
591 SETTINGS_ROOT, SETTINGS_MATCH);
592 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
593 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530594 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530595 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530596 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530597 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530598 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530599 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530600 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530601 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530602 IP_INTERFACE, "Gateway", std::string(gateway));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530603 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530604 IP_INTERFACE, "Type",
605 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530606 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530607 MAC_INTERFACE,"MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500608
Ratan Guptad70f4532017-08-04 02:07:31 +0530609 log<level::DEBUG>("Network configuration changed",
610 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
611
Ratan Guptafd28dd72016-08-01 04:58:01 -0500612 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530613 catch (InternalFailure& e)
614 {
615 commit<InternalFailure>();
616 return -1;
617 }
618
619 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500620}
621
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500622uint32_t getPOHCounter()
623{
624 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
625
626 auto chassisStateObj = ipmi::getDbusObject(bus, chassisPOHStateIntf,
627 chassisStateRoot, match);
628
629 auto service = ipmi::getService(bus, chassisPOHStateIntf,
630 chassisStateObj.first);
631
632 auto propValue = ipmi::getDbusProperty(bus, service, chassisStateObj.first,
633 chassisPOHStateIntf,
634 pOHCounterProperty);
635
636 return propValue.get<uint32_t>();
637}
638
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500639ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
640 ipmi_request_t request,
641 ipmi_response_t response,
642 ipmi_data_len_t data_len,
643 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500644{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500645 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800646 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500647 *data_len = 0;
648 return rc;
649}
650
Nan Li8d15fb42016-08-16 22:29:40 +0800651ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500652 ipmi_request_t request, ipmi_response_t response,
653 ipmi_data_len_t data_len, ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800654{
655 // sd_bus error
656 ipmi_ret_t rc = IPMI_CC_OK;
657
658 ipmi_chassis_cap_t chassis_cap{};
659
660 *data_len = sizeof(ipmi_chassis_cap_t);
661
662 // TODO: need future work. Get those flag from MRW.
663
664 // capabilities flags
665 // [7..4] - reserved
666 // [3] – 1b = provides power interlock (IPM 1.5)
667 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
668 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis has capabilities
669 // to lock out external power control and reset button or front panel interfaces
670 // and/or detect tampering with those interfaces).
671 // [0] -1b = Chassis provides intrusion (physical security) sensor.
672 // set to default value 0x0.
673 chassis_cap.cap_flags = 0x0;
674
675 // Since we do not have a separate SDR Device/SEL Device/ FRU repository.
676 // The 20h was given as those 5 device addresses.
677 // Chassis FRU info Device Address
678 chassis_cap.fru_info_dev_addr = 0x20;
679
680 // Chassis SDR Device Address
681 chassis_cap.sdr_dev_addr = 0x20;
682
683 // Chassis SEL Device Address
684 chassis_cap.sel_dev_addr = 0x20;
685
686 // Chassis System Management Device Address
687 chassis_cap.system_management_dev_addr = 0x20;
688
689 // Chassis Bridge Device Address.
690 chassis_cap.bridge_dev_addr = 0x20;
691
692 memcpy(response, &chassis_cap, *data_len);
693
694 return rc;
695}
696
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530697//------------------------------------------
698// Calls into Host State Manager Dbus object
699//------------------------------------------
700int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600701{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500702 // OpenBMC Host State Manager dbus framework
703 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
704 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
705 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
706 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530707
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500708 // sd_bus error
709 int rc = 0;
710 char *busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600711
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500712 // SD Bus error report mechanism.
713 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600714
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500715 // Gets a hook onto either a SYSTEM or SESSION bus
716 sd_bus *bus_type = ipmid_get_sd_bus_connection();
717 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
718 if (rc < 0)
719 {
720 log<level::ERR>("Failed to get bus name",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530721 entry("ERRNO=0x%X, OBJPATH=%s",
722 -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500723 return rc;
724 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530725
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500726 // Convert to string equivalent of the passed in transition enum.
727 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530728
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500729 rc = sd_bus_call_method(bus_type, // On the system bus
730 busname, // Service to contact
731 HOST_STATE_MANAGER_ROOT, // Object path
732 DBUS_PROPERTY_IFACE, // Interface name
733 "Set", // Method to be called
734 &bus_error, // object to return error
735 nullptr, // Response buffer if any
736 "ssv", // Takes 3 arguments
737 HOST_STATE_MANAGER_IFACE,
738 PROPERTY,
739 "s", request.c_str());
740 if(rc < 0)
741 {
742 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530743 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500744 }
745 else
746 {
747 log<level::INFO>("Transition request initiated successfully");
748 }
vishwa36993272015-11-20 12:43:49 -0600749
750 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500751 free(busname);
vishwa36993272015-11-20 12:43:49 -0600752
Sergey Solomineb9b8142016-08-23 09:07:28 -0500753 return rc;
vishwa36993272015-11-20 12:43:49 -0600754}
755
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500756namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500757{
Nan Lifdd8ec52016-08-28 03:57:40 +0800758
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500759using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
760using IpmiValue = uint8_t;
761using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800762
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500763std::map<DbusValue, IpmiValue> dbusToIpmi =
764{
765 {RestorePolicy::Policy::AlwaysOff, 0x00},
766 {RestorePolicy::Policy::Restore, 0x01},
767 {RestorePolicy::Policy::AlwaysOn, 0x02}
768};
Nan Lifdd8ec52016-08-28 03:57:40 +0800769
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500770} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800771
772//----------------------------------------------------------------------
773// Get Chassis Status commands
774//----------------------------------------------------------------------
775ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500776 ipmi_request_t request,
777 ipmi_response_t response,
778 ipmi_data_len_t data_len,
779 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800780{
781 const char *objname = "/org/openbmc/control/power0";
782 const char *intf = "org.openbmc.control.Power";
783
784 sd_bus *bus = NULL;
785 sd_bus_message *reply = NULL;
786 int r = 0;
787 int pgood = 0;
788 char *busname = NULL;
789 ipmi_ret_t rc = IPMI_CC_OK;
790 ipmi_get_chassis_status_t chassis_status{};
791
Nan Lifdd8ec52016-08-28 03:57:40 +0800792 uint8_t s = 0;
793
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500794 using namespace chassis::internal;
795 using namespace chassis::internal::cache;
796 using namespace power_policy;
797
Deepak Kodihallie6027092017-08-27 08:13:37 -0500798 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500799 auto method =
800 dbus.new_method_call(
801 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
802 powerRestoreSetting.c_str(),
Ratan Guptacc8feb42017-07-25 21:52:10 +0530803 ipmi::PROP_INTF,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500804 "Get");
805 method.append(powerRestoreIntf, "PowerRestorePolicy");
806 auto resp = dbus.call(method);
807 if (resp.is_method_error())
808 {
809 log<level::ERR>("Error in PowerRestorePolicy Get");
810 report<InternalFailure>();
811 *data_len = 0;
812 return IPMI_CC_UNSPECIFIED_ERROR;
813 }
814 sdbusplus::message::variant<std::string> result;
815 resp.read(result);
816 auto powerRestore =
817 RestorePolicy::convertPolicyFromString(result.get<std::string>());
Nan Lifdd8ec52016-08-28 03:57:40 +0800818
819 *data_len = 4;
820
Tom Joseph63a00512017-08-09 23:39:59 +0530821 bus = ipmid_get_sd_bus_connection();
822
Nan Lifdd8ec52016-08-28 03:57:40 +0800823 r = mapper_get_service(bus, objname, &busname);
824 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530825 log<level::ERR>("Failed to get bus name",
826 entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800827 rc = IPMI_CC_UNSPECIFIED_ERROR;
828 goto finish;
829 }
830
831 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply, "i");
832 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530833 log<level::ERR>("Failed to call sd_bus_get_property",
834 entry("PROPERTY=%s","pgood"),
835 entry("ERRNO=0x%X", -r),
836 entry("BUS=%s", busname),
837 entry("PATH=%s", objname),
838 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800839 rc = IPMI_CC_UNSPECIFIED_ERROR;
840 goto finish;
841 }
842
843 r = sd_bus_message_read(reply, "i", &pgood);
844 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530845 log<level::ERR>("Failed to read sensor:",
846 entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800847 rc = IPMI_CC_UNSPECIFIED_ERROR;
848 goto finish;
849 }
850
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500851 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800852
853 // Current Power State
854 // [7] reserved
855 // [6..5] power restore policy
856 // 00b = chassis stays powered off after AC/mains returns
857 // 01b = after AC returns, power is restored to the state that was
858 // in effect when AC/mains was lost.
859 // 10b = chassis always powers up after AC/mains returns
860 // 11b = unknow
861 // Set to 00b, by observing the hardware behavior.
862 // Do we need to define a dbus property to identify the restore policy?
863
864 // [4] power control fault
865 // 1b = controller attempted to turn system power on or off, but
866 // system did not enter desired state.
867 // Set to 0b, since We don't support it..
868
869 // [3] power fault
870 // 1b = fault detected in main power subsystem.
871 // set to 0b. for we don't support it.
872
873 // [2] 1b = interlock (chassis is presently shut down because a chassis
874 // panel interlock switch is active). (IPMI 1.5)
875 // set to 0b, for we don't support it.
876
877 // [1] power overload
878 // 1b = system shutdown because of power overload condition.
879 // set to 0b, for we don't support it.
880
881 // [0] power is on
882 // 1b = system power is on
883 // 0b = system power is off(soft-off S4/S5, or mechanical off)
884
885 chassis_status.cur_power_state = ((s & 0x3)<<5) | (pgood & 0x1);
886
887 // Last Power Event
888 // [7..5] – reserved
889 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
890 // [3] – 1b = last power down caused by power fault
891 // [2] – 1b = last power down caused by a power interlock being activated
892 // [1] – 1b = last power down caused by a Power overload
893 // [0] – 1b = AC failed
894 // set to 0x0, for we don't support these fields.
895
896 chassis_status.last_power_event = 0;
897
898 // Misc. Chassis State
899 // [7] – reserved
900 // [6] – 1b = Chassis Identify command and state info supported (Optional)
901 // 0b = Chassis Identify command support unspecified via this command.
902 // (The Get Command Support command , if implemented, would still
903 // indicate support for the Chassis Identify command)
904 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved (return
905 // as 00b) otherwise. Returns the present chassis identify state.
906 // Refer to the Chassis Identify command for more info.
907 // 00b = chassis identify state = Off
908 // 01b = chassis identify state = Temporary(timed) On
909 // 10b = chassis identify state = Indefinite On
910 // 11b = reserved
911 // [3] – 1b = Cooling/fan fault detected
912 // [2] – 1b = Drive Fault
913 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
914 // push-buttons disabled.)
915 // [0] – 1b = Chassis Intrusion active
916 // set to 0, for we don't support them.
917 chassis_status.misc_power_state = 0;
918
919 // Front Panel Button Capabilities and disable/enable status(Optional)
920 // set to 0, for we don't support them.
921 chassis_status.front_panel_button_cap_status = 0;
922
923 // Pack the actual response
924 memcpy(response, &chassis_status, *data_len);
925
926finish:
927 free(busname);
928 reply = sd_bus_message_unref(reply);
929
930 return rc;
931}
Chris Austen7888c4d2015-12-03 15:26:20 -0600932
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530933//-------------------------------------------------------------
934// Send a command to SoftPowerOff application to stop any timer
935//-------------------------------------------------------------
936int stop_soft_off_timer()
937{
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530938 constexpr auto iface = "org.freedesktop.DBus.Properties";
939 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500940 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530941
942 constexpr auto property = "ResponseReceived";
943 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500944 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530945
946 // Get the system bus where most system services are provided.
947 auto bus = ipmid_get_sd_bus_connection();
948
949 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500950 // TODO openbmc/openbmc#1661 - Mapper refactor
951 //
952 // See openbmc/openbmc#1743 for some details but high level summary is that
953 // for now the code will directly call the soft off interface due to a
954 // race condition with mapper usage
955 //
956 //char *busname = nullptr;
957 //auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
958 //if (r < 0)
959 //{
960 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530961 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500962 // return r;
963 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530964
965 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500966 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500967 "Set", nullptr, nullptr, "ssv",
968 soft_off_iface, property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530969 if (rc < 0)
970 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530971 log<level::ERR>("Failed to set property in SoftPowerOff object",
972 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530973 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500974
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500975 //TODO openbmc/openbmc#1661 - Mapper refactor
976 //free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530977 return rc;
978}
979
vishwa36993272015-11-20 12:43:49 -0600980//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500981// Create file to indicate there is no need for softoff notification to host
982//----------------------------------------------------------------------
983void indicate_no_softoff_needed()
984{
985 fs::path path{HOST_INBAND_REQUEST_DIR};
986 if (!fs::is_directory(path))
987 {
988 fs::create_directory(path);
989 }
990
991 // Add the host instance (default 0 for now) to the file name
992 std::string file{HOST_INBAND_REQUEST_FILE};
993 auto size = std::snprintf(nullptr,0,file.c_str(),0);
994 size++; // null
995 std::unique_ptr<char[]> buf(new char[size]);
996 std::snprintf(buf.get(),size,file.c_str(),0);
997
998 // Append file name to directory and create it
999 path /= buf.get();
1000 std::ofstream(path.c_str());
1001}
1002
1003//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -06001004// Chassis Control commands
1005//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001006ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1007 ipmi_request_t request,
1008 ipmi_response_t response,
1009 ipmi_data_len_t data_len,
1010 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001011{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001012 // Error from power off.
1013 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001014
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001015 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001016 *data_len = 0;
1017
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001018 // Catch the actual operaton by peeking into request buffer
1019 uint8_t chassis_ctrl_cmd = *(uint8_t *)request;
vishwa36993272015-11-20 12:43:49 -06001020
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001021 switch(chassis_ctrl_cmd)
1022 {
1023 case CMD_POWER_ON:
1024 rc = initiate_state_transition(State::Host::Transition::On);
1025 break;
1026 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301027 // This path would be hit in 2 conditions.
1028 // 1: When user asks for power off using ipmi chassis command 0x04
1029 // 2: Host asking for power off post shutting down.
1030
1031 // If it's a host requested power off, then need to nudge Softoff
1032 // application that it needs to stop the watchdog timer if running.
1033 // If it is a user requested power off, then this is not really
1034 // needed. But then we need to differentiate between user and host
1035 // calling this same command
1036
1037 // For now, we are going ahead with trying to nudge the soft off and
1038 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001039 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301040
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001041 // Only request the Off transition if the soft power off
1042 // application is not running
1043 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001044 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001045 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301046 // that it should not run. Not doing this will result in State
1047 // manager doing a default soft power off when asked for power
1048 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001049 indicate_no_softoff_needed();
1050
1051 // Now request the shutdown
1052 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001053 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001054 else
1055 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301056 log<level::INFO>("Soft off is running, so let shutdown target "
1057 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001058 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001059 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301060
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001061 case CMD_HARD_RESET:
1062 case CMD_POWER_CYCLE:
1063 // SPEC has a section that says certain implementations can trigger
1064 // PowerOn if power is Off when a command to power cycle is
1065 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001066
1067 // First create a file to indicate to the soft off application
1068 // that it should not run since this is a direct user initiated
1069 // power reboot request (i.e. a reboot request that is not
1070 // originating via a soft power off SMS request)
1071 indicate_no_softoff_needed();
1072
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001073 rc = initiate_state_transition(State::Host::Transition::Reboot);
1074 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301075
1076 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1077 // Request Host State Manager to do a soft power off
1078 rc = initiate_state_transition(State::Host::Transition::Off);
1079 break;
1080
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001081 default:
1082 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301083 log<level::ERR>("Invalid Chassis Control command",
1084 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001085 rc = -1;
1086 }
1087 }
vishwa36993272015-11-20 12:43:49 -06001088
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001089 return ( (rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001090}
1091
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001092/** @brief Return D-Bus connection string to enclosure identify LED object
1093 *
1094 * @param[in, out] connection - connection to D-Bus object
1095 * @return a IPMI return code
1096 */
1097std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301098{
Tom Joseph5110c122018-03-23 17:55:40 +05301099 // lookup enclosure_identify group owner(s) in mapper
1100 auto mapperCall = chassis::internal::dbus.new_method_call(
1101 ipmi::MAPPER_BUS_NAME,
1102 ipmi::MAPPER_OBJ,
1103 ipmi::MAPPER_INTF,
1104 "GetObject");
1105
1106 mapperCall.append(identify_led_object_name);
1107 static const std::vector<std::string> interfaces =
1108 {
1109 "xyz.openbmc_project.Led.Group"
1110 };
1111 mapperCall.append(interfaces);
1112 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1113 if (mapperReply.is_method_error())
1114 {
1115 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001116 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301117 }
1118 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1119 mapperReply.read(mapperResp);
1120
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001121 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301122 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001123 log<level::ERR>("Invalid number of enclosure identify objects.",
1124 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
1125 elog<InternalFailure>();
1126 }
1127 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1128 return pair.first;
1129}
Tom Joseph5110c122018-03-23 17:55:40 +05301130
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001131/** @brief Turn On/Off enclosure identify LED
1132 *
1133 * @param[in] flag - true to turn on LED, false to turn off
1134 * @return a IPMI return code
1135 */
1136void enclosureIdentifyLed(bool flag)
1137{
1138 using namespace chassis::internal;
1139 std::string connection = std::move(getEnclosureIdentifyConnection());
1140 auto led = dbus.new_method_call(connection.c_str(),
1141 identify_led_object_name, "org.freedesktop.DBus.Properties", "Set");
1142 led.append("xyz.openbmc_project.Led.Group", "Asserted",
1143 sdbusplus::message::variant<bool>(flag));
1144 auto ledReply = dbus.call(led);
1145 if (ledReply.is_method_error())
1146 {
1147 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
1148 entry("LED_STATE=%d", flag));
1149 elog<InternalFailure>();
1150 }
1151}
1152
1153/** @brief Callback method to turn off LED
1154 */
1155void enclosureIdentifyLedOff()
1156{
1157 try
1158 {
1159 enclosureIdentifyLed(false);
1160 }
1161 catch (const InternalFailure& e)
1162 {
1163 report<InternalFailure>();
1164 }
1165}
1166
1167/** @brief Create timer to turn on and off the enclosure LED
1168 */
1169void createIdentifyTimer()
1170{
1171 if (!identifyTimer)
1172 {
1173 identifyTimer = std::make_unique<phosphor::ipmi::Timer>(
1174 ipmid_get_sd_event_connection(), enclosureIdentifyLedOff);
1175 }
1176}
1177
1178ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1179 ipmi_request_t request,
1180 ipmi_response_t response,
1181 ipmi_data_len_t data_len,
1182 ipmi_context_t context)
1183{
1184 if (*data_len > chassisIdentifyReqLength)
1185 {
1186 return IPMI_CC_REQ_DATA_LEN_INVALID;
1187 }
1188 uint8_t identifyInterval = *data_len > identifyIntervalPos ?
1189 (static_cast<uint8_t*>(request))[identifyIntervalPos] :
1190 DEFAULT_IDENTIFY_TIME_OUT;
1191 bool forceIdentify = (*data_len == chassisIdentifyReqLength) ?
1192 (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01 : false;
Tom Josephbed26992018-07-31 23:00:24 +05301193
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001194 if (identifyInterval || forceIdentify)
1195 {
1196 // stop the timer if already started, for force identify we should
1197 // not turn off LED
1198 identifyTimer->setTimer(SD_EVENT_OFF);
1199 try
Tom Joseph5110c122018-03-23 17:55:40 +05301200 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001201 enclosureIdentifyLed(true);
1202 }
1203 catch (const InternalFailure& e)
1204 {
1205 report<InternalFailure>();
1206 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301207 }
1208
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001209 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301210 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001211 return IPMI_CC_OK;
1212 }
1213 // start the timer
1214 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
1215 std::chrono::seconds(identifyInterval));
1216 identifyTimer->startTimer(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301217 }
Tom Josephbed26992018-07-31 23:00:24 +05301218 else if (!identifyInterval)
1219 {
1220 identifyTimer->setTimer(SD_EVENT_OFF);
1221 enclosureIdentifyLedOff();
1222 }
Tom Joseph5110c122018-03-23 17:55:40 +05301223 return IPMI_CC_OK;
1224}
1225
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001226namespace boot_options
1227{
1228
1229using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1230using IpmiValue = uint8_t;
1231constexpr auto ipmiDefault = 0;
1232
1233std::map<IpmiValue, Source::Sources> sourceIpmiToDbus =
1234{
1235 {0x01, Source::Sources::Network},
1236 {0x02, Source::Sources::Disk},
1237 {0x05, Source::Sources::ExternalMedia},
1238 {ipmiDefault, Source::Sources::Default}
shgoupfd84fbbf2015-12-17 10:05:51 +08001239};
1240
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001241std::map<IpmiValue, Mode::Modes> modeIpmiToDbus =
1242{
1243 {0x03, Mode::Modes::Safe},
1244 {0x06, Mode::Modes::Setup},
1245 {ipmiDefault, Mode::Modes::Regular}
shgoupfd84fbbf2015-12-17 10:05:51 +08001246};
1247
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001248std::map<Source::Sources, IpmiValue> sourceDbusToIpmi =
1249{
1250 {Source::Sources::Network, 0x01},
1251 {Source::Sources::Disk, 0x02},
1252 {Source::Sources::ExternalMedia, 0x05},
1253 {Source::Sources::Default, ipmiDefault}
1254};
shgoupfd84fbbf2015-12-17 10:05:51 +08001255
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001256std::map<Mode::Modes, IpmiValue> modeDbusToIpmi =
1257{
1258 {Mode::Modes::Safe, 0x03},
1259 {Mode::Modes::Setup, 0x06},
1260 {Mode::Modes::Regular, ipmiDefault}
1261};
shgoupfd84fbbf2015-12-17 10:05:51 +08001262
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001263} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001264
Marri Devender Rao81719702018-05-07 00:53:48 -05001265
1266/** @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);
1278 auto method =
1279 dbus.new_method_call(
1280 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1281 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
1282 method.append(bootSourceIntf, "BootSource", property);
1283 auto reply = dbus.call(method);
1284 if (reply.is_method_error())
1285 {
1286 log<level::ERR>("Error in BootSource Set");
1287 report<InternalFailure>();
1288 return IPMI_CC_UNSPECIFIED_ERROR;
1289 }
1290 return IPMI_CC_OK;
1291}
1292
1293 /** @brief Set the property value for boot mode
1294 * @param[in] mode - boot mode value
1295 * @return On failure return IPMI error.
1296 */
1297static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1298{
1299 using namespace chassis::internal;
1300 using namespace chassis::internal::cache;
1301 sdbusplus::message::variant<std::string> property =
1302 convertForMessage(mode);
1303 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1304 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1305 auto method =
1306 dbus.new_method_call(
1307 objects.service(bootModeSetting, bootModeIntf).c_str(),
1308 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
1309 method.append(bootModeIntf, "BootMode", property);
1310 auto reply = dbus.call(method);
1311 if (reply.is_method_error())
1312 {
1313 log<level::ERR>("Error in BootMode Set");
1314 report<InternalFailure>();
1315 return IPMI_CC_UNSPECIFIED_ERROR;
1316 }
1317 return IPMI_CC_OK;
1318}
1319
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001320ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1321 ipmi_request_t request,
1322 ipmi_response_t response,
1323 ipmi_data_len_t data_len,
1324 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001325{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001326 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001327 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
1328 char *p = NULL;
1329 get_sys_boot_options_response_t *resp = (get_sys_boot_options_response_t *) response;
1330 get_sys_boot_options_t *reqptr = (get_sys_boot_options_t*) request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001331 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001332
shgoupfd84fbbf2015-12-17 10:05:51 +08001333 memset(resp,0,sizeof(*resp));
1334 resp->version = SET_PARM_VERSION;
1335 resp->parm = 5;
ratagupta6f6bff2016-04-04 06:20:11 -05001336 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001337
Adriana Kobylak40814c62015-10-27 15:58:44 -05001338
shgoupfd84fbbf2015-12-17 10:05:51 +08001339 /*
1340 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1341 * This is the only parameter used by petitboot.
1342 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001343 if ( reqptr->parameter == static_cast<uint8_t>
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001344 ( BootOptionParameter::BOOT_FLAGS )) {
shgoupfd84fbbf2015-12-17 10:05:51 +08001345
Ratan Guptafd28dd72016-08-01 04:58:01 -05001346 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001347 using namespace chassis::internal;
1348 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001349
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001350 try
ratagupta6f6bff2016-04-04 06:20:11 -05001351 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001352 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1353 const auto& bootSourceSetting =
1354 std::get<settings::Path>(bootSetting);
1355 auto oneTimeEnabled =
1356 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1357 auto method =
1358 dbus.new_method_call(
1359 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1360 bootSourceSetting.c_str(),
1361 ipmi::PROP_INTF,
1362 "Get");
1363 method.append(bootSourceIntf, "BootSource");
1364 auto reply = dbus.call(method);
1365 if (reply.is_method_error())
1366 {
1367 log<level::ERR>("Error in BootSource Get");
1368 report<InternalFailure>();
1369 *data_len = 0;
1370 return IPMI_CC_UNSPECIFIED_ERROR;
1371 }
1372 sdbusplus::message::variant<std::string> result;
1373 reply.read(result);
1374 auto bootSource =
1375 Source::convertSourcesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001376
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001377 bootSetting = settings::boot::setting(objects, bootModeIntf);
1378 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1379 method = dbus.new_method_call(
1380 objects.service(bootModeSetting, bootModeIntf).
1381 c_str(),
1382 bootModeSetting.c_str(),
1383 ipmi::PROP_INTF,
1384 "Get");
1385 method.append(bootModeIntf, "BootMode");
1386 reply = dbus.call(method);
1387 if (reply.is_method_error())
1388 {
1389 log<level::ERR>("Error in BootMode Get");
1390 report<InternalFailure>();
1391 *data_len = 0;
1392 return IPMI_CC_UNSPECIFIED_ERROR;
1393 }
1394 reply.read(result);
1395 auto bootMode =
1396 Mode::convertModesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001397
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001398 bootOption = sourceDbusToIpmi.at(bootSource);
1399 if ((Mode::Modes::Regular == bootMode) &&
1400 (Source::Sources::Default == bootSource))
1401 {
1402 bootOption = ipmiDefault;
1403 }
1404 else if (Source::Sources::Default == bootSource)
1405 {
1406 bootOption = modeDbusToIpmi.at(bootMode);
1407 }
1408 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001409
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001410 resp->data[0] = oneTimeEnabled ?
1411 SET_PARM_BOOT_FLAGS_VALID_ONE_TIME:
1412 SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001413
ratagupta6f6bff2016-04-04 06:20:11 -05001414 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001415 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001416 catch (InternalFailure& e)
1417 {
1418 report<InternalFailure>();
1419 *data_len = 0;
1420 return IPMI_CC_UNSPECIFIED_ERROR;
1421 }
Ratan Guptafd28dd72016-08-01 04:58:01 -05001422 } else if ( reqptr->parameter == static_cast<uint8_t>
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001423 ( BootOptionParameter::OPAL_NETWORK_SETTINGS )) {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001424
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001425 *data_len = static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001426
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001427 resp->parm = static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001428
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001429 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001430
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001431 if (ret < 0) {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001432
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301433 log<level::ERR>(
1434 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001435 rc = IPMI_CC_UNSPECIFIED_ERROR;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001436
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001437 }else
1438 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001439 }
1440
1441 else {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301442 log<level::ERR>("Unsupported parameter", entry(
1443 "PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001444 }
1445
1446 if (p)
1447 free(p);
1448
Ratan Guptafd28dd72016-08-01 04:58:01 -05001449 if (rc == IPMI_CC_OK)
1450 {
1451 *data_len += 2;
1452 }
1453
shgoupfd84fbbf2015-12-17 10:05:51 +08001454 return rc;
1455}
1456
1457
1458
1459ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001460 ipmi_request_t request,
1461 ipmi_response_t response,
1462 ipmi_data_len_t data_len,
1463 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001464{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001465 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001466 ipmi_ret_t rc = IPMI_CC_OK;
shgoupfd84fbbf2015-12-17 10:05:51 +08001467 set_sys_boot_options_t *reqptr = (set_sys_boot_options_t *) request;
1468
Ratan Guptafd28dd72016-08-01 04:58:01 -05001469 printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",reqptr->parameter);
1470
shgoupfd84fbbf2015-12-17 10:05:51 +08001471 // This IPMI command does not have any resposne data
1472 *data_len = 0;
1473
1474 /* 000101
1475 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1476 * This is the only parameter used by petitboot.
1477 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001478
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001479 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1480 {
1481 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1482 using namespace chassis::internal;
1483 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001484 auto oneTimeEnabled = false;
1485 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301486 constexpr auto oneTimePath =
1487 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001488
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001489 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001490 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001491 bool permanent =
1492 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1493 SET_PARM_BOOT_FLAGS_PERMANENT;
1494
Tom Joseph57e8eb72017-09-25 18:05:02 +05301495 auto bootSetting =
1496 settings::boot::setting(objects, bootSourceIntf);
1497
1498 oneTimeEnabled =
1499 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1500
1501 /*
1502 * Check if the current boot setting is onetime or permanent, if the
1503 * request in the command is otherwise, then set the "Enabled"
1504 * property in one_time object path to 'True' to indicate onetime
1505 * and 'False' to indicate permanent.
1506 *
1507 * Once the onetime/permanent setting is applied, then the bootMode
1508 * and bootSource is updated for the corresponding object.
1509 */
1510 if ((permanent && oneTimeEnabled) ||
1511 (!permanent && !oneTimeEnabled))
1512 {
1513 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1514
1515 ipmi::setDbusProperty(dbus,
1516 service,
1517 oneTimePath,
1518 enabledIntf,
1519 "Enabled",
1520 !permanent);
1521 }
1522
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001523 auto modeItr = modeIpmiToDbus.find(bootOption);
1524 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1525 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001526 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001527 rc = setBootSource(sourceItr->second);
1528 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001529 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001530 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001531 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001532 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001533 // If a set boot device is mapping to a boot source, then reset
1534 // the boot mode D-Bus property to default.
1535 // This way the ipmid code can determine which property is not
1536 // at the default value
1537 if(sourceItr->second != Source::Sources::Default)
1538 {
1539 setBootMode(Mode::Modes::Regular);
1540 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001541 }
1542 if (modeIpmiToDbus.end() != modeItr)
1543 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001544 rc = setBootMode(modeItr->second);
1545 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001546 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001547 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001548 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001549 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001550 // If a set boot device is mapping to a boot mode, then reset
1551 // the boot source D-Bus property to default.
1552 // This way the ipmid code can determine which property is not
1553 // at the default value
1554 if(modeItr->second != Mode::Modes::Regular)
1555 {
1556 setBootSource(Source::Sources::Default);
1557 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001558 }
1559 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001560 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001561 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001562 report<InternalFailure>();
1563 *data_len = 0;
1564 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001565 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001566 } else if (reqptr->parameter ==
1567 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS) {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001568
1569 int ret = setHostNetworkData(reqptr);
1570 if (ret < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301571 log<level::ERR>(
1572 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001573 rc = IPMI_CC_UNSPECIFIED_ERROR;
1574 }
Tom Josephf536c902017-09-25 18:08:15 +05301575 } else if (reqptr->parameter ==
1576 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO)) {
1577 // Handle parameter #4 and return command completed normally
1578 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1579 // parameter. This is added to support the ipmitool command `chassis
1580 // bootdev` which sends set on parameter #4, before setting the boot
1581 // flags.
1582 rc = IPMI_CC_OK;
1583 } else {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301584 log<level::ERR>("Unsupported parameter", entry(
1585 "PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001586 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001587 }
1588
1589 return rc;
1590}
1591
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001592ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1593 ipmi_request_t request, ipmi_response_t response,
1594 ipmi_data_len_t data_len, ipmi_context_t context)
1595{
1596 // sd_bus error
1597 ipmi_ret_t rc = IPMI_CC_OK;
1598
1599 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1600
1601 try
1602 {
1603 auto pohCounter = getPOHCounter();
1604 resptr->counterReading[0] = pohCounter;
1605 resptr->counterReading[1] = pohCounter >> 8;
1606 resptr->counterReading[2] = pohCounter >> 16;
1607 resptr->counterReading[3] = pohCounter >> 24;
1608 }
1609 catch (std::exception& e)
1610 {
1611 log<level::ERR>(e.what());
1612 return IPMI_CC_UNSPECIFIED_ERROR;
1613 }
1614
1615 resptr->minPerCount = poh::minutesPerCount;
1616 *data_len = sizeof(GetPOHCountResponse);
1617
1618 return rc;
1619}
1620
Adriana Kobylak40814c62015-10-27 15:58:44 -05001621void register_netfn_chassis_functions()
1622{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001623 createIdentifyTimer();
1624
Tom05732372016-09-06 17:21:23 +05301625 // <Wildcard Command>
Tom05732372016-09-06 17:21:23 +05301626 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL, ipmi_chassis_wildcard,
1627 PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001628
Tom05732372016-09-06 17:21:23 +05301629 // Get Chassis Capabilities
Tom05732372016-09-06 17:21:23 +05301630 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL, ipmi_get_chassis_cap,
1631 PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001632
Tom05732372016-09-06 17:21:23 +05301633 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301634 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
1635 ipmi_chassis_get_sys_boot_options, PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001636
Tom05732372016-09-06 17:21:23 +05301637 // <Get Chassis Status>
Tom05732372016-09-06 17:21:23 +05301638 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL, ipmi_get_chassis_status,
1639 PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001640
Tom05732372016-09-06 17:21:23 +05301641 // <Chassis Control>
Tom05732372016-09-06 17:21:23 +05301642 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL, ipmi_chassis_control,
1643 PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001644
Tom Joseph5110c122018-03-23 17:55:40 +05301645 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301646 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1647 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1648
Tom05732372016-09-06 17:21:23 +05301649 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301650 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
1651 ipmi_chassis_set_sys_boot_options, PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001652 // <Get POH Counter>
1653 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1654 ipmiGetPOHCounter, PRIVILEGE_USER);
vishwa36993272015-11-20 12:43:49 -06001655}