blob: 8c7bddc7379539974c074a081c8226f2ce9b100c [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "chassishandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Patrick Venture0b02be92018-08-31 11:55:55 -07005#include <arpa/inet.h>
6#include <endian.h>
7#include <limits.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include <netinet/in.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07009
Vernon Mauerye08fbff2019-04-03 09:19:34 -070010#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070011#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070012#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070013#include <phosphor-logging/elog-errors.hpp>
Vernon Mauery38108d92024-03-11 12:53:55 -070014#include <phosphor-logging/lg2.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070015#include <sdbusplus/bus.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070016#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070017#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070018#include <sdbusplus/timer.hpp>
Vernon Mauerye278ead2018-10-09 09:23:43 -070019#include <settings.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070020#include <xyz/openbmc_project/Common/error.hpp>
21#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
22#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +030023#include <xyz/openbmc_project/Control/Boot/Type/server.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070024#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Jason M. Billsad588bf2020-01-30 16:18:33 -080025#include <xyz/openbmc_project/State/Chassis/server.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070026#include <xyz/openbmc_project/State/Host/server.hpp>
27#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
28
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050029#include <array>
30#include <chrono>
31#include <cstring>
32#include <filesystem>
33#include <fstream>
34#include <future>
35#include <map>
36#include <sstream>
37#include <string>
38
Patrick Williams95655222023-12-05 12:45:02 -060039std::unique_ptr<sdbusplus::Timer> identifyTimer
Lei YU4b0ddb62019-01-25 16:43:50 +080040 __attribute__((init_priority(101)));
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050041
Yong Lif4e38512019-05-21 14:46:55 +080042static ChassisIDState chassisIDState = ChassisIDState::reserved;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000043static constexpr uint8_t setParmVersion = 0x01;
Yong Lif4e38512019-05-21 14:46:55 +080044
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000045constexpr size_t sizeVersion = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053046constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053047
Patrick Venture0b02be92018-08-31 11:55:55 -070048// PetiBoot-Specific
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000049static constexpr uint8_t netConfInitialBytes[] = {0x80, 0x21, 0x70, 0x62,
50 0x21, 0x00, 0x01, 0x06};
51static constexpr uint8_t oemParmStart = 96;
52static constexpr uint8_t oemParmEnd = 127;
Ratan Guptafd28dd72016-08-01 04:58:01 -050053
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000054static constexpr size_t cookieOffset = 1;
55static constexpr size_t versionOffset = 5;
56static constexpr size_t addrSizeOffset = 8;
57static constexpr size_t macOffset = 9;
58static constexpr size_t addrTypeOffset = 16;
59static constexpr size_t ipAddrOffset = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050060
Marri Devender Rao6706c1c2018-05-14 00:29:38 -050061static constexpr size_t encIdentifyObjectsSize = 1;
62static constexpr size_t chassisIdentifyReqLength = 2;
63static constexpr size_t identifyIntervalPos = 0;
64static constexpr size_t forceIdentifyPos = 1;
shgoupfd84fbbf2015-12-17 10:05:51 +080065
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000066namespace ipmi
67{
68constexpr Cc ccParmNotSupported = 0x80;
69
70static inline auto responseParmNotSupported()
71{
72 return response(ccParmNotSupported);
73}
74} // namespace ipmi
75
Adriana Kobylak40814c62015-10-27 15:58:44 -050076void register_netfn_chassis_functions() __attribute__((constructor));
77
shgoupfd84fbbf2015-12-17 10:05:51 +080078// Host settings in dbus
79// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070080const char* settings_object_name = "/org/openbmc/settings/host0";
81const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070082const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053083 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080084
Ratan Guptadcb10672017-07-10 10:33:50 +053085constexpr auto SETTINGS_ROOT = "/";
86constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053087
88constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
89constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
90
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050091static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
92static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070093 "xyz.openbmc_project.State.PowerOnHours";
Patrick Williams7345ac42021-04-30 20:57:39 -050094static constexpr auto pohCounterProperty = "POHCounter";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050095static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080096const static constexpr char chassisCapIntf[] =
97 "xyz.openbmc_project.Control.ChassisCapabilities";
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -080098const static constexpr char chassisIntrusionProp[] = "ChassisIntrusionEnabled";
99const static constexpr char chassisFrontPanelLockoutProp[] =
100 "ChassisFrontPanelLockoutEnabled";
101const static constexpr char chassisNMIProp[] = "ChassisNMIEnabled";
102const static constexpr char chassisPowerInterlockProp[] =
103 "ChassisPowerInterlockEnabled";
Yong Liae4b0402018-11-02 11:12:14 +0800104const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
105const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
106const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
107const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
108const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
109static constexpr uint8_t chassisCapFlagMask = 0x0f;
110static constexpr uint8_t chassisCapAddrMask = 0xfe;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700111static constexpr const char* powerButtonIntf =
112 "xyz.openbmc_project.Chassis.Buttons.Power";
113static constexpr const char* powerButtonPath =
114 "/xyz/openbmc_project/Chassis/Buttons/Power0";
115static constexpr const char* resetButtonIntf =
116 "xyz.openbmc_project.Chassis.Buttons.Reset";
117static constexpr const char* resetButtonPath =
118 "/xyz/openbmc_project/Chassis/Buttons/Reset0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530119
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530120// Phosphor Host State manager
Willy Tu523e2d12023-09-05 11:36:48 -0700121namespace State = sdbusplus::server::xyz::openbmc_project::state;
Vernon Mauery185b9f82018-07-20 10:52:36 -0700122namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500123
Ratan Guptadcb10672017-07-10 10:33:50 +0530124using namespace phosphor::logging;
Willy Tu523e2d12023-09-05 11:36:48 -0700125using namespace sdbusplus::error::xyz::openbmc_project::common;
126using namespace sdbusplus::server::xyz::openbmc_project::control::boot;
William A. Kennington III4c008022018-10-12 17:18:14 -0700127
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500128namespace chassis
129{
130namespace internal
131{
132
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +0300133constexpr auto bootSettingsPath = "/xyz/openbmc_project/control/host0/boot";
134constexpr auto bootEnableIntf = "xyz.openbmc_project.Object.Enable";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500135constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +0300136constexpr auto bootTypeIntf = "xyz.openbmc_project.Control.Boot.Type";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500137constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +0300138constexpr auto bootSettingsOneTimePath =
139 "/xyz/openbmc_project/control/host0/boot/one_time";
140constexpr auto bootOneTimeIntf = "xyz.openbmc_project.Object.Enable";
141
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500142constexpr auto powerRestoreIntf =
143 "xyz.openbmc_project.Control.Power.RestorePolicy";
Patrick Williams5d82f472022-07-22 19:26:53 -0500144sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500145
146namespace cache
147{
148
James Feist225dec82019-11-26 16:25:06 -0800149std::unique_ptr<settings::Objects> objectsPtr = nullptr;
150
151settings::Objects& getObjects()
152{
153 if (objectsPtr == nullptr)
154 {
155 objectsPtr = std::make_unique<settings::Objects>(
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +0300156 dbus, std::vector<std::string>{bootModeIntf, bootTypeIntf,
157 bootSourceIntf, powerRestoreIntf});
James Feist225dec82019-11-26 16:25:06 -0800158 }
159 return *objectsPtr;
160}
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500161
162} // namespace cache
163} // namespace internal
164} // namespace chassis
165
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500166namespace poh
167{
168
169constexpr auto minutesPerCount = 60;
170
171} // namespace poh
172
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000173int getHostNetworkData(ipmi::message::Payload& payload)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500174{
Ratan Guptadcb10672017-07-10 10:33:50 +0530175 ipmi::PropertyMap properties;
176 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530177 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500178
Ratan Guptadcb10672017-07-10 10:33:50 +0530179 try
180 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700181 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530182 // objects,to handle such cases we are interested on that object
183 // which are on interested busname.
184 // Currenlty mapper doesn't give the readable busname(gives busid)
185 // so we can't match with bus name so giving some object specific info
186 // as SETTINGS_MATCH.
187 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500188
Patrick Williams5d82f472022-07-22 19:26:53 -0500189 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530190
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530191 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
192 SETTINGS_ROOT, SETTINGS_MATCH);
193
194 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
195 SETTINGS_ROOT, SETTINGS_MATCH);
196
Patrick Venture0b02be92018-08-31 11:55:55 -0700197 properties = ipmi::getAllDbusProperties(
198 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
199 auto variant = ipmi::getDbusProperty(bus, macObjectInfo.second,
200 macObjectInfo.first, MAC_INTERFACE,
201 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530202
Vernon Maueryf442e112019-04-09 11:44:36 -0700203 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530204
Vernon Maueryf442e112019-04-09 11:44:36 -0700205 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530206
Vernon Maueryf442e112019-04-09 11:44:36 -0700207 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530208
Patrick Venture0b02be92018-08-31 11:55:55 -0700209 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700210 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700211 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
212 ? 1
213 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530214
Vernon Maueryf442e112019-04-09 11:44:36 -0700215 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530216
Ratan Guptad70f4532017-08-04 02:07:31 +0530217 // it is expected here that we should get the valid data
218 // but we may also get the default values.
219 // Validation of the data is done by settings.
220 //
221 // if mac address is default mac address then
222 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530223 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530224 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530225 rc = -1;
226 return rc;
227 }
228 // if addr is static then ipaddress,gateway,prefix
229 // should not be default one,don't send blank override.
230 if (isStatic)
231 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700232 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
233 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530234 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530235 rc = -1;
236 return rc;
237 }
238 }
239
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000240 std::string token;
241 std::stringstream ss(MACAddress);
Ratan Guptadcb10672017-07-10 10:33:50 +0530242
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000243 // First pack macOffset no of bytes in payload.
244 // Latter this PetiBoot-Specific data will be populated.
245 std::vector<uint8_t> payloadInitialBytes(macOffset);
246 payload.pack(payloadInitialBytes);
Ratan Guptadcb10672017-07-10 10:33:50 +0530247
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000248 while (std::getline(ss, token, ':'))
249 {
250 payload.pack(stoi(token, nullptr, 16));
251 }
252
253 payload.pack(0x00);
254
255 payload.pack(isStatic);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530256
Vernon Maueryf442e112019-04-09 11:44:36 -0700257 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
258 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
259 ? AF_INET
260 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530261
Patrick Venture0b02be92018-08-31 11:55:55 -0700262 addrSize = (addressFamily == AF_INET)
263 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
264 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530265
266 // ipaddress and gateway would be in IPv4 format
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000267 std::vector<uint8_t> addrInBinary(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530268 inet_pton(addressFamily, ipAddress.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000269 reinterpret_cast<void*>(addrInBinary.data()));
Ratan Guptadcb10672017-07-10 10:33:50 +0530270
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000271 payload.pack(addrInBinary);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530272
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000273 payload.pack(prefix);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530274
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000275 std::vector<uint8_t> gatewayDetails(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530276 inet_pton(addressFamily, gateway.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000277 reinterpret_cast<void*>(gatewayDetails.data()));
278 payload.pack(gatewayDetails);
Ratan Guptadcb10672017-07-10 10:33:50 +0530279 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500280 catch (const InternalFailure& e)
Ratan Guptadcb10672017-07-10 10:33:50 +0530281 {
282 commit<InternalFailure>();
Ratan Guptadcb10672017-07-10 10:33:50 +0530283 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500284 return rc;
285 }
286
Patrick Venture0b02be92018-08-31 11:55:55 -0700287 // PetiBoot-Specific
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000288 // If success then copy the first 9 bytes to the payload message
289 // payload first 2 bytes contain the parameter values. Skip that 2 bytes.
290 uint8_t skipFirstTwoBytes = 2;
291 size_t payloadSize = payload.size();
292 uint8_t* configDataStartingAddress = payload.data() + skipFirstTwoBytes;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500293
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000294 if (payloadSize < skipFirstTwoBytes + sizeof(netConfInitialBytes))
295 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700296 lg2::error("Invalid net config");
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000297 rc = -1;
298 return rc;
299 }
300 std::copy(netConfInitialBytes,
301 netConfInitialBytes + sizeof(netConfInitialBytes),
302 configDataStartingAddress);
303
304 if (payloadSize < skipFirstTwoBytes + addrSizeOffset + sizeof(addrSize))
305 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700306 lg2::error("Invalid length of address size");
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000307 rc = -1;
308 return rc;
309 }
310 std::copy(&addrSize, &(addrSize) + sizeof(addrSize),
311 configDataStartingAddress + addrSizeOffset);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530312
Ratan Guptafd28dd72016-08-01 04:58:01 -0500313#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700314 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500315
Ratan Guptadcb10672017-07-10 10:33:50 +0530316 for (uint8_t pos = 0; pos < index; pos++)
317 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000318 std::printf("%02x ", payloadStartingAddress[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530319 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500320#endif
321
Ratan Guptafd28dd72016-08-01 04:58:01 -0500322 return rc;
323}
324
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530325/** @brief convert IPv4 and IPv6 addresses from binary to text form.
326 * @param[in] family - IPv4/Ipv6
327 * @param[in] data - req data pointer.
328 * @param[in] offset - offset in the data.
329 * @param[in] addrSize - size of the data which needs to be read from offset.
330 * @returns address in text form.
331 */
332
Patrick Venture0b02be92018-08-31 11:55:55 -0700333std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
334 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530335{
336 char ipAddr[INET6_ADDRSTRLEN] = {};
337
Patrick Venture0b02be92018-08-31 11:55:55 -0700338 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530339 {
340 case AF_INET:
341 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700342 struct sockaddr_in addr4
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500343 {};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700344 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530345
Patrick Venture0b02be92018-08-31 11:55:55 -0700346 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530347
348 break;
349 }
350 case AF_INET6:
351 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700352 struct sockaddr_in6 addr6
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500353 {};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700354 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530355
Patrick Venture0b02be92018-08-31 11:55:55 -0700356 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530357
358 break;
359 }
360 default:
361 {
362 return {};
363 }
364 }
365
366 return ipAddr;
367}
368
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000369ipmi::Cc setHostNetworkData(ipmi::message::Payload& data)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500370{
Ratan Guptadcb10672017-07-10 10:33:50 +0530371 using namespace std::string_literals;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000372 std::string hostNetworkConfig;
373 std::string mac("00:00:00:00:00:00");
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530374 std::string ipAddress, gateway;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000375 std::string addrOrigin{0};
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530377 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530378 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700379 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
380 uint8_t prefix{0};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530381 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500382
Patrick Venture0b02be92018-08-31 11:55:55 -0700383 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500384 // version starts from sixth byte
385
Ratan Guptadcb10672017-07-10 10:33:50 +0530386 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500387 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530388 do
389 {
390 // cookie == 0x21 0x70 0x62 0x21
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000391 data.trailingOk = true;
392 auto msgLen = data.size();
393 std::vector<uint8_t> msgPayloadBytes(msgLen);
394 if (data.unpack(msgPayloadBytes) != 0 || !data.fullyUnpacked())
Ratan Guptadcb10672017-07-10 10:33:50 +0530395 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700396 lg2::error("Error in unpacking message of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000397 return ipmi::ccReqDataLenInvalid;
398 }
399
400 uint8_t* msgPayloadStartingPos = msgPayloadBytes.data();
401 constexpr size_t cookieSize = 4;
402 if (msgLen < cookieOffset + cookieSize)
403 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700404 lg2::error("Error in cookie getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000405 return ipmi::ccReqDataLenInvalid;
406 }
407 if (std::equal(msgPayloadStartingPos + cookieOffset,
408 msgPayloadStartingPos + cookieOffset + cookieSize,
409 (netConfInitialBytes + cookieOffset)) != 0)
410 {
411 // all cookie == 0
412 if (std::all_of(msgPayloadStartingPos + cookieOffset,
413 msgPayloadStartingPos + cookieOffset +
414 cookieSize,
415 [](int i) { return i == 0; }) == true)
Ratan Guptadcb10672017-07-10 10:33:50 +0530416 {
417 // need to zero out the network settings.
418 break;
419 }
420
Vernon Mauery38108d92024-03-11 12:53:55 -0700421 lg2::error("Invalid Cookie");
Ratan Guptadcb10672017-07-10 10:33:50 +0530422 elog<InternalFailure>();
423 }
424
425 // vesion == 0x00 0x01
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000426 if (msgLen < versionOffset + sizeVersion)
Ratan Guptadcb10672017-07-10 10:33:50 +0530427 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700428 lg2::error("Error in version getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000429 return ipmi::ccReqDataLenInvalid;
430 }
431 if (std::equal(msgPayloadStartingPos + versionOffset,
432 msgPayloadStartingPos + versionOffset + sizeVersion,
433 (netConfInitialBytes + versionOffset)) != 0)
434 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700435 lg2::error("Invalid Version");
Ratan Guptadcb10672017-07-10 10:33:50 +0530436 elog<InternalFailure>();
437 }
438
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000439 if (msgLen < macOffset + 6)
440 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700441 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000442 "Error in mac address getting of setHostNetworkData");
443 return ipmi::ccReqDataLenInvalid;
444 }
445 std::stringstream result;
446 std::copy((msgPayloadStartingPos + macOffset),
447 (msgPayloadStartingPos + macOffset + 5),
448 std::ostream_iterator<int>(result, ":"));
449 mac = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530450
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000451 if (msgLen < addrTypeOffset + sizeof(decltype(addrOrigin)))
452 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700453 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000454 "Error in original address getting of setHostNetworkData");
455 return ipmi::ccReqDataLenInvalid;
456 }
457 std::copy(msgPayloadStartingPos + addrTypeOffset,
458 msgPayloadStartingPos + addrTypeOffset +
459 sizeof(decltype(addrOrigin)),
460 std::ostream_iterator<int>(result, ""));
461 addrOrigin = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530462
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000463 if (!addrOrigin.empty())
Ratan Guptadcb10672017-07-10 10:33:50 +0530464 {
465 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530466 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530467 }
468
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000469 if (msgLen < addrSizeOffset + sizeof(decltype(addrSize)))
470 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700471 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000472 "Error in address size getting of setHostNetworkData");
473 return ipmi::ccReqDataLenInvalid;
474 }
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530475 // Get the address size
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000476 std::copy(msgPayloadStartingPos + addrSizeOffset,
477 (msgPayloadStartingPos + addrSizeOffset +
478 sizeof(decltype(addrSize))),
479 &addrSize);
Ratan Guptadcb10672017-07-10 10:33:50 +0530480
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000481 uint8_t prefixOffset = ipAddrOffset + addrSize;
482 if (msgLen < prefixOffset + sizeof(decltype(prefix)))
483 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700484 lg2::error("Error in prefix getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000485 return ipmi::ccReqDataLenInvalid;
486 }
William A. Kennington III4c521022023-07-28 13:07:30 -0700487 // std::copy(msgPayloadStartingPos + prefixOffset,
488 // msgPayloadStartingPos + prefixOffset +
489 // sizeof(decltype(prefix)),
490 // &prefix);
491 // Workaround compiler misdetecting out of bounds memcpy
492 prefix = msgPayloadStartingPos[prefixOffset];
Ratan Guptadcb10672017-07-10 10:33:50 +0530493
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530494 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
Ratan Gupta8c31d232017-08-13 05:49:43 +0530495 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530496 {
497 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
498 family = AF_INET6;
499 }
500
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000501 if (msgLen < ipAddrOffset + addrSize)
502 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700503 lg2::error("Error in IP address getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000504 return ipmi::ccReqDataLenInvalid;
505 }
506 ipAddress = getAddrStr(family, msgPayloadStartingPos, ipAddrOffset,
507 addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530508
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000509 if (msgLen < gatewayOffset + addrSize)
510 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700511 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000512 "Error in gateway address getting of setHostNetworkData");
513 return ipmi::ccReqDataLenInvalid;
514 }
515 gateway = getAddrStr(family, msgPayloadStartingPos, gatewayOffset,
516 addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530517
Patrick Venture0b02be92018-08-31 11:55:55 -0700518 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530519
Patrick Venture0b02be92018-08-31 11:55:55 -0700520 // Cookie == 0 or it is a valid cookie
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000521 hostNetworkConfig += "ipaddress="s + ipAddress + ",prefix="s +
522 std::to_string(prefix) + ",gateway="s + gateway +
523 ",mac="s + mac + ",addressOrigin="s +
524 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500525
Patrick Williams5d82f472022-07-22 19:26:53 -0500526 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530527
528 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
529 SETTINGS_ROOT, SETTINGS_MATCH);
530 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
531 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530532 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530533 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700534 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530535 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700536 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530537 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700538 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530539 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700540 IP_INTERFACE, "Gateway", std::string(gateway));
541 ipmi::setDbusProperty(
542 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
543 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530544 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700545 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500546
Vernon Mauery38108d92024-03-11 12:53:55 -0700547 lg2::debug("Network configuration changed: {NETWORKCONFIG}",
548 "NETWORKCONFIG", hostNetworkConfig);
Ratan Guptafd28dd72016-08-01 04:58:01 -0500549 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500550 catch (const sdbusplus::exception_t& e)
Ratan Guptadcb10672017-07-10 10:33:50 +0530551 {
552 commit<InternalFailure>();
Vernon Mauery38108d92024-03-11 12:53:55 -0700553 lg2::error("Error in ipmiChassisSetSysBootOptions call");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000554 return ipmi::ccUnspecifiedError;
Ratan Guptadcb10672017-07-10 10:33:50 +0530555 }
556
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000557 return ipmi::ccSuccess;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500558}
559
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500560uint32_t getPOHCounter()
561{
Patrick Williams5d82f472022-07-22 19:26:53 -0500562 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500563
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500564 auto chassisStateObj = ipmi::getDbusObject(bus, chassisPOHStateIntf,
565 chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500566
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500567 auto service = ipmi::getService(bus, chassisPOHStateIntf,
568 chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500569
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500570 auto propValue = ipmi::getDbusProperty(bus, service, chassisStateObj.first,
571 chassisPOHStateIntf,
572 pohCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500573
Vernon Maueryf442e112019-04-09 11:44:36 -0700574 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500575}
576
anil kumar appana43263c62019-05-27 12:45:04 +0000577/** @brief Implements the get chassis capabilities command
578 *
579 * @returns IPMI completion code plus response data
580 * chassisCapFlags - chassis capability flag
581 * chassisFRUInfoDevAddr - chassis FRU info Device Address
582 * chassisSDRDevAddr - chassis SDR device address
583 * chassisSELDevAddr - chassis SEL device address
584 * chassisSMDevAddr - chassis system management device address
585 * chassisBridgeDevAddr - chassis bridge device address
586 */
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800587ipmi::RspType<bool, // chassis intrusion sensor
588 bool, // chassis Front panel lockout
589 bool, // chassis NMI
590 bool, // chassis power interlock
591 uint4_t, // reserved
anil kumar appana43263c62019-05-27 12:45:04 +0000592 uint8_t, // chassis FRU info Device Address
593 uint8_t, // chassis SDR device address
594 uint8_t, // chassis SEL device address
595 uint8_t, // chassis system management device address
596 uint8_t // chassis bridge device address
597 >
598 ipmiGetChassisCap()
Nan Li8d15fb42016-08-16 22:29:40 +0800599{
anil kumar appana43263c62019-05-27 12:45:04 +0000600 ipmi::PropertyMap properties;
Yong Liae4b0402018-11-02 11:12:14 +0800601 try
602 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500603 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800604
Yong Liae4b0402018-11-02 11:12:14 +0800605 ipmi::DbusObjectInfo chassisCapObject =
606 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800607
Yong Liae4b0402018-11-02 11:12:14 +0800608 // capabilities flags
609 // [7..4] - reserved
610 // [3] – 1b = provides power interlock (IPM 1.5)
611 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
612 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
613 // has capabilities
614 // to lock out external power control and reset button or
615 // front panel interfaces and/or detect tampering with those
616 // interfaces).
617 // [0] -1b = Chassis provides intrusion (physical security) sensor.
618 // set to default value 0x0.
Nan Li8d15fb42016-08-16 22:29:40 +0800619
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500620 properties = ipmi::getAllDbusProperties(bus, chassisCapObject.second,
621 chassisCapObject.first,
622 chassisCapIntf);
Yong Liae4b0402018-11-02 11:12:14 +0800623 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500624 catch (const std::exception& e)
Yong Liae4b0402018-11-02 11:12:14 +0800625 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700626 lg2::error("Failed to fetch Chassis Capability properties: {ERROR}",
627 "ERROR", e);
anil kumar appana43263c62019-05-27 12:45:04 +0000628 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800629 }
630
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800631 bool* chassisIntrusionFlag =
632 std::get_if<bool>(&properties[chassisIntrusionProp]);
633 if (chassisIntrusionFlag == nullptr)
anil kumar appana43263c62019-05-27 12:45:04 +0000634 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700635 lg2::error("Error to get chassis Intrusion flags");
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800636 return ipmi::responseUnspecifiedError();
637 }
638 bool* chassisFrontPanelFlag =
639 std::get_if<bool>(&properties[chassisFrontPanelLockoutProp]);
640 if (chassisFrontPanelFlag == nullptr)
641 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700642 lg2::error("Error to get chassis intrusion flags");
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800643 return ipmi::responseUnspecifiedError();
644 }
645 bool* chassisNMIFlag = std::get_if<bool>(&properties[chassisNMIProp]);
646 if (chassisNMIFlag == nullptr)
647 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700648 lg2::error("Error to get chassis NMI flags");
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800649 return ipmi::responseUnspecifiedError();
650 }
651 bool* chassisPowerInterlockFlag =
652 std::get_if<bool>(&properties[chassisPowerInterlockProp]);
653 if (chassisPowerInterlockFlag == nullptr)
654 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700655 lg2::error("Error to get chassis power interlock flags");
anil kumar appana43263c62019-05-27 12:45:04 +0000656 return ipmi::responseUnspecifiedError();
657 }
658 uint8_t* chassisFRUInfoDevAddr =
659 std::get_if<uint8_t>(&properties[chassisFRUDevAddrProp]);
660 if (chassisFRUInfoDevAddr == nullptr)
661 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700662 lg2::error("Error to get chassis FRU info device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000663 return ipmi::responseUnspecifiedError();
664 }
665 uint8_t* chassisSDRDevAddr =
666 std::get_if<uint8_t>(&properties[chassisSDRDevAddrProp]);
667 if (chassisSDRDevAddr == nullptr)
668 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700669 lg2::error("Error to get chassis SDR device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000670 return ipmi::responseUnspecifiedError();
671 }
672 uint8_t* chassisSELDevAddr =
673 std::get_if<uint8_t>(&properties[chassisSELDevAddrProp]);
674 if (chassisSELDevAddr == nullptr)
675 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700676 lg2::error("Error to get chassis SEL device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000677 return ipmi::responseUnspecifiedError();
678 }
679 uint8_t* chassisSMDevAddr =
680 std::get_if<uint8_t>(&properties[chassisSMDevAddrProp]);
681 if (chassisSMDevAddr == nullptr)
682 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700683 lg2::error("Error to get chassis SM device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000684 return ipmi::responseUnspecifiedError();
685 }
686 uint8_t* chassisBridgeDevAddr =
687 std::get_if<uint8_t>(&properties[chassisBridgeDevAddrProp]);
688 if (chassisBridgeDevAddr == nullptr)
689 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700690 lg2::error("Error to get chassis bridge device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000691 return ipmi::responseUnspecifiedError();
692 }
693
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800694 return ipmi::responseSuccess(*chassisIntrusionFlag, *chassisFrontPanelFlag,
695 *chassisNMIFlag, *chassisPowerInterlockFlag, 0,
696 *chassisFRUInfoDevAddr, *chassisSDRDevAddr,
697 *chassisSELDevAddr, *chassisSMDevAddr,
698 *chassisBridgeDevAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800699}
700
anil kumar appana894d0222019-05-27 16:32:14 +0000701/** @brief implements set chassis capalibities command
702 * @param intrusion - chassis intrusion
703 * @param fpLockout - frontpannel lockout
704 * @param reserved1 - skip one bit
705 * @param fruDeviceAddr - chassis FRU info Device Address
706 * @param sdrDeviceAddr - chassis SDR device address
707 * @param selDeviceAddr - chassis SEL device address
708 * @param smDeviceAddr - chassis system management device address
709 * @param bridgeDeviceAddr - chassis bridge device address
710 *
711 * @returns IPMI completion code
712 */
713ipmi::RspType<> ipmiSetChassisCap(bool intrusion, bool fpLockout,
714 uint6_t reserved1,
715
716 uint8_t fruDeviceAddr,
717
718 uint8_t sdrDeviceAddr,
719
720 uint8_t selDeviceAddr,
721
722 uint8_t smDeviceAddr,
723
724 uint8_t bridgeDeviceAddr)
Yong Liae4b0402018-11-02 11:12:14 +0800725{
Yong Liae4b0402018-11-02 11:12:14 +0800726 // check input data
anil kumar appana894d0222019-05-27 16:32:14 +0000727 if (reserved1 != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800728 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700729 lg2::error("Unsupported request parameter");
anil kumar appana894d0222019-05-27 16:32:14 +0000730 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800731 }
732
anil kumar appana894d0222019-05-27 16:32:14 +0000733 if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800734 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700735 lg2::error("Unsupported request parameter(FRU Addr) for REQ={REQ}",
736 "REQ", lg2::hex, fruDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000737 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800738 }
anil kumar appana894d0222019-05-27 16:32:14 +0000739 if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800740 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700741 lg2::error("Unsupported request parameter(SDR Addr) for REQ={REQ}",
742 "REQ", lg2::hex, sdrDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000743 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800744 }
745
anil kumar appana894d0222019-05-27 16:32:14 +0000746 if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800747 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700748 lg2::error("Unsupported request parameter(SEL Addr) for REQ={REQ}",
749 "REQ", lg2::hex, selDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000750 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800751 }
752
anil kumar appana894d0222019-05-27 16:32:14 +0000753 if ((smDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800754 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700755 lg2::error("Unsupported request parameter(SM Addr) for REQ={REQ}",
756 "REQ", lg2::hex, smDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000757 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800758 }
759
anil kumar appana894d0222019-05-27 16:32:14 +0000760 if ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800761 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700762 lg2::error("Unsupported request parameter(Bridge Addr) for REQ={REQ}",
763 "REQ", lg2::hex, bridgeDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000764 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800765 }
766
767 try
768 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500769 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Yong Liae4b0402018-11-02 11:12:14 +0800770 ipmi::DbusObjectInfo chassisCapObject =
771 ipmi::getDbusObject(bus, chassisCapIntf);
772
773 ipmi::setDbusProperty(bus, chassisCapObject.second,
774 chassisCapObject.first, chassisCapIntf,
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800775 chassisIntrusionProp, intrusion);
776
777 ipmi::setDbusProperty(bus, chassisCapObject.second,
778 chassisCapObject.first, chassisCapIntf,
779 chassisFrontPanelLockoutProp, fpLockout);
Yong Liae4b0402018-11-02 11:12:14 +0800780
781 ipmi::setDbusProperty(bus, chassisCapObject.second,
782 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000783 chassisFRUDevAddrProp, fruDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800784
785 ipmi::setDbusProperty(bus, chassisCapObject.second,
786 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000787 chassisSDRDevAddrProp, sdrDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800788
789 ipmi::setDbusProperty(bus, chassisCapObject.second,
790 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000791 chassisSELDevAddrProp, selDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800792
793 ipmi::setDbusProperty(bus, chassisCapObject.second,
794 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000795 chassisSMDevAddrProp, smDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800796
797 ipmi::setDbusProperty(bus, chassisCapObject.second,
798 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000799 chassisBridgeDevAddrProp, bridgeDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800800 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500801 catch (const std::exception& e)
Yong Liae4b0402018-11-02 11:12:14 +0800802 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700803 lg2::error("Failed to set chassis capability properties: {ERR}", "ERR",
804 e);
anil kumar appana894d0222019-05-27 16:32:14 +0000805 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800806 }
anil kumar appana894d0222019-05-27 16:32:14 +0000807 return ipmi::responseSuccess();
Nan Li8d15fb42016-08-16 22:29:40 +0800808}
809
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530810//------------------------------------------
811// Calls into Host State Manager Dbus object
812//------------------------------------------
Jason M. Bills664e1c32020-01-30 16:02:39 -0800813int initiateHostStateTransition(ipmi::Context::ptr& ctx,
814 State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600815{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500816 // OpenBMC Host State Manager dbus framework
Jason M. Bills664e1c32020-01-30 16:02:39 -0800817 constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0";
818 constexpr auto hostStateIntf = "xyz.openbmc_project.State.Host";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530819
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500820 // Convert to string equivalent of the passed in transition enum.
Willy Tu523e2d12023-09-05 11:36:48 -0700821 auto request =
822 sdbusplus::common::xyz::openbmc_project::state::convertForMessage(
823 transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530824
Jason M. Bills664e1c32020-01-30 16:02:39 -0800825 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500826 boost::system::error_code ec = ipmi::getService(ctx, hostStateIntf,
827 hostStatePath, service);
Jason M. Bills664e1c32020-01-30 16:02:39 -0800828
829 if (!ec)
830 {
831 ec = ipmi::setDbusProperty(ctx, service, hostStatePath, hostStateIntf,
832 "RequestedHostTransition", request);
833 }
834 if (ec)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500835 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700836 lg2::error(
837 "Failed to initiate transition for request {REQUEST}: {EXCEPTION}",
838 "REQUEST", request, "EXCEPTION", ec.message());
Jason M. Bills664e1c32020-01-30 16:02:39 -0800839 return -1;
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500840 }
Vernon Mauery38108d92024-03-11 12:53:55 -0700841 lg2::info(
842 "Transition request {REQUEST} initiated successfully by user {USERID}",
843 "REQUEST", request, "USERID", ctx->userId);
Jason M. Bills664e1c32020-01-30 16:02:39 -0800844 return 0;
vishwa36993272015-11-20 12:43:49 -0600845}
846
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800847//------------------------------------------
Jason M. Billsad588bf2020-01-30 16:18:33 -0800848// Calls into Chassis State Manager Dbus object
849//------------------------------------------
850int initiateChassisStateTransition(ipmi::Context::ptr& ctx,
851 State::Chassis::Transition transition)
852{
853 // OpenBMC Chassis State Manager dbus framework
854 constexpr auto chassisStatePath = "/xyz/openbmc_project/state/chassis0";
855 constexpr auto chassisStateIntf = "xyz.openbmc_project.State.Chassis";
856
857 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500858 boost::system::error_code ec = ipmi::getService(ctx, chassisStateIntf,
859 chassisStatePath, service);
Jason M. Billsad588bf2020-01-30 16:18:33 -0800860
861 // Convert to string equivalent of the passed in transition enum.
Willy Tu523e2d12023-09-05 11:36:48 -0700862 auto request =
863 sdbusplus::common::xyz::openbmc_project::state::convertForMessage(
864 transition);
Jason M. Billsad588bf2020-01-30 16:18:33 -0800865
866 if (!ec)
867 {
868 ec = ipmi::setDbusProperty(ctx, service, chassisStatePath,
869 chassisStateIntf, "RequestedPowerTransition",
870 request);
871 }
872 if (ec)
873 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700874 lg2::error("Failed to initiate transition {REQUEST}: {EXCEPTION}",
875 "REQUEST", request, "EXCEPTION", ec.message());
876
Jason M. Billsad588bf2020-01-30 16:18:33 -0800877 return -1;
878 }
879
880 return 0;
881}
882
883//------------------------------------------
Zev Weiss337a0972024-03-08 08:31:15 +0000884// Trigger an NMI on the host via dbus
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800885//------------------------------------------
Zev Weiss337a0972024-03-08 08:31:15 +0000886static int doNmi(ipmi::Context::ptr& ctx)
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800887{
Zev Weiss337a0972024-03-08 08:31:15 +0000888 constexpr const char* nmiIntfName = "xyz.openbmc_project.Control.Host.NMI";
889 ipmi::DbusObjectInfo nmiObj{};
890 boost::system::error_code ec;
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800891
Zev Weiss337a0972024-03-08 08:31:15 +0000892 ec = ipmi::getDbusObject(ctx, nmiIntfName, nmiObj);
Vernon Mauery9bb4a382021-11-05 12:46:19 -0700893 if (ec)
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800894 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700895 lg2::error("Failed to find NMI service: {ERROR}", "ERROR",
896 ec.message());
Zev Weiss337a0972024-03-08 08:31:15 +0000897 return -1;
898 }
899
900 ctx->bus->yield_method_call<void>(ctx->yield, ec, nmiObj.second,
901 nmiObj.first, nmiIntfName, "NMI");
902 if (ec)
903 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700904 lg2::error("NMI call failed: {ERROR}", "ERROR", ec.message());
Zev Weiss337a0972024-03-08 08:31:15 +0000905 elog<InternalFailure>();
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800906 return -1;
907 }
908
909 return 0;
910}
911
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500912namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500913{
Nan Lifdd8ec52016-08-28 03:57:40 +0800914
Willy Tu523e2d12023-09-05 11:36:48 -0700915using namespace sdbusplus::server::xyz::openbmc_project::control::power;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500916using IpmiValue = uint8_t;
917using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800918
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700919const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500920 {RestorePolicy::Policy::AlwaysOff, 0x00},
921 {RestorePolicy::Policy::Restore, 0x01},
George Liue561d3e2024-02-04 15:19:30 +0800922 {RestorePolicy::Policy::AlwaysOn, 0x02},
923 {RestorePolicy::Policy::None, 0x03}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800924
Yong Lic6713cf2018-09-12 12:35:13 +0800925static constexpr uint8_t noChange = 0x03;
926static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700927
928/* helper function for Get Chassis Status Command
929 */
930std::optional<uint2_t> getPowerRestorePolicy()
931{
932 uint2_t restorePolicy = 0;
933 using namespace chassis::internal;
934
James Feist225dec82019-11-26 16:25:06 -0800935 settings::Objects& objects = cache::getObjects();
936
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700937 try
938 {
939 const auto& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -0800940 objects.map.at(powerRestoreIntf).front();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700941 ipmi::Value result = ipmi::getDbusProperty(
942 *getSdBus(),
James Feist225dec82019-11-26 16:25:06 -0800943 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700944 powerRestoreSetting.c_str(), powerRestoreIntf,
945 "PowerRestorePolicy");
946 auto powerRestore = RestorePolicy::convertPolicyFromString(
947 std::get<std::string>(result));
948 restorePolicy = dbusToIpmi.at(powerRestore);
949 }
950 catch (const std::exception& e)
951 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700952 lg2::error(
953 "Failed to fetch pgood property ({PATH}/{INTERFACE}): {ERROR}",
954 "PATH", objects.map.at(powerRestoreIntf).front(), "INTERFACE",
955 powerRestoreIntf, "ERROR", e);
James Feist225dec82019-11-26 16:25:06 -0800956 cache::objectsPtr.reset();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700957 return std::nullopt;
958 }
959 return std::make_optional(restorePolicy);
960}
961
962/*
963 * getPowerStatus
964 * helper function for Get Chassis Status Command
965 * return - optional value for pgood (no value on error)
966 */
967std::optional<bool> getPowerStatus()
968{
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700969 bool powerGood = false;
970 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
971 try
972 {
Jason M. Bills3de424c2019-05-21 09:57:16 -0700973 constexpr const char* chassisStatePath =
974 "/xyz/openbmc_project/state/chassis0";
975 constexpr const char* chassisStateIntf =
976 "xyz.openbmc_project.State.Chassis";
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500977 auto service = ipmi::getService(*busp, chassisStateIntf,
978 chassisStatePath);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700979
Jason M. Bills3de424c2019-05-21 09:57:16 -0700980 ipmi::Value powerState =
981 ipmi::getDbusProperty(*busp, service, chassisStatePath,
982 chassisStateIntf, "CurrentPowerState");
983 powerGood = std::get<std::string>(powerState) ==
984 "xyz.openbmc_project.State.Chassis.PowerState.On";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700985 }
986 catch (const std::exception& e)
987 {
988 try
989 {
990 // FIXME: some legacy modules use the older path; try that next
991 constexpr const char* legacyPwrCtrlObj =
992 "/org/openbmc/control/power0";
993 constexpr const char* legacyPwrCtrlIntf =
994 "org.openbmc.control.Power";
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500995 auto service = ipmi::getService(*busp, legacyPwrCtrlIntf,
996 legacyPwrCtrlObj);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700997
998 ipmi::Value variant = ipmi::getDbusProperty(
999 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
1000 powerGood = static_cast<bool>(std::get<int>(variant));
1001 }
1002 catch (const std::exception& e)
1003 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001004 lg2::error("Failed to fetch pgood property: {ERROR}", "ERROR", e);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001005 return std::nullopt;
1006 }
1007 }
1008 return std::make_optional(powerGood);
1009}
1010
Yong Li70ce7352019-05-16 21:15:27 +08001011/*
1012 * getACFailStatus
1013 * helper function for Get Chassis Status Command
1014 * return - bool value for ACFail (false on error)
1015 */
1016bool getACFailStatus()
1017{
1018 constexpr const char* powerControlObj =
1019 "/xyz/openbmc_project/Chassis/Control/Power0";
1020 constexpr const char* powerControlIntf =
1021 "xyz.openbmc_project.Chassis.Control.Power";
1022 bool acFail = false;
1023 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
1024 try
1025 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001026 auto service = ipmi::getService(*bus, powerControlIntf,
1027 powerControlObj);
Yong Li70ce7352019-05-16 21:15:27 +08001028
1029 ipmi::Value variant = ipmi::getDbusProperty(
1030 *bus, service, powerControlObj, powerControlIntf, "PFail");
1031 acFail = std::get<bool>(variant);
1032 }
1033 catch (const std::exception& e)
1034 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001035 lg2::error(
1036 "Failed to fetch PFail property ({PATH}/{INTERFAC}): {ERROR}",
1037 "PATH", powerControlObj, "INTERFACE", powerControlIntf, "ERROR", e);
Yong Li70ce7352019-05-16 21:15:27 +08001038 }
1039 return acFail;
1040}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001041} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +08001042
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001043static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
1044 const std::string& buttonIntf)
1045{
1046 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
1047 bool buttonDisabled = false;
1048 try
1049 {
1050 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
1051 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
1052 buttonIntf, "Enabled");
1053 buttonDisabled = !std::get<bool>(enabled);
1054 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001055 catch (const sdbusplus::exception_t& e)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001056 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001057 lg2::error("Fail to get button Enabled property ({PATH}): {ERROR}",
1058 "PATH", buttonPath, "ERROR", e);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001059 return std::nullopt;
1060 }
1061 return std::make_optional(buttonDisabled);
1062}
1063
Kuiying Wang21addc52019-01-04 10:50:21 +08001064static bool setButtonEnabled(ipmi::Context::ptr& ctx,
1065 const std::string& buttonPath,
1066 const std::string& buttonIntf, bool enable)
1067{
1068 std::string service;
1069 boost::system::error_code ec;
1070 ec = ipmi::getService(ctx, buttonIntf, buttonPath, service);
1071 if (!ec)
1072 {
1073 ec = ipmi::setDbusProperty(ctx, service, buttonPath, buttonIntf,
1074 "Enabled", enable);
1075 }
1076 if (ec)
1077 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001078 lg2::error(
1079 "Fail to set button Enabled property ({SERVICE}:{PATH}): {ERROR}",
1080 "SERVICE", service, "PATH", buttonPath, "ERROR", ec.message());
Kuiying Wang21addc52019-01-04 10:50:21 +08001081 return false;
1082 }
1083 return true;
1084}
1085
Chau Ly9e666cd2023-03-06 08:46:14 +00001086static std::optional<bool> getChassisIntrusionStatus(ipmi::Context::ptr& ctx)
1087{
1088 constexpr const char* chassisIntrusionPath =
1089 "/xyz/openbmc_project/Chassis/Intrusion";
1090 constexpr const char* chassisIntrusionInf =
1091 "xyz.openbmc_project.Chassis.Intrusion";
1092
1093 std::string service;
1094 boost::system::error_code ec = ipmi::getService(
1095 ctx, chassisIntrusionInf, chassisIntrusionPath, service);
1096 if (!ec)
1097 {
1098 std::string chassisIntrusionStr;
1099 ec = ipmi::getDbusProperty<std::string>(
1100 ctx, service, chassisIntrusionPath, chassisIntrusionInf, "Status",
1101 chassisIntrusionStr);
1102 if (!ec)
1103 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001104 bool ret = (chassisIntrusionStr == "HardwareIntrusion") ? true
1105 : false;
Chau Ly9e666cd2023-03-06 08:46:14 +00001106 return std::make_optional(ret);
1107 }
1108 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001109 lg2::error("Fail to get Chassis Intrusion Status property "
1110 "({PATH}/{INTERFACE}): {ERROR}",
1111 "PATH", chassisIntrusionPath, "INTERFACE", chassisIntrusionInf,
1112 "ERROR", ec.message());
Chau Ly9e666cd2023-03-06 08:46:14 +00001113 return std::nullopt;
1114}
1115
Nan Lifdd8ec52016-08-28 03:57:40 +08001116//----------------------------------------------------------------------
1117// Get Chassis Status commands
1118//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001119ipmi::RspType<bool, // Power is on
1120 bool, // Power overload
1121 bool, // Interlock
1122 bool, // power fault
1123 bool, // power control fault
1124 uint2_t, // power restore policy
1125 bool, // reserved
1126
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001127 bool, // AC failed
1128 bool, // last power down caused by a Power overload
1129 bool, // last power down caused by a power interlock
1130 bool, // last power down caused by power fault
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001131 bool, // last ‘Power is on’ state was entered via IPMI command
1132 uint3_t, // reserved
1133
1134 bool, // Chassis intrusion active
1135 bool, // Front Panel Lockout active
1136 bool, // Drive Fault
1137 bool, // Cooling/fan fault detected
1138 uint2_t, // Chassis Identify State
1139 bool, // Chassis Identify command and state info supported
1140 bool, // reserved
1141
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001142 bool, // Power off button disabled
1143 bool, // Reset button disabled
1144 bool, // Diagnostic Interrupt button disabled
1145 bool, // Standby (sleep) button disabled
1146 bool, // Power off button disable allowed
1147 bool, // Reset button disable allowed
1148 bool, // Diagnostic Interrupt button disable allowed
1149 bool // Standby (sleep) button disable allowed
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001150 >
Chau Ly9e666cd2023-03-06 08:46:14 +00001151 ipmiGetChassisStatus(ipmi::Context::ptr& ctx)
Nan Lifdd8ec52016-08-28 03:57:40 +08001152{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001153 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001154 std::optional<uint2_t> restorePolicy =
1155 power_policy::getPowerRestorePolicy();
1156 std::optional<bool> powerGood = power_policy::getPowerStatus();
1157 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001158 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001159 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001160 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001161
1162 // Front Panel Button Capabilities and disable/enable status(Optional)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001163 std::optional<bool> powerButtonReading = getButtonEnabled(powerButtonPath,
1164 powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001165 // allow disable if the interface is present
1166 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
1167 // default return the button is enabled (not disabled)
1168 bool powerButtonDisabled = false;
1169 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001170 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001171 // return the real value of the button status, if present
1172 powerButtonDisabled = *powerButtonReading;
1173 }
1174
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001175 std::optional<bool> resetButtonReading = getButtonEnabled(resetButtonPath,
1176 resetButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001177 // allow disable if the interface is present
1178 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
1179 // default return the button is enabled (not disabled)
1180 bool resetButtonDisabled = false;
1181 if (resetButtonDisableAllow)
1182 {
1183 // return the real value of the button status, if present
1184 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001185 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001186
Yong Li70ce7352019-05-16 21:15:27 +08001187 bool powerDownAcFailed = power_policy::getACFailStatus();
1188
Chau Ly9e666cd2023-03-06 08:46:14 +00001189 bool chassisIntrusionActive = false;
1190 std::optional<bool> chassisIntrusionStatus = getChassisIntrusionStatus(ctx);
1191 if (chassisIntrusionStatus)
1192 {
1193 chassisIntrusionActive = chassisIntrusionStatus.value();
1194 }
1195
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001196 // This response has a lot of hard-coded, unsupported fields
1197 // They are set to false or 0
1198 constexpr bool powerOverload = false;
1199 constexpr bool chassisInterlock = false;
1200 constexpr bool powerFault = false;
1201 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001202 constexpr bool powerDownOverload = false;
1203 constexpr bool powerDownInterlock = false;
1204 constexpr bool powerDownPowerFault = false;
1205 constexpr bool powerStatusIPMI = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001206 constexpr bool frontPanelLockoutActive = false;
1207 constexpr bool driveFault = false;
1208 constexpr bool coolingFanFault = false;
1209 // chassisIdentifySupport set because this command is implemented
1210 constexpr bool chassisIdentifySupport = true;
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001211 uint2_t chassisIdentifyState = types::enum_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001212 constexpr bool diagButtonDisabled = false;
1213 constexpr bool sleepButtonDisabled = false;
1214 constexpr bool diagButtonDisableAllow = false;
1215 constexpr bool sleepButtonDisableAllow = false;
1216
1217 return ipmi::responseSuccess(
1218 *powerGood, powerOverload, chassisInterlock, powerFault,
1219 powerControlFault, *restorePolicy,
1220 false, // reserved
1221
1222 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1223 powerDownPowerFault, powerStatusIPMI,
1224 uint3_t(0), // reserved
1225
1226 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1227 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1228 false, // reserved
1229
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001230 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001231 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1232 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001233}
Chris Austen7888c4d2015-12-03 15:26:20 -06001234
Vijay Khemka074f64d2020-03-03 15:08:43 -08001235enum class IpmiRestartCause
1236{
1237 Unknown = 0x0,
1238 RemoteCommand = 0x1,
1239 ResetButton = 0x2,
1240 PowerButton = 0x3,
1241 WatchdogTimer = 0x4,
1242 PowerPolicyAlwaysOn = 0x6,
1243 PowerPolicyPreviousState = 0x7,
1244 SoftReset = 0xa,
1245};
1246
1247static IpmiRestartCause
1248 restartCauseToIpmiRestartCause(State::Host::RestartCause cause)
1249{
1250 switch (cause)
1251 {
1252 case State::Host::RestartCause::Unknown:
1253 {
1254 return IpmiRestartCause::Unknown;
1255 }
1256 case State::Host::RestartCause::RemoteCommand:
1257 {
1258 return IpmiRestartCause::RemoteCommand;
1259 }
1260 case State::Host::RestartCause::ResetButton:
1261 {
1262 return IpmiRestartCause::ResetButton;
1263 }
1264 case State::Host::RestartCause::PowerButton:
1265 {
1266 return IpmiRestartCause::PowerButton;
1267 }
1268 case State::Host::RestartCause::WatchdogTimer:
1269 {
1270 return IpmiRestartCause::WatchdogTimer;
1271 }
1272 case State::Host::RestartCause::PowerPolicyAlwaysOn:
1273 {
1274 return IpmiRestartCause::PowerPolicyAlwaysOn;
1275 }
1276 case State::Host::RestartCause::PowerPolicyPreviousState:
1277 {
1278 return IpmiRestartCause::PowerPolicyPreviousState;
1279 }
1280 case State::Host::RestartCause::SoftReset:
1281 {
1282 return IpmiRestartCause::SoftReset;
1283 }
1284 default:
1285 {
1286 return IpmiRestartCause::Unknown;
1287 }
1288 }
1289}
1290
1291/*
1292 * getRestartCause
1293 * helper function for Get Host restart cause Command
1294 * return - optional value for RestartCause (no value on error)
1295 */
1296static std::optional<uint4_t> getRestartCause(ipmi::Context::ptr ctx)
1297{
1298 constexpr const char* restartCausePath =
1299 "/xyz/openbmc_project/control/host0/restart_cause";
1300 constexpr const char* restartCauseIntf =
1301 "xyz.openbmc_project.Control.Host.RestartCause";
1302
1303 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001304 boost::system::error_code ec = ipmi::getService(ctx, restartCauseIntf,
1305 restartCausePath, service);
Vijay Khemka074f64d2020-03-03 15:08:43 -08001306 if (!ec)
1307 {
1308 std::string restartCauseStr;
1309 ec = ipmi::getDbusProperty<std::string>(
1310 ctx, service, restartCausePath, restartCauseIntf, "RestartCause",
1311 restartCauseStr);
1312 if (!ec)
1313 {
1314 auto cause =
1315 State::Host::convertRestartCauseFromString(restartCauseStr);
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001316 return types::enum_cast<uint4_t>(
1317 restartCauseToIpmiRestartCause(cause));
Vijay Khemka074f64d2020-03-03 15:08:43 -08001318 }
1319 }
1320
Vernon Mauery38108d92024-03-11 12:53:55 -07001321 lg2::error(
1322 "Failed to fetch RestartCause property ({PATH}/{INTERFACE}): {ERROR}",
1323 "ERROR", ec.message(), "PATH", restartCausePath, "INTERFACE",
1324 restartCauseIntf);
Vijay Khemka074f64d2020-03-03 15:08:43 -08001325 return std::nullopt;
1326}
1327
1328ipmi::RspType<uint4_t, // Restart Cause
1329 uint4_t, // reserved
1330 uint8_t // channel number (not supported)
1331 >
1332 ipmiGetSystemRestartCause(ipmi::Context::ptr ctx)
1333{
1334 std::optional<uint4_t> cause = getRestartCause(ctx);
1335 if (!cause)
1336 {
1337 return ipmi::responseUnspecifiedError();
1338 }
1339
Vernon Mauery916d4232021-03-29 13:42:16 -07001340 constexpr uint4_t reserved = 0;
1341 auto channel = static_cast<uint8_t>(ctx->channel);
1342 return ipmi::responseSuccess(cause.value(), reserved, channel);
Vijay Khemka074f64d2020-03-03 15:08:43 -08001343}
anil kumar appanadafff5f2019-04-27 18:06:00 +00001344/** @brief Implementation of chassis control command
1345 *
1346 * @param - chassisControl command byte
1347 *
1348 * @return Success or InvalidFieldRequest.
1349 */
Jason M. Bills664e1c32020-01-30 16:02:39 -08001350ipmi::RspType<> ipmiChassisControl(ipmi::Context::ptr& ctx,
1351 uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001352{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001353 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001354 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001355 {
1356 case CMD_POWER_ON:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001357 rc = initiateHostStateTransition(ctx, State::Host::Transition::On);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001358 break;
1359 case CMD_POWER_OFF:
Jason M. Billsc2c3a402020-01-30 16:22:24 -08001360 rc = initiateChassisStateTransition(
1361 ctx, State::Chassis::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001362 break;
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001363 case CMD_HARD_RESET:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001364 rc = initiateHostStateTransition(
1365 ctx, State::Host::Transition::ForceWarmReboot);
Chanh Nguyend0a7fcd2021-07-16 22:59:28 +07001366 break;
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001367 case CMD_POWER_CYCLE:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001368 rc = initiateHostStateTransition(ctx,
1369 State::Host::Transition::Reboot);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001370 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301371 case CMD_SOFT_OFF_VIA_OVER_TEMP:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001372 rc = initiateHostStateTransition(ctx, State::Host::Transition::Off);
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301373 break;
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001374 case CMD_PULSE_DIAGNOSTIC_INTR:
Zev Weiss337a0972024-03-08 08:31:15 +00001375 rc = doNmi(ctx);
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001376 break;
1377
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001378 default:
1379 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001380 lg2::error("Invalid Chassis Control command: {CMD}", "CMD",
1381 lg2::hex, chassisControl);
anil kumar appanadafff5f2019-04-27 18:06:00 +00001382 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001383 }
1384 }
vishwa36993272015-11-20 12:43:49 -06001385
anil kumar appanadafff5f2019-04-27 18:06:00 +00001386 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1387 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001388}
1389
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001390/** @brief Return D-Bus connection string to enclosure identify LED object
1391 *
1392 * @param[in, out] connection - connection to D-Bus object
1393 * @return a IPMI return code
1394 */
1395std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301396{
Tom Joseph5110c122018-03-23 17:55:40 +05301397 // lookup enclosure_identify group owner(s) in mapper
George Liu3e3cc352023-07-26 15:59:31 +08001398 try
Tom Joseph5110c122018-03-23 17:55:40 +05301399 {
George Liu3e3cc352023-07-26 15:59:31 +08001400 return ipmi::getService(*getSdBus(), "xyz.openbmc_project.Led.Group",
1401 identify_led_object_name);
1402 }
1403 catch (const std::exception& e)
1404 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001405 lg2::error("Chassis Identify: Error communicating to mapper: {ERROR}",
1406 "ERROR", e);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001407 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301408 }
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001409}
Tom Joseph5110c122018-03-23 17:55:40 +05301410
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001411/** @brief Turn On/Off enclosure identify LED
1412 *
1413 * @param[in] flag - true to turn on LED, false to turn off
1414 * @return a IPMI return code
1415 */
1416void enclosureIdentifyLed(bool flag)
1417{
1418 using namespace chassis::internal;
George Liu3e3cc352023-07-26 15:59:31 +08001419 try
1420 {
1421 std::string connection = getEnclosureIdentifyConnection();
1422
1423 auto msg = std::string("enclosureIdentifyLed(") +
1424 boost::lexical_cast<std::string>(flag) + ")";
Vernon Mauery38108d92024-03-11 12:53:55 -07001425 lg2::debug(msg.c_str());
George Liu3e3cc352023-07-26 15:59:31 +08001426
1427 ipmi::setDbusProperty(*getSdBus(), connection, identify_led_object_name,
1428 "xyz.openbmc_project.Led.Group", "Asserted",
1429 flag);
1430 }
1431 catch (const std::exception& e)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001432 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001433 lg2::error("Chassis Identify: Error Setting State {LED_STATE}: {ERROR}",
1434 "LED_STATE", flag, "ERROR", e);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001435 elog<InternalFailure>();
1436 }
1437}
1438
1439/** @brief Callback method to turn off LED
1440 */
1441void enclosureIdentifyLedOff()
1442{
1443 try
1444 {
Yong Lif4e38512019-05-21 14:46:55 +08001445 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001446 enclosureIdentifyLed(false);
1447 }
1448 catch (const InternalFailure& e)
1449 {
1450 report<InternalFailure>();
1451 }
1452}
1453
1454/** @brief Create timer to turn on and off the enclosure LED
1455 */
1456void createIdentifyTimer()
1457{
1458 if (!identifyTimer)
1459 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001460 identifyTimer =
Patrick Williams95655222023-12-05 12:45:02 -06001461 std::make_unique<sdbusplus::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001462 }
1463}
1464
Vernon Mauery400cc782018-10-09 13:49:53 -07001465ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1466 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001467{
Vernon Mauery400cc782018-10-09 13:49:53 -07001468 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1469 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301470
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001471 if (identifyInterval || forceIdentify)
1472 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001473 // stop the timer if already started;
1474 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001475 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001476 try
Tom Joseph5110c122018-03-23 17:55:40 +05301477 {
Yong Lif4e38512019-05-21 14:46:55 +08001478 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001479 enclosureIdentifyLed(true);
1480 }
1481 catch (const InternalFailure& e)
1482 {
1483 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001484 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301485 }
1486
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001487 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301488 {
Yong Lif4e38512019-05-21 14:46:55 +08001489 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001490 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001491 }
1492 // start the timer
1493 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001494 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001495 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301496 }
Tom Josephbed26992018-07-31 23:00:24 +05301497 else if (!identifyInterval)
1498 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001499 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301500 enclosureIdentifyLedOff();
1501 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001502 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301503}
1504
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001505namespace boot_options
1506{
1507
Willy Tu523e2d12023-09-05 11:36:48 -07001508using namespace sdbusplus::server::xyz::openbmc_project::control::boot;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001509using IpmiValue = uint8_t;
1510constexpr auto ipmiDefault = 0;
1511
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001512std::map<IpmiValue, Type::Types> typeIpmiToDbus = {{0x00, Type::Types::Legacy},
1513 {0x01, Type::Types::EFI}};
1514
Patrick Venture0b02be92018-08-31 11:55:55 -07001515std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001516 {0x01, Source::Sources::Network},
1517 {0x02, Source::Sources::Disk},
1518 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001519 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001520 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001521
Patrick Venture0b02be92018-08-31 11:55:55 -07001522std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Yong Li5833cb62019-10-30 13:27:12 +08001523#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001524 {0x03, Mode::Modes::Safe},
Yong Li5833cb62019-10-30 13:27:12 +08001525#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001526 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001527 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001528
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001529std::map<Type::Types, IpmiValue> typeDbusToIpmi = {{Type::Types::Legacy, 0x00},
1530 {Type::Types::EFI, 0x01}};
1531
Patrick Venture0b02be92018-08-31 11:55:55 -07001532std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001533 {Source::Sources::Network, 0x01},
1534 {Source::Sources::Disk, 0x02},
1535 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001536 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001537 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001538
Patrick Venture0b02be92018-08-31 11:55:55 -07001539std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Yong Li5833cb62019-10-30 13:27:12 +08001540#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001541 {Mode::Modes::Safe, 0x03},
Yong Li5833cb62019-10-30 13:27:12 +08001542#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001543 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001544 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001545
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001546} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001547
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001548/** @brief Get the property value for boot source
1549 * @param[in] ctx - context pointer
1550 * @param[out] source - boot source value
1551 * @return On failure return IPMI error.
1552 */
1553static ipmi::Cc getBootSource(ipmi::Context::ptr& ctx, Source::Sources& source)
1554{
1555 using namespace chassis::internal;
1556 std::string result;
1557 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001558 boost::system::error_code ec = getService(ctx, bootSourceIntf,
1559 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001560 if (!ec)
1561 {
1562 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath,
1563 bootSourceIntf, "BootSource", result);
1564 if (!ec)
1565 {
1566 source = Source::convertSourcesFromString(result);
1567 return ipmi::ccSuccess;
1568 }
1569 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001570 lg2::error("Error in BootSource Get: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001571 return ipmi::ccUnspecifiedError;
1572}
1573
Marri Devender Rao81719702018-05-07 00:53:48 -05001574/** @brief Set the property value for boot source
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001575 * @param[in] ctx - context pointer
Marri Devender Rao81719702018-05-07 00:53:48 -05001576 * @param[in] source - boot source value
1577 * @return On failure return IPMI error.
1578 */
Jayaprakash Mutyala6088e9f2021-07-14 07:40:29 +00001579static ipmi::Cc setBootSource(ipmi::Context::ptr& ctx,
1580 const Source::Sources& source)
Marri Devender Rao81719702018-05-07 00:53:48 -05001581{
1582 using namespace chassis::internal;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001583 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001584 boost::system::error_code ec = getService(ctx, bootSourceIntf,
1585 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001586 if (!ec)
Marri Devender Rao81719702018-05-07 00:53:48 -05001587 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001588 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath,
1589 bootSourceIntf, "BootSource",
1590 convertForMessage(source));
1591 if (!ec)
1592 {
1593 return ipmi::ccSuccess;
1594 }
Marri Devender Rao81719702018-05-07 00:53:48 -05001595 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001596 lg2::error("Error in BootSource Set: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001597 return ipmi::ccUnspecifiedError;
1598}
1599
1600/** @brief Get the property value for boot mode
1601 * @param[in] ctx - context pointer
1602 * @param[out] mode - boot mode value
1603 * @return On failure return IPMI error.
1604 */
1605static ipmi::Cc getBootMode(ipmi::Context::ptr& ctx, Mode::Modes& mode)
1606{
1607 using namespace chassis::internal;
1608 std::string result;
1609 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001610 boost::system::error_code ec = getService(ctx, bootModeIntf,
1611 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001612 if (!ec)
1613 {
1614 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath, bootModeIntf,
1615 "BootMode", result);
1616 if (!ec)
1617 {
1618 mode = Mode::convertModesFromString(result);
1619 return ipmi::ccSuccess;
1620 }
1621 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001622 lg2::error("Error in BootMode Get: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001623 return ipmi::ccUnspecifiedError;
Marri Devender Rao81719702018-05-07 00:53:48 -05001624}
1625
Patrick Venture0b02be92018-08-31 11:55:55 -07001626/** @brief Set the property value for boot mode
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001627 * @param[in] ctx - context pointer
Marri Devender Rao81719702018-05-07 00:53:48 -05001628 * @param[in] mode - boot mode value
1629 * @return On failure return IPMI error.
1630 */
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001631static ipmi::Cc setBootMode(ipmi::Context::ptr& ctx, const Mode::Modes& mode)
Marri Devender Rao81719702018-05-07 00:53:48 -05001632{
1633 using namespace chassis::internal;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001634 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001635 boost::system::error_code ec = getService(ctx, bootModeIntf,
1636 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001637 if (!ec)
Marri Devender Rao81719702018-05-07 00:53:48 -05001638 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001639 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath, bootModeIntf,
1640 "BootMode", convertForMessage(mode));
1641 if (!ec)
1642 {
1643 return ipmi::ccSuccess;
1644 }
Marri Devender Rao81719702018-05-07 00:53:48 -05001645 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001646 lg2::error("Error in BootMode Set: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001647 return ipmi::ccUnspecifiedError;
1648}
1649
1650/** @brief Get the property value for boot type
1651 * @param[in] ctx - context pointer
1652 * @param[out] type - boot type value
1653 * @return On failure return IPMI error.
1654 */
1655static ipmi::Cc getBootType(ipmi::Context::ptr& ctx, Type::Types& type)
1656{
1657 using namespace chassis::internal;
1658 std::string result;
1659 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001660 boost::system::error_code ec = getService(ctx, bootTypeIntf,
1661 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001662
1663 // Don't throw error if BootType interface is not present.
1664 // This interface is not relevant for some Host architectures
1665 // (for example POWER). In this case we don't won't IPMI to
1666 // return an error, but simply return bootType as EFI.
1667 type = Type::Types::EFI;
1668 if (!ec)
1669 {
1670 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath, bootTypeIntf,
1671 "BootType", result);
1672 if (ec)
1673 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001674 lg2::error("Error in BootType Get: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001675 return ipmi::ccUnspecifiedError;
1676 }
1677 type = Type::convertTypesFromString(result);
1678 }
1679
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001680 return ipmi::ccSuccess;
Marri Devender Rao81719702018-05-07 00:53:48 -05001681}
1682
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001683/** @brief Set the property value for boot type
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001684 * @param[in] ctx - context pointer
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001685 * @param[in] type - boot type value
1686 * @return On failure return IPMI error.
1687 */
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001688static ipmi::Cc setBootType(ipmi::Context::ptr& ctx, const Type::Types& type)
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001689{
1690 using namespace chassis::internal;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001691 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001692 boost::system::error_code ec = getService(ctx, bootTypeIntf,
1693 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001694 if (!ec)
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001695 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001696 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath, bootTypeIntf,
1697 "BootType", convertForMessage(type));
1698 if (ec)
1699 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001700 lg2::error("Error in BootType Set: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001701 return ipmi::ccUnspecifiedError;
1702 }
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001703 }
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001704 // Don't throw error if BootType interface is not present.
1705 // This interface is not relevant for some Host architectures
1706 // (for example POWER). In this case we don't won't IPMI to
1707 // return an error, but want to just skip this function.
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001708 return ipmi::ccSuccess;
1709}
1710
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001711/** @brief Get the property value for boot override enable
1712 * @param[in] ctx - context pointer
1713 * @param[out] enable - boot override enable
1714 * @return On failure return IPMI error.
1715 */
1716static ipmi::Cc getBootEnable(ipmi::Context::ptr& ctx, bool& enable)
1717{
1718 using namespace chassis::internal;
1719 std::string result;
1720 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001721 boost::system::error_code ec = getService(ctx, bootEnableIntf,
1722 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001723 if (!ec)
1724 {
1725 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath,
1726 bootEnableIntf, "Enabled", enable);
1727 if (!ec)
1728 {
1729 return ipmi::ccSuccess;
1730 }
1731 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001732 lg2::error("Error in Boot Override Enable Get: {ERROR}", "ERROR",
1733 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001734 return ipmi::ccUnspecifiedError;
1735}
1736
1737/** @brief Set the property value for boot override enable
1738 * @param[in] ctx - context pointer
1739 * @param[in] enable - boot override enable
1740 * @return On failure return IPMI error.
1741 */
1742static ipmi::Cc setBootEnable(ipmi::Context::ptr& ctx, const bool& enable)
1743{
1744 using namespace chassis::internal;
1745 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001746 boost::system::error_code ec = getService(ctx, bootEnableIntf,
1747 bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001748 if (!ec)
1749 {
1750 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath,
1751 bootEnableIntf, "Enabled", enable);
1752 if (!ec)
1753 {
1754 return ipmi::ccSuccess;
1755 }
1756 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001757 lg2::error("Error in Boot Source Override Enable Set: {ERROR}", "ERROR",
1758 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001759 return ipmi::ccUnspecifiedError;
1760}
1761
1762/** @brief Get the property value for boot override one-time
1763 * @param[in] ctx - context pointer
1764 * @param[out] onetime - boot override one-time
1765 * @return On failure return IPMI error.
1766 */
1767static ipmi::Cc getBootOneTime(ipmi::Context::ptr& ctx, bool& onetime)
1768{
1769 using namespace chassis::internal;
1770 std::string result;
1771 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001772 boost::system::error_code ec = getService(ctx, bootOneTimeIntf,
1773 bootSettingsOneTimePath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001774 if (!ec)
1775 {
1776 ec = ipmi::getDbusProperty(ctx, service, bootSettingsOneTimePath,
1777 bootOneTimeIntf, "Enabled", onetime);
1778 if (!ec)
1779 {
1780 return ipmi::ccSuccess;
1781 }
1782 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001783 lg2::error("Error in Boot Override OneTime Get: {ERROR}", "ERROR",
1784 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001785 return ipmi::ccUnspecifiedError;
1786}
1787
1788/** @brief Set the property value for boot override one-time
1789 * @param[in] ctx - context pointer
1790 * @param[in] onetime - boot override one-time
1791 * @return On failure return IPMI error.
1792 */
1793static ipmi::Cc setBootOneTime(ipmi::Context::ptr& ctx, const bool& onetime)
1794{
1795 using namespace chassis::internal;
1796 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001797 boost::system::error_code ec = getService(ctx, bootOneTimeIntf,
1798 bootSettingsOneTimePath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001799 if (!ec)
1800 {
1801 ec = ipmi::setDbusProperty(ctx, service, bootSettingsOneTimePath,
1802 bootOneTimeIntf, "Enabled", onetime);
1803 if (!ec)
1804 {
1805 return ipmi::ccSuccess;
1806 }
1807 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001808 lg2::error("Error in Boot Source Override OneTime Set: {ERROR}", "ERROR",
1809 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001810 return ipmi::ccUnspecifiedError;
1811}
1812
huangheab369282020-10-10 14:40:00 +08001813static constexpr uint8_t setComplete = 0x0;
1814static constexpr uint8_t setInProgress = 0x1;
1815static uint8_t transferStatus = setComplete;
Chen Yugang86ac4992021-06-25 08:14:52 +08001816static uint8_t bootFlagValidBitClr = 0;
Hieu Huynh38ffafc2021-05-14 08:40:14 +00001817static uint5_t bootInitiatorAckData = 0x0;
Hieu Huynh1982a3f2021-09-21 03:06:45 +00001818static bool cmosClear = false;
huangheab369282020-10-10 14:40:00 +08001819
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001820/** @brief implements the Get Chassis system boot option
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001821 * @param ctx - context pointer
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001822 * @param bootOptionParameter - boot option parameter selector
1823 * @param reserved1 - reserved bit
1824 * @param setSelector - selects a particular block or set of parameters
1825 * under the given parameter selector
1826 * write as 00h if parameter doesn't use a setSelector
1827 * @param blockSelector- selects a particular block within a set of
1828 * parameters write as 00h if parameter doesn't use a
1829 * blockSelector
1830 *
1831 * @return IPMI completion code plus response data
1832 * @return Payload contains below parameters:
1833 * version - parameter version
1834 * bootOptionParameter - boot option parameter selector
Patrick Venture8b1c3032020-09-15 09:43:03 -07001835 * parmIndicator - parameter valid/invalid indicator
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001836 * data - configuration parameter data
1837 */
1838ipmi::RspType<ipmi::message::Payload>
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001839 ipmiChassisGetSysBootOptions(ipmi::Context::ptr ctx,
1840 uint7_t bootOptionParameter, bool reserved1,
Willy Tu11d68892022-01-20 10:37:34 -08001841 [[maybe_unused]] uint8_t setSelector,
1842 [[maybe_unused]] uint8_t blockSelector)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001843{
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001844 ipmi::Cc rc;
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001845 if (reserved1)
1846 {
1847 return ipmi::responseInvalidFieldRequest();
1848 }
1849
1850 constexpr uint4_t version = 0x01;
1851 ipmi::message::Payload response;
1852 response.pack(version, uint4_t{});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001853 using namespace boot_options;
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001854
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001855 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001856
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001857 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1858 BootOptionParameter::setInProgress)
huangheab369282020-10-10 14:40:00 +08001859 {
1860 response.pack(bootOptionParameter, reserved1, transferStatus);
1861 return ipmi::responseSuccess(std::move(response));
1862 }
1863
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001864 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1865 BootOptionParameter::bootInfo)
John Wang0213f902020-12-28 14:49:57 +08001866 {
1867 constexpr uint8_t writeMask = 0;
Hieu Huynh38ffafc2021-05-14 08:40:14 +00001868 response.pack(bootOptionParameter, reserved1, writeMask,
1869 bootInitiatorAckData);
John Wang0213f902020-12-28 14:49:57 +08001870 return ipmi::responseSuccess(std::move(response));
1871 }
1872
Chen Yugang86ac4992021-06-25 08:14:52 +08001873 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1874 BootOptionParameter::bootFlagValidClr)
1875 {
1876 response.pack(bootOptionParameter, reserved1,
1877 uint5_t{bootFlagValidBitClr}, uint3_t{});
1878 return ipmi::responseSuccess(std::move(response));
1879 }
1880
shgoupfd84fbbf2015-12-17 10:05:51 +08001881 /*
1882 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1883 * This is the only parameter used by petitboot.
1884 */
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001885 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1886 BootOptionParameter::bootFlags)
Patrick Venture0b02be92018-08-31 11:55:55 -07001887 {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001888 using namespace chassis::internal;
1889 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001890
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001891 try
ratagupta6f6bff2016-04-04 06:20:11 -05001892 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001893 Source::Sources bootSource;
1894 rc = getBootSource(ctx, bootSource);
1895 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001896 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001897 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001898 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001899
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001900 Type::Types bootType;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001901 rc = getBootType(ctx, bootType);
1902 if (rc != ipmi::ccSuccess)
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001903 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001904 return ipmi::response(rc);
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001905 }
1906
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001907 Mode::Modes bootMode;
1908 rc = getBootMode(ctx, bootMode);
1909 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001910 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001911 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001912 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001913
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001914 bootOption = sourceDbusToIpmi.at(bootSource);
1915 if ((Mode::Modes::Regular == bootMode) &&
1916 (Source::Sources::Default == bootSource))
1917 {
1918 bootOption = ipmiDefault;
1919 }
1920 else if (Source::Sources::Default == bootSource)
1921 {
1922 bootOption = modeDbusToIpmi.at(bootMode);
1923 }
ratagupta6f6bff2016-04-04 06:20:11 -05001924
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001925 IpmiValue biosBootType = typeDbusToIpmi.at(bootType);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001926
1927 bool oneTimeEnabled;
1928 rc = getBootOneTime(ctx, oneTimeEnabled);
1929 if (rc != ipmi::ccSuccess)
1930 {
1931 return ipmi::response(rc);
1932 }
1933
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001934 uint1_t permanent = oneTimeEnabled ? 0 : 1;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001935
1936 bool valid;
1937 rc = getBootEnable(ctx, valid);
1938 if (rc != ipmi::ccSuccess)
1939 {
1940 return ipmi::response(rc);
1941 }
1942
1943 uint1_t validFlag = valid ? 1 : 0;
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001944
1945 response.pack(bootOptionParameter, reserved1, uint5_t{},
1946 uint1_t{biosBootType}, uint1_t{permanent},
1947 uint1_t{validFlag}, uint2_t{}, uint4_t{bootOption},
Hieu Huynh1982a3f2021-09-21 03:06:45 +00001948 uint1_t{}, cmosClear, uint8_t{}, uint8_t{},
1949 uint8_t{});
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001950 return ipmi::responseSuccess(std::move(response));
ratagupta6f6bff2016-04-04 06:20:11 -05001951 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001952 catch (const InternalFailure& e)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001953 {
James Feist225dec82019-11-26 16:25:06 -08001954 cache::objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001955 report<InternalFailure>();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001956 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001957 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001958 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001959 else
1960 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001961 if ((bootOptionParameter >= oemParmStart) &&
1962 (bootOptionParameter <= oemParmEnd))
1963 {
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001964 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1965 BootOptionParameter::opalNetworkSettings)
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001966 {
1967 response.pack(bootOptionParameter, reserved1);
1968 int ret = getHostNetworkData(response);
1969 if (ret < 0)
1970 {
1971 response.trailingOk = true;
Vernon Mauery38108d92024-03-11 12:53:55 -07001972 lg2::error(
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001973 "getHostNetworkData failed for GetSysBootOptions.");
1974 return ipmi::responseUnspecifiedError();
1975 }
1976 else
1977 {
1978 return ipmi::responseSuccess(std::move(response));
1979 }
1980 }
Jayaprakash Mutyala43a88102021-08-04 16:41:58 +00001981 else
1982 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001983 lg2::error(
1984 "ipmiChassisGetSysBootOptions: Unsupported parameter {PARAM}",
1985 "PARAM", lg2::hex,
1986 static_cast<uint8_t>(bootOptionParameter));
Jayaprakash Mutyala43a88102021-08-04 16:41:58 +00001987 return ipmi::responseParmNotSupported();
1988 }
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001989 }
1990 else
1991 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001992 lg2::error(
1993 "ipmiChassisGetSysBootOptions: Unsupported parameter {PARAM}",
1994 "PARAM", lg2::hex, static_cast<uint8_t>(bootOptionParameter));
Jayaprakash Mutyala6088e9f2021-07-14 07:40:29 +00001995 return ipmi::responseParmNotSupported();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001996 }
shgoupfd84fbbf2015-12-17 10:05:51 +08001997 }
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001998 return ipmi::responseUnspecifiedError();
shgoupfd84fbbf2015-12-17 10:05:51 +08001999}
2000
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002001ipmi::RspType<> ipmiChassisSetSysBootOptions(ipmi::Context::ptr ctx,
Willy Tu11d68892022-01-20 10:37:34 -08002002 uint7_t parameterSelector, bool,
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002003 ipmi::message::Payload& data)
shgoupfd84fbbf2015-12-17 10:05:51 +08002004{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002005 using namespace boot_options;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002006 ipmi::Cc rc;
shgoupfd84fbbf2015-12-17 10:05:51 +08002007
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002008 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2009 BootOptionParameter::setInProgress)
huangheab369282020-10-10 14:40:00 +08002010 {
2011 uint2_t setInProgressFlag;
2012 uint6_t rsvd;
2013 if (data.unpack(setInProgressFlag, rsvd) != 0 || !data.fullyUnpacked())
2014 {
2015 return ipmi::responseReqDataLenInvalid();
2016 }
2017 if (rsvd)
2018 {
2019 return ipmi::responseInvalidFieldRequest();
2020 }
2021 if ((transferStatus == setInProgress) &&
2022 (static_cast<uint8_t>(setInProgressFlag) != setComplete))
2023 {
2024 return ipmi::response(IPMI_CC_FAIL_SET_IN_PROGRESS);
2025 }
2026 transferStatus = static_cast<uint8_t>(setInProgressFlag);
2027 return ipmi::responseSuccess();
2028 }
2029
shgoupfd84fbbf2015-12-17 10:05:51 +08002030 /* 000101
2031 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
2032 * This is the only parameter used by petitboot.
2033 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05002034
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002035 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2036 BootOptionParameter::bootFlags)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002037 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002038 uint5_t rsvd;
2039 bool validFlag;
2040 bool permanent;
2041 bool biosBootType;
2042 bool lockOutResetButton;
2043 bool screenBlank;
2044 uint4_t bootDeviceSelector;
2045 bool lockKeyboard;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002046 uint8_t data3;
2047 uint4_t biosInfo;
2048 uint4_t rsvd1;
2049 uint5_t deviceInstance;
2050 uint3_t rsvd2;
2051
2052 if (data.unpack(rsvd, biosBootType, permanent, validFlag,
2053 lockOutResetButton, screenBlank, bootDeviceSelector,
2054 lockKeyboard, cmosClear, data3, biosInfo, rsvd1,
2055 deviceInstance, rsvd2) != 0 ||
2056 !data.fullyUnpacked())
2057 {
2058 return ipmi::responseReqDataLenInvalid();
2059 }
2060 if (rsvd || rsvd1 || rsvd2)
2061 {
2062 return ipmi::responseInvalidFieldRequest();
2063 }
2064
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002065 using namespace chassis::internal;
2066 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08002067
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002068 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002069 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002070 rc = setBootOneTime(ctx, !permanent);
2071 if (rc != ipmi::ccSuccess)
Konstantin Aladyshev69e8e842021-06-18 17:01:18 +03002072 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002073 return ipmi::response(rc);
2074 }
Vernon Maueryafd45db2021-07-29 16:27:25 +00002075
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002076 rc = setBootEnable(ctx, validFlag);
2077 if (rc != ipmi::ccSuccess)
2078 {
2079 return ipmi::response(rc);
Tom Joseph57e8eb72017-09-25 18:05:02 +05302080 }
2081
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002082 auto modeItr =
2083 modeIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002084 auto typeItr =
2085 typeIpmiToDbus.find(static_cast<uint8_t>(biosBootType));
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002086 auto sourceItr =
2087 sourceIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002088 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002089 {
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03002090 rc = setBootSource(ctx, sourceItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002091 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002092 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002093 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002094 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002095 // If a set boot device is mapping to a boot source, then reset
2096 // the boot mode D-Bus property to default.
2097 // This way the ipmid code can determine which property is not
2098 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07002099 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002100 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002101 rc = setBootMode(ctx, Mode::Modes::Regular);
2102 if (rc != ipmi::ccSuccess)
2103 {
2104 return ipmi::response(rc);
2105 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002106 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002107 }
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002108
2109 if (typeIpmiToDbus.end() != typeItr)
2110 {
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03002111 rc = setBootType(ctx, typeItr->second);
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002112 if (rc != ipmi::ccSuccess)
2113 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002114 return ipmi::response(rc);
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002115 }
2116 }
2117
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002118 if (modeIpmiToDbus.end() != modeItr)
2119 {
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03002120 rc = setBootMode(ctx, modeItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002121 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002122 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002123 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002124 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002125 // If a set boot device is mapping to a boot mode, then reset
2126 // the boot source D-Bus property to default.
2127 // This way the ipmid code can determine which property is not
2128 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07002129 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002130 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002131 rc = setBootSource(ctx, Source::Sources::Default);
2132 if (rc != ipmi::ccSuccess)
2133 {
2134 return ipmi::response(rc);
2135 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002136 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002137 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08002138 if ((modeIpmiToDbus.end() == modeItr) &&
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002139 (typeIpmiToDbus.end() == typeItr) &&
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08002140 (sourceIpmiToDbus.end() == sourceItr))
2141 {
2142 // return error if boot option is not supported
Vernon Mauery38108d92024-03-11 12:53:55 -07002143 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002144 "ipmiChassisSetSysBootOptions: Boot option not supported");
2145 return ipmi::responseInvalidFieldRequest();
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08002146 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002147 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05002148 catch (const sdbusplus::exception_t& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002149 {
James Feist225dec82019-11-26 16:25:06 -08002150 objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002151 report<InternalFailure>();
Vernon Mauery38108d92024-03-11 12:53:55 -07002152 lg2::error("ipmiChassisSetSysBootOptions: Error in setting Boot "
2153 "flag parameters");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002154 return ipmi::responseUnspecifiedError();
Ratan Guptafd28dd72016-08-01 04:58:01 -05002155 }
Patrick Venture0b02be92018-08-31 11:55:55 -07002156 }
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002157 else if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2158 BootOptionParameter::bootInfo)
Patrick Venture0b02be92018-08-31 11:55:55 -07002159 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002160 uint8_t writeMak;
Hieu Huynh38ffafc2021-05-14 08:40:14 +00002161 uint5_t bootInfoAck;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002162 uint3_t rsvd;
2163
Hieu Huynh38ffafc2021-05-14 08:40:14 +00002164 if (data.unpack(writeMak, bootInfoAck, rsvd) != 0 ||
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002165 !data.fullyUnpacked())
2166 {
2167 return ipmi::responseReqDataLenInvalid();
2168 }
2169 if (rsvd)
2170 {
2171 return ipmi::responseInvalidFieldRequest();
2172 }
Hieu Huynh38ffafc2021-05-14 08:40:14 +00002173 bootInitiatorAckData &= ~writeMak;
2174 bootInitiatorAckData |= (writeMak & bootInfoAck);
Vernon Mauery38108d92024-03-11 12:53:55 -07002175 lg2::info("ipmiChassisSetSysBootOptions: bootInfo parameter set "
2176 "successfully");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002177 data.trailingOk = true;
2178 return ipmi::responseSuccess();
Patrick Venture0b02be92018-08-31 11:55:55 -07002179 }
Chen Yugang86ac4992021-06-25 08:14:52 +08002180 else if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2181 BootOptionParameter::bootFlagValidClr)
2182 {
2183 uint5_t bootFlagValidClr;
2184 uint3_t rsvd;
2185
2186 if (data.unpack(bootFlagValidClr, rsvd) != 0 || !data.fullyUnpacked())
2187 {
2188 return ipmi::responseReqDataLenInvalid();
2189 }
2190 if (rsvd)
2191 {
2192 return ipmi::responseInvalidFieldRequest();
2193 }
2194 // store boot flag valid bits clear value
2195 bootFlagValidBitClr = static_cast<uint8_t>(bootFlagValidClr);
Vernon Mauery38108d92024-03-11 12:53:55 -07002196 lg2::info(
Chen Yugang86ac4992021-06-25 08:14:52 +08002197 "ipmiChassisSetSysBootOptions: bootFlagValidBits parameter set "
Vernon Mauery38108d92024-03-11 12:53:55 -07002198 "successfully to {VALUE}",
2199 "VALUE", lg2::hex, bootFlagValidBitClr);
Chen Yugang86ac4992021-06-25 08:14:52 +08002200 return ipmi::responseSuccess();
2201 }
Patrick Venture0b02be92018-08-31 11:55:55 -07002202 else
2203 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002204 if ((parameterSelector >= static_cast<uint7_t>(oemParmStart)) &&
2205 (parameterSelector <= static_cast<uint7_t>(oemParmEnd)))
2206 {
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002207 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2208 BootOptionParameter::opalNetworkSettings)
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002209 {
2210 ipmi::Cc ret = setHostNetworkData(data);
2211 if (ret != ipmi::ccSuccess)
2212 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002213 lg2::error("ipmiChassisSetSysBootOptions: Error in "
2214 "setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002215 data.trailingOk = true;
2216 return ipmi::response(ret);
2217 }
2218 data.trailingOk = true;
2219 return ipmi::responseSuccess();
2220 }
2221 else
2222 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002223 lg2::error(
2224 "ipmiChassisSetSysBootOptions: Unsupported param: {PARAM}",
2225 "PARAM", lg2::hex, static_cast<uint8_t>(parameterSelector));
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002226 data.trailingOk = true;
2227 return ipmi::responseParmNotSupported();
2228 }
2229 }
2230 data.trailingOk = true;
2231 return ipmi::responseParmNotSupported();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002232 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002233 return ipmi::responseSuccess();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002234}
2235
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002236/** @brief implements Get POH counter command
2237 * @parameter
2238 * - none
2239 * @returns IPMI completion code plus response data
2240 * - minPerCount - Minutes per count
2241 * - counterReading - counter reading
2242 */
2243ipmi::RspType<uint8_t, // Minutes per count
2244 uint32_t // Counter reading
2245 >
2246 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002247{
2248 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002249 try
2250 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002251 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
2252 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002253 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05002254 catch (const std::exception& e)
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002255 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002256 lg2::error("getPOHCounter error: {ERROR}", "ERROR", e);
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002257 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002258 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002259}
2260
Jason M. Billsbc996a32019-06-17 15:46:37 -07002261ipmi::RspType<uint3_t, // policy support
2262 uint5_t // reserved
2263 >
Vernon Mauerye278ead2018-10-09 09:23:43 -07002264 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
Jason M. Billsbc996a32019-06-17 15:46:37 -07002265 uint3_t policy, uint5_t reserved)
Yong Lic6713cf2018-09-12 12:35:13 +08002266{
Yong Lic6713cf2018-09-12 12:35:13 +08002267 power_policy::DbusValue value =
2268 power_policy::RestorePolicy::Policy::AlwaysOff;
2269
Jason M. Billsbc996a32019-06-17 15:46:37 -07002270 if (reserved || (policy > power_policy::noChange))
Yong Lic6713cf2018-09-12 12:35:13 +08002271 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002272 lg2::error("Reserved request parameter: {REQ}", "REQ", lg2::hex,
2273 static_cast<int>(policy));
Jason M. Billsbc996a32019-06-17 15:46:37 -07002274 return ipmi::responseInvalidFieldRequest();
Yong Lic6713cf2018-09-12 12:35:13 +08002275 }
2276
Vernon Mauerye278ead2018-10-09 09:23:43 -07002277 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08002278 {
2279 // just return the supported policy
Jason M. Billsbc996a32019-06-17 15:46:37 -07002280 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002281 }
2282
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05002283 for (const auto& it : power_policy::dbusToIpmi)
Yong Lic6713cf2018-09-12 12:35:13 +08002284 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07002285 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08002286 {
2287 value = it.first;
2288 break;
2289 }
2290 }
2291
2292 try
2293 {
James Feist225dec82019-11-26 16:25:06 -08002294 settings::Objects& objects = chassis::internal::cache::getObjects();
Yong Lic6713cf2018-09-12 12:35:13 +08002295 const settings::Path& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -08002296 objects.map.at(chassis::internal::powerRestoreIntf).front();
Vernon Mauery16b86932019-05-01 08:36:11 -07002297 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08002298
Vernon Mauerye278ead2018-10-09 09:23:43 -07002299 auto sdbusp = getSdBus();
2300 boost::system::error_code ec;
2301 sdbusp->yield_method_call<void>(
2302 yield, ec,
James Feist225dec82019-11-26 16:25:06 -08002303 objects
Yong Lic6713cf2018-09-12 12:35:13 +08002304 .service(powerRestoreSetting,
2305 chassis::internal::powerRestoreIntf)
2306 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07002307 powerRestoreSetting, ipmi::PROP_INTF, "Set",
2308 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
2309 property);
2310 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08002311 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002312 lg2::error("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07002313 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002314 }
2315 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05002316 catch (const InternalFailure& e)
Yong Lic6713cf2018-09-12 12:35:13 +08002317 {
James Feist225dec82019-11-26 16:25:06 -08002318 chassis::internal::cache::objectsPtr.reset();
Yong Lic6713cf2018-09-12 12:35:13 +08002319 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07002320 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002321 }
2322
Jason M. Billsbc996a32019-06-17 15:46:37 -07002323 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002324}
2325
Willy Tu11d68892022-01-20 10:37:34 -08002326ipmi::RspType<> ipmiSetFrontPanelButtonEnables(ipmi::Context::ptr ctx,
2327 bool disablePowerButton,
2328 bool disableResetButton, bool,
2329 bool, uint4_t)
Kuiying Wang21addc52019-01-04 10:50:21 +08002330{
2331 using namespace chassis::internal;
2332
2333 // set power button Enabled property
2334 bool success = setButtonEnabled(ctx, powerButtonPath, powerButtonIntf,
2335 !disablePowerButton);
2336
2337 // set reset button Enabled property
2338 success &= setButtonEnabled(ctx, resetButtonPath, resetButtonIntf,
2339 !disableResetButton);
2340
2341 if (!success)
2342 {
2343 // not all buttons were successfully set
2344 return ipmi::responseUnspecifiedError();
2345 }
2346 return ipmi::responseSuccess();
2347}
2348
Adriana Kobylak40814c62015-10-27 15:58:44 -05002349void register_netfn_chassis_functions()
2350{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05002351 createIdentifyTimer();
2352
Tom05732372016-09-06 17:21:23 +05302353 // Get Chassis Capabilities
anil kumar appana43263c62019-05-27 12:45:04 +00002354 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2355 ipmi::chassis::cmdGetChassisCapabilities,
2356 ipmi::Privilege::User, ipmiGetChassisCap);
Nan Li8d15fb42016-08-16 22:29:40 +08002357
Kuiying Wang21addc52019-01-04 10:50:21 +08002358 // Set Front Panel Button Enables
2359 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2360 ipmi::chassis::cmdSetFrontPanelButtonEnables,
2361 ipmi::Privilege::Admin,
2362 ipmiSetFrontPanelButtonEnables);
2363
Yong Liae4b0402018-11-02 11:12:14 +08002364 // Set Chassis Capabilities
anil kumar appana894d0222019-05-27 16:32:14 +00002365 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2366 ipmi::chassis::cmdSetChassisCapabilities,
2367 ipmi::Privilege::User, ipmiSetChassisCap);
Yong Liae4b0402018-11-02 11:12:14 +08002368
Tom05732372016-09-06 17:21:23 +05302369 // <Get System Boot Options>
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00002370 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2371 ipmi::chassis::cmdGetSystemBootOptions,
2372 ipmi::Privilege::Operator,
2373 ipmiChassisGetSysBootOptions);
Adriana Kobylak40814c62015-10-27 15:58:44 -05002374
Tom05732372016-09-06 17:21:23 +05302375 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07002376 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2377 ipmi::chassis::cmdGetChassisStatus,
2378 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08002379
Vijay Khemka074f64d2020-03-03 15:08:43 -08002380 // <Chassis Get System Restart Cause>
2381 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2382 ipmi::chassis::cmdGetSystemRestartCause,
2383 ipmi::Privilege::User, ipmiGetSystemRestartCause);
2384
Tom05732372016-09-06 17:21:23 +05302385 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00002386 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2387 ipmi::chassis::cmdChassisControl,
2388 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08002389
Tom Joseph5110c122018-03-23 17:55:40 +05302390 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07002391 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2392 ipmi::chassis::cmdChassisIdentify,
2393 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05302394
Tom05732372016-09-06 17:21:23 +05302395 // <Set System Boot Options>
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002396 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2397 ipmi::chassis::cmdSetSystemBootOptions,
2398 ipmi::Privilege::Operator,
2399 ipmiChassisSetSysBootOptions);
2400
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002401 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002402 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2403 ipmi::chassis::cmdGetPohCounter,
2404 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08002405
2406 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07002407 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2408 ipmi::chassis::cmdSetPowerRestorePolicy,
2409 ipmi::Privilege::Operator,
2410 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06002411}