blob: 3092e7cdff81603288a8e153d4d4fe60e4f14fb1 [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>
Andrew Geisslera6e3a302017-05-31 19:34:00 -050022#include <experimental/filesystem>
Ratan Guptadcb10672017-07-10 10:33:50 +053023#include <string>
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050024#include <map>
Ratan Guptadcb10672017-07-10 10:33:50 +053025
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +053026#include <phosphor-logging/log.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053027#include <phosphor-logging/elog-errors.hpp>
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +053028#include <xyz/openbmc_project/State/Host/server.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053029#include "xyz/openbmc_project/Common/error.hpp"
30
31#include <sdbusplus/bus.hpp>
32#include <sdbusplus/server/object.hpp>
Deepak Kodihalli8cc19362017-07-21 11:18:38 -050033#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
34#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
Deepak Kodihalli18b70d12017-07-21 13:36:33 -050035#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050036#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
Ratan Guptadcb10672017-07-10 10:33:50 +053037
Vishwanatha Subbannab891a572017-03-31 11:34:48 +053038#include "config.h"
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050039#include "timer.hpp"
ratagupta6f6bff2016-04-04 06:20:11 -050040//Defines
Ratan Guptafd28dd72016-08-01 04:58:01 -050041#define SET_PARM_VERSION 0x01
42#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40 //boot flags data1 7th bit on
ratagupta6f6bff2016-04-04 06:20:11 -050043#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80 //boot flags data1 8th bit on
Ratan Guptafd28dd72016-08-01 04:58:01 -050044#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0 //boot flags data1 7 & 8 bit on
ratagupta6f6bff2016-04-04 06:20:11 -050045
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050046std::unique_ptr<phosphor::ipmi::Timer> identifyTimer = nullptr;
47
Ratan Guptafd28dd72016-08-01 04:58:01 -050048constexpr size_t SIZE_MAC = 18;
49constexpr size_t SIZE_BOOT_OPTION = (uint8_t)BootOptionResponseSize::
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -050050 OPAL_NETWORK_SETTINGS;//Maximum size of the boot option parametrs
Ratan Guptafd28dd72016-08-01 04:58:01 -050051constexpr size_t SIZE_PREFIX = 7;
52constexpr size_t MAX_PREFIX_VALUE = 32;
53constexpr size_t SIZE_COOKIE = 4;
54constexpr size_t SIZE_VERSION = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053055constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053056
Ratan Guptafd28dd72016-08-01 04:58:01 -050057//PetiBoot-Specific
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053058static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
59 0x21, 0x00, 0x01, 0x06};
Ratan Guptafd28dd72016-08-01 04:58:01 -050060
61static constexpr size_t COOKIE_OFFSET = 1;
62static constexpr size_t VERSION_OFFSET = 5;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053063static constexpr size_t ADDR_SIZE_OFFSET = 8;
Ratan Guptafd28dd72016-08-01 04:58:01 -050064static constexpr size_t MAC_OFFSET = 9;
65static constexpr size_t ADDRTYPE_OFFSET = 16;
66static constexpr size_t IPADDR_OFFSET = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050067
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050068static constexpr size_t encIdentifyObjectsSize = 1;
69static constexpr size_t chassisIdentifyReqLength = 2;
70static constexpr size_t identifyIntervalPos = 0;
71static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080072
Adriana Kobylak40814c62015-10-27 15:58:44 -050073void register_netfn_chassis_functions() __attribute__((constructor));
74
shgoupfd84fbbf2015-12-17 10:05:51 +080075// Host settings in dbus
76// Service name should be referenced by connection name got via object mapper
77const char *settings_object_name = "/org/openbmc/settings/host0";
78const char *settings_intf_name = "org.freedesktop.DBus.Properties";
79const char *host_intf_name = "org.openbmc.settings.Host";
Tom Joseph5110c122018-03-23 17:55:40 +053080const char *identify_led_object_name =
81 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080082
Ratan Guptadcb10672017-07-10 10:33:50 +053083constexpr auto SETTINGS_ROOT = "/";
84constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053085
86constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
87constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
88
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050089static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
90static constexpr auto chassisPOHStateIntf =
91 "xyz.openbmc_project.State.PowerOnHours";
92static constexpr auto pOHCounterProperty = "POHCounter";
93static constexpr auto match = "chassis0";
Ratan Guptadcb10672017-07-10 10:33:50 +053094
Nan Li8d15fb42016-08-16 22:29:40 +080095typedef struct
96{
97 uint8_t cap_flags;
98 uint8_t fru_info_dev_addr;
99 uint8_t sdr_dev_addr;
100 uint8_t sel_dev_addr;
101 uint8_t system_management_dev_addr;
102 uint8_t bridge_dev_addr;
103}__attribute__((packed)) ipmi_chassis_cap_t;
104
Nan Lifdd8ec52016-08-28 03:57:40 +0800105typedef struct
106{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500107 uint8_t cur_power_state;
108 uint8_t last_power_event;
109 uint8_t misc_power_state;
110 uint8_t front_panel_button_cap_status;
Nan Lifdd8ec52016-08-28 03:57:40 +0800111}__attribute__((packed)) ipmi_get_chassis_status_t;
112
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500113/**
114 * @struct Get POH counter command response data
115 */
116struct GetPOHCountResponse
117{
118 uint8_t minPerCount; ///< Minutes per count
119 uint8_t counterReading[4]; ///< Counter reading
120}__attribute__((packed));
121
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530122// Phosphor Host State manager
123namespace State = sdbusplus::xyz::openbmc_project::State::server;
124
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500125namespace fs = std::experimental::filesystem;
126
Ratan Guptadcb10672017-07-10 10:33:50 +0530127using namespace phosphor::logging;
128using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Marri Devender Rao81719702018-05-07 00:53:48 -0500129using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500130namespace chassis
131{
132namespace internal
133{
134
135constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
136constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500137constexpr auto powerRestoreIntf =
138 "xyz.openbmc_project.Control.Power.RestorePolicy";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500139sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
140
141namespace cache
142{
143
144settings::Objects objects(dbus,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500145 {bootModeIntf, bootSourceIntf, powerRestoreIntf});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500146
147} // namespace cache
148} // namespace internal
149} // namespace chassis
150
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500151namespace poh
152{
153
154constexpr auto minutesPerCount = 60;
155
156} // namespace poh
157
Ratan Guptadcb10672017-07-10 10:33:50 +0530158//TODO : Can remove the below function as we have
159// new functions which uses sdbusplus.
160//
161// openbmc/openbmc#1489
ratagupta6f6bff2016-04-04 06:20:11 -0500162int dbus_get_property(const char *name, char **buf)
shgoupfd84fbbf2015-12-17 10:05:51 +0800163{
164 sd_bus_error error = SD_BUS_ERROR_NULL;
165 sd_bus_message *m = NULL;
166 sd_bus *bus = NULL;
167 char *temp_buf = NULL;
168 char *connection = NULL;
169 int r;
170
Brad Bishop35518682016-07-22 08:35:41 -0400171 // Get the system bus where most system services are provided.
172 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800173
Brad Bishop35518682016-07-22 08:35:41 -0400174 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +0800175 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530176 log<level::ERR>("Failed to get connection",
177 entry("OBJ_NAME=%s", settings_object_name),
178 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800179 goto finish;
180 }
181
shgoupfd84fbbf2015-12-17 10:05:51 +0800182 /*
183 * Bus, service, object path, interface and method are provided to call
184 * the method.
185 * Signatures and input arguments are provided by the arguments at the
186 * end.
187 */
188 r = sd_bus_call_method(bus,
189 connection, /* service to contact */
190 settings_object_name, /* object path */
191 settings_intf_name, /* interface name */
192 "Get", /* method name */
193 &error, /* object to return error in */
194 &m, /* return message on success */
195 "ss", /* input signature */
196 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500197 name); /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800198
199 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530200 log<level::ERR>("Failed to issue Get method call",
201 entry("ERRNO=0x%X", r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800202 goto finish;
203 }
204
205 /*
206 * The output should be parsed exactly the same as the output formatting
207 * specified.
208 */
209 r = sd_bus_message_read(m, "v", "s", &temp_buf);
210 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530211 log<level::ERR>("Failed to parse response message",
212 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800213 goto finish;
214 }
215
Matthew Barth56181052017-01-23 09:36:29 -0600216 *buf = strdup(temp_buf);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500217 /* *buf = (char*) malloc(strlen(temp_buf));
shgoupfd84fbbf2015-12-17 10:05:51 +0800218 if (*buf) {
219 strcpy(*buf, temp_buf);
220 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500221 */
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530222
shgoupfd84fbbf2015-12-17 10:05:51 +0800223
224finish:
225 sd_bus_error_free(&error);
226 sd_bus_message_unref(m);
227 free(connection);
228
229 return r;
230}
231
Ratan Guptadcb10672017-07-10 10:33:50 +0530232//TODO : Can remove the below function as we have
233// new functions which uses sdbusplus.
234//
235// openbmc/openbmc#1489
236
ratagupta6f6bff2016-04-04 06:20:11 -0500237int dbus_set_property(const char * name, const char *value)
shgoupfd84fbbf2015-12-17 10:05:51 +0800238{
239 sd_bus_error error = SD_BUS_ERROR_NULL;
240 sd_bus_message *m = NULL;
241 sd_bus *bus = NULL;
242 char *connection = NULL;
243 int r;
244
Brad Bishop35518682016-07-22 08:35:41 -0400245 // Get the system bus where most system services are provided.
246 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800247
Brad Bishop35518682016-07-22 08:35:41 -0400248 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +0800249 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530250 log<level::ERR>("Failed to get connection",
251 entry("OBJ_NAME=%s", settings_object_name),
252 entry("ERRNO=0x%X", -r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800253 goto finish;
254 }
255
shgoupfd84fbbf2015-12-17 10:05:51 +0800256 /*
257 * Bus, service, object path, interface and method are provided to call
258 * the method.
259 * Signatures and input arguments are provided by the arguments at the
260 * end.
261 */
262 r = sd_bus_call_method(bus,
263 connection, /* service to contact */
264 settings_object_name, /* object path */
265 settings_intf_name, /* interface name */
266 "Set", /* method name */
267 &error, /* object to return error in */
268 &m, /* return message on success */
269 "ssv", /* input signature */
270 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500271 name, /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800272 "s", /* third argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500273 value); /* fourth argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800274
275 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530276 log<level::ERR>("Failed to issue Set method call",
277 entry("ERRNO=0x%X", r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800278 goto finish;
279 }
280
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500281 finish:
shgoupfd84fbbf2015-12-17 10:05:51 +0800282 sd_bus_error_free(&error);
283 sd_bus_message_unref(m);
284 free(connection);
285
286 return r;
287}
288
Adriana Kobylak40814c62015-10-27 15:58:44 -0500289struct get_sys_boot_options_t {
290 uint8_t parameter;
291 uint8_t set;
292 uint8_t block;
293} __attribute__ ((packed));
294
shgoupfd84fbbf2015-12-17 10:05:51 +0800295struct get_sys_boot_options_response_t {
296 uint8_t version;
297 uint8_t parm;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500298 uint8_t data[SIZE_BOOT_OPTION];
shgoupfd84fbbf2015-12-17 10:05:51 +0800299} __attribute__ ((packed));
300
301struct set_sys_boot_options_t {
302 uint8_t parameter;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500303 uint8_t data[SIZE_BOOT_OPTION];
shgoupfd84fbbf2015-12-17 10:05:51 +0800304} __attribute__ ((packed));
305
Ratan Guptafd28dd72016-08-01 04:58:01 -0500306
Ratan Guptadcb10672017-07-10 10:33:50 +0530307int getHostNetworkData(get_sys_boot_options_response_t* respptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500308{
Ratan Guptadcb10672017-07-10 10:33:50 +0530309 ipmi::PropertyMap properties;
310 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530311 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500312
Ratan Guptadcb10672017-07-10 10:33:50 +0530313 try
314 {
315 //TODO There may be cases where an interface is implemented by multiple
316 // objects,to handle such cases we are interested on that object
317 // which are on interested busname.
318 // Currenlty mapper doesn't give the readable busname(gives busid)
319 // so we can't match with bus name so giving some object specific info
320 // as SETTINGS_MATCH.
321 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500322
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530323 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530324
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530325 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
326 SETTINGS_ROOT, SETTINGS_MATCH);
327
328 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
329 SETTINGS_ROOT, SETTINGS_MATCH);
330
331 properties = ipmi::getAllDbusProperties(bus, ipObjectInfo.second,
332 ipObjectInfo.first, IP_INTERFACE);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530333 auto variant =
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530334 ipmi::getDbusProperty(bus, macObjectInfo.second,
335 macObjectInfo.first,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530336 MAC_INTERFACE, "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530337
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530338 auto ipAddress = properties["Address"].get<std::string>();
Ratan Guptad70f4532017-08-04 02:07:31 +0530339
340 auto gateway = properties["Gateway"].get<std::string>();
341
342 auto prefix = properties["PrefixLength"].get<uint8_t>();
343
344 uint8_t isStatic = (properties["Origin"].get<std::string>() ==
345 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
346 ? 1 : 0;
347
Ratan Guptacc8feb42017-07-25 21:52:10 +0530348 auto MACAddress = variant.get<std::string>();
349
Ratan Guptad70f4532017-08-04 02:07:31 +0530350 // it is expected here that we should get the valid data
351 // but we may also get the default values.
352 // Validation of the data is done by settings.
353 //
354 // if mac address is default mac address then
355 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530356 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530357 {
358 memset(respptr->data, 0, SIZE_BOOT_OPTION);
359 rc = -1;
360 return rc;
361 }
362 // if addr is static then ipaddress,gateway,prefix
363 // should not be default one,don't send blank override.
364 if (isStatic)
365 {
Ratan Gupta8c31d232017-08-13 05:49:43 +0530366 if((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
367 (gateway == ipmi::network::DEFAULT_ADDRESS) ||
Ratan Guptad70f4532017-08-04 02:07:31 +0530368 (!prefix))
369 {
370 memset(respptr->data, 0, SIZE_BOOT_OPTION);
371 rc = -1;
372 return rc;
373 }
374 }
375
Ratan Gupta8c31d232017-08-13 05:49:43 +0530376 sscanf(MACAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
Ratan Guptadcb10672017-07-10 10:33:50 +0530377 (respptr->data + MAC_OFFSET),
378 (respptr->data + MAC_OFFSET + 1),
379 (respptr->data + MAC_OFFSET + 2),
380 (respptr->data + MAC_OFFSET + 3),
381 (respptr->data + MAC_OFFSET + 4),
382 (respptr->data + MAC_OFFSET + 5));
383
Ratan Guptadcb10672017-07-10 10:33:50 +0530384 respptr->data[MAC_OFFSET + 6] = 0x00;
385
Ratan Guptad70f4532017-08-04 02:07:31 +0530386 memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
387 sizeof(isStatic));
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530388
389 uint8_t addressFamily = (properties["Type"].get<std::string>() ==
390 "xyz.openbmc_project.Network.IP.Protocol.IPv4") ?
391 AF_INET : AF_INET6;
392
Ratan Gupta8c31d232017-08-13 05:49:43 +0530393 addrSize = (addressFamily == AF_INET) ?
394 ipmi::network::IPV4_ADDRESS_SIZE_BYTE :
395 ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530396
397 // ipaddress and gateway would be in IPv4 format
Ratan Guptad70f4532017-08-04 02:07:31 +0530398 inet_pton(addressFamily, ipAddress.c_str(),
399 (respptr->data + IPADDR_OFFSET));
Ratan Guptadcb10672017-07-10 10:33:50 +0530400
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530401 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
402
403 memcpy(respptr->data + prefixOffset, &prefix, sizeof(prefix));
404
405 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
406
Ratan Guptad70f4532017-08-04 02:07:31 +0530407 inet_pton(addressFamily, gateway.c_str(),
408 (respptr->data + gatewayOffset));
Ratan Guptadcb10672017-07-10 10:33:50 +0530409
410 }
411 catch (InternalFailure& e)
412 {
413 commit<InternalFailure>();
414 memset(respptr->data, 0, SIZE_BOOT_OPTION);
415 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500416 return rc;
417 }
418
Ratan Guptadcb10672017-07-10 10:33:50 +0530419 //PetiBoot-Specific
Gunnar Mills8991dd62017-10-25 17:11:29 -0500420 //If success then copy the first 9 bytes to the data
Ratan Guptadcb10672017-07-10 10:33:50 +0530421 memcpy(respptr->data, net_conf_initial_bytes,
422 sizeof(net_conf_initial_bytes));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500423
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530424 memcpy(respptr->data + ADDR_SIZE_OFFSET, &addrSize, sizeof(addrSize));
425
Ratan Guptafd28dd72016-08-01 04:58:01 -0500426#ifdef _IPMI_DEBUG_
Ratan Guptadcb10672017-07-10 10:33:50 +0530427 printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500428
Ratan Guptadcb10672017-07-10 10:33:50 +0530429 for (uint8_t pos = 0; pos < index; pos++)
430 {
431 printf("%02x ", respptr->data[pos]);
432 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500433#endif
434
Ratan Guptafd28dd72016-08-01 04:58:01 -0500435 return rc;
436}
437
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530438/** @brief convert IPv4 and IPv6 addresses from binary to text form.
439 * @param[in] family - IPv4/Ipv6
440 * @param[in] data - req data pointer.
441 * @param[in] offset - offset in the data.
442 * @param[in] addrSize - size of the data which needs to be read from offset.
443 * @returns address in text form.
444 */
445
446std::string getAddrStr(uint8_t family, uint8_t* data,
447 uint8_t offset, uint8_t addrSize)
448{
449 char ipAddr[INET6_ADDRSTRLEN] = {};
450
451 switch(family)
452 {
453 case AF_INET:
454 {
455 struct sockaddr_in addr4 {};
456 memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
457
458 inet_ntop(AF_INET, &addr4.sin_addr,
459 ipAddr, INET_ADDRSTRLEN);
460
461 break;
462 }
463 case AF_INET6:
464 {
465 struct sockaddr_in6 addr6 {};
466 memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
467
468 inet_ntop(AF_INET6, &addr6.sin6_addr,
469 ipAddr, INET6_ADDRSTRLEN);
470
471 break;
472 }
473 default:
474 {
475 return {};
476 }
477 }
478
479 return ipAddr;
480}
481
Ratan Guptadcb10672017-07-10 10:33:50 +0530482int setHostNetworkData(set_sys_boot_options_t* reqptr)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500483{
Ratan Guptadcb10672017-07-10 10:33:50 +0530484 using namespace std::string_literals;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500485 std::string host_network_config;
Ratan Guptad70f4532017-08-04 02:07:31 +0530486 char mac[] {"00:00:00:00:00:00"};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530487 std::string ipAddress, gateway;
488 char addrOrigin {0};
489 uint8_t addrSize {0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530490 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530491 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
492 std::string addressType =
493 "xyz.openbmc_project.Network.IP.Protocol.IPv4";
Ratan Guptadcb10672017-07-10 10:33:50 +0530494 uint8_t prefix {0};
495 uint32_t zeroCookie = 0;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530496 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500497
498 //cookie starts from second byte
499 // version starts from sixth byte
500
Ratan Guptadcb10672017-07-10 10:33:50 +0530501 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500502 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530503 do
504 {
505 // cookie == 0x21 0x70 0x62 0x21
506 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
507 (net_conf_initial_bytes + COOKIE_OFFSET),
508 SIZE_COOKIE) != 0)
509 {
510 //cookie == 0
511 if (memcmp(&(reqptr->data[COOKIE_OFFSET]),
512 &zeroCookie,
513 SIZE_COOKIE) == 0)
514 {
515 // need to zero out the network settings.
516 break;
517 }
518
519 log<level::ERR>("Invalid Cookie");
520 elog<InternalFailure>();
521 }
522
523 // vesion == 0x00 0x01
524 if (memcmp(&(reqptr->data[VERSION_OFFSET]),
525 (net_conf_initial_bytes + VERSION_OFFSET),
526 SIZE_VERSION) != 0)
527 {
528
529 log<level::ERR>("Invalid Version");
530 elog<InternalFailure>();
531 }
532
Ratan Gupta8c31d232017-08-13 05:49:43 +0530533 snprintf(mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
Ratan Guptadcb10672017-07-10 10:33:50 +0530534 reqptr->data[MAC_OFFSET],
535 reqptr->data[MAC_OFFSET + 1],
536 reqptr->data[MAC_OFFSET + 2],
537 reqptr->data[MAC_OFFSET + 3],
538 reqptr->data[MAC_OFFSET + 4],
539 reqptr->data[MAC_OFFSET + 5]);
540
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530541 memcpy(&addrOrigin, &(reqptr->data[ADDRTYPE_OFFSET]),
542 sizeof(decltype(addrOrigin)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530543
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530544 if (addrOrigin)
Ratan Guptadcb10672017-07-10 10:33:50 +0530545 {
546 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530547 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530548 }
549
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530550 // Get the address size
551 memcpy(&addrSize ,&reqptr->data[ADDR_SIZE_OFFSET], sizeof(addrSize));
Ratan Guptadcb10672017-07-10 10:33:50 +0530552
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530553 uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
Ratan Guptadcb10672017-07-10 10:33:50 +0530554
Ratan Guptad70f4532017-08-04 02:07:31 +0530555 memcpy(&prefix, &(reqptr->data[prefixOffset]),
556 sizeof(decltype(prefix)));
Ratan Guptadcb10672017-07-10 10:33:50 +0530557
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530558 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
559
Ratan Gupta8c31d232017-08-13 05:49:43 +0530560 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530561 {
562 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
563 family = AF_INET6;
564 }
565
566 ipAddress = getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530567
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530568 gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
569
Ratan Guptadcb10672017-07-10 10:33:50 +0530570 } while(0);
571
Ratan Guptafd28dd72016-08-01 04:58:01 -0500572 //Cookie == 0 or it is a valid cookie
Ratan Guptadcb10672017-07-10 10:33:50 +0530573 host_network_config += "ipaddress="s + ipAddress +
574 ",prefix="s + std::to_string(prefix) + ",gateway="s + gateway +
575 ",mac="s + mac + ",addressOrigin="s + addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500576
Ratan Guptafd28dd72016-08-01 04:58:01 -0500577
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530578 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
579
580 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
581 SETTINGS_ROOT, SETTINGS_MATCH);
582 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
583 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530584 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530585 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530586 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530587 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530588 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530589 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530590 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530591 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530592 IP_INTERFACE, "Gateway", std::string(gateway));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530593 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530594 IP_INTERFACE, "Type",
595 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530596 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Ratan Guptadcb10672017-07-10 10:33:50 +0530597 MAC_INTERFACE,"MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500598
Ratan Guptad70f4532017-08-04 02:07:31 +0530599 log<level::DEBUG>("Network configuration changed",
600 entry("NETWORKCONFIG=%s", host_network_config.c_str()));
601
Ratan Guptafd28dd72016-08-01 04:58:01 -0500602 }
Ratan Guptadcb10672017-07-10 10:33:50 +0530603 catch (InternalFailure& e)
604 {
605 commit<InternalFailure>();
606 return -1;
607 }
608
609 return 0;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500610}
611
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500612uint32_t getPOHCounter()
613{
614 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
615
616 auto chassisStateObj = ipmi::getDbusObject(bus, chassisPOHStateIntf,
617 chassisStateRoot, match);
618
619 auto service = ipmi::getService(bus, chassisPOHStateIntf,
620 chassisStateObj.first);
621
622 auto propValue = ipmi::getDbusProperty(bus, service, chassisStateObj.first,
623 chassisPOHStateIntf,
624 pOHCounterProperty);
625
626 return propValue.get<uint32_t>();
627}
628
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500629ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
630 ipmi_request_t request,
631 ipmi_response_t response,
632 ipmi_data_len_t data_len,
633 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -0500634{
Adriana Kobylak40814c62015-10-27 15:58:44 -0500635 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800636 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500637 *data_len = 0;
638 return rc;
639}
640
Nan Li8d15fb42016-08-16 22:29:40 +0800641ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500642 ipmi_request_t request, ipmi_response_t response,
643 ipmi_data_len_t data_len, ipmi_context_t context)
Nan Li8d15fb42016-08-16 22:29:40 +0800644{
645 // sd_bus error
646 ipmi_ret_t rc = IPMI_CC_OK;
647
648 ipmi_chassis_cap_t chassis_cap{};
649
650 *data_len = sizeof(ipmi_chassis_cap_t);
651
652 // TODO: need future work. Get those flag from MRW.
653
654 // capabilities flags
655 // [7..4] - reserved
656 // [3] – 1b = provides power interlock (IPM 1.5)
657 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
658 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis has capabilities
659 // to lock out external power control and reset button or front panel interfaces
660 // and/or detect tampering with those interfaces).
661 // [0] -1b = Chassis provides intrusion (physical security) sensor.
662 // set to default value 0x0.
663 chassis_cap.cap_flags = 0x0;
664
665 // Since we do not have a separate SDR Device/SEL Device/ FRU repository.
666 // The 20h was given as those 5 device addresses.
667 // Chassis FRU info Device Address
668 chassis_cap.fru_info_dev_addr = 0x20;
669
670 // Chassis SDR Device Address
671 chassis_cap.sdr_dev_addr = 0x20;
672
673 // Chassis SEL Device Address
674 chassis_cap.sel_dev_addr = 0x20;
675
676 // Chassis System Management Device Address
677 chassis_cap.system_management_dev_addr = 0x20;
678
679 // Chassis Bridge Device Address.
680 chassis_cap.bridge_dev_addr = 0x20;
681
682 memcpy(response, &chassis_cap, *data_len);
683
684 return rc;
685}
686
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530687//------------------------------------------
688// Calls into Host State Manager Dbus object
689//------------------------------------------
690int initiate_state_transition(State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600691{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500692 // OpenBMC Host State Manager dbus framework
693 constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0";
694 constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host";
695 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
696 constexpr auto PROPERTY = "RequestedHostTransition";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530697
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500698 // sd_bus error
699 int rc = 0;
700 char *busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600701
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500702 // SD Bus error report mechanism.
703 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600704
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500705 // Gets a hook onto either a SYSTEM or SESSION bus
706 sd_bus *bus_type = ipmid_get_sd_bus_connection();
707 rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname);
708 if (rc < 0)
709 {
710 log<level::ERR>("Failed to get bus name",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530711 entry("ERRNO=0x%X, OBJPATH=%s",
712 -rc, HOST_STATE_MANAGER_ROOT));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500713 return rc;
714 }
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530715
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500716 // Convert to string equivalent of the passed in transition enum.
717 auto request = State::convertForMessage(transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530718
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500719 rc = sd_bus_call_method(bus_type, // On the system bus
720 busname, // Service to contact
721 HOST_STATE_MANAGER_ROOT, // Object path
722 DBUS_PROPERTY_IFACE, // Interface name
723 "Set", // Method to be called
724 &bus_error, // object to return error
725 nullptr, // Response buffer if any
726 "ssv", // Takes 3 arguments
727 HOST_STATE_MANAGER_IFACE,
728 PROPERTY,
729 "s", request.c_str());
730 if(rc < 0)
731 {
732 log<level::ERR>("Failed to initiate transition",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530733 entry("ERRNO=0x%X, REQUEST=%s", -rc, request.c_str()));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500734 }
735 else
736 {
737 log<level::INFO>("Transition request initiated successfully");
738 }
vishwa36993272015-11-20 12:43:49 -0600739
740 sd_bus_error_free(&bus_error);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500741 free(busname);
vishwa36993272015-11-20 12:43:49 -0600742
Sergey Solomineb9b8142016-08-23 09:07:28 -0500743 return rc;
vishwa36993272015-11-20 12:43:49 -0600744}
745
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500746namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500747{
Nan Lifdd8ec52016-08-28 03:57:40 +0800748
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500749using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
750using IpmiValue = uint8_t;
751using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800752
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500753std::map<DbusValue, IpmiValue> dbusToIpmi =
754{
755 {RestorePolicy::Policy::AlwaysOff, 0x00},
756 {RestorePolicy::Policy::Restore, 0x01},
757 {RestorePolicy::Policy::AlwaysOn, 0x02}
758};
Nan Lifdd8ec52016-08-28 03:57:40 +0800759
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500760} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +0800761
762//----------------------------------------------------------------------
763// Get Chassis Status commands
764//----------------------------------------------------------------------
765ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500766 ipmi_request_t request,
767 ipmi_response_t response,
768 ipmi_data_len_t data_len,
769 ipmi_context_t context)
Nan Lifdd8ec52016-08-28 03:57:40 +0800770{
771 const char *objname = "/org/openbmc/control/power0";
772 const char *intf = "org.openbmc.control.Power";
773
774 sd_bus *bus = NULL;
775 sd_bus_message *reply = NULL;
776 int r = 0;
777 int pgood = 0;
778 char *busname = NULL;
779 ipmi_ret_t rc = IPMI_CC_OK;
780 ipmi_get_chassis_status_t chassis_status{};
781
Nan Lifdd8ec52016-08-28 03:57:40 +0800782 uint8_t s = 0;
783
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500784 using namespace chassis::internal;
785 using namespace chassis::internal::cache;
786 using namespace power_policy;
787
Deepak Kodihallie6027092017-08-27 08:13:37 -0500788 const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf).front();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500789 auto method =
790 dbus.new_method_call(
791 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
792 powerRestoreSetting.c_str(),
Ratan Guptacc8feb42017-07-25 21:52:10 +0530793 ipmi::PROP_INTF,
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500794 "Get");
795 method.append(powerRestoreIntf, "PowerRestorePolicy");
796 auto resp = dbus.call(method);
797 if (resp.is_method_error())
798 {
799 log<level::ERR>("Error in PowerRestorePolicy Get");
800 report<InternalFailure>();
801 *data_len = 0;
802 return IPMI_CC_UNSPECIFIED_ERROR;
803 }
804 sdbusplus::message::variant<std::string> result;
805 resp.read(result);
806 auto powerRestore =
807 RestorePolicy::convertPolicyFromString(result.get<std::string>());
Nan Lifdd8ec52016-08-28 03:57:40 +0800808
809 *data_len = 4;
810
Tom Joseph63a00512017-08-09 23:39:59 +0530811 bus = ipmid_get_sd_bus_connection();
812
Nan Lifdd8ec52016-08-28 03:57:40 +0800813 r = mapper_get_service(bus, objname, &busname);
814 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530815 log<level::ERR>("Failed to get bus name",
816 entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800817 rc = IPMI_CC_UNSPECIFIED_ERROR;
818 goto finish;
819 }
820
821 r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply, "i");
822 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530823 log<level::ERR>("Failed to call sd_bus_get_property",
824 entry("PROPERTY=%s","pgood"),
825 entry("ERRNO=0x%X", -r),
826 entry("BUS=%s", busname),
827 entry("PATH=%s", objname),
828 entry("INTERFACE=%s", intf));
Nan Lifdd8ec52016-08-28 03:57:40 +0800829 rc = IPMI_CC_UNSPECIFIED_ERROR;
830 goto finish;
831 }
832
833 r = sd_bus_message_read(reply, "i", &pgood);
834 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530835 log<level::ERR>("Failed to read sensor:",
836 entry("ERRNO=0x%X", -r));
Nan Lifdd8ec52016-08-28 03:57:40 +0800837 rc = IPMI_CC_UNSPECIFIED_ERROR;
838 goto finish;
839 }
840
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500841 s = dbusToIpmi.at(powerRestore);
Nan Lifdd8ec52016-08-28 03:57:40 +0800842
843 // Current Power State
844 // [7] reserved
845 // [6..5] power restore policy
846 // 00b = chassis stays powered off after AC/mains returns
847 // 01b = after AC returns, power is restored to the state that was
848 // in effect when AC/mains was lost.
849 // 10b = chassis always powers up after AC/mains returns
850 // 11b = unknow
851 // Set to 00b, by observing the hardware behavior.
852 // Do we need to define a dbus property to identify the restore policy?
853
854 // [4] power control fault
855 // 1b = controller attempted to turn system power on or off, but
856 // system did not enter desired state.
857 // Set to 0b, since We don't support it..
858
859 // [3] power fault
860 // 1b = fault detected in main power subsystem.
861 // set to 0b. for we don't support it.
862
863 // [2] 1b = interlock (chassis is presently shut down because a chassis
864 // panel interlock switch is active). (IPMI 1.5)
865 // set to 0b, for we don't support it.
866
867 // [1] power overload
868 // 1b = system shutdown because of power overload condition.
869 // set to 0b, for we don't support it.
870
871 // [0] power is on
872 // 1b = system power is on
873 // 0b = system power is off(soft-off S4/S5, or mechanical off)
874
875 chassis_status.cur_power_state = ((s & 0x3)<<5) | (pgood & 0x1);
876
877 // Last Power Event
878 // [7..5] – reserved
879 // [4] – 1b = last ‘Power is on’ state was entered via IPMI command
880 // [3] – 1b = last power down caused by power fault
881 // [2] – 1b = last power down caused by a power interlock being activated
882 // [1] – 1b = last power down caused by a Power overload
883 // [0] – 1b = AC failed
884 // set to 0x0, for we don't support these fields.
885
886 chassis_status.last_power_event = 0;
887
888 // Misc. Chassis State
889 // [7] – reserved
890 // [6] – 1b = Chassis Identify command and state info supported (Optional)
891 // 0b = Chassis Identify command support unspecified via this command.
892 // (The Get Command Support command , if implemented, would still
893 // indicate support for the Chassis Identify command)
894 // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved (return
895 // as 00b) otherwise. Returns the present chassis identify state.
896 // Refer to the Chassis Identify command for more info.
897 // 00b = chassis identify state = Off
898 // 01b = chassis identify state = Temporary(timed) On
899 // 10b = chassis identify state = Indefinite On
900 // 11b = reserved
901 // [3] – 1b = Cooling/fan fault detected
902 // [2] – 1b = Drive Fault
903 // [1] – 1b = Front Panel Lockout active (power off and reset via chassis
904 // push-buttons disabled.)
905 // [0] – 1b = Chassis Intrusion active
906 // set to 0, for we don't support them.
907 chassis_status.misc_power_state = 0;
908
909 // Front Panel Button Capabilities and disable/enable status(Optional)
910 // set to 0, for we don't support them.
911 chassis_status.front_panel_button_cap_status = 0;
912
913 // Pack the actual response
914 memcpy(response, &chassis_status, *data_len);
915
916finish:
917 free(busname);
918 reply = sd_bus_message_unref(reply);
919
920 return rc;
921}
Chris Austen7888c4d2015-12-03 15:26:20 -0600922
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530923//-------------------------------------------------------------
924// Send a command to SoftPowerOff application to stop any timer
925//-------------------------------------------------------------
926int stop_soft_off_timer()
927{
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530928 constexpr auto iface = "org.freedesktop.DBus.Properties";
929 constexpr auto soft_off_iface = "xyz.openbmc_project.Ipmi.Internal."
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500930 "SoftPowerOff";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530931
932 constexpr auto property = "ResponseReceived";
933 constexpr auto value = "xyz.openbmc_project.Ipmi.Internal."
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500934 "SoftPowerOff.HostResponse.HostShutdown";
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530935
936 // Get the system bus where most system services are provided.
937 auto bus = ipmid_get_sd_bus_connection();
938
939 // Get the service name
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500940 // TODO openbmc/openbmc#1661 - Mapper refactor
941 //
942 // See openbmc/openbmc#1743 for some details but high level summary is that
943 // for now the code will directly call the soft off interface due to a
944 // race condition with mapper usage
945 //
946 //char *busname = nullptr;
947 //auto r = mapper_get_service(bus, SOFTOFF_OBJPATH, &busname);
948 //if (r < 0)
949 //{
950 // fprintf(stderr, "Failed to get %s bus name: %s\n",
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530951 // SOFTOFF_OBJPATH, -r);
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500952 // return r;
953 //}
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530954
955 // No error object or reply expected.
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500956 int rc = sd_bus_call_method(bus, SOFTOFF_BUSNAME, SOFTOFF_OBJPATH, iface,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500957 "Set", nullptr, nullptr, "ssv",
958 soft_off_iface, property, "s", value);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530959 if (rc < 0)
960 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530961 log<level::ERR>("Failed to set property in SoftPowerOff object",
962 entry("ERRNO=0x%X", -rc));
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530963 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500964
Andrew Geissler2b4e4592017-06-08 11:18:35 -0500965 //TODO openbmc/openbmc#1661 - Mapper refactor
966 //free(busname);
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +0530967 return rc;
968}
969
vishwa36993272015-11-20 12:43:49 -0600970//----------------------------------------------------------------------
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500971// Create file to indicate there is no need for softoff notification to host
972//----------------------------------------------------------------------
973void indicate_no_softoff_needed()
974{
975 fs::path path{HOST_INBAND_REQUEST_DIR};
976 if (!fs::is_directory(path))
977 {
978 fs::create_directory(path);
979 }
980
981 // Add the host instance (default 0 for now) to the file name
982 std::string file{HOST_INBAND_REQUEST_FILE};
983 auto size = std::snprintf(nullptr,0,file.c_str(),0);
984 size++; // null
985 std::unique_ptr<char[]> buf(new char[size]);
986 std::snprintf(buf.get(),size,file.c_str(),0);
987
988 // Append file name to directory and create it
989 path /= buf.get();
990 std::ofstream(path.c_str());
991}
992
993//----------------------------------------------------------------------
vishwa36993272015-11-20 12:43:49 -0600994// Chassis Control commands
995//----------------------------------------------------------------------
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500996ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
997 ipmi_request_t request,
998 ipmi_response_t response,
999 ipmi_data_len_t data_len,
1000 ipmi_context_t context)
vishwa36993272015-11-20 12:43:49 -06001001{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001002 // Error from power off.
1003 int rc = 0;
vishwa36993272015-11-20 12:43:49 -06001004
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001005 // No response for this command.
vishwa36993272015-11-20 12:43:49 -06001006 *data_len = 0;
1007
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001008 // Catch the actual operaton by peeking into request buffer
1009 uint8_t chassis_ctrl_cmd = *(uint8_t *)request;
vishwa36993272015-11-20 12:43:49 -06001010
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001011 switch(chassis_ctrl_cmd)
1012 {
1013 case CMD_POWER_ON:
1014 rc = initiate_state_transition(State::Host::Transition::On);
1015 break;
1016 case CMD_POWER_OFF:
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301017 // This path would be hit in 2 conditions.
1018 // 1: When user asks for power off using ipmi chassis command 0x04
1019 // 2: Host asking for power off post shutting down.
1020
1021 // If it's a host requested power off, then need to nudge Softoff
1022 // application that it needs to stop the watchdog timer if running.
1023 // If it is a user requested power off, then this is not really
1024 // needed. But then we need to differentiate between user and host
1025 // calling this same command
1026
1027 // For now, we are going ahead with trying to nudge the soft off and
1028 // interpret the failure to do so as a non softoff case
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001029 rc = stop_soft_off_timer();
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301030
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001031 // Only request the Off transition if the soft power off
1032 // application is not running
1033 if (rc < 0)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001034 {
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001035 // First create a file to indicate to the soft off application
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301036 // that it should not run. Not doing this will result in State
1037 // manager doing a default soft power off when asked for power
1038 // off.
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001039 indicate_no_softoff_needed();
1040
1041 // Now request the shutdown
1042 rc = initiate_state_transition(State::Host::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001043 }
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001044 else
1045 {
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301046 log<level::INFO>("Soft off is running, so let shutdown target "
1047 "stop the host");
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001048 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001049 break;
Vishwanatha Subbanna83b5c1c2017-01-25 18:41:51 +05301050
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001051 case CMD_HARD_RESET:
1052 case CMD_POWER_CYCLE:
1053 // SPEC has a section that says certain implementations can trigger
1054 // PowerOn if power is Off when a command to power cycle is
1055 // requested
Andrew Geisslera6e3a302017-05-31 19:34:00 -05001056
1057 // First create a file to indicate to the soft off application
1058 // that it should not run since this is a direct user initiated
1059 // power reboot request (i.e. a reboot request that is not
1060 // originating via a soft power off SMS request)
1061 indicate_no_softoff_needed();
1062
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001063 rc = initiate_state_transition(State::Host::Transition::Reboot);
1064 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301065
1066 case CMD_SOFT_OFF_VIA_OVER_TEMP:
1067 // Request Host State Manager to do a soft power off
1068 rc = initiate_state_transition(State::Host::Transition::Off);
1069 break;
1070
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001071 default:
1072 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301073 log<level::ERR>("Invalid Chassis Control command",
1074 entry("CMD=0x%X", chassis_ctrl_cmd));
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001075 rc = -1;
1076 }
1077 }
vishwa36993272015-11-20 12:43:49 -06001078
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001079 return ( (rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
vishwa36993272015-11-20 12:43:49 -06001080}
1081
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001082/** @brief Return D-Bus connection string to enclosure identify LED object
1083 *
1084 * @param[in, out] connection - connection to D-Bus object
1085 * @return a IPMI return code
1086 */
1087std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301088{
Tom Joseph5110c122018-03-23 17:55:40 +05301089 // lookup enclosure_identify group owner(s) in mapper
1090 auto mapperCall = chassis::internal::dbus.new_method_call(
1091 ipmi::MAPPER_BUS_NAME,
1092 ipmi::MAPPER_OBJ,
1093 ipmi::MAPPER_INTF,
1094 "GetObject");
1095
1096 mapperCall.append(identify_led_object_name);
1097 static const std::vector<std::string> interfaces =
1098 {
1099 "xyz.openbmc_project.Led.Group"
1100 };
1101 mapperCall.append(interfaces);
1102 auto mapperReply = chassis::internal::dbus.call(mapperCall);
1103 if (mapperReply.is_method_error())
1104 {
1105 log<level::ERR>("Chassis Identify: Error communicating to mapper.");
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001106 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301107 }
1108 std::vector<std::pair<std::string, std::vector<std::string>>> mapperResp;
1109 mapperReply.read(mapperResp);
1110
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001111 if (mapperResp.size() != encIdentifyObjectsSize)
Tom Joseph5110c122018-03-23 17:55:40 +05301112 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001113 log<level::ERR>("Invalid number of enclosure identify objects.",
1114 entry("ENC_IDENTITY_OBJECTS_SIZE=%d", mapperResp.size()));
1115 elog<InternalFailure>();
1116 }
1117 auto pair = mapperResp[encIdentifyObjectsSize - 1];
1118 return pair.first;
1119}
Tom Joseph5110c122018-03-23 17:55:40 +05301120
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001121/** @brief Turn On/Off enclosure identify LED
1122 *
1123 * @param[in] flag - true to turn on LED, false to turn off
1124 * @return a IPMI return code
1125 */
1126void enclosureIdentifyLed(bool flag)
1127{
1128 using namespace chassis::internal;
1129 std::string connection = std::move(getEnclosureIdentifyConnection());
1130 auto led = dbus.new_method_call(connection.c_str(),
1131 identify_led_object_name, "org.freedesktop.DBus.Properties", "Set");
1132 led.append("xyz.openbmc_project.Led.Group", "Asserted",
1133 sdbusplus::message::variant<bool>(flag));
1134 auto ledReply = dbus.call(led);
1135 if (ledReply.is_method_error())
1136 {
1137 log<level::ERR>("Chassis Identify: Error Setting State On/Off\n",
1138 entry("LED_STATE=%d", flag));
1139 elog<InternalFailure>();
1140 }
1141}
1142
1143/** @brief Callback method to turn off LED
1144 */
1145void enclosureIdentifyLedOff()
1146{
1147 try
1148 {
1149 enclosureIdentifyLed(false);
1150 }
1151 catch (const InternalFailure& e)
1152 {
1153 report<InternalFailure>();
1154 }
1155}
1156
1157/** @brief Create timer to turn on and off the enclosure LED
1158 */
1159void createIdentifyTimer()
1160{
1161 if (!identifyTimer)
1162 {
1163 identifyTimer = std::make_unique<phosphor::ipmi::Timer>(
1164 ipmid_get_sd_event_connection(), enclosureIdentifyLedOff);
1165 }
1166}
1167
1168ipmi_ret_t ipmi_chassis_identify(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1169 ipmi_request_t request,
1170 ipmi_response_t response,
1171 ipmi_data_len_t data_len,
1172 ipmi_context_t context)
1173{
1174 if (*data_len > chassisIdentifyReqLength)
1175 {
1176 return IPMI_CC_REQ_DATA_LEN_INVALID;
1177 }
1178 uint8_t identifyInterval = *data_len > identifyIntervalPos ?
1179 (static_cast<uint8_t*>(request))[identifyIntervalPos] :
1180 DEFAULT_IDENTIFY_TIME_OUT;
1181 bool forceIdentify = (*data_len == chassisIdentifyReqLength) ?
1182 (static_cast<uint8_t*>(request))[forceIdentifyPos] & 0x01 : false;
1183 if (identifyInterval || forceIdentify)
1184 {
1185 // stop the timer if already started, for force identify we should
1186 // not turn off LED
1187 identifyTimer->setTimer(SD_EVENT_OFF);
1188 try
Tom Joseph5110c122018-03-23 17:55:40 +05301189 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001190 enclosureIdentifyLed(true);
1191 }
1192 catch (const InternalFailure& e)
1193 {
1194 report<InternalFailure>();
1195 return IPMI_CC_RESPONSE_ERROR;
Tom Joseph5110c122018-03-23 17:55:40 +05301196 }
1197
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001198 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301199 {
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001200 return IPMI_CC_OK;
1201 }
1202 // start the timer
1203 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
1204 std::chrono::seconds(identifyInterval));
1205 identifyTimer->startTimer(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301206 }
1207 return IPMI_CC_OK;
1208}
1209
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001210namespace boot_options
1211{
1212
1213using namespace sdbusplus::xyz::openbmc_project::Control::Boot::server;
1214using IpmiValue = uint8_t;
1215constexpr auto ipmiDefault = 0;
1216
1217std::map<IpmiValue, Source::Sources> sourceIpmiToDbus =
1218{
1219 {0x01, Source::Sources::Network},
1220 {0x02, Source::Sources::Disk},
1221 {0x05, Source::Sources::ExternalMedia},
1222 {ipmiDefault, Source::Sources::Default}
shgoupfd84fbbf2015-12-17 10:05:51 +08001223};
1224
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001225std::map<IpmiValue, Mode::Modes> modeIpmiToDbus =
1226{
1227 {0x03, Mode::Modes::Safe},
1228 {0x06, Mode::Modes::Setup},
1229 {ipmiDefault, Mode::Modes::Regular}
shgoupfd84fbbf2015-12-17 10:05:51 +08001230};
1231
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001232std::map<Source::Sources, IpmiValue> sourceDbusToIpmi =
1233{
1234 {Source::Sources::Network, 0x01},
1235 {Source::Sources::Disk, 0x02},
1236 {Source::Sources::ExternalMedia, 0x05},
1237 {Source::Sources::Default, ipmiDefault}
1238};
shgoupfd84fbbf2015-12-17 10:05:51 +08001239
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001240std::map<Mode::Modes, IpmiValue> modeDbusToIpmi =
1241{
1242 {Mode::Modes::Safe, 0x03},
1243 {Mode::Modes::Setup, 0x06},
1244 {Mode::Modes::Regular, ipmiDefault}
1245};
shgoupfd84fbbf2015-12-17 10:05:51 +08001246
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001247} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001248
Marri Devender Rao81719702018-05-07 00:53:48 -05001249
1250/** @brief Set the property value for boot source
1251 * @param[in] source - boot source value
1252 * @return On failure return IPMI error.
1253 */
1254static ipmi_ret_t setBootSource(const Source::Sources& source)
1255{
1256 using namespace chassis::internal;
1257 using namespace chassis::internal::cache;
1258 sdbusplus::message::variant<std::string> property =
1259 convertForMessage(source);
1260 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1261 const auto& bootSourceSetting = std::get<settings::Path>(bootSetting);
1262 auto method =
1263 dbus.new_method_call(
1264 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1265 bootSourceSetting.c_str(), ipmi::PROP_INTF, "Set");
1266 method.append(bootSourceIntf, "BootSource", property);
1267 auto reply = dbus.call(method);
1268 if (reply.is_method_error())
1269 {
1270 log<level::ERR>("Error in BootSource Set");
1271 report<InternalFailure>();
1272 return IPMI_CC_UNSPECIFIED_ERROR;
1273 }
1274 return IPMI_CC_OK;
1275}
1276
1277 /** @brief Set the property value for boot mode
1278 * @param[in] mode - boot mode value
1279 * @return On failure return IPMI error.
1280 */
1281static ipmi_ret_t setBootMode(const Mode::Modes& mode)
1282{
1283 using namespace chassis::internal;
1284 using namespace chassis::internal::cache;
1285 sdbusplus::message::variant<std::string> property =
1286 convertForMessage(mode);
1287 auto bootSetting = settings::boot::setting(objects, bootModeIntf);
1288 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1289 auto method =
1290 dbus.new_method_call(
1291 objects.service(bootModeSetting, bootModeIntf).c_str(),
1292 bootModeSetting.c_str(), ipmi::PROP_INTF, "Set");
1293 method.append(bootModeIntf, "BootMode", property);
1294 auto reply = dbus.call(method);
1295 if (reply.is_method_error())
1296 {
1297 log<level::ERR>("Error in BootMode Set");
1298 report<InternalFailure>();
1299 return IPMI_CC_UNSPECIFIED_ERROR;
1300 }
1301 return IPMI_CC_OK;
1302}
1303
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001304ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1305 ipmi_request_t request,
1306 ipmi_response_t response,
1307 ipmi_data_len_t data_len,
1308 ipmi_context_t context)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001309{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001310 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001311 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
1312 char *p = NULL;
1313 get_sys_boot_options_response_t *resp = (get_sys_boot_options_response_t *) response;
1314 get_sys_boot_options_t *reqptr = (get_sys_boot_options_t*) request;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001315 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001316
shgoupfd84fbbf2015-12-17 10:05:51 +08001317 memset(resp,0,sizeof(*resp));
1318 resp->version = SET_PARM_VERSION;
1319 resp->parm = 5;
ratagupta6f6bff2016-04-04 06:20:11 -05001320 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001321
Adriana Kobylak40814c62015-10-27 15:58:44 -05001322
shgoupfd84fbbf2015-12-17 10:05:51 +08001323 /*
1324 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1325 * This is the only parameter used by petitboot.
1326 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001327 if ( reqptr->parameter == static_cast<uint8_t>
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001328 ( BootOptionParameter::BOOT_FLAGS )) {
shgoupfd84fbbf2015-12-17 10:05:51 +08001329
Ratan Guptafd28dd72016-08-01 04:58:01 -05001330 *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS);
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001331 using namespace chassis::internal;
1332 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001333
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001334 try
ratagupta6f6bff2016-04-04 06:20:11 -05001335 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001336 auto bootSetting = settings::boot::setting(objects, bootSourceIntf);
1337 const auto& bootSourceSetting =
1338 std::get<settings::Path>(bootSetting);
1339 auto oneTimeEnabled =
1340 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1341 auto method =
1342 dbus.new_method_call(
1343 objects.service(bootSourceSetting, bootSourceIntf).c_str(),
1344 bootSourceSetting.c_str(),
1345 ipmi::PROP_INTF,
1346 "Get");
1347 method.append(bootSourceIntf, "BootSource");
1348 auto reply = dbus.call(method);
1349 if (reply.is_method_error())
1350 {
1351 log<level::ERR>("Error in BootSource Get");
1352 report<InternalFailure>();
1353 *data_len = 0;
1354 return IPMI_CC_UNSPECIFIED_ERROR;
1355 }
1356 sdbusplus::message::variant<std::string> result;
1357 reply.read(result);
1358 auto bootSource =
1359 Source::convertSourcesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001360
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001361 bootSetting = settings::boot::setting(objects, bootModeIntf);
1362 const auto& bootModeSetting = std::get<settings::Path>(bootSetting);
1363 method = dbus.new_method_call(
1364 objects.service(bootModeSetting, bootModeIntf).
1365 c_str(),
1366 bootModeSetting.c_str(),
1367 ipmi::PROP_INTF,
1368 "Get");
1369 method.append(bootModeIntf, "BootMode");
1370 reply = dbus.call(method);
1371 if (reply.is_method_error())
1372 {
1373 log<level::ERR>("Error in BootMode Get");
1374 report<InternalFailure>();
1375 *data_len = 0;
1376 return IPMI_CC_UNSPECIFIED_ERROR;
1377 }
1378 reply.read(result);
1379 auto bootMode =
1380 Mode::convertModesFromString(result.get<std::string>());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001381
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001382 bootOption = sourceDbusToIpmi.at(bootSource);
1383 if ((Mode::Modes::Regular == bootMode) &&
1384 (Source::Sources::Default == bootSource))
1385 {
1386 bootOption = ipmiDefault;
1387 }
1388 else if (Source::Sources::Default == bootSource)
1389 {
1390 bootOption = modeDbusToIpmi.at(bootMode);
1391 }
1392 resp->data[1] = (bootOption << 2);
ratagupta6f6bff2016-04-04 06:20:11 -05001393
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001394 resp->data[0] = oneTimeEnabled ?
1395 SET_PARM_BOOT_FLAGS_VALID_ONE_TIME:
1396 SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
ratagupta6f6bff2016-04-04 06:20:11 -05001397
ratagupta6f6bff2016-04-04 06:20:11 -05001398 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -05001399 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001400 catch (InternalFailure& e)
1401 {
1402 report<InternalFailure>();
1403 *data_len = 0;
1404 return IPMI_CC_UNSPECIFIED_ERROR;
1405 }
Ratan Guptafd28dd72016-08-01 04:58:01 -05001406 } else if ( reqptr->parameter == static_cast<uint8_t>
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001407 ( BootOptionParameter::OPAL_NETWORK_SETTINGS )) {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001408
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001409 *data_len = static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001410
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001411 resp->parm = static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001412
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001413 int ret = getHostNetworkData(resp);
Ratan Guptafd28dd72016-08-01 04:58:01 -05001414
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001415 if (ret < 0) {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001416
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301417 log<level::ERR>(
1418 "getHostNetworkData failed for get_sys_boot_options.");
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001419 rc = IPMI_CC_UNSPECIFIED_ERROR;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001420
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001421 }else
1422 rc = IPMI_CC_OK;
Ratan Guptafd28dd72016-08-01 04:58:01 -05001423 }
1424
1425 else {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301426 log<level::ERR>("Unsupported parameter", entry(
1427 "PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001428 }
1429
1430 if (p)
1431 free(p);
1432
Ratan Guptafd28dd72016-08-01 04:58:01 -05001433 if (rc == IPMI_CC_OK)
1434 {
1435 *data_len += 2;
1436 }
1437
shgoupfd84fbbf2015-12-17 10:05:51 +08001438 return rc;
1439}
1440
1441
1442
1443ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001444 ipmi_request_t request,
1445 ipmi_response_t response,
1446 ipmi_data_len_t data_len,
1447 ipmi_context_t context)
shgoupfd84fbbf2015-12-17 10:05:51 +08001448{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001449 using namespace boot_options;
shgoupfd84fbbf2015-12-17 10:05:51 +08001450 ipmi_ret_t rc = IPMI_CC_OK;
shgoupfd84fbbf2015-12-17 10:05:51 +08001451 set_sys_boot_options_t *reqptr = (set_sys_boot_options_t *) request;
1452
Ratan Guptafd28dd72016-08-01 04:58:01 -05001453 printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",reqptr->parameter);
1454
shgoupfd84fbbf2015-12-17 10:05:51 +08001455 // This IPMI command does not have any resposne data
1456 *data_len = 0;
1457
1458 /* 000101
1459 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1460 * This is the only parameter used by petitboot.
1461 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05001462
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001463 if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS)
1464 {
1465 IpmiValue bootOption = ((reqptr->data[1] & 0x3C) >> 2);
1466 using namespace chassis::internal;
1467 using namespace chassis::internal::cache;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001468 auto oneTimeEnabled = false;
1469 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
Tom Joseph57e8eb72017-09-25 18:05:02 +05301470 constexpr auto oneTimePath =
1471 "/xyz/openbmc_project/control/host0/boot/one_time";
shgoupfd84fbbf2015-12-17 10:05:51 +08001472
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001473 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001474 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001475 bool permanent =
1476 (reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
1477 SET_PARM_BOOT_FLAGS_PERMANENT;
1478
Tom Joseph57e8eb72017-09-25 18:05:02 +05301479 auto bootSetting =
1480 settings::boot::setting(objects, bootSourceIntf);
1481
1482 oneTimeEnabled =
1483 std::get<settings::boot::OneTimeEnabled>(bootSetting);
1484
1485 /*
1486 * Check if the current boot setting is onetime or permanent, if the
1487 * request in the command is otherwise, then set the "Enabled"
1488 * property in one_time object path to 'True' to indicate onetime
1489 * and 'False' to indicate permanent.
1490 *
1491 * Once the onetime/permanent setting is applied, then the bootMode
1492 * and bootSource is updated for the corresponding object.
1493 */
1494 if ((permanent && oneTimeEnabled) ||
1495 (!permanent && !oneTimeEnabled))
1496 {
1497 auto service = ipmi::getService(dbus, enabledIntf, oneTimePath);
1498
1499 ipmi::setDbusProperty(dbus,
1500 service,
1501 oneTimePath,
1502 enabledIntf,
1503 "Enabled",
1504 !permanent);
1505 }
1506
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001507 auto modeItr = modeIpmiToDbus.find(bootOption);
1508 auto sourceItr = sourceIpmiToDbus.find(bootOption);
1509 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001510 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001511 rc = setBootSource(sourceItr->second);
1512 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001513 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001514 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001515 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001516 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001517 // If a set boot device is mapping to a boot source, then reset
1518 // the boot mode D-Bus property to default.
1519 // This way the ipmid code can determine which property is not
1520 // at the default value
1521 if(sourceItr->second != Source::Sources::Default)
1522 {
1523 setBootMode(Mode::Modes::Regular);
1524 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001525 }
1526 if (modeIpmiToDbus.end() != modeItr)
1527 {
Marri Devender Rao81719702018-05-07 00:53:48 -05001528 rc = setBootMode(modeItr->second);
1529 if (rc != IPMI_CC_OK)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001530 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001531 *data_len = 0;
Marri Devender Rao81719702018-05-07 00:53:48 -05001532 return rc;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001533 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05001534 // If a set boot device is mapping to a boot mode, then reset
1535 // the boot source D-Bus property to default.
1536 // This way the ipmid code can determine which property is not
1537 // at the default value
1538 if(modeItr->second != Mode::Modes::Regular)
1539 {
1540 setBootSource(Source::Sources::Default);
1541 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001542 }
1543 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001544 catch (InternalFailure& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001545 {
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001546 report<InternalFailure>();
1547 *data_len = 0;
1548 return IPMI_CC_UNSPECIFIED_ERROR;
shgoupfd84fbbf2015-12-17 10:05:51 +08001549 }
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001550 } else if (reqptr->parameter ==
1551 (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS) {
Ratan Guptafd28dd72016-08-01 04:58:01 -05001552
1553 int ret = setHostNetworkData(reqptr);
1554 if (ret < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301555 log<level::ERR>(
1556 "setHostNetworkData failed for set_sys_boot_options");
Ratan Guptafd28dd72016-08-01 04:58:01 -05001557 rc = IPMI_CC_UNSPECIFIED_ERROR;
1558 }
Tom Josephf536c902017-09-25 18:08:15 +05301559 } else if (reqptr->parameter ==
1560 static_cast<uint8_t>(BootOptionParameter::BOOT_INFO)) {
1561 // Handle parameter #4 and return command completed normally
1562 // (IPMI_CC_OK). There is no implementation in OpenBMC for this
1563 // parameter. This is added to support the ipmitool command `chassis
1564 // bootdev` which sends set on parameter #4, before setting the boot
1565 // flags.
1566 rc = IPMI_CC_OK;
1567 } else {
Aditya Saripalli5fb14602017-11-09 14:46:27 +05301568 log<level::ERR>("Unsupported parameter", entry(
1569 "PARAM=0x%x", reqptr->parameter));
shgoupfd84fbbf2015-12-17 10:05:51 +08001570 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001571 }
1572
1573 return rc;
1574}
1575
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001576ipmi_ret_t ipmiGetPOHCounter(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1577 ipmi_request_t request, ipmi_response_t response,
1578 ipmi_data_len_t data_len, ipmi_context_t context)
1579{
1580 // sd_bus error
1581 ipmi_ret_t rc = IPMI_CC_OK;
1582
1583 auto resptr = reinterpret_cast<GetPOHCountResponse*>(response);
1584
1585 try
1586 {
1587 auto pohCounter = getPOHCounter();
1588 resptr->counterReading[0] = pohCounter;
1589 resptr->counterReading[1] = pohCounter >> 8;
1590 resptr->counterReading[2] = pohCounter >> 16;
1591 resptr->counterReading[3] = pohCounter >> 24;
1592 }
1593 catch (std::exception& e)
1594 {
1595 log<level::ERR>(e.what());
1596 return IPMI_CC_UNSPECIFIED_ERROR;
1597 }
1598
1599 resptr->minPerCount = poh::minutesPerCount;
1600 *data_len = sizeof(GetPOHCountResponse);
1601
1602 return rc;
1603}
1604
Adriana Kobylak40814c62015-10-27 15:58:44 -05001605void register_netfn_chassis_functions()
1606{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001607 createIdentifyTimer();
1608
Tom05732372016-09-06 17:21:23 +05301609 // <Wildcard Command>
Tom05732372016-09-06 17:21:23 +05301610 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL, ipmi_chassis_wildcard,
1611 PRIVILEGE_USER);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001612
Tom05732372016-09-06 17:21:23 +05301613 // Get Chassis Capabilities
Tom05732372016-09-06 17:21:23 +05301614 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL, ipmi_get_chassis_cap,
1615 PRIVILEGE_USER);
Nan Li8d15fb42016-08-16 22:29:40 +08001616
Tom05732372016-09-06 17:21:23 +05301617 // <Get System Boot Options>
Tom05732372016-09-06 17:21:23 +05301618 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL,
1619 ipmi_chassis_get_sys_boot_options, PRIVILEGE_OPERATOR);
Adriana Kobylak40814c62015-10-27 15:58:44 -05001620
Tom05732372016-09-06 17:21:23 +05301621 // <Get Chassis Status>
Tom05732372016-09-06 17:21:23 +05301622 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL, ipmi_get_chassis_status,
1623 PRIVILEGE_USER);
Nan Lifdd8ec52016-08-28 03:57:40 +08001624
Tom05732372016-09-06 17:21:23 +05301625 // <Chassis Control>
Tom05732372016-09-06 17:21:23 +05301626 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL, ipmi_chassis_control,
1627 PRIVILEGE_OPERATOR);
shgoupfd84fbbf2015-12-17 10:05:51 +08001628
Tom Joseph5110c122018-03-23 17:55:40 +05301629 // <Chassis Identify>
Tom Joseph5110c122018-03-23 17:55:40 +05301630 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_IDENTIFY, NULL,
1631 ipmi_chassis_identify, PRIVILEGE_OPERATOR);
1632
Tom05732372016-09-06 17:21:23 +05301633 // <Set System Boot Options>
Tom05732372016-09-06 17:21:23 +05301634 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL,
1635 ipmi_chassis_set_sys_boot_options, PRIVILEGE_OPERATOR);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05001636 // <Get POH Counter>
1637 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_POH_COUNTER, NULL,
1638 ipmiGetPOHCounter, PRIVILEGE_USER);
vishwa36993272015-11-20 12:43:49 -06001639}