blob: 850b09054f4a4308c98eedfb0e87303171e9ed24 [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;
43
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000044constexpr size_t sizeVersion = 2;
Tom Joseph5110c122018-03-23 17:55:40 +053045constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +053046
Patrick Venture0b02be92018-08-31 11:55:55 -070047// PetiBoot-Specific
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000048static constexpr uint8_t netConfInitialBytes[] = {0x80, 0x21, 0x70, 0x62,
49 0x21, 0x00, 0x01, 0x06};
50static constexpr uint8_t oemParmStart = 96;
51static constexpr uint8_t oemParmEnd = 127;
Ratan Guptafd28dd72016-08-01 04:58:01 -050052
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000053static constexpr size_t cookieOffset = 1;
54static constexpr size_t versionOffset = 5;
55static constexpr size_t addrSizeOffset = 8;
56static constexpr size_t macOffset = 9;
57static constexpr size_t addrTypeOffset = 16;
58static constexpr size_t ipAddrOffset = 17;
ratagupta6f6bff2016-04-04 06:20:11 -050059
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +000060namespace ipmi
61{
62constexpr Cc ccParmNotSupported = 0x80;
63
64static inline auto responseParmNotSupported()
65{
66 return response(ccParmNotSupported);
67}
68} // namespace ipmi
69
Adriana Kobylak40814c62015-10-27 15:58:44 -050070void register_netfn_chassis_functions() __attribute__((constructor));
71
shgoupfd84fbbf2015-12-17 10:05:51 +080072// Host settings in dbus
73// Service name should be referenced by connection name got via object mapper
Patrick Venture0b02be92018-08-31 11:55:55 -070074const char* settings_object_name = "/org/openbmc/settings/host0";
75const char* settings_intf_name = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070076const char* identify_led_object_name =
Tom Joseph5110c122018-03-23 17:55:40 +053077 "/xyz/openbmc_project/led/groups/enclosure_identify";
shgoupfd84fbbf2015-12-17 10:05:51 +080078
Ratan Guptadcb10672017-07-10 10:33:50 +053079constexpr auto SETTINGS_ROOT = "/";
80constexpr auto SETTINGS_MATCH = "host0";
Ratan Guptadcb10672017-07-10 10:33:50 +053081
82constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
83constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
84
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050085static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
86static constexpr auto chassisPOHStateIntf =
Patrick Venture0b02be92018-08-31 11:55:55 -070087 "xyz.openbmc_project.State.PowerOnHours";
Patrick Williams7345ac42021-04-30 20:57:39 -050088static constexpr auto pohCounterProperty = "POHCounter";
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -050089static constexpr auto match = "chassis0";
Yong Liae4b0402018-11-02 11:12:14 +080090const static constexpr char chassisCapIntf[] =
91 "xyz.openbmc_project.Control.ChassisCapabilities";
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -080092const static constexpr char chassisIntrusionProp[] = "ChassisIntrusionEnabled";
93const static constexpr char chassisFrontPanelLockoutProp[] =
94 "ChassisFrontPanelLockoutEnabled";
95const static constexpr char chassisNMIProp[] = "ChassisNMIEnabled";
96const static constexpr char chassisPowerInterlockProp[] =
97 "ChassisPowerInterlockEnabled";
Yong Liae4b0402018-11-02 11:12:14 +080098const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
99const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
100const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
101const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
102const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
Yong Liae4b0402018-11-02 11:12:14 +0800103static constexpr uint8_t chassisCapAddrMask = 0xfe;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700104static constexpr const char* powerButtonIntf =
105 "xyz.openbmc_project.Chassis.Buttons.Power";
106static constexpr const char* powerButtonPath =
107 "/xyz/openbmc_project/Chassis/Buttons/Power0";
108static constexpr const char* resetButtonIntf =
109 "xyz.openbmc_project.Chassis.Buttons.Reset";
110static constexpr const char* resetButtonPath =
111 "/xyz/openbmc_project/Chassis/Buttons/Reset0";
Ratan Guptadcb10672017-07-10 10:33:50 +0530112
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530113// Phosphor Host State manager
Willy Tu523e2d12023-09-05 11:36:48 -0700114namespace State = sdbusplus::server::xyz::openbmc_project::state;
Vernon Mauery185b9f82018-07-20 10:52:36 -0700115namespace fs = std::filesystem;
Andrew Geisslera6e3a302017-05-31 19:34:00 -0500116
Ratan Guptadcb10672017-07-10 10:33:50 +0530117using namespace phosphor::logging;
Willy Tu523e2d12023-09-05 11:36:48 -0700118using namespace sdbusplus::error::xyz::openbmc_project::common;
119using namespace sdbusplus::server::xyz::openbmc_project::control::boot;
William A. Kennington III4c008022018-10-12 17:18:14 -0700120
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500121namespace chassis
122{
123namespace internal
124{
125
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +0300126constexpr auto bootSettingsPath = "/xyz/openbmc_project/control/host0/boot";
127constexpr auto bootEnableIntf = "xyz.openbmc_project.Object.Enable";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500128constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +0300129constexpr auto bootTypeIntf = "xyz.openbmc_project.Control.Boot.Type";
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500130constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +0300131constexpr auto bootSettingsOneTimePath =
132 "/xyz/openbmc_project/control/host0/boot/one_time";
133constexpr auto bootOneTimeIntf = "xyz.openbmc_project.Object.Enable";
134
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500135constexpr auto powerRestoreIntf =
136 "xyz.openbmc_project.Control.Power.RestorePolicy";
Patrick Williams5d82f472022-07-22 19:26:53 -0500137sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection());
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500138
139namespace cache
140{
141
James Feist225dec82019-11-26 16:25:06 -0800142std::unique_ptr<settings::Objects> objectsPtr = nullptr;
143
144settings::Objects& getObjects()
145{
146 if (objectsPtr == nullptr)
147 {
148 objectsPtr = std::make_unique<settings::Objects>(
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +0300149 dbus, std::vector<std::string>{bootModeIntf, bootTypeIntf,
150 bootSourceIntf, powerRestoreIntf});
James Feist225dec82019-11-26 16:25:06 -0800151 }
152 return *objectsPtr;
153}
Deepak Kodihalli8cc19362017-07-21 11:18:38 -0500154
155} // namespace cache
156} // namespace internal
157} // namespace chassis
158
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500159namespace poh
160{
161
162constexpr auto minutesPerCount = 60;
163
164} // namespace poh
165
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000166int getHostNetworkData(ipmi::message::Payload& payload)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500167{
Ratan Guptadcb10672017-07-10 10:33:50 +0530168 ipmi::PropertyMap properties;
169 int rc = 0;
Ratan Gupta8c31d232017-08-13 05:49:43 +0530170 uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500171
Ratan Guptadcb10672017-07-10 10:33:50 +0530172 try
173 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700174 // TODO There may be cases where an interface is implemented by multiple
Ratan Guptadcb10672017-07-10 10:33:50 +0530175 // objects,to handle such cases we are interested on that object
176 // which are on interested busname.
177 // Currenlty mapper doesn't give the readable busname(gives busid)
178 // so we can't match with bus name so giving some object specific info
179 // as SETTINGS_MATCH.
180 // Later SETTINGS_MATCH will be replaced with busname.
Ratan Guptafd28dd72016-08-01 04:58:01 -0500181
Patrick Williams5d82f472022-07-22 19:26:53 -0500182 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Ratan Guptadcb10672017-07-10 10:33:50 +0530183
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530184 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
185 SETTINGS_ROOT, SETTINGS_MATCH);
186
187 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
188 SETTINGS_ROOT, SETTINGS_MATCH);
189
Patrick Venture0b02be92018-08-31 11:55:55 -0700190 properties = ipmi::getAllDbusProperties(
191 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
Patrick Williams1318a5e2024-08-16 15:19:54 -0400192 auto variant = ipmi::getDbusProperty(
193 bus, macObjectInfo.second, macObjectInfo.first, MAC_INTERFACE,
194 "MACAddress");
Ratan Guptadcb10672017-07-10 10:33:50 +0530195
Vernon Maueryf442e112019-04-09 11:44:36 -0700196 auto ipAddress = std::get<std::string>(properties["Address"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530197
Vernon Maueryf442e112019-04-09 11:44:36 -0700198 auto gateway = std::get<std::string>(properties["Gateway"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530199
Vernon Maueryf442e112019-04-09 11:44:36 -0700200 auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptad70f4532017-08-04 02:07:31 +0530201
Patrick Venture0b02be92018-08-31 11:55:55 -0700202 uint8_t isStatic =
Vernon Maueryf442e112019-04-09 11:44:36 -0700203 (std::get<std::string>(properties["Origin"]) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700204 "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
205 ? 1
206 : 0;
Ratan Guptad70f4532017-08-04 02:07:31 +0530207
Vernon Maueryf442e112019-04-09 11:44:36 -0700208 auto MACAddress = std::get<std::string>(variant);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530209
Ratan Guptad70f4532017-08-04 02:07:31 +0530210 // it is expected here that we should get the valid data
211 // but we may also get the default values.
212 // Validation of the data is done by settings.
213 //
214 // if mac address is default mac address then
215 // don't send blank override.
Ratan Gupta8c31d232017-08-13 05:49:43 +0530216 if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
Ratan Guptad70f4532017-08-04 02:07:31 +0530217 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530218 rc = -1;
219 return rc;
220 }
221 // if addr is static then ipaddress,gateway,prefix
222 // should not be default one,don't send blank override.
223 if (isStatic)
224 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700225 if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
226 (gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
Ratan Guptad70f4532017-08-04 02:07:31 +0530227 {
Ratan Guptad70f4532017-08-04 02:07:31 +0530228 rc = -1;
229 return rc;
230 }
231 }
232
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000233 std::string token;
234 std::stringstream ss(MACAddress);
Ratan Guptadcb10672017-07-10 10:33:50 +0530235
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000236 // First pack macOffset no of bytes in payload.
237 // Latter this PetiBoot-Specific data will be populated.
238 std::vector<uint8_t> payloadInitialBytes(macOffset);
239 payload.pack(payloadInitialBytes);
Ratan Guptadcb10672017-07-10 10:33:50 +0530240
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000241 while (std::getline(ss, token, ':'))
242 {
243 payload.pack(stoi(token, nullptr, 16));
244 }
245
246 payload.pack(0x00);
247
248 payload.pack(isStatic);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530249
Vernon Maueryf442e112019-04-09 11:44:36 -0700250 uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
251 "xyz.openbmc_project.Network.IP.Protocol.IPv4")
252 ? AF_INET
253 : AF_INET6;
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530254
Patrick Venture0b02be92018-08-31 11:55:55 -0700255 addrSize = (addressFamily == AF_INET)
256 ? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
257 : ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
Ratan Guptadcb10672017-07-10 10:33:50 +0530258
259 // ipaddress and gateway would be in IPv4 format
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000260 std::vector<uint8_t> addrInBinary(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530261 inet_pton(addressFamily, ipAddress.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000262 reinterpret_cast<void*>(addrInBinary.data()));
Ratan Guptadcb10672017-07-10 10:33:50 +0530263
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000264 payload.pack(addrInBinary);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530265
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000266 payload.pack(prefix);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530267
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000268 std::vector<uint8_t> gatewayDetails(addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530269 inet_pton(addressFamily, gateway.c_str(),
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000270 reinterpret_cast<void*>(gatewayDetails.data()));
271 payload.pack(gatewayDetails);
Ratan Guptadcb10672017-07-10 10:33:50 +0530272 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500273 catch (const InternalFailure& e)
Ratan Guptadcb10672017-07-10 10:33:50 +0530274 {
275 commit<InternalFailure>();
Ratan Guptadcb10672017-07-10 10:33:50 +0530276 rc = -1;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500277 return rc;
278 }
279
Patrick Venture0b02be92018-08-31 11:55:55 -0700280 // PetiBoot-Specific
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000281 // If success then copy the first 9 bytes to the payload message
282 // payload first 2 bytes contain the parameter values. Skip that 2 bytes.
283 uint8_t skipFirstTwoBytes = 2;
284 size_t payloadSize = payload.size();
285 uint8_t* configDataStartingAddress = payload.data() + skipFirstTwoBytes;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500286
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000287 if (payloadSize < skipFirstTwoBytes + sizeof(netConfInitialBytes))
288 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700289 lg2::error("Invalid net config");
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000290 rc = -1;
291 return rc;
292 }
293 std::copy(netConfInitialBytes,
294 netConfInitialBytes + sizeof(netConfInitialBytes),
295 configDataStartingAddress);
296
297 if (payloadSize < skipFirstTwoBytes + addrSizeOffset + sizeof(addrSize))
298 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700299 lg2::error("Invalid length of address size");
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000300 rc = -1;
301 return rc;
302 }
303 std::copy(&addrSize, &(addrSize) + sizeof(addrSize),
304 configDataStartingAddress + addrSizeOffset);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530305
Ratan Guptafd28dd72016-08-01 04:58:01 -0500306#ifdef _IPMI_DEBUG_
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700307 std::printf("\n===Printing the IPMI Formatted Data========\n");
Ratan Guptafd28dd72016-08-01 04:58:01 -0500308
Ratan Guptadcb10672017-07-10 10:33:50 +0530309 for (uint8_t pos = 0; pos < index; pos++)
310 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +0000311 std::printf("%02x ", payloadStartingAddress[pos]);
Ratan Guptadcb10672017-07-10 10:33:50 +0530312 }
Ratan Guptafd28dd72016-08-01 04:58:01 -0500313#endif
314
Ratan Guptafd28dd72016-08-01 04:58:01 -0500315 return rc;
316}
317
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530318/** @brief convert IPv4 and IPv6 addresses from binary to text form.
319 * @param[in] family - IPv4/Ipv6
320 * @param[in] data - req data pointer.
321 * @param[in] offset - offset in the data.
322 * @param[in] addrSize - size of the data which needs to be read from offset.
323 * @returns address in text form.
324 */
325
Patrick Venture0b02be92018-08-31 11:55:55 -0700326std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
327 uint8_t addrSize)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530328{
329 char ipAddr[INET6_ADDRSTRLEN] = {};
330
Patrick Venture0b02be92018-08-31 11:55:55 -0700331 switch (family)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530332 {
333 case AF_INET:
334 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700335 struct sockaddr_in addr4
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500336 {};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700337 std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530338
Patrick Venture0b02be92018-08-31 11:55:55 -0700339 inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530340
341 break;
342 }
343 case AF_INET6:
344 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700345 struct sockaddr_in6 addr6
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500346 {};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700347 std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530348
Patrick Venture0b02be92018-08-31 11:55:55 -0700349 inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530350
351 break;
352 }
353 default:
354 {
355 return {};
356 }
357 }
358
359 return ipAddr;
360}
361
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000362ipmi::Cc setHostNetworkData(ipmi::message::Payload& data)
Ratan Guptafd28dd72016-08-01 04:58:01 -0500363{
Ratan Guptadcb10672017-07-10 10:33:50 +0530364 using namespace std::string_literals;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000365 std::string hostNetworkConfig;
366 std::string mac("00:00:00:00:00:00");
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530367 std::string ipAddress, gateway;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000368 std::string addrOrigin{0};
Patrick Venture0b02be92018-08-31 11:55:55 -0700369 uint8_t addrSize{0};
Ratan Guptadcb10672017-07-10 10:33:50 +0530370 std::string addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530371 "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
Patrick Venture0b02be92018-08-31 11:55:55 -0700372 std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
373 uint8_t prefix{0};
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530374 uint8_t family = AF_INET;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500375
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 // cookie starts from second byte
Ratan Guptafd28dd72016-08-01 04:58:01 -0500377 // version starts from sixth byte
378
Ratan Guptadcb10672017-07-10 10:33:50 +0530379 try
Ratan Guptafd28dd72016-08-01 04:58:01 -0500380 {
Ratan Guptadcb10672017-07-10 10:33:50 +0530381 do
382 {
383 // cookie == 0x21 0x70 0x62 0x21
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000384 data.trailingOk = true;
385 auto msgLen = data.size();
386 std::vector<uint8_t> msgPayloadBytes(msgLen);
387 if (data.unpack(msgPayloadBytes) != 0 || !data.fullyUnpacked())
Ratan Guptadcb10672017-07-10 10:33:50 +0530388 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700389 lg2::error("Error in unpacking message of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000390 return ipmi::ccReqDataLenInvalid;
391 }
392
393 uint8_t* msgPayloadStartingPos = msgPayloadBytes.data();
394 constexpr size_t cookieSize = 4;
395 if (msgLen < cookieOffset + cookieSize)
396 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700397 lg2::error("Error in cookie getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000398 return ipmi::ccReqDataLenInvalid;
399 }
400 if (std::equal(msgPayloadStartingPos + cookieOffset,
401 msgPayloadStartingPos + cookieOffset + cookieSize,
402 (netConfInitialBytes + cookieOffset)) != 0)
403 {
404 // all cookie == 0
405 if (std::all_of(msgPayloadStartingPos + cookieOffset,
406 msgPayloadStartingPos + cookieOffset +
407 cookieSize,
408 [](int i) { return i == 0; }) == true)
Ratan Guptadcb10672017-07-10 10:33:50 +0530409 {
410 // need to zero out the network settings.
411 break;
412 }
413
Vernon Mauery38108d92024-03-11 12:53:55 -0700414 lg2::error("Invalid Cookie");
Ratan Guptadcb10672017-07-10 10:33:50 +0530415 elog<InternalFailure>();
416 }
417
418 // vesion == 0x00 0x01
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000419 if (msgLen < versionOffset + sizeVersion)
Ratan Guptadcb10672017-07-10 10:33:50 +0530420 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700421 lg2::error("Error in version getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000422 return ipmi::ccReqDataLenInvalid;
423 }
424 if (std::equal(msgPayloadStartingPos + versionOffset,
425 msgPayloadStartingPos + versionOffset + sizeVersion,
426 (netConfInitialBytes + versionOffset)) != 0)
427 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700428 lg2::error("Invalid Version");
Ratan Guptadcb10672017-07-10 10:33:50 +0530429 elog<InternalFailure>();
430 }
431
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000432 if (msgLen < macOffset + 6)
433 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700434 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000435 "Error in mac address getting of setHostNetworkData");
436 return ipmi::ccReqDataLenInvalid;
437 }
438 std::stringstream result;
439 std::copy((msgPayloadStartingPos + macOffset),
440 (msgPayloadStartingPos + macOffset + 5),
441 std::ostream_iterator<int>(result, ":"));
442 mac = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530443
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000444 if (msgLen < addrTypeOffset + sizeof(decltype(addrOrigin)))
445 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700446 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000447 "Error in original address getting of setHostNetworkData");
448 return ipmi::ccReqDataLenInvalid;
449 }
450 std::copy(msgPayloadStartingPos + addrTypeOffset,
451 msgPayloadStartingPos + addrTypeOffset +
452 sizeof(decltype(addrOrigin)),
453 std::ostream_iterator<int>(result, ""));
454 addrOrigin = result.str();
Ratan Guptadcb10672017-07-10 10:33:50 +0530455
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000456 if (!addrOrigin.empty())
Ratan Guptadcb10672017-07-10 10:33:50 +0530457 {
458 addressOrigin =
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530459 "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
Ratan Guptadcb10672017-07-10 10:33:50 +0530460 }
461
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000462 if (msgLen < addrSizeOffset + sizeof(decltype(addrSize)))
463 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700464 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000465 "Error in address size getting of setHostNetworkData");
466 return ipmi::ccReqDataLenInvalid;
467 }
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530468 // Get the address size
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000469 std::copy(msgPayloadStartingPos + addrSizeOffset,
470 (msgPayloadStartingPos + addrSizeOffset +
471 sizeof(decltype(addrSize))),
472 &addrSize);
Ratan Guptadcb10672017-07-10 10:33:50 +0530473
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000474 uint8_t prefixOffset = ipAddrOffset + addrSize;
475 if (msgLen < prefixOffset + sizeof(decltype(prefix)))
476 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700477 lg2::error("Error in prefix getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000478 return ipmi::ccReqDataLenInvalid;
479 }
William A. Kennington III4c521022023-07-28 13:07:30 -0700480 // std::copy(msgPayloadStartingPos + prefixOffset,
481 // msgPayloadStartingPos + prefixOffset +
482 // sizeof(decltype(prefix)),
483 // &prefix);
484 // Workaround compiler misdetecting out of bounds memcpy
485 prefix = msgPayloadStartingPos[prefixOffset];
Ratan Guptadcb10672017-07-10 10:33:50 +0530486
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530487 uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
Ratan Gupta8c31d232017-08-13 05:49:43 +0530488 if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530489 {
490 addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
491 family = AF_INET6;
492 }
493
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000494 if (msgLen < ipAddrOffset + addrSize)
495 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700496 lg2::error("Error in IP address getting of setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000497 return ipmi::ccReqDataLenInvalid;
498 }
499 ipAddress = getAddrStr(family, msgPayloadStartingPos, ipAddrOffset,
500 addrSize);
Ratan Guptad70f4532017-08-04 02:07:31 +0530501
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000502 if (msgLen < gatewayOffset + addrSize)
503 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700504 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000505 "Error in gateway address getting of setHostNetworkData");
506 return ipmi::ccReqDataLenInvalid;
507 }
508 gateway = getAddrStr(family, msgPayloadStartingPos, gatewayOffset,
509 addrSize);
Ratan Gupta6ec7daa2017-07-15 14:13:01 +0530510
Patrick Venture0b02be92018-08-31 11:55:55 -0700511 } while (0);
Ratan Guptadcb10672017-07-10 10:33:50 +0530512
Patrick Venture0b02be92018-08-31 11:55:55 -0700513 // Cookie == 0 or it is a valid cookie
Patrick Williams1318a5e2024-08-16 15:19:54 -0400514 hostNetworkConfig +=
515 "ipaddress="s + ipAddress + ",prefix="s + std::to_string(prefix) +
516 ",gateway="s + gateway + ",mac="s + mac + ",addressOrigin="s +
517 addressOrigin;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500518
Patrick Williams5d82f472022-07-22 19:26:53 -0500519 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530520
521 auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
522 SETTINGS_ROOT, SETTINGS_MATCH);
523 auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
524 SETTINGS_ROOT, SETTINGS_MATCH);
Ratan Guptadcb10672017-07-10 10:33:50 +0530525 // set the dbus property
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530526 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700527 IP_INTERFACE, "Address", std::string(ipAddress));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530528 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700529 IP_INTERFACE, "PrefixLength", prefix);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530530 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700531 IP_INTERFACE, "Origin", addressOrigin);
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530532 ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700533 IP_INTERFACE, "Gateway", std::string(gateway));
534 ipmi::setDbusProperty(
535 bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
536 std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530537 ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
Patrick Venture0b02be92018-08-31 11:55:55 -0700538 MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptafd28dd72016-08-01 04:58:01 -0500539
Vernon Mauery38108d92024-03-11 12:53:55 -0700540 lg2::debug("Network configuration changed: {NETWORKCONFIG}",
541 "NETWORKCONFIG", hostNetworkConfig);
Ratan Guptafd28dd72016-08-01 04:58:01 -0500542 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500543 catch (const sdbusplus::exception_t& e)
Ratan Guptadcb10672017-07-10 10:33:50 +0530544 {
545 commit<InternalFailure>();
Vernon Mauery38108d92024-03-11 12:53:55 -0700546 lg2::error("Error in ipmiChassisSetSysBootOptions call");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000547 return ipmi::ccUnspecifiedError;
Ratan Guptadcb10672017-07-10 10:33:50 +0530548 }
549
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +0000550 return ipmi::ccSuccess;
Ratan Guptafd28dd72016-08-01 04:58:01 -0500551}
552
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500553uint32_t getPOHCounter()
554{
Patrick Williams5d82f472022-07-22 19:26:53 -0500555 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500556
Patrick Williams1318a5e2024-08-16 15:19:54 -0400557 auto chassisStateObj =
558 ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500559
Patrick Williams1318a5e2024-08-16 15:19:54 -0400560 auto service =
561 ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500562
Patrick Williams1318a5e2024-08-16 15:19:54 -0400563 auto propValue =
564 ipmi::getDbusProperty(bus, service, chassisStateObj.first,
565 chassisPOHStateIntf, pohCounterProperty);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500566
Vernon Maueryf442e112019-04-09 11:44:36 -0700567 return std::get<uint32_t>(propValue);
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -0500568}
569
anil kumar appana43263c62019-05-27 12:45:04 +0000570/** @brief Implements the get chassis capabilities command
571 *
572 * @returns IPMI completion code plus response data
573 * chassisCapFlags - chassis capability flag
574 * chassisFRUInfoDevAddr - chassis FRU info Device Address
575 * chassisSDRDevAddr - chassis SDR device address
576 * chassisSELDevAddr - chassis SEL device address
577 * chassisSMDevAddr - chassis system management device address
578 * chassisBridgeDevAddr - chassis bridge device address
579 */
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800580ipmi::RspType<bool, // chassis intrusion sensor
581 bool, // chassis Front panel lockout
582 bool, // chassis NMI
583 bool, // chassis power interlock
584 uint4_t, // reserved
anil kumar appana43263c62019-05-27 12:45:04 +0000585 uint8_t, // chassis FRU info Device Address
586 uint8_t, // chassis SDR device address
587 uint8_t, // chassis SEL device address
588 uint8_t, // chassis system management device address
589 uint8_t // chassis bridge device address
590 >
591 ipmiGetChassisCap()
Nan Li8d15fb42016-08-16 22:29:40 +0800592{
anil kumar appana43263c62019-05-27 12:45:04 +0000593 ipmi::PropertyMap properties;
Yong Liae4b0402018-11-02 11:12:14 +0800594 try
595 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500596 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Nan Li8d15fb42016-08-16 22:29:40 +0800597
Yong Liae4b0402018-11-02 11:12:14 +0800598 ipmi::DbusObjectInfo chassisCapObject =
599 ipmi::getDbusObject(bus, chassisCapIntf);
Nan Li8d15fb42016-08-16 22:29:40 +0800600
Yong Liae4b0402018-11-02 11:12:14 +0800601 // capabilities flags
602 // [7..4] - reserved
603 // [3] – 1b = provides power interlock (IPM 1.5)
604 // [2] – 1b = provides Diagnostic Interrupt (FP NMI)
605 // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
606 // has capabilities
607 // to lock out external power control and reset button or
608 // front panel interfaces and/or detect tampering with those
609 // interfaces).
610 // [0] -1b = Chassis provides intrusion (physical security) sensor.
611 // set to default value 0x0.
Nan Li8d15fb42016-08-16 22:29:40 +0800612
Patrick Williams1318a5e2024-08-16 15:19:54 -0400613 properties =
614 ipmi::getAllDbusProperties(bus, chassisCapObject.second,
615 chassisCapObject.first, chassisCapIntf);
Yong Liae4b0402018-11-02 11:12:14 +0800616 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500617 catch (const std::exception& e)
Yong Liae4b0402018-11-02 11:12:14 +0800618 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700619 lg2::error("Failed to fetch Chassis Capability properties: {ERROR}",
620 "ERROR", e);
anil kumar appana43263c62019-05-27 12:45:04 +0000621 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800622 }
623
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800624 bool* chassisIntrusionFlag =
625 std::get_if<bool>(&properties[chassisIntrusionProp]);
626 if (chassisIntrusionFlag == nullptr)
anil kumar appana43263c62019-05-27 12:45:04 +0000627 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700628 lg2::error("Error to get chassis Intrusion flags");
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800629 return ipmi::responseUnspecifiedError();
630 }
631 bool* chassisFrontPanelFlag =
632 std::get_if<bool>(&properties[chassisFrontPanelLockoutProp]);
633 if (chassisFrontPanelFlag == nullptr)
634 {
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* chassisNMIFlag = std::get_if<bool>(&properties[chassisNMIProp]);
639 if (chassisNMIFlag == nullptr)
640 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700641 lg2::error("Error to get chassis NMI flags");
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800642 return ipmi::responseUnspecifiedError();
643 }
644 bool* chassisPowerInterlockFlag =
645 std::get_if<bool>(&properties[chassisPowerInterlockProp]);
646 if (chassisPowerInterlockFlag == nullptr)
647 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700648 lg2::error("Error to get chassis power interlock flags");
anil kumar appana43263c62019-05-27 12:45:04 +0000649 return ipmi::responseUnspecifiedError();
650 }
651 uint8_t* chassisFRUInfoDevAddr =
652 std::get_if<uint8_t>(&properties[chassisFRUDevAddrProp]);
653 if (chassisFRUInfoDevAddr == nullptr)
654 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700655 lg2::error("Error to get chassis FRU info device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000656 return ipmi::responseUnspecifiedError();
657 }
658 uint8_t* chassisSDRDevAddr =
659 std::get_if<uint8_t>(&properties[chassisSDRDevAddrProp]);
660 if (chassisSDRDevAddr == nullptr)
661 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700662 lg2::error("Error to get chassis SDR device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000663 return ipmi::responseUnspecifiedError();
664 }
665 uint8_t* chassisSELDevAddr =
666 std::get_if<uint8_t>(&properties[chassisSELDevAddrProp]);
667 if (chassisSELDevAddr == nullptr)
668 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700669 lg2::error("Error to get chassis SEL device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000670 return ipmi::responseUnspecifiedError();
671 }
672 uint8_t* chassisSMDevAddr =
673 std::get_if<uint8_t>(&properties[chassisSMDevAddrProp]);
674 if (chassisSMDevAddr == nullptr)
675 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700676 lg2::error("Error to get chassis SM device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000677 return ipmi::responseUnspecifiedError();
678 }
679 uint8_t* chassisBridgeDevAddr =
680 std::get_if<uint8_t>(&properties[chassisBridgeDevAddrProp]);
681 if (chassisBridgeDevAddr == nullptr)
682 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700683 lg2::error("Error to get chassis bridge device address");
anil kumar appana43263c62019-05-27 12:45:04 +0000684 return ipmi::responseUnspecifiedError();
685 }
686
Patrick Williams1318a5e2024-08-16 15:19:54 -0400687 return ipmi::responseSuccess(
688 *chassisIntrusionFlag, *chassisFrontPanelFlag, *chassisNMIFlag,
689 *chassisPowerInterlockFlag, 0, *chassisFRUInfoDevAddr,
690 *chassisSDRDevAddr, *chassisSELDevAddr, *chassisSMDevAddr,
691 *chassisBridgeDevAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800692}
693
anil kumar appana894d0222019-05-27 16:32:14 +0000694/** @brief implements set chassis capalibities command
695 * @param intrusion - chassis intrusion
696 * @param fpLockout - frontpannel lockout
697 * @param reserved1 - skip one bit
698 * @param fruDeviceAddr - chassis FRU info Device Address
699 * @param sdrDeviceAddr - chassis SDR device address
700 * @param selDeviceAddr - chassis SEL device address
701 * @param smDeviceAddr - chassis system management device address
702 * @param bridgeDeviceAddr - chassis bridge device address
703 *
704 * @returns IPMI completion code
705 */
Patrick Williams1318a5e2024-08-16 15:19:54 -0400706ipmi::RspType<> ipmiSetChassisCap(
707 bool intrusion, bool fpLockout, uint6_t reserved1,
anil kumar appana894d0222019-05-27 16:32:14 +0000708
Patrick Williams1318a5e2024-08-16 15:19:54 -0400709 uint8_t fruDeviceAddr,
anil kumar appana894d0222019-05-27 16:32:14 +0000710
Patrick Williams1318a5e2024-08-16 15:19:54 -0400711 uint8_t sdrDeviceAddr,
anil kumar appana894d0222019-05-27 16:32:14 +0000712
Patrick Williams1318a5e2024-08-16 15:19:54 -0400713 uint8_t selDeviceAddr,
anil kumar appana894d0222019-05-27 16:32:14 +0000714
Patrick Williams1318a5e2024-08-16 15:19:54 -0400715 uint8_t smDeviceAddr,
anil kumar appana894d0222019-05-27 16:32:14 +0000716
Patrick Williams1318a5e2024-08-16 15:19:54 -0400717 uint8_t bridgeDeviceAddr)
Yong Liae4b0402018-11-02 11:12:14 +0800718{
Yong Liae4b0402018-11-02 11:12:14 +0800719 // check input data
anil kumar appana894d0222019-05-27 16:32:14 +0000720 if (reserved1 != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800721 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700722 lg2::error("Unsupported request parameter");
anil kumar appana894d0222019-05-27 16:32:14 +0000723 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800724 }
725
anil kumar appana894d0222019-05-27 16:32:14 +0000726 if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800727 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700728 lg2::error("Unsupported request parameter(FRU Addr) for REQ={REQ}",
729 "REQ", lg2::hex, fruDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000730 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800731 }
anil kumar appana894d0222019-05-27 16:32:14 +0000732 if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800733 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700734 lg2::error("Unsupported request parameter(SDR Addr) for REQ={REQ}",
735 "REQ", lg2::hex, sdrDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000736 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800737 }
738
anil kumar appana894d0222019-05-27 16:32:14 +0000739 if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800740 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700741 lg2::error("Unsupported request parameter(SEL Addr) for REQ={REQ}",
742 "REQ", lg2::hex, selDeviceAddr);
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 ((smDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800747 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700748 lg2::error("Unsupported request parameter(SM Addr) for REQ={REQ}",
749 "REQ", lg2::hex, smDeviceAddr);
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 ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
Yong Liae4b0402018-11-02 11:12:14 +0800754 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700755 lg2::error("Unsupported request parameter(Bridge Addr) for REQ={REQ}",
756 "REQ", lg2::hex, bridgeDeviceAddr);
anil kumar appana894d0222019-05-27 16:32:14 +0000757 return ipmi::responseInvalidFieldRequest();
Yong Liae4b0402018-11-02 11:12:14 +0800758 }
759
760 try
761 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500762 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Yong Liae4b0402018-11-02 11:12:14 +0800763 ipmi::DbusObjectInfo chassisCapObject =
764 ipmi::getDbusObject(bus, chassisCapIntf);
765
766 ipmi::setDbusProperty(bus, chassisCapObject.second,
767 chassisCapObject.first, chassisCapIntf,
Karthick Sundarrajan86d8bd72019-11-26 12:48:50 -0800768 chassisIntrusionProp, intrusion);
769
770 ipmi::setDbusProperty(bus, chassisCapObject.second,
771 chassisCapObject.first, chassisCapIntf,
772 chassisFrontPanelLockoutProp, fpLockout);
Yong Liae4b0402018-11-02 11:12:14 +0800773
774 ipmi::setDbusProperty(bus, chassisCapObject.second,
775 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000776 chassisFRUDevAddrProp, fruDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800777
778 ipmi::setDbusProperty(bus, chassisCapObject.second,
779 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000780 chassisSDRDevAddrProp, sdrDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800781
782 ipmi::setDbusProperty(bus, chassisCapObject.second,
783 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000784 chassisSELDevAddrProp, selDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800785
786 ipmi::setDbusProperty(bus, chassisCapObject.second,
787 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000788 chassisSMDevAddrProp, smDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800789
790 ipmi::setDbusProperty(bus, chassisCapObject.second,
791 chassisCapObject.first, chassisCapIntf,
anil kumar appana894d0222019-05-27 16:32:14 +0000792 chassisBridgeDevAddrProp, bridgeDeviceAddr);
Yong Liae4b0402018-11-02 11:12:14 +0800793 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500794 catch (const std::exception& e)
Yong Liae4b0402018-11-02 11:12:14 +0800795 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700796 lg2::error("Failed to set chassis capability properties: {ERR}", "ERR",
797 e);
anil kumar appana894d0222019-05-27 16:32:14 +0000798 return ipmi::responseUnspecifiedError();
Yong Liae4b0402018-11-02 11:12:14 +0800799 }
anil kumar appana894d0222019-05-27 16:32:14 +0000800 return ipmi::responseSuccess();
Nan Li8d15fb42016-08-16 22:29:40 +0800801}
802
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530803//------------------------------------------
804// Calls into Host State Manager Dbus object
805//------------------------------------------
Jason M. Bills664e1c32020-01-30 16:02:39 -0800806int initiateHostStateTransition(ipmi::Context::ptr& ctx,
807 State::Host::Transition transition)
vishwa36993272015-11-20 12:43:49 -0600808{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500809 // OpenBMC Host State Manager dbus framework
Jason M. Bills664e1c32020-01-30 16:02:39 -0800810 constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0";
811 constexpr auto hostStateIntf = "xyz.openbmc_project.State.Host";
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530812
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500813 // Convert to string equivalent of the passed in transition enum.
Willy Tu523e2d12023-09-05 11:36:48 -0700814 auto request =
815 sdbusplus::common::xyz::openbmc_project::state::convertForMessage(
816 transition);
Vishwanatha Subbannab12b0c02017-03-07 18:17:19 +0530817
Jason M. Bills664e1c32020-01-30 16:02:39 -0800818 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400819 boost::system::error_code ec =
820 ipmi::getService(ctx, hostStateIntf, hostStatePath, service);
Jason M. Bills664e1c32020-01-30 16:02:39 -0800821
822 if (!ec)
823 {
824 ec = ipmi::setDbusProperty(ctx, service, hostStatePath, hostStateIntf,
825 "RequestedHostTransition", request);
826 }
827 if (ec)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500828 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700829 lg2::error(
830 "Failed to initiate transition for request {REQUEST}: {EXCEPTION}",
831 "REQUEST", request, "EXCEPTION", ec.message());
Jason M. Bills664e1c32020-01-30 16:02:39 -0800832 return -1;
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500833 }
Vernon Mauery38108d92024-03-11 12:53:55 -0700834 lg2::info(
835 "Transition request {REQUEST} initiated successfully by user {USERID}",
836 "REQUEST", request, "USERID", ctx->userId);
Jason M. Bills664e1c32020-01-30 16:02:39 -0800837 return 0;
vishwa36993272015-11-20 12:43:49 -0600838}
839
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800840//------------------------------------------
Jason M. Billsad588bf2020-01-30 16:18:33 -0800841// Calls into Chassis State Manager Dbus object
842//------------------------------------------
843int initiateChassisStateTransition(ipmi::Context::ptr& ctx,
844 State::Chassis::Transition transition)
845{
846 // OpenBMC Chassis State Manager dbus framework
847 constexpr auto chassisStatePath = "/xyz/openbmc_project/state/chassis0";
848 constexpr auto chassisStateIntf = "xyz.openbmc_project.State.Chassis";
849
850 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400851 boost::system::error_code ec =
852 ipmi::getService(ctx, chassisStateIntf, chassisStatePath, service);
Jason M. Billsad588bf2020-01-30 16:18:33 -0800853
854 // Convert to string equivalent of the passed in transition enum.
Willy Tu523e2d12023-09-05 11:36:48 -0700855 auto request =
856 sdbusplus::common::xyz::openbmc_project::state::convertForMessage(
857 transition);
Jason M. Billsad588bf2020-01-30 16:18:33 -0800858
859 if (!ec)
860 {
861 ec = ipmi::setDbusProperty(ctx, service, chassisStatePath,
862 chassisStateIntf, "RequestedPowerTransition",
863 request);
864 }
865 if (ec)
866 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700867 lg2::error("Failed to initiate transition {REQUEST}: {EXCEPTION}",
868 "REQUEST", request, "EXCEPTION", ec.message());
869
Jason M. Billsad588bf2020-01-30 16:18:33 -0800870 return -1;
871 }
872
873 return 0;
874}
875
876//------------------------------------------
Zev Weiss337a0972024-03-08 08:31:15 +0000877// Trigger an NMI on the host via dbus
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800878//------------------------------------------
Zev Weiss337a0972024-03-08 08:31:15 +0000879static int doNmi(ipmi::Context::ptr& ctx)
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800880{
Zev Weiss337a0972024-03-08 08:31:15 +0000881 constexpr const char* nmiIntfName = "xyz.openbmc_project.Control.Host.NMI";
882 ipmi::DbusObjectInfo nmiObj{};
883 boost::system::error_code ec;
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800884
Zev Weiss337a0972024-03-08 08:31:15 +0000885 ec = ipmi::getDbusObject(ctx, nmiIntfName, nmiObj);
Vernon Mauery9bb4a382021-11-05 12:46:19 -0700886 if (ec)
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800887 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700888 lg2::error("Failed to find NMI service: {ERROR}", "ERROR",
889 ec.message());
Zev Weiss337a0972024-03-08 08:31:15 +0000890 return -1;
891 }
892
893 ctx->bus->yield_method_call<void>(ctx->yield, ec, nmiObj.second,
894 nmiObj.first, nmiIntfName, "NMI");
895 if (ec)
896 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700897 lg2::error("NMI call failed: {ERROR}", "ERROR", ec.message());
Zev Weiss337a0972024-03-08 08:31:15 +0000898 elog<InternalFailure>();
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +0800899 return -1;
900 }
901
902 return 0;
903}
904
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500905namespace power_policy
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -0500906{
Nan Lifdd8ec52016-08-28 03:57:40 +0800907
Willy Tu523e2d12023-09-05 11:36:48 -0700908using namespace sdbusplus::server::xyz::openbmc_project::control::power;
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500909using IpmiValue = uint8_t;
910using DbusValue = RestorePolicy::Policy;
Nan Lifdd8ec52016-08-28 03:57:40 +0800911
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700912const std::map<DbusValue, IpmiValue> dbusToIpmi = {
Deepak Kodihalli18b70d12017-07-21 13:36:33 -0500913 {RestorePolicy::Policy::AlwaysOff, 0x00},
914 {RestorePolicy::Policy::Restore, 0x01},
George Liue561d3e2024-02-04 15:19:30 +0800915 {RestorePolicy::Policy::AlwaysOn, 0x02},
916 {RestorePolicy::Policy::None, 0x03}};
Nan Lifdd8ec52016-08-28 03:57:40 +0800917
Yong Lic6713cf2018-09-12 12:35:13 +0800918static constexpr uint8_t noChange = 0x03;
919static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700920
921/* helper function for Get Chassis Status Command
922 */
923std::optional<uint2_t> getPowerRestorePolicy()
924{
925 uint2_t restorePolicy = 0;
926 using namespace chassis::internal;
927
James Feist225dec82019-11-26 16:25:06 -0800928 settings::Objects& objects = cache::getObjects();
929
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700930 try
931 {
932 const auto& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -0800933 objects.map.at(powerRestoreIntf).front();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700934 ipmi::Value result = ipmi::getDbusProperty(
935 *getSdBus(),
James Feist225dec82019-11-26 16:25:06 -0800936 objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700937 powerRestoreSetting.c_str(), powerRestoreIntf,
938 "PowerRestorePolicy");
939 auto powerRestore = RestorePolicy::convertPolicyFromString(
940 std::get<std::string>(result));
941 restorePolicy = dbusToIpmi.at(powerRestore);
942 }
943 catch (const std::exception& e)
944 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700945 lg2::error(
946 "Failed to fetch pgood property ({PATH}/{INTERFACE}): {ERROR}",
947 "PATH", objects.map.at(powerRestoreIntf).front(), "INTERFACE",
948 powerRestoreIntf, "ERROR", e);
James Feist225dec82019-11-26 16:25:06 -0800949 cache::objectsPtr.reset();
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700950 return std::nullopt;
951 }
952 return std::make_optional(restorePolicy);
953}
954
955/*
956 * getPowerStatus
957 * helper function for Get Chassis Status Command
958 * return - optional value for pgood (no value on error)
959 */
960std::optional<bool> getPowerStatus()
961{
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700962 bool powerGood = false;
963 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
964 try
965 {
Jason M. Bills3de424c2019-05-21 09:57:16 -0700966 constexpr const char* chassisStatePath =
967 "/xyz/openbmc_project/state/chassis0";
968 constexpr const char* chassisStateIntf =
969 "xyz.openbmc_project.State.Chassis";
Patrick Williams1318a5e2024-08-16 15:19:54 -0400970 auto service =
971 ipmi::getService(*busp, chassisStateIntf, chassisStatePath);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700972
Jason M. Bills3de424c2019-05-21 09:57:16 -0700973 ipmi::Value powerState =
974 ipmi::getDbusProperty(*busp, service, chassisStatePath,
975 chassisStateIntf, "CurrentPowerState");
976 powerGood = std::get<std::string>(powerState) ==
977 "xyz.openbmc_project.State.Chassis.PowerState.On";
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700978 }
979 catch (const std::exception& e)
980 {
981 try
982 {
983 // FIXME: some legacy modules use the older path; try that next
984 constexpr const char* legacyPwrCtrlObj =
985 "/org/openbmc/control/power0";
986 constexpr const char* legacyPwrCtrlIntf =
987 "org.openbmc.control.Power";
Patrick Williams1318a5e2024-08-16 15:19:54 -0400988 auto service =
989 ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700990
991 ipmi::Value variant = ipmi::getDbusProperty(
992 *busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
993 powerGood = static_cast<bool>(std::get<int>(variant));
994 }
995 catch (const std::exception& e)
996 {
Vernon Mauery38108d92024-03-11 12:53:55 -0700997 lg2::error("Failed to fetch pgood property: {ERROR}", "ERROR", e);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -0700998 return std::nullopt;
999 }
1000 }
1001 return std::make_optional(powerGood);
1002}
1003
Yong Li70ce7352019-05-16 21:15:27 +08001004/*
1005 * getACFailStatus
1006 * helper function for Get Chassis Status Command
1007 * return - bool value for ACFail (false on error)
1008 */
1009bool getACFailStatus()
1010{
1011 constexpr const char* powerControlObj =
1012 "/xyz/openbmc_project/Chassis/Control/Power0";
1013 constexpr const char* powerControlIntf =
1014 "xyz.openbmc_project.Chassis.Control.Power";
1015 bool acFail = false;
1016 std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
1017 try
1018 {
Patrick Williams1318a5e2024-08-16 15:19:54 -04001019 auto service =
1020 ipmi::getService(*bus, powerControlIntf, powerControlObj);
Yong Li70ce7352019-05-16 21:15:27 +08001021
1022 ipmi::Value variant = ipmi::getDbusProperty(
1023 *bus, service, powerControlObj, powerControlIntf, "PFail");
1024 acFail = std::get<bool>(variant);
1025 }
1026 catch (const std::exception& e)
1027 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001028 lg2::error(
1029 "Failed to fetch PFail property ({PATH}/{INTERFAC}): {ERROR}",
1030 "PATH", powerControlObj, "INTERFACE", powerControlIntf, "ERROR", e);
Yong Li70ce7352019-05-16 21:15:27 +08001031 }
1032 return acFail;
1033}
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001034} // namespace power_policy
Nan Lifdd8ec52016-08-28 03:57:40 +08001035
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001036static std::optional<bool> getButtonEnabled(const std::string& buttonPath,
1037 const std::string& buttonIntf)
1038{
1039 std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
1040 bool buttonDisabled = false;
1041 try
1042 {
1043 auto service = ipmi::getService(*busp, buttonIntf, buttonPath);
1044 ipmi::Value enabled = ipmi::getDbusProperty(*busp, service, buttonPath,
1045 buttonIntf, "Enabled");
1046 buttonDisabled = !std::get<bool>(enabled);
1047 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001048 catch (const sdbusplus::exception_t& e)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001049 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001050 lg2::error("Fail to get button Enabled property ({PATH}): {ERROR}",
1051 "PATH", buttonPath, "ERROR", e);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001052 return std::nullopt;
1053 }
1054 return std::make_optional(buttonDisabled);
1055}
1056
Kuiying Wang21addc52019-01-04 10:50:21 +08001057static bool setButtonEnabled(ipmi::Context::ptr& ctx,
1058 const std::string& buttonPath,
1059 const std::string& buttonIntf, bool enable)
1060{
1061 std::string service;
1062 boost::system::error_code ec;
1063 ec = ipmi::getService(ctx, buttonIntf, buttonPath, service);
1064 if (!ec)
1065 {
1066 ec = ipmi::setDbusProperty(ctx, service, buttonPath, buttonIntf,
1067 "Enabled", enable);
1068 }
1069 if (ec)
1070 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001071 lg2::error(
1072 "Fail to set button Enabled property ({SERVICE}:{PATH}): {ERROR}",
1073 "SERVICE", service, "PATH", buttonPath, "ERROR", ec.message());
Kuiying Wang21addc52019-01-04 10:50:21 +08001074 return false;
1075 }
1076 return true;
1077}
1078
Chau Ly9e666cd2023-03-06 08:46:14 +00001079static std::optional<bool> getChassisIntrusionStatus(ipmi::Context::ptr& ctx)
1080{
1081 constexpr const char* chassisIntrusionPath =
1082 "/xyz/openbmc_project/Chassis/Intrusion";
1083 constexpr const char* chassisIntrusionInf =
1084 "xyz.openbmc_project.Chassis.Intrusion";
1085
1086 std::string service;
1087 boost::system::error_code ec = ipmi::getService(
1088 ctx, chassisIntrusionInf, chassisIntrusionPath, service);
1089 if (!ec)
1090 {
1091 std::string chassisIntrusionStr;
1092 ec = ipmi::getDbusProperty<std::string>(
1093 ctx, service, chassisIntrusionPath, chassisIntrusionInf, "Status",
1094 chassisIntrusionStr);
1095 if (!ec)
1096 {
Patrick Williams1318a5e2024-08-16 15:19:54 -04001097 bool ret =
1098 (chassisIntrusionStr == "HardwareIntrusion") ? true : false;
Chau Ly9e666cd2023-03-06 08:46:14 +00001099 return std::make_optional(ret);
1100 }
1101 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001102 lg2::error("Fail to get Chassis Intrusion Status property "
1103 "({PATH}/{INTERFACE}): {ERROR}",
1104 "PATH", chassisIntrusionPath, "INTERFACE", chassisIntrusionInf,
1105 "ERROR", ec.message());
Chau Ly9e666cd2023-03-06 08:46:14 +00001106 return std::nullopt;
1107}
1108
Nan Lifdd8ec52016-08-28 03:57:40 +08001109//----------------------------------------------------------------------
1110// Get Chassis Status commands
1111//----------------------------------------------------------------------
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001112ipmi::RspType<bool, // Power is on
1113 bool, // Power overload
1114 bool, // Interlock
1115 bool, // power fault
1116 bool, // power control fault
1117 uint2_t, // power restore policy
1118 bool, // reserved
1119
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001120 bool, // AC failed
1121 bool, // last power down caused by a Power overload
1122 bool, // last power down caused by a power interlock
1123 bool, // last power down caused by power fault
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001124 bool, // last ‘Power is on’ state was entered via IPMI command
1125 uint3_t, // reserved
1126
1127 bool, // Chassis intrusion active
1128 bool, // Front Panel Lockout active
1129 bool, // Drive Fault
1130 bool, // Cooling/fan fault detected
1131 uint2_t, // Chassis Identify State
1132 bool, // Chassis Identify command and state info supported
1133 bool, // reserved
1134
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001135 bool, // Power off button disabled
1136 bool, // Reset button disabled
1137 bool, // Diagnostic Interrupt button disabled
1138 bool, // Standby (sleep) button disabled
1139 bool, // Power off button disable allowed
1140 bool, // Reset button disable allowed
1141 bool, // Diagnostic Interrupt button disable allowed
1142 bool // Standby (sleep) button disable allowed
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001143 >
Chau Ly9e666cd2023-03-06 08:46:14 +00001144 ipmiGetChassisStatus(ipmi::Context::ptr& ctx)
Nan Lifdd8ec52016-08-28 03:57:40 +08001145{
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001146 using namespace chassis::internal;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001147 std::optional<uint2_t> restorePolicy =
1148 power_policy::getPowerRestorePolicy();
1149 std::optional<bool> powerGood = power_policy::getPowerStatus();
1150 if (!restorePolicy || !powerGood)
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001151 {
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001152 return ipmi::responseUnspecifiedError();
Deepak Kodihalli18b70d12017-07-21 13:36:33 -05001153 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001154
1155 // Front Panel Button Capabilities and disable/enable status(Optional)
Patrick Williams1318a5e2024-08-16 15:19:54 -04001156 std::optional<bool> powerButtonReading =
1157 getButtonEnabled(powerButtonPath, powerButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001158 // allow disable if the interface is present
1159 bool powerButtonDisableAllow = static_cast<bool>(powerButtonReading);
1160 // default return the button is enabled (not disabled)
1161 bool powerButtonDisabled = false;
1162 if (powerButtonDisableAllow)
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001163 {
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001164 // return the real value of the button status, if present
1165 powerButtonDisabled = *powerButtonReading;
1166 }
1167
Patrick Williams1318a5e2024-08-16 15:19:54 -04001168 std::optional<bool> resetButtonReading =
1169 getButtonEnabled(resetButtonPath, resetButtonIntf);
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001170 // allow disable if the interface is present
1171 bool resetButtonDisableAllow = static_cast<bool>(resetButtonReading);
1172 // default return the button is enabled (not disabled)
1173 bool resetButtonDisabled = false;
1174 if (resetButtonDisableAllow)
1175 {
1176 // return the real value of the button status, if present
1177 resetButtonDisabled = *resetButtonReading;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001178 }
Nan Lifdd8ec52016-08-28 03:57:40 +08001179
Yong Li70ce7352019-05-16 21:15:27 +08001180 bool powerDownAcFailed = power_policy::getACFailStatus();
1181
Chau Ly9e666cd2023-03-06 08:46:14 +00001182 bool chassisIntrusionActive = false;
1183 std::optional<bool> chassisIntrusionStatus = getChassisIntrusionStatus(ctx);
1184 if (chassisIntrusionStatus)
1185 {
1186 chassisIntrusionActive = chassisIntrusionStatus.value();
1187 }
1188
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001189 // This response has a lot of hard-coded, unsupported fields
1190 // They are set to false or 0
1191 constexpr bool powerOverload = false;
1192 constexpr bool chassisInterlock = false;
1193 constexpr bool powerFault = false;
1194 constexpr bool powerControlFault = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001195 constexpr bool powerDownOverload = false;
1196 constexpr bool powerDownInterlock = false;
1197 constexpr bool powerDownPowerFault = false;
1198 constexpr bool powerStatusIPMI = false;
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001199 constexpr bool frontPanelLockoutActive = false;
1200 constexpr bool driveFault = false;
1201 constexpr bool coolingFanFault = false;
1202 // chassisIdentifySupport set because this command is implemented
1203 constexpr bool chassisIdentifySupport = true;
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001204 uint2_t chassisIdentifyState = types::enum_cast<uint2_t>(chassisIDState);
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001205 constexpr bool diagButtonDisabled = false;
1206 constexpr bool sleepButtonDisabled = false;
1207 constexpr bool diagButtonDisableAllow = false;
1208 constexpr bool sleepButtonDisableAllow = false;
1209
1210 return ipmi::responseSuccess(
1211 *powerGood, powerOverload, chassisInterlock, powerFault,
1212 powerControlFault, *restorePolicy,
1213 false, // reserved
1214
1215 powerDownAcFailed, powerDownOverload, powerDownInterlock,
1216 powerDownPowerFault, powerStatusIPMI,
1217 uint3_t(0), // reserved
1218
1219 chassisIntrusionActive, frontPanelLockoutActive, driveFault,
1220 coolingFanFault, chassisIdentifyState, chassisIdentifySupport,
1221 false, // reserved
1222
Vernon Mauery6d5b2f72019-05-16 14:48:47 -07001223 powerButtonDisabled, resetButtonDisabled, diagButtonDisabled,
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07001224 sleepButtonDisabled, powerButtonDisableAllow, resetButtonDisableAllow,
1225 diagButtonDisableAllow, sleepButtonDisableAllow);
Nan Lifdd8ec52016-08-28 03:57:40 +08001226}
Chris Austen7888c4d2015-12-03 15:26:20 -06001227
Vijay Khemka074f64d2020-03-03 15:08:43 -08001228enum class IpmiRestartCause
1229{
1230 Unknown = 0x0,
1231 RemoteCommand = 0x1,
1232 ResetButton = 0x2,
1233 PowerButton = 0x3,
1234 WatchdogTimer = 0x4,
1235 PowerPolicyAlwaysOn = 0x6,
1236 PowerPolicyPreviousState = 0x7,
1237 SoftReset = 0xa,
1238};
1239
1240static IpmiRestartCause
1241 restartCauseToIpmiRestartCause(State::Host::RestartCause cause)
1242{
1243 switch (cause)
1244 {
1245 case State::Host::RestartCause::Unknown:
1246 {
1247 return IpmiRestartCause::Unknown;
1248 }
1249 case State::Host::RestartCause::RemoteCommand:
1250 {
1251 return IpmiRestartCause::RemoteCommand;
1252 }
1253 case State::Host::RestartCause::ResetButton:
1254 {
1255 return IpmiRestartCause::ResetButton;
1256 }
1257 case State::Host::RestartCause::PowerButton:
1258 {
1259 return IpmiRestartCause::PowerButton;
1260 }
1261 case State::Host::RestartCause::WatchdogTimer:
1262 {
1263 return IpmiRestartCause::WatchdogTimer;
1264 }
1265 case State::Host::RestartCause::PowerPolicyAlwaysOn:
1266 {
1267 return IpmiRestartCause::PowerPolicyAlwaysOn;
1268 }
1269 case State::Host::RestartCause::PowerPolicyPreviousState:
1270 {
1271 return IpmiRestartCause::PowerPolicyPreviousState;
1272 }
1273 case State::Host::RestartCause::SoftReset:
1274 {
1275 return IpmiRestartCause::SoftReset;
1276 }
1277 default:
1278 {
1279 return IpmiRestartCause::Unknown;
1280 }
1281 }
1282}
1283
1284/*
1285 * getRestartCause
1286 * helper function for Get Host restart cause Command
1287 * return - optional value for RestartCause (no value on error)
1288 */
1289static std::optional<uint4_t> getRestartCause(ipmi::Context::ptr ctx)
1290{
okashanyced0ddf2023-05-18 18:35:09 +00001291 constexpr const char* restartCausePath = "/xyz/openbmc_project/state/host0";
1292 constexpr const char* restartCauseIntf = "xyz.openbmc_project.State.Host";
Vijay Khemka074f64d2020-03-03 15:08:43 -08001293
1294 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001295 boost::system::error_code ec =
1296 ipmi::getService(ctx, restartCauseIntf, restartCausePath, service);
Vijay Khemka074f64d2020-03-03 15:08:43 -08001297 if (!ec)
1298 {
1299 std::string restartCauseStr;
1300 ec = ipmi::getDbusProperty<std::string>(
1301 ctx, service, restartCausePath, restartCauseIntf, "RestartCause",
1302 restartCauseStr);
1303 if (!ec)
1304 {
1305 auto cause =
1306 State::Host::convertRestartCauseFromString(restartCauseStr);
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001307 return types::enum_cast<uint4_t>(
1308 restartCauseToIpmiRestartCause(cause));
Vijay Khemka074f64d2020-03-03 15:08:43 -08001309 }
1310 }
1311
Vernon Mauery38108d92024-03-11 12:53:55 -07001312 lg2::error(
1313 "Failed to fetch RestartCause property ({PATH}/{INTERFACE}): {ERROR}",
1314 "ERROR", ec.message(), "PATH", restartCausePath, "INTERFACE",
1315 restartCauseIntf);
Vijay Khemka074f64d2020-03-03 15:08:43 -08001316 return std::nullopt;
1317}
1318
1319ipmi::RspType<uint4_t, // Restart Cause
1320 uint4_t, // reserved
1321 uint8_t // channel number (not supported)
1322 >
1323 ipmiGetSystemRestartCause(ipmi::Context::ptr ctx)
1324{
1325 std::optional<uint4_t> cause = getRestartCause(ctx);
1326 if (!cause)
1327 {
1328 return ipmi::responseUnspecifiedError();
1329 }
1330
Vernon Mauery916d4232021-03-29 13:42:16 -07001331 constexpr uint4_t reserved = 0;
1332 auto channel = static_cast<uint8_t>(ctx->channel);
1333 return ipmi::responseSuccess(cause.value(), reserved, channel);
Vijay Khemka074f64d2020-03-03 15:08:43 -08001334}
anil kumar appanadafff5f2019-04-27 18:06:00 +00001335/** @brief Implementation of chassis control command
1336 *
1337 * @param - chassisControl command byte
1338 *
1339 * @return Success or InvalidFieldRequest.
1340 */
Jason M. Bills664e1c32020-01-30 16:02:39 -08001341ipmi::RspType<> ipmiChassisControl(ipmi::Context::ptr& ctx,
1342 uint8_t chassisControl)
vishwa36993272015-11-20 12:43:49 -06001343{
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001344 int rc = 0;
anil kumar appanadafff5f2019-04-27 18:06:00 +00001345 switch (chassisControl)
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001346 {
1347 case CMD_POWER_ON:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001348 rc = initiateHostStateTransition(ctx, State::Host::Transition::On);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001349 break;
1350 case CMD_POWER_OFF:
Jason M. Billsc2c3a402020-01-30 16:22:24 -08001351 rc = initiateChassisStateTransition(
1352 ctx, State::Chassis::Transition::Off);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001353 break;
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001354 case CMD_HARD_RESET:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001355 rc = initiateHostStateTransition(
1356 ctx, State::Host::Transition::ForceWarmReboot);
Chanh Nguyend0a7fcd2021-07-16 22:59:28 +07001357 break;
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001358 case CMD_POWER_CYCLE:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001359 rc = initiateHostStateTransition(ctx,
1360 State::Host::Transition::Reboot);
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001361 break;
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301362 case CMD_SOFT_OFF_VIA_OVER_TEMP:
Jason M. Bills664e1c32020-01-30 16:02:39 -08001363 rc = initiateHostStateTransition(ctx, State::Host::Transition::Off);
Vishwanatha Subbanna8b26d352017-08-04 18:35:18 +05301364 break;
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001365 case CMD_PULSE_DIAGNOSTIC_INTR:
Zev Weiss337a0972024-03-08 08:31:15 +00001366 rc = doNmi(ctx);
Kuiying Wang6b0ceaa2019-11-05 15:13:40 +08001367 break;
1368
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001369 default:
1370 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001371 lg2::error("Invalid Chassis Control command: {CMD}", "CMD",
1372 lg2::hex, chassisControl);
anil kumar appanadafff5f2019-04-27 18:06:00 +00001373 return ipmi::responseInvalidFieldRequest();
Andrew Geisslerfca6a4f2017-05-30 10:55:39 -05001374 }
1375 }
vishwa36993272015-11-20 12:43:49 -06001376
anil kumar appanadafff5f2019-04-27 18:06:00 +00001377 return ((rc < 0) ? ipmi::responseUnspecifiedError()
1378 : ipmi::responseSuccess());
vishwa36993272015-11-20 12:43:49 -06001379}
1380
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001381/** @brief Return D-Bus connection string to enclosure identify LED object
1382 *
1383 * @param[in, out] connection - connection to D-Bus object
1384 * @return a IPMI return code
1385 */
1386std::string getEnclosureIdentifyConnection()
Tom Joseph5110c122018-03-23 17:55:40 +05301387{
Tom Joseph5110c122018-03-23 17:55:40 +05301388 // lookup enclosure_identify group owner(s) in mapper
George Liu3e3cc352023-07-26 15:59:31 +08001389 try
Tom Joseph5110c122018-03-23 17:55:40 +05301390 {
George Liu3e3cc352023-07-26 15:59:31 +08001391 return ipmi::getService(*getSdBus(), "xyz.openbmc_project.Led.Group",
1392 identify_led_object_name);
1393 }
1394 catch (const std::exception& e)
1395 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001396 lg2::error("Chassis Identify: Error communicating to mapper: {ERROR}",
1397 "ERROR", e);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001398 elog<InternalFailure>();
Tom Joseph5110c122018-03-23 17:55:40 +05301399 }
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001400}
Tom Joseph5110c122018-03-23 17:55:40 +05301401
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001402/** @brief Turn On/Off enclosure identify LED
1403 *
1404 * @param[in] flag - true to turn on LED, false to turn off
1405 * @return a IPMI return code
1406 */
1407void enclosureIdentifyLed(bool flag)
1408{
1409 using namespace chassis::internal;
George Liu3e3cc352023-07-26 15:59:31 +08001410 try
1411 {
1412 std::string connection = getEnclosureIdentifyConnection();
1413
1414 auto msg = std::string("enclosureIdentifyLed(") +
1415 boost::lexical_cast<std::string>(flag) + ")";
Vernon Mauery38108d92024-03-11 12:53:55 -07001416 lg2::debug(msg.c_str());
George Liu3e3cc352023-07-26 15:59:31 +08001417
1418 ipmi::setDbusProperty(*getSdBus(), connection, identify_led_object_name,
1419 "xyz.openbmc_project.Led.Group", "Asserted",
1420 flag);
1421 }
1422 catch (const std::exception& e)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001423 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001424 lg2::error("Chassis Identify: Error Setting State {LED_STATE}: {ERROR}",
1425 "LED_STATE", flag, "ERROR", e);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001426 elog<InternalFailure>();
1427 }
1428}
1429
1430/** @brief Callback method to turn off LED
1431 */
1432void enclosureIdentifyLedOff()
1433{
1434 try
1435 {
Yong Lif4e38512019-05-21 14:46:55 +08001436 chassisIDState = ChassisIDState::off;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001437 enclosureIdentifyLed(false);
1438 }
1439 catch (const InternalFailure& e)
1440 {
1441 report<InternalFailure>();
1442 }
1443}
1444
1445/** @brief Create timer to turn on and off the enclosure LED
1446 */
1447void createIdentifyTimer()
1448{
1449 if (!identifyTimer)
1450 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001451 identifyTimer =
Patrick Williams95655222023-12-05 12:45:02 -06001452 std::make_unique<sdbusplus::Timer>(enclosureIdentifyLedOff);
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001453 }
1454}
1455
Vernon Mauery400cc782018-10-09 13:49:53 -07001456ipmi::RspType<> ipmiChassisIdentify(std::optional<uint8_t> interval,
1457 std::optional<uint8_t> force)
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001458{
Vernon Mauery400cc782018-10-09 13:49:53 -07001459 uint8_t identifyInterval = interval.value_or(DEFAULT_IDENTIFY_TIME_OUT);
1460 bool forceIdentify = force.value_or(0) & 0x01;
Tom Josephbed26992018-07-31 23:00:24 +05301461
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001462 if (identifyInterval || forceIdentify)
1463 {
Vernon Mauery400cc782018-10-09 13:49:53 -07001464 // stop the timer if already started;
1465 // for force identify we should not turn off LED
Vernon Mauery1181af72018-10-08 12:05:00 -07001466 identifyTimer->stop();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001467 try
Tom Joseph5110c122018-03-23 17:55:40 +05301468 {
Yong Lif4e38512019-05-21 14:46:55 +08001469 chassisIDState = ChassisIDState::temporaryOn;
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001470 enclosureIdentifyLed(true);
1471 }
1472 catch (const InternalFailure& e)
1473 {
1474 report<InternalFailure>();
Vernon Mauery400cc782018-10-09 13:49:53 -07001475 return ipmi::responseResponseError();
Tom Joseph5110c122018-03-23 17:55:40 +05301476 }
1477
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001478 if (forceIdentify)
Tom Joseph5110c122018-03-23 17:55:40 +05301479 {
Yong Lif4e38512019-05-21 14:46:55 +08001480 chassisIDState = ChassisIDState::indefiniteOn;
Vernon Mauery400cc782018-10-09 13:49:53 -07001481 return ipmi::responseSuccess();
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05001482 }
1483 // start the timer
1484 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -07001485 std::chrono::seconds(identifyInterval));
Vernon Mauery1181af72018-10-08 12:05:00 -07001486 identifyTimer->start(time);
Tom Joseph5110c122018-03-23 17:55:40 +05301487 }
Tom Josephbed26992018-07-31 23:00:24 +05301488 else if (!identifyInterval)
1489 {
Vernon Mauery1181af72018-10-08 12:05:00 -07001490 identifyTimer->stop();
Tom Josephbed26992018-07-31 23:00:24 +05301491 enclosureIdentifyLedOff();
1492 }
Vernon Mauery400cc782018-10-09 13:49:53 -07001493 return ipmi::responseSuccess();
Tom Joseph5110c122018-03-23 17:55:40 +05301494}
1495
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001496namespace boot_options
1497{
1498
Willy Tu523e2d12023-09-05 11:36:48 -07001499using namespace sdbusplus::server::xyz::openbmc_project::control::boot;
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001500using IpmiValue = uint8_t;
1501constexpr auto ipmiDefault = 0;
1502
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001503std::map<IpmiValue, Type::Types> typeIpmiToDbus = {{0x00, Type::Types::Legacy},
1504 {0x01, Type::Types::EFI}};
1505
Patrick Venture0b02be92018-08-31 11:55:55 -07001506std::map<IpmiValue, Source::Sources> sourceIpmiToDbus = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001507 {0x01, Source::Sources::Network},
1508 {0x02, Source::Sources::Disk},
1509 {0x05, Source::Sources::ExternalMedia},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001510 {0x0f, Source::Sources::RemovableMedia},
Patrick Venture0b02be92018-08-31 11:55:55 -07001511 {ipmiDefault, Source::Sources::Default}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001512
Patrick Venture0b02be92018-08-31 11:55:55 -07001513std::map<IpmiValue, Mode::Modes> modeIpmiToDbus = {
Yong Li5833cb62019-10-30 13:27:12 +08001514#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001515 {0x03, Mode::Modes::Safe},
Yong Li5833cb62019-10-30 13:27:12 +08001516#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001517 {0x06, Mode::Modes::Setup},
Patrick Venture0b02be92018-08-31 11:55:55 -07001518 {ipmiDefault, Mode::Modes::Regular}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001519
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001520std::map<Type::Types, IpmiValue> typeDbusToIpmi = {{Type::Types::Legacy, 0x00},
1521 {Type::Types::EFI, 0x01}};
1522
Patrick Venture0b02be92018-08-31 11:55:55 -07001523std::map<Source::Sources, IpmiValue> sourceDbusToIpmi = {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001524 {Source::Sources::Network, 0x01},
1525 {Source::Sources::Disk, 0x02},
1526 {Source::Sources::ExternalMedia, 0x05},
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08001527 {Source::Sources::RemovableMedia, 0x0f},
Patrick Venture0b02be92018-08-31 11:55:55 -07001528 {Source::Sources::Default, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001529
Patrick Venture0b02be92018-08-31 11:55:55 -07001530std::map<Mode::Modes, IpmiValue> modeDbusToIpmi = {
Yong Li5833cb62019-10-30 13:27:12 +08001531#ifdef ENABLE_BOOT_FLAG_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001532 {Mode::Modes::Safe, 0x03},
Yong Li5833cb62019-10-30 13:27:12 +08001533#endif // ENABLE_BOOT_SAFE_MODE_SUPPORT
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001534 {Mode::Modes::Setup, 0x06},
Patrick Venture0b02be92018-08-31 11:55:55 -07001535 {Mode::Modes::Regular, ipmiDefault}};
shgoupfd84fbbf2015-12-17 10:05:51 +08001536
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001537} // namespace boot_options
shgoupfd84fbbf2015-12-17 10:05:51 +08001538
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001539/** @brief Get the property value for boot source
1540 * @param[in] ctx - context pointer
1541 * @param[out] source - boot source value
1542 * @return On failure return IPMI error.
1543 */
1544static ipmi::Cc getBootSource(ipmi::Context::ptr& ctx, Source::Sources& source)
1545{
1546 using namespace chassis::internal;
1547 std::string result;
1548 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001549 boost::system::error_code ec =
1550 getService(ctx, bootSourceIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001551 if (!ec)
1552 {
1553 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath,
1554 bootSourceIntf, "BootSource", result);
1555 if (!ec)
1556 {
1557 source = Source::convertSourcesFromString(result);
1558 return ipmi::ccSuccess;
1559 }
1560 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001561 lg2::error("Error in BootSource Get: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001562 return ipmi::ccUnspecifiedError;
1563}
1564
Marri Devender Rao81719702018-05-07 00:53:48 -05001565/** @brief Set the property value for boot source
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001566 * @param[in] ctx - context pointer
Marri Devender Rao81719702018-05-07 00:53:48 -05001567 * @param[in] source - boot source value
1568 * @return On failure return IPMI error.
1569 */
Jayaprakash Mutyala6088e9f2021-07-14 07:40:29 +00001570static ipmi::Cc setBootSource(ipmi::Context::ptr& ctx,
1571 const Source::Sources& source)
Marri Devender Rao81719702018-05-07 00:53:48 -05001572{
1573 using namespace chassis::internal;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001574 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001575 boost::system::error_code ec =
1576 getService(ctx, bootSourceIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001577 if (!ec)
Marri Devender Rao81719702018-05-07 00:53:48 -05001578 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001579 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath,
1580 bootSourceIntf, "BootSource",
1581 convertForMessage(source));
1582 if (!ec)
1583 {
1584 return ipmi::ccSuccess;
1585 }
Marri Devender Rao81719702018-05-07 00:53:48 -05001586 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001587 lg2::error("Error in BootSource Set: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001588 return ipmi::ccUnspecifiedError;
1589}
1590
1591/** @brief Get the property value for boot mode
1592 * @param[in] ctx - context pointer
1593 * @param[out] mode - boot mode value
1594 * @return On failure return IPMI error.
1595 */
1596static ipmi::Cc getBootMode(ipmi::Context::ptr& ctx, Mode::Modes& mode)
1597{
1598 using namespace chassis::internal;
1599 std::string result;
1600 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001601 boost::system::error_code ec =
1602 getService(ctx, bootModeIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001603 if (!ec)
1604 {
1605 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath, bootModeIntf,
1606 "BootMode", result);
1607 if (!ec)
1608 {
1609 mode = Mode::convertModesFromString(result);
1610 return ipmi::ccSuccess;
1611 }
1612 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001613 lg2::error("Error in BootMode Get: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001614 return ipmi::ccUnspecifiedError;
Marri Devender Rao81719702018-05-07 00:53:48 -05001615}
1616
Patrick Venture0b02be92018-08-31 11:55:55 -07001617/** @brief Set the property value for boot mode
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001618 * @param[in] ctx - context pointer
Marri Devender Rao81719702018-05-07 00:53:48 -05001619 * @param[in] mode - boot mode value
1620 * @return On failure return IPMI error.
1621 */
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001622static ipmi::Cc setBootMode(ipmi::Context::ptr& ctx, const Mode::Modes& mode)
Marri Devender Rao81719702018-05-07 00:53:48 -05001623{
1624 using namespace chassis::internal;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001625 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001626 boost::system::error_code ec =
1627 getService(ctx, bootModeIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001628 if (!ec)
Marri Devender Rao81719702018-05-07 00:53:48 -05001629 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001630 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath, bootModeIntf,
1631 "BootMode", convertForMessage(mode));
1632 if (!ec)
1633 {
1634 return ipmi::ccSuccess;
1635 }
Marri Devender Rao81719702018-05-07 00:53:48 -05001636 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001637 lg2::error("Error in BootMode Set: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001638 return ipmi::ccUnspecifiedError;
1639}
1640
1641/** @brief Get the property value for boot type
1642 * @param[in] ctx - context pointer
1643 * @param[out] type - boot type value
1644 * @return On failure return IPMI error.
1645 */
1646static ipmi::Cc getBootType(ipmi::Context::ptr& ctx, Type::Types& type)
1647{
1648 using namespace chassis::internal;
1649 std::string result;
1650 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001651 boost::system::error_code ec =
1652 getService(ctx, bootTypeIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001653
1654 // Don't throw error if BootType interface is not present.
1655 // This interface is not relevant for some Host architectures
1656 // (for example POWER). In this case we don't won't IPMI to
1657 // return an error, but simply return bootType as EFI.
1658 type = Type::Types::EFI;
1659 if (!ec)
1660 {
1661 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath, bootTypeIntf,
1662 "BootType", result);
1663 if (ec)
1664 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001665 lg2::error("Error in BootType Get: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001666 return ipmi::ccUnspecifiedError;
1667 }
1668 type = Type::convertTypesFromString(result);
1669 }
1670
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001671 return ipmi::ccSuccess;
Marri Devender Rao81719702018-05-07 00:53:48 -05001672}
1673
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001674/** @brief Set the property value for boot type
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001675 * @param[in] ctx - context pointer
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001676 * @param[in] type - boot type value
1677 * @return On failure return IPMI error.
1678 */
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001679static ipmi::Cc setBootType(ipmi::Context::ptr& ctx, const Type::Types& type)
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001680{
1681 using namespace chassis::internal;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001682 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001683 boost::system::error_code ec =
1684 getService(ctx, bootTypeIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001685 if (!ec)
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001686 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001687 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath, bootTypeIntf,
1688 "BootType", convertForMessage(type));
1689 if (ec)
1690 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001691 lg2::error("Error in BootType Set: {ERROR}", "ERROR", ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001692 return ipmi::ccUnspecifiedError;
1693 }
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001694 }
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001695 // Don't throw error if BootType interface is not present.
1696 // This interface is not relevant for some Host architectures
1697 // (for example POWER). In this case we don't won't IPMI to
1698 // return an error, but want to just skip this function.
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001699 return ipmi::ccSuccess;
1700}
1701
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001702/** @brief Get the property value for boot override enable
1703 * @param[in] ctx - context pointer
1704 * @param[out] enable - boot override enable
1705 * @return On failure return IPMI error.
1706 */
1707static ipmi::Cc getBootEnable(ipmi::Context::ptr& ctx, bool& enable)
1708{
1709 using namespace chassis::internal;
1710 std::string result;
1711 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001712 boost::system::error_code ec =
1713 getService(ctx, bootEnableIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001714 if (!ec)
1715 {
1716 ec = ipmi::getDbusProperty(ctx, service, bootSettingsPath,
1717 bootEnableIntf, "Enabled", enable);
1718 if (!ec)
1719 {
1720 return ipmi::ccSuccess;
1721 }
1722 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001723 lg2::error("Error in Boot Override Enable Get: {ERROR}", "ERROR",
1724 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001725 return ipmi::ccUnspecifiedError;
1726}
1727
1728/** @brief Set the property value for boot override enable
1729 * @param[in] ctx - context pointer
1730 * @param[in] enable - boot override enable
1731 * @return On failure return IPMI error.
1732 */
1733static ipmi::Cc setBootEnable(ipmi::Context::ptr& ctx, const bool& enable)
1734{
1735 using namespace chassis::internal;
1736 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001737 boost::system::error_code ec =
1738 getService(ctx, bootEnableIntf, bootSettingsPath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001739 if (!ec)
1740 {
1741 ec = ipmi::setDbusProperty(ctx, service, bootSettingsPath,
1742 bootEnableIntf, "Enabled", enable);
1743 if (!ec)
1744 {
1745 return ipmi::ccSuccess;
1746 }
1747 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001748 lg2::error("Error in Boot Source Override Enable Set: {ERROR}", "ERROR",
1749 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001750 return ipmi::ccUnspecifiedError;
1751}
1752
1753/** @brief Get the property value for boot override one-time
1754 * @param[in] ctx - context pointer
1755 * @param[out] onetime - boot override one-time
1756 * @return On failure return IPMI error.
1757 */
1758static ipmi::Cc getBootOneTime(ipmi::Context::ptr& ctx, bool& onetime)
1759{
1760 using namespace chassis::internal;
1761 std::string result;
1762 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001763 boost::system::error_code ec =
1764 getService(ctx, bootOneTimeIntf, bootSettingsOneTimePath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001765 if (!ec)
1766 {
1767 ec = ipmi::getDbusProperty(ctx, service, bootSettingsOneTimePath,
1768 bootOneTimeIntf, "Enabled", onetime);
1769 if (!ec)
1770 {
1771 return ipmi::ccSuccess;
1772 }
1773 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001774 lg2::error("Error in Boot Override OneTime Get: {ERROR}", "ERROR",
1775 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001776 return ipmi::ccUnspecifiedError;
1777}
1778
1779/** @brief Set the property value for boot override one-time
1780 * @param[in] ctx - context pointer
1781 * @param[in] onetime - boot override one-time
1782 * @return On failure return IPMI error.
1783 */
1784static ipmi::Cc setBootOneTime(ipmi::Context::ptr& ctx, const bool& onetime)
1785{
1786 using namespace chassis::internal;
1787 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -04001788 boost::system::error_code ec =
1789 getService(ctx, bootOneTimeIntf, bootSettingsOneTimePath, service);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001790 if (!ec)
1791 {
1792 ec = ipmi::setDbusProperty(ctx, service, bootSettingsOneTimePath,
1793 bootOneTimeIntf, "Enabled", onetime);
1794 if (!ec)
1795 {
1796 return ipmi::ccSuccess;
1797 }
1798 }
Vernon Mauery38108d92024-03-11 12:53:55 -07001799 lg2::error("Error in Boot Source Override OneTime Set: {ERROR}", "ERROR",
1800 ec.message());
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001801 return ipmi::ccUnspecifiedError;
1802}
1803
huangheab369282020-10-10 14:40:00 +08001804static constexpr uint8_t setComplete = 0x0;
1805static constexpr uint8_t setInProgress = 0x1;
1806static uint8_t transferStatus = setComplete;
Chen Yugang86ac4992021-06-25 08:14:52 +08001807static uint8_t bootFlagValidBitClr = 0;
Hieu Huynh38ffafc2021-05-14 08:40:14 +00001808static uint5_t bootInitiatorAckData = 0x0;
Hieu Huynh1982a3f2021-09-21 03:06:45 +00001809static bool cmosClear = false;
huangheab369282020-10-10 14:40:00 +08001810
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001811/** @brief implements the Get Chassis system boot option
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03001812 * @param ctx - context pointer
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001813 * @param bootOptionParameter - boot option parameter selector
1814 * @param reserved1 - reserved bit
1815 * @param setSelector - selects a particular block or set of parameters
1816 * under the given parameter selector
1817 * write as 00h if parameter doesn't use a setSelector
1818 * @param blockSelector- selects a particular block within a set of
1819 * parameters write as 00h if parameter doesn't use a
1820 * blockSelector
1821 *
1822 * @return IPMI completion code plus response data
1823 * @return Payload contains below parameters:
1824 * version - parameter version
1825 * bootOptionParameter - boot option parameter selector
Patrick Venture8b1c3032020-09-15 09:43:03 -07001826 * parmIndicator - parameter valid/invalid indicator
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001827 * data - configuration parameter data
1828 */
Patrick Williams1318a5e2024-08-16 15:19:54 -04001829ipmi::RspType<ipmi::message::Payload> ipmiChassisGetSysBootOptions(
1830 ipmi::Context::ptr ctx, uint7_t bootOptionParameter, bool reserved1,
1831 [[maybe_unused]] uint8_t setSelector,
1832 [[maybe_unused]] uint8_t blockSelector)
Adriana Kobylak40814c62015-10-27 15:58:44 -05001833{
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001834 ipmi::Cc rc;
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001835 if (reserved1)
1836 {
1837 return ipmi::responseInvalidFieldRequest();
1838 }
1839
1840 constexpr uint4_t version = 0x01;
1841 ipmi::message::Payload response;
1842 response.pack(version, uint4_t{});
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001843 using namespace boot_options;
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001844
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001845 IpmiValue bootOption = ipmiDefault;
Adriana Kobylak40814c62015-10-27 15:58:44 -05001846
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001847 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1848 BootOptionParameter::setInProgress)
huangheab369282020-10-10 14:40:00 +08001849 {
1850 response.pack(bootOptionParameter, reserved1, transferStatus);
1851 return ipmi::responseSuccess(std::move(response));
1852 }
1853
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001854 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1855 BootOptionParameter::bootInfo)
John Wang0213f902020-12-28 14:49:57 +08001856 {
1857 constexpr uint8_t writeMask = 0;
Hieu Huynh38ffafc2021-05-14 08:40:14 +00001858 response.pack(bootOptionParameter, reserved1, writeMask,
1859 bootInitiatorAckData);
John Wang0213f902020-12-28 14:49:57 +08001860 return ipmi::responseSuccess(std::move(response));
1861 }
1862
Chen Yugang86ac4992021-06-25 08:14:52 +08001863 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1864 BootOptionParameter::bootFlagValidClr)
1865 {
1866 response.pack(bootOptionParameter, reserved1,
1867 uint5_t{bootFlagValidBitClr}, uint3_t{});
1868 return ipmi::responseSuccess(std::move(response));
1869 }
1870
shgoupfd84fbbf2015-12-17 10:05:51 +08001871 /*
1872 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
1873 * This is the only parameter used by petitboot.
1874 */
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001875 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1876 BootOptionParameter::bootFlags)
Patrick Venture0b02be92018-08-31 11:55:55 -07001877 {
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001878 using namespace chassis::internal;
1879 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08001880
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001881 try
ratagupta6f6bff2016-04-04 06:20:11 -05001882 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001883 Source::Sources bootSource;
1884 rc = getBootSource(ctx, bootSource);
1885 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001886 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001887 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001888 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001889
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001890 Type::Types bootType;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001891 rc = getBootType(ctx, bootType);
1892 if (rc != ipmi::ccSuccess)
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001893 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001894 return ipmi::response(rc);
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001895 }
1896
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001897 Mode::Modes bootMode;
1898 rc = getBootMode(ctx, bootMode);
1899 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001900 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001901 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001902 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001903
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001904 bootOption = sourceDbusToIpmi.at(bootSource);
1905 if ((Mode::Modes::Regular == bootMode) &&
1906 (Source::Sources::Default == bootSource))
1907 {
1908 bootOption = ipmiDefault;
1909 }
1910 else if (Source::Sources::Default == bootSource)
1911 {
1912 bootOption = modeDbusToIpmi.at(bootMode);
1913 }
ratagupta6f6bff2016-04-04 06:20:11 -05001914
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001915 IpmiValue biosBootType = typeDbusToIpmi.at(bootType);
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001916
1917 bool oneTimeEnabled;
1918 rc = getBootOneTime(ctx, oneTimeEnabled);
1919 if (rc != ipmi::ccSuccess)
1920 {
1921 return ipmi::response(rc);
1922 }
1923
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001924 uint1_t permanent = oneTimeEnabled ? 0 : 1;
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03001925
1926 bool valid;
1927 rc = getBootEnable(ctx, valid);
1928 if (rc != ipmi::ccSuccess)
1929 {
1930 return ipmi::response(rc);
1931 }
1932
1933 uint1_t validFlag = valid ? 1 : 0;
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03001934
1935 response.pack(bootOptionParameter, reserved1, uint5_t{},
1936 uint1_t{biosBootType}, uint1_t{permanent},
1937 uint1_t{validFlag}, uint2_t{}, uint4_t{bootOption},
Hieu Huynh1982a3f2021-09-21 03:06:45 +00001938 uint1_t{}, cmosClear, uint8_t{}, uint8_t{},
1939 uint8_t{});
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001940 return ipmi::responseSuccess(std::move(response));
ratagupta6f6bff2016-04-04 06:20:11 -05001941 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001942 catch (const InternalFailure& e)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001943 {
James Feist225dec82019-11-26 16:25:06 -08001944 cache::objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001945 report<InternalFailure>();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001946 return ipmi::responseUnspecifiedError();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05001947 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001948 }
Patrick Venture0b02be92018-08-31 11:55:55 -07001949 else
1950 {
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001951 if ((bootOptionParameter >= oemParmStart) &&
1952 (bootOptionParameter <= oemParmEnd))
1953 {
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001954 if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
1955 BootOptionParameter::opalNetworkSettings)
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001956 {
1957 response.pack(bootOptionParameter, reserved1);
1958 int ret = getHostNetworkData(response);
1959 if (ret < 0)
1960 {
1961 response.trailingOk = true;
Vernon Mauery38108d92024-03-11 12:53:55 -07001962 lg2::error(
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001963 "getHostNetworkData failed for GetSysBootOptions.");
1964 return ipmi::responseUnspecifiedError();
1965 }
1966 else
1967 {
1968 return ipmi::responseSuccess(std::move(response));
1969 }
1970 }
Jayaprakash Mutyala43a88102021-08-04 16:41:58 +00001971 else
1972 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001973 lg2::error(
1974 "ipmiChassisGetSysBootOptions: Unsupported parameter {PARAM}",
1975 "PARAM", lg2::hex,
1976 static_cast<uint8_t>(bootOptionParameter));
Jayaprakash Mutyala43a88102021-08-04 16:41:58 +00001977 return ipmi::responseParmNotSupported();
1978 }
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001979 }
1980 else
1981 {
Vernon Mauery38108d92024-03-11 12:53:55 -07001982 lg2::error(
1983 "ipmiChassisGetSysBootOptions: Unsupported parameter {PARAM}",
1984 "PARAM", lg2::hex, static_cast<uint8_t>(bootOptionParameter));
Jayaprakash Mutyala6088e9f2021-07-14 07:40:29 +00001985 return ipmi::responseParmNotSupported();
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001986 }
shgoupfd84fbbf2015-12-17 10:05:51 +08001987 }
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00001988 return ipmi::responseUnspecifiedError();
shgoupfd84fbbf2015-12-17 10:05:51 +08001989}
1990
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001991ipmi::RspType<> ipmiChassisSetSysBootOptions(ipmi::Context::ptr ctx,
Willy Tu11d68892022-01-20 10:37:34 -08001992 uint7_t parameterSelector, bool,
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001993 ipmi::message::Payload& data)
shgoupfd84fbbf2015-12-17 10:05:51 +08001994{
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05001995 using namespace boot_options;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00001996 ipmi::Cc rc;
shgoupfd84fbbf2015-12-17 10:05:51 +08001997
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07001998 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
1999 BootOptionParameter::setInProgress)
huangheab369282020-10-10 14:40:00 +08002000 {
2001 uint2_t setInProgressFlag;
2002 uint6_t rsvd;
2003 if (data.unpack(setInProgressFlag, rsvd) != 0 || !data.fullyUnpacked())
2004 {
2005 return ipmi::responseReqDataLenInvalid();
2006 }
2007 if (rsvd)
2008 {
2009 return ipmi::responseInvalidFieldRequest();
2010 }
2011 if ((transferStatus == setInProgress) &&
2012 (static_cast<uint8_t>(setInProgressFlag) != setComplete))
2013 {
2014 return ipmi::response(IPMI_CC_FAIL_SET_IN_PROGRESS);
2015 }
2016 transferStatus = static_cast<uint8_t>(setInProgressFlag);
2017 return ipmi::responseSuccess();
2018 }
2019
shgoupfd84fbbf2015-12-17 10:05:51 +08002020 /* 000101
2021 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
2022 * This is the only parameter used by petitboot.
2023 */
Ratan Guptafd28dd72016-08-01 04:58:01 -05002024
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002025 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2026 BootOptionParameter::bootFlags)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002027 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002028 uint5_t rsvd;
2029 bool validFlag;
2030 bool permanent;
2031 bool biosBootType;
2032 bool lockOutResetButton;
2033 bool screenBlank;
2034 uint4_t bootDeviceSelector;
2035 bool lockKeyboard;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002036 uint8_t data3;
2037 uint4_t biosInfo;
2038 uint4_t rsvd1;
2039 uint5_t deviceInstance;
2040 uint3_t rsvd2;
2041
2042 if (data.unpack(rsvd, biosBootType, permanent, validFlag,
2043 lockOutResetButton, screenBlank, bootDeviceSelector,
2044 lockKeyboard, cmosClear, data3, biosInfo, rsvd1,
2045 deviceInstance, rsvd2) != 0 ||
2046 !data.fullyUnpacked())
2047 {
2048 return ipmi::responseReqDataLenInvalid();
2049 }
2050 if (rsvd || rsvd1 || rsvd2)
2051 {
2052 return ipmi::responseInvalidFieldRequest();
2053 }
2054
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002055 using namespace chassis::internal;
2056 using namespace chassis::internal::cache;
shgoupfd84fbbf2015-12-17 10:05:51 +08002057
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002058 try
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002059 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002060 rc = setBootOneTime(ctx, !permanent);
2061 if (rc != ipmi::ccSuccess)
Konstantin Aladyshev69e8e842021-06-18 17:01:18 +03002062 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002063 return ipmi::response(rc);
2064 }
Vernon Maueryafd45db2021-07-29 16:27:25 +00002065
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002066 rc = setBootEnable(ctx, validFlag);
2067 if (rc != ipmi::ccSuccess)
2068 {
2069 return ipmi::response(rc);
Tom Joseph57e8eb72017-09-25 18:05:02 +05302070 }
2071
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002072 auto modeItr =
2073 modeIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002074 auto typeItr =
2075 typeIpmiToDbus.find(static_cast<uint8_t>(biosBootType));
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002076 auto sourceItr =
2077 sourceIpmiToDbus.find(static_cast<uint8_t>(bootDeviceSelector));
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002078 if (sourceIpmiToDbus.end() != sourceItr)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002079 {
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03002080 rc = setBootSource(ctx, sourceItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002081 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002082 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002083 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002084 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002085 // If a set boot device is mapping to a boot source, then reset
2086 // the boot mode D-Bus property to default.
2087 // This way the ipmid code can determine which property is not
2088 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07002089 if (sourceItr->second != Source::Sources::Default)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002090 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002091 rc = setBootMode(ctx, Mode::Modes::Regular);
2092 if (rc != ipmi::ccSuccess)
2093 {
2094 return ipmi::response(rc);
2095 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002096 }
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002097 }
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002098
2099 if (typeIpmiToDbus.end() != typeItr)
2100 {
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03002101 rc = setBootType(ctx, typeItr->second);
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002102 if (rc != ipmi::ccSuccess)
2103 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002104 return ipmi::response(rc);
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002105 }
2106 }
2107
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002108 if (modeIpmiToDbus.end() != modeItr)
2109 {
Konstantin Aladysheve76a61a2021-03-18 22:19:13 +03002110 rc = setBootMode(ctx, modeItr->second);
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002111 if (rc != ipmi::ccSuccess)
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002112 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002113 return ipmi::response(rc);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002114 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002115 // If a set boot device is mapping to a boot mode, then reset
2116 // the boot source D-Bus property to default.
2117 // This way the ipmid code can determine which property is not
2118 // at the default value
Patrick Venture0b02be92018-08-31 11:55:55 -07002119 if (modeItr->second != Mode::Modes::Regular)
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002120 {
Konstantin Aladyshev08a080a2021-06-18 17:01:18 +03002121 rc = setBootSource(ctx, Source::Sources::Default);
2122 if (rc != ipmi::ccSuccess)
2123 {
2124 return ipmi::response(rc);
2125 }
Marri Devender Rao54fa1302018-05-07 01:06:23 -05002126 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002127 }
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08002128 if ((modeIpmiToDbus.end() == modeItr) &&
Konstantin Aladyshev96ef0282021-02-09 13:57:10 +03002129 (typeIpmiToDbus.end() == typeItr) &&
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08002130 (sourceIpmiToDbus.end() == sourceItr))
2131 {
2132 // return error if boot option is not supported
Vernon Mauery38108d92024-03-11 12:53:55 -07002133 lg2::error(
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002134 "ipmiChassisSetSysBootOptions: Boot option not supported");
2135 return ipmi::responseInvalidFieldRequest();
Jia, chunhui67c5e5d2019-05-06 11:29:54 +08002136 }
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002137 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05002138 catch (const sdbusplus::exception_t& e)
Deepak Kodihalli8cc19362017-07-21 11:18:38 -05002139 {
James Feist225dec82019-11-26 16:25:06 -08002140 objectsPtr.reset();
Deepak Kodihalli13791bd2017-08-28 06:50:51 -05002141 report<InternalFailure>();
Vernon Mauery38108d92024-03-11 12:53:55 -07002142 lg2::error("ipmiChassisSetSysBootOptions: Error in setting Boot "
2143 "flag parameters");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002144 return ipmi::responseUnspecifiedError();
Ratan Guptafd28dd72016-08-01 04:58:01 -05002145 }
Patrick Venture0b02be92018-08-31 11:55:55 -07002146 }
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002147 else if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2148 BootOptionParameter::bootInfo)
Patrick Venture0b02be92018-08-31 11:55:55 -07002149 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002150 uint8_t writeMak;
Hieu Huynh38ffafc2021-05-14 08:40:14 +00002151 uint5_t bootInfoAck;
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002152 uint3_t rsvd;
2153
Hieu Huynh38ffafc2021-05-14 08:40:14 +00002154 if (data.unpack(writeMak, bootInfoAck, rsvd) != 0 ||
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002155 !data.fullyUnpacked())
2156 {
2157 return ipmi::responseReqDataLenInvalid();
2158 }
2159 if (rsvd)
2160 {
2161 return ipmi::responseInvalidFieldRequest();
2162 }
Hieu Huynh38ffafc2021-05-14 08:40:14 +00002163 bootInitiatorAckData &= ~writeMak;
2164 bootInitiatorAckData |= (writeMak & bootInfoAck);
Vernon Mauery38108d92024-03-11 12:53:55 -07002165 lg2::info("ipmiChassisSetSysBootOptions: bootInfo parameter set "
2166 "successfully");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002167 data.trailingOk = true;
2168 return ipmi::responseSuccess();
Patrick Venture0b02be92018-08-31 11:55:55 -07002169 }
Chen Yugang86ac4992021-06-25 08:14:52 +08002170 else if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2171 BootOptionParameter::bootFlagValidClr)
2172 {
2173 uint5_t bootFlagValidClr;
2174 uint3_t rsvd;
2175
2176 if (data.unpack(bootFlagValidClr, rsvd) != 0 || !data.fullyUnpacked())
2177 {
2178 return ipmi::responseReqDataLenInvalid();
2179 }
2180 if (rsvd)
2181 {
2182 return ipmi::responseInvalidFieldRequest();
2183 }
2184 // store boot flag valid bits clear value
2185 bootFlagValidBitClr = static_cast<uint8_t>(bootFlagValidClr);
Vernon Mauery38108d92024-03-11 12:53:55 -07002186 lg2::info(
Chen Yugang86ac4992021-06-25 08:14:52 +08002187 "ipmiChassisSetSysBootOptions: bootFlagValidBits parameter set "
Vernon Mauery38108d92024-03-11 12:53:55 -07002188 "successfully to {VALUE}",
2189 "VALUE", lg2::hex, bootFlagValidBitClr);
Chen Yugang86ac4992021-06-25 08:14:52 +08002190 return ipmi::responseSuccess();
2191 }
Patrick Venture0b02be92018-08-31 11:55:55 -07002192 else
2193 {
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002194 if ((parameterSelector >= static_cast<uint7_t>(oemParmStart)) &&
2195 (parameterSelector <= static_cast<uint7_t>(oemParmEnd)))
2196 {
William A. Kennington III7a0e5df2021-05-19 13:31:29 -07002197 if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
2198 BootOptionParameter::opalNetworkSettings)
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002199 {
2200 ipmi::Cc ret = setHostNetworkData(data);
2201 if (ret != ipmi::ccSuccess)
2202 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002203 lg2::error("ipmiChassisSetSysBootOptions: Error in "
2204 "setHostNetworkData");
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002205 data.trailingOk = true;
2206 return ipmi::response(ret);
2207 }
2208 data.trailingOk = true;
2209 return ipmi::responseSuccess();
2210 }
2211 else
2212 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002213 lg2::error(
2214 "ipmiChassisSetSysBootOptions: Unsupported param: {PARAM}",
2215 "PARAM", lg2::hex, static_cast<uint8_t>(parameterSelector));
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002216 data.trailingOk = true;
2217 return ipmi::responseParmNotSupported();
2218 }
2219 }
2220 data.trailingOk = true;
2221 return ipmi::responseParmNotSupported();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002222 }
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002223 return ipmi::responseSuccess();
Adriana Kobylak40814c62015-10-27 15:58:44 -05002224}
2225
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002226/** @brief implements Get POH counter command
2227 * @parameter
2228 * - none
2229 * @returns IPMI completion code plus response data
2230 * - minPerCount - Minutes per count
2231 * - counterReading - counter reading
2232 */
2233ipmi::RspType<uint8_t, // Minutes per count
2234 uint32_t // Counter reading
2235 >
2236 ipmiGetPOHCounter()
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002237{
2238 // sd_bus error
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002239 try
2240 {
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002241 return ipmi::responseSuccess(static_cast<uint8_t>(poh::minutesPerCount),
2242 getPOHCounter());
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002243 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05002244 catch (const std::exception& e)
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002245 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002246 lg2::error("getPOHCounter error: {ERROR}", "ERROR", e);
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002247 return ipmi::responseUnspecifiedError();
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002248 }
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002249}
2250
Jason M. Billsbc996a32019-06-17 15:46:37 -07002251ipmi::RspType<uint3_t, // policy support
2252 uint5_t // reserved
2253 >
Vernon Mauerye278ead2018-10-09 09:23:43 -07002254 ipmiChassisSetPowerRestorePolicy(boost::asio::yield_context yield,
Jason M. Billsbc996a32019-06-17 15:46:37 -07002255 uint3_t policy, uint5_t reserved)
Yong Lic6713cf2018-09-12 12:35:13 +08002256{
Yong Lic6713cf2018-09-12 12:35:13 +08002257 power_policy::DbusValue value =
2258 power_policy::RestorePolicy::Policy::AlwaysOff;
2259
Jason M. Billsbc996a32019-06-17 15:46:37 -07002260 if (reserved || (policy > power_policy::noChange))
Yong Lic6713cf2018-09-12 12:35:13 +08002261 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002262 lg2::error("Reserved request parameter: {REQ}", "REQ", lg2::hex,
2263 static_cast<int>(policy));
Jason M. Billsbc996a32019-06-17 15:46:37 -07002264 return ipmi::responseInvalidFieldRequest();
Yong Lic6713cf2018-09-12 12:35:13 +08002265 }
2266
Vernon Mauerye278ead2018-10-09 09:23:43 -07002267 if (policy == power_policy::noChange)
Yong Lic6713cf2018-09-12 12:35:13 +08002268 {
2269 // just return the supported policy
Jason M. Billsbc996a32019-06-17 15:46:37 -07002270 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002271 }
2272
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05002273 for (const auto& it : power_policy::dbusToIpmi)
Yong Lic6713cf2018-09-12 12:35:13 +08002274 {
Vernon Mauerye278ead2018-10-09 09:23:43 -07002275 if (it.second == policy)
Yong Lic6713cf2018-09-12 12:35:13 +08002276 {
2277 value = it.first;
2278 break;
2279 }
2280 }
2281
2282 try
2283 {
James Feist225dec82019-11-26 16:25:06 -08002284 settings::Objects& objects = chassis::internal::cache::getObjects();
Yong Lic6713cf2018-09-12 12:35:13 +08002285 const settings::Path& powerRestoreSetting =
James Feist225dec82019-11-26 16:25:06 -08002286 objects.map.at(chassis::internal::powerRestoreIntf).front();
Vernon Mauery16b86932019-05-01 08:36:11 -07002287 std::variant<std::string> property = convertForMessage(value);
Yong Lic6713cf2018-09-12 12:35:13 +08002288
Vernon Mauerye278ead2018-10-09 09:23:43 -07002289 auto sdbusp = getSdBus();
2290 boost::system::error_code ec;
2291 sdbusp->yield_method_call<void>(
2292 yield, ec,
James Feist225dec82019-11-26 16:25:06 -08002293 objects
Yong Lic6713cf2018-09-12 12:35:13 +08002294 .service(powerRestoreSetting,
2295 chassis::internal::powerRestoreIntf)
2296 .c_str(),
Vernon Mauerye278ead2018-10-09 09:23:43 -07002297 powerRestoreSetting, ipmi::PROP_INTF, "Set",
2298 chassis::internal::powerRestoreIntf, "PowerRestorePolicy",
2299 property);
2300 if (ec)
Yong Lic6713cf2018-09-12 12:35:13 +08002301 {
Vernon Mauery38108d92024-03-11 12:53:55 -07002302 lg2::error("Unspecified Error");
Vernon Mauerye278ead2018-10-09 09:23:43 -07002303 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002304 }
2305 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05002306 catch (const InternalFailure& e)
Yong Lic6713cf2018-09-12 12:35:13 +08002307 {
James Feist225dec82019-11-26 16:25:06 -08002308 chassis::internal::cache::objectsPtr.reset();
Yong Lic6713cf2018-09-12 12:35:13 +08002309 report<InternalFailure>();
Vernon Mauerye278ead2018-10-09 09:23:43 -07002310 return ipmi::responseUnspecifiedError();
Yong Lic6713cf2018-09-12 12:35:13 +08002311 }
2312
Jason M. Billsbc996a32019-06-17 15:46:37 -07002313 return ipmi::responseSuccess(power_policy::allSupport, reserved);
Yong Lic6713cf2018-09-12 12:35:13 +08002314}
2315
Patrick Williams1318a5e2024-08-16 15:19:54 -04002316ipmi::RspType<> ipmiSetFrontPanelButtonEnables(
2317 ipmi::Context::ptr ctx, bool disablePowerButton, bool disableResetButton,
2318 bool, bool, uint4_t)
Kuiying Wang21addc52019-01-04 10:50:21 +08002319{
2320 using namespace chassis::internal;
2321
2322 // set power button Enabled property
2323 bool success = setButtonEnabled(ctx, powerButtonPath, powerButtonIntf,
2324 !disablePowerButton);
2325
2326 // set reset button Enabled property
2327 success &= setButtonEnabled(ctx, resetButtonPath, resetButtonIntf,
2328 !disableResetButton);
2329
2330 if (!success)
2331 {
2332 // not all buttons were successfully set
2333 return ipmi::responseUnspecifiedError();
2334 }
2335 return ipmi::responseSuccess();
2336}
2337
Adriana Kobylak40814c62015-10-27 15:58:44 -05002338void register_netfn_chassis_functions()
2339{
Marri Devender Rao6706c1c2018-05-14 00:29:38 -05002340 createIdentifyTimer();
2341
Tom05732372016-09-06 17:21:23 +05302342 // Get Chassis Capabilities
anil kumar appana43263c62019-05-27 12:45:04 +00002343 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2344 ipmi::chassis::cmdGetChassisCapabilities,
2345 ipmi::Privilege::User, ipmiGetChassisCap);
Nan Li8d15fb42016-08-16 22:29:40 +08002346
Kuiying Wang21addc52019-01-04 10:50:21 +08002347 // Set Front Panel Button Enables
2348 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2349 ipmi::chassis::cmdSetFrontPanelButtonEnables,
2350 ipmi::Privilege::Admin,
2351 ipmiSetFrontPanelButtonEnables);
2352
Yong Liae4b0402018-11-02 11:12:14 +08002353 // Set Chassis Capabilities
anil kumar appana894d0222019-05-27 16:32:14 +00002354 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2355 ipmi::chassis::cmdSetChassisCapabilities,
2356 ipmi::Privilege::User, ipmiSetChassisCap);
Yong Liae4b0402018-11-02 11:12:14 +08002357
Tom05732372016-09-06 17:21:23 +05302358 // <Get System Boot Options>
Jayaprakash Mutyalad1ef8772020-08-25 10:32:58 +00002359 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2360 ipmi::chassis::cmdGetSystemBootOptions,
2361 ipmi::Privilege::Operator,
2362 ipmiChassisGetSysBootOptions);
Adriana Kobylak40814c62015-10-27 15:58:44 -05002363
Tom05732372016-09-06 17:21:23 +05302364 // <Get Chassis Status>
Vernon Mauery4a8a4eb2019-04-04 15:09:37 -07002365 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2366 ipmi::chassis::cmdGetChassisStatus,
2367 ipmi::Privilege::User, ipmiGetChassisStatus);
Nan Lifdd8ec52016-08-28 03:57:40 +08002368
Vijay Khemka074f64d2020-03-03 15:08:43 -08002369 // <Chassis Get System Restart Cause>
2370 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2371 ipmi::chassis::cmdGetSystemRestartCause,
2372 ipmi::Privilege::User, ipmiGetSystemRestartCause);
2373
Tom05732372016-09-06 17:21:23 +05302374 // <Chassis Control>
anil kumar appanadafff5f2019-04-27 18:06:00 +00002375 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2376 ipmi::chassis::cmdChassisControl,
2377 ipmi::Privilege::Operator, ipmiChassisControl);
shgoupfd84fbbf2015-12-17 10:05:51 +08002378
Tom Joseph5110c122018-03-23 17:55:40 +05302379 // <Chassis Identify>
Vernon Mauery400cc782018-10-09 13:49:53 -07002380 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2381 ipmi::chassis::cmdChassisIdentify,
2382 ipmi::Privilege::Operator, ipmiChassisIdentify);
Tom Joseph5110c122018-03-23 17:55:40 +05302383
Tom05732372016-09-06 17:21:23 +05302384 // <Set System Boot Options>
jayaprakash Mutyalabfd8fc42020-05-05 22:38:03 +00002385 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2386 ipmi::chassis::cmdSetSystemBootOptions,
2387 ipmi::Privilege::Operator,
2388 ipmiChassisSetSysBootOptions);
2389
Nagaraju Gorugantia59d83f2018-04-06 05:55:42 -05002390 // <Get POH Counter>
anil kumar appanaa5a76eb2019-04-30 14:57:24 +00002391 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2392 ipmi::chassis::cmdGetPohCounter,
2393 ipmi::Privilege::User, ipmiGetPOHCounter);
Yong Lic6713cf2018-09-12 12:35:13 +08002394
2395 // <Set Power Restore Policy>
Vernon Mauerye278ead2018-10-09 09:23:43 -07002396 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnChassis,
2397 ipmi::chassis::cmdSetPowerRestorePolicy,
2398 ipmi::Privilege::Operator,
2399 ipmiChassisSetPowerRestorePolicy);
vishwa36993272015-11-20 12:43:49 -06002400}